Panda3D

virtualFileSystem.h

00001 // Filename: virtualFileSystem.h
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 #ifndef VIRTUALFILESYSTEM_H
00016 #define VIRTUALFILESYSTEM_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "virtualFile.h"
00021 #include "virtualFileMount.h"
00022 #include "virtualFileList.h"
00023 #include "filename.h"
00024 #include "dSearchPath.h"
00025 #include "pointerTo.h"
00026 #include "config_express.h"
00027 #include "mutexImpl.h"
00028 #include "pvector.h"
00029 
00030 class Multifile;
00031 class VirtualFileComposite;
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //       Class : VirtualFileSystem
00035 // Description : A hierarchy of directories and files that appears to
00036 //               be one continuous file system, even though the files
00037 //               may originate from several different sources that may
00038 //               not be related to the actual OS's file system.
00039 //
00040 //               For instance, a VirtualFileSystem can transparently
00041 //               mount one or more Multifiles as their own
00042 //               subdirectory hierarchies.
00043 ////////////////////////////////////////////////////////////////////
00044 class EXPCL_PANDAEXPRESS VirtualFileSystem {
00045 PUBLISHED:
00046   VirtualFileSystem();
00047   ~VirtualFileSystem();
00048 
00049   enum MountFlags {
00050     MF_read_only      = 0x0002,
00051   };
00052 
00053   BLOCKING bool mount(Multifile *multifile, const Filename &mount_point, int flags);
00054   BLOCKING bool mount(const Filename &physical_filename, const Filename &mount_point, 
00055                       int flags, const string &password = "");
00056   BLOCKING bool mount_loop(const Filename &virtual_filename, const Filename &mount_point, 
00057                       int flags, const string &password = "");
00058   bool mount(VirtualFileMount *mount, const Filename &mount_point, int flags);
00059   BLOCKING int unmount(Multifile *multifile);
00060   BLOCKING int unmount(const Filename &physical_filename);
00061   int unmount(VirtualFileMount *mount);
00062   BLOCKING int unmount_point(const Filename &mount_point);
00063   BLOCKING int unmount_all();
00064 
00065   int get_num_mounts() const;
00066   PT(VirtualFileMount) get_mount(int n) const;
00067   MAKE_SEQ(get_mounts, get_num_mounts, get_mount);
00068 
00069   BLOCKING bool chdir(const Filename &new_directory);
00070   BLOCKING Filename get_cwd() const;
00071   BLOCKING bool make_directory(const Filename &filename);
00072   BLOCKING bool make_directory_full(const Filename &filename);
00073 
00074   BLOCKING PT(VirtualFile) get_file(const Filename &filename, bool status_only = false) const;
00075   BLOCKING PT(VirtualFile) create_file(const Filename &filename);
00076   BLOCKING PT(VirtualFile) find_file(const Filename &filename, 
00077                                      const DSearchPath &searchpath,
00078                                      bool status_only = false) const;
00079   BLOCKING bool delete_file(const Filename &filename);
00080   BLOCKING bool rename_file(const Filename &orig_filename, const Filename &new_filename);
00081   BLOCKING bool copy_file(const Filename &orig_filename, const Filename &new_filename);
00082 
00083   BLOCKING bool resolve_filename(Filename &filename, const DSearchPath &searchpath,
00084                                  const string &default_extension = string()) const;
00085   BLOCKING int find_all_files(const Filename &filename, const DSearchPath &searchpath,
00086                               DSearchPath::Results &results) const;
00087 
00088   BLOCKING INLINE bool exists(const Filename &filename) const;
00089   BLOCKING INLINE bool is_directory(const Filename &filename) const;
00090   BLOCKING INLINE bool is_regular_file(const Filename &filename) const;
00091 
00092   BLOCKING INLINE PT(VirtualFileList) scan_directory(const Filename &filename) const;
00093 
00094   INLINE void ls(const Filename &filename) const;
00095   INLINE void ls_all(const Filename &filename) const;
00096 
00097   void write(ostream &out) const;
00098 
00099   static VirtualFileSystem *get_global_ptr();
00100 
00101 #ifdef HAVE_PYTHON
00102   BLOCKING PyObject *__py__read_file(const Filename &filename, bool auto_unwrap) const;
00103 #endif  // HAVE_PYTHON
00104   BLOCKING INLINE string read_file(const Filename &filename, bool auto_unwrap) const;
00105 
00106   BLOCKING istream *open_read_file(const Filename &filename, bool auto_unwrap) const;
00107   BLOCKING static void close_read_file(istream *stream);
00108 
00109 #ifdef HAVE_PYTHON
00110   BLOCKING PyObject *__py__write_file(const Filename &filename, PyObject *data, bool auto_wrap);
00111 #endif  // HAVE_PYTHON
00112   BLOCKING INLINE bool write_file(const Filename &filename, const string &data, bool auto_wrap);
00113   BLOCKING ostream *open_write_file(const Filename &filename, bool auto_wrap, bool truncate);
00114   BLOCKING ostream *open_append_file(const Filename &filename);
00115   BLOCKING static void close_write_file(ostream *stream);
00116 
00117   BLOCKING iostream *open_read_write_file(const Filename &filename, bool truncate);
00118   BLOCKING iostream *open_read_append_file(const Filename &filename);
00119   BLOCKING static void close_read_write_file(iostream *stream);
00120 
00121 public:
00122   bool atomic_compare_and_exchange_contents(const Filename &filename, string &orig_contents, const string &old_contents, const string &new_contents);
00123   bool atomic_read_contents(const Filename &filename, string &contents) const;
00124 
00125   INLINE bool read_file(const Filename &filename, string &result, bool auto_unwrap) const;
00126   INLINE bool read_file(const Filename &filename, pvector<unsigned char> &result, bool auto_unwrap) const;
00127   INLINE bool write_file(const Filename &filename, const unsigned char *data, size_t data_size, bool auto_wrap);
00128 
00129   void scan_mount_points(vector_string &names, const Filename &path) const;
00130  
00131   static void parse_options(const string &options,
00132                             int &flags, string &password);
00133   static void parse_option(const string &option,
00134                           int &flags, string &password);
00135 
00136 public:
00137   // These flags are passed to do_get_file() and
00138   // VirtualFileMount::make_virtual_file() to quality the kind of
00139   // VirtualFile pointer we want to get.
00140   enum OpenFlags {
00141     OF_status_only    = 0x0001,
00142     OF_create_file    = 0x0002,
00143     OF_make_directory = 0x0004,
00144     OF_allow_nonexist = 0x0008,
00145   };
00146 
00147   // These are declared as class instances, instead of as globals, to
00148   // guarantee they will be initialized by the time the
00149   // VirtualFileSystem's constructor runs.
00150   ConfigVariableBool vfs_case_sensitive;
00151   ConfigVariableBool vfs_implicit_pz;
00152   ConfigVariableBool vfs_implicit_mf;
00153 
00154 private:
00155   Filename normalize_mount_point(const Filename &mount_point) const;
00156   bool do_mount(VirtualFileMount *mount, const Filename &mount_point, int flags);
00157   PT(VirtualFile) do_get_file(const Filename &filename, int open_flags) const;
00158 
00159   bool consider_match(PT(VirtualFile) &found_file, VirtualFileComposite *&composite_file,
00160                       VirtualFileMount *mount, const Filename &local_filename,
00161                       const Filename &original_filename, bool implicit_pz_file,
00162                       int open_flags) const;
00163   bool consider_mount_mf(const Filename &filename);
00164 
00165   MutexImpl _lock;
00166   typedef pvector<PT(VirtualFileMount) > Mounts;
00167   Mounts _mounts;
00168   unsigned int _mount_seq;
00169 
00170   Filename _cwd;
00171 
00172   static VirtualFileSystem *_global_ptr;
00173 };
00174 
00175 #include "virtualFileSystem.I"
00176 
00177 #endif
 All Classes Functions Variables Enumerations