Panda3D
 All Classes Functions Variables Enumerations
virtualFileSystem.h
1 // Filename: virtualFileSystem.h
2 // Created by: drose (03Aug02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef VIRTUALFILESYSTEM_H
16 #define VIRTUALFILESYSTEM_H
17 
18 #include "pandabase.h"
19 
20 #include "virtualFile.h"
21 #include "virtualFileMount.h"
22 #include "virtualFileList.h"
23 #include "filename.h"
24 #include "dSearchPath.h"
25 #include "pointerTo.h"
26 #include "config_express.h"
27 #include "mutexImpl.h"
28 #include "pvector.h"
29 
30 class Multifile;
32 
33 ////////////////////////////////////////////////////////////////////
34 // Class : VirtualFileSystem
35 // Description : A hierarchy of directories and files that appears to
36 // be one continuous file system, even though the files
37 // may originate from several different sources that may
38 // not be related to the actual OS's file system.
39 //
40 // For instance, a VirtualFileSystem can transparently
41 // mount one or more Multifiles as their own
42 // subdirectory hierarchies.
43 ////////////////////////////////////////////////////////////////////
44 class EXPCL_PANDAEXPRESS VirtualFileSystem {
45 PUBLISHED:
48 
49  enum MountFlags {
50  MF_read_only = 0x0002,
51  };
52 
53  BLOCKING bool mount(Multifile *multifile, const Filename &mount_point, int flags);
54  BLOCKING bool mount(const Filename &physical_filename, const Filename &mount_point,
55  int flags, const string &password = "");
56  BLOCKING bool mount_loop(const Filename &virtual_filename, const Filename &mount_point,
57  int flags, const string &password = "");
58  bool mount(VirtualFileMount *mount, const Filename &mount_point, int flags);
59  BLOCKING int unmount(Multifile *multifile);
60  BLOCKING int unmount(const Filename &physical_filename);
61  int unmount(VirtualFileMount *mount);
62  BLOCKING int unmount_point(const Filename &mount_point);
63  BLOCKING int unmount_all();
64 
65  int get_num_mounts() const;
66  PT(VirtualFileMount) get_mount(int n) const;
67  MAKE_SEQ(get_mounts, get_num_mounts, get_mount);
68 
69  BLOCKING bool chdir(const Filename &new_directory);
70  BLOCKING Filename get_cwd() const;
71  BLOCKING bool make_directory(const Filename &filename);
72  BLOCKING bool make_directory_full(const Filename &filename);
73 
74  BLOCKING PT(VirtualFile) get_file(const Filename &filename, bool status_only = false) const;
75  BLOCKING PT(VirtualFile) create_file(const Filename &filename);
76  BLOCKING PT(VirtualFile) find_file(const Filename &filename,
77  const DSearchPath &searchpath,
78  bool status_only = false) const;
79  BLOCKING bool delete_file(const Filename &filename);
80  BLOCKING bool rename_file(const Filename &orig_filename, const Filename &new_filename);
81  BLOCKING bool copy_file(const Filename &orig_filename, const Filename &new_filename);
82 
83  BLOCKING bool resolve_filename(Filename &filename, const DSearchPath &searchpath,
84  const string &default_extension = string()) const;
85  BLOCKING int find_all_files(const Filename &filename, const DSearchPath &searchpath,
86  DSearchPath::Results &results) const;
87 
88  BLOCKING INLINE bool exists(const Filename &filename) const;
89  BLOCKING INLINE bool is_directory(const Filename &filename) const;
90  BLOCKING INLINE bool is_regular_file(const Filename &filename) const;
91 
92  BLOCKING INLINE PT(VirtualFileList) scan_directory(const Filename &filename) const;
93 
94  INLINE void ls(const Filename &filename) const;
95  INLINE void ls_all(const Filename &filename) const;
96 
97  void write(ostream &out) const;
98 
99  static VirtualFileSystem *get_global_ptr();
100 
101  EXTENSION(BLOCKING PyObject *read_file(const Filename &filename, bool auto_unwrap) const);
102  BLOCKING istream *open_read_file(const Filename &filename, bool auto_unwrap) const;
103  BLOCKING static void close_read_file(istream *stream);
104 
105  EXTENSION(BLOCKING PyObject *write_file(const Filename &filename, PyObject *data, bool auto_wrap));
106  BLOCKING ostream *open_write_file(const Filename &filename, bool auto_wrap, bool truncate);
107  BLOCKING ostream *open_append_file(const Filename &filename);
108  BLOCKING static void close_write_file(ostream *stream);
109 
110  BLOCKING iostream *open_read_write_file(const Filename &filename, bool truncate);
111  BLOCKING iostream *open_read_append_file(const Filename &filename);
112  BLOCKING static void close_read_write_file(iostream *stream);
113 
114 public:
115  // We provide Python versions of these as efficient extension methods, above.
116  BLOCKING INLINE string read_file(const Filename &filename, bool auto_unwrap) const;
117  BLOCKING INLINE bool write_file(const Filename &filename, const string &data, bool auto_wrap);
118 
119  bool atomic_compare_and_exchange_contents(const Filename &filename, string &orig_contents, const string &old_contents, const string &new_contents);
120  bool atomic_read_contents(const Filename &filename, string &contents) const;
121 
122  INLINE bool read_file(const Filename &filename, string &result, bool auto_unwrap) const;
123  INLINE bool read_file(const Filename &filename, pvector<unsigned char> &result, bool auto_unwrap) const;
124  INLINE bool write_file(const Filename &filename, const unsigned char *data, size_t data_size, bool auto_wrap);
125 
126  void scan_mount_points(vector_string &names, const Filename &path) const;
127 
128  static void parse_options(const string &options,
129  int &flags, string &password);
130  static void parse_option(const string &option,
131  int &flags, string &password);
132 
133 public:
134  // These flags are passed to do_get_file() and
135  // VirtualFileMount::make_virtual_file() to quality the kind of
136  // VirtualFile pointer we want to get.
137  enum OpenFlags {
138  OF_status_only = 0x0001,
139  OF_create_file = 0x0002,
140  OF_make_directory = 0x0004,
141  OF_allow_nonexist = 0x0008,
142  };
143 
144  // These are declared as class instances, instead of as globals, to
145  // guarantee they will be initialized by the time the
146  // VirtualFileSystem's constructor runs.
147  ConfigVariableBool vfs_case_sensitive;
148  ConfigVariableBool vfs_implicit_pz;
149  ConfigVariableBool vfs_implicit_mf;
150 
151 private:
152  Filename normalize_mount_point(const Filename &mount_point) const;
153  bool do_mount(VirtualFileMount *mount, const Filename &mount_point, int flags);
154  PT(VirtualFile) do_get_file(const Filename &filename, int open_flags) const;
155 
156  bool consider_match(PT(VirtualFile) &found_file, VirtualFileComposite *&composite_file,
157  VirtualFileMount *mount, const Filename &local_filename,
158  const Filename &original_filename, bool implicit_pz_file,
159  int open_flags) const;
160  bool consider_mount_mf(const Filename &filename);
161 
162  MutexImpl _lock;
164  Mounts _mounts;
165  unsigned int _mount_seq;
166 
167  Filename _cwd;
168 
169  static VirtualFileSystem *_global_ptr;
170 };
171 
172 #include "virtualFileSystem.I"
173 
174 #endif
A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS&#39;s file system.
This is a convenience class to specialize ConfigVariable as a boolean type.
The abstract base class for a file or directory within the VirtualFileSystem.
Definition: virtualFile.h:37
A list of VirtualFiles, as returned by VirtualFile::scan_directory().
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
The abstract base class for a mount definition used within a VirtualFileSystem.
A file that contains a set of files.
Definition: multifile.h:34
This class stores a list of directories that can be searched, in order, to locate a particular file...
Definition: dSearchPath.h:32
A fake mutex implementation for single-threaded applications that don&#39;t need any synchronization cont...
A composite directory within the VirtualFileSystem: this maps to more than one directory on different...