3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/file/copy.c
6 * PURPOSE: Copying files
7 * PROGRAMMER: Ariadne (ariadne@xs4all.nl)
10 * 07/02/99 Moved to seperate file
13 /* INCLUDES ****************************************************************/
15 #include <ddk/ntddk.h>
19 #include <kernel32/kernel32.h>
20 #include <kernel32/error.h>
23 #define LPPROGRESS_ROUTINE void*
26 /* FUNCTIONS ****************************************************************/
31 LPCWSTR lpExistingFileName,
32 LPCWSTR lpNewFileName,
33 LPPROGRESS_ROUTINE lpProgressRoutine,
40 HANDLE FileHandleSource, FileHandleDest;
41 IO_STATUS_BLOCK IoStatusBlock;
42 FILE_STANDARD_INFORMATION FileStandard;
43 FILE_BASIC_INFORMATION FileBasic;
44 FILE_POSITION_INFORMATION FilePosition;
45 UCHAR *lpBuffer = NULL;
46 ULONG RegionSize = 0x1000000;
49 FileHandleSource = CreateFileW(lpExistingFileName,
54 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,
56 if (FileHandleSource == NULL)
61 errCode = NtQueryInformationFile(FileHandleSource,
64 sizeof(FILE_STANDARD_INFORMATION),
65 FileStandardInformation);
66 if (!NT_SUCCESS(errCode))
68 NtClose(FileHandleSource);
69 SetLastErrorByStatus(errCode);
73 errCode = NtQueryInformationFile(FileHandleSource,
74 &IoStatusBlock,&FileBasic,
75 sizeof(FILE_BASIC_INFORMATION),
76 FileBasicInformation);
77 if (!NT_SUCCESS(errCode))
79 NtClose(FileHandleSource);
80 SetLastErrorByStatus(errCode);
84 FileHandleDest = CreateFileW(lpNewFileName,
88 dwCopyFlags ? CREATE_NEW : CREATE_ALWAYS,
89 FileBasic.FileAttributes|FILE_FLAG_NO_BUFFERING,
91 if (FileHandleDest == NULL)
96 FilePosition.CurrentByteOffset.QuadPart = 0;
98 errCode = NtSetInformationFile(FileHandleSource,
101 sizeof(FILE_POSITION_INFORMATION),
102 FilePositionInformation);
103 if (!NT_SUCCESS(errCode))
105 NtClose(FileHandleSource);
106 NtClose(FileHandleDest);
107 SetLastErrorByStatus(errCode);
111 errCode = NtSetInformationFile(FileHandleDest,
114 sizeof(FILE_POSITION_INFORMATION),
115 FilePositionInformation);
116 if (!NT_SUCCESS(errCode))
118 NtClose(FileHandleSource);
119 NtClose(FileHandleDest);
120 SetLastErrorByStatus(errCode);
124 errCode = NtAllocateVirtualMemory(NtCurrentProcess(),
128 MEM_RESERVE | MEM_COMMIT,
131 if (!NT_SUCCESS(errCode))
133 NtClose(FileHandleSource);
134 NtClose(FileHandleDest);
135 SetLastErrorByStatus(errCode);
140 errCode = NtReadFile(FileHandleSource,
144 (PIO_STATUS_BLOCK)&IoStatusBlock,
149 if (pbCancel != NULL)
152 if (!NT_SUCCESS(errCode) || bCancel)
154 NtFreeVirtualMemory(NtCurrentProcess(),
155 (PVOID *)&lpBuffer, &RegionSize,MEM_RELEASE);
156 NtClose(FileHandleSource);
157 NtClose(FileHandleDest);
158 if ( errCode == STATUS_END_OF_FILE )
164 errCode = NtWriteFile(FileHandleDest,
168 (PIO_STATUS_BLOCK)&IoStatusBlock,
170 IoStatusBlock.Information,
174 if (!NT_SUCCESS(errCode))
176 NtFreeVirtualMemory(NtCurrentProcess(),
180 NtClose(FileHandleSource);
181 NtClose(FileHandleDest);
193 LPCSTR lpExistingFileName,
194 LPCSTR lpNewFileName,
195 LPPROGRESS_ROUTINE lpProgressRoutine,
201 UNICODE_STRING ExistingFileNameU;
202 UNICODE_STRING NewFileNameU;
203 ANSI_STRING ExistingFileName;
204 ANSI_STRING NewFileName;
207 RtlInitAnsiString (&ExistingFileName,
208 (LPSTR)lpExistingFileName);
210 RtlInitAnsiString (&NewFileName,
211 (LPSTR)lpNewFileName);
213 /* convert ansi (or oem) string to unicode */
216 RtlAnsiStringToUnicodeString (&ExistingFileNameU,
219 RtlAnsiStringToUnicodeString (&NewFileNameU,
225 RtlOemStringToUnicodeString (&ExistingFileNameU,
228 RtlOemStringToUnicodeString (&NewFileNameU,
233 Result = CopyFileExW (ExistingFileNameU.Buffer,
240 RtlFreeHeap (RtlGetProcessHeap (),
242 ExistingFileNameU.Buffer);
243 RtlFreeHeap (RtlGetProcessHeap (),
245 NewFileNameU.Buffer);
254 LPCSTR lpExistingFileName,
255 LPCSTR lpNewFileName,
256 WINBOOL bFailIfExists
259 return CopyFileExA (lpExistingFileName,
271 LPCWSTR lpExistingFileName,
272 LPCWSTR lpNewFileName,
273 WINBOOL bFailIfExists
276 return CopyFileExW (lpExistingFileName,