00001 // Filename: virtualFileMount.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 "virtualFileMount.h" 00016 #include "virtualFileSimple.h" 00017 #include "zStream.h" 00018 00019 TypeHandle VirtualFileMount::_type_handle; 00020 00021 00022 //////////////////////////////////////////////////////////////////// 00023 // Function: VirtualFileMount::Destructor 00024 // Access: Public, Virtual 00025 // Description: 00026 //////////////////////////////////////////////////////////////////// 00027 VirtualFileMount:: 00028 ~VirtualFileMount() { 00029 nassertv(_file_system == NULL); 00030 } 00031 00032 //////////////////////////////////////////////////////////////////// 00033 // Function: VirtualFileMount::make_virtual_file 00034 // Access: Public, Virtual 00035 // Description: Constructs and returns a new VirtualFile instance 00036 // that corresponds to the indicated filename within 00037 // this mount point. The returned VirtualFile object 00038 // does not imply that the given file actually exists; 00039 // but if the file does exist, then the handle can be 00040 // used to read it. 00041 //////////////////////////////////////////////////////////////////// 00042 PT(VirtualFile) VirtualFileMount:: 00043 make_virtual_file(const Filename &local_filename, 00044 const Filename &original_filename, bool implicit_pz_file, 00045 int open_flags) { 00046 Filename local(local_filename); 00047 if (original_filename.is_text()) { 00048 local.set_text(); 00049 } else { 00050 local.set_binary(); 00051 } 00052 PT(VirtualFileSimple) file = 00053 new VirtualFileSimple(this, local, implicit_pz_file, open_flags); 00054 file->set_original_filename(original_filename); 00055 00056 if ((open_flags & VirtualFileSystem::OF_create_file) != 0) { 00057 create_file(local); 00058 } else if ((open_flags & VirtualFileSystem::OF_make_directory) != 0) { 00059 make_directory(local); 00060 } 00061 00062 return file.p(); 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: VirtualFileMount::create_file 00067 // Access: Public, Virtual 00068 // Description: Attempts to create the indicated file within the 00069 // mount, if it does not already exist. Returns true on 00070 // success (or if the file already exists), or false if 00071 // it cannot be created. 00072 //////////////////////////////////////////////////////////////////// 00073 bool VirtualFileMount:: 00074 create_file(const Filename &file) { 00075 return false; 00076 } 00077 00078 //////////////////////////////////////////////////////////////////// 00079 // Function: VirtualFileMount::delete_file 00080 // Access: Public, Virtual 00081 // Description: Attempts to delete the indicated file or directory 00082 // within the mount. This can remove a single file or 00083 // an empty directory. It will not remove a nonempty 00084 // directory. Returns true on success, false on 00085 // failure. 00086 //////////////////////////////////////////////////////////////////// 00087 bool VirtualFileMount:: 00088 delete_file(const Filename &file) { 00089 return false; 00090 } 00091 00092 //////////////////////////////////////////////////////////////////// 00093 // Function: VirtualFileMount::rename_file 00094 // Access: Public 00095 // Description: Attempts to rename the contents of the indicated file 00096 // to the indicated file. Both filenames will be within 00097 // the mount. Returns true on success, false on 00098 // failure. If this returns false, this will be 00099 // attempted again with a copy-and-delete operation. 00100 //////////////////////////////////////////////////////////////////// 00101 bool VirtualFileMount:: 00102 rename_file(const Filename &orig_filename, const Filename &new_filename) { 00103 return false; 00104 } 00105 00106 //////////////////////////////////////////////////////////////////// 00107 // Function: VirtualFileMount::copy_file 00108 // Access: Public 00109 // Description: Attempts to copy the contents of the indicated file 00110 // to the indicated file. Both filenames will be within 00111 // the mount. Returns true on success, false on 00112 // failure. If this returns false, the copy will be 00113 // performed by explicit read-and-write operations. 00114 //////////////////////////////////////////////////////////////////// 00115 bool VirtualFileMount:: 00116 copy_file(const Filename &orig_filename, const Filename &new_filename) { 00117 return false; 00118 } 00119 00120 //////////////////////////////////////////////////////////////////// 00121 // Function: VirtualFileMount::make_directory 00122 // Access: Public, Virtual 00123 // Description: Attempts to create the indicated file within the 00124 // mount, if it does not already exist. Returns true on 00125 // success, or false if it cannot be created. If the 00126 // directory already existed prior to this call, may 00127 // return either true or false. 00128 //////////////////////////////////////////////////////////////////// 00129 bool VirtualFileMount:: 00130 make_directory(const Filename &file) { 00131 return false; 00132 } 00133 00134 //////////////////////////////////////////////////////////////////// 00135 // Function: VirtualFileMount::is_writable 00136 // Access: Public, Virtual 00137 // Description: Returns true if the named file or directory may be 00138 // written to, false otherwise. 00139 //////////////////////////////////////////////////////////////////// 00140 bool VirtualFileMount:: 00141 is_writable(const Filename &file) const { 00142 return false; 00143 } 00144 00145 //////////////////////////////////////////////////////////////////// 00146 // Function: VirtualFileMount::read_file 00147 // Access: Public, Virtual 00148 // Description: Fills up the indicated pvector with the contents of 00149 // the file, if it is a regular file. Returns true on 00150 // success, false otherwise. 00151 //////////////////////////////////////////////////////////////////// 00152 bool VirtualFileMount:: 00153 read_file(const Filename &file, bool do_uncompress, 00154 pvector<unsigned char> &result) const { 00155 result.clear(); 00156 00157 istream *in = open_read_file(file, do_uncompress); 00158 if (in == (istream *)NULL) { 00159 express_cat.info() 00160 << "Unable to read " << file << "\n"; 00161 return false; 00162 } 00163 00164 off_t file_size = get_file_size(file, in); 00165 if (file_size > 0) { 00166 result.reserve((size_t)file_size); 00167 } 00168 00169 bool okflag = VirtualFile::simple_read_file(in, result); 00170 00171 close_read_file(in); 00172 00173 if (!okflag) { 00174 express_cat.info() 00175 << "Error while reading " << file << "\n"; 00176 } 00177 return okflag; 00178 } 00179 00180 //////////////////////////////////////////////////////////////////// 00181 // Function: VirtualFileMount::write_file 00182 // Access: Public, Virtual 00183 // Description: Writes the indicated data to the file, if it is a 00184 // writable file. Returns true on success, false 00185 // otherwise. 00186 //////////////////////////////////////////////////////////////////// 00187 bool VirtualFileMount:: 00188 write_file(const Filename &file, bool do_compress, 00189 const unsigned char *data, size_t data_size) { 00190 ostream *out = open_write_file(file, do_compress, true); 00191 if (out == (ostream *)NULL) { 00192 express_cat.info() 00193 << "Unable to write " << file << "\n"; 00194 return false; 00195 } 00196 00197 out->write((const char *)data, data_size); 00198 bool okflag = (!out->fail()); 00199 close_write_file(out); 00200 00201 if (!okflag) { 00202 express_cat.info() 00203 << "Error while writing " << file << "\n"; 00204 } 00205 return okflag; 00206 } 00207 00208 //////////////////////////////////////////////////////////////////// 00209 // Function: VirtualFileMount::open_read_file 00210 // Access: Published, Virtual 00211 // Description: Opens the file for reading. Returns a newly 00212 // allocated istream on success (which you should 00213 // eventually delete when you are done reading). 00214 // Returns NULL on failure. 00215 // 00216 // If do_uncompress is true, the file is also 00217 // decompressed on-the-fly using zlib. 00218 //////////////////////////////////////////////////////////////////// 00219 istream *VirtualFileMount:: 00220 open_read_file(const Filename &file, bool do_uncompress) const { 00221 istream *result = open_read_file(file); 00222 00223 #ifdef HAVE_ZLIB 00224 if (result != (istream *)NULL && do_uncompress) { 00225 // We have to slip in a layer to decompress the file on the fly. 00226 IDecompressStream *wrapper = new IDecompressStream(result, true); 00227 result = wrapper; 00228 } 00229 #endif // HAVE_ZLIB 00230 00231 return result; 00232 } 00233 00234 //////////////////////////////////////////////////////////////////// 00235 // Function: VirtualFileMount::close_read_file 00236 // Access: Public, Virtual 00237 // Description: Closes a file opened by a previous call to 00238 // open_read_file(). This really just deletes the 00239 // istream pointer, but it is recommended to use this 00240 // interface instead of deleting it explicitly, to help 00241 // work around compiler issues. 00242 //////////////////////////////////////////////////////////////////// 00243 void VirtualFileMount:: 00244 close_read_file(istream *stream) const { 00245 VirtualFileSystem::close_read_file(stream); 00246 } 00247 00248 //////////////////////////////////////////////////////////////////// 00249 // Function: VirtualFileMount::open_write_file 00250 // Access: Published, Virtual 00251 // Description: Opens the file for writing. Returns a newly 00252 // allocated ostream on success (which you should 00253 // eventually delete when you are done writing). 00254 // Returns NULL on failure. 00255 //////////////////////////////////////////////////////////////////// 00256 ostream *VirtualFileMount:: 00257 open_write_file(const Filename &file, bool truncate) { 00258 return NULL; 00259 } 00260 00261 //////////////////////////////////////////////////////////////////// 00262 // Function: VirtualFileMount::open_write_file 00263 // Access: Published 00264 // Description: Opens the file for writing. Returns a newly 00265 // allocated ostream on success (which you should 00266 // eventually delete when you are done writing). 00267 // Returns NULL on failure. 00268 // 00269 // If do_compress is true, the file is also 00270 // compressed on-the-fly using zlib. 00271 //////////////////////////////////////////////////////////////////// 00272 ostream *VirtualFileMount:: 00273 open_write_file(const Filename &file, bool do_compress, bool truncate) { 00274 ostream *result = open_write_file(file, truncate); 00275 00276 #ifdef HAVE_ZLIB 00277 if (result != (ostream *)NULL && do_compress) { 00278 // We have to slip in a layer to compress the file on the fly. 00279 OCompressStream *wrapper = new OCompressStream(result, true); 00280 result = wrapper; 00281 } 00282 #endif // HAVE_ZLIB 00283 00284 return result; 00285 } 00286 00287 //////////////////////////////////////////////////////////////////// 00288 // Function: VirtualFileMount::open_append_file 00289 // Access: Published, Virtual 00290 // Description: Works like open_write_file(), but the file is opened 00291 // in append mode. Like open_write_file, the returned 00292 // pointer should eventually be passed to 00293 // close_write_file(). 00294 //////////////////////////////////////////////////////////////////// 00295 ostream *VirtualFileMount:: 00296 open_append_file(const Filename &file) { 00297 return NULL; 00298 } 00299 00300 //////////////////////////////////////////////////////////////////// 00301 // Function: VirtualFileMount::close_write_file 00302 // Access: Public, Virtual 00303 // Description: Closes a file opened by a previous call to 00304 // open_write_file(). This really just deletes the 00305 // ostream pointer, but it is recommended to use this 00306 // interface instead of deleting it explicitly, to help 00307 // work around compiler issues. 00308 //////////////////////////////////////////////////////////////////// 00309 void VirtualFileMount:: 00310 close_write_file(ostream *stream) { 00311 VirtualFileSystem::close_write_file(stream); 00312 } 00313 00314 //////////////////////////////////////////////////////////////////// 00315 // Function: VirtualFileMount::open_read_write_file 00316 // Access: Published, Virtual 00317 // Description: Opens the file for writing. Returns a newly 00318 // allocated iostream on success (which you should 00319 // eventually delete when you are done writing). 00320 // Returns NULL on failure. 00321 //////////////////////////////////////////////////////////////////// 00322 iostream *VirtualFileMount:: 00323 open_read_write_file(const Filename &file, bool truncate) { 00324 return NULL; 00325 } 00326 00327 //////////////////////////////////////////////////////////////////// 00328 // Function: VirtualFileMount::open_read_append_file 00329 // Access: Published, Virtual 00330 // Description: Works like open_read_write_file(), but the file is opened 00331 // in append mode. Like open_read_write_file, the returned 00332 // pointer should eventually be passed to 00333 // close_read_write_file(). 00334 //////////////////////////////////////////////////////////////////// 00335 iostream *VirtualFileMount:: 00336 open_read_append_file(const Filename &file) { 00337 return NULL; 00338 } 00339 00340 //////////////////////////////////////////////////////////////////// 00341 // Function: VirtualFileMount::close_read_write_file 00342 // Access: Public, Virtual 00343 // Description: Closes a file opened by a previous call to 00344 // open_read_write_file(). This really just deletes the 00345 // iostream pointer, but it is recommended to use this 00346 // interface instead of deleting it explicitly, to help 00347 // work around compiler issues. 00348 //////////////////////////////////////////////////////////////////// 00349 void VirtualFileMount:: 00350 close_read_write_file(iostream *stream) { 00351 VirtualFileSystem::close_read_write_file(stream); 00352 } 00353 00354 //////////////////////////////////////////////////////////////////// 00355 // Function: VirtualFileMount::get_system_info 00356 // Access: Public, Virtual 00357 // Description: Populates the SubfileInfo structure with the data 00358 // representing where the file actually resides on disk, 00359 // if this is knowable. Returns true if the file might 00360 // reside on disk, and the info is populated, or false 00361 // if it does not (or it is not known where the file 00362 // resides), in which case the info is meaningless. 00363 //////////////////////////////////////////////////////////////////// 00364 bool VirtualFileMount:: 00365 get_system_info(const Filename &file, SubfileInfo &info) { 00366 return false; 00367 } 00368 00369 //////////////////////////////////////////////////////////////////// 00370 // Function: VirtualFileMount::atomic_compare_and_exchange_contents 00371 // Access: Public, Virtual 00372 // Description: See Filename::atomic_compare_and_exchange_contents(). 00373 //////////////////////////////////////////////////////////////////// 00374 bool VirtualFileMount:: 00375 atomic_compare_and_exchange_contents(const Filename &file, string &orig_contents, 00376 const string &old_contents, 00377 const string &new_contents) { 00378 return false; 00379 } 00380 00381 //////////////////////////////////////////////////////////////////// 00382 // Function: VirtualFileMount::atomic_read_contents 00383 // Access: Public, Virtual 00384 // Description: See Filename::atomic_read_contents(). 00385 //////////////////////////////////////////////////////////////////// 00386 bool VirtualFileMount:: 00387 atomic_read_contents(const Filename &file, string &contents) const { 00388 return false; 00389 } 00390 00391 //////////////////////////////////////////////////////////////////// 00392 // Function: VirtualFileMount::output 00393 // Access: Public, Virtual 00394 // Description: 00395 //////////////////////////////////////////////////////////////////// 00396 void VirtualFileMount:: 00397 output(ostream &out) const { 00398 out << get_type(); 00399 } 00400 00401 //////////////////////////////////////////////////////////////////// 00402 // Function: VirtualFileMount::write 00403 // Access: Public, Virtual 00404 // Description: 00405 //////////////////////////////////////////////////////////////////// 00406 void VirtualFileMount:: 00407 write(ostream &out) const { 00408 out << *this << " on /" << get_mount_point() << "\n"; 00409 }