Panda3D

virtualFileSimple.cxx

00001 // Filename: virtualFileSimple.cxx
00002 // Created by:  drose (03Aug02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "virtualFileSimple.h"
00016 #include "virtualFileMount.h"
00017 #include "virtualFileList.h"
00018 
00019 TypeHandle VirtualFileSimple::_type_handle;
00020 
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: VirtualFileSimple::get_file_system
00024 //       Access: Published, Virtual
00025 //  Description: Returns the VirtualFileSystem this file is associated
00026 //               with.
00027 ////////////////////////////////////////////////////////////////////
00028 VirtualFileSystem *VirtualFileSimple::
00029 get_file_system() const {
00030   return _mount->get_file_system();
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: VirtualFileSimple::get_filename
00035 //       Access: Published, Virtual
00036 //  Description: Returns the full pathname to this file within the
00037 //               virtual file system.
00038 ////////////////////////////////////////////////////////////////////
00039 Filename VirtualFileSimple::
00040 get_filename() const {
00041   string mount_point = _mount->get_mount_point();
00042   if (_local_filename.empty()) {
00043     if (mount_point.empty()) {
00044       return "/";
00045     } else {
00046       return string("/") + mount_point;
00047     }
00048 
00049   } else {
00050     if (mount_point.empty()) {
00051       return string("/") + _local_filename.get_fullpath();
00052     } else {
00053       return string("/") + mount_point + string("/") + _local_filename.get_fullpath();
00054     }
00055   }
00056 }
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //     Function: VirtualFileSimple::has_file
00060 //       Access: Published, Virtual
00061 //  Description: Returns true if this file exists, false otherwise.
00062 ////////////////////////////////////////////////////////////////////
00063 bool VirtualFileSimple::
00064 has_file() const {
00065   return _mount->has_file(_local_filename);
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: VirtualFileSimple::is_directory
00070 //       Access: Published, Virtual
00071 //  Description: Returns true if this file represents a directory (and
00072 //               scan_directory() may be called), false otherwise.
00073 ////////////////////////////////////////////////////////////////////
00074 bool VirtualFileSimple::
00075 is_directory() const {
00076   return _mount->is_directory(_local_filename);
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: VirtualFileSimple::is_regular_file
00081 //       Access: Published, Virtual
00082 //  Description: Returns true if this file represents a regular file
00083 //               (and read_file() may be called), false otherwise.
00084 ////////////////////////////////////////////////////////////////////
00085 bool VirtualFileSimple::
00086 is_regular_file() const {
00087   return _mount->is_regular_file(_local_filename);
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: VirtualFileSimple::is_writable
00092 //       Access: Published, Virtual
00093 //  Description: Returns true if this file represents a writable
00094 //               regular file (and write_file() may be called), false
00095 //               otherwise.
00096 ////////////////////////////////////////////////////////////////////
00097 bool VirtualFileSimple::
00098 is_writable() const {
00099   return _mount->is_writable(_local_filename);
00100 }
00101 
00102 ////////////////////////////////////////////////////////////////////
00103 //     Function: VirtualFileSimple::delete_file
00104 //       Access: Public
00105 //  Description: Attempts to delete this file or directory.  This can
00106 //               remove a single file or an empty directory.  It will
00107 //               not remove a nonempty directory.  Returns true on
00108 //               success, false on failure.
00109 ////////////////////////////////////////////////////////////////////
00110 bool VirtualFileSimple::
00111 delete_file() {
00112   return _mount->delete_file(_local_filename);
00113 }
00114 
00115 ////////////////////////////////////////////////////////////////////
00116 //     Function: VirtualFileSimple::rename_file
00117 //       Access: Public
00118 //  Description: Attempts to move or rename this file or directory.
00119 //               If the original file is an ordinary file, it will
00120 //               quietly replace any already-existing file in the new
00121 //               filename (but not a directory).  If the original file
00122 //               is a directory, the new filename must not already
00123 //               exist.
00124 //
00125 //               If the file is a directory, the new filename must be
00126 //               within the same mount point.  If the file is an
00127 //               ordinary file, the new filename may be anywhere; but
00128 //               if it is not within the same mount point then the
00129 //               rename operation is automatically performed as a
00130 //               two-step copy-and-delete operation.
00131 ////////////////////////////////////////////////////////////////////
00132 bool VirtualFileSimple::
00133 rename_file(VirtualFile *new_file) {
00134   if (new_file->is_of_type(VirtualFileSimple::get_class_type())) {
00135     VirtualFileSimple *new_file_simple = DCAST(VirtualFileSimple, new_file);
00136     if (new_file_simple->_mount == _mount) {
00137       // Same mount pount.
00138       if (_mount->rename_file(_local_filename, new_file_simple->_local_filename)) {
00139         return true;
00140       }
00141     }
00142   }
00143 
00144   // Different mount point, or the mount doesn't support renaming.  Do
00145   // it by hand.
00146   if (is_regular_file() && !new_file->is_directory()) {
00147     // copy-and-delete.
00148     new_file->delete_file();
00149     if (copy_file(new_file)) {
00150       delete_file();
00151       return true;
00152     }
00153   }
00154 
00155   return false;
00156 }
00157 
00158 ////////////////////////////////////////////////////////////////////
00159 //     Function: VirtualFileSimple::copy_file
00160 //       Access: Public
00161 //  Description: Attempts to copy the contents of this file to the
00162 //               indicated file.  Returns true on success, false on
00163 //               failure.
00164 ////////////////////////////////////////////////////////////////////
00165 bool VirtualFileSimple::
00166 copy_file(VirtualFile *new_file) {
00167   if (new_file->is_of_type(VirtualFileSimple::get_class_type())) {
00168     VirtualFileSimple *new_file_simple = DCAST(VirtualFileSimple, new_file);
00169     if (new_file_simple->_mount == _mount) {
00170       // Same mount pount.
00171       if (_mount->copy_file(_local_filename, new_file_simple->_local_filename)) {
00172         return true;
00173       }
00174     }
00175   }
00176 
00177   // Different mount point, or the mount doesn't support copying.  Do
00178   // it by hand.
00179   ostream *out = new_file->open_write_file(false, true);
00180   istream *in = open_read_file(false);
00181 
00182   static const size_t buffer_size = 4096;
00183   char buffer[buffer_size];
00184   
00185   in->read(buffer, buffer_size);
00186   size_t count = in->gcount();
00187   while (count != 0) {
00188     out->write(buffer, count);
00189     if (out->fail()) {
00190       new_file->close_write_file(out);
00191       close_read_file(in);
00192       new_file->delete_file();
00193       return false;
00194     }
00195     in->read(buffer, buffer_size);
00196     count = in->gcount();
00197   }
00198 
00199   if (!in->eof()) {
00200     new_file->close_write_file(out);
00201     close_read_file(in);
00202     new_file->delete_file();
00203     return false;
00204   }
00205 
00206   new_file->close_write_file(out);
00207   close_read_file(in);
00208   return true;
00209 }
00210 
00211 ////////////////////////////////////////////////////////////////////
00212 //     Function: VirtualFileSimple::open_read_file
00213 //       Access: Published, Virtual
00214 //  Description: Opens the file for reading.  Returns a newly
00215 //               allocated istream on success (which you should
00216 //               eventually delete when you are done reading).
00217 //               Returns NULL on failure.
00218 //
00219 //               If auto_unwrap is true, an explicitly-named .pz file
00220 //               is automatically decompressed and the decompressed
00221 //               contents are returned.  This is different than
00222 //               vfs-implicit-pz, which will automatically decompress
00223 //               a file if the extension .pz is *not* given.
00224 ////////////////////////////////////////////////////////////////////
00225 istream *VirtualFileSimple::
00226 open_read_file(bool auto_unwrap) const {
00227 
00228   // Will we be automatically unwrapping a .pz file?
00229   bool do_uncompress = (_implicit_pz_file || (auto_unwrap && _local_filename.get_extension() == "pz"));
00230 
00231   Filename local_filename(_local_filename);
00232   if (do_uncompress) {
00233     // .pz files are always binary, of course.
00234     local_filename.set_binary();
00235   }
00236 
00237   return _mount->open_read_file(local_filename, do_uncompress);
00238 }
00239 
00240 ////////////////////////////////////////////////////////////////////
00241 //     Function: VirtualFileSimple::close_read_file
00242 //       Access: Published
00243 //  Description: Closes a file opened by a previous call to
00244 //               open_read_file().  This really just deletes the
00245 //               istream pointer, but it is recommended to use this
00246 //               interface instead of deleting it explicitly, to help
00247 //               work around compiler issues.
00248 ////////////////////////////////////////////////////////////////////
00249 void VirtualFileSimple::
00250 close_read_file(istream *stream) const {
00251   _mount->close_read_file(stream);
00252 }
00253 
00254 ////////////////////////////////////////////////////////////////////
00255 //     Function: VirtualFileSimple::open_write_file
00256 //       Access: Published, Virtual
00257 //  Description: Opens the file for writing.  Returns a newly
00258 //               allocated ostream on success (which you should
00259 //               eventually delete when you are done writing).
00260 //               Returns NULL on failure.
00261 //
00262 //               If auto_wrap is true, an explicitly-named .pz file is
00263 //               automatically compressed while writing.  If truncate
00264 //               is true, the file is truncated to zero length before
00265 //               writing.
00266 ////////////////////////////////////////////////////////////////////
00267 ostream *VirtualFileSimple::
00268 open_write_file(bool auto_wrap, bool truncate) {
00269   // Will we be automatically wrapping a .pz file?
00270   bool do_compress = (_implicit_pz_file || (auto_wrap && _local_filename.get_extension() == "pz"));
00271 
00272   Filename local_filename(_local_filename);
00273   if (do_compress) {
00274     // .pz files are always binary, of course.
00275     local_filename.set_binary();
00276   }
00277 
00278   return _mount->open_write_file(local_filename, do_compress, truncate);
00279 }
00280 
00281 ////////////////////////////////////////////////////////////////////
00282 //     Function: VirtualFileSimple::open_append_file
00283 //       Access: Published, Virtual
00284 //  Description: Works like open_write_file(), but the file is opened
00285 //               in append mode.  Like open_write_file, the returned
00286 //               pointer should eventually be passed to
00287 //               close_write_file().
00288 ////////////////////////////////////////////////////////////////////
00289 ostream *VirtualFileSimple::
00290 open_append_file() {
00291   return _mount->open_append_file(_local_filename);
00292 }
00293 
00294 ////////////////////////////////////////////////////////////////////
00295 //     Function: VirtualFileSimple::close_write_file
00296 //       Access: Published
00297 //  Description: Closes a file opened by a previous call to
00298 //               open_write_file().  This really just deletes the
00299 //               ostream pointer, but it is recommended to use this
00300 //               interface instead of deleting it explicitly, to help
00301 //               work around compiler issues.
00302 ////////////////////////////////////////////////////////////////////
00303 void VirtualFileSimple::
00304 close_write_file(ostream *stream) {
00305   _mount->close_write_file(stream);
00306 }
00307 
00308 ////////////////////////////////////////////////////////////////////
00309 //     Function: VirtualFileSimple::open_read_write_file
00310 //       Access: Published, Virtual
00311 //  Description: Opens the file for writing.  Returns a newly
00312 //               allocated iostream on success (which you should
00313 //               eventually delete when you are done writing).
00314 //               Returns NULL on failure.
00315 ////////////////////////////////////////////////////////////////////
00316 iostream *VirtualFileSimple::
00317 open_read_write_file(bool truncate) {
00318   return _mount->open_read_write_file(_local_filename, truncate);
00319 }
00320 
00321 ////////////////////////////////////////////////////////////////////
00322 //     Function: VirtualFileSimple::open_read_append_file
00323 //       Access: Published, Virtual
00324 //  Description: Works like open_read_write_file(), but the file is opened
00325 //               in append mode.  Like open_read_write_file, the returned
00326 //               pointer should eventually be passed to
00327 //               close_read_write_file().
00328 ////////////////////////////////////////////////////////////////////
00329 iostream *VirtualFileSimple::
00330 open_read_append_file() {
00331   return _mount->open_read_append_file(_local_filename);
00332 }
00333 
00334 ////////////////////////////////////////////////////////////////////
00335 //     Function: VirtualFileSimple::close_read_write_file
00336 //       Access: Published
00337 //  Description: Closes a file opened by a previous call to
00338 //               open_read_write_file().  This really just deletes the
00339 //               iostream pointer, but it is recommended to use this
00340 //               interface instead of deleting it explicitly, to help
00341 //               work around compiler issues.
00342 ////////////////////////////////////////////////////////////////////
00343 void VirtualFileSimple::
00344 close_read_write_file(iostream *stream) {
00345   _mount->close_read_write_file(stream);
00346 }
00347 
00348 ////////////////////////////////////////////////////////////////////
00349 //     Function: VirtualFileSimple::get_file_size
00350 //       Access: Published, Virtual
00351 //  Description: Returns the current size on disk (or wherever it is)
00352 //               of the already-open file.  Pass in the stream that
00353 //               was returned by open_read_file(); some
00354 //               implementations may require this stream to determine
00355 //               the size.
00356 ////////////////////////////////////////////////////////////////////
00357 off_t VirtualFileSimple::
00358 get_file_size(istream *stream) const {
00359   return _mount->get_file_size(_local_filename, stream);
00360 }
00361 
00362 ////////////////////////////////////////////////////////////////////
00363 //     Function: VirtualFileSimple::get_file_size
00364 //       Access: Published, Virtual
00365 //  Description: Returns the current size on disk (or wherever it is)
00366 //               of the file before it has been opened.
00367 ////////////////////////////////////////////////////////////////////
00368 off_t VirtualFileSimple::
00369 get_file_size() const {
00370   return _mount->get_file_size(_local_filename);
00371 }
00372 
00373 ////////////////////////////////////////////////////////////////////
00374 //     Function: VirtualFileSimple::get_timestamp
00375 //       Access: Published, Virtual
00376 //  Description: Returns a time_t value that represents the time the
00377 //               file was last modified, to within whatever precision
00378 //               the operating system records this information (on a
00379 //               Windows95 system, for instance, this may only be
00380 //               accurate to within 2 seconds).
00381 //
00382 //               If the timestamp cannot be determined, either because
00383 //               it is not supported by the operating system or
00384 //               because there is some error (such as file not found),
00385 //               returns 0.
00386 ////////////////////////////////////////////////////////////////////
00387 time_t VirtualFileSimple::
00388 get_timestamp() const {
00389   return _mount->get_timestamp(_local_filename);
00390 }
00391 
00392 ////////////////////////////////////////////////////////////////////
00393 //     Function: VirtualFileSimple::get_system_info
00394 //       Access: Published, Virtual
00395 //  Description: Populates the SubfileInfo structure with the data
00396 //               representing where the file actually resides on disk,
00397 //               if this is knowable.  Returns true if the file might
00398 //               reside on disk, and the info is populated, or false
00399 //               if it does not (or it is not known where the file
00400 //               resides), in which case the info is meaningless.
00401 ////////////////////////////////////////////////////////////////////
00402 bool VirtualFileSimple::
00403 get_system_info(SubfileInfo &info) {
00404   return _mount->get_system_info(_local_filename, info);
00405 }
00406 
00407 ////////////////////////////////////////////////////////////////////
00408 //     Function: VirtualFileSimple::atomic_compare_and_exchange_contents
00409 //       Access: Public, Virtual
00410 //  Description: See Filename::atomic_compare_and_exchange_contents().
00411 ////////////////////////////////////////////////////////////////////
00412 bool VirtualFileSimple::
00413 atomic_compare_and_exchange_contents(string &orig_contents,
00414                                      const string &old_contents, 
00415                                      const string &new_contents) {
00416   return _mount->atomic_compare_and_exchange_contents(_local_filename, orig_contents, old_contents, new_contents);
00417 }
00418 
00419 ////////////////////////////////////////////////////////////////////
00420 //     Function: VirtualFileSimple::atomic_read_contents
00421 //       Access: Public, Virtual
00422 //  Description: See Filename::atomic_read_contents().
00423 ////////////////////////////////////////////////////////////////////
00424 bool VirtualFileSimple::
00425 atomic_read_contents(string &contents) const {
00426   return _mount->atomic_read_contents(_local_filename, contents);
00427 }
00428 
00429 ////////////////////////////////////////////////////////////////////
00430 //     Function: VirtualFileSimple::read_file
00431 //       Access: Public, Virtual
00432 //  Description: Fills up the indicated pvector with the contents of
00433 //               the file, if it is a regular file.  Returns true on
00434 //               success, false otherwise.
00435 ////////////////////////////////////////////////////////////////////
00436 bool VirtualFileSimple::
00437 read_file(pvector<unsigned char> &result, bool auto_unwrap) const {
00438 
00439   // Will we be automatically unwrapping a .pz file?
00440   bool do_uncompress = (_implicit_pz_file || (auto_unwrap && _local_filename.get_extension() == "pz"));
00441 
00442   Filename local_filename(_local_filename);
00443   if (do_uncompress) {
00444     // .pz files are always binary, of course.
00445     local_filename.set_binary();
00446   }
00447 
00448   return _mount->read_file(local_filename, do_uncompress, result);
00449 }
00450 
00451 ////////////////////////////////////////////////////////////////////
00452 //     Function: VirtualFileSimple::write_file
00453 //       Access: Public, Virtual
00454 //  Description: Writes the indicated data to the file, if it is
00455 //               writable.  Returns true on success, false otherwise.
00456 ////////////////////////////////////////////////////////////////////
00457 bool VirtualFileSimple::
00458 write_file(const unsigned char *data, size_t data_size, bool auto_wrap) {
00459   // Will we be automatically wrapping a .pz file?
00460   bool do_compress = (_implicit_pz_file || (auto_wrap && _local_filename.get_extension() == "pz"));
00461 
00462   Filename local_filename(_local_filename);
00463   if (do_compress) {
00464     // .pz files are always binary, of course.
00465     local_filename.set_binary();
00466   }
00467 
00468   return _mount->write_file(local_filename, do_compress, data, data_size);
00469 }
00470 
00471 ////////////////////////////////////////////////////////////////////
00472 //     Function: VirtualFileSimple::scan_local_directory
00473 //       Access: Protected, Virtual
00474 //  Description: Fills file_list up with the list of files that are
00475 //               within this directory, excluding those whose
00476 //               basenames are listed in mount_points.  Returns true
00477 //               if successful, false if the file is not a directory
00478 //               or the directory cannot be read.
00479 ////////////////////////////////////////////////////////////////////
00480 bool VirtualFileSimple::
00481 scan_local_directory(VirtualFileList *file_list, 
00482                      const ov_set<string> &mount_points) const {
00483   vector_string names;
00484   if (!_mount->scan_directory(names, _local_filename)) {
00485     return false;
00486   }
00487 
00488   // Now the scan above gave us a list of basenames.  Turn these back
00489   // into VirtualFile pointers.
00490 
00491   // Each of the files returned by the mount will be just a simple
00492   // file within the same mount tree, unless it is shadowed by a
00493   // mount point listed in mount_points.
00494 
00495   vector_string::const_iterator ni;
00496   for (ni = names.begin(); ni != names.end(); ++ni) {
00497     const string &basename = (*ni);
00498     if (mount_points.find(basename) == mount_points.end()) {
00499       Filename filename(_local_filename, basename);
00500       VirtualFileSimple *file = new VirtualFileSimple(_mount, filename, false, 0);
00501       file_list->add_file(file);
00502     }
00503   }
00504   
00505   return true;
00506 }
 All Classes Functions Variables Enumerations