diff options
| author | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 19:08:54 +0100 |
|---|---|---|
| committer | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 19:08:54 +0100 |
| commit | 3af42c619253941610d9809e8a28a07e8dfdfdf2 (patch) | |
| tree | a675793195f8bc69c3caebb0e9614ecfb65e1c4c /runtime/shims/file_system.cpp | |
| parent | e6af0a0931639ff33cdd4a28289da984f194c87e (diff) | |
| parent | ec8ec4c9a46c6623f1a9c125afd3566da97f1e0c (diff) | |
merge: preserve upstream changes alongside the asset fix
Diffstat (limited to 'runtime/shims/file_system.cpp')
| -rw-r--r-- | runtime/shims/file_system.cpp | 513 |
1 files changed, 497 insertions, 16 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index f0ae08811..2e7d93718 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp | |||
| @@ -1,9 +1,10 @@ | |||
| 1 | // file_system: the file API of a mapped title, seen through the console's drives. | 1 | // file_system: the file API of a mapped title, seen through the console's drives. |
| 2 | // | 2 | // |
| 3 | // A GDK title names its files on the console's drives, "GAME:\\data\\x.sps" being one in the installed game folder, so | 3 | // A GDK title names its files on the console's drives, "GAME:\\data\\x.sps" being one in the installed game folder, so |
| 4 | // the imports it makes of CreateFile*, FindFirstFile*/FindNextFile*/FindClose, GetFileAttributes*/GetFileAttributesEx* | 4 | // the imports it makes of CreateFile*, FindFirstFile*/FindNextFile*/FindClose, GetFileAttributes*/GetFileAttributesEx*, |
| 5 | // and DeleteFile* are answered here (docs/runtime_imports.md, "Console paths and the write overlay"; the prefixes come | 5 | // DeleteFile*, CreateDirectory*/RemoveDirectory*, MoveFile*, CopyFile*/CopyFile2 and SetFileAttributes* are answered |
| 6 | // from ivall/xcl, GPL-2.0-only, as facts). The rules: | 6 | // here (docs/runtime_imports.md, "Console paths and the write overlay"; the prefixes come from ivall/xcl, |
| 7 | // GPL-2.0-only, as facts). The rules: | ||
| 7 | // - "GAME:" (any case, "\\" or "/") maps under --game-dir; any other path is a host path and goes to the host; | 8 | // - "GAME:" (any case, "\\" or "/") maps under --game-dir; any other path is a host path and goes to the host; |
| 8 | // - an open that can write, and DeleteFile, go to the overlay (--overlay, default <game-dir>\\overlay), so the | 9 | // - an open that can write, and DeleteFile, go to the overlay (--overlay, default <game-dir>\\overlay), so the |
| 9 | // package stays exactly as installed. A write-open of a package file copies it into the overlay first, so | 10 | // package stays exactly as installed. A write-open of a package file copies it into the overlay first, so |
| @@ -14,6 +15,8 @@ | |||
| 14 | // - reads prefer the overlay copy and then the package; | 15 | // - reads prefer the overlay copy and then the package; |
| 15 | // - FindFirstFile merges the two folders, the overlay winning on a name, and hides deleted names and the marker | 16 | // - FindFirstFile merges the two folders, the overlay winning on a name, and hides deleted names and the marker |
| 16 | // folder; | 17 | // folder; |
| 18 | // - folders are made in the overlay; a removed package folder is hidden by a marker, a moved package file is | ||
| 19 | // copied into the overlay and its old name deleted, and attributes are set on the overlay copy; | ||
| 17 | // - a path that climbs above the root with ".." or names another drive is refused with ERROR_INVALID_NAME, so a | 20 | // - a path that climbs above the root with ".." or names another drive is refused with ERROR_INVALID_NAME, so a |
| 18 | // title cannot reach outside the package and the overlay. | 21 | // title cannot reach outside the package and the overlay. |
| 19 | // Only images mapped by the loader see these: they are bound through the registry, the runtime's own code calls the | 22 | // Only images mapped by the loader see these: they are bound through the registry, the runtime's own code calls the |
| @@ -39,6 +42,9 @@ namespace { | |||
| 39 | constexpr wchar_t kGamePrefix[] = L"game:"; | 42 | constexpr wchar_t kGamePrefix[] = L"game:"; |
| 40 | constexpr size_t kGamePrefixLength = 5; | 43 | constexpr size_t kGamePrefixLength = 5; |
| 41 | constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; | 44 | constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; |
| 45 | // A removed package folder is marked by this file inside its marker folder: the marker folder itself can already | ||
| 46 | // exist because files below it were deleted, and that alone does not hide the folder. | ||
| 47 | constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; | ||
| 42 | 48 | ||
| 43 | std::mutex g_mutex; | 49 | std::mutex g_mutex; |
| 44 | std::filesystem::path g_game_dir; | 50 | std::filesystem::path g_game_dir; |
| @@ -139,12 +145,115 @@ std::filesystem::path marker(const std::filesystem::path& overlay, const std::ve | |||
| 139 | return join(overlay / kDeletedDir, parts); | 145 | return join(overlay / kDeletedDir, parts); |
| 140 | } | 146 | } |
| 141 | 147 | ||
| 148 | // A deleted file leaves an empty file at its marker path and a removed folder leaves kRemovedDirMarker inside its | ||
| 149 | // marker folder. | ||
| 142 | bool deleted(const std::filesystem::path& overlay, const std::vector<std::wstring>& parts) { | 150 | bool deleted(const std::filesystem::path& overlay, const std::vector<std::wstring>& parts) { |
| 143 | if (overlay.empty() || parts.empty()) return false; | 151 | if (overlay.empty() || parts.empty()) return false; |
| 144 | std::error_code ec; | 152 | std::error_code ec; |
| 145 | // Only regular files are deletion markers; parent directories inside .gta6rt-deleted/ exist only to hold markers | 153 | // Only regular files are deletion markers; parent directories inside .gta6rt-deleted/ exist only to hold markers |
| 146 | // for nested deleted files and must not hide the corresponding ancestor directories in the package. | 154 | // for nested deleted files and must not hide the corresponding ancestor directories in the package. |
| 147 | return std::filesystem::is_regular_file(marker(overlay, parts), ec); | 155 | const std::filesystem::path m = marker(overlay, parts); |
| 156 | return std::filesystem::is_regular_file(m, ec) || std::filesystem::is_regular_file(m / kRemovedDirMarker, ec); | ||
| 157 | } | ||
| 158 | |||
| 159 | // Writes an empty hidden marker file. On failure `error` holds the reason. | ||
| 160 | bool write_marker(const std::filesystem::path& mark, DWORD& error) { | ||
| 161 | std::error_code ec; | ||
| 162 | std::filesystem::create_directories(mark.parent_path(), ec); | ||
| 163 | if (ec) { | ||
| 164 | error = static_cast<DWORD>(ec.value()); | ||
| 165 | return false; | ||
| 166 | } | ||
| 167 | const HANDLE h = ::CreateFileW(mark.c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | ||
| 168 | nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, nullptr); | ||
| 169 | if (h == INVALID_HANDLE_VALUE) { | ||
| 170 | error = ::GetLastError(); | ||
| 171 | return false; | ||
| 172 | } | ||
| 173 | ::CloseHandle(h); | ||
| 174 | return true; | ||
| 175 | } | ||
| 176 | |||
| 177 | // Flushes a file the shim wrote on the title's behalf, for MOVEFILE_WRITE_THROUGH. On failure `error` holds the reason. | ||
| 178 | bool flush_file(const std::filesystem::path& path, DWORD& error) { | ||
| 179 | const HANDLE h = ::CreateFileW(path.c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | ||
| 180 | nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); | ||
| 181 | if (h == INVALID_HANDLE_VALUE) { | ||
| 182 | error = ::GetLastError(); | ||
| 183 | return false; | ||
| 184 | } | ||
| 185 | const BOOL ok = ::FlushFileBuffers(h); | ||
| 186 | if (!ok) error = ::GetLastError(); | ||
| 187 | ::CloseHandle(h); | ||
| 188 | return ok != FALSE; | ||
| 189 | } | ||
| 190 | |||
| 191 | // What a console path is in the title's view: the overlay copy if there is one, else the package's unless deleted. | ||
| 192 | enum class Kind { None, File, Directory }; | ||
| 193 | struct View { | ||
| 194 | Kind kind = Kind::None; | ||
| 195 | bool in_overlay = false; // the overlay holds the name | ||
| 196 | bool in_package = false; // the package holds the name and it is not deleted | ||
| 197 | }; | ||
| 198 | |||
| 199 | View view(const Roots& r, const std::vector<std::wstring>& parts) { | ||
| 200 | View v; | ||
| 201 | std::error_code ec; | ||
| 202 | if (parts.empty()) { // "GAME:" itself | ||
| 203 | v.kind = Kind::Directory; | ||
| 204 | v.in_package = !r.game.empty(); | ||
| 205 | return v; | ||
| 206 | } | ||
| 207 | if (!r.overlay.empty()) { | ||
| 208 | const std::filesystem::path p = join(r.overlay, parts); | ||
| 209 | if (std::filesystem::exists(p, ec)) { | ||
| 210 | v.in_overlay = true; | ||
| 211 | v.kind = std::filesystem::is_directory(p, ec) ? Kind::Directory : Kind::File; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | if (!r.game.empty() && !deleted(r.overlay, parts)) { | ||
| 215 | const std::filesystem::path p = join(r.game, parts); | ||
| 216 | if (std::filesystem::exists(p, ec)) { | ||
| 217 | v.in_package = true; | ||
| 218 | if (v.kind == Kind::None) v.kind = std::filesystem::is_directory(p, ec) ? Kind::Directory : Kind::File; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | return v; | ||
| 222 | } | ||
| 223 | |||
| 224 | bool is_folder(const Roots& r, const std::vector<std::wstring>& parts) { return view(r, parts).kind == Kind::Directory; } | ||
| 225 | |||
| 226 | std::vector<std::wstring> parent_of(const std::vector<std::wstring>& parts) { | ||
| 227 | return parts.empty() ? parts : std::vector<std::wstring>(parts.begin(), parts.end() - 1); | ||
| 228 | } | ||
| 229 | |||
| 230 | // Whether a folder has nothing in the title's view: no overlay entry and no package entry that is not deleted. | ||
| 231 | bool empty_in_view(const Roots& r, const std::vector<std::wstring>& parts) { | ||
| 232 | std::error_code ec; | ||
| 233 | if (!r.overlay.empty()) { | ||
| 234 | const std::filesystem::path p = join(r.overlay, parts); | ||
| 235 | for (auto it = std::filesystem::directory_iterator(p, ec); !ec && it != std::filesystem::directory_iterator(); | ||
| 236 | it.increment(ec)) { | ||
| 237 | if (parts.empty() && same_name(it->path().filename().c_str(), kDeletedDir)) continue; | ||
| 238 | return false; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | ec.clear(); | ||
| 242 | if (!r.game.empty()) { | ||
| 243 | const std::filesystem::path p = join(r.game, parts); | ||
| 244 | for (auto it = std::filesystem::directory_iterator(p, ec); !ec && it != std::filesystem::directory_iterator(); | ||
| 245 | it.increment(ec)) { | ||
| 246 | std::vector<std::wstring> child = parts; | ||
| 247 | child.push_back(it->path().filename().wstring()); | ||
| 248 | if (!deleted(r.overlay, child)) return false; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | return true; | ||
| 252 | } | ||
| 253 | |||
| 254 | BOOL fail_bool(DWORD error) { | ||
| 255 | SetLastError(error); | ||
| 256 | return FALSE; | ||
| 148 | } | 257 | } |
| 149 | 258 | ||
| 150 | Target read_target(const std::wstring& path) { | 259 | Target read_target(const std::wstring& path) { |
| @@ -369,6 +478,20 @@ HANDLE WINAPI create_file_a(LPCSTR name, DWORD access, DWORD share, LPSECURITY_A | |||
| 369 | return create_file_w(wide.c_str(), access, share, attributes, disposition, flags, template_file); | 478 | return create_file_w(wide.c_str(), access, share, attributes, disposition, flags, template_file); |
| 370 | } | 479 | } |
| 371 | 480 | ||
| 481 | HANDLE WINAPI create_file2(LPCWSTR name, DWORD access, DWORD share, DWORD disposition, | ||
| 482 | LPCREATEFILE2_EXTENDED_PARAMETERS params) { | ||
| 483 | if (!name) { | ||
| 484 | SetLastError(ERROR_INVALID_PARAMETER); | ||
| 485 | return INVALID_HANDLE_VALUE; | ||
| 486 | } | ||
| 487 | const Target t = open_target(name, access, disposition); | ||
| 488 | if (t.error) { | ||
| 489 | SetLastError(t.error); | ||
| 490 | return INVALID_HANDLE_VALUE; | ||
| 491 | } | ||
| 492 | return ::CreateFile2(t.path.c_str(), access, share, disposition, params); | ||
| 493 | } | ||
| 494 | |||
| 372 | DWORD WINAPI get_file_attributes_w(LPCWSTR name) { | 495 | DWORD WINAPI get_file_attributes_w(LPCWSTR name) { |
| 373 | if (!name) { | 496 | if (!name) { |
| 374 | SetLastError(ERROR_INVALID_PARAMETER); | 497 | SetLastError(ERROR_INVALID_PARAMETER); |
| @@ -439,6 +562,72 @@ HANDLE WINAPI find_first_file_w(LPCWSTR pattern, LPWIN32_FIND_DATAW data) { | |||
| 439 | return register_enumeration(std::move(e)); | 562 | return register_enumeration(std::move(e)); |
| 440 | } | 563 | } |
| 441 | 564 | ||
| 565 | HANDLE WINAPI find_first_file_ex_w(LPCWSTR pattern, FINDEX_INFO_LEVELS info_level, LPVOID data, | ||
| 566 | FINDEX_SEARCH_OPS search_op, LPVOID search_filter, DWORD additional_flags) { | ||
| 567 | if (!pattern || !data) { | ||
| 568 | SetLastError(ERROR_INVALID_PARAMETER); | ||
| 569 | return INVALID_HANDLE_VALUE; | ||
| 570 | } | ||
| 571 | std::vector<std::wstring> parts; | ||
| 572 | const Parsed parsed = parse_console(pattern, parts); | ||
| 573 | if (parsed == Parsed::Host) | ||
| 574 | return ::FindFirstFileExW(pattern, info_level, data, search_op, search_filter, additional_flags); | ||
| 575 | if (parsed == Parsed::Refused) { | ||
| 576 | SetLastError(ERROR_INVALID_NAME); | ||
| 577 | return INVALID_HANDLE_VALUE; | ||
| 578 | } | ||
| 579 | if (info_level != FindExInfoStandard && info_level != FindExInfoBasic) { | ||
| 580 | SetLastError(ERROR_INVALID_PARAMETER); | ||
| 581 | return INVALID_HANDLE_VALUE; | ||
| 582 | } | ||
| 583 | if (search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories) { | ||
| 584 | SetLastError(ERROR_INVALID_PARAMETER); | ||
| 585 | return INVALID_HANDLE_VALUE; | ||
| 586 | } | ||
| 587 | |||
| 588 | std::vector<WIN32_FIND_DATAW> entries; | ||
| 589 | DWORD error = 0; | ||
| 590 | if (!collect(pattern, parts, entries, error)) { | ||
| 591 | SetLastError(error); | ||
| 592 | return INVALID_HANDLE_VALUE; | ||
| 593 | } | ||
| 594 | if (search_op == FindExSearchLimitToDirectories) { | ||
| 595 | std::vector<WIN32_FIND_DATAW> dirs_only; | ||
| 596 | for (auto& entry : entries) | ||
| 597 | if ((entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) dirs_only.push_back(entry); | ||
| 598 | entries = std::move(dirs_only); | ||
| 599 | if (entries.empty()) { | ||
| 600 | SetLastError(ERROR_FILE_NOT_FOUND); | ||
| 601 | return INVALID_HANDLE_VALUE; | ||
| 602 | } | ||
| 603 | } | ||
| 604 | if (info_level == FindExInfoBasic) { | ||
| 605 | for (auto& entry : entries) entry.cAlternateFileName[0] = L'\0'; | ||
| 606 | } | ||
| 607 | auto e = std::make_unique<Enumeration>(); | ||
| 608 | e->entries = std::move(entries); | ||
| 609 | e->next = 1; | ||
| 610 | *static_cast<LPWIN32_FIND_DATAW>(data) = e->entries[0]; | ||
| 611 | return register_enumeration(std::move(e)); | ||
| 612 | } | ||
| 613 | |||
| 614 | HANDLE WINAPI find_first_file_ex_a(LPCSTR pattern, FINDEX_INFO_LEVELS info_level, LPVOID data, | ||
| 615 | FINDEX_SEARCH_OPS search_op, LPVOID search_filter, DWORD additional_flags) { | ||
| 616 | if (!pattern || !data) { | ||
| 617 | SetLastError(ERROR_INVALID_PARAMETER); | ||
| 618 | return INVALID_HANDLE_VALUE; | ||
| 619 | } | ||
| 620 | const std::wstring wide = widen_ansi(pattern); | ||
| 621 | std::vector<std::wstring> parts; | ||
| 622 | const Parsed parsed = parse_console(wide, parts); | ||
| 623 | if (parsed == Parsed::Host) | ||
| 624 | return ::FindFirstFileExA(pattern, info_level, data, search_op, search_filter, additional_flags); | ||
| 625 | WIN32_FIND_DATAW wide_data{}; | ||
| 626 | const HANDLE h = find_first_file_ex_w(wide.c_str(), info_level, &wide_data, search_op, search_filter, additional_flags); | ||
| 627 | if (h != INVALID_HANDLE_VALUE) to_ansi(wide_data, static_cast<LPWIN32_FIND_DATAA>(data)); | ||
| 628 | return h; | ||
| 629 | } | ||
| 630 | |||
| 442 | HANDLE WINAPI find_first_file_a(LPCSTR pattern, LPWIN32_FIND_DATAA data) { | 631 | HANDLE WINAPI find_first_file_a(LPCSTR pattern, LPWIN32_FIND_DATAA data) { |
| 443 | if (!pattern || !data) { | 632 | if (!pattern || !data) { |
| 444 | SetLastError(ERROR_INVALID_PARAMETER); | 633 | SetLastError(ERROR_INVALID_PARAMETER); |
| @@ -527,17 +716,8 @@ BOOL WINAPI delete_file_w(LPCWSTR name) { | |||
| 527 | return FALSE; | 716 | return FALSE; |
| 528 | } | 717 | } |
| 529 | // The package file stays where it is; the marker is what hides it. | 718 | // The package file stays where it is; the marker is what hides it. |
| 530 | const std::filesystem::path mark = marker(r.overlay, parts); | 719 | DWORD error = 0; |
| 531 | std::filesystem::create_directories(mark.parent_path(), ec); | 720 | if (!write_marker(marker(r.overlay, parts), error)) return fail_bool(error); |
| 532 | if (ec) { | ||
| 533 | SetLastError(static_cast<DWORD>(ec.value())); | ||
| 534 | return FALSE; | ||
| 535 | } | ||
| 536 | const HANDLE h = ::CreateFileW(mark.c_str(), GENERIC_WRITE, | ||
| 537 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, | ||
| 538 | CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, nullptr); | ||
| 539 | if (h == INVALID_HANDLE_VALUE) return FALSE; // CreateFileW's error | ||
| 540 | ::CloseHandle(h); | ||
| 541 | removed = true; | 721 | removed = true; |
| 542 | } | 722 | } |
| 543 | } | 723 | } |
| @@ -557,6 +737,287 @@ BOOL WINAPI delete_file_a(LPCSTR name) { | |||
| 557 | return delete_file_w(wide.c_str()); | 737 | return delete_file_w(wide.c_str()); |
| 558 | } | 738 | } |
| 559 | 739 | ||
| 740 | // ---- Folders: created in the overlay, removed by a marker when the package has them | ||
| 741 | |||
| 742 | namespace { | ||
| 743 | |||
| 744 | BOOL create_directory(LPCWSTR template_name, LPCWSTR name, LPSECURITY_ATTRIBUTES security) { | ||
| 745 | if (!name) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 746 | std::wstring template_path; | ||
| 747 | if (template_name) { | ||
| 748 | const Target t = read_target(template_name); | ||
| 749 | if (t.error) return fail_bool(t.error); | ||
| 750 | template_path = t.path.wstring(); | ||
| 751 | } | ||
| 752 | std::vector<std::wstring> parts; | ||
| 753 | const Parsed parsed = parse_console(name, parts); | ||
| 754 | if (parsed == Parsed::Host) | ||
| 755 | return template_name ? ::CreateDirectoryExW(template_path.c_str(), name, security) | ||
| 756 | : ::CreateDirectoryW(name, security); | ||
| 757 | if (parsed == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME); | ||
| 758 | |||
| 759 | const Roots r = roots(); | ||
| 760 | if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); | ||
| 761 | if (view(r, parts).kind != Kind::None) return fail_bool(ERROR_ALREADY_EXISTS); // "GAME:" included | ||
| 762 | if (!is_folder(r, parent_of(parts))) return fail_bool(ERROR_PATH_NOT_FOUND); | ||
| 763 | |||
| 764 | const std::filesystem::path target = join(r.overlay, parts); | ||
| 765 | std::error_code ec; | ||
| 766 | std::filesystem::create_directories(target.parent_path(), ec); | ||
| 767 | if (ec) return fail_bool(static_cast<DWORD>(ec.value())); | ||
| 768 | return template_name ? ::CreateDirectoryExW(template_path.c_str(), target.c_str(), security) | ||
| 769 | : ::CreateDirectoryW(target.c_str(), security); | ||
| 770 | } | ||
| 771 | |||
| 772 | } // namespace | ||
| 773 | |||
| 774 | BOOL WINAPI create_directory_w(LPCWSTR name, LPSECURITY_ATTRIBUTES security) { | ||
| 775 | return create_directory(nullptr, name, security); | ||
| 776 | } | ||
| 777 | |||
| 778 | BOOL WINAPI create_directory_a(LPCSTR name, LPSECURITY_ATTRIBUTES security) { | ||
| 779 | if (!name) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 780 | const std::wstring wide = widen_ansi(name); | ||
| 781 | return create_directory(nullptr, wide.c_str(), security); | ||
| 782 | } | ||
| 783 | |||
| 784 | BOOL WINAPI create_directory_ex_w(LPCWSTR template_name, LPCWSTR name, LPSECURITY_ATTRIBUTES security) { | ||
| 785 | if (!template_name) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 786 | return create_directory(template_name, name, security); | ||
| 787 | } | ||
| 788 | |||
| 789 | BOOL WINAPI create_directory_ex_a(LPCSTR template_name, LPCSTR name, LPSECURITY_ATTRIBUTES security) { | ||
| 790 | if (!template_name || !name) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 791 | const std::wstring wide_template = widen_ansi(template_name), wide = widen_ansi(name); | ||
| 792 | return create_directory(wide_template.c_str(), wide.c_str(), security); | ||
| 793 | } | ||
| 794 | |||
| 795 | BOOL WINAPI remove_directory_w(LPCWSTR name) { | ||
| 796 | if (!name) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 797 | std::vector<std::wstring> parts; | ||
| 798 | const Parsed parsed = parse_console(name, parts); | ||
| 799 | if (parsed == Parsed::Host) return ::RemoveDirectoryW(name); | ||
| 800 | if (parsed == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME); | ||
| 801 | |||
| 802 | const Roots r = roots(); | ||
| 803 | if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); | ||
| 804 | if (parts.empty()) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" itself stays | ||
| 805 | const View v = view(r, parts); | ||
| 806 | if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND); | ||
| 807 | if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY); | ||
| 808 | if (!empty_in_view(r, parts)) return fail_bool(ERROR_DIR_NOT_EMPTY); | ||
| 809 | |||
| 810 | if (v.in_overlay && !::RemoveDirectoryW(join(r.overlay, parts).c_str())) return FALSE; // its error | ||
| 811 | if (v.in_package) { | ||
| 812 | // The package folder stays where it is; the marker hides it, and its files are already hidden one by one. | ||
| 813 | DWORD error = 0; | ||
| 814 | if (!write_marker(marker(r.overlay, parts) / kRemovedDirMarker, error)) return fail_bool(error); | ||
| 815 | } | ||
| 816 | return TRUE; | ||
| 817 | } | ||
| 818 | |||
| 819 | BOOL WINAPI remove_directory_a(LPCSTR name) { | ||
| 820 | if (!name) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 821 | const std::wstring wide = widen_ansi(name); | ||
| 822 | return remove_directory_w(wide.c_str()); | ||
| 823 | } | ||
| 824 | |||
| 825 | // ---- Move, copy and attributes: the result lands in the overlay, a moved package name is deleted by a marker | ||
| 826 | |||
| 827 | BOOL WINAPI move_file_ex_w(LPCWSTR from, LPCWSTR to, DWORD flags) { | ||
| 828 | if (!from) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 829 | std::vector<std::wstring> src, dst; | ||
| 830 | const Parsed from_kind = parse_console(from, src); | ||
| 831 | const Parsed to_kind = to ? parse_console(to, dst) : Parsed::Host; | ||
| 832 | if (from_kind == Parsed::Host && to_kind == Parsed::Host) return ::MoveFileExW(from, to, flags); | ||
| 833 | if (from_kind == Parsed::Refused || to_kind == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME); | ||
| 834 | // A boot-time move or delete is a host feature a title cannot use on the console's drives. | ||
| 835 | if (!to || (flags & MOVEFILE_DELAY_UNTIL_REBOOT) != 0) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 836 | // GAME: is its own volume: a move between it and a host path is refused the way a cross-volume move is. | ||
| 837 | if (from_kind != to_kind) return fail_bool(ERROR_NOT_SAME_DEVICE); | ||
| 838 | |||
| 839 | const Roots r = roots(); | ||
| 840 | if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); | ||
| 841 | if (src.empty() || dst.empty()) return fail_bool(ERROR_ACCESS_DENIED); | ||
| 842 | const View from_view = view(r, src); | ||
| 843 | if (from_view.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND); | ||
| 844 | const View to_view = view(r, dst); | ||
| 845 | if (to_view.kind == Kind::Directory) return fail_bool(ERROR_ACCESS_DENIED); | ||
| 846 | if (to_view.kind == Kind::File && (flags & MOVEFILE_REPLACE_EXISTING) == 0) return fail_bool(ERROR_ALREADY_EXISTS); | ||
| 847 | if (!is_folder(r, parent_of(dst))) return fail_bool(ERROR_PATH_NOT_FOUND); | ||
| 848 | |||
| 849 | const std::filesystem::path overlay_from = join(r.overlay, src), overlay_to = join(r.overlay, dst); | ||
| 850 | std::error_code ec; | ||
| 851 | std::filesystem::create_directories(overlay_to.parent_path(), ec); | ||
| 852 | if (ec) return fail_bool(static_cast<DWORD>(ec.value())); | ||
| 853 | |||
| 854 | if (from_view.kind == Kind::Directory) { | ||
| 855 | // A package folder is left where it is (as DeleteFile does); an overlay-only folder is renamed in place. | ||
| 856 | if (from_view.in_package) return fail_bool(ERROR_ACCESS_DENIED); | ||
| 857 | if (to_view.kind != Kind::None) return fail_bool(ERROR_ACCESS_DENIED); | ||
| 858 | return ::MoveFileExW(overlay_from.c_str(), overlay_to.c_str(), flags & MOVEFILE_WRITE_THROUGH); | ||
| 859 | } | ||
| 860 | |||
| 861 | const bool write_through = (flags & MOVEFILE_WRITE_THROUGH) != 0; | ||
| 862 | const DWORD keep = flags & (MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH); | ||
| 863 | DWORD error = 0; | ||
| 864 | if (from_view.in_overlay) { | ||
| 865 | if (!::MoveFileExW(overlay_from.c_str(), overlay_to.c_str(), keep | MOVEFILE_COPY_ALLOWED)) return FALSE; | ||
| 866 | } else { | ||
| 867 | // The host never sees a move here, only a copy and a marker, so MOVEFILE_WRITE_THROUGH is honoured by | ||
| 868 | // flushing both before returning, as MoveFileEx does for its own copy and delete. | ||
| 869 | const BOOL fail_if_exists = (flags & MOVEFILE_REPLACE_EXISTING) == 0; | ||
| 870 | if (!::CopyFileW(join(r.game, src).c_str(), overlay_to.c_str(), fail_if_exists)) return FALSE; | ||
| 871 | if (write_through && !flush_file(overlay_to, error)) return fail_bool(error); | ||
| 872 | } | ||
| 873 | if (from_view.in_package) { | ||
| 874 | const std::filesystem::path mark = marker(r.overlay, src); | ||
| 875 | if (!write_marker(mark, error)) return fail_bool(error); | ||
| 876 | if (write_through && !flush_file(mark, error)) return fail_bool(error); | ||
| 877 | } | ||
| 878 | return TRUE; | ||
| 879 | } | ||
| 880 | |||
| 881 | BOOL WINAPI move_file_w(LPCWSTR from, LPCWSTR to) { | ||
| 882 | if (!from || !to) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 883 | return move_file_ex_w(from, to, MOVEFILE_COPY_ALLOWED); // MoveFile moves files across volumes | ||
| 884 | } | ||
| 885 | |||
| 886 | BOOL WINAPI move_file_ex_a(LPCSTR from, LPCSTR to, DWORD flags) { | ||
| 887 | if (!from) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 888 | const std::wstring wide_from = widen_ansi(from), wide_to = to ? widen_ansi(to) : std::wstring(); | ||
| 889 | return move_file_ex_w(wide_from.c_str(), to ? wide_to.c_str() : nullptr, flags); | ||
| 890 | } | ||
| 891 | |||
| 892 | BOOL WINAPI move_file_a(LPCSTR from, LPCSTR to) { | ||
| 893 | if (!from || !to) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 894 | return move_file_ex_a(from, to, MOVEFILE_COPY_ALLOWED); | ||
| 895 | } | ||
| 896 | |||
| 897 | namespace { | ||
| 898 | |||
| 899 | // Where a copy writes: a host path as given; a GAME: path is checked against the title's view (an existing name | ||
| 900 | // counts even when only the package has it) and then means its overlay copy. | ||
| 901 | bool copy_destination(LPCWSTR to, bool fail_if_exists, std::filesystem::path& out, DWORD& error) { | ||
| 902 | std::vector<std::wstring> parts; | ||
| 903 | const Parsed parsed = parse_console(to, parts); | ||
| 904 | if (parsed == Parsed::Host) { | ||
| 905 | out = std::filesystem::path(to); | ||
| 906 | return true; | ||
| 907 | } | ||
| 908 | error = ERROR_INVALID_NAME; | ||
| 909 | if (parsed == Parsed::Refused || parts.empty()) return false; | ||
| 910 | const Roots r = roots(); | ||
| 911 | error = ERROR_PATH_NOT_FOUND; | ||
| 912 | if (r.game.empty() || r.overlay.empty()) return false; | ||
| 913 | const View v = view(r, parts); | ||
| 914 | error = ERROR_ACCESS_DENIED; | ||
| 915 | if (v.kind == Kind::Directory) return false; | ||
| 916 | error = ERROR_FILE_EXISTS; | ||
| 917 | if (v.kind == Kind::File && fail_if_exists) return false; | ||
| 918 | error = ERROR_PATH_NOT_FOUND; | ||
| 919 | if (!is_folder(r, parent_of(parts))) return false; | ||
| 920 | out = join(r.overlay, parts); | ||
| 921 | std::error_code ec; | ||
| 922 | std::filesystem::create_directories(out.parent_path(), ec); | ||
| 923 | if (ec) { | ||
| 924 | error = static_cast<DWORD>(ec.value()); | ||
| 925 | return false; | ||
| 926 | } | ||
| 927 | error = 0; | ||
| 928 | return true; | ||
| 929 | } | ||
| 930 | |||
| 931 | // The source and destination a copy opens, or the error it fails with. | ||
| 932 | bool copy_paths(LPCWSTR from, LPCWSTR to, bool fail_if_exists, std::filesystem::path& source, | ||
| 933 | std::filesystem::path& destination, DWORD& error) { | ||
| 934 | if (!from || !to) { | ||
| 935 | error = ERROR_INVALID_PARAMETER; | ||
| 936 | return false; | ||
| 937 | } | ||
| 938 | const Target t = read_target(from); | ||
| 939 | if (t.error) { | ||
| 940 | error = t.error; | ||
| 941 | return false; | ||
| 942 | } | ||
| 943 | source = t.path; | ||
| 944 | return copy_destination(to, fail_if_exists, destination, error); | ||
| 945 | } | ||
| 946 | |||
| 947 | } // namespace | ||
| 948 | |||
| 949 | BOOL WINAPI copy_file_w(LPCWSTR from, LPCWSTR to, BOOL fail_if_exists) { | ||
| 950 | std::filesystem::path source, destination; | ||
| 951 | DWORD error = 0; | ||
| 952 | if (!copy_paths(from, to, fail_if_exists != FALSE, source, destination, error)) return fail_bool(error); | ||
| 953 | return ::CopyFileW(source.c_str(), destination.c_str(), fail_if_exists); | ||
| 954 | } | ||
| 955 | |||
| 956 | BOOL WINAPI copy_file_a(LPCSTR from, LPCSTR to, BOOL fail_if_exists) { | ||
| 957 | if (!from || !to) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 958 | const std::wstring wide_from = widen_ansi(from), wide_to = widen_ansi(to); | ||
| 959 | return copy_file_w(wide_from.c_str(), wide_to.c_str(), fail_if_exists); | ||
| 960 | } | ||
| 961 | |||
| 962 | BOOL WINAPI copy_file_ex_w(LPCWSTR from, LPCWSTR to, LPPROGRESS_ROUTINE progress, LPVOID data, LPBOOL cancel, | ||
| 963 | DWORD flags) { | ||
| 964 | std::filesystem::path source, destination; | ||
| 965 | DWORD error = 0; | ||
| 966 | if (!copy_paths(from, to, (flags & COPY_FILE_FAIL_IF_EXISTS) != 0, source, destination, error)) | ||
| 967 | return fail_bool(error); | ||
| 968 | return ::CopyFileExW(source.c_str(), destination.c_str(), progress, data, cancel, flags); | ||
| 969 | } | ||
| 970 | |||
| 971 | BOOL WINAPI copy_file_ex_a(LPCSTR from, LPCSTR to, LPPROGRESS_ROUTINE progress, LPVOID data, LPBOOL cancel, | ||
| 972 | DWORD flags) { | ||
| 973 | if (!from || !to) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 974 | const std::wstring wide_from = widen_ansi(from), wide_to = widen_ansi(to); | ||
| 975 | return copy_file_ex_w(wide_from.c_str(), wide_to.c_str(), progress, data, cancel, flags); | ||
| 976 | } | ||
| 977 | |||
| 978 | HRESULT WINAPI copy_file2(PCWSTR from, PCWSTR to, COPYFILE2_EXTENDED_PARAMETERS* parameters) { | ||
| 979 | const bool fail_if_exists = parameters && (parameters->dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0; | ||
| 980 | std::filesystem::path source, destination; | ||
| 981 | DWORD error = 0; | ||
| 982 | if (!copy_paths(from, to, fail_if_exists, source, destination, error)) return HRESULT_FROM_WIN32(error); | ||
| 983 | return ::CopyFile2(source.c_str(), destination.c_str(), parameters); | ||
| 984 | } | ||
| 985 | |||
| 986 | BOOL WINAPI set_file_attributes_w(LPCWSTR name, DWORD attributes) { | ||
| 987 | if (!name) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 988 | std::vector<std::wstring> parts; | ||
| 989 | const Parsed parsed = parse_console(name, parts); | ||
| 990 | if (parsed == Parsed::Host) return ::SetFileAttributesW(name, attributes); | ||
| 991 | if (parsed == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME); | ||
| 992 | |||
| 993 | const Roots r = roots(); | ||
| 994 | if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); | ||
| 995 | if (parts.empty()) return fail_bool(ERROR_ACCESS_DENIED); | ||
| 996 | const View v = view(r, parts); | ||
| 997 | if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND); | ||
| 998 | |||
| 999 | // The attributes belong to the overlay copy: a package file is copied up and a package folder mirrored first, | ||
| 1000 | // so the package keeps its own attributes and reads, which prefer the overlay, see the new ones. | ||
| 1001 | const std::filesystem::path target = join(r.overlay, parts); | ||
| 1002 | if (!v.in_overlay) { | ||
| 1003 | std::error_code ec; | ||
| 1004 | if (v.kind == Kind::Directory) { | ||
| 1005 | std::filesystem::create_directories(target, ec); | ||
| 1006 | if (ec) return fail_bool(static_cast<DWORD>(ec.value())); | ||
| 1007 | } else { | ||
| 1008 | const Target t = write_target(name, true); | ||
| 1009 | if (t.error) return fail_bool(t.error); | ||
| 1010 | } | ||
| 1011 | } | ||
| 1012 | return ::SetFileAttributesW(target.c_str(), attributes); | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | BOOL WINAPI set_file_attributes_a(LPCSTR name, DWORD attributes) { | ||
| 1016 | if (!name) return fail_bool(ERROR_INVALID_PARAMETER); | ||
| 1017 | const std::wstring wide = widen_ansi(name); | ||
| 1018 | return set_file_attributes_w(wide.c_str(), attributes); | ||
| 1019 | } | ||
| 1020 | |||
| 560 | } // namespace rt::fs | 1021 | } // namespace rt::fs |
| 561 | 1022 | ||
| 562 | namespace { | 1023 | namespace { |
| @@ -568,8 +1029,11 @@ using namespace rt::fs; | |||
| 568 | #define FILE_API(dll) \ | 1029 | #define FILE_API(dll) \ |
| 569 | {dll, "CreateFileA", reinterpret_cast<void*>(&create_file_a)}, \ | 1030 | {dll, "CreateFileA", reinterpret_cast<void*>(&create_file_a)}, \ |
| 570 | {dll, "CreateFileW", reinterpret_cast<void*>(&create_file_w)}, \ | 1031 | {dll, "CreateFileW", reinterpret_cast<void*>(&create_file_w)}, \ |
| 1032 | {dll, "CreateFile2", reinterpret_cast<void*>(&create_file2)}, \ | ||
| 571 | {dll, "FindFirstFileA", reinterpret_cast<void*>(&find_first_file_a)}, \ | 1033 | {dll, "FindFirstFileA", reinterpret_cast<void*>(&find_first_file_a)}, \ |
| 572 | {dll, "FindFirstFileW", reinterpret_cast<void*>(&find_first_file_w)}, \ | 1034 | {dll, "FindFirstFileW", reinterpret_cast<void*>(&find_first_file_w)}, \ |
| 1035 | {dll, "FindFirstFileExA", reinterpret_cast<void*>(&find_first_file_ex_a)}, \ | ||
| 1036 | {dll, "FindFirstFileExW", reinterpret_cast<void*>(&find_first_file_ex_w)}, \ | ||
| 573 | {dll, "FindNextFileA", reinterpret_cast<void*>(&find_next_file_a)}, \ | 1037 | {dll, "FindNextFileA", reinterpret_cast<void*>(&find_next_file_a)}, \ |
| 574 | {dll, "FindNextFileW", reinterpret_cast<void*>(&find_next_file_w)}, \ | 1038 | {dll, "FindNextFileW", reinterpret_cast<void*>(&find_next_file_w)}, \ |
| 575 | {dll, "FindClose", reinterpret_cast<void*>(&find_close)}, \ | 1039 | {dll, "FindClose", reinterpret_cast<void*>(&find_close)}, \ |
| @@ -578,7 +1042,24 @@ using namespace rt::fs; | |||
| 578 | {dll, "GetFileAttributesExA", reinterpret_cast<void*>(&get_file_attributes_ex_a)}, \ | 1042 | {dll, "GetFileAttributesExA", reinterpret_cast<void*>(&get_file_attributes_ex_a)}, \ |
| 579 | {dll, "GetFileAttributesExW", reinterpret_cast<void*>(&get_file_attributes_ex_w)}, \ | 1043 | {dll, "GetFileAttributesExW", reinterpret_cast<void*>(&get_file_attributes_ex_w)}, \ |
| 580 | {dll, "DeleteFileA", reinterpret_cast<void*>(&delete_file_a)}, \ | 1044 | {dll, "DeleteFileA", reinterpret_cast<void*>(&delete_file_a)}, \ |
| 581 | {dll, "DeleteFileW", reinterpret_cast<void*>(&delete_file_w)} | 1045 | {dll, "DeleteFileW", reinterpret_cast<void*>(&delete_file_w)}, \ |
| 1046 | {dll, "CreateDirectoryA", reinterpret_cast<void*>(&create_directory_a)}, \ | ||
| 1047 | {dll, "CreateDirectoryW", reinterpret_cast<void*>(&create_directory_w)}, \ | ||
| 1048 | {dll, "CreateDirectoryExA", reinterpret_cast<void*>(&create_directory_ex_a)}, \ | ||
| 1049 | {dll, "CreateDirectoryExW", reinterpret_cast<void*>(&create_directory_ex_w)}, \ | ||
| 1050 | {dll, "RemoveDirectoryA", reinterpret_cast<void*>(&remove_directory_a)}, \ | ||
| 1051 | {dll, "RemoveDirectoryW", reinterpret_cast<void*>(&remove_directory_w)}, \ | ||
| 1052 | {dll, "MoveFileA", reinterpret_cast<void*>(&move_file_a)}, \ | ||
| 1053 | {dll, "MoveFileW", reinterpret_cast<void*>(&move_file_w)}, \ | ||
| 1054 | {dll, "MoveFileExA", reinterpret_cast<void*>(&move_file_ex_a)}, \ | ||
| 1055 | {dll, "MoveFileExW", reinterpret_cast<void*>(&move_file_ex_w)}, \ | ||
| 1056 | {dll, "CopyFileA", reinterpret_cast<void*>(©_file_a)}, \ | ||
| 1057 | {dll, "CopyFileW", reinterpret_cast<void*>(©_file_w)}, \ | ||
| 1058 | {dll, "CopyFileExA", reinterpret_cast<void*>(©_file_ex_a)}, \ | ||
| 1059 | {dll, "CopyFileExW", reinterpret_cast<void*>(©_file_ex_w)}, \ | ||
| 1060 | {dll, "CopyFile2", reinterpret_cast<void*>(©_file2)}, \ | ||
| 1061 | {dll, "SetFileAttributesA", reinterpret_cast<void*>(&set_file_attributes_a)}, \ | ||
| 1062 | {dll, "SetFileAttributesW", reinterpret_cast<void*>(&set_file_attributes_w)} | ||
| 582 | } // namespace | 1063 | } // namespace |
| 583 | 1064 | ||
| 584 | SHIM_TABLE(file_system, | 1065 | SHIM_TABLE(file_system, |