2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
36 #define WIN32_NO_STATUS
42 #include "wine/winbase16.h"
43 #include "kernel_private.h"
45 #include "wine/exception.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(file
);
52 HANDLE dos_handles
[DOS_TABLE_SIZE
];
54 /* info structure for FindFirstFile handle */
57 DWORD magic
; /* magic number */
58 HANDLE handle
; /* handle to directory */
59 CRITICAL_SECTION cs
; /* crit section protecting this structure */
60 FINDEX_SEARCH_OPS search_op
; /* Flags 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 BYTE data
[8192]; /* directory data */
69 #define FIND_FIRST_MAGIC 0xc0ffee11
71 static BOOL oem_file_apis
;
73 static const WCHAR wildcardsW
[] = { '*','?',0 };
75 /***********************************************************************
78 * Wrapper for CreateFile that takes OF_* mode flags.
80 static HANDLE
create_file_OF( LPCSTR path
, INT mode
)
82 DWORD access
, sharing
, creation
;
86 creation
= CREATE_ALWAYS
;
87 access
= GENERIC_READ
| GENERIC_WRITE
;
91 creation
= OPEN_EXISTING
;
94 case OF_READ
: access
= GENERIC_READ
; break;
95 case OF_WRITE
: access
= GENERIC_WRITE
; break;
96 case OF_READWRITE
: access
= GENERIC_READ
| GENERIC_WRITE
; break;
97 default: access
= 0; break;
103 case OF_SHARE_EXCLUSIVE
: sharing
= 0; break;
104 case OF_SHARE_DENY_WRITE
: sharing
= FILE_SHARE_READ
; break;
105 case OF_SHARE_DENY_READ
: sharing
= FILE_SHARE_WRITE
; break;
106 case OF_SHARE_DENY_NONE
:
107 case OF_SHARE_COMPAT
:
108 default: sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
110 return CreateFileA( path
, access
, sharing
, NULL
, creation
, FILE_ATTRIBUTE_NORMAL
, 0 );
114 /***********************************************************************
117 * Check if a dir symlink should be returned by FindNextFile.
119 static BOOL
check_dir_symlink( FIND_FIRST_INFO
*info
, const FILE_BOTH_DIR_INFORMATION
*file_info
)
122 ANSI_STRING unix_name
;
123 struct stat st
, parent_st
;
127 str
.MaximumLength
= info
->path
.Length
+ sizeof(WCHAR
) + file_info
->FileNameLength
;
128 if (!(str
.Buffer
= HeapAlloc( GetProcessHeap(), 0, str
.MaximumLength
))) return TRUE
;
129 memcpy( str
.Buffer
, info
->path
.Buffer
, info
->path
.Length
);
130 len
= info
->path
.Length
/ sizeof(WCHAR
);
131 if (!len
|| str
.Buffer
[len
-1] != '\\') str
.Buffer
[len
++] = '\\';
132 memcpy( str
.Buffer
+ len
, file_info
->FileName
, file_info
->FileNameLength
);
133 str
.Length
= len
* sizeof(WCHAR
) + file_info
->FileNameLength
;
135 unix_name
.Buffer
= NULL
;
136 if (!wine_nt_to_unix_file_name( &str
, &unix_name
, OPEN_EXISTING
, FALSE
) &&
137 !stat( unix_name
.Buffer
, &st
))
139 char *p
= unix_name
.Buffer
+ unix_name
.Length
- 1;
141 /* skip trailing slashes */
142 while (p
> unix_name
.Buffer
&& *p
== '/') p
--;
144 while (ret
&& p
> unix_name
.Buffer
)
146 while (p
> unix_name
.Buffer
&& *p
!= '/') p
--;
147 while (p
> unix_name
.Buffer
&& *p
== '/') p
--;
149 if (!stat( unix_name
.Buffer
, &parent_st
) &&
150 parent_st
.st_dev
== st
.st_dev
&&
151 parent_st
.st_ino
== st
.st_ino
)
153 WARN( "suppressing dir symlink %s pointing to parent %s\n",
154 debugstr_wn( str
.Buffer
, str
.Length
/sizeof(WCHAR
) ),
155 debugstr_a( unix_name
.Buffer
));
160 RtlFreeAnsiString( &unix_name
);
161 RtlFreeUnicodeString( &str
);
166 /***********************************************************************
169 * Set the DOS error code from errno.
171 void FILE_SetDosError(void)
173 int save_errno
= errno
; /* errno gets overwritten by printf */
175 TRACE("errno = %d %s\n", errno
, strerror(errno
));
179 SetLastError( ERROR_SHARING_VIOLATION
);
182 SetLastError( ERROR_INVALID_HANDLE
);
185 SetLastError( ERROR_HANDLE_DISK_FULL
);
190 SetLastError( ERROR_ACCESS_DENIED
);
193 SetLastError( ERROR_LOCK_VIOLATION
);
196 SetLastError( ERROR_FILE_NOT_FOUND
);
199 SetLastError( ERROR_CANNOT_MAKE
);
203 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
206 SetLastError( ERROR_FILE_EXISTS
);
210 SetLastError( ERROR_SEEK
);
213 SetLastError( ERROR_DIR_NOT_EMPTY
);
216 SetLastError( ERROR_BAD_FORMAT
);
219 SetLastError( ERROR_PATH_NOT_FOUND
);
222 SetLastError( ERROR_NOT_SAME_DEVICE
);
225 WARN("unknown file error: %s\n", strerror(save_errno
) );
226 SetLastError( ERROR_GEN_FAILURE
);
233 /***********************************************************************
236 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
238 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
239 * there is no possibility for the function to do that twice, taking into
240 * account any called function.
242 WCHAR
*FILE_name_AtoW( LPCSTR name
, BOOL alloc
)
245 UNICODE_STRING strW
, *pstrW
;
248 RtlInitAnsiString( &str
, name
);
249 pstrW
= alloc
? &strW
: &NtCurrentTeb()->StaticUnicodeString
;
251 status
= RtlOemStringToUnicodeString( pstrW
, &str
, alloc
);
253 status
= RtlAnsiStringToUnicodeString( pstrW
, &str
, alloc
);
254 if (status
== STATUS_SUCCESS
) return pstrW
->Buffer
;
256 if (status
== STATUS_BUFFER_OVERFLOW
)
257 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
259 SetLastError( RtlNtStatusToDosError(status
) );
264 /***********************************************************************
267 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
269 DWORD
FILE_name_WtoA( LPCWSTR src
, INT srclen
, LPSTR dest
, INT destlen
)
273 if (srclen
< 0) srclen
= strlenW( src
) + 1;
275 RtlUnicodeToOemN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
277 RtlUnicodeToMultiByteN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
282 /**************************************************************************
283 * SetFileApisToOEM (KERNEL32.@)
285 VOID WINAPI
SetFileApisToOEM(void)
287 oem_file_apis
= TRUE
;
291 /**************************************************************************
292 * SetFileApisToANSI (KERNEL32.@)
294 VOID WINAPI
SetFileApisToANSI(void)
296 oem_file_apis
= FALSE
;
300 /******************************************************************************
301 * AreFileApisANSI (KERNEL32.@)
303 * Determines if file functions are using ANSI
306 * TRUE: Set of file functions is using ANSI code page
307 * FALSE: Set of file functions is using OEM code page
309 BOOL WINAPI
AreFileApisANSI(void)
311 return !oem_file_apis
;
315 /**************************************************************************
316 * Operations on file handles *
317 **************************************************************************/
319 /***********************************************************************
320 * FILE_InitProcessDosHandles
322 * Allocates the default DOS handles for a process. Called either by
323 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
325 static void FILE_InitProcessDosHandles( void )
327 static BOOL init_done
/* = FALSE */;
328 HANDLE cp
= GetCurrentProcess();
330 if (init_done
) return;
332 DuplicateHandle(cp
, GetStdHandle(STD_INPUT_HANDLE
), cp
, &dos_handles
[0],
333 0, TRUE
, DUPLICATE_SAME_ACCESS
);
334 DuplicateHandle(cp
, GetStdHandle(STD_OUTPUT_HANDLE
), cp
, &dos_handles
[1],
335 0, TRUE
, DUPLICATE_SAME_ACCESS
);
336 DuplicateHandle(cp
, GetStdHandle(STD_ERROR_HANDLE
), cp
, &dos_handles
[2],
337 0, TRUE
, DUPLICATE_SAME_ACCESS
);
338 DuplicateHandle(cp
, GetStdHandle(STD_ERROR_HANDLE
), cp
, &dos_handles
[3],
339 0, TRUE
, DUPLICATE_SAME_ACCESS
);
340 DuplicateHandle(cp
, GetStdHandle(STD_ERROR_HANDLE
), cp
, &dos_handles
[4],
341 0, TRUE
, DUPLICATE_SAME_ACCESS
);
345 /******************************************************************
346 * FILE_ReadWriteApc (internal)
348 static void WINAPI
FILE_ReadWriteApc(void* apc_user
, PIO_STATUS_BLOCK io_status
, ULONG reserved
)
350 LPOVERLAPPED_COMPLETION_ROUTINE cr
= (LPOVERLAPPED_COMPLETION_ROUTINE
)apc_user
;
352 cr(RtlNtStatusToDosError(io_status
->u
.Status
), io_status
->Information
, (LPOVERLAPPED
)io_status
);
356 /***********************************************************************
357 * ReadFileEx (KERNEL32.@)
359 BOOL WINAPI
ReadFileEx(HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
360 LPOVERLAPPED overlapped
,
361 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
363 LARGE_INTEGER offset
;
365 PIO_STATUS_BLOCK io_status
;
367 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile
, buffer
, bytesToRead
, overlapped
, lpCompletionRoutine
);
371 SetLastError(ERROR_INVALID_PARAMETER
);
375 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
376 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
377 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
378 io_status
->u
.Status
= STATUS_PENDING
;
379 io_status
->Information
= 0;
381 status
= NtReadFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
382 io_status
, buffer
, bytesToRead
, &offset
, NULL
);
386 SetLastError( RtlNtStatusToDosError(status
) );
393 /***********************************************************************
394 * ReadFile (KERNEL32.@)
396 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
397 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
399 LARGE_INTEGER offset
;
400 PLARGE_INTEGER poffset
= NULL
;
401 IO_STATUS_BLOCK iosb
;
402 PIO_STATUS_BLOCK io_status
= &iosb
;
405 LPVOID cvalue
= NULL
;
407 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToRead
,
408 bytesRead
, overlapped
);
410 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
411 if (!bytesToRead
) return TRUE
;
413 if (is_console_handle(hFile
))
414 return ReadConsoleA(hFile
, buffer
, bytesToRead
, bytesRead
, NULL
);
416 if (overlapped
!= NULL
)
418 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
419 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
421 hEvent
= overlapped
->hEvent
;
422 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
423 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
425 io_status
->u
.Status
= STATUS_PENDING
;
426 io_status
->Information
= 0;
428 status
= NtReadFile(hFile
, hEvent
, NULL
, cvalue
, io_status
, buffer
, bytesToRead
, poffset
, NULL
);
430 if (status
== STATUS_PENDING
&& !overlapped
)
432 WaitForSingleObject( hFile
, INFINITE
);
433 status
= io_status
->u
.Status
;
436 if (status
!= STATUS_PENDING
&& bytesRead
)
437 *bytesRead
= io_status
->Information
;
439 if (status
&& status
!= STATUS_END_OF_FILE
&& status
!= STATUS_TIMEOUT
)
441 SetLastError( RtlNtStatusToDosError(status
) );
448 /***********************************************************************
449 * WriteFileEx (KERNEL32.@)
451 BOOL WINAPI
WriteFileEx(HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
452 LPOVERLAPPED overlapped
,
453 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
455 LARGE_INTEGER offset
;
457 PIO_STATUS_BLOCK io_status
;
459 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, overlapped
, lpCompletionRoutine
);
461 if (overlapped
== NULL
)
463 SetLastError(ERROR_INVALID_PARAMETER
);
466 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
467 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
469 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
470 io_status
->u
.Status
= STATUS_PENDING
;
471 io_status
->Information
= 0;
473 status
= NtWriteFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
474 io_status
, buffer
, bytesToWrite
, &offset
, NULL
);
476 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
481 /***********************************************************************
482 * WriteFile (KERNEL32.@)
484 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
485 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
487 HANDLE hEvent
= NULL
;
488 LARGE_INTEGER offset
;
489 PLARGE_INTEGER poffset
= NULL
;
491 IO_STATUS_BLOCK iosb
;
492 PIO_STATUS_BLOCK piosb
= &iosb
;
493 LPVOID cvalue
= NULL
;
495 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, bytesWritten
, overlapped
);
497 if (is_console_handle(hFile
))
498 return WriteConsoleA(hFile
, buffer
, bytesToWrite
, bytesWritten
, NULL
);
502 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
503 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
505 hEvent
= overlapped
->hEvent
;
506 piosb
= (PIO_STATUS_BLOCK
)overlapped
;
507 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
509 piosb
->u
.Status
= STATUS_PENDING
;
510 piosb
->Information
= 0;
512 status
= NtWriteFile(hFile
, hEvent
, NULL
, cvalue
, piosb
,
513 buffer
, bytesToWrite
, poffset
, NULL
);
515 /* FIXME: NtWriteFile does not always cause page faults, generate them now */
516 if (status
== STATUS_INVALID_USER_BUFFER
&& !IsBadReadPtr( buffer
, bytesToWrite
))
518 status
= NtWriteFile(hFile
, hEvent
, NULL
, cvalue
, piosb
,
519 buffer
, bytesToWrite
, poffset
, NULL
);
520 if (status
!= STATUS_INVALID_USER_BUFFER
)
521 FIXME("Could not access memory (%p,%d) at first, now OK. Protected by DIBSection code?\n",
522 buffer
, bytesToWrite
);
525 if (status
== STATUS_PENDING
&& !overlapped
)
527 WaitForSingleObject( hFile
, INFINITE
);
528 status
= piosb
->u
.Status
;
531 if (status
!= STATUS_PENDING
&& bytesWritten
)
532 *bytesWritten
= piosb
->Information
;
534 if (status
&& status
!= STATUS_TIMEOUT
)
536 SetLastError( RtlNtStatusToDosError(status
) );
543 /***********************************************************************
544 * GetOverlappedResult (KERNEL32.@)
546 * Check the result of an Asynchronous data transfer from a file.
549 * HANDLE hFile [in] handle of file to check on
550 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
551 * LPDWORD lpTransferred [in/out] number of bytes transferred
552 * BOOL bWait [in] wait for the transfer to complete ?
558 * If successful (and relevant) lpTransferred will hold the number of
559 * bytes transferred during the async operation.
561 BOOL WINAPI
GetOverlappedResult(HANDLE hFile
, LPOVERLAPPED lpOverlapped
,
562 LPDWORD lpTransferred
, BOOL bWait
)
566 TRACE( "(%p %p %p %x)\n", hFile
, lpOverlapped
, lpTransferred
, bWait
);
568 if ( lpOverlapped
== NULL
)
570 ERR("lpOverlapped was null\n");
574 status
= lpOverlapped
->Internal
;
575 if (status
== STATUS_PENDING
)
579 SetLastError( ERROR_IO_INCOMPLETE
);
583 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: hFile
,
584 INFINITE
) == WAIT_FAILED
)
586 status
= lpOverlapped
->Internal
;
589 if (lpTransferred
) *lpTransferred
= lpOverlapped
->InternalHigh
;
591 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
595 /***********************************************************************
596 * CancelIo (KERNEL32.@)
598 * Cancels pending I/O operations initiated by the current thread on a file.
601 * handle [I] File handle.
605 * Failure: FALSE, check GetLastError().
607 BOOL WINAPI
CancelIo(HANDLE handle
)
609 IO_STATUS_BLOCK io_status
;
611 NtCancelIoFile(handle
, &io_status
);
612 if (io_status
.u
.Status
)
614 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
620 /***********************************************************************
621 * _hread (KERNEL32.@)
623 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
625 return _lread( hFile
, buffer
, count
);
629 /***********************************************************************
630 * _hwrite (KERNEL32.@)
632 * experimentation yields that _lwrite:
633 * o truncates the file at the current position with
635 * o returns 0 on a 0 length write
636 * o works with console handles
639 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
643 TRACE("%d %p %d\n", handle
, buffer
, count
);
647 /* Expand or truncate at current position */
648 if (!SetEndOfFile( LongToHandle(handle
) )) return HFILE_ERROR
;
651 if (!WriteFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
657 /***********************************************************************
658 * _lclose (KERNEL32.@)
660 HFILE WINAPI
_lclose( HFILE hFile
)
662 TRACE("handle %d\n", hFile
);
663 return CloseHandle( LongToHandle(hFile
) ) ? 0 : HFILE_ERROR
;
667 /***********************************************************************
668 * _lcreat (KERNEL32.@)
670 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
674 /* Mask off all flags not explicitly allowed by the doc */
675 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
676 TRACE("%s %02x\n", path
, attr
);
677 hfile
= CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
678 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
679 CREATE_ALWAYS
, attr
, 0 );
680 return HandleToLong(hfile
);
684 /***********************************************************************
685 * _lopen (KERNEL32.@)
687 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
691 TRACE("(%s,%04x)\n", debugstr_a(path
), mode
);
692 hfile
= create_file_OF( path
, mode
& ~OF_CREATE
);
693 return HandleToLong(hfile
);
696 /***********************************************************************
697 * _lread (KERNEL32.@)
699 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
702 if (!ReadFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
708 /***********************************************************************
709 * _llseek (KERNEL32.@)
711 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
713 return SetFilePointer( LongToHandle(hFile
), lOffset
, NULL
, nOrigin
);
717 /***********************************************************************
718 * _lwrite (KERNEL32.@)
720 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
722 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
726 /***********************************************************************
727 * FlushFileBuffers (KERNEL32.@)
729 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
732 IO_STATUS_BLOCK ioblk
;
734 if (is_console_handle( hFile
))
736 /* this will fail (as expected) for an output handle */
737 return FlushConsoleInputBuffer( hFile
);
739 nts
= NtFlushBuffersFile( hFile
, &ioblk
);
740 if (nts
!= STATUS_SUCCESS
)
742 SetLastError( RtlNtStatusToDosError( nts
) );
750 /***********************************************************************
751 * GetFileType (KERNEL32.@)
753 DWORD WINAPI
GetFileType( HANDLE hFile
)
755 FILE_FS_DEVICE_INFORMATION info
;
759 if (is_console_handle( hFile
)) return FILE_TYPE_CHAR
;
761 status
= NtQueryVolumeInformationFile( hFile
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
762 if (status
!= STATUS_SUCCESS
)
764 SetLastError( RtlNtStatusToDosError(status
) );
765 return FILE_TYPE_UNKNOWN
;
768 switch(info
.DeviceType
)
770 case FILE_DEVICE_NULL
:
771 case FILE_DEVICE_SERIAL_PORT
:
772 case FILE_DEVICE_PARALLEL_PORT
:
773 case FILE_DEVICE_TAPE
:
774 case FILE_DEVICE_UNKNOWN
:
775 return FILE_TYPE_CHAR
;
776 case FILE_DEVICE_NAMED_PIPE
:
777 return FILE_TYPE_PIPE
;
779 return FILE_TYPE_DISK
;
784 /***********************************************************************
785 * GetFileInformationByHandle (KERNEL32.@)
787 BOOL WINAPI
GetFileInformationByHandle( HANDLE hFile
, BY_HANDLE_FILE_INFORMATION
*info
)
789 FILE_ALL_INFORMATION all_info
;
793 status
= NtQueryInformationFile( hFile
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
794 if (status
== STATUS_SUCCESS
)
796 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
797 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
798 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
799 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
800 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
801 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
802 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
803 info
->dwVolumeSerialNumber
= 0; /* FIXME */
804 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
805 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
806 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
807 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
808 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
811 SetLastError( RtlNtStatusToDosError(status
) );
816 /***********************************************************************
817 * GetFileSize (KERNEL32.@)
819 * Retrieve the size of a file.
822 * hFile [I] File to retrieve size of.
823 * filesizehigh [O] On return, the high bits of the file size.
826 * Success: The low bits of the file size.
827 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
828 * check GetLastError() for values other than ERROR_SUCCESS.
830 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
833 if (!GetFileSizeEx( hFile
, &size
)) return INVALID_FILE_SIZE
;
834 if (filesizehigh
) *filesizehigh
= size
.u
.HighPart
;
835 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError(0);
836 return size
.u
.LowPart
;
840 /***********************************************************************
841 * GetFileSizeEx (KERNEL32.@)
843 * Retrieve the size of a file.
846 * hFile [I] File to retrieve size of.
847 * lpFileSIze [O] On return, the size of the file.
851 * Failure: FALSE, check GetLastError().
853 BOOL WINAPI
GetFileSizeEx( HANDLE hFile
, PLARGE_INTEGER lpFileSize
)
855 FILE_END_OF_FILE_INFORMATION info
;
859 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileEndOfFileInformation
);
860 if (status
== STATUS_SUCCESS
)
862 *lpFileSize
= info
.EndOfFile
;
865 SetLastError( RtlNtStatusToDosError(status
) );
870 /**************************************************************************
871 * SetEndOfFile (KERNEL32.@)
873 * Sets the current position as the end of the file.
876 * hFile [I] File handle.
880 * Failure: FALSE, check GetLastError().
882 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
884 FILE_POSITION_INFORMATION pos
;
885 FILE_END_OF_FILE_INFORMATION eof
;
889 status
= NtQueryInformationFile( hFile
, &io
, &pos
, sizeof(pos
), FilePositionInformation
);
890 if (status
== STATUS_SUCCESS
)
892 eof
.EndOfFile
= pos
.CurrentByteOffset
;
893 status
= NtSetInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
895 if (status
== STATUS_SUCCESS
) return TRUE
;
896 SetLastError( RtlNtStatusToDosError(status
) );
901 /***********************************************************************
902 * SetFilePointer (KERNEL32.@)
904 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
, DWORD method
)
906 LARGE_INTEGER dist
, newpos
;
910 dist
.u
.LowPart
= distance
;
911 dist
.u
.HighPart
= *highword
;
913 else dist
.QuadPart
= distance
;
915 if (!SetFilePointerEx( hFile
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
917 if (highword
) *highword
= newpos
.u
.HighPart
;
918 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
919 return newpos
.u
.LowPart
;
923 /***********************************************************************
924 * SetFilePointerEx (KERNEL32.@)
926 BOOL WINAPI
SetFilePointerEx( HANDLE hFile
, LARGE_INTEGER distance
,
927 LARGE_INTEGER
*newpos
, DWORD method
)
931 FILE_POSITION_INFORMATION info
;
936 pos
= distance
.QuadPart
;
939 if (NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
941 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
945 FILE_END_OF_FILE_INFORMATION eof
;
946 if (NtQueryInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
948 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
952 SetLastError( ERROR_INVALID_PARAMETER
);
958 SetLastError( ERROR_NEGATIVE_SEEK
);
962 info
.CurrentByteOffset
.QuadPart
= pos
;
963 if (NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
965 if (newpos
) newpos
->QuadPart
= pos
;
969 SetLastError( RtlNtStatusToDosError(io
.u
.Status
) );
973 /***********************************************************************
974 * GetFileTime (KERNEL32.@)
976 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
977 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
979 FILE_BASIC_INFORMATION info
;
983 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
984 if (status
== STATUS_SUCCESS
)
988 lpCreationTime
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
989 lpCreationTime
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
991 if (lpLastAccessTime
)
993 lpLastAccessTime
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
994 lpLastAccessTime
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
998 lpLastWriteTime
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
999 lpLastWriteTime
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1003 SetLastError( RtlNtStatusToDosError(status
) );
1008 /***********************************************************************
1009 * SetFileTime (KERNEL32.@)
1011 BOOL WINAPI
SetFileTime( HANDLE hFile
, const FILETIME
*ctime
,
1012 const FILETIME
*atime
, const FILETIME
*mtime
)
1014 FILE_BASIC_INFORMATION info
;
1018 memset( &info
, 0, sizeof(info
) );
1021 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
1022 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
1026 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
1027 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
1031 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
1032 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
1035 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1036 if (status
== STATUS_SUCCESS
) return TRUE
;
1037 SetLastError( RtlNtStatusToDosError(status
) );
1042 /**************************************************************************
1043 * LockFile (KERNEL32.@)
1045 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1046 DWORD count_low
, DWORD count_high
)
1049 LARGE_INTEGER count
, offset
;
1051 TRACE( "%p %x%08x %x%08x\n",
1052 hFile
, offset_high
, offset_low
, count_high
, count_low
);
1054 count
.u
.LowPart
= count_low
;
1055 count
.u
.HighPart
= count_high
;
1056 offset
.u
.LowPart
= offset_low
;
1057 offset
.u
.HighPart
= offset_high
;
1059 status
= NtLockFile( hFile
, 0, NULL
, NULL
,
1060 NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
);
1062 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1067 /**************************************************************************
1068 * LockFileEx [KERNEL32.@]
1070 * Locks a byte range within an open file for shared or exclusive access.
1077 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1079 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1080 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
1083 LARGE_INTEGER count
, offset
;
1084 LPVOID cvalue
= NULL
;
1088 SetLastError( ERROR_INVALID_PARAMETER
);
1092 TRACE( "%p %x%08x %x%08x flags %x\n",
1093 hFile
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
,
1094 count_high
, count_low
, flags
);
1096 count
.u
.LowPart
= count_low
;
1097 count
.u
.HighPart
= count_high
;
1098 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
1099 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
1101 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1103 status
= NtLockFile( hFile
, overlapped
->hEvent
, NULL
, cvalue
,
1104 NULL
, &offset
, &count
, NULL
,
1105 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
1106 flags
& LOCKFILE_EXCLUSIVE_LOCK
);
1108 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1113 /**************************************************************************
1114 * UnlockFile (KERNEL32.@)
1116 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1117 DWORD count_low
, DWORD count_high
)
1120 LARGE_INTEGER count
, offset
;
1122 count
.u
.LowPart
= count_low
;
1123 count
.u
.HighPart
= count_high
;
1124 offset
.u
.LowPart
= offset_low
;
1125 offset
.u
.HighPart
= offset_high
;
1127 status
= NtUnlockFile( hFile
, NULL
, &offset
, &count
, NULL
);
1128 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1133 /**************************************************************************
1134 * UnlockFileEx (KERNEL32.@)
1136 BOOL WINAPI
UnlockFileEx( HANDLE hFile
, DWORD reserved
, DWORD count_low
, DWORD count_high
,
1137 LPOVERLAPPED overlapped
)
1141 SetLastError( ERROR_INVALID_PARAMETER
);
1144 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
1146 return UnlockFile( hFile
, overlapped
->u
.s
.Offset
, overlapped
->u
.s
.OffsetHigh
, count_low
, count_high
);
1150 /***********************************************************************
1151 * Win32HandleToDosFileHandle (KERNEL32.21)
1153 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1154 * longer valid after this function (even on failure).
1156 * Note: this is not exactly right, since on Win95 the Win32 handles
1157 * are on top of DOS handles and we do it the other way
1158 * around. Should be good enough though.
1160 HFILE WINAPI
Win32HandleToDosFileHandle( HANDLE handle
)
1164 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
1167 FILE_InitProcessDosHandles();
1168 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
1169 if (!dos_handles
[i
])
1171 dos_handles
[i
] = handle
;
1172 TRACE("Got %d for h32 %p\n", i
, handle
);
1175 CloseHandle( handle
);
1176 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
1181 /***********************************************************************
1182 * DosFileHandleToWin32Handle (KERNEL32.20)
1184 * Return the Win32 handle for a DOS handle.
1186 * Note: this is not exactly right, since on Win95 the Win32 handles
1187 * are on top of DOS handles and we do it the other way
1188 * around. Should be good enough though.
1190 HANDLE WINAPI
DosFileHandleToWin32Handle( HFILE handle
)
1192 HFILE16 hfile
= (HFILE16
)handle
;
1193 if (hfile
< 5) FILE_InitProcessDosHandles();
1194 if ((hfile
>= DOS_TABLE_SIZE
) || !dos_handles
[hfile
])
1196 SetLastError( ERROR_INVALID_HANDLE
);
1197 return INVALID_HANDLE_VALUE
;
1199 return dos_handles
[hfile
];
1203 /*************************************************************************
1204 * SetHandleCount (KERNEL32.@)
1206 UINT WINAPI
SetHandleCount( UINT count
)
1208 return min( 256, count
);
1212 /***********************************************************************
1213 * DisposeLZ32Handle (KERNEL32.22)
1215 * Note: this is not entirely correct, we should only close the
1216 * 32-bit handle and not the 16-bit one, but we cannot do
1217 * this because of the way our DOS handles are implemented.
1218 * It shouldn't break anything though.
1220 void WINAPI
DisposeLZ32Handle( HANDLE handle
)
1224 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
)) return;
1226 for (i
= 5; i
< DOS_TABLE_SIZE
; i
++)
1227 if (dos_handles
[i
] == handle
)
1230 CloseHandle( handle
);
1235 /**************************************************************************
1236 * Operations on file names *
1237 **************************************************************************/
1240 /*************************************************************************
1241 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1243 * Creates or opens an object, and returns a handle that can be used to
1244 * access that object.
1248 * filename [in] pointer to filename to be accessed
1249 * access [in] access mode requested
1250 * sharing [in] share mode
1251 * sa [in] pointer to security attributes
1252 * creation [in] how to create the file
1253 * attributes [in] attributes for newly created file
1254 * template [in] handle to file with extended attributes to copy
1257 * Success: Open handle to specified file
1258 * Failure: INVALID_HANDLE_VALUE
1260 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
1261 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1262 DWORD attributes
, HANDLE
template )
1266 OBJECT_ATTRIBUTES attr
;
1267 UNICODE_STRING nameW
;
1271 static const WCHAR bkslashes_with_dotW
[] = {'\\','\\','.','\\',0};
1272 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
1273 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
1274 SECURITY_QUALITY_OF_SERVICE qos
;
1276 static const UINT nt_disposition
[5] =
1278 FILE_CREATE
, /* CREATE_NEW */
1279 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
1280 FILE_OPEN
, /* OPEN_EXISTING */
1281 FILE_OPEN_IF
, /* OPEN_ALWAYS */
1282 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
1288 if (!filename
|| !filename
[0])
1290 SetLastError( ERROR_PATH_NOT_FOUND
);
1291 return INVALID_HANDLE_VALUE
;
1294 TRACE("%s %s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
1295 (access
& GENERIC_READ
)?"GENERIC_READ ":"",
1296 (access
& GENERIC_WRITE
)?"GENERIC_WRITE ":"",
1297 (!access
)?"QUERY_ACCESS ":"",
1298 (sharing
& FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
1299 (sharing
& FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
1300 (sharing
& FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
1301 creation
, attributes
);
1303 /* Open a console for CONIN$ or CONOUT$ */
1305 if (!strcmpiW(filename
, coninW
) || !strcmpiW(filename
, conoutW
))
1307 ret
= OpenConsoleW(filename
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1311 if (!strncmpW(filename
, bkslashes_with_dotW
, 4))
1313 static const WCHAR pipeW
[] = {'P','I','P','E','\\',0};
1314 static const WCHAR mailslotW
[] = {'M','A','I','L','S','L','O','T','\\',0};
1316 if ((isalphaW(filename
[4]) && filename
[5] == ':' && filename
[6] == '\0') ||
1317 !strncmpiW( filename
+ 4, pipeW
, 5 ) ||
1318 !strncmpiW( filename
+ 4, mailslotW
, 9 ))
1322 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
1324 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
1326 else if (!(GetVersion() & 0x80000000))
1330 else if (filename
[4])
1332 ret
= VXD_Open( filename
+4, access
, sa
);
1337 SetLastError( ERROR_INVALID_NAME
);
1338 return INVALID_HANDLE_VALUE
;
1341 else dosdev
= RtlIsDosDeviceName_U( filename
);
1345 static const WCHAR conW
[] = {'C','O','N'};
1347 if (LOWORD(dosdev
) == sizeof(conW
) &&
1348 !memicmpW( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), conW
, sizeof(conW
)/sizeof(WCHAR
)))
1350 switch (access
& (GENERIC_READ
|GENERIC_WRITE
))
1353 ret
= OpenConsoleW(coninW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1356 ret
= OpenConsoleW(conoutW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1359 SetLastError( ERROR_FILE_NOT_FOUND
);
1360 return INVALID_HANDLE_VALUE
;
1365 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
1367 SetLastError( ERROR_INVALID_PARAMETER
);
1368 return INVALID_HANDLE_VALUE
;
1371 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
1373 SetLastError( ERROR_PATH_NOT_FOUND
);
1374 return INVALID_HANDLE_VALUE
;
1377 /* now call NtCreateFile */
1380 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
1381 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
1383 options
|= FILE_NON_DIRECTORY_FILE
;
1384 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
1386 options
|= FILE_DELETE_ON_CLOSE
;
1389 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
1390 options
|= FILE_SYNCHRONOUS_IO_ALERT
;
1391 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
1392 options
|= FILE_RANDOM_ACCESS
;
1393 attributes
&= FILE_ATTRIBUTE_VALID_FLAGS
;
1395 attr
.Length
= sizeof(attr
);
1396 attr
.RootDirectory
= 0;
1397 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1398 attr
.ObjectName
= &nameW
;
1399 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1400 if (attributes
& SECURITY_SQOS_PRESENT
)
1402 qos
.Length
= sizeof(qos
);
1403 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
1404 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
1405 qos
.EffectiveOnly
= attributes
& SECURITY_EFFECTIVE_ONLY
? TRUE
: FALSE
;
1406 attr
.SecurityQualityOfService
= &qos
;
1409 attr
.SecurityQualityOfService
= NULL
;
1411 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
1413 status
= NtCreateFile( &ret
, access
, &attr
, &io
, NULL
, attributes
,
1414 sharing
, nt_disposition
[creation
- CREATE_NEW
],
1418 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
1419 ret
= INVALID_HANDLE_VALUE
;
1421 /* In the case file creation was rejected due to CREATE_NEW flag
1422 * was specified and file with that name already exists, correct
1423 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1424 * Note: RtlNtStatusToDosError is not the subject to blame here.
1426 if (status
== STATUS_OBJECT_NAME_COLLISION
)
1427 SetLastError( ERROR_FILE_EXISTS
);
1429 SetLastError( RtlNtStatusToDosError(status
) );
1431 else SetLastError(0);
1432 RtlFreeUnicodeString( &nameW
);
1435 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
1436 TRACE("returning %p\n", ret
);
1442 /*************************************************************************
1443 * CreateFileA (KERNEL32.@)
1447 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
1448 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1449 DWORD attributes
, HANDLE
template)
1453 if (!(nameW
= FILE_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1454 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
1458 /***********************************************************************
1459 * DeleteFileW (KERNEL32.@)
1464 * path [I] Path to the file to delete.
1468 * Failure: FALSE, check GetLastError().
1470 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1472 UNICODE_STRING nameW
;
1473 OBJECT_ATTRIBUTES attr
;
1476 TRACE("%s\n", debugstr_w(path
) );
1478 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
1480 SetLastError( ERROR_PATH_NOT_FOUND
);
1484 attr
.Length
= sizeof(attr
);
1485 attr
.RootDirectory
= 0;
1486 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1487 attr
.ObjectName
= &nameW
;
1488 attr
.SecurityDescriptor
= NULL
;
1489 attr
.SecurityQualityOfService
= NULL
;
1491 status
= NtDeleteFile(&attr
);
1492 RtlFreeUnicodeString( &nameW
);
1495 SetLastError( RtlNtStatusToDosError(status
) );
1502 /***********************************************************************
1503 * DeleteFileA (KERNEL32.@)
1507 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1511 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1512 return DeleteFileW( pathW
);
1516 /**************************************************************************
1517 * ReplaceFileW (KERNEL32.@)
1518 * ReplaceFile (KERNEL32.@)
1520 BOOL WINAPI
ReplaceFileW(LPCWSTR lpReplacedFileName
,LPCWSTR lpReplacementFileName
,
1521 LPCWSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1522 LPVOID lpExclude
, LPVOID lpReserved
)
1524 FIXME("(%s,%s,%s,%08x,%p,%p) stub\n",debugstr_w(lpReplacedFileName
),debugstr_w(lpReplacementFileName
),
1525 debugstr_w(lpBackupFileName
),dwReplaceFlags
,lpExclude
,lpReserved
);
1526 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT
);
1531 /**************************************************************************
1532 * ReplaceFileA (KERNEL32.@)
1534 BOOL WINAPI
ReplaceFileA(LPCSTR lpReplacedFileName
,LPCSTR lpReplacementFileName
,
1535 LPCSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1536 LPVOID lpExclude
, LPVOID lpReserved
)
1538 FIXME("(%s,%s,%s,%08x,%p,%p) stub\n",lpReplacedFileName
,lpReplacementFileName
,
1539 lpBackupFileName
,dwReplaceFlags
,lpExclude
,lpReserved
);
1540 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT
);
1545 /*************************************************************************
1546 * FindFirstFileExW (KERNEL32.@)
1548 HANDLE WINAPI
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1549 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1550 LPVOID filter
, DWORD flags
)
1553 FIND_FIRST_INFO
*info
= NULL
;
1554 UNICODE_STRING nt_name
;
1555 OBJECT_ATTRIBUTES attr
;
1560 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1562 if ((search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1565 FIXME("options not implemented 0x%08x 0x%08x\n", search_op
, flags
);
1566 return INVALID_HANDLE_VALUE
;
1568 if (level
!= FindExInfoStandard
)
1570 FIXME("info level %d not implemented\n", level
);
1571 return INVALID_HANDLE_VALUE
;
1574 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1576 SetLastError( ERROR_PATH_NOT_FOUND
);
1577 return INVALID_HANDLE_VALUE
;
1580 if (!(info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
))))
1582 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1586 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1588 static const WCHAR dotW
[] = {'.',0};
1591 /* we still need to check that the directory can be opened */
1595 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1597 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1600 memcpy( dir
, filename
, HIWORD(device
) );
1601 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1603 RtlFreeUnicodeString( &nt_name
);
1604 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: dotW
, &nt_name
, &mask
, NULL
))
1606 HeapFree( GetProcessHeap(), 0, dir
);
1607 SetLastError( ERROR_PATH_NOT_FOUND
);
1610 HeapFree( GetProcessHeap(), 0, dir
);
1611 RtlInitUnicodeString( &info
->mask
, NULL
);
1613 else if (!mask
|| !*mask
)
1615 SetLastError( ERROR_FILE_NOT_FOUND
);
1620 if (!RtlCreateUnicodeString( &info
->mask
, mask
))
1622 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1626 /* truncate dir name before mask */
1628 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
1631 /* check if path is the root of the drive */
1632 info
->is_root
= FALSE
;
1633 p
= nt_name
.Buffer
+ 4; /* skip \??\ prefix */
1634 if (p
[0] && p
[1] == ':')
1637 while (*p
== '\\') p
++;
1638 info
->is_root
= (*p
== 0);
1641 attr
.Length
= sizeof(attr
);
1642 attr
.RootDirectory
= 0;
1643 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1644 attr
.ObjectName
= &nt_name
;
1645 attr
.SecurityDescriptor
= NULL
;
1646 attr
.SecurityQualityOfService
= NULL
;
1648 status
= NtOpenFile( &info
->handle
, GENERIC_READ
, &attr
, &io
,
1649 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1650 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1652 if (status
!= STATUS_SUCCESS
)
1654 RtlFreeUnicodeString( &info
->mask
);
1655 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1656 SetLastError( ERROR_PATH_NOT_FOUND
);
1658 SetLastError( RtlNtStatusToDosError(status
) );
1662 RtlInitializeCriticalSection( &info
->cs
);
1663 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
1664 info
->path
= nt_name
;
1665 info
->magic
= FIND_FIRST_MAGIC
;
1668 info
->search_op
= search_op
;
1672 WIN32_FIND_DATAW
*wfd
= data
;
1674 memset( wfd
, 0, sizeof(*wfd
) );
1675 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
1676 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1677 CloseHandle( info
->handle
);
1684 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1685 FileBothDirectoryInformation
, FALSE
, &info
->mask
, TRUE
);
1689 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1690 return INVALID_HANDLE_VALUE
;
1692 info
->data_len
= io
.Information
;
1693 if (!FindNextFileW( info
, data
))
1695 TRACE( "%s not found\n", debugstr_w(filename
) );
1697 SetLastError( ERROR_FILE_NOT_FOUND
);
1698 return INVALID_HANDLE_VALUE
;
1700 if (!strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1702 /* we can't find two files with the same name */
1703 CloseHandle( info
->handle
);
1710 HeapFree( GetProcessHeap(), 0, info
);
1711 RtlFreeUnicodeString( &nt_name
);
1712 return INVALID_HANDLE_VALUE
;
1716 /*************************************************************************
1717 * FindNextFileW (KERNEL32.@)
1719 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1721 FIND_FIRST_INFO
*info
;
1722 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1725 TRACE("%p %p\n", handle
, data
);
1727 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1729 SetLastError( ERROR_INVALID_HANDLE
);
1732 info
= (FIND_FIRST_INFO
*)handle
;
1733 if (info
->magic
!= FIND_FIRST_MAGIC
)
1735 SetLastError( ERROR_INVALID_HANDLE
);
1739 RtlEnterCriticalSection( &info
->cs
);
1741 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
1744 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
1748 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1749 FileBothDirectoryInformation
, FALSE
, &info
->mask
, FALSE
);
1752 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1753 if (io
.u
.Status
== STATUS_NO_MORE_FILES
)
1755 CloseHandle( info
->handle
);
1760 info
->data_len
= io
.Information
;
1764 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
1766 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
1767 else info
->data_pos
= info
->data_len
;
1769 /* don't return '.' and '..' in the root of the drive */
1772 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
1773 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
1774 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
1777 /* check for dir symlink */
1778 if ((dir_info
->FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
1779 (dir_info
->FileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
) &&
1780 strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1782 if (!check_dir_symlink( info
, dir_info
)) continue;
1784 if (info
->search_op
== FindExSearchLimitToDirectories
&&
1785 (dir_info
->FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == 0)
1788 data
->dwFileAttributes
= dir_info
->FileAttributes
;
1789 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
1790 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
1791 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
1792 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
1793 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
1794 data
->dwReserved0
= 0;
1795 data
->dwReserved1
= 0;
1797 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
1798 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
1799 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
1800 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
1802 TRACE("returning %s (%s)\n",
1803 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
1809 RtlLeaveCriticalSection( &info
->cs
);
1814 /*************************************************************************
1815 * FindClose (KERNEL32.@)
1817 BOOL WINAPI
FindClose( HANDLE handle
)
1819 FIND_FIRST_INFO
*info
= (FIND_FIRST_INFO
*)handle
;
1821 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1823 SetLastError( ERROR_INVALID_HANDLE
);
1829 if (info
->magic
== FIND_FIRST_MAGIC
)
1831 RtlEnterCriticalSection( &info
->cs
);
1832 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
1835 if (info
->handle
) CloseHandle( info
->handle
);
1837 RtlFreeUnicodeString( &info
->mask
);
1838 info
->mask
.Buffer
= NULL
;
1839 RtlFreeUnicodeString( &info
->path
);
1842 RtlLeaveCriticalSection( &info
->cs
);
1843 info
->cs
.DebugInfo
->Spare
[0] = 0;
1844 RtlDeleteCriticalSection( &info
->cs
);
1845 HeapFree( GetProcessHeap(), 0, info
);
1851 WARN("Illegal handle %p\n", handle
);
1852 SetLastError( ERROR_INVALID_HANDLE
);
1861 /*************************************************************************
1862 * FindFirstFileA (KERNEL32.@)
1864 HANDLE WINAPI
FindFirstFileA( LPCSTR lpFileName
, WIN32_FIND_DATAA
*lpFindData
)
1866 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
1867 FindExSearchNameMatch
, NULL
, 0);
1870 /*************************************************************************
1871 * FindFirstFileExA (KERNEL32.@)
1873 HANDLE WINAPI
FindFirstFileExA( LPCSTR lpFileName
, FINDEX_INFO_LEVELS fInfoLevelId
,
1874 LPVOID lpFindFileData
, FINDEX_SEARCH_OPS fSearchOp
,
1875 LPVOID lpSearchFilter
, DWORD dwAdditionalFlags
)
1878 WIN32_FIND_DATAA
*dataA
;
1879 WIN32_FIND_DATAW dataW
;
1882 if (!(nameW
= FILE_name_AtoW( lpFileName
, FALSE
))) return INVALID_HANDLE_VALUE
;
1884 handle
= FindFirstFileExW(nameW
, fInfoLevelId
, &dataW
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
1885 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
1887 dataA
= (WIN32_FIND_DATAA
*) lpFindFileData
;
1888 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
1889 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
1890 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
1891 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
1892 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
1893 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
1894 FILE_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
1895 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
1896 sizeof(dataA
->cAlternateFileName
) );
1901 /*************************************************************************
1902 * FindFirstFileW (KERNEL32.@)
1904 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
1906 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
1907 FindExSearchNameMatch
, NULL
, 0);
1911 /*************************************************************************
1912 * FindNextFileA (KERNEL32.@)
1914 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
1916 WIN32_FIND_DATAW dataW
;
1918 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
1919 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
1920 data
->ftCreationTime
= dataW
.ftCreationTime
;
1921 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
1922 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
1923 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
1924 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
1925 FILE_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
1926 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
1927 sizeof(data
->cAlternateFileName
) );
1932 /**************************************************************************
1933 * GetFileAttributesW (KERNEL32.@)
1935 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
1937 FILE_BASIC_INFORMATION info
;
1938 UNICODE_STRING nt_name
;
1939 OBJECT_ATTRIBUTES attr
;
1942 TRACE("%s\n", debugstr_w(name
));
1944 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1946 SetLastError( ERROR_PATH_NOT_FOUND
);
1947 return INVALID_FILE_ATTRIBUTES
;
1950 attr
.Length
= sizeof(attr
);
1951 attr
.RootDirectory
= 0;
1952 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1953 attr
.ObjectName
= &nt_name
;
1954 attr
.SecurityDescriptor
= NULL
;
1955 attr
.SecurityQualityOfService
= NULL
;
1957 status
= NtQueryAttributesFile( &attr
, &info
);
1958 RtlFreeUnicodeString( &nt_name
);
1960 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
1962 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1963 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
1965 SetLastError( RtlNtStatusToDosError(status
) );
1966 return INVALID_FILE_ATTRIBUTES
;
1970 /**************************************************************************
1971 * GetFileAttributesA (KERNEL32.@)
1973 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
1977 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
1978 return GetFileAttributesW( nameW
);
1982 /**************************************************************************
1983 * SetFileAttributesW (KERNEL32.@)
1985 BOOL WINAPI
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
1987 UNICODE_STRING nt_name
;
1988 OBJECT_ATTRIBUTES attr
;
1993 TRACE("%s %x\n", debugstr_w(name
), attributes
);
1995 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1997 SetLastError( ERROR_PATH_NOT_FOUND
);
2001 attr
.Length
= sizeof(attr
);
2002 attr
.RootDirectory
= 0;
2003 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2004 attr
.ObjectName
= &nt_name
;
2005 attr
.SecurityDescriptor
= NULL
;
2006 attr
.SecurityQualityOfService
= NULL
;
2008 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2009 RtlFreeUnicodeString( &nt_name
);
2011 if (status
== STATUS_SUCCESS
)
2013 FILE_BASIC_INFORMATION info
;
2015 memset( &info
, 0, sizeof(info
) );
2016 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2017 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2021 if (status
== STATUS_SUCCESS
) return TRUE
;
2022 SetLastError( RtlNtStatusToDosError(status
) );
2027 /**************************************************************************
2028 * SetFileAttributesA (KERNEL32.@)
2030 BOOL WINAPI
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2034 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2035 return SetFileAttributesW( nameW
, attributes
);
2039 /**************************************************************************
2040 * GetFileAttributesExW (KERNEL32.@)
2042 BOOL WINAPI
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2044 FILE_NETWORK_OPEN_INFORMATION info
;
2045 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
2046 UNICODE_STRING nt_name
;
2047 OBJECT_ATTRIBUTES attr
;
2050 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
2052 if (level
!= GetFileExInfoStandard
)
2054 SetLastError( ERROR_INVALID_PARAMETER
);
2058 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2060 SetLastError( ERROR_PATH_NOT_FOUND
);
2064 attr
.Length
= sizeof(attr
);
2065 attr
.RootDirectory
= 0;
2066 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2067 attr
.ObjectName
= &nt_name
;
2068 attr
.SecurityDescriptor
= NULL
;
2069 attr
.SecurityQualityOfService
= NULL
;
2071 status
= NtQueryFullAttributesFile( &attr
, &info
);
2072 RtlFreeUnicodeString( &nt_name
);
2074 if (status
!= STATUS_SUCCESS
)
2076 SetLastError( RtlNtStatusToDosError(status
) );
2080 data
->dwFileAttributes
= info
.FileAttributes
;
2081 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
2082 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
2083 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
2084 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
2085 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
2086 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
2087 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
2088 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
2093 /**************************************************************************
2094 * GetFileAttributesExA (KERNEL32.@)
2096 BOOL WINAPI
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2100 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2101 return GetFileAttributesExW( nameW
, level
, ptr
);
2105 /******************************************************************************
2106 * GetCompressedFileSizeW (KERNEL32.@)
2108 * Get the actual number of bytes used on disk.
2111 * Success: Low-order doubleword of number of bytes
2112 * Failure: INVALID_FILE_SIZE
2114 DWORD WINAPI
GetCompressedFileSizeW(
2115 LPCWSTR name
, /* [in] Pointer to name of file */
2116 LPDWORD size_high
) /* [out] Receives high-order doubleword of size */
2118 UNICODE_STRING nt_name
;
2119 OBJECT_ATTRIBUTES attr
;
2123 DWORD ret
= INVALID_FILE_SIZE
;
2125 TRACE("%s %p\n", debugstr_w(name
), size_high
);
2127 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2129 SetLastError( ERROR_PATH_NOT_FOUND
);
2130 return INVALID_FILE_SIZE
;
2133 attr
.Length
= sizeof(attr
);
2134 attr
.RootDirectory
= 0;
2135 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2136 attr
.ObjectName
= &nt_name
;
2137 attr
.SecurityDescriptor
= NULL
;
2138 attr
.SecurityQualityOfService
= NULL
;
2140 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2141 RtlFreeUnicodeString( &nt_name
);
2143 if (status
== STATUS_SUCCESS
)
2145 /* we don't support compressed files, simply return the file size */
2146 ret
= GetFileSize( handle
, size_high
);
2149 else SetLastError( RtlNtStatusToDosError(status
) );
2155 /******************************************************************************
2156 * GetCompressedFileSizeA (KERNEL32.@)
2158 * See GetCompressedFileSizeW.
2160 DWORD WINAPI
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
2164 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
2165 return GetCompressedFileSizeW( nameW
, size_high
);
2169 /***********************************************************************
2170 * OpenFile (KERNEL32.@)
2172 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
2176 WORD filedatetime
[2];
2178 if (!ofs
) return HFILE_ERROR
;
2180 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
2181 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
2182 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
2183 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
2184 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
2185 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
2186 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
2187 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
2188 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
2189 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
2190 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
2191 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
2192 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
2193 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
2194 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
2195 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
2196 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
2197 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
2201 ofs
->cBytes
= sizeof(OFSTRUCT
);
2203 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
2205 if (!name
) return HFILE_ERROR
;
2207 TRACE("%s %04x\n", name
, mode
);
2209 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2210 Are there any cases where getting the path here is wrong?
2211 Uwe Bonnes 1997 Apr 2 */
2212 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
)) goto error
;
2214 /* OF_PARSE simply fills the structure */
2216 if (mode
& OF_PARSE
)
2218 ofs
->fFixedDisk
= (GetDriveTypeA( ofs
->szPathName
) != DRIVE_REMOVABLE
);
2219 TRACE("(%s): OF_PARSE, res = '%s'\n", name
, ofs
->szPathName
);
2223 /* OF_CREATE is completely different from all other options, so
2226 if (mode
& OF_CREATE
)
2228 if ((handle
= create_file_OF( name
, mode
)) == INVALID_HANDLE_VALUE
)
2233 /* Now look for the file */
2235 if (!SearchPathA( NULL
, name
, NULL
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
))
2238 TRACE("found %s\n", debugstr_a(ofs
->szPathName
) );
2240 if (mode
& OF_DELETE
)
2242 if (!DeleteFileA( ofs
->szPathName
)) goto error
;
2243 TRACE("(%s): OF_DELETE return = OK\n", name
);
2247 handle
= LongToHandle(_lopen( ofs
->szPathName
, mode
));
2248 if (handle
== INVALID_HANDLE_VALUE
) goto error
;
2250 GetFileTime( handle
, NULL
, NULL
, &filetime
);
2251 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
2252 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
2254 if (ofs
->Reserved1
!= filedatetime
[0] || ofs
->Reserved2
!= filedatetime
[1] )
2256 CloseHandle( handle
);
2257 WARN("(%s): OF_VERIFY failed\n", name
);
2258 /* FIXME: what error here? */
2259 SetLastError( ERROR_FILE_NOT_FOUND
);
2263 ofs
->Reserved1
= filedatetime
[0];
2264 ofs
->Reserved2
= filedatetime
[1];
2266 TRACE("(%s): OK, return = %p\n", name
, handle
);
2267 if (mode
& OF_EXIST
) /* Return TRUE instead of a handle */
2269 CloseHandle( handle
);
2272 return HandleToLong(handle
);
2274 error
: /* We get here if there was an error opening the file */
2275 ofs
->nErrCode
= GetLastError();
2276 WARN("(%s): return = HFILE_ERROR error= %d\n", name
,ofs
->nErrCode
);