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
);
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
) SetLastError( RtlNtStatusToDosError(status
) );
492 /***********************************************************************
493 * WriteFileGather (KERNEL32.@)
495 BOOL WINAPI
WriteFileGather( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
496 LPDWORD reserved
, LPOVERLAPPED overlapped
)
498 PIO_STATUS_BLOCK io_status
;
499 LARGE_INTEGER offset
;
502 TRACE( "%p %p %u %p\n", file
, segments
, count
, overlapped
);
504 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
505 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
506 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
507 io_status
->u
.Status
= STATUS_PENDING
;
508 io_status
->Information
= 0;
510 status
= NtWriteFileGather( file
, NULL
, NULL
, NULL
, io_status
, segments
, count
, &offset
, NULL
);
511 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
516 /***********************************************************************
517 * WriteFile (KERNEL32.@)
519 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
520 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
522 HANDLE hEvent
= NULL
;
523 LARGE_INTEGER offset
;
524 PLARGE_INTEGER poffset
= NULL
;
526 IO_STATUS_BLOCK iosb
;
527 PIO_STATUS_BLOCK piosb
= &iosb
;
528 LPVOID cvalue
= NULL
;
530 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, bytesWritten
, overlapped
);
532 if (is_console_handle(hFile
))
533 return WriteConsoleA(hFile
, buffer
, bytesToWrite
, bytesWritten
, NULL
);
537 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
538 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
540 hEvent
= overlapped
->hEvent
;
541 piosb
= (PIO_STATUS_BLOCK
)overlapped
;
542 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
544 piosb
->u
.Status
= STATUS_PENDING
;
545 piosb
->Information
= 0;
547 status
= NtWriteFile(hFile
, hEvent
, NULL
, cvalue
, piosb
,
548 buffer
, bytesToWrite
, poffset
, NULL
);
550 if (status
== STATUS_PENDING
&& !overlapped
)
552 WaitForSingleObject( hFile
, INFINITE
);
553 status
= piosb
->u
.Status
;
556 if (status
!= STATUS_PENDING
&& bytesWritten
)
557 *bytesWritten
= piosb
->Information
;
559 if (status
&& status
!= STATUS_TIMEOUT
)
561 SetLastError( RtlNtStatusToDosError(status
) );
568 /***********************************************************************
569 * GetOverlappedResult (KERNEL32.@)
571 * Check the result of an Asynchronous data transfer from a file.
574 * HANDLE hFile [in] handle of file to check on
575 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
576 * LPDWORD lpTransferred [in/out] number of bytes transferred
577 * BOOL bWait [in] wait for the transfer to complete ?
583 * If successful (and relevant) lpTransferred will hold the number of
584 * bytes transferred during the async operation.
586 BOOL WINAPI
GetOverlappedResult(HANDLE hFile
, LPOVERLAPPED lpOverlapped
,
587 LPDWORD lpTransferred
, BOOL bWait
)
591 TRACE( "(%p %p %p %x)\n", hFile
, lpOverlapped
, lpTransferred
, bWait
);
593 status
= lpOverlapped
->Internal
;
594 if (status
== STATUS_PENDING
)
598 SetLastError( ERROR_IO_INCOMPLETE
);
602 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: hFile
,
603 INFINITE
) == WAIT_FAILED
)
605 status
= lpOverlapped
->Internal
;
608 *lpTransferred
= lpOverlapped
->InternalHigh
;
610 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
614 /***********************************************************************
615 * CancelIoEx (KERNEL32.@)
617 * Cancels pending I/O operations on a file given the overlapped used.
620 * handle [I] File handle.
621 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
625 * Failure: FALSE, check GetLastError().
627 BOOL WINAPI
CancelIoEx(HANDLE handle
, LPOVERLAPPED lpOverlapped
)
629 IO_STATUS_BLOCK io_status
;
631 NtCancelIoFileEx(handle
, (PIO_STATUS_BLOCK
) lpOverlapped
, &io_status
);
632 if (io_status
.u
.Status
)
634 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
640 /***********************************************************************
641 * CancelIo (KERNEL32.@)
643 * Cancels pending I/O operations initiated by the current thread on a file.
646 * handle [I] File handle.
650 * Failure: FALSE, check GetLastError().
652 BOOL WINAPI
CancelIo(HANDLE handle
)
654 IO_STATUS_BLOCK io_status
;
656 NtCancelIoFile(handle
, &io_status
);
657 if (io_status
.u
.Status
)
659 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
665 /***********************************************************************
666 * _hread (KERNEL32.@)
668 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
670 return _lread( hFile
, buffer
, count
);
674 /***********************************************************************
675 * _hwrite (KERNEL32.@)
677 * experimentation yields that _lwrite:
678 * o truncates the file at the current position with
680 * o returns 0 on a 0 length write
681 * o works with console handles
684 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
688 TRACE("%d %p %d\n", handle
, buffer
, count
);
692 /* Expand or truncate at current position */
693 if (!SetEndOfFile( LongToHandle(handle
) )) return HFILE_ERROR
;
696 if (!WriteFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
702 /***********************************************************************
703 * _lclose (KERNEL32.@)
705 HFILE WINAPI
_lclose( HFILE hFile
)
707 TRACE("handle %d\n", hFile
);
708 return CloseHandle( LongToHandle(hFile
) ) ? 0 : HFILE_ERROR
;
712 /***********************************************************************
713 * _lcreat (KERNEL32.@)
715 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
719 /* Mask off all flags not explicitly allowed by the doc */
720 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
721 TRACE("%s %02x\n", path
, attr
);
722 hfile
= CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
723 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
724 CREATE_ALWAYS
, attr
, 0 );
725 return HandleToLong(hfile
);
729 /***********************************************************************
730 * _lopen (KERNEL32.@)
732 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
736 TRACE("(%s,%04x)\n", debugstr_a(path
), mode
);
737 hfile
= create_file_OF( path
, mode
& ~OF_CREATE
);
738 return HandleToLong(hfile
);
741 /***********************************************************************
742 * _lread (KERNEL32.@)
744 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
747 if (!ReadFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
753 /***********************************************************************
754 * _llseek (KERNEL32.@)
756 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
758 return SetFilePointer( LongToHandle(hFile
), lOffset
, NULL
, nOrigin
);
762 /***********************************************************************
763 * _lwrite (KERNEL32.@)
765 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
767 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
771 /***********************************************************************
772 * FlushFileBuffers (KERNEL32.@)
774 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
777 IO_STATUS_BLOCK ioblk
;
779 if (is_console_handle( hFile
))
781 /* this will fail (as expected) for an output handle */
782 return FlushConsoleInputBuffer( hFile
);
784 nts
= NtFlushBuffersFile( hFile
, &ioblk
);
785 if (nts
!= STATUS_SUCCESS
)
787 SetLastError( RtlNtStatusToDosError( nts
) );
795 /***********************************************************************
796 * GetFileType (KERNEL32.@)
798 DWORD WINAPI
GetFileType( HANDLE hFile
)
800 FILE_FS_DEVICE_INFORMATION info
;
804 if (is_console_handle( hFile
)) return FILE_TYPE_CHAR
;
806 status
= NtQueryVolumeInformationFile( hFile
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
807 if (status
!= STATUS_SUCCESS
)
809 SetLastError( RtlNtStatusToDosError(status
) );
810 return FILE_TYPE_UNKNOWN
;
813 switch(info
.DeviceType
)
815 case FILE_DEVICE_NULL
:
816 case FILE_DEVICE_SERIAL_PORT
:
817 case FILE_DEVICE_PARALLEL_PORT
:
818 case FILE_DEVICE_TAPE
:
819 case FILE_DEVICE_UNKNOWN
:
820 return FILE_TYPE_CHAR
;
821 case FILE_DEVICE_NAMED_PIPE
:
822 return FILE_TYPE_PIPE
;
824 return FILE_TYPE_DISK
;
829 /***********************************************************************
830 * GetFileInformationByHandle (KERNEL32.@)
832 BOOL WINAPI
GetFileInformationByHandle( HANDLE hFile
, BY_HANDLE_FILE_INFORMATION
*info
)
834 FILE_ALL_INFORMATION all_info
;
838 status
= NtQueryInformationFile( hFile
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
839 if (status
== STATUS_BUFFER_OVERFLOW
) status
= STATUS_SUCCESS
;
840 if (status
== STATUS_SUCCESS
)
842 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
843 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
844 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
845 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
846 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
847 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
848 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
849 info
->dwVolumeSerialNumber
= 0; /* FIXME */
850 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
851 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
852 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
853 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
854 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
857 SetLastError( RtlNtStatusToDosError(status
) );
862 /***********************************************************************
863 * GetFileSize (KERNEL32.@)
865 * Retrieve the size of a file.
868 * hFile [I] File to retrieve size of.
869 * filesizehigh [O] On return, the high bits of the file size.
872 * Success: The low bits of the file size.
873 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
874 * check GetLastError() for values other than ERROR_SUCCESS.
876 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
879 if (!GetFileSizeEx( hFile
, &size
)) return INVALID_FILE_SIZE
;
880 if (filesizehigh
) *filesizehigh
= size
.u
.HighPart
;
881 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError(0);
882 return size
.u
.LowPart
;
886 /***********************************************************************
887 * GetFileSizeEx (KERNEL32.@)
889 * Retrieve the size of a file.
892 * hFile [I] File to retrieve size of.
893 * lpFileSIze [O] On return, the size of the file.
897 * Failure: FALSE, check GetLastError().
899 BOOL WINAPI
GetFileSizeEx( HANDLE hFile
, PLARGE_INTEGER lpFileSize
)
901 FILE_STANDARD_INFORMATION info
;
905 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileStandardInformation
);
906 if (status
== STATUS_SUCCESS
)
908 *lpFileSize
= info
.EndOfFile
;
911 SetLastError( RtlNtStatusToDosError(status
) );
916 /**************************************************************************
917 * SetEndOfFile (KERNEL32.@)
919 * Sets the current position as the end of the file.
922 * hFile [I] File handle.
926 * Failure: FALSE, check GetLastError().
928 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
930 FILE_POSITION_INFORMATION pos
;
931 FILE_END_OF_FILE_INFORMATION eof
;
935 status
= NtQueryInformationFile( hFile
, &io
, &pos
, sizeof(pos
), FilePositionInformation
);
936 if (status
== STATUS_SUCCESS
)
938 eof
.EndOfFile
= pos
.CurrentByteOffset
;
939 status
= NtSetInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
941 if (status
== STATUS_SUCCESS
) return TRUE
;
942 SetLastError( RtlNtStatusToDosError(status
) );
947 /***********************************************************************
948 * SetFilePointer (KERNEL32.@)
950 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
, DWORD method
)
952 LARGE_INTEGER dist
, newpos
;
956 dist
.u
.LowPart
= distance
;
957 dist
.u
.HighPart
= *highword
;
959 else dist
.QuadPart
= distance
;
961 if (!SetFilePointerEx( hFile
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
963 if (highword
) *highword
= newpos
.u
.HighPart
;
964 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
965 return newpos
.u
.LowPart
;
969 /***********************************************************************
970 * SetFilePointerEx (KERNEL32.@)
972 BOOL WINAPI
SetFilePointerEx( HANDLE hFile
, LARGE_INTEGER distance
,
973 LARGE_INTEGER
*newpos
, DWORD method
)
977 FILE_POSITION_INFORMATION info
;
982 pos
= distance
.QuadPart
;
985 if (NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
987 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
991 FILE_END_OF_FILE_INFORMATION eof
;
992 if (NtQueryInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
994 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
998 SetLastError( ERROR_INVALID_PARAMETER
);
1004 SetLastError( ERROR_NEGATIVE_SEEK
);
1008 info
.CurrentByteOffset
.QuadPart
= pos
;
1009 if (NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
1011 if (newpos
) newpos
->QuadPart
= pos
;
1015 SetLastError( RtlNtStatusToDosError(io
.u
.Status
) );
1019 /***********************************************************************
1020 * SetFileValidData (KERNEL32.@)
1022 BOOL WINAPI
SetFileValidData( HANDLE hFile
, LONGLONG ValidDataLength
)
1024 FIXME("stub: %p, %s\n", hFile
, wine_dbgstr_longlong(ValidDataLength
));
1025 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1029 /***********************************************************************
1030 * GetFileTime (KERNEL32.@)
1032 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
1033 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
1035 FILE_BASIC_INFORMATION info
;
1039 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1040 if (status
== STATUS_SUCCESS
)
1044 lpCreationTime
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1045 lpCreationTime
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1047 if (lpLastAccessTime
)
1049 lpLastAccessTime
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1050 lpLastAccessTime
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1052 if (lpLastWriteTime
)
1054 lpLastWriteTime
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1055 lpLastWriteTime
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1059 SetLastError( RtlNtStatusToDosError(status
) );
1064 /***********************************************************************
1065 * SetFileTime (KERNEL32.@)
1067 BOOL WINAPI
SetFileTime( HANDLE hFile
, const FILETIME
*ctime
,
1068 const FILETIME
*atime
, const FILETIME
*mtime
)
1070 FILE_BASIC_INFORMATION info
;
1074 memset( &info
, 0, sizeof(info
) );
1077 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
1078 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
1082 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
1083 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
1087 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
1088 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
1091 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1092 if (status
== STATUS_SUCCESS
) return TRUE
;
1093 SetLastError( RtlNtStatusToDosError(status
) );
1098 /**************************************************************************
1099 * LockFile (KERNEL32.@)
1101 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1102 DWORD count_low
, DWORD count_high
)
1105 LARGE_INTEGER count
, offset
;
1107 TRACE( "%p %x%08x %x%08x\n",
1108 hFile
, offset_high
, offset_low
, count_high
, count_low
);
1110 count
.u
.LowPart
= count_low
;
1111 count
.u
.HighPart
= count_high
;
1112 offset
.u
.LowPart
= offset_low
;
1113 offset
.u
.HighPart
= offset_high
;
1115 status
= NtLockFile( hFile
, 0, NULL
, NULL
,
1116 NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
);
1118 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1123 /**************************************************************************
1124 * LockFileEx [KERNEL32.@]
1126 * Locks a byte range within an open file for shared or exclusive access.
1133 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1135 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1136 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
1139 LARGE_INTEGER count
, offset
;
1140 LPVOID cvalue
= NULL
;
1144 SetLastError( ERROR_INVALID_PARAMETER
);
1148 TRACE( "%p %x%08x %x%08x flags %x\n",
1149 hFile
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
,
1150 count_high
, count_low
, flags
);
1152 count
.u
.LowPart
= count_low
;
1153 count
.u
.HighPart
= count_high
;
1154 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
1155 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
1157 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1159 status
= NtLockFile( hFile
, overlapped
->hEvent
, NULL
, cvalue
,
1160 NULL
, &offset
, &count
, NULL
,
1161 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
1162 flags
& LOCKFILE_EXCLUSIVE_LOCK
);
1164 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1169 /**************************************************************************
1170 * UnlockFile (KERNEL32.@)
1172 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1173 DWORD count_low
, DWORD count_high
)
1176 LARGE_INTEGER count
, offset
;
1178 count
.u
.LowPart
= count_low
;
1179 count
.u
.HighPart
= count_high
;
1180 offset
.u
.LowPart
= offset_low
;
1181 offset
.u
.HighPart
= offset_high
;
1183 status
= NtUnlockFile( hFile
, NULL
, &offset
, &count
, NULL
);
1184 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1189 /**************************************************************************
1190 * UnlockFileEx (KERNEL32.@)
1192 BOOL WINAPI
UnlockFileEx( HANDLE hFile
, DWORD reserved
, DWORD count_low
, DWORD count_high
,
1193 LPOVERLAPPED overlapped
)
1197 SetLastError( ERROR_INVALID_PARAMETER
);
1200 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
1202 return UnlockFile( hFile
, overlapped
->u
.s
.Offset
, overlapped
->u
.s
.OffsetHigh
, count_low
, count_high
);
1206 /*************************************************************************
1207 * SetHandleCount (KERNEL32.@)
1209 UINT WINAPI
SetHandleCount( UINT count
)
1215 /**************************************************************************
1216 * Operations on file names *
1217 **************************************************************************/
1220 /*************************************************************************
1221 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1223 * Creates or opens an object, and returns a handle that can be used to
1224 * access that object.
1228 * filename [in] pointer to filename to be accessed
1229 * access [in] access mode requested
1230 * sharing [in] share mode
1231 * sa [in] pointer to security attributes
1232 * creation [in] how to create the file
1233 * attributes [in] attributes for newly created file
1234 * template [in] handle to file with extended attributes to copy
1237 * Success: Open handle to specified file
1238 * Failure: INVALID_HANDLE_VALUE
1240 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
1241 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1242 DWORD attributes
, HANDLE
template )
1246 OBJECT_ATTRIBUTES attr
;
1247 UNICODE_STRING nameW
;
1251 const WCHAR
*vxd_name
= NULL
;
1252 static const WCHAR bkslashes_with_dotW
[] = {'\\','\\','.','\\',0};
1253 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
1254 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
1255 SECURITY_QUALITY_OF_SERVICE qos
;
1257 static const UINT nt_disposition
[5] =
1259 FILE_CREATE
, /* CREATE_NEW */
1260 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
1261 FILE_OPEN
, /* OPEN_EXISTING */
1262 FILE_OPEN_IF
, /* OPEN_ALWAYS */
1263 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
1269 if (!filename
|| !filename
[0])
1271 SetLastError( ERROR_PATH_NOT_FOUND
);
1272 return INVALID_HANDLE_VALUE
;
1275 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
1276 (access
& GENERIC_READ
)?"GENERIC_READ ":"",
1277 (access
& GENERIC_WRITE
)?"GENERIC_WRITE ":"",
1278 (access
& GENERIC_EXECUTE
)?"GENERIC_EXECUTE ":"",
1279 (!access
)?"QUERY_ACCESS ":"",
1280 (sharing
& FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
1281 (sharing
& FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
1282 (sharing
& FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
1283 creation
, attributes
);
1285 /* Open a console for CONIN$ or CONOUT$ */
1287 if (!strcmpiW(filename
, coninW
) || !strcmpiW(filename
, conoutW
))
1289 ret
= OpenConsoleW(filename
, access
, (sa
&& sa
->bInheritHandle
),
1290 creation
? OPEN_EXISTING
: 0);
1291 if (ret
== INVALID_HANDLE_VALUE
) SetLastError(ERROR_INVALID_PARAMETER
);
1295 if (!strncmpW(filename
, bkslashes_with_dotW
, 4))
1297 static const WCHAR pipeW
[] = {'P','I','P','E','\\',0};
1298 static const WCHAR mailslotW
[] = {'M','A','I','L','S','L','O','T','\\',0};
1300 if ((isalphaW(filename
[4]) && filename
[5] == ':' && filename
[6] == '\0') ||
1301 !strncmpiW( filename
+ 4, pipeW
, 5 ) ||
1302 !strncmpiW( filename
+ 4, mailslotW
, 9 ))
1306 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
1308 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
1310 else if (GetVersion() & 0x80000000)
1312 vxd_name
= filename
+ 4;
1313 if (!creation
) creation
= OPEN_EXISTING
;
1316 else dosdev
= RtlIsDosDeviceName_U( filename
);
1320 static const WCHAR conW
[] = {'C','O','N'};
1322 if (LOWORD(dosdev
) == sizeof(conW
) &&
1323 !memicmpW( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), conW
, sizeof(conW
)/sizeof(WCHAR
)))
1325 switch (access
& (GENERIC_READ
|GENERIC_WRITE
))
1328 ret
= OpenConsoleW(coninW
, access
, (sa
&& sa
->bInheritHandle
), OPEN_EXISTING
);
1331 ret
= OpenConsoleW(conoutW
, access
, (sa
&& sa
->bInheritHandle
), OPEN_EXISTING
);
1334 SetLastError( ERROR_FILE_NOT_FOUND
);
1335 return INVALID_HANDLE_VALUE
;
1340 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
1342 SetLastError( ERROR_INVALID_PARAMETER
);
1343 return INVALID_HANDLE_VALUE
;
1346 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
1348 SetLastError( ERROR_PATH_NOT_FOUND
);
1349 return INVALID_HANDLE_VALUE
;
1352 /* now call NtCreateFile */
1355 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
1356 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
1358 options
|= FILE_NON_DIRECTORY_FILE
;
1359 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
1361 options
|= FILE_DELETE_ON_CLOSE
;
1364 if (attributes
& FILE_FLAG_NO_BUFFERING
)
1365 options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
1366 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
1367 options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
1368 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
1369 options
|= FILE_RANDOM_ACCESS
;
1370 attributes
&= FILE_ATTRIBUTE_VALID_FLAGS
;
1372 attr
.Length
= sizeof(attr
);
1373 attr
.RootDirectory
= 0;
1374 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1375 attr
.ObjectName
= &nameW
;
1376 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1377 if (attributes
& SECURITY_SQOS_PRESENT
)
1379 qos
.Length
= sizeof(qos
);
1380 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
1381 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
1382 qos
.EffectiveOnly
= attributes
& SECURITY_EFFECTIVE_ONLY
? TRUE
: FALSE
;
1383 attr
.SecurityQualityOfService
= &qos
;
1386 attr
.SecurityQualityOfService
= NULL
;
1388 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
1390 status
= NtCreateFile( &ret
, access
, &attr
, &io
, NULL
, attributes
,
1391 sharing
, nt_disposition
[creation
- CREATE_NEW
],
1395 if (vxd_name
&& vxd_name
[0])
1397 static HANDLE (*vxd_open
)(LPCWSTR
,DWORD
,SECURITY_ATTRIBUTES
*);
1398 if (!vxd_open
) vxd_open
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1399 "__wine_vxd_open" );
1400 if (vxd_open
&& (ret
= vxd_open( vxd_name
, access
, sa
))) goto done
;
1403 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
1404 ret
= INVALID_HANDLE_VALUE
;
1406 /* In the case file creation was rejected due to CREATE_NEW flag
1407 * was specified and file with that name already exists, correct
1408 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1409 * Note: RtlNtStatusToDosError is not the subject to blame here.
1411 if (status
== STATUS_OBJECT_NAME_COLLISION
)
1412 SetLastError( ERROR_FILE_EXISTS
);
1414 SetLastError( RtlNtStatusToDosError(status
) );
1418 if ((creation
== CREATE_ALWAYS
&& io
.Information
== FILE_OVERWRITTEN
) ||
1419 (creation
== OPEN_ALWAYS
&& io
.Information
== FILE_OPENED
))
1420 SetLastError( ERROR_ALREADY_EXISTS
);
1424 RtlFreeUnicodeString( &nameW
);
1427 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
1428 TRACE("returning %p\n", ret
);
1434 /*************************************************************************
1435 * CreateFileA (KERNEL32.@)
1439 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
1440 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1441 DWORD attributes
, HANDLE
template)
1445 if (!(nameW
= FILE_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1446 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
1450 /***********************************************************************
1451 * DeleteFileW (KERNEL32.@)
1456 * path [I] Path to the file to delete.
1460 * Failure: FALSE, check GetLastError().
1462 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1464 UNICODE_STRING nameW
;
1465 OBJECT_ATTRIBUTES attr
;
1470 TRACE("%s\n", debugstr_w(path
) );
1472 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
1474 SetLastError( ERROR_PATH_NOT_FOUND
);
1478 attr
.Length
= sizeof(attr
);
1479 attr
.RootDirectory
= 0;
1480 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1481 attr
.ObjectName
= &nameW
;
1482 attr
.SecurityDescriptor
= NULL
;
1483 attr
.SecurityQualityOfService
= NULL
;
1485 status
= NtCreateFile(&hFile
, GENERIC_READ
| GENERIC_WRITE
| DELETE
,
1486 &attr
, &io
, NULL
, 0,
1487 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1488 FILE_OPEN
, FILE_DELETE_ON_CLOSE
| FILE_NON_DIRECTORY_FILE
, NULL
, 0);
1489 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
1491 RtlFreeUnicodeString( &nameW
);
1494 SetLastError( RtlNtStatusToDosError(status
) );
1501 /***********************************************************************
1502 * DeleteFileA (KERNEL32.@)
1506 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1510 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1511 return DeleteFileW( pathW
);
1515 /**************************************************************************
1516 * ReplaceFileW (KERNEL32.@)
1517 * ReplaceFile (KERNEL32.@)
1519 BOOL WINAPI
ReplaceFileW(LPCWSTR lpReplacedFileName
, LPCWSTR lpReplacementFileName
,
1520 LPCWSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1521 LPVOID lpExclude
, LPVOID lpReserved
)
1523 UNICODE_STRING nt_replaced_name
, nt_replacement_name
;
1524 ANSI_STRING unix_replaced_name
, unix_replacement_name
, unix_backup_name
;
1525 HANDLE hReplaced
= NULL
, hReplacement
= NULL
, hBackup
= NULL
;
1526 DWORD error
= ERROR_SUCCESS
;
1527 UINT replaced_flags
;
1531 OBJECT_ATTRIBUTES attr
;
1533 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName
),
1534 debugstr_w(lpReplacementFileName
), debugstr_w(lpBackupFileName
),
1535 dwReplaceFlags
, lpExclude
, lpReserved
);
1538 FIXME("Ignoring flags %x\n", dwReplaceFlags
);
1540 /* First two arguments are mandatory */
1541 if (!lpReplacedFileName
|| !lpReplacementFileName
)
1543 SetLastError(ERROR_INVALID_PARAMETER
);
1547 unix_replaced_name
.Buffer
= NULL
;
1548 unix_replacement_name
.Buffer
= NULL
;
1549 unix_backup_name
.Buffer
= NULL
;
1551 attr
.Length
= sizeof(attr
);
1552 attr
.RootDirectory
= 0;
1553 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1554 attr
.ObjectName
= NULL
;
1555 attr
.SecurityDescriptor
= NULL
;
1556 attr
.SecurityQualityOfService
= NULL
;
1558 /* Open the "replaced" file for reading and writing */
1559 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName
, &nt_replaced_name
, NULL
, NULL
)))
1561 error
= ERROR_PATH_NOT_FOUND
;
1564 replaced_flags
= lpBackupFileName
? FILE_OPEN
: FILE_OPEN_IF
;
1565 attr
.ObjectName
= &nt_replaced_name
;
1566 status
= NtOpenFile(&hReplaced
, GENERIC_READ
|GENERIC_WRITE
|DELETE
|SYNCHRONIZE
,
1568 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
1569 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1570 if (status
== STATUS_SUCCESS
)
1571 status
= wine_nt_to_unix_file_name(&nt_replaced_name
, &unix_replaced_name
, replaced_flags
, FALSE
);
1572 RtlFreeUnicodeString(&nt_replaced_name
);
1573 if (status
!= STATUS_SUCCESS
)
1575 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1576 error
= ERROR_FILE_NOT_FOUND
;
1578 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1583 * Open the replacement file for reading, writing, and deleting
1584 * (writing and deleting are needed when finished)
1586 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName
, &nt_replacement_name
, NULL
, NULL
)))
1588 error
= ERROR_PATH_NOT_FOUND
;
1591 attr
.ObjectName
= &nt_replacement_name
;
1592 status
= NtOpenFile(&hReplacement
,
1593 GENERIC_READ
|GENERIC_WRITE
|DELETE
|WRITE_DAC
|SYNCHRONIZE
,
1595 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1596 if (status
== STATUS_SUCCESS
)
1597 status
= wine_nt_to_unix_file_name(&nt_replacement_name
, &unix_replacement_name
, FILE_OPEN
, FALSE
);
1598 RtlFreeUnicodeString(&nt_replacement_name
);
1599 if (status
!= STATUS_SUCCESS
)
1601 error
= RtlNtStatusToDosError(status
);
1605 /* If the user wants a backup then that needs to be performed first */
1606 if (lpBackupFileName
)
1608 UNICODE_STRING nt_backup_name
;
1609 FILE_BASIC_INFORMATION replaced_info
;
1611 /* Obtain the file attributes from the "replaced" file */
1612 status
= NtQueryInformationFile(hReplaced
, &io
, &replaced_info
,
1613 sizeof(replaced_info
),
1614 FileBasicInformation
);
1615 if (status
!= STATUS_SUCCESS
)
1617 error
= RtlNtStatusToDosError(status
);
1621 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName
, &nt_backup_name
, NULL
, NULL
)))
1623 error
= ERROR_PATH_NOT_FOUND
;
1626 attr
.ObjectName
= &nt_backup_name
;
1627 /* Open the backup with permissions to write over it */
1628 status
= NtCreateFile(&hBackup
, GENERIC_WRITE
,
1629 &attr
, &io
, NULL
, replaced_info
.FileAttributes
,
1630 FILE_SHARE_WRITE
, FILE_OPEN_IF
,
1631 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
,
1633 if (status
== STATUS_SUCCESS
)
1634 status
= wine_nt_to_unix_file_name(&nt_backup_name
, &unix_backup_name
, FILE_OPEN_IF
, FALSE
);
1635 RtlFreeUnicodeString(&nt_backup_name
);
1636 if (status
!= STATUS_SUCCESS
)
1638 error
= RtlNtStatusToDosError(status
);
1642 /* If an existing backup exists then copy over it */
1643 if (rename(unix_replaced_name
.Buffer
, unix_backup_name
.Buffer
) == -1)
1645 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
; /* is this correct? */
1651 * Now that the backup has been performed (if requested), copy the replacement
1654 if (rename(unix_replacement_name
.Buffer
, unix_replaced_name
.Buffer
) == -1)
1656 if (errno
== EACCES
)
1658 /* Inappropriate permissions on "replaced", rename will fail */
1659 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1662 /* on failure we need to indicate whether a backup was made */
1663 if (!lpBackupFileName
)
1664 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT
;
1666 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
;
1672 /* Perform resource cleanup */
1674 if (hBackup
) CloseHandle(hBackup
);
1675 if (hReplaced
) CloseHandle(hReplaced
);
1676 if (hReplacement
) CloseHandle(hReplacement
);
1677 RtlFreeAnsiString(&unix_backup_name
);
1678 RtlFreeAnsiString(&unix_replacement_name
);
1679 RtlFreeAnsiString(&unix_replaced_name
);
1681 /* If there was an error, set the error code */
1683 SetLastError(error
);
1688 /**************************************************************************
1689 * ReplaceFileA (KERNEL32.@)
1691 BOOL WINAPI
ReplaceFileA(LPCSTR lpReplacedFileName
,LPCSTR lpReplacementFileName
,
1692 LPCSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1693 LPVOID lpExclude
, LPVOID lpReserved
)
1695 WCHAR
*replacedW
, *replacementW
, *backupW
= NULL
;
1698 /* This function only makes sense when the first two parameters are defined */
1699 if (!lpReplacedFileName
|| !(replacedW
= FILE_name_AtoW( lpReplacedFileName
, TRUE
)))
1701 SetLastError(ERROR_INVALID_PARAMETER
);
1704 if (!lpReplacementFileName
|| !(replacementW
= FILE_name_AtoW( lpReplacementFileName
, TRUE
)))
1706 HeapFree( GetProcessHeap(), 0, replacedW
);
1707 SetLastError(ERROR_INVALID_PARAMETER
);
1710 /* The backup parameter, however, is optional */
1711 if (lpBackupFileName
)
1713 if (!(backupW
= FILE_name_AtoW( lpBackupFileName
, TRUE
)))
1715 HeapFree( GetProcessHeap(), 0, replacedW
);
1716 HeapFree( GetProcessHeap(), 0, replacementW
);
1717 SetLastError(ERROR_INVALID_PARAMETER
);
1721 ret
= ReplaceFileW( replacedW
, replacementW
, backupW
, dwReplaceFlags
, lpExclude
, lpReserved
);
1722 HeapFree( GetProcessHeap(), 0, replacedW
);
1723 HeapFree( GetProcessHeap(), 0, replacementW
);
1724 HeapFree( GetProcessHeap(), 0, backupW
);
1729 /*************************************************************************
1730 * FindFirstFileExW (KERNEL32.@)
1732 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1733 * results as FindExSearchNameMatch
1735 HANDLE WINAPI
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1736 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1737 LPVOID filter
, DWORD flags
)
1740 FIND_FIRST_INFO
*info
= NULL
;
1741 UNICODE_STRING nt_name
;
1742 OBJECT_ATTRIBUTES attr
;
1747 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1749 if ((search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1752 FIXME("options not implemented 0x%08x 0x%08x\n", search_op
, flags
);
1753 return INVALID_HANDLE_VALUE
;
1755 if (level
!= FindExInfoStandard
)
1757 FIXME("info level %d not implemented\n", level
);
1758 return INVALID_HANDLE_VALUE
;
1761 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1763 SetLastError( ERROR_PATH_NOT_FOUND
);
1764 return INVALID_HANDLE_VALUE
;
1767 if (!(info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
))))
1769 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1773 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1775 static const WCHAR dotW
[] = {'.',0};
1778 /* we still need to check that the directory can be opened */
1782 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1784 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1787 memcpy( dir
, filename
, HIWORD(device
) );
1788 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1790 RtlFreeUnicodeString( &nt_name
);
1791 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: dotW
, &nt_name
, &mask
, NULL
))
1793 HeapFree( GetProcessHeap(), 0, dir
);
1794 SetLastError( ERROR_PATH_NOT_FOUND
);
1797 HeapFree( GetProcessHeap(), 0, dir
);
1798 RtlInitUnicodeString( &info
->mask
, NULL
);
1800 else if (!mask
|| !*mask
)
1802 SetLastError( ERROR_FILE_NOT_FOUND
);
1807 if (!RtlCreateUnicodeString( &info
->mask
, mask
))
1809 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1813 /* truncate dir name before mask */
1815 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
1818 /* check if path is the root of the drive */
1819 info
->is_root
= FALSE
;
1820 p
= nt_name
.Buffer
+ 4; /* skip \??\ prefix */
1821 if (p
[0] && p
[1] == ':')
1824 while (*p
== '\\') p
++;
1825 info
->is_root
= (*p
== 0);
1828 attr
.Length
= sizeof(attr
);
1829 attr
.RootDirectory
= 0;
1830 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1831 attr
.ObjectName
= &nt_name
;
1832 attr
.SecurityDescriptor
= NULL
;
1833 attr
.SecurityQualityOfService
= NULL
;
1835 status
= NtOpenFile( &info
->handle
, GENERIC_READ
, &attr
, &io
,
1836 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1837 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1839 if (status
!= STATUS_SUCCESS
)
1841 RtlFreeUnicodeString( &info
->mask
);
1842 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1843 SetLastError( ERROR_PATH_NOT_FOUND
);
1845 SetLastError( RtlNtStatusToDosError(status
) );
1849 RtlInitializeCriticalSection( &info
->cs
);
1850 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
1851 info
->path
= nt_name
;
1852 info
->magic
= FIND_FIRST_MAGIC
;
1855 info
->search_op
= search_op
;
1859 WIN32_FIND_DATAW
*wfd
= data
;
1861 memset( wfd
, 0, sizeof(*wfd
) );
1862 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
1863 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1864 CloseHandle( info
->handle
);
1871 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1872 FileBothDirectoryInformation
, FALSE
, &info
->mask
, TRUE
);
1876 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1877 return INVALID_HANDLE_VALUE
;
1879 info
->data_len
= io
.Information
;
1880 if (!FindNextFileW( info
, data
))
1882 TRACE( "%s not found\n", debugstr_w(filename
) );
1884 SetLastError( ERROR_FILE_NOT_FOUND
);
1885 return INVALID_HANDLE_VALUE
;
1887 if (!strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1889 /* we can't find two files with the same name */
1890 CloseHandle( info
->handle
);
1897 HeapFree( GetProcessHeap(), 0, info
);
1898 RtlFreeUnicodeString( &nt_name
);
1899 return INVALID_HANDLE_VALUE
;
1903 /*************************************************************************
1904 * FindNextFileW (KERNEL32.@)
1906 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1908 FIND_FIRST_INFO
*info
;
1909 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1912 TRACE("%p %p\n", handle
, data
);
1914 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1916 SetLastError( ERROR_INVALID_HANDLE
);
1920 if (info
->magic
!= FIND_FIRST_MAGIC
)
1922 SetLastError( ERROR_INVALID_HANDLE
);
1926 RtlEnterCriticalSection( &info
->cs
);
1928 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
1931 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
1935 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1936 FileBothDirectoryInformation
, FALSE
, &info
->mask
, FALSE
);
1939 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1940 if (io
.u
.Status
== STATUS_NO_MORE_FILES
)
1942 CloseHandle( info
->handle
);
1947 info
->data_len
= io
.Information
;
1951 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
1953 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
1954 else info
->data_pos
= info
->data_len
;
1956 /* don't return '.' and '..' in the root of the drive */
1959 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
1960 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
1961 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
1964 /* check for dir symlink */
1965 if ((dir_info
->FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
1966 (dir_info
->FileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
) &&
1967 strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1969 if (!check_dir_symlink( info
, dir_info
)) continue;
1972 data
->dwFileAttributes
= dir_info
->FileAttributes
;
1973 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
1974 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
1975 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
1976 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
1977 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
1978 data
->dwReserved0
= 0;
1979 data
->dwReserved1
= 0;
1981 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
1982 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
1983 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
1984 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
1986 TRACE("returning %s (%s)\n",
1987 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
1993 RtlLeaveCriticalSection( &info
->cs
);
1998 /*************************************************************************
1999 * FindClose (KERNEL32.@)
2001 BOOL WINAPI
FindClose( HANDLE handle
)
2003 FIND_FIRST_INFO
*info
= handle
;
2005 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
2007 SetLastError( ERROR_INVALID_HANDLE
);
2013 if (info
->magic
== FIND_FIRST_MAGIC
)
2015 RtlEnterCriticalSection( &info
->cs
);
2016 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
2019 if (info
->handle
) CloseHandle( info
->handle
);
2021 RtlFreeUnicodeString( &info
->mask
);
2022 info
->mask
.Buffer
= NULL
;
2023 RtlFreeUnicodeString( &info
->path
);
2026 RtlLeaveCriticalSection( &info
->cs
);
2027 info
->cs
.DebugInfo
->Spare
[0] = 0;
2028 RtlDeleteCriticalSection( &info
->cs
);
2029 HeapFree( GetProcessHeap(), 0, info
);
2035 WARN("Illegal handle %p\n", handle
);
2036 SetLastError( ERROR_INVALID_HANDLE
);
2045 /*************************************************************************
2046 * FindFirstFileA (KERNEL32.@)
2048 HANDLE WINAPI
FindFirstFileA( LPCSTR lpFileName
, WIN32_FIND_DATAA
*lpFindData
)
2050 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
2051 FindExSearchNameMatch
, NULL
, 0);
2054 /*************************************************************************
2055 * FindFirstFileExA (KERNEL32.@)
2057 HANDLE WINAPI
FindFirstFileExA( LPCSTR lpFileName
, FINDEX_INFO_LEVELS fInfoLevelId
,
2058 LPVOID lpFindFileData
, FINDEX_SEARCH_OPS fSearchOp
,
2059 LPVOID lpSearchFilter
, DWORD dwAdditionalFlags
)
2062 WIN32_FIND_DATAA
*dataA
;
2063 WIN32_FIND_DATAW dataW
;
2066 if (!(nameW
= FILE_name_AtoW( lpFileName
, FALSE
))) return INVALID_HANDLE_VALUE
;
2068 handle
= FindFirstFileExW(nameW
, fInfoLevelId
, &dataW
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
2069 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
2071 dataA
= lpFindFileData
;
2072 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
2073 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
2074 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2075 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2076 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2077 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
2078 FILE_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
2079 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
2080 sizeof(dataA
->cAlternateFileName
) );
2085 /*************************************************************************
2086 * FindFirstFileW (KERNEL32.@)
2088 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
2090 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
2091 FindExSearchNameMatch
, NULL
, 0);
2095 /*************************************************************************
2096 * FindNextFileA (KERNEL32.@)
2098 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
2100 WIN32_FIND_DATAW dataW
;
2102 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
2103 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
2104 data
->ftCreationTime
= dataW
.ftCreationTime
;
2105 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2106 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2107 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2108 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
2109 FILE_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
2110 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
2111 sizeof(data
->cAlternateFileName
) );
2116 /**************************************************************************
2117 * GetFileAttributesW (KERNEL32.@)
2119 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
2121 FILE_BASIC_INFORMATION info
;
2122 UNICODE_STRING nt_name
;
2123 OBJECT_ATTRIBUTES attr
;
2126 TRACE("%s\n", debugstr_w(name
));
2128 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2130 SetLastError( ERROR_PATH_NOT_FOUND
);
2131 return INVALID_FILE_ATTRIBUTES
;
2134 attr
.Length
= sizeof(attr
);
2135 attr
.RootDirectory
= 0;
2136 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2137 attr
.ObjectName
= &nt_name
;
2138 attr
.SecurityDescriptor
= NULL
;
2139 attr
.SecurityQualityOfService
= NULL
;
2141 status
= NtQueryAttributesFile( &attr
, &info
);
2142 RtlFreeUnicodeString( &nt_name
);
2144 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
2146 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2147 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
2149 SetLastError( RtlNtStatusToDosError(status
) );
2150 return INVALID_FILE_ATTRIBUTES
;
2154 /**************************************************************************
2155 * GetFileAttributesA (KERNEL32.@)
2157 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
2161 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
2162 return GetFileAttributesW( nameW
);
2166 /**************************************************************************
2167 * SetFileAttributesW (KERNEL32.@)
2169 BOOL WINAPI
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
2171 UNICODE_STRING nt_name
;
2172 OBJECT_ATTRIBUTES attr
;
2177 TRACE("%s %x\n", debugstr_w(name
), attributes
);
2179 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2181 SetLastError( ERROR_PATH_NOT_FOUND
);
2185 attr
.Length
= sizeof(attr
);
2186 attr
.RootDirectory
= 0;
2187 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2188 attr
.ObjectName
= &nt_name
;
2189 attr
.SecurityDescriptor
= NULL
;
2190 attr
.SecurityQualityOfService
= NULL
;
2192 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2193 RtlFreeUnicodeString( &nt_name
);
2195 if (status
== STATUS_SUCCESS
)
2197 FILE_BASIC_INFORMATION info
;
2199 memset( &info
, 0, sizeof(info
) );
2200 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2201 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2205 if (status
== STATUS_SUCCESS
) return TRUE
;
2206 SetLastError( RtlNtStatusToDosError(status
) );
2211 /**************************************************************************
2212 * SetFileAttributesA (KERNEL32.@)
2214 BOOL WINAPI
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2218 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2219 return SetFileAttributesW( nameW
, attributes
);
2223 /**************************************************************************
2224 * GetFileAttributesExW (KERNEL32.@)
2226 BOOL WINAPI
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2228 FILE_NETWORK_OPEN_INFORMATION info
;
2229 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
2230 UNICODE_STRING nt_name
;
2231 OBJECT_ATTRIBUTES attr
;
2234 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
2236 if (level
!= GetFileExInfoStandard
)
2238 SetLastError( ERROR_INVALID_PARAMETER
);
2242 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2244 SetLastError( ERROR_PATH_NOT_FOUND
);
2248 attr
.Length
= sizeof(attr
);
2249 attr
.RootDirectory
= 0;
2250 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2251 attr
.ObjectName
= &nt_name
;
2252 attr
.SecurityDescriptor
= NULL
;
2253 attr
.SecurityQualityOfService
= NULL
;
2255 status
= NtQueryFullAttributesFile( &attr
, &info
);
2256 RtlFreeUnicodeString( &nt_name
);
2258 if (status
!= STATUS_SUCCESS
)
2260 SetLastError( RtlNtStatusToDosError(status
) );
2264 data
->dwFileAttributes
= info
.FileAttributes
;
2265 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
2266 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
2267 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
2268 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
2269 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
2270 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
2271 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
2272 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
2277 /**************************************************************************
2278 * GetFileAttributesExA (KERNEL32.@)
2280 BOOL WINAPI
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2284 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2285 return GetFileAttributesExW( nameW
, level
, ptr
);
2289 /******************************************************************************
2290 * GetCompressedFileSizeW (KERNEL32.@)
2292 * Get the actual number of bytes used on disk.
2295 * Success: Low-order doubleword of number of bytes
2296 * Failure: INVALID_FILE_SIZE
2298 DWORD WINAPI
GetCompressedFileSizeW(
2299 LPCWSTR name
, /* [in] Pointer to name of file */
2300 LPDWORD size_high
) /* [out] Receives high-order doubleword of size */
2302 UNICODE_STRING nt_name
;
2303 OBJECT_ATTRIBUTES attr
;
2307 DWORD ret
= INVALID_FILE_SIZE
;
2309 TRACE("%s %p\n", debugstr_w(name
), size_high
);
2311 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2313 SetLastError( ERROR_PATH_NOT_FOUND
);
2314 return INVALID_FILE_SIZE
;
2317 attr
.Length
= sizeof(attr
);
2318 attr
.RootDirectory
= 0;
2319 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2320 attr
.ObjectName
= &nt_name
;
2321 attr
.SecurityDescriptor
= NULL
;
2322 attr
.SecurityQualityOfService
= NULL
;
2324 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2325 RtlFreeUnicodeString( &nt_name
);
2327 if (status
== STATUS_SUCCESS
)
2329 /* we don't support compressed files, simply return the file size */
2330 ret
= GetFileSize( handle
, size_high
);
2333 else SetLastError( RtlNtStatusToDosError(status
) );
2339 /******************************************************************************
2340 * GetCompressedFileSizeA (KERNEL32.@)
2342 * See GetCompressedFileSizeW.
2344 DWORD WINAPI
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
2348 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
2349 return GetCompressedFileSizeW( nameW
, size_high
);
2353 /***********************************************************************
2354 * OpenVxDHandle (KERNEL32.@)
2356 * This function is supposed to return the corresponding Ring 0
2357 * ("kernel") handle for a Ring 3 handle in Win9x.
2358 * Evidently, Wine will have problems with this. But we try anyway,
2361 HANDLE WINAPI
OpenVxDHandle(HANDLE hHandleRing3
)
2363 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3
);
2364 return hHandleRing3
;
2368 /****************************************************************************
2369 * DeviceIoControl (KERNEL32.@)
2371 BOOL WINAPI
DeviceIoControl(HANDLE hDevice
, DWORD dwIoControlCode
,
2372 LPVOID lpvInBuffer
, DWORD cbInBuffer
,
2373 LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
2374 LPDWORD lpcbBytesReturned
,
2375 LPOVERLAPPED lpOverlapped
)
2379 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2380 hDevice
,dwIoControlCode
,lpvInBuffer
,cbInBuffer
,
2381 lpvOutBuffer
,cbOutBuffer
,lpcbBytesReturned
,lpOverlapped
);
2383 /* Check if this is a user defined control code for a VxD */
2385 if (HIWORD( dwIoControlCode
) == 0 && (GetVersion() & 0x80000000))
2387 typedef BOOL (WINAPI
*DeviceIoProc
)(DWORD
, LPVOID
, DWORD
, LPVOID
, DWORD
, LPDWORD
, LPOVERLAPPED
);
2388 static DeviceIoProc (*vxd_get_proc
)(HANDLE
);
2389 DeviceIoProc proc
= NULL
;
2391 if (!vxd_get_proc
) vxd_get_proc
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2392 "__wine_vxd_get_proc" );
2393 if (vxd_get_proc
) proc
= vxd_get_proc( hDevice
);
2394 if (proc
) return proc( dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2395 lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
);
2398 /* Not a VxD, let ntdll handle it */
2402 LPVOID cvalue
= ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) ? NULL
: lpOverlapped
;
2403 lpOverlapped
->Internal
= STATUS_PENDING
;
2404 lpOverlapped
->InternalHigh
= 0;
2405 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2406 status
= NtFsControlFile(hDevice
, lpOverlapped
->hEvent
,
2407 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2408 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2409 lpvOutBuffer
, cbOutBuffer
);
2411 status
= NtDeviceIoControlFile(hDevice
, lpOverlapped
->hEvent
,
2412 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2413 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2414 lpvOutBuffer
, cbOutBuffer
);
2415 if (lpcbBytesReturned
) *lpcbBytesReturned
= lpOverlapped
->InternalHigh
;
2419 IO_STATUS_BLOCK iosb
;
2421 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2422 status
= NtFsControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2423 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2424 lpvOutBuffer
, cbOutBuffer
);
2426 status
= NtDeviceIoControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2427 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2428 lpvOutBuffer
, cbOutBuffer
);
2429 if (lpcbBytesReturned
) *lpcbBytesReturned
= iosb
.Information
;
2431 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2436 /***********************************************************************
2437 * OpenFile (KERNEL32.@)
2439 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
2443 WORD filedatetime
[2];
2445 if (!ofs
) return HFILE_ERROR
;
2447 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
2448 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
2449 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
2450 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
2451 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
2452 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
2453 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
2454 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
2455 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
2456 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
2457 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
2458 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
2459 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
2460 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
2461 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
2462 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
2463 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
2464 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
2468 ofs
->cBytes
= sizeof(OFSTRUCT
);
2470 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
2472 if (!name
) return HFILE_ERROR
;
2474 TRACE("%s %04x\n", name
, mode
);
2476 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2477 Are there any cases where getting the path here is wrong?
2478 Uwe Bonnes 1997 Apr 2 */
2479 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
)) goto error
;
2481 /* OF_PARSE simply fills the structure */
2483 if (mode
& OF_PARSE
)
2485 ofs
->fFixedDisk
= (GetDriveTypeA( ofs
->szPathName
) != DRIVE_REMOVABLE
);
2486 TRACE("(%s): OF_PARSE, res = '%s'\n", name
, ofs
->szPathName
);
2490 /* OF_CREATE is completely different from all other options, so
2493 if (mode
& OF_CREATE
)
2495 if ((handle
= create_file_OF( name
, mode
)) == INVALID_HANDLE_VALUE
)
2500 /* Now look for the file */
2502 if (!SearchPathA( NULL
, name
, NULL
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
))
2505 TRACE("found %s\n", debugstr_a(ofs
->szPathName
) );
2507 if (mode
& OF_DELETE
)
2509 if (!DeleteFileA( ofs
->szPathName
)) goto error
;
2510 TRACE("(%s): OF_DELETE return = OK\n", name
);
2514 handle
= LongToHandle(_lopen( ofs
->szPathName
, mode
));
2515 if (handle
== INVALID_HANDLE_VALUE
) goto error
;
2517 GetFileTime( handle
, NULL
, NULL
, &filetime
);
2518 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
2519 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
2521 if (ofs
->Reserved1
!= filedatetime
[0] || ofs
->Reserved2
!= filedatetime
[1] )
2523 CloseHandle( handle
);
2524 WARN("(%s): OF_VERIFY failed\n", name
);
2525 /* FIXME: what error here? */
2526 SetLastError( ERROR_FILE_NOT_FOUND
);
2530 ofs
->Reserved1
= filedatetime
[0];
2531 ofs
->Reserved2
= filedatetime
[1];
2533 TRACE("(%s): OK, return = %p\n", name
, handle
);
2534 if (mode
& OF_EXIST
) /* Return TRUE instead of a handle */
2536 CloseHandle( handle
);
2539 return HandleToLong(handle
);
2541 error
: /* We get here if there was an error opening the file */
2542 ofs
->nErrCode
= GetLastError();
2543 WARN("(%s): return = HFILE_ERROR error= %d\n", name
,ofs
->nErrCode
);
2547 /***********************************************************************
2548 * K32EnumDeviceDrivers (KERNEL32.@)
2550 BOOL WINAPI
K32EnumDeviceDrivers(void **image_base
, DWORD cb
, DWORD
*needed
)
2552 FIXME("(%p, %d, %p): stub\n", image_base
, cb
, needed
);
2560 /***********************************************************************
2561 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2563 DWORD WINAPI
K32GetDeviceDriverBaseNameA(void *image_base
, LPSTR base_name
, DWORD size
)
2565 FIXME("(%p, %p, %d): stub\n", image_base
, base_name
, size
);
2567 if (base_name
&& size
)
2568 base_name
[0] = '\0';
2573 /***********************************************************************
2574 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2576 DWORD WINAPI
K32GetDeviceDriverBaseNameW(void *image_base
, LPWSTR base_name
, DWORD size
)
2578 FIXME("(%p, %p, %d): stub\n", image_base
, base_name
, size
);
2580 if (base_name
&& size
)
2581 base_name
[0] = '\0';
2586 /***********************************************************************
2587 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2589 DWORD WINAPI
K32GetDeviceDriverFileNameA(void *image_base
, LPSTR file_name
, DWORD size
)
2591 FIXME("(%p, %p, %d): stub\n", image_base
, file_name
, size
);
2593 if (file_name
&& size
)
2594 file_name
[0] = '\0';
2599 /***********************************************************************
2600 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2602 DWORD WINAPI
K32GetDeviceDriverFileNameW(void *image_base
, LPWSTR file_name
, DWORD size
)
2604 FIXME("(%p, %p, %d): stub\n", image_base
, file_name
, size
);
2606 if (file_name
&& size
)
2607 file_name
[0] = '\0';