[PATCH] kernelbase: Add support for progress callback in CopyFileEx.
Alistair Leslie-Hughes
leslie_alistair at hotmail.com
Thu Apr 21 04:53:55 CDT 2022
Based on patch by Michael Müller.
Signed-off-by: Alistair Leslie-Hughes <leslie_alistair at hotmail.com>
---
dlls/kernel32/tests/file.c | 7 +----
dlls/kernelbase/file.c | 54 +++++++++++++++++++++++++++++++++++---
2 files changed, 51 insertions(+), 10 deletions(-)
diff --git a/dlls/kernel32/tests/file.c b/dlls/kernel32/tests/file.c
index 1fb9e98cf41..4f47dd8a51f 100644
--- a/dlls/kernel32/tests/file.c
+++ b/dlls/kernel32/tests/file.c
@@ -1176,21 +1176,16 @@ static void test_CopyFileEx(void)
ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %ld\n", GetLastError());
SetLastError(0xdeadbeef);
retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
- todo_wine
ok(!retok, "CopyFileExA unexpectedly succeeded\n");
- todo_wine
ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %ld\n", GetLastError());
ok(GetFileAttributesA(dest) != INVALID_FILE_ATTRIBUTES, "file was deleted\n");
hfile = CreateFileA(dest, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, 0, 0);
- todo_wine
ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %ld\n", GetLastError());
SetLastError(0xdeadbeef);
retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
- todo_wine
ok(!retok, "CopyFileExA unexpectedly succeeded\n");
- todo_wine
ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %ld\n", GetLastError());
todo_wine
ok(GetFileAttributesA(dest) == INVALID_FILE_ATTRIBUTES, "file was not deleted\n");
@@ -1205,7 +1200,7 @@ static void test_CopyFileEx(void)
ret = DeleteFileA(source);
ok(ret, "DeleteFileA failed with error %ld\n", GetLastError());
ret = DeleteFileA(dest);
- ok(!ret, "DeleteFileA unexpectedly succeeded\n");
+ todo_wine ok(!ret, "DeleteFileA unexpectedly succeeded\n");
}
/*
diff --git a/dlls/kernelbase/file.c b/dlls/kernelbase/file.c
index 8ae982294f6..6803c79a4a4 100644
--- a/dlls/kernelbase/file.c
+++ b/dlls/kernelbase/file.c
@@ -490,6 +490,23 @@ BOOL WINAPI DECLSPEC_HOTPATCH AreFileApisANSI(void)
return !oem_file_apis;
}
+static DWORD call_progress_dialog(LPPROGRESS_ROUTINE progress, LARGE_INTEGER size, LARGE_INTEGER transferred,
+ DWORD cbtype, HANDLE h1, HANDLE h2, void *param)
+{
+ DWORD cbret;
+
+ cbret = progress( size, transferred, size, transferred, 1, cbtype, h1, h2, param );
+ if (cbret == PROGRESS_STOP)
+ SetLastError( ERROR_REQUEST_ABORTED );
+ else if (cbret == PROGRESS_CANCEL)
+ {
+ BOOLEAN disp = TRUE;
+ SetFileInformationByHandle( h2, FileDispositionInfo, &disp, sizeof(disp) );
+ SetLastError( ERROR_REQUEST_ABORTED );
+ }
+
+ return cbret;
+}
/***********************************************************************
* CopyFileExW (kernelbase.@)
@@ -499,11 +516,14 @@ BOOL WINAPI CopyFileExW( const WCHAR *source, const WCHAR *dest, LPPROGRESS_ROUT
{
static const int buffer_size = 65536;
HANDLE h1, h2;
- FILE_BASIC_INFORMATION info;
+ FILE_NETWORK_OPEN_INFORMATION info;
+ FILE_BASIC_INFORMATION basic_info;
IO_STATUS_BLOCK io;
DWORD count;
BOOL ret = FALSE;
char *buffer;
+ LARGE_INTEGER transferred;
+ DWORD cbret;
if (!source || !dest)
{
@@ -533,7 +553,7 @@ BOOL WINAPI CopyFileExW( const WCHAR *source, const WCHAR *dest, LPPROGRESS_ROUT
return FALSE;
}
- if (!set_ntstatus( NtQueryInformationFile( h1, &io, &info, sizeof(info), FileBasicInformation )))
+ if (!set_ntstatus( NtQueryInformationFile( h1, &io, &info, sizeof(info), FileNetworkOpenInformation )))
{
WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
HeapFree( GetProcessHeap(), 0, buffer );
@@ -569,6 +589,17 @@ BOOL WINAPI CopyFileExW( const WCHAR *source, const WCHAR *dest, LPPROGRESS_ROUT
return FALSE;
}
+ if (progress)
+ {
+ transferred.QuadPart = 0;
+
+ cbret = call_progress_dialog(progress, info.EndOfFile, transferred, CALLBACK_STREAM_SWITCH, h1, h2, param);
+ if (cbret == PROGRESS_QUIET)
+ progress = NULL;
+ else if (cbret != PROGRESS_CONTINUE)
+ goto done;
+ }
+
while (ReadFile( h1, buffer, buffer_size, &count, NULL ) && count)
{
char *p = buffer;
@@ -578,13 +609,28 @@ BOOL WINAPI CopyFileExW( const WCHAR *source, const WCHAR *dest, LPPROGRESS_ROUT
if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
p += res;
count -= res;
+
+ if (progress)
+ {
+ transferred.QuadPart += res;
+
+ cbret = call_progress_dialog(progress, info.EndOfFile, transferred, CALLBACK_CHUNK_FINISHED, h1, h2, param);
+ if (cbret == PROGRESS_QUIET)
+ progress = NULL;
+ else if (cbret != PROGRESS_CONTINUE)
+ goto done;
+ }
}
}
ret = TRUE;
done:
/* Maintain the timestamp of source file to destination file */
- info.FileAttributes = 0;
- NtSetInformationFile( h2, &io, &info, sizeof(info), FileBasicInformation );
+ basic_info.CreationTime = info.CreationTime;
+ basic_info.LastAccessTime = info.LastAccessTime;
+ basic_info.LastWriteTime = info.LastWriteTime;
+ basic_info.ChangeTime = info.ChangeTime;
+ basic_info.FileAttributes = 0;
+ NtSetInformationFile( h2, &io, &basic_info, sizeof(basic_info), FileBasicInformation );
HeapFree( GetProcessHeap(), 0, buffer );
CloseHandle( h1 );
CloseHandle( h2 );
--
2.35.1
More information about the wine-devel
mailing list