upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
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
parent8d3a8336bc07b5e7347c202e484c50c1533213d4 (diff)
parent08fc8ec748e35bc79985cf040310a7898b5d694c (diff)
Merge pull request #3651 from kotorvr
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--docs/runtime_imports.md8
-rw-r--r--runtime/shims/file_system.cpp45
-rw-r--r--runtime/test/file_system_test.cpp164
3 files changed, 213 insertions, 4 deletions
diff --git a/docs/runtime_imports.md b/docs/runtime_imports.md
index 790fbc0de..9a209e150 100644
--- a/docs/runtime_imports.md
+++ b/docs/runtime_imports.md
@@ -120,6 +120,14 @@ and the `api-ms-win-core-file-*` sets a GDK title links against.
120 dropped, and a `COPYFILE2` progress routine is not called. 120 dropped, and a `COPYFILE2` progress routine is not called.
121- `SetFileAttributes*` sets the attributes on the overlay copy, copying a package file up (or mirroring a package 121- `SetFileAttributes*` sets the attributes on the overlay copy, copying a package file up (or mirroring a package
122 folder) first, so the package keeps its own. 122 folder) first, so the package keeps its own.
123- A folder has to exist in the title's view before it is used. An open that writes fails with `ERROR_PATH_NOT_FOUND`
124 when its folder is missing, was removed, or is a file, and creates nothing (`T:` is the exception: a create there
125 makes its parent folders); `FindFirstFile` fails the same way for a
126 listing inside such a folder (a pattern that matches nothing in a folder that exists is still
127 `ERROR_FILE_NOT_FOUND`, and a listing directly below a file is `ERROR_DIRECTORY`, as on the host).
128- `MoveFile*` onto the same name succeeds and changes nothing. A rename that changes only the case renames the overlay
129 entry; a package file gets an overlay copy under the new spelling, and a package folder is refused like any other
130 move of one.
123 131
124`runtime/test/file_system_test.cpp` (ctest `file_system`) covers this over folders it makes in `%TEMP%`. 132`runtime/test/file_system_test.cpp` (ctest `file_system`) covers this over folders it makes in `%TEMP%`.
125 133
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp
index 014514597..7b932be3c 100644
--- a/runtime/shims/file_system.cpp
+++ b/runtime/shims/file_system.cpp
@@ -271,6 +271,14 @@ View view(const Roots& r, const std::vector<std::wstring>& parts) {
271 271
272bool is_folder(const Roots& r, const std::vector<std::wstring>& parts) { return view(r, parts).kind == Kind::Directory; } 272bool is_folder(const Roots& r, const std::vector<std::wstring>& parts) { return view(r, parts).kind == Kind::Directory; }
273 273
274// Whether two console paths name the same entry: names compare without case, as the lookups do.
275bool same_entry(const std::vector<std::wstring>& a, const std::vector<std::wstring>& b) {
276 if (a.size() != b.size()) return false;
277 for (size_t i = 0; i < a.size(); ++i)
278 if (!same_name(a[i].c_str(), b[i].c_str())) return false;
279 return true;
280}
281
274std::vector<std::wstring> parent_of(const std::vector<std::wstring>& parts) { 282std::vector<std::wstring> parent_of(const std::vector<std::wstring>& parts) {
275 return parts.empty() ? parts : std::vector<std::wstring>(parts.begin(), parts.end() - 1); 283 return parts.empty() ? parts : std::vector<std::wstring>(parts.begin(), parts.end() - 1);
276} 284}
@@ -348,6 +356,14 @@ Target write_target(const std::wstring& path, bool copy_up) {
348 if (r.overlay.empty()) return fail(ERROR_PATH_NOT_FOUND); 356 if (r.overlay.empty()) return fail(ERROR_PATH_NOT_FOUND);
349 if (parts.empty()) return fail(ERROR_INVALID_NAME); // "GAME:" is a folder, not a file 357 if (parts.empty()) return fail(ERROR_INVALID_NAME); // "GAME:" is a folder, not a file
350 358
359 // The folder has to exist in the title's view, as CreateDirectory, MoveFile and CopyFile require: without the
360 // check a failed open left the missing folders behind, and a create succeeded in a folder that was never made,
361 // below a package file (which then read as a folder) and inside a folder the title had removed. The overlay may
362 // still lack a folder only the package has, so it is mirrored here, which the title cannot see.
363 // T:'s scratch folder is the exception: it creates its parent folders, as merged with RT-218.
364 const bool scratch = !parts.empty() && same_name(parts.front().c_str(), kScratchDir);
365 if (!scratch && !is_folder(r, parent_of(parts))) return fail(ERROR_PATH_NOT_FOUND);
366
351 Target t; 367 Target t;
352 t.path = join(r.overlay, parts); 368 t.path = join(r.overlay, parts);
353 369
@@ -456,6 +472,14 @@ bool collect(const std::wstring& pattern, const std::vector<std::wstring>& parts
456 error = ERROR_PATH_NOT_FOUND; 472 error = ERROR_PATH_NOT_FOUND;
457 return false; 473 return false;
458 } 474 }
475 // The folder being listed has to exist in the title's view. The host would still enumerate a package folder the
476 // title removed (its "." and ".."), and a folder that is missing is a missing path, not a missing file. A listing
477 // below a file is a third answer on the host, ERROR_DIRECTORY.
478 const Kind listed = view(r, dirs).kind;
479 if (listed != Kind::Directory) {
480 error = listed == Kind::File ? ERROR_DIRECTORY : ERROR_PATH_NOT_FOUND;
481 return false;
482 }
459 483
460 auto add_root = [&](const std::filesystem::path& root, bool overlay_root) { 484 auto add_root = [&](const std::filesystem::path& root, bool overlay_root) {
461 if (root.empty()) return; 485 if (root.empty()) return;
@@ -911,12 +935,31 @@ BOOL WINAPI move_file_ex_w(LPCWSTR from, LPCWSTR to, DWORD flags) {
911 if (src.empty() || dst.empty()) return fail_bool(ERROR_ACCESS_DENIED); 935 if (src.empty() || dst.empty()) return fail_bool(ERROR_ACCESS_DENIED);
912 const View from_view = view(r, src); 936 const View from_view = view(r, src);
913 if (from_view.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND); 937 if (from_view.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND);
938 const std::filesystem::path overlay_from = join(r.overlay, src), overlay_to = join(r.overlay, dst);
939 if (same_entry(src, dst)) {
940 // The destination is the source, so it is not "already there". The host succeeds at a move onto the same
941 // name and changes nothing, and renames when only the case differs. Here the case of a name is the overlay's
942 // when the overlay holds it, so a package file gets an overlay copy under the new spelling (no marker: the
943 // listing takes the overlay's entry and skips the package's as the same name); a package folder is left
944 // where it is, as for any other move of one.
945 if (src == dst) return TRUE;
946 if (from_view.kind == Kind::Directory && from_view.in_package) return fail_bool(ERROR_ACCESS_DENIED);
947 if (from_view.in_overlay)
948 return ::MoveFileExW(overlay_from.c_str(), overlay_to.c_str(), flags & MOVEFILE_WRITE_THROUGH);
949 std::error_code same_ec;
950 std::filesystem::create_directories(overlay_to.parent_path(), same_ec);
951 if (same_ec) return fail_bool(static_cast<DWORD>(same_ec.value()));
952 if (!::CopyFileW(join(r.game, src).c_str(), overlay_to.c_str(), TRUE)) return FALSE;
953 DWORD flush_error = 0;
954 if ((flags & MOVEFILE_WRITE_THROUGH) != 0 && !flush_file(overlay_to, flush_error)) return fail_bool(flush_error);
955 return TRUE;
956 }
957
914 const View to_view = view(r, dst); 958 const View to_view = view(r, dst);
915 if (to_view.kind == Kind::Directory) return fail_bool(ERROR_ACCESS_DENIED); 959 if (to_view.kind == Kind::Directory) return fail_bool(ERROR_ACCESS_DENIED);
916 if (to_view.kind == Kind::File && (flags & MOVEFILE_REPLACE_EXISTING) == 0) return fail_bool(ERROR_ALREADY_EXISTS); 960 if (to_view.kind == Kind::File && (flags & MOVEFILE_REPLACE_EXISTING) == 0) return fail_bool(ERROR_ALREADY_EXISTS);
917 if (!is_folder(r, parent_of(dst))) return fail_bool(ERROR_PATH_NOT_FOUND); 961 if (!is_folder(r, parent_of(dst))) return fail_bool(ERROR_PATH_NOT_FOUND);
918 962
919 const std::filesystem::path overlay_from = join(r.overlay, src), overlay_to = join(r.overlay, dst);
920 std::error_code ec; 963 std::error_code ec;
921 std::filesystem::create_directories(overlay_to.parent_path(), ec); 964 std::filesystem::create_directories(overlay_to.parent_path(), ec);
922 if (ec) return fail_bool(static_cast<DWORD>(ec.value())); 965 if (ec) return fail_bool(static_cast<DWORD>(ec.value()));
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;