00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00024
00025
00026
00027
00028 VirtualFileSystem *VirtualFileSimple::
00029 get_file_system() const {
00030 return _mount->get_file_system();
00031 }
00032
00033
00034
00035
00036
00037
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
00060
00061
00062
00063 bool VirtualFileSimple::
00064 has_file() const {
00065 return _mount->has_file(_local_filename);
00066 }
00067
00068
00069
00070
00071
00072
00073
00074 bool VirtualFileSimple::
00075 is_directory() const {
00076 return _mount->is_directory(_local_filename);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085 bool VirtualFileSimple::
00086 is_regular_file() const {
00087 return _mount->is_regular_file(_local_filename);
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097 bool VirtualFileSimple::
00098 is_writable() const {
00099 return _mount->is_writable(_local_filename);
00100 }
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 bool VirtualFileSimple::
00111 delete_file() {
00112 return _mount->delete_file(_local_filename);
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
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
00138 if (_mount->rename_file(_local_filename, new_file_simple->_local_filename)) {
00139 return true;
00140 }
00141 }
00142 }
00143
00144
00145
00146 if (is_regular_file() && !new_file->is_directory()) {
00147
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
00160
00161
00162
00163
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
00171 if (_mount->copy_file(_local_filename, new_file_simple->_local_filename)) {
00172 return true;
00173 }
00174 }
00175 }
00176
00177
00178
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
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 istream *VirtualFileSimple::
00226 open_read_file(bool auto_unwrap) const {
00227
00228
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
00234 local_filename.set_binary();
00235 }
00236
00237 return _mount->open_read_file(local_filename, do_uncompress);
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 void VirtualFileSimple::
00250 close_read_file(istream *stream) const {
00251 _mount->close_read_file(stream);
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 ostream *VirtualFileSimple::
00268 open_write_file(bool auto_wrap, bool truncate) {
00269
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
00275 local_filename.set_binary();
00276 }
00277
00278 return _mount->open_write_file(local_filename, do_compress, truncate);
00279 }
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 ostream *VirtualFileSimple::
00290 open_append_file() {
00291 return _mount->open_append_file(_local_filename);
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 void VirtualFileSimple::
00304 close_write_file(ostream *stream) {
00305 _mount->close_write_file(stream);
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
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
00323
00324
00325
00326
00327
00328
00329 iostream *VirtualFileSimple::
00330 open_read_append_file() {
00331 return _mount->open_read_append_file(_local_filename);
00332 }
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 void VirtualFileSimple::
00344 close_read_write_file(iostream *stream) {
00345 _mount->close_read_write_file(stream);
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
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
00364
00365
00366
00367
00368 off_t VirtualFileSimple::
00369 get_file_size() const {
00370 return _mount->get_file_size(_local_filename);
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387 time_t VirtualFileSimple::
00388 get_timestamp() const {
00389 return _mount->get_timestamp(_local_filename);
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402 bool VirtualFileSimple::
00403 get_system_info(SubfileInfo &info) {
00404 return _mount->get_system_info(_local_filename, info);
00405 }
00406
00407
00408
00409
00410
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
00421
00422
00423
00424 bool VirtualFileSimple::
00425 atomic_read_contents(string &contents) const {
00426 return _mount->atomic_read_contents(_local_filename, contents);
00427 }
00428
00429
00430
00431
00432
00433
00434
00435
00436 bool VirtualFileSimple::
00437 read_file(pvector<unsigned char> &result, bool auto_unwrap) const {
00438
00439
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
00445 local_filename.set_binary();
00446 }
00447
00448 return _mount->read_file(local_filename, do_uncompress, result);
00449 }
00450
00451
00452
00453
00454
00455
00456
00457 bool VirtualFileSimple::
00458 write_file(const unsigned char *data, size_t data_size, bool auto_wrap) {
00459
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
00465 local_filename.set_binary();
00466 }
00467
00468 return _mount->write_file(local_filename, do_compress, data, data_size);
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478
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
00489
00490
00491
00492
00493
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 }