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 : * look up directories/files by traversing path components. 25 : */ 26 : 27 : #include "precompiled.h" 28 : 29 : #include "vfs_lookup.h" 30 : 31 : #include "lib/debug.h" 32 : #include "lib/file/common/real_directory.h" 33 : #include "lib/file/file.h" 34 : #include "lib/file/file_system.h" 35 : #include "lib/file/vfs/vfs.h" // error codes 36 : #include "lib/file/vfs/vfs_populate.h" 37 : #include "lib/file/vfs/vfs_tree.h" 38 : #include "lib/os_path.h" 39 : #include "lib/path.h" 40 : #include "lib/posix/posix_filesystem.h" 41 : 42 : #include <filesystem> 43 : #include <string> 44 : #include <system_error> 45 : 46 239 : static Status CreateDirectory(const OsPath& dirpath) 47 : { 48 239 : const std::filesystem::path path{dirpath.string()}; 49 239 : std::error_code ec{}; 50 : 51 239 : if (std::filesystem::is_directory(path)) 52 : return INFO::OK; 53 : 54 239 : std::filesystem::create_directory(path, ec); 55 239 : if (ec) 56 0 : return StatusFromSystemError(ec); 57 : 58 : // 0700 as prescribed by XDG basedir 59 239 : std::filesystem::permissions(path, std::filesystem::perms::owner_all, ec); 60 239 : return StatusFromSystemError(ec); 61 239 : } 62 : 63 : 64 83250 : Status vfs_Lookup(const VfsPath& pathname, VfsDirectory* startDirectory, VfsDirectory*& directory, VfsFile** pfile, size_t flags) 65 : { 66 : // extract and validate flags (ensure no unknown bits are set) 67 83250 : const bool addMissingDirectories = (flags & VFS_LOOKUP_ADD) != 0; 68 83250 : const bool skipPopulate = (flags & VFS_LOOKUP_SKIP_POPULATE) != 0; 69 83250 : const bool realPath = (flags & VFS_LOOKUP_REAL_PATH) != 0; 70 83250 : ENSURE((flags & ~(VFS_LOOKUP_ADD|VFS_LOOKUP_SKIP_POPULATE|VFS_LOOKUP_REAL_PATH)) == 0); 71 : 72 83250 : directory = startDirectory; 73 83250 : if (pfile) 74 80177 : *pfile = 0; 75 : 76 83250 : if (!skipPopulate) 77 82980 : RETURN_STATUS_IF_ERR(vfs_Populate(directory)); 78 : 79 : // early-out for pathname == "" when mounting into VFS root 80 83250 : if (pathname.empty()) // (prevent iterator error in loop end condition) 81 : { 82 : // Preserve a guarantee that if pfile then we either return an error or set *pfile, 83 : // and if looking for a real path ensure an associated directory. 84 166 : if (pfile || (realPath && !directory->AssociatedDirectory())) 85 : return ERR::VFS_FILE_NOT_FOUND; 86 : else 87 : return INFO::OK; 88 : } 89 : 90 : // for each directory component: 91 : size_t pos = 0; // (needed outside of loop) 92 346143 : for(;;) 93 : { 94 346143 : const size_t nextSlash = pathname.string().find_first_of('/', pos); 95 346143 : if (nextSlash == VfsPath::String::npos) 96 : break; 97 268416 : const VfsPath subdirectoryName = pathname.string().substr(pos, nextSlash-pos); 98 268416 : pos = nextSlash+1; 99 : 100 268416 : VfsDirectory* subdirectory = directory->GetSubdirectory(subdirectoryName); 101 268416 : if (!subdirectory) 102 : { 103 5620 : if (addMissingDirectories) 104 263 : subdirectory = directory->AddSubdirectory(subdirectoryName); 105 : else 106 : return ERR::VFS_DIR_NOT_FOUND; // NOWARN 107 : } 108 : // When looking for a real path, we need to keep the path of the highest priority subdirectory. 109 : // If the current directory has an associated directory, and the subdir does not / is lower priority, 110 : // we will overwrite it. 111 263059 : PRealDirectory realDir = directory->AssociatedDirectory(); 112 263059 : if (realPath && realDir && 113 11561 : (!subdirectory->AssociatedDirectory() || 114 11331 : realDir->Priority() > subdirectory->AssociatedDirectory()->Priority())) 115 : { 116 239 : OsPath currentPath = directory->AssociatedDirectory()->Path(); 117 956 : currentPath = currentPath / subdirectoryName / ""; 118 : 119 : // Only actually create the directory if we're in LOOKUP_ADD mode. 120 239 : if (addMissingDirectories) 121 239 : RETURN_STATUS_IF_ERR(CreateDirectory(currentPath)); 122 0 : else if (!DirectoryExists(currentPath)) 123 : return ERR::VFS_DIR_NOT_FOUND; 124 : 125 : // Propagate priority and flags to the subdirectory. 126 : // If it already existed, it will be replaced & the memory freed. 127 239 : PRealDirectory realDirectory(new RealDirectory(currentPath, 128 239 : realDir ? realDir->Priority() : 0, 129 239 : realDir ? realDir->Flags() : 0) 130 239 : ); 131 239 : RETURN_STATUS_IF_ERR(vfs_Attach(subdirectory, realDirectory)); 132 478 : } 133 : 134 263059 : if (!skipPopulate) 135 263015 : RETURN_STATUS_IF_ERR(vfs_Populate(subdirectory)); 136 : 137 263059 : directory = subdirectory; 138 534253 : } 139 : 140 77727 : if (realPath && !directory->AssociatedDirectory()) 141 : return ERR::VFS_DIR_NOT_FOUND; 142 : 143 77727 : if (pfile) 144 : { 145 74891 : ENSURE(!pathname.IsDirectory()); 146 126891 : const VfsPath filename = pathname.string().substr(pos); 147 74891 : *pfile = directory->GetFile(filename); 148 74891 : if (!*pfile) 149 45781 : return ERR::VFS_FILE_NOT_FOUND; // NOWARN 150 : } 151 : 152 : return INFO::OK; 153 : }