2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 2008 Jeff Zaroyko
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
37 #define WIN32_NO_STATUS
43 #include "wine/winbase16.h"
44 #include "kernel_private.h"
46 #include "wine/exception.h"
47 #include "wine/unicode.h"
48 #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 * ReadFileScatter (KERNEL32.@)
396 BOOL WINAPI
ReadFileScatter( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
397 LPDWORD reserved
, LPOVERLAPPED overlapped
)
399 PIO_STATUS_BLOCK io_status
;
400 LARGE_INTEGER offset
;
403 TRACE( "(%p %p %u %p)\n", file
, segments
, count
, overlapped
);
405 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
406 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
407 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
408 io_status
->u
.Status
= STATUS_PENDING
;
409 io_status
->Information
= 0;
411 status
= NtReadFileScatter( file
, NULL
, NULL
, NULL
, io_status
, segments
, count
, &offset
, NULL
);
412 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
417 /***********************************************************************
418 * ReadFile (KERNEL32.@)
420 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
421 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
423 LARGE_INTEGER offset
;
424 PLARGE_INTEGER poffset
= NULL
;
425 IO_STATUS_BLOCK iosb
;
426 PIO_STATUS_BLOCK io_status
= &iosb
;
429 LPVOID cvalue
= NULL
;
431 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToRead
,
432 bytesRead
, overlapped
);
434 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
435 if (!bytesToRead
) return TRUE
;
437 if (is_console_handle(hFile
))
438 return ReadConsoleA(hFile
, buffer
, bytesToRead
, bytesRead
, NULL
);
440 if (overlapped
!= NULL
)
442 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
443 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
445 hEvent
= overlapped
->hEvent
;
446 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
447 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
449 io_status
->u
.Status
= STATUS_PENDING
;
450 io_status
->Information
= 0;
452 status
= NtReadFile(hFile
, hEvent
, NULL
, cvalue
, io_status
, buffer
, bytesToRead
, poffset
, NULL
);
454 if (status
== STATUS_PENDING
&& !overlapped
)
456 WaitForSingleObject( hFile
, INFINITE
);
457 status
= io_status
->u
.Status
;
460 if (status
!= STATUS_PENDING
&& bytesRead
)
461 *bytesRead
= io_status
->Information
;
463 if (status
&& status
!= STATUS_END_OF_FILE
&& status
!= STATUS_TIMEOUT
)
465 SetLastError( RtlNtStatusToDosError(status
) );
472 /***********************************************************************
473 * WriteFileEx (KERNEL32.@)
475 BOOL WINAPI
WriteFileEx(HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
476 LPOVERLAPPED overlapped
,
477 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
479 LARGE_INTEGER offset
;
481 PIO_STATUS_BLOCK io_status
;
483 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, overlapped
, lpCompletionRoutine
);
485 if (overlapped
== NULL
)
487 SetLastError(ERROR_INVALID_PARAMETER
);
490 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
491 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
493 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
494 io_status
->u
.Status
= STATUS_PENDING
;
495 io_status
->Information
= 0;
497 status
= NtWriteFile(hFile
, NULL
, FILE_ReadWriteApc
, lpCompletionRoutine
,
498 io_status
, buffer
, bytesToWrite
, &offset
, NULL
);
500 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
505 /***********************************************************************
506 * WriteFileGather (KERNEL32.@)
508 BOOL WINAPI
WriteFileGather( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
509 LPDWORD reserved
, LPOVERLAPPED overlapped
)
511 PIO_STATUS_BLOCK io_status
;
512 LARGE_INTEGER offset
;
515 TRACE( "%p %p %u %p\n", file
, segments
, count
, overlapped
);
517 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
518 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
519 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
520 io_status
->u
.Status
= STATUS_PENDING
;
521 io_status
->Information
= 0;
523 status
= NtWriteFileGather( file
, NULL
, NULL
, NULL
, io_status
, segments
, count
, &offset
, NULL
);
524 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
529 /***********************************************************************
530 * WriteFile (KERNEL32.@)
532 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
533 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
535 HANDLE hEvent
= NULL
;
536 LARGE_INTEGER offset
;
537 PLARGE_INTEGER poffset
= NULL
;
539 IO_STATUS_BLOCK iosb
;
540 PIO_STATUS_BLOCK piosb
= &iosb
;
541 LPVOID cvalue
= NULL
;
543 TRACE("%p %p %d %p %p\n", hFile
, buffer
, bytesToWrite
, bytesWritten
, overlapped
);
545 if (is_console_handle(hFile
))
546 return WriteConsoleA(hFile
, buffer
, bytesToWrite
, bytesWritten
, NULL
);
550 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
551 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
553 hEvent
= overlapped
->hEvent
;
554 piosb
= (PIO_STATUS_BLOCK
)overlapped
;
555 if (((ULONG_PTR
)hEvent
& 1) == 0) cvalue
= overlapped
;
557 piosb
->u
.Status
= STATUS_PENDING
;
558 piosb
->Information
= 0;
560 status
= NtWriteFile(hFile
, hEvent
, NULL
, cvalue
, piosb
,
561 buffer
, bytesToWrite
, poffset
, NULL
);
563 if (status
== STATUS_PENDING
&& !overlapped
)
565 WaitForSingleObject( hFile
, INFINITE
);
566 status
= piosb
->u
.Status
;
569 if (status
!= STATUS_PENDING
&& bytesWritten
)
570 *bytesWritten
= piosb
->Information
;
572 if (status
&& status
!= STATUS_TIMEOUT
)
574 SetLastError( RtlNtStatusToDosError(status
) );
581 /***********************************************************************
582 * GetOverlappedResult (KERNEL32.@)
584 * Check the result of an Asynchronous data transfer from a file.
587 * HANDLE hFile [in] handle of file to check on
588 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
589 * LPDWORD lpTransferred [in/out] number of bytes transferred
590 * BOOL bWait [in] wait for the transfer to complete ?
596 * If successful (and relevant) lpTransferred will hold the number of
597 * bytes transferred during the async operation.
599 BOOL WINAPI
GetOverlappedResult(HANDLE hFile
, LPOVERLAPPED lpOverlapped
,
600 LPDWORD lpTransferred
, BOOL bWait
)
604 TRACE( "(%p %p %p %x)\n", hFile
, lpOverlapped
, lpTransferred
, bWait
);
606 status
= lpOverlapped
->Internal
;
607 if (status
== STATUS_PENDING
)
611 SetLastError( ERROR_IO_INCOMPLETE
);
615 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: hFile
,
616 INFINITE
) == WAIT_FAILED
)
618 status
= lpOverlapped
->Internal
;
621 *lpTransferred
= lpOverlapped
->InternalHigh
;
623 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
627 /***********************************************************************
628 * CancelIo (KERNEL32.@)
630 * Cancels pending I/O operations initiated by the current thread on a file.
633 * handle [I] File handle.
637 * Failure: FALSE, check GetLastError().
639 BOOL WINAPI
CancelIo(HANDLE handle
)
641 IO_STATUS_BLOCK io_status
;
643 NtCancelIoFile(handle
, &io_status
);
644 if (io_status
.u
.Status
)
646 SetLastError( RtlNtStatusToDosError( io_status
.u
.Status
) );
652 /***********************************************************************
653 * _hread (KERNEL32.@)
655 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
657 return _lread( hFile
, buffer
, count
);
661 /***********************************************************************
662 * _hwrite (KERNEL32.@)
664 * experimentation yields that _lwrite:
665 * o truncates the file at the current position with
667 * o returns 0 on a 0 length write
668 * o works with console handles
671 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
675 TRACE("%d %p %d\n", handle
, buffer
, count
);
679 /* Expand or truncate at current position */
680 if (!SetEndOfFile( LongToHandle(handle
) )) return HFILE_ERROR
;
683 if (!WriteFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
689 /***********************************************************************
690 * _lclose (KERNEL32.@)
692 HFILE WINAPI
_lclose( HFILE hFile
)
694 TRACE("handle %d\n", hFile
);
695 return CloseHandle( LongToHandle(hFile
) ) ? 0 : HFILE_ERROR
;
699 /***********************************************************************
700 * _lcreat (KERNEL32.@)
702 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
706 /* Mask off all flags not explicitly allowed by the doc */
707 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
708 TRACE("%s %02x\n", path
, attr
);
709 hfile
= CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
710 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
711 CREATE_ALWAYS
, attr
, 0 );
712 return HandleToLong(hfile
);
716 /***********************************************************************
717 * _lopen (KERNEL32.@)
719 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
723 TRACE("(%s,%04x)\n", debugstr_a(path
), mode
);
724 hfile
= create_file_OF( path
, mode
& ~OF_CREATE
);
725 return HandleToLong(hfile
);
728 /***********************************************************************
729 * _lread (KERNEL32.@)
731 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
734 if (!ReadFile( LongToHandle(handle
), buffer
, count
, &result
, NULL
))
740 /***********************************************************************
741 * _llseek (KERNEL32.@)
743 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
745 return SetFilePointer( LongToHandle(hFile
), lOffset
, NULL
, nOrigin
);
749 /***********************************************************************
750 * _lwrite (KERNEL32.@)
752 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
754 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
758 /***********************************************************************
759 * FlushFileBuffers (KERNEL32.@)
761 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
764 IO_STATUS_BLOCK ioblk
;
766 if (is_console_handle( hFile
))
768 /* this will fail (as expected) for an output handle */
769 return FlushConsoleInputBuffer( hFile
);
771 nts
= NtFlushBuffersFile( hFile
, &ioblk
);
772 if (nts
!= STATUS_SUCCESS
)
774 SetLastError( RtlNtStatusToDosError( nts
) );
782 /***********************************************************************
783 * GetFileType (KERNEL32.@)
785 DWORD WINAPI
GetFileType( HANDLE hFile
)
787 FILE_FS_DEVICE_INFORMATION info
;
791 if (is_console_handle( hFile
)) return FILE_TYPE_CHAR
;
793 status
= NtQueryVolumeInformationFile( hFile
, &io
, &info
, sizeof(info
), FileFsDeviceInformation
);
794 if (status
!= STATUS_SUCCESS
)
796 SetLastError( RtlNtStatusToDosError(status
) );
797 return FILE_TYPE_UNKNOWN
;
800 switch(info
.DeviceType
)
802 case FILE_DEVICE_NULL
:
803 case FILE_DEVICE_SERIAL_PORT
:
804 case FILE_DEVICE_PARALLEL_PORT
:
805 case FILE_DEVICE_TAPE
:
806 case FILE_DEVICE_UNKNOWN
:
807 return FILE_TYPE_CHAR
;
808 case FILE_DEVICE_NAMED_PIPE
:
809 return FILE_TYPE_PIPE
;
811 return FILE_TYPE_DISK
;
816 /***********************************************************************
817 * GetFileInformationByHandle (KERNEL32.@)
819 BOOL WINAPI
GetFileInformationByHandle( HANDLE hFile
, BY_HANDLE_FILE_INFORMATION
*info
)
821 FILE_ALL_INFORMATION all_info
;
825 status
= NtQueryInformationFile( hFile
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
826 if (status
== STATUS_SUCCESS
)
828 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
829 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
830 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
831 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
832 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
833 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
834 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
835 info
->dwVolumeSerialNumber
= 0; /* FIXME */
836 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
837 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
838 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
839 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
840 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
843 SetLastError( RtlNtStatusToDosError(status
) );
848 /***********************************************************************
849 * GetFileSize (KERNEL32.@)
851 * Retrieve the size of a file.
854 * hFile [I] File to retrieve size of.
855 * filesizehigh [O] On return, the high bits of the file size.
858 * Success: The low bits of the file size.
859 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
860 * check GetLastError() for values other than ERROR_SUCCESS.
862 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
865 if (!GetFileSizeEx( hFile
, &size
)) return INVALID_FILE_SIZE
;
866 if (filesizehigh
) *filesizehigh
= size
.u
.HighPart
;
867 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError(0);
868 return size
.u
.LowPart
;
872 /***********************************************************************
873 * GetFileSizeEx (KERNEL32.@)
875 * Retrieve the size of a file.
878 * hFile [I] File to retrieve size of.
879 * lpFileSIze [O] On return, the size of the file.
883 * Failure: FALSE, check GetLastError().
885 BOOL WINAPI
GetFileSizeEx( HANDLE hFile
, PLARGE_INTEGER lpFileSize
)
887 FILE_STANDARD_INFORMATION info
;
891 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileStandardInformation
);
892 if (status
== STATUS_SUCCESS
)
894 *lpFileSize
= info
.EndOfFile
;
897 SetLastError( RtlNtStatusToDosError(status
) );
902 /**************************************************************************
903 * SetEndOfFile (KERNEL32.@)
905 * Sets the current position as the end of the file.
908 * hFile [I] File handle.
912 * Failure: FALSE, check GetLastError().
914 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
916 FILE_POSITION_INFORMATION pos
;
917 FILE_END_OF_FILE_INFORMATION eof
;
921 status
= NtQueryInformationFile( hFile
, &io
, &pos
, sizeof(pos
), FilePositionInformation
);
922 if (status
== STATUS_SUCCESS
)
924 eof
.EndOfFile
= pos
.CurrentByteOffset
;
925 status
= NtSetInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
927 if (status
== STATUS_SUCCESS
) return TRUE
;
928 SetLastError( RtlNtStatusToDosError(status
) );
933 /***********************************************************************
934 * SetFilePointer (KERNEL32.@)
936 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
, DWORD method
)
938 LARGE_INTEGER dist
, newpos
;
942 dist
.u
.LowPart
= distance
;
943 dist
.u
.HighPart
= *highword
;
945 else dist
.QuadPart
= distance
;
947 if (!SetFilePointerEx( hFile
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
949 if (highword
) *highword
= newpos
.u
.HighPart
;
950 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
951 return newpos
.u
.LowPart
;
955 /***********************************************************************
956 * SetFilePointerEx (KERNEL32.@)
958 BOOL WINAPI
SetFilePointerEx( HANDLE hFile
, LARGE_INTEGER distance
,
959 LARGE_INTEGER
*newpos
, DWORD method
)
963 FILE_POSITION_INFORMATION info
;
968 pos
= distance
.QuadPart
;
971 if (NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
973 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
977 FILE_END_OF_FILE_INFORMATION eof
;
978 if (NtQueryInformationFile( hFile
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
980 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
984 SetLastError( ERROR_INVALID_PARAMETER
);
990 SetLastError( ERROR_NEGATIVE_SEEK
);
994 info
.CurrentByteOffset
.QuadPart
= pos
;
995 if (NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FilePositionInformation
))
997 if (newpos
) newpos
->QuadPart
= pos
;
1001 SetLastError( RtlNtStatusToDosError(io
.u
.Status
) );
1005 /***********************************************************************
1006 * GetFileTime (KERNEL32.@)
1008 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
1009 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
1011 FILE_BASIC_INFORMATION info
;
1015 status
= NtQueryInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1016 if (status
== STATUS_SUCCESS
)
1020 lpCreationTime
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1021 lpCreationTime
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1023 if (lpLastAccessTime
)
1025 lpLastAccessTime
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1026 lpLastAccessTime
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1028 if (lpLastWriteTime
)
1030 lpLastWriteTime
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1031 lpLastWriteTime
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1035 SetLastError( RtlNtStatusToDosError(status
) );
1040 /***********************************************************************
1041 * SetFileTime (KERNEL32.@)
1043 BOOL WINAPI
SetFileTime( HANDLE hFile
, const FILETIME
*ctime
,
1044 const FILETIME
*atime
, const FILETIME
*mtime
)
1046 FILE_BASIC_INFORMATION info
;
1050 memset( &info
, 0, sizeof(info
) );
1053 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
1054 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
1058 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
1059 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
1063 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
1064 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
1067 status
= NtSetInformationFile( hFile
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1068 if (status
== STATUS_SUCCESS
) return TRUE
;
1069 SetLastError( RtlNtStatusToDosError(status
) );
1074 /**************************************************************************
1075 * LockFile (KERNEL32.@)
1077 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1078 DWORD count_low
, DWORD count_high
)
1081 LARGE_INTEGER count
, offset
;
1083 TRACE( "%p %x%08x %x%08x\n",
1084 hFile
, offset_high
, offset_low
, count_high
, count_low
);
1086 count
.u
.LowPart
= count_low
;
1087 count
.u
.HighPart
= count_high
;
1088 offset
.u
.LowPart
= offset_low
;
1089 offset
.u
.HighPart
= offset_high
;
1091 status
= NtLockFile( hFile
, 0, NULL
, NULL
,
1092 NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
);
1094 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1099 /**************************************************************************
1100 * LockFileEx [KERNEL32.@]
1102 * Locks a byte range within an open file for shared or exclusive access.
1109 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1111 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1112 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
1115 LARGE_INTEGER count
, offset
;
1116 LPVOID cvalue
= NULL
;
1120 SetLastError( ERROR_INVALID_PARAMETER
);
1124 TRACE( "%p %x%08x %x%08x flags %x\n",
1125 hFile
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
,
1126 count_high
, count_low
, flags
);
1128 count
.u
.LowPart
= count_low
;
1129 count
.u
.HighPart
= count_high
;
1130 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
1131 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
1133 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1135 status
= NtLockFile( hFile
, overlapped
->hEvent
, NULL
, cvalue
,
1136 NULL
, &offset
, &count
, NULL
,
1137 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
1138 flags
& LOCKFILE_EXCLUSIVE_LOCK
);
1140 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1145 /**************************************************************************
1146 * UnlockFile (KERNEL32.@)
1148 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD offset_low
, DWORD offset_high
,
1149 DWORD count_low
, DWORD count_high
)
1152 LARGE_INTEGER count
, offset
;
1154 count
.u
.LowPart
= count_low
;
1155 count
.u
.HighPart
= count_high
;
1156 offset
.u
.LowPart
= offset_low
;
1157 offset
.u
.HighPart
= offset_high
;
1159 status
= NtUnlockFile( hFile
, NULL
, &offset
, &count
, NULL
);
1160 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1165 /**************************************************************************
1166 * UnlockFileEx (KERNEL32.@)
1168 BOOL WINAPI
UnlockFileEx( HANDLE hFile
, DWORD reserved
, DWORD count_low
, DWORD count_high
,
1169 LPOVERLAPPED overlapped
)
1173 SetLastError( ERROR_INVALID_PARAMETER
);
1176 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
1178 return UnlockFile( hFile
, overlapped
->u
.s
.Offset
, overlapped
->u
.s
.OffsetHigh
, count_low
, count_high
);
1182 /***********************************************************************
1183 * Win32HandleToDosFileHandle (KERNEL32.21)
1185 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1186 * longer valid after this function (even on failure).
1188 * Note: this is not exactly right, since on Win95 the Win32 handles
1189 * are on top of DOS handles and we do it the other way
1190 * around. Should be good enough though.
1192 HFILE WINAPI
Win32HandleToDosFileHandle( HANDLE handle
)
1196 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
1199 FILE_InitProcessDosHandles();
1200 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++)
1201 if (!dos_handles
[i
])
1203 dos_handles
[i
] = handle
;
1204 TRACE("Got %d for h32 %p\n", i
, handle
);
1207 CloseHandle( handle
);
1208 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
1213 /***********************************************************************
1214 * DosFileHandleToWin32Handle (KERNEL32.20)
1216 * Return the Win32 handle for a DOS handle.
1218 * Note: this is not exactly right, since on Win95 the Win32 handles
1219 * are on top of DOS handles and we do it the other way
1220 * around. Should be good enough though.
1222 HANDLE WINAPI
DosFileHandleToWin32Handle( HFILE handle
)
1224 HFILE16 hfile
= (HFILE16
)handle
;
1225 if (hfile
< 5) FILE_InitProcessDosHandles();
1226 if ((hfile
>= DOS_TABLE_SIZE
) || !dos_handles
[hfile
])
1228 SetLastError( ERROR_INVALID_HANDLE
);
1229 return INVALID_HANDLE_VALUE
;
1231 return dos_handles
[hfile
];
1235 /*************************************************************************
1236 * SetHandleCount (KERNEL32.@)
1238 UINT WINAPI
SetHandleCount( UINT count
)
1240 return min( 256, count
);
1244 /***********************************************************************
1245 * DisposeLZ32Handle (KERNEL32.22)
1247 * Note: this is not entirely correct, we should only close the
1248 * 32-bit handle and not the 16-bit one, but we cannot do
1249 * this because of the way our DOS handles are implemented.
1250 * It shouldn't break anything though.
1252 void WINAPI
DisposeLZ32Handle( HANDLE handle
)
1256 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
)) return;
1258 for (i
= 5; i
< DOS_TABLE_SIZE
; i
++)
1259 if (dos_handles
[i
] == handle
)
1262 CloseHandle( handle
);
1267 /**************************************************************************
1268 * Operations on file names *
1269 **************************************************************************/
1272 /*************************************************************************
1273 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1275 * Creates or opens an object, and returns a handle that can be used to
1276 * access that object.
1280 * filename [in] pointer to filename to be accessed
1281 * access [in] access mode requested
1282 * sharing [in] share mode
1283 * sa [in] pointer to security attributes
1284 * creation [in] how to create the file
1285 * attributes [in] attributes for newly created file
1286 * template [in] handle to file with extended attributes to copy
1289 * Success: Open handle to specified file
1290 * Failure: INVALID_HANDLE_VALUE
1292 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
1293 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1294 DWORD attributes
, HANDLE
template )
1298 OBJECT_ATTRIBUTES attr
;
1299 UNICODE_STRING nameW
;
1303 const WCHAR
*vxd_name
= NULL
;
1304 static const WCHAR bkslashes_with_dotW
[] = {'\\','\\','.','\\',0};
1305 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
1306 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
1307 SECURITY_QUALITY_OF_SERVICE qos
;
1309 static const UINT nt_disposition
[5] =
1311 FILE_CREATE
, /* CREATE_NEW */
1312 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
1313 FILE_OPEN
, /* OPEN_EXISTING */
1314 FILE_OPEN_IF
, /* OPEN_ALWAYS */
1315 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
1321 if (!filename
|| !filename
[0])
1323 SetLastError( ERROR_PATH_NOT_FOUND
);
1324 return INVALID_HANDLE_VALUE
;
1327 TRACE("%s %s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
1328 (access
& GENERIC_READ
)?"GENERIC_READ ":"",
1329 (access
& GENERIC_WRITE
)?"GENERIC_WRITE ":"",
1330 (!access
)?"QUERY_ACCESS ":"",
1331 (sharing
& FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
1332 (sharing
& FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
1333 (sharing
& FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
1334 creation
, attributes
);
1336 /* Open a console for CONIN$ or CONOUT$ */
1338 if (!strcmpiW(filename
, coninW
) || !strcmpiW(filename
, conoutW
))
1340 ret
= OpenConsoleW(filename
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1344 if (!strncmpW(filename
, bkslashes_with_dotW
, 4))
1346 static const WCHAR pipeW
[] = {'P','I','P','E','\\',0};
1347 static const WCHAR mailslotW
[] = {'M','A','I','L','S','L','O','T','\\',0};
1349 if ((isalphaW(filename
[4]) && filename
[5] == ':' && filename
[6] == '\0') ||
1350 !strncmpiW( filename
+ 4, pipeW
, 5 ) ||
1351 !strncmpiW( filename
+ 4, mailslotW
, 9 ))
1355 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
1357 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
1359 else if (GetVersion() & 0x80000000)
1361 vxd_name
= filename
+ 4;
1364 else dosdev
= RtlIsDosDeviceName_U( filename
);
1368 static const WCHAR conW
[] = {'C','O','N'};
1370 if (LOWORD(dosdev
) == sizeof(conW
) &&
1371 !memicmpW( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), conW
, sizeof(conW
)/sizeof(WCHAR
)))
1373 switch (access
& (GENERIC_READ
|GENERIC_WRITE
))
1376 ret
= OpenConsoleW(coninW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1379 ret
= OpenConsoleW(conoutW
, access
, (sa
&& sa
->bInheritHandle
), creation
);
1382 SetLastError( ERROR_FILE_NOT_FOUND
);
1383 return INVALID_HANDLE_VALUE
;
1388 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
1390 SetLastError( ERROR_INVALID_PARAMETER
);
1391 return INVALID_HANDLE_VALUE
;
1394 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
1396 SetLastError( ERROR_PATH_NOT_FOUND
);
1397 return INVALID_HANDLE_VALUE
;
1400 /* now call NtCreateFile */
1403 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
1404 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
1406 options
|= FILE_NON_DIRECTORY_FILE
;
1407 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
1409 options
|= FILE_DELETE_ON_CLOSE
;
1412 if (attributes
& FILE_FLAG_NO_BUFFERING
)
1413 options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
1414 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
1415 options
|= FILE_SYNCHRONOUS_IO_ALERT
;
1416 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
1417 options
|= FILE_RANDOM_ACCESS
;
1418 attributes
&= FILE_ATTRIBUTE_VALID_FLAGS
;
1420 attr
.Length
= sizeof(attr
);
1421 attr
.RootDirectory
= 0;
1422 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1423 attr
.ObjectName
= &nameW
;
1424 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1425 if (attributes
& SECURITY_SQOS_PRESENT
)
1427 qos
.Length
= sizeof(qos
);
1428 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
1429 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
1430 qos
.EffectiveOnly
= attributes
& SECURITY_EFFECTIVE_ONLY
? TRUE
: FALSE
;
1431 attr
.SecurityQualityOfService
= &qos
;
1434 attr
.SecurityQualityOfService
= NULL
;
1436 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
1438 status
= NtCreateFile( &ret
, access
, &attr
, &io
, NULL
, attributes
,
1439 sharing
, nt_disposition
[creation
- CREATE_NEW
],
1443 if (vxd_name
&& vxd_name
[0] && (ret
= VXD_Open( vxd_name
, access
, sa
))) goto done
;
1445 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
1446 ret
= INVALID_HANDLE_VALUE
;
1448 /* In the case file creation was rejected due to CREATE_NEW flag
1449 * was specified and file with that name already exists, correct
1450 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1451 * Note: RtlNtStatusToDosError is not the subject to blame here.
1453 if (status
== STATUS_OBJECT_NAME_COLLISION
)
1454 SetLastError( ERROR_FILE_EXISTS
);
1456 SetLastError( RtlNtStatusToDosError(status
) );
1460 if ((creation
== CREATE_ALWAYS
&& io
.Information
== FILE_OVERWRITTEN
) ||
1461 (creation
== OPEN_ALWAYS
&& io
.Information
== FILE_OPENED
))
1462 SetLastError( ERROR_ALREADY_EXISTS
);
1466 RtlFreeUnicodeString( &nameW
);
1469 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
1470 TRACE("returning %p\n", ret
);
1476 /*************************************************************************
1477 * CreateFileA (KERNEL32.@)
1481 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
1482 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
1483 DWORD attributes
, HANDLE
template)
1487 if (!(nameW
= FILE_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1488 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
1492 /***********************************************************************
1493 * DeleteFileW (KERNEL32.@)
1498 * path [I] Path to the file to delete.
1502 * Failure: FALSE, check GetLastError().
1504 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1506 UNICODE_STRING nameW
;
1507 OBJECT_ATTRIBUTES attr
;
1512 TRACE("%s\n", debugstr_w(path
) );
1514 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
1516 SetLastError( ERROR_PATH_NOT_FOUND
);
1520 attr
.Length
= sizeof(attr
);
1521 attr
.RootDirectory
= 0;
1522 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1523 attr
.ObjectName
= &nameW
;
1524 attr
.SecurityDescriptor
= NULL
;
1525 attr
.SecurityQualityOfService
= NULL
;
1527 status
= NtCreateFile(&hFile
, GENERIC_READ
| GENERIC_WRITE
| DELETE
,
1528 &attr
, &io
, NULL
, 0,
1529 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1530 FILE_OPEN
, FILE_DELETE_ON_CLOSE
| FILE_NON_DIRECTORY_FILE
, NULL
, 0);
1531 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
1533 RtlFreeUnicodeString( &nameW
);
1536 SetLastError( RtlNtStatusToDosError(status
) );
1543 /***********************************************************************
1544 * DeleteFileA (KERNEL32.@)
1548 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1552 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1553 return DeleteFileW( pathW
);
1557 /**************************************************************************
1558 * ReplaceFileW (KERNEL32.@)
1559 * ReplaceFile (KERNEL32.@)
1561 BOOL WINAPI
ReplaceFileW(LPCWSTR lpReplacedFileName
, LPCWSTR lpReplacementFileName
,
1562 LPCWSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1563 LPVOID lpExclude
, LPVOID lpReserved
)
1565 UNICODE_STRING nt_replaced_name
, nt_replacement_name
;
1566 ANSI_STRING unix_replaced_name
, unix_replacement_name
, unix_backup_name
;
1567 HANDLE hReplaced
= NULL
, hReplacement
= NULL
, hBackup
= NULL
;
1568 DWORD error
= ERROR_SUCCESS
;
1569 UINT replaced_flags
;
1573 OBJECT_ATTRIBUTES attr
;
1576 FIXME("Ignoring flags %x\n", dwReplaceFlags
);
1578 /* First two arguments are mandatory */
1579 if (!lpReplacedFileName
|| !lpReplacementFileName
)
1581 SetLastError(ERROR_INVALID_PARAMETER
);
1585 unix_replaced_name
.Buffer
= NULL
;
1586 unix_replacement_name
.Buffer
= NULL
;
1587 unix_backup_name
.Buffer
= NULL
;
1589 attr
.Length
= sizeof(attr
);
1590 attr
.RootDirectory
= 0;
1591 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1592 attr
.ObjectName
= NULL
;
1593 attr
.SecurityDescriptor
= NULL
;
1594 attr
.SecurityQualityOfService
= NULL
;
1596 /* Open the "replaced" file for reading and writing */
1597 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName
, &nt_replaced_name
, NULL
, NULL
)))
1599 error
= ERROR_PATH_NOT_FOUND
;
1602 replaced_flags
= lpBackupFileName
? FILE_OPEN
: FILE_OPEN_IF
;
1603 attr
.ObjectName
= &nt_replaced_name
;
1604 status
= NtOpenFile(&hReplaced
, GENERIC_READ
|GENERIC_WRITE
|DELETE
|SYNCHRONIZE
,
1606 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
1607 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1608 if (status
== STATUS_SUCCESS
)
1609 status
= wine_nt_to_unix_file_name(&nt_replaced_name
, &unix_replaced_name
, replaced_flags
, FALSE
);
1610 RtlFreeUnicodeString(&nt_replaced_name
);
1611 if (status
!= STATUS_SUCCESS
)
1613 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1614 error
= ERROR_FILE_NOT_FOUND
;
1616 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1621 * Open the replacement file for reading, writing, and deleting
1622 * (writing and deleting are needed when finished)
1624 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName
, &nt_replacement_name
, NULL
, NULL
)))
1626 error
= ERROR_PATH_NOT_FOUND
;
1629 attr
.ObjectName
= &nt_replacement_name
;
1630 status
= NtOpenFile(&hReplacement
,
1631 GENERIC_READ
|GENERIC_WRITE
|DELETE
|WRITE_DAC
|SYNCHRONIZE
,
1633 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1634 if (status
== STATUS_SUCCESS
)
1635 status
= wine_nt_to_unix_file_name(&nt_replacement_name
, &unix_replacement_name
, FILE_OPEN
, FALSE
);
1636 RtlFreeUnicodeString(&nt_replacement_name
);
1637 if (status
!= STATUS_SUCCESS
)
1639 error
= RtlNtStatusToDosError(status
);
1643 /* If the user wants a backup then that needs to be performed first */
1644 if (lpBackupFileName
)
1646 UNICODE_STRING nt_backup_name
;
1647 FILE_BASIC_INFORMATION replaced_info
;
1649 /* Obtain the file attributes from the "replaced" file */
1650 status
= NtQueryInformationFile(hReplaced
, &io
, &replaced_info
,
1651 sizeof(replaced_info
),
1652 FileBasicInformation
);
1653 if (status
!= STATUS_SUCCESS
)
1655 error
= RtlNtStatusToDosError(status
);
1659 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName
, &nt_backup_name
, NULL
, NULL
)))
1661 error
= ERROR_PATH_NOT_FOUND
;
1664 attr
.ObjectName
= &nt_backup_name
;
1665 /* Open the backup with permissions to write over it */
1666 status
= NtCreateFile(&hBackup
, GENERIC_WRITE
,
1667 &attr
, &io
, NULL
, replaced_info
.FileAttributes
,
1668 FILE_SHARE_WRITE
, FILE_OPEN_IF
,
1669 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
,
1671 if (status
== STATUS_SUCCESS
)
1672 status
= wine_nt_to_unix_file_name(&nt_backup_name
, &unix_backup_name
, FILE_OPEN_IF
, FALSE
);
1673 if (status
!= STATUS_SUCCESS
)
1675 error
= RtlNtStatusToDosError(status
);
1679 /* If an existing backup exists then copy over it */
1680 if (rename(unix_replaced_name
.Buffer
, unix_backup_name
.Buffer
) == -1)
1682 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
; /* is this correct? */
1688 * Now that the backup has been performed (if requested), copy the replacement
1691 if (rename(unix_replacement_name
.Buffer
, unix_replaced_name
.Buffer
) == -1)
1693 if (errno
== EACCES
)
1695 /* Inappropriate permissions on "replaced", rename will fail */
1696 error
= ERROR_UNABLE_TO_REMOVE_REPLACED
;
1699 /* on failure we need to indicate whether a backup was made */
1700 if (!lpBackupFileName
)
1701 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT
;
1703 error
= ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
;
1709 /* Perform resource cleanup */
1711 if (hBackup
) CloseHandle(hBackup
);
1712 if (hReplaced
) CloseHandle(hReplaced
);
1713 if (hReplacement
) CloseHandle(hReplacement
);
1714 RtlFreeAnsiString(&unix_backup_name
);
1715 RtlFreeAnsiString(&unix_replacement_name
);
1716 RtlFreeAnsiString(&unix_replaced_name
);
1718 /* If there was an error, set the error code */
1720 SetLastError(error
);
1725 /**************************************************************************
1726 * ReplaceFileA (KERNEL32.@)
1728 BOOL WINAPI
ReplaceFileA(LPCSTR lpReplacedFileName
,LPCSTR lpReplacementFileName
,
1729 LPCSTR lpBackupFileName
, DWORD dwReplaceFlags
,
1730 LPVOID lpExclude
, LPVOID lpReserved
)
1732 WCHAR
*replacedW
, *replacementW
, *backupW
= NULL
;
1735 /* This function only makes sense when the first two parameters are defined */
1736 if (!lpReplacedFileName
|| !(replacedW
= FILE_name_AtoW( lpReplacedFileName
, TRUE
)))
1738 SetLastError(ERROR_INVALID_PARAMETER
);
1741 if (!lpReplacementFileName
|| !(replacementW
= FILE_name_AtoW( lpReplacementFileName
, TRUE
)))
1743 HeapFree( GetProcessHeap(), 0, replacedW
);
1744 SetLastError(ERROR_INVALID_PARAMETER
);
1747 /* The backup parameter, however, is optional */
1748 if (lpBackupFileName
)
1750 if (!(backupW
= FILE_name_AtoW( lpBackupFileName
, TRUE
)))
1752 HeapFree( GetProcessHeap(), 0, replacedW
);
1753 HeapFree( GetProcessHeap(), 0, replacementW
);
1754 SetLastError(ERROR_INVALID_PARAMETER
);
1758 ret
= ReplaceFileW( replacedW
, replacementW
, backupW
, dwReplaceFlags
, lpExclude
, lpReserved
);
1759 HeapFree( GetProcessHeap(), 0, replacedW
);
1760 HeapFree( GetProcessHeap(), 0, replacementW
);
1761 HeapFree( GetProcessHeap(), 0, backupW
);
1766 /*************************************************************************
1767 * FindFirstFileExW (KERNEL32.@)
1769 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1770 * results as FindExSearchNameMatch
1772 HANDLE WINAPI
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1773 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1774 LPVOID filter
, DWORD flags
)
1777 FIND_FIRST_INFO
*info
= NULL
;
1778 UNICODE_STRING nt_name
;
1779 OBJECT_ATTRIBUTES attr
;
1784 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1786 if ((search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1789 FIXME("options not implemented 0x%08x 0x%08x\n", search_op
, flags
);
1790 return INVALID_HANDLE_VALUE
;
1792 if (level
!= FindExInfoStandard
)
1794 FIXME("info level %d not implemented\n", level
);
1795 return INVALID_HANDLE_VALUE
;
1798 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1800 SetLastError( ERROR_PATH_NOT_FOUND
);
1801 return INVALID_HANDLE_VALUE
;
1804 if (!(info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
))))
1806 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1810 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1812 static const WCHAR dotW
[] = {'.',0};
1815 /* we still need to check that the directory can be opened */
1819 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1821 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1824 memcpy( dir
, filename
, HIWORD(device
) );
1825 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1827 RtlFreeUnicodeString( &nt_name
);
1828 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: dotW
, &nt_name
, &mask
, NULL
))
1830 HeapFree( GetProcessHeap(), 0, dir
);
1831 SetLastError( ERROR_PATH_NOT_FOUND
);
1834 HeapFree( GetProcessHeap(), 0, dir
);
1835 RtlInitUnicodeString( &info
->mask
, NULL
);
1837 else if (!mask
|| !*mask
)
1839 SetLastError( ERROR_FILE_NOT_FOUND
);
1844 if (!RtlCreateUnicodeString( &info
->mask
, mask
))
1846 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1850 /* truncate dir name before mask */
1852 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
1855 /* check if path is the root of the drive */
1856 info
->is_root
= FALSE
;
1857 p
= nt_name
.Buffer
+ 4; /* skip \??\ prefix */
1858 if (p
[0] && p
[1] == ':')
1861 while (*p
== '\\') p
++;
1862 info
->is_root
= (*p
== 0);
1865 attr
.Length
= sizeof(attr
);
1866 attr
.RootDirectory
= 0;
1867 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1868 attr
.ObjectName
= &nt_name
;
1869 attr
.SecurityDescriptor
= NULL
;
1870 attr
.SecurityQualityOfService
= NULL
;
1872 status
= NtOpenFile( &info
->handle
, GENERIC_READ
, &attr
, &io
,
1873 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1874 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1876 if (status
!= STATUS_SUCCESS
)
1878 RtlFreeUnicodeString( &info
->mask
);
1879 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1880 SetLastError( ERROR_PATH_NOT_FOUND
);
1882 SetLastError( RtlNtStatusToDosError(status
) );
1886 RtlInitializeCriticalSection( &info
->cs
);
1887 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
1888 info
->path
= nt_name
;
1889 info
->magic
= FIND_FIRST_MAGIC
;
1892 info
->search_op
= search_op
;
1896 WIN32_FIND_DATAW
*wfd
= data
;
1898 memset( wfd
, 0, sizeof(*wfd
) );
1899 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
1900 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1901 CloseHandle( info
->handle
);
1908 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1909 FileBothDirectoryInformation
, FALSE
, &info
->mask
, TRUE
);
1913 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1914 return INVALID_HANDLE_VALUE
;
1916 info
->data_len
= io
.Information
;
1917 if (!FindNextFileW( info
, data
))
1919 TRACE( "%s not found\n", debugstr_w(filename
) );
1921 SetLastError( ERROR_FILE_NOT_FOUND
);
1922 return INVALID_HANDLE_VALUE
;
1924 if (!strpbrkW( info
->mask
.Buffer
, wildcardsW
))
1926 /* we can't find two files with the same name */
1927 CloseHandle( info
->handle
);
1934 HeapFree( GetProcessHeap(), 0, info
);
1935 RtlFreeUnicodeString( &nt_name
);
1936 return INVALID_HANDLE_VALUE
;
1940 /*************************************************************************
1941 * FindNextFileW (KERNEL32.@)
1943 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1945 FIND_FIRST_INFO
*info
;
1946 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1949 TRACE("%p %p\n", handle
, data
);
1951 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1953 SetLastError( ERROR_INVALID_HANDLE
);
1956 info
= (FIND_FIRST_INFO
*)handle
;
1957 if (info
->magic
!= FIND_FIRST_MAGIC
)
1959 SetLastError( ERROR_INVALID_HANDLE
);
1963 RtlEnterCriticalSection( &info
->cs
);
1965 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
1968 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
1972 NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, sizeof(info
->data
),
1973 FileBothDirectoryInformation
, FALSE
, &info
->mask
, FALSE
);
1976 SetLastError( RtlNtStatusToDosError( io
.u
.Status
) );
1977 if (io
.u
.Status
== STATUS_NO_MORE_FILES
)
1979 CloseHandle( info
->handle
);
1984 info
->data_len
= io
.Information
;
1988 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
1990 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
1991 else info
->data_pos
= info
->data_len
;
1993 /* don't return '.' and '..' in the root of the drive */
1996 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
1997 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
1998 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
2001 /* check for dir symlink */
2002 if ((dir_info
->FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
2003 (dir_info
->FileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
) &&
2004 strpbrkW( info
->mask
.Buffer
, wildcardsW
))
2006 if (!check_dir_symlink( info
, dir_info
)) continue;
2009 data
->dwFileAttributes
= dir_info
->FileAttributes
;
2010 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
2011 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
2012 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
2013 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
2014 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
2015 data
->dwReserved0
= 0;
2016 data
->dwReserved1
= 0;
2018 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
2019 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
2020 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
2021 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
2023 TRACE("returning %s (%s)\n",
2024 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
2030 RtlLeaveCriticalSection( &info
->cs
);
2035 /*************************************************************************
2036 * FindClose (KERNEL32.@)
2038 BOOL WINAPI
FindClose( HANDLE handle
)
2040 FIND_FIRST_INFO
*info
= (FIND_FIRST_INFO
*)handle
;
2042 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
2044 SetLastError( ERROR_INVALID_HANDLE
);
2050 if (info
->magic
== FIND_FIRST_MAGIC
)
2052 RtlEnterCriticalSection( &info
->cs
);
2053 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
2056 if (info
->handle
) CloseHandle( info
->handle
);
2058 RtlFreeUnicodeString( &info
->mask
);
2059 info
->mask
.Buffer
= NULL
;
2060 RtlFreeUnicodeString( &info
->path
);
2063 RtlLeaveCriticalSection( &info
->cs
);
2064 info
->cs
.DebugInfo
->Spare
[0] = 0;
2065 RtlDeleteCriticalSection( &info
->cs
);
2066 HeapFree( GetProcessHeap(), 0, info
);
2072 WARN("Illegal handle %p\n", handle
);
2073 SetLastError( ERROR_INVALID_HANDLE
);
2082 /*************************************************************************
2083 * FindFirstFileA (KERNEL32.@)
2085 HANDLE WINAPI
FindFirstFileA( LPCSTR lpFileName
, WIN32_FIND_DATAA
*lpFindData
)
2087 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
2088 FindExSearchNameMatch
, NULL
, 0);
2091 /*************************************************************************
2092 * FindFirstFileExA (KERNEL32.@)
2094 HANDLE WINAPI
FindFirstFileExA( LPCSTR lpFileName
, FINDEX_INFO_LEVELS fInfoLevelId
,
2095 LPVOID lpFindFileData
, FINDEX_SEARCH_OPS fSearchOp
,
2096 LPVOID lpSearchFilter
, DWORD dwAdditionalFlags
)
2099 WIN32_FIND_DATAA
*dataA
;
2100 WIN32_FIND_DATAW dataW
;
2103 if (!(nameW
= FILE_name_AtoW( lpFileName
, FALSE
))) return INVALID_HANDLE_VALUE
;
2105 handle
= FindFirstFileExW(nameW
, fInfoLevelId
, &dataW
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
2106 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
2108 dataA
= (WIN32_FIND_DATAA
*) lpFindFileData
;
2109 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
2110 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
2111 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2112 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2113 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2114 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
2115 FILE_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
2116 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
2117 sizeof(dataA
->cAlternateFileName
) );
2122 /*************************************************************************
2123 * FindFirstFileW (KERNEL32.@)
2125 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
2127 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
2128 FindExSearchNameMatch
, NULL
, 0);
2132 /*************************************************************************
2133 * FindNextFileA (KERNEL32.@)
2135 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
2137 WIN32_FIND_DATAW dataW
;
2139 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
2140 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
2141 data
->ftCreationTime
= dataW
.ftCreationTime
;
2142 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
2143 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
2144 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
2145 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
2146 FILE_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
2147 FILE_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
2148 sizeof(data
->cAlternateFileName
) );
2153 /**************************************************************************
2154 * GetFileAttributesW (KERNEL32.@)
2156 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
2158 FILE_BASIC_INFORMATION info
;
2159 UNICODE_STRING nt_name
;
2160 OBJECT_ATTRIBUTES attr
;
2163 TRACE("%s\n", debugstr_w(name
));
2165 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2167 SetLastError( ERROR_PATH_NOT_FOUND
);
2168 return INVALID_FILE_ATTRIBUTES
;
2171 attr
.Length
= sizeof(attr
);
2172 attr
.RootDirectory
= 0;
2173 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2174 attr
.ObjectName
= &nt_name
;
2175 attr
.SecurityDescriptor
= NULL
;
2176 attr
.SecurityQualityOfService
= NULL
;
2178 status
= NtQueryAttributesFile( &attr
, &info
);
2179 RtlFreeUnicodeString( &nt_name
);
2181 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
2183 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2184 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
2186 SetLastError( RtlNtStatusToDosError(status
) );
2187 return INVALID_FILE_ATTRIBUTES
;
2191 /**************************************************************************
2192 * GetFileAttributesA (KERNEL32.@)
2194 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
2198 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
2199 return GetFileAttributesW( nameW
);
2203 /**************************************************************************
2204 * SetFileAttributesW (KERNEL32.@)
2206 BOOL WINAPI
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
2208 UNICODE_STRING nt_name
;
2209 OBJECT_ATTRIBUTES attr
;
2214 TRACE("%s %x\n", debugstr_w(name
), attributes
);
2216 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2218 SetLastError( ERROR_PATH_NOT_FOUND
);
2222 attr
.Length
= sizeof(attr
);
2223 attr
.RootDirectory
= 0;
2224 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2225 attr
.ObjectName
= &nt_name
;
2226 attr
.SecurityDescriptor
= NULL
;
2227 attr
.SecurityQualityOfService
= NULL
;
2229 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2230 RtlFreeUnicodeString( &nt_name
);
2232 if (status
== STATUS_SUCCESS
)
2234 FILE_BASIC_INFORMATION info
;
2236 memset( &info
, 0, sizeof(info
) );
2237 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2238 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2242 if (status
== STATUS_SUCCESS
) return TRUE
;
2243 SetLastError( RtlNtStatusToDosError(status
) );
2248 /**************************************************************************
2249 * SetFileAttributesA (KERNEL32.@)
2251 BOOL WINAPI
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2255 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2256 return SetFileAttributesW( nameW
, attributes
);
2260 /**************************************************************************
2261 * GetFileAttributesExW (KERNEL32.@)
2263 BOOL WINAPI
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2265 FILE_NETWORK_OPEN_INFORMATION info
;
2266 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
2267 UNICODE_STRING nt_name
;
2268 OBJECT_ATTRIBUTES attr
;
2271 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
2273 if (level
!= GetFileExInfoStandard
)
2275 SetLastError( ERROR_INVALID_PARAMETER
);
2279 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2281 SetLastError( ERROR_PATH_NOT_FOUND
);
2285 attr
.Length
= sizeof(attr
);
2286 attr
.RootDirectory
= 0;
2287 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2288 attr
.ObjectName
= &nt_name
;
2289 attr
.SecurityDescriptor
= NULL
;
2290 attr
.SecurityQualityOfService
= NULL
;
2292 status
= NtQueryFullAttributesFile( &attr
, &info
);
2293 RtlFreeUnicodeString( &nt_name
);
2295 if (status
!= STATUS_SUCCESS
)
2297 SetLastError( RtlNtStatusToDosError(status
) );
2301 data
->dwFileAttributes
= info
.FileAttributes
;
2302 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
2303 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
2304 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
2305 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
2306 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
2307 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
2308 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
2309 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
2314 /**************************************************************************
2315 * GetFileAttributesExA (KERNEL32.@)
2317 BOOL WINAPI
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, LPVOID ptr
)
2321 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2322 return GetFileAttributesExW( nameW
, level
, ptr
);
2326 /******************************************************************************
2327 * GetCompressedFileSizeW (KERNEL32.@)
2329 * Get the actual number of bytes used on disk.
2332 * Success: Low-order doubleword of number of bytes
2333 * Failure: INVALID_FILE_SIZE
2335 DWORD WINAPI
GetCompressedFileSizeW(
2336 LPCWSTR name
, /* [in] Pointer to name of file */
2337 LPDWORD size_high
) /* [out] Receives high-order doubleword of size */
2339 UNICODE_STRING nt_name
;
2340 OBJECT_ATTRIBUTES attr
;
2344 DWORD ret
= INVALID_FILE_SIZE
;
2346 TRACE("%s %p\n", debugstr_w(name
), size_high
);
2348 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2350 SetLastError( ERROR_PATH_NOT_FOUND
);
2351 return INVALID_FILE_SIZE
;
2354 attr
.Length
= sizeof(attr
);
2355 attr
.RootDirectory
= 0;
2356 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2357 attr
.ObjectName
= &nt_name
;
2358 attr
.SecurityDescriptor
= NULL
;
2359 attr
.SecurityQualityOfService
= NULL
;
2361 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2362 RtlFreeUnicodeString( &nt_name
);
2364 if (status
== STATUS_SUCCESS
)
2366 /* we don't support compressed files, simply return the file size */
2367 ret
= GetFileSize( handle
, size_high
);
2370 else SetLastError( RtlNtStatusToDosError(status
) );
2376 /******************************************************************************
2377 * GetCompressedFileSizeA (KERNEL32.@)
2379 * See GetCompressedFileSizeW.
2381 DWORD WINAPI
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
2385 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
2386 return GetCompressedFileSizeW( nameW
, size_high
);
2390 /***********************************************************************
2391 * OpenFile (KERNEL32.@)
2393 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
2397 WORD filedatetime
[2];
2399 if (!ofs
) return HFILE_ERROR
;
2401 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
2402 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
2403 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
2404 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
2405 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
2406 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
2407 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
2408 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
2409 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
2410 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
2411 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
2412 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
2413 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
2414 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
2415 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
2416 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
2417 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
2418 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
2422 ofs
->cBytes
= sizeof(OFSTRUCT
);
2424 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
2426 if (!name
) return HFILE_ERROR
;
2428 TRACE("%s %04x\n", name
, mode
);
2430 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2431 Are there any cases where getting the path here is wrong?
2432 Uwe Bonnes 1997 Apr 2 */
2433 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
)) goto error
;
2435 /* OF_PARSE simply fills the structure */
2437 if (mode
& OF_PARSE
)
2439 ofs
->fFixedDisk
= (GetDriveTypeA( ofs
->szPathName
) != DRIVE_REMOVABLE
);
2440 TRACE("(%s): OF_PARSE, res = '%s'\n", name
, ofs
->szPathName
);
2444 /* OF_CREATE is completely different from all other options, so
2447 if (mode
& OF_CREATE
)
2449 if ((handle
= create_file_OF( name
, mode
)) == INVALID_HANDLE_VALUE
)
2454 /* Now look for the file */
2456 if (!SearchPathA( NULL
, name
, NULL
, sizeof(ofs
->szPathName
), ofs
->szPathName
, NULL
))
2459 TRACE("found %s\n", debugstr_a(ofs
->szPathName
) );
2461 if (mode
& OF_DELETE
)
2463 if (!DeleteFileA( ofs
->szPathName
)) goto error
;
2464 TRACE("(%s): OF_DELETE return = OK\n", name
);
2468 handle
= LongToHandle(_lopen( ofs
->szPathName
, mode
));
2469 if (handle
== INVALID_HANDLE_VALUE
) goto error
;
2471 GetFileTime( handle
, NULL
, NULL
, &filetime
);
2472 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
2473 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
2475 if (ofs
->Reserved1
!= filedatetime
[0] || ofs
->Reserved2
!= filedatetime
[1] )
2477 CloseHandle( handle
);
2478 WARN("(%s): OF_VERIFY failed\n", name
);
2479 /* FIXME: what error here? */
2480 SetLastError( ERROR_FILE_NOT_FOUND
);
2484 ofs
->Reserved1
= filedatetime
[0];
2485 ofs
->Reserved2
= filedatetime
[1];
2487 TRACE("(%s): OK, return = %p\n", name
, handle
);
2488 if (mode
& OF_EXIST
) /* Return TRUE instead of a handle */
2490 CloseHandle( handle
);
2493 return HandleToLong(handle
);
2495 error
: /* We get here if there was an error opening the file */
2496 ofs
->nErrCode
= GetLastError();
2497 WARN("(%s): return = HFILE_ERROR error= %d\n", name
,ofs
->nErrCode
);