Panda3D
|
00001 // Filename: virtualFileMountRamdisk.h 00002 // Created by: drose (19Sep11) 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 #ifndef VIRTUALFILEMOUNTRAMDISK_H 00016 #define VIRTUALFILEMOUNTRAMDISK_H 00017 00018 #include "pandabase.h" 00019 00020 #include "virtualFileMount.h" 00021 #include "mutexImpl.h" 00022 #include "streamWrapper.h" 00023 00024 //////////////////////////////////////////////////////////////////// 00025 // Class : VirtualFileMountRamdisk 00026 // Description : Simulates an actual directory on disk with in-memory 00027 // storage. This is useful mainly for performing high 00028 // level functions that expect disk I/O without actually 00029 // writing files to disk. Naturally, there are 00030 // significant limits to the size of the files that may 00031 // be written with this system; and "files" written here 00032 // are not automatically persistent between sessions. 00033 //////////////////////////////////////////////////////////////////// 00034 class EXPCL_PANDAEXPRESS VirtualFileMountRamdisk : public VirtualFileMount { 00035 PUBLISHED: 00036 VirtualFileMountRamdisk(); 00037 00038 public: 00039 virtual bool has_file(const Filename &file) const; 00040 virtual bool create_file(const Filename &file); 00041 virtual bool make_directory(const Filename &file); 00042 virtual bool delete_file(const Filename &file); 00043 virtual bool rename_file(const Filename &orig_filename, const Filename &new_filename); 00044 virtual bool copy_file(const Filename &orig_filename, const Filename &new_filename); 00045 virtual bool is_directory(const Filename &file) const; 00046 virtual bool is_regular_file(const Filename &file) const; 00047 virtual bool is_writable(const Filename &file) const; 00048 00049 virtual istream *open_read_file(const Filename &file) const; 00050 virtual ostream *open_write_file(const Filename &file, bool truncate); 00051 virtual ostream *open_append_file(const Filename &file); 00052 virtual iostream *open_read_write_file(const Filename &file, bool truncate); 00053 virtual iostream *open_read_append_file(const Filename &file); 00054 00055 virtual off_t get_file_size(const Filename &file, istream *stream) const; 00056 virtual off_t get_file_size(const Filename &file) const; 00057 virtual time_t get_timestamp(const Filename &file) const; 00058 00059 virtual bool scan_directory(vector_string &contents, 00060 const Filename &dir) const; 00061 00062 virtual bool atomic_compare_and_exchange_contents(const Filename &file, string &orig_contents, const string &old_contents, const string &new_contents); 00063 virtual bool atomic_read_contents(const Filename &file, string &contents) const; 00064 00065 virtual void output(ostream &out) const; 00066 00067 private: 00068 class FileBase; 00069 class File; 00070 class Directory; 00071 00072 class FileBase : public TypedReferenceCount { 00073 public: 00074 INLINE FileBase(const string &basename); 00075 virtual ~FileBase(); 00076 INLINE bool operator < (const FileBase &other) const; 00077 00078 virtual bool is_directory() const; 00079 00080 string _basename; 00081 time_t _timestamp; 00082 00083 public: 00084 virtual TypeHandle get_type() const { 00085 return get_class_type(); 00086 } 00087 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00088 static TypeHandle get_class_type() { 00089 return _type_handle; 00090 } 00091 static void init_type() { 00092 TypedReferenceCount::init_type(); 00093 register_type(_type_handle, "VirtualFileMountRamdisk::FileBase", 00094 TypedReferenceCount::get_class_type()); 00095 } 00096 00097 private: 00098 static TypeHandle _type_handle; 00099 }; 00100 00101 class File : public FileBase { 00102 public: 00103 INLINE File(const string &basename); 00104 00105 stringstream _data; 00106 StreamWrapper _wrapper; 00107 00108 public: 00109 virtual TypeHandle get_type() const { 00110 return get_class_type(); 00111 } 00112 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00113 static TypeHandle get_class_type() { 00114 return _type_handle; 00115 } 00116 static void init_type() { 00117 FileBase::init_type(); 00118 register_type(_type_handle, "VirtualFileMountRamdisk::File", 00119 FileBase::get_class_type()); 00120 } 00121 00122 private: 00123 static TypeHandle _type_handle; 00124 }; 00125 00126 typedef pset<PT(FileBase), indirect_less<FileBase *> > Files; 00127 00128 class Directory : public FileBase { 00129 public: 00130 INLINE Directory(const string &basename); 00131 00132 virtual bool is_directory() const; 00133 00134 PT(FileBase) do_find_file(const string &filename) const; 00135 PT(File) do_create_file(const string &filename); 00136 PT(Directory) do_make_directory(const string &filename); 00137 PT(FileBase) do_delete_file(const string &filename); 00138 bool do_scan_directory(vector_string &contents) const; 00139 00140 Files _files; 00141 00142 public: 00143 virtual TypeHandle get_type() const { 00144 return get_class_type(); 00145 } 00146 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00147 static TypeHandle get_class_type() { 00148 return _type_handle; 00149 } 00150 static void init_type() { 00151 FileBase::init_type(); 00152 register_type(_type_handle, "VirtualFileMountRamdisk::Directory", 00153 FileBase::get_class_type()); 00154 } 00155 00156 private: 00157 static TypeHandle _type_handle; 00158 }; 00159 00160 Directory _root; 00161 MutexImpl _lock; 00162 00163 public: 00164 virtual TypeHandle get_type() const { 00165 return get_class_type(); 00166 } 00167 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00168 static TypeHandle get_class_type() { 00169 return _type_handle; 00170 } 00171 static void init_type() { 00172 VirtualFileMount::init_type(); 00173 register_type(_type_handle, "VirtualFileMountRamdisk", 00174 VirtualFileMount::get_class_type()); 00175 FileBase::init_type(); 00176 File::init_type(); 00177 Directory::init_type(); 00178 } 00179 00180 private: 00181 static TypeHandle _type_handle; 00182 }; 00183 00184 #include "virtualFileMountRamdisk.I" 00185 00186 #endif