00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "virtualFile.h"
00016 #include "virtualFileSystem.h"
00017 #include "virtualFileList.h"
00018 #include "config_express.h"
00019 #include "pvector.h"
00020 #include <iterator>
00021
00022 TypeHandle VirtualFile::_type_handle;
00023
00024
00025
00026
00027
00028
00029 bool VirtualFile::
00030 has_file() const {
00031 return false;
00032 }
00033
00034
00035
00036
00037
00038
00039
00040 bool VirtualFile::
00041 is_directory() const {
00042 return false;
00043 }
00044
00045
00046
00047
00048
00049
00050
00051 bool VirtualFile::
00052 is_regular_file() const {
00053 return false;
00054 }
00055
00056
00057
00058
00059
00060
00061
00062
00063 bool VirtualFile::
00064 is_writable() const {
00065 return false;
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 bool VirtualFile::
00077 delete_file() {
00078 return false;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 bool VirtualFile::
00099 rename_file(VirtualFile *new_file) {
00100 return false;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110 bool VirtualFile::
00111 copy_file(VirtualFile *new_file) {
00112 return false;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 PT(VirtualFileList) VirtualFile::
00125 scan_directory() const {
00126
00127
00128 VirtualFileSystem *file_system = get_file_system();
00129 Filename this_filename = get_filename();
00130 vector_string mount_points_flat;
00131 file_system->scan_mount_points(mount_points_flat, this_filename);
00132
00133
00134
00135 ov_set<string> mount_points;
00136 copy(mount_points_flat.begin(), mount_points_flat.end(),
00137 back_inserter(mount_points));
00138 mount_points.sort();
00139
00140
00141 PT(VirtualFileList) file_list = new VirtualFileList;
00142
00143
00144
00145 ov_set<string>::const_iterator mi;
00146 for (mi = mount_points.begin(); mi != mount_points.end(); ++mi) {
00147 const string &basename = (*mi);
00148 Filename filename(this_filename, basename);
00149 PT(VirtualFile) file = file_system->get_file(filename);
00150 file_list->add_file(file);
00151 }
00152
00153
00154 vector_string names;
00155 if (!scan_local_directory(file_list, mount_points)) {
00156
00157 if (file_list->get_num_files() == 0) {
00158 return NULL;
00159 }
00160
00161
00162
00163 return file_list;
00164 }
00165
00166 return file_list;
00167 }
00168
00169
00170
00171
00172
00173
00174 void VirtualFile::
00175 output(ostream &out) const {
00176 out << get_filename();
00177 }
00178
00179
00180
00181
00182
00183
00184
00185 void VirtualFile::
00186 ls(ostream &out) const {
00187 CPT(VirtualFileList) contents = scan_directory();
00188 if (contents == (VirtualFileList *)NULL) {
00189 if (!is_directory()) {
00190 out << get_filename() << "\n";
00191 } else {
00192 out << get_filename() << " cannot be read.\n";
00193 }
00194 return;
00195 }
00196
00197 int num_files = contents->get_num_files();
00198 for (int i = 0; i < num_files; i++) {
00199 VirtualFile *file = contents->get_file(i);
00200 out << file->get_filename().get_basename() << "\n";
00201 }
00202 }
00203
00204
00205
00206
00207
00208
00209
00210 void VirtualFile::
00211 ls_all(ostream &out) const {
00212 if (!is_directory()) {
00213 out << get_filename() << " is not a directory.\n";
00214 } else {
00215 r_ls_all(out, get_filename());
00216 }
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 istream *VirtualFile::
00228 open_read_file(bool auto_unwrap) const {
00229 return NULL;
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 void VirtualFile::
00242 close_read_file(istream *stream) const {
00243 nassertv(false);
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 bool VirtualFile::
00257 was_read_successful() const {
00258 return true;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 ostream *VirtualFile::
00270 open_write_file(bool auto_wrap, bool truncate) {
00271 return NULL;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 ostream *VirtualFile::
00283 open_append_file() {
00284 return NULL;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 void VirtualFile::
00297 close_write_file(ostream *stream) {
00298 nassertv(false);
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 iostream *VirtualFile::
00310 open_read_write_file(bool truncate) {
00311 return NULL;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 iostream *VirtualFile::
00323 open_read_append_file() {
00324 return NULL;
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 void VirtualFile::
00337 close_read_write_file(iostream *stream) {
00338 nassertv(false);
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 off_t VirtualFile::
00351 get_file_size(istream *stream) const {
00352 return get_file_size();
00353 }
00354
00355
00356
00357
00358
00359
00360
00361 off_t VirtualFile::
00362 get_file_size() const {
00363 return 0;
00364 }
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 time_t VirtualFile::
00381 get_timestamp() const {
00382 return 0;
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 bool VirtualFile::
00396 get_system_info(SubfileInfo &info) {
00397 return false;
00398 }
00399
00400
00401
00402
00403
00404
00405 bool VirtualFile::
00406 atomic_compare_and_exchange_contents(string &orig_contents,
00407 const string &old_contents,
00408 const string &new_contents) {
00409 return false;
00410 }
00411
00412
00413
00414
00415
00416
00417 bool VirtualFile::
00418 atomic_read_contents(string &contents) const {
00419 return false;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429 bool VirtualFile::
00430 read_file(string &result, bool auto_unwrap) const {
00431 result = string();
00432
00433 pvector<unsigned char> pv;
00434 if (!read_file(pv, auto_unwrap)) {
00435 return false;
00436 }
00437
00438 if (!pv.empty()) {
00439 result.append((const char *)&pv[0], pv.size());
00440 }
00441
00442 return true;
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452 bool VirtualFile::
00453 read_file(pvector<unsigned char> &result, bool auto_unwrap) const {
00454 return false;
00455 }
00456
00457
00458
00459
00460
00461
00462
00463 bool VirtualFile::
00464 write_file(const unsigned char *data, size_t data_size, bool auto_wrap) {
00465 return false;
00466 }
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 bool VirtualFile::
00477 simple_read_file(istream *in, pvector<unsigned char> &result) {
00478 static const size_t buffer_size = 4096;
00479 char buffer[buffer_size];
00480
00481 in->read(buffer, buffer_size);
00482 size_t count = in->gcount();
00483 while (count != 0) {
00484 thread_consider_yield();
00485 result.insert(result.end(), buffer, buffer + count);
00486 in->read(buffer, buffer_size);
00487 count = in->gcount();
00488 }
00489
00490 return (!in->fail() || in->eof());
00491 }
00492
00493
00494
00495
00496
00497
00498
00499 bool VirtualFile::
00500 simple_read_file(istream *in, pvector<unsigned char> &result, size_t max_bytes) {
00501 static const size_t buffer_size = 4096;
00502 char buffer[buffer_size];
00503
00504 in->read(buffer, min(buffer_size, max_bytes));
00505 size_t count = in->gcount();
00506 while (count != 0) {
00507 thread_consider_yield();
00508 nassertr(count <= max_bytes, false);
00509 result.insert(result.end(), buffer, buffer + count);
00510 max_bytes -= count;
00511 in->read(buffer, min(buffer_size, max_bytes));
00512 count = in->gcount();
00513 }
00514
00515 return (!in->fail() || in->eof());
00516 }
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 bool VirtualFile::
00528 scan_local_directory(VirtualFileList *, const ov_set<string> &) const {
00529 return false;
00530 }
00531
00532
00533
00534
00535
00536
00537 void VirtualFile::
00538 r_ls_all(ostream &out, const Filename &root) const {
00539 CPT(VirtualFileList) contents = scan_directory();
00540 if (contents == (VirtualFileList *)NULL) {
00541 return;
00542 }
00543
00544 int num_files = contents->get_num_files();
00545 for (int i = 0; i < num_files; i++) {
00546 VirtualFile *file = contents->get_file(i);
00547 Filename filename = file->get_filename();
00548 filename.make_relative_to(root);
00549 out << filename << "\n";
00550 if (file->is_directory()) {
00551 file->r_ls_all(out, root);
00552 }
00553 }
00554 }