1*fc0bc008SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*fc0bc008SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*fc0bc008SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*fc0bc008SAndrew Rist  * distributed with this work for additional information
6*fc0bc008SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*fc0bc008SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*fc0bc008SAndrew Rist  * "License"); you may not use this file except in compliance
9*fc0bc008SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*fc0bc008SAndrew Rist  *
11*fc0bc008SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*fc0bc008SAndrew Rist  *
13*fc0bc008SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*fc0bc008SAndrew Rist  * software distributed under the License is distributed on an
15*fc0bc008SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*fc0bc008SAndrew Rist  * KIND, either express or implied.  See the License for the
17*fc0bc008SAndrew Rist  * specific language governing permissions and limitations
18*fc0bc008SAndrew Rist  * under the License.
19*fc0bc008SAndrew Rist  *
20*fc0bc008SAndrew Rist  *************************************************************/
21*fc0bc008SAndrew Rist 
22*fc0bc008SAndrew Rist 
23cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER >= 1400)
24cdf0e10cSrcweir #pragma warning(disable:4740)
25cdf0e10cSrcweir #endif
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #define _WIN32_WINNT 0x0400
28cdf0e10cSrcweir #include "macros.h"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #define	BUFSIZE	16384
31cdf0e10cSrcweir 
DefCopyProgressRoutine(LARGE_INTEGER TotalFileSize,LARGE_INTEGER TotalBytesTransferred,LARGE_INTEGER StreamSize,LARGE_INTEGER StreamBytesTransferred,DWORD dwStreamNumber,DWORD dwCallbackReason,HANDLE hSourceFile,HANDLE hDestinationFile,LPVOID lpData)32cdf0e10cSrcweir static DWORD CALLBACK DefCopyProgressRoutine(
33cdf0e10cSrcweir 	LARGE_INTEGER	TotalFileSize,	// total file size, in bytes
34cdf0e10cSrcweir 	LARGE_INTEGER	TotalBytesTransferred,
35cdf0e10cSrcweir 									// total number of bytes transferred
36cdf0e10cSrcweir 	LARGE_INTEGER	StreamSize,		// total number of bytes for this stream
37cdf0e10cSrcweir 	LARGE_INTEGER	StreamBytesTransferred,
38cdf0e10cSrcweir 									// total number of bytes transferred for
39cdf0e10cSrcweir 									// this stream
40cdf0e10cSrcweir 	DWORD		dwStreamNumber,		// the current stream
41cdf0e10cSrcweir 	DWORD		dwCallbackReason,	// reason for callback
42cdf0e10cSrcweir 	HANDLE	hSourceFile,			// handle to the source file
43cdf0e10cSrcweir 	HANDLE	hDestinationFile,		// handle to the destination file
44cdf0e10cSrcweir 	LPVOID	lpData					// passed by CopyFileEx
45cdf0e10cSrcweir )
46cdf0e10cSrcweir {
47cdf0e10cSrcweir 	return PROGRESS_CONTINUE;
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir IMPLEMENT_THUNK( kernel32, WINDOWS, BOOL, WINAPI, CopyFileExA, ( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, LPPROGRESS_ROUTINE  lpProgressRoutine, LPVOID lpData, LPBOOL pbCancel, DWORD dwCopyFlags ) )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 	BOOL	fSuccess = FALSE; // Assume failure
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	HANDLE	hSourceFile = CreateFileA(
56cdf0e10cSrcweir 		lpExistingFileNameA,
57cdf0e10cSrcweir 		GENERIC_READ,
58cdf0e10cSrcweir 		FILE_SHARE_READ | FILE_SHARE_WRITE,
59cdf0e10cSrcweir 		NULL,
60cdf0e10cSrcweir 		OPEN_EXISTING,
61cdf0e10cSrcweir 		0,
62cdf0e10cSrcweir 		NULL
63cdf0e10cSrcweir 		);
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	if ( IsValidHandle(hSourceFile) )
66cdf0e10cSrcweir 	{
67cdf0e10cSrcweir 		LARGE_INTEGER	FileSize, BytesTransferred;
68cdf0e10cSrcweir 		HANDLE	hTargetFile = NULL;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 		SetLastError( ERROR_SUCCESS );
71cdf0e10cSrcweir 		FileSize.LowPart = GetFileSize( hSourceFile, (LPDWORD)&FileSize.HighPart );
72cdf0e10cSrcweir 		BytesTransferred.QuadPart = 0;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 		if ( (DWORD)-1 != FileSize.LowPart || ERROR_SUCCESS == GetLastError() )
75cdf0e10cSrcweir 			hTargetFile = CreateFileA(
76cdf0e10cSrcweir 				lpNewFileNameA,
77cdf0e10cSrcweir 				GENERIC_WRITE,
78cdf0e10cSrcweir 				0,
79cdf0e10cSrcweir 				NULL,
80cdf0e10cSrcweir 				(DWORD) ((dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) ? CREATE_NEW : CREATE_ALWAYS),
81cdf0e10cSrcweir 				0,
82cdf0e10cSrcweir 				NULL
83cdf0e10cSrcweir 				);
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 		if ( IsValidHandle(hTargetFile) )
86cdf0e10cSrcweir 		{
87cdf0e10cSrcweir 			DWORD dwProgressResult = PROGRESS_CONTINUE;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 			fSuccess = SetEndOfFile( hTargetFile );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 			if ( fSuccess )
92cdf0e10cSrcweir 			{
93cdf0e10cSrcweir 				if ( !lpProgressRoutine )
94cdf0e10cSrcweir 					lpProgressRoutine = DefCopyProgressRoutine;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 				dwProgressResult = lpProgressRoutine(
97cdf0e10cSrcweir 					FileSize,
98cdf0e10cSrcweir 					BytesTransferred,
99cdf0e10cSrcweir 					FileSize,
100cdf0e10cSrcweir 					BytesTransferred,
101cdf0e10cSrcweir 					1,
102cdf0e10cSrcweir 					CALLBACK_STREAM_SWITCH,
103cdf0e10cSrcweir 					hSourceFile,
104cdf0e10cSrcweir 					hTargetFile,
105cdf0e10cSrcweir 					lpData
106cdf0e10cSrcweir 					);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 				// Suppress further notifications
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 				if ( PROGRESS_QUIET == dwProgressResult )
111cdf0e10cSrcweir 				{
112cdf0e10cSrcweir 					lpProgressRoutine = DefCopyProgressRoutine;
113cdf0e10cSrcweir 					dwProgressResult = PROGRESS_CONTINUE;
114cdf0e10cSrcweir 				}
115cdf0e10cSrcweir 			}
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 			while ( fSuccess && PROGRESS_CONTINUE == dwProgressResult )
118cdf0e10cSrcweir 			{
119cdf0e10cSrcweir 				BYTE	buffer[BUFSIZE];
120cdf0e10cSrcweir 				DWORD	dwBytesRead, dwBytesWritten = 0;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 				fSuccess = ReadFile( hSourceFile, buffer, BUFSIZE, &dwBytesRead, NULL );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 				if ( !dwBytesRead ) break;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 				if ( fSuccess )
127cdf0e10cSrcweir 					fSuccess = WriteFile( hTargetFile, buffer, dwBytesRead, &dwBytesWritten, NULL );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 				if ( fSuccess )
130cdf0e10cSrcweir 				{
131cdf0e10cSrcweir 					BytesTransferred.QuadPart += (LONGLONG)dwBytesWritten;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 					if ( pbCancel && *pbCancel )
134cdf0e10cSrcweir 						dwProgressResult = PROGRESS_CANCEL;
135cdf0e10cSrcweir 					else
136cdf0e10cSrcweir 						dwProgressResult = lpProgressRoutine(
137cdf0e10cSrcweir 							FileSize,
138cdf0e10cSrcweir 							BytesTransferred,
139cdf0e10cSrcweir 							FileSize,
140cdf0e10cSrcweir 							BytesTransferred,
141cdf0e10cSrcweir 							1,
142cdf0e10cSrcweir 							CALLBACK_CHUNK_FINISHED,
143cdf0e10cSrcweir 							hSourceFile,
144cdf0e10cSrcweir 							hTargetFile,
145cdf0e10cSrcweir 							lpData
146cdf0e10cSrcweir 							);
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 				}
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 			}
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 			CloseHandle( hTargetFile );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 			if ( PROGRESS_CANCEL == dwProgressResult )
155cdf0e10cSrcweir 				DeleteFileA( lpNewFileNameA );
156cdf0e10cSrcweir 		}
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 		CloseHandle( hSourceFile );
160cdf0e10cSrcweir 	}
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	return fSuccess;
163cdf0e10cSrcweir }