diff options
Diffstat (limited to 'runtime/shims/file_system.cpp')
| -rw-r--r-- | runtime/shims/file_system.cpp | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index 2e7d93718..e63d5cdec 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp | |||
| @@ -975,12 +975,50 @@ BOOL WINAPI copy_file_ex_a(LPCSTR from, LPCSTR to, LPPROGRESS_ROUTINE progress, | |||
| 975 | return copy_file_ex_w(wide_from.c_str(), wide_to.c_str(), progress, data, cancel, flags); | 975 | return copy_file_ex_w(wide_from.c_str(), wide_to.c_str(), progress, data, cancel, flags); |
| 976 | } | 976 | } |
| 977 | 977 | ||
| 978 | namespace { | ||
| 979 | |||
| 980 | // The host's CopyFile2, or null where the host has none. Wine 9's kernel32 and kernelbase don't export it: Wine's | ||
| 981 | // loader binds a static import of it to a stub that aborts the process when called ("unimplemented function | ||
| 982 | // KERNEL32.dll.CopyFile2, aborting"), so a direct ::CopyFile2 call (or MSVC's std::filesystem::copy_file, which uses | ||
| 983 | // 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 | ||
| 984 | // reason. GetProcAddress returns null there instead, and the fallback below runs. | ||
| 985 | using CopyFile2Fn = HRESULT(WINAPI*)(PCWSTR, PCWSTR, COPYFILE2_EXTENDED_PARAMETERS*); | ||
| 986 | CopyFile2Fn host_copy_file2() { | ||
| 987 | static const CopyFile2Fn fn = [] { | ||
| 988 | for (const wchar_t* dll : {L"kernelbase.dll", L"kernel32.dll"}) { | ||
| 989 | if (const HMODULE module = GetModuleHandleW(dll)) | ||
| 990 | if (const FARPROC p = GetProcAddress(module, "CopyFile2")) return reinterpret_cast<CopyFile2Fn>(p); | ||
| 991 | } | ||
| 992 | return CopyFile2Fn{}; | ||
| 993 | }(); | ||
| 994 | return fn; | ||
| 995 | } | ||
| 996 | |||
| 997 | // CopyFile2 through CopyFileExW, for hosts without it. The flags both calls define are passed on; the rest are | ||
| 998 | // CopyFile2-only hints and privilege requests (no offload, compressed traffic, low-free-space mode, ...) that don't | ||
| 999 | // change what a plain file copy produces, and CopyFileExW refuses flags it doesn't know. The cancel flag has the same | ||
| 1000 | // type in both. A COPYFILE2 progress routine has another signature and message set, so it isn't called here. | ||
| 1001 | HRESULT copy_file2_fallback(const wchar_t* source, const wchar_t* destination, | ||
| 1002 | const COPYFILE2_EXTENDED_PARAMETERS* parameters) { | ||
| 1003 | constexpr DWORD kCopyFileExFlags = COPY_FILE_FAIL_IF_EXISTS | COPY_FILE_RESTARTABLE | | ||
| 1004 | COPY_FILE_OPEN_SOURCE_FOR_WRITE | COPY_FILE_ALLOW_DECRYPTED_DESTINATION | | ||
| 1005 | COPY_FILE_COPY_SYMLINK | COPY_FILE_NO_BUFFERING; | ||
| 1006 | const DWORD flags = parameters ? parameters->dwCopyFlags & kCopyFileExFlags : 0; | ||
| 1007 | BOOL* const cancel = parameters ? parameters->pfCancel : nullptr; | ||
| 1008 | if (::CopyFileExW(source, destination, nullptr, nullptr, cancel, flags)) return S_OK; | ||
| 1009 | const DWORD error = GetLastError(); | ||
| 1010 | return HRESULT_FROM_WIN32(error ? error : ERROR_GEN_FAILURE); | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | } // namespace | ||
| 1014 | |||
| 978 | HRESULT WINAPI copy_file2(PCWSTR from, PCWSTR to, COPYFILE2_EXTENDED_PARAMETERS* parameters) { | 1015 | 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; | 1016 | const bool fail_if_exists = parameters && (parameters->dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0; |
| 980 | std::filesystem::path source, destination; | 1017 | std::filesystem::path source, destination; |
| 981 | DWORD error = 0; | 1018 | DWORD error = 0; |
| 982 | if (!copy_paths(from, to, fail_if_exists, source, destination, error)) return HRESULT_FROM_WIN32(error); | 1019 | 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); | 1020 | if (const CopyFile2Fn host = host_copy_file2()) return host(source.c_str(), destination.c_str(), parameters); |
| 1021 | return copy_file2_fallback(source.c_str(), destination.c_str(), parameters); | ||
| 984 | } | 1022 | } |
| 985 | 1023 | ||
| 986 | BOOL WINAPI set_file_attributes_w(LPCWSTR name, DWORD attributes) { | 1024 | BOOL WINAPI set_file_attributes_w(LPCWSTR name, DWORD attributes) { |