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 "ddk/ntddk.h"
44 #include "kernel_private.h"
47 #include "wine/exception.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(file
);
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 FINDEX_INFO_LEVELS level
; /* Level passed to FindFirst */
61 UNICODE_STRING mask
; /* file mask */
62 UNICODE_STRING path
; /* NT path used to open the directory */
63 BOOL is_root
; /* is directory the root of the drive? */
64 UINT data_pos
; /* current position in dir data */
65 UINT data_len
; /* length of dir data */
66 UINT data_size
; /* size of data buffer, or 0 when everything has been read */
67 BYTE
*data
; /* directory data */
70 #define FIND_FIRST_MAGIC 0xc0ffee11
72 static const UINT max_entry_size
= offsetof( FILE_BOTH_DIRECTORY_INFORMATION
, FileName
[256] );
74 static BOOL oem_file_apis
;
76 static const WCHAR wildcardsW
[] = { '*','?',0 };
78 /***********************************************************************
81 * Wrapper for CreateFile that takes OF_* mode flags.
83 static HANDLE
create_file_OF( LPCSTR path
, INT mode
)
85 DWORD access
, sharing
, creation
;
89 creation
= CREATE_ALWAYS
;
90 access
= GENERIC_READ
| GENERIC_WRITE
;
94 creation
= OPEN_EXISTING
;
97 case OF_READ
: access
= GENERIC_READ
; break;
98 case OF_WRITE
: access
= GENERIC_WRITE
; break;
99 case OF_READWRITE
: access
= GENERIC_READ
| GENERIC_WRITE
; break;
100 default: access
= 0; break;
106 case OF_SHARE_EXCLUSIVE
: sharing
= 0; break;
107 case OF_SHARE_DENY_WRITE
: sharing
= FILE_SHARE_READ
; break;
108 case OF_SHARE_DENY_READ
: sharing
= FILE_SHARE_WRITE
; break;
109 case OF_SHARE_DENY_NONE
:
110 case OF_SHARE_COMPAT
:
111 default: sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
113 return CreateFileA( path
, access
, sharing
, NULL
, creation
, FILE_ATTRIBUTE_NORMAL
, 0 );
117 /***********************************************************************
120 * Check if a dir symlink should be returned by FindNextFile.
122 static BOOL
check_dir_symlink( FIND_FIRST_INFO
*info
, const FILE_BOTH_DIR_INFORMATION
*file_info
)
125 ANSI_STRING unix_name
;
126 struct stat st
, parent_st
;
130 str
.MaximumLength
= info
->path
.Length
+ sizeof(WCHAR
) + file_info
->FileNameLength
;
131 if (!(str
.Buffer
= HeapAlloc( GetProcessHeap(), 0, str
.MaximumLength
))) return TRUE
;
132 memcpy( str
.Buffer
, info
->path
.Buffer
, info
->path
.Length
);
133 len
= info
->path
.Length
/ sizeof(WCHAR
);
134 if (!len
|| str
.Buffer
[len
-1] != '\\') str
.Buffer
[len
++] = '\\';
135 memcpy( str
.Buffer
+ len
, file_info
->FileName
, file_info
->FileNameLength
);
136 str
.Length
= len
* sizeof(WCHAR
) + file_info
->FileNameLength
;
138 unix_name
.Buffer
= NULL
;
139 if (!wine_nt_to_unix_file_name( &str
, &unix_name
, OPEN_EXISTING
, FALSE
) &&
140 !stat( unix_name
.Buffer
, &st
))
142 char *p
= unix_name
.Buffer
+ unix_name
.Length
- 1;
144 /* skip trailing slashes */
145 while (p
> unix_name
.Buffer
&& *p
== '/') p
--;
147 while (ret
&& p
> unix_name
.Buffer
)
149 while (p
> unix_name
.Buffer
&& *p
!= '/') p
--;
150 while (p
> unix_name
.Buffer
&& *p
== '/') p
--;
152 if (!stat( unix_name
.Buffer
, &parent_st
) &&
153 parent_st
.st_dev
== st
.st_dev
&&
154 parent_st
.st_ino
== st
.st_ino
)
156 WARN( "suppressing dir symlink %s pointing to parent %s\n",
157 debugstr_wn( str
.Buffer
, str
.Length
/sizeof(WCHAR
) ),
158 debugstr_a( unix_name
.Buffer
));
163 RtlFreeAnsiString( &unix_name
);
164 RtlFreeUnicodeString( &str
);
169 /***********************************************************************
172 * Set the DOS error code from errno.
174 void FILE_SetDosError(void)
176 int save_errno
= errno
; /* errno gets overwritten by printf */
178 TRACE("errno = %d %s\n", errno
, strerror(errno
));
182 SetLastError( ERROR_SHARING_VIOLATION
);
185 SetLastError( ERROR_INVALID_HANDLE
);
188 SetLastError( ERROR_HANDLE_DISK_FULL
);
193 SetLastError( ERROR_ACCESS_DENIED
);
196 SetLastError( ERROR_LOCK_VIOLATION
);
199 SetLastError( ERROR_FILE_NOT_FOUND
);
202 SetLastError( ERROR_CANNOT_MAKE
);
206 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
209 SetLastError( ERROR_FILE_EXISTS
);
213 SetLastError( ERROR_SEEK
);
216 SetLastError( ERROR_DIR_NOT_EMPTY
);
219 SetLastError( ERROR_BAD_FORMAT
);
222 SetLastError( ERROR_PATH_NOT_FOUND
);
225 SetLastError( ERROR_NOT_SAME_DEVICE
);
228 WARN("unknown file error: %s\n", strerror(save_errno
) );
229 SetLastError( ERROR_GEN_FAILURE
);
236 /***********************************************************************
239 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
241 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
242 * there is no possibility for the function to do that twice, taking into
243 * account any called function.
245 WCHAR
*FILE_name_AtoW( LPCSTR name
, BOOL alloc
)
248 UNICODE_STRING strW
, *pstrW
;
251 RtlInitAnsiString( &str
, name
);
252 pstrW
= alloc
? &strW
: &NtCurrentTeb()->StaticUnicodeString
;
254 status
= RtlOemStringToUnicodeString( pstrW
, &str
, alloc
);
256 status
= RtlAnsiStringToUnicodeString( pstrW
, &str
, alloc
);
257 if (status
== STATUS_SUCCESS
) return pstrW
->Buffer
;
259 if (status
== STATUS_BUFFER_OVERFLOW
)
260 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
262 SetLastError( RtlNtStatusToDosError(status
) );
267 /***********************************************************************
270 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
272 DWORD
FILE_name_WtoA( LPCWSTR src
, INT srclen
, LPSTR dest
, INT destlen
)
276 if (srclen
< 0) srclen
= strlenW( src
) + 1;
278 RtlUnicodeToOemN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
280 RtlUnicodeToMultiByteN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
285 /**************************************************************************
286 * SetFileApisToOEM (KERNEL32.@)
288 VOID WINAPI
SetFileApisToOEM(void)
290 oem_file_apis
= TRUE
;
294 /**************************************************************************
295 * SetFileApisToANSI (KERNEL32.@)
297 VOID WINAPI
SetFileApisToANSI(void)
299 oem_file_apis
= FALSE
;
303 /******************************************************************************
304 * AreFileApisANSI (KERNEL32.@)
306 * Determines if file functions are using ANSI
309 * TRUE: Set of file functions is using ANSI code page
310 * FALSE: Set of file functions is using OEM code page
312 BOOL WINAPI
AreFileApisANSI(void)
314 return !oem_file_apis
;
318 /**************************************************************************
319 * Operations on file handles *
320 **************************************************************************/
322 /******************************************************************
323 * FILE_ReadWriteApc (internal)
325 static void WINAPI
FILE_ReadWriteApc(void* apc_user
, PIO_STATUS_BLOCK io_status
, ULONG reserved
)
327 LPOVERLAPPED_COMPLETION_ROUTINE cr
= apc_user
;
329 cr(RtlNtStatusToDosError(io_status
->u
.Status
), io_status
->Information
, (LPOVERLAPPED
)io_status
);
333 /***********************************************************************
334 * ReadFileEx (KERNEL32.@)
336 BOOL WINAPI
ReadFileEx(HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
337 LPOVERLAPPED overlapped
,
338 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
340 LARGE_INTEGER offset
;
342 PIO_STATUS_BLOCK io_status
;
344 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile
, buffer
, bytesToRead
, overlapped
, lpCompletionRoutine
);
348 SetLastError(ERROR_INVALID_PARAMETER
);
352 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
353 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
354 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
355 io_status
->u
.Status
= STATUS_PENDING
;
356 io_status
->Information
= 0;
358 status
= NtReadFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
359 io_status
, buffer
, bytesToRead
, &offset
, NULL
);
361 if (status
&& status
!= STATUS_PENDING
)
363 SetLastError( RtlNtStatusToDosError(status
) );
370 /***********************************************************************
371 * ReadFileScatter (KERNEL32.@)
373 BOOL WINAPI
ReadFileScatter( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
374 LPDWORD reserved
, LPOVERLAPPED overlapped
)
376 PIO_STATUS_BLOCK io_status
;
377 LARGE_INTEGER offset
;
381 TRACE( "(%p %p %u %p)\n", file
, segments
, count
, overlapped
);
383 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
384 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
385 if (!((ULONG_PTR
)overlapped
->hEvent
& 1)) cvalue
= overlapped
;
386 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
387 io_status
->u
.Status
= STATUS_PENDING
;
388 io_status
->Information
= 0;
390 status
= NtReadFileScatter( file
, overlapped
->hEvent
, NULL
, cvalue
, io_status
,
391 segments
, count
, &offset
, NULL
);
392 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
397 /***********************************************************************
398 * ReadFile (KERNEL32.@)
400 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
401 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
403 LARGE_INTEGER offset
;
404 PLARGE_INTEGER poffset
= NULL
;
405 IO_STATUS_BLOCK iosb
;
406 PIO_STATUS_BLOCK io_status
= &iosb
;
409 LPVOID cvalue
= NULL
;
411 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToRead
,
412 bytesRead
, overlapped
);
414 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
416 if (is_console_handle(hFile
))
419 if (!ReadConsoleA(hFile
, buffer
, bytesToRead
, &conread
, NULL
) ||
420 !GetConsoleMode(hFile
, &mode
))
422 /* ctrl-Z (26) means end of file on window (if at beginning of buffer)
423 * but Unix uses ctrl-D (4), and ctrl-Z is a bad idea on Unix :-/
424 * So map both ctrl-D ctrl-Z to EOF.
426 if ((mode
& ENABLE_PROCESSED_INPUT
) && conread
> 0 &&
427 (((char*)buffer
)[0] == 26 || ((char*)buffer
)[0] == 4))
431 if (bytesRead
) *bytesRead
= conread
;
435 if (overlapped
!= NULL
)
437 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
438 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
440 hEvent
= overlapped
->hEvent
;
441 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
442 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
444 io_status
->u
.Status
= STATUS_PENDING
;
445 io_status
->Information
= 0;
447 status
= NtReadFile(hFile
, hEvent
, NULL
, cvalue
, io_status
, buffer
, bytesToRead
, poffset
, NULL
);
449 if (status
== STATUS_PENDING
&& !overlapped
)
451 WaitForSingleObject( hFile
, INFINITE
);
452 status
= io_status
->u
.Status
;
455 if (status
!= STATUS_PENDING
&& bytesRead
)
456 *bytesRead
= io_status
->Information
;
458 if (status
== STATUS_END_OF_FILE
)
460 if (overlapped
!= NULL
)
462 SetLastError( RtlNtStatusToDosError(status
) );
466 else if (status
&& status
!= STATUS_TIMEOUT
)
468 SetLastError( RtlNtStatusToDosError(status
) );
475 /***********************************************************************
476 * WriteFileEx (KERNEL32.@)
478 BOOL WINAPI
WriteFileEx(HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
479 LPOVERLAPPED overlapped
,
480 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
482 LARGE_INTEGER offset
;
484 PIO_STATUS_BLOCK io_status
;
486 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, overlapped
, lpCompletionRoutine
);
488 if (overlapped
== NULL
)
490 SetLastError(ERROR_INVALID_PARAMETER
);
493 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
494 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
496 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
497 io_status
->u
.Status
= STATUS_PENDING
;
498 io_status
->Information
= 0;
500 status
= NtWriteFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
501 io_status
, buffer
, bytesToWrite
, &offset
, NULL
);
503 if (status
&& status
!= STATUS_PENDING
)
505 SetLastError( RtlNtStatusToDosError(status
) );
512 /***********************************************************************
513 * WriteFileGather (KERNEL32.@)
515 BOOL WINAPI
WriteFileGather( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
516 LPDWORD reserved
, LPOVERLAPPED overlapped
)
518 PIO_STATUS_BLOCK io_status
;
519 LARGE_INTEGER offset
;
523 TRACE( "%p %p %u %p\n", file
, segments
, count
, overlapped
);
525 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
526 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
527 if (!((ULONG_PTR
)overlapped
->hEvent
& 1)) cvalue
= overlapped
;
528 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
529 io_status
->u
.Status
= STATUS_PENDING
;
530 io_status
->Information
= 0;
532 status
= NtWriteFileGather( file
, overlapped
->hEvent
, NULL
, cvalue
, io_status
,
533 segments
, count
, &offset
, NULL
);
534 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
539 /***********************************************************************
540 * WriteFile (KERNEL32.@)
542 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
543 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
545 HANDLE hEvent
= NULL
;
546 LARGE_INTEGER offset
;
547 PLARGE_INTEGER poffset
= NULL
;
549 IO_STATUS_BLOCK iosb
;
550 PIO_STATUS_BLOCK piosb
= &iosb
;
551 LPVOID cvalue
= NULL
;
553 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, bytesWritten
, overlapped
);
555 if (is_console_handle(hFile
))
556 return WriteConsoleA(hFile
, buffer
, bytesToWrite
, bytesWritten
, NULL
);
560 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
561 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
563 hEvent
= overlapped
->hEvent
;
564 piosb
= (PIO_STATUS_BLOCK
)overlapped
;
565 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
567 piosb
->u
.Status
= STATUS_PENDING
;
568 piosb
->Information
= 0;
570 status
= NtWriteFile(hFile
, hEvent
, NULL
, cvalue
, piosb
,
571 buffer
, bytesToWrite
, poffset
, NULL
);
573 if (status
== STATUS_PENDING
&& !overlapped
)
575 WaitForSingleObject( hFile
, INFINITE
);
576 status
= piosb
->u
.Status
;
579 if (status
!= STATUS_PENDING
&& bytesWritten
)
580 *bytesWritten
= piosb
->Information
;
582 if (status
&& status
!= STATUS_TIMEOUT
)
584 SetLastError( RtlNtStatusToDosError(status
) );
591 /***********************************************************************
592 * GetOverlappedResult (KERNEL32.@)
594 * Check the result of an Asynchronous data transfer from a file.
597 * HANDLE hFile [in] handle of file to check on
598 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
599 * LPDWORD lpTransferred [in/out] number of bytes transferred
600 * BOOL bWait [in] wait for the transfer to complete ?
606 * If successful (and relevant) lpTransferred will hold the number of
607 * bytes transferred during the async operation.
609 BOOL WINAPI
GetOverlappedResult(HANDLE hFile
, LPOVERLAPPED lpOverlapped
,
610 LPDWORD lpTransferred
, BOOL bWait
)
614 TRACE( "(%p %p %p %x)\n", hFile
, lpOverlapped
, lpTransferred
, bWait
);
616 status
= lpOverlapped
->Internal
;
617 if (status
== STATUS_PENDING
)
621 SetLastError( ERROR_IO_INCOMPLETE
);
625 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: hFile
,
626 INFINITE
) == WAIT_FAILED
)
628 status
= lpOverlapped
->Internal
;
631 *lpTransferred
= lpOverlapped
->InternalHigh
;
633 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
637 /***********************************************************************
638 * CancelIoEx (KERNEL32.@)
640 * Cancels pending I/O operations on a file given the overlapped used.
643 * handle [I] File handle.
644 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
648 * Failure: FALSE, check GetLastError().
650 BOOL WINAPI
CancelIoEx(HANDLE handle
, LPOVERLAPPED lpOverlapped
)
652 IO_STATUS_BLOCK io_status
;
654 NtCancelIoFileEx(handle
, (PIO_STATUS_BLOCK
) lpOverlapped
, &io_status
);
655 if (io_status
.u
.Status
)
657 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
663 /***********************************************************************
664 * CancelIo (KERNEL32.@)
666 * Cancels pending I/O operations initiated by the current thread on a file.
669 * handle [I] File handle.
673 * Failure: FALSE, check GetLastError().
675 BOOL WINAPI
CancelIo(HANDLE handle
)
677 IO_STATUS_BLOCK io_status
;
679 NtCancelIoFile(handle
, &io_status
);
680 if (io_status
.u
.Status
)
682 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
688 /***********************************************************************
689 * _hread (KERNEL32.@)
691 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
693 return _lread( hFile
, buffer
, count
);
697 /***********************************************************************
698 * _hwrite (KERNEL32.@)
700 * experimentation yields that _lwrite:
701 * o truncates the file at the current position with
703 * o returns 0 on a 0 length write
704 * o works with console handles
707 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
711 TRACE("%d %p %d\n", handle
, buffer
, count
);
715 /* Expand or truncate at current position */
716 if (!SetEndOfFile( LongToHandle(handle
) )) return HFILE_ERROR
;
719 if (!WriteFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
725 /***********************************************************************
726 * _lclose (KERNEL32.@)
728 HFILE WINAPI
_lclose( HFILE hFile
)
730 TRACE("handle %d\n", hFile
);
731 return CloseHandle( LongToHandle(hFile
) ) ? 0 : HFILE_ERROR
;
735 /***********************************************************************
736 * _lcreat (KERNEL32.@)
738 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
742 /* Mask off all flags not explicitly allowed by the doc */
743 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
744 TRACE("%s %02x\n", path
, attr
);
745 hfile
= CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
746 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
747 CREATE_ALWAYS
, attr
, 0 );
748 return HandleToLong(hfile
);
752 /***********************************************************************
753 * _lopen (KERNEL32.@)
755 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
759 TRACE("(%s,%04x)\n", debugstr_a(path
), mode
);
760 hfile
= create_file_OF( path
, mode
& ~OF_CREATE
);
761 return HandleToLong(hfile
);
764 /***********************************************************************
765 * _lread (KERNEL32.@)
767 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
770 if (!ReadFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
776 /***********************************************************************
777 * _llseek (KERNEL32.@)
779 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
781 return SetFilePointer( LongToHandle(hFile
), lOffset
, NULL
, nOrigin
);
785 /***********************************************************************
786 * _lwrite (KERNEL32.@)
788 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
790 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
794 /***********************************************************************
795 * FlushFileBuffers (KERNEL32.@)
797 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
800 IO_STATUS_BLOCK ioblk
;
802 if (is_console_handle( hFile
))
804 /* this will fail (as expected) for an output handle */
805 return FlushConsoleInputBuffer( hFile
);
807 nts
= NtFlushBuffersFile( hFile
, &ioblk
);
808 if (nts
!= STATUS_SUCCESS
)
810 SetLastError( RtlNtStatusToDosError( nts
) );
818 /***********************************************************************
819 * GetFileType (KERNEL32.@)
821 DWORD WINAPI
GetFileType( HANDLE hFile
)
823 FILE_FS_DEVICE_INFORMATION info
;
827 if (hFile
== (HANDLE
)STD_INPUT_HANDLE
|| hFile
== (HANDLE
)STD_OUTPUT_HANDLE
828 || hFile
== (HANDLE
)STD_ERROR_HANDLE
)
829 hFile
= GetStdHandle((DWORD_PTR
)hFile
);
831 if (is_console_handle( hFile
)) return FILE_TYPE_CHAR
;
833 status
= NtQueryVolumeInformationFile( hFile
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
834 if (status
!= STATUS_SUCCESS
)
836 SetLastError( RtlNtStatusToDosError(status
) );
837 return FILE_TYPE_UNKNOWN
;
840 switch(info
.DeviceType
)
842 case FILE_DEVICE_NULL
:
843 case FILE_DEVICE_SERIAL_PORT
:
844 case FILE_DEVICE_PARALLEL_PORT
:
845 case FILE_DEVICE_TAPE
:
846 case FILE_DEVICE_UNKNOWN
:
847 return FILE_TYPE_CHAR
;
848 case FILE_DEVICE_NAMED_PIPE
:
849 return FILE_TYPE_PIPE
;
851 return FILE_TYPE_DISK
;
856 /***********************************************************************
857 * GetFileInformationByHandle (KERNEL32.@)
859 BOOL WINAPI
GetFileInformationByHandle( HANDLE hFile
, BY_HANDLE_FILE_INFORMATION
*info
)
861 FILE_ALL_INFORMATION all_info
;
865 status
= NtQueryInformationFile( hFile
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
866 if (status
== STATUS_BUFFER_OVERFLOW
) status
= STATUS_SUCCESS
;
867 if (status
== STATUS_SUCCESS
)
869 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
870 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
871 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
872 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
873 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
874 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
875 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
876 info
->dwVolumeSerialNumber
= 0; /* FIXME */
877 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
878 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
879 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
880 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
881 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
884 SetLastError( RtlNtStatusToDosError(status
) );
889 /***********************************************************************
890 * GetFileInformationByHandleEx (KERNEL32.@)
892 BOOL WINAPI
GetFileInformationByHandleEx( HANDLE handle
, FILE_INFO_BY_HANDLE_CLASS
class,
893 LPVOID info
, DWORD size
)
901 case FileCompressionInfo
:
902 case FileAttributeTagInfo
:
903 case FileRemoteProtocolInfo
:
904 case FileFullDirectoryInfo
:
905 case FileFullDirectoryRestartInfo
:
906 case FileStorageInfo
:
907 case FileAlignmentInfo
:
909 case FileIdExtdDirectoryInfo
:
910 case FileIdExtdDirectoryRestartInfo
:
911 FIXME( "%p, %u, %p, %u\n", handle
, class, info
, size
);
912 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
916 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileBasicInformation
);
919 case FileStandardInfo
:
920 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileStandardInformation
);
924 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileNameInformation
);
927 case FileIdBothDirectoryRestartInfo
:
928 case FileIdBothDirectoryInfo
:
929 status
= NtQueryDirectoryFile( handle
, NULL
, NULL
, NULL
, &io
, info
, size
,
930 FileIdBothDirectoryInformation
, FALSE
, NULL
,
931 (class == FileIdBothDirectoryRestartInfo
) );
935 case FileDispositionInfo
:
936 case FileAllocationInfo
:
937 case FileIoPriorityHintInfo
:
938 case FileEndOfFileInfo
:
940 SetLastError( ERROR_INVALID_PARAMETER
);
944 if (status
!= STATUS_SUCCESS
)
946 SetLastError( RtlNtStatusToDosError( status
) );
953 /***********************************************************************
954 * GetFileSize (KERNEL32.@)
956 * Retrieve the size of a file.
959 * hFile [I] File to retrieve size of.
960 * filesizehigh [O] On return, the high bits of the file size.
963 * Success: The low bits of the file size.
964 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
965 * check GetLastError() for values other than ERROR_SUCCESS.
967 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
970 if (!GetFileSizeEx( hFile
, &size
)) return INVALID_FILE_SIZE
;
971 if (filesizehigh
) *filesizehigh
= size
.u
.HighPart
;
972 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError(0);
973 return size
.u
.LowPart
;
977 /***********************************************************************
978 * GetFileSizeEx (KERNEL32.@)
980 * Retrieve the size of a file.
983 * hFile [I] File to retrieve size of.
984 * lpFileSIze [O] On return, the size of the file.
988 * Failure: FALSE, check GetLastError().
990 BOOL WINAPI
GetFileSizeEx( HANDLE hFile
, PLARGE_INTEGER lpFileSize
)
992 FILE_STANDARD_INFORMATION info
;
996 if (is_console_handle( hFile
))
998 SetLastError( ERROR_INVALID_HANDLE
);
1002 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileStandardInformation
);
1003 if (status
== STATUS_SUCCESS
)
1005 *lpFileSize
= info
.EndOfFile
;
1008 SetLastError( RtlNtStatusToDosError(status
) );
1013 /**************************************************************************
1014 * SetEndOfFile (KERNEL32.@)
1016 * Sets the current position as the end of the file.
1019 * hFile [I] File handle.
1023 * Failure: FALSE, check GetLastError().
1025 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
1027 FILE_POSITION_INFORMATION pos
;
1028 FILE_END_OF_FILE_INFORMATION eof
;
1032 status
= NtQueryInformationFile( hFile
, &io
, &pos
, sizeof(pos
), FilePositionInformation
);
1033 if (status
== STATUS_SUCCESS
)
1035 eof
.EndOfFile
= pos
.CurrentByteOffset
;
1036 status
= NtSetInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
1038 if (status
== STATUS_SUCCESS
) return TRUE
;
1039 SetLastError( RtlNtStatusToDosError(status
) );
1043 /**************************************************************************
1044 * SetFileCompletionNotificationModes (KERNEL32.@)
1046 BOOL WINAPI
SetFileCompletionNotificationModes( HANDLE handle
, UCHAR flags
)
1048 FIXME("%p %x - stub\n", handle
, flags
);
1049 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1054 /***********************************************************************
1055 * SetFileInformationByHandle (KERNEL32.@)
1057 BOOL WINAPI
SetFileInformationByHandle( HANDLE file
, FILE_INFO_BY_HANDLE_CLASS
class, VOID
*info
, DWORD size
)
1062 TRACE( "%p %u %p %u\n", file
, class, info
, size
);
1068 case FileRenameInfo
:
1069 case FileAllocationInfo
:
1070 case FileEndOfFileInfo
:
1071 case FileStreamInfo
:
1072 case FileIdBothDirectoryInfo
:
1073 case FileIdBothDirectoryRestartInfo
:
1074 case FileIoPriorityHintInfo
:
1075 case FileFullDirectoryInfo
:
1076 case FileFullDirectoryRestartInfo
:
1077 case FileStorageInfo
:
1078 case FileAlignmentInfo
:
1080 case FileIdExtdDirectoryInfo
:
1081 case FileIdExtdDirectoryRestartInfo
:
1082 FIXME( "%p, %u, %p, %u\n", file
, class, info
, size
);
1083 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1086 case FileDispositionInfo
:
1087 status
= NtSetInformationFile( file
, &io
, info
, size
, FileDispositionInformation
);
1090 case FileStandardInfo
:
1091 case FileCompressionInfo
:
1092 case FileAttributeTagInfo
:
1093 case FileRemoteProtocolInfo
:
1095 SetLastError( ERROR_INVALID_PARAMETER
);
1099 if (status
!= STATUS_SUCCESS
)
1101 SetLastError( RtlNtStatusToDosError( status
) );
1108 /***********************************************************************
1109 * SetFilePointer (KERNEL32.@)
1111 DWORD WINAPI DECLSPEC_HOTPATCH
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
, DWORD method
)
1113 LARGE_INTEGER dist
, newpos
;
1117 dist
.u
.LowPart
= distance
;
1118 dist
.u
.HighPart
= *highword
;
1120 else dist
.QuadPart
= distance
;
1122 if (!SetFilePointerEx( hFile
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
1124 if (highword
) *highword
= newpos
.u
.HighPart
;
1125 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
1126 return newpos
.u
.LowPart
;
1130 /***********************************************************************
1131 * SetFilePointerEx (KERNEL32.@)
1133 BOOL WINAPI
SetFilePointerEx( HANDLE hFile
, LARGE_INTEGER distance
,
1134 LARGE_INTEGER
*newpos
, DWORD method
)
1138 FILE_POSITION_INFORMATION info
;
1143 pos
= distance
.QuadPart
;
1146 if (NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
1148 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
1152 FILE_END_OF_FILE_INFORMATION eof
;
1153 if (NtQueryInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
1155 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
1159 SetLastError( ERROR_INVALID_PARAMETER
);
1165 SetLastError( ERROR_NEGATIVE_SEEK
);
1169 info
.CurrentByteOffset
.QuadPart
= pos
;
1170 if (NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
1172 if (newpos
) newpos
->QuadPart
= pos
;
1176 SetLastError( RtlNtStatusToDosError(io
.u
.Status
) );
1180 /***********************************************************************
1181 * SetFileValidData (KERNEL32.@)
1183 BOOL WINAPI
SetFileValidData( HANDLE hFile
, LONGLONG ValidDataLength
)
1185 FILE_VALID_DATA_LENGTH_INFORMATION info
;
1189 info
.ValidDataLength
.QuadPart
= ValidDataLength
;
1190 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileValidDataLengthInformation
);
1192 if (status
== STATUS_SUCCESS
) return TRUE
;
1193 SetLastError( RtlNtStatusToDosError(status
) );
1197 /***********************************************************************
1198 * GetFileTime (KERNEL32.@)
1200 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
1201 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
1203 FILE_BASIC_INFORMATION info
;
1207 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1208 if (status
== STATUS_SUCCESS
)
1212 lpCreationTime
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1213 lpCreationTime
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1215 if (lpLastAccessTime
)
1217 lpLastAccessTime
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1218 lpLastAccessTime
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1220 if (lpLastWriteTime
)
1222 lpLastWriteTime
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1223 lpLastWriteTime
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1227 SetLastError( RtlNtStatusToDosError(status
) );
1232 /***********************************************************************
1233 * SetFileTime (KERNEL32.@)
1235 BOOL WINAPI
SetFileTime( HANDLE hFile
, const FILETIME
*ctime
,
1236 const FILETIME
*atime
, const FILETIME
*mtime
)
1238 FILE_BASIC_INFORMATION info
;
1242 memset( &info
, 0, sizeof(info
) );
1245 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
1246 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
1250 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
1251 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
1255 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
1256 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
1259 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1260 if (status
== STATUS_SUCCESS
) return TRUE
;
1261 SetLastError( RtlNtStatusToDosError(status
) );
1266 /**************************************************************************
1267 * LockFile (KERNEL32.@)
1269 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1270 DWORD count_low
, DWORD count_high
)
1273 LARGE_INTEGER count
, offset
;
1275 TRACE( "%p %x%08x %x%08x\n",
1276 hFile
, offset_high
, offset_low
, count_high
, count_low
);
1278 count
.u
.LowPart
= count_low
;
1279 count
.u
.HighPart
= count_high
;
1280 offset
.u
.LowPart
= offset_low
;
1281 offset
.u
.HighPart
= offset_high
;
1283 status
= NtLockFile( hFile
, 0, NULL
, NULL
,
1284 NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
);
1286 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1291 /**************************************************************************
1292 * LockFileEx [KERNEL32.@]
1294 * Locks a byte range within an open file for shared or exclusive access.
1301 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1303 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1304 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
1307 LARGE_INTEGER count
, offset
;
1308 LPVOID cvalue
= NULL
;
1312 SetLastError( ERROR_INVALID_PARAMETER
);
1316 TRACE( "%p %x%08x %x%08x flags %x\n",
1317 hFile
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
,
1318 count_high
, count_low
, flags
);
1320 count
.u
.LowPart
= count_low
;
1321 count
.u
.HighPart
= count_high
;
1322 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
1323 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
1325 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1327 status
= NtLockFile( hFile
, overlapped
->hEvent
, NULL
, cvalue
,
1328 NULL
, &offset
, &count
, NULL
,
1329 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
1330 flags
& LOCKFILE_EXCLUSIVE_LOCK
);
1332 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1337 /**************************************************************************
1338 * UnlockFile (KERNEL32.@)
1340 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1341 DWORD count_low
, DWORD count_high
)
1344 LARGE_INTEGER count
, offset
;
1346 count
.u
.LowPart
= count_low
;
1347 count
.u
.HighPart
= count_high
;
1348 offset
.u
.LowPart
= offset_low
;
1349 offset
.u
.HighPart
= offset_high
;
1351 status
= NtUnlockFile( hFile
, NULL
, &offset
, &count
, NULL
);
1352 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1357 /**************************************************************************
1358 * UnlockFileEx (KERNEL32.@)
1360 BOOL WINAPI
UnlockFileEx( HANDLE hFile
, DWORD reserved
, DWORD count_low
, DWORD count_high
,
1361 LPOVERLAPPED overlapped
)
1365 SetLastError( ERROR_INVALID_PARAMETER
);
1368 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
1370 return UnlockFile( hFile
, overlapped
->u
.s
.Offset
, overlapped
->u
.s
.OffsetHigh
, count_low
, count_high
);
1374 /*************************************************************************
1375 * SetHandleCount (KERNEL32.@)
1377 UINT WINAPI
SetHandleCount( UINT count
)
1383 /**************************************************************************
1384 * Operations on file names *
1385 **************************************************************************/
1388 /*************************************************************************
1389 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1391 * Creates or opens an object, and returns a handle that can be used to
1392 * access that object.
1396 * filename [in] pointer to filename to be accessed
1397 * access [in] access mode requested
1398 * sharing [in] share mode
1399 * sa [in] pointer to security attributes
1400 * creation [in] how to create the file
1401 * attributes [in] attributes for newly created file
1402 * template [in] handle to file with extended attributes to copy
1405 * Success: Open handle to specified file
1406 * Failure: INVALID_HANDLE_VALUE
1408 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
1409 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1410 DWORD attributes
, HANDLE
template )
1414 OBJECT_ATTRIBUTES attr
;
1415 UNICODE_STRING nameW
;
1419 const WCHAR
*vxd_name
= NULL
;
1420 static const WCHAR bkslashes_with_dotW
[] = {'\\','\\','.','\\',0};
1421 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
1422 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
1423 SECURITY_QUALITY_OF_SERVICE qos
;
1425 static const UINT nt_disposition
[5] =
1427 FILE_CREATE
, /* CREATE_NEW */
1428 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
1429 FILE_OPEN
, /* OPEN_EXISTING */
1430 FILE_OPEN_IF
, /* OPEN_ALWAYS */
1431 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
1437 if (!filename
|| !filename
[0])
1439 SetLastError( ERROR_PATH_NOT_FOUND
);
1440 return INVALID_HANDLE_VALUE
;
1443 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
1444 (access
& GENERIC_READ
)?"GENERIC_READ ":"",
1445 (access
& GENERIC_WRITE
)?"GENERIC_WRITE ":"",
1446 (access
& GENERIC_EXECUTE
)?"GENERIC_EXECUTE ":"",
1447 (!access
)?"QUERY_ACCESS ":"",
1448 (sharing
& FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
1449 (sharing
& FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
1450 (sharing
& FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
1451 creation
, attributes
);
1453 /* Open a console for CONIN$ or CONOUT$ */
1455 if (!strcmpiW(filename
, coninW
) || !strcmpiW(filename
, conoutW
))
1457 ret
= OpenConsoleW(filename
, access
, (sa
&& sa
->bInheritHandle
),
1458 creation
? OPEN_EXISTING
: 0);
1459 if (ret
== INVALID_HANDLE_VALUE
) SetLastError(ERROR_INVALID_PARAMETER
);
1463 if (!strncmpW(filename
, bkslashes_with_dotW
, 4))
1465 static const WCHAR pipeW
[] = {'P','I','P','E','\\',0};
1466 static const WCHAR mailslotW
[] = {'M','A','I','L','S','L','O','T','\\',0};
1468 if ((isalphaW(filename
[4]) && filename
[5] == ':' && filename
[6] == '\0') ||
1469 !strncmpiW( filename
+ 4, pipeW
, 5 ) ||
1470 !strncmpiW( filename
+ 4, mailslotW
, 9 ))
1474 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
1476 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
1478 else if (GetVersion() & 0x80000000)
1480 vxd_name
= filename
+ 4;
1481 if (!creation
) creation
= OPEN_EXISTING
;
1484 else dosdev
= RtlIsDosDeviceName_U( filename
);
1488 static const WCHAR conW
[] = {'C','O','N'};
1490 if (LOWORD(dosdev
) == sizeof(conW
) &&
1491 !memicmpW( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), conW
, sizeof(conW
)/sizeof(WCHAR
)))
1493 switch (access
& (GENERIC_READ
|GENERIC_WRITE
))
1496 ret
= OpenConsoleW(coninW
, access
, (sa
&& sa
->bInheritHandle
), OPEN_EXISTING
);
1499 ret
= OpenConsoleW(conoutW
, access
, (sa
&& sa
->bInheritHandle
), OPEN_EXISTING
);
1502 SetLastError( ERROR_FILE_NOT_FOUND
);
1503 return INVALID_HANDLE_VALUE
;
1508 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
1510 SetLastError( ERROR_INVALID_PARAMETER
);
1511 return INVALID_HANDLE_VALUE
;
1514 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
1516 SetLastError( ERROR_PATH_NOT_FOUND
);
1517 return INVALID_HANDLE_VALUE
;
1520 /* now call NtCreateFile */
1523 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
1524 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
1526 options
|= FILE_NON_DIRECTORY_FILE
;
1527 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
1529 options
|= FILE_DELETE_ON_CLOSE
;
1532 if (attributes
& FILE_FLAG_NO_BUFFERING
)
1533 options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
1534 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
1535 options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
1536 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
1537 options
|= FILE_RANDOM_ACCESS
;
1538 attributes
&= FILE_ATTRIBUTE_VALID_FLAGS
;
1540 attr
.Length
= sizeof(attr
);
1541 attr
.RootDirectory
= 0;
1542 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1543 attr
.ObjectName
= &nameW
;
1544 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1545 if (attributes
& SECURITY_SQOS_PRESENT
)
1547 qos
.Length
= sizeof(qos
);
1548 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
1549 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
1550 qos
.EffectiveOnly
= (attributes
& SECURITY_EFFECTIVE_ONLY
) != 0;
1551 attr
.SecurityQualityOfService
= &qos
;
1554 attr
.SecurityQualityOfService
= NULL
;
1556 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
1558 status
= NtCreateFile( &ret
, access
, &attr
, &io
, NULL
, attributes
,
1559 sharing
, nt_disposition
[creation
- CREATE_NEW
],
1563 if (vxd_name
&& vxd_name
[0])
1565 static HANDLE (*vxd_open
)(LPCWSTR
,DWORD
,SECURITY_ATTRIBUTES
*);
1566 if (!vxd_open
) vxd_open
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1567 "__wine_vxd_open" );
1568 if (vxd_open
&& (ret
= vxd_open( vxd_name
, access
, sa
))) goto done
;
1571 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
1572 ret
= INVALID_HANDLE_VALUE
;
1574 /* In the case file creation was rejected due to CREATE_NEW flag
1575 * was specified and file with that name already exists, correct
1576 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1577 * Note: RtlNtStatusToDosError is not the subject to blame here.
1579 if (status
== STATUS_OBJECT_NAME_COLLISION
)
1580 SetLastError( ERROR_FILE_EXISTS
);
1582 SetLastError( RtlNtStatusToDosError(status
) );
1586 if ((creation
== CREATE_ALWAYS
&& io
.Information
== FILE_OVERWRITTEN
) ||
1587 (creation
== OPEN_ALWAYS
&& io
.Information
== FILE_OPENED
))
1588 SetLastError( ERROR_ALREADY_EXISTS
);
1592 RtlFreeUnicodeString( &nameW
);
1595 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
1596 TRACE("returning %p\n", ret
);
1602 /*************************************************************************
1603 * CreateFileA (KERNEL32.@)
1607 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
1608 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1609 DWORD attributes
, HANDLE
template)
1613 if (!(nameW
= FILE_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1614 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
1617 /*************************************************************************
1618 * CreateFile2 (KERNEL32.@)
1620 HANDLE WINAPI
CreateFile2( LPCWSTR filename
, DWORD access
, DWORD sharing
, DWORD creation
,
1621 CREATEFILE2_EXTENDED_PARAMETERS
*exparams
)
1623 LPSECURITY_ATTRIBUTES sa
= exparams
? exparams
->lpSecurityAttributes
: NULL
;
1624 DWORD attributes
= exparams
? exparams
->dwFileAttributes
: 0;
1625 HANDLE
template = exparams
? exparams
->hTemplateFile
: NULL
;
1627 FIXME("(%s %x %x %x %p), partial stub\n", debugstr_w(filename
), access
, sharing
, creation
, exparams
);
1629 return CreateFileW( filename
, access
, sharing
, sa
, creation
, attributes
, template );
1632 /***********************************************************************
1633 * DeleteFileW (KERNEL32.@)
1638 * path [I] Path to the file to delete.
1642 * Failure: FALSE, check GetLastError().
1644 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1646 UNICODE_STRING nameW
;
1647 OBJECT_ATTRIBUTES attr
;
1652 TRACE("%s\n", debugstr_w(path
) );
1654 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
1656 SetLastError( ERROR_PATH_NOT_FOUND
);
1660 attr
.Length
= sizeof(attr
);
1661 attr
.RootDirectory
= 0;
1662 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1663 attr
.ObjectName
= &nameW
;
1664 attr
.SecurityDescriptor
= NULL
;
1665 attr
.SecurityQualityOfService
= NULL
;
1667 status
= NtCreateFile(&hFile
, GENERIC_READ
| GENERIC_WRITE
| DELETE
,
1668 &attr
, &io
, NULL
, 0,
1669 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1670 FILE_OPEN
, FILE_DELETE_ON_CLOSE
| FILE_NON_DIRECTORY_FILE
, NULL
, 0);
1671 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
1673 RtlFreeUnicodeString( &nameW
);
1676 SetLastError( RtlNtStatusToDosError(status
) );
1683 /***********************************************************************
1684 * DeleteFileA (KERNEL32.@)
1688 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1692 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1693 return DeleteFileW( pathW
);
1697 /**************************************************************************
1698 * ReplaceFileW (KERNEL32.@)
1699 * ReplaceFile (KERNEL32.@)
1701 BOOL WINAPI
ReplaceFileW(LPCWSTR lpReplacedFileName
, LPCWSTR lpReplacementFileName
,
1702 LPCWSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1703 LPVOID lpExclude
, LPVOID lpReserved
)
1705 UNICODE_STRING nt_replaced_name
, nt_replacement_name
;
1706 ANSI_STRING unix_replaced_name
, unix_replacement_name
, unix_backup_name
;
1707 HANDLE hReplaced
= NULL
, hReplacement
= NULL
, hBackup
= NULL
;
1708 DWORD error
= ERROR_SUCCESS
;
1709 UINT replaced_flags
;
1713 OBJECT_ATTRIBUTES attr
;
1715 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName
),
1716 debugstr_w(lpReplacementFileName
), debugstr_w(lpBackupFileName
),
1717 dwReplaceFlags
, lpExclude
, lpReserved
);
1720 FIXME("Ignoring flags %x\n", dwReplaceFlags
);
1722 /* First two arguments are mandatory */
1723 if (!lpReplacedFileName
|| !lpReplacementFileName
)
1725 SetLastError(ERROR_INVALID_PARAMETER
);
1729 unix_replaced_name
.Buffer
= NULL
;
1730 unix_replacement_name
.Buffer
= NULL
;
1731 unix_backup_name
.Buffer
= NULL
;
1733 attr
.Length
= sizeof(attr
);
1734 attr
.RootDirectory
= 0;
1735 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1736 attr
.ObjectName
= NULL
;
1737 attr
.SecurityDescriptor
= NULL
;
1738 attr
.SecurityQualityOfService
= NULL
;
1740 /* Open the "replaced" file for reading and writing */
1741 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName
, &nt_replaced_name
, NULL
, NULL
)))
1743 error
= ERROR_PATH_NOT_FOUND
;
1746 replaced_flags
= lpBackupFileName
? FILE_OPEN
: FILE_OPEN_IF
;
1747 attr
.ObjectName
= &nt_replaced_name
;
1748 status
= NtOpenFile(&hReplaced
, GENERIC_READ
|GENERIC_WRITE
|DELETE
|SYNCHRONIZE
,
1750 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
1751 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1752 if (status
== STATUS_SUCCESS
)
1753 status
= wine_nt_to_unix_file_name(&nt_replaced_name
, &unix_replaced_name
, replaced_flags
, FALSE
);
1754 RtlFreeUnicodeString(&nt_replaced_name
);
1755 if (status
!= STATUS_SUCCESS
)
1757 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1758 error
= ERROR_FILE_NOT_FOUND
;
1760 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1765 * Open the replacement file for reading, writing, and deleting
1766 * (writing and deleting are needed when finished)
1768 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName
, &nt_replacement_name
, NULL
, NULL
)))
1770 error
= ERROR_PATH_NOT_FOUND
;
1773 attr
.ObjectName
= &nt_replacement_name
;
1774 status
= NtOpenFile(&hReplacement
,
1775 GENERIC_READ
|GENERIC_WRITE
|DELETE
|WRITE_DAC
|SYNCHRONIZE
,
1777 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1778 if (status
== STATUS_SUCCESS
)
1779 status
= wine_nt_to_unix_file_name(&nt_replacement_name
, &unix_replacement_name
, FILE_OPEN
, FALSE
);
1780 RtlFreeUnicodeString(&nt_replacement_name
);
1781 if (status
!= STATUS_SUCCESS
)
1783 error
= RtlNtStatusToDosError(status
);
1787 /* If the user wants a backup then that needs to be performed first */
1788 if (lpBackupFileName
)
1790 UNICODE_STRING nt_backup_name
;
1791 FILE_BASIC_INFORMATION replaced_info
;
1793 /* Obtain the file attributes from the "replaced" file */
1794 status
= NtQueryInformationFile(hReplaced
, &io
, &replaced_info
,
1795 sizeof(replaced_info
),
1796 FileBasicInformation
);
1797 if (status
!= STATUS_SUCCESS
)
1799 error
= RtlNtStatusToDosError(status
);
1803 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName
, &nt_backup_name
, NULL
, NULL
)))
1805 error
= ERROR_PATH_NOT_FOUND
;
1808 attr
.ObjectName
= &nt_backup_name
;
1809 /* Open the backup with permissions to write over it */
1810 status
= NtCreateFile(&hBackup
, GENERIC_WRITE
,
1811 &attr
, &io
, NULL
, replaced_info
.FileAttributes
,
1812 FILE_SHARE_WRITE
, FILE_OPEN_IF
,
1813 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
,
1815 if (status
== STATUS_SUCCESS
)
1816 status
= wine_nt_to_unix_file_name(&nt_backup_name
, &unix_backup_name
, FILE_OPEN_IF
, FALSE
);
1817 RtlFreeUnicodeString(&nt_backup_name
);
1818 if (status
!= STATUS_SUCCESS
)
1820 error
= RtlNtStatusToDosError(status
);
1824 /* If an existing backup exists then copy over it */
1825 if (rename(unix_replaced_name
.Buffer
, unix_backup_name
.Buffer
) == -1)
1827 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
; /* is this correct? */
1833 * Now that the backup has been performed (if requested), copy the replacement
1836 if (rename(unix_replacement_name
.Buffer
, unix_replaced_name
.Buffer
) == -1)
1838 if (errno
== EACCES
)
1840 /* Inappropriate permissions on "replaced", rename will fail */
1841 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1844 /* on failure we need to indicate whether a backup was made */
1845 if (!lpBackupFileName
)
1846 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT
;
1848 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
;
1854 /* Perform resource cleanup */
1856 if (hBackup
) CloseHandle(hBackup
);
1857 if (hReplaced
) CloseHandle(hReplaced
);
1858 if (hReplacement
) CloseHandle(hReplacement
);
1859 RtlFreeAnsiString(&unix_backup_name
);
1860 RtlFreeAnsiString(&unix_replacement_name
);
1861 RtlFreeAnsiString(&unix_replaced_name
);
1863 /* If there was an error, set the error code */
1865 SetLastError(error
);
1870 /**************************************************************************
1871 * ReplaceFileA (KERNEL32.@)
1873 BOOL WINAPI
ReplaceFileA(LPCSTR lpReplacedFileName
,LPCSTR lpReplacementFileName
,
1874 LPCSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1875 LPVOID lpExclude
, LPVOID lpReserved
)
1877 WCHAR
*replacedW
, *replacementW
, *backupW
= NULL
;
1880 /* This function only makes sense when the first two parameters are defined */
1881 if (!lpReplacedFileName
|| !(replacedW
= FILE_name_AtoW( lpReplacedFileName
, TRUE
)))
1883 SetLastError(ERROR_INVALID_PARAMETER
);
1886 if (!lpReplacementFileName
|| !(replacementW
= FILE_name_AtoW( lpReplacementFileName
, TRUE
)))
1888 HeapFree( GetProcessHeap(), 0, replacedW
);
1889 SetLastError(ERROR_INVALID_PARAMETER
);
1892 /* The backup parameter, however, is optional */
1893 if (lpBackupFileName
)
1895 if (!(backupW
= FILE_name_AtoW( lpBackupFileName
, TRUE
)))
1897 HeapFree( GetProcessHeap(), 0, replacedW
);
1898 HeapFree( GetProcessHeap(), 0, replacementW
);
1899 SetLastError(ERROR_INVALID_PARAMETER
);
1903 ret
= ReplaceFileW( replacedW
, replacementW
, backupW
, dwReplaceFlags
, lpExclude
, lpReserved
);
1904 HeapFree( GetProcessHeap(), 0, replacedW
);
1905 HeapFree( GetProcessHeap(), 0, replacementW
);
1906 HeapFree( GetProcessHeap(), 0, backupW
);
1911 /*************************************************************************
1912 * FindFirstFileExW (KERNEL32.@)
1914 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1915 * results as FindExSearchNameMatch
1917 HANDLE WINAPI
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1918 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1919 LPVOID filter
, DWORD flags
)
1922 FIND_FIRST_INFO
*info
= NULL
;
1923 UNICODE_STRING nt_name
;
1924 OBJECT_ATTRIBUTES attr
;
1929 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1933 FIXME("flags not implemented 0x%08x\n", flags
);
1935 if (search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1937 FIXME("search_op not implemented 0x%08x\n", search_op
);
1938 SetLastError( ERROR_INVALID_PARAMETER
);
1939 return INVALID_HANDLE_VALUE
;
1941 if (level
!= FindExInfoStandard
&& level
!= FindExInfoBasic
)
1943 FIXME("info level %d not implemented\n", level
);
1944 SetLastError( ERROR_INVALID_PARAMETER
);
1945 return INVALID_HANDLE_VALUE
;
1948 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1950 SetLastError( ERROR_PATH_NOT_FOUND
);
1951 return INVALID_HANDLE_VALUE
;
1954 if (!(info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
))))
1956 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1960 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1962 static const WCHAR dotW
[] = {'.',0};
1965 /* we still need to check that the directory can be opened */
1969 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1971 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1974 memcpy( dir
, filename
, HIWORD(device
) );
1975 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1977 RtlFreeUnicodeString( &nt_name
);
1978 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: dotW
, &nt_name
, &mask
, NULL
))
1980 HeapFree( GetProcessHeap(), 0, dir
);
1981 SetLastError( ERROR_PATH_NOT_FOUND
);
1984 HeapFree( GetProcessHeap(), 0, dir
);
1985 RtlInitUnicodeString( &info
->mask
, NULL
);
1987 else if (!mask
|| !*mask
)
1989 SetLastError( ERROR_FILE_NOT_FOUND
);
1994 if (!RtlCreateUnicodeString( &info
->mask
, mask
))
1996 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
2000 /* truncate dir name before mask */
2002 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
2005 /* check if path is the root of the drive */
2006 info
->is_root
= FALSE
;
2007 p
= nt_name
.Buffer
+ 4; /* skip \??\ prefix */
2008 if (p
[0] && p
[1] == ':')
2011 while (*p
== '\\') p
++;
2012 info
->is_root
= (*p
== 0);
2015 attr
.Length
= sizeof(attr
);
2016 attr
.RootDirectory
= 0;
2017 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2018 attr
.ObjectName
= &nt_name
;
2019 attr
.SecurityDescriptor
= NULL
;
2020 attr
.SecurityQualityOfService
= NULL
;
2022 status
= NtOpenFile( &info
->handle
, GENERIC_READ
, &attr
, &io
,
2023 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2024 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
2026 if (status
!= STATUS_SUCCESS
)
2028 RtlFreeUnicodeString( &info
->mask
);
2029 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
2030 SetLastError( ERROR_PATH_NOT_FOUND
);
2032 SetLastError( RtlNtStatusToDosError(status
) );
2036 RtlInitializeCriticalSection( &info
->cs
);
2037 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
2038 info
->path
= nt_name
;
2039 info
->magic
= FIND_FIRST_MAGIC
;
2042 info
->data_size
= 0;
2044 info
->search_op
= search_op
;
2045 info
->level
= level
;
2049 WIN32_FIND_DATAW
*wfd
= data
;
2051 memset( wfd
, 0, sizeof(*wfd
) );
2052 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
2053 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
2054 CloseHandle( info
->handle
);
2060 BOOL has_wildcard
= strpbrkW( info
->mask
.Buffer
, wildcardsW
) != NULL
;
2062 info
->data_size
= has_wildcard
? 8192 : max_entry_size
* 2;
2064 while (info
->data_size
)
2066 if (!(info
->data
= HeapAlloc( GetProcessHeap(), 0, info
->data_size
)))
2069 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
2070 return INVALID_HANDLE_VALUE
;
2073 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, info
->data_size
,
2074 FileBothDirectoryInformation
, FALSE
, &info
->mask
, TRUE
);
2078 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
2079 return INVALID_HANDLE_VALUE
;
2082 if (io
.Information
< info
->data_size
- max_entry_size
)
2084 info
->data_size
= 0; /* we read everything */
2086 else if (info
->data_size
< 1024 * 1024)
2088 HeapFree( GetProcessHeap(), 0, info
->data
);
2089 info
->data_size
*= 2;
2094 info
->data_len
= io
.Information
;
2095 if (!info
->data_size
&& has_wildcard
) /* release unused buffer space */
2096 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, info
->data
, info
->data_len
);
2098 if (!FindNextFileW( info
, data
))
2100 TRACE( "%s not found\n", debugstr_w(filename
) );
2102 SetLastError( ERROR_FILE_NOT_FOUND
);
2103 return INVALID_HANDLE_VALUE
;
2105 if (!has_wildcard
) /* we can't find two files with the same name */
2107 CloseHandle( info
->handle
);
2108 HeapFree( GetProcessHeap(), 0, info
->data
);
2116 HeapFree( GetProcessHeap(), 0, info
);
2117 RtlFreeUnicodeString( &nt_name
);
2118 return INVALID_HANDLE_VALUE
;
2122 /*************************************************************************
2123 * FindNextFileW (KERNEL32.@)
2125 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
2127 FIND_FIRST_INFO
*info
;
2128 FILE_BOTH_DIR_INFORMATION
*dir_info
;
2131 TRACE("%p %p\n", handle
, data
);
2133 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
2135 SetLastError( ERROR_INVALID_HANDLE
);
2139 if (info
->magic
!= FIND_FIRST_MAGIC
)
2141 SetLastError( ERROR_INVALID_HANDLE
);
2145 RtlEnterCriticalSection( &info
->cs
);
2147 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
2150 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
2154 if (info
->data_size
)
2155 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, info
->data_size
,
2156 FileBothDirectoryInformation
, FALSE
, &info
->mask
, FALSE
);
2158 io
.u
.Status
= STATUS_NO_MORE_FILES
;
2162 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
2163 if (io
.u
.Status
== STATUS_NO_MORE_FILES
)
2165 CloseHandle( info
->handle
);
2166 HeapFree( GetProcessHeap(), 0, info
->data
);
2172 info
->data_len
= io
.Information
;
2176 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
2178 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
2179 else info
->data_pos
= info
->data_len
;
2181 /* don't return '.' and '..' in the root of the drive */
2184 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
2185 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
2186 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
2189 /* check for dir symlink */
2190 if ((dir_info
->FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
2191 (dir_info
->FileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
) &&
2192 strpbrkW( info
->mask
.Buffer
, wildcardsW
))
2194 if (!check_dir_symlink( info
, dir_info
)) continue;
2197 data
->dwFileAttributes
= dir_info
->FileAttributes
;
2198 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
2199 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
2200 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
2201 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
2202 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
2203 data
->dwReserved0
= 0;
2204 data
->dwReserved1
= 0;
2206 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
2207 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
2209 if (info
->level
!= FindExInfoBasic
)
2211 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
2212 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
2215 data
->cAlternateFileName
[0] = 0;
2217 TRACE("returning %s (%s)\n",
2218 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
2224 RtlLeaveCriticalSection( &info
->cs
);
2229 /*************************************************************************
2230 * FindClose (KERNEL32.@)
2232 BOOL WINAPI
FindClose( HANDLE handle
)
2234 FIND_FIRST_INFO
*info
= handle
;
2236 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
2238 SetLastError( ERROR_INVALID_HANDLE
);
2244 if (info
->magic
== FIND_FIRST_MAGIC
)
2246 RtlEnterCriticalSection( &info
->cs
);
2247 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
2250 if (info
->handle
) CloseHandle( info
->handle
);
2252 RtlFreeUnicodeString( &info
->mask
);
2253 info
->mask
.Buffer
= NULL
;
2254 RtlFreeUnicodeString( &info
->path
);
2257 HeapFree( GetProcessHeap(), 0, info
->data
);
2258 RtlLeaveCriticalSection( &info
->cs
);
2259 info
->cs
.DebugInfo
->Spare
[0] = 0;
2260 RtlDeleteCriticalSection( &info
->cs
);
2261 HeapFree( GetProcessHeap(), 0, info
);
2267 WARN("Illegal handle %p\n", handle
);
2268 SetLastError( ERROR_INVALID_HANDLE
);
2277 /*************************************************************************
2278 * FindFirstFileA (KERNEL32.@)
2280 HANDLE WINAPI
FindFirstFileA( LPCSTR lpFileName
, WIN32_FIND_DATAA
*lpFindData
)
2282 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
2283 FindExSearchNameMatch
, NULL
, 0);
2286 /*************************************************************************
2287 * FindFirstFileExA (KERNEL32.@)
2289 HANDLE WINAPI
FindFirstFileExA( LPCSTR lpFileName
, FINDEX_INFO_LEVELS fInfoLevelId
,
2290 LPVOID lpFindFileData
, FINDEX_SEARCH_OPS fSearchOp
,
2291 LPVOID lpSearchFilter
, DWORD dwAdditionalFlags
)
2294 WIN32_FIND_DATAA
*dataA
;
2295 WIN32_FIND_DATAW dataW
;
2298 if (!(nameW
= FILE_name_AtoW( lpFileName
, FALSE
))) return INVALID_HANDLE_VALUE
;
2300 handle
= FindFirstFileExW(nameW
, fInfoLevelId
, &dataW
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
2301 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
2303 dataA
= lpFindFileData
;
2304 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
2305 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
2306 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2307 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2308 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2309 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
2310 FILE_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
2311 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
2312 sizeof(dataA
->cAlternateFileName
) );
2317 /*************************************************************************
2318 * FindFirstFileW (KERNEL32.@)
2320 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
2322 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
2323 FindExSearchNameMatch
, NULL
, 0);
2327 /*************************************************************************
2328 * FindNextFileA (KERNEL32.@)
2330 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
2332 WIN32_FIND_DATAW dataW
;
2334 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
2335 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
2336 data
->ftCreationTime
= dataW
.ftCreationTime
;
2337 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2338 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2339 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2340 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
2341 FILE_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
2342 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
2343 sizeof(data
->cAlternateFileName
) );
2348 /**************************************************************************
2349 * GetFileAttributesW (KERNEL32.@)
2351 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
2353 FILE_BASIC_INFORMATION info
;
2354 UNICODE_STRING nt_name
;
2355 OBJECT_ATTRIBUTES attr
;
2358 TRACE("%s\n", debugstr_w(name
));
2360 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2362 SetLastError( ERROR_PATH_NOT_FOUND
);
2363 return INVALID_FILE_ATTRIBUTES
;
2366 attr
.Length
= sizeof(attr
);
2367 attr
.RootDirectory
= 0;
2368 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2369 attr
.ObjectName
= &nt_name
;
2370 attr
.SecurityDescriptor
= NULL
;
2371 attr
.SecurityQualityOfService
= NULL
;
2373 status
= NtQueryAttributesFile( &attr
, &info
);
2374 RtlFreeUnicodeString( &nt_name
);
2376 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
2378 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2379 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
2381 SetLastError( RtlNtStatusToDosError(status
) );
2382 return INVALID_FILE_ATTRIBUTES
;
2386 /**************************************************************************
2387 * GetFileAttributesA (KERNEL32.@)
2389 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
2393 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
2394 return GetFileAttributesW( nameW
);
2398 /**************************************************************************
2399 * SetFileAttributesW (KERNEL32.@)
2401 BOOL WINAPI
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
2403 UNICODE_STRING nt_name
;
2404 OBJECT_ATTRIBUTES attr
;
2409 TRACE("%s %x\n", debugstr_w(name
), attributes
);
2411 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2413 SetLastError( ERROR_PATH_NOT_FOUND
);
2417 attr
.Length
= sizeof(attr
);
2418 attr
.RootDirectory
= 0;
2419 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2420 attr
.ObjectName
= &nt_name
;
2421 attr
.SecurityDescriptor
= NULL
;
2422 attr
.SecurityQualityOfService
= NULL
;
2424 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2425 RtlFreeUnicodeString( &nt_name
);
2427 if (status
== STATUS_SUCCESS
)
2429 FILE_BASIC_INFORMATION info
;
2431 memset( &info
, 0, sizeof(info
) );
2432 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2433 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2437 if (status
== STATUS_SUCCESS
) return TRUE
;
2438 SetLastError( RtlNtStatusToDosError(status
) );
2443 /**************************************************************************
2444 * SetFileAttributesA (KERNEL32.@)
2446 BOOL WINAPI
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2450 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2451 return SetFileAttributesW( nameW
, attributes
);
2455 /**************************************************************************
2456 * GetFileAttributesExW (KERNEL32.@)
2458 BOOL WINAPI
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2460 FILE_NETWORK_OPEN_INFORMATION info
;
2461 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
2462 UNICODE_STRING nt_name
;
2463 OBJECT_ATTRIBUTES attr
;
2466 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
2468 if (level
!= GetFileExInfoStandard
)
2470 SetLastError( ERROR_INVALID_PARAMETER
);
2474 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2476 SetLastError( ERROR_PATH_NOT_FOUND
);
2480 attr
.Length
= sizeof(attr
);
2481 attr
.RootDirectory
= 0;
2482 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2483 attr
.ObjectName
= &nt_name
;
2484 attr
.SecurityDescriptor
= NULL
;
2485 attr
.SecurityQualityOfService
= NULL
;
2487 status
= NtQueryFullAttributesFile( &attr
, &info
);
2488 RtlFreeUnicodeString( &nt_name
);
2490 if (status
!= STATUS_SUCCESS
)
2492 SetLastError( RtlNtStatusToDosError(status
) );
2496 data
->dwFileAttributes
= info
.FileAttributes
;
2497 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
2498 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
2499 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
2500 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
2501 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
2502 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
2503 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
2504 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
2509 /**************************************************************************
2510 * GetFileAttributesExA (KERNEL32.@)
2512 BOOL WINAPI
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2516 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2517 return GetFileAttributesExW( nameW
, level
, ptr
);
2521 /******************************************************************************
2522 * GetCompressedFileSizeW (KERNEL32.@)
2524 * Get the actual number of bytes used on disk.
2527 * Success: Low-order doubleword of number of bytes
2528 * Failure: INVALID_FILE_SIZE
2530 DWORD WINAPI
GetCompressedFileSizeW(
2531 LPCWSTR name
, /* [in] Pointer to name of file */
2532 LPDWORD size_high
) /* [out] Receives high-order doubleword of size */
2534 UNICODE_STRING nt_name
;
2535 OBJECT_ATTRIBUTES attr
;
2539 DWORD ret
= INVALID_FILE_SIZE
;
2541 TRACE("%s %p\n", debugstr_w(name
), size_high
);
2543 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2545 SetLastError( ERROR_PATH_NOT_FOUND
);
2546 return INVALID_FILE_SIZE
;
2549 attr
.Length
= sizeof(attr
);
2550 attr
.RootDirectory
= 0;
2551 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2552 attr
.ObjectName
= &nt_name
;
2553 attr
.SecurityDescriptor
= NULL
;
2554 attr
.SecurityQualityOfService
= NULL
;
2556 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2557 RtlFreeUnicodeString( &nt_name
);
2559 if (status
== STATUS_SUCCESS
)
2561 /* we don't support compressed files, simply return the file size */
2562 ret
= GetFileSize( handle
, size_high
);
2565 else SetLastError( RtlNtStatusToDosError(status
) );
2571 /******************************************************************************
2572 * GetCompressedFileSizeA (KERNEL32.@)
2574 * See GetCompressedFileSizeW.
2576 DWORD WINAPI
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
2580 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
2581 return GetCompressedFileSizeW( nameW
, size_high
);
2585 /***********************************************************************
2586 * OpenVxDHandle (KERNEL32.@)
2588 * This function is supposed to return the corresponding Ring 0
2589 * ("kernel") handle for a Ring 3 handle in Win9x.
2590 * Evidently, Wine will have problems with this. But we try anyway,
2593 HANDLE WINAPI
OpenVxDHandle(HANDLE hHandleRing3
)
2595 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3
);
2596 return hHandleRing3
;
2600 /****************************************************************************
2601 * DeviceIoControl (KERNEL32.@)
2603 BOOL WINAPI
DeviceIoControl(HANDLE hDevice
, DWORD dwIoControlCode
,
2604 LPVOID lpvInBuffer
, DWORD cbInBuffer
,
2605 LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
2606 LPDWORD lpcbBytesReturned
,
2607 LPOVERLAPPED lpOverlapped
)
2611 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2612 hDevice
,dwIoControlCode
,lpvInBuffer
,cbInBuffer
,
2613 lpvOutBuffer
,cbOutBuffer
,lpcbBytesReturned
,lpOverlapped
);
2615 /* Check if this is a user defined control code for a VxD */
2617 if (HIWORD( dwIoControlCode
) == 0 && (GetVersion() & 0x80000000))
2619 typedef BOOL (WINAPI
*DeviceIoProc
)(DWORD
, LPVOID
, DWORD
, LPVOID
, DWORD
, LPDWORD
, LPOVERLAPPED
);
2620 static DeviceIoProc (*vxd_get_proc
)(HANDLE
);
2621 DeviceIoProc proc
= NULL
;
2623 if (!vxd_get_proc
) vxd_get_proc
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2624 "__wine_vxd_get_proc" );
2625 if (vxd_get_proc
) proc
= vxd_get_proc( hDevice
);
2626 if (proc
) return proc( dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2627 lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
);
2630 /* Not a VxD, let ntdll handle it */
2634 LPVOID cvalue
= ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) ? NULL
: lpOverlapped
;
2635 lpOverlapped
->Internal
= STATUS_PENDING
;
2636 lpOverlapped
->InternalHigh
= 0;
2637 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2638 status
= NtFsControlFile(hDevice
, lpOverlapped
->hEvent
,
2639 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2640 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2641 lpvOutBuffer
, cbOutBuffer
);
2643 status
= NtDeviceIoControlFile(hDevice
, lpOverlapped
->hEvent
,
2644 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2645 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2646 lpvOutBuffer
, cbOutBuffer
);
2647 if (lpcbBytesReturned
) *lpcbBytesReturned
= lpOverlapped
->InternalHigh
;
2651 IO_STATUS_BLOCK iosb
;
2653 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2654 status
= NtFsControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2655 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2656 lpvOutBuffer
, cbOutBuffer
);
2658 status
= NtDeviceIoControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2659 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2660 lpvOutBuffer
, cbOutBuffer
);
2661 if (lpcbBytesReturned
) *lpcbBytesReturned
= iosb
.Information
;
2663 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2668 /***********************************************************************
2669 * OpenFile (KERNEL32.@)
2671 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
2675 WORD filedatetime
[2];
2677 if (!ofs
) return HFILE_ERROR
;
2679 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
2680 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
2681 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
2682 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
2683 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
2684 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
2685 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
2686 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
2687 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
2688 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
2689 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
2690 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
2691 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
2692 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
2693 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
2694 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
2695 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
2696 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
2700 ofs
->cBytes
= sizeof(OFSTRUCT
);
2702 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
2704 if (!name
) return HFILE_ERROR
;
2706 TRACE("%s %04x\n", name
, mode
);
2708 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2709 Are there any cases where getting the path here is wrong?
2710 Uwe Bonnes 1997 Apr 2 */
2711 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
)) goto error
;
2713 /* OF_PARSE simply fills the structure */
2715 if (mode
& OF_PARSE
)
2717 ofs
->fFixedDisk
= (GetDriveTypeA( ofs
->szPathName
) != DRIVE_REMOVABLE
);
2718 TRACE("(%s): OF_PARSE, res = '%s'\n", name
, ofs
->szPathName
);
2722 /* OF_CREATE is completely different from all other options, so
2725 if (mode
& OF_CREATE
)
2727 if ((handle
= create_file_OF( name
, mode
)) == INVALID_HANDLE_VALUE
)
2732 /* Now look for the file */
2734 if (!SearchPathA( NULL
, name
, NULL
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
))
2737 TRACE("found %s\n", debugstr_a(ofs
->szPathName
) );
2739 if (mode
& OF_DELETE
)
2741 if (!DeleteFileA( ofs
->szPathName
)) goto error
;
2742 TRACE("(%s): OF_DELETE return = OK\n", name
);
2746 handle
= LongToHandle(_lopen( ofs
->szPathName
, mode
));
2747 if (handle
== INVALID_HANDLE_VALUE
) goto error
;
2749 GetFileTime( handle
, NULL
, NULL
, &filetime
);
2750 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
2751 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
2753 if (ofs
->Reserved1
!= filedatetime
[0] || ofs
->Reserved2
!= filedatetime
[1] )
2755 CloseHandle( handle
);
2756 WARN("(%s): OF_VERIFY failed\n", name
);
2757 /* FIXME: what error here? */
2758 SetLastError( ERROR_FILE_NOT_FOUND
);
2762 ofs
->Reserved1
= filedatetime
[0];
2763 ofs
->Reserved2
= filedatetime
[1];
2765 TRACE("(%s): OK, return = %p\n", name
, handle
);
2766 if (mode
& OF_EXIST
) /* Return TRUE instead of a handle */
2768 CloseHandle( handle
);
2771 return HandleToLong(handle
);
2773 error
: /* We get here if there was an error opening the file */
2774 ofs
->nErrCode
= GetLastError();
2775 WARN("(%s): return = HFILE_ERROR error= %d\n", name
,ofs
->nErrCode
);
2780 /***********************************************************************
2781 * OpenFileById (KERNEL32.@)
2783 HANDLE WINAPI
OpenFileById( HANDLE handle
, LPFILE_ID_DESCRIPTOR id
, DWORD access
,
2784 DWORD share
, LPSECURITY_ATTRIBUTES sec_attr
, DWORD flags
)
2788 OBJECT_ATTRIBUTES attr
;
2791 UNICODE_STRING objectName
;
2795 SetLastError( ERROR_INVALID_PARAMETER
);
2796 return INVALID_HANDLE_VALUE
;
2799 options
= FILE_OPEN_BY_FILE_ID
;
2800 if (flags
& FILE_FLAG_BACKUP_SEMANTICS
)
2801 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
2803 options
|= FILE_NON_DIRECTORY_FILE
;
2804 if (flags
& FILE_FLAG_NO_BUFFERING
) options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
2805 if (!(flags
& FILE_FLAG_OVERLAPPED
)) options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
2806 if (flags
& FILE_FLAG_RANDOM_ACCESS
) options
|= FILE_RANDOM_ACCESS
;
2807 flags
&= FILE_ATTRIBUTE_VALID_FLAGS
;
2809 objectName
.Length
= sizeof(ULONGLONG
);
2810 objectName
.Buffer
= (WCHAR
*)&id
->u
.FileId
;
2811 attr
.Length
= sizeof(attr
);
2812 attr
.RootDirectory
= handle
;
2813 attr
.Attributes
= 0;
2814 attr
.ObjectName
= &objectName
;
2815 attr
.SecurityDescriptor
= sec_attr
? sec_attr
->lpSecurityDescriptor
: NULL
;
2816 attr
.SecurityQualityOfService
= NULL
;
2817 if (sec_attr
&& sec_attr
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
2819 status
= NtCreateFile( &result
, access
, &attr
, &io
, NULL
, flags
,
2820 share
, OPEN_EXISTING
, options
, NULL
, 0 );
2821 if (status
!= STATUS_SUCCESS
)
2823 SetLastError( RtlNtStatusToDosError( status
) );
2824 return INVALID_HANDLE_VALUE
;
2830 /***********************************************************************
2831 * K32EnumDeviceDrivers (KERNEL32.@)
2833 BOOL WINAPI
K32EnumDeviceDrivers(void **image_base
, DWORD cb
, DWORD
*needed
)
2835 FIXME("(%p, %d, %p): stub\n", image_base
, cb
, needed
);
2843 /***********************************************************************
2844 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2846 DWORD WINAPI
K32GetDeviceDriverBaseNameA(void *image_base
, LPSTR base_name
, DWORD size
)
2848 FIXME("(%p, %p, %d): stub\n", image_base
, base_name
, size
);
2850 if (base_name
&& size
)
2851 base_name
[0] = '\0';
2856 /***********************************************************************
2857 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2859 DWORD WINAPI
K32GetDeviceDriverBaseNameW(void *image_base
, LPWSTR base_name
, DWORD size
)
2861 FIXME("(%p, %p, %d): stub\n", image_base
, base_name
, size
);
2863 if (base_name
&& size
)
2864 base_name
[0] = '\0';
2869 /***********************************************************************
2870 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2872 DWORD WINAPI
K32GetDeviceDriverFileNameA(void *image_base
, LPSTR file_name
, DWORD size
)
2874 FIXME("(%p, %p, %d): stub\n", image_base
, file_name
, size
);
2876 if (file_name
&& size
)
2877 file_name
[0] = '\0';
2882 /***********************************************************************
2883 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2885 DWORD WINAPI
K32GetDeviceDriverFileNameW(void *image_base
, LPWSTR file_name
, DWORD size
)
2887 FIXME("(%p, %p, %d): stub\n", image_base
, file_name
, size
);
2889 if (file_name
&& size
)
2890 file_name
[0] = '\0';