diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:08:08 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:08:08 -0500 |
| commit | 9180103a7daa120f34fc5064409a58be594cfcbf (patch) | |
| tree | 7f639ab1fd900a64d730634dfa8a280473febab4 /runtime/shims/file_system.cpp | |
| parent | 0c1437dfd988e5d7d57d5ffdf144b8ea7bd08391 (diff) | |
| parent | c0b821cda743abee02c6d504056ea68c3a6751b2 (diff) | |
Merge pull request #3655 from Claude
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.cpp | 62 |
1 files changed, 50 insertions, 12 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index 7b932be3c..3c5417101 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp | |||
| @@ -24,6 +24,7 @@ | |||
| 24 | // host directly. | 24 | // host directly. |
| 25 | #include <windows.h> | 25 | #include <windows.h> |
| 26 | 26 | ||
| 27 | #include <atomic> | ||
| 27 | #include <cstring> | 28 | #include <cstring> |
| 28 | #include <cwchar> | 29 | #include <cwchar> |
| 29 | #include <cwctype> | 30 | #include <cwctype> |
| @@ -1096,6 +1097,8 @@ namespace { | |||
| 1096 | // 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 | 1097 | // 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 |
| 1097 | // reason. GetProcAddress returns null there instead, and the fallback below runs. | 1098 | // reason. GetProcAddress returns null there instead, and the fallback below runs. |
| 1098 | using CopyFile2Fn = HRESULT(WINAPI*)(PCWSTR, PCWSTR, COPYFILE2_EXTENDED_PARAMETERS*); | 1099 | using CopyFile2Fn = HRESULT(WINAPI*)(PCWSTR, PCWSTR, COPYFILE2_EXTENDED_PARAMETERS*); |
| 1100 | std::atomic<bool> g_force_copy_file2_fallback{false}; | ||
| 1101 | |||
| 1099 | CopyFile2Fn host_copy_file2() { | 1102 | CopyFile2Fn host_copy_file2() { |
| 1100 | static const CopyFile2Fn fn = [] { | 1103 | static const CopyFile2Fn fn = [] { |
| 1101 | for (const wchar_t* dll : {L"kernelbase.dll", L"kernel32.dll"}) { | 1104 | for (const wchar_t* dll : {L"kernelbase.dll", L"kernel32.dll"}) { |
| @@ -1104,20 +1107,46 @@ CopyFile2Fn host_copy_file2() { | |||
| 1104 | } | 1107 | } |
| 1105 | return CopyFile2Fn{}; | 1108 | return CopyFile2Fn{}; |
| 1106 | }(); | 1109 | }(); |
| 1107 | return fn; | 1110 | return g_force_copy_file2_fallback.load(std::memory_order_relaxed) ? CopyFile2Fn{} : fn; |
| 1108 | } | 1111 | } |
| 1109 | 1112 | ||
| 1110 | // CopyFile2 through CopyFileExW, for hosts without it. The flags both calls define are passed on; the rest are | 1113 | // The fallback's flags, by what CopyFileExW can do with them (values from the SDK's WinBase.h, spelled out because |
| 1111 | // CopyFile2-only hints and privilege requests (no offload, compressed traffic, low-free-space mode, ...) that don't | 1114 | // some of the names are only declared for newer NTDDI_VERSION targets): |
| 1112 | // change what a plain file copy produces, and CopyFileExW refuses flags it doesn't know. The cancel flag has the same | 1115 | // - passed on: the flags both calls define, with the same meaning; |
| 1113 | // type in both. A COPYFILE2 progress routine has another signature and message set, so it isn't called here. | 1116 | // - dropped: CopyFile2-only requests about how the copy runs (privileges, resuming a paused copy, no offload, EDP, |
| 1114 | HRESULT copy_file2_fallback(const wchar_t* source, const wchar_t* destination, | 1117 | // destination DACL, compressed SMB traffic, pre-allocation, low-free-space mode, sparse copy) that leave the |
| 1115 | const COPYFILE2_EXTENDED_PARAMETERS* parameters) { | 1118 | // destination's contents as CopyFileExW writes them; |
| 1116 | constexpr DWORD kCopyFileExFlags = COPY_FILE_FAIL_IF_EXISTS | COPY_FILE_RESTARTABLE | | 1119 | // - everything else changes what the copy produces (0x80 DIRECTORY, 0x8000 SKIP_ALTERNATE_STREAMS, 0x200000 |
| 1120 | // OPEN_AND_COPY_REPARSE_POINT, 0x800000 IGNORE_SOURCE_ENCRYPTION) or isn't defined, so the fallback refuses the | ||
| 1121 | // call with ERROR_NOT_SUPPORTED instead of copying something else. | ||
| 1122 | constexpr DWORD kFallbackPassedFlags = COPY_FILE_FAIL_IF_EXISTS | COPY_FILE_RESTARTABLE | | ||
| 1117 | COPY_FILE_OPEN_SOURCE_FOR_WRITE | COPY_FILE_ALLOW_DECRYPTED_DESTINATION | | 1123 | COPY_FILE_OPEN_SOURCE_FOR_WRITE | COPY_FILE_ALLOW_DECRYPTED_DESTINATION | |
| 1118 | COPY_FILE_COPY_SYMLINK | COPY_FILE_NO_BUFFERING; | 1124 | COPY_FILE_COPY_SYMLINK | COPY_FILE_NO_BUFFERING; |
| 1119 | const DWORD flags = parameters ? parameters->dwCopyFlags & kCopyFileExFlags : 0; | 1125 | constexpr DWORD kFallbackDroppedFlags = 0x00002000 /* REQUEST_SECURITY_PRIVILEGES */ | |
| 1126 | 0x00004000 /* RESUME_FROM_PAUSE */ | 0x00040000 /* NO_OFFLOAD */ | | ||
| 1127 | 0x00400000 /* IGNORE_EDP_BLOCK */ | | ||
| 1128 | 0x02000000 /* DONT_REQUEST_DEST_WRITE_DAC */ | | ||
| 1129 | 0x04000000 /* DISABLE_PRE_ALLOCATION */ | | ||
| 1130 | 0x08000000 /* ENABLE_LOW_FREE_SPACE_MODE */ | | ||
| 1131 | 0x10000000 /* REQUEST_COMPRESSED_TRAFFIC */ | | ||
| 1132 | 0x20000000 /* ENABLE_SPARSE_COPY */ | 0x80000000 /* DISABLE_SPARSE_COPY */; | ||
| 1133 | |||
| 1134 | // Whether the fallback can run this call as CopyFile2 would: a COPYFILE2_EXTENDED_PARAMETERS block (the V2 block's | ||
| 1135 | // extra fields have no CopyFileExW equivalent) and no flag outside the two sets above. | ||
| 1136 | bool fallback_supports(const COPYFILE2_EXTENDED_PARAMETERS* parameters) { | ||
| 1137 | if (!parameters) return true; | ||
| 1138 | if (parameters->dwSize != sizeof(COPYFILE2_EXTENDED_PARAMETERS)) return false; | ||
| 1139 | return (parameters->dwCopyFlags & ~(kFallbackPassedFlags | kFallbackDroppedFlags)) == 0; | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | // CopyFile2 through CopyFileExW, for hosts without it (only for calls fallback_supports). The cancel flag has the | ||
| 1143 | // same type in both; one already set is answered here, since Wine 9's CopyFileExW doesn't read it. A COPYFILE2 | ||
| 1144 | // progress routine has another signature and message set, so it isn't called here. | ||
| 1145 | HRESULT copy_file2_fallback(const wchar_t* source, const wchar_t* destination, | ||
| 1146 | const COPYFILE2_EXTENDED_PARAMETERS* parameters) { | ||
| 1147 | const DWORD flags = parameters ? parameters->dwCopyFlags & kFallbackPassedFlags : 0; | ||
| 1120 | BOOL* const cancel = parameters ? parameters->pfCancel : nullptr; | 1148 | BOOL* const cancel = parameters ? parameters->pfCancel : nullptr; |
| 1149 | if (cancel && *cancel) return HRESULT_FROM_WIN32(ERROR_REQUEST_ABORTED); | ||
| 1121 | if (::CopyFileExW(source, destination, nullptr, nullptr, cancel, flags)) return S_OK; | 1150 | if (::CopyFileExW(source, destination, nullptr, nullptr, cancel, flags)) return S_OK; |
| 1122 | const DWORD error = GetLastError(); | 1151 | const DWORD error = GetLastError(); |
| 1123 | return HRESULT_FROM_WIN32(error ? error : ERROR_GEN_FAILURE); | 1152 | return HRESULT_FROM_WIN32(error ? error : ERROR_GEN_FAILURE); |
| @@ -1125,12 +1154,21 @@ HRESULT copy_file2_fallback(const wchar_t* source, const wchar_t* destination, | |||
| 1125 | 1154 | ||
| 1126 | } // namespace | 1155 | } // namespace |
| 1127 | 1156 | ||
| 1157 | void force_copy_file2_fallback(bool force) { g_force_copy_file2_fallback.store(force, std::memory_order_relaxed); } | ||
| 1158 | |||
| 1159 | // The parameter block is checked before the destination is prepared, so a refused call leaves nothing behind: a | ||
| 1160 | // block shorter than COPYFILE2_EXTENDED_PARAMETERS is E_INVALIDARG, as the host's CopyFile2 answers (measured on | ||
| 1161 | // Windows 11 for dwSize 0, 4 and 31, which also left no destination), and a call the fallback can't run as asked is | ||
| 1162 | // ERROR_NOT_SUPPORTED. A longer block goes to the host's CopyFile2, which checks it itself. | ||
| 1128 | HRESULT WINAPI copy_file2(PCWSTR from, PCWSTR to, COPYFILE2_EXTENDED_PARAMETERS* parameters) { | 1163 | HRESULT WINAPI copy_file2(PCWSTR from, PCWSTR to, COPYFILE2_EXTENDED_PARAMETERS* parameters) { |
| 1164 | if (parameters && parameters->dwSize < sizeof(COPYFILE2_EXTENDED_PARAMETERS)) return E_INVALIDARG; | ||
| 1165 | const CopyFile2Fn host = host_copy_file2(); | ||
| 1166 | if (!host && !fallback_supports(parameters)) return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); | ||
| 1129 | const bool fail_if_exists = parameters && (parameters->dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0; | 1167 | const bool fail_if_exists = parameters && (parameters->dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0; |
| 1130 | std::filesystem::path source, destination; | 1168 | std::filesystem::path source, destination; |
| 1131 | DWORD error = 0; | 1169 | DWORD error = 0; |
| 1132 | if (!copy_paths(from, to, fail_if_exists, source, destination, error)) return HRESULT_FROM_WIN32(error); | 1170 | if (!copy_paths(from, to, fail_if_exists, source, destination, error)) return HRESULT_FROM_WIN32(error); |
| 1133 | if (const CopyFile2Fn host = host_copy_file2()) return host(source.c_str(), destination.c_str(), parameters); | 1171 | if (host) return host(source.c_str(), destination.c_str(), parameters); |
| 1134 | return copy_file2_fallback(source.c_str(), destination.c_str(), parameters); | 1172 | return copy_file2_fallback(source.c_str(), destination.c_str(), parameters); |
| 1135 | } | 1173 | } |
| 1136 | 1174 | ||