From 191ce517da245a81857102115cec0dc4a4b2b67d Mon Sep 17 00:00:00 2001 From: David Rose Date: Sat, 17 Sep 2011 00:05:23 +0000 Subject: [PATCH] migrate a few use cases to writable vfs --- panda/src/downloader/httpChannel.cxx | 61 +++++++++++++------- panda/src/downloader/httpChannel.h | 2 +- panda/src/egg/eggData.cxx | 27 +++------ panda/src/express/compress_string.cxx | 26 ++++++--- panda/src/express/encrypt_string.cxx | 54 +++++++---------- panda/src/express/virtualFileMount.cxx | 2 + panda/src/express/virtualFileMountSystem.cxx | 29 +--------- 7 files changed, 92 insertions(+), 109 deletions(-) diff --git a/panda/src/downloader/httpChannel.cxx b/panda/src/downloader/httpChannel.cxx index fdd37664f6..237aeca414 100644 --- a/panda/src/downloader/httpChannel.cxx +++ b/panda/src/downloader/httpChannel.cxx @@ -19,6 +19,7 @@ #include "chunkedStream.h" #include "identityStream.h" #include "config_downloader.h" +#include "virtualFileSystem.h" #include "virtualFileMountHTTP.h" #include "ramfile.h" #include "globPattern.h" @@ -112,6 +113,7 @@ HTTPChannel(HTTPClient *client) : _last_status_code = 0; _last_run_time = 0.0f; _download_to_ramfile = NULL; + _download_to_stream = NULL; } //////////////////////////////////////////////////////////////////// @@ -667,8 +669,6 @@ download_to_file(const Filename &filename, bool subdocument_resumes) { reset_download_to(); _download_to_filename = filename; _download_to_filename.set_binary(); - _download_to_file.close(); - _download_to_file.clear(); _subdocument_resumes = subdocument_resumes; _download_dest = DD_file; @@ -2361,7 +2361,7 @@ run_download_to_file() { _body_stream->read(buffer, min(buffer_size, remaining_this_pass)); size_t count = _body_stream->gcount(); while (count != 0) { - _download_to_file.write(buffer, count); + _download_to_stream->write(buffer, count); _bytes_downloaded += count; if (do_throttle) { nassertr(count <= remaining_this_pass, false); @@ -2377,7 +2377,7 @@ run_download_to_file() { count = _body_stream->gcount(); } - if (_download_to_file.fail()) { + if (_download_to_stream->fail()) { downloader_cat.warning() << _NOTIFY_HTTP_CHANNEL_ID << "Error writing to " << _download_to_filename << "\n"; @@ -2387,12 +2387,12 @@ run_download_to_file() { return false; } - _download_to_file.flush(); + _download_to_stream->flush(); if (_body_stream->is_closed()) { // Done. reset_body_stream(); - _download_to_file.close(); + close_download_stream(); _started_download = false; return false; } else { @@ -2444,6 +2444,7 @@ run_download_to_ram() { if (_body_stream->is_closed()) { // Done. reset_body_stream(); + close_download_stream(); _started_download = false; return false; } else { @@ -2506,7 +2507,7 @@ run_download_to_stream() { if (_body_stream->is_closed()) { // Done. reset_body_stream(); - _download_to_stream = NULL; + close_download_stream(); _started_download = false; return false; } else { @@ -2796,7 +2797,9 @@ open_download_file() { _subdocument_resumes = (_subdocument_resumes && _first_byte_delivered != 0); if (_download_dest == DD_file) { - if (!_download_to_filename.open_write(_download_to_file, !_subdocument_resumes)) { + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + _download_to_stream = vfs->open_write_file(_download_to_filename, false, !_subdocument_resumes); + if (_download_to_stream == NULL) { downloader_cat.info() << _NOTIFY_HTTP_CHANNEL_ID << "Could not open " << _download_to_filename << " for writing.\n"; @@ -2812,20 +2815,20 @@ open_download_file() { // file--it happily appends enough zero bytes to make the // difference. Blecch. That means we need to get the file size // first to check it ourselves. - _download_to_file.seekp(0, ios::end); - if (_first_byte_delivered > (size_t)_download_to_file.tellp()) { + _download_to_stream->seekp(0, ios::end); + if (_first_byte_delivered > (size_t)_download_to_stream->tellp()) { downloader_cat.info() << _NOTIFY_HTTP_CHANNEL_ID << "Invalid starting position of byte " << _first_byte_delivered << " within " << _download_to_filename << " (which has " - << _download_to_file.tellp() << " bytes)\n"; - _download_to_file.close(); + << _download_to_stream->tellp() << " bytes)\n"; + close_download_stream(); _status_entry._status_code = SC_download_invalid_range; _state = S_failure; return false; } - _download_to_file.seekp(_first_byte_delivered); + _download_to_stream->seekp(_first_byte_delivered); } else if (_download_dest == DD_ram) { if (_first_byte_delivered > _download_to_ramfile->_data.length()) { @@ -2834,6 +2837,7 @@ open_download_file() { << "Invalid starting position of byte " << _first_byte_delivered << " within Ramfile (which has " << _download_to_ramfile->_data.length() << " bytes)\n"; + close_download_stream(); _status_entry._status_code = SC_download_invalid_range; _state = S_failure; return false; @@ -2857,7 +2861,7 @@ open_download_file() { << "Invalid starting position of byte " << _first_byte_delivered << " within stream (which has " << _download_to_stream->tellp() << " bytes)\n"; - _download_to_stream = NULL; + close_download_stream(); _status_entry._status_code = SC_download_invalid_range; _state = S_failure; return false; @@ -2870,12 +2874,10 @@ open_download_file() { // If _subdocument_resumes is false, we should be sure to reset to // the beginning of the file, regardless of the value of // _first_byte_delivered. - if (_download_dest == DD_file) { - _download_to_file.seekp(0); + if (_download_dest == DD_file || _download_dest == DD_stream) { + _download_to_stream->seekp(0); } else if (_download_dest == DD_ram) { _download_to_ramfile->_data = string(); - } else if (_download_dest == DD_stream) { - _download_to_stream->seekp(0); } } @@ -3958,12 +3960,29 @@ show_send(const string &message) { void HTTPChannel:: reset_download_to() { _started_download = false; - _download_to_file.close(); - _download_to_ramfile = (Ramfile *)NULL; - _download_to_stream = NULL; + close_download_stream(); _download_dest = DD_none; } +//////////////////////////////////////////////////////////////////// +// Function: HTTPChannel::close_download_stream +// Access: Private +// Description: Ensures the file opened for receiving the download +// has been correctly closed. +//////////////////////////////////////////////////////////////////// +void HTTPChannel:: +close_download_stream() { + if (_download_to_stream != NULL) { + _download_to_stream->flush(); + if (_download_dest == DD_file) { + VirtualFileSystem::close_write_file(_download_to_stream); + } + } + _download_to_ramfile = (Ramfile *)NULL; + _download_to_stream = NULL; +} + + //////////////////////////////////////////////////////////////////// // Function: HTTPChannel::reset_to_new // Access: Private diff --git a/panda/src/downloader/httpChannel.h b/panda/src/downloader/httpChannel.h index 5b5abb66ba..51e69c1ba6 100644 --- a/panda/src/downloader/httpChannel.h +++ b/panda/src/downloader/httpChannel.h @@ -267,6 +267,7 @@ private: #endif void reset_download_to(); + void close_download_stream(); void reset_to_new(); void reset_body_stream(); void close_connection(); @@ -361,7 +362,6 @@ private: DownloadDest _download_dest; bool _subdocument_resumes; Filename _download_to_filename; - pofstream _download_to_file; Ramfile *_download_to_ramfile; ostream *_download_to_stream; diff --git a/panda/src/egg/eggData.cxx b/panda/src/egg/eggData.cxx index 86c2edd9d2..2bbcdc6a29 100644 --- a/panda/src/egg/eggData.cxx +++ b/panda/src/egg/eggData.cxx @@ -237,30 +237,17 @@ write_egg(Filename filename) { filename.unlink(); filename.set_text(); -#ifdef HAVE_ZLIB - bool pz_file = false; - if (filename.get_extension() == "pz") { - // The filename ends in .pz, which means to automatically compress - // the egg file that we write. - pz_file = true; - filename.set_binary(); - } -#endif // HAVE_ZLIB - - pofstream file; - if (!filename.open_write(file)) { + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + + ostream *file = vfs->open_write_file(filename, true, true); + if (file == (ostream *)NULL) { egg_cat.error() << "Unable to open " << filename << " for writing.\n"; return false; } -#ifdef HAVE_ZLIB - if (pz_file) { - OCompressStream compressor(&file, false); - return write_egg(compressor); - } -#endif // HAVE_ZLIB - - return write_egg(file); + bool wrote_ok = write_egg(*file); + vfs->close_write_file(file); + return wrote_ok; } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/express/compress_string.cxx b/panda/src/express/compress_string.cxx index 4fe5a2f34c..32371958e9 100644 --- a/panda/src/express/compress_string.cxx +++ b/panda/src/express/compress_string.cxx @@ -77,8 +77,12 @@ decompress_string(const string &source) { //////////////////////////////////////////////////////////////////// EXPCL_PANDAEXPRESS bool compress_file(const Filename &source, const Filename &dest, int compression_level) { - Filename source_filename = Filename::binary_filename(source); VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); + Filename source_filename = source; + if (!source_filename.is_binary_or_text()) { + // The default is binary, if not specified otherwise. + source_filename.set_binary(); + } istream *source_stream = vfs->open_read_file(source_filename, true); if (source_stream == NULL) { express_cat.info() << "Couldn't open file " << source_filename << "\n"; @@ -86,15 +90,16 @@ compress_file(const Filename &source, const Filename &dest, int compression_leve } Filename dest_filename = Filename::binary_filename(dest); - pofstream dest_stream; - if (!dest_filename.open_write(dest_stream)) { + ostream *dest_stream = vfs->open_write_file(dest_filename, true, true); + if (dest_stream == NULL) { express_cat.info() << "Couldn't open file " << dest_filename << "\n"; vfs->close_read_file(source_stream); return false; } - bool result = compress_stream(*source_stream, dest_stream, compression_level); + bool result = compress_stream(*source_stream, *dest_stream, compression_level); vfs->close_read_file(source_stream); + vfs->close_write_file(dest_stream); return result; } @@ -121,16 +126,21 @@ decompress_file(const Filename &source, const Filename &dest) { return false; } - Filename dest_filename = Filename::binary_filename(dest); - pofstream dest_stream; - if (!dest_filename.open_write(dest_stream)) { + Filename dest_filename = dest; + if (!dest_filename.is_binary_or_text()) { + // The default is binary, if not specified otherwise. + dest_filename.set_binary(); + } + ostream *dest_stream = vfs->open_write_file(dest_filename, true, true); + if (dest_stream == NULL) { express_cat.info() << "Couldn't open file " << dest_filename << "\n"; vfs->close_read_file(source_stream); return false; } - bool result = decompress_stream(*source_stream, dest_stream); + bool result = decompress_stream(*source_stream, *dest_stream); vfs->close_read_file(source_stream); + vfs->close_write_file(dest_stream); return result; } diff --git a/panda/src/express/encrypt_string.cxx b/panda/src/express/encrypt_string.cxx index 12fb7c2757..10fdd33524 100644 --- a/panda/src/express/encrypt_string.cxx +++ b/panda/src/express/encrypt_string.cxx @@ -89,45 +89,30 @@ decrypt_string(const string &source, const string &password) { EXPCL_PANDAEXPRESS bool encrypt_file(const Filename &source, const Filename &dest, const string &password, const string &algorithm, int key_length, int iteration_count) { + VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); Filename source_filename = source; - if (!source_filename.is_binary() && !source_filename.is_text()) { + if (!source_filename.is_binary_or_text()) { // The default is binary, if not specified otherwise. source_filename.set_binary(); } - - Filename dest_filename = Filename::binary_filename(dest); - pofstream dest_stream; - if (!dest_filename.open_write(dest_stream)) { - express_cat.info() << "Couldn't open file " << dest_filename << "\n"; - return false; - } - - // Try to open the file from disk first, instead of using the vfs, - // so we can get the newline conversion with a text file. This is a - // little weird if you have a vfs file shadowing a disk file, but - // whatever. - if (source_filename.is_text()) { - pifstream source_stream; - if (source_filename.open_read(source_stream)) { - bool result = encrypt_stream(source_stream, dest_stream, password, - algorithm, key_length, iteration_count); - return result; - } - } - - // OK, couldn't read the disk file, or it wasn't set in text mode. - // Read the file from the vfs, and sorry--no text conversion for - // you. - VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); istream *source_stream = vfs->open_read_file(source_filename, true); if (source_stream == NULL) { express_cat.info() << "Couldn't open file " << source_filename << "\n"; return false; } + + Filename dest_filename = Filename::binary_filename(dest); + ostream *dest_stream = vfs->open_write_file(dest_filename, true, true); + if (dest_stream == NULL) { + express_cat.info() << "Couldn't open file " << dest_filename << "\n"; + vfs->close_read_file(source_stream); + return false; + } - bool result = encrypt_stream(*source_stream, dest_stream, password, + bool result = encrypt_stream(*source_stream, *dest_stream, password, algorithm, key_length, iteration_count); vfs->close_read_file(source_stream); + vfs->close_write_file(dest_stream); return result; } @@ -149,22 +134,27 @@ EXPCL_PANDAEXPRESS bool decrypt_file(const Filename &source, const Filename &dest, const string &password) { Filename source_filename = Filename::binary_filename(source); VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); - istream *source_stream = vfs->open_read_file(source_filename, true); + istream *source_stream = vfs->open_read_file(source_filename, false); if (source_stream == NULL) { express_cat.info() << "Couldn't open file " << source_filename << "\n"; return false; } - Filename dest_filename = Filename::binary_filename(dest); - pofstream dest_stream; - if (!dest_filename.open_write(dest_stream)) { + Filename dest_filename = dest; + if (!dest_filename.is_binary_or_text()) { + // The default is binary, if not specified otherwise. + dest_filename.set_binary(); + } + ostream *dest_stream = vfs->open_write_file(dest_filename, true, true); + if (dest_stream == NULL) { express_cat.info() << "Couldn't open file " << dest_filename << "\n"; vfs->close_read_file(source_stream); return false; } - bool result = decrypt_stream(*source_stream, dest_stream, password); + bool result = decrypt_stream(*source_stream, *dest_stream, password); vfs->close_read_file(source_stream); + vfs->close_write_file(dest_stream); return result; } diff --git a/panda/src/express/virtualFileMount.cxx b/panda/src/express/virtualFileMount.cxx index ce84ab68e9..e372389957 100644 --- a/panda/src/express/virtualFileMount.cxx +++ b/panda/src/express/virtualFileMount.cxx @@ -46,6 +46,8 @@ make_virtual_file(const Filename &local_filename, Filename local(local_filename); if (original_filename.is_text()) { local.set_text(); + } else { + local.set_binary(); } PT(VirtualFileSimple) file = new VirtualFileSimple(this, local, implicit_pz_file, open_flags); diff --git a/panda/src/express/virtualFileMountSystem.cxx b/panda/src/express/virtualFileMountSystem.cxx index 9f9bb1eb4e..94150e4932 100644 --- a/panda/src/express/virtualFileMountSystem.cxx +++ b/panda/src/express/virtualFileMountSystem.cxx @@ -153,11 +153,6 @@ open_read_file(const Filename &file) const { } #endif // WIN32 Filename pathname(_physical_filename, file); - if (file.is_text()) { - pathname.set_text(); - } else { - pathname.set_binary(); - } pifstream *stream = new pifstream; if (!pathname.open_read(*stream)) { // Couldn't open the file for some reason. @@ -187,11 +182,6 @@ open_write_file(const Filename &file, bool truncate) { } #endif // WIN32 Filename pathname(_physical_filename, file); - if (file.is_text()) { - pathname.set_text(); - } else { - pathname.set_binary(); - } pofstream *stream = new pofstream; if (!pathname.open_write(*stream, truncate)) { // Couldn't open the file for some reason. @@ -221,11 +211,6 @@ open_append_file(const Filename &file) { } #endif // WIN32 Filename pathname(_physical_filename, file); - if (file.is_text()) { - pathname.set_text(); - } else { - pathname.set_binary(); - } pofstream *stream = new pofstream; if (!pathname.open_append(*stream)) { // Couldn't open the file for some reason. @@ -255,15 +240,10 @@ open_read_write_file(const Filename &file, bool truncate) { } #endif // WIN32 Filename pathname(_physical_filename, file); - if (file.is_text()) { - pathname.set_text(); - } else { - pathname.set_binary(); - } pfstream *stream = new pfstream; if (!pathname.open_read_write(*stream, truncate)) { // Couldn't open the file for some reason. - close_write_file(stream); + close_read_write_file(stream); return NULL; } @@ -289,15 +269,10 @@ open_read_append_file(const Filename &file) { } #endif // WIN32 Filename pathname(_physical_filename, file); - if (file.is_text()) { - pathname.set_text(); - } else { - pathname.set_binary(); - } pfstream *stream = new pfstream; if (!pathname.open_read_append(*stream)) { // Couldn't open the file for some reason. - close_write_file(stream); + close_read_write_file(stream); return NULL; }