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:
authorNolan Cotter <186326284+NolanCotter@users.noreply.github.com>2026-11-22 20:39:59 -0700
committerNolan Cotter <186326284+NolanCotter@users.noreply.github.com>2026-11-22 20:39:59 -0700
commitfa5f26db8eafd2cc27f627b68c0a5a5949bcf2ff (patch)
treefc57e238be829295fc216dff89a6d93e64c16b50 /runtime/shims/file_system.cpp
parent9b693ecd8a2f90799f6c47f8f06cf062feb9820c (diff)
parent7b7d73f890f9fbbf5967bbb694390bbc99e33ba6 (diff)
Merge origin/main: DXGI device fields coexist with the console-ABI device state
Diffstat (limited to 'runtime/shims/file_system.cpp')
-rw-r--r--runtime/shims/file_system.cpp46
1 files changed, 43 insertions, 3 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp
index 5b9f92199..014514597 100644
--- a/runtime/shims/file_system.cpp
+++ b/runtime/shims/file_system.cpp
@@ -393,9 +393,11 @@ Target write_target(const std::wstring& path, bool copy_up) {
393 return t; 393 return t;
394} 394}
395 395
396// MAXIMUM_ALLOWED names no right and grants every one the file allows, write included, so it is a write-open: opened
397// as a read it handed out a handle to the package file itself, and a WriteFile through it changed the package.
396bool writes(DWORD access, DWORD disposition) { 398bool writes(DWORD access, DWORD disposition) {
397 constexpr DWORD kWriteAccess = GENERIC_WRITE | GENERIC_ALL | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA | 399 constexpr DWORD kWriteAccess = GENERIC_WRITE | GENERIC_ALL | MAXIMUM_ALLOWED | FILE_WRITE_DATA | FILE_APPEND_DATA |
398 FILE_WRITE_ATTRIBUTES | DELETE | WRITE_DAC | WRITE_OWNER; 400 FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | DELETE | WRITE_DAC | WRITE_OWNER;
399 if ((access & kWriteAccess) != 0) return true; 401 if ((access & kWriteAccess) != 0) return true;
400 return disposition == CREATE_NEW || disposition == CREATE_ALWAYS || disposition == OPEN_ALWAYS || 402 return disposition == CREATE_NEW || disposition == CREATE_ALWAYS || disposition == OPEN_ALWAYS ||
401 disposition == TRUNCATE_EXISTING; 403 disposition == TRUNCATE_EXISTING;
@@ -1043,12 +1045,50 @@ BOOL WINAPI copy_file_ex_a(LPCSTR from, LPCSTR to, LPPROGRESS_ROUTINE progress,
1043 return copy_file_ex_w(wide_from.c_str(), wide_to.c_str(), progress, data, cancel, flags); 1045 return copy_file_ex_w(wide_from.c_str(), wide_to.c_str(), progress, data, cancel, flags);
1044} 1046}
1045 1047
1048namespace {
1049
1050// The host's CopyFile2, or null where the host has none. Wine 9's kernel32 and kernelbase don't export it: Wine's
1051// loader binds a static import of it to a stub that aborts the process when called ("unimplemented function
1052// KERNEL32.dll.CopyFile2, aborting"), so a direct ::CopyFile2 call (or MSVC's std::filesystem::copy_file, which uses
1053// it) kills a title under Wine 9 or a Proton built on it; pe_loader.cpp and loader_tls_test.cpp avoid it for the same
1054// reason. GetProcAddress returns null there instead, and the fallback below runs.
1055using CopyFile2Fn = HRESULT(WINAPI*)(PCWSTR, PCWSTR, COPYFILE2_EXTENDED_PARAMETERS*);
1056CopyFile2Fn host_copy_file2() {
1057 static const CopyFile2Fn fn = [] {
1058 for (const wchar_t* dll : {L"kernelbase.dll", L"kernel32.dll"}) {
1059 if (const HMODULE module = GetModuleHandleW(dll))
1060 if (const FARPROC p = GetProcAddress(module, "CopyFile2")) return reinterpret_cast<CopyFile2Fn>(p);
1061 }
1062 return CopyFile2Fn{};
1063 }();
1064 return fn;
1065}
1066
1067// CopyFile2 through CopyFileExW, for hosts without it. The flags both calls define are passed on; the rest are
1068// CopyFile2-only hints and privilege requests (no offload, compressed traffic, low-free-space mode, ...) that don't
1069// change what a plain file copy produces, and CopyFileExW refuses flags it doesn't know. The cancel flag has the same
1070// type in both. A COPYFILE2 progress routine has another signature and message set, so it isn't called here.
1071HRESULT copy_file2_fallback(const wchar_t* source, const wchar_t* destination,
1072 const COPYFILE2_EXTENDED_PARAMETERS* parameters) {
1073 constexpr DWORD kCopyFileExFlags = COPY_FILE_FAIL_IF_EXISTS | COPY_FILE_RESTARTABLE |
1074 COPY_FILE_OPEN_SOURCE_FOR_WRITE | COPY_FILE_ALLOW_DECRYPTED_DESTINATION |
1075 COPY_FILE_COPY_SYMLINK | COPY_FILE_NO_BUFFERING;
1076 const DWORD flags = parameters ? parameters->dwCopyFlags & kCopyFileExFlags : 0;
1077 BOOL* const cancel = parameters ? parameters->pfCancel : nullptr;
1078 if (::CopyFileExW(source, destination, nullptr, nullptr, cancel, flags)) return S_OK;
1079 const DWORD error = GetLastError();
1080 return HRESULT_FROM_WIN32(error ? error : ERROR_GEN_FAILURE);
1081}
1082
1083} // namespace
1084
1046HRESULT WINAPI copy_file2(PCWSTR from, PCWSTR to, COPYFILE2_EXTENDED_PARAMETERS* parameters) { 1085HRESULT WINAPI copy_file2(PCWSTR from, PCWSTR to, COPYFILE2_EXTENDED_PARAMETERS* parameters) {
1047 const bool fail_if_exists = parameters && (parameters->dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0; 1086 const bool fail_if_exists = parameters && (parameters->dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0;
1048 std::filesystem::path source, destination; 1087 std::filesystem::path source, destination;
1049 DWORD error = 0; 1088 DWORD error = 0;
1050 if (!copy_paths(from, to, fail_if_exists, source, destination, error)) return HRESULT_FROM_WIN32(error); 1089 if (!copy_paths(from, to, fail_if_exists, source, destination, error)) return HRESULT_FROM_WIN32(error);
1051 return ::CopyFile2(source.c_str(), destination.c_str(), parameters); 1090 if (const CopyFile2Fn host = host_copy_file2()) return host(source.c_str(), destination.c_str(), parameters);
1091 return copy_file2_fallback(source.c_str(), destination.c_str(), parameters);
1052} 1092}
1053 1093
1054BOOL WINAPI set_file_attributes_w(LPCWSTR name, DWORD attributes) { 1094BOOL WINAPI set_file_attributes_w(LPCWSTR name, DWORD attributes) {