upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/file_system.cpp
diff options
context:
space:
mode:
authorRaveN499 <96947941+RaveN499@users.noreply.github.com>2026-11-22 16:45:19 +0300
committerRaveN499 <96947941+RaveN499@users.noreply.github.com>2026-11-22 18:42:08 +0300
commitc9fbc9d07eb81656b3ad20686e8268d890a5cc52 (patch)
tree81970df4a00604d9bb1dbc70346327f6c6dbf270 /runtime/shims/file_system.cpp
parent4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff)
gta6rt: GAME: paths for folders, moves, copies and attributes (RT-97 follow-up)
The calls docs/runtime_imports.md listed as not mapped now follow the RT-97 rules, under kernel32, kernelbase and the api-ms-win-core-file-* sets: - CreateDirectory/CreateDirectoryEx make the folder in the overlay; a name the title can already see, package-only folders included, is ERROR_ALREADY_EXISTS. - RemoveDirectory needs the folder empty in the title's view. An overlay folder is removed; a package folder is hidden by a .gta6rt-dir marker file inside its marker folder and stays on disk (deleted() now also checks that file, on top of the regular-file rule from 3f26a95a). - MoveFile/MoveFileEx write the destination in the overlay; a moved package file is copied there and its old name deleted by a marker. MOVEFILE_WRITE_THROUGH flushes that copy and the marker before returning (review on #1742). Package folders are not moved (ERROR_ACCESS_DENIED), GAME: <-> host is ERROR_NOT_SAME_DEVICE and MOVEFILE_DELAY_UNTIL_REBOOT is ERROR_INVALID_PARAMETER. - CopyFile/CopyFileEx/CopyFile2 read like a read and write a GAME: destination in the overlay; fail-if-exists counts names only the package has. - SetFileAttributes sets the overlay copy (copy-up or folder mirror first). file_system: 123/123 checks. No game files. AI-assisted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/file_system.cpp')
-rw-r--r--runtime/shims/file_system.cpp430
1 files changed, 414 insertions, 16 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp
index f0ae08811..12f0c75ae 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 {
39constexpr wchar_t kGamePrefix[] = L"game:"; 42constexpr wchar_t kGamePrefix[] = L"game:";
40constexpr size_t kGamePrefixLength = 5; 43constexpr size_t kGamePrefixLength = 5;
41constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; 44constexpr 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.
47constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir";
42 48
43std::mutex g_mutex; 49std::mutex g_mutex;
44std::filesystem::path g_game_dir; 50std::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.
142bool deleted(const std::filesystem::path& overlay, const std::vector<std::wstring>& parts) { 150bool 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.
160bool 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.
178bool 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.
192enum class Kind { None, File, Directory };
193struct 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
199View 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
224bool is_folder(const Roots& r, const std::vector<std::wstring>& parts) { return view(r, parts).kind == Kind::Directory; }
225
226std::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.
231bool 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
254BOOL fail_bool(DWORD error) {
255 SetLastError(error);
256 return FALSE;
148} 257}
149 258
150Target read_target(const std::wstring& path) { 259Target read_target(const std::wstring& path) {
@@ -527,17 +636,8 @@ BOOL WINAPI delete_file_w(LPCWSTR name) {
527 return FALSE; 636 return FALSE;
528 } 637 }
529 // The package file stays where it is; the marker is what hides it. 638 // The package file stays where it is; the marker is what hides it.
530 const std::filesystem::path mark = marker(r.overlay, parts); 639 DWORD error = 0;
531 std::filesystem::create_directories(mark.parent_path(), ec); 640 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; 641 removed = true;
542 } 642 }
543 } 643 }
@@ -557,6 +657,287 @@ BOOL WINAPI delete_file_a(LPCSTR name) {
557 return delete_file_w(wide.c_str()); 657 return delete_file_w(wide.c_str());
558} 658}
559 659
660// ---- Folders: created in the overlay, removed by a marker when the package has them
661
662namespace {
663
664BOOL create_directory(LPCWSTR template_name, LPCWSTR name, LPSECURITY_ATTRIBUTES security) {
665 if (!name) return fail_bool(ERROR_INVALID_PARAMETER);
666 std::wstring template_path;
667 if (template_name) {
668 const Target t = read_target(template_name);
669 if (t.error) return fail_bool(t.error);
670 template_path = t.path.wstring();
671 }
672 std::vector<std::wstring> parts;
673 const Parsed parsed = parse_console(name, parts);
674 if (parsed == Parsed::Host)
675 return template_name ? ::CreateDirectoryExW(template_path.c_str(), name, security)
676 : ::CreateDirectoryW(name, security);
677 if (parsed == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME);
678
679 const Roots r = roots();
680 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
681 if (view(r, parts).kind != Kind::None) return fail_bool(ERROR_ALREADY_EXISTS); // "GAME:" included
682 if (!is_folder(r, parent_of(parts))) return fail_bool(ERROR_PATH_NOT_FOUND);
683
684 const std::filesystem::path target = join(r.overlay, parts);
685 std::error_code ec;
686 std::filesystem::create_directories(target.parent_path(), ec);
687 if (ec) return fail_bool(static_cast<DWORD>(ec.value()));
688 return template_name ? ::CreateDirectoryExW(template_path.c_str(), target.c_str(), security)
689 : ::CreateDirectoryW(target.c_str(), security);
690}
691
692} // namespace
693
694BOOL WINAPI create_directory_w(LPCWSTR name, LPSECURITY_ATTRIBUTES security) {
695 return create_directory(nullptr, name, security);
696}
697
698BOOL WINAPI create_directory_a(LPCSTR name, LPSECURITY_ATTRIBUTES security) {
699 if (!name) return fail_bool(ERROR_INVALID_PARAMETER);
700 const std::wstring wide = widen_ansi(name);
701 return create_directory(nullptr, wide.c_str(), security);
702}
703
704BOOL WINAPI create_directory_ex_w(LPCWSTR template_name, LPCWSTR name, LPSECURITY_ATTRIBUTES security) {
705 if (!template_name) return fail_bool(ERROR_INVALID_PARAMETER);
706 return create_directory(template_name, name, security);
707}
708
709BOOL WINAPI create_directory_ex_a(LPCSTR template_name, LPCSTR name, LPSECURITY_ATTRIBUTES security) {
710 if (!template_name || !name) return fail_bool(ERROR_INVALID_PARAMETER);
711 const std::wstring wide_template = widen_ansi(template_name), wide = widen_ansi(name);
712 return create_directory(wide_template.c_str(), wide.c_str(), security);
713}
714
715BOOL WINAPI remove_directory_w(LPCWSTR name) {
716 if (!name) return fail_bool(ERROR_INVALID_PARAMETER);
717 std::vector<std::wstring> parts;
718 const Parsed parsed = parse_console(name, parts);
719 if (parsed == Parsed::Host) return ::RemoveDirectoryW(name);
720 if (parsed == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME);
721
722 const Roots r = roots();
723 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
724 if (parts.empty()) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" itself stays
725 const View v = view(r, parts);
726 if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND);
727 if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY);
728 if (!empty_in_view(r, parts)) return fail_bool(ERROR_DIR_NOT_EMPTY);
729
730 if (v.in_overlay && !::RemoveDirectoryW(join(r.overlay, parts).c_str())) return FALSE; // its error
731 if (v.in_package) {
732 // The package folder stays where it is; the marker hides it, and its files are already hidden one by one.
733 DWORD error = 0;
734 if (!write_marker(marker(r.overlay, parts) / kRemovedDirMarker, error)) return fail_bool(error);
735 }
736 return TRUE;
737}
738
739BOOL WINAPI remove_directory_a(LPCSTR name) {
740 if (!name) return fail_bool(ERROR_INVALID_PARAMETER);
741 const std::wstring wide = widen_ansi(name);
742 return remove_directory_w(wide.c_str());
743}
744
745// ---- Move, copy and attributes: the result lands in the overlay, a moved package name is deleted by a marker
746
747BOOL WINAPI move_file_ex_w(LPCWSTR from, LPCWSTR to, DWORD flags) {
748 if (!from) return fail_bool(ERROR_INVALID_PARAMETER);
749 std::vector<std::wstring> src, dst;
750 const Parsed from_kind = parse_console(from, src);
751 const Parsed to_kind = to ? parse_console(to, dst) : Parsed::Host;
752 if (from_kind == Parsed::Host && to_kind == Parsed::Host) return ::MoveFileExW(from, to, flags);
753 if (from_kind == Parsed::Refused || to_kind == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME);
754 // A boot-time move or delete is a host feature a title cannot use on the console's drives.
755 if (!to || (flags & MOVEFILE_DELAY_UNTIL_REBOOT) != 0) return fail_bool(ERROR_INVALID_PARAMETER);
756 // GAME: is its own volume: a move between it and a host path is refused the way a cross-volume move is.
757 if (from_kind != to_kind) return fail_bool(ERROR_NOT_SAME_DEVICE);
758
759 const Roots r = roots();
760 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
761 if (src.empty() || dst.empty()) return fail_bool(ERROR_ACCESS_DENIED);
762 const View from_view = view(r, src);
763 if (from_view.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND);
764 const View to_view = view(r, dst);
765 if (to_view.kind == Kind::Directory) return fail_bool(ERROR_ACCESS_DENIED);
766 if (to_view.kind == Kind::File && (flags & MOVEFILE_REPLACE_EXISTING) == 0) return fail_bool(ERROR_ALREADY_EXISTS);
767 if (!is_folder(r, parent_of(dst))) return fail_bool(ERROR_PATH_NOT_FOUND);
768
769 const std::filesystem::path overlay_from = join(r.overlay, src), overlay_to = join(r.overlay, dst);
770 std::error_code ec;
771 std::filesystem::create_directories(overlay_to.parent_path(), ec);
772 if (ec) return fail_bool(static_cast<DWORD>(ec.value()));
773
774 if (from_view.kind == Kind::Directory) {
775 // A package folder is left where it is (as DeleteFile does); an overlay-only folder is renamed in place.
776 if (from_view.in_package) return fail_bool(ERROR_ACCESS_DENIED);
777 if (to_view.kind != Kind::None) return fail_bool(ERROR_ACCESS_DENIED);
778 return ::MoveFileExW(overlay_from.c_str(), overlay_to.c_str(), flags & MOVEFILE_WRITE_THROUGH);
779 }
780
781 const bool write_through = (flags & MOVEFILE_WRITE_THROUGH) != 0;
782 const DWORD keep = flags & (MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
783 DWORD error = 0;
784 if (from_view.in_overlay) {
785 if (!::MoveFileExW(overlay_from.c_str(), overlay_to.c_str(), keep | MOVEFILE_COPY_ALLOWED)) return FALSE;
786 } else {
787 // The host never sees a move here, only a copy and a marker, so MOVEFILE_WRITE_THROUGH is honoured by
788 // flushing both before returning, as MoveFileEx does for its own copy and delete.
789 const BOOL fail_if_exists = (flags & MOVEFILE_REPLACE_EXISTING) == 0;
790 if (!::CopyFileW(join(r.game, src).c_str(), overlay_to.c_str(), fail_if_exists)) return FALSE;
791 if (write_through && !flush_file(overlay_to, error)) return fail_bool(error);
792 }
793 if (from_view.in_package) {
794 const std::filesystem::path mark = marker(r.overlay, src);
795 if (!write_marker(mark, error)) return fail_bool(error);
796 if (write_through && !flush_file(mark, error)) return fail_bool(error);
797 }
798 return TRUE;
799}
800
801BOOL WINAPI move_file_w(LPCWSTR from, LPCWSTR to) {
802 if (!from || !to) return fail_bool(ERROR_INVALID_PARAMETER);
803 return move_file_ex_w(from, to, MOVEFILE_COPY_ALLOWED); // MoveFile moves files across volumes
804}
805
806BOOL WINAPI move_file_ex_a(LPCSTR from, LPCSTR to, DWORD flags) {
807 if (!from) return fail_bool(ERROR_INVALID_PARAMETER);
808 const std::wstring wide_from = widen_ansi(from), wide_to = to ? widen_ansi(to) : std::wstring();
809 return move_file_ex_w(wide_from.c_str(), to ? wide_to.c_str() : nullptr, flags);
810}
811
812BOOL WINAPI move_file_a(LPCSTR from, LPCSTR to) {
813 if (!from || !to) return fail_bool(ERROR_INVALID_PARAMETER);
814 return move_file_ex_a(from, to, MOVEFILE_COPY_ALLOWED);
815}
816
817namespace {
818
819// Where a copy writes: a host path as given; a GAME: path is checked against the title's view (an existing name
820// counts even when only the package has it) and then means its overlay copy.
821bool copy_destination(LPCWSTR to, bool fail_if_exists, std::filesystem::path& out, DWORD& error) {
822 std::vector<std::wstring> parts;
823 const Parsed parsed = parse_console(to, parts);
824 if (parsed == Parsed::Host) {
825 out = std::filesystem::path(to);
826 return true;
827 }
828 error = ERROR_INVALID_NAME;
829 if (parsed == Parsed::Refused || parts.empty()) return false;
830 const Roots r = roots();
831 error = ERROR_PATH_NOT_FOUND;
832 if (r.game.empty() || r.overlay.empty()) return false;
833 const View v = view(r, parts);
834 error = ERROR_ACCESS_DENIED;
835 if (v.kind == Kind::Directory) return false;
836 error = ERROR_FILE_EXISTS;
837 if (v.kind == Kind::File && fail_if_exists) return false;
838 error = ERROR_PATH_NOT_FOUND;
839 if (!is_folder(r, parent_of(parts))) return false;
840 out = join(r.overlay, parts);
841 std::error_code ec;
842 std::filesystem::create_directories(out.parent_path(), ec);
843 if (ec) {
844 error = static_cast<DWORD>(ec.value());
845 return false;
846 }
847 error = 0;
848 return true;
849}
850
851// The source and destination a copy opens, or the error it fails with.
852bool copy_paths(LPCWSTR from, LPCWSTR to, bool fail_if_exists, std::filesystem::path& source,
853 std::filesystem::path& destination, DWORD& error) {
854 if (!from || !to) {
855 error = ERROR_INVALID_PARAMETER;
856 return false;
857 }
858 const Target t = read_target(from);
859 if (t.error) {
860 error = t.error;
861 return false;
862 }
863 source = t.path;
864 return copy_destination(to, fail_if_exists, destination, error);
865}
866
867} // namespace
868
869BOOL WINAPI copy_file_w(LPCWSTR from, LPCWSTR to, BOOL fail_if_exists) {
870 std::filesystem::path source, destination;
871 DWORD error = 0;
872 if (!copy_paths(from, to, fail_if_exists != FALSE, source, destination, error)) return fail_bool(error);
873 return ::CopyFileW(source.c_str(), destination.c_str(), fail_if_exists);
874}
875
876BOOL WINAPI copy_file_a(LPCSTR from, LPCSTR to, BOOL fail_if_exists) {
877 if (!from || !to) return fail_bool(ERROR_INVALID_PARAMETER);
878 const std::wstring wide_from = widen_ansi(from), wide_to = widen_ansi(to);
879 return copy_file_w(wide_from.c_str(), wide_to.c_str(), fail_if_exists);
880}
881
882BOOL WINAPI copy_file_ex_w(LPCWSTR from, LPCWSTR to, LPPROGRESS_ROUTINE progress, LPVOID data, LPBOOL cancel,
883 DWORD flags) {
884 std::filesystem::path source, destination;
885 DWORD error = 0;
886 if (!copy_paths(from, to, (flags & COPY_FILE_FAIL_IF_EXISTS) != 0, source, destination, error))
887 return fail_bool(error);
888 return ::CopyFileExW(source.c_str(), destination.c_str(), progress, data, cancel, flags);
889}
890
891BOOL WINAPI copy_file_ex_a(LPCSTR from, LPCSTR to, LPPROGRESS_ROUTINE progress, LPVOID data, LPBOOL cancel,
892 DWORD flags) {
893 if (!from || !to) return fail_bool(ERROR_INVALID_PARAMETER);
894 const std::wstring wide_from = widen_ansi(from), wide_to = widen_ansi(to);
895 return copy_file_ex_w(wide_from.c_str(), wide_to.c_str(), progress, data, cancel, flags);
896}
897
898HRESULT WINAPI copy_file2(PCWSTR from, PCWSTR to, COPYFILE2_EXTENDED_PARAMETERS* parameters) {
899 const bool fail_if_exists = parameters && (parameters->dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0;
900 std::filesystem::path source, destination;
901 DWORD error = 0;
902 if (!copy_paths(from, to, fail_if_exists, source, destination, error)) return HRESULT_FROM_WIN32(error);
903 return ::CopyFile2(source.c_str(), destination.c_str(), parameters);
904}
905
906BOOL WINAPI set_file_attributes_w(LPCWSTR name, DWORD attributes) {
907 if (!name) return fail_bool(ERROR_INVALID_PARAMETER);
908 std::vector<std::wstring> parts;
909 const Parsed parsed = parse_console(name, parts);
910 if (parsed == Parsed::Host) return ::SetFileAttributesW(name, attributes);
911 if (parsed == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME);
912
913 const Roots r = roots();
914 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
915 if (parts.empty()) return fail_bool(ERROR_ACCESS_DENIED);
916 const View v = view(r, parts);
917 if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND);
918
919 // The attributes belong to the overlay copy: a package file is copied up and a package folder mirrored first,
920 // so the package keeps its own attributes and reads, which prefer the overlay, see the new ones.
921 const std::filesystem::path target = join(r.overlay, parts);
922 if (!v.in_overlay) {
923 std::error_code ec;
924 if (v.kind == Kind::Directory) {
925 std::filesystem::create_directories(target, ec);
926 if (ec) return fail_bool(static_cast<DWORD>(ec.value()));
927 } else {
928 const Target t = write_target(name, true);
929 if (t.error) return fail_bool(t.error);
930 }
931 }
932 return ::SetFileAttributesW(target.c_str(), attributes);
933}
934
935BOOL WINAPI set_file_attributes_a(LPCSTR name, DWORD attributes) {
936 if (!name) return fail_bool(ERROR_INVALID_PARAMETER);
937 const std::wstring wide = widen_ansi(name);
938 return set_file_attributes_w(wide.c_str(), attributes);
939}
940
560} // namespace rt::fs 941} // namespace rt::fs
561 942
562namespace { 943namespace {
@@ -578,7 +959,24 @@ using namespace rt::fs;
578 {dll, "GetFileAttributesExA", reinterpret_cast<void*>(&get_file_attributes_ex_a)}, \ 959 {dll, "GetFileAttributesExA", reinterpret_cast<void*>(&get_file_attributes_ex_a)}, \
579 {dll, "GetFileAttributesExW", reinterpret_cast<void*>(&get_file_attributes_ex_w)}, \ 960 {dll, "GetFileAttributesExW", reinterpret_cast<void*>(&get_file_attributes_ex_w)}, \
580 {dll, "DeleteFileA", reinterpret_cast<void*>(&delete_file_a)}, \ 961 {dll, "DeleteFileA", reinterpret_cast<void*>(&delete_file_a)}, \
581 {dll, "DeleteFileW", reinterpret_cast<void*>(&delete_file_w)} 962 {dll, "DeleteFileW", reinterpret_cast<void*>(&delete_file_w)}, \
963 {dll, "CreateDirectoryA", reinterpret_cast<void*>(&create_directory_a)}, \
964 {dll, "CreateDirectoryW", reinterpret_cast<void*>(&create_directory_w)}, \
965 {dll, "CreateDirectoryExA", reinterpret_cast<void*>(&create_directory_ex_a)}, \
966 {dll, "CreateDirectoryExW", reinterpret_cast<void*>(&create_directory_ex_w)}, \
967 {dll, "RemoveDirectoryA", reinterpret_cast<void*>(&remove_directory_a)}, \
968 {dll, "RemoveDirectoryW", reinterpret_cast<void*>(&remove_directory_w)}, \
969 {dll, "MoveFileA", reinterpret_cast<void*>(&move_file_a)}, \
970 {dll, "MoveFileW", reinterpret_cast<void*>(&move_file_w)}, \
971 {dll, "MoveFileExA", reinterpret_cast<void*>(&move_file_ex_a)}, \
972 {dll, "MoveFileExW", reinterpret_cast<void*>(&move_file_ex_w)}, \
973 {dll, "CopyFileA", reinterpret_cast<void*>(&copy_file_a)}, \
974 {dll, "CopyFileW", reinterpret_cast<void*>(&copy_file_w)}, \
975 {dll, "CopyFileExA", reinterpret_cast<void*>(&copy_file_ex_a)}, \
976 {dll, "CopyFileExW", reinterpret_cast<void*>(&copy_file_ex_w)}, \
977 {dll, "CopyFile2", reinterpret_cast<void*>(&copy_file2)}, \
978 {dll, "SetFileAttributesA", reinterpret_cast<void*>(&set_file_attributes_a)}, \
979 {dll, "SetFileAttributesW", reinterpret_cast<void*>(&set_file_attributes_w)}
582} // namespace 980} // namespace
583 981
584SHIM_TABLE(file_system, 982SHIM_TABLE(file_system,