From 91146de1cd0d51116ef73d5f8117cc5f51d2bc47 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Mollazadeh Date: Tue, 16 Jun 2026 16:40:23 +0330 Subject: [PATCH] [KERNEL32] BasepCopyFileExW: Check dwCopyFlags properly (#9133) The function didn't check if `COPY_FILE_FAIL_IF_EXISTS` bit flag is set or not, always failing if _any_ flags were set and the destination file already existed. This behavior was probably implemented under assumption that `COPY_FILE_FAIL_IF_EXISTS` is the only valid flag, but it's not the case. There are other flags, so this was breaking applications when they intend to overwrite a file with e.g. `COPY_FILE_ALLOW_DECRYPTED_DESTINATION`. These functions were affected by the problem: - CopyFileEx - exposed by Total Commander not being able to overwrite file - PrivCopyFileEx - MoveFileWithProgress Also cover `ERROR_ALREADY_EXISTS` returned by CreateFileW to match Windows behavior, making the API tests pass. CORE-10271 --- dll/win32/kernel32/client/file/copy.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dll/win32/kernel32/client/file/copy.c b/dll/win32/kernel32/client/file/copy.c index 9df97ba1596..6e3f1ae8a8c 100644 --- a/dll/win32/kernel32/client/file/copy.c +++ b/dll/win32/kernel32/client/file/copy.c @@ -254,11 +254,13 @@ BasepCopyFileExW(IN LPCWSTR lpExistingFileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, - dwCopyFlags ? CREATE_NEW : CREATE_ALWAYS, + (dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) ? CREATE_NEW : CREATE_ALWAYS, FileBasic.FileAttributes, NULL); if (INVALID_HANDLE_VALUE != FileHandleDest) { + if (!(dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) && GetLastError() == ERROR_ALREADY_EXISTS) + SetLastError(ERROR_SUCCESS); errCode = CopyLoop(FileHandleSource, FileHandleDest, FileStandard.EndOfFile,