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:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 23:08:07 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 23:08:07 -0500
commit0c1437dfd988e5d7d57d5ffdf144b8ea7bd08391 (patch)
tree9730e263294374bdabf5bbb00dff98b91cec96cb /runtime/test/file_system_test.cpp
parent8d3a8336bc07b5e7347c202e484c50c1533213d4 (diff)
parent08fc8ec748e35bc79985cf040310a7898b5d694c (diff)
Merge pull request #3651 from kotorvr
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.cpp164
1 files changed, 161 insertions, 3 deletions
diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp
index 58233ee98..24385f718 100644
--- a/runtime/test/file_system_test.cpp
+++ b/runtime/test/file_system_test.cpp
@@ -7,6 +7,7 @@
7 7
8#include <cstdio> 8#include <cstdio>
9#include <filesystem> 9#include <filesystem>
10#include <initializer_list>
10#include <string> 11#include <string>
11#include <thread> 12#include <thread>
12#include <vector> 13#include <vector>
@@ -178,8 +179,10 @@ int main() {
178 if (r != INVALID_HANDLE_VALUE) ::CloseHandle(r); 179 if (r != INVALID_HANDLE_VALUE) ::CloseHandle(r);
179 } 180 }
180 181
181 // A new file, in folders the package does not have. 182 // A new file, in a folder the package does not have: the title makes the folder first, as it must on the host.
182 { 183 {
184 const auto make_folder = resolve<CreateDirectoryWFn>("kernel32.dll", "CreateDirectoryW");
185 check(make_folder && make_folder(L"GAME:\\save", nullptr) != FALSE, "CreateDirectoryW makes GAME:\\save");
183 const HANDLE h = create_w(L"GAME:\\save\\slot1.bin", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr); 186 const HANDLE h = create_w(L"GAME:\\save\\slot1.bin", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
184 check(h != INVALID_HANDLE_VALUE, "CREATE_ALWAYS makes a new console file"); 187 check(h != INVALID_HANDLE_VALUE, "CREATE_ALWAYS makes a new console file");
185 DWORD written = 0; 188 DWORD written = 0;
@@ -216,8 +219,8 @@ int main() {
216 SetLastError(0); 219 SetLastError(0);
217 WIN32_FIND_DATAW none{}; 220 WIN32_FIND_DATAW none{};
218 const HANDLE missing = first_w(L"GAME:\\missing\\*", &none); 221 const HANDLE missing = first_w(L"GAME:\\missing\\*", &none);
219 check(missing == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND, 222 check(missing == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
220 "a pattern with no match reports ERROR_FILE_NOT_FOUND"); 223 "a listing inside a folder that does not exist reports ERROR_PATH_NOT_FOUND, as the host does");
221 } 224 }
222 225
223 // A delete hides the package file without removing it. 226 // A delete hides the package file without removing it.
@@ -744,6 +747,161 @@ int main() {
744 rt::fs::configure(package, overlay); 747 rt::fs::configure(package, overlay);
745 } 748 }
746 749
750 // An open that writes needs its folder to exist in the title's view, and a failed open changes nothing.
751 {
752 const auto overlay_has = [&](const wchar_t* name) { return std::filesystem::exists(overlay / name); };
753 write_path(package / "rules" / "file.txt", "package");
754 write_path(package / "rules" / "gone" / "only.txt", "package");
755
756 SetLastError(0);
757 HANDLE h = create_w(L"GAME:\\nodir\\sub\\x.dat", GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0,
758 nullptr);
759 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
760 "a write-open of an existing file in a missing folder fails with ERROR_PATH_NOT_FOUND");
761 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
762 check(attributes_w(L"GAME:\\nodir") == INVALID_FILE_ATTRIBUTES && !overlay_has(L"nodir"),
763 "the failed open did not create the missing folders");
764
765 SetLastError(0);
766 h = create_w(L"GAME:\\nodir2\\x.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
767 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
768 "CREATE_ALWAYS in a missing folder fails with ERROR_PATH_NOT_FOUND, as on the host");
769 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
770 check(attributes_w(L"GAME:\\nodir2") == INVALID_FILE_ATTRIBUTES && !overlay_has(L"nodir2"),
771 "and creates neither the folder nor the file");
772
773 SetLastError(0);
774 h = create_w(L"GAME:\\rules\\file.txt\\x.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
775 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
776 "CREATE_ALWAYS below a package file fails with ERROR_PATH_NOT_FOUND");
777 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
778 const DWORD file_attributes = attributes_w(L"GAME:\\rules\\file.txt");
779 check(file_attributes != INVALID_FILE_ATTRIBUTES && (file_attributes & FILE_ATTRIBUTE_DIRECTORY) == 0,
780 "and the package file is still a file in the title's view");
781
782 check(delete_w(L"GAME:\\rules\\gone\\only.txt") != FALSE && rmdir_w(L"GAME:\\rules\\gone") != FALSE,
783 "a package folder is emptied and removed");
784 SetLastError(0);
785 h = create_w(L"GAME:\\rules\\gone\\new.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
786 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
787 "CREATE_ALWAYS inside a removed folder fails with ERROR_PATH_NOT_FOUND");
788 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
789 check(attributes_w(L"GAME:\\rules\\gone") == INVALID_FILE_ATTRIBUTES,
790 "and does not bring the removed folder back");
791
792 // The folders that do exist still work: one only the package has, and one the title made.
793 h = create_w(L"GAME:\\rules\\new.dat", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
794 check(h != INVALID_HANDLE_VALUE, "CREATE_NEW in a folder only the package has still succeeds");
795 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
796 check(overlay_has(L"rules\\new.dat") && !std::filesystem::exists(package / "rules" / "new.dat"),
797 "and the file is in the overlay");
798 check(mkdir_w(L"GAME:\\rules\\made", nullptr) != FALSE, "a folder the title makes");
799 h = create_w(L"GAME:\\rules\\made\\x.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
800 check(h != INVALID_HANDLE_VALUE, "takes a new file");
801 if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
802 }
803
804 // A listing needs its folder to exist in the title's view: a removed or missing folder is a missing path, not an
805 // empty folder and not a missing file.
806 {
807 write_path(package / "listing" / "old" / "a.txt", "package");
808 WIN32_FIND_DATAW found{};
809 check(delete_w(L"GAME:\\listing\\old\\a.txt") != FALSE && rmdir_w(L"GAME:\\listing\\old") != FALSE,
810 "a package folder is emptied and removed (listing)");
811 SetLastError(0);
812 HANDLE h = first_w(L"GAME:\\listing\\old\\*", &found);
813 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
814 "FindFirstFileW inside a removed package folder fails with ERROR_PATH_NOT_FOUND");
815 if (h != INVALID_HANDLE_VALUE) find_close(h);
816 SetLastError(0);
817 h = first_w(L"GAME:\\listing\\never\\*", &found);
818 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
819 "FindFirstFileW inside a folder that never existed fails with ERROR_PATH_NOT_FOUND");
820 if (h != INVALID_HANDLE_VALUE) find_close(h);
821 SetLastError(0);
822 h = first_w(L"GAME:\\listing\\old", &found);
823 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
824 "FindFirstFileW of the removed folder's own name is ERROR_FILE_NOT_FOUND: its parent exists");
825 if (h != INVALID_HANDLE_VALUE) find_close(h);
826 SetLastError(0);
827 h = first_w(L"GAME:\\listing\\nothing*.bin", &found);
828 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
829 "a pattern that matches nothing in an existing folder is still ERROR_FILE_NOT_FOUND");
830 if (h != INVALID_HANDLE_VALUE) find_close(h);
831 check(list(first_w, next_w, find_close, L"GAME:\\listing\\*").size() == 2,
832 "and the emptied parent lists only . and ..");
833
834 // A listing whose folder is a file is neither: the host answers ERROR_DIRECTORY. One level further down the
835 // path is simply missing again. (Case measured on the host and reported in review by shdann.)
836 write_path(package / "listing" / "file.txt", "package");
837 for (const wchar_t* pattern : {L"GAME:\\listing\\file.txt\\*", L"GAME:\\listing\\file.txt\\x"}) {
838 SetLastError(0);
839 h = first_w(pattern, &found);
840 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_DIRECTORY,
841 "FindFirstFileW below a file fails with ERROR_DIRECTORY, as the host does");
842 if (h != INVALID_HANDLE_VALUE) find_close(h);
843 }
844 SetLastError(0);
845 h = first_w(L"GAME:\\listing\\file.txt\\sub\\*", &found);
846 check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
847 "and two levels below a file with ERROR_PATH_NOT_FOUND");
848 if (h != INVALID_HANDLE_VALUE) find_close(h);
849 }
850
851 // A move onto the same name succeeds and changes nothing; a rename that changes only the case renames.
852 {
853 const auto names_in = [&](const wchar_t* pattern) { return list(first_w, next_w, find_close, pattern); };
854 const auto has_exact = [](const std::vector<std::wstring>& names, const wchar_t* name) {
855 for (const std::wstring& n : names)
856 if (n == name) return true;
857 return false;
858 };
859 write_path(package / "moves" / "pkg.dat", "package");
860 write_path(package / "moves" / "same.dat", "package");
861 check(mkdir_w(L"GAME:\\moves\\dir", nullptr) != FALSE, "an overlay folder for the case rename");
862 HANDLE h = create_w(L"GAME:\\moves\\save.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
863 DWORD written = 0;
864 if (h != INVALID_HANDLE_VALUE) {
865 ::WriteFile(h, "save", 4, &written, nullptr);
866 ::CloseHandle(h);
867 }
868
869 check(move_w(L"GAME:\\moves\\save.dat", L"GAME:\\moves\\save.dat") != FALSE,
870 "MoveFileW of an overlay file onto itself succeeds");
871 check(move_w(L"GAME:\\moves\\same.dat", L"GAME:\\moves\\same.dat") != FALSE,
872 "MoveFileW of a package file onto itself succeeds");
873 check(read_path(package / "moves" / "same.dat") == "package" && !std::filesystem::exists(overlay / "moves" / "same.dat"),
874 "and a package file moved onto itself is neither copied nor hidden");
875 HANDLE r = create_w(L"GAME:\\moves\\same.dat", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
876 check(r != INVALID_HANDLE_VALUE && read_handle(r) == "package", "it still reads");
877 if (r != INVALID_HANDLE_VALUE) ::CloseHandle(r);
878
879 check(move_w(L"GAME:\\moves\\save.dat", L"GAME:\\moves\\SAVE.DAT") != FALSE,
880 "MoveFileW renames an overlay file to the same name in another case");
881 std::vector<std::wstring> names = names_in(L"GAME:\\moves\\*");
882 check(has_exact(names, L"SAVE.DAT") && !has_exact(names, L"save.dat") && count(names, L"save.dat") == 1,
883 "the listing shows the new case, once");
884 check(read_path(overlay / "moves" / "SAVE.DAT") == "save", "and the bytes are the same");
885
886 check(move_w(L"GAME:\\moves\\pkg.dat", L"GAME:\\moves\\PKG.DAT") != FALSE,
887 "MoveFileW renames a package file to the same name in another case");
888 names = names_in(L"GAME:\\moves\\*");
889 check(has_exact(names, L"PKG.DAT") && !has_exact(names, L"pkg.dat") && count(names, L"pkg.dat") == 1,
890 "the listing shows the package file under its new case, once");
891 r = create_w(L"GAME:\\moves\\pkg.dat", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
892 check(r != INVALID_HANDLE_VALUE && read_handle(r) == "package", "and it opens under either case");
893 if (r != INVALID_HANDLE_VALUE) ::CloseHandle(r);
894 check(read_path(package / "moves" / "pkg.dat") == "package", "the package file is untouched");
895
896 check(move_w(L"GAME:\\moves\\dir", L"GAME:\\moves\\DIR") != FALSE,
897 "MoveFileW renames an overlay folder to the same name in another case");
898 check(has_exact(names_in(L"GAME:\\moves\\*"), L"DIR"), "the listing shows the folder's new case");
899 check(move_w(L"GAME:\\moves", L"GAME:\\moves") != FALSE, "MoveFileW of a package folder onto itself succeeds");
900 SetLastError(0);
901 check(move_w(L"GAME:\\moves", L"GAME:\\MOVES") == FALSE && GetLastError() == ERROR_ACCESS_DENIED,
902 "a case rename of a package folder is refused like any other move of one");
903 }
904
747 std::filesystem::remove_all(root); 905 std::filesystem::remove_all(root);
748 std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures); 906 std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures);
749 return g_failures == 0 ? 0 : 1; 907 return g_failures == 0 ? 0 : 1;