Fixed last error code check in GetVolumeInformationW, opening a
[wine/multimedia.git] / files / file.c
blobae5859da2b2d15f2f00da2a9603d2841798f3e4a
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * TODO:
22 * Fix the CopyFileEx methods to implement the "extended" functionality.
23 * Right now, they simply call the CopyFile method.
26 #include "config.h"
27 #include "wine/port.h"
29 #include <assert.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <string.h>
37 #ifdef HAVE_SYS_ERRNO_H
38 #include <sys/errno.h>
39 #endif
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #ifdef HAVE_SYS_MMAN_H
43 #include <sys/mman.h>
44 #endif
45 #ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
47 #endif
48 #ifdef HAVE_SYS_POLL_H
49 # include <sys/poll.h>
50 #endif
51 #include <time.h>
52 #ifdef HAVE_UNISTD_H
53 # include <unistd.h>
54 #endif
55 #ifdef HAVE_UTIME_H
56 # include <utime.h>
57 #endif
58 #ifdef HAVE_IO_H
59 # include <io.h>
60 #endif
62 #define NONAMELESSUNION
63 #define NONAMELESSSTRUCT
64 #include "winerror.h"
65 #include "ntstatus.h"
66 #include "windef.h"
67 #include "winbase.h"
68 #include "winreg.h"
69 #include "winternl.h"
70 #include "wine/winbase16.h"
71 #include "wine/server.h"
73 #include "file.h"
74 #include "wincon.h"
75 #include "kernel_private.h"
77 #include "smb.h"
78 #include "wine/unicode.h"
79 #include "wine/debug.h"
81 WINE_DEFAULT_DEBUG_CHANNEL(file);
84 /***********************************************************************
85 * FILE_ConvertOFMode
87 * Convert OF_* mode into flags for CreateFile.
89 void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
91 switch(mode & 0x03)
93 case OF_READ: *access = GENERIC_READ; break;
94 case OF_WRITE: *access = GENERIC_WRITE; break;
95 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
96 default: *access = 0; break;
98 switch(mode & 0x70)
100 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
101 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
102 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
103 case OF_SHARE_DENY_NONE:
104 case OF_SHARE_COMPAT:
105 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
110 /***********************************************************************
111 * FILE_SetDosError
113 * Set the DOS error code from errno.
115 void FILE_SetDosError(void)
117 int save_errno = errno; /* errno gets overwritten by printf */
119 TRACE("errno = %d %s\n", errno, strerror(errno));
120 switch (save_errno)
122 case EAGAIN:
123 SetLastError( ERROR_SHARING_VIOLATION );
124 break;
125 case EBADF:
126 SetLastError( ERROR_INVALID_HANDLE );
127 break;
128 case ENOSPC:
129 SetLastError( ERROR_HANDLE_DISK_FULL );
130 break;
131 case EACCES:
132 case EPERM:
133 case EROFS:
134 SetLastError( ERROR_ACCESS_DENIED );
135 break;
136 case EBUSY:
137 SetLastError( ERROR_LOCK_VIOLATION );
138 break;
139 case ENOENT:
140 SetLastError( ERROR_FILE_NOT_FOUND );
141 break;
142 case EISDIR:
143 SetLastError( ERROR_CANNOT_MAKE );
144 break;
145 case ENFILE:
146 case EMFILE:
147 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
148 break;
149 case EEXIST:
150 SetLastError( ERROR_FILE_EXISTS );
151 break;
152 case EINVAL:
153 case ESPIPE:
154 SetLastError( ERROR_SEEK );
155 break;
156 case ENOTEMPTY:
157 SetLastError( ERROR_DIR_NOT_EMPTY );
158 break;
159 case ENOEXEC:
160 SetLastError( ERROR_BAD_FORMAT );
161 break;
162 case ENOTDIR:
163 SetLastError( ERROR_PATH_NOT_FOUND );
164 break;
165 case EXDEV:
166 SetLastError( ERROR_NOT_SAME_DEVICE );
167 break;
168 default:
169 WARN("unknown file error: %s\n", strerror(save_errno) );
170 SetLastError( ERROR_GEN_FAILURE );
171 break;
173 errno = save_errno;
177 /*************************************************************************
178 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
180 * Creates or opens an object, and returns a handle that can be used to
181 * access that object.
183 * PARAMS
185 * filename [in] pointer to filename to be accessed
186 * access [in] access mode requested
187 * sharing [in] share mode
188 * sa [in] pointer to security attributes
189 * creation [in] how to create the file
190 * attributes [in] attributes for newly created file
191 * template [in] handle to file with extended attributes to copy
193 * RETURNS
194 * Success: Open handle to specified file
195 * Failure: INVALID_HANDLE_VALUE
197 * NOTES
198 * Should call SetLastError() on failure.
200 * BUGS
202 * Doesn't support character devices, template files, or a
203 * lot of the 'attributes' flags yet.
205 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
206 LPSECURITY_ATTRIBUTES sa, DWORD creation,
207 DWORD attributes, HANDLE template )
209 NTSTATUS status;
210 UINT options;
211 OBJECT_ATTRIBUTES attr;
212 UNICODE_STRING nameW;
213 IO_STATUS_BLOCK io;
214 HANDLE ret;
215 DWORD dosdev;
216 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
217 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
218 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
220 static const char * const creation_name[5] =
221 { "CREATE_NEW", "CREATE_ALWAYS", "OPEN_EXISTING", "OPEN_ALWAYS", "TRUNCATE_EXISTING" };
223 static const UINT nt_disposition[5] =
225 FILE_CREATE, /* CREATE_NEW */
226 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
227 FILE_OPEN, /* OPEN_EXISTING */
228 FILE_OPEN_IF, /* OPEN_ALWAYS */
229 FILE_OVERWRITE /* TRUNCATE_EXISTING */
233 /* sanity checks */
235 if (!filename || !filename[0])
237 SetLastError( ERROR_PATH_NOT_FOUND );
238 return INVALID_HANDLE_VALUE;
241 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
243 SetLastError( ERROR_INVALID_PARAMETER );
244 return INVALID_HANDLE_VALUE;
247 TRACE("%s %s%s%s%s%s%s%s attributes 0x%lx\n", debugstr_w(filename),
248 (access & GENERIC_READ)?"GENERIC_READ ":"",
249 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
250 (!access)?"QUERY_ACCESS ":"",
251 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
252 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
253 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
254 creation_name[creation - CREATE_NEW], attributes);
256 /* Open a console for CONIN$ or CONOUT$ */
258 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
260 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle), creation);
261 goto done;
264 if (!strncmpW(filename, bkslashes_with_dotW, 4))
266 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
268 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
269 !strncmpiW( filename + 4, pipeW, 5 ))
271 dosdev = 0;
273 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
275 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
277 else if (filename[4])
279 ret = VXD_Open( filename+4, access, sa );
280 goto done;
282 else
284 SetLastError( ERROR_INVALID_NAME );
285 return INVALID_HANDLE_VALUE;
288 else dosdev = RtlIsDosDeviceName_U( filename );
290 if (dosdev)
292 static const WCHAR conW[] = {'C','O','N'};
294 if (LOWORD(dosdev) == sizeof(conW) &&
295 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)))
297 switch (access & (GENERIC_READ|GENERIC_WRITE))
299 case GENERIC_READ:
300 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), creation);
301 goto done;
302 case GENERIC_WRITE:
303 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), creation);
304 goto done;
305 default:
306 SetLastError( ERROR_FILE_NOT_FOUND );
307 return INVALID_HANDLE_VALUE;
312 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
314 SetLastError( ERROR_PATH_NOT_FOUND );
315 return INVALID_HANDLE_VALUE;
318 /* now call NtCreateFile */
320 options = 0;
321 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
322 options |= FILE_OPEN_FOR_BACKUP_INTENT;
323 else
324 options |= FILE_NON_DIRECTORY_FILE;
325 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
326 options |= FILE_DELETE_ON_CLOSE;
327 if (!(attributes & FILE_FLAG_OVERLAPPED))
328 options |= FILE_SYNCHRONOUS_IO_ALERT;
329 if (attributes & FILE_FLAG_RANDOM_ACCESS)
330 options |= FILE_RANDOM_ACCESS;
331 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
333 attr.Length = sizeof(attr);
334 attr.RootDirectory = 0;
335 attr.Attributes = OBJ_CASE_INSENSITIVE;
336 attr.ObjectName = &nameW;
337 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
338 attr.SecurityQualityOfService = NULL;
340 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
342 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
343 sharing, nt_disposition[creation - CREATE_NEW],
344 options, NULL, 0 );
345 if (status)
347 WARN("Unable to create file %s (status %lx)\n", debugstr_w(filename), status);
348 ret = INVALID_HANDLE_VALUE;
350 /* In the case file creation was rejected due to CREATE_NEW flag
351 * was specified and file with that name already exists, correct
352 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
353 * Note: RtlNtStatusToDosError is not the subject to blame here.
355 if (status == STATUS_OBJECT_NAME_COLLISION)
356 SetLastError( ERROR_FILE_EXISTS );
357 else
358 SetLastError( RtlNtStatusToDosError(status) );
360 else SetLastError(0);
361 RtlFreeUnicodeString( &nameW );
363 done:
364 if (!ret) ret = INVALID_HANDLE_VALUE;
365 TRACE("returning %p\n", ret);
366 return ret;
371 /*************************************************************************
372 * CreateFileA (KERNEL32.@)
374 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
375 LPSECURITY_ATTRIBUTES sa, DWORD creation,
376 DWORD attributes, HANDLE template)
378 UNICODE_STRING filenameW;
379 HANDLE ret = INVALID_HANDLE_VALUE;
381 if (!filename)
383 SetLastError( ERROR_INVALID_PARAMETER );
384 return INVALID_HANDLE_VALUE;
387 if (RtlCreateUnicodeStringFromAsciiz(&filenameW, filename))
389 ret = CreateFileW(filenameW.Buffer, access, sharing, sa, creation,
390 attributes, template);
391 RtlFreeUnicodeString(&filenameW);
393 else
394 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
395 return ret;
399 /***********************************************************************
400 * GetTempFileNameA (KERNEL32.@)
402 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
403 LPSTR buffer)
405 UNICODE_STRING pathW, prefixW;
406 WCHAR bufferW[MAX_PATH];
407 UINT ret;
409 if ( !path || !prefix || !buffer )
411 SetLastError( ERROR_INVALID_PARAMETER );
412 return 0;
415 RtlCreateUnicodeStringFromAsciiz(&pathW, path);
416 RtlCreateUnicodeStringFromAsciiz(&prefixW, prefix);
418 ret = GetTempFileNameW(pathW.Buffer, prefixW.Buffer, unique, bufferW);
419 if (ret)
420 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
422 RtlFreeUnicodeString(&pathW);
423 RtlFreeUnicodeString(&prefixW);
424 return ret;
427 /***********************************************************************
428 * GetTempFileNameW (KERNEL32.@)
430 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
431 LPWSTR buffer )
433 static const WCHAR formatW[] = {'%','x','.','t','m','p',0};
435 int i;
436 LPWSTR p;
438 if ( !path || !prefix || !buffer )
440 SetLastError( ERROR_INVALID_PARAMETER );
441 return 0;
444 strcpyW( buffer, path );
445 p = buffer + strlenW(buffer);
447 /* add a \, if there isn't one */
448 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
450 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
452 unique &= 0xffff;
454 if (unique) sprintfW( p, formatW, unique );
455 else
457 /* get a "random" unique number and try to create the file */
458 HANDLE handle;
459 UINT num = GetTickCount() & 0xffff;
461 if (!num) num = 1;
462 unique = num;
465 sprintfW( p, formatW, unique );
466 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
467 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
468 if (handle != INVALID_HANDLE_VALUE)
469 { /* We created it */
470 TRACE("created %s\n", debugstr_w(buffer) );
471 CloseHandle( handle );
472 break;
474 if (GetLastError() != ERROR_FILE_EXISTS &&
475 GetLastError() != ERROR_SHARING_VIOLATION)
476 break; /* No need to go on */
477 if (!(++unique & 0xffff)) unique = 1;
478 } while (unique != num);
481 TRACE("returning %s\n", debugstr_w(buffer) );
482 return unique;
486 /******************************************************************
487 * FILE_ReadWriteApc (internal)
491 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG len)
493 LPOVERLAPPED_COMPLETION_ROUTINE cr = (LPOVERLAPPED_COMPLETION_ROUTINE)apc_user;
495 cr(RtlNtStatusToDosError(io_status->u.Status), len, (LPOVERLAPPED)io_status);
498 /***********************************************************************
499 * ReadFileEx (KERNEL32.@)
501 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
502 LPOVERLAPPED overlapped,
503 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
505 LARGE_INTEGER offset;
506 NTSTATUS status;
507 PIO_STATUS_BLOCK io_status;
509 if (!overlapped)
511 SetLastError(ERROR_INVALID_PARAMETER);
512 return FALSE;
515 offset.u.LowPart = overlapped->Offset;
516 offset.u.HighPart = overlapped->OffsetHigh;
517 io_status = (PIO_STATUS_BLOCK)overlapped;
518 io_status->u.Status = STATUS_PENDING;
520 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
521 io_status, buffer, bytesToRead, &offset, NULL);
523 if (status)
525 SetLastError( RtlNtStatusToDosError(status) );
526 return FALSE;
528 return TRUE;
531 /***********************************************************************
532 * ReadFile (KERNEL32.@)
534 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
535 LPDWORD bytesRead, LPOVERLAPPED overlapped )
537 LARGE_INTEGER offset;
538 PLARGE_INTEGER poffset = NULL;
539 IO_STATUS_BLOCK iosb;
540 PIO_STATUS_BLOCK io_status = &iosb;
541 HANDLE hEvent = 0;
542 NTSTATUS status;
544 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToRead,
545 bytesRead, overlapped );
547 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
548 if (!bytesToRead) return TRUE;
550 if (IsBadReadPtr(buffer, bytesToRead))
552 SetLastError(ERROR_WRITE_FAULT); /* FIXME */
553 return FALSE;
555 if (is_console_handle(hFile))
556 return ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
558 if (overlapped != NULL)
560 offset.u.LowPart = overlapped->Offset;
561 offset.u.HighPart = overlapped->OffsetHigh;
562 poffset = &offset;
563 hEvent = overlapped->hEvent;
564 io_status = (PIO_STATUS_BLOCK)overlapped;
566 io_status->u.Status = STATUS_PENDING;
567 io_status->Information = 0;
569 status = NtReadFile(hFile, hEvent, NULL, NULL, io_status, buffer, bytesToRead, poffset, NULL);
571 if (status != STATUS_PENDING && bytesRead)
572 *bytesRead = io_status->Information;
574 if (status && status != STATUS_END_OF_FILE)
576 SetLastError( RtlNtStatusToDosError(status) );
577 return FALSE;
579 return TRUE;
583 /***********************************************************************
584 * WriteFileEx (KERNEL32.@)
586 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
587 LPOVERLAPPED overlapped,
588 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
590 LARGE_INTEGER offset;
591 NTSTATUS status;
592 PIO_STATUS_BLOCK io_status;
594 TRACE("%p %p %ld %p %p\n",
595 hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
597 if (overlapped == NULL)
599 SetLastError(ERROR_INVALID_PARAMETER);
600 return FALSE;
602 offset.u.LowPart = overlapped->Offset;
603 offset.u.HighPart = overlapped->OffsetHigh;
605 io_status = (PIO_STATUS_BLOCK)overlapped;
606 io_status->u.Status = STATUS_PENDING;
608 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
609 io_status, buffer, bytesToWrite, &offset, NULL);
611 if (status) SetLastError( RtlNtStatusToDosError(status) );
612 return !status;
615 /***********************************************************************
616 * WriteFile (KERNEL32.@)
618 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
619 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
621 HANDLE hEvent = NULL;
622 LARGE_INTEGER offset;
623 PLARGE_INTEGER poffset = NULL;
624 NTSTATUS status;
625 IO_STATUS_BLOCK iosb;
626 PIO_STATUS_BLOCK piosb = &iosb;
628 TRACE("%p %p %ld %p %p\n",
629 hFile, buffer, bytesToWrite, bytesWritten, overlapped );
631 if (is_console_handle(hFile))
632 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
634 if (IsBadReadPtr(buffer, bytesToWrite))
636 SetLastError(ERROR_READ_FAULT); /* FIXME */
637 return FALSE;
640 if (overlapped)
642 offset.u.LowPart = overlapped->Offset;
643 offset.u.HighPart = overlapped->OffsetHigh;
644 poffset = &offset;
645 hEvent = overlapped->hEvent;
646 piosb = (PIO_STATUS_BLOCK)overlapped;
648 piosb->u.Status = STATUS_PENDING;
649 piosb->Information = 0;
651 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
652 buffer, bytesToWrite, poffset, NULL);
653 if (status)
655 SetLastError( RtlNtStatusToDosError(status) );
656 return FALSE;
658 if (bytesWritten) *bytesWritten = piosb->Information;
660 return TRUE;
664 /***********************************************************************
665 * SetFilePointer (KERNEL32.@)
667 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword,
668 DWORD method )
670 static const int whence[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
671 DWORD ret = INVALID_SET_FILE_POINTER;
672 NTSTATUS status;
673 int fd;
675 TRACE("handle %p offset %ld high %ld origin %ld\n",
676 hFile, distance, highword?*highword:0, method );
678 if (method > FILE_END)
680 SetLastError( ERROR_INVALID_PARAMETER );
681 return ret;
684 if (!(status = wine_server_handle_to_fd( hFile, 0, &fd, NULL, NULL )))
686 off_t pos, res;
688 if (highword) pos = ((off_t)*highword << 32) | (ULONG)distance;
689 else pos = (off_t)distance;
690 if ((res = lseek( fd, pos, whence[method] )) == (off_t)-1)
692 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
693 if (((errno == EINVAL) || (errno == EPERM)) && (method != FILE_BEGIN) && (pos < 0))
694 SetLastError( ERROR_NEGATIVE_SEEK );
695 else
696 FILE_SetDosError();
698 else
700 ret = (DWORD)res;
701 if (highword) *highword = (res >> 32);
702 if (ret == INVALID_SET_FILE_POINTER) SetLastError( 0 );
704 wine_server_release_fd( hFile, fd );
706 else SetLastError( RtlNtStatusToDosError(status) );
708 return ret;
712 /*************************************************************************
713 * SetHandleCount (KERNEL32.@)
715 UINT WINAPI SetHandleCount( UINT count )
717 return min( 256, count );
721 /**************************************************************************
722 * CopyFileW (KERNEL32.@)
724 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists )
726 HANDLE h1, h2;
727 BY_HANDLE_FILE_INFORMATION info;
728 DWORD count;
729 BOOL ret = FALSE;
730 char buffer[2048];
732 if (!source || !dest)
734 SetLastError(ERROR_INVALID_PARAMETER);
735 return FALSE;
738 TRACE("%s -> %s\n", debugstr_w(source), debugstr_w(dest));
740 if ((h1 = CreateFileW(source, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
741 NULL, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
743 WARN("Unable to open source %s\n", debugstr_w(source));
744 return FALSE;
747 if (!GetFileInformationByHandle( h1, &info ))
749 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
750 CloseHandle( h1 );
751 return FALSE;
754 if ((h2 = CreateFileW( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
755 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
756 info.dwFileAttributes, h1 )) == INVALID_HANDLE_VALUE)
758 WARN("Unable to open dest %s\n", debugstr_w(dest));
759 CloseHandle( h1 );
760 return FALSE;
763 while (ReadFile( h1, buffer, sizeof(buffer), &count, NULL ) && count)
765 char *p = buffer;
766 while (count != 0)
768 DWORD res;
769 if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
770 p += res;
771 count -= res;
774 ret = TRUE;
775 done:
776 CloseHandle( h1 );
777 CloseHandle( h2 );
778 return ret;
782 /**************************************************************************
783 * CopyFileA (KERNEL32.@)
785 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists)
787 UNICODE_STRING sourceW, destW;
788 BOOL ret;
790 if (!source || !dest)
792 SetLastError(ERROR_INVALID_PARAMETER);
793 return FALSE;
796 RtlCreateUnicodeStringFromAsciiz(&sourceW, source);
797 RtlCreateUnicodeStringFromAsciiz(&destW, dest);
799 ret = CopyFileW(sourceW.Buffer, destW.Buffer, fail_if_exists);
801 RtlFreeUnicodeString(&sourceW);
802 RtlFreeUnicodeString(&destW);
803 return ret;
807 /**************************************************************************
808 * CopyFileExW (KERNEL32.@)
810 * This implementation ignores most of the extra parameters passed-in into
811 * the "ex" version of the method and calls the CopyFile method.
812 * It will have to be fixed eventually.
814 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename, LPCWSTR destFilename,
815 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
816 LPBOOL cancelFlagPointer, DWORD copyFlags)
819 * Interpret the only flag that CopyFile can interpret.
821 return CopyFileW(sourceFilename, destFilename, (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0);
825 /**************************************************************************
826 * CopyFileExA (KERNEL32.@)
828 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename, LPCSTR destFilename,
829 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
830 LPBOOL cancelFlagPointer, DWORD copyFlags)
832 UNICODE_STRING sourceW, destW;
833 BOOL ret;
835 if (!sourceFilename || !destFilename)
837 SetLastError(ERROR_INVALID_PARAMETER);
838 return FALSE;
841 RtlCreateUnicodeStringFromAsciiz(&sourceW, sourceFilename);
842 RtlCreateUnicodeStringFromAsciiz(&destW, destFilename);
844 ret = CopyFileExW(sourceW.Buffer, destW.Buffer, progressRoutine, appData,
845 cancelFlagPointer, copyFlags);
847 RtlFreeUnicodeString(&sourceW);
848 RtlFreeUnicodeString(&destW);
849 return ret;