upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/file_system_test.cpp
diff options
context:
space:
mode:
authorairp1ane <279825357+airp1ane@users.noreply.github.com>2026-11-22 14:15:36 -0500
committerairp1ane <279825357+airp1ane@users.noreply.github.com>2026-11-22 14:15:36 -0500
commitfc1e0058e0e3ecdbac61fe89bc69e7c5ec15bc21 (patch)
tree323bac2c0c358db892b55e3b776e28a68717bc8b /runtime/test/file_system_test.cpp
parent162821017ea3c286c8bec860f3ff94f037912061 (diff)
file_system: copy-up through a staging name, one at a time
A write-open of a package file copied it straight to its overlay name with no lock. A process killed mid-copy left a truncated file there that every later read preferred over the package; two threads write-opening the same package file both copied, and the second copy_file failed on the taken name, so that open failed. write_target now copies into <overlay>\.gta6rt-copy under a private name and renames it into place, under a mutex that only copy-ups take. Listings hide the staging folder like .gta6rt-deleted. Without the mutex, copy-then-rename alone still failed about 2 in 100 runs: a losing rename onto the existing name briefly holds it, and the winner's open got ERROR_SHARING_VIOLATION. file_system_test: two threads write-open one 256 KiB package file, 50 rounds; both opens succeed and the overlay copy matches (fails 3 of 3 runs on the old code, passes 200 of 200 with this); no staging file is left; a crash leftover is hidden from a GAME: listing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/file_system_test.cpp')
-rw-r--r--runtime/test/file_system_test.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp
index 3a4bc5860..d584e5948 100644
--- a/runtime/test/file_system_test.cpp
+++ b/runtime/test/file_system_test.cpp
@@ -8,6 +8,7 @@
8#include <cstdio> 8#include <cstdio>
9#include <filesystem> 9#include <filesystem>
10#include <string> 10#include <string>
11#include <thread>
11#include <vector> 12#include <vector>
12 13
13#include "file_system.h" 14#include "file_system.h"
@@ -607,6 +608,40 @@ int main() {
607 if (hfa != INVALID_HANDLE_VALUE) find_close(hfa); 608 if (hfa != INVALID_HANDLE_VALUE) find_close(hfa);
608 } 609 }
609 610
611 // Copy-up under two threads: both write-open the same untouched package file at once. Each open must succeed and
612 // the overlay copy must hold the package's bytes; the copy goes through a private name, so neither thread can find
613 // the name taken mid-copy or read a half-written file.
614 {
615 const std::string big(256 * 1024, 'p'); // long enough that the two copies overlap
616 bool all_opened = true, all_intact = true;
617 for (int round = 0; round < 50; ++round) {
618 const std::wstring name = L"race" + std::to_wstring(round) + L".bin";
619 write_path(package / "data" / name, big);
620 const std::wstring game = L"GAME:\\data\\" + name;
621 HANDLE handles[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
622 std::thread threads[2];
623 for (int i = 0; i < 2; ++i)
624 threads[i] = std::thread([&, i] {
625 handles[i] = create_w(game.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
626 nullptr, OPEN_EXISTING, 0, nullptr); });
627 for (auto& t : threads) t.join();
628 for (HANDLE h : handles) {
629 if (h == INVALID_HANDLE_VALUE) all_opened = false;
630 else ::CloseHandle(h);
631 }
632 if (read_path(overlay / "data" / name) != big) all_intact = false;
633 }
634 check(all_opened, "two threads write-opening one package file both succeed");
635 check(all_intact, "the overlay copy made under two threads holds the whole package file");
636 std::error_code ec;
637 check(std::filesystem::is_empty(overlay / ".gta6rt-copy", ec) && !ec, "copy-up leaves no staging file behind");
638
639 // A leftover from a process killed mid-copy stays out of the title's view.
640 write_path(overlay / ".gta6rt-copy" / "123-1", "half");
641 check(!contains(list(first_w, next_w, find_close, L"GAME:\\*"), L".gta6rt-copy"),
642 "the copy staging folder is hidden from a GAME: listing");
643 }
644
610 std::filesystem::remove_all(root); 645 std::filesystem::remove_all(root);
611 std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures); 646 std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures);
612 return g_failures == 0 ? 0 : 1; 647 return g_failures == 0 ? 0 : 1;