2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
36 #define WIN32_NO_STATUS
42 #include "wine/winbase16.h"
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 HANDLE dos_handles
[DOS_TABLE_SIZE
];
53 /* info structure for FindFirstFile handle */
56 DWORD magic
; /* magic number */
57 HANDLE handle
; /* handle to directory */
58 CRITICAL_SECTION cs
; /* crit section protecting this structure */
59 FINDEX_SEARCH_OPS search_op
; /* Flags passed to FindFirst. */
60 UNICODE_STRING mask
; /* file mask */
61 UNICODE_STRING path
; /* NT path used to open the directory */
62 BOOL is_root
; /* is directory the root of the drive? */
63 UINT data_pos
; /* current position in dir data */
64 UINT data_len
; /* length of dir data */
65 BYTE data
[8192]; /* directory data */
68 #define FIND_FIRST_MAGIC 0xc0ffee11
70 static BOOL oem_file_apis
;
72 static const WCHAR wildcardsW
[] = { '*','?',0 };
74 /***********************************************************************
77 * Wrapper for CreateFile that takes OF_* mode flags.
79 static HANDLE
create_file_OF( LPCSTR path
, INT mode
)
81 DWORD access
, sharing
, creation
;
85 creation
= CREATE_ALWAYS
;
86 access
= GENERIC_READ
| GENERIC_WRITE
;
90 creation
= OPEN_EXISTING
;
93 case OF_READ
: access
= GENERIC_READ
; break;
94 case OF_WRITE
: access
= GENERIC_WRITE
; break;
95 case OF_READWRITE
: access
= GENERIC_READ
| GENERIC_WRITE
; break;
96 default: access
= 0; break;
102 case OF_SHARE_EXCLUSIVE
: sharing
= 0; break;
103 case OF_SHARE_DENY_WRITE
: sharing
= FILE_SHARE_READ
; break;
104 case OF_SHARE_DENY_READ
: sharing
= FILE_SHARE_WRITE
; break;
105 case OF_SHARE_DENY_NONE
:
106 case OF_SHARE_COMPAT
:
107 default: sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
109 return CreateFileA( path
, access
, sharing
, NULL
, creation
, FILE_ATTRIBUTE_NORMAL
, 0 );
113 /***********************************************************************
116 * Check if a dir symlink should be returned by FindNextFile.
118 static BOOL
check_dir_symlink( FIND_FIRST_INFO
*info
, const FILE_BOTH_DIR_INFORMATION
*file_info
)
121 ANSI_STRING unix_name
;
122 struct stat st
, parent_st
;
126 str
.MaximumLength
= info
->path
.Length
+ sizeof(WCHAR
) + file_info
->FileNameLength
;
127 if (!(str
.Buffer
= HeapAlloc( GetProcessHeap(), 0, str
.MaximumLength
))) return TRUE
;
128 memcpy( str
.Buffer
, info
->path
.Buffer
, info
->path
.Length
);
129 len
= info
->path
.Length
/ sizeof(WCHAR
);
130 if (!len
|| str
.Buffer
[len
-1] != '\\') str
.Buffer
[len
++] = '\\';
131 memcpy( str
.Buffer
+ len
, file_info
->FileName
, file_info
->FileNameLength
);
132 str
.Length
= len
* sizeof(WCHAR
) + file_info
->FileNameLength
;
134 unix_name
.Buffer
= NULL
;
135 if (!wine_nt_to_unix_file_name( &str
, &unix_name
, OPEN_EXISTING
, FALSE
) &&
136 !stat( unix_name
.Buffer
, &st
))
138 char *p
= unix_name
.Buffer
+ unix_name
.Length
- 1;
140 /* skip trailing slashes */
141 while (p
> unix_name
.Buffer
&& *p
== '/') p
--;
143 while (ret
&& p
> unix_name
.Buffer
)
145 while (p
> unix_name
.Buffer
&& *p
!= '/') p
--;
146 while (p
> unix_name
.Buffer
&& *p
== '/') p
--;
148 if (!stat( unix_name
.Buffer
, &parent_st
) &&
149 parent_st
.st_dev
== st
.st_dev
&&
150 parent_st
.st_ino
== st
.st_ino
)
152 WARN( "suppressing dir symlink %s pointing to parent %s\n",
153 debugstr_wn( str
.Buffer
, str
.Length
/sizeof(WCHAR
) ),
154 debugstr_a( unix_name
.Buffer
));
159 RtlFreeAnsiString( &unix_name
);
160 RtlFreeUnicodeString( &str
);
165 /***********************************************************************
168 * Set the DOS error code from errno.
170 void FILE_SetDosError(void)
172 int save_errno
= errno
; /* errno gets overwritten by printf */
174 TRACE("errno = %d %s\n", errno
, strerror(errno
));
178 SetLastError( ERROR_SHARING_VIOLATION
);
181 SetLastError( ERROR_INVALID_HANDLE
);
184 SetLastError( ERROR_HANDLE_DISK_FULL
);
189 SetLastError( ERROR_ACCESS_DENIED
);
192 SetLastError( ERROR_LOCK_VIOLATION
);
195 SetLastError( ERROR_FILE_NOT_FOUND
);
198 SetLastError( ERROR_CANNOT_MAKE
);
202 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
205 SetLastError( ERROR_FILE_EXISTS
);
209 SetLastError( ERROR_SEEK
);
212 SetLastError( ERROR_DIR_NOT_EMPTY
);
215 SetLastError( ERROR_BAD_FORMAT
);
218 SetLastError( ERROR_PATH_NOT_FOUND
);
221 SetLastError( ERROR_NOT_SAME_DEVICE
);
224 WARN("unknown file error: %s\n", strerror(save_errno
) );
225 SetLastError( ERROR_GEN_FAILURE
);
232 /***********************************************************************
235 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
237 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
238 * there is no possibility for the function to do that twice, taking into
239 * account any called function.
241 WCHAR
*FILE_name_AtoW( LPCSTR name
, BOOL alloc
)
244 UNICODE_STRING strW
, *pstrW
;
247 RtlInitAnsiString( &str
, name
);
248 pstrW
= alloc
? &strW
: &NtCurrentTeb()->StaticUnicodeString
;
250 status
= RtlOemStringToUnicodeString( pstrW
, &str
, alloc
);
252 status
= RtlAnsiStringToUnicodeString( pstrW
, &str
, alloc
);
253 if (status
== STATUS_SUCCESS
) return pstrW
->Buffer
;
255 if (status
== STATUS_BUFFER_OVERFLOW
)
256 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
258 SetLastError( RtlNtStatusToDosError(status
) );
263 /***********************************************************************
266 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
268 DWORD
FILE_name_WtoA( LPCWSTR src
, INT srclen
, LPSTR dest
, INT destlen
)
272 if (srclen
< 0) srclen
= strlenW( src
) + 1;
274 RtlUnicodeToOemN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
276 RtlUnicodeToMultiByteN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
281 /**************************************************************************
282 * SetFileApisToOEM (KERNEL32.@)
284 VOID WINAPI
SetFileApisToOEM(void)
286 oem_file_apis
= TRUE
;
290 /**************************************************************************
291 * SetFileApisToANSI (KERNEL32.@)
293 VOID WINAPI
SetFileApisToANSI(void)
295 oem_file_apis
= FALSE
;
299 /******************************************************************************
300 * AreFileApisANSI (KERNEL32.@)
302 * Determines if file functions are using ANSI
305 * TRUE: Set of file functions is using ANSI code page
306 * FALSE: Set of file functions is using OEM code page
308 BOOL WINAPI
AreFileApisANSI(void)
310 return !oem_file_apis
;
314 /**************************************************************************
315 * Operations on file handles *
316 **************************************************************************/
318 /***********************************************************************
319 * FILE_InitProcessDosHandles
321 * Allocates the default DOS handles for a process. Called either by
322 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
324 static void FILE_InitProcessDosHandles( void )
326 static BOOL init_done
/* = FALSE */;
327 HANDLE cp
= GetCurrentProcess();
329 if (init_done
) return;
331 DuplicateHandle(cp
, GetStdHandle(STD_INPUT_HANDLE
), cp
, &dos_handles
[0],
332 0, TRUE
, DUPLICATE_SAME_ACCESS
);
333 DuplicateHandle(cp
, GetStdHandle(STD_OUTPUT_HANDLE
), cp
, &dos_handles
[1],
334 0, TRUE
, DUPLICATE_SAME_ACCESS
);
335 DuplicateHandle(cp
, GetStdHandle(STD_ERROR_HANDLE
), cp
, &dos_handles
[2],
336 0, TRUE
, DUPLICATE_SAME_ACCESS
);
337 DuplicateHandle(cp
, GetStdHandle(STD_ERROR_HANDLE
), cp
, &dos_handles
[3],
338 0, TRUE
, DUPLICATE_SAME_ACCESS
);
339 DuplicateHandle(cp
, GetStdHandle(STD_ERROR_HANDLE
), cp
, &dos_handles
[4],
340 0, TRUE
, DUPLICATE_SAME_ACCESS
);
344 /******************************************************************
345 * FILE_ReadWriteApc (internal)
347 static void WINAPI
FILE_ReadWriteApc(void* apc_user
, PIO_STATUS_BLOCK io_status
, ULONG reserved
)
349 LPOVERLAPPED_COMPLETION_ROUTINE cr
= (LPOVERLAPPED_COMPLETION_ROUTINE
)apc_user
;
351 cr(RtlNtStatusToDosError(io_status
->u
.Status
), io_status
->Information
, (LPOVERLAPPED
)io_status
);
355 /***********************************************************************
356 * ReadFileEx (KERNEL32.@)
358 BOOL WINAPI
ReadFileEx(HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
359 LPOVERLAPPED overlapped
,
360 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
362 LARGE_INTEGER offset
;
364 PIO_STATUS_BLOCK io_status
;
366 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile
, buffer
, bytesToRead
, overlapped
, lpCompletionRoutine
);
370 SetLastError(ERROR_INVALID_PARAMETER
);
374 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
375 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
376 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
377 io_status
->u
.Status
= STATUS_PENDING
;
378 io_status
->Information
= 0;
380 status
= NtReadFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
381 io_status
, buffer
, bytesToRead
, &offset
, NULL
);
385 SetLastError( RtlNtStatusToDosError(status
) );
392 /***********************************************************************
393 * ReadFileScatter (KERNEL32.@)
395 BOOL WINAPI
ReadFileScatter( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
396 LPDWORD reserved
, LPOVERLAPPED overlapped
)
398 PIO_STATUS_BLOCK io_status
;
399 LARGE_INTEGER offset
;
402 TRACE( "(%p %p %u %p)\n", file
, segments
, count
, overlapped
);
404 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
405 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
406 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
407 io_status
->u
.Status
= STATUS_PENDING
;
408 io_status
->Information
= 0;
410 status
= NtReadFileScatter( file
, NULL
, NULL
, NULL
, io_status
, segments
, count
, &offset
, NULL
);
411 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
416 /***********************************************************************
417 * ReadFile (KERNEL32.@)
419 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
420 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
422 LARGE_INTEGER offset
;
423 PLARGE_INTEGER poffset
= NULL
;
424 IO_STATUS_BLOCK iosb
;
425 PIO_STATUS_BLOCK io_status
= &iosb
;
428 LPVOID cvalue
= NULL
;
430 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToRead
,
431 bytesRead
, overlapped
);
433 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
434 if (!bytesToRead
) return TRUE
;
436 if (is_console_handle(hFile
))
437 return ReadConsoleA(hFile
, buffer
, bytesToRead
, bytesRead
, NULL
);
439 if (overlapped
!= NULL
)
441 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
442 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
444 hEvent
= overlapped
->hEvent
;
445 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
446 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
448 io_status
->u
.Status
= STATUS_PENDING
;
449 io_status
->Information
= 0;
451 status
= NtReadFile(hFile
, hEvent
, NULL
, cvalue
, io_status
, buffer
, bytesToRead
, poffset
, NULL
);
453 if (status
== STATUS_PENDING
&& !overlapped
)
455 WaitForSingleObject( hFile
, INFINITE
);
456 status
= io_status
->u
.Status
;
459 if (status
!= STATUS_PENDING
&& bytesRead
)
460 *bytesRead
= io_status
->Information
;
462 if (status
&& status
!= STATUS_END_OF_FILE
&& status
!= STATUS_TIMEOUT
)
464 SetLastError( RtlNtStatusToDosError(status
) );
471 /***********************************************************************
472 * WriteFileEx (KERNEL32.@)
474 BOOL WINAPI
WriteFileEx(HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
475 LPOVERLAPPED overlapped
,
476 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
478 LARGE_INTEGER offset
;
480 PIO_STATUS_BLOCK io_status
;
482 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, overlapped
, lpCompletionRoutine
);
484 if (overlapped
== NULL
)
486 SetLastError(ERROR_INVALID_PARAMETER
);
489 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
490 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
492 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
493 io_status
->u
.Status
= STATUS_PENDING
;
494 io_status
->Information
= 0;
496 status
= NtWriteFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
497 io_status
, buffer
, bytesToWrite
, &offset
, NULL
);
499 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
504 /***********************************************************************
505 * WriteFileGather (KERNEL32.@)
507 BOOL WINAPI
WriteFileGather( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
508 LPDWORD reserved
, LPOVERLAPPED overlapped
)
510 PIO_STATUS_BLOCK io_status
;
511 LARGE_INTEGER offset
;
514 TRACE( "%p %p %u %p\n", file
, segments
, count
, overlapped
);
516 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
517 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
518 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
519 io_status
->u
.Status
= STATUS_PENDING
;
520 io_status
->Information
= 0;
522 status
= NtWriteFileGather( file
, NULL
, NULL
, NULL
, io_status
, segments
, count
, &offset
, NULL
);
523 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
528 /***********************************************************************
529 * WriteFile (KERNEL32.@)
531 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
532 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
534 HANDLE hEvent
= NULL
;
535 LARGE_INTEGER offset
;
536 PLARGE_INTEGER poffset
= NULL
;
538 IO_STATUS_BLOCK iosb
;
539 PIO_STATUS_BLOCK piosb
= &iosb
;
540 LPVOID cvalue
= NULL
;
542 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, bytesWritten
, overlapped
);
544 if (is_console_handle(hFile
))
545 return WriteConsoleA(hFile
, buffer
, bytesToWrite
, bytesWritten
, NULL
);
549 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
550 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
552 hEvent
= overlapped
->hEvent
;
553 piosb
= (PIO_STATUS_BLOCK
)overlapped
;
554 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
556 piosb
->u
.Status
= STATUS_PENDING
;
557 piosb
->Information
= 0;
559 status
= NtWriteFile(hFile
, hEvent
, NULL
, cvalue
, piosb
,
560 buffer
, bytesToWrite
, poffset
, NULL
);
562 /* FIXME: NtWriteFile does not always cause page faults, generate them now */
563 if (status
== STATUS_INVALID_USER_BUFFER
&& !IsBadReadPtr( buffer
, bytesToWrite
))
565 status
= NtWriteFile(hFile
, hEvent
, NULL
, cvalue
, piosb
,
566 buffer
, bytesToWrite
, poffset
, NULL
);
567 if (status
!= STATUS_INVALID_USER_BUFFER
)
568 FIXME("Could not access memory (%p,%d) at first, now OK. Protected by DIBSection code?\n",
569 buffer
, bytesToWrite
);
572 if (status
== STATUS_PENDING
&& !overlapped
)
574 WaitForSingleObject( hFile
, INFINITE
);
575 status
= piosb
->u
.Status
;
578 if (status
!= STATUS_PENDING
&& bytesWritten
)
579 *bytesWritten
= piosb
->Information
;
581 if (status
&& status
!= STATUS_TIMEOUT
)
583 SetLastError( RtlNtStatusToDosError(status
) );
590 /***********************************************************************
591 * GetOverlappedResult (KERNEL32.@)
593 * Check the result of an Asynchronous data transfer from a file.
596 * HANDLE hFile [in] handle of file to check on
597 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
598 * LPDWORD lpTransferred [in/out] number of bytes transferred
599 * BOOL bWait [in] wait for the transfer to complete ?
605 * If successful (and relevant) lpTransferred will hold the number of
606 * bytes transferred during the async operation.
608 BOOL WINAPI
GetOverlappedResult(HANDLE hFile
, LPOVERLAPPED lpOverlapped
,
609 LPDWORD lpTransferred
, BOOL bWait
)
613 TRACE( "(%p %p %p %x)\n", hFile
, lpOverlapped
, lpTransferred
, bWait
);
615 if ( lpOverlapped
== NULL
)
617 ERR("lpOverlapped was null\n");
621 status
= lpOverlapped
->Internal
;
622 if (status
== STATUS_PENDING
)
626 SetLastError( ERROR_IO_INCOMPLETE
);
630 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: hFile
,
631 INFINITE
) == WAIT_FAILED
)
633 status
= lpOverlapped
->Internal
;
636 if (lpTransferred
) *lpTransferred
= lpOverlapped
->InternalHigh
;
638 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
642 /***********************************************************************
643 * CancelIo (KERNEL32.@)
645 * Cancels pending I/O operations initiated by the current thread on a file.
648 * handle [I] File handle.
652 * Failure: FALSE, check GetLastError().
654 BOOL WINAPI
CancelIo(HANDLE handle
)
656 IO_STATUS_BLOCK io_status
;
658 NtCancelIoFile(handle
, &io_status
);
659 if (io_status
.u
.Status
)
661 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
667 /***********************************************************************
668 * _hread (KERNEL32.@)
670 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
672 return _lread( hFile
, buffer
, count
);
676 /***********************************************************************
677 * _hwrite (KERNEL32.@)
679 * experimentation yields that _lwrite:
680 * o truncates the file at the current position with
682 * o returns 0 on a 0 length write
683 * o works with console handles
686 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
690 TRACE("%d %p %d\n", handle
, buffer
, count
);
694 /* Expand or truncate at current position */
695 if (!SetEndOfFile( LongToHandle(handle
) )) return HFILE_ERROR
;
698 if (!WriteFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
704 /***********************************************************************
705 * _lclose (KERNEL32.@)
707 HFILE WINAPI
_lclose( HFILE hFile
)
709 TRACE("handle %d\n", hFile
);
710 return CloseHandle( LongToHandle(hFile
) ) ? 0 : HFILE_ERROR
;
714 /***********************************************************************
715 * _lcreat (KERNEL32.@)
717 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
721 /* Mask off all flags not explicitly allowed by the doc */
722 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
723 TRACE("%s %02x\n", path
, attr
);
724 hfile
= CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
725 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
726 CREATE_ALWAYS
, attr
, 0 );
727 return HandleToLong(hfile
);
731 /***********************************************************************
732 * _lopen (KERNEL32.@)
734 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
738 TRACE("(%s,%04x)\n", debugstr_a(path
), mode
);
739 hfile
= create_file_OF( path
, mode
& ~OF_CREATE
);
740 return HandleToLong(hfile
);
743 /***********************************************************************
744 * _lread (KERNEL32.@)
746 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
749 if (!ReadFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
755 /***********************************************************************
756 * _llseek (KERNEL32.@)
758 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
760 return SetFilePointer( LongToHandle(hFile
), lOffset
, NULL
, nOrigin
);
764 /***********************************************************************
765 * _lwrite (KERNEL32.@)
767 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
769 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
773 /***********************************************************************
774 * FlushFileBuffers (KERNEL32.@)
776 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
779 IO_STATUS_BLOCK ioblk
;
781 if (is_console_handle( hFile
))
783 /* this will fail (as expected) for an output handle */
784 return FlushConsoleInputBuffer( hFile
);
786 nts
= NtFlushBuffersFile( hFile
, &ioblk
);
787 if (nts
!= STATUS_SUCCESS
)
789 SetLastError( RtlNtStatusToDosError( nts
) );
797 /***********************************************************************
798 * GetFileType (KERNEL32.@)
800 DWORD WINAPI
GetFileType( HANDLE hFile
)
802 FILE_FS_DEVICE_INFORMATION info
;
806 if (is_console_handle( hFile
)) return FILE_TYPE_CHAR
;
808 status
= NtQueryVolumeInformationFile( hFile
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
809 if (status
!= STATUS_SUCCESS
)
811 SetLastError( RtlNtStatusToDosError(status
) );
812 return FILE_TYPE_UNKNOWN
;
815 switch(info
.DeviceType
)
817 case FILE_DEVICE_NULL
:
818 case FILE_DEVICE_SERIAL_PORT
:
819 case FILE_DEVICE_PARALLEL_PORT
:
820 case FILE_DEVICE_TAPE
:
821 case FILE_DEVICE_UNKNOWN
:
822 return FILE_TYPE_CHAR
;
823 case FILE_DEVICE_NAMED_PIPE
:
824 return FILE_TYPE_PIPE
;
826 return FILE_TYPE_DISK
;
831 /***********************************************************************
832 * GetFileInformationByHandle (KERNEL32.@)
834 BOOL WINAPI
GetFileInformationByHandle( HANDLE hFile
, BY_HANDLE_FILE_INFORMATION
*info
)
836 FILE_ALL_INFORMATION all_info
;
840 status
= NtQueryInformationFile( hFile
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
841 if (status
== STATUS_SUCCESS
)
843 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
844 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
845 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
846 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
847 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
848 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
849 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
850 info
->dwVolumeSerialNumber
= 0; /* FIXME */
851 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
852 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
853 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
854 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
855 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
858 SetLastError( RtlNtStatusToDosError(status
) );
863 /***********************************************************************
864 * GetFileSize (KERNEL32.@)
866 * Retrieve the size of a file.
869 * hFile [I] File to retrieve size of.
870 * filesizehigh [O] On return, the high bits of the file size.
873 * Success: The low bits of the file size.
874 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
875 * check GetLastError() for values other than ERROR_SUCCESS.
877 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
880 if (!GetFileSizeEx( hFile
, &size
)) return INVALID_FILE_SIZE
;
881 if (filesizehigh
) *filesizehigh
= size
.u
.HighPart
;
882 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError(0);
883 return size
.u
.LowPart
;
887 /***********************************************************************
888 * GetFileSizeEx (KERNEL32.@)
890 * Retrieve the size of a file.
893 * hFile [I] File to retrieve size of.
894 * lpFileSIze [O] On return, the size of the file.
898 * Failure: FALSE, check GetLastError().
900 BOOL WINAPI
GetFileSizeEx( HANDLE hFile
, PLARGE_INTEGER lpFileSize
)
902 FILE_END_OF_FILE_INFORMATION info
;
906 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileEndOfFileInformation
);
907 if (status
== STATUS_SUCCESS
)
909 *lpFileSize
= info
.EndOfFile
;
912 SetLastError( RtlNtStatusToDosError(status
) );
917 /**************************************************************************
918 * SetEndOfFile (KERNEL32.@)
920 * Sets the current position as the end of the file.
923 * hFile [I] File handle.
927 * Failure: FALSE, check GetLastError().
929 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
931 FILE_POSITION_INFORMATION pos
;
932 FILE_END_OF_FILE_INFORMATION eof
;
936 status
= NtQueryInformationFile( hFile
, &io
, &pos
, sizeof(pos
), FilePositionInformation
);
937 if (status
== STATUS_SUCCESS
)
939 eof
.EndOfFile
= pos
.CurrentByteOffset
;
940 status
= NtSetInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
942 if (status
== STATUS_SUCCESS
) return TRUE
;
943 SetLastError( RtlNtStatusToDosError(status
) );
948 /***********************************************************************
949 * SetFilePointer (KERNEL32.@)
951 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
, DWORD method
)
953 LARGE_INTEGER dist
, newpos
;
957 dist
.u
.LowPart
= distance
;
958 dist
.u
.HighPart
= *highword
;
960 else dist
.QuadPart
= distance
;
962 if (!SetFilePointerEx( hFile
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
964 if (highword
) *highword
= newpos
.u
.HighPart
;
965 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
966 return newpos
.u
.LowPart
;
970 /***********************************************************************
971 * SetFilePointerEx (KERNEL32.@)
973 BOOL WINAPI
SetFilePointerEx( HANDLE hFile
, LARGE_INTEGER distance
,
974 LARGE_INTEGER
*newpos
, DWORD method
)
978 FILE_POSITION_INFORMATION info
;
983 pos
= distance
.QuadPart
;
986 if (NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
988 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
992 FILE_END_OF_FILE_INFORMATION eof
;
993 if (NtQueryInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
995 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
999 SetLastError( ERROR_INVALID_PARAMETER
);
1005 SetLastError( ERROR_NEGATIVE_SEEK
);
1009 info
.CurrentByteOffset
.QuadPart
= pos
;
1010 if (NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
1012 if (newpos
) newpos
->QuadPart
= pos
;
1016 SetLastError( RtlNtStatusToDosError(io
.u
.Status
) );
1020 /***********************************************************************
1021 * GetFileTime (KERNEL32.@)
1023 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
1024 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
1026 FILE_BASIC_INFORMATION info
;
1030 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1031 if (status
== STATUS_SUCCESS
)
1035 lpCreationTime
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1036 lpCreationTime
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1038 if (lpLastAccessTime
)
1040 lpLastAccessTime
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1041 lpLastAccessTime
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1043 if (lpLastWriteTime
)
1045 lpLastWriteTime
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1046 lpLastWriteTime
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1050 SetLastError( RtlNtStatusToDosError(status
) );
1055 /***********************************************************************
1056 * SetFileTime (KERNEL32.@)
1058 BOOL WINAPI
SetFileTime( HANDLE hFile
, const FILETIME
*ctime
,
1059 const FILETIME
*atime
, const FILETIME
*mtime
)
1061 FILE_BASIC_INFORMATION info
;
1065 memset( &info
, 0, sizeof(info
) );
1068 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
1069 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
1073 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
1074 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
1078 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
1079 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
1082 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1083 if (status
== STATUS_SUCCESS
) return TRUE
;
1084 SetLastError( RtlNtStatusToDosError(status
) );
1089 /**************************************************************************
1090 * LockFile (KERNEL32.@)
1092 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1093 DWORD count_low
, DWORD count_high
)
1096 LARGE_INTEGER count
, offset
;
1098 TRACE( "%p %x%08x %x%08x\n",
1099 hFile
, offset_high
, offset_low
, count_high
, count_low
);
1101 count
.u
.LowPart
= count_low
;
1102 count
.u
.HighPart
= count_high
;
1103 offset
.u
.LowPart
= offset_low
;
1104 offset
.u
.HighPart
= offset_high
;
1106 status
= NtLockFile( hFile
, 0, NULL
, NULL
,
1107 NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
);
1109 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1114 /**************************************************************************
1115 * LockFileEx [KERNEL32.@]
1117 * Locks a byte range within an open file for shared or exclusive access.
1124 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1126 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1127 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
1130 LARGE_INTEGER count
, offset
;
1131 LPVOID cvalue
= NULL
;
1135 SetLastError( ERROR_INVALID_PARAMETER
);
1139 TRACE( "%p %x%08x %x%08x flags %x\n",
1140 hFile
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
,
1141 count_high
, count_low
, flags
);
1143 count
.u
.LowPart
= count_low
;
1144 count
.u
.HighPart
= count_high
;
1145 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
1146 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
1148 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1150 status
= NtLockFile( hFile
, overlapped
->hEvent
, NULL
, cvalue
,
1151 NULL
, &offset
, &count
, NULL
,
1152 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
1153 flags
& LOCKFILE_EXCLUSIVE_LOCK
);
1155 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1160 /**************************************************************************
1161 * UnlockFile (KERNEL32.@)
1163 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1164 DWORD count_low
, DWORD count_high
)
1167 LARGE_INTEGER count
, offset
;
1169 count
.u
.LowPart
= count_low
;
1170 count
.u
.HighPart
= count_high
;
1171 offset
.u
.LowPart
= offset_low
;
1172 offset
.u
.HighPart
= offset_high
;
1174 status
= NtUnlockFile( hFile
, NULL
, &offset
, &count
, NULL
);
1175 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1180 /**************************************************************************
1181 * UnlockFileEx (KERNEL32.@)
1183 BOOL WINAPI
UnlockFileEx( HANDLE hFile
, DWORD reserved
, DWORD count_low
, DWORD count_high
,
1184 LPOVERLAPPED overlapped
)
1188 SetLastError( ERROR_INVALID_PARAMETER
);
1191 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
1193 return UnlockFile( hFile
, overlapped
->u
.s
.Offset
, overlapped
->u
.s
.OffsetHigh
, count_low
, count_high
);
1197 /***********************************************************************
1198 * Win32HandleToDosFileHandle (KERNEL32.21)
1200 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1201 * longer valid after this function (even on failure).
1203 * Note: this is not exactly right, since on Win95 the Win32 handles
1204 * are on top of DOS handles and we do it the other way
1205 * around. Should be good enough though.
1207 HFILE WINAPI
Win32HandleToDosFileHandle( HANDLE handle
)
1211 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
1214 FILE_InitProcessDosHandles();
1215 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
1216 if (!dos_handles
[i
])
1218 dos_handles
[i
] = handle
;
1219 TRACE("Got %d for h32 %p\n", i
, handle
);
1222 CloseHandle( handle
);
1223 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
1228 /***********************************************************************
1229 * DosFileHandleToWin32Handle (KERNEL32.20)
1231 * Return the Win32 handle for a DOS handle.
1233 * Note: this is not exactly right, since on Win95 the Win32 handles
1234 * are on top of DOS handles and we do it the other way
1235 * around. Should be good enough though.
1237 HANDLE WINAPI
DosFileHandleToWin32Handle( HFILE handle
)
1239 HFILE16 hfile
= (HFILE16
)handle
;
1240 if (hfile
< 5) FILE_InitProcessDosHandles();
1241 if ((hfile
>= DOS_TABLE_SIZE
) || !dos_handles
[hfile
])
1243 SetLastError( ERROR_INVALID_HANDLE
);
1244 return INVALID_HANDLE_VALUE
;
1246 return dos_handles
[hfile
];
1250 /*************************************************************************
1251 * SetHandleCount (KERNEL32.@)
1253 UINT WINAPI
SetHandleCount( UINT count
)
1255 return min( 256, count
);
1259 /***********************************************************************
1260 * DisposeLZ32Handle (KERNEL32.22)
1262 * Note: this is not entirely correct, we should only close the
1263 * 32-bit handle and not the 16-bit one, but we cannot do
1264 * this because of the way our DOS handles are implemented.
1265 * It shouldn't break anything though.
1267 void WINAPI
DisposeLZ32Handle( HANDLE handle
)
1271 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
)) return;
1273 for (i
= 5; i
< DOS_TABLE_SIZE
; i
++)
1274 if (dos_handles
[i
] == handle
)
1277 CloseHandle( handle
);
1282 /**************************************************************************
1283 * Operations on file names *
1284 **************************************************************************/
1287 /*************************************************************************
1288 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1290 * Creates or opens an object, and returns a handle that can be used to
1291 * access that object.
1295 * filename [in] pointer to filename to be accessed
1296 * access [in] access mode requested
1297 * sharing [in] share mode
1298 * sa [in] pointer to security attributes
1299 * creation [in] how to create the file
1300 * attributes [in] attributes for newly created file
1301 * template [in] handle to file with extended attributes to copy
1304 * Success: Open handle to specified file
1305 * Failure: INVALID_HANDLE_VALUE
1307 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
1308 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1309 DWORD attributes
, HANDLE
template )
1313 OBJECT_ATTRIBUTES attr
;
1314 UNICODE_STRING nameW
;
1318 static const WCHAR bkslashes_with_dotW
[] = {'\\','\\','.','\\',0};
1319 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
1320 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
1321 SECURITY_QUALITY_OF_SERVICE qos
;
1323 static const UINT nt_disposition
[5] =
1325 FILE_CREATE
, /* CREATE_NEW */
1326 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
1327 FILE_OPEN
, /* OPEN_EXISTING */
1328 FILE_OPEN_IF
, /* OPEN_ALWAYS */
1329 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
1335 if (!filename
|| !filename
[0])
1337 SetLastError( ERROR_PATH_NOT_FOUND
);
1338 return INVALID_HANDLE_VALUE
;
1341 TRACE("%s %s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
1342 (access
& GENERIC_READ
)?"GENERIC_READ ":"",
1343 (access
& GENERIC_WRITE
)?"GENERIC_WRITE ":"",
1344 (!access
)?"QUERY_ACCESS ":"",
1345 (sharing
& FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
1346 (sharing
& FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
1347 (sharing
& FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
1348 creation
, attributes
);
1350 /* Open a console for CONIN$ or CONOUT$ */
1352 if (!strcmpiW(filename
, coninW
) || !strcmpiW(filename
, conoutW
))
1354 ret
= OpenConsoleW(filename
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1358 if (!strncmpW(filename
, bkslashes_with_dotW
, 4))
1360 static const WCHAR pipeW
[] = {'P','I','P','E','\\',0};
1361 static const WCHAR mailslotW
[] = {'M','A','I','L','S','L','O','T','\\',0};
1363 if ((isalphaW(filename
[4]) && filename
[5] == ':' && filename
[6] == '\0') ||
1364 !strncmpiW( filename
+ 4, pipeW
, 5 ) ||
1365 !strncmpiW( filename
+ 4, mailslotW
, 9 ))
1369 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
1371 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
1373 else if (!(GetVersion() & 0x80000000))
1377 else if (filename
[4])
1379 ret
= VXD_Open( filename
+4, access
, sa
);
1384 SetLastError( ERROR_INVALID_NAME
);
1385 return INVALID_HANDLE_VALUE
;
1388 else dosdev
= RtlIsDosDeviceName_U( filename
);
1392 static const WCHAR conW
[] = {'C','O','N'};
1394 if (LOWORD(dosdev
) == sizeof(conW
) &&
1395 !memicmpW( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), conW
, sizeof(conW
)/sizeof(WCHAR
)))
1397 switch (access
& (GENERIC_READ
|GENERIC_WRITE
))
1400 ret
= OpenConsoleW(coninW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1403 ret
= OpenConsoleW(conoutW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1406 SetLastError( ERROR_FILE_NOT_FOUND
);
1407 return INVALID_HANDLE_VALUE
;
1412 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
1414 SetLastError( ERROR_INVALID_PARAMETER
);
1415 return INVALID_HANDLE_VALUE
;
1418 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
1420 SetLastError( ERROR_PATH_NOT_FOUND
);
1421 return INVALID_HANDLE_VALUE
;
1424 /* now call NtCreateFile */
1427 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
1428 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
1430 options
|= FILE_NON_DIRECTORY_FILE
;
1431 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
1433 options
|= FILE_DELETE_ON_CLOSE
;
1436 if (attributes
& FILE_FLAG_NO_BUFFERING
)
1437 options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
1438 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
1439 options
|= FILE_SYNCHRONOUS_IO_ALERT
;
1440 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
1441 options
|= FILE_RANDOM_ACCESS
;
1442 attributes
&= FILE_ATTRIBUTE_VALID_FLAGS
;
1444 attr
.Length
= sizeof(attr
);
1445 attr
.RootDirectory
= 0;
1446 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1447 attr
.ObjectName
= &nameW
;
1448 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1449 if (attributes
& SECURITY_SQOS_PRESENT
)
1451 qos
.Length
= sizeof(qos
);
1452 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
1453 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
1454 qos
.EffectiveOnly
= attributes
& SECURITY_EFFECTIVE_ONLY
? TRUE
: FALSE
;
1455 attr
.SecurityQualityOfService
= &qos
;
1458 attr
.SecurityQualityOfService
= NULL
;
1460 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
1462 status
= NtCreateFile( &ret
, access
, &attr
, &io
, NULL
, attributes
,
1463 sharing
, nt_disposition
[creation
- CREATE_NEW
],
1467 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
1468 ret
= INVALID_HANDLE_VALUE
;
1470 /* In the case file creation was rejected due to CREATE_NEW flag
1471 * was specified and file with that name already exists, correct
1472 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1473 * Note: RtlNtStatusToDosError is not the subject to blame here.
1475 if (status
== STATUS_OBJECT_NAME_COLLISION
)
1476 SetLastError( ERROR_FILE_EXISTS
);
1478 SetLastError( RtlNtStatusToDosError(status
) );
1482 if ((creation
== CREATE_ALWAYS
&& io
.Information
== FILE_OVERWRITTEN
) ||
1483 (creation
== OPEN_ALWAYS
&& io
.Information
== FILE_OPENED
))
1484 SetLastError( ERROR_ALREADY_EXISTS
);
1488 RtlFreeUnicodeString( &nameW
);
1491 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
1492 TRACE("returning %p\n", ret
);
1498 /*************************************************************************
1499 * CreateFileA (KERNEL32.@)
1503 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
1504 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1505 DWORD attributes
, HANDLE
template)
1509 if (!(nameW
= FILE_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1510 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
1514 /***********************************************************************
1515 * DeleteFileW (KERNEL32.@)
1520 * path [I] Path to the file to delete.
1524 * Failure: FALSE, check GetLastError().
1526 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1528 UNICODE_STRING nameW
;
1529 OBJECT_ATTRIBUTES attr
;
1532 TRACE("%s\n", debugstr_w(path
) );
1534 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
1536 SetLastError( ERROR_PATH_NOT_FOUND
);
1540 attr
.Length
= sizeof(attr
);
1541 attr
.RootDirectory
= 0;
1542 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1543 attr
.ObjectName
= &nameW
;
1544 attr
.SecurityDescriptor
= NULL
;
1545 attr
.SecurityQualityOfService
= NULL
;
1547 status
= NtDeleteFile(&attr
);
1548 RtlFreeUnicodeString( &nameW
);
1551 SetLastError( RtlNtStatusToDosError(status
) );
1558 /***********************************************************************
1559 * DeleteFileA (KERNEL32.@)
1563 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1567 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1568 return DeleteFileW( pathW
);
1572 /**************************************************************************
1573 * ReplaceFileW (KERNEL32.@)
1574 * ReplaceFile (KERNEL32.@)
1576 BOOL WINAPI
ReplaceFileW(LPCWSTR lpReplacedFileName
, LPCWSTR lpReplacementFileName
,
1577 LPCWSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1578 LPVOID lpExclude
, LPVOID lpReserved
)
1580 UNICODE_STRING nt_replaced_name
, nt_replacement_name
;
1581 ANSI_STRING unix_replaced_name
, unix_replacement_name
, unix_backup_name
;
1582 HANDLE hReplaced
= NULL
, hReplacement
= NULL
, hBackup
= NULL
;
1583 DWORD error
= ERROR_SUCCESS
;
1584 UINT replaced_flags
;
1588 OBJECT_ATTRIBUTES attr
;
1591 FIXME("Ignoring flags %x\n", dwReplaceFlags
);
1593 /* First two arguments are mandatory */
1594 if (!lpReplacedFileName
|| !lpReplacementFileName
)
1596 SetLastError(ERROR_INVALID_PARAMETER
);
1600 unix_replaced_name
.Buffer
= NULL
;
1601 unix_replacement_name
.Buffer
= NULL
;
1602 unix_backup_name
.Buffer
= NULL
;
1604 attr
.Length
= sizeof(attr
);
1605 attr
.RootDirectory
= 0;
1606 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1607 attr
.ObjectName
= NULL
;
1608 attr
.SecurityDescriptor
= NULL
;
1609 attr
.SecurityQualityOfService
= NULL
;
1611 /* Open the "replaced" file for reading and writing */
1612 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName
, &nt_replaced_name
, NULL
, NULL
)))
1614 error
= ERROR_PATH_NOT_FOUND
;
1617 replaced_flags
= lpBackupFileName
? FILE_OPEN
: FILE_OPEN_IF
;
1618 attr
.ObjectName
= &nt_replaced_name
;
1619 status
= NtOpenFile(&hReplaced
, GENERIC_READ
|GENERIC_WRITE
|DELETE
|SYNCHRONIZE
,
1621 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
1622 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1623 if (status
== STATUS_SUCCESS
)
1624 status
= wine_nt_to_unix_file_name(&nt_replaced_name
, &unix_replaced_name
, replaced_flags
, FALSE
);
1625 RtlFreeUnicodeString(&nt_replaced_name
);
1626 if (status
!= STATUS_SUCCESS
)
1628 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1629 error
= ERROR_FILE_NOT_FOUND
;
1631 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1636 * Open the replacement file for reading, writing, and deleting
1637 * (writing and deleting are needed when finished)
1639 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName
, &nt_replacement_name
, NULL
, NULL
)))
1641 error
= ERROR_PATH_NOT_FOUND
;
1644 attr
.ObjectName
= &nt_replacement_name
;
1645 status
= NtOpenFile(&hReplacement
,
1646 GENERIC_READ
|GENERIC_WRITE
|DELETE
|WRITE_DAC
|SYNCHRONIZE
,
1648 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1649 if (status
== STATUS_SUCCESS
)
1650 status
= wine_nt_to_unix_file_name(&nt_replacement_name
, &unix_replacement_name
, FILE_OPEN
, FALSE
);
1651 RtlFreeUnicodeString(&nt_replacement_name
);
1652 if (status
!= STATUS_SUCCESS
)
1654 error
= RtlNtStatusToDosError(status
);
1658 /* If the user wants a backup then that needs to be performed first */
1659 if (lpBackupFileName
)
1661 UNICODE_STRING nt_backup_name
;
1662 FILE_BASIC_INFORMATION replaced_info
;
1664 /* Obtain the file attributes from the "replaced" file */
1665 status
= NtQueryInformationFile(hReplaced
, &io
, &replaced_info
,
1666 sizeof(replaced_info
),
1667 FileBasicInformation
);
1668 if (status
!= STATUS_SUCCESS
)
1670 error
= RtlNtStatusToDosError(status
);
1674 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName
, &nt_backup_name
, NULL
, NULL
)))
1676 error
= ERROR_PATH_NOT_FOUND
;
1679 attr
.ObjectName
= &nt_backup_name
;
1680 /* Open the backup with permissions to write over it */
1681 status
= NtCreateFile(&hBackup
, GENERIC_WRITE
,
1682 &attr
, &io
, NULL
, replaced_info
.FileAttributes
,
1683 FILE_SHARE_WRITE
, FILE_OPEN_IF
,
1684 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
,
1686 if (status
== STATUS_SUCCESS
)
1687 status
= wine_nt_to_unix_file_name(&nt_backup_name
, &unix_backup_name
, FILE_OPEN_IF
, FALSE
);
1688 if (status
!= STATUS_SUCCESS
)
1690 error
= RtlNtStatusToDosError(status
);
1694 /* If an existing backup exists then copy over it */
1695 if (rename(unix_replaced_name
.Buffer
, unix_backup_name
.Buffer
) == -1)
1697 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
; /* is this correct? */
1703 * Now that the backup has been performed (if requested), copy the replacement
1706 if (rename(unix_replacement_name
.Buffer
, unix_replaced_name
.Buffer
) == -1)
1708 if (errno
== EACCES
)
1710 /* Inappropriate permissions on "replaced", rename will fail */
1711 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1714 /* on failure we need to indicate whether a backup was made */
1715 if (!lpBackupFileName
)
1716 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT
;
1718 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
;
1724 /* Perform resource cleanup */
1726 if (hBackup
) CloseHandle(hBackup
);
1727 if (hReplaced
) CloseHandle(hReplaced
);
1728 if (hReplacement
) CloseHandle(hReplacement
);
1729 RtlFreeAnsiString(&unix_backup_name
);
1730 RtlFreeAnsiString(&unix_replacement_name
);
1731 RtlFreeAnsiString(&unix_replaced_name
);
1733 /* If there was an error, set the error code */
1735 SetLastError(error
);
1740 /**************************************************************************
1741 * ReplaceFileA (KERNEL32.@)
1743 BOOL WINAPI
ReplaceFileA(LPCSTR lpReplacedFileName
,LPCSTR lpReplacementFileName
,
1744 LPCSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1745 LPVOID lpExclude
, LPVOID lpReserved
)
1747 WCHAR
*replacedW
, *replacementW
, *backupW
= NULL
;
1750 /* This function only makes sense when the first two parameters are defined */
1751 if (!lpReplacedFileName
|| !(replacedW
= FILE_name_AtoW( lpReplacedFileName
, TRUE
)))
1753 SetLastError(ERROR_INVALID_PARAMETER
);
1756 if (!lpReplacementFileName
|| !(replacementW
= FILE_name_AtoW( lpReplacementFileName
, TRUE
)))
1758 HeapFree( GetProcessHeap(), 0, replacedW
);
1759 SetLastError(ERROR_INVALID_PARAMETER
);
1762 /* The backup parameter, however, is optional */
1763 if (lpBackupFileName
)
1765 if (!(backupW
= FILE_name_AtoW( lpBackupFileName
, TRUE
)))
1767 HeapFree( GetProcessHeap(), 0, replacedW
);
1768 HeapFree( GetProcessHeap(), 0, replacementW
);
1769 SetLastError(ERROR_INVALID_PARAMETER
);
1773 ret
= ReplaceFileW( replacedW
, replacementW
, backupW
, dwReplaceFlags
, lpExclude
, lpReserved
);
1774 HeapFree( GetProcessHeap(), 0, replacedW
);
1775 HeapFree( GetProcessHeap(), 0, replacementW
);
1776 HeapFree( GetProcessHeap(), 0, backupW
);
1781 /*************************************************************************
1782 * FindFirstFileExW (KERNEL32.@)
1784 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1785 * results as FindExSearchNameMatch
1787 HANDLE WINAPI
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1788 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1789 LPVOID filter
, DWORD flags
)
1792 FIND_FIRST_INFO
*info
= NULL
;
1793 UNICODE_STRING nt_name
;
1794 OBJECT_ATTRIBUTES attr
;
1799 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1801 if ((search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1804 FIXME("options not implemented 0x%08x 0x%08x\n", search_op
, flags
);
1805 return INVALID_HANDLE_VALUE
;
1807 if (level
!= FindExInfoStandard
)
1809 FIXME("info level %d not implemented\n", level
);
1810 return INVALID_HANDLE_VALUE
;
1813 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1815 SetLastError( ERROR_PATH_NOT_FOUND
);
1816 return INVALID_HANDLE_VALUE
;
1819 if (!(info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
))))
1821 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1825 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1827 static const WCHAR dotW
[] = {'.',0};
1830 /* we still need to check that the directory can be opened */
1834 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1836 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1839 memcpy( dir
, filename
, HIWORD(device
) );
1840 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1842 RtlFreeUnicodeString( &nt_name
);
1843 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: dotW
, &nt_name
, &mask
, NULL
))
1845 HeapFree( GetProcessHeap(), 0, dir
);
1846 SetLastError( ERROR_PATH_NOT_FOUND
);
1849 HeapFree( GetProcessHeap(), 0, dir
);
1850 RtlInitUnicodeString( &info
->mask
, NULL
);
1852 else if (!mask
|| !*mask
)
1854 SetLastError( ERROR_FILE_NOT_FOUND
);
1859 if (!RtlCreateUnicodeString( &info
->mask
, mask
))
1861 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1865 /* truncate dir name before mask */
1867 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
1870 /* check if path is the root of the drive */
1871 info
->is_root
= FALSE
;
1872 p
= nt_name
.Buffer
+ 4; /* skip \??\ prefix */
1873 if (p
[0] && p
[1] == ':')
1876 while (*p
== '\\') p
++;
1877 info
->is_root
= (*p
== 0);
1880 attr
.Length
= sizeof(attr
);
1881 attr
.RootDirectory
= 0;
1882 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1883 attr
.ObjectName
= &nt_name
;
1884 attr
.SecurityDescriptor
= NULL
;
1885 attr
.SecurityQualityOfService
= NULL
;
1887 status
= NtOpenFile( &info
->handle
, GENERIC_READ
, &attr
, &io
,
1888 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1889 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1891 if (status
!= STATUS_SUCCESS
)
1893 RtlFreeUnicodeString( &info
->mask
);
1894 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1895 SetLastError( ERROR_PATH_NOT_FOUND
);
1897 SetLastError( RtlNtStatusToDosError(status
) );
1901 RtlInitializeCriticalSection( &info
->cs
);
1902 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
1903 info
->path
= nt_name
;
1904 info
->magic
= FIND_FIRST_MAGIC
;
1907 info
->search_op
= search_op
;
1911 WIN32_FIND_DATAW
*wfd
= data
;
1913 memset( wfd
, 0, sizeof(*wfd
) );
1914 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
1915 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1916 CloseHandle( info
->handle
);
1923 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1924 FileBothDirectoryInformation
, FALSE
, &info
->mask
, TRUE
);
1928 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1929 return INVALID_HANDLE_VALUE
;
1931 info
->data_len
= io
.Information
;
1932 if (!FindNextFileW( info
, data
))
1934 TRACE( "%s not found\n", debugstr_w(filename
) );
1936 SetLastError( ERROR_FILE_NOT_FOUND
);
1937 return INVALID_HANDLE_VALUE
;
1939 if (!strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1941 /* we can't find two files with the same name */
1942 CloseHandle( info
->handle
);
1949 HeapFree( GetProcessHeap(), 0, info
);
1950 RtlFreeUnicodeString( &nt_name
);
1951 return INVALID_HANDLE_VALUE
;
1955 /*************************************************************************
1956 * FindNextFileW (KERNEL32.@)
1958 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1960 FIND_FIRST_INFO
*info
;
1961 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1964 TRACE("%p %p\n", handle
, data
);
1966 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1968 SetLastError( ERROR_INVALID_HANDLE
);
1971 info
= (FIND_FIRST_INFO
*)handle
;
1972 if (info
->magic
!= FIND_FIRST_MAGIC
)
1974 SetLastError( ERROR_INVALID_HANDLE
);
1978 RtlEnterCriticalSection( &info
->cs
);
1980 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
1983 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
1987 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1988 FileBothDirectoryInformation
, FALSE
, &info
->mask
, FALSE
);
1991 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1992 if (io
.u
.Status
== STATUS_NO_MORE_FILES
)
1994 CloseHandle( info
->handle
);
1999 info
->data_len
= io
.Information
;
2003 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
2005 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
2006 else info
->data_pos
= info
->data_len
;
2008 /* don't return '.' and '..' in the root of the drive */
2011 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
2012 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
2013 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
2016 /* check for dir symlink */
2017 if ((dir_info
->FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
2018 (dir_info
->FileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
) &&
2019 strpbrkW( info
->mask
.Buffer
, wildcardsW
))
2021 if (!check_dir_symlink( info
, dir_info
)) continue;
2024 data
->dwFileAttributes
= dir_info
->FileAttributes
;
2025 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
2026 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
2027 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
2028 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
2029 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
2030 data
->dwReserved0
= 0;
2031 data
->dwReserved1
= 0;
2033 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
2034 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
2035 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
2036 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
2038 TRACE("returning %s (%s)\n",
2039 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
2045 RtlLeaveCriticalSection( &info
->cs
);
2050 /*************************************************************************
2051 * FindClose (KERNEL32.@)
2053 BOOL WINAPI
FindClose( HANDLE handle
)
2055 FIND_FIRST_INFO
*info
= (FIND_FIRST_INFO
*)handle
;
2057 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
2059 SetLastError( ERROR_INVALID_HANDLE
);
2065 if (info
->magic
== FIND_FIRST_MAGIC
)
2067 RtlEnterCriticalSection( &info
->cs
);
2068 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
2071 if (info
->handle
) CloseHandle( info
->handle
);
2073 RtlFreeUnicodeString( &info
->mask
);
2074 info
->mask
.Buffer
= NULL
;
2075 RtlFreeUnicodeString( &info
->path
);
2078 RtlLeaveCriticalSection( &info
->cs
);
2079 info
->cs
.DebugInfo
->Spare
[0] = 0;
2080 RtlDeleteCriticalSection( &info
->cs
);
2081 HeapFree( GetProcessHeap(), 0, info
);
2087 WARN("Illegal handle %p\n", handle
);
2088 SetLastError( ERROR_INVALID_HANDLE
);
2097 /*************************************************************************
2098 * FindFirstFileA (KERNEL32.@)
2100 HANDLE WINAPI
FindFirstFileA( LPCSTR lpFileName
, WIN32_FIND_DATAA
*lpFindData
)
2102 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
2103 FindExSearchNameMatch
, NULL
, 0);
2106 /*************************************************************************
2107 * FindFirstFileExA (KERNEL32.@)
2109 HANDLE WINAPI
FindFirstFileExA( LPCSTR lpFileName
, FINDEX_INFO_LEVELS fInfoLevelId
,
2110 LPVOID lpFindFileData
, FINDEX_SEARCH_OPS fSearchOp
,
2111 LPVOID lpSearchFilter
, DWORD dwAdditionalFlags
)
2114 WIN32_FIND_DATAA
*dataA
;
2115 WIN32_FIND_DATAW dataW
;
2118 if (!(nameW
= FILE_name_AtoW( lpFileName
, FALSE
))) return INVALID_HANDLE_VALUE
;
2120 handle
= FindFirstFileExW(nameW
, fInfoLevelId
, &dataW
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
2121 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
2123 dataA
= (WIN32_FIND_DATAA
*) lpFindFileData
;
2124 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
2125 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
2126 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2127 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2128 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2129 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
2130 FILE_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
2131 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
2132 sizeof(dataA
->cAlternateFileName
) );
2137 /*************************************************************************
2138 * FindFirstFileW (KERNEL32.@)
2140 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
2142 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
2143 FindExSearchNameMatch
, NULL
, 0);
2147 /*************************************************************************
2148 * FindNextFileA (KERNEL32.@)
2150 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
2152 WIN32_FIND_DATAW dataW
;
2154 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
2155 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
2156 data
->ftCreationTime
= dataW
.ftCreationTime
;
2157 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2158 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2159 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2160 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
2161 FILE_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
2162 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
2163 sizeof(data
->cAlternateFileName
) );
2168 /**************************************************************************
2169 * GetFileAttributesW (KERNEL32.@)
2171 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
2173 FILE_BASIC_INFORMATION info
;
2174 UNICODE_STRING nt_name
;
2175 OBJECT_ATTRIBUTES attr
;
2178 TRACE("%s\n", debugstr_w(name
));
2180 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2182 SetLastError( ERROR_PATH_NOT_FOUND
);
2183 return INVALID_FILE_ATTRIBUTES
;
2186 attr
.Length
= sizeof(attr
);
2187 attr
.RootDirectory
= 0;
2188 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2189 attr
.ObjectName
= &nt_name
;
2190 attr
.SecurityDescriptor
= NULL
;
2191 attr
.SecurityQualityOfService
= NULL
;
2193 status
= NtQueryAttributesFile( &attr
, &info
);
2194 RtlFreeUnicodeString( &nt_name
);
2196 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
2198 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2199 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
2201 SetLastError( RtlNtStatusToDosError(status
) );
2202 return INVALID_FILE_ATTRIBUTES
;
2206 /**************************************************************************
2207 * GetFileAttributesA (KERNEL32.@)
2209 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
2213 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
2214 return GetFileAttributesW( nameW
);
2218 /**************************************************************************
2219 * SetFileAttributesW (KERNEL32.@)
2221 BOOL WINAPI
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
2223 UNICODE_STRING nt_name
;
2224 OBJECT_ATTRIBUTES attr
;
2229 TRACE("%s %x\n", debugstr_w(name
), attributes
);
2231 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2233 SetLastError( ERROR_PATH_NOT_FOUND
);
2237 attr
.Length
= sizeof(attr
);
2238 attr
.RootDirectory
= 0;
2239 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2240 attr
.ObjectName
= &nt_name
;
2241 attr
.SecurityDescriptor
= NULL
;
2242 attr
.SecurityQualityOfService
= NULL
;
2244 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2245 RtlFreeUnicodeString( &nt_name
);
2247 if (status
== STATUS_SUCCESS
)
2249 FILE_BASIC_INFORMATION info
;
2251 memset( &info
, 0, sizeof(info
) );
2252 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2253 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2257 if (status
== STATUS_SUCCESS
) return TRUE
;
2258 SetLastError( RtlNtStatusToDosError(status
) );
2263 /**************************************************************************
2264 * SetFileAttributesA (KERNEL32.@)
2266 BOOL WINAPI
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2270 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2271 return SetFileAttributesW( nameW
, attributes
);
2275 /**************************************************************************
2276 * GetFileAttributesExW (KERNEL32.@)
2278 BOOL WINAPI
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2280 FILE_NETWORK_OPEN_INFORMATION info
;
2281 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
2282 UNICODE_STRING nt_name
;
2283 OBJECT_ATTRIBUTES attr
;
2286 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
2288 if (level
!= GetFileExInfoStandard
)
2290 SetLastError( ERROR_INVALID_PARAMETER
);
2294 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2296 SetLastError( ERROR_PATH_NOT_FOUND
);
2300 attr
.Length
= sizeof(attr
);
2301 attr
.RootDirectory
= 0;
2302 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2303 attr
.ObjectName
= &nt_name
;
2304 attr
.SecurityDescriptor
= NULL
;
2305 attr
.SecurityQualityOfService
= NULL
;
2307 status
= NtQueryFullAttributesFile( &attr
, &info
);
2308 RtlFreeUnicodeString( &nt_name
);
2310 if (status
!= STATUS_SUCCESS
)
2312 SetLastError( RtlNtStatusToDosError(status
) );
2316 data
->dwFileAttributes
= info
.FileAttributes
;
2317 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
2318 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
2319 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
2320 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
2321 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
2322 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
2323 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
2324 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
2329 /**************************************************************************
2330 * GetFileAttributesExA (KERNEL32.@)
2332 BOOL WINAPI
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2336 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2337 return GetFileAttributesExW( nameW
, level
, ptr
);
2341 /******************************************************************************
2342 * GetCompressedFileSizeW (KERNEL32.@)
2344 * Get the actual number of bytes used on disk.
2347 * Success: Low-order doubleword of number of bytes
2348 * Failure: INVALID_FILE_SIZE
2350 DWORD WINAPI
GetCompressedFileSizeW(
2351 LPCWSTR name
, /* [in] Pointer to name of file */
2352 LPDWORD size_high
) /* [out] Receives high-order doubleword of size */
2354 UNICODE_STRING nt_name
;
2355 OBJECT_ATTRIBUTES attr
;
2359 DWORD ret
= INVALID_FILE_SIZE
;
2361 TRACE("%s %p\n", debugstr_w(name
), size_high
);
2363 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2365 SetLastError( ERROR_PATH_NOT_FOUND
);
2366 return INVALID_FILE_SIZE
;
2369 attr
.Length
= sizeof(attr
);
2370 attr
.RootDirectory
= 0;
2371 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2372 attr
.ObjectName
= &nt_name
;
2373 attr
.SecurityDescriptor
= NULL
;
2374 attr
.SecurityQualityOfService
= NULL
;
2376 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2377 RtlFreeUnicodeString( &nt_name
);
2379 if (status
== STATUS_SUCCESS
)
2381 /* we don't support compressed files, simply return the file size */
2382 ret
= GetFileSize( handle
, size_high
);
2385 else SetLastError( RtlNtStatusToDosError(status
) );
2391 /******************************************************************************
2392 * GetCompressedFileSizeA (KERNEL32.@)
2394 * See GetCompressedFileSizeW.
2396 DWORD WINAPI
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
2400 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
2401 return GetCompressedFileSizeW( nameW
, size_high
);
2405 /***********************************************************************
2406 * OpenFile (KERNEL32.@)
2408 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
2412 WORD filedatetime
[2];
2414 if (!ofs
) return HFILE_ERROR
;
2416 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
2417 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
2418 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
2419 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
2420 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
2421 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
2422 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
2423 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
2424 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
2425 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
2426 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
2427 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
2428 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
2429 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
2430 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
2431 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
2432 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
2433 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
2437 ofs
->cBytes
= sizeof(OFSTRUCT
);
2439 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
2441 if (!name
) return HFILE_ERROR
;
2443 TRACE("%s %04x\n", name
, mode
);
2445 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2446 Are there any cases where getting the path here is wrong?
2447 Uwe Bonnes 1997 Apr 2 */
2448 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
)) goto error
;
2450 /* OF_PARSE simply fills the structure */
2452 if (mode
& OF_PARSE
)
2454 ofs
->fFixedDisk
= (GetDriveTypeA( ofs
->szPathName
) != DRIVE_REMOVABLE
);
2455 TRACE("(%s): OF_PARSE, res = '%s'\n", name
, ofs
->szPathName
);
2459 /* OF_CREATE is completely different from all other options, so
2462 if (mode
& OF_CREATE
)
2464 if ((handle
= create_file_OF( name
, mode
)) == INVALID_HANDLE_VALUE
)
2469 /* Now look for the file */
2471 if (!SearchPathA( NULL
, name
, NULL
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
))
2474 TRACE("found %s\n", debugstr_a(ofs
->szPathName
) );
2476 if (mode
& OF_DELETE
)
2478 if (!DeleteFileA( ofs
->szPathName
)) goto error
;
2479 TRACE("(%s): OF_DELETE return = OK\n", name
);
2483 handle
= LongToHandle(_lopen( ofs
->szPathName
, mode
));
2484 if (handle
== INVALID_HANDLE_VALUE
) goto error
;
2486 GetFileTime( handle
, NULL
, NULL
, &filetime
);
2487 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
2488 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
2490 if (ofs
->Reserved1
!= filedatetime
[0] || ofs
->Reserved2
!= filedatetime
[1] )
2492 CloseHandle( handle
);
2493 WARN("(%s): OF_VERIFY failed\n", name
);
2494 /* FIXME: what error here? */
2495 SetLastError( ERROR_FILE_NOT_FOUND
);
2499 ofs
->Reserved1
= filedatetime
[0];
2500 ofs
->Reserved2
= filedatetime
[1];
2502 TRACE("(%s): OK, return = %p\n", name
, handle
);
2503 if (mode
& OF_EXIST
) /* Return TRUE instead of a handle */
2505 CloseHandle( handle
);
2508 return HandleToLong(handle
);
2510 error
: /* We get here if there was an error opening the file */
2511 ofs
->nErrCode
= GetLastError();
2512 WARN("(%s): return = HFILE_ERROR error= %d\n", name
,ofs
->nErrCode
);