diff options
| author | aslan2142 <jan.trencansky@protonmail.com> | 2026-11-25 23:56:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-25 23:56:38 +0100 |
| commit | d34c426ef9f89e63c1261eb1d8a40bf8cc62a1db (patch) | |
| tree | 3d951f8968c9b17c27b79ba27e9eb02b658c50ac /runtime/shims/file_system.cpp | |
| parent | 6ef26a648d29f9466f6f3885318885039e598300 (diff) | |
| parent | c116c3fa28983444cd47feea0461e1e7eaac83b5 (diff) | |
Merge pull request #684 from aslan2142/t-drive-overlay
OLD-8161: restore old upstream PR 8161 - file_system: T: is a folder of each process's own in the overlay
Diffstat (limited to 'runtime/shims/file_system.cpp')
| -rw-r--r-- | runtime/shims/file_system.cpp | 107 |
1 files changed, 100 insertions, 7 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index dad6f37a8..b3cc6b219 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp | |||
| @@ -6,7 +6,8 @@ | |||
| 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, "G:\\" is the same folder and "T:\\" a scratch folder in | 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 | // the overlay (RT-218), <overlay>\\.gta6rt-temp\\<run>, a new one reserved by every process; any other path is a |
| 10 | // host path and goes to the host; | ||
| 10 | // - an open that can write, and DeleteFile, go to the overlay (--overlay, default <game-dir>\\overlay), so the | 11 | // - an open that can write, and DeleteFile, go to the overlay (--overlay, default <game-dir>\\overlay), so the |
| 11 | // package stays exactly as installed. A write-open of a package file copies it into the overlay first, so | 12 | // package stays exactly as installed. A write-open of a package file copies it into the overlay first, so |
| 12 | // OPEN_EXISTING keeps its bytes; a delete of a package file leaves an empty marker under | 13 | // OPEN_EXISTING keeps its bytes; a delete of a package file leaves an empty marker under |
| @@ -26,6 +27,7 @@ | |||
| 26 | #include <intrin.h> | 27 | #include <intrin.h> |
| 27 | 28 | ||
| 28 | #include <atomic> | 29 | #include <atomic> |
| 30 | #include <cstdio> | ||
| 29 | #include <cstring> | 31 | #include <cstring> |
| 30 | #include <cwchar> | 32 | #include <cwchar> |
| 31 | #include <cwctype> | 33 | #include <cwctype> |
| @@ -49,7 +51,7 @@ namespace { | |||
| 49 | constexpr wchar_t kGamePrefix[] = L"game:"; | 51 | constexpr wchar_t kGamePrefix[] = L"game:"; |
| 50 | constexpr size_t kGamePrefixLength = 5; | 52 | constexpr size_t kGamePrefixLength = 5; |
| 51 | constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; | 53 | constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; |
| 52 | constexpr wchar_t kScratchDir[] = L".gta6rt-temp"; // what "T:\" names, inside the overlay | 54 | constexpr wchar_t kScratchDir[] = L".gta6rt-temp"; // holds one folder per process; "T:\" names this process's |
| 53 | // A removed package folder is marked by this file inside its marker folder: the marker folder itself can already | 55 | // A removed package folder is marked by this file inside its marker folder: the marker folder itself can already |
| 54 | // exist because files below it were deleted, and that alone does not hide the folder. | 56 | // exist because files below it were deleted, and that alone does not hide the folder. |
| 55 | constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; | 57 | constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; |
| @@ -167,9 +169,68 @@ std::filesystem::path win32_find_path(const std::filesystem::path& path, const s | |||
| 167 | // other console layers map those two drives (RT-218). Only the executable can say which spelling the title uses, so | 169 | // other console layers map those two drives (RT-218). Only the executable can say which spelling the title uses, so |
| 168 | // a host drive of the same letter must keep working: a path under --game-dir or the overlay (a package on drive G:, | 170 | // a host drive of the same letter must keep working: a path under --game-dir or the overlay (a package on drive G:, |
| 169 | // or a path GetModuleFileName handed back) stays a host path, and so does every G:/T: path while no roots are | 171 | // or a path GetModuleFileName handed back) stays a host path, and so does every G:/T: path while no roots are |
| 170 | // configured. The scratch folder lives in the overlay, as `.gta6rt-temp`. | 172 | // configured. The scratch folder lives in the overlay, as `.gta6rt-temp\<run>`, one per process (scratch_folder()). |
| 171 | enum class Drive { None, Game, Scratch }; | 173 | enum class Drive { None, Game, Scratch }; |
| 172 | 174 | ||
| 175 | // The stem of this process's T: folder below `.gta6rt-temp`: the process's start time (UTC, to the millisecond) and | ||
| 176 | // its id, e.g. "20261123-181502-123-pid4242". On the console T: is the title's own temporary drive, empty at launch | ||
| 177 | // and shared with nothing; one folder that every run shared kept the files earlier runs left, so a title that makes a | ||
| 178 | // file there with CREATE_NEW (its crash dump and crash log, measured under Wine) got ERROR_FILE_EXISTS. The stem only | ||
| 179 | // makes the folder easy to find: a reused process id in the same millisecond gives the same stem, so the folder is | ||
| 180 | // reserved by reserve_unique_dir, which never hands out one that already existed. | ||
| 181 | const std::wstring& scratch_run() { | ||
| 182 | static const std::wstring name = [] { | ||
| 183 | const DWORD last_error = ::GetLastError(); | ||
| 184 | FILETIME created{}, exited{}, kernel{}, user{}; | ||
| 185 | SYSTEMTIME t{}; | ||
| 186 | if (!::GetProcessTimes(::GetCurrentProcess(), &created, &exited, &kernel, &user) || | ||
| 187 | !::FileTimeToSystemTime(&created, &t)) | ||
| 188 | ::GetSystemTime(&t); | ||
| 189 | wchar_t buffer[64]; | ||
| 190 | const unsigned long pid = ::GetCurrentProcessId(); | ||
| 191 | std::swprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), L"%04u%02u%02u-%02u%02u%02u-%03u-pid%lu", | ||
| 192 | unsigned{t.wYear}, unsigned{t.wMonth}, unsigned{t.wDay}, unsigned{t.wHour}, unsigned{t.wMinute}, | ||
| 193 | unsigned{t.wSecond}, unsigned{t.wMilliseconds}, pid); | ||
| 194 | ::SetLastError(last_error); | ||
| 195 | return std::wstring(buffer); | ||
| 196 | }(); | ||
| 197 | return name; | ||
| 198 | } | ||
| 199 | |||
| 200 | // The T: folder this process reserved in each overlay it was given (configure() may name several, as the test does), | ||
| 201 | // or the error that stopped the reservation. Decided once per overlay, under the mutex, so concurrent first T: calls | ||
| 202 | // get the same folder. | ||
| 203 | struct ScratchFolder { | ||
| 204 | std::wstring name; // empty: the reservation failed with `error` | ||
| 205 | DWORD error = 0; | ||
| 206 | }; | ||
| 207 | std::mutex g_scratch_mutex; | ||
| 208 | std::map<std::wstring, ScratchFolder> g_scratch_folders; // keyed by the overlay path, lowercased | ||
| 209 | |||
| 210 | // The name of this process's folder below <overlay>\.gta6rt-temp, reserved on the first call for that overlay; empty | ||
| 211 | // when it could not be reserved (logged once). With no overlay configured nothing is made and the stem is the name. | ||
| 212 | std::wstring scratch_folder(const std::filesystem::path& overlay) { | ||
| 213 | if (overlay.empty()) return scratch_run(); | ||
| 214 | std::lock_guard lock(g_scratch_mutex); | ||
| 215 | ScratchFolder& folder = g_scratch_folders[lower(overlay.native())]; | ||
| 216 | if (folder.name.empty() && folder.error == 0) { | ||
| 217 | const DWORD last_error = ::GetLastError(); | ||
| 218 | DWORD error = 0; | ||
| 219 | const std::filesystem::path reserved = reserve_unique_dir(overlay / kScratchDir, scratch_run(), error); | ||
| 220 | if (reserved.empty()) { | ||
| 221 | folder.error = error ? error : ERROR_CANNOT_MAKE; | ||
| 222 | std::fprintf(stderr, | ||
| 223 | "[rt] file_system: no T: folder could be reserved below %ls (Win32 error %lu); T: paths " | ||
| 224 | "fail with ERROR_INVALID_NAME\n", | ||
| 225 | (overlay / kScratchDir).c_str(), static_cast<unsigned long>(folder.error)); | ||
| 226 | } else { | ||
| 227 | folder.name = reserved.filename().native(); | ||
| 228 | } | ||
| 229 | ::SetLastError(last_error); | ||
| 230 | } | ||
| 231 | return folder.name; | ||
| 232 | } | ||
| 233 | |||
| 173 | Drive console_drive(const std::wstring& path, size_t& prefix_length) { | 234 | Drive console_drive(const std::wstring& path, size_t& prefix_length) { |
| 174 | if (path.size() >= kGamePrefixLength && lower(path.substr(0, kGamePrefixLength)) == kGamePrefix) { | 235 | if (path.size() >= kGamePrefixLength && lower(path.substr(0, kGamePrefixLength)) == kGamePrefix) { |
| 175 | prefix_length = kGamePrefixLength; | 236 | prefix_length = kGamePrefixLength; |
| @@ -191,11 +252,15 @@ Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) | |||
| 191 | if (drive == Drive::None) return Parsed::Host; | 252 | if (drive == Drive::None) return Parsed::Host; |
| 192 | parts.clear(); | 253 | parts.clear(); |
| 193 | if (drive == Drive::Scratch) { | 254 | if (drive == Drive::Scratch) { |
| 194 | parts.push_back(kScratchDir); | ||
| 195 | const std::filesystem::path overlay = roots().overlay; | 255 | const std::filesystem::path overlay = roots().overlay; |
| 256 | const std::wstring folder = scratch_folder(overlay); | ||
| 257 | if (folder.empty()) return Parsed::Refused; | ||
| 258 | parts.push_back(kScratchDir); | ||
| 259 | parts.push_back(folder); | ||
| 260 | // The folder is this process's from its reservation on; made again if something removed it since. | ||
| 196 | const DWORD last_error = ::GetLastError(); | 261 | const DWORD last_error = ::GetLastError(); |
| 197 | std::error_code ec; | 262 | std::error_code ec; |
| 198 | if (!overlay.empty()) std::filesystem::create_directories(win32_io_path(overlay / kScratchDir), ec); | 263 | if (!overlay.empty()) std::filesystem::create_directories(win32_io_path(overlay / kScratchDir / folder), ec); |
| 199 | ::SetLastError(last_error); | 264 | ::SetLastError(last_error); |
| 200 | } | 265 | } |
| 201 | const size_t floor = parts.size(); // ".." may not climb out of the drive's own root | 266 | const size_t floor = parts.size(); // ".." may not climb out of the drive's own root |
| @@ -227,7 +292,7 @@ Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) | |||
| 227 | if (!flush()) return Parsed::Refused; | 292 | if (!flush()) return Parsed::Refused; |
| 228 | // The overlay's own folders are not part of the title's GAME: tree: hidden from its listings (internal_dir) and | 293 | // The overlay's own folders are not part of the title's GAME: tree: hidden from its listings (internal_dir) and |
| 229 | // refused as an explicit path too, in every spelling the loop above normalises (case, slashes, "." and ".."). | 294 | // refused as an explicit path too, in every spelling the loop above normalises (case, slashes, "." and ".."). |
| 230 | // T: still names the scratch folder: its first part is pushed above, not parsed from the title's path. | 295 | // T: still names the scratch folder: its first two parts are pushed above, not parsed from the title's path. |
| 231 | if (drive == Drive::Game && !parts.empty() && internal_dir(parts.front().c_str())) return Parsed::Refused; | 296 | if (drive == Drive::Game && !parts.empty() && internal_dir(parts.front().c_str())) return Parsed::Refused; |
| 232 | return Parsed::Console; | 297 | return Parsed::Console; |
| 233 | } | 298 | } |
| @@ -244,7 +309,7 @@ std::filesystem::path disk_path(const std::filesystem::path& root, const std::ve | |||
| 244 | 309 | ||
| 245 | bool drive_root(const std::wstring& path, const std::vector<std::wstring>& parts) { | 310 | bool drive_root(const std::wstring& path, const std::vector<std::wstring>& parts) { |
| 246 | size_t prefix_length = 0; | 311 | size_t prefix_length = 0; |
| 247 | return parts.empty() || (parts.size() == 1 && console_drive(path, prefix_length) == Drive::Scratch); | 312 | return parts.empty() || (parts.size() == 2 && console_drive(path, prefix_length) == Drive::Scratch); |
| 248 | } | 313 | } |
| 249 | 314 | ||
| 250 | struct Target { | 315 | struct Target { |
| @@ -687,6 +752,34 @@ std::filesystem::path overlay_dir() { | |||
| 687 | return g_overlay_dir; | 752 | return g_overlay_dir; |
| 688 | } | 753 | } |
| 689 | 754 | ||
| 755 | std::filesystem::path scratch_dir() { | ||
| 756 | const std::filesystem::path overlay = overlay_dir(); | ||
| 757 | if (overlay.empty()) return {}; | ||
| 758 | const std::wstring folder = scratch_folder(overlay); | ||
| 759 | return folder.empty() ? std::filesystem::path() : overlay / kScratchDir / folder; | ||
| 760 | } | ||
| 761 | |||
| 762 | std::wstring scratch_stem() { return scratch_run(); } | ||
| 763 | |||
| 764 | std::filesystem::path reserve_unique_dir(const std::filesystem::path& base, const std::wstring& stem, DWORD& error, | ||
| 765 | unsigned max_tries) { | ||
| 766 | error = 0; | ||
| 767 | std::error_code ec; | ||
| 768 | std::filesystem::create_directories(win32_io_path(base), ec); | ||
| 769 | for (unsigned n = 1; n <= max_tries; ++n) { | ||
| 770 | const std::filesystem::path candidate = base / (n == 1 ? stem : stem + L"-" + std::to_wstring(n)); | ||
| 771 | // Create-only: success means no other process or run had this name, so the folder is new and ours. Through | ||
| 772 | // win32_io_path, as every other create here, so a long overlay root keeps working. | ||
| 773 | if (::CreateDirectoryW(win32_io_path(candidate).c_str(), nullptr)) { | ||
| 774 | error = 0; | ||
| 775 | return candidate; | ||
| 776 | } | ||
| 777 | error = ::GetLastError(); | ||
| 778 | if (error != ERROR_ALREADY_EXISTS) return {}; // another name will not get past this error | ||
| 779 | } | ||
| 780 | return {}; | ||
| 781 | } | ||
| 782 | |||
| 690 | // gta6rt --trace FILE: a failed open of a guest path goes to the startup trace as a "file_open_failed" event (the | 783 | // gta6rt --trace FILE: a failed open of a guest path goes to the startup trace as a "file_open_failed" event (the |
| 691 | // path the title passed, the Win32 error, the caller as module+RVA), so a boot that stops on a missing or refused | 784 | // path the title passed, the Win32 error, the caller as module+RVA), so a boot that stops on a missing or refused |
| 692 | // file names it. Failures only, and at most 256 of them, so a title probing many optional files can't fill the file. | 785 | // file names it. Failures only, and at most 256 of them, so a title probing many optional files can't fill the file. |