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:
authorNotRexed <therealnothingfn@gmail.com>2026-11-27 17:50:37 -0500
committerNotRexed <therealnothingfn@gmail.com>2026-11-27 17:50:37 -0500
commitb87abdef5735adc68a614b2025ffcf9869a47cc5 (patch)
tree0d8b473e8e753883a01e00153175994d6b7949c2 /runtime/test/file_system_test.cpp
parent84e728faffb36cd96a08934e5632ba9b194801f3 (diff)
parent34e33d3ddc4dd95ad14a074db886df3d25588201 (diff)
Merge mandy's main into port/d3d12-nvidia
Brings the D3D12 runtime branch (d3d12/, its launcher scripts and notes, and the pull requests reviewed and applied on it) together with mandy's main. Conflicts (67 files, none under d3d12/): docs/, make_game.py, runtime/, scripts/build_runtime.ps1, tests/ and tools/. Every one takes main's side: main is where that code is maintained, and this branch's copies were older states of the same files or the same pull requests in another form. tests/test_fspx_unpack.py is main's too, as it tests main's tools/fspx_unpack.py. Nothing under d3d12/ is changed by the merge. Checked before pushing: python -m compileall over make_game.py, tools and tests; make_game.py --help; scripts/check_no_game_data.py over the files this adds to main; tests/test_text_write_line_endings.py. The translator suite, the C++ runtime build and CI have not run on the result. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/file_system_test.cpp')
-rw-r--r--runtime/test/file_system_test.cpp434
1 files changed, 421 insertions, 13 deletions
diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp
index df7f1f4d2..db8a8f8fb 100644
--- a/runtime/test/file_system_test.cpp
+++ b/runtime/test/file_system_test.cpp
@@ -8,7 +8,9 @@
8#include <aclapi.h> 8#include <aclapi.h>
9 9
10#include <algorithm> 10#include <algorithm>
11#include <atomic>
11#include <clocale> 12#include <clocale>
13#include <cstring>
12#include <cstdio> 14#include <cstdio>
13#include <exception> 15#include <exception>
14#include <filesystem> 16#include <filesystem>
@@ -117,6 +119,81 @@ int count(const std::vector<std::wstring>& names, const wchar_t* name) {
117 119
118bool contains(const std::vector<std::wstring>& names, const wchar_t* name) { return count(names, name) != 0; } 120bool contains(const std::vector<std::wstring>& names, const wchar_t* name) { return count(names, name) != 0; }
119 121
122// The folder "T:\" names in another process `pid`: the one below <overlay>\.gta6rt-temp whose name ends in
123// "-pid<pid>" or "-pid<pid>-<n>" (rt::fs::scratch_dir() in that process). Empty when there is none, as when every
124// process shared .gta6rt-temp.
125std::filesystem::path t_folder(const std::filesystem::path& overlay, DWORD pid) {
126 const std::wstring marker = L"-pid" + std::to_wstring(pid);
127 std::error_code ec;
128 for (auto it = std::filesystem::directory_iterator(overlay / ".gta6rt-temp", ec);
129 !ec && it != std::filesystem::directory_iterator(); it.increment(ec)) {
130 const std::wstring name = it->path().filename().wstring();
131 const size_t at = name.rfind(marker);
132 std::error_code type_ec;
133 if (it->is_directory(type_ec) && at != std::wstring::npos &&
134 (at + marker.size() == name.size() || name[at + marker.size()] == L'-'))
135 return it->path();
136 }
137 return {};
138}
139
140// This process's T: folder in the configured overlay.
141std::filesystem::path own_t_folder(const std::filesystem::path& overlay) {
142 return rt::fs::overlay_dir() == overlay ? rt::fs::scratch_dir() : std::filesystem::path();
143}
144
145// file_system_test started by itself as a second process (run_t_drive_child below): it opens T:\crash.dmp with
146// CREATE_NEW on the overlay its parent uses, writes "child process" and exits with 0, or with the open's Win32 error.
147constexpr wchar_t kChildPackage[] = L"LEONIDA_FS_TEST_CHILD_PACKAGE";
148constexpr wchar_t kChildOverlay[] = L"LEONIDA_FS_TEST_CHILD_OVERLAY";
149
150std::wstring environment(const wchar_t* name) {
151 std::wstring buffer(32768, L'\0');
152 const DWORD n = ::GetEnvironmentVariableW(name, buffer.data(), static_cast<DWORD>(buffer.size()));
153 if (n == 0 || n >= buffer.size()) return {};
154 buffer.resize(n);
155 return buffer;
156}
157
158int t_drive_child(const std::wstring& package, const std::wstring& overlay) {
159 rt::fs::configure(package, overlay);
160 const auto create_w = resolve<CreateFileWFn>("kernel32.dll", "CreateFileW");
161 if (!create_w) return 1000;
162 const HANDLE h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
163 if (h == INVALID_HANDLE_VALUE) return static_cast<int>(::GetLastError());
164 DWORD written = 0;
165 ::WriteFile(h, "child process", 13, &written, nullptr);
166 ::CloseHandle(h);
167 return 0;
168}
169
170// Runs t_drive_child in a new process of this executable on the given folders. Returns its exit code (~0u when it
171// could not be started or did not finish) and its process id in `pid`.
172DWORD run_t_drive_child(const std::filesystem::path& package, const std::filesystem::path& overlay, DWORD& pid) {
173 pid = 0;
174 std::wstring self(32768, L'\0');
175 const DWORD length = ::GetModuleFileNameW(nullptr, self.data(), static_cast<DWORD>(self.size()));
176 if (length == 0 || length >= self.size()) return ~0u;
177 self.resize(length);
178 std::wstring command = L"\"" + self + L"\"";
179 ::SetEnvironmentVariableW(kChildPackage, package.c_str());
180 ::SetEnvironmentVariableW(kChildOverlay, overlay.c_str());
181 STARTUPINFOW startup{};
182 startup.cb = sizeof(startup);
183 PROCESS_INFORMATION process{};
184 const BOOL started = ::CreateProcessW(self.c_str(), command.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr,
185 &startup, &process);
186 ::SetEnvironmentVariableW(kChildPackage, nullptr);
187 ::SetEnvironmentVariableW(kChildOverlay, nullptr);
188 if (!started) return ~0u;
189 DWORD code = ~0u;
190 if (::WaitForSingleObject(process.hProcess, 60000) == WAIT_OBJECT_0) ::GetExitCodeProcess(process.hProcess, &code);
191 pid = process.dwProcessId;
192 ::CloseHandle(process.hThread);
193 ::CloseHandle(process.hProcess);
194 return code;
195}
196
120// Denies the current user `rights` on a folder while it lives and puts the folder's old DACL back when it goes, on 197// Denies the current user `rights` on a folder while it lives and puts the folder's old DACL back when it goes, on
121// every way out of the check. Both changes go through one handle opened before the deny. Under Wine the DACL is the 198// every way out of the check. Both changes go through one handle opened before the deny. Under Wine the DACL is the
122// folder's unix mode: a deny of FILE_LIST_DIRECTORY clears its owner and group read bits (server/file.c, sd_to_mode), 199// folder's unix mode: a deny of FILE_LIST_DIRECTORY clears its owner and group read bits (server/file.c, sd_to_mode),
@@ -183,9 +260,68 @@ bool host_renames_case(const std::filesystem::path& folder) {
183 260
184void run_checks(const std::filesystem::path& root); 261void run_checks(const std::filesystem::path& root);
185 262
263// Title memory committed on first touch, as xmem maps physical pages: a view of a reserve-only pagefile section whose
264// pages a vectored handler commits when user-mode code faults on them. The kernel's write in ReadFile raises no such
265// fault, so the host ReadFile fails with ERROR_NOACCESS; the shim commits the pages and reads again.
266char* g_first_touch = nullptr;
267SIZE_T g_first_touch_size = 0;
268
269LONG CALLBACK commit_on_touch(EXCEPTION_POINTERS* info) {
270 const EXCEPTION_RECORD* r = info->ExceptionRecord;
271 if (r->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || r->NumberParameters < 2) return EXCEPTION_CONTINUE_SEARCH;
272 auto* at = reinterpret_cast<char*>(r->ExceptionInformation[1]);
273 if (at < g_first_touch || at >= g_first_touch + g_first_touch_size) return EXCEPTION_CONTINUE_SEARCH;
274 auto* page = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(at) & ~uintptr_t{0xfff});
275 return VirtualAlloc(page, 0x1000, MEM_COMMIT, PAGE_READWRITE) ? EXCEPTION_CONTINUE_EXECUTION
276 : EXCEPTION_CONTINUE_SEARCH;
277}
278
279void check_read_file_first_touch(const std::filesystem::path& root) {
280 using ReadFileFn = BOOL(WINAPI*)(HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
281 const auto shim_read = reinterpret_cast<ReadFileFn>(rt::shims::find("kernel32.dll", "ReadFile"));
282 check(shim_read != nullptr, "ReadFile is served by the file shims");
283 if (!shim_read) return;
284
285 std::filesystem::create_directories(root);
286 const std::filesystem::path file = root / "first_touch.bin";
287 constexpr DWORD kSize = 3 * 0x10000 + 123; // crosses several pages and ends inside one
288 std::vector<unsigned char> data(kSize);
289 for (DWORD i = 0; i < kSize; ++i) data[i] = static_cast<unsigned char>(i * 7 + 3);
290 std::ofstream(file, std::ios::binary).write(reinterpret_cast<const char*>(data.data()), kSize);
291
292 g_first_touch_size = 4 * 0x10000;
293 HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE | SEC_RESERVE, 0,
294 static_cast<DWORD>(g_first_touch_size), nullptr);
295 g_first_touch = section ? static_cast<char*>(MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, 0)) : nullptr;
296 check(g_first_touch != nullptr, "a reserve-only section view stands in for first-touch title memory");
297 if (!g_first_touch) return;
298 void* const handler = AddVectoredExceptionHandler(1, &commit_on_touch);
299
300 auto read_into_view = [&](ReadFileFn read) {
301 HANDLE h = CreateFileW(file.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
302 DWORD got = 0;
303 const BOOL ok = read(h, g_first_touch + 0x800, kSize, &got, nullptr);
304 const DWORD error = ok ? 0 : GetLastError();
305 CloseHandle(h);
306 return std::make_pair(ok ? got : 0, error);
307 };
308 const auto host = read_into_view(&::ReadFile);
309 check(host.second == ERROR_NOACCESS, "the host ReadFile into uncommitted pages fails with ERROR_NOACCESS");
310 const auto shimmed = read_into_view(shim_read);
311 check(shimmed.second == 0 && shimmed.first == kSize, "the shimmed ReadFile commits the pages and reads it all");
312 check(std::memcmp(g_first_touch + 0x800, data.data(), kSize) == 0, "the bytes read are the file's");
313
314 RemoveVectoredExceptionHandler(handler);
315 UnmapViewOfFile(g_first_touch);
316 CloseHandle(section);
317 g_first_touch = nullptr;
318}
319
186} // namespace 320} // namespace
187 321
188int main() { 322int main() {
323 if (const std::wstring child_overlay = environment(kChildOverlay); !child_overlay.empty())
324 return t_drive_child(environment(kChildPackage), child_overlay);
189 // A folder of this run's own: Wine hands each fresh prefix the same small process ids, so a folder named after 325 // A folder of this run's own: Wine hands each fresh prefix the same small process ids, so a folder named after
190 // the process id alone can be one an earlier run left behind. 326 // the process id alone can be one an earlier run left behind.
191 LARGE_INTEGER now{}; 327 LARGE_INTEGER now{};
@@ -198,6 +334,7 @@ int main() {
198 // An exception from one check (std::filesystem throws on a host error) ends the checks, not the run: it is a 334 // An exception from one check (std::filesystem throws on a host error) ends the checks, not the run: it is a
199 // failure, the folder is still removed and the summary is still printed. 335 // failure, the folder is still removed and the summary is still printed.
200 try { 336 try {
337 check_read_file_first_touch(root / "first_touch");
201 run_checks(root); 338 run_checks(root);
202 } catch (const std::exception& e) { 339 } catch (const std::exception& e) {
203 check(false, (std::string("the checks run to the end (stopped by: ") + e.what() + ")").c_str()); 340 check(false, (std::string("the checks run to the end (stopped by: ") + e.what() + ")").c_str());
@@ -492,8 +629,8 @@ void run_checks(const std::filesystem::path& root) {
492 check(delete_w(L"GAME:\\data\\bye.txt") == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND, 629 check(delete_w(L"GAME:\\data\\bye.txt") == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
493 "deleting an already hidden file reports ERROR_FILE_NOT_FOUND"); 630 "deleting an already hidden file reports ERROR_FILE_NOT_FOUND");
494 SetLastError(0); 631 SetLastError(0);
495 check(delete_w(L"GAME:\\nope\\missing.txt") == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND, 632 check(delete_w(L"GAME:\\nope\\missing.txt") == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
496 "deleting a file that never existed reports ERROR_FILE_NOT_FOUND"); 633 "deleting a file in a folder that never existed reports ERROR_PATH_NOT_FOUND");
497 634
498 // Deleting a nested package file creates parent directories inside .gta6rt-deleted/; those marker parent 635 // Deleting a nested package file creates parent directories inside .gta6rt-deleted/; those marker parent
499 // directories must not hide the corresponding ancestor directories in the package. 636 // directories must not hide the corresponding ancestor directories in the package.
@@ -709,6 +846,49 @@ void run_checks(const std::filesystem::path& root) {
709 "and the new folder is empty: only . and .."); 846 "and the new folder is empty: only . and ..");
710 } 847 }
711 848
849 {
850 const std::filesystem::path host = root / "host_missing";
851 write_path(host / "file.txt", "file");
852 const std::wstring host_dest = (host / "dest.txt").wstring();
853 auto error_of = [](BOOL ok) { return ok ? DWORD{0} : GetLastError(); };
854 struct Name {
855 const wchar_t* game;
856 std::filesystem::path host;
857 DWORD expected;
858 const char* what;
859 };
860 const Name names[] = {
861 {L"GAME:\\nowhere\\x.txt", host / "nowhere" / "x.txt", ERROR_PATH_NOT_FOUND, "inside a missing folder"},
862 {L"GAME:\\misc\\keep.txt\\x.txt", host / "file.txt" / "x.txt", ERROR_PATH_NOT_FOUND, "below a file"},
863 {L"GAME:\\misc\\absent.txt", host / "absent.txt", ERROR_FILE_NOT_FOUND, "missing from a folder that exists"},
864 };
865 for (const Name& n : names) {
866 const std::wstring native = n.host.wstring();
867 const std::string suffix = std::string(" of a name ") + n.what + " fails as on the host";
868 SetLastError(0);
869 const DWORD host_delete = error_of(::DeleteFileW(native.c_str()));
870 SetLastError(0);
871 check(error_of(delete_w(n.game)) == n.expected && host_delete == n.expected, ("DeleteFileW" + suffix).c_str());
872 SetLastError(0);
873 const DWORD host_rmdir = error_of(::RemoveDirectoryW(native.c_str()));
874 SetLastError(0);
875 check(error_of(rmdir_w(n.game)) == n.expected && host_rmdir == n.expected,
876 ("RemoveDirectoryW" + suffix).c_str());
877 SetLastError(0);
878 const DWORD host_setattr = error_of(::SetFileAttributesW(native.c_str(), FILE_ATTRIBUTE_NORMAL));
879 SetLastError(0);
880 check(error_of(setattr_w(n.game, FILE_ATTRIBUTE_NORMAL)) == n.expected && host_setattr == n.expected,
881 ("SetFileAttributesW" + suffix).c_str());
882 SetLastError(0);
883 const DWORD host_move = error_of(::MoveFileExW(native.c_str(), host_dest.c_str(), MOVEFILE_REPLACE_EXISTING));
884 SetLastError(0);
885 check(error_of(move_ex_w(n.game, L"GAME:\\misc\\dest.txt", MOVEFILE_REPLACE_EXISTING)) == n.expected &&
886 host_move == n.expected,
887 ("MoveFileExW" + suffix).c_str());
888 }
889 check(!std::filesystem::exists(overlay / "nowhere") && !std::filesystem::exists(overlay / "misc" / "dest.txt"),
890 "and none of them leaves anything in the overlay");
891 }
712 // Recreating a removed package directory clears its own marker, but not nested deletion markers. 892 // Recreating a removed package directory clears its own marker, but not nested deletion markers.
713 { 893 {
714 std::filesystem::create_directories(package / "recreate_dir"); 894 std::filesystem::create_directories(package / "recreate_dir");
@@ -748,6 +928,75 @@ void run_checks(const std::filesystem::path& root) {
748 } 928 }
749 } 929 }
750 930
931 // RemoveDirectory of a folder on one thread and, on another, a creating open of a file in it (CREATE_NEW,
932 // CREATE_ALWAYS, OPEN_ALWAYS) or CreateDirectory of a folder in it. The host orders the two: the remove goes first
933 // and the create fails with ERROR_PATH_NOT_FOUND, or the create goes first and the remove fails with
934 // ERROR_DIR_NOT_EMPTY. Both succeeding left the removed folder back in the title's view with the new entry in it.
935 // Each round takes a folder of its own that only the package has, empty in the title's view, and starts the two
936 // threads together; the remover waits a few more spins each round so the calls meet at different points.
937 {
938 constexpr int kRounds = 2000;
939 const auto race = [&](bool make_folder, const wchar_t* prefix, const char* what) {
940 for (int i = 0; i < kRounds; ++i)
941 std::filesystem::create_directories(package / "rmrace" / (prefix + std::to_wstring(i)));
942 int create_first = 0, remove_first = 0, both = 0, other = 0, back = 0;
943 for (int i = 0; i < kRounds; ++i) {
944 const std::wstring folder = L"GAME:\\rmrace\\" + (prefix + std::to_wstring(i));
945 const std::wstring child = folder + (make_folder ? L"\\sub" : L"\\new.tmp");
946 const DWORD disposition = i % 3 == 0 ? CREATE_NEW : i % 3 == 1 ? CREATE_ALWAYS : OPEN_ALWAYS;
947 std::atomic<int> ready{0};
948 HANDLE handle = INVALID_HANDLE_VALUE;
949 bool made = false;
950 DWORD create_error = 0;
951 std::thread creator([&] {
952 ready.fetch_add(1);
953 while (ready.load() < 2) {
954 }
955 if (make_folder) {
956 made = mkdir_w(child.c_str(), nullptr) != FALSE;
957 } else {
958 handle = create_w(child.c_str(), GENERIC_WRITE, 0, nullptr, disposition, 0, nullptr);
959 made = handle != INVALID_HANDLE_VALUE;
960 }
961 create_error = made ? 0 : GetLastError();
962 });
963 ready.fetch_add(1);
964 while (ready.load() < 2) {
965 }
966 for (int spin = 0; spin < (i % 64) * 64; ++spin) YieldProcessor();
967 const bool removed = rmdir_w(folder.c_str()) != FALSE;
968 const DWORD remove_error = removed ? 0 : GetLastError();
969 creator.join();
970 if (handle != INVALID_HANDLE_VALUE) ::CloseHandle(handle);
971 if (made && removed) {
972 ++both;
973 } else if (made && remove_error == ERROR_DIR_NOT_EMPTY) {
974 ++create_first;
975 } else if (removed && create_error == ERROR_PATH_NOT_FOUND) {
976 ++remove_first;
977 } else {
978 ++other;
979 }
980 // A folder the remove took away stays away, whatever the create did.
981 if (removed) {
982 SetLastError(0);
983 if (attributes_w(folder.c_str()) != INVALID_FILE_ATTRIBUTES || GetLastError() != ERROR_FILE_NOT_FOUND)
984 ++back;
985 }
986 }
987 std::printf("NOTE %s against RemoveDirectoryW, %d rounds: create first %d, remove first %d, both succeeded "
988 "%d, other %d, removed folder back %d\n",
989 what, kRounds, create_first, remove_first, both, other, back);
990 const std::string w = what;
991 check(both == 0, (w + " and RemoveDirectoryW of its folder never both succeed").c_str());
992 check(other == 0, (w + " or RemoveDirectoryW fails as on the host (ERROR_PATH_NOT_FOUND or "
993 "ERROR_DIR_NOT_EMPTY)").c_str());
994 check(back == 0, (w + ": a folder RemoveDirectoryW removed stays removed").c_str());
995 };
996 race(false, L"f", "a creating CreateFileW");
997 race(true, L"d", "CreateDirectoryW");
998 }
999
751 // MoveFile: the destination is in the overlay; a moved package file is deleted by a marker. 1000 // MoveFile: the destination is in the overlay; a moved package file is deleted by a marker.
752 { 1001 {
753 check(move_w(L"GAME:\\misc\\one.txt", L"GAME:\\misc\\moved.txt") != FALSE, "MoveFileW moves a package file"); 1002 check(move_w(L"GAME:\\misc\\one.txt", L"GAME:\\misc\\moved.txt") != FALSE, "MoveFileW moves a package file");
@@ -777,7 +1026,9 @@ void run_checks(const std::filesystem::path& root) {
777 check(move_w(L"GAME:\\misc", L"GAME:\\misc2") == FALSE && GetLastError() == ERROR_ACCESS_DENIED, 1026 check(move_w(L"GAME:\\misc", L"GAME:\\misc2") == FALSE && GetLastError() == ERROR_ACCESS_DENIED,
778 "a package folder is not moved"); 1027 "a package folder is not moved");
779 SetLastError(0); 1028 SetLastError(0);
780 check(move_w(L"GAME:\\misc\\keep.txt", (root / "host.txt").c_str()) == FALSE && 1029 // G: and T: outside the configured roots are console aliases, even when TEMP uses that host drive.
1030 // Paths within the synthetic package root are explicitly host paths on every drive.
1031 check(move_w(L"GAME:\\misc\\keep.txt", (package / "host.txt").c_str()) == FALSE &&
781 GetLastError() == ERROR_NOT_SAME_DEVICE, 1032 GetLastError() == ERROR_NOT_SAME_DEVICE,
782 "a move between GAME: and a host path is refused as cross-volume"); 1033 "a move between GAME: and a host path is refused as cross-volume");
783 SetLastError(0); 1034 SetLastError(0);
@@ -821,7 +1072,8 @@ void run_checks(const std::filesystem::path& root) {
821 check(copy_a("GAME:\\misc\\keep.txt", "GAME:\\misc\\target.txt", FALSE) != FALSE && 1072 check(copy_a("GAME:\\misc\\keep.txt", "GAME:\\misc\\target.txt", FALSE) != FALSE &&
822 read_path(overlay / "misc" / "target.txt") == "keep" && read_path(package / "misc" / "target.txt") == "target", 1073 read_path(overlay / "misc" / "target.txt") == "keep" && read_path(package / "misc" / "target.txt") == "target",
823 "CopyFileA overwrites through the overlay and leaves the package file alone"); 1074 "CopyFileA overwrites through the overlay and leaves the package file alone");
824 const std::filesystem::path host_copy = root / "host_copy.txt"; 1075 // Keep this host path inside a configured root too; an unqualified G:/T: path is virtual.
1076 const std::filesystem::path host_copy = package / "host_copy.txt";
825 check(copy_w(L"GAME:\\misc\\keep.txt", host_copy.c_str(), TRUE) != FALSE && read_path(host_copy) == "keep", 1077 check(copy_w(L"GAME:\\misc\\keep.txt", host_copy.c_str(), TRUE) != FALSE && read_path(host_copy) == "keep",
826 "a copy from GAME: to a host path works"); 1078 "a copy from GAME: to a host path works");
827 check(copy_ex_w(host_copy.c_str(), L"GAME:\\cache\\from_host.txt", nullptr, nullptr, nullptr, COPY_FILE_FAIL_IF_EXISTS) != 1079 check(copy_ex_w(host_copy.c_str(), L"GAME:\\cache\\from_host.txt", nullptr, nullptr, nullptr, COPY_FILE_FAIL_IF_EXISTS) !=
@@ -1195,18 +1447,18 @@ void run_checks(const std::filesystem::path& root) {
1195 "on a fresh overlay CreateDirectoryW(T:\\shadercache) succeeds"); 1447 "on a fresh overlay CreateDirectoryW(T:\\shadercache) succeeds");
1196 std::filesystem::path fresh = fresh_overlay(); 1448 std::filesystem::path fresh = fresh_overlay();
1197 check(copy_w(L"GAME:\\data\\hello.txt", L"T:\\hello.tmp", FALSE) != FALSE && 1449 check(copy_w(L"GAME:\\data\\hello.txt", L"T:\\hello.tmp", FALSE) != FALSE &&
1198 read_path(fresh / ".gta6rt-temp" / "hello.tmp") == "package", 1450 read_path(own_t_folder(fresh) / "hello.tmp") == "package",
1199 "on a fresh overlay CopyFileW(GAME:\\data\\hello.txt, T:\\hello.tmp) succeeds"); 1451 "on a fresh overlay CopyFileW(GAME:\\data\\hello.txt, T:\\hello.tmp) succeeds");
1200 fresh = fresh_overlay(); 1452 fresh = fresh_overlay();
1201 SetLastError(0); 1453 SetLastError(0);
1202 check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && 1454 check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED &&
1203 std::filesystem::is_directory(fresh / ".gta6rt-temp"), 1455 std::filesystem::is_directory(own_t_folder(fresh)),
1204 "T:\\ cannot be removed, like GAME:\\"); 1456 "T:\\ cannot be removed, like GAME:\\");
1205 fresh = fresh_overlay(); 1457 fresh = fresh_overlay();
1206 SetLastError(0); 1458 SetLastError(0);
1207 check(move_w(L"T:\\", L"GAME:\\moved") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && 1459 check(move_w(L"T:\\", L"GAME:\\moved") == FALSE && GetLastError() == ERROR_ACCESS_DENIED &&
1208 attributes_w(L"GAME:\\moved") == INVALID_FILE_ATTRIBUTES && 1460 attributes_w(L"GAME:\\moved") == INVALID_FILE_ATTRIBUTES &&
1209 std::filesystem::is_directory(fresh / ".gta6rt-temp"), 1461 std::filesystem::is_directory(own_t_folder(fresh)),
1210 "T:\\ cannot be moved"); 1462 "T:\\ cannot be moved");
1211 fresh_overlay(); 1463 fresh_overlay();
1212 attributes_w(L"T:\\"); 1464 attributes_w(L"T:\\");
@@ -1223,8 +1475,8 @@ void run_checks(const std::filesystem::path& root) {
1223 ::WriteFile(h, "scratch", 7, &written, nullptr); 1475 ::WriteFile(h, "scratch", 7, &written, nullptr);
1224 ::CloseHandle(h); 1476 ::CloseHandle(h);
1225 } 1477 }
1226 check(read_path(overlay / ".gta6rt-temp" / "cache" / "scratch.bin") == "scratch", 1478 check(read_path(own_t_folder(overlay) / "cache" / "scratch.bin") == "scratch",
1227 "a T: file lands in the overlay's scratch folder"); 1479 "a T: file lands in this process's scratch folder in the overlay");
1228 check(!std::filesystem::exists(package / "cache" / "scratch.bin") && !std::filesystem::exists(package / ".gta6rt-temp"), 1480 check(!std::filesystem::exists(package / "cache" / "scratch.bin") && !std::filesystem::exists(package / ".gta6rt-temp"),
1229 "and the package is untouched"); 1481 "and the package is untouched");
1230 h = create_w(L"t:/cache/scratch.bin", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); 1482 h = create_w(L"t:/cache/scratch.bin", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
@@ -1244,6 +1496,8 @@ void run_checks(const std::filesystem::path& root) {
1244 // as an explicit GAME: or G: path, refused like any other invalid name, in every spelling parse_console() 1496 // as an explicit GAME: or G: path, refused like any other invalid name, in every spelling parse_console()
1245 // normalises (RT-294). T:\ still names the scratch folder, and an ordinary dotted name is not reserved. 1497 // normalises (RT-294). T:\ still names the scratch folder, and an ordinary dotted name is not reserved.
1246 write_path(overlay / ".gta6rt-temp" / "runtime.tmp", "runtime"); 1498 write_path(overlay / ".gta6rt-temp" / "runtime.tmp", "runtime");
1499 if (const std::filesystem::path t_dir = own_t_folder(overlay); !t_dir.empty())
1500 write_path(t_dir / "runtime.tmp", "runtime");
1247 write_path(overlay / ".gta6rt-deleted" / "data" / "gone.txt", ""); 1501 write_path(overlay / ".gta6rt-deleted" / "data" / "gone.txt", "");
1248 write_path(overlay / ".gta6rt-other" / "ok.txt", "ok"); 1502 write_path(overlay / ".gta6rt-other" / "ok.txt", "ok");
1249 for (const wchar_t* reserved : {L"GAME:\\.gta6rt-temp\\runtime.tmp", L"GAME:\\.GTA6RT-TEMP\\runtime.tmp", 1503 for (const wchar_t* reserved : {L"GAME:\\.gta6rt-temp\\runtime.tmp", L"GAME:\\.GTA6RT-TEMP\\runtime.tmp",
@@ -1268,7 +1522,8 @@ void run_checks(const std::filesystem::path& root) {
1268 if (markers != INVALID_HANDLE_VALUE) find_close(markers); 1522 if (markers != INVALID_HANDLE_VALUE) find_close(markers);
1269 SetLastError(0); 1523 SetLastError(0);
1270 const HANDLE probe = create_w(L"GAME:\\.gta6rt-temp\\probe.txt", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr); 1524 const HANDLE probe = create_w(L"GAME:\\.gta6rt-temp\\probe.txt", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
1271 check(probe == INVALID_HANDLE_VALUE && !std::filesystem::exists(overlay / ".gta6rt-temp" / "probe.txt"), 1525 check(probe == INVALID_HANDLE_VALUE && !std::filesystem::exists(overlay / ".gta6rt-temp" / "probe.txt") &&
1526 !std::filesystem::exists(own_t_folder(overlay) / "probe.txt"),
1272 "a write through GAME:\\.gta6rt-temp creates nothing in the scratch folder"); 1527 "a write through GAME:\\.gta6rt-temp creates nothing in the scratch folder");
1273 if (probe != INVALID_HANDLE_VALUE) ::CloseHandle(probe); 1528 if (probe != INVALID_HANDLE_VALUE) ::CloseHandle(probe);
1274 check(mkdir_w(L"GAME:\\.gta6rt-copy\\sub", nullptr) == FALSE && !std::filesystem::exists(overlay / ".gta6rt-copy" / "sub"), 1529 check(mkdir_w(L"GAME:\\.gta6rt-copy\\sub", nullptr) == FALSE && !std::filesystem::exists(overlay / ".gta6rt-copy" / "sub"),
@@ -1297,6 +1552,157 @@ void run_checks(const std::filesystem::path& root) {
1297 rt::fs::configure(package, overlay); 1552 rt::fs::configure(package, overlay);
1298 } 1553 }
1299 1554
1555 // T: is the title's temporary drive, which on the console no other run shares: each process gets a folder of its
1556 // own below <overlay>\.gta6rt-temp. With one folder for every run, the title's crash dump and crash log, made with
1557 // CREATE_NEW, failed with ERROR_FILE_EXISTS on names an earlier run had left (old upstream issue 7913, measured
1558 // under Wine).
1559 {
1560 const std::filesystem::path t_overlay = root / "t_overlay";
1561 const std::filesystem::path temp = t_overlay / ".gta6rt-temp";
1562 // What earlier runs left: a file in the folder they all shared, and one in an earlier process's own folder.
1563 write_path(temp / "crash.dmp", "earlier run");
1564 write_path(temp / "20200101-000000-000-pid1" / "crash.dmp", "earlier process");
1565 write_path(package / "crash.dmp", "package"); // and a GAME: file of the same name
1566 rt::fs::configure(package, t_overlay);
1567
1568 SetLastError(0);
1569 HANDLE h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
1570 const DWORD create_error = h == INVALID_HANDLE_VALUE ? GetLastError() : 0;
1571 check(h != INVALID_HANDLE_VALUE,
1572 "CreateFileW(T:\\crash.dmp, CREATE_NEW) succeeds when earlier runs left that name in .gta6rt-temp");
1573 if (create_error) std::printf(" (failed with Win32 error %lu)\n", static_cast<unsigned long>(create_error));
1574 if (h != INVALID_HANDLE_VALUE) {
1575 DWORD written = 0;
1576 ::WriteFile(h, "this process", 12, &written, nullptr);
1577 ::CloseHandle(h);
1578 }
1579 const std::filesystem::path mine = own_t_folder(t_overlay);
1580 check(!mine.empty() && mine.parent_path() == temp && read_path(mine / "crash.dmp") == "this process",
1581 "T:\\crash.dmp lands in this process's own folder <overlay>\\.gta6rt-temp\\<start time>-pid<id>");
1582 const std::wstring name = mine.filename().wstring();
1583 const auto digits = [&](size_t from, size_t n) {
1584 if (name.size() < from + n) return false;
1585 for (size_t i = from; i < from + n; ++i)
1586 if (name[i] < L'0' || name[i] > L'9') return false;
1587 return true;
1588 };
1589 check(digits(0, 8) && name.size() > 20 && name[8] == L'-' && digits(9, 6) && name[15] == L'-' &&
1590 digits(16, 3) && name.compare(19, 4, L"-pid") == 0,
1591 "the folder is named YYYYMMDD-HHMMSS-mmm-pid<id>, the process's start time and id");
1592 check(name == rt::fs::scratch_stem(), "with no folder of that name yet, it is the stem itself");
1593 check(read_path(temp / "crash.dmp") == "earlier run" &&
1594 read_path(temp / "20200101-000000-000-pid1" / "crash.dmp") == "earlier process" &&
1595 read_path(package / "crash.dmp") == "package",
1596 "the earlier runs' files and the package file of the same name are left as they were");
1597
1598 // The same folder for every T: call of this process, also after configure() names the overlay again.
1599 rt::fs::configure(package, t_overlay);
1600 h = create_w(L"t:/crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
1601 check(h != INVALID_HANDLE_VALUE && read_handle(h) == "this process",
1602 "T: keeps naming the same folder for the rest of the process");
1603 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1604 const std::vector<std::wstring> t_names = list(first_w, next_w, find_close, L"T:\\*");
1605 check(count(t_names, L"crash.dmp") == 1 && !contains(t_names, L"20200101-000000-000-pid1") &&
1606 !contains(t_names, L".gta6rt-temp"),
1607 "a T:\\ listing shows this process's files only");
1608 SetLastError(0);
1609 check(create_w(L"T:\\..\\crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr) ==
1610 INVALID_HANDLE_VALUE &&
1611 GetLastError() == ERROR_INVALID_NAME,
1612 "T:\\.. cannot climb into the shared .gta6rt-temp folder");
1613 SetLastError(0);
1614 check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && std::filesystem::is_directory(mine),
1615 "this process's T:\\ folder cannot be removed, like GAME:\\");
1616
1617 // A second process on the same overlay gets a folder of its own, and its CREATE_NEW of the same name succeeds.
1618 DWORD child_pid = 0;
1619 const DWORD child_code = run_t_drive_child(package, t_overlay, child_pid);
1620 check(child_code == 0,
1621 "a second process's CreateFileW(T:\\crash.dmp, CREATE_NEW) on the same overlay succeeds");
1622 if (child_code != 0)
1623 std::printf(" (the second process exited with %lu)\n", static_cast<unsigned long>(child_code));
1624 const std::filesystem::path theirs = child_pid ? t_folder(t_overlay, child_pid) : std::filesystem::path();
1625 check(!theirs.empty() && theirs != mine && theirs.parent_path() == temp &&
1626 read_path(theirs / "crash.dmp") == "child process",
1627 "the second process wrote into a folder of its own");
1628 check(read_path(mine / "crash.dmp") == "this process", "and this process's T: file is unchanged");
1629
1630 // A reused process id in the same millisecond gives the same stem as a folder an earlier run kept. The folder
1631 // is reserved with a create-only call, so that one (and the next name, taken too) is never used again.
1632 const std::filesystem::path stale_overlay = root / "t_stale_overlay";
1633 const std::filesystem::path stale = stale_overlay / ".gta6rt-temp" / rt::fs::scratch_stem();
1634 const std::filesystem::path stale_2 = stale_overlay / ".gta6rt-temp" / (rt::fs::scratch_stem() + L"-2");
1635 write_path(stale / "crash.dmp", "same stem, earlier run");
1636 std::filesystem::create_directories(stale_2);
1637 rt::fs::configure(package, stale_overlay);
1638 h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
1639 check(h != INVALID_HANDLE_VALUE,
1640 "CreateFileW(T:\\crash.dmp, CREATE_NEW) succeeds when a kept folder has this process's own stem");
1641 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1642 check(rt::fs::scratch_dir() == stale_overlay / ".gta6rt-temp" / (rt::fs::scratch_stem() + L"-3") &&
1643 std::filesystem::exists(rt::fs::scratch_dir() / "crash.dmp"),
1644 "T: is <stem>-3 when <stem> and <stem>-2 already existed");
1645 check(read_path(stale / "crash.dmp") == "same stem, earlier run" && std::filesystem::is_empty(stale_2),
1646 "and the folders that already existed are left as they were");
1647
1648 // Concurrent first T: calls on a new overlay reserve one folder between them.
1649 const std::filesystem::path race_overlay = root / "t_race_overlay";
1650 rt::fs::configure(package, race_overlay);
1651 {
1652 std::vector<std::thread> threads;
1653 for (int i = 0; i < 8; ++i) threads.emplace_back([&] { attributes_w(L"T:\\"); });
1654 for (auto& thread : threads) thread.join();
1655 }
1656 size_t reserved = 0;
1657 for (const auto& entry : std::filesystem::directory_iterator(race_overlay / ".gta6rt-temp")) {
1658 (void)entry;
1659 ++reserved;
1660 }
1661 check(reserved == 1 && std::filesystem::is_directory(rt::fs::scratch_dir()),
1662 "eight threads making their first T: call at once reserve one folder");
1663 rt::fs::configure(package, t_overlay);
1664
1665 // GAME: is not affected: reads come from the package, writes land in the overlay's mirror of the package.
1666 h = create_w(L"GAME:\\crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
1667 check(h != INVALID_HANDLE_VALUE && read_handle(h) == "package",
1668 "GAME:\\crash.dmp still reads the package file");
1669 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1670 h = create_w(L"GAME:\\data\\t_written.txt", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
1671 check(h != INVALID_HANDLE_VALUE, "a GAME: write-open still succeeds");
1672 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1673 check(std::filesystem::exists(t_overlay / "data" / "t_written.txt") &&
1674 !std::filesystem::exists(mine / "data") &&
1675 !std::filesystem::exists(package / "data" / "t_written.txt"),
1676 "and lands at <overlay>\\data, not in T:'s folder or the package");
1677 rt::fs::configure(package, overlay);
1678 }
1679
1680 // reserve_unique_dir: a create-only CreateDirectoryW per name, so the folder it returns is new; a name that exists
1681 // (a folder or a file) is skipped, and after max_tries names the call fails instead of reusing one.
1682 {
1683 const std::filesystem::path base = root / "reserve";
1684 std::filesystem::create_directories(base / "stem");
1685 write_path(base / "stem-2" / "old.txt", "old");
1686 DWORD error = 1;
1687 const std::filesystem::path third = rt::fs::reserve_unique_dir(base, L"stem", error);
1688 check(third == base / "stem-3" && error == 0 && std::filesystem::is_directory(third) &&
1689 std::filesystem::is_empty(third),
1690 "reserve_unique_dir(base, stem) with stem and stem-2 taken makes stem-3, new and empty");
1691 check(read_path(base / "stem-2" / "old.txt") == "old", "and leaves the taken folders alone");
1692 check(rt::fs::reserve_unique_dir(base, L"stem", error) == base / "stem-4",
1693 "a second call never hands out stem-3 again: it makes stem-4");
1694 write_path(base / "stem-5", "a file");
1695 check(rt::fs::reserve_unique_dir(base, L"stem", error) == base / "stem-6",
1696 "a file with the next name counts as taken too");
1697 error = 0;
1698 check(rt::fs::reserve_unique_dir(base, L"stem", error, 6).empty() && error == ERROR_ALREADY_EXISTS,
1699 "with every name up to max_tries taken it fails with ERROR_ALREADY_EXISTS");
1700 const std::filesystem::path fresh_base = root / "reserve_new" / "deep";
1701 check(rt::fs::reserve_unique_dir(fresh_base, L"stem", error) == fresh_base / "stem" &&
1702 std::filesystem::is_directory(fresh_base / "stem"),
1703 "a missing base folder is made first, and the stem itself is the first name");
1704 }
1705
1300 // An open that writes needs its folder to exist in the title's view, and a failed open changes nothing. 1706 // An open that writes needs its folder to exist in the title's view, and a failed open changes nothing.
1301 { 1707 {
1302 const auto overlay_has = [&](const wchar_t* name) { return std::filesystem::exists(overlay / name); }; 1708 const auto overlay_has = [&](const wchar_t* name) { return std::filesystem::exists(overlay / name); };
@@ -1571,9 +1977,11 @@ void run_checks(const std::filesystem::path& root) {
1571 check(h != INVALID_HANDLE_VALUE && ::WriteFile(h, "test", 4, &written, nullptr) && written == 4, 1977 check(h != INVALID_HANDLE_VALUE && ::WriteFile(h, "test", 4, &written, nullptr) && written == 4,
1572 "T: creates and writes its scratch directory under a long overlay root"); 1978 "T: creates and writes its scratch directory under a long overlay root");
1573 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h); 1979 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1574 check(read_guest(L"T:\\scratch.bin") == "test" && 1980 const std::filesystem::path scratch_folder = rt::fs::scratch_dir(); // configured on scratch_overlay above
1575 read_path(extended(scratch_overlay / ".gta6rt-temp" / "scratch.bin")) == "test", 1981 check(read_guest(L"T:\\scratch.bin") == "test" && !scratch_folder.empty() &&
1576 "T: reads from the same long backing directory"); 1982 scratch_folder.parent_path() == scratch_overlay / ".gta6rt-temp" &&
1983 read_path(extended(scratch_folder / "scratch.bin")) == "test",
1984 "T: reads from the same long backing directory, this process's own folder in it");
1577 rt::fs::configure(package, overlay); 1985 rt::fs::configure(package, overlay);
1578 1986
1579 std::error_code cleanup_error; 1987 std::error_code cleanup_error;