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.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp
index fcc45d4fb..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>
@@ -927,6 +928,75 @@ void run_checks(const std::filesystem::path& root) {
927 } 928 }
928 } 929 }
929 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
930 // 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.
931 { 1001 {
932 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");