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:
Diffstat (limited to 'runtime/test/file_system_test.cpp')
-rw-r--r--runtime/test/file_system_test.cpp321
1 files changed, 312 insertions, 9 deletions
diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp
index 493ffd6bf..af37935f6 100644
--- a/runtime/test/file_system_test.cpp
+++ b/runtime/test/file_system_test.cpp
@@ -8,6 +8,7 @@
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>
12#include <cstring> 13#include <cstring>
13#include <cstdio> 14#include <cstdio>
@@ -118,6 +119,81 @@ int count(const std::vector<std::wstring>& names, const wchar_t* name) {
118 119
119bool 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; }
120 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
121// 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
122// 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
123// 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),
@@ -244,6 +320,8 @@ void check_read_file_first_touch(const std::filesystem::path& root) {
244} // namespace 320} // namespace
245 321
246int main() { 322int main() {
323 if (const std::wstring child_overlay = environment(kChildOverlay); !child_overlay.empty())
324 return t_drive_child(environment(kChildPackage), child_overlay);
247 // 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
248 // 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.
249 LARGE_INTEGER now{}; 327 LARGE_INTEGER now{};
@@ -850,6 +928,75 @@ void run_checks(const std::filesystem::path& root) {
850 } 928 }
851 } 929 }
852 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
853 // 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.
854 { 1001 {
855 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");
@@ -1274,18 +1421,18 @@ void run_checks(const std::filesystem::path& root) {
1274 "on a fresh overlay CreateDirectoryW(T:\\shadercache) succeeds"); 1421 "on a fresh overlay CreateDirectoryW(T:\\shadercache) succeeds");
1275 std::filesystem::path fresh = fresh_overlay(); 1422 std::filesystem::path fresh = fresh_overlay();
1276 check(copy_w(L"GAME:\\data\\hello.txt", L"T:\\hello.tmp", FALSE) != FALSE && 1423 check(copy_w(L"GAME:\\data\\hello.txt", L"T:\\hello.tmp", FALSE) != FALSE &&
1277 read_path(fresh / ".gta6rt-temp" / "hello.tmp") == "package", 1424 read_path(own_t_folder(fresh) / "hello.tmp") == "package",
1278 "on a fresh overlay CopyFileW(GAME:\\data\\hello.txt, T:\\hello.tmp) succeeds"); 1425 "on a fresh overlay CopyFileW(GAME:\\data\\hello.txt, T:\\hello.tmp) succeeds");
1279 fresh = fresh_overlay(); 1426 fresh = fresh_overlay();
1280 SetLastError(0); 1427 SetLastError(0);
1281 check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && 1428 check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED &&
1282 std::filesystem::is_directory(fresh / ".gta6rt-temp"), 1429 std::filesystem::is_directory(own_t_folder(fresh)),
1283 "T:\\ cannot be removed, like GAME:\\"); 1430 "T:\\ cannot be removed, like GAME:\\");
1284 fresh = fresh_overlay(); 1431 fresh = fresh_overlay();
1285 SetLastError(0); 1432 SetLastError(0);
1286 check(move_w(L"T:\\", L"GAME:\\moved") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && 1433 check(move_w(L"T:\\", L"GAME:\\moved") == FALSE && GetLastError() == ERROR_ACCESS_DENIED &&
1287 attributes_w(L"GAME:\\moved") == INVALID_FILE_ATTRIBUTES && 1434 attributes_w(L"GAME:\\moved") == INVALID_FILE_ATTRIBUTES &&
1288 std::filesystem::is_directory(fresh / ".gta6rt-temp"), 1435 std::filesystem::is_directory(own_t_folder(fresh)),
1289 "T:\\ cannot be moved"); 1436 "T:\\ cannot be moved");
1290 fresh_overlay(); 1437 fresh_overlay();
1291 attributes_w(L"T:\\"); 1438 attributes_w(L"T:\\");
@@ -1302,8 +1449,8 @@ void run_checks(const std::filesystem::path& root) {
1302 ::WriteFile(h, "scratch", 7, &written, nullptr); 1449 ::WriteFile(h, "scratch", 7, &written, nullptr);
1303 ::CloseHandle(h); 1450 ::CloseHandle(h);
1304 } 1451 }
1305 check(read_path(overlay / ".gta6rt-temp" / "cache" / "scratch.bin") == "scratch", 1452 check(read_path(own_t_folder(overlay) / "cache" / "scratch.bin") == "scratch",
1306 "a T: file lands in the overlay's scratch folder"); 1453 "a T: file lands in this process's scratch folder in the overlay");
1307 check(!std::filesystem::exists(package / "cache" / "scratch.bin") && !std::filesystem::exists(package / ".gta6rt-temp"), 1454 check(!std::filesystem::exists(package / "cache" / "scratch.bin") && !std::filesystem::exists(package / ".gta6rt-temp"),
1308 "and the package is untouched"); 1455 "and the package is untouched");
1309 h = create_w(L"t:/cache/scratch.bin", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); 1456 h = create_w(L"t:/cache/scratch.bin", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
@@ -1323,6 +1470,8 @@ void run_checks(const std::filesystem::path& root) {
1323 // as an explicit GAME: or G: path, refused like any other invalid name, in every spelling parse_console() 1470 // as an explicit GAME: or G: path, refused like any other invalid name, in every spelling parse_console()
1324 // normalises (RT-294). T:\ still names the scratch folder, and an ordinary dotted name is not reserved. 1471 // normalises (RT-294). T:\ still names the scratch folder, and an ordinary dotted name is not reserved.
1325 write_path(overlay / ".gta6rt-temp" / "runtime.tmp", "runtime"); 1472 write_path(overlay / ".gta6rt-temp" / "runtime.tmp", "runtime");
1473 if (const std::filesystem::path t_dir = own_t_folder(overlay); !t_dir.empty())
1474 write_path(t_dir / "runtime.tmp", "runtime");
1326 write_path(overlay / ".gta6rt-deleted" / "data" / "gone.txt", ""); 1475 write_path(overlay / ".gta6rt-deleted" / "data" / "gone.txt", "");
1327 write_path(overlay / ".gta6rt-other" / "ok.txt", "ok"); 1476 write_path(overlay / ".gta6rt-other" / "ok.txt", "ok");
1328 for (const wchar_t* reserved : {L"GAME:\\.gta6rt-temp\\runtime.tmp", L"GAME:\\.GTA6RT-TEMP\\runtime.tmp", 1477 for (const wchar_t* reserved : {L"GAME:\\.gta6rt-temp\\runtime.tmp", L"GAME:\\.GTA6RT-TEMP\\runtime.tmp",
@@ -1347,7 +1496,8 @@ void run_checks(const std::filesystem::path& root) {
1347 if (markers != INVALID_HANDLE_VALUE) find_close(markers); 1496 if (markers != INVALID_HANDLE_VALUE) find_close(markers);
1348 SetLastError(0); 1497 SetLastError(0);
1349 const HANDLE probe = create_w(L"GAME:\\.gta6rt-temp\\probe.txt", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr); 1498 const HANDLE probe = create_w(L"GAME:\\.gta6rt-temp\\probe.txt", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
1350 check(probe == INVALID_HANDLE_VALUE && !std::filesystem::exists(overlay / ".gta6rt-temp" / "probe.txt"), 1499 check(probe == INVALID_HANDLE_VALUE && !std::filesystem::exists(overlay / ".gta6rt-temp" / "probe.txt") &&
1500 !std::filesystem::exists(own_t_folder(overlay) / "probe.txt"),
1351 "a write through GAME:\\.gta6rt-temp creates nothing in the scratch folder"); 1501 "a write through GAME:\\.gta6rt-temp creates nothing in the scratch folder");
1352 if (probe != INVALID_HANDLE_VALUE) ::CloseHandle(probe); 1502 if (probe != INVALID_HANDLE_VALUE) ::CloseHandle(probe);
1353 check(mkdir_w(L"GAME:\\.gta6rt-copy\\sub", nullptr) == FALSE && !std::filesystem::exists(overlay / ".gta6rt-copy" / "sub"), 1503 check(mkdir_w(L"GAME:\\.gta6rt-copy\\sub", nullptr) == FALSE && !std::filesystem::exists(overlay / ".gta6rt-copy" / "sub"),
@@ -1376,6 +1526,157 @@ void run_checks(const std::filesystem::path& root) {
1376 rt::fs::configure(package, overlay); 1526 rt::fs::configure(package, overlay);
1377 } 1527 }
1378 1528
1529 // T: is the title's temporary drive, which on the console no other run shares: each process gets a folder of its
1530 // own below <overlay>\.gta6rt-temp. With one folder for every run, the title's crash dump and crash log, made with
1531 // CREATE_NEW, failed with ERROR_FILE_EXISTS on names an earlier run had left (old upstream issue 7913, measured
1532 // under Wine).
1533 {
1534 const std::filesystem::path t_overlay = root / "t_overlay";
1535 const std::filesystem::path temp = t_overlay / ".gta6rt-temp";
1536 // What earlier runs left: a file in the folder they all shared, and one in an earlier process's own folder.
1537 write_path(temp / "crash.dmp", "earlier run");
1538 write_path(temp / "20200101-000000-000-pid1" / "crash.dmp", "earlier process");
1539 write_path(package / "crash.dmp", "package"); // and a GAME: file of the same name
1540 rt::fs::configure(package, t_overlay);
1541
1542 SetLastError(0);
1543 HANDLE h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
1544 const DWORD create_error = h == INVALID_HANDLE_VALUE ? GetLastError() : 0;
1545 check(h != INVALID_HANDLE_VALUE,
1546 "CreateFileW(T:\\crash.dmp, CREATE_NEW) succeeds when earlier runs left that name in .gta6rt-temp");
1547 if (create_error) std::printf(" (failed with Win32 error %lu)\n", static_cast<unsigned long>(create_error));
1548 if (h != INVALID_HANDLE_VALUE) {
1549 DWORD written = 0;
1550 ::WriteFile(h, "this process", 12, &written, nullptr);
1551 ::CloseHandle(h);
1552 }
1553 const std::filesystem::path mine = own_t_folder(t_overlay);
1554 check(!mine.empty() && mine.parent_path() == temp && read_path(mine / "crash.dmp") == "this process",
1555 "T:\\crash.dmp lands in this process's own folder <overlay>\\.gta6rt-temp\\<start time>-pid<id>");
1556 const std::wstring name = mine.filename().wstring();
1557 const auto digits = [&](size_t from, size_t n) {
1558 if (name.size() < from + n) return false;
1559 for (size_t i = from; i < from + n; ++i)
1560 if (name[i] < L'0' || name[i] > L'9') return false;
1561 return true;
1562 };
1563 check(digits(0, 8) && name.size() > 20 && name[8] == L'-' && digits(9, 6) && name[15] == L'-' &&
1564 digits(16, 3) && name.compare(19, 4, L"-pid") == 0,
1565 "the folder is named YYYYMMDD-HHMMSS-mmm-pid<id>, the process's start time and id");
1566 check(name == rt::fs::scratch_stem(), "with no folder of that name yet, it is the stem itself");
1567 check(read_path(temp / "crash.dmp") == "earlier run" &&
1568 read_path(temp / "20200101-000000-000-pid1" / "crash.dmp") == "earlier process" &&
1569 read_path(package / "crash.dmp") == "package",
1570 "the earlier runs' files and the package file of the same name are left as they were");
1571
1572 // The same folder for every T: call of this process, also after configure() names the overlay again.
1573 rt::fs::configure(package, t_overlay);
1574 h = create_w(L"t:/crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
1575 check(h != INVALID_HANDLE_VALUE && read_handle(h) == "this process",
1576 "T: keeps naming the same folder for the rest of the process");
1577 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1578 const std::vector<std::wstring> t_names = list(first_w, next_w, find_close, L"T:\\*");
1579 check(count(t_names, L"crash.dmp") == 1 && !contains(t_names, L"20200101-000000-000-pid1") &&
1580 !contains(t_names, L".gta6rt-temp"),
1581 "a T:\\ listing shows this process's files only");
1582 SetLastError(0);
1583 check(create_w(L"T:\\..\\crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr) ==
1584 INVALID_HANDLE_VALUE &&
1585 GetLastError() == ERROR_INVALID_NAME,
1586 "T:\\.. cannot climb into the shared .gta6rt-temp folder");
1587 SetLastError(0);
1588 check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && std::filesystem::is_directory(mine),
1589 "this process's T:\\ folder cannot be removed, like GAME:\\");
1590
1591 // A second process on the same overlay gets a folder of its own, and its CREATE_NEW of the same name succeeds.
1592 DWORD child_pid = 0;
1593 const DWORD child_code = run_t_drive_child(package, t_overlay, child_pid);
1594 check(child_code == 0,
1595 "a second process's CreateFileW(T:\\crash.dmp, CREATE_NEW) on the same overlay succeeds");
1596 if (child_code != 0)
1597 std::printf(" (the second process exited with %lu)\n", static_cast<unsigned long>(child_code));
1598 const std::filesystem::path theirs = child_pid ? t_folder(t_overlay, child_pid) : std::filesystem::path();
1599 check(!theirs.empty() && theirs != mine && theirs.parent_path() == temp &&
1600 read_path(theirs / "crash.dmp") == "child process",
1601 "the second process wrote into a folder of its own");
1602 check(read_path(mine / "crash.dmp") == "this process", "and this process's T: file is unchanged");
1603
1604 // A reused process id in the same millisecond gives the same stem as a folder an earlier run kept. The folder
1605 // is reserved with a create-only call, so that one (and the next name, taken too) is never used again.
1606 const std::filesystem::path stale_overlay = root / "t_stale_overlay";
1607 const std::filesystem::path stale = stale_overlay / ".gta6rt-temp" / rt::fs::scratch_stem();
1608 const std::filesystem::path stale_2 = stale_overlay / ".gta6rt-temp" / (rt::fs::scratch_stem() + L"-2");
1609 write_path(stale / "crash.dmp", "same stem, earlier run");
1610 std::filesystem::create_directories(stale_2);
1611 rt::fs::configure(package, stale_overlay);
1612 h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
1613 check(h != INVALID_HANDLE_VALUE,
1614 "CreateFileW(T:\\crash.dmp, CREATE_NEW) succeeds when a kept folder has this process's own stem");
1615 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1616 check(rt::fs::scratch_dir() == stale_overlay / ".gta6rt-temp" / (rt::fs::scratch_stem() + L"-3") &&
1617 std::filesystem::exists(rt::fs::scratch_dir() / "crash.dmp"),
1618 "T: is <stem>-3 when <stem> and <stem>-2 already existed");
1619 check(read_path(stale / "crash.dmp") == "same stem, earlier run" && std::filesystem::is_empty(stale_2),
1620 "and the folders that already existed are left as they were");
1621
1622 // Concurrent first T: calls on a new overlay reserve one folder between them.
1623 const std::filesystem::path race_overlay = root / "t_race_overlay";
1624 rt::fs::configure(package, race_overlay);
1625 {
1626 std::vector<std::thread> threads;
1627 for (int i = 0; i < 8; ++i) threads.emplace_back([&] { attributes_w(L"T:\\"); });
1628 for (auto& thread : threads) thread.join();
1629 }
1630 size_t reserved = 0;
1631 for (const auto& entry : std::filesystem::directory_iterator(race_overlay / ".gta6rt-temp")) {
1632 (void)entry;
1633 ++reserved;
1634 }
1635 check(reserved == 1 && std::filesystem::is_directory(rt::fs::scratch_dir()),
1636 "eight threads making their first T: call at once reserve one folder");
1637 rt::fs::configure(package, t_overlay);
1638
1639 // GAME: is not affected: reads come from the package, writes land in the overlay's mirror of the package.
1640 h = create_w(L"GAME:\\crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
1641 check(h != INVALID_HANDLE_VALUE && read_handle(h) == "package",
1642 "GAME:\\crash.dmp still reads the package file");
1643 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1644 h = create_w(L"GAME:\\data\\t_written.txt", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
1645 check(h != INVALID_HANDLE_VALUE, "a GAME: write-open still succeeds");
1646 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1647 check(std::filesystem::exists(t_overlay / "data" / "t_written.txt") &&
1648 !std::filesystem::exists(mine / "data") &&
1649 !std::filesystem::exists(package / "data" / "t_written.txt"),
1650 "and lands at <overlay>\\data, not in T:'s folder or the package");
1651 rt::fs::configure(package, overlay);
1652 }
1653
1654 // reserve_unique_dir: a create-only CreateDirectoryW per name, so the folder it returns is new; a name that exists
1655 // (a folder or a file) is skipped, and after max_tries names the call fails instead of reusing one.
1656 {
1657 const std::filesystem::path base = root / "reserve";
1658 std::filesystem::create_directories(base / "stem");
1659 write_path(base / "stem-2" / "old.txt", "old");
1660 DWORD error = 1;
1661 const std::filesystem::path third = rt::fs::reserve_unique_dir(base, L"stem", error);
1662 check(third == base / "stem-3" && error == 0 && std::filesystem::is_directory(third) &&
1663 std::filesystem::is_empty(third),
1664 "reserve_unique_dir(base, stem) with stem and stem-2 taken makes stem-3, new and empty");
1665 check(read_path(base / "stem-2" / "old.txt") == "old", "and leaves the taken folders alone");
1666 check(rt::fs::reserve_unique_dir(base, L"stem", error) == base / "stem-4",
1667 "a second call never hands out stem-3 again: it makes stem-4");
1668 write_path(base / "stem-5", "a file");
1669 check(rt::fs::reserve_unique_dir(base, L"stem", error) == base / "stem-6",
1670 "a file with the next name counts as taken too");
1671 error = 0;
1672 check(rt::fs::reserve_unique_dir(base, L"stem", error, 6).empty() && error == ERROR_ALREADY_EXISTS,
1673 "with every name up to max_tries taken it fails with ERROR_ALREADY_EXISTS");
1674 const std::filesystem::path fresh_base = root / "reserve_new" / "deep";
1675 check(rt::fs::reserve_unique_dir(fresh_base, L"stem", error) == fresh_base / "stem" &&
1676 std::filesystem::is_directory(fresh_base / "stem"),
1677 "a missing base folder is made first, and the stem itself is the first name");
1678 }
1679
1379 // An open that writes needs its folder to exist in the title's view, and a failed open changes nothing. 1680 // An open that writes needs its folder to exist in the title's view, and a failed open changes nothing.
1380 { 1681 {
1381 const auto overlay_has = [&](const wchar_t* name) { return std::filesystem::exists(overlay / name); }; 1682 const auto overlay_has = [&](const wchar_t* name) { return std::filesystem::exists(overlay / name); };
@@ -1650,9 +1951,11 @@ void run_checks(const std::filesystem::path& root) {
1650 check(h != INVALID_HANDLE_VALUE && ::WriteFile(h, "test", 4, &written, nullptr) && written == 4, 1951 check(h != INVALID_HANDLE_VALUE && ::WriteFile(h, "test", 4, &written, nullptr) && written == 4,
1651 "T: creates and writes its scratch directory under a long overlay root"); 1952 "T: creates and writes its scratch directory under a long overlay root");
1652 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h); 1953 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
1653 check(read_guest(L"T:\\scratch.bin") == "test" && 1954 const std::filesystem::path scratch_folder = rt::fs::scratch_dir(); // configured on scratch_overlay above
1654 read_path(extended(scratch_overlay / ".gta6rt-temp" / "scratch.bin")) == "test", 1955 check(read_guest(L"T:\\scratch.bin") == "test" && !scratch_folder.empty() &&
1655 "T: reads from the same long backing directory"); 1956 scratch_folder.parent_path() == scratch_overlay / ".gta6rt-temp" &&
1957 read_path(extended(scratch_folder / "scratch.bin")) == "test",
1958 "T: reads from the same long backing directory, this process's own folder in it");
1656 rt::fs::configure(package, overlay); 1959 rt::fs::configure(package, overlay);
1657 1960
1658 std::error_code cleanup_error; 1961 std::error_code cleanup_error;