2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 2008 Jeff Zaroyko
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
37 #define WIN32_NO_STATUS
43 #include "kernel_private.h"
45 #include "wine/exception.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(file
);
51 /* info structure for FindFirstFile handle */
54 DWORD magic
; /* magic number */
55 HANDLE handle
; /* handle to directory */
56 CRITICAL_SECTION cs
; /* crit section protecting this structure */
57 FINDEX_SEARCH_OPS search_op
; /* Flags passed to FindFirst. */
58 UNICODE_STRING mask
; /* file mask */
59 UNICODE_STRING path
; /* NT path used to open the directory */
60 BOOL is_root
; /* is directory the root of the drive? */
61 UINT data_pos
; /* current position in dir data */
62 UINT data_len
; /* length of dir data */
63 BYTE data
[8192]; /* directory data */
66 #define FIND_FIRST_MAGIC 0xc0ffee11
68 static BOOL oem_file_apis
;
70 static const WCHAR wildcardsW
[] = { '*','?',0 };
72 /***********************************************************************
75 * Wrapper for CreateFile that takes OF_* mode flags.
77 static HANDLE
create_file_OF( LPCSTR path
, INT mode
)
79 DWORD access
, sharing
, creation
;
83 creation
= CREATE_ALWAYS
;
84 access
= GENERIC_READ
| GENERIC_WRITE
;
88 creation
= OPEN_EXISTING
;
91 case OF_READ
: access
= GENERIC_READ
; break;
92 case OF_WRITE
: access
= GENERIC_WRITE
; break;
93 case OF_READWRITE
: access
= GENERIC_READ
| GENERIC_WRITE
; break;
94 default: access
= 0; break;
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;
107 return CreateFileA( path
, access
, sharing
, NULL
, creation
, FILE_ATTRIBUTE_NORMAL
, 0 );
111 /***********************************************************************
114 * Check if a dir symlink should be returned by FindNextFile.
116 static BOOL
check_dir_symlink( FIND_FIRST_INFO
*info
, const FILE_BOTH_DIR_INFORMATION
*file_info
)
119 ANSI_STRING unix_name
;
120 struct stat st
, parent_st
;
124 str
.MaximumLength
= info
->path
.Length
+ sizeof(WCHAR
) + file_info
->FileNameLength
;
125 if (!(str
.Buffer
= HeapAlloc( GetProcessHeap(), 0, str
.MaximumLength
))) return TRUE
;
126 memcpy( str
.Buffer
, info
->path
.Buffer
, info
->path
.Length
);
127 len
= info
->path
.Length
/ sizeof(WCHAR
);
128 if (!len
|| str
.Buffer
[len
-1] != '\\') str
.Buffer
[len
++] = '\\';
129 memcpy( str
.Buffer
+ len
, file_info
->FileName
, file_info
->FileNameLength
);
130 str
.Length
= len
* sizeof(WCHAR
) + file_info
->FileNameLength
;
132 unix_name
.Buffer
= NULL
;
133 if (!wine_nt_to_unix_file_name( &str
, &unix_name
, OPEN_EXISTING
, FALSE
) &&
134 !stat( unix_name
.Buffer
, &st
))
136 char *p
= unix_name
.Buffer
+ unix_name
.Length
- 1;
138 /* skip trailing slashes */
139 while (p
> unix_name
.Buffer
&& *p
== '/') p
--;
141 while (ret
&& p
> unix_name
.Buffer
)
143 while (p
> unix_name
.Buffer
&& *p
!= '/') p
--;
144 while (p
> unix_name
.Buffer
&& *p
== '/') p
--;
146 if (!stat( unix_name
.Buffer
, &parent_st
) &&
147 parent_st
.st_dev
== st
.st_dev
&&
148 parent_st
.st_ino
== st
.st_ino
)
150 WARN( "suppressing dir symlink %s pointing to parent %s\n",
151 debugstr_wn( str
.Buffer
, str
.Length
/sizeof(WCHAR
) ),
152 debugstr_a( unix_name
.Buffer
));
157 RtlFreeAnsiString( &unix_name
);
158 RtlFreeUnicodeString( &str
);
163 /***********************************************************************
166 * Set the DOS error code from errno.
168 void FILE_SetDosError(void)
170 int save_errno
= errno
; /* errno gets overwritten by printf */
172 TRACE("errno = %d %s\n", errno
, strerror(errno
));
176 SetLastError( ERROR_SHARING_VIOLATION
);
179 SetLastError( ERROR_INVALID_HANDLE
);
182 SetLastError( ERROR_HANDLE_DISK_FULL
);
187 SetLastError( ERROR_ACCESS_DENIED
);
190 SetLastError( ERROR_LOCK_VIOLATION
);
193 SetLastError( ERROR_FILE_NOT_FOUND
);
196 SetLastError( ERROR_CANNOT_MAKE
);
200 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
203 SetLastError( ERROR_FILE_EXISTS
);
207 SetLastError( ERROR_SEEK
);
210 SetLastError( ERROR_DIR_NOT_EMPTY
);
213 SetLastError( ERROR_BAD_FORMAT
);
216 SetLastError( ERROR_PATH_NOT_FOUND
);
219 SetLastError( ERROR_NOT_SAME_DEVICE
);
222 WARN("unknown file error: %s\n", strerror(save_errno
) );
223 SetLastError( ERROR_GEN_FAILURE
);
230 /***********************************************************************
233 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
235 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
236 * there is no possibility for the function to do that twice, taking into
237 * account any called function.
239 WCHAR
*FILE_name_AtoW( LPCSTR name
, BOOL alloc
)
242 UNICODE_STRING strW
, *pstrW
;
245 RtlInitAnsiString( &str
, name
);
246 pstrW
= alloc
? &strW
: &NtCurrentTeb()->StaticUnicodeString
;
248 status
= RtlOemStringToUnicodeString( pstrW
, &str
, alloc
);
250 status
= RtlAnsiStringToUnicodeString( pstrW
, &str
, alloc
);
251 if (status
== STATUS_SUCCESS
) return pstrW
->Buffer
;
253 if (status
== STATUS_BUFFER_OVERFLOW
)
254 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
256 SetLastError( RtlNtStatusToDosError(status
) );
261 /***********************************************************************
264 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
266 DWORD
FILE_name_WtoA( LPCWSTR src
, INT srclen
, LPSTR dest
, INT destlen
)
270 if (srclen
< 0) srclen
= strlenW( src
) + 1;
272 RtlUnicodeToOemN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
274 RtlUnicodeToMultiByteN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
279 /**************************************************************************
280 * SetFileApisToOEM (KERNEL32.@)
282 VOID WINAPI
SetFileApisToOEM(void)
284 oem_file_apis
= TRUE
;
288 /**************************************************************************
289 * SetFileApisToANSI (KERNEL32.@)
291 VOID WINAPI
SetFileApisToANSI(void)
293 oem_file_apis
= FALSE
;
297 /******************************************************************************
298 * AreFileApisANSI (KERNEL32.@)
300 * Determines if file functions are using ANSI
303 * TRUE: Set of file functions is using ANSI code page
304 * FALSE: Set of file functions is using OEM code page
306 BOOL WINAPI
AreFileApisANSI(void)
308 return !oem_file_apis
;
312 /**************************************************************************
313 * Operations on file handles *
314 **************************************************************************/
316 /******************************************************************
317 * FILE_ReadWriteApc (internal)
319 static void WINAPI
FILE_ReadWriteApc(void* apc_user
, PIO_STATUS_BLOCK io_status
, ULONG reserved
)
321 LPOVERLAPPED_COMPLETION_ROUTINE cr
= apc_user
;
323 cr(RtlNtStatusToDosError(io_status
->u
.Status
), io_status
->Information
, (LPOVERLAPPED
)io_status
);
327 /***********************************************************************
328 * ReadFileEx (KERNEL32.@)
330 BOOL WINAPI
ReadFileEx(HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
331 LPOVERLAPPED overlapped
,
332 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
334 LARGE_INTEGER offset
;
336 PIO_STATUS_BLOCK io_status
;
338 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile
, buffer
, bytesToRead
, overlapped
, lpCompletionRoutine
);
342 SetLastError(ERROR_INVALID_PARAMETER
);
346 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
347 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
348 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
349 io_status
->u
.Status
= STATUS_PENDING
;
350 io_status
->Information
= 0;
352 status
= NtReadFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
353 io_status
, buffer
, bytesToRead
, &offset
, NULL
);
355 if (status
&& status
!= STATUS_PENDING
)
357 SetLastError( RtlNtStatusToDosError(status
) );
364 /***********************************************************************
365 * ReadFileScatter (KERNEL32.@)
367 BOOL WINAPI
ReadFileScatter( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
368 LPDWORD reserved
, LPOVERLAPPED overlapped
)
370 PIO_STATUS_BLOCK io_status
;
371 LARGE_INTEGER offset
;
374 TRACE( "(%p %p %u %p)\n", file
, segments
, count
, overlapped
);
376 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
377 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
378 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
379 io_status
->u
.Status
= STATUS_PENDING
;
380 io_status
->Information
= 0;
382 status
= NtReadFileScatter( file
, NULL
, NULL
, NULL
, io_status
, segments
, count
, &offset
, NULL
);
383 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
388 /***********************************************************************
389 * ReadFile (KERNEL32.@)
391 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
392 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
394 LARGE_INTEGER offset
;
395 PLARGE_INTEGER poffset
= NULL
;
396 IO_STATUS_BLOCK iosb
;
397 PIO_STATUS_BLOCK io_status
= &iosb
;
400 LPVOID cvalue
= NULL
;
402 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToRead
,
403 bytesRead
, overlapped
);
405 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
406 if (!bytesToRead
) return TRUE
;
408 if (is_console_handle(hFile
))
411 if (!ReadConsoleA(hFile
, buffer
, bytesToRead
, &conread
, NULL
) ||
412 !GetConsoleMode(hFile
, &mode
))
414 /* ctrl-Z (26) means end of file on window (if at beginning of buffer)
415 * but Unix uses ctrl-D (4), and ctrl-Z is a bad idea on Unix :-/
416 * So map both ctrl-D ctrl-Z to EOF.
418 if ((mode
& ENABLE_PROCESSED_INPUT
) && conread
> 0 &&
419 (((char*)buffer
)[0] == 26 || ((char*)buffer
)[0] == 4))
423 if (bytesRead
) *bytesRead
= conread
;
427 if (overlapped
!= NULL
)
429 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
430 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
432 hEvent
= overlapped
->hEvent
;
433 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
434 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
436 io_status
->u
.Status
= STATUS_PENDING
;
437 io_status
->Information
= 0;
439 status
= NtReadFile(hFile
, hEvent
, NULL
, cvalue
, io_status
, buffer
, bytesToRead
, poffset
, NULL
);
441 if (status
== STATUS_PENDING
&& !overlapped
)
443 WaitForSingleObject( hFile
, INFINITE
);
444 status
= io_status
->u
.Status
;
447 if (status
!= STATUS_PENDING
&& bytesRead
)
448 *bytesRead
= io_status
->Information
;
450 if (status
&& status
!= STATUS_END_OF_FILE
&& status
!= STATUS_TIMEOUT
)
452 SetLastError( RtlNtStatusToDosError(status
) );
459 /***********************************************************************
460 * WriteFileEx (KERNEL32.@)
462 BOOL WINAPI
WriteFileEx(HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
463 LPOVERLAPPED overlapped
,
464 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
466 LARGE_INTEGER offset
;
468 PIO_STATUS_BLOCK io_status
;
470 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, overlapped
, lpCompletionRoutine
);
472 if (overlapped
== NULL
)
474 SetLastError(ERROR_INVALID_PARAMETER
);
477 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
478 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
480 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
481 io_status
->u
.Status
= STATUS_PENDING
;
482 io_status
->Information
= 0;
484 status
= NtWriteFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
485 io_status
, buffer
, bytesToWrite
, &offset
, NULL
);
487 if (status
&& status
!= STATUS_PENDING
)
489 SetLastError( RtlNtStatusToDosError(status
) );
496 /***********************************************************************
497 * WriteFileGather (KERNEL32.@)
499 BOOL WINAPI
WriteFileGather( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
500 LPDWORD reserved
, LPOVERLAPPED overlapped
)
502 PIO_STATUS_BLOCK io_status
;
503 LARGE_INTEGER offset
;
506 TRACE( "%p %p %u %p\n", file
, segments
, count
, overlapped
);
508 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
509 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
510 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
511 io_status
->u
.Status
= STATUS_PENDING
;
512 io_status
->Information
= 0;
514 status
= NtWriteFileGather( file
, NULL
, NULL
, NULL
, io_status
, segments
, count
, &offset
, NULL
);
515 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
520 /***********************************************************************
521 * WriteFile (KERNEL32.@)
523 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
524 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
526 HANDLE hEvent
= NULL
;
527 LARGE_INTEGER offset
;
528 PLARGE_INTEGER poffset
= NULL
;
530 IO_STATUS_BLOCK iosb
;
531 PIO_STATUS_BLOCK piosb
= &iosb
;
532 LPVOID cvalue
= NULL
;
534 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, bytesWritten
, overlapped
);
536 if (is_console_handle(hFile
))
537 return WriteConsoleA(hFile
, buffer
, bytesToWrite
, bytesWritten
, NULL
);
541 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
542 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
544 hEvent
= overlapped
->hEvent
;
545 piosb
= (PIO_STATUS_BLOCK
)overlapped
;
546 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
548 piosb
->u
.Status
= STATUS_PENDING
;
549 piosb
->Information
= 0;
551 status
= NtWriteFile(hFile
, hEvent
, NULL
, cvalue
, piosb
,
552 buffer
, bytesToWrite
, poffset
, NULL
);
554 if (status
== STATUS_PENDING
&& !overlapped
)
556 WaitForSingleObject( hFile
, INFINITE
);
557 status
= piosb
->u
.Status
;
560 if (status
!= STATUS_PENDING
&& bytesWritten
)
561 *bytesWritten
= piosb
->Information
;
563 if (status
&& status
!= STATUS_TIMEOUT
)
565 SetLastError( RtlNtStatusToDosError(status
) );
572 /***********************************************************************
573 * GetOverlappedResult (KERNEL32.@)
575 * Check the result of an Asynchronous data transfer from a file.
578 * HANDLE hFile [in] handle of file to check on
579 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
580 * LPDWORD lpTransferred [in/out] number of bytes transferred
581 * BOOL bWait [in] wait for the transfer to complete ?
587 * If successful (and relevant) lpTransferred will hold the number of
588 * bytes transferred during the async operation.
590 BOOL WINAPI
GetOverlappedResult(HANDLE hFile
, LPOVERLAPPED lpOverlapped
,
591 LPDWORD lpTransferred
, BOOL bWait
)
595 TRACE( "(%p %p %p %x)\n", hFile
, lpOverlapped
, lpTransferred
, bWait
);
597 status
= lpOverlapped
->Internal
;
598 if (status
== STATUS_PENDING
)
602 SetLastError( ERROR_IO_INCOMPLETE
);
606 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: hFile
,
607 INFINITE
) == WAIT_FAILED
)
609 status
= lpOverlapped
->Internal
;
612 *lpTransferred
= lpOverlapped
->InternalHigh
;
614 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
618 /***********************************************************************
619 * CancelIoEx (KERNEL32.@)
621 * Cancels pending I/O operations on a file given the overlapped used.
624 * handle [I] File handle.
625 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
629 * Failure: FALSE, check GetLastError().
631 BOOL WINAPI
CancelIoEx(HANDLE handle
, LPOVERLAPPED lpOverlapped
)
633 IO_STATUS_BLOCK io_status
;
635 NtCancelIoFileEx(handle
, (PIO_STATUS_BLOCK
) lpOverlapped
, &io_status
);
636 if (io_status
.u
.Status
)
638 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
644 /***********************************************************************
645 * CancelIo (KERNEL32.@)
647 * Cancels pending I/O operations initiated by the current thread on a file.
650 * handle [I] File handle.
654 * Failure: FALSE, check GetLastError().
656 BOOL WINAPI
CancelIo(HANDLE handle
)
658 IO_STATUS_BLOCK io_status
;
660 NtCancelIoFile(handle
, &io_status
);
661 if (io_status
.u
.Status
)
663 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
669 /***********************************************************************
670 * _hread (KERNEL32.@)
672 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
674 return _lread( hFile
, buffer
, count
);
678 /***********************************************************************
679 * _hwrite (KERNEL32.@)
681 * experimentation yields that _lwrite:
682 * o truncates the file at the current position with
684 * o returns 0 on a 0 length write
685 * o works with console handles
688 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
692 TRACE("%d %p %d\n", handle
, buffer
, count
);
696 /* Expand or truncate at current position */
697 if (!SetEndOfFile( LongToHandle(handle
) )) return HFILE_ERROR
;
700 if (!WriteFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
706 /***********************************************************************
707 * _lclose (KERNEL32.@)
709 HFILE WINAPI
_lclose( HFILE hFile
)
711 TRACE("handle %d\n", hFile
);
712 return CloseHandle( LongToHandle(hFile
) ) ? 0 : HFILE_ERROR
;
716 /***********************************************************************
717 * _lcreat (KERNEL32.@)
719 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
723 /* Mask off all flags not explicitly allowed by the doc */
724 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
725 TRACE("%s %02x\n", path
, attr
);
726 hfile
= CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
727 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
728 CREATE_ALWAYS
, attr
, 0 );
729 return HandleToLong(hfile
);
733 /***********************************************************************
734 * _lopen (KERNEL32.@)
736 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
740 TRACE("(%s,%04x)\n", debugstr_a(path
), mode
);
741 hfile
= create_file_OF( path
, mode
& ~OF_CREATE
);
742 return HandleToLong(hfile
);
745 /***********************************************************************
746 * _lread (KERNEL32.@)
748 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
751 if (!ReadFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
757 /***********************************************************************
758 * _llseek (KERNEL32.@)
760 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
762 return SetFilePointer( LongToHandle(hFile
), lOffset
, NULL
, nOrigin
);
766 /***********************************************************************
767 * _lwrite (KERNEL32.@)
769 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
771 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
775 /***********************************************************************
776 * FlushFileBuffers (KERNEL32.@)
778 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
781 IO_STATUS_BLOCK ioblk
;
783 if (is_console_handle( hFile
))
785 /* this will fail (as expected) for an output handle */
786 return FlushConsoleInputBuffer( hFile
);
788 nts
= NtFlushBuffersFile( hFile
, &ioblk
);
789 if (nts
!= STATUS_SUCCESS
)
791 SetLastError( RtlNtStatusToDosError( nts
) );
799 /***********************************************************************
800 * GetFileType (KERNEL32.@)
802 DWORD WINAPI
GetFileType( HANDLE hFile
)
804 FILE_FS_DEVICE_INFORMATION info
;
808 if (is_console_handle( hFile
)) return FILE_TYPE_CHAR
;
810 status
= NtQueryVolumeInformationFile( hFile
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
811 if (status
!= STATUS_SUCCESS
)
813 SetLastError( RtlNtStatusToDosError(status
) );
814 return FILE_TYPE_UNKNOWN
;
817 switch(info
.DeviceType
)
819 case FILE_DEVICE_NULL
:
820 case FILE_DEVICE_SERIAL_PORT
:
821 case FILE_DEVICE_PARALLEL_PORT
:
822 case FILE_DEVICE_TAPE
:
823 case FILE_DEVICE_UNKNOWN
:
824 return FILE_TYPE_CHAR
;
825 case FILE_DEVICE_NAMED_PIPE
:
826 return FILE_TYPE_PIPE
;
828 return FILE_TYPE_DISK
;
833 /***********************************************************************
834 * GetFileInformationByHandle (KERNEL32.@)
836 BOOL WINAPI
GetFileInformationByHandle( HANDLE hFile
, BY_HANDLE_FILE_INFORMATION
*info
)
838 FILE_ALL_INFORMATION all_info
;
842 status
= NtQueryInformationFile( hFile
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
843 if (status
== STATUS_BUFFER_OVERFLOW
) status
= STATUS_SUCCESS
;
844 if (status
== STATUS_SUCCESS
)
846 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
847 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
848 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
849 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
850 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
851 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
852 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
853 info
->dwVolumeSerialNumber
= 0; /* FIXME */
854 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
855 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
856 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
857 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
858 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
861 SetLastError( RtlNtStatusToDosError(status
) );
866 /***********************************************************************
867 * GetFileSize (KERNEL32.@)
869 * Retrieve the size of a file.
872 * hFile [I] File to retrieve size of.
873 * filesizehigh [O] On return, the high bits of the file size.
876 * Success: The low bits of the file size.
877 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
878 * check GetLastError() for values other than ERROR_SUCCESS.
880 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
883 if (!GetFileSizeEx( hFile
, &size
)) return INVALID_FILE_SIZE
;
884 if (filesizehigh
) *filesizehigh
= size
.u
.HighPart
;
885 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError(0);
886 return size
.u
.LowPart
;
890 /***********************************************************************
891 * GetFileSizeEx (KERNEL32.@)
893 * Retrieve the size of a file.
896 * hFile [I] File to retrieve size of.
897 * lpFileSIze [O] On return, the size of the file.
901 * Failure: FALSE, check GetLastError().
903 BOOL WINAPI
GetFileSizeEx( HANDLE hFile
, PLARGE_INTEGER lpFileSize
)
905 FILE_STANDARD_INFORMATION info
;
909 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileStandardInformation
);
910 if (status
== STATUS_SUCCESS
)
912 *lpFileSize
= info
.EndOfFile
;
915 SetLastError( RtlNtStatusToDosError(status
) );
920 /**************************************************************************
921 * SetEndOfFile (KERNEL32.@)
923 * Sets the current position as the end of the file.
926 * hFile [I] File handle.
930 * Failure: FALSE, check GetLastError().
932 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
934 FILE_POSITION_INFORMATION pos
;
935 FILE_END_OF_FILE_INFORMATION eof
;
939 status
= NtQueryInformationFile( hFile
, &io
, &pos
, sizeof(pos
), FilePositionInformation
);
940 if (status
== STATUS_SUCCESS
)
942 eof
.EndOfFile
= pos
.CurrentByteOffset
;
943 status
= NtSetInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
945 if (status
== STATUS_SUCCESS
) return TRUE
;
946 SetLastError( RtlNtStatusToDosError(status
) );
951 /***********************************************************************
952 * SetFilePointer (KERNEL32.@)
954 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
, DWORD method
)
956 LARGE_INTEGER dist
, newpos
;
960 dist
.u
.LowPart
= distance
;
961 dist
.u
.HighPart
= *highword
;
963 else dist
.QuadPart
= distance
;
965 if (!SetFilePointerEx( hFile
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
967 if (highword
) *highword
= newpos
.u
.HighPart
;
968 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
969 return newpos
.u
.LowPart
;
973 /***********************************************************************
974 * SetFilePointerEx (KERNEL32.@)
976 BOOL WINAPI
SetFilePointerEx( HANDLE hFile
, LARGE_INTEGER distance
,
977 LARGE_INTEGER
*newpos
, DWORD method
)
981 FILE_POSITION_INFORMATION info
;
986 pos
= distance
.QuadPart
;
989 if (NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
991 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
995 FILE_END_OF_FILE_INFORMATION eof
;
996 if (NtQueryInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
998 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
1002 SetLastError( ERROR_INVALID_PARAMETER
);
1008 SetLastError( ERROR_NEGATIVE_SEEK
);
1012 info
.CurrentByteOffset
.QuadPart
= pos
;
1013 if (NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
1015 if (newpos
) newpos
->QuadPart
= pos
;
1019 SetLastError( RtlNtStatusToDosError(io
.u
.Status
) );
1023 /***********************************************************************
1024 * SetFileValidData (KERNEL32.@)
1026 BOOL WINAPI
SetFileValidData( HANDLE hFile
, LONGLONG ValidDataLength
)
1028 FIXME("stub: %p, %s\n", hFile
, wine_dbgstr_longlong(ValidDataLength
));
1029 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1033 /***********************************************************************
1034 * GetFileTime (KERNEL32.@)
1036 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
1037 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
1039 FILE_BASIC_INFORMATION info
;
1043 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1044 if (status
== STATUS_SUCCESS
)
1048 lpCreationTime
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1049 lpCreationTime
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1051 if (lpLastAccessTime
)
1053 lpLastAccessTime
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1054 lpLastAccessTime
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1056 if (lpLastWriteTime
)
1058 lpLastWriteTime
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1059 lpLastWriteTime
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1063 SetLastError( RtlNtStatusToDosError(status
) );
1068 /***********************************************************************
1069 * SetFileTime (KERNEL32.@)
1071 BOOL WINAPI
SetFileTime( HANDLE hFile
, const FILETIME
*ctime
,
1072 const FILETIME
*atime
, const FILETIME
*mtime
)
1074 FILE_BASIC_INFORMATION info
;
1078 memset( &info
, 0, sizeof(info
) );
1081 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
1082 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
1086 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
1087 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
1091 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
1092 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
1095 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1096 if (status
== STATUS_SUCCESS
) return TRUE
;
1097 SetLastError( RtlNtStatusToDosError(status
) );
1102 /**************************************************************************
1103 * LockFile (KERNEL32.@)
1105 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1106 DWORD count_low
, DWORD count_high
)
1109 LARGE_INTEGER count
, offset
;
1111 TRACE( "%p %x%08x %x%08x\n",
1112 hFile
, offset_high
, offset_low
, count_high
, count_low
);
1114 count
.u
.LowPart
= count_low
;
1115 count
.u
.HighPart
= count_high
;
1116 offset
.u
.LowPart
= offset_low
;
1117 offset
.u
.HighPart
= offset_high
;
1119 status
= NtLockFile( hFile
, 0, NULL
, NULL
,
1120 NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
);
1122 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1127 /**************************************************************************
1128 * LockFileEx [KERNEL32.@]
1130 * Locks a byte range within an open file for shared or exclusive access.
1137 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1139 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1140 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
1143 LARGE_INTEGER count
, offset
;
1144 LPVOID cvalue
= NULL
;
1148 SetLastError( ERROR_INVALID_PARAMETER
);
1152 TRACE( "%p %x%08x %x%08x flags %x\n",
1153 hFile
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
,
1154 count_high
, count_low
, flags
);
1156 count
.u
.LowPart
= count_low
;
1157 count
.u
.HighPart
= count_high
;
1158 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
1159 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
1161 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1163 status
= NtLockFile( hFile
, overlapped
->hEvent
, NULL
, cvalue
,
1164 NULL
, &offset
, &count
, NULL
,
1165 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
1166 flags
& LOCKFILE_EXCLUSIVE_LOCK
);
1168 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1173 /**************************************************************************
1174 * UnlockFile (KERNEL32.@)
1176 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1177 DWORD count_low
, DWORD count_high
)
1180 LARGE_INTEGER count
, offset
;
1182 count
.u
.LowPart
= count_low
;
1183 count
.u
.HighPart
= count_high
;
1184 offset
.u
.LowPart
= offset_low
;
1185 offset
.u
.HighPart
= offset_high
;
1187 status
= NtUnlockFile( hFile
, NULL
, &offset
, &count
, NULL
);
1188 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1193 /**************************************************************************
1194 * UnlockFileEx (KERNEL32.@)
1196 BOOL WINAPI
UnlockFileEx( HANDLE hFile
, DWORD reserved
, DWORD count_low
, DWORD count_high
,
1197 LPOVERLAPPED overlapped
)
1201 SetLastError( ERROR_INVALID_PARAMETER
);
1204 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
1206 return UnlockFile( hFile
, overlapped
->u
.s
.Offset
, overlapped
->u
.s
.OffsetHigh
, count_low
, count_high
);
1210 /*************************************************************************
1211 * SetHandleCount (KERNEL32.@)
1213 UINT WINAPI
SetHandleCount( UINT count
)
1219 /**************************************************************************
1220 * Operations on file names *
1221 **************************************************************************/
1224 /*************************************************************************
1225 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1227 * Creates or opens an object, and returns a handle that can be used to
1228 * access that object.
1232 * filename [in] pointer to filename to be accessed
1233 * access [in] access mode requested
1234 * sharing [in] share mode
1235 * sa [in] pointer to security attributes
1236 * creation [in] how to create the file
1237 * attributes [in] attributes for newly created file
1238 * template [in] handle to file with extended attributes to copy
1241 * Success: Open handle to specified file
1242 * Failure: INVALID_HANDLE_VALUE
1244 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
1245 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1246 DWORD attributes
, HANDLE
template )
1250 OBJECT_ATTRIBUTES attr
;
1251 UNICODE_STRING nameW
;
1255 const WCHAR
*vxd_name
= NULL
;
1256 static const WCHAR bkslashes_with_dotW
[] = {'\\','\\','.','\\',0};
1257 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
1258 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
1259 SECURITY_QUALITY_OF_SERVICE qos
;
1261 static const UINT nt_disposition
[5] =
1263 FILE_CREATE
, /* CREATE_NEW */
1264 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
1265 FILE_OPEN
, /* OPEN_EXISTING */
1266 FILE_OPEN_IF
, /* OPEN_ALWAYS */
1267 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
1273 if (!filename
|| !filename
[0])
1275 SetLastError( ERROR_PATH_NOT_FOUND
);
1276 return INVALID_HANDLE_VALUE
;
1279 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
1280 (access
& GENERIC_READ
)?"GENERIC_READ ":"",
1281 (access
& GENERIC_WRITE
)?"GENERIC_WRITE ":"",
1282 (access
& GENERIC_EXECUTE
)?"GENERIC_EXECUTE ":"",
1283 (!access
)?"QUERY_ACCESS ":"",
1284 (sharing
& FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
1285 (sharing
& FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
1286 (sharing
& FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
1287 creation
, attributes
);
1289 /* Open a console for CONIN$ or CONOUT$ */
1291 if (!strcmpiW(filename
, coninW
) || !strcmpiW(filename
, conoutW
))
1293 ret
= OpenConsoleW(filename
, access
, (sa
&& sa
->bInheritHandle
),
1294 creation
? OPEN_EXISTING
: 0);
1295 if (ret
== INVALID_HANDLE_VALUE
) SetLastError(ERROR_INVALID_PARAMETER
);
1299 if (!strncmpW(filename
, bkslashes_with_dotW
, 4))
1301 static const WCHAR pipeW
[] = {'P','I','P','E','\\',0};
1302 static const WCHAR mailslotW
[] = {'M','A','I','L','S','L','O','T','\\',0};
1304 if ((isalphaW(filename
[4]) && filename
[5] == ':' && filename
[6] == '\0') ||
1305 !strncmpiW( filename
+ 4, pipeW
, 5 ) ||
1306 !strncmpiW( filename
+ 4, mailslotW
, 9 ))
1310 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
1312 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
1314 else if (GetVersion() & 0x80000000)
1316 vxd_name
= filename
+ 4;
1317 if (!creation
) creation
= OPEN_EXISTING
;
1320 else dosdev
= RtlIsDosDeviceName_U( filename
);
1324 static const WCHAR conW
[] = {'C','O','N'};
1326 if (LOWORD(dosdev
) == sizeof(conW
) &&
1327 !memicmpW( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), conW
, sizeof(conW
)/sizeof(WCHAR
)))
1329 switch (access
& (GENERIC_READ
|GENERIC_WRITE
))
1332 ret
= OpenConsoleW(coninW
, access
, (sa
&& sa
->bInheritHandle
), OPEN_EXISTING
);
1335 ret
= OpenConsoleW(conoutW
, access
, (sa
&& sa
->bInheritHandle
), OPEN_EXISTING
);
1338 SetLastError( ERROR_FILE_NOT_FOUND
);
1339 return INVALID_HANDLE_VALUE
;
1344 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
1346 SetLastError( ERROR_INVALID_PARAMETER
);
1347 return INVALID_HANDLE_VALUE
;
1350 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
1352 SetLastError( ERROR_PATH_NOT_FOUND
);
1353 return INVALID_HANDLE_VALUE
;
1356 /* now call NtCreateFile */
1359 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
1360 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
1362 options
|= FILE_NON_DIRECTORY_FILE
;
1363 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
1365 options
|= FILE_DELETE_ON_CLOSE
;
1368 if (attributes
& FILE_FLAG_NO_BUFFERING
)
1369 options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
1370 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
1371 options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
1372 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
1373 options
|= FILE_RANDOM_ACCESS
;
1374 attributes
&= FILE_ATTRIBUTE_VALID_FLAGS
;
1376 attr
.Length
= sizeof(attr
);
1377 attr
.RootDirectory
= 0;
1378 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1379 attr
.ObjectName
= &nameW
;
1380 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1381 if (attributes
& SECURITY_SQOS_PRESENT
)
1383 qos
.Length
= sizeof(qos
);
1384 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
1385 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
1386 qos
.EffectiveOnly
= attributes
& SECURITY_EFFECTIVE_ONLY
? TRUE
: FALSE
;
1387 attr
.SecurityQualityOfService
= &qos
;
1390 attr
.SecurityQualityOfService
= NULL
;
1392 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
1394 status
= NtCreateFile( &ret
, access
, &attr
, &io
, NULL
, attributes
,
1395 sharing
, nt_disposition
[creation
- CREATE_NEW
],
1399 if (vxd_name
&& vxd_name
[0])
1401 static HANDLE (*vxd_open
)(LPCWSTR
,DWORD
,SECURITY_ATTRIBUTES
*);
1402 if (!vxd_open
) vxd_open
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1403 "__wine_vxd_open" );
1404 if (vxd_open
&& (ret
= vxd_open( vxd_name
, access
, sa
))) goto done
;
1407 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
1408 ret
= INVALID_HANDLE_VALUE
;
1410 /* In the case file creation was rejected due to CREATE_NEW flag
1411 * was specified and file with that name already exists, correct
1412 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1413 * Note: RtlNtStatusToDosError is not the subject to blame here.
1415 if (status
== STATUS_OBJECT_NAME_COLLISION
)
1416 SetLastError( ERROR_FILE_EXISTS
);
1418 SetLastError( RtlNtStatusToDosError(status
) );
1422 if ((creation
== CREATE_ALWAYS
&& io
.Information
== FILE_OVERWRITTEN
) ||
1423 (creation
== OPEN_ALWAYS
&& io
.Information
== FILE_OPENED
))
1424 SetLastError( ERROR_ALREADY_EXISTS
);
1428 RtlFreeUnicodeString( &nameW
);
1431 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
1432 TRACE("returning %p\n", ret
);
1438 /*************************************************************************
1439 * CreateFileA (KERNEL32.@)
1443 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
1444 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1445 DWORD attributes
, HANDLE
template)
1449 if (!(nameW
= FILE_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1450 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
1454 /***********************************************************************
1455 * DeleteFileW (KERNEL32.@)
1460 * path [I] Path to the file to delete.
1464 * Failure: FALSE, check GetLastError().
1466 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1468 UNICODE_STRING nameW
;
1469 OBJECT_ATTRIBUTES attr
;
1474 TRACE("%s\n", debugstr_w(path
) );
1476 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
1478 SetLastError( ERROR_PATH_NOT_FOUND
);
1482 attr
.Length
= sizeof(attr
);
1483 attr
.RootDirectory
= 0;
1484 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1485 attr
.ObjectName
= &nameW
;
1486 attr
.SecurityDescriptor
= NULL
;
1487 attr
.SecurityQualityOfService
= NULL
;
1489 status
= NtCreateFile(&hFile
, GENERIC_READ
| GENERIC_WRITE
| DELETE
,
1490 &attr
, &io
, NULL
, 0,
1491 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1492 FILE_OPEN
, FILE_DELETE_ON_CLOSE
| FILE_NON_DIRECTORY_FILE
, NULL
, 0);
1493 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
1495 RtlFreeUnicodeString( &nameW
);
1498 SetLastError( RtlNtStatusToDosError(status
) );
1505 /***********************************************************************
1506 * DeleteFileA (KERNEL32.@)
1510 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1514 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1515 return DeleteFileW( pathW
);
1519 /**************************************************************************
1520 * ReplaceFileW (KERNEL32.@)
1521 * ReplaceFile (KERNEL32.@)
1523 BOOL WINAPI
ReplaceFileW(LPCWSTR lpReplacedFileName
, LPCWSTR lpReplacementFileName
,
1524 LPCWSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1525 LPVOID lpExclude
, LPVOID lpReserved
)
1527 UNICODE_STRING nt_replaced_name
, nt_replacement_name
;
1528 ANSI_STRING unix_replaced_name
, unix_replacement_name
, unix_backup_name
;
1529 HANDLE hReplaced
= NULL
, hReplacement
= NULL
, hBackup
= NULL
;
1530 DWORD error
= ERROR_SUCCESS
;
1531 UINT replaced_flags
;
1535 OBJECT_ATTRIBUTES attr
;
1537 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName
),
1538 debugstr_w(lpReplacementFileName
), debugstr_w(lpBackupFileName
),
1539 dwReplaceFlags
, lpExclude
, lpReserved
);
1542 FIXME("Ignoring flags %x\n", dwReplaceFlags
);
1544 /* First two arguments are mandatory */
1545 if (!lpReplacedFileName
|| !lpReplacementFileName
)
1547 SetLastError(ERROR_INVALID_PARAMETER
);
1551 unix_replaced_name
.Buffer
= NULL
;
1552 unix_replacement_name
.Buffer
= NULL
;
1553 unix_backup_name
.Buffer
= NULL
;
1555 attr
.Length
= sizeof(attr
);
1556 attr
.RootDirectory
= 0;
1557 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1558 attr
.ObjectName
= NULL
;
1559 attr
.SecurityDescriptor
= NULL
;
1560 attr
.SecurityQualityOfService
= NULL
;
1562 /* Open the "replaced" file for reading and writing */
1563 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName
, &nt_replaced_name
, NULL
, NULL
)))
1565 error
= ERROR_PATH_NOT_FOUND
;
1568 replaced_flags
= lpBackupFileName
? FILE_OPEN
: FILE_OPEN_IF
;
1569 attr
.ObjectName
= &nt_replaced_name
;
1570 status
= NtOpenFile(&hReplaced
, GENERIC_READ
|GENERIC_WRITE
|DELETE
|SYNCHRONIZE
,
1572 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
1573 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1574 if (status
== STATUS_SUCCESS
)
1575 status
= wine_nt_to_unix_file_name(&nt_replaced_name
, &unix_replaced_name
, replaced_flags
, FALSE
);
1576 RtlFreeUnicodeString(&nt_replaced_name
);
1577 if (status
!= STATUS_SUCCESS
)
1579 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1580 error
= ERROR_FILE_NOT_FOUND
;
1582 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1587 * Open the replacement file for reading, writing, and deleting
1588 * (writing and deleting are needed when finished)
1590 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName
, &nt_replacement_name
, NULL
, NULL
)))
1592 error
= ERROR_PATH_NOT_FOUND
;
1595 attr
.ObjectName
= &nt_replacement_name
;
1596 status
= NtOpenFile(&hReplacement
,
1597 GENERIC_READ
|GENERIC_WRITE
|DELETE
|WRITE_DAC
|SYNCHRONIZE
,
1599 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1600 if (status
== STATUS_SUCCESS
)
1601 status
= wine_nt_to_unix_file_name(&nt_replacement_name
, &unix_replacement_name
, FILE_OPEN
, FALSE
);
1602 RtlFreeUnicodeString(&nt_replacement_name
);
1603 if (status
!= STATUS_SUCCESS
)
1605 error
= RtlNtStatusToDosError(status
);
1609 /* If the user wants a backup then that needs to be performed first */
1610 if (lpBackupFileName
)
1612 UNICODE_STRING nt_backup_name
;
1613 FILE_BASIC_INFORMATION replaced_info
;
1615 /* Obtain the file attributes from the "replaced" file */
1616 status
= NtQueryInformationFile(hReplaced
, &io
, &replaced_info
,
1617 sizeof(replaced_info
),
1618 FileBasicInformation
);
1619 if (status
!= STATUS_SUCCESS
)
1621 error
= RtlNtStatusToDosError(status
);
1625 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName
, &nt_backup_name
, NULL
, NULL
)))
1627 error
= ERROR_PATH_NOT_FOUND
;
1630 attr
.ObjectName
= &nt_backup_name
;
1631 /* Open the backup with permissions to write over it */
1632 status
= NtCreateFile(&hBackup
, GENERIC_WRITE
,
1633 &attr
, &io
, NULL
, replaced_info
.FileAttributes
,
1634 FILE_SHARE_WRITE
, FILE_OPEN_IF
,
1635 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
,
1637 if (status
== STATUS_SUCCESS
)
1638 status
= wine_nt_to_unix_file_name(&nt_backup_name
, &unix_backup_name
, FILE_OPEN_IF
, FALSE
);
1639 RtlFreeUnicodeString(&nt_backup_name
);
1640 if (status
!= STATUS_SUCCESS
)
1642 error
= RtlNtStatusToDosError(status
);
1646 /* If an existing backup exists then copy over it */
1647 if (rename(unix_replaced_name
.Buffer
, unix_backup_name
.Buffer
) == -1)
1649 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
; /* is this correct? */
1655 * Now that the backup has been performed (if requested), copy the replacement
1658 if (rename(unix_replacement_name
.Buffer
, unix_replaced_name
.Buffer
) == -1)
1660 if (errno
== EACCES
)
1662 /* Inappropriate permissions on "replaced", rename will fail */
1663 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1666 /* on failure we need to indicate whether a backup was made */
1667 if (!lpBackupFileName
)
1668 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT
;
1670 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
;
1676 /* Perform resource cleanup */
1678 if (hBackup
) CloseHandle(hBackup
);
1679 if (hReplaced
) CloseHandle(hReplaced
);
1680 if (hReplacement
) CloseHandle(hReplacement
);
1681 RtlFreeAnsiString(&unix_backup_name
);
1682 RtlFreeAnsiString(&unix_replacement_name
);
1683 RtlFreeAnsiString(&unix_replaced_name
);
1685 /* If there was an error, set the error code */
1687 SetLastError(error
);
1692 /**************************************************************************
1693 * ReplaceFileA (KERNEL32.@)
1695 BOOL WINAPI
ReplaceFileA(LPCSTR lpReplacedFileName
,LPCSTR lpReplacementFileName
,
1696 LPCSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1697 LPVOID lpExclude
, LPVOID lpReserved
)
1699 WCHAR
*replacedW
, *replacementW
, *backupW
= NULL
;
1702 /* This function only makes sense when the first two parameters are defined */
1703 if (!lpReplacedFileName
|| !(replacedW
= FILE_name_AtoW( lpReplacedFileName
, TRUE
)))
1705 SetLastError(ERROR_INVALID_PARAMETER
);
1708 if (!lpReplacementFileName
|| !(replacementW
= FILE_name_AtoW( lpReplacementFileName
, TRUE
)))
1710 HeapFree( GetProcessHeap(), 0, replacedW
);
1711 SetLastError(ERROR_INVALID_PARAMETER
);
1714 /* The backup parameter, however, is optional */
1715 if (lpBackupFileName
)
1717 if (!(backupW
= FILE_name_AtoW( lpBackupFileName
, TRUE
)))
1719 HeapFree( GetProcessHeap(), 0, replacedW
);
1720 HeapFree( GetProcessHeap(), 0, replacementW
);
1721 SetLastError(ERROR_INVALID_PARAMETER
);
1725 ret
= ReplaceFileW( replacedW
, replacementW
, backupW
, dwReplaceFlags
, lpExclude
, lpReserved
);
1726 HeapFree( GetProcessHeap(), 0, replacedW
);
1727 HeapFree( GetProcessHeap(), 0, replacementW
);
1728 HeapFree( GetProcessHeap(), 0, backupW
);
1733 /*************************************************************************
1734 * FindFirstFileExW (KERNEL32.@)
1736 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1737 * results as FindExSearchNameMatch
1739 HANDLE WINAPI
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1740 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1741 LPVOID filter
, DWORD flags
)
1744 FIND_FIRST_INFO
*info
= NULL
;
1745 UNICODE_STRING nt_name
;
1746 OBJECT_ATTRIBUTES attr
;
1751 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1753 if ((search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1756 FIXME("options not implemented 0x%08x 0x%08x\n", search_op
, flags
);
1757 return INVALID_HANDLE_VALUE
;
1759 if (level
!= FindExInfoStandard
)
1761 FIXME("info level %d not implemented\n", level
);
1762 return INVALID_HANDLE_VALUE
;
1765 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1767 SetLastError( ERROR_PATH_NOT_FOUND
);
1768 return INVALID_HANDLE_VALUE
;
1771 if (!(info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
))))
1773 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1777 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1779 static const WCHAR dotW
[] = {'.',0};
1782 /* we still need to check that the directory can be opened */
1786 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1788 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1791 memcpy( dir
, filename
, HIWORD(device
) );
1792 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1794 RtlFreeUnicodeString( &nt_name
);
1795 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: dotW
, &nt_name
, &mask
, NULL
))
1797 HeapFree( GetProcessHeap(), 0, dir
);
1798 SetLastError( ERROR_PATH_NOT_FOUND
);
1801 HeapFree( GetProcessHeap(), 0, dir
);
1802 RtlInitUnicodeString( &info
->mask
, NULL
);
1804 else if (!mask
|| !*mask
)
1806 SetLastError( ERROR_FILE_NOT_FOUND
);
1811 if (!RtlCreateUnicodeString( &info
->mask
, mask
))
1813 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1817 /* truncate dir name before mask */
1819 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
1822 /* check if path is the root of the drive */
1823 info
->is_root
= FALSE
;
1824 p
= nt_name
.Buffer
+ 4; /* skip \??\ prefix */
1825 if (p
[0] && p
[1] == ':')
1828 while (*p
== '\\') p
++;
1829 info
->is_root
= (*p
== 0);
1832 attr
.Length
= sizeof(attr
);
1833 attr
.RootDirectory
= 0;
1834 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1835 attr
.ObjectName
= &nt_name
;
1836 attr
.SecurityDescriptor
= NULL
;
1837 attr
.SecurityQualityOfService
= NULL
;
1839 status
= NtOpenFile( &info
->handle
, GENERIC_READ
, &attr
, &io
,
1840 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1841 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1843 if (status
!= STATUS_SUCCESS
)
1845 RtlFreeUnicodeString( &info
->mask
);
1846 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1847 SetLastError( ERROR_PATH_NOT_FOUND
);
1849 SetLastError( RtlNtStatusToDosError(status
) );
1853 RtlInitializeCriticalSection( &info
->cs
);
1854 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
1855 info
->path
= nt_name
;
1856 info
->magic
= FIND_FIRST_MAGIC
;
1859 info
->search_op
= search_op
;
1863 WIN32_FIND_DATAW
*wfd
= data
;
1865 memset( wfd
, 0, sizeof(*wfd
) );
1866 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
1867 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1868 CloseHandle( info
->handle
);
1875 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1876 FileBothDirectoryInformation
, FALSE
, &info
->mask
, TRUE
);
1880 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1881 return INVALID_HANDLE_VALUE
;
1883 info
->data_len
= io
.Information
;
1884 if (!FindNextFileW( info
, data
))
1886 TRACE( "%s not found\n", debugstr_w(filename
) );
1888 SetLastError( ERROR_FILE_NOT_FOUND
);
1889 return INVALID_HANDLE_VALUE
;
1891 if (!strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1893 /* we can't find two files with the same name */
1894 CloseHandle( info
->handle
);
1901 HeapFree( GetProcessHeap(), 0, info
);
1902 RtlFreeUnicodeString( &nt_name
);
1903 return INVALID_HANDLE_VALUE
;
1907 /*************************************************************************
1908 * FindNextFileW (KERNEL32.@)
1910 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1912 FIND_FIRST_INFO
*info
;
1913 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1916 TRACE("%p %p\n", handle
, data
);
1918 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1920 SetLastError( ERROR_INVALID_HANDLE
);
1924 if (info
->magic
!= FIND_FIRST_MAGIC
)
1926 SetLastError( ERROR_INVALID_HANDLE
);
1930 RtlEnterCriticalSection( &info
->cs
);
1932 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
1935 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
1939 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1940 FileBothDirectoryInformation
, FALSE
, &info
->mask
, FALSE
);
1943 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1944 if (io
.u
.Status
== STATUS_NO_MORE_FILES
)
1946 CloseHandle( info
->handle
);
1951 info
->data_len
= io
.Information
;
1955 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
1957 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
1958 else info
->data_pos
= info
->data_len
;
1960 /* don't return '.' and '..' in the root of the drive */
1963 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
1964 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
1965 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
1968 /* check for dir symlink */
1969 if ((dir_info
->FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
1970 (dir_info
->FileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
) &&
1971 strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1973 if (!check_dir_symlink( info
, dir_info
)) continue;
1976 data
->dwFileAttributes
= dir_info
->FileAttributes
;
1977 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
1978 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
1979 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
1980 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
1981 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
1982 data
->dwReserved0
= 0;
1983 data
->dwReserved1
= 0;
1985 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
1986 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
1987 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
1988 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
1990 TRACE("returning %s (%s)\n",
1991 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
1997 RtlLeaveCriticalSection( &info
->cs
);
2002 /*************************************************************************
2003 * FindClose (KERNEL32.@)
2005 BOOL WINAPI
FindClose( HANDLE handle
)
2007 FIND_FIRST_INFO
*info
= handle
;
2009 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
2011 SetLastError( ERROR_INVALID_HANDLE
);
2017 if (info
->magic
== FIND_FIRST_MAGIC
)
2019 RtlEnterCriticalSection( &info
->cs
);
2020 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
2023 if (info
->handle
) CloseHandle( info
->handle
);
2025 RtlFreeUnicodeString( &info
->mask
);
2026 info
->mask
.Buffer
= NULL
;
2027 RtlFreeUnicodeString( &info
->path
);
2030 RtlLeaveCriticalSection( &info
->cs
);
2031 info
->cs
.DebugInfo
->Spare
[0] = 0;
2032 RtlDeleteCriticalSection( &info
->cs
);
2033 HeapFree( GetProcessHeap(), 0, info
);
2039 WARN("Illegal handle %p\n", handle
);
2040 SetLastError( ERROR_INVALID_HANDLE
);
2049 /*************************************************************************
2050 * FindFirstFileA (KERNEL32.@)
2052 HANDLE WINAPI
FindFirstFileA( LPCSTR lpFileName
, WIN32_FIND_DATAA
*lpFindData
)
2054 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
2055 FindExSearchNameMatch
, NULL
, 0);
2058 /*************************************************************************
2059 * FindFirstFileExA (KERNEL32.@)
2061 HANDLE WINAPI
FindFirstFileExA( LPCSTR lpFileName
, FINDEX_INFO_LEVELS fInfoLevelId
,
2062 LPVOID lpFindFileData
, FINDEX_SEARCH_OPS fSearchOp
,
2063 LPVOID lpSearchFilter
, DWORD dwAdditionalFlags
)
2066 WIN32_FIND_DATAA
*dataA
;
2067 WIN32_FIND_DATAW dataW
;
2070 if (!(nameW
= FILE_name_AtoW( lpFileName
, FALSE
))) return INVALID_HANDLE_VALUE
;
2072 handle
= FindFirstFileExW(nameW
, fInfoLevelId
, &dataW
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
2073 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
2075 dataA
= lpFindFileData
;
2076 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
2077 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
2078 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2079 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2080 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2081 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
2082 FILE_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
2083 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
2084 sizeof(dataA
->cAlternateFileName
) );
2089 /*************************************************************************
2090 * FindFirstFileW (KERNEL32.@)
2092 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
2094 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
2095 FindExSearchNameMatch
, NULL
, 0);
2099 /*************************************************************************
2100 * FindNextFileA (KERNEL32.@)
2102 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
2104 WIN32_FIND_DATAW dataW
;
2106 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
2107 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
2108 data
->ftCreationTime
= dataW
.ftCreationTime
;
2109 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2110 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2111 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2112 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
2113 FILE_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
2114 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
2115 sizeof(data
->cAlternateFileName
) );
2120 /**************************************************************************
2121 * GetFileAttributesW (KERNEL32.@)
2123 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
2125 FILE_BASIC_INFORMATION info
;
2126 UNICODE_STRING nt_name
;
2127 OBJECT_ATTRIBUTES attr
;
2130 TRACE("%s\n", debugstr_w(name
));
2132 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2134 SetLastError( ERROR_PATH_NOT_FOUND
);
2135 return INVALID_FILE_ATTRIBUTES
;
2138 attr
.Length
= sizeof(attr
);
2139 attr
.RootDirectory
= 0;
2140 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2141 attr
.ObjectName
= &nt_name
;
2142 attr
.SecurityDescriptor
= NULL
;
2143 attr
.SecurityQualityOfService
= NULL
;
2145 status
= NtQueryAttributesFile( &attr
, &info
);
2146 RtlFreeUnicodeString( &nt_name
);
2148 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
2150 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2151 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
2153 SetLastError( RtlNtStatusToDosError(status
) );
2154 return INVALID_FILE_ATTRIBUTES
;
2158 /**************************************************************************
2159 * GetFileAttributesA (KERNEL32.@)
2161 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
2165 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
2166 return GetFileAttributesW( nameW
);
2170 /**************************************************************************
2171 * SetFileAttributesW (KERNEL32.@)
2173 BOOL WINAPI
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
2175 UNICODE_STRING nt_name
;
2176 OBJECT_ATTRIBUTES attr
;
2181 TRACE("%s %x\n", debugstr_w(name
), attributes
);
2183 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2185 SetLastError( ERROR_PATH_NOT_FOUND
);
2189 attr
.Length
= sizeof(attr
);
2190 attr
.RootDirectory
= 0;
2191 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2192 attr
.ObjectName
= &nt_name
;
2193 attr
.SecurityDescriptor
= NULL
;
2194 attr
.SecurityQualityOfService
= NULL
;
2196 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2197 RtlFreeUnicodeString( &nt_name
);
2199 if (status
== STATUS_SUCCESS
)
2201 FILE_BASIC_INFORMATION info
;
2203 memset( &info
, 0, sizeof(info
) );
2204 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2205 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2209 if (status
== STATUS_SUCCESS
) return TRUE
;
2210 SetLastError( RtlNtStatusToDosError(status
) );
2215 /**************************************************************************
2216 * SetFileAttributesA (KERNEL32.@)
2218 BOOL WINAPI
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2222 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2223 return SetFileAttributesW( nameW
, attributes
);
2227 /**************************************************************************
2228 * GetFileAttributesExW (KERNEL32.@)
2230 BOOL WINAPI
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2232 FILE_NETWORK_OPEN_INFORMATION info
;
2233 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
2234 UNICODE_STRING nt_name
;
2235 OBJECT_ATTRIBUTES attr
;
2238 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
2240 if (level
!= GetFileExInfoStandard
)
2242 SetLastError( ERROR_INVALID_PARAMETER
);
2246 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2248 SetLastError( ERROR_PATH_NOT_FOUND
);
2252 attr
.Length
= sizeof(attr
);
2253 attr
.RootDirectory
= 0;
2254 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2255 attr
.ObjectName
= &nt_name
;
2256 attr
.SecurityDescriptor
= NULL
;
2257 attr
.SecurityQualityOfService
= NULL
;
2259 status
= NtQueryFullAttributesFile( &attr
, &info
);
2260 RtlFreeUnicodeString( &nt_name
);
2262 if (status
!= STATUS_SUCCESS
)
2264 SetLastError( RtlNtStatusToDosError(status
) );
2268 data
->dwFileAttributes
= info
.FileAttributes
;
2269 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
2270 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
2271 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
2272 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
2273 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
2274 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
2275 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
2276 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
2281 /**************************************************************************
2282 * GetFileAttributesExA (KERNEL32.@)
2284 BOOL WINAPI
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2288 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2289 return GetFileAttributesExW( nameW
, level
, ptr
);
2293 /******************************************************************************
2294 * GetCompressedFileSizeW (KERNEL32.@)
2296 * Get the actual number of bytes used on disk.
2299 * Success: Low-order doubleword of number of bytes
2300 * Failure: INVALID_FILE_SIZE
2302 DWORD WINAPI
GetCompressedFileSizeW(
2303 LPCWSTR name
, /* [in] Pointer to name of file */
2304 LPDWORD size_high
) /* [out] Receives high-order doubleword of size */
2306 UNICODE_STRING nt_name
;
2307 OBJECT_ATTRIBUTES attr
;
2311 DWORD ret
= INVALID_FILE_SIZE
;
2313 TRACE("%s %p\n", debugstr_w(name
), size_high
);
2315 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2317 SetLastError( ERROR_PATH_NOT_FOUND
);
2318 return INVALID_FILE_SIZE
;
2321 attr
.Length
= sizeof(attr
);
2322 attr
.RootDirectory
= 0;
2323 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2324 attr
.ObjectName
= &nt_name
;
2325 attr
.SecurityDescriptor
= NULL
;
2326 attr
.SecurityQualityOfService
= NULL
;
2328 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2329 RtlFreeUnicodeString( &nt_name
);
2331 if (status
== STATUS_SUCCESS
)
2333 /* we don't support compressed files, simply return the file size */
2334 ret
= GetFileSize( handle
, size_high
);
2337 else SetLastError( RtlNtStatusToDosError(status
) );
2343 /******************************************************************************
2344 * GetCompressedFileSizeA (KERNEL32.@)
2346 * See GetCompressedFileSizeW.
2348 DWORD WINAPI
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
2352 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
2353 return GetCompressedFileSizeW( nameW
, size_high
);
2357 /***********************************************************************
2358 * OpenVxDHandle (KERNEL32.@)
2360 * This function is supposed to return the corresponding Ring 0
2361 * ("kernel") handle for a Ring 3 handle in Win9x.
2362 * Evidently, Wine will have problems with this. But we try anyway,
2365 HANDLE WINAPI
OpenVxDHandle(HANDLE hHandleRing3
)
2367 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3
);
2368 return hHandleRing3
;
2372 /****************************************************************************
2373 * DeviceIoControl (KERNEL32.@)
2375 BOOL WINAPI
DeviceIoControl(HANDLE hDevice
, DWORD dwIoControlCode
,
2376 LPVOID lpvInBuffer
, DWORD cbInBuffer
,
2377 LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
2378 LPDWORD lpcbBytesReturned
,
2379 LPOVERLAPPED lpOverlapped
)
2383 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2384 hDevice
,dwIoControlCode
,lpvInBuffer
,cbInBuffer
,
2385 lpvOutBuffer
,cbOutBuffer
,lpcbBytesReturned
,lpOverlapped
);
2387 /* Check if this is a user defined control code for a VxD */
2389 if (HIWORD( dwIoControlCode
) == 0 && (GetVersion() & 0x80000000))
2391 typedef BOOL (WINAPI
*DeviceIoProc
)(DWORD
, LPVOID
, DWORD
, LPVOID
, DWORD
, LPDWORD
, LPOVERLAPPED
);
2392 static DeviceIoProc (*vxd_get_proc
)(HANDLE
);
2393 DeviceIoProc proc
= NULL
;
2395 if (!vxd_get_proc
) vxd_get_proc
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2396 "__wine_vxd_get_proc" );
2397 if (vxd_get_proc
) proc
= vxd_get_proc( hDevice
);
2398 if (proc
) return proc( dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2399 lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
);
2402 /* Not a VxD, let ntdll handle it */
2406 LPVOID cvalue
= ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) ? NULL
: lpOverlapped
;
2407 lpOverlapped
->Internal
= STATUS_PENDING
;
2408 lpOverlapped
->InternalHigh
= 0;
2409 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2410 status
= NtFsControlFile(hDevice
, lpOverlapped
->hEvent
,
2411 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2412 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2413 lpvOutBuffer
, cbOutBuffer
);
2415 status
= NtDeviceIoControlFile(hDevice
, lpOverlapped
->hEvent
,
2416 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2417 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2418 lpvOutBuffer
, cbOutBuffer
);
2419 if (lpcbBytesReturned
) *lpcbBytesReturned
= lpOverlapped
->InternalHigh
;
2423 IO_STATUS_BLOCK iosb
;
2425 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2426 status
= NtFsControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2427 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2428 lpvOutBuffer
, cbOutBuffer
);
2430 status
= NtDeviceIoControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2431 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2432 lpvOutBuffer
, cbOutBuffer
);
2433 if (lpcbBytesReturned
) *lpcbBytesReturned
= iosb
.Information
;
2435 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2440 /***********************************************************************
2441 * OpenFile (KERNEL32.@)
2443 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
2447 WORD filedatetime
[2];
2449 if (!ofs
) return HFILE_ERROR
;
2451 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
2452 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
2453 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
2454 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
2455 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
2456 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
2457 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
2458 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
2459 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
2460 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
2461 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
2462 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
2463 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
2464 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
2465 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
2466 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
2467 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
2468 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
2472 ofs
->cBytes
= sizeof(OFSTRUCT
);
2474 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
2476 if (!name
) return HFILE_ERROR
;
2478 TRACE("%s %04x\n", name
, mode
);
2480 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2481 Are there any cases where getting the path here is wrong?
2482 Uwe Bonnes 1997 Apr 2 */
2483 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
)) goto error
;
2485 /* OF_PARSE simply fills the structure */
2487 if (mode
& OF_PARSE
)
2489 ofs
->fFixedDisk
= (GetDriveTypeA( ofs
->szPathName
) != DRIVE_REMOVABLE
);
2490 TRACE("(%s): OF_PARSE, res = '%s'\n", name
, ofs
->szPathName
);
2494 /* OF_CREATE is completely different from all other options, so
2497 if (mode
& OF_CREATE
)
2499 if ((handle
= create_file_OF( name
, mode
)) == INVALID_HANDLE_VALUE
)
2504 /* Now look for the file */
2506 if (!SearchPathA( NULL
, name
, NULL
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
))
2509 TRACE("found %s\n", debugstr_a(ofs
->szPathName
) );
2511 if (mode
& OF_DELETE
)
2513 if (!DeleteFileA( ofs
->szPathName
)) goto error
;
2514 TRACE("(%s): OF_DELETE return = OK\n", name
);
2518 handle
= LongToHandle(_lopen( ofs
->szPathName
, mode
));
2519 if (handle
== INVALID_HANDLE_VALUE
) goto error
;
2521 GetFileTime( handle
, NULL
, NULL
, &filetime
);
2522 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
2523 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
2525 if (ofs
->Reserved1
!= filedatetime
[0] || ofs
->Reserved2
!= filedatetime
[1] )
2527 CloseHandle( handle
);
2528 WARN("(%s): OF_VERIFY failed\n", name
);
2529 /* FIXME: what error here? */
2530 SetLastError( ERROR_FILE_NOT_FOUND
);
2534 ofs
->Reserved1
= filedatetime
[0];
2535 ofs
->Reserved2
= filedatetime
[1];
2537 TRACE("(%s): OK, return = %p\n", name
, handle
);
2538 if (mode
& OF_EXIST
) /* Return TRUE instead of a handle */
2540 CloseHandle( handle
);
2543 return HandleToLong(handle
);
2545 error
: /* We get here if there was an error opening the file */
2546 ofs
->nErrCode
= GetLastError();
2547 WARN("(%s): return = HFILE_ERROR error= %d\n", name
,ofs
->nErrCode
);
2551 /***********************************************************************
2552 * K32EnumDeviceDrivers (KERNEL32.@)
2554 BOOL WINAPI
K32EnumDeviceDrivers(void **image_base
, DWORD cb
, DWORD
*needed
)
2556 FIXME("(%p, %d, %p): stub\n", image_base
, cb
, needed
);
2564 /***********************************************************************
2565 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2567 DWORD WINAPI
K32GetDeviceDriverBaseNameA(void *image_base
, LPSTR base_name
, DWORD size
)
2569 FIXME("(%p, %p, %d): stub\n", image_base
, base_name
, size
);
2571 if (base_name
&& size
)
2572 base_name
[0] = '\0';
2577 /***********************************************************************
2578 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2580 DWORD WINAPI
K32GetDeviceDriverBaseNameW(void *image_base
, LPWSTR base_name
, DWORD size
)
2582 FIXME("(%p, %p, %d): stub\n", image_base
, base_name
, size
);
2584 if (base_name
&& size
)
2585 base_name
[0] = '\0';
2590 /***********************************************************************
2591 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2593 DWORD WINAPI
K32GetDeviceDriverFileNameA(void *image_base
, LPSTR file_name
, DWORD size
)
2595 FIXME("(%p, %p, %d): stub\n", image_base
, file_name
, size
);
2597 if (file_name
&& size
)
2598 file_name
[0] = '\0';
2603 /***********************************************************************
2604 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2606 DWORD WINAPI
K32GetDeviceDriverFileNameW(void *image_base
, LPWSTR file_name
, DWORD size
)
2608 FIXME("(%p, %p, %d): stub\n", image_base
, file_name
, size
);
2610 if (file_name
&& size
)
2611 file_name
[0] = '\0';