upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/file_system.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-23 13:25:37 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 06:54:37 -0500
commit1d37e033eb7094f922492de777ee8f1d5cc63b23 (patch)
tree4a0c81a67a8a5a8a7be60c23d45ef536d71f6a05 /runtime/shims/file_system.cpp
parent297ad8685486dc9523d492e8fce19ae4aa52429b (diff)
file_system: reserve T:'s folder with a create-only call, never reuse one
Xiiano's review of old upstream PR 8161: the folder name (start time to the millisecond plus process id) is not guaranteed unique, and earlier runs' folders are kept, so a reused id in the same millisecond would land in a stale folder and bring ERROR_FILE_EXISTS back. Under Wine every fresh run gets the same small ids (both run_tiny_game_dir runs here were pid 32). rt::fs::reserve_unique_dir(base, stem) makes base\stem with CreateDirectoryW and takes success as ownership; on ERROR_ALREADY_EXISTS it tries stem-2, stem-3, ... up to 1000 names, and fails with the Win32 error otherwise. T:'s folder is reserved once per overlay under a mutex (the first T: call or rt::fs::scratch_dir(), which the startup line calls), so concurrent first calls share it. A failed reservation is printed once and T: paths fail with ERROR_INVALID_NAME. rt::fs::scratch_stem() gives the first name tried. file_system_test: with stem and stem-2 taken, reserve_unique_dir makes stem-3, new and empty, then stem-4; a file with the next name counts as taken; max_tries names taken fail with ERROR_ALREADY_EXISTS; a missing base is made. T: with folders of this process's own stem and stem-2 already kept takes stem-3 and CREATE_NEW succeeds; eight threads making their first T: call at once reserve one folder. The second-process test stays. Restored from old upstream PR 8161 and ported onto the new main: the base folder and every candidate go through main's win32_io_path, like the other creates in file_system.cpp, so a long overlay root keeps working. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/file_system.cpp')
-rw-r--r--runtime/shims/file_system.cpp84
1 files changed, 72 insertions, 12 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp
index 97dad46df..3e4cd97be 100644
--- a/runtime/shims/file_system.cpp
+++ b/runtime/shims/file_system.cpp
@@ -6,8 +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), <overlay>\\.gta6rt-temp\\<run>, a new one for every process; any other path is a host 9// the overlay (RT-218), <overlay>\\.gta6rt-temp\\<run>, a new one reserved by every process; any other path is a
10// path and goes to the host; 10// host path and goes to the host;
11// - 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
12// 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
13// 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
@@ -27,6 +27,7 @@
27#include <intrin.h> 27#include <intrin.h>
28 28
29#include <atomic> 29#include <atomic>
30#include <cstdio>
30#include <cstring> 31#include <cstring>
31#include <cwchar> 32#include <cwchar>
32#include <cwctype> 33#include <cwctype>
@@ -167,15 +168,15 @@ 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 168// 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:, 169// 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 170// 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\<run>`, one per process (scratch_run()). 171// configured. The scratch folder lives in the overlay, as `.gta6rt-temp\<run>`, one per process (scratch_folder()).
171enum class Drive { None, Game, Scratch }; 172enum class Drive { None, Game, Scratch };
172 173
173// The name of this process's T: folder below `.gta6rt-temp`: the process's start time (UTC, to the millisecond) and 174// The stem of this process's T: folder below `.gta6rt-temp`: the process's start time (UTC, to the millisecond) and
174// its id, e.g. "20261123-181502-123-pid4242". On the console T: is the title's own temporary drive, empty at launch 175// its id, e.g. "20261123-181502-123-pid4242". On the console T: is the title's own temporary drive, empty at launch
175// and shared with nothing; one folder that every run shared kept the files earlier runs left, so a title that makes a 176// and shared with nothing; one folder that every run shared kept the files earlier runs left, so a title that makes a
176// file there with CREATE_NEW (its crash dump and crash log, measured under Wine) got ERROR_FILE_EXISTS. The start 177// file there with CREATE_NEW (its crash dump and crash log, measured under Wine) got ERROR_FILE_EXISTS. The stem only
177// time keeps a reused process id from landing in an old run's folder; the folders of earlier runs are left in place 178// makes the folder easy to find: a reused process id in the same millisecond gives the same stem, so the folder is
178// for whoever wants to read what those runs wrote. 179// reserved by reserve_unique_dir, which never hands out one that already existed.
179const std::wstring& scratch_run() { 180const std::wstring& scratch_run() {
180 static const std::wstring name = [] { 181 static const std::wstring name = [] {
181 const DWORD last_error = ::GetLastError(); 182 const DWORD last_error = ::GetLastError();
@@ -195,6 +196,40 @@ const std::wstring& scratch_run() {
195 return name; 196 return name;
196} 197}
197 198
199// The T: folder this process reserved in each overlay it was given (configure() may name several, as the test does),
200// or the error that stopped the reservation. Decided once per overlay, under the mutex, so concurrent first T: calls
201// get the same folder.
202struct ScratchFolder {
203 std::wstring name; // empty: the reservation failed with `error`
204 DWORD error = 0;
205};
206std::mutex g_scratch_mutex;
207std::map<std::wstring, ScratchFolder> g_scratch_folders; // keyed by the overlay path, lowercased
208
209// The name of this process's folder below <overlay>\.gta6rt-temp, reserved on the first call for that overlay; empty
210// when it could not be reserved (logged once). With no overlay configured nothing is made and the stem is the name.
211std::wstring scratch_folder(const std::filesystem::path& overlay) {
212 if (overlay.empty()) return scratch_run();
213 std::lock_guard lock(g_scratch_mutex);
214 ScratchFolder& folder = g_scratch_folders[lower(overlay.native())];
215 if (folder.name.empty() && folder.error == 0) {
216 const DWORD last_error = ::GetLastError();
217 DWORD error = 0;
218 const std::filesystem::path reserved = reserve_unique_dir(overlay / kScratchDir, scratch_run(), error);
219 if (reserved.empty()) {
220 folder.error = error ? error : ERROR_CANNOT_MAKE;
221 std::fprintf(stderr,
222 "[rt] file_system: no T: folder could be reserved below %ls (Win32 error %lu); T: paths "
223 "fail with ERROR_INVALID_NAME\n",
224 (overlay / kScratchDir).c_str(), static_cast<unsigned long>(folder.error));
225 } else {
226 folder.name = reserved.filename().native();
227 }
228 ::SetLastError(last_error);
229 }
230 return folder.name;
231}
232
198Drive console_drive(const std::wstring& path, size_t& prefix_length) { 233Drive console_drive(const std::wstring& path, size_t& prefix_length) {
199 if (path.size() >= kGamePrefixLength && lower(path.substr(0, kGamePrefixLength)) == kGamePrefix) { 234 if (path.size() >= kGamePrefixLength && lower(path.substr(0, kGamePrefixLength)) == kGamePrefix) {
200 prefix_length = kGamePrefixLength; 235 prefix_length = kGamePrefixLength;
@@ -216,13 +251,15 @@ Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts)
216 if (drive == Drive::None) return Parsed::Host; 251 if (drive == Drive::None) return Parsed::Host;
217 parts.clear(); 252 parts.clear();
218 if (drive == Drive::Scratch) { 253 if (drive == Drive::Scratch) {
219 parts.push_back(kScratchDir);
220 parts.push_back(scratch_run());
221 const std::filesystem::path overlay = roots().overlay; 254 const std::filesystem::path overlay = roots().overlay;
255 const std::wstring folder = scratch_folder(overlay);
256 if (folder.empty()) return Parsed::Refused;
257 parts.push_back(kScratchDir);
258 parts.push_back(folder);
259 // The folder is this process's from its reservation on; made again if something removed it since.
222 const DWORD last_error = ::GetLastError(); 260 const DWORD last_error = ::GetLastError();
223 std::error_code ec; 261 std::error_code ec;
224 if (!overlay.empty()) 262 if (!overlay.empty()) std::filesystem::create_directories(win32_io_path(overlay / kScratchDir / folder), ec);
225 std::filesystem::create_directories(win32_io_path(overlay / kScratchDir / scratch_run()), ec);
226 ::SetLastError(last_error); 263 ::SetLastError(last_error);
227 } 264 }
228 const size_t floor = parts.size(); // ".." may not climb out of the drive's own root 265 const size_t floor = parts.size(); // ".." may not climb out of the drive's own root
@@ -712,7 +749,30 @@ std::filesystem::path overlay_dir() {
712 749
713std::filesystem::path scratch_dir() { 750std::filesystem::path scratch_dir() {
714 const std::filesystem::path overlay = overlay_dir(); 751 const std::filesystem::path overlay = overlay_dir();
715 return overlay.empty() ? overlay : overlay / kScratchDir / scratch_run(); 752 if (overlay.empty()) return {};
753 const std::wstring folder = scratch_folder(overlay);
754 return folder.empty() ? std::filesystem::path() : overlay / kScratchDir / folder;
755}
756
757std::wstring scratch_stem() { return scratch_run(); }
758
759std::filesystem::path reserve_unique_dir(const std::filesystem::path& base, const std::wstring& stem, DWORD& error,
760 unsigned max_tries) {
761 error = 0;
762 std::error_code ec;
763 std::filesystem::create_directories(win32_io_path(base), ec);
764 for (unsigned n = 1; n <= max_tries; ++n) {
765 const std::filesystem::path candidate = base / (n == 1 ? stem : stem + L"-" + std::to_wstring(n));
766 // Create-only: success means no other process or run had this name, so the folder is new and ours. Through
767 // win32_io_path, as every other create here, so a long overlay root keeps working.
768 if (::CreateDirectoryW(win32_io_path(candidate).c_str(), nullptr)) {
769 error = 0;
770 return candidate;
771 }
772 error = ::GetLastError();
773 if (error != ERROR_ALREADY_EXISTS) return {}; // another name will not get past this error
774 }
775 return {};
716} 776}
717 777
718// gta6rt --trace FILE: a failed open of a guest path goes to the startup trace as a "file_open_failed" event (the 778// gta6rt --trace FILE: a failed open of a guest path goes to the startup trace as a "file_open_failed" event (the