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
))
409 return ReadConsoleA(hFile
, buffer
, bytesToRead
, bytesRead
, NULL
);
411 if (overlapped
!= NULL
)
413 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
414 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
416 hEvent
= overlapped
->hEvent
;
417 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
418 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
420 io_status
->u
.Status
= STATUS_PENDING
;
421 io_status
->Information
= 0;
423 status
= NtReadFile(hFile
, hEvent
, NULL
, cvalue
, io_status
, buffer
, bytesToRead
, poffset
, NULL
);
425 if (status
== STATUS_PENDING
&& !overlapped
)
427 WaitForSingleObject( hFile
, INFINITE
);
428 status
= io_status
->u
.Status
;
431 if (status
!= STATUS_PENDING
&& bytesRead
)
432 *bytesRead
= io_status
->Information
;
434 if (status
&& status
!= STATUS_END_OF_FILE
&& status
!= STATUS_TIMEOUT
)
436 SetLastError( RtlNtStatusToDosError(status
) );
443 /***********************************************************************
444 * WriteFileEx (KERNEL32.@)
446 BOOL WINAPI
WriteFileEx(HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
447 LPOVERLAPPED overlapped
,
448 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
450 LARGE_INTEGER offset
;
452 PIO_STATUS_BLOCK io_status
;
454 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, overlapped
, lpCompletionRoutine
);
456 if (overlapped
== NULL
)
458 SetLastError(ERROR_INVALID_PARAMETER
);
461 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
462 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
464 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
465 io_status
->u
.Status
= STATUS_PENDING
;
466 io_status
->Information
= 0;
468 status
= NtWriteFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
469 io_status
, buffer
, bytesToWrite
, &offset
, NULL
);
471 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
476 /***********************************************************************
477 * WriteFileGather (KERNEL32.@)
479 BOOL WINAPI
WriteFileGather( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
480 LPDWORD reserved
, LPOVERLAPPED overlapped
)
482 PIO_STATUS_BLOCK io_status
;
483 LARGE_INTEGER offset
;
486 TRACE( "%p %p %u %p\n", file
, segments
, count
, overlapped
);
488 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
489 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
490 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
491 io_status
->u
.Status
= STATUS_PENDING
;
492 io_status
->Information
= 0;
494 status
= NtWriteFileGather( file
, NULL
, NULL
, NULL
, io_status
, segments
, count
, &offset
, NULL
);
495 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
500 /***********************************************************************
501 * WriteFile (KERNEL32.@)
503 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
504 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
506 HANDLE hEvent
= NULL
;
507 LARGE_INTEGER offset
;
508 PLARGE_INTEGER poffset
= NULL
;
510 IO_STATUS_BLOCK iosb
;
511 PIO_STATUS_BLOCK piosb
= &iosb
;
512 LPVOID cvalue
= NULL
;
514 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, bytesWritten
, overlapped
);
516 if (is_console_handle(hFile
))
517 return WriteConsoleA(hFile
, buffer
, bytesToWrite
, bytesWritten
, NULL
);
521 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
522 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
524 hEvent
= overlapped
->hEvent
;
525 piosb
= (PIO_STATUS_BLOCK
)overlapped
;
526 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
528 piosb
->u
.Status
= STATUS_PENDING
;
529 piosb
->Information
= 0;
531 status
= NtWriteFile(hFile
, hEvent
, NULL
, cvalue
, piosb
,
532 buffer
, bytesToWrite
, poffset
, NULL
);
534 if (status
== STATUS_PENDING
&& !overlapped
)
536 WaitForSingleObject( hFile
, INFINITE
);
537 status
= piosb
->u
.Status
;
540 if (status
!= STATUS_PENDING
&& bytesWritten
)
541 *bytesWritten
= piosb
->Information
;
543 if (status
&& status
!= STATUS_TIMEOUT
)
545 SetLastError( RtlNtStatusToDosError(status
) );
552 /***********************************************************************
553 * GetOverlappedResult (KERNEL32.@)
555 * Check the result of an Asynchronous data transfer from a file.
558 * HANDLE hFile [in] handle of file to check on
559 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
560 * LPDWORD lpTransferred [in/out] number of bytes transferred
561 * BOOL bWait [in] wait for the transfer to complete ?
567 * If successful (and relevant) lpTransferred will hold the number of
568 * bytes transferred during the async operation.
570 BOOL WINAPI
GetOverlappedResult(HANDLE hFile
, LPOVERLAPPED lpOverlapped
,
571 LPDWORD lpTransferred
, BOOL bWait
)
575 TRACE( "(%p %p %p %x)\n", hFile
, lpOverlapped
, lpTransferred
, bWait
);
577 status
= lpOverlapped
->Internal
;
578 if (status
== STATUS_PENDING
)
582 SetLastError( ERROR_IO_INCOMPLETE
);
586 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: hFile
,
587 INFINITE
) == WAIT_FAILED
)
589 status
= lpOverlapped
->Internal
;
592 *lpTransferred
= lpOverlapped
->InternalHigh
;
594 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
598 /***********************************************************************
599 * CancelIoEx (KERNEL32.@)
601 * Cancels pending I/O operations on a file given the overlapped used.
604 * handle [I] File handle.
605 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
609 * Failure: FALSE, check GetLastError().
611 BOOL WINAPI
CancelIoEx(HANDLE handle
, LPOVERLAPPED lpOverlapped
)
613 IO_STATUS_BLOCK io_status
;
615 NtCancelIoFileEx(handle
, (PIO_STATUS_BLOCK
) lpOverlapped
, &io_status
);
616 if (io_status
.u
.Status
)
618 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
624 /***********************************************************************
625 * CancelIo (KERNEL32.@)
627 * Cancels pending I/O operations initiated by the current thread on a file.
630 * handle [I] File handle.
634 * Failure: FALSE, check GetLastError().
636 BOOL WINAPI
CancelIo(HANDLE handle
)
638 IO_STATUS_BLOCK io_status
;
640 NtCancelIoFile(handle
, &io_status
);
641 if (io_status
.u
.Status
)
643 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
649 /***********************************************************************
650 * _hread (KERNEL32.@)
652 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
654 return _lread( hFile
, buffer
, count
);
658 /***********************************************************************
659 * _hwrite (KERNEL32.@)
661 * experimentation yields that _lwrite:
662 * o truncates the file at the current position with
664 * o returns 0 on a 0 length write
665 * o works with console handles
668 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
672 TRACE("%d %p %d\n", handle
, buffer
, count
);
676 /* Expand or truncate at current position */
677 if (!SetEndOfFile( LongToHandle(handle
) )) return HFILE_ERROR
;
680 if (!WriteFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
686 /***********************************************************************
687 * _lclose (KERNEL32.@)
689 HFILE WINAPI
_lclose( HFILE hFile
)
691 TRACE("handle %d\n", hFile
);
692 return CloseHandle( LongToHandle(hFile
) ) ? 0 : HFILE_ERROR
;
696 /***********************************************************************
697 * _lcreat (KERNEL32.@)
699 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
703 /* Mask off all flags not explicitly allowed by the doc */
704 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
705 TRACE("%s %02x\n", path
, attr
);
706 hfile
= CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
707 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
708 CREATE_ALWAYS
, attr
, 0 );
709 return HandleToLong(hfile
);
713 /***********************************************************************
714 * _lopen (KERNEL32.@)
716 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
720 TRACE("(%s,%04x)\n", debugstr_a(path
), mode
);
721 hfile
= create_file_OF( path
, mode
& ~OF_CREATE
);
722 return HandleToLong(hfile
);
725 /***********************************************************************
726 * _lread (KERNEL32.@)
728 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
731 if (!ReadFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
737 /***********************************************************************
738 * _llseek (KERNEL32.@)
740 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
742 return SetFilePointer( LongToHandle(hFile
), lOffset
, NULL
, nOrigin
);
746 /***********************************************************************
747 * _lwrite (KERNEL32.@)
749 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
751 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
755 /***********************************************************************
756 * FlushFileBuffers (KERNEL32.@)
758 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
761 IO_STATUS_BLOCK ioblk
;
763 if (is_console_handle( hFile
))
765 /* this will fail (as expected) for an output handle */
766 return FlushConsoleInputBuffer( hFile
);
768 nts
= NtFlushBuffersFile( hFile
, &ioblk
);
769 if (nts
!= STATUS_SUCCESS
)
771 SetLastError( RtlNtStatusToDosError( nts
) );
779 /***********************************************************************
780 * GetFileType (KERNEL32.@)
782 DWORD WINAPI
GetFileType( HANDLE hFile
)
784 FILE_FS_DEVICE_INFORMATION info
;
788 if (is_console_handle( hFile
)) return FILE_TYPE_CHAR
;
790 status
= NtQueryVolumeInformationFile( hFile
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
791 if (status
!= STATUS_SUCCESS
)
793 SetLastError( RtlNtStatusToDosError(status
) );
794 return FILE_TYPE_UNKNOWN
;
797 switch(info
.DeviceType
)
799 case FILE_DEVICE_NULL
:
800 case FILE_DEVICE_SERIAL_PORT
:
801 case FILE_DEVICE_PARALLEL_PORT
:
802 case FILE_DEVICE_TAPE
:
803 case FILE_DEVICE_UNKNOWN
:
804 return FILE_TYPE_CHAR
;
805 case FILE_DEVICE_NAMED_PIPE
:
806 return FILE_TYPE_PIPE
;
808 return FILE_TYPE_DISK
;
813 /***********************************************************************
814 * GetFileInformationByHandle (KERNEL32.@)
816 BOOL WINAPI
GetFileInformationByHandle( HANDLE hFile
, BY_HANDLE_FILE_INFORMATION
*info
)
818 FILE_ALL_INFORMATION all_info
;
822 status
= NtQueryInformationFile( hFile
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
823 if (status
== STATUS_BUFFER_OVERFLOW
) status
= STATUS_SUCCESS
;
824 if (status
== STATUS_SUCCESS
)
826 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
827 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
828 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
829 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
830 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
831 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
832 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
833 info
->dwVolumeSerialNumber
= 0; /* FIXME */
834 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
835 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
836 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
837 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
838 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
841 SetLastError( RtlNtStatusToDosError(status
) );
846 /***********************************************************************
847 * GetFileSize (KERNEL32.@)
849 * Retrieve the size of a file.
852 * hFile [I] File to retrieve size of.
853 * filesizehigh [O] On return, the high bits of the file size.
856 * Success: The low bits of the file size.
857 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
858 * check GetLastError() for values other than ERROR_SUCCESS.
860 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
863 if (!GetFileSizeEx( hFile
, &size
)) return INVALID_FILE_SIZE
;
864 if (filesizehigh
) *filesizehigh
= size
.u
.HighPart
;
865 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError(0);
866 return size
.u
.LowPart
;
870 /***********************************************************************
871 * GetFileSizeEx (KERNEL32.@)
873 * Retrieve the size of a file.
876 * hFile [I] File to retrieve size of.
877 * lpFileSIze [O] On return, the size of the file.
881 * Failure: FALSE, check GetLastError().
883 BOOL WINAPI
GetFileSizeEx( HANDLE hFile
, PLARGE_INTEGER lpFileSize
)
885 FILE_STANDARD_INFORMATION info
;
889 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileStandardInformation
);
890 if (status
== STATUS_SUCCESS
)
892 *lpFileSize
= info
.EndOfFile
;
895 SetLastError( RtlNtStatusToDosError(status
) );
900 /**************************************************************************
901 * SetEndOfFile (KERNEL32.@)
903 * Sets the current position as the end of the file.
906 * hFile [I] File handle.
910 * Failure: FALSE, check GetLastError().
912 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
914 FILE_POSITION_INFORMATION pos
;
915 FILE_END_OF_FILE_INFORMATION eof
;
919 status
= NtQueryInformationFile( hFile
, &io
, &pos
, sizeof(pos
), FilePositionInformation
);
920 if (status
== STATUS_SUCCESS
)
922 eof
.EndOfFile
= pos
.CurrentByteOffset
;
923 status
= NtSetInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
925 if (status
== STATUS_SUCCESS
) return TRUE
;
926 SetLastError( RtlNtStatusToDosError(status
) );
931 /***********************************************************************
932 * SetFilePointer (KERNEL32.@)
934 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
, DWORD method
)
936 LARGE_INTEGER dist
, newpos
;
940 dist
.u
.LowPart
= distance
;
941 dist
.u
.HighPart
= *highword
;
943 else dist
.QuadPart
= distance
;
945 if (!SetFilePointerEx( hFile
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
947 if (highword
) *highword
= newpos
.u
.HighPart
;
948 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
949 return newpos
.u
.LowPart
;
953 /***********************************************************************
954 * SetFilePointerEx (KERNEL32.@)
956 BOOL WINAPI
SetFilePointerEx( HANDLE hFile
, LARGE_INTEGER distance
,
957 LARGE_INTEGER
*newpos
, DWORD method
)
961 FILE_POSITION_INFORMATION info
;
966 pos
= distance
.QuadPart
;
969 if (NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
971 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
975 FILE_END_OF_FILE_INFORMATION eof
;
976 if (NtQueryInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
978 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
982 SetLastError( ERROR_INVALID_PARAMETER
);
988 SetLastError( ERROR_NEGATIVE_SEEK
);
992 info
.CurrentByteOffset
.QuadPart
= pos
;
993 if (NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
995 if (newpos
) newpos
->QuadPart
= pos
;
999 SetLastError( RtlNtStatusToDosError(io
.u
.Status
) );
1003 /***********************************************************************
1004 * GetFileTime (KERNEL32.@)
1006 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
1007 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
1009 FILE_BASIC_INFORMATION info
;
1013 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1014 if (status
== STATUS_SUCCESS
)
1018 lpCreationTime
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1019 lpCreationTime
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1021 if (lpLastAccessTime
)
1023 lpLastAccessTime
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1024 lpLastAccessTime
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1026 if (lpLastWriteTime
)
1028 lpLastWriteTime
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1029 lpLastWriteTime
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1033 SetLastError( RtlNtStatusToDosError(status
) );
1038 /***********************************************************************
1039 * SetFileTime (KERNEL32.@)
1041 BOOL WINAPI
SetFileTime( HANDLE hFile
, const FILETIME
*ctime
,
1042 const FILETIME
*atime
, const FILETIME
*mtime
)
1044 FILE_BASIC_INFORMATION info
;
1048 memset( &info
, 0, sizeof(info
) );
1051 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
1052 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
1056 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
1057 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
1061 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
1062 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
1065 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1066 if (status
== STATUS_SUCCESS
) return TRUE
;
1067 SetLastError( RtlNtStatusToDosError(status
) );
1072 /**************************************************************************
1073 * LockFile (KERNEL32.@)
1075 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1076 DWORD count_low
, DWORD count_high
)
1079 LARGE_INTEGER count
, offset
;
1081 TRACE( "%p %x%08x %x%08x\n",
1082 hFile
, offset_high
, offset_low
, count_high
, count_low
);
1084 count
.u
.LowPart
= count_low
;
1085 count
.u
.HighPart
= count_high
;
1086 offset
.u
.LowPart
= offset_low
;
1087 offset
.u
.HighPart
= offset_high
;
1089 status
= NtLockFile( hFile
, 0, NULL
, NULL
,
1090 NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
);
1092 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1097 /**************************************************************************
1098 * LockFileEx [KERNEL32.@]
1100 * Locks a byte range within an open file for shared or exclusive access.
1107 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1109 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1110 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
1113 LARGE_INTEGER count
, offset
;
1114 LPVOID cvalue
= NULL
;
1118 SetLastError( ERROR_INVALID_PARAMETER
);
1122 TRACE( "%p %x%08x %x%08x flags %x\n",
1123 hFile
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
,
1124 count_high
, count_low
, flags
);
1126 count
.u
.LowPart
= count_low
;
1127 count
.u
.HighPart
= count_high
;
1128 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
1129 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
1131 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1133 status
= NtLockFile( hFile
, overlapped
->hEvent
, NULL
, cvalue
,
1134 NULL
, &offset
, &count
, NULL
,
1135 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
1136 flags
& LOCKFILE_EXCLUSIVE_LOCK
);
1138 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1143 /**************************************************************************
1144 * UnlockFile (KERNEL32.@)
1146 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1147 DWORD count_low
, DWORD count_high
)
1150 LARGE_INTEGER count
, offset
;
1152 count
.u
.LowPart
= count_low
;
1153 count
.u
.HighPart
= count_high
;
1154 offset
.u
.LowPart
= offset_low
;
1155 offset
.u
.HighPart
= offset_high
;
1157 status
= NtUnlockFile( hFile
, NULL
, &offset
, &count
, NULL
);
1158 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1163 /**************************************************************************
1164 * UnlockFileEx (KERNEL32.@)
1166 BOOL WINAPI
UnlockFileEx( HANDLE hFile
, DWORD reserved
, DWORD count_low
, DWORD count_high
,
1167 LPOVERLAPPED overlapped
)
1171 SetLastError( ERROR_INVALID_PARAMETER
);
1174 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
1176 return UnlockFile( hFile
, overlapped
->u
.s
.Offset
, overlapped
->u
.s
.OffsetHigh
, count_low
, count_high
);
1180 /*************************************************************************
1181 * SetHandleCount (KERNEL32.@)
1183 UINT WINAPI
SetHandleCount( UINT count
)
1185 return min( 256, count
);
1189 /**************************************************************************
1190 * Operations on file names *
1191 **************************************************************************/
1194 /*************************************************************************
1195 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1197 * Creates or opens an object, and returns a handle that can be used to
1198 * access that object.
1202 * filename [in] pointer to filename to be accessed
1203 * access [in] access mode requested
1204 * sharing [in] share mode
1205 * sa [in] pointer to security attributes
1206 * creation [in] how to create the file
1207 * attributes [in] attributes for newly created file
1208 * template [in] handle to file with extended attributes to copy
1211 * Success: Open handle to specified file
1212 * Failure: INVALID_HANDLE_VALUE
1214 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
1215 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1216 DWORD attributes
, HANDLE
template )
1220 OBJECT_ATTRIBUTES attr
;
1221 UNICODE_STRING nameW
;
1225 const WCHAR
*vxd_name
= NULL
;
1226 static const WCHAR bkslashes_with_dotW
[] = {'\\','\\','.','\\',0};
1227 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
1228 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
1229 SECURITY_QUALITY_OF_SERVICE qos
;
1231 static const UINT nt_disposition
[5] =
1233 FILE_CREATE
, /* CREATE_NEW */
1234 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
1235 FILE_OPEN
, /* OPEN_EXISTING */
1236 FILE_OPEN_IF
, /* OPEN_ALWAYS */
1237 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
1243 if (!filename
|| !filename
[0])
1245 SetLastError( ERROR_PATH_NOT_FOUND
);
1246 return INVALID_HANDLE_VALUE
;
1249 TRACE("%s %s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
1250 (access
& GENERIC_READ
)?"GENERIC_READ ":"",
1251 (access
& GENERIC_WRITE
)?"GENERIC_WRITE ":"",
1252 (!access
)?"QUERY_ACCESS ":"",
1253 (sharing
& FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
1254 (sharing
& FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
1255 (sharing
& FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
1256 creation
, attributes
);
1258 /* Open a console for CONIN$ or CONOUT$ */
1260 if (!strcmpiW(filename
, coninW
) || !strcmpiW(filename
, conoutW
))
1262 ret
= OpenConsoleW(filename
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1266 if (!strncmpW(filename
, bkslashes_with_dotW
, 4))
1268 static const WCHAR pipeW
[] = {'P','I','P','E','\\',0};
1269 static const WCHAR mailslotW
[] = {'M','A','I','L','S','L','O','T','\\',0};
1271 if ((isalphaW(filename
[4]) && filename
[5] == ':' && filename
[6] == '\0') ||
1272 !strncmpiW( filename
+ 4, pipeW
, 5 ) ||
1273 !strncmpiW( filename
+ 4, mailslotW
, 9 ))
1277 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
1279 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
1281 else if (GetVersion() & 0x80000000)
1283 vxd_name
= filename
+ 4;
1286 else dosdev
= RtlIsDosDeviceName_U( filename
);
1290 static const WCHAR conW
[] = {'C','O','N'};
1292 if (LOWORD(dosdev
) == sizeof(conW
) &&
1293 !memicmpW( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), conW
, sizeof(conW
)/sizeof(WCHAR
)))
1295 switch (access
& (GENERIC_READ
|GENERIC_WRITE
))
1298 ret
= OpenConsoleW(coninW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1301 ret
= OpenConsoleW(conoutW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1304 SetLastError( ERROR_FILE_NOT_FOUND
);
1305 return INVALID_HANDLE_VALUE
;
1310 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
1312 SetLastError( ERROR_INVALID_PARAMETER
);
1313 return INVALID_HANDLE_VALUE
;
1316 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
1318 SetLastError( ERROR_PATH_NOT_FOUND
);
1319 return INVALID_HANDLE_VALUE
;
1322 /* now call NtCreateFile */
1325 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
1326 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
1328 options
|= FILE_NON_DIRECTORY_FILE
;
1329 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
1331 options
|= FILE_DELETE_ON_CLOSE
;
1334 if (attributes
& FILE_FLAG_NO_BUFFERING
)
1335 options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
1336 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
1337 options
|= FILE_SYNCHRONOUS_IO_ALERT
;
1338 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
1339 options
|= FILE_RANDOM_ACCESS
;
1340 attributes
&= FILE_ATTRIBUTE_VALID_FLAGS
;
1342 attr
.Length
= sizeof(attr
);
1343 attr
.RootDirectory
= 0;
1344 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1345 attr
.ObjectName
= &nameW
;
1346 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1347 if (attributes
& SECURITY_SQOS_PRESENT
)
1349 qos
.Length
= sizeof(qos
);
1350 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
1351 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
1352 qos
.EffectiveOnly
= attributes
& SECURITY_EFFECTIVE_ONLY
? TRUE
: FALSE
;
1353 attr
.SecurityQualityOfService
= &qos
;
1356 attr
.SecurityQualityOfService
= NULL
;
1358 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
1360 status
= NtCreateFile( &ret
, access
, &attr
, &io
, NULL
, attributes
,
1361 sharing
, nt_disposition
[creation
- CREATE_NEW
],
1365 if (vxd_name
&& vxd_name
[0])
1367 static HANDLE (*vxd_open
)(LPCWSTR
,DWORD
,SECURITY_ATTRIBUTES
*);
1368 if (!vxd_open
) vxd_open
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1369 "__wine_vxd_open" );
1370 if (vxd_open
&& (ret
= vxd_open( vxd_name
, access
, sa
))) goto done
;
1373 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
1374 ret
= INVALID_HANDLE_VALUE
;
1376 /* In the case file creation was rejected due to CREATE_NEW flag
1377 * was specified and file with that name already exists, correct
1378 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1379 * Note: RtlNtStatusToDosError is not the subject to blame here.
1381 if (status
== STATUS_OBJECT_NAME_COLLISION
)
1382 SetLastError( ERROR_FILE_EXISTS
);
1384 SetLastError( RtlNtStatusToDosError(status
) );
1388 if ((creation
== CREATE_ALWAYS
&& io
.Information
== FILE_OVERWRITTEN
) ||
1389 (creation
== OPEN_ALWAYS
&& io
.Information
== FILE_OPENED
))
1390 SetLastError( ERROR_ALREADY_EXISTS
);
1394 RtlFreeUnicodeString( &nameW
);
1397 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
1398 TRACE("returning %p\n", ret
);
1404 /*************************************************************************
1405 * CreateFileA (KERNEL32.@)
1409 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
1410 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1411 DWORD attributes
, HANDLE
template)
1415 if (!(nameW
= FILE_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1416 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
1420 /***********************************************************************
1421 * DeleteFileW (KERNEL32.@)
1426 * path [I] Path to the file to delete.
1430 * Failure: FALSE, check GetLastError().
1432 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1434 UNICODE_STRING nameW
;
1435 OBJECT_ATTRIBUTES attr
;
1440 TRACE("%s\n", debugstr_w(path
) );
1442 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
1444 SetLastError( ERROR_PATH_NOT_FOUND
);
1448 attr
.Length
= sizeof(attr
);
1449 attr
.RootDirectory
= 0;
1450 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1451 attr
.ObjectName
= &nameW
;
1452 attr
.SecurityDescriptor
= NULL
;
1453 attr
.SecurityQualityOfService
= NULL
;
1455 status
= NtCreateFile(&hFile
, GENERIC_READ
| GENERIC_WRITE
| DELETE
,
1456 &attr
, &io
, NULL
, 0,
1457 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1458 FILE_OPEN
, FILE_DELETE_ON_CLOSE
| FILE_NON_DIRECTORY_FILE
, NULL
, 0);
1459 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
1461 RtlFreeUnicodeString( &nameW
);
1464 SetLastError( RtlNtStatusToDosError(status
) );
1471 /***********************************************************************
1472 * DeleteFileA (KERNEL32.@)
1476 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1480 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1481 return DeleteFileW( pathW
);
1485 /**************************************************************************
1486 * ReplaceFileW (KERNEL32.@)
1487 * ReplaceFile (KERNEL32.@)
1489 BOOL WINAPI
ReplaceFileW(LPCWSTR lpReplacedFileName
, LPCWSTR lpReplacementFileName
,
1490 LPCWSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1491 LPVOID lpExclude
, LPVOID lpReserved
)
1493 UNICODE_STRING nt_replaced_name
, nt_replacement_name
;
1494 ANSI_STRING unix_replaced_name
, unix_replacement_name
, unix_backup_name
;
1495 HANDLE hReplaced
= NULL
, hReplacement
= NULL
, hBackup
= NULL
;
1496 DWORD error
= ERROR_SUCCESS
;
1497 UINT replaced_flags
;
1501 OBJECT_ATTRIBUTES attr
;
1504 FIXME("Ignoring flags %x\n", dwReplaceFlags
);
1506 /* First two arguments are mandatory */
1507 if (!lpReplacedFileName
|| !lpReplacementFileName
)
1509 SetLastError(ERROR_INVALID_PARAMETER
);
1513 unix_replaced_name
.Buffer
= NULL
;
1514 unix_replacement_name
.Buffer
= NULL
;
1515 unix_backup_name
.Buffer
= NULL
;
1517 attr
.Length
= sizeof(attr
);
1518 attr
.RootDirectory
= 0;
1519 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1520 attr
.ObjectName
= NULL
;
1521 attr
.SecurityDescriptor
= NULL
;
1522 attr
.SecurityQualityOfService
= NULL
;
1524 /* Open the "replaced" file for reading and writing */
1525 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName
, &nt_replaced_name
, NULL
, NULL
)))
1527 error
= ERROR_PATH_NOT_FOUND
;
1530 replaced_flags
= lpBackupFileName
? FILE_OPEN
: FILE_OPEN_IF
;
1531 attr
.ObjectName
= &nt_replaced_name
;
1532 status
= NtOpenFile(&hReplaced
, GENERIC_READ
|GENERIC_WRITE
|DELETE
|SYNCHRONIZE
,
1534 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
1535 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1536 if (status
== STATUS_SUCCESS
)
1537 status
= wine_nt_to_unix_file_name(&nt_replaced_name
, &unix_replaced_name
, replaced_flags
, FALSE
);
1538 RtlFreeUnicodeString(&nt_replaced_name
);
1539 if (status
!= STATUS_SUCCESS
)
1541 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1542 error
= ERROR_FILE_NOT_FOUND
;
1544 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1549 * Open the replacement file for reading, writing, and deleting
1550 * (writing and deleting are needed when finished)
1552 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName
, &nt_replacement_name
, NULL
, NULL
)))
1554 error
= ERROR_PATH_NOT_FOUND
;
1557 attr
.ObjectName
= &nt_replacement_name
;
1558 status
= NtOpenFile(&hReplacement
,
1559 GENERIC_READ
|GENERIC_WRITE
|DELETE
|WRITE_DAC
|SYNCHRONIZE
,
1561 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1562 if (status
== STATUS_SUCCESS
)
1563 status
= wine_nt_to_unix_file_name(&nt_replacement_name
, &unix_replacement_name
, FILE_OPEN
, FALSE
);
1564 RtlFreeUnicodeString(&nt_replacement_name
);
1565 if (status
!= STATUS_SUCCESS
)
1567 error
= RtlNtStatusToDosError(status
);
1571 /* If the user wants a backup then that needs to be performed first */
1572 if (lpBackupFileName
)
1574 UNICODE_STRING nt_backup_name
;
1575 FILE_BASIC_INFORMATION replaced_info
;
1577 /* Obtain the file attributes from the "replaced" file */
1578 status
= NtQueryInformationFile(hReplaced
, &io
, &replaced_info
,
1579 sizeof(replaced_info
),
1580 FileBasicInformation
);
1581 if (status
!= STATUS_SUCCESS
)
1583 error
= RtlNtStatusToDosError(status
);
1587 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName
, &nt_backup_name
, NULL
, NULL
)))
1589 error
= ERROR_PATH_NOT_FOUND
;
1592 attr
.ObjectName
= &nt_backup_name
;
1593 /* Open the backup with permissions to write over it */
1594 status
= NtCreateFile(&hBackup
, GENERIC_WRITE
,
1595 &attr
, &io
, NULL
, replaced_info
.FileAttributes
,
1596 FILE_SHARE_WRITE
, FILE_OPEN_IF
,
1597 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
,
1599 if (status
== STATUS_SUCCESS
)
1600 status
= wine_nt_to_unix_file_name(&nt_backup_name
, &unix_backup_name
, FILE_OPEN_IF
, FALSE
);
1601 RtlFreeUnicodeString(&nt_backup_name
);
1602 if (status
!= STATUS_SUCCESS
)
1604 error
= RtlNtStatusToDosError(status
);
1608 /* If an existing backup exists then copy over it */
1609 if (rename(unix_replaced_name
.Buffer
, unix_backup_name
.Buffer
) == -1)
1611 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
; /* is this correct? */
1617 * Now that the backup has been performed (if requested), copy the replacement
1620 if (rename(unix_replacement_name
.Buffer
, unix_replaced_name
.Buffer
) == -1)
1622 if (errno
== EACCES
)
1624 /* Inappropriate permissions on "replaced", rename will fail */
1625 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1628 /* on failure we need to indicate whether a backup was made */
1629 if (!lpBackupFileName
)
1630 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT
;
1632 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
;
1638 /* Perform resource cleanup */
1640 if (hBackup
) CloseHandle(hBackup
);
1641 if (hReplaced
) CloseHandle(hReplaced
);
1642 if (hReplacement
) CloseHandle(hReplacement
);
1643 RtlFreeAnsiString(&unix_backup_name
);
1644 RtlFreeAnsiString(&unix_replacement_name
);
1645 RtlFreeAnsiString(&unix_replaced_name
);
1647 /* If there was an error, set the error code */
1649 SetLastError(error
);
1654 /**************************************************************************
1655 * ReplaceFileA (KERNEL32.@)
1657 BOOL WINAPI
ReplaceFileA(LPCSTR lpReplacedFileName
,LPCSTR lpReplacementFileName
,
1658 LPCSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1659 LPVOID lpExclude
, LPVOID lpReserved
)
1661 WCHAR
*replacedW
, *replacementW
, *backupW
= NULL
;
1664 /* This function only makes sense when the first two parameters are defined */
1665 if (!lpReplacedFileName
|| !(replacedW
= FILE_name_AtoW( lpReplacedFileName
, TRUE
)))
1667 SetLastError(ERROR_INVALID_PARAMETER
);
1670 if (!lpReplacementFileName
|| !(replacementW
= FILE_name_AtoW( lpReplacementFileName
, TRUE
)))
1672 HeapFree( GetProcessHeap(), 0, replacedW
);
1673 SetLastError(ERROR_INVALID_PARAMETER
);
1676 /* The backup parameter, however, is optional */
1677 if (lpBackupFileName
)
1679 if (!(backupW
= FILE_name_AtoW( lpBackupFileName
, TRUE
)))
1681 HeapFree( GetProcessHeap(), 0, replacedW
);
1682 HeapFree( GetProcessHeap(), 0, replacementW
);
1683 SetLastError(ERROR_INVALID_PARAMETER
);
1687 ret
= ReplaceFileW( replacedW
, replacementW
, backupW
, dwReplaceFlags
, lpExclude
, lpReserved
);
1688 HeapFree( GetProcessHeap(), 0, replacedW
);
1689 HeapFree( GetProcessHeap(), 0, replacementW
);
1690 HeapFree( GetProcessHeap(), 0, backupW
);
1695 /*************************************************************************
1696 * FindFirstFileExW (KERNEL32.@)
1698 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1699 * results as FindExSearchNameMatch
1701 HANDLE WINAPI
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1702 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1703 LPVOID filter
, DWORD flags
)
1706 FIND_FIRST_INFO
*info
= NULL
;
1707 UNICODE_STRING nt_name
;
1708 OBJECT_ATTRIBUTES attr
;
1713 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1715 if ((search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1718 FIXME("options not implemented 0x%08x 0x%08x\n", search_op
, flags
);
1719 return INVALID_HANDLE_VALUE
;
1721 if (level
!= FindExInfoStandard
)
1723 FIXME("info level %d not implemented\n", level
);
1724 return INVALID_HANDLE_VALUE
;
1727 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1729 SetLastError( ERROR_PATH_NOT_FOUND
);
1730 return INVALID_HANDLE_VALUE
;
1733 if (!(info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
))))
1735 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1739 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1741 static const WCHAR dotW
[] = {'.',0};
1744 /* we still need to check that the directory can be opened */
1748 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1750 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1753 memcpy( dir
, filename
, HIWORD(device
) );
1754 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1756 RtlFreeUnicodeString( &nt_name
);
1757 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: dotW
, &nt_name
, &mask
, NULL
))
1759 HeapFree( GetProcessHeap(), 0, dir
);
1760 SetLastError( ERROR_PATH_NOT_FOUND
);
1763 HeapFree( GetProcessHeap(), 0, dir
);
1764 RtlInitUnicodeString( &info
->mask
, NULL
);
1766 else if (!mask
|| !*mask
)
1768 SetLastError( ERROR_FILE_NOT_FOUND
);
1773 if (!RtlCreateUnicodeString( &info
->mask
, mask
))
1775 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1779 /* truncate dir name before mask */
1781 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
1784 /* check if path is the root of the drive */
1785 info
->is_root
= FALSE
;
1786 p
= nt_name
.Buffer
+ 4; /* skip \??\ prefix */
1787 if (p
[0] && p
[1] == ':')
1790 while (*p
== '\\') p
++;
1791 info
->is_root
= (*p
== 0);
1794 attr
.Length
= sizeof(attr
);
1795 attr
.RootDirectory
= 0;
1796 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1797 attr
.ObjectName
= &nt_name
;
1798 attr
.SecurityDescriptor
= NULL
;
1799 attr
.SecurityQualityOfService
= NULL
;
1801 status
= NtOpenFile( &info
->handle
, GENERIC_READ
, &attr
, &io
,
1802 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1803 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1805 if (status
!= STATUS_SUCCESS
)
1807 RtlFreeUnicodeString( &info
->mask
);
1808 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1809 SetLastError( ERROR_PATH_NOT_FOUND
);
1811 SetLastError( RtlNtStatusToDosError(status
) );
1815 RtlInitializeCriticalSection( &info
->cs
);
1816 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
1817 info
->path
= nt_name
;
1818 info
->magic
= FIND_FIRST_MAGIC
;
1821 info
->search_op
= search_op
;
1825 WIN32_FIND_DATAW
*wfd
= data
;
1827 memset( wfd
, 0, sizeof(*wfd
) );
1828 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
1829 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1830 CloseHandle( info
->handle
);
1837 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1838 FileBothDirectoryInformation
, FALSE
, &info
->mask
, TRUE
);
1842 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1843 return INVALID_HANDLE_VALUE
;
1845 info
->data_len
= io
.Information
;
1846 if (!FindNextFileW( info
, data
))
1848 TRACE( "%s not found\n", debugstr_w(filename
) );
1850 SetLastError( ERROR_FILE_NOT_FOUND
);
1851 return INVALID_HANDLE_VALUE
;
1853 if (!strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1855 /* we can't find two files with the same name */
1856 CloseHandle( info
->handle
);
1863 HeapFree( GetProcessHeap(), 0, info
);
1864 RtlFreeUnicodeString( &nt_name
);
1865 return INVALID_HANDLE_VALUE
;
1869 /*************************************************************************
1870 * FindNextFileW (KERNEL32.@)
1872 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1874 FIND_FIRST_INFO
*info
;
1875 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1878 TRACE("%p %p\n", handle
, data
);
1880 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1882 SetLastError( ERROR_INVALID_HANDLE
);
1886 if (info
->magic
!= FIND_FIRST_MAGIC
)
1888 SetLastError( ERROR_INVALID_HANDLE
);
1892 RtlEnterCriticalSection( &info
->cs
);
1894 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
1897 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
1901 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1902 FileBothDirectoryInformation
, FALSE
, &info
->mask
, FALSE
);
1905 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1906 if (io
.u
.Status
== STATUS_NO_MORE_FILES
)
1908 CloseHandle( info
->handle
);
1913 info
->data_len
= io
.Information
;
1917 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
1919 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
1920 else info
->data_pos
= info
->data_len
;
1922 /* don't return '.' and '..' in the root of the drive */
1925 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
1926 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
1927 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
1930 /* check for dir symlink */
1931 if ((dir_info
->FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
1932 (dir_info
->FileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
) &&
1933 strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1935 if (!check_dir_symlink( info
, dir_info
)) continue;
1938 data
->dwFileAttributes
= dir_info
->FileAttributes
;
1939 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
1940 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
1941 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
1942 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
1943 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
1944 data
->dwReserved0
= 0;
1945 data
->dwReserved1
= 0;
1947 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
1948 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
1949 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
1950 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
1952 TRACE("returning %s (%s)\n",
1953 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
1959 RtlLeaveCriticalSection( &info
->cs
);
1964 /*************************************************************************
1965 * FindClose (KERNEL32.@)
1967 BOOL WINAPI
FindClose( HANDLE handle
)
1969 FIND_FIRST_INFO
*info
= handle
;
1971 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1973 SetLastError( ERROR_INVALID_HANDLE
);
1979 if (info
->magic
== FIND_FIRST_MAGIC
)
1981 RtlEnterCriticalSection( &info
->cs
);
1982 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
1985 if (info
->handle
) CloseHandle( info
->handle
);
1987 RtlFreeUnicodeString( &info
->mask
);
1988 info
->mask
.Buffer
= NULL
;
1989 RtlFreeUnicodeString( &info
->path
);
1992 RtlLeaveCriticalSection( &info
->cs
);
1993 info
->cs
.DebugInfo
->Spare
[0] = 0;
1994 RtlDeleteCriticalSection( &info
->cs
);
1995 HeapFree( GetProcessHeap(), 0, info
);
2001 WARN("Illegal handle %p\n", handle
);
2002 SetLastError( ERROR_INVALID_HANDLE
);
2011 /*************************************************************************
2012 * FindFirstFileA (KERNEL32.@)
2014 HANDLE WINAPI
FindFirstFileA( LPCSTR lpFileName
, WIN32_FIND_DATAA
*lpFindData
)
2016 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
2017 FindExSearchNameMatch
, NULL
, 0);
2020 /*************************************************************************
2021 * FindFirstFileExA (KERNEL32.@)
2023 HANDLE WINAPI
FindFirstFileExA( LPCSTR lpFileName
, FINDEX_INFO_LEVELS fInfoLevelId
,
2024 LPVOID lpFindFileData
, FINDEX_SEARCH_OPS fSearchOp
,
2025 LPVOID lpSearchFilter
, DWORD dwAdditionalFlags
)
2028 WIN32_FIND_DATAA
*dataA
;
2029 WIN32_FIND_DATAW dataW
;
2032 if (!(nameW
= FILE_name_AtoW( lpFileName
, FALSE
))) return INVALID_HANDLE_VALUE
;
2034 handle
= FindFirstFileExW(nameW
, fInfoLevelId
, &dataW
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
2035 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
2037 dataA
= lpFindFileData
;
2038 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
2039 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
2040 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2041 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2042 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2043 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
2044 FILE_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
2045 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
2046 sizeof(dataA
->cAlternateFileName
) );
2051 /*************************************************************************
2052 * FindFirstFileW (KERNEL32.@)
2054 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
2056 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
2057 FindExSearchNameMatch
, NULL
, 0);
2061 /*************************************************************************
2062 * FindNextFileA (KERNEL32.@)
2064 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
2066 WIN32_FIND_DATAW dataW
;
2068 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
2069 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
2070 data
->ftCreationTime
= dataW
.ftCreationTime
;
2071 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2072 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2073 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2074 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
2075 FILE_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
2076 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
2077 sizeof(data
->cAlternateFileName
) );
2082 /**************************************************************************
2083 * GetFileAttributesW (KERNEL32.@)
2085 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
2087 FILE_BASIC_INFORMATION info
;
2088 UNICODE_STRING nt_name
;
2089 OBJECT_ATTRIBUTES attr
;
2092 TRACE("%s\n", debugstr_w(name
));
2094 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2096 SetLastError( ERROR_PATH_NOT_FOUND
);
2097 return INVALID_FILE_ATTRIBUTES
;
2100 attr
.Length
= sizeof(attr
);
2101 attr
.RootDirectory
= 0;
2102 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2103 attr
.ObjectName
= &nt_name
;
2104 attr
.SecurityDescriptor
= NULL
;
2105 attr
.SecurityQualityOfService
= NULL
;
2107 status
= NtQueryAttributesFile( &attr
, &info
);
2108 RtlFreeUnicodeString( &nt_name
);
2110 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
2112 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2113 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
2115 SetLastError( RtlNtStatusToDosError(status
) );
2116 return INVALID_FILE_ATTRIBUTES
;
2120 /**************************************************************************
2121 * GetFileAttributesA (KERNEL32.@)
2123 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
2127 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
2128 return GetFileAttributesW( nameW
);
2132 /**************************************************************************
2133 * SetFileAttributesW (KERNEL32.@)
2135 BOOL WINAPI
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
2137 UNICODE_STRING nt_name
;
2138 OBJECT_ATTRIBUTES attr
;
2143 TRACE("%s %x\n", debugstr_w(name
), attributes
);
2145 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2147 SetLastError( ERROR_PATH_NOT_FOUND
);
2151 attr
.Length
= sizeof(attr
);
2152 attr
.RootDirectory
= 0;
2153 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2154 attr
.ObjectName
= &nt_name
;
2155 attr
.SecurityDescriptor
= NULL
;
2156 attr
.SecurityQualityOfService
= NULL
;
2158 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2159 RtlFreeUnicodeString( &nt_name
);
2161 if (status
== STATUS_SUCCESS
)
2163 FILE_BASIC_INFORMATION info
;
2165 memset( &info
, 0, sizeof(info
) );
2166 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2167 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2171 if (status
== STATUS_SUCCESS
) return TRUE
;
2172 SetLastError( RtlNtStatusToDosError(status
) );
2177 /**************************************************************************
2178 * SetFileAttributesA (KERNEL32.@)
2180 BOOL WINAPI
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2184 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2185 return SetFileAttributesW( nameW
, attributes
);
2189 /**************************************************************************
2190 * GetFileAttributesExW (KERNEL32.@)
2192 BOOL WINAPI
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2194 FILE_NETWORK_OPEN_INFORMATION info
;
2195 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
2196 UNICODE_STRING nt_name
;
2197 OBJECT_ATTRIBUTES attr
;
2200 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
2202 if (level
!= GetFileExInfoStandard
)
2204 SetLastError( ERROR_INVALID_PARAMETER
);
2208 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2210 SetLastError( ERROR_PATH_NOT_FOUND
);
2214 attr
.Length
= sizeof(attr
);
2215 attr
.RootDirectory
= 0;
2216 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2217 attr
.ObjectName
= &nt_name
;
2218 attr
.SecurityDescriptor
= NULL
;
2219 attr
.SecurityQualityOfService
= NULL
;
2221 status
= NtQueryFullAttributesFile( &attr
, &info
);
2222 RtlFreeUnicodeString( &nt_name
);
2224 if (status
!= STATUS_SUCCESS
)
2226 SetLastError( RtlNtStatusToDosError(status
) );
2230 data
->dwFileAttributes
= info
.FileAttributes
;
2231 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
2232 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
2233 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
2234 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
2235 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
2236 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
2237 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
2238 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
2243 /**************************************************************************
2244 * GetFileAttributesExA (KERNEL32.@)
2246 BOOL WINAPI
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2250 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2251 return GetFileAttributesExW( nameW
, level
, ptr
);
2255 /******************************************************************************
2256 * GetCompressedFileSizeW (KERNEL32.@)
2258 * Get the actual number of bytes used on disk.
2261 * Success: Low-order doubleword of number of bytes
2262 * Failure: INVALID_FILE_SIZE
2264 DWORD WINAPI
GetCompressedFileSizeW(
2265 LPCWSTR name
, /* [in] Pointer to name of file */
2266 LPDWORD size_high
) /* [out] Receives high-order doubleword of size */
2268 UNICODE_STRING nt_name
;
2269 OBJECT_ATTRIBUTES attr
;
2273 DWORD ret
= INVALID_FILE_SIZE
;
2275 TRACE("%s %p\n", debugstr_w(name
), size_high
);
2277 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2279 SetLastError( ERROR_PATH_NOT_FOUND
);
2280 return INVALID_FILE_SIZE
;
2283 attr
.Length
= sizeof(attr
);
2284 attr
.RootDirectory
= 0;
2285 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2286 attr
.ObjectName
= &nt_name
;
2287 attr
.SecurityDescriptor
= NULL
;
2288 attr
.SecurityQualityOfService
= NULL
;
2290 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2291 RtlFreeUnicodeString( &nt_name
);
2293 if (status
== STATUS_SUCCESS
)
2295 /* we don't support compressed files, simply return the file size */
2296 ret
= GetFileSize( handle
, size_high
);
2299 else SetLastError( RtlNtStatusToDosError(status
) );
2305 /******************************************************************************
2306 * GetCompressedFileSizeA (KERNEL32.@)
2308 * See GetCompressedFileSizeW.
2310 DWORD WINAPI
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
2314 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
2315 return GetCompressedFileSizeW( nameW
, size_high
);
2319 /***********************************************************************
2320 * OpenVxDHandle (KERNEL32.@)
2322 * This function is supposed to return the corresponding Ring 0
2323 * ("kernel") handle for a Ring 3 handle in Win9x.
2324 * Evidently, Wine will have problems with this. But we try anyway,
2327 HANDLE WINAPI
OpenVxDHandle(HANDLE hHandleRing3
)
2329 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3
);
2330 return hHandleRing3
;
2334 /****************************************************************************
2335 * DeviceIoControl (KERNEL32.@)
2337 BOOL WINAPI
DeviceIoControl(HANDLE hDevice
, DWORD dwIoControlCode
,
2338 LPVOID lpvInBuffer
, DWORD cbInBuffer
,
2339 LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
2340 LPDWORD lpcbBytesReturned
,
2341 LPOVERLAPPED lpOverlapped
)
2345 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2346 hDevice
,dwIoControlCode
,lpvInBuffer
,cbInBuffer
,
2347 lpvOutBuffer
,cbOutBuffer
,lpcbBytesReturned
,lpOverlapped
);
2349 /* Check if this is a user defined control code for a VxD */
2351 if (HIWORD( dwIoControlCode
) == 0 && (GetVersion() & 0x80000000))
2353 typedef BOOL (WINAPI
*DeviceIoProc
)(DWORD
, LPVOID
, DWORD
, LPVOID
, DWORD
, LPDWORD
, LPOVERLAPPED
);
2354 static DeviceIoProc (*vxd_get_proc
)(HANDLE
);
2355 DeviceIoProc proc
= NULL
;
2357 if (!vxd_get_proc
) vxd_get_proc
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2358 "__wine_vxd_get_proc" );
2359 if (vxd_get_proc
) proc
= vxd_get_proc( hDevice
);
2360 if (proc
) return proc( dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2361 lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
);
2364 /* Not a VxD, let ntdll handle it */
2368 LPVOID cvalue
= ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) ? NULL
: lpOverlapped
;
2369 lpOverlapped
->Internal
= STATUS_PENDING
;
2370 lpOverlapped
->InternalHigh
= 0;
2371 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2372 status
= NtFsControlFile(hDevice
, lpOverlapped
->hEvent
,
2373 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2374 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2375 lpvOutBuffer
, cbOutBuffer
);
2377 status
= NtDeviceIoControlFile(hDevice
, lpOverlapped
->hEvent
,
2378 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2379 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2380 lpvOutBuffer
, cbOutBuffer
);
2381 if (lpcbBytesReturned
) *lpcbBytesReturned
= lpOverlapped
->InternalHigh
;
2385 IO_STATUS_BLOCK iosb
;
2387 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2388 status
= NtFsControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2389 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2390 lpvOutBuffer
, cbOutBuffer
);
2392 status
= NtDeviceIoControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2393 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2394 lpvOutBuffer
, cbOutBuffer
);
2395 if (lpcbBytesReturned
) *lpcbBytesReturned
= iosb
.Information
;
2397 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2402 /***********************************************************************
2403 * OpenFile (KERNEL32.@)
2405 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
2409 WORD filedatetime
[2];
2411 if (!ofs
) return HFILE_ERROR
;
2413 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
2414 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
2415 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
2416 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
2417 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
2418 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
2419 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
2420 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
2421 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
2422 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
2423 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
2424 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
2425 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
2426 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
2427 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
2428 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
2429 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
2430 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
2434 ofs
->cBytes
= sizeof(OFSTRUCT
);
2436 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
2438 if (!name
) return HFILE_ERROR
;
2440 TRACE("%s %04x\n", name
, mode
);
2442 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2443 Are there any cases where getting the path here is wrong?
2444 Uwe Bonnes 1997 Apr 2 */
2445 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
)) goto error
;
2447 /* OF_PARSE simply fills the structure */
2449 if (mode
& OF_PARSE
)
2451 ofs
->fFixedDisk
= (GetDriveTypeA( ofs
->szPathName
) != DRIVE_REMOVABLE
);
2452 TRACE("(%s): OF_PARSE, res = '%s'\n", name
, ofs
->szPathName
);
2456 /* OF_CREATE is completely different from all other options, so
2459 if (mode
& OF_CREATE
)
2461 if ((handle
= create_file_OF( name
, mode
)) == INVALID_HANDLE_VALUE
)
2466 /* Now look for the file */
2468 if (!SearchPathA( NULL
, name
, NULL
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
))
2471 TRACE("found %s\n", debugstr_a(ofs
->szPathName
) );
2473 if (mode
& OF_DELETE
)
2475 if (!DeleteFileA( ofs
->szPathName
)) goto error
;
2476 TRACE("(%s): OF_DELETE return = OK\n", name
);
2480 handle
= LongToHandle(_lopen( ofs
->szPathName
, mode
));
2481 if (handle
== INVALID_HANDLE_VALUE
) goto error
;
2483 GetFileTime( handle
, NULL
, NULL
, &filetime
);
2484 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
2485 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
2487 if (ofs
->Reserved1
!= filedatetime
[0] || ofs
->Reserved2
!= filedatetime
[1] )
2489 CloseHandle( handle
);
2490 WARN("(%s): OF_VERIFY failed\n", name
);
2491 /* FIXME: what error here? */
2492 SetLastError( ERROR_FILE_NOT_FOUND
);
2496 ofs
->Reserved1
= filedatetime
[0];
2497 ofs
->Reserved2
= filedatetime
[1];
2499 TRACE("(%s): OK, return = %p\n", name
, handle
);
2500 if (mode
& OF_EXIST
) /* Return TRUE instead of a handle */
2502 CloseHandle( handle
);
2505 return HandleToLong(handle
);
2507 error
: /* We get here if there was an error opening the file */
2508 ofs
->nErrCode
= GetLastError();
2509 WARN("(%s): return = HFILE_ERROR error= %d\n", name
,ofs
->nErrCode
);