Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : /*
24 : * Virtual File System API - allows transparent access to files in
25 : * archives, modding via multiple mount points and hotloading.
26 : */
27 :
28 : #ifndef INCLUDED_VFS
29 : #define INCLUDED_VFS
30 :
31 : #include "lib/allocators/shared_ptr.h"
32 : #include "lib/file/file_system.h" // CFileInfo
33 : #include "lib/file/vfs/vfs_path.h"
34 : #include "lib/os_path.h"
35 : #include "lib/status.h"
36 :
37 : #include <cstddef>
38 : #include <limits>
39 : #include <memory>
40 : #include <span>
41 : #include <string>
42 :
43 : constexpr size_t VFS_MIN_PRIORITY = 0;
44 : constexpr size_t VFS_MAX_PRIORITY = std::numeric_limits<size_t>::max();
45 :
46 : namespace ERR
47 : {
48 : const Status VFS_DIR_NOT_FOUND = -110100;
49 : const Status VFS_FILE_NOT_FOUND = -110101;
50 : const Status VFS_ALREADY_MOUNTED = -110102;
51 : }
52 :
53 : // (recursive mounting and mounting archives are no longer optional since they don't hurt)
54 : enum VfsMountFlags
55 : {
56 : /**
57 : * all real directories mounted during this operation will be watched
58 : * for changes. this flag is provided to avoid watches in output-only
59 : * directories, e.g. screenshots/ (only causes unnecessary overhead).
60 : **/
61 : VFS_MOUNT_WATCH = 1,
62 :
63 : /**
64 : * anything mounted from here should be included when building archives.
65 : **/
66 : VFS_MOUNT_ARCHIVABLE = 2,
67 :
68 : /**
69 : * return ERR::VFS_DIR_NOT_FOUND if the given real path doesn't exist.
70 : * (the default behavior is to create all real directories in the path)
71 : **/
72 : VFS_MOUNT_MUST_EXIST = 4,
73 :
74 : /**
75 : * keep the files named "*.DELETED" visible in the VFS directories.
76 : * the standard behavior of hiding the file with the same name minus the
77 : * ".DELETED" suffix will still apply.
78 : * (the default behavior is to hide both the suffixed and unsuffixed files)
79 : **/
80 : VFS_MOUNT_KEEP_DELETED = 8
81 : };
82 :
83 : // (member functions are thread-safe after the instance has been
84 : // constructed - each acquires a mutex.)
85 143 : struct IVFS
86 : {
87 : virtual ~IVFS() {}
88 :
89 : /**
90 : * mount a directory into the VFS.
91 : *
92 : * @param mountPoint (will be created if it does not already exist)
93 : * @param path real directory path
94 : * @param flags
95 : * @param priority
96 : * @return Status.
97 : *
98 : * if files are encountered that already exist in the VFS (sub)directories,
99 : * the most recent / highest priority/precedence version is preferred.
100 : *
101 : * Note that the 'real directory' associated with a VFS Path
102 : * will be relative to the highest priority subdirectory in the path,
103 : * and that in case of equal priority, the order is _undefined_,
104 : * and will depend on the exact order of populate() calls.
105 : *
106 : * if files with archive extensions are seen, their contents are added
107 : * as well.
108 : **/
109 : virtual Status Mount(const VfsPath& mountPoint, const OsPath& path, size_t flags = 0, size_t priority = 0) = 0;
110 :
111 : /**
112 : * Retrieve information about a file (similar to POSIX stat).
113 : *
114 : * @param pathname
115 : * @param pfileInfo receives information about the file. Passing NULL
116 : * suppresses warnings if the file doesn't exist.
117 : *
118 : * @return Status.
119 : **/
120 : virtual Status GetFileInfo(const VfsPath& pathname, CFileInfo* pfileInfo) const = 0;
121 :
122 : /**
123 : * Retrieve mount priority for a file.
124 : *
125 : * @param pathname
126 : * @param ppriority receives priority value, if the file can be found.
127 : *
128 : * @return Status.
129 : **/
130 : virtual Status GetFilePriority(const VfsPath& pathname, size_t* ppriority) const = 0;
131 :
132 : /**
133 : * Retrieve lists of all files and subdirectories in a directory.
134 : *
135 : * @return Status.
136 : *
137 : * Rationale:
138 : * - this interface avoids having to lock the directory while an
139 : * iterator is extant.
140 : * - we cannot efficiently provide routines for returning files and
141 : * subdirectories separately due to the underlying POSIX interface.
142 : **/
143 : virtual Status GetDirectoryEntries(const VfsPath& path, CFileInfos* fileInfos, DirectoryNames* subdirectoryNames) const = 0;
144 :
145 : /**
146 : * Create a file with the given contents.
147 : * @param pathname
148 : * @param fileContents the pointer must be aligned to maxSectorSize
149 : * @return Status.
150 : **/
151 : virtual Status CreateFile(const VfsPath& pathname, std::span<const std::uint8_t> fileContents) = 0;
152 :
153 : /**
154 : * Read an entire file into memory.
155 : *
156 : * @param pathname
157 : * @param fileContents receives a smart pointer to the contents.
158 : * @param size receives the size [bytes] of the file contents.
159 : * @return Status.
160 : **/
161 : virtual Status LoadFile(const VfsPath& pathname, std::shared_ptr<std::uint8_t>& fileContents,
162 : size_t& size) = 0;
163 :
164 : /**
165 : * Read an entire file into memory.
166 : *
167 : * @param pathname
168 : * @param fileContents receives a unique pointer to the contents.
169 : * @param size receives the size [bytes] of the file contents.
170 : * @return Status.
171 : **/
172 : virtual Status LoadFile(const VfsPath& pathname,
173 : std::unique_ptr<std::uint8_t[], AlignedDeleter>& fileContents, size_t& size) = 0;
174 :
175 : /**
176 : * @return a string representation of all files and directories.
177 : **/
178 : virtual std::wstring TextRepresentation() const = 0;
179 :
180 : /**
181 : * Retrieve the POSIX pathname a VFS file was loaded from.
182 : * This is distinct from the current 'real' path, since that depends on the parent directory's real path,
183 : * which may have been overwritten by a mod or another call to Mount().
184 : *
185 : * This is used by the caching to split by mod, and you also ought to call this to delete a file.
186 : * (note that deleting has other issues, see below).
187 : **/
188 : virtual Status GetOriginalPath(const VfsPath& filename, OsPath& loadedPathname) = 0;
189 :
190 : /**
191 : * Retrieve the real (POSIX) pathname underlying a VFS file.
192 : * This is useful for passing paths to external libraries.
193 : * Note that this returns the real path relative to the highest priority VFS subpath.
194 : * @param createMissingDirectories - if true, create subdirectories on the file system as required.
195 : * (this defaults to true because, in general, this function is then used to create new files).
196 : **/
197 : virtual Status GetRealPath(const VfsPath& pathname, OsPath& realPathname, bool createMissingDirectories = true) = 0;
198 :
199 : /**
200 : * Retrieve the real (POSIX) pathname underlying a VFS directory.
201 : * This is useful for passing paths to external libraries.
202 : * Note that this returns the real path relative to the highest priority VFS subpath.
203 : * @param createMissingDirectories - if true, create subdirectories on the file system as required.
204 : * (this defaults to true because, in general, this function is then used to create new files).
205 : **/
206 : virtual Status GetDirectoryRealPath(const VfsPath& pathname, OsPath& realPathname, bool createMissingDirectories = true) = 0;
207 :
208 : /**
209 : * retrieve the VFS pathname that corresponds to a real file.
210 : *
211 : * this is useful for reacting to file change notifications.
212 : *
213 : * the current implementation requires time proportional to the
214 : * number of directories; this could be accelerated by only checking
215 : * directories below a mount point with a matching real path.
216 : **/
217 : virtual Status GetVirtualPath(const OsPath& realPathname, VfsPath& pathname) = 0;
218 :
219 : /**
220 : * remove file from the virtual directory listing.
221 : **/
222 : virtual Status RemoveFile(const VfsPath& pathname) = 0;
223 :
224 : /**
225 : * request the directory be re-populated when it is next accessed.
226 : * useful for synchronizing with the underlying filesystem after
227 : * files have been created or their metadata changed.
228 : **/
229 : virtual Status RepopulateDirectory(const VfsPath& path) = 0;
230 :
231 : /**
232 : * empty the contents of the filesystem.
233 : * this is typically only necessary when changing the set of
234 : * mounted directories, e.g. when switching mods.
235 : * NB: open files are not affected.
236 : **/
237 : virtual void Clear() = 0;
238 : };
239 :
240 : typedef std::shared_ptr<IVFS> PIVFS;
241 :
242 : /**
243 : * create an instance of a Virtual File System.
244 : *
245 : * note: there is no limitation to a single instance, it may make sense
246 : * to create and destroy VFS instances during each unit test.
247 : **/
248 : PIVFS CreateVfs();
249 :
250 : #endif // #ifndef INCLUDED_VFS
|