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