From 297ad8685486dc9523d492e8fce19ae4aa52429b Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Mon, 23 Nov 2026 13:07:27 -0500 Subject: file_system: T: is a folder of each process's own in the overlay T:\ mapped to \.gta6rt-temp, one folder shared by every run and kept between runs. The title makes its crash dump and crash log there with CREATE_NEW, and a name an earlier run (or a second process) had left failed with ERROR_FILE_EXISTS (80), so no report landed (old upstream issue 7913, measured by Spacecer2 under Wine 11.22). T:\ now names \.gta6rt-temp\-pid, the process's start time in UTC and its id, made on the first T: call. The start time keeps a reused process id (Wine hands each fresh prefix the same small ids) out of an old run's folder. T:\.. cannot climb into the shared folder, T:\ itself still cannot be removed or moved, and earlier runs' folders are kept so their crash files can be read. GAME: and G: are unchanged. rt::fs::scratch_dir() returns the folder, and the startup line names it: "console paths: GAME: -> ..., writes -> ..., T: -> ...". The two CMake checks of that line (run_tiny_game_console_paths, run_tiny_game_dir) and the synthetic test of the first now expect the T: part. file_system_test: a CREATE_NEW of a name earlier runs left in .gta6rt-temp succeeds and lands in the process's own folder; the folder is stable for the process; a second process (the test started again) creating the same name on the same overlay succeeds in a folder of its own; GAME: reads and writes are unchanged. On main these fail with ERROR_FILE_EXISTS (80), in this process and in the second one. Restored from old upstream PR 8161 and ported onto the new main: the folder is made through main's win32_io_path, so long overlay roots keep working, and the scratch-folder check of the write-open path is kept. Main's long-overlay T: check now finds scratch.bin in the process's own folder below .gta6rt-temp. Co-Authored-By: Claude Opus 5.5 --- runtime/test/file_system_test.cpp | 186 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 177 insertions(+), 9 deletions(-) (limited to 'runtime/test/file_system_test.cpp') diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp index 299ceda3b..b5b75d1ea 100644 --- a/runtime/test/file_system_test.cpp +++ b/runtime/test/file_system_test.cpp @@ -118,6 +118,78 @@ int count(const std::vector& names, const wchar_t* name) { bool contains(const std::vector& names, const wchar_t* name) { return count(names, name) != 0; } +// The folder "T:\" names in process `pid`: the one below \.gta6rt-temp whose name ends in "-pid" +// (rt::fs::scratch_dir() in that process). Empty when there is none, as when every process shared .gta6rt-temp. +std::filesystem::path t_folder(const std::filesystem::path& overlay, DWORD pid) { + const std::wstring suffix = L"-pid" + std::to_wstring(pid); + std::error_code ec; + for (auto it = std::filesystem::directory_iterator(overlay / ".gta6rt-temp", ec); + !ec && it != std::filesystem::directory_iterator(); it.increment(ec)) { + const std::wstring name = it->path().filename().wstring(); + std::error_code type_ec; + if (it->is_directory(type_ec) && name.size() > suffix.size() && + name.compare(name.size() - suffix.size(), suffix.size(), suffix) == 0) + return it->path(); + } + return {}; +} + +std::filesystem::path own_t_folder(const std::filesystem::path& overlay) { + return t_folder(overlay, ::GetCurrentProcessId()); +} + +// file_system_test started by itself as a second process (run_t_drive_child below): it opens T:\crash.dmp with +// CREATE_NEW on the overlay its parent uses, writes "child process" and exits with 0, or with the open's Win32 error. +constexpr wchar_t kChildPackage[] = L"LEONIDA_FS_TEST_CHILD_PACKAGE"; +constexpr wchar_t kChildOverlay[] = L"LEONIDA_FS_TEST_CHILD_OVERLAY"; + +std::wstring environment(const wchar_t* name) { + std::wstring buffer(32768, L'\0'); + const DWORD n = ::GetEnvironmentVariableW(name, buffer.data(), static_cast(buffer.size())); + if (n == 0 || n >= buffer.size()) return {}; + buffer.resize(n); + return buffer; +} + +int t_drive_child(const std::wstring& package, const std::wstring& overlay) { + rt::fs::configure(package, overlay); + const auto create_w = resolve("kernel32.dll", "CreateFileW"); + if (!create_w) return 1000; + const HANDLE h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr); + if (h == INVALID_HANDLE_VALUE) return static_cast(::GetLastError()); + DWORD written = 0; + ::WriteFile(h, "child process", 13, &written, nullptr); + ::CloseHandle(h); + return 0; +} + +// Runs t_drive_child in a new process of this executable on the given folders. Returns its exit code (~0u when it +// could not be started or did not finish) and its process id in `pid`. +DWORD run_t_drive_child(const std::filesystem::path& package, const std::filesystem::path& overlay, DWORD& pid) { + pid = 0; + std::wstring self(32768, L'\0'); + const DWORD length = ::GetModuleFileNameW(nullptr, self.data(), static_cast(self.size())); + if (length == 0 || length >= self.size()) return ~0u; + self.resize(length); + std::wstring command = L"\"" + self + L"\""; + ::SetEnvironmentVariableW(kChildPackage, package.c_str()); + ::SetEnvironmentVariableW(kChildOverlay, overlay.c_str()); + STARTUPINFOW startup{}; + startup.cb = sizeof(startup); + PROCESS_INFORMATION process{}; + const BOOL started = ::CreateProcessW(self.c_str(), command.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, + &startup, &process); + ::SetEnvironmentVariableW(kChildPackage, nullptr); + ::SetEnvironmentVariableW(kChildOverlay, nullptr); + if (!started) return ~0u; + DWORD code = ~0u; + if (::WaitForSingleObject(process.hProcess, 60000) == WAIT_OBJECT_0) ::GetExitCodeProcess(process.hProcess, &code); + pid = process.dwProcessId; + ::CloseHandle(process.hThread); + ::CloseHandle(process.hProcess); + return code; +} + // Denies the current user `rights` on a folder while it lives and puts the folder's old DACL back when it goes, on // every way out of the check. Both changes go through one handle opened before the deny. Under Wine the DACL is the // folder's unix mode: a deny of FILE_LIST_DIRECTORY clears its owner and group read bits (server/file.c, sd_to_mode), @@ -244,6 +316,8 @@ void check_read_file_first_touch(const std::filesystem::path& root) { } // namespace int main() { + if (const std::wstring child_overlay = environment(kChildOverlay); !child_overlay.empty()) + return t_drive_child(environment(kChildPackage), child_overlay); // A folder of this run's own: Wine hands each fresh prefix the same small process ids, so a folder named after // the process id alone can be one an earlier run left behind. LARGE_INTEGER now{}; @@ -1231,18 +1305,18 @@ void run_checks(const std::filesystem::path& root) { "on a fresh overlay CreateDirectoryW(T:\\shadercache) succeeds"); std::filesystem::path fresh = fresh_overlay(); check(copy_w(L"GAME:\\data\\hello.txt", L"T:\\hello.tmp", FALSE) != FALSE && - read_path(fresh / ".gta6rt-temp" / "hello.tmp") == "package", + read_path(own_t_folder(fresh) / "hello.tmp") == "package", "on a fresh overlay CopyFileW(GAME:\\data\\hello.txt, T:\\hello.tmp) succeeds"); fresh = fresh_overlay(); SetLastError(0); check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && - std::filesystem::is_directory(fresh / ".gta6rt-temp"), + std::filesystem::is_directory(own_t_folder(fresh)), "T:\\ cannot be removed, like GAME:\\"); fresh = fresh_overlay(); SetLastError(0); check(move_w(L"T:\\", L"GAME:\\moved") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && attributes_w(L"GAME:\\moved") == INVALID_FILE_ATTRIBUTES && - std::filesystem::is_directory(fresh / ".gta6rt-temp"), + std::filesystem::is_directory(own_t_folder(fresh)), "T:\\ cannot be moved"); fresh_overlay(); attributes_w(L"T:\\"); @@ -1259,8 +1333,8 @@ void run_checks(const std::filesystem::path& root) { ::WriteFile(h, "scratch", 7, &written, nullptr); ::CloseHandle(h); } - check(read_path(overlay / ".gta6rt-temp" / "cache" / "scratch.bin") == "scratch", - "a T: file lands in the overlay's scratch folder"); + check(read_path(own_t_folder(overlay) / "cache" / "scratch.bin") == "scratch", + "a T: file lands in this process's scratch folder in the overlay"); check(!std::filesystem::exists(package / "cache" / "scratch.bin") && !std::filesystem::exists(package / ".gta6rt-temp"), "and the package is untouched"); h = create_w(L"t:/cache/scratch.bin", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); @@ -1280,6 +1354,8 @@ void run_checks(const std::filesystem::path& root) { // as an explicit GAME: or G: path, refused like any other invalid name, in every spelling parse_console() // normalises (RT-294). T:\ still names the scratch folder, and an ordinary dotted name is not reserved. write_path(overlay / ".gta6rt-temp" / "runtime.tmp", "runtime"); + if (const std::filesystem::path t_dir = own_t_folder(overlay); !t_dir.empty()) + write_path(t_dir / "runtime.tmp", "runtime"); write_path(overlay / ".gta6rt-deleted" / "data" / "gone.txt", ""); write_path(overlay / ".gta6rt-other" / "ok.txt", "ok"); for (const wchar_t* reserved : {L"GAME:\\.gta6rt-temp\\runtime.tmp", L"GAME:\\.GTA6RT-TEMP\\runtime.tmp", @@ -1304,7 +1380,8 @@ void run_checks(const std::filesystem::path& root) { if (markers != INVALID_HANDLE_VALUE) find_close(markers); SetLastError(0); const HANDLE probe = create_w(L"GAME:\\.gta6rt-temp\\probe.txt", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr); - check(probe == INVALID_HANDLE_VALUE && !std::filesystem::exists(overlay / ".gta6rt-temp" / "probe.txt"), + check(probe == INVALID_HANDLE_VALUE && !std::filesystem::exists(overlay / ".gta6rt-temp" / "probe.txt") && + !std::filesystem::exists(own_t_folder(overlay) / "probe.txt"), "a write through GAME:\\.gta6rt-temp creates nothing in the scratch folder"); if (probe != INVALID_HANDLE_VALUE) ::CloseHandle(probe); check(mkdir_w(L"GAME:\\.gta6rt-copy\\sub", nullptr) == FALSE && !std::filesystem::exists(overlay / ".gta6rt-copy" / "sub"), @@ -1333,6 +1410,95 @@ void run_checks(const std::filesystem::path& root) { rt::fs::configure(package, overlay); } + // T: is the title's temporary drive, which on the console no other run shares: each process gets a folder of its + // own below \.gta6rt-temp. With one folder for every run, the title's crash dump and crash log, made with + // CREATE_NEW, failed with ERROR_FILE_EXISTS on names an earlier run had left (old upstream issue 7913, measured + // under Wine). + { + const std::filesystem::path t_overlay = root / "t_overlay"; + const std::filesystem::path temp = t_overlay / ".gta6rt-temp"; + // What earlier runs left: a file in the folder they all shared, and one in an earlier process's own folder. + write_path(temp / "crash.dmp", "earlier run"); + write_path(temp / "20200101-000000-000-pid1" / "crash.dmp", "earlier process"); + write_path(package / "crash.dmp", "package"); // and a GAME: file of the same name + rt::fs::configure(package, t_overlay); + + SetLastError(0); + HANDLE h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr); + const DWORD create_error = h == INVALID_HANDLE_VALUE ? GetLastError() : 0; + check(h != INVALID_HANDLE_VALUE, + "CreateFileW(T:\\crash.dmp, CREATE_NEW) succeeds when earlier runs left that name in .gta6rt-temp"); + if (create_error) std::printf(" (failed with Win32 error %lu)\n", static_cast(create_error)); + if (h != INVALID_HANDLE_VALUE) { + DWORD written = 0; + ::WriteFile(h, "this process", 12, &written, nullptr); + ::CloseHandle(h); + } + const std::filesystem::path mine = own_t_folder(t_overlay); + check(!mine.empty() && mine.parent_path() == temp && read_path(mine / "crash.dmp") == "this process", + "T:\\crash.dmp lands in this process's own folder \\.gta6rt-temp\\-pid"); + const std::wstring name = mine.filename().wstring(); + const auto digits = [&](size_t from, size_t n) { + if (name.size() < from + n) return false; + for (size_t i = from; i < from + n; ++i) + if (name[i] < L'0' || name[i] > L'9') return false; + return true; + }; + check(digits(0, 8) && name.size() > 20 && name[8] == L'-' && digits(9, 6) && name[15] == L'-' && + digits(16, 3) && name.compare(19, 4, L"-pid") == 0, + "the folder is named YYYYMMDD-HHMMSS-mmm-pid, the process's start time and id"); + check(read_path(temp / "crash.dmp") == "earlier run" && + read_path(temp / "20200101-000000-000-pid1" / "crash.dmp") == "earlier process" && + read_path(package / "crash.dmp") == "package", + "the earlier runs' files and the package file of the same name are left as they were"); + + // The same folder for every T: call of this process, also after configure() names the overlay again. + rt::fs::configure(package, t_overlay); + h = create_w(L"t:/crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); + check(h != INVALID_HANDLE_VALUE && read_handle(h) == "this process", + "T: keeps naming the same folder for the rest of the process"); + if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h); + const std::vector t_names = list(first_w, next_w, find_close, L"T:\\*"); + check(count(t_names, L"crash.dmp") == 1 && !contains(t_names, L"20200101-000000-000-pid1") && + !contains(t_names, L".gta6rt-temp"), + "a T:\\ listing shows this process's files only"); + SetLastError(0); + check(create_w(L"T:\\..\\crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr) == + INVALID_HANDLE_VALUE && + GetLastError() == ERROR_INVALID_NAME, + "T:\\.. cannot climb into the shared .gta6rt-temp folder"); + SetLastError(0); + check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && std::filesystem::is_directory(mine), + "this process's T:\\ folder cannot be removed, like GAME:\\"); + + // A second process on the same overlay gets a folder of its own, and its CREATE_NEW of the same name succeeds. + DWORD child_pid = 0; + const DWORD child_code = run_t_drive_child(package, t_overlay, child_pid); + check(child_code == 0, + "a second process's CreateFileW(T:\\crash.dmp, CREATE_NEW) on the same overlay succeeds"); + if (child_code != 0) + std::printf(" (the second process exited with %lu)\n", static_cast(child_code)); + const std::filesystem::path theirs = child_pid ? t_folder(t_overlay, child_pid) : std::filesystem::path(); + check(!theirs.empty() && theirs != mine && theirs.parent_path() == temp && + read_path(theirs / "crash.dmp") == "child process", + "the second process wrote into a folder of its own"); + check(read_path(mine / "crash.dmp") == "this process", "and this process's T: file is unchanged"); + + // GAME: is not affected: reads come from the package, writes land in the overlay's mirror of the package. + h = create_w(L"GAME:\\crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); + check(h != INVALID_HANDLE_VALUE && read_handle(h) == "package", + "GAME:\\crash.dmp still reads the package file"); + if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h); + h = create_w(L"GAME:\\data\\t_written.txt", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr); + check(h != INVALID_HANDLE_VALUE, "a GAME: write-open still succeeds"); + if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h); + check(std::filesystem::exists(t_overlay / "data" / "t_written.txt") && + !std::filesystem::exists(mine / "data") && + !std::filesystem::exists(package / "data" / "t_written.txt"), + "and lands at \\data, not in T:'s folder or the package"); + rt::fs::configure(package, overlay); + } + // An open that writes needs its folder to exist in the title's view, and a failed open changes nothing. { const auto overlay_has = [&](const wchar_t* name) { return std::filesystem::exists(overlay / name); }; @@ -1607,9 +1773,11 @@ void run_checks(const std::filesystem::path& root) { check(h != INVALID_HANDLE_VALUE && ::WriteFile(h, "test", 4, &written, nullptr) && written == 4, "T: creates and writes its scratch directory under a long overlay root"); if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h); - check(read_guest(L"T:\\scratch.bin") == "test" && - read_path(extended(scratch_overlay / ".gta6rt-temp" / "scratch.bin")) == "test", - "T: reads from the same long backing directory"); + const std::filesystem::path scratch_folder = rt::fs::scratch_dir(); // configured on scratch_overlay above + check(read_guest(L"T:\\scratch.bin") == "test" && !scratch_folder.empty() && + scratch_folder.parent_path() == scratch_overlay / ".gta6rt-temp" && + read_path(extended(scratch_folder / "scratch.bin")) == "test", + "T: reads from the same long backing directory, this process's own folder in it"); rt::fs::configure(package, overlay); std::error_code cleanup_error; -- cgit v1.2.3 From 1d37e033eb7094f922492de777ee8f1d5cc63b23 Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Mon, 23 Nov 2026 13:25:37 -0500 Subject: 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 --- docs/boot.md | 7 +++- docs/runtime_imports.md | 7 +++- runtime/shims/file_system.cpp | 84 +++++++++++++++++++++++++++++++++------ runtime/shims/file_system.h | 14 ++++++- runtime/test/file_system_test.cpp | 77 ++++++++++++++++++++++++++++++++--- 5 files changed, 165 insertions(+), 24 deletions(-) (limited to 'runtime/test/file_system_test.cpp') diff --git a/docs/boot.md b/docs/boot.md index b3467b07e..5a24b8682 100644 --- a/docs/boot.md +++ b/docs/boot.md @@ -92,8 +92,11 @@ Useful switches: problems"). * `--overlay DIR` sends the guest's writes to a private folder instead of the package. The title's `T:` drive (its temporary drive, where it writes its crash dump `T:\.dmp` and crash log) is a folder of each process's own - inside it, `\.gta6rt-temp\-pid` (the process's start time in UTC and its id), - made on the first `T:` call. The startup line names it (`console paths: GAME: -> ..., writes -> ..., T: -> ...`). + inside it, `\.gta6rt-temp\-pid` (the process's start time in UTC and its id). + The folder is reserved with a create-only `CreateDirectoryW` when `gta6rt` starts the title (or on the first `T:` + call): when that name already exists, as after a reused process id in the same millisecond, it takes `-2`, `-3`, + ... (up to 1000 names), so a run never writes into a folder that existed before it. The startup line names it + (`console paths: GAME: -> ..., writes -> ..., T: -> ...`). Folders of earlier runs are kept, so a run's crash files can be read after it stops; delete them by hand. With one folder for every run, a `CREATE_NEW` of a name an earlier run had left failed with `ERROR_FILE_EXISTS` (80) and no report landed (old upstream issue 7913). diff --git a/docs/runtime_imports.md b/docs/runtime_imports.md index 88263deba..9bc928b10 100644 --- a/docs/runtime_imports.md +++ b/docs/runtime_imports.md @@ -210,8 +210,11 @@ and the `api-ms-win-core-file-*` sets a GDK title links against. process has its own, `\.gta6rt-temp\-pid` (start time in UTC and process id, `rt::fs::scratch_dir()`, named on the startup line), as the console's temporary drive is empty at launch and shared with no other run: with one folder for all runs, a `CREATE_NEW` of a name an earlier run left failed with - `ERROR_FILE_EXISTS` (old upstream issue 7913). Earlier runs' folders are kept, and `T:\..` cannot reach them. A - path under `--game-dir` or the overlay is always a host path, so a package that sits on a host drive `G:` keeps working, and + `ERROR_FILE_EXISTS` (old upstream issue 7913). `rt::fs::reserve_unique_dir` reserves it once per overlay with a + create-only `CreateDirectoryW`; a name that already exists (a reused process id in the same millisecond) is skipped + for `-2`, `-3`, ... up to 1000 names, after which `T:` paths fail with `ERROR_INVALID_NAME` and the reason is + printed once. Earlier runs' folders are kept, never reused, and `T:\..` cannot reach them. A path under + `--game-dir` or the overlay is always a host path, so a package that sits on a host drive `G:` keeps working, and with no folders configured `G:` and `T:` stay host drives. `T:\` is a drive root like `GAME:\`: its folder is made on the first `T:` call, so it is a folder before anything is written there, and removing or moving it fails with `ERROR_ACCESS_DENIED`. 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 @@ // here (docs/runtime_imports.md, "Console paths and the write overlay"; the prefixes come from ivall/xcl, // GPL-2.0-only, as facts). The rules: // - "GAME:" (any case, "\\" or "/") maps under --game-dir, "G:\\" is the same folder and "T:\\" a scratch folder in -// the overlay (RT-218), \\.gta6rt-temp\\, a new one for every process; any other path is a host -// path and goes to the host; +// the overlay (RT-218), \\.gta6rt-temp\\, a new one reserved by every process; any other path is a +// host path and goes to the host; // - an open that can write, and DeleteFile, go to the overlay (--overlay, default \\overlay), so the // package stays exactly as installed. A write-open of a package file copies it into the overlay first, so // OPEN_EXISTING keeps its bytes; a delete of a package file leaves an empty marker under @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -167,15 +168,15 @@ std::filesystem::path win32_find_path(const std::filesystem::path& path, const s // other console layers map those two drives (RT-218). Only the executable can say which spelling the title uses, so // a host drive of the same letter must keep working: a path under --game-dir or the overlay (a package on drive G:, // or a path GetModuleFileName handed back) stays a host path, and so does every G:/T: path while no roots are -// configured. The scratch folder lives in the overlay, as `.gta6rt-temp\`, one per process (scratch_run()). +// configured. The scratch folder lives in the overlay, as `.gta6rt-temp\`, one per process (scratch_folder()). enum class Drive { None, Game, Scratch }; -// The name of this process's T: folder below `.gta6rt-temp`: the process's start time (UTC, to the millisecond) and +// The stem of this process's T: folder below `.gta6rt-temp`: the process's start time (UTC, to the millisecond) and // its id, e.g. "20261123-181502-123-pid4242". On the console T: is the title's own temporary drive, empty at launch // and shared with nothing; one folder that every run shared kept the files earlier runs left, so a title that makes a -// file there with CREATE_NEW (its crash dump and crash log, measured under Wine) got ERROR_FILE_EXISTS. The start -// time keeps a reused process id from landing in an old run's folder; the folders of earlier runs are left in place -// for whoever wants to read what those runs wrote. +// file there with CREATE_NEW (its crash dump and crash log, measured under Wine) got ERROR_FILE_EXISTS. The stem only +// makes the folder easy to find: a reused process id in the same millisecond gives the same stem, so the folder is +// reserved by reserve_unique_dir, which never hands out one that already existed. const std::wstring& scratch_run() { static const std::wstring name = [] { const DWORD last_error = ::GetLastError(); @@ -195,6 +196,40 @@ const std::wstring& scratch_run() { return name; } +// The T: folder this process reserved in each overlay it was given (configure() may name several, as the test does), +// or the error that stopped the reservation. Decided once per overlay, under the mutex, so concurrent first T: calls +// get the same folder. +struct ScratchFolder { + std::wstring name; // empty: the reservation failed with `error` + DWORD error = 0; +}; +std::mutex g_scratch_mutex; +std::map g_scratch_folders; // keyed by the overlay path, lowercased + +// The name of this process's folder below \.gta6rt-temp, reserved on the first call for that overlay; empty +// when it could not be reserved (logged once). With no overlay configured nothing is made and the stem is the name. +std::wstring scratch_folder(const std::filesystem::path& overlay) { + if (overlay.empty()) return scratch_run(); + std::lock_guard lock(g_scratch_mutex); + ScratchFolder& folder = g_scratch_folders[lower(overlay.native())]; + if (folder.name.empty() && folder.error == 0) { + const DWORD last_error = ::GetLastError(); + DWORD error = 0; + const std::filesystem::path reserved = reserve_unique_dir(overlay / kScratchDir, scratch_run(), error); + if (reserved.empty()) { + folder.error = error ? error : ERROR_CANNOT_MAKE; + std::fprintf(stderr, + "[rt] file_system: no T: folder could be reserved below %ls (Win32 error %lu); T: paths " + "fail with ERROR_INVALID_NAME\n", + (overlay / kScratchDir).c_str(), static_cast(folder.error)); + } else { + folder.name = reserved.filename().native(); + } + ::SetLastError(last_error); + } + return folder.name; +} + Drive console_drive(const std::wstring& path, size_t& prefix_length) { if (path.size() >= kGamePrefixLength && lower(path.substr(0, kGamePrefixLength)) == kGamePrefix) { prefix_length = kGamePrefixLength; @@ -216,13 +251,15 @@ Parsed parse_console(const std::wstring& path, std::vector& parts) if (drive == Drive::None) return Parsed::Host; parts.clear(); if (drive == Drive::Scratch) { - parts.push_back(kScratchDir); - parts.push_back(scratch_run()); const std::filesystem::path overlay = roots().overlay; + const std::wstring folder = scratch_folder(overlay); + if (folder.empty()) return Parsed::Refused; + parts.push_back(kScratchDir); + parts.push_back(folder); + // The folder is this process's from its reservation on; made again if something removed it since. const DWORD last_error = ::GetLastError(); std::error_code ec; - if (!overlay.empty()) - std::filesystem::create_directories(win32_io_path(overlay / kScratchDir / scratch_run()), ec); + if (!overlay.empty()) std::filesystem::create_directories(win32_io_path(overlay / kScratchDir / folder), ec); ::SetLastError(last_error); } 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() { std::filesystem::path scratch_dir() { const std::filesystem::path overlay = overlay_dir(); - return overlay.empty() ? overlay : overlay / kScratchDir / scratch_run(); + if (overlay.empty()) return {}; + const std::wstring folder = scratch_folder(overlay); + return folder.empty() ? std::filesystem::path() : overlay / kScratchDir / folder; +} + +std::wstring scratch_stem() { return scratch_run(); } + +std::filesystem::path reserve_unique_dir(const std::filesystem::path& base, const std::wstring& stem, DWORD& error, + unsigned max_tries) { + error = 0; + std::error_code ec; + std::filesystem::create_directories(win32_io_path(base), ec); + for (unsigned n = 1; n <= max_tries; ++n) { + const std::filesystem::path candidate = base / (n == 1 ? stem : stem + L"-" + std::to_wstring(n)); + // Create-only: success means no other process or run had this name, so the folder is new and ours. Through + // win32_io_path, as every other create here, so a long overlay root keeps working. + if (::CreateDirectoryW(win32_io_path(candidate).c_str(), nullptr)) { + error = 0; + return candidate; + } + error = ::GetLastError(); + if (error != ERROR_ALREADY_EXISTS) return {}; // another name will not get past this error + } + return {}; } // gta6rt --trace FILE: a failed open of a guest path goes to the startup trace as a "file_open_failed" event (the diff --git a/runtime/shims/file_system.h b/runtime/shims/file_system.h index af2cb0891..420430e13 100644 --- a/runtime/shims/file_system.h +++ b/runtime/shims/file_system.h @@ -28,9 +28,19 @@ void configure(const std::filesystem::path& game_dir, const std::filesystem::pat std::filesystem::path game_dir(); std::filesystem::path overlay_dir(); -// The folder "T:\" names in this process: \.gta6rt-temp\-pid, a different one for every -// process, made on the first T: call; empty while no overlay is configured. +// The folder "T:\" names in this process: \.gta6rt-temp\, or -2, -3, ... when that name was +// taken (scratch_stem(), reserve_unique_dir). Reserved on the first T: call or the first call of this function for the +// overlay and kept for the rest of the process; empty while no overlay is configured or when no folder could be made. std::filesystem::path scratch_dir(); +// The first name tried for that folder: the process's start time (UTC) and id, "YYYYMMDD-HHMMSS-mmm-pid". +std::wstring scratch_stem(); + +// Makes a new folder base\stem, or base\stem-2 ... base\stem- when the names before it exist, each with a +// create-only CreateDirectoryW, so the folder returned did not exist before and no other process got it. Makes `base` +// first. Returns an empty path, with the Win32 error in `error`, when every name was taken (ERROR_ALREADY_EXISTS) or +// a create failed for another reason. +std::filesystem::path reserve_unique_dir(const std::filesystem::path& base, const std::wstring& stem, + unsigned long& error, unsigned max_tries = 1000); // For tests: true makes the CopyFile2 shim use its CopyFileExW fallback even where the host has CopyFile2, so the // path Wine 9 takes can be checked on Windows. Off by default; main.cpp never sets it. diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp index b5b75d1ea..7b68d0c69 100644 --- a/runtime/test/file_system_test.cpp +++ b/runtime/test/file_system_test.cpp @@ -118,24 +118,27 @@ int count(const std::vector& names, const wchar_t* name) { bool contains(const std::vector& names, const wchar_t* name) { return count(names, name) != 0; } -// The folder "T:\" names in process `pid`: the one below \.gta6rt-temp whose name ends in "-pid" -// (rt::fs::scratch_dir() in that process). Empty when there is none, as when every process shared .gta6rt-temp. +// The folder "T:\" names in another process `pid`: the one below \.gta6rt-temp whose name ends in +// "-pid" or "-pid-" (rt::fs::scratch_dir() in that process). Empty when there is none, as when every +// process shared .gta6rt-temp. std::filesystem::path t_folder(const std::filesystem::path& overlay, DWORD pid) { - const std::wstring suffix = L"-pid" + std::to_wstring(pid); + const std::wstring marker = L"-pid" + std::to_wstring(pid); std::error_code ec; for (auto it = std::filesystem::directory_iterator(overlay / ".gta6rt-temp", ec); !ec && it != std::filesystem::directory_iterator(); it.increment(ec)) { const std::wstring name = it->path().filename().wstring(); + const size_t at = name.rfind(marker); std::error_code type_ec; - if (it->is_directory(type_ec) && name.size() > suffix.size() && - name.compare(name.size() - suffix.size(), suffix.size(), suffix) == 0) + if (it->is_directory(type_ec) && at != std::wstring::npos && + (at + marker.size() == name.size() || name[at + marker.size()] == L'-')) return it->path(); } return {}; } +// This process's T: folder in the configured overlay. std::filesystem::path own_t_folder(const std::filesystem::path& overlay) { - return t_folder(overlay, ::GetCurrentProcessId()); + return rt::fs::overlay_dir() == overlay ? rt::fs::scratch_dir() : std::filesystem::path(); } // file_system_test started by itself as a second process (run_t_drive_child below): it opens T:\crash.dmp with @@ -1447,6 +1450,7 @@ void run_checks(const std::filesystem::path& root) { check(digits(0, 8) && name.size() > 20 && name[8] == L'-' && digits(9, 6) && name[15] == L'-' && digits(16, 3) && name.compare(19, 4, L"-pid") == 0, "the folder is named YYYYMMDD-HHMMSS-mmm-pid, the process's start time and id"); + check(name == rt::fs::scratch_stem(), "with no folder of that name yet, it is the stem itself"); check(read_path(temp / "crash.dmp") == "earlier run" && read_path(temp / "20200101-000000-000-pid1" / "crash.dmp") == "earlier process" && read_path(package / "crash.dmp") == "package", @@ -1484,6 +1488,41 @@ void run_checks(const std::filesystem::path& root) { "the second process wrote into a folder of its own"); check(read_path(mine / "crash.dmp") == "this process", "and this process's T: file is unchanged"); + // A reused process id in the same millisecond gives the same stem as a folder an earlier run kept. The folder + // is reserved with a create-only call, so that one (and the next name, taken too) is never used again. + const std::filesystem::path stale_overlay = root / "t_stale_overlay"; + const std::filesystem::path stale = stale_overlay / ".gta6rt-temp" / rt::fs::scratch_stem(); + const std::filesystem::path stale_2 = stale_overlay / ".gta6rt-temp" / (rt::fs::scratch_stem() + L"-2"); + write_path(stale / "crash.dmp", "same stem, earlier run"); + std::filesystem::create_directories(stale_2); + rt::fs::configure(package, stale_overlay); + h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr); + check(h != INVALID_HANDLE_VALUE, + "CreateFileW(T:\\crash.dmp, CREATE_NEW) succeeds when a kept folder has this process's own stem"); + if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h); + check(rt::fs::scratch_dir() == stale_overlay / ".gta6rt-temp" / (rt::fs::scratch_stem() + L"-3") && + std::filesystem::exists(rt::fs::scratch_dir() / "crash.dmp"), + "T: is -3 when and -2 already existed"); + check(read_path(stale / "crash.dmp") == "same stem, earlier run" && std::filesystem::is_empty(stale_2), + "and the folders that already existed are left as they were"); + + // Concurrent first T: calls on a new overlay reserve one folder between them. + const std::filesystem::path race_overlay = root / "t_race_overlay"; + rt::fs::configure(package, race_overlay); + { + std::vector threads; + for (int i = 0; i < 8; ++i) threads.emplace_back([&] { attributes_w(L"T:\\"); }); + for (auto& thread : threads) thread.join(); + } + size_t reserved = 0; + for (const auto& entry : std::filesystem::directory_iterator(race_overlay / ".gta6rt-temp")) { + (void)entry; + ++reserved; + } + check(reserved == 1 && std::filesystem::is_directory(rt::fs::scratch_dir()), + "eight threads making their first T: call at once reserve one folder"); + rt::fs::configure(package, t_overlay); + // GAME: is not affected: reads come from the package, writes land in the overlay's mirror of the package. h = create_w(L"GAME:\\crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); check(h != INVALID_HANDLE_VALUE && read_handle(h) == "package", @@ -1499,6 +1538,32 @@ void run_checks(const std::filesystem::path& root) { rt::fs::configure(package, overlay); } + // reserve_unique_dir: a create-only CreateDirectoryW per name, so the folder it returns is new; a name that exists + // (a folder or a file) is skipped, and after max_tries names the call fails instead of reusing one. + { + const std::filesystem::path base = root / "reserve"; + std::filesystem::create_directories(base / "stem"); + write_path(base / "stem-2" / "old.txt", "old"); + DWORD error = 1; + const std::filesystem::path third = rt::fs::reserve_unique_dir(base, L"stem", error); + check(third == base / "stem-3" && error == 0 && std::filesystem::is_directory(third) && + std::filesystem::is_empty(third), + "reserve_unique_dir(base, stem) with stem and stem-2 taken makes stem-3, new and empty"); + check(read_path(base / "stem-2" / "old.txt") == "old", "and leaves the taken folders alone"); + check(rt::fs::reserve_unique_dir(base, L"stem", error) == base / "stem-4", + "a second call never hands out stem-3 again: it makes stem-4"); + write_path(base / "stem-5", "a file"); + check(rt::fs::reserve_unique_dir(base, L"stem", error) == base / "stem-6", + "a file with the next name counts as taken too"); + error = 0; + check(rt::fs::reserve_unique_dir(base, L"stem", error, 6).empty() && error == ERROR_ALREADY_EXISTS, + "with every name up to max_tries taken it fails with ERROR_ALREADY_EXISTS"); + const std::filesystem::path fresh_base = root / "reserve_new" / "deep"; + check(rt::fs::reserve_unique_dir(fresh_base, L"stem", error) == fresh_base / "stem" && + std::filesystem::is_directory(fresh_base / "stem"), + "a missing base folder is made first, and the stem itself is the first name"); + } + // An open that writes needs its folder to exist in the title's view, and a failed open changes nothing. { const auto overlay_has = [&](const wchar_t* name) { return std::filesystem::exists(overlay / name); }; -- cgit v1.2.3 From de8cb53d5498ce914ba82bc02c6a3f39dc03f3bf Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Tue, 24 Nov 2026 07:02:11 -0500 Subject: file_system: RemoveDirectory and a create inside the folder are ordered as on the host Restores old upstream PR 9215 in ItsNotRexed/Leonida (closed unmerged when that repository went private), ported onto the current main: the write-open keeps main's long-path disk_path() for its overlay path, and the new race test sits after main's tests that recreate a removed package folder. A GAME: write-open checked that its folder exists, mirrored it into the overlay and then opened the file, and RemoveDirectory checked that the folder was empty and wrote its marker, neither under a lock. A remove that ran between the open's check and its host CreateFile succeeded on an empty folder, and the open then made the folder and the file again: both calls succeeded and the removed folder was back. CreateDirectory of a folder inside it had the same gap. On Windows one of the two fails: the create with ERROR_PATH_NOT_FOUND or the remove with ERROR_DIR_NOT_EMPTY. A striped lock keyed by the folder's upper-cased name (64 namespace-scope std::mutex, no function-local static): write_target holds the lock of the folder it opens in from the parent check until the Target is gone, after the host CreateFile/CreateFile2; CreateDirectory holds the parent's from its check to the host call; RemoveDirectory holds the folder's from its view to the marker. Reads take no lock. file_system_test races the two calls on two threads, 2,000 rounds each for a creating CreateFileW (CREATE_NEW, CREATE_ALWAYS, OPEN_ALWAYS) and for CreateDirectoryW, and checks that they never both succeed, that the loser fails with the host's error and that a removed folder stays removed. Co-Authored-By: Claude Opus 5.5 --- docs/runtime_imports.md | 4 ++- runtime/shims/file_system.cpp | 40 ++++++++++++++++++++++ runtime/test/file_system_test.cpp | 70 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) (limited to 'runtime/test/file_system_test.cpp') diff --git a/docs/runtime_imports.md b/docs/runtime_imports.md index 7b9f62d5a..7cad0eed7 100644 --- a/docs/runtime_imports.md +++ b/docs/runtime_imports.md @@ -232,7 +232,9 @@ and the `api-ms-win-core-file-*` sets a GDK title links against. package has, fails with `ERROR_ALREADY_EXISTS`; a missing parent with `ERROR_PATH_NOT_FOUND`. - `RemoveDirectory*` needs the folder empty in the title's view (`ERROR_DIR_NOT_EMPTY` otherwise). An overlay folder is removed; a package folder is hidden by a `.gta6rt-dir` marker inside its marker folder, and stays on disk. A marker - folder on its own (left by a file deleted below it) does not hide anything. + folder on its own (left by a file deleted below it) does not hide anything. A remove and a write-open or + `CreateDirectory*` inside the folder on another thread are ordered as on the host: one goes first, and the create + fails with `ERROR_PATH_NOT_FOUND` or the remove with `ERROR_DIR_NOT_EMPTY`, never both succeeding. - `MoveFile*` writes the destination in the overlay. A package file is copied there and its old name deleted by a marker; an overlay file or an overlay-only folder is renamed. An existing destination needs `MOVEFILE_REPLACE_EXISTING` even when only the package has it. Moving a package folder fails with diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index 75f5e9810..bafd06b71 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp @@ -246,9 +246,41 @@ bool drive_root(const std::wstring& path, const std::vector& parts return parts.empty() || (parts.size() == 1 && console_drive(path, prefix_length) == Drive::Scratch); } +// A folder's entries change one step at a time, as on the host: RemoveDirectory holds the folder's lock from its +// emptiness check to its marker, and a GAME: write-open or CreateDirectory holds the lock of the folder it creates in +// from its "does the folder exist" check, through mirroring that folder into the overlay, to the host call that makes +// the entry. One of the two goes first: the create fails with ERROR_PATH_NOT_FOUND, or the remove with +// ERROR_DIR_NOT_EMPTY. Without it both succeeded and the removed folder was back, the new entry in it. The locks are +// striped by the folder's name, so calls in different folders rarely wait on each other; reads never take one. +// Namespace scope, not function-local statics: these run on the title's threads. Taken after roots(); nothing holding +// one takes another of them or calls back into the title (a copy-up's lock may be taken inside one, never around one). +constexpr size_t kFolderLocks = 64; +std::mutex g_folder_locks[kFolderLocks]; + +std::mutex& folder_lock(const std::vector& folder) { + // FNV-1a over the upper-cased components: names compare without case, so every spelling of a folder gets its lock. + uint64_t hash = 14695981039346656037ull; + const auto mix = [&hash](wchar_t c) { + hash ^= static_cast(c); + hash *= 1099511628211ull; + }; + for (const auto& part : folder) { + std::wstring upper(part.size(), L'\0'); + if (part.empty() || LCMapStringEx(LOCALE_NAME_INVARIANT, LCMAP_UPPERCASE, part.data(), + static_cast(part.size()), upper.data(), static_cast(upper.size()), + nullptr, nullptr, 0) != static_cast(part.size())) + upper = part; + for (const wchar_t c : upper) mix(c); + mix(L'\\'); + } + return g_folder_locks[hash % kFolderLocks]; +} + struct Target { std::filesystem::path path; DWORD error = 0; // non-zero: the caller fails with this error and does not touch the host + // A write-open's hold on the folder it opens in, until the Target goes after the host open (g_folder_locks). + std::unique_lock folder; }; Target fail(DWORD error) { @@ -433,10 +465,14 @@ Target write_target(const std::wstring& path, bool copy_up) { // below a package file (which then read as a folder) and inside a folder the title had removed. The overlay may // still lack a folder only the package has, so it is mirrored here, which the title cannot see. // T:'s scratch folder is the exception: it creates its parent folders, as merged with RT-218. + // The folder's lock is held from this check to the caller's host open, so a RemoveDirectory of the folder comes + // wholly before or after (g_folder_locks). + std::unique_lock folder(folder_lock(parent_of(parts))); const bool scratch = !parts.empty() && same_name(parts.front().c_str(), kScratchDir); if (!scratch && !is_folder(r, parent_of(parts))) return fail(ERROR_PATH_NOT_FOUND); Target t; + t.folder = std::move(folder); t.path = disk_path(r.overlay, parts); std::error_code ec; @@ -1062,6 +1098,8 @@ BOOL create_directory(LPCWSTR template_name, LPCWSTR name, LPSECURITY_ATTRIBUTES const Roots r = roots(); if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); if (view(r, parts).kind != Kind::None) return fail_bool(ERROR_ALREADY_EXISTS); // "GAME:" included + // From the parent's check to the new folder: a RemoveDirectory of the parent comes wholly before or after. + std::lock_guard folder(folder_lock(parent_of(parts))); if (!is_folder(r, parent_of(parts))) return fail_bool(ERROR_PATH_NOT_FOUND); const std::filesystem::path target = disk_path(r.overlay, parts); @@ -1115,6 +1153,8 @@ BOOL WINAPI remove_directory_w(LPCWSTR name) { const Roots r = roots(); if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); if (drive_root(name, parts)) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" and "T:" themselves stay + // From the emptiness check to the marker: a create in the folder comes wholly before or after (g_folder_locks). + std::lock_guard folder(folder_lock(parts)); const View v = view(r, parts); if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND); if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY); diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp index 299ceda3b..540b83522 100644 --- a/runtime/test/file_system_test.cpp +++ b/runtime/test/file_system_test.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -807,6 +808,75 @@ void run_checks(const std::filesystem::path& root) { } } + // RemoveDirectory of a folder on one thread and, on another, a creating open of a file in it (CREATE_NEW, + // CREATE_ALWAYS, OPEN_ALWAYS) or CreateDirectory of a folder in it. The host orders the two: the remove goes first + // and the create fails with ERROR_PATH_NOT_FOUND, or the create goes first and the remove fails with + // ERROR_DIR_NOT_EMPTY. Both succeeding left the removed folder back in the title's view with the new entry in it. + // Each round takes a folder of its own that only the package has, empty in the title's view, and starts the two + // threads together; the remover waits a few more spins each round so the calls meet at different points. + { + constexpr int kRounds = 2000; + const auto race = [&](bool make_folder, const wchar_t* prefix, const char* what) { + for (int i = 0; i < kRounds; ++i) + std::filesystem::create_directories(package / "rmrace" / (prefix + std::to_wstring(i))); + int create_first = 0, remove_first = 0, both = 0, other = 0, back = 0; + for (int i = 0; i < kRounds; ++i) { + const std::wstring folder = L"GAME:\\rmrace\\" + (prefix + std::to_wstring(i)); + const std::wstring child = folder + (make_folder ? L"\\sub" : L"\\new.tmp"); + const DWORD disposition = i % 3 == 0 ? CREATE_NEW : i % 3 == 1 ? CREATE_ALWAYS : OPEN_ALWAYS; + std::atomic ready{0}; + HANDLE handle = INVALID_HANDLE_VALUE; + bool made = false; + DWORD create_error = 0; + std::thread creator([&] { + ready.fetch_add(1); + while (ready.load() < 2) { + } + if (make_folder) { + made = mkdir_w(child.c_str(), nullptr) != FALSE; + } else { + handle = create_w(child.c_str(), GENERIC_WRITE, 0, nullptr, disposition, 0, nullptr); + made = handle != INVALID_HANDLE_VALUE; + } + create_error = made ? 0 : GetLastError(); + }); + ready.fetch_add(1); + while (ready.load() < 2) { + } + for (int spin = 0; spin < (i % 64) * 64; ++spin) YieldProcessor(); + const bool removed = rmdir_w(folder.c_str()) != FALSE; + const DWORD remove_error = removed ? 0 : GetLastError(); + creator.join(); + if (handle != INVALID_HANDLE_VALUE) ::CloseHandle(handle); + if (made && removed) { + ++both; + } else if (made && remove_error == ERROR_DIR_NOT_EMPTY) { + ++create_first; + } else if (removed && create_error == ERROR_PATH_NOT_FOUND) { + ++remove_first; + } else { + ++other; + } + // A folder the remove took away stays away, whatever the create did. + if (removed) { + SetLastError(0); + if (attributes_w(folder.c_str()) != INVALID_FILE_ATTRIBUTES || GetLastError() != ERROR_FILE_NOT_FOUND) + ++back; + } + } + std::printf("NOTE %s against RemoveDirectoryW, %d rounds: create first %d, remove first %d, both succeeded " + "%d, other %d, removed folder back %d\n", + what, kRounds, create_first, remove_first, both, other, back); + const std::string w = what; + check(both == 0, (w + " and RemoveDirectoryW of its folder never both succeed").c_str()); + check(other == 0, (w + " or RemoveDirectoryW fails as on the host (ERROR_PATH_NOT_FOUND or " + "ERROR_DIR_NOT_EMPTY)").c_str()); + check(back == 0, (w + ": a folder RemoveDirectoryW removed stays removed").c_str()); + }; + race(false, L"f", "a creating CreateFileW"); + race(true, L"d", "CreateDirectoryW"); + } + // MoveFile: the destination is in the overlay; a moved package file is deleted by a marker. { check(move_w(L"GAME:\\misc\\one.txt", L"GAME:\\misc\\moved.txt") != FALSE, "MoveFileW moves a package file"); -- cgit v1.2.3