diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 07:02:11 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 07:05:40 -0500 |
| commit | de8cb53d5498ce914ba82bc02c6a3f39dc03f3bf (patch) | |
| tree | 48e08d747d41b9f46e18325d3fe11f45de34f0af /runtime/test/file_system_test.cpp | |
| parent | 21625bca8c675c62f9dbffc318ce6a59d57faf31 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/file_system_test.cpp')
| -rw-r--r-- | runtime/test/file_system_test.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
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 @@ | |||
| 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> |
| @@ -807,6 +808,75 @@ void run_checks(const std::filesystem::path& root) { | |||
| 807 | } | 808 | } |
| 808 | } | 809 | } |
| 809 | 810 | ||
| 811 | // RemoveDirectory of a folder on one thread and, on another, a creating open of a file in it (CREATE_NEW, | ||
| 812 | // CREATE_ALWAYS, OPEN_ALWAYS) or CreateDirectory of a folder in it. The host orders the two: the remove goes first | ||
| 813 | // and the create fails with ERROR_PATH_NOT_FOUND, or the create goes first and the remove fails with | ||
| 814 | // ERROR_DIR_NOT_EMPTY. Both succeeding left the removed folder back in the title's view with the new entry in it. | ||
| 815 | // Each round takes a folder of its own that only the package has, empty in the title's view, and starts the two | ||
| 816 | // threads together; the remover waits a few more spins each round so the calls meet at different points. | ||
| 817 | { | ||
| 818 | constexpr int kRounds = 2000; | ||
| 819 | const auto race = [&](bool make_folder, const wchar_t* prefix, const char* what) { | ||
| 820 | for (int i = 0; i < kRounds; ++i) | ||
| 821 | std::filesystem::create_directories(package / "rmrace" / (prefix + std::to_wstring(i))); | ||
| 822 | int create_first = 0, remove_first = 0, both = 0, other = 0, back = 0; | ||
| 823 | for (int i = 0; i < kRounds; ++i) { | ||
| 824 | const std::wstring folder = L"GAME:\\rmrace\\" + (prefix + std::to_wstring(i)); | ||
| 825 | const std::wstring child = folder + (make_folder ? L"\\sub" : L"\\new.tmp"); | ||
| 826 | const DWORD disposition = i % 3 == 0 ? CREATE_NEW : i % 3 == 1 ? CREATE_ALWAYS : OPEN_ALWAYS; | ||
| 827 | std::atomic<int> ready{0}; | ||
| 828 | HANDLE handle = INVALID_HANDLE_VALUE; | ||
| 829 | bool made = false; | ||
| 830 | DWORD create_error = 0; | ||
| 831 | std::thread creator([&] { | ||
| 832 | ready.fetch_add(1); | ||
| 833 | while (ready.load() < 2) { | ||
| 834 | } | ||
| 835 | if (make_folder) { | ||
| 836 | made = mkdir_w(child.c_str(), nullptr) != FALSE; | ||
| 837 | } else { | ||
| 838 | handle = create_w(child.c_str(), GENERIC_WRITE, 0, nullptr, disposition, 0, nullptr); | ||
| 839 | made = handle != INVALID_HANDLE_VALUE; | ||
| 840 | } | ||
| 841 | create_error = made ? 0 : GetLastError(); | ||
| 842 | }); | ||
| 843 | ready.fetch_add(1); | ||
| 844 | while (ready.load() < 2) { | ||
| 845 | } | ||
| 846 | for (int spin = 0; spin < (i % 64) * 64; ++spin) YieldProcessor(); | ||
| 847 | const bool removed = rmdir_w(folder.c_str()) != FALSE; | ||
| 848 | const DWORD remove_error = removed ? 0 : GetLastError(); | ||
| 849 | creator.join(); | ||
| 850 | if (handle != INVALID_HANDLE_VALUE) ::CloseHandle(handle); | ||
| 851 | if (made && removed) { | ||
| 852 | ++both; | ||
| 853 | } else if (made && remove_error == ERROR_DIR_NOT_EMPTY) { | ||
| 854 | ++create_first; | ||
| 855 | } else if (removed && create_error == ERROR_PATH_NOT_FOUND) { | ||
| 856 | ++remove_first; | ||
| 857 | } else { | ||
| 858 | ++other; | ||
| 859 | } | ||
| 860 | // A folder the remove took away stays away, whatever the create did. | ||
| 861 | if (removed) { | ||
| 862 | SetLastError(0); | ||
| 863 | if (attributes_w(folder.c_str()) != INVALID_FILE_ATTRIBUTES || GetLastError() != ERROR_FILE_NOT_FOUND) | ||
| 864 | ++back; | ||
| 865 | } | ||
| 866 | } | ||
| 867 | std::printf("NOTE %s against RemoveDirectoryW, %d rounds: create first %d, remove first %d, both succeeded " | ||
| 868 | "%d, other %d, removed folder back %d\n", | ||
| 869 | what, kRounds, create_first, remove_first, both, other, back); | ||
| 870 | const std::string w = what; | ||
| 871 | check(both == 0, (w + " and RemoveDirectoryW of its folder never both succeed").c_str()); | ||
| 872 | check(other == 0, (w + " or RemoveDirectoryW fails as on the host (ERROR_PATH_NOT_FOUND or " | ||
| 873 | "ERROR_DIR_NOT_EMPTY)").c_str()); | ||
| 874 | check(back == 0, (w + ": a folder RemoveDirectoryW removed stays removed").c_str()); | ||
| 875 | }; | ||
| 876 | race(false, L"f", "a creating CreateFileW"); | ||
| 877 | race(true, L"d", "CreateDirectoryW"); | ||
| 878 | } | ||
| 879 | |||
| 810 | // MoveFile: the destination is in the overlay; a moved package file is deleted by a marker. | 880 | // MoveFile: the destination is in the overlay; a moved package file is deleted by a marker. |
| 811 | { | 881 | { |
| 812 | check(move_w(L"GAME:\\misc\\one.txt", L"GAME:\\misc\\moved.txt") != FALSE, "MoveFileW moves a package file"); | 882 | check(move_w(L"GAME:\\misc\\one.txt", L"GAME:\\misc\\moved.txt") != FALSE, "MoveFileW moves a package file"); |