[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
This commit is contained in:
Mohammad Amin Mollazadeh
2026-06-16 16:40:23 +03:30
committed by GitHub
parent 9127a5319b
commit 91146de1cd

View File

@@ -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,