diff options
Diffstat (limited to 'runtime/shims/file_system.cpp')
| -rw-r--r-- | runtime/shims/file_system.cpp | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index b81adb31f..014514597 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp | |||
| @@ -5,7 +5,8 @@ | |||
| 5 | // DeleteFile*, CreateDirectory*/RemoveDirectory*, MoveFile*, CopyFile*/CopyFile2 and SetFileAttributes* are answered | 5 | // DeleteFile*, CreateDirectory*/RemoveDirectory*, MoveFile*, CopyFile*/CopyFile2 and SetFileAttributes* are answered |
| 6 | // here (docs/runtime_imports.md, "Console paths and the write overlay"; the prefixes come from ivall/xcl, | 6 | // here (docs/runtime_imports.md, "Console paths and the write overlay"; the prefixes come from ivall/xcl, |
| 7 | // GPL-2.0-only, as facts). The rules: | 7 | // GPL-2.0-only, as facts). The rules: |
| 8 | // - "GAME:" (any case, "\\" or "/") maps under --game-dir; any other path is a host path and goes to the host; | 8 | // - "GAME:" (any case, "\\" or "/") maps under --game-dir, "G:\\" is the same folder and "T:\\" a scratch folder in |
| 9 | // the overlay (RT-218); any other path is a host path and goes to the host; | ||
| 9 | // - an open that can write, and DeleteFile, go to the overlay (--overlay, default <game-dir>\\overlay), so the | 10 | // - an open that can write, and DeleteFile, go to the overlay (--overlay, default <game-dir>\\overlay), so the |
| 10 | // package stays exactly as installed. A write-open of a package file copies it into the overlay first, so | 11 | // package stays exactly as installed. A write-open of a package file copies it into the overlay first, so |
| 11 | // OPEN_EXISTING keeps its bytes; a delete of a package file leaves an empty marker under | 12 | // OPEN_EXISTING keeps its bytes; a delete of a package file leaves an empty marker under |
| @@ -42,6 +43,7 @@ namespace { | |||
| 42 | constexpr wchar_t kGamePrefix[] = L"game:"; | 43 | constexpr wchar_t kGamePrefix[] = L"game:"; |
| 43 | constexpr size_t kGamePrefixLength = 5; | 44 | constexpr size_t kGamePrefixLength = 5; |
| 44 | constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; | 45 | constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; |
| 46 | constexpr wchar_t kScratchDir[] = L".gta6rt-temp"; // what "T:\" names, inside the overlay | ||
| 45 | // A removed package folder is marked by this file inside its marker folder: the marker folder itself can already | 47 | // A removed package folder is marked by this file inside its marker folder: the marker folder itself can already |
| 46 | // exist because files below it were deleted, and that alone does not hide the folder. | 48 | // exist because files below it were deleted, and that alone does not hide the folder. |
| 47 | constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; | 49 | constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; |
| @@ -49,7 +51,9 @@ constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; | |||
| 49 | constexpr wchar_t kCopyDir[] = L".gta6rt-copy"; | 51 | constexpr wchar_t kCopyDir[] = L".gta6rt-copy"; |
| 50 | 52 | ||
| 51 | // The overlay root's own folders, which a title never sees in a listing. | 53 | // The overlay root's own folders, which a title never sees in a listing. |
| 52 | bool internal_dir(const wchar_t* name) { return _wcsicmp(name, kDeletedDir) == 0 || _wcsicmp(name, kCopyDir) == 0; } | 54 | bool internal_dir(const wchar_t* name) { |
| 55 | return _wcsicmp(name, kDeletedDir) == 0 || _wcsicmp(name, kCopyDir) == 0 || _wcsicmp(name, kScratchDir) == 0; | ||
| 56 | } | ||
| 53 | 57 | ||
| 54 | std::mutex g_mutex; | 58 | std::mutex g_mutex; |
| 55 | std::filesystem::path g_game_dir; | 59 | std::filesystem::path g_game_dir; |
| @@ -96,9 +100,48 @@ enum class Parsed { Host, Console, Refused }; | |||
| 96 | 100 | ||
| 97 | // Splits a console path into the components below the root. A component that names a drive or a stream ("C:", "x:y") | 101 | // Splits a console path into the components below the root. A component that names a drive or a stream ("C:", "x:y") |
| 98 | // and a ".." that would leave the root are refused: the package and the overlay are the only folders a title may name. | 102 | // and a ".." that would leave the root are refused: the package and the overlay are the only folders a title may name. |
| 103 | bool is_separator(wchar_t c) { return c == L'\\' || c == L'/'; } | ||
| 104 | |||
| 105 | // True when `path` is `root` or lies below it, compared by name (case-insensitive, either separator). | ||
| 106 | bool lies_under(const std::wstring& path, const std::filesystem::path& root) { | ||
| 107 | std::wstring base = lower(root.native()); | ||
| 108 | for (auto& c : base) if (c == L'/') c = L'\\'; | ||
| 109 | while (!base.empty() && base.back() == L'\\') base.pop_back(); | ||
| 110 | if (base.empty()) return false; | ||
| 111 | std::wstring p = lower(path); | ||
| 112 | for (auto& c : p) if (c == L'/') c = L'\\'; | ||
| 113 | return p.compare(0, base.size(), base) == 0 && (p.size() == base.size() || p[base.size()] == L'\\'); | ||
| 114 | } | ||
| 115 | |||
| 116 | // "G:\x" is the installed package, like "GAME:\x", and "T:\x" a scratch folder the title may write to, the way the | ||
| 117 | // other console layers map those two drives (RT-218). Only the executable can say which spelling the title uses, so | ||
| 118 | // a host drive of the same letter must keep working: a path under --game-dir or the overlay (a package on drive G:, | ||
| 119 | // or a path GetModuleFileName handed back) stays a host path, and so does every G:/T: path while no roots are | ||
| 120 | // configured. The scratch folder lives in the overlay, as `.gta6rt-temp`. | ||
| 121 | enum class Drive { None, Game, Scratch }; | ||
| 122 | |||
| 123 | Drive console_drive(const std::wstring& path, size_t& prefix_length) { | ||
| 124 | if (path.size() >= kGamePrefixLength && lower(path.substr(0, kGamePrefixLength)) == kGamePrefix) { | ||
| 125 | prefix_length = kGamePrefixLength; | ||
| 126 | return Drive::Game; | ||
| 127 | } | ||
| 128 | if (path.size() < 2 || path[1] != L':' || (path.size() > 2 && !is_separator(path[2]))) return Drive::None; | ||
| 129 | const wchar_t letter = static_cast<wchar_t>(std::towlower(path[0])); | ||
| 130 | if (letter != L'g' && letter != L't') return Drive::None; | ||
| 131 | const Roots r = roots(); | ||
| 132 | if (r.game.empty() && r.overlay.empty()) return Drive::None; | ||
| 133 | if (lies_under(path, r.game) || lies_under(path, r.overlay)) return Drive::None; | ||
| 134 | prefix_length = 2; | ||
| 135 | return letter == L'g' ? Drive::Game : Drive::Scratch; | ||
| 136 | } | ||
| 137 | |||
| 99 | Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) { | 138 | Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) { |
| 100 | if (path.size() < kGamePrefixLength || lower(path.substr(0, kGamePrefixLength)) != kGamePrefix) return Parsed::Host; | 139 | size_t prefix_length = 0; |
| 140 | const Drive drive = console_drive(path, prefix_length); | ||
| 141 | if (drive == Drive::None) return Parsed::Host; | ||
| 101 | parts.clear(); | 142 | parts.clear(); |
| 143 | if (drive == Drive::Scratch) parts.push_back(kScratchDir); | ||
| 144 | const size_t floor = parts.size(); // ".." may not climb out of the drive's own root | ||
| 102 | std::wstring part; | 145 | std::wstring part; |
| 103 | auto flush = [&]() -> bool { | 146 | auto flush = [&]() -> bool { |
| 104 | if (part.empty() || part == L".") { | 147 | if (part.empty() || part == L".") { |
| @@ -106,7 +149,7 @@ Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) | |||
| 106 | return true; | 149 | return true; |
| 107 | } | 150 | } |
| 108 | if (part == L"..") { | 151 | if (part == L"..") { |
| 109 | if (parts.empty()) return false; | 152 | if (parts.size() <= floor) return false; |
| 110 | parts.pop_back(); | 153 | parts.pop_back(); |
| 111 | } else if (part.find(L':') != std::wstring::npos) { | 154 | } else if (part.find(L':') != std::wstring::npos) { |
| 112 | return false; | 155 | return false; |
| @@ -116,9 +159,9 @@ Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) | |||
| 116 | part.clear(); | 159 | part.clear(); |
| 117 | return true; | 160 | return true; |
| 118 | }; | 161 | }; |
| 119 | for (size_t i = kGamePrefixLength; i < path.size(); ++i) { | 162 | for (size_t i = prefix_length; i < path.size(); ++i) { |
| 120 | const wchar_t c = path[i]; | 163 | const wchar_t c = path[i]; |
| 121 | if (c == L'\\' || c == L'/') { | 164 | if (is_separator(c)) { |
| 122 | if (!flush()) return Parsed::Refused; | 165 | if (!flush()) return Parsed::Refused; |
| 123 | } else { | 166 | } else { |
| 124 | part.push_back(c); | 167 | part.push_back(c); |