23TypeHandle VirtualFileMountRamdisk::_type_handle;
24TypeHandle VirtualFileMountRamdisk::FileBase::_type_handle;
25TypeHandle VirtualFileMountRamdisk::File::_type_handle;
26TypeHandle VirtualFileMountRamdisk::Directory::_type_handle;
31VirtualFileMountRamdisk::
32VirtualFileMountRamdisk() : _root(
"") {
42 PT(FileBase) f = _root.do_find_file(file);
44 return (f !=
nullptr);
55 PT(File) f = _root.do_create_file(file);
57 return (f !=
nullptr);
68 PT(FileBase) f = _root.do_delete_file(file);
70 return (f !=
nullptr);
82 PT(FileBase) orig_fb = _root.do_find_file(orig_filename);
83 if (orig_fb ==
nullptr) {
88 if (orig_fb->is_directory()) {
90 Directory *orig_d = DCAST(Directory, orig_fb);
91 PT(Directory) new_d = _root.do_make_directory(new_filename);
92 if (new_d ==
nullptr || !new_d->_files.empty()) {
97 if (express_cat.is_debug()) {
99 <<
"Renaming ramdisk directory " << orig_filename <<
" to " << new_filename <<
"\n";
102 new_d->_files.swap(orig_d->_files);
103 _root.do_delete_file(orig_filename);
109 File *orig_f = DCAST(File, orig_fb);
110 PT(File) new_f = _root.do_create_file(new_filename);
111 if (new_f ==
nullptr) {
116 if (express_cat.is_debug()) {
118 <<
"Renaming ramdisk file " << orig_filename <<
" to " << new_filename <<
"\n";
121 new_f->_data.str(orig_f->_data.str());
122 _root.do_delete_file(orig_filename);
137 PT(FileBase) orig_fb = _root.do_find_file(orig_filename);
138 if (orig_fb ==
nullptr || orig_fb->is_directory()) {
144 File *orig_f = DCAST(File, orig_fb);
145 PT(File) new_f = _root.do_create_file(new_filename);
146 if (new_f ==
nullptr) {
151 if (express_cat.is_debug()) {
153 <<
"Copying ramdisk file " << orig_filename <<
" to " << new_filename <<
"\n";
156 new_f->_data.str(orig_f->_data.str());
171 PT(Directory) f = _root.do_make_directory(file);
173 return (f !=
nullptr);
183 PT(FileBase) f = _root.do_find_file(file);
185 return (f !=
nullptr && f->is_directory());
195 PT(FileBase) f = _root.do_find_file(file);
197 return (f !=
nullptr && !f->is_directory());
217 PT(FileBase) f = _root.do_find_file(file);
219 if (f ==
nullptr || f->is_directory()) {
223 File *f2 = DCAST(File, f);
235 PT(File) f = _root.do_create_file(file);
243 f->_data.str(
string());
250 f->_timestamp = std::max(f->_timestamp + 1, time(
nullptr));
264 PT(File) f = _root.do_create_file(file);
270 return new OSubStream(&f->_wrapper, 0, 0,
true);
281 PT(File) f = _root.do_create_file(file);
289 f->_data.str(
string());
292 f->_timestamp = std::max(f->_timestamp + 1, time(
nullptr));
295 return new SubStream(&f->_wrapper, 0, 0);
306 PT(FileBase) f = _root.do_find_file(file);
308 if (f ==
nullptr || f->is_directory()) {
312 File *f2 = DCAST(File, f);
313 return new SubStream(&f2->_wrapper, 0, 0,
true);
324 PT(FileBase) f = _root.do_find_file(file);
326 if (f ==
nullptr || f->is_directory()) {
330 File *f2 = DCAST(File, f);
331 return f2->_data.str().length();
341 PT(FileBase) f = _root.do_find_file(file);
343 if (f ==
nullptr || f->is_directory()) {
347 File *f2 = DCAST(File, f);
348 return f2->_data.str().length();
364 PT(FileBase) f = _root.do_find_file(file);
369 time_t timestamp = f->_timestamp;
382 PT(FileBase) f = _root.do_find_file(dir);
383 if (f ==
nullptr || !f->is_directory()) {
388 Directory *f2 = DCAST(Directory, f);
389 bool result = f2->do_scan_directory(contents);
400 const string &old_contents,
401 const string &new_contents) {
403 PT(FileBase) f = _root.do_find_file(file);
404 if (f ==
nullptr || f->is_directory()) {
410 File *f2 = DCAST(File, f);
411 orig_contents = f2->_data.str();
412 if (orig_contents == old_contents) {
413 f2->_data.str(new_contents);
414 f2->_timestamp = time(
nullptr);
428 PT(FileBase) f = _root.do_find_file(file);
429 if (f ==
nullptr || f->is_directory()) {
434 File *f2 = DCAST(File, f);
435 contents = f2->_data.str();
445void VirtualFileMountRamdisk::
446output(ostream &out)
const {
447 out <<
"VirtualFileMountRamdisk";
453VirtualFileMountRamdisk::FileBase::
460bool VirtualFileMountRamdisk::FileBase::
461is_directory()
const {
468bool VirtualFileMountRamdisk::Directory::
469is_directory()
const {
477PT(VirtualFileMountRamdisk::FileBase) VirtualFileMountRamdisk::Directory::
478do_find_file(
const string &filename)
const {
479 size_t slash = filename.find(
'/');
480 if (slash == string::npos) {
481 if (filename.empty()) {
482 return (FileBase *)
this;
486 FileBase tfile(filename);
487 tfile.local_object();
488 Files::const_iterator fi = _files.find(&tfile);
489 if (fi != _files.end()) {
496 string dirname = filename.substr(0, slash);
497 string remainder = filename.substr(slash + 1);
498 FileBase tfile(dirname);
499 tfile.local_object();
500 Files::const_iterator fi = _files.find(&tfile);
501 if (fi != _files.end()) {
502 PT(FileBase) file = (*fi);
503 if (file->is_directory()) {
504 return DCAST(Directory, file.p())->do_find_file(remainder);
515PT(VirtualFileMountRamdisk::File) VirtualFileMountRamdisk::Directory::
516do_create_file(
const string &filename) {
517 size_t slash = filename.find(
'/');
518 if (slash == string::npos) {
520 FileBase tfile(filename);
521 tfile.local_object();
522 Files::iterator fi = _files.find(&tfile);
523 if (fi != _files.end()) {
524 PT(FileBase) file = (*fi);
525 if (!file->is_directory()) {
526 return DCAST(File, file.p());
533 if (express_cat.is_debug()) {
535 <<
"Making ramdisk file " << filename <<
"\n";
537 PT(File) file =
new File(filename);
538 _files.insert(file.p());
539 _timestamp = time(
nullptr);
544 string dirname = filename.substr(0, slash);
545 string remainder = filename.substr(slash + 1);
546 FileBase tfile(dirname);
547 tfile.local_object();
548 Files::iterator fi = _files.find(&tfile);
549 if (fi != _files.end()) {
550 PT(FileBase) file = (*fi);
551 if (file->is_directory()) {
552 return DCAST(Directory, file.p())->do_create_file(remainder);
563PT(VirtualFileMountRamdisk::Directory) VirtualFileMountRamdisk::Directory::
564do_make_directory(
const string &filename) {
565 size_t slash = filename.find(
'/');
566 if (slash == string::npos) {
568 FileBase tfile(filename);
569 tfile.local_object();
570 Files::iterator fi = _files.find(&tfile);
571 if (fi != _files.end()) {
572 PT(FileBase) file = (*fi);
573 if (file->is_directory()) {
574 return DCAST(Directory, file.p());
581 if (express_cat.is_debug()) {
583 <<
"Making ramdisk directory " << filename <<
"\n";
585 PT(Directory) file =
new Directory(filename);
586 _files.insert(file.p());
587 _timestamp = time(
nullptr);
592 string dirname = filename.substr(0, slash);
593 string remainder = filename.substr(slash + 1);
594 FileBase tfile(dirname);
595 tfile.local_object();
596 Files::iterator fi = _files.find(&tfile);
597 if (fi != _files.end()) {
598 PT(FileBase) file = (*fi);
599 if (file->is_directory()) {
600 return DCAST(Directory, file.p())->do_make_directory(remainder);
611PT(VirtualFileMountRamdisk::FileBase) VirtualFileMountRamdisk::Directory::
612do_delete_file(
const string &filename) {
613 size_t slash = filename.find(
'/');
614 if (slash == string::npos) {
616 FileBase tfile(filename);
617 tfile.local_object();
618 Files::iterator fi = _files.find(&tfile);
619 if (fi != _files.end()) {
620 PT(FileBase) file = (*fi);
621 if (file->is_directory()) {
622 Directory *dir = DCAST(Directory, file.p());
623 if (!dir->_files.empty()) {
629 _timestamp = time(
nullptr);
636 string dirname = filename.substr(0, slash);
637 string remainder = filename.substr(slash + 1);
638 FileBase tfile(dirname);
639 tfile.local_object();
640 Files::iterator fi = _files.find(&tfile);
641 if (fi != _files.end()) {
642 PT(FileBase) file = (*fi);
643 if (file->is_directory()) {
644 return DCAST(Directory, file.p())->do_delete_file(remainder);
654bool VirtualFileMountRamdisk::Directory::
655do_scan_directory(vector_string &contents)
const {
656 Files::const_iterator fi;
657 for (fi = _files.begin(); fi != _files.end(); ++fi) {
658 FileBase *file = (*fi);
659 contents.push_back(file->_basename);
The name of a file, such as a texture file or an Egg file.
An istream object that presents a subwindow into another istream.
An ostream object that presents a subwindow into another ostream.
Combined ISubStream and OSubStream for bidirectional I/O.
TypeHandle is the identifier used to differentiate C++ class types.
virtual bool atomic_read_contents(const Filename &file, std::string &contents) const
See Filename::atomic_read_contents().
virtual std::iostream * open_read_append_file(const Filename &file)
Works like open_read_write_file(), but the file is opened in append mode.
virtual bool scan_directory(vector_string &contents, const Filename &dir) const
Fills the given vector up with the list of filenames that are local to this directory,...
virtual bool is_directory(const Filename &file) const
Returns true if the indicated file exists within the mount system and is a directory.
virtual bool make_directory(const Filename &file)
Attempts to create the indicated file within the mount, if it does not already exist.
virtual time_t get_timestamp(const Filename &file) const
Returns a time_t value that represents the time the file was last modified, to within whatever precis...
virtual bool create_file(const Filename &file)
Attempts to create the indicated file within the mount, if it does not already exist.
virtual bool is_writable(const Filename &file) const
Returns true if the named file or directory may be written to, false otherwise.
virtual std::iostream * open_read_write_file(const Filename &file, bool truncate)
Opens the file for writing.
virtual bool delete_file(const Filename &file)
Attempts to delete the indicated file or directory within the mount.
virtual bool copy_file(const Filename &orig_filename, const Filename &new_filename)
Attempts to copy the contents of the indicated file to the indicated file.
virtual bool has_file(const Filename &file) const
Returns true if the indicated file exists within the mount system.
virtual bool atomic_compare_and_exchange_contents(const Filename &file, std::string &orig_contents, const std::string &old_contents, const std::string &new_contents)
See Filename::atomic_compare_and_exchange_contents().
virtual std::istream * open_read_file(const Filename &file) const
Opens the file for reading, if it exists.
virtual std::ostream * open_append_file(const Filename &file)
Works like open_write_file(), but the file is opened in append mode.
virtual bool rename_file(const Filename &orig_filename, const Filename &new_filename)
Attempts to rename the contents of the indicated file to the indicated file.
virtual std::streamsize get_file_size(const Filename &file, std::istream *stream) const
Returns the current size on disk (or wherever it is) of the already-open file.
virtual std::ostream * open_write_file(const Filename &file, bool truncate)
Opens the file for writing.
virtual bool is_regular_file(const Filename &file) const
Returns true if the indicated file exists within the mount system and is a regular file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.