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
)
629 status
= lpOverlapped
->Internal
;
630 if (status
== STATUS_PENDING
) status
= STATUS_SUCCESS
;
633 *lpTransferred
= lpOverlapped
->InternalHigh
;
635 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
639 /***********************************************************************
640 * CancelIoEx (KERNEL32.@)
642 * Cancels pending I/O operations on a file given the overlapped used.
645 * handle [I] File handle.
646 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
650 * Failure: FALSE, check GetLastError().
652 BOOL WINAPI
CancelIoEx(HANDLE handle
, LPOVERLAPPED lpOverlapped
)
654 IO_STATUS_BLOCK io_status
;
656 NtCancelIoFileEx(handle
, (PIO_STATUS_BLOCK
) lpOverlapped
, &io_status
);
657 if (io_status
.u
.Status
)
659 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
665 /***********************************************************************
666 * CancelIo (KERNEL32.@)
668 * Cancels pending I/O operations initiated by the current thread on a file.
671 * handle [I] File handle.
675 * Failure: FALSE, check GetLastError().
677 BOOL WINAPI
CancelIo(HANDLE handle
)
679 IO_STATUS_BLOCK io_status
;
681 NtCancelIoFile(handle
, &io_status
);
682 if (io_status
.u
.Status
)
684 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
690 /***********************************************************************
691 * CancelSynchronousIo (KERNEL32.@)
693 * Marks pending synchronous I/O operations issued by the specified thread as cancelled
696 * handle [I] handle to the thread whose I/O operations should be cancelled
700 * Failure: FALSE, check GetLastError().
702 BOOL WINAPI
CancelSynchronousIo(HANDLE thread
)
704 FIXME("(%p): stub\n", thread
);
705 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
709 /***********************************************************************
710 * _hread (KERNEL32.@)
712 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
714 return _lread( hFile
, buffer
, count
);
718 /***********************************************************************
719 * _hwrite (KERNEL32.@)
721 * experimentation yields that _lwrite:
722 * o truncates the file at the current position with
724 * o returns 0 on a 0 length write
725 * o works with console handles
728 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
732 TRACE("%d %p %d\n", handle
, buffer
, count
);
736 /* Expand or truncate at current position */
737 if (!SetEndOfFile( LongToHandle(handle
) )) return HFILE_ERROR
;
740 if (!WriteFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
746 /***********************************************************************
747 * _lclose (KERNEL32.@)
749 HFILE WINAPI
_lclose( HFILE hFile
)
751 TRACE("handle %d\n", hFile
);
752 return CloseHandle( LongToHandle(hFile
) ) ? 0 : HFILE_ERROR
;
756 /***********************************************************************
757 * _lcreat (KERNEL32.@)
759 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
763 /* Mask off all flags not explicitly allowed by the doc */
764 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
765 TRACE("%s %02x\n", path
, attr
);
766 hfile
= CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
767 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
768 CREATE_ALWAYS
, attr
, 0 );
769 return HandleToLong(hfile
);
773 /***********************************************************************
774 * _lopen (KERNEL32.@)
776 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
780 TRACE("(%s,%04x)\n", debugstr_a(path
), mode
);
781 hfile
= create_file_OF( path
, mode
& ~OF_CREATE
);
782 return HandleToLong(hfile
);
785 /***********************************************************************
786 * _lread (KERNEL32.@)
788 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
791 if (!ReadFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
797 /***********************************************************************
798 * _llseek (KERNEL32.@)
800 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
802 return SetFilePointer( LongToHandle(hFile
), lOffset
, NULL
, nOrigin
);
806 /***********************************************************************
807 * _lwrite (KERNEL32.@)
809 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
811 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
815 /***********************************************************************
816 * FlushFileBuffers (KERNEL32.@)
818 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
821 IO_STATUS_BLOCK ioblk
;
823 if (is_console_handle( hFile
))
825 /* this will fail (as expected) for an output handle */
826 return FlushConsoleInputBuffer( hFile
);
828 nts
= NtFlushBuffersFile( hFile
, &ioblk
);
829 if (nts
!= STATUS_SUCCESS
)
831 SetLastError( RtlNtStatusToDosError( nts
) );
839 /***********************************************************************
840 * GetFileType (KERNEL32.@)
842 DWORD WINAPI
GetFileType( HANDLE hFile
)
844 FILE_FS_DEVICE_INFORMATION info
;
848 if (hFile
== (HANDLE
)STD_INPUT_HANDLE
|| hFile
== (HANDLE
)STD_OUTPUT_HANDLE
849 || hFile
== (HANDLE
)STD_ERROR_HANDLE
)
850 hFile
= GetStdHandle((DWORD_PTR
)hFile
);
852 if (is_console_handle( hFile
)) return FILE_TYPE_CHAR
;
854 status
= NtQueryVolumeInformationFile( hFile
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
855 if (status
!= STATUS_SUCCESS
)
857 SetLastError( RtlNtStatusToDosError(status
) );
858 return FILE_TYPE_UNKNOWN
;
861 switch(info
.DeviceType
)
863 case FILE_DEVICE_NULL
:
864 case FILE_DEVICE_SERIAL_PORT
:
865 case FILE_DEVICE_PARALLEL_PORT
:
866 case FILE_DEVICE_TAPE
:
867 case FILE_DEVICE_UNKNOWN
:
868 return FILE_TYPE_CHAR
;
869 case FILE_DEVICE_NAMED_PIPE
:
870 return FILE_TYPE_PIPE
;
872 return FILE_TYPE_DISK
;
877 /***********************************************************************
878 * GetFileInformationByHandle (KERNEL32.@)
880 BOOL WINAPI
GetFileInformationByHandle( HANDLE hFile
, BY_HANDLE_FILE_INFORMATION
*info
)
882 FILE_ALL_INFORMATION all_info
;
886 status
= NtQueryInformationFile( hFile
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
887 if (status
== STATUS_BUFFER_OVERFLOW
) status
= STATUS_SUCCESS
;
888 if (status
== STATUS_SUCCESS
)
890 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
891 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
892 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
893 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
894 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
895 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
896 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
897 info
->dwVolumeSerialNumber
= 0; /* FIXME */
898 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
899 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
900 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
901 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
902 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
905 SetLastError( RtlNtStatusToDosError(status
) );
910 /***********************************************************************
911 * GetFileInformationByHandleEx (KERNEL32.@)
913 BOOL WINAPI
GetFileInformationByHandleEx( HANDLE handle
, FILE_INFO_BY_HANDLE_CLASS
class,
914 LPVOID info
, DWORD size
)
922 case FileCompressionInfo
:
923 case FileAttributeTagInfo
:
924 case FileRemoteProtocolInfo
:
925 case FileFullDirectoryInfo
:
926 case FileFullDirectoryRestartInfo
:
927 case FileStorageInfo
:
928 case FileAlignmentInfo
:
930 case FileIdExtdDirectoryInfo
:
931 case FileIdExtdDirectoryRestartInfo
:
932 FIXME( "%p, %u, %p, %u\n", handle
, class, info
, size
);
933 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
937 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileBasicInformation
);
940 case FileStandardInfo
:
941 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileStandardInformation
);
945 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileNameInformation
);
948 case FileIdBothDirectoryRestartInfo
:
949 case FileIdBothDirectoryInfo
:
950 status
= NtQueryDirectoryFile( handle
, NULL
, NULL
, NULL
, &io
, info
, size
,
951 FileIdBothDirectoryInformation
, FALSE
, NULL
,
952 (class == FileIdBothDirectoryRestartInfo
) );
956 case FileDispositionInfo
:
957 case FileAllocationInfo
:
958 case FileIoPriorityHintInfo
:
959 case FileEndOfFileInfo
:
961 SetLastError( ERROR_INVALID_PARAMETER
);
965 if (status
!= STATUS_SUCCESS
)
967 SetLastError( RtlNtStatusToDosError( status
) );
974 /***********************************************************************
975 * GetFileSize (KERNEL32.@)
977 * Retrieve the size of a file.
980 * hFile [I] File to retrieve size of.
981 * filesizehigh [O] On return, the high bits of the file size.
984 * Success: The low bits of the file size.
985 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
986 * check GetLastError() for values other than ERROR_SUCCESS.
988 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
991 if (!GetFileSizeEx( hFile
, &size
)) return INVALID_FILE_SIZE
;
992 if (filesizehigh
) *filesizehigh
= size
.u
.HighPart
;
993 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError(0);
994 return size
.u
.LowPart
;
998 /***********************************************************************
999 * GetFileSizeEx (KERNEL32.@)
1001 * Retrieve the size of a file.
1004 * hFile [I] File to retrieve size of.
1005 * lpFileSIze [O] On return, the size of the file.
1009 * Failure: FALSE, check GetLastError().
1011 BOOL WINAPI
GetFileSizeEx( HANDLE hFile
, PLARGE_INTEGER lpFileSize
)
1013 FILE_STANDARD_INFORMATION info
;
1017 if (is_console_handle( hFile
))
1019 SetLastError( ERROR_INVALID_HANDLE
);
1023 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileStandardInformation
);
1024 if (status
== STATUS_SUCCESS
)
1026 *lpFileSize
= info
.EndOfFile
;
1029 SetLastError( RtlNtStatusToDosError(status
) );
1034 /**************************************************************************
1035 * SetEndOfFile (KERNEL32.@)
1037 * Sets the current position as the end of the file.
1040 * hFile [I] File handle.
1044 * Failure: FALSE, check GetLastError().
1046 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
1048 FILE_POSITION_INFORMATION pos
;
1049 FILE_END_OF_FILE_INFORMATION eof
;
1053 status
= NtQueryInformationFile( hFile
, &io
, &pos
, sizeof(pos
), FilePositionInformation
);
1054 if (status
== STATUS_SUCCESS
)
1056 eof
.EndOfFile
= pos
.CurrentByteOffset
;
1057 status
= NtSetInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
1059 if (status
== STATUS_SUCCESS
) return TRUE
;
1060 SetLastError( RtlNtStatusToDosError(status
) );
1064 /**************************************************************************
1065 * SetFileCompletionNotificationModes (KERNEL32.@)
1067 BOOL WINAPI
SetFileCompletionNotificationModes( HANDLE handle
, UCHAR flags
)
1069 FIXME("%p %x - stub\n", handle
, flags
);
1070 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1075 /***********************************************************************
1076 * SetFileInformationByHandle (KERNEL32.@)
1078 BOOL WINAPI
SetFileInformationByHandle( HANDLE file
, FILE_INFO_BY_HANDLE_CLASS
class, VOID
*info
, DWORD size
)
1083 TRACE( "%p %u %p %u\n", file
, class, info
, size
);
1089 case FileRenameInfo
:
1090 case FileAllocationInfo
:
1091 case FileEndOfFileInfo
:
1092 case FileStreamInfo
:
1093 case FileIdBothDirectoryInfo
:
1094 case FileIdBothDirectoryRestartInfo
:
1095 case FileIoPriorityHintInfo
:
1096 case FileFullDirectoryInfo
:
1097 case FileFullDirectoryRestartInfo
:
1098 case FileStorageInfo
:
1099 case FileAlignmentInfo
:
1101 case FileIdExtdDirectoryInfo
:
1102 case FileIdExtdDirectoryRestartInfo
:
1103 FIXME( "%p, %u, %p, %u\n", file
, class, info
, size
);
1104 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1107 case FileDispositionInfo
:
1108 status
= NtSetInformationFile( file
, &io
, info
, size
, FileDispositionInformation
);
1111 case FileStandardInfo
:
1112 case FileCompressionInfo
:
1113 case FileAttributeTagInfo
:
1114 case FileRemoteProtocolInfo
:
1116 SetLastError( ERROR_INVALID_PARAMETER
);
1120 if (status
!= STATUS_SUCCESS
)
1122 SetLastError( RtlNtStatusToDosError( status
) );
1129 /***********************************************************************
1130 * SetFilePointer (KERNEL32.@)
1132 DWORD WINAPI DECLSPEC_HOTPATCH
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
, DWORD method
)
1134 LARGE_INTEGER dist
, newpos
;
1138 dist
.u
.LowPart
= distance
;
1139 dist
.u
.HighPart
= *highword
;
1141 else dist
.QuadPart
= distance
;
1143 if (!SetFilePointerEx( hFile
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
1145 if (highword
) *highword
= newpos
.u
.HighPart
;
1146 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
1147 return newpos
.u
.LowPart
;
1151 /***********************************************************************
1152 * SetFilePointerEx (KERNEL32.@)
1154 BOOL WINAPI
SetFilePointerEx( HANDLE hFile
, LARGE_INTEGER distance
,
1155 LARGE_INTEGER
*newpos
, DWORD method
)
1159 FILE_POSITION_INFORMATION info
;
1164 pos
= distance
.QuadPart
;
1167 if (NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
1169 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
1173 FILE_END_OF_FILE_INFORMATION eof
;
1174 if (NtQueryInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
1176 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
1180 SetLastError( ERROR_INVALID_PARAMETER
);
1186 SetLastError( ERROR_NEGATIVE_SEEK
);
1190 info
.CurrentByteOffset
.QuadPart
= pos
;
1191 if (NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
1193 if (newpos
) newpos
->QuadPart
= pos
;
1197 SetLastError( RtlNtStatusToDosError(io
.u
.Status
) );
1201 /***********************************************************************
1202 * SetFileValidData (KERNEL32.@)
1204 BOOL WINAPI
SetFileValidData( HANDLE hFile
, LONGLONG ValidDataLength
)
1206 FILE_VALID_DATA_LENGTH_INFORMATION info
;
1210 info
.ValidDataLength
.QuadPart
= ValidDataLength
;
1211 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileValidDataLengthInformation
);
1213 if (status
== STATUS_SUCCESS
) return TRUE
;
1214 SetLastError( RtlNtStatusToDosError(status
) );
1218 /***********************************************************************
1219 * GetFileTime (KERNEL32.@)
1221 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
1222 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
1224 FILE_BASIC_INFORMATION info
;
1228 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1229 if (status
== STATUS_SUCCESS
)
1233 lpCreationTime
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1234 lpCreationTime
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1236 if (lpLastAccessTime
)
1238 lpLastAccessTime
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1239 lpLastAccessTime
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1241 if (lpLastWriteTime
)
1243 lpLastWriteTime
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1244 lpLastWriteTime
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1248 SetLastError( RtlNtStatusToDosError(status
) );
1253 /***********************************************************************
1254 * SetFileTime (KERNEL32.@)
1256 BOOL WINAPI
SetFileTime( HANDLE hFile
, const FILETIME
*ctime
,
1257 const FILETIME
*atime
, const FILETIME
*mtime
)
1259 FILE_BASIC_INFORMATION info
;
1263 memset( &info
, 0, sizeof(info
) );
1266 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
1267 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
1271 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
1272 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
1276 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
1277 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
1280 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1281 if (status
== STATUS_SUCCESS
) return TRUE
;
1282 SetLastError( RtlNtStatusToDosError(status
) );
1287 /**************************************************************************
1288 * LockFile (KERNEL32.@)
1290 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1291 DWORD count_low
, DWORD count_high
)
1294 LARGE_INTEGER count
, offset
;
1296 TRACE( "%p %x%08x %x%08x\n",
1297 hFile
, offset_high
, offset_low
, count_high
, count_low
);
1299 count
.u
.LowPart
= count_low
;
1300 count
.u
.HighPart
= count_high
;
1301 offset
.u
.LowPart
= offset_low
;
1302 offset
.u
.HighPart
= offset_high
;
1304 status
= NtLockFile( hFile
, 0, NULL
, NULL
,
1305 NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
);
1307 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1312 /**************************************************************************
1313 * LockFileEx [KERNEL32.@]
1315 * Locks a byte range within an open file for shared or exclusive access.
1322 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1324 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1325 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
1328 LARGE_INTEGER count
, offset
;
1329 LPVOID cvalue
= NULL
;
1333 SetLastError( ERROR_INVALID_PARAMETER
);
1337 TRACE( "%p %x%08x %x%08x flags %x\n",
1338 hFile
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
,
1339 count_high
, count_low
, flags
);
1341 count
.u
.LowPart
= count_low
;
1342 count
.u
.HighPart
= count_high
;
1343 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
1344 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
1346 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1348 status
= NtLockFile( hFile
, overlapped
->hEvent
, NULL
, cvalue
,
1349 NULL
, &offset
, &count
, NULL
,
1350 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
1351 flags
& LOCKFILE_EXCLUSIVE_LOCK
);
1353 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1358 /**************************************************************************
1359 * UnlockFile (KERNEL32.@)
1361 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1362 DWORD count_low
, DWORD count_high
)
1365 LARGE_INTEGER count
, offset
;
1367 count
.u
.LowPart
= count_low
;
1368 count
.u
.HighPart
= count_high
;
1369 offset
.u
.LowPart
= offset_low
;
1370 offset
.u
.HighPart
= offset_high
;
1372 status
= NtUnlockFile( hFile
, NULL
, &offset
, &count
, NULL
);
1373 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1378 /**************************************************************************
1379 * UnlockFileEx (KERNEL32.@)
1381 BOOL WINAPI
UnlockFileEx( HANDLE hFile
, DWORD reserved
, DWORD count_low
, DWORD count_high
,
1382 LPOVERLAPPED overlapped
)
1386 SetLastError( ERROR_INVALID_PARAMETER
);
1389 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
1391 return UnlockFile( hFile
, overlapped
->u
.s
.Offset
, overlapped
->u
.s
.OffsetHigh
, count_low
, count_high
);
1395 /*************************************************************************
1396 * SetHandleCount (KERNEL32.@)
1398 UINT WINAPI
SetHandleCount( UINT count
)
1404 /**************************************************************************
1405 * Operations on file names *
1406 **************************************************************************/
1409 /*************************************************************************
1410 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1412 * Creates or opens an object, and returns a handle that can be used to
1413 * access that object.
1417 * filename [in] pointer to filename to be accessed
1418 * access [in] access mode requested
1419 * sharing [in] share mode
1420 * sa [in] pointer to security attributes
1421 * creation [in] how to create the file
1422 * attributes [in] attributes for newly created file
1423 * template [in] handle to file with extended attributes to copy
1426 * Success: Open handle to specified file
1427 * Failure: INVALID_HANDLE_VALUE
1429 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
1430 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1431 DWORD attributes
, HANDLE
template )
1435 OBJECT_ATTRIBUTES attr
;
1436 UNICODE_STRING nameW
;
1440 const WCHAR
*vxd_name
= NULL
;
1441 static const WCHAR bkslashes_with_dotW
[] = {'\\','\\','.','\\',0};
1442 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
1443 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
1444 SECURITY_QUALITY_OF_SERVICE qos
;
1446 static const UINT nt_disposition
[5] =
1448 FILE_CREATE
, /* CREATE_NEW */
1449 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
1450 FILE_OPEN
, /* OPEN_EXISTING */
1451 FILE_OPEN_IF
, /* OPEN_ALWAYS */
1452 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
1458 if (!filename
|| !filename
[0])
1460 SetLastError( ERROR_PATH_NOT_FOUND
);
1461 return INVALID_HANDLE_VALUE
;
1464 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
1465 (access
& GENERIC_READ
)?"GENERIC_READ ":"",
1466 (access
& GENERIC_WRITE
)?"GENERIC_WRITE ":"",
1467 (access
& GENERIC_EXECUTE
)?"GENERIC_EXECUTE ":"",
1468 (!access
)?"QUERY_ACCESS ":"",
1469 (sharing
& FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
1470 (sharing
& FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
1471 (sharing
& FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
1472 creation
, attributes
);
1474 /* Open a console for CONIN$ or CONOUT$ */
1476 if (!strcmpiW(filename
, coninW
) || !strcmpiW(filename
, conoutW
))
1478 ret
= OpenConsoleW(filename
, access
, (sa
&& sa
->bInheritHandle
),
1479 creation
? OPEN_EXISTING
: 0);
1480 if (ret
== INVALID_HANDLE_VALUE
) SetLastError(ERROR_INVALID_PARAMETER
);
1484 if (!strncmpW(filename
, bkslashes_with_dotW
, 4))
1486 static const WCHAR pipeW
[] = {'P','I','P','E','\\',0};
1487 static const WCHAR mailslotW
[] = {'M','A','I','L','S','L','O','T','\\',0};
1489 if ((isalphaW(filename
[4]) && filename
[5] == ':' && filename
[6] == '\0') ||
1490 !strncmpiW( filename
+ 4, pipeW
, 5 ) ||
1491 !strncmpiW( filename
+ 4, mailslotW
, 9 ))
1495 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
1497 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
1499 else if (GetVersion() & 0x80000000)
1501 vxd_name
= filename
+ 4;
1502 if (!creation
) creation
= OPEN_EXISTING
;
1505 else dosdev
= RtlIsDosDeviceName_U( filename
);
1509 static const WCHAR conW
[] = {'C','O','N'};
1511 if (LOWORD(dosdev
) == sizeof(conW
) &&
1512 !memicmpW( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), conW
, sizeof(conW
)/sizeof(WCHAR
)))
1514 switch (access
& (GENERIC_READ
|GENERIC_WRITE
))
1517 ret
= OpenConsoleW(coninW
, access
, (sa
&& sa
->bInheritHandle
), OPEN_EXISTING
);
1520 ret
= OpenConsoleW(conoutW
, access
, (sa
&& sa
->bInheritHandle
), OPEN_EXISTING
);
1523 SetLastError( ERROR_FILE_NOT_FOUND
);
1524 return INVALID_HANDLE_VALUE
;
1529 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
1531 SetLastError( ERROR_INVALID_PARAMETER
);
1532 return INVALID_HANDLE_VALUE
;
1535 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
1537 SetLastError( ERROR_PATH_NOT_FOUND
);
1538 return INVALID_HANDLE_VALUE
;
1541 /* now call NtCreateFile */
1544 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
1545 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
1547 options
|= FILE_NON_DIRECTORY_FILE
;
1548 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
1550 options
|= FILE_DELETE_ON_CLOSE
;
1553 if (attributes
& FILE_FLAG_NO_BUFFERING
)
1554 options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
1555 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
1556 options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
1557 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
1558 options
|= FILE_RANDOM_ACCESS
;
1559 attributes
&= FILE_ATTRIBUTE_VALID_FLAGS
;
1561 attr
.Length
= sizeof(attr
);
1562 attr
.RootDirectory
= 0;
1563 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1564 attr
.ObjectName
= &nameW
;
1565 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1566 if (attributes
& SECURITY_SQOS_PRESENT
)
1568 qos
.Length
= sizeof(qos
);
1569 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
1570 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
1571 qos
.EffectiveOnly
= (attributes
& SECURITY_EFFECTIVE_ONLY
) != 0;
1572 attr
.SecurityQualityOfService
= &qos
;
1575 attr
.SecurityQualityOfService
= NULL
;
1577 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
1579 status
= NtCreateFile( &ret
, access
| SYNCHRONIZE
, &attr
, &io
, NULL
, attributes
,
1580 sharing
, nt_disposition
[creation
- CREATE_NEW
],
1584 if (vxd_name
&& vxd_name
[0])
1586 static HANDLE (*vxd_open
)(LPCWSTR
,DWORD
,SECURITY_ATTRIBUTES
*);
1587 if (!vxd_open
) vxd_open
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1588 "__wine_vxd_open" );
1589 if (vxd_open
&& (ret
= vxd_open( vxd_name
, access
, sa
))) goto done
;
1592 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
1593 ret
= INVALID_HANDLE_VALUE
;
1595 /* In the case file creation was rejected due to CREATE_NEW flag
1596 * was specified and file with that name already exists, correct
1597 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1598 * Note: RtlNtStatusToDosError is not the subject to blame here.
1600 if (status
== STATUS_OBJECT_NAME_COLLISION
)
1601 SetLastError( ERROR_FILE_EXISTS
);
1603 SetLastError( RtlNtStatusToDosError(status
) );
1607 if ((creation
== CREATE_ALWAYS
&& io
.Information
== FILE_OVERWRITTEN
) ||
1608 (creation
== OPEN_ALWAYS
&& io
.Information
== FILE_OPENED
))
1609 SetLastError( ERROR_ALREADY_EXISTS
);
1613 RtlFreeUnicodeString( &nameW
);
1616 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
1617 TRACE("returning %p\n", ret
);
1623 /*************************************************************************
1624 * CreateFileA (KERNEL32.@)
1628 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
1629 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1630 DWORD attributes
, HANDLE
template)
1634 if ((GetVersion() & 0x80000000) && IsBadStringPtrA(filename
, -1)) return INVALID_HANDLE_VALUE
;
1635 if (!(nameW
= FILE_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1636 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
1639 /*************************************************************************
1640 * CreateFile2 (KERNEL32.@)
1642 HANDLE WINAPI
CreateFile2( LPCWSTR filename
, DWORD access
, DWORD sharing
, DWORD creation
,
1643 CREATEFILE2_EXTENDED_PARAMETERS
*exparams
)
1645 LPSECURITY_ATTRIBUTES sa
= exparams
? exparams
->lpSecurityAttributes
: NULL
;
1646 DWORD attributes
= exparams
? exparams
->dwFileAttributes
: 0;
1647 HANDLE
template = exparams
? exparams
->hTemplateFile
: NULL
;
1649 FIXME("(%s %x %x %x %p), partial stub\n", debugstr_w(filename
), access
, sharing
, creation
, exparams
);
1651 return CreateFileW( filename
, access
, sharing
, sa
, creation
, attributes
, template );
1654 /***********************************************************************
1655 * DeleteFileW (KERNEL32.@)
1660 * path [I] Path to the file to delete.
1664 * Failure: FALSE, check GetLastError().
1666 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1668 UNICODE_STRING nameW
;
1669 OBJECT_ATTRIBUTES attr
;
1674 TRACE("%s\n", debugstr_w(path
) );
1676 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
1678 SetLastError( ERROR_PATH_NOT_FOUND
);
1682 attr
.Length
= sizeof(attr
);
1683 attr
.RootDirectory
= 0;
1684 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1685 attr
.ObjectName
= &nameW
;
1686 attr
.SecurityDescriptor
= NULL
;
1687 attr
.SecurityQualityOfService
= NULL
;
1689 status
= NtCreateFile(&hFile
, SYNCHRONIZE
| DELETE
, &attr
, &io
, NULL
, 0,
1690 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1691 FILE_OPEN
, FILE_DELETE_ON_CLOSE
| FILE_NON_DIRECTORY_FILE
, NULL
, 0);
1692 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
1694 RtlFreeUnicodeString( &nameW
);
1697 SetLastError( RtlNtStatusToDosError(status
) );
1704 /***********************************************************************
1705 * DeleteFileA (KERNEL32.@)
1709 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1713 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1714 return DeleteFileW( pathW
);
1718 /**************************************************************************
1719 * ReplaceFileW (KERNEL32.@)
1720 * ReplaceFile (KERNEL32.@)
1722 BOOL WINAPI
ReplaceFileW(LPCWSTR lpReplacedFileName
, LPCWSTR lpReplacementFileName
,
1723 LPCWSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1724 LPVOID lpExclude
, LPVOID lpReserved
)
1726 UNICODE_STRING nt_replaced_name
, nt_replacement_name
;
1727 ANSI_STRING unix_replaced_name
, unix_replacement_name
, unix_backup_name
;
1728 HANDLE hReplaced
= NULL
, hReplacement
= NULL
, hBackup
= NULL
;
1729 DWORD error
= ERROR_SUCCESS
;
1730 UINT replaced_flags
;
1734 OBJECT_ATTRIBUTES attr
;
1736 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName
),
1737 debugstr_w(lpReplacementFileName
), debugstr_w(lpBackupFileName
),
1738 dwReplaceFlags
, lpExclude
, lpReserved
);
1741 FIXME("Ignoring flags %x\n", dwReplaceFlags
);
1743 /* First two arguments are mandatory */
1744 if (!lpReplacedFileName
|| !lpReplacementFileName
)
1746 SetLastError(ERROR_INVALID_PARAMETER
);
1750 unix_replaced_name
.Buffer
= NULL
;
1751 unix_replacement_name
.Buffer
= NULL
;
1752 unix_backup_name
.Buffer
= NULL
;
1754 attr
.Length
= sizeof(attr
);
1755 attr
.RootDirectory
= 0;
1756 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1757 attr
.ObjectName
= NULL
;
1758 attr
.SecurityDescriptor
= NULL
;
1759 attr
.SecurityQualityOfService
= NULL
;
1761 /* Open the "replaced" file for reading and writing */
1762 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName
, &nt_replaced_name
, NULL
, NULL
)))
1764 error
= ERROR_PATH_NOT_FOUND
;
1767 replaced_flags
= lpBackupFileName
? FILE_OPEN
: FILE_OPEN_IF
;
1768 attr
.ObjectName
= &nt_replaced_name
;
1769 status
= NtOpenFile(&hReplaced
, GENERIC_READ
|GENERIC_WRITE
|DELETE
|SYNCHRONIZE
,
1771 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
1772 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1773 if (status
== STATUS_SUCCESS
)
1774 status
= wine_nt_to_unix_file_name(&nt_replaced_name
, &unix_replaced_name
, replaced_flags
, FALSE
);
1775 RtlFreeUnicodeString(&nt_replaced_name
);
1776 if (status
!= STATUS_SUCCESS
)
1778 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1779 error
= ERROR_FILE_NOT_FOUND
;
1781 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1786 * Open the replacement file for reading, writing, and deleting
1787 * (writing and deleting are needed when finished)
1789 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName
, &nt_replacement_name
, NULL
, NULL
)))
1791 error
= ERROR_PATH_NOT_FOUND
;
1794 attr
.ObjectName
= &nt_replacement_name
;
1795 status
= NtOpenFile(&hReplacement
,
1796 GENERIC_READ
|GENERIC_WRITE
|DELETE
|WRITE_DAC
|SYNCHRONIZE
,
1798 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1799 if (status
== STATUS_SUCCESS
)
1800 status
= wine_nt_to_unix_file_name(&nt_replacement_name
, &unix_replacement_name
, FILE_OPEN
, FALSE
);
1801 RtlFreeUnicodeString(&nt_replacement_name
);
1802 if (status
!= STATUS_SUCCESS
)
1804 error
= RtlNtStatusToDosError(status
);
1808 /* If the user wants a backup then that needs to be performed first */
1809 if (lpBackupFileName
)
1811 UNICODE_STRING nt_backup_name
;
1812 FILE_BASIC_INFORMATION replaced_info
;
1814 /* Obtain the file attributes from the "replaced" file */
1815 status
= NtQueryInformationFile(hReplaced
, &io
, &replaced_info
,
1816 sizeof(replaced_info
),
1817 FileBasicInformation
);
1818 if (status
!= STATUS_SUCCESS
)
1820 error
= RtlNtStatusToDosError(status
);
1824 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName
, &nt_backup_name
, NULL
, NULL
)))
1826 error
= ERROR_PATH_NOT_FOUND
;
1829 attr
.ObjectName
= &nt_backup_name
;
1830 /* Open the backup with permissions to write over it */
1831 status
= NtCreateFile(&hBackup
, GENERIC_WRITE
| SYNCHRONIZE
,
1832 &attr
, &io
, NULL
, replaced_info
.FileAttributes
,
1833 FILE_SHARE_WRITE
, FILE_OPEN_IF
,
1834 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
,
1836 if (status
== STATUS_SUCCESS
)
1837 status
= wine_nt_to_unix_file_name(&nt_backup_name
, &unix_backup_name
, FILE_OPEN_IF
, FALSE
);
1838 RtlFreeUnicodeString(&nt_backup_name
);
1839 if (status
!= STATUS_SUCCESS
)
1841 error
= RtlNtStatusToDosError(status
);
1845 /* If an existing backup exists then copy over it */
1846 if (rename(unix_replaced_name
.Buffer
, unix_backup_name
.Buffer
) == -1)
1848 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
; /* is this correct? */
1854 * Now that the backup has been performed (if requested), copy the replacement
1857 if (rename(unix_replacement_name
.Buffer
, unix_replaced_name
.Buffer
) == -1)
1859 if (errno
== EACCES
)
1861 /* Inappropriate permissions on "replaced", rename will fail */
1862 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1865 /* on failure we need to indicate whether a backup was made */
1866 if (!lpBackupFileName
)
1867 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT
;
1869 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
;
1875 /* Perform resource cleanup */
1877 if (hBackup
) CloseHandle(hBackup
);
1878 if (hReplaced
) CloseHandle(hReplaced
);
1879 if (hReplacement
) CloseHandle(hReplacement
);
1880 RtlFreeAnsiString(&unix_backup_name
);
1881 RtlFreeAnsiString(&unix_replacement_name
);
1882 RtlFreeAnsiString(&unix_replaced_name
);
1884 /* If there was an error, set the error code */
1886 SetLastError(error
);
1891 /**************************************************************************
1892 * ReplaceFileA (KERNEL32.@)
1894 BOOL WINAPI
ReplaceFileA(LPCSTR lpReplacedFileName
,LPCSTR lpReplacementFileName
,
1895 LPCSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1896 LPVOID lpExclude
, LPVOID lpReserved
)
1898 WCHAR
*replacedW
, *replacementW
, *backupW
= NULL
;
1901 /* This function only makes sense when the first two parameters are defined */
1902 if (!lpReplacedFileName
|| !(replacedW
= FILE_name_AtoW( lpReplacedFileName
, TRUE
)))
1904 SetLastError(ERROR_INVALID_PARAMETER
);
1907 if (!lpReplacementFileName
|| !(replacementW
= FILE_name_AtoW( lpReplacementFileName
, TRUE
)))
1909 HeapFree( GetProcessHeap(), 0, replacedW
);
1910 SetLastError(ERROR_INVALID_PARAMETER
);
1913 /* The backup parameter, however, is optional */
1914 if (lpBackupFileName
)
1916 if (!(backupW
= FILE_name_AtoW( lpBackupFileName
, TRUE
)))
1918 HeapFree( GetProcessHeap(), 0, replacedW
);
1919 HeapFree( GetProcessHeap(), 0, replacementW
);
1920 SetLastError(ERROR_INVALID_PARAMETER
);
1924 ret
= ReplaceFileW( replacedW
, replacementW
, backupW
, dwReplaceFlags
, lpExclude
, lpReserved
);
1925 HeapFree( GetProcessHeap(), 0, replacedW
);
1926 HeapFree( GetProcessHeap(), 0, replacementW
);
1927 HeapFree( GetProcessHeap(), 0, backupW
);
1932 /*************************************************************************
1933 * FindFirstFileExW (KERNEL32.@)
1935 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1936 * results as FindExSearchNameMatch
1938 HANDLE WINAPI
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1939 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1940 LPVOID filter
, DWORD flags
)
1943 FIND_FIRST_INFO
*info
= NULL
;
1944 UNICODE_STRING nt_name
;
1945 OBJECT_ATTRIBUTES attr
;
1950 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1954 FIXME("flags not implemented 0x%08x\n", flags
);
1956 if (search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1958 FIXME("search_op not implemented 0x%08x\n", search_op
);
1959 SetLastError( ERROR_INVALID_PARAMETER
);
1960 return INVALID_HANDLE_VALUE
;
1962 if (level
!= FindExInfoStandard
&& level
!= FindExInfoBasic
)
1964 FIXME("info level %d not implemented\n", level
);
1965 SetLastError( ERROR_INVALID_PARAMETER
);
1966 return INVALID_HANDLE_VALUE
;
1969 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1971 SetLastError( ERROR_PATH_NOT_FOUND
);
1972 return INVALID_HANDLE_VALUE
;
1975 if (!(info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
))))
1977 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1981 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1983 static const WCHAR dotW
[] = {'.',0};
1986 /* we still need to check that the directory can be opened */
1990 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1992 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1995 memcpy( dir
, filename
, HIWORD(device
) );
1996 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1998 RtlFreeUnicodeString( &nt_name
);
1999 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: dotW
, &nt_name
, &mask
, NULL
))
2001 HeapFree( GetProcessHeap(), 0, dir
);
2002 SetLastError( ERROR_PATH_NOT_FOUND
);
2005 HeapFree( GetProcessHeap(), 0, dir
);
2006 RtlInitUnicodeString( &info
->mask
, NULL
);
2008 else if (!mask
|| !*mask
)
2010 SetLastError( ERROR_FILE_NOT_FOUND
);
2015 if (!RtlCreateUnicodeString( &info
->mask
, mask
))
2017 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
2021 /* truncate dir name before mask */
2023 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
2026 /* check if path is the root of the drive */
2027 info
->is_root
= FALSE
;
2028 p
= nt_name
.Buffer
+ 4; /* skip \??\ prefix */
2029 if (p
[0] && p
[1] == ':')
2032 while (*p
== '\\') p
++;
2033 info
->is_root
= (*p
== 0);
2036 attr
.Length
= sizeof(attr
);
2037 attr
.RootDirectory
= 0;
2038 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2039 attr
.ObjectName
= &nt_name
;
2040 attr
.SecurityDescriptor
= NULL
;
2041 attr
.SecurityQualityOfService
= NULL
;
2043 status
= NtOpenFile( &info
->handle
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &io
,
2044 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2045 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
2047 if (status
!= STATUS_SUCCESS
)
2049 RtlFreeUnicodeString( &info
->mask
);
2050 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
2051 SetLastError( ERROR_PATH_NOT_FOUND
);
2053 SetLastError( RtlNtStatusToDosError(status
) );
2057 RtlInitializeCriticalSection( &info
->cs
);
2058 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
2059 info
->path
= nt_name
;
2060 info
->magic
= FIND_FIRST_MAGIC
;
2063 info
->data_size
= 0;
2065 info
->search_op
= search_op
;
2066 info
->level
= level
;
2070 WIN32_FIND_DATAW
*wfd
= data
;
2072 memset( wfd
, 0, sizeof(*wfd
) );
2073 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
2074 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
2075 CloseHandle( info
->handle
);
2080 BOOL has_wildcard
= strpbrkW( info
->mask
.Buffer
, wildcardsW
) != NULL
;
2082 info
->data_size
= has_wildcard
? 8192 : max_entry_size
* 2;
2084 while (info
->data_size
)
2086 if (!(info
->data
= HeapAlloc( GetProcessHeap(), 0, info
->data_size
)))
2089 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
2090 return INVALID_HANDLE_VALUE
;
2093 status
= NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, info
->data_size
,
2094 FileBothDirectoryInformation
, FALSE
, &info
->mask
, TRUE
);
2098 SetLastError( RtlNtStatusToDosError( status
) );
2099 return INVALID_HANDLE_VALUE
;
2102 if (io
.Information
< info
->data_size
- max_entry_size
)
2104 info
->data_size
= 0; /* we read everything */
2106 else if (info
->data_size
< 1024 * 1024)
2108 HeapFree( GetProcessHeap(), 0, info
->data
);
2109 info
->data_size
*= 2;
2114 info
->data_len
= io
.Information
;
2115 if (!info
->data_size
&& has_wildcard
) /* release unused buffer space */
2116 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, info
->data
, info
->data_len
);
2118 if (!FindNextFileW( info
, data
))
2120 TRACE( "%s not found\n", debugstr_w(filename
) );
2122 SetLastError( ERROR_FILE_NOT_FOUND
);
2123 return INVALID_HANDLE_VALUE
;
2125 if (!has_wildcard
) /* we can't find two files with the same name */
2127 CloseHandle( info
->handle
);
2128 HeapFree( GetProcessHeap(), 0, info
->data
);
2136 HeapFree( GetProcessHeap(), 0, info
);
2137 RtlFreeUnicodeString( &nt_name
);
2138 return INVALID_HANDLE_VALUE
;
2142 /*************************************************************************
2143 * FindNextFileW (KERNEL32.@)
2145 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
2147 FIND_FIRST_INFO
*info
;
2148 FILE_BOTH_DIR_INFORMATION
*dir_info
;
2152 TRACE("%p %p\n", handle
, data
);
2154 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
2156 SetLastError( ERROR_INVALID_HANDLE
);
2160 if (info
->magic
!= FIND_FIRST_MAGIC
)
2162 SetLastError( ERROR_INVALID_HANDLE
);
2166 RtlEnterCriticalSection( &info
->cs
);
2168 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
2171 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
2175 if (info
->data_size
)
2176 status
= NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, info
->data_size
,
2177 FileBothDirectoryInformation
, FALSE
, &info
->mask
, FALSE
);
2179 status
= STATUS_NO_MORE_FILES
;
2183 SetLastError( RtlNtStatusToDosError( status
) );
2184 if (status
== STATUS_NO_MORE_FILES
)
2186 CloseHandle( info
->handle
);
2187 HeapFree( GetProcessHeap(), 0, info
->data
);
2193 info
->data_len
= io
.Information
;
2197 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
2199 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
2200 else info
->data_pos
= info
->data_len
;
2202 /* don't return '.' and '..' in the root of the drive */
2205 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
2206 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
2207 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
2210 /* check for dir symlink */
2211 if ((dir_info
->FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
2212 (dir_info
->FileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
) &&
2213 strpbrkW( info
->mask
.Buffer
, wildcardsW
))
2215 if (!check_dir_symlink( info
, dir_info
)) continue;
2218 data
->dwFileAttributes
= dir_info
->FileAttributes
;
2219 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
2220 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
2221 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
2222 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
2223 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
2224 data
->dwReserved0
= 0;
2225 data
->dwReserved1
= 0;
2227 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
2228 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
2230 if (info
->level
!= FindExInfoBasic
)
2232 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
2233 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
2236 data
->cAlternateFileName
[0] = 0;
2238 TRACE("returning %s (%s)\n",
2239 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
2245 RtlLeaveCriticalSection( &info
->cs
);
2250 /*************************************************************************
2251 * FindClose (KERNEL32.@)
2253 BOOL WINAPI
FindClose( HANDLE handle
)
2255 FIND_FIRST_INFO
*info
= handle
;
2257 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
2259 SetLastError( ERROR_INVALID_HANDLE
);
2265 if (info
->magic
== FIND_FIRST_MAGIC
)
2267 RtlEnterCriticalSection( &info
->cs
);
2268 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
2271 if (info
->handle
) CloseHandle( info
->handle
);
2273 RtlFreeUnicodeString( &info
->mask
);
2274 info
->mask
.Buffer
= NULL
;
2275 RtlFreeUnicodeString( &info
->path
);
2278 HeapFree( GetProcessHeap(), 0, info
->data
);
2279 RtlLeaveCriticalSection( &info
->cs
);
2280 info
->cs
.DebugInfo
->Spare
[0] = 0;
2281 RtlDeleteCriticalSection( &info
->cs
);
2282 HeapFree( GetProcessHeap(), 0, info
);
2288 WARN("Illegal handle %p\n", handle
);
2289 SetLastError( ERROR_INVALID_HANDLE
);
2298 /*************************************************************************
2299 * FindFirstFileA (KERNEL32.@)
2301 HANDLE WINAPI
FindFirstFileA( LPCSTR lpFileName
, WIN32_FIND_DATAA
*lpFindData
)
2303 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
2304 FindExSearchNameMatch
, NULL
, 0);
2307 /*************************************************************************
2308 * FindFirstFileExA (KERNEL32.@)
2310 HANDLE WINAPI
FindFirstFileExA( LPCSTR lpFileName
, FINDEX_INFO_LEVELS fInfoLevelId
,
2311 LPVOID lpFindFileData
, FINDEX_SEARCH_OPS fSearchOp
,
2312 LPVOID lpSearchFilter
, DWORD dwAdditionalFlags
)
2315 WIN32_FIND_DATAA
*dataA
;
2316 WIN32_FIND_DATAW dataW
;
2319 if (!(nameW
= FILE_name_AtoW( lpFileName
, FALSE
))) return INVALID_HANDLE_VALUE
;
2321 handle
= FindFirstFileExW(nameW
, fInfoLevelId
, &dataW
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
2322 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
2324 dataA
= lpFindFileData
;
2325 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
2326 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
2327 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2328 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2329 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2330 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
2331 FILE_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
2332 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
2333 sizeof(dataA
->cAlternateFileName
) );
2338 /*************************************************************************
2339 * FindFirstFileW (KERNEL32.@)
2341 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
2343 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
2344 FindExSearchNameMatch
, NULL
, 0);
2348 /*************************************************************************
2349 * FindNextFileA (KERNEL32.@)
2351 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
2353 WIN32_FIND_DATAW dataW
;
2355 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
2356 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
2357 data
->ftCreationTime
= dataW
.ftCreationTime
;
2358 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2359 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2360 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2361 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
2362 FILE_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
2363 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
2364 sizeof(data
->cAlternateFileName
) );
2369 /**************************************************************************
2370 * GetFileAttributesW (KERNEL32.@)
2372 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
2374 FILE_BASIC_INFORMATION info
;
2375 UNICODE_STRING nt_name
;
2376 OBJECT_ATTRIBUTES attr
;
2379 TRACE("%s\n", debugstr_w(name
));
2381 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2383 SetLastError( ERROR_PATH_NOT_FOUND
);
2384 return INVALID_FILE_ATTRIBUTES
;
2387 attr
.Length
= sizeof(attr
);
2388 attr
.RootDirectory
= 0;
2389 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2390 attr
.ObjectName
= &nt_name
;
2391 attr
.SecurityDescriptor
= NULL
;
2392 attr
.SecurityQualityOfService
= NULL
;
2394 status
= NtQueryAttributesFile( &attr
, &info
);
2395 RtlFreeUnicodeString( &nt_name
);
2397 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
2399 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2400 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
2402 SetLastError( RtlNtStatusToDosError(status
) );
2403 return INVALID_FILE_ATTRIBUTES
;
2407 /**************************************************************************
2408 * GetFileAttributesA (KERNEL32.@)
2410 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
2414 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
2415 return GetFileAttributesW( nameW
);
2419 /**************************************************************************
2420 * SetFileAttributesW (KERNEL32.@)
2422 BOOL WINAPI
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
2424 UNICODE_STRING nt_name
;
2425 OBJECT_ATTRIBUTES attr
;
2430 TRACE("%s %x\n", debugstr_w(name
), attributes
);
2432 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2434 SetLastError( ERROR_PATH_NOT_FOUND
);
2438 attr
.Length
= sizeof(attr
);
2439 attr
.RootDirectory
= 0;
2440 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2441 attr
.ObjectName
= &nt_name
;
2442 attr
.SecurityDescriptor
= NULL
;
2443 attr
.SecurityQualityOfService
= NULL
;
2445 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2446 RtlFreeUnicodeString( &nt_name
);
2448 if (status
== STATUS_SUCCESS
)
2450 FILE_BASIC_INFORMATION info
;
2452 memset( &info
, 0, sizeof(info
) );
2453 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2454 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2458 if (status
== STATUS_SUCCESS
) return TRUE
;
2459 SetLastError( RtlNtStatusToDosError(status
) );
2464 /**************************************************************************
2465 * SetFileAttributesA (KERNEL32.@)
2467 BOOL WINAPI
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2471 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2472 return SetFileAttributesW( nameW
, attributes
);
2476 /**************************************************************************
2477 * GetFileAttributesExW (KERNEL32.@)
2479 BOOL WINAPI
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2481 FILE_NETWORK_OPEN_INFORMATION info
;
2482 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
2483 UNICODE_STRING nt_name
;
2484 OBJECT_ATTRIBUTES attr
;
2487 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
2489 if (level
!= GetFileExInfoStandard
)
2491 SetLastError( ERROR_INVALID_PARAMETER
);
2495 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2497 SetLastError( ERROR_PATH_NOT_FOUND
);
2501 attr
.Length
= sizeof(attr
);
2502 attr
.RootDirectory
= 0;
2503 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2504 attr
.ObjectName
= &nt_name
;
2505 attr
.SecurityDescriptor
= NULL
;
2506 attr
.SecurityQualityOfService
= NULL
;
2508 status
= NtQueryFullAttributesFile( &attr
, &info
);
2509 RtlFreeUnicodeString( &nt_name
);
2511 if (status
!= STATUS_SUCCESS
)
2513 SetLastError( RtlNtStatusToDosError(status
) );
2517 data
->dwFileAttributes
= info
.FileAttributes
;
2518 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
2519 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
2520 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
2521 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
2522 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
2523 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
2524 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
2525 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
2530 /**************************************************************************
2531 * GetFileAttributesExA (KERNEL32.@)
2533 BOOL WINAPI
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2537 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2538 return GetFileAttributesExW( nameW
, level
, ptr
);
2542 /******************************************************************************
2543 * GetCompressedFileSizeW (KERNEL32.@)
2545 * Get the actual number of bytes used on disk.
2548 * Success: Low-order doubleword of number of bytes
2549 * Failure: INVALID_FILE_SIZE
2551 DWORD WINAPI
GetCompressedFileSizeW(
2552 LPCWSTR name
, /* [in] Pointer to name of file */
2553 LPDWORD size_high
) /* [out] Receives high-order doubleword of size */
2555 UNICODE_STRING nt_name
;
2556 OBJECT_ATTRIBUTES attr
;
2560 DWORD ret
= INVALID_FILE_SIZE
;
2562 TRACE("%s %p\n", debugstr_w(name
), size_high
);
2564 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2566 SetLastError( ERROR_PATH_NOT_FOUND
);
2567 return INVALID_FILE_SIZE
;
2570 attr
.Length
= sizeof(attr
);
2571 attr
.RootDirectory
= 0;
2572 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2573 attr
.ObjectName
= &nt_name
;
2574 attr
.SecurityDescriptor
= NULL
;
2575 attr
.SecurityQualityOfService
= NULL
;
2577 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2578 RtlFreeUnicodeString( &nt_name
);
2580 if (status
== STATUS_SUCCESS
)
2582 /* we don't support compressed files, simply return the file size */
2583 ret
= GetFileSize( handle
, size_high
);
2586 else SetLastError( RtlNtStatusToDosError(status
) );
2592 /******************************************************************************
2593 * GetCompressedFileSizeA (KERNEL32.@)
2595 * See GetCompressedFileSizeW.
2597 DWORD WINAPI
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
2601 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
2602 return GetCompressedFileSizeW( nameW
, size_high
);
2606 /***********************************************************************
2607 * OpenVxDHandle (KERNEL32.@)
2609 * This function is supposed to return the corresponding Ring 0
2610 * ("kernel") handle for a Ring 3 handle in Win9x.
2611 * Evidently, Wine will have problems with this. But we try anyway,
2614 HANDLE WINAPI
OpenVxDHandle(HANDLE hHandleRing3
)
2616 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3
);
2617 return hHandleRing3
;
2621 /****************************************************************************
2622 * DeviceIoControl (KERNEL32.@)
2624 BOOL WINAPI
DeviceIoControl(HANDLE hDevice
, DWORD dwIoControlCode
,
2625 LPVOID lpvInBuffer
, DWORD cbInBuffer
,
2626 LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
2627 LPDWORD lpcbBytesReturned
,
2628 LPOVERLAPPED lpOverlapped
)
2632 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2633 hDevice
,dwIoControlCode
,lpvInBuffer
,cbInBuffer
,
2634 lpvOutBuffer
,cbOutBuffer
,lpcbBytesReturned
,lpOverlapped
);
2636 /* Check if this is a user defined control code for a VxD */
2638 if (HIWORD( dwIoControlCode
) == 0 && (GetVersion() & 0x80000000))
2640 typedef BOOL (WINAPI
*DeviceIoProc
)(DWORD
, LPVOID
, DWORD
, LPVOID
, DWORD
, LPDWORD
, LPOVERLAPPED
);
2641 static DeviceIoProc (*vxd_get_proc
)(HANDLE
);
2642 DeviceIoProc proc
= NULL
;
2644 if (!vxd_get_proc
) vxd_get_proc
= (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2645 "__wine_vxd_get_proc" );
2646 if (vxd_get_proc
) proc
= vxd_get_proc( hDevice
);
2647 if (proc
) return proc( dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2648 lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
);
2651 /* Not a VxD, let ntdll handle it */
2655 LPVOID cvalue
= ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) ? NULL
: lpOverlapped
;
2656 lpOverlapped
->Internal
= STATUS_PENDING
;
2657 lpOverlapped
->InternalHigh
= 0;
2658 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2659 status
= NtFsControlFile(hDevice
, lpOverlapped
->hEvent
,
2660 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2661 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2662 lpvOutBuffer
, cbOutBuffer
);
2664 status
= NtDeviceIoControlFile(hDevice
, lpOverlapped
->hEvent
,
2665 NULL
, cvalue
, (PIO_STATUS_BLOCK
)lpOverlapped
,
2666 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2667 lpvOutBuffer
, cbOutBuffer
);
2668 if (lpcbBytesReturned
) *lpcbBytesReturned
= lpOverlapped
->InternalHigh
;
2672 IO_STATUS_BLOCK iosb
;
2674 if (HIWORD(dwIoControlCode
) == FILE_DEVICE_FILE_SYSTEM
)
2675 status
= NtFsControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2676 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2677 lpvOutBuffer
, cbOutBuffer
);
2679 status
= NtDeviceIoControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
2680 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
2681 lpvOutBuffer
, cbOutBuffer
);
2682 if (lpcbBytesReturned
) *lpcbBytesReturned
= iosb
.Information
;
2684 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2689 /***********************************************************************
2690 * OpenFile (KERNEL32.@)
2692 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
2696 WORD filedatetime
[2];
2698 if (!ofs
) return HFILE_ERROR
;
2700 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
2701 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
2702 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
2703 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
2704 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
2705 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
2706 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
2707 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
2708 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
2709 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
2710 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
2711 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
2712 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
2713 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
2714 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
2715 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
2716 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
2717 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
2721 ofs
->cBytes
= sizeof(OFSTRUCT
);
2723 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
2725 if (!name
) return HFILE_ERROR
;
2727 TRACE("%s %04x\n", name
, mode
);
2729 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2730 Are there any cases where getting the path here is wrong?
2731 Uwe Bonnes 1997 Apr 2 */
2732 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
)) goto error
;
2734 /* OF_PARSE simply fills the structure */
2736 if (mode
& OF_PARSE
)
2738 ofs
->fFixedDisk
= (GetDriveTypeA( ofs
->szPathName
) != DRIVE_REMOVABLE
);
2739 TRACE("(%s): OF_PARSE, res = '%s'\n", name
, ofs
->szPathName
);
2743 /* OF_CREATE is completely different from all other options, so
2746 if (mode
& OF_CREATE
)
2748 if ((handle
= create_file_OF( name
, mode
)) == INVALID_HANDLE_VALUE
)
2753 /* Now look for the file */
2755 if (!SearchPathA( NULL
, name
, NULL
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
))
2758 TRACE("found %s\n", debugstr_a(ofs
->szPathName
) );
2760 if (mode
& OF_DELETE
)
2762 if (!DeleteFileA( ofs
->szPathName
)) goto error
;
2763 TRACE("(%s): OF_DELETE return = OK\n", name
);
2767 handle
= LongToHandle(_lopen( ofs
->szPathName
, mode
));
2768 if (handle
== INVALID_HANDLE_VALUE
) goto error
;
2770 GetFileTime( handle
, NULL
, NULL
, &filetime
);
2771 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
2772 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
2774 if (ofs
->Reserved1
!= filedatetime
[0] || ofs
->Reserved2
!= filedatetime
[1] )
2776 CloseHandle( handle
);
2777 WARN("(%s): OF_VERIFY failed\n", name
);
2778 /* FIXME: what error here? */
2779 SetLastError( ERROR_FILE_NOT_FOUND
);
2783 ofs
->Reserved1
= filedatetime
[0];
2784 ofs
->Reserved2
= filedatetime
[1];
2786 TRACE("(%s): OK, return = %p\n", name
, handle
);
2787 if (mode
& OF_EXIST
) /* Return TRUE instead of a handle */
2789 CloseHandle( handle
);
2792 return HandleToLong(handle
);
2794 error
: /* We get here if there was an error opening the file */
2795 ofs
->nErrCode
= GetLastError();
2796 WARN("(%s): return = HFILE_ERROR error= %d\n", name
,ofs
->nErrCode
);
2801 /***********************************************************************
2802 * OpenFileById (KERNEL32.@)
2804 HANDLE WINAPI
OpenFileById( HANDLE handle
, LPFILE_ID_DESCRIPTOR id
, DWORD access
,
2805 DWORD share
, LPSECURITY_ATTRIBUTES sec_attr
, DWORD flags
)
2809 OBJECT_ATTRIBUTES attr
;
2812 UNICODE_STRING objectName
;
2816 SetLastError( ERROR_INVALID_PARAMETER
);
2817 return INVALID_HANDLE_VALUE
;
2820 options
= FILE_OPEN_BY_FILE_ID
;
2821 if (flags
& FILE_FLAG_BACKUP_SEMANTICS
)
2822 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
2824 options
|= FILE_NON_DIRECTORY_FILE
;
2825 if (flags
& FILE_FLAG_NO_BUFFERING
) options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
2826 if (!(flags
& FILE_FLAG_OVERLAPPED
)) options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
2827 if (flags
& FILE_FLAG_RANDOM_ACCESS
) options
|= FILE_RANDOM_ACCESS
;
2828 flags
&= FILE_ATTRIBUTE_VALID_FLAGS
;
2830 objectName
.Length
= sizeof(ULONGLONG
);
2831 objectName
.Buffer
= (WCHAR
*)&id
->u
.FileId
;
2832 attr
.Length
= sizeof(attr
);
2833 attr
.RootDirectory
= handle
;
2834 attr
.Attributes
= 0;
2835 attr
.ObjectName
= &objectName
;
2836 attr
.SecurityDescriptor
= sec_attr
? sec_attr
->lpSecurityDescriptor
: NULL
;
2837 attr
.SecurityQualityOfService
= NULL
;
2838 if (sec_attr
&& sec_attr
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
2840 status
= NtCreateFile( &result
, access
| SYNCHRONIZE
, &attr
, &io
, NULL
, flags
,
2841 share
, OPEN_EXISTING
, options
, NULL
, 0 );
2842 if (status
!= STATUS_SUCCESS
)
2844 SetLastError( RtlNtStatusToDosError( status
) );
2845 return INVALID_HANDLE_VALUE
;
2851 /***********************************************************************
2852 * K32EnumDeviceDrivers (KERNEL32.@)
2854 BOOL WINAPI
K32EnumDeviceDrivers(void **image_base
, DWORD cb
, DWORD
*needed
)
2856 FIXME("(%p, %d, %p): stub\n", image_base
, cb
, needed
);
2864 /***********************************************************************
2865 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2867 DWORD WINAPI
K32GetDeviceDriverBaseNameA(void *image_base
, LPSTR base_name
, DWORD size
)
2869 FIXME("(%p, %p, %d): stub\n", image_base
, base_name
, size
);
2871 if (base_name
&& size
)
2872 base_name
[0] = '\0';
2877 /***********************************************************************
2878 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2880 DWORD WINAPI
K32GetDeviceDriverBaseNameW(void *image_base
, LPWSTR base_name
, DWORD size
)
2882 FIXME("(%p, %p, %d): stub\n", image_base
, base_name
, size
);
2884 if (base_name
&& size
)
2885 base_name
[0] = '\0';
2890 /***********************************************************************
2891 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2893 DWORD WINAPI
K32GetDeviceDriverFileNameA(void *image_base
, LPSTR file_name
, DWORD size
)
2895 FIXME("(%p, %p, %d): stub\n", image_base
, file_name
, size
);
2897 if (file_name
&& size
)
2898 file_name
[0] = '\0';
2903 /***********************************************************************
2904 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2906 DWORD WINAPI
K32GetDeviceDriverFileNameW(void *image_base
, LPWSTR file_name
, DWORD size
)
2908 FIXME("(%p, %p, %d): stub\n", image_base
, file_name
, size
);
2910 if (file_name
&& size
)
2911 file_name
[0] = '\0';
2916 /***********************************************************************
2917 * GetFinalPathNameByHandleW (KERNEL32.@)
2919 DWORD WINAPI
GetFinalPathNameByHandleW(HANDLE file
, LPWSTR path
, DWORD charcount
, DWORD flags
)
2921 WCHAR buffer
[sizeof(OBJECT_NAME_INFORMATION
) + MAX_PATH
+ 1];
2922 OBJECT_NAME_INFORMATION
*info
= (OBJECT_NAME_INFORMATION
*)&buffer
;
2923 WCHAR drive_part
[MAX_PATH
];
2924 DWORD drive_part_len
= 0;
2930 TRACE( "(%p,%p,%d,%x)\n", file
, path
, charcount
, flags
);
2932 if (flags
& ~(FILE_NAME_OPENED
| VOLUME_NAME_GUID
| VOLUME_NAME_NONE
| VOLUME_NAME_NT
))
2934 WARN("Unknown flags: %x\n", flags
);
2935 SetLastError( ERROR_INVALID_PARAMETER
);
2939 /* get object name */
2940 status
= NtQueryObject( file
, ObjectNameInformation
, &buffer
, sizeof(buffer
) - sizeof(WCHAR
), &dummy
);
2941 if (status
!= STATUS_SUCCESS
)
2943 SetLastError( RtlNtStatusToDosError( status
) );
2946 if (!info
->Name
.Buffer
)
2948 SetLastError( ERROR_INVALID_HANDLE
);
2951 if (info
->Name
.Length
< 4 * sizeof(WCHAR
) || info
->Name
.Buffer
[0] != '\\' ||
2952 info
->Name
.Buffer
[1] != '?' || info
->Name
.Buffer
[2] != '?' || info
->Name
.Buffer
[3] != '\\' )
2954 FIXME("Unexpected object name: %s\n", debugstr_wn(info
->Name
.Buffer
, info
->Name
.Length
/ sizeof(WCHAR
)));
2955 SetLastError( ERROR_GEN_FAILURE
);
2959 /* add terminating null character, remove "\\??\\" */
2960 info
->Name
.Buffer
[info
->Name
.Length
/ sizeof(WCHAR
)] = 0;
2961 info
->Name
.Length
-= 4 * sizeof(WCHAR
);
2962 info
->Name
.Buffer
+= 4;
2964 /* FILE_NAME_OPENED is not supported yet, and would require Wineserver changes */
2965 if (flags
& FILE_NAME_OPENED
)
2967 FIXME("FILE_NAME_OPENED not supported\n");
2968 flags
&= ~FILE_NAME_OPENED
;
2971 /* Get information required for VOLUME_NAME_NONE, VOLUME_NAME_GUID and VOLUME_NAME_NT */
2972 if (flags
== VOLUME_NAME_NONE
|| flags
== VOLUME_NAME_GUID
|| flags
== VOLUME_NAME_NT
)
2974 if (!GetVolumePathNameW( info
->Name
.Buffer
, drive_part
, MAX_PATH
))
2977 drive_part_len
= strlenW(drive_part
);
2978 if (!drive_part_len
|| drive_part_len
> strlenW(info
->Name
.Buffer
) ||
2979 drive_part
[drive_part_len
-1] != '\\' ||
2980 strncmpiW( info
->Name
.Buffer
, drive_part
, drive_part_len
))
2982 FIXME("Path %s returned by GetVolumePathNameW does not match file path %s\n",
2983 debugstr_w(drive_part
), debugstr_w(info
->Name
.Buffer
));
2984 SetLastError( ERROR_GEN_FAILURE
);
2989 if (flags
== VOLUME_NAME_NONE
)
2991 ptr
= info
->Name
.Buffer
+ drive_part_len
- 1;
2992 result
= strlenW(ptr
);
2993 if (result
< charcount
)
2994 memcpy(path
, ptr
, (result
+ 1) * sizeof(WCHAR
));
2997 else if (flags
== VOLUME_NAME_GUID
)
2999 WCHAR volume_prefix
[51];
3001 /* GetVolumeNameForVolumeMountPointW sets error code on failure */
3002 if (!GetVolumeNameForVolumeMountPointW( drive_part
, volume_prefix
, 50 ))
3005 ptr
= info
->Name
.Buffer
+ drive_part_len
;
3006 result
= strlenW(volume_prefix
) + strlenW(ptr
);
3007 if (result
< charcount
)
3010 strcatW(path
, volume_prefix
);
3015 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3019 else if (flags
== VOLUME_NAME_NT
)
3021 WCHAR nt_prefix
[MAX_PATH
];
3023 /* QueryDosDeviceW sets error code on failure */
3024 drive_part
[drive_part_len
- 1] = 0;
3025 if (!QueryDosDeviceW( drive_part
, nt_prefix
, MAX_PATH
))
3028 ptr
= info
->Name
.Buffer
+ drive_part_len
- 1;
3029 result
= strlenW(nt_prefix
) + strlenW(ptr
);
3030 if (result
< charcount
)
3033 strcatW(path
, nt_prefix
);
3038 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3042 else if (flags
== VOLUME_NAME_DOS
)
3044 static const WCHAR dos_prefix
[] = {'\\','\\','?','\\', '\0'};
3046 result
= strlenW(dos_prefix
) + strlenW(info
->Name
.Buffer
);
3047 if (result
< charcount
)
3050 strcatW(path
, dos_prefix
);
3051 strcatW(path
, info
->Name
.Buffer
);
3055 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3061 /* Windows crashes here, but we prefer returning ERROR_INVALID_PARAMETER */
3062 WARN("Invalid combination of flags: %x\n", flags
);
3063 SetLastError( ERROR_INVALID_PARAMETER
);
3069 /***********************************************************************
3070 * GetFinalPathNameByHandleA (KERNEL32.@)
3072 DWORD WINAPI
GetFinalPathNameByHandleA(HANDLE file
, LPSTR path
, DWORD charcount
, DWORD flags
)
3075 DWORD result
, len
, cp
;
3077 TRACE( "(%p,%p,%d,%x)\n", file
, path
, charcount
, flags
);
3079 len
= GetFinalPathNameByHandleW(file
, NULL
, 0, flags
);
3083 str
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3086 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3090 result
= GetFinalPathNameByHandleW(file
, str
, len
, flags
);
3091 if (result
!= len
- 1)
3093 HeapFree(GetProcessHeap(), 0, str
);
3094 WARN("GetFinalPathNameByHandleW failed unexpectedly: %u\n", result
);
3098 cp
= oem_file_apis
? CP_OEMCP
: CP_ACP
;
3100 len
= WideCharToMultiByte(cp
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
3103 HeapFree(GetProcessHeap(), 0, str
);
3104 WARN("Failed to get multibyte length\n");
3108 if (charcount
< len
)
3110 HeapFree(GetProcessHeap(), 0, str
);
3114 len
= WideCharToMultiByte(cp
, 0, str
, -1, path
, charcount
, NULL
, NULL
);
3117 HeapFree(GetProcessHeap(), 0, str
);
3118 WARN("WideCharToMultiByte failed\n");
3122 HeapFree(GetProcessHeap(), 0, str
);