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 
00072   BLOCKING PT(VirtualFile) get_file(const Filename &filename, bool status_only = false) const;
00073   BLOCKING PT(VirtualFile) find_file(const Filename &filename, 
00074                                      const DSearchPath &searchpath,
00075                                      bool status_only = false) const;
00076   BLOCKING bool resolve_filename(Filename &filename, const DSearchPath &searchpath,
00077                                  const string &default_extension = string()) const;
00078   BLOCKING int find_all_files(const Filename &filename, const DSearchPath &searchpath,
00079                               DSearchPath::Results &results) const;
00080 
00081   BLOCKING INLINE bool exists(const Filename &filename) const;
00082   BLOCKING INLINE bool is_directory(const Filename &filename) const;
00083   BLOCKING INLINE bool is_regular_file(const Filename &filename) const;
00084 
00085   BLOCKING INLINE PT(VirtualFileList) scan_directory(const Filename &filename) const;
00086 
00087   INLINE void ls(const Filename &filename) const;
00088   INLINE void ls_all(const Filename &filename) const;
00089 
00090   void write(ostream &out) const;
00091 
00092   static VirtualFileSystem *get_global_ptr();
00093 
00094 #ifdef HAVE_PYTHON
00095   BLOCKING PyObject *__py__read_file(const Filename &filename, bool auto_unwrap) const;
00096 #endif  // HAVE_PYTHON
00097   BLOCKING INLINE string read_file(const Filename &filename, bool auto_unwrap) const;
00098 
00099   BLOCKING istream *open_read_file(const Filename &filename, bool auto_unwrap) const;
00100   BLOCKING static void close_read_file(istream *stream);
00101 
00102 public:
00103   INLINE bool read_file(const Filename &filename, string &result, bool auto_unwrap) const;
00104   INLINE bool read_file(const Filename &filename, pvector<unsigned char> &result, bool auto_unwrap) const;
00105 
00106   void scan_mount_points(vector_string &names, const Filename &path) const;
00107 
00108   static void parse_option(const string &option,
00109                            int &flags, string &password);
00110 
00111 public:
00112   // These are declared as class instances, instead of as globals, to
00113   // guarantee they will be initialized by the time the
00114   // VirtualFileSystem's constructor runs.
00115   ConfigVariableBool vfs_case_sensitive;
00116   ConfigVariableBool vfs_implicit_pz;
00117   ConfigVariableBool vfs_implicit_mf;
00118 
00119 private:
00120   Filename normalize_mount_point(const Filename &mount_point) const;
00121   bool do_mount(VirtualFileMount *mount, const Filename &mount_point, int flags);
00122   PT(VirtualFile) do_get_file(const Filename &filename, bool status_only) const;
00123   bool consider_match(PT(VirtualFile) &found_file, VirtualFileComposite *&composite_file,
00124                       VirtualFileMount *mount, const Filename &local_filename,
00125                       const Filename &original_filename, bool implicit_pz_file,
00126                       bool status_only) const;
00127   bool consider_mount_mf(const Filename &filename);
00128 
00129   MutexImpl _lock;
00130   typedef pvector<PT(VirtualFileMount) > Mounts;
00131   Mounts _mounts;
00132   unsigned int _mount_seq;
00133 
00134   Filename _cwd;
00135 
00136   static VirtualFileSystem *_global_ptr;
00137 };
00138 
00139 #include "virtualFileSystem.I"
00140 
00141 #endif
 All Classes Functions Variables Enumerations