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 : #include "precompiled.h"
24 :
25 : #include "vfs.h"
26 :
27 : #include "lib/alignment.h"
28 : #include "lib/allocators/shared_ptr.h"
29 : #include "lib/code_annotation.h"
30 : #include "lib/debug.h"
31 : #include "lib/file/common/file_loader.h"
32 : #include "lib/file/common/file_stats.h"
33 : #include "lib/file/common/real_directory.h"
34 : #include "lib/file/common/trace.h"
35 : #include "lib/file/file.h"
36 : #include "lib/file/file_system.h"
37 : #include "lib/file/vfs/vfs_lookup.h"
38 : #include "lib/file/vfs/vfs_populate.h"
39 : #include "lib/file/vfs/vfs_tree.h"
40 : #include "lib/path.h"
41 :
42 : #include <ctime>
43 : #include <map>
44 : #include <mutex>
45 : #include <utility>
46 :
47 : static const StatusDefinition vfsStatusDefinitions[] = {
48 : { ERR::VFS_DIR_NOT_FOUND, L"VFS directory not found" },
49 : { ERR::VFS_FILE_NOT_FOUND, L"VFS file not found" },
50 : { ERR::VFS_ALREADY_MOUNTED, L"VFS path already mounted" }
51 : };
52 : STATUS_ADD_DEFINITIONS(vfsStatusDefinitions);
53 :
54 : static std::mutex vfs_mutex;
55 :
56 143 : class VFS final : public IVFS
57 : {
58 : public:
59 143 : VFS() : m_trace(CreateDummyTrace(8*MiB))
60 : {
61 143 : }
62 :
63 260 : Status Mount(const VfsPath& mountPoint, const OsPath& path, size_t flags /* = 0 */, size_t priority /* = 0 */) final
64 : {
65 260 : ENSURE(path.IsDirectory());
66 :
67 520 : std::lock_guard<std::mutex> lock(vfs_mutex);
68 260 : if(!DirectoryExists(path))
69 : {
70 101 : if(flags & VFS_MOUNT_MUST_EXIST)
71 : return ERR::VFS_DIR_NOT_FOUND; // NOWARN
72 : else
73 101 : RETURN_STATUS_IF_ERR(CreateDirectories(path, 0700));
74 : }
75 :
76 260 : VfsDirectory* directory;
77 260 : WARN_RETURN_STATUS_IF_ERR(vfs_Lookup(mountPoint, &m_rootDirectory, directory, 0, VFS_LOOKUP_ADD|VFS_LOOKUP_SKIP_POPULATE));
78 :
79 520 : PRealDirectory realDirectory(new RealDirectory(path, priority, flags));
80 260 : RETURN_STATUS_IF_ERR(vfs_Attach(directory, realDirectory));
81 : return INFO::OK;
82 : }
83 :
84 48153 : Status GetFileInfo(const VfsPath& pathname, CFileInfo* pfileInfo) const final
85 : {
86 48153 : std::lock_guard<std::mutex> lock(vfs_mutex);
87 48153 : VfsDirectory* directory;
88 48153 : VfsFile* file;
89 :
90 48153 : Status ret = vfs_Lookup(pathname, &m_rootDirectory, directory, &file);
91 48153 : if(!pfileInfo) // just indicate if the file exists without raising warnings.
92 : return ret;
93 8889 : WARN_RETURN_STATUS_IF_ERR(ret);
94 17778 : *pfileInfo = CFileInfo(file->Name(), file->Size(), file->MTime());
95 8889 : return INFO::OK;
96 48153 : }
97 :
98 9017 : Status GetFilePriority(const VfsPath& pathname, size_t* ppriority) const final
99 : {
100 9017 : std::lock_guard<std::mutex> lock(vfs_mutex);
101 9017 : VfsDirectory* directory; VfsFile* file;
102 9017 : RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file));
103 0 : *ppriority = file->Priority();
104 0 : return INFO::OK;
105 9017 : }
106 :
107 682 : Status GetDirectoryEntries(const VfsPath& path, CFileInfos* fileInfos, DirectoryNames* subdirectoryNames) const final
108 : {
109 682 : std::lock_guard<std::mutex> lock(vfs_mutex);
110 682 : VfsDirectory* directory;
111 682 : RETURN_STATUS_IF_ERR(vfs_Lookup(path, &m_rootDirectory, directory, 0));
112 :
113 609 : if(fileInfos)
114 : {
115 604 : const VfsDirectory::VfsFiles& files = directory->Files();
116 604 : fileInfos->clear();
117 604 : fileInfos->reserve(files.size());
118 13822 : for(VfsDirectory::VfsFiles::const_iterator it = files.begin(); it != files.end(); ++it)
119 : {
120 13218 : const VfsFile& file = it->second;
121 13218 : fileInfos->push_back(CFileInfo(file.Name(), file.Size(), file.MTime()));
122 : }
123 : }
124 :
125 609 : if(subdirectoryNames)
126 : {
127 241 : const VfsDirectory::VfsSubdirectories& subdirectories = directory->Subdirectories();
128 241 : subdirectoryNames->clear();
129 241 : subdirectoryNames->reserve(subdirectories.size());
130 456 : for(VfsDirectory::VfsSubdirectories::const_iterator it = subdirectories.begin(); it != subdirectories.end(); ++it)
131 215 : subdirectoryNames->push_back(it->first);
132 : }
133 :
134 : return INFO::OK;
135 682 : }
136 :
137 2108 : Status CreateFile(const VfsPath& pathname, std::span<const std::uint8_t> fileContents) final
138 : {
139 2108 : std::lock_guard<std::mutex> lock(vfs_mutex);
140 2108 : VfsDirectory* directory;
141 2108 : Status st;
142 2108 : st = vfs_Lookup(pathname, &m_rootDirectory, directory, 0, VFS_LOOKUP_ADD|VFS_LOOKUP_REAL_PATH);
143 2108 : if (st == ERR::FILE_ACCESS)
144 : return ERR::FILE_ACCESS;
145 :
146 2108 : WARN_RETURN_STATUS_IF_ERR(st);
147 :
148 2108 : ENSURE(IsAligned(fileContents.data(), maxSectorSize));
149 :
150 4216 : const PRealDirectory& realDirectory = directory->AssociatedDirectory();
151 4216 : const OsPath name = pathname.Filename();
152 2108 : RETURN_STATUS_IF_ERR(realDirectory->Store(name, fileContents));
153 :
154 2108 : const VfsFile file(name, fileContents.size(), time(0), realDirectory->Priority(), realDirectory);
155 2108 : directory->AddFile(file);
156 :
157 2108 : m_trace->NotifyStore(pathname, fileContents.size());
158 2108 : return INFO::OK;
159 2108 : }
160 :
161 6843 : Status LoadFile(const VfsPath& pathname, std::shared_ptr<std::uint8_t>& fileContents,
162 : size_t& size) final
163 : {
164 6843 : std::lock_guard<std::mutex> lock(vfs_mutex);
165 :
166 6843 : VfsDirectory* directory; VfsFile* file;
167 : // per 2010-05-01 meeting, this shouldn't raise 'scary error
168 : // dialogs', which might fail to display the culprit pathname
169 : // instead, callers should log the error, including pathname.
170 6843 : RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file));
171 :
172 13686 : fileContents = DummySharedPtr(static_cast<std::uint8_t*>(0));
173 6843 : size = file->Size();
174 :
175 6843 : RETURN_STATUS_IF_ERR(AllocateAligned(fileContents, size, maxSectorSize));
176 6843 : RETURN_STATUS_IF_ERR(file->Loader()->Load(file->Name(), {fileContents.get(), file->Size()}));
177 :
178 6843 : stats_io_user_request(size);
179 6843 : m_trace->NotifyLoad(pathname, size);
180 :
181 : return INFO::OK;
182 6843 : }
183 :
184 7256 : Status LoadFile(const VfsPath& pathname,
185 : std::unique_ptr<std::uint8_t[], AlignedDeleter>& fileContents, size_t& size) final
186 : {
187 7256 : std::lock_guard<std::mutex> lock(vfs_mutex);
188 :
189 7256 : VfsDirectory* directory; VfsFile* file;
190 : // per 2010-05-01 meeting, this shouldn't raise 'scary error
191 : // dialogs', which might fail to display the culprit pathname
192 : // instead, callers should log the error, including pathname.
193 7256 : RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file));
194 :
195 7245 : size = file->Size();
196 7245 : fileContents.reset(reinterpret_cast<std::uint8_t*>(rtl_AllocateAligned(size, maxSectorSize)));
197 7245 : if (!fileContents)
198 0 : WARN_RETURN(ERR::NO_MEM);
199 :
200 7245 : RETURN_STATUS_IF_ERR(file->Loader()->Load(file->Name(), {fileContents.get(), file->Size()}));
201 :
202 7245 : stats_io_user_request(size);
203 7245 : m_trace->NotifyLoad(pathname, size);
204 :
205 : return INFO::OK;
206 7256 : }
207 :
208 1 : std::wstring TextRepresentation() const final
209 : {
210 1 : std::lock_guard<std::mutex> lock(vfs_mutex);
211 1 : std::wstring textRepresentation;
212 1 : textRepresentation.reserve(100*KiB);
213 1 : DirectoryDescriptionR(textRepresentation, m_rootDirectory, 0);
214 1 : return textRepresentation;
215 1 : }
216 :
217 8877 : Status GetOriginalPath(const VfsPath& pathname, OsPath& realPathname) final
218 : {
219 8877 : std::lock_guard<std::mutex> lock(vfs_mutex);
220 8877 : VfsDirectory* directory; VfsFile* file;
221 8877 : WARN_RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file));
222 17754 : realPathname = file->Loader()->Path() / pathname.Filename();
223 8877 : return INFO::OK;
224 8877 : }
225 :
226 11 : Status GetRealPath(const VfsPath& pathname, OsPath& realPathname, bool createMissingDirectories) final
227 : {
228 11 : std::lock_guard<std::mutex> lock(vfs_mutex);
229 11 : VfsDirectory* directory; VfsFile* file;
230 11 : size_t flags = VFS_LOOKUP_REAL_PATH | (createMissingDirectories ? VFS_LOOKUP_ADD : 0);
231 11 : WARN_RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file, flags));
232 11 : ENSURE(directory->AssociatedDirectory());
233 24 : realPathname = directory->AssociatedDirectory()->Path() / pathname.Filename();
234 11 : return INFO::OK;
235 : }
236 :
237 9 : Status GetDirectoryRealPath(const VfsPath& pathname, OsPath& realPathname, bool createMissingDirectories) final
238 : {
239 9 : std::lock_guard<std::mutex> lock(vfs_mutex);
240 9 : VfsDirectory* directory;
241 9 : size_t flags = VFS_LOOKUP_REAL_PATH | (createMissingDirectories ? VFS_LOOKUP_ADD : 0);
242 9 : WARN_RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, NULL, flags));
243 9 : ENSURE(directory->AssociatedDirectory());
244 18 : realPathname = directory->AssociatedDirectory()->Path();
245 : return INFO::OK;
246 9 : }
247 :
248 9 : Status GetVirtualPath(const OsPath& realPathname, VfsPath& pathname) final
249 : {
250 9 : std::lock_guard<std::mutex> lock(vfs_mutex);
251 18 : const OsPath realPath = realPathname.Parent()/"";
252 9 : VfsPath path;
253 9 : RETURN_STATUS_IF_ERR(FindRealPathR(realPath, m_rootDirectory, L"", path));
254 18 : pathname = path / realPathname.Filename();
255 9 : return INFO::OK;
256 18 : }
257 :
258 12 : Status RemoveFile(const VfsPath& pathname) final
259 : {
260 12 : std::lock_guard<std::mutex> lock(vfs_mutex);
261 :
262 12 : VfsDirectory* directory; VfsFile* file;
263 12 : RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file));
264 12 : directory->RemoveFile(file->Name());
265 :
266 : return INFO::OK;
267 12 : }
268 :
269 12 : Status RepopulateDirectory(const VfsPath& path) final
270 : {
271 12 : std::lock_guard<std::mutex> lock(vfs_mutex);
272 :
273 12 : VfsDirectory* directory;
274 12 : RETURN_STATUS_IF_ERR(vfs_Lookup(path, &m_rootDirectory, directory, 0));
275 12 : directory->RequestRepopulate();
276 :
277 : return INFO::OK;
278 12 : }
279 :
280 0 : void Clear() final
281 : {
282 0 : std::lock_guard<std::mutex> lock(vfs_mutex);
283 0 : m_rootDirectory.Clear();
284 0 : }
285 :
286 : private:
287 9 : Status FindRealPathR(const OsPath& realPath, const VfsDirectory& directory, const VfsPath& curPath, VfsPath& path)
288 : {
289 9 : PRealDirectory realDirectory = directory.AssociatedDirectory();
290 9 : if(realDirectory && realDirectory->Path() == realPath)
291 : {
292 9 : path = curPath;
293 : return INFO::OK;
294 : }
295 :
296 0 : const VfsDirectory::VfsSubdirectories& subdirectories = directory.Subdirectories();
297 0 : for(VfsDirectory::VfsSubdirectories::const_iterator it = subdirectories.begin(); it != subdirectories.end(); ++it)
298 : {
299 0 : const OsPath& subdirectoryName = it->first;
300 0 : const VfsDirectory& subdirectory = it->second;
301 0 : Status ret = FindRealPathR(realPath, subdirectory, curPath / subdirectoryName/"", path);
302 0 : if(ret == INFO::OK)
303 9 : return INFO::OK;
304 : }
305 :
306 : return ERR::PATH_NOT_FOUND; // NOWARN
307 9 : }
308 :
309 : PITrace m_trace;
310 : mutable VfsDirectory m_rootDirectory;
311 : };
312 :
313 : //-----------------------------------------------------------------------------
314 :
315 143 : PIVFS CreateVfs()
316 : {
317 143 : return PIVFS(new VFS());
318 : }
|