2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
38 #include "wine/winbase16.h"
39 #include "kernel_private.h"
41 #include "wine/exception.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(file
);
49 HANDLE dos_handles
[DOS_TABLE_SIZE
];
51 /* info structure for FindFirstFile handle */
54 DWORD magic
; /* magic number */
55 HANDLE handle
; /* handle to directory */
56 CRITICAL_SECTION cs
; /* crit section protecting this structure */
57 UNICODE_STRING mask
; /* file mask */
58 BOOL is_root
; /* is directory the root of the drive? */
59 UINT data_pos
; /* current position in dir data */
60 UINT data_len
; /* length of dir data */
61 BYTE data
[8192]; /* directory data */
64 #define FIND_FIRST_MAGIC 0xc0ffee11
66 static BOOL oem_file_apis
;
68 static WINE_EXCEPTION_FILTER(page_fault
)
70 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
71 return EXCEPTION_EXECUTE_HANDLER
;
72 return EXCEPTION_CONTINUE_SEARCH
;
76 /***********************************************************************
79 * Wrapper for CreateFile that takes OF_* mode flags.
81 static HANDLE
create_file_OF( LPCSTR path
, INT mode
)
83 DWORD access
, sharing
, creation
;
87 creation
= CREATE_ALWAYS
;
88 access
= GENERIC_READ
| GENERIC_WRITE
;
92 creation
= OPEN_EXISTING
;
95 case OF_READ
: access
= GENERIC_READ
; break;
96 case OF_WRITE
: access
= GENERIC_WRITE
; break;
97 case OF_READWRITE
: access
= GENERIC_READ
| GENERIC_WRITE
; break;
98 default: access
= 0; break;
104 case OF_SHARE_EXCLUSIVE
: sharing
= 0; break;
105 case OF_SHARE_DENY_WRITE
: sharing
= FILE_SHARE_READ
; break;
106 case OF_SHARE_DENY_READ
: sharing
= FILE_SHARE_WRITE
; break;
107 case OF_SHARE_DENY_NONE
:
108 case OF_SHARE_COMPAT
:
109 default: sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
111 return CreateFileA( path
, access
, sharing
, NULL
, creation
, FILE_ATTRIBUTE_NORMAL
, 0 );
115 /***********************************************************************
118 * Set the DOS error code from errno.
120 void FILE_SetDosError(void)
122 int save_errno
= errno
; /* errno gets overwritten by printf */
124 TRACE("errno = %d %s\n", errno
, strerror(errno
));
128 SetLastError( ERROR_SHARING_VIOLATION
);
131 SetLastError( ERROR_INVALID_HANDLE
);
134 SetLastError( ERROR_HANDLE_DISK_FULL
);
139 SetLastError( ERROR_ACCESS_DENIED
);
142 SetLastError( ERROR_LOCK_VIOLATION
);
145 SetLastError( ERROR_FILE_NOT_FOUND
);
148 SetLastError( ERROR_CANNOT_MAKE
);
152 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
155 SetLastError( ERROR_FILE_EXISTS
);
159 SetLastError( ERROR_SEEK
);
162 SetLastError( ERROR_DIR_NOT_EMPTY
);
165 SetLastError( ERROR_BAD_FORMAT
);
168 SetLastError( ERROR_PATH_NOT_FOUND
);
171 SetLastError( ERROR_NOT_SAME_DEVICE
);
174 WARN("unknown file error: %s\n", strerror(save_errno
) );
175 SetLastError( ERROR_GEN_FAILURE
);
182 /***********************************************************************
185 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
187 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
188 * there is no possibility for the function to do that twice, taking into
189 * account any called function.
191 WCHAR
*FILE_name_AtoW( LPCSTR name
, BOOL alloc
)
194 UNICODE_STRING strW
, *pstrW
;
197 RtlInitAnsiString( &str
, name
);
198 pstrW
= alloc
? &strW
: &NtCurrentTeb()->StaticUnicodeString
;
200 status
= RtlOemStringToUnicodeString( pstrW
, &str
, alloc
);
202 status
= RtlAnsiStringToUnicodeString( pstrW
, &str
, alloc
);
203 if (status
== STATUS_SUCCESS
) return pstrW
->Buffer
;
205 if (status
== STATUS_BUFFER_OVERFLOW
)
206 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
208 SetLastError( RtlNtStatusToDosError(status
) );
213 /***********************************************************************
216 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
218 DWORD
FILE_name_WtoA( LPCWSTR src
, INT srclen
, LPSTR dest
, INT destlen
)
222 if (srclen
< 0) srclen
= strlenW( src
) + 1;
224 RtlUnicodeToOemN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
226 RtlUnicodeToMultiByteN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
231 /**************************************************************************
232 * SetFileApisToOEM (KERNEL32.@)
234 VOID WINAPI
SetFileApisToOEM(void)
236 oem_file_apis
= TRUE
;
240 /**************************************************************************
241 * SetFileApisToANSI (KERNEL32.@)
243 VOID WINAPI
SetFileApisToANSI(void)
245 oem_file_apis
= FALSE
;
249 /******************************************************************************
250 * AreFileApisANSI (KERNEL32.@)
252 * Determines if file functions are using ANSI
255 * TRUE: Set of file functions is using ANSI code page
256 * FALSE: Set of file functions is using OEM code page
258 BOOL WINAPI
AreFileApisANSI(void)
260 return !oem_file_apis
;
264 /**************************************************************************
265 * Operations on file handles *
266 **************************************************************************/
268 /***********************************************************************
269 * FILE_InitProcessDosHandles
271 * Allocates the default DOS handles for a process. Called either by
272 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
274 static void FILE_InitProcessDosHandles( void )
276 static BOOL init_done
/* = FALSE */;
277 HANDLE cp
= GetCurrentProcess();
279 if (init_done
) return;
281 DuplicateHandle(cp
, GetStdHandle(STD_INPUT_HANDLE
), cp
, &dos_handles
[0],
282 0, TRUE
, DUPLICATE_SAME_ACCESS
);
283 DuplicateHandle(cp
, GetStdHandle(STD_OUTPUT_HANDLE
), cp
, &dos_handles
[1],
284 0, TRUE
, DUPLICATE_SAME_ACCESS
);
285 DuplicateHandle(cp
, GetStdHandle(STD_ERROR_HANDLE
), cp
, &dos_handles
[2],
286 0, TRUE
, DUPLICATE_SAME_ACCESS
);
287 DuplicateHandle(cp
, GetStdHandle(STD_ERROR_HANDLE
), cp
, &dos_handles
[3],
288 0, TRUE
, DUPLICATE_SAME_ACCESS
);
289 DuplicateHandle(cp
, GetStdHandle(STD_ERROR_HANDLE
), cp
, &dos_handles
[4],
290 0, TRUE
, DUPLICATE_SAME_ACCESS
);
294 /******************************************************************
295 * FILE_ReadWriteApc (internal)
297 static void WINAPI
FILE_ReadWriteApc(void* apc_user
, PIO_STATUS_BLOCK io_status
, ULONG len
)
299 LPOVERLAPPED_COMPLETION_ROUTINE cr
= (LPOVERLAPPED_COMPLETION_ROUTINE
)apc_user
;
301 cr(RtlNtStatusToDosError(io_status
->u
.Status
), len
, (LPOVERLAPPED
)io_status
);
305 /***********************************************************************
306 * ReadFileEx (KERNEL32.@)
308 BOOL WINAPI
ReadFileEx(HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
309 LPOVERLAPPED overlapped
,
310 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
312 LARGE_INTEGER offset
;
314 PIO_STATUS_BLOCK io_status
;
316 TRACE("(hFile=%p, buffer=%p, bytes=%lu, ovl=%p, ovl_fn=%p)\n", hFile
, buffer
, bytesToRead
, overlapped
, lpCompletionRoutine
);
320 SetLastError(ERROR_INVALID_PARAMETER
);
324 offset
.u
.LowPart
= overlapped
->Offset
;
325 offset
.u
.HighPart
= overlapped
->OffsetHigh
;
326 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
327 io_status
->u
.Status
= STATUS_PENDING
;
329 status
= NtReadFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
330 io_status
, buffer
, bytesToRead
, &offset
, NULL
);
334 SetLastError( RtlNtStatusToDosError(status
) );
341 /***********************************************************************
342 * ReadFile (KERNEL32.@)
344 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
345 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
347 LARGE_INTEGER offset
;
348 PLARGE_INTEGER poffset
= NULL
;
349 IO_STATUS_BLOCK iosb
;
350 PIO_STATUS_BLOCK io_status
= &iosb
;
354 TRACE("%p %p %ld %p %p\n", hFile
, buffer
, bytesToRead
,
355 bytesRead
, overlapped
);
357 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
358 if (!bytesToRead
) return TRUE
;
360 if (IsBadReadPtr(buffer
, bytesToRead
))
362 SetLastError(ERROR_WRITE_FAULT
); /* FIXME */
365 if (is_console_handle(hFile
))
366 return ReadConsoleA(hFile
, buffer
, bytesToRead
, bytesRead
, NULL
);
368 if (overlapped
!= NULL
)
370 offset
.u
.LowPart
= overlapped
->Offset
;
371 offset
.u
.HighPart
= overlapped
->OffsetHigh
;
373 hEvent
= overlapped
->hEvent
;
374 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
376 io_status
->u
.Status
= STATUS_PENDING
;
377 io_status
->Information
= 0;
379 status
= NtReadFile(hFile
, hEvent
, NULL
, NULL
, io_status
, buffer
, bytesToRead
, poffset
, NULL
);
381 if (status
!= STATUS_PENDING
&& bytesRead
)
382 *bytesRead
= io_status
->Information
;
384 if (status
&& status
!= STATUS_END_OF_FILE
)
386 SetLastError( RtlNtStatusToDosError(status
) );
393 /***********************************************************************
394 * WriteFileEx (KERNEL32.@)
396 BOOL WINAPI
WriteFileEx(HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
397 LPOVERLAPPED overlapped
,
398 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
400 LARGE_INTEGER offset
;
402 PIO_STATUS_BLOCK io_status
;
404 TRACE("%p %p %ld %p %p\n", hFile
, buffer
, bytesToWrite
, overlapped
, lpCompletionRoutine
);
406 if (overlapped
== NULL
)
408 SetLastError(ERROR_INVALID_PARAMETER
);
411 offset
.u
.LowPart
= overlapped
->Offset
;
412 offset
.u
.HighPart
= overlapped
->OffsetHigh
;
414 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
415 io_status
->u
.Status
= STATUS_PENDING
;
417 status
= NtWriteFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
418 io_status
, buffer
, bytesToWrite
, &offset
, NULL
);
420 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
425 /***********************************************************************
426 * WriteFile (KERNEL32.@)
428 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
429 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
431 HANDLE hEvent
= NULL
;
432 LARGE_INTEGER offset
;
433 PLARGE_INTEGER poffset
= NULL
;
435 IO_STATUS_BLOCK iosb
;
436 PIO_STATUS_BLOCK piosb
= &iosb
;
438 TRACE("%p %p %ld %p %p\n", hFile
, buffer
, bytesToWrite
, bytesWritten
, overlapped
);
440 if (is_console_handle(hFile
))
441 return WriteConsoleA(hFile
, buffer
, bytesToWrite
, bytesWritten
, NULL
);
443 if (IsBadReadPtr(buffer
, bytesToWrite
))
445 SetLastError(ERROR_READ_FAULT
); /* FIXME */
451 offset
.u
.LowPart
= overlapped
->Offset
;
452 offset
.u
.HighPart
= overlapped
->OffsetHigh
;
454 hEvent
= overlapped
->hEvent
;
455 piosb
= (PIO_STATUS_BLOCK
)overlapped
;
457 piosb
->u
.Status
= STATUS_PENDING
;
458 piosb
->Information
= 0;
460 status
= NtWriteFile(hFile
, hEvent
, NULL
, NULL
, piosb
,
461 buffer
, bytesToWrite
, poffset
, NULL
);
464 SetLastError( RtlNtStatusToDosError(status
) );
467 if (bytesWritten
) *bytesWritten
= piosb
->Information
;
473 /***********************************************************************
474 * GetOverlappedResult (KERNEL32.@)
476 * Check the result of an Asynchronous data transfer from a file.
479 * HANDLE hFile [in] handle of file to check on
480 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
481 * LPDWORD lpTransferred [in/out] number of bytes transferred
482 * BOOL bWait [in] wait for the transfer to complete ?
488 * If successful (and relevant) lpTransferred will hold the number of
489 * bytes transferred during the async operation.
493 * Currently only works for WaitCommEvent, ReadFile, WriteFile
494 * with communications ports.
497 BOOL WINAPI
GetOverlappedResult(HANDLE hFile
, LPOVERLAPPED lpOverlapped
,
498 LPDWORD lpTransferred
, BOOL bWait
)
500 DWORD r
= WAIT_OBJECT_0
;
502 TRACE( "(%p %p %p %x)\n", hFile
, lpOverlapped
, lpTransferred
, bWait
);
504 if ( lpOverlapped
== NULL
)
506 ERR("lpOverlapped was null\n");
511 if ( lpOverlapped
->hEvent
)
515 TRACE( "waiting on %p\n", lpOverlapped
);
516 r
= WaitForSingleObjectEx( lpOverlapped
->hEvent
, INFINITE
, TRUE
);
517 TRACE( "wait on %p returned %ld\n", lpOverlapped
, r
);
518 } while ( r
== WAIT_IO_COMPLETION
);
523 while ( (volatile DWORD
)lpOverlapped
->Internal
== STATUS_PENDING
)
527 else if ( lpOverlapped
->Internal
== STATUS_PENDING
)
529 /* Wait in order to give APCs a chance to run. */
530 /* This is cheating, so we must set the event again in case of success -
531 it may be a non-manual reset event. */
534 TRACE( "waiting on %p\n", lpOverlapped
);
535 r
= WaitForSingleObjectEx( lpOverlapped
->hEvent
, 0, TRUE
);
536 TRACE( "wait on %p returned %ld\n", lpOverlapped
, r
);
537 } while ( r
== WAIT_IO_COMPLETION
);
538 if ( r
== WAIT_OBJECT_0
&& lpOverlapped
->hEvent
)
539 NtSetEvent( lpOverlapped
->hEvent
, NULL
);
541 if ( r
== WAIT_FAILED
)
543 ERR("wait operation failed\n");
546 if (lpTransferred
) *lpTransferred
= lpOverlapped
->InternalHigh
;
548 switch ( lpOverlapped
->Internal
)
553 SetLastError( ERROR_IO_INCOMPLETE
);
554 if ( bWait
) ERR("PENDING status after waiting!\n");
557 SetLastError( RtlNtStatusToDosError( lpOverlapped
->Internal
) );
562 /***********************************************************************
563 * CancelIo (KERNEL32.@)
565 BOOL WINAPI
CancelIo(HANDLE handle
)
567 async_private
*ovp
,*t
;
569 TRACE("handle = %p\n",handle
);
571 for (ovp
= NtCurrentTeb()->pending_list
; ovp
; ovp
= t
)
574 if ( ovp
->handle
== handle
)
575 cancel_async ( ovp
);
581 /***********************************************************************
582 * _hread (KERNEL32.@)
584 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
586 return _lread( hFile
, buffer
, count
);
590 /***********************************************************************
591 * _hwrite (KERNEL32.@)
593 * experimentation yields that _lwrite:
594 * o truncates the file at the current position with
596 * o returns 0 on a 0 length write
597 * o works with console handles
600 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
604 TRACE("%d %p %ld\n", handle
, buffer
, count
);
608 /* Expand or truncate at current position */
609 if (!SetEndOfFile( (HANDLE
)handle
)) return HFILE_ERROR
;
612 if (!WriteFile( (HANDLE
)handle
, buffer
, count
, &result
, NULL
))
618 /***********************************************************************
619 * _lclose (KERNEL32.@)
621 HFILE WINAPI
_lclose( HFILE hFile
)
623 TRACE("handle %d\n", hFile
);
624 return CloseHandle( (HANDLE
)hFile
) ? 0 : HFILE_ERROR
;
628 /***********************************************************************
629 * _lcreat (KERNEL32.@)
631 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
633 /* Mask off all flags not explicitly allowed by the doc */
634 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
635 TRACE("%s %02x\n", path
, attr
);
636 return (HFILE
)CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
637 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
638 CREATE_ALWAYS
, attr
, 0 );
642 /***********************************************************************
643 * _lopen (KERNEL32.@)
645 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
647 TRACE("(%s,%04x)\n", debugstr_a(path
), mode
);
648 return (HFILE
)create_file_OF( path
, mode
& ~OF_CREATE
);
651 /***********************************************************************
652 * _lread (KERNEL32.@)
654 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
657 if (!ReadFile( (HANDLE
)handle
, buffer
, count
, &result
, NULL
))
663 /***********************************************************************
664 * _llseek (KERNEL32.@)
666 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
668 return SetFilePointer( (HANDLE
)hFile
, lOffset
, NULL
, nOrigin
);
672 /***********************************************************************
673 * _lwrite (KERNEL32.@)
675 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
677 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
681 /***********************************************************************
682 * FlushFileBuffers (KERNEL32.@)
684 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
687 IO_STATUS_BLOCK ioblk
;
689 if (is_console_handle( hFile
))
691 /* this will fail (as expected) for an output handle */
692 /* FIXME: wait until FlushFileBuffers is moved to dll/kernel */
693 /* return FlushConsoleInputBuffer( hFile ); */
696 nts
= NtFlushBuffersFile( hFile
, &ioblk
);
697 if (nts
!= STATUS_SUCCESS
)
699 SetLastError( RtlNtStatusToDosError( nts
) );
707 /***********************************************************************
708 * GetFileType (KERNEL32.@)
710 DWORD WINAPI
GetFileType( HANDLE hFile
)
712 FILE_FS_DEVICE_INFORMATION info
;
716 if (is_console_handle( hFile
)) return FILE_TYPE_CHAR
;
718 status
= NtQueryVolumeInformationFile( hFile
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
719 if (status
!= STATUS_SUCCESS
)
721 SetLastError( RtlNtStatusToDosError(status
) );
722 return FILE_TYPE_UNKNOWN
;
725 switch(info
.DeviceType
)
727 case FILE_DEVICE_NULL
:
728 case FILE_DEVICE_SERIAL_PORT
:
729 case FILE_DEVICE_PARALLEL_PORT
:
730 case FILE_DEVICE_UNKNOWN
:
731 return FILE_TYPE_CHAR
;
732 case FILE_DEVICE_NAMED_PIPE
:
733 return FILE_TYPE_PIPE
;
735 return FILE_TYPE_DISK
;
740 /***********************************************************************
741 * GetFileInformationByHandle (KERNEL32.@)
743 BOOL WINAPI
GetFileInformationByHandle( HANDLE hFile
, BY_HANDLE_FILE_INFORMATION
*info
)
745 FILE_ALL_INFORMATION all_info
;
749 status
= NtQueryInformationFile( hFile
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
750 if (status
== STATUS_SUCCESS
)
752 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
753 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
754 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
755 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
756 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
757 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
758 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
759 info
->dwVolumeSerialNumber
= 0; /* FIXME */
760 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
761 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
762 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
763 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
764 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
767 SetLastError( RtlNtStatusToDosError(status
) );
772 /***********************************************************************
773 * GetFileSize (KERNEL32.@)
775 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
778 if (!GetFileSizeEx( hFile
, &size
)) return INVALID_FILE_SIZE
;
779 if (filesizehigh
) *filesizehigh
= size
.u
.HighPart
;
780 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError(0);
781 return size
.u
.LowPart
;
785 /***********************************************************************
786 * GetFileSizeEx (KERNEL32.@)
788 BOOL WINAPI
GetFileSizeEx( HANDLE hFile
, PLARGE_INTEGER lpFileSize
)
790 FILE_END_OF_FILE_INFORMATION info
;
794 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileEndOfFileInformation
);
795 if (status
== STATUS_SUCCESS
)
797 *lpFileSize
= info
.EndOfFile
;
800 SetLastError( RtlNtStatusToDosError(status
) );
805 /**************************************************************************
806 * SetEndOfFile (KERNEL32.@)
808 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
810 FILE_POSITION_INFORMATION pos
;
811 FILE_END_OF_FILE_INFORMATION eof
;
815 status
= NtQueryInformationFile( hFile
, &io
, &pos
, sizeof(pos
), FilePositionInformation
);
816 if (status
== STATUS_SUCCESS
)
818 eof
.EndOfFile
= pos
.CurrentByteOffset
;
819 status
= NtSetInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
821 if (status
== STATUS_SUCCESS
) return TRUE
;
822 SetLastError( RtlNtStatusToDosError(status
) );
827 /***********************************************************************
828 * SetFilePointer (KERNEL32.@)
830 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
, DWORD method
)
832 LARGE_INTEGER dist
, newpos
;
836 dist
.u
.LowPart
= distance
;
837 dist
.u
.HighPart
= *highword
;
839 else dist
.QuadPart
= distance
;
841 if (!SetFilePointerEx( hFile
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
843 if (highword
) *highword
= newpos
.u
.HighPart
;
844 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
845 return newpos
.u
.LowPart
;
849 /***********************************************************************
850 * SetFilePointerEx (KERNEL32.@)
852 BOOL WINAPI
SetFilePointerEx( HANDLE hFile
, LARGE_INTEGER distance
,
853 LARGE_INTEGER
*newpos
, DWORD method
)
855 static const int whence
[3] = { SEEK_SET
, SEEK_CUR
, SEEK_END
};
860 TRACE("handle %p offset %s newpos %p origin %ld\n",
861 hFile
, wine_dbgstr_longlong(distance
.QuadPart
), newpos
, method
);
863 if (method
> FILE_END
)
865 SetLastError( ERROR_INVALID_PARAMETER
);
869 if (!(status
= wine_server_handle_to_fd( hFile
, 0, &fd
, NULL
)))
873 pos
= distance
.QuadPart
;
874 if ((res
= lseek( fd
, pos
, whence
[method
] )) == (off_t
)-1)
876 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
877 if (((errno
== EINVAL
) || (errno
== EPERM
)) && (method
!= FILE_BEGIN
) && (pos
< 0))
878 SetLastError( ERROR_NEGATIVE_SEEK
);
886 newpos
->QuadPart
= res
;
888 wine_server_release_fd( hFile
, fd
);
890 else SetLastError( RtlNtStatusToDosError(status
) );
895 /***********************************************************************
896 * GetFileTime (KERNEL32.@)
898 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
899 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
901 FILE_BASIC_INFORMATION info
;
905 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
906 if (status
== STATUS_SUCCESS
)
910 lpCreationTime
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
911 lpCreationTime
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
913 if (lpLastAccessTime
)
915 lpLastAccessTime
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
916 lpLastAccessTime
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
920 lpLastWriteTime
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
921 lpLastWriteTime
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
925 SetLastError( RtlNtStatusToDosError(status
) );
930 /***********************************************************************
931 * SetFileTime (KERNEL32.@)
933 BOOL WINAPI
SetFileTime( HANDLE hFile
, const FILETIME
*ctime
,
934 const FILETIME
*atime
, const FILETIME
*mtime
)
936 FILE_BASIC_INFORMATION info
;
940 memset( &info
, 0, sizeof(info
) );
943 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
944 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
948 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
949 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
953 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
954 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
957 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
958 if (status
== STATUS_SUCCESS
) return TRUE
;
959 SetLastError( RtlNtStatusToDosError(status
) );
964 /**************************************************************************
965 * LockFile (KERNEL32.@)
967 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
968 DWORD count_low
, DWORD count_high
)
971 LARGE_INTEGER count
, offset
;
973 TRACE( "%p %lx%08lx %lx%08lx\n",
974 hFile
, offset_high
, offset_low
, count_high
, count_low
);
976 count
.u
.LowPart
= count_low
;
977 count
.u
.HighPart
= count_high
;
978 offset
.u
.LowPart
= offset_low
;
979 offset
.u
.HighPart
= offset_high
;
981 status
= NtLockFile( hFile
, 0, NULL
, NULL
,
982 NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
);
984 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
989 /**************************************************************************
990 * LockFileEx [KERNEL32.@]
992 * Locks a byte range within an open file for shared or exclusive access.
999 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1001 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1002 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
1005 LARGE_INTEGER count
, offset
;
1009 SetLastError( ERROR_INVALID_PARAMETER
);
1013 TRACE( "%p %lx%08lx %lx%08lx flags %lx\n",
1014 hFile
, overlapped
->OffsetHigh
, overlapped
->Offset
,
1015 count_high
, count_low
, flags
);
1017 count
.u
.LowPart
= count_low
;
1018 count
.u
.HighPart
= count_high
;
1019 offset
.u
.LowPart
= overlapped
->Offset
;
1020 offset
.u
.HighPart
= overlapped
->OffsetHigh
;
1022 status
= NtLockFile( hFile
, overlapped
->hEvent
, NULL
, NULL
,
1023 NULL
, &offset
, &count
, NULL
,
1024 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
1025 flags
& LOCKFILE_EXCLUSIVE_LOCK
);
1027 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1032 /**************************************************************************
1033 * UnlockFile (KERNEL32.@)
1035 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1036 DWORD count_low
, DWORD count_high
)
1039 LARGE_INTEGER count
, offset
;
1041 count
.u
.LowPart
= count_low
;
1042 count
.u
.HighPart
= count_high
;
1043 offset
.u
.LowPart
= offset_low
;
1044 offset
.u
.HighPart
= offset_high
;
1046 status
= NtUnlockFile( hFile
, NULL
, &offset
, &count
, NULL
);
1047 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1052 /**************************************************************************
1053 * UnlockFileEx (KERNEL32.@)
1055 BOOL WINAPI
UnlockFileEx( HANDLE hFile
, DWORD reserved
, DWORD count_low
, DWORD count_high
,
1056 LPOVERLAPPED overlapped
)
1060 SetLastError( ERROR_INVALID_PARAMETER
);
1063 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
1065 return UnlockFile( hFile
, overlapped
->Offset
, overlapped
->OffsetHigh
, count_low
, count_high
);
1069 /***********************************************************************
1070 * Win32HandleToDosFileHandle (KERNEL32.21)
1072 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1073 * longer valid after this function (even on failure).
1075 * Note: this is not exactly right, since on Win95 the Win32 handles
1076 * are on top of DOS handles and we do it the other way
1077 * around. Should be good enough though.
1079 HFILE WINAPI
Win32HandleToDosFileHandle( HANDLE handle
)
1083 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
1086 FILE_InitProcessDosHandles();
1087 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
1088 if (!dos_handles
[i
])
1090 dos_handles
[i
] = handle
;
1091 TRACE("Got %d for h32 %p\n", i
, handle
);
1094 CloseHandle( handle
);
1095 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
1100 /***********************************************************************
1101 * DosFileHandleToWin32Handle (KERNEL32.20)
1103 * Return the Win32 handle for a DOS handle.
1105 * Note: this is not exactly right, since on Win95 the Win32 handles
1106 * are on top of DOS handles and we do it the other way
1107 * around. Should be good enough though.
1109 HANDLE WINAPI
DosFileHandleToWin32Handle( HFILE handle
)
1111 HFILE16 hfile
= (HFILE16
)handle
;
1112 if (hfile
< 5) FILE_InitProcessDosHandles();
1113 if ((hfile
>= DOS_TABLE_SIZE
) || !dos_handles
[hfile
])
1115 SetLastError( ERROR_INVALID_HANDLE
);
1116 return INVALID_HANDLE_VALUE
;
1118 return dos_handles
[hfile
];
1122 /*************************************************************************
1123 * SetHandleCount (KERNEL32.@)
1125 UINT WINAPI
SetHandleCount( UINT count
)
1127 return min( 256, count
);
1131 /***********************************************************************
1132 * DisposeLZ32Handle (KERNEL32.22)
1134 * Note: this is not entirely correct, we should only close the
1135 * 32-bit handle and not the 16-bit one, but we cannot do
1136 * this because of the way our DOS handles are implemented.
1137 * It shouldn't break anything though.
1139 void WINAPI
DisposeLZ32Handle( HANDLE handle
)
1143 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
)) return;
1145 for (i
= 5; i
< DOS_TABLE_SIZE
; i
++)
1146 if (dos_handles
[i
] == handle
)
1149 CloseHandle( handle
);
1154 /**************************************************************************
1155 * Operations on file names *
1156 **************************************************************************/
1159 /*************************************************************************
1160 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1162 * Creates or opens an object, and returns a handle that can be used to
1163 * access that object.
1167 * filename [in] pointer to filename to be accessed
1168 * access [in] access mode requested
1169 * sharing [in] share mode
1170 * sa [in] pointer to security attributes
1171 * creation [in] how to create the file
1172 * attributes [in] attributes for newly created file
1173 * template [in] handle to file with extended attributes to copy
1176 * Success: Open handle to specified file
1177 * Failure: INVALID_HANDLE_VALUE
1179 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
1180 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1181 DWORD attributes
, HANDLE
template )
1185 OBJECT_ATTRIBUTES attr
;
1186 UNICODE_STRING nameW
;
1190 static const WCHAR bkslashes_with_dotW
[] = {'\\','\\','.','\\',0};
1191 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
1192 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
1194 static const char * const creation_name
[5] =
1195 { "CREATE_NEW", "CREATE_ALWAYS", "OPEN_EXISTING", "OPEN_ALWAYS", "TRUNCATE_EXISTING" };
1197 static const UINT nt_disposition
[5] =
1199 FILE_CREATE
, /* CREATE_NEW */
1200 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
1201 FILE_OPEN
, /* OPEN_EXISTING */
1202 FILE_OPEN_IF
, /* OPEN_ALWAYS */
1203 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
1209 if (!filename
|| !filename
[0])
1211 SetLastError( ERROR_PATH_NOT_FOUND
);
1212 return INVALID_HANDLE_VALUE
;
1215 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
1217 SetLastError( ERROR_INVALID_PARAMETER
);
1218 return INVALID_HANDLE_VALUE
;
1221 TRACE("%s %s%s%s%s%s%s%s attributes 0x%lx\n", debugstr_w(filename
),
1222 (access
& GENERIC_READ
)?"GENERIC_READ ":"",
1223 (access
& GENERIC_WRITE
)?"GENERIC_WRITE ":"",
1224 (!access
)?"QUERY_ACCESS ":"",
1225 (sharing
& FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
1226 (sharing
& FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
1227 (sharing
& FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
1228 creation_name
[creation
- CREATE_NEW
], attributes
);
1230 /* Open a console for CONIN$ or CONOUT$ */
1232 if (!strcmpiW(filename
, coninW
) || !strcmpiW(filename
, conoutW
))
1234 ret
= OpenConsoleW(filename
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1238 if (!strncmpW(filename
, bkslashes_with_dotW
, 4))
1240 static const WCHAR pipeW
[] = {'P','I','P','E','\\',0};
1242 if ((isalphaW(filename
[4]) && filename
[5] == ':' && filename
[6] == '\0') ||
1243 !strncmpiW( filename
+ 4, pipeW
, 5 ))
1247 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
1249 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
1251 else if (filename
[4])
1253 ret
= VXD_Open( filename
+4, access
, sa
);
1258 SetLastError( ERROR_INVALID_NAME
);
1259 return INVALID_HANDLE_VALUE
;
1262 else dosdev
= RtlIsDosDeviceName_U( filename
);
1266 static const WCHAR conW
[] = {'C','O','N'};
1268 if (LOWORD(dosdev
) == sizeof(conW
) &&
1269 !memicmpW( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), conW
, sizeof(conW
)))
1271 switch (access
& (GENERIC_READ
|GENERIC_WRITE
))
1274 ret
= OpenConsoleW(coninW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1277 ret
= OpenConsoleW(conoutW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1280 SetLastError( ERROR_FILE_NOT_FOUND
);
1281 return INVALID_HANDLE_VALUE
;
1286 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
1288 SetLastError( ERROR_PATH_NOT_FOUND
);
1289 return INVALID_HANDLE_VALUE
;
1292 /* now call NtCreateFile */
1295 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
1296 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
1298 options
|= FILE_NON_DIRECTORY_FILE
;
1299 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
1300 options
|= FILE_DELETE_ON_CLOSE
;
1301 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
1302 options
|= FILE_SYNCHRONOUS_IO_ALERT
;
1303 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
1304 options
|= FILE_RANDOM_ACCESS
;
1305 attributes
&= FILE_ATTRIBUTE_VALID_FLAGS
;
1307 attr
.Length
= sizeof(attr
);
1308 attr
.RootDirectory
= 0;
1309 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1310 attr
.ObjectName
= &nameW
;
1311 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1312 attr
.SecurityQualityOfService
= NULL
;
1314 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
1316 status
= NtCreateFile( &ret
, access
, &attr
, &io
, NULL
, attributes
,
1317 sharing
, nt_disposition
[creation
- CREATE_NEW
],
1321 WARN("Unable to create file %s (status %lx)\n", debugstr_w(filename
), status
);
1322 ret
= INVALID_HANDLE_VALUE
;
1324 /* In the case file creation was rejected due to CREATE_NEW flag
1325 * was specified and file with that name already exists, correct
1326 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1327 * Note: RtlNtStatusToDosError is not the subject to blame here.
1329 if (status
== STATUS_OBJECT_NAME_COLLISION
)
1330 SetLastError( ERROR_FILE_EXISTS
);
1332 SetLastError( RtlNtStatusToDosError(status
) );
1334 else SetLastError(0);
1335 RtlFreeUnicodeString( &nameW
);
1338 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
1339 TRACE("returning %p\n", ret
);
1345 /*************************************************************************
1346 * CreateFileA (KERNEL32.@)
1348 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
1349 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1350 DWORD attributes
, HANDLE
template)
1354 if (!(nameW
= FILE_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1355 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
1359 /***********************************************************************
1360 * DeleteFileW (KERNEL32.@)
1362 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1366 TRACE("%s\n", debugstr_w(path
) );
1368 hFile
= CreateFileW( path
, GENERIC_READ
| GENERIC_WRITE
,
1369 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1370 NULL
, OPEN_EXISTING
, FILE_FLAG_DELETE_ON_CLOSE
, 0 );
1371 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
1373 CloseHandle(hFile
); /* last close will delete the file */
1378 /***********************************************************************
1379 * DeleteFileA (KERNEL32.@)
1381 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1385 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1386 return DeleteFileW( pathW
);
1390 /**************************************************************************
1391 * ReplaceFileW (KERNEL32.@)
1392 * ReplaceFile (KERNEL32.@)
1394 BOOL WINAPI
ReplaceFileW(LPCWSTR lpReplacedFileName
,LPCWSTR lpReplacementFileName
,
1395 LPCWSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1396 LPVOID lpExclude
, LPVOID lpReserved
)
1398 FIXME("(%s,%s,%s,%08lx,%p,%p) stub\n",debugstr_w(lpReplacedFileName
),debugstr_w(lpReplacementFileName
),
1399 debugstr_w(lpBackupFileName
),dwReplaceFlags
,lpExclude
,lpReserved
);
1400 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT
);
1405 /**************************************************************************
1406 * ReplaceFileA (KERNEL32.@)
1408 BOOL WINAPI
ReplaceFileA(LPCSTR lpReplacedFileName
,LPCSTR lpReplacementFileName
,
1409 LPCSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1410 LPVOID lpExclude
, LPVOID lpReserved
)
1412 FIXME("(%s,%s,%s,%08lx,%p,%p) stub\n",lpReplacedFileName
,lpReplacementFileName
,
1413 lpBackupFileName
,dwReplaceFlags
,lpExclude
,lpReserved
);
1414 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT
);
1419 /*************************************************************************
1420 * FindFirstFileExW (KERNEL32.@)
1422 HANDLE WINAPI
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1423 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1424 LPVOID filter
, DWORD flags
)
1427 FIND_FIRST_INFO
*info
= NULL
;
1428 UNICODE_STRING nt_name
;
1429 OBJECT_ATTRIBUTES attr
;
1433 TRACE("%s %d %p %d %p %lx\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1435 if ((search_op
!= FindExSearchNameMatch
) || (flags
!= 0))
1437 FIXME("options not implemented 0x%08x 0x%08lx\n", search_op
, flags
);
1438 return INVALID_HANDLE_VALUE
;
1440 if (level
!= FindExInfoStandard
)
1442 FIXME("info level %d not implemented\n", level
);
1443 return INVALID_HANDLE_VALUE
;
1446 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1448 SetLastError( ERROR_PATH_NOT_FOUND
);
1449 return INVALID_HANDLE_VALUE
;
1452 if (!mask
|| !*mask
)
1454 SetLastError( ERROR_FILE_NOT_FOUND
);
1458 if (!(info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
))))
1460 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1464 if (!RtlCreateUnicodeString( &info
->mask
, mask
))
1466 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1470 /* truncate dir name before mask */
1472 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
1474 /* check if path is the root of the drive */
1475 info
->is_root
= FALSE
;
1476 p
= nt_name
.Buffer
+ 4; /* skip \??\ prefix */
1477 if (p
[0] && p
[1] == ':')
1480 while (*p
== '\\') p
++;
1481 info
->is_root
= (*p
== 0);
1484 attr
.Length
= sizeof(attr
);
1485 attr
.RootDirectory
= 0;
1486 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1487 attr
.ObjectName
= &nt_name
;
1488 attr
.SecurityDescriptor
= NULL
;
1489 attr
.SecurityQualityOfService
= NULL
;
1491 status
= NtOpenFile( &info
->handle
, GENERIC_READ
, &attr
, &io
,
1492 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1493 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1495 if (status
!= STATUS_SUCCESS
)
1497 RtlFreeUnicodeString( &info
->mask
);
1498 SetLastError( RtlNtStatusToDosError(status
) );
1501 RtlFreeUnicodeString( &nt_name
);
1503 RtlInitializeCriticalSection( &info
->cs
);
1504 info
->magic
= FIND_FIRST_MAGIC
;
1508 if (!FindNextFileW( (HANDLE
)info
, data
))
1510 TRACE( "%s not found\n", debugstr_w(filename
) );
1511 FindClose( (HANDLE
)info
);
1512 SetLastError( ERROR_FILE_NOT_FOUND
);
1513 return INVALID_HANDLE_VALUE
;
1515 return (HANDLE
)info
;
1518 if (info
) HeapFree( GetProcessHeap(), 0, info
);
1519 RtlFreeUnicodeString( &nt_name
);
1520 return INVALID_HANDLE_VALUE
;
1524 /*************************************************************************
1525 * FindNextFileW (KERNEL32.@)
1527 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1529 FIND_FIRST_INFO
*info
;
1530 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1533 TRACE("%p %p\n", handle
, data
);
1535 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1537 SetLastError( ERROR_INVALID_HANDLE
);
1540 info
= (FIND_FIRST_INFO
*)handle
;
1541 if (info
->magic
!= FIND_FIRST_MAGIC
)
1543 SetLastError( ERROR_INVALID_HANDLE
);
1547 RtlEnterCriticalSection( &info
->cs
);
1551 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
1555 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1556 FileBothDirectoryInformation
, FALSE
, &info
->mask
, FALSE
);
1559 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1562 info
->data_len
= io
.Information
;
1566 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
1568 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
1569 else info
->data_pos
= info
->data_len
;
1571 /* don't return '.' and '..' in the root of the drive */
1574 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
1575 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
1576 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
1579 data
->dwFileAttributes
= dir_info
->FileAttributes
;
1580 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
1581 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
1582 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
1583 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
1584 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
1585 data
->dwReserved0
= 0;
1586 data
->dwReserved1
= 0;
1588 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
1589 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
1590 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
1591 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
1593 TRACE("returning %s (%s)\n",
1594 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
1600 RtlLeaveCriticalSection( &info
->cs
);
1605 /*************************************************************************
1606 * FindClose (KERNEL32.@)
1608 BOOL WINAPI
FindClose( HANDLE handle
)
1610 FIND_FIRST_INFO
*info
= (FIND_FIRST_INFO
*)handle
;
1612 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1614 SetLastError( ERROR_INVALID_HANDLE
);
1620 if (info
->magic
== FIND_FIRST_MAGIC
)
1622 RtlEnterCriticalSection( &info
->cs
);
1623 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
1626 if (info
->handle
) CloseHandle( info
->handle
);
1628 RtlFreeUnicodeString( &info
->mask
);
1629 info
->mask
.Buffer
= NULL
;
1632 RtlLeaveCriticalSection( &info
->cs
);
1633 RtlDeleteCriticalSection( &info
->cs
);
1634 HeapFree( GetProcessHeap(), 0, info
);
1638 __EXCEPT(page_fault
)
1640 WARN("Illegal handle %p\n", handle
);
1641 SetLastError( ERROR_INVALID_HANDLE
);
1650 /*************************************************************************
1651 * FindFirstFileA (KERNEL32.@)
1653 HANDLE WINAPI
FindFirstFileA( LPCSTR lpFileName
, WIN32_FIND_DATAA
*lpFindData
)
1655 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
1656 FindExSearchNameMatch
, NULL
, 0);
1659 /*************************************************************************
1660 * FindFirstFileExA (KERNEL32.@)
1662 HANDLE WINAPI
FindFirstFileExA( LPCSTR lpFileName
, FINDEX_INFO_LEVELS fInfoLevelId
,
1663 LPVOID lpFindFileData
, FINDEX_SEARCH_OPS fSearchOp
,
1664 LPVOID lpSearchFilter
, DWORD dwAdditionalFlags
)
1667 WIN32_FIND_DATAA
*dataA
;
1668 WIN32_FIND_DATAW dataW
;
1671 if (!(nameW
= FILE_name_AtoW( lpFileName
, FALSE
))) return INVALID_HANDLE_VALUE
;
1673 handle
= FindFirstFileExW(nameW
, fInfoLevelId
, &dataW
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
1674 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
1676 dataA
= (WIN32_FIND_DATAA
*) lpFindFileData
;
1677 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
1678 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
1679 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
1680 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
1681 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
1682 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
1683 FILE_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
1684 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
1685 sizeof(dataA
->cAlternateFileName
) );
1690 /*************************************************************************
1691 * FindFirstFileW (KERNEL32.@)
1693 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
1695 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
1696 FindExSearchNameMatch
, NULL
, 0);
1700 /*************************************************************************
1701 * FindNextFileA (KERNEL32.@)
1703 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
1705 WIN32_FIND_DATAW dataW
;
1707 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
1708 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
1709 data
->ftCreationTime
= dataW
.ftCreationTime
;
1710 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
1711 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
1712 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
1713 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
1714 FILE_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
1715 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
1716 sizeof(data
->cAlternateFileName
) );
1721 /**************************************************************************
1722 * GetFileAttributesW (KERNEL32.@)
1724 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
1726 FILE_BASIC_INFORMATION info
;
1727 UNICODE_STRING nt_name
;
1728 OBJECT_ATTRIBUTES attr
;
1731 TRACE("%s\n", debugstr_w(name
));
1733 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1735 SetLastError( ERROR_PATH_NOT_FOUND
);
1736 return INVALID_FILE_ATTRIBUTES
;
1739 attr
.Length
= sizeof(attr
);
1740 attr
.RootDirectory
= 0;
1741 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1742 attr
.ObjectName
= &nt_name
;
1743 attr
.SecurityDescriptor
= NULL
;
1744 attr
.SecurityQualityOfService
= NULL
;
1746 status
= NtQueryAttributesFile( &attr
, &info
);
1747 RtlFreeUnicodeString( &nt_name
);
1749 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
1751 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1752 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
1754 SetLastError( RtlNtStatusToDosError(status
) );
1755 return INVALID_FILE_ATTRIBUTES
;
1759 /**************************************************************************
1760 * GetFileAttributesA (KERNEL32.@)
1762 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
1766 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
1767 return GetFileAttributesW( nameW
);
1771 /**************************************************************************
1772 * SetFileAttributesW (KERNEL32.@)
1774 BOOL WINAPI
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
1776 UNICODE_STRING nt_name
;
1777 OBJECT_ATTRIBUTES attr
;
1782 TRACE("%s %lx\n", debugstr_w(name
), attributes
);
1784 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1786 SetLastError( ERROR_PATH_NOT_FOUND
);
1790 attr
.Length
= sizeof(attr
);
1791 attr
.RootDirectory
= 0;
1792 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1793 attr
.ObjectName
= &nt_name
;
1794 attr
.SecurityDescriptor
= NULL
;
1795 attr
.SecurityQualityOfService
= NULL
;
1797 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
1798 RtlFreeUnicodeString( &nt_name
);
1800 if (status
== STATUS_SUCCESS
)
1802 FILE_BASIC_INFORMATION info
;
1804 memset( &info
, 0, sizeof(info
) );
1805 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
1806 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1810 if (status
== STATUS_SUCCESS
) return TRUE
;
1811 SetLastError( RtlNtStatusToDosError(status
) );
1816 /**************************************************************************
1817 * SetFileAttributesA (KERNEL32.@)
1819 BOOL WINAPI
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
1823 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
1824 return SetFileAttributesW( nameW
, attributes
);
1828 /**************************************************************************
1829 * GetFileAttributesExW (KERNEL32.@)
1831 BOOL WINAPI
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
1833 FILE_NETWORK_OPEN_INFORMATION info
;
1834 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
1835 UNICODE_STRING nt_name
;
1836 OBJECT_ATTRIBUTES attr
;
1839 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
1841 if (level
!= GetFileExInfoStandard
)
1843 SetLastError( ERROR_INVALID_PARAMETER
);
1847 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1849 SetLastError( ERROR_PATH_NOT_FOUND
);
1853 attr
.Length
= sizeof(attr
);
1854 attr
.RootDirectory
= 0;
1855 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1856 attr
.ObjectName
= &nt_name
;
1857 attr
.SecurityDescriptor
= NULL
;
1858 attr
.SecurityQualityOfService
= NULL
;
1860 status
= NtQueryFullAttributesFile( &attr
, &info
);
1861 RtlFreeUnicodeString( &nt_name
);
1863 if (status
!= STATUS_SUCCESS
)
1865 SetLastError( RtlNtStatusToDosError(status
) );
1869 data
->dwFileAttributes
= info
.FileAttributes
;
1870 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1871 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1872 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1873 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1874 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1875 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1876 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
1877 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
1882 /**************************************************************************
1883 * GetFileAttributesExA (KERNEL32.@)
1885 BOOL WINAPI
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
1889 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
1890 return GetFileAttributesExW( nameW
, level
, ptr
);
1894 /******************************************************************************
1895 * GetCompressedFileSizeW (KERNEL32.@)
1898 * Success: Low-order doubleword of number of bytes
1899 * Failure: INVALID_FILE_SIZE
1901 DWORD WINAPI
GetCompressedFileSizeW(
1902 LPCWSTR name
, /* [in] Pointer to name of file */
1903 LPDWORD size_high
) /* [out] Receives high-order doubleword of size */
1905 UNICODE_STRING nt_name
;
1906 OBJECT_ATTRIBUTES attr
;
1910 DWORD ret
= INVALID_FILE_SIZE
;
1912 TRACE("%s %p\n", debugstr_w(name
), size_high
);
1914 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1916 SetLastError( ERROR_PATH_NOT_FOUND
);
1917 return INVALID_FILE_SIZE
;
1920 attr
.Length
= sizeof(attr
);
1921 attr
.RootDirectory
= 0;
1922 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1923 attr
.ObjectName
= &nt_name
;
1924 attr
.SecurityDescriptor
= NULL
;
1925 attr
.SecurityQualityOfService
= NULL
;
1927 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
1928 RtlFreeUnicodeString( &nt_name
);
1930 if (status
== STATUS_SUCCESS
)
1932 /* we don't support compressed files, simply return the file size */
1933 ret
= GetFileSize( handle
, size_high
);
1936 else SetLastError( RtlNtStatusToDosError(status
) );
1942 /******************************************************************************
1943 * GetCompressedFileSizeA (KERNEL32.@)
1945 DWORD WINAPI
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
1949 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
1950 return GetCompressedFileSizeW( nameW
, size_high
);
1954 /***********************************************************************
1955 * OpenFile (KERNEL32.@)
1957 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
1961 WORD filedatetime
[2];
1963 if (!ofs
) return HFILE_ERROR
;
1965 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
1966 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
1967 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
1968 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
1969 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
1970 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
1971 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
1972 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
1973 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
1974 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
1975 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
1976 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
1977 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
1978 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
1979 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
1980 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
1981 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
1982 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
1986 ofs
->cBytes
= sizeof(OFSTRUCT
);
1988 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
1990 if (!name
) return HFILE_ERROR
;
1992 TRACE("%s %04x\n", name
, mode
);
1994 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
1995 Are there any cases where getting the path here is wrong?
1996 Uwe Bonnes 1997 Apr 2 */
1997 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
)) goto error
;
1999 /* OF_PARSE simply fills the structure */
2001 if (mode
& OF_PARSE
)
2003 ofs
->fFixedDisk
= (GetDriveTypeA( ofs
->szPathName
) != DRIVE_REMOVABLE
);
2004 TRACE("(%s): OF_PARSE, res = '%s'\n", name
, ofs
->szPathName
);
2008 /* OF_CREATE is completely different from all other options, so
2011 if (mode
& OF_CREATE
)
2013 if ((handle
= create_file_OF( name
, mode
)) == INVALID_HANDLE_VALUE
)
2018 /* Now look for the file */
2020 if (!SearchPathA( NULL
, name
, NULL
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
))
2023 TRACE("found %s\n", debugstr_a(ofs
->szPathName
) );
2025 if (mode
& OF_DELETE
)
2027 if (!DeleteFileA( ofs
->szPathName
)) goto error
;
2028 TRACE("(%s): OF_DELETE return = OK\n", name
);
2032 handle
= (HANDLE
)_lopen( ofs
->szPathName
, mode
);
2033 if (handle
== INVALID_HANDLE_VALUE
) goto error
;
2035 GetFileTime( handle
, NULL
, NULL
, &filetime
);
2036 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
2037 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
2039 if (ofs
->Reserved1
!= filedatetime
[0] || ofs
->Reserved2
!= filedatetime
[1] )
2041 CloseHandle( handle
);
2042 WARN("(%s): OF_VERIFY failed\n", name
);
2043 /* FIXME: what error here? */
2044 SetLastError( ERROR_FILE_NOT_FOUND
);
2048 ofs
->Reserved1
= filedatetime
[0];
2049 ofs
->Reserved2
= filedatetime
[1];
2051 TRACE("(%s): OK, return = %p\n", name
, handle
);
2052 if (mode
& OF_EXIST
) /* Return TRUE instead of a handle */
2054 CloseHandle( handle
);
2057 else return (HFILE
)handle
;
2059 error
: /* We get here if there was an error opening the file */
2060 ofs
->nErrCode
= GetLastError();
2061 WARN("(%s): return = HFILE_ERROR error= %d\n", name
,ofs
->nErrCode
);