2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 2008 Jeff Zaroyko
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
37 #define WIN32_NO_STATUS
43 #include "ddk/ntddk.h"
44 #include "kernel_private.h"
46 #include "wine/exception.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(file);
52 /* info structure for FindFirstFile handle */
55 DWORD magic; /* magic number */
56 HANDLE handle; /* handle to directory */
57 CRITICAL_SECTION cs; /* crit section protecting this structure */
58 FINDEX_SEARCH_OPS search_op; /* Flags passed to FindFirst. */
59 UNICODE_STRING mask; /* file mask */
60 UNICODE_STRING path; /* NT path used to open the directory */
61 BOOL is_root; /* is directory the root of the drive? */
62 UINT data_pos; /* current position in dir data */
63 UINT data_len; /* length of dir data */
64 UINT data_size; /* size of data buffer, or 0 when everything has been read */
65 BYTE *data; /* directory data */
68 #define FIND_FIRST_MAGIC 0xc0ffee11
70 static const UINT max_entry_size = offsetof( FILE_BOTH_DIRECTORY_INFORMATION, FileName[256] );
72 static BOOL oem_file_apis;
74 static const WCHAR wildcardsW[] = { '*','?',0 };
76 /***********************************************************************
79 * Wrapper for CreateFile that takes OF_* mode flags.
81 static HANDLE create_file_OF( LPCSTR path, INT mode )
83 DWORD access, sharing, creation;
87 creation = CREATE_ALWAYS;
88 access = GENERIC_READ | GENERIC_WRITE;
92 creation = OPEN_EXISTING;
95 case OF_READ: access = GENERIC_READ; break;
96 case OF_WRITE: access = GENERIC_WRITE; break;
97 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
98 default: access = 0; break;
104 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
105 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
106 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
107 case OF_SHARE_DENY_NONE:
108 case OF_SHARE_COMPAT:
109 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
111 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
115 /***********************************************************************
118 * Check if a dir symlink should be returned by FindNextFile.
120 static BOOL check_dir_symlink( FIND_FIRST_INFO *info, const FILE_BOTH_DIR_INFORMATION *file_info )
123 ANSI_STRING unix_name;
124 struct stat st, parent_st;
128 str.MaximumLength = info->path.Length + sizeof(WCHAR) + file_info->FileNameLength;
129 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength ))) return TRUE;
130 memcpy( str.Buffer, info->path.Buffer, info->path.Length );
131 len = info->path.Length / sizeof(WCHAR);
132 if (!len || str.Buffer[len-1] != '\\') str.Buffer[len++] = '\\';
133 memcpy( str.Buffer + len, file_info->FileName, file_info->FileNameLength );
134 str.Length = len * sizeof(WCHAR) + file_info->FileNameLength;
136 unix_name.Buffer = NULL;
137 if (!wine_nt_to_unix_file_name( &str, &unix_name, OPEN_EXISTING, FALSE ) &&
138 !stat( unix_name.Buffer, &st ))
140 char *p = unix_name.Buffer + unix_name.Length - 1;
142 /* skip trailing slashes */
143 while (p > unix_name.Buffer && *p == '/') p--;
145 while (ret && p > unix_name.Buffer)
147 while (p > unix_name.Buffer && *p != '/') p--;
148 while (p > unix_name.Buffer && *p == '/') p--;
150 if (!stat( unix_name.Buffer, &parent_st ) &&
151 parent_st.st_dev == st.st_dev &&
152 parent_st.st_ino == st.st_ino)
154 WARN( "suppressing dir symlink %s pointing to parent %s\n",
155 debugstr_wn( str.Buffer, str.Length/sizeof(WCHAR) ),
156 debugstr_a( unix_name.Buffer ));
161 RtlFreeAnsiString( &unix_name );
162 RtlFreeUnicodeString( &str );
167 /***********************************************************************
170 * Set the DOS error code from errno.
172 void FILE_SetDosError(void)
174 int save_errno = errno; /* errno gets overwritten by printf */
176 TRACE("errno = %d %s\n", errno, strerror(errno));
180 SetLastError( ERROR_SHARING_VIOLATION );
183 SetLastError( ERROR_INVALID_HANDLE );
186 SetLastError( ERROR_HANDLE_DISK_FULL );
191 SetLastError( ERROR_ACCESS_DENIED );
194 SetLastError( ERROR_LOCK_VIOLATION );
197 SetLastError( ERROR_FILE_NOT_FOUND );
200 SetLastError( ERROR_CANNOT_MAKE );
204 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
207 SetLastError( ERROR_FILE_EXISTS );
211 SetLastError( ERROR_SEEK );
214 SetLastError( ERROR_DIR_NOT_EMPTY );
217 SetLastError( ERROR_BAD_FORMAT );
220 SetLastError( ERROR_PATH_NOT_FOUND );
223 SetLastError( ERROR_NOT_SAME_DEVICE );
226 WARN("unknown file error: %s\n", strerror(save_errno) );
227 SetLastError( ERROR_GEN_FAILURE );
234 /***********************************************************************
237 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
239 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
240 * there is no possibility for the function to do that twice, taking into
241 * account any called function.
243 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
246 UNICODE_STRING strW, *pstrW;
249 RtlInitAnsiString( &str, name );
250 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
252 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
254 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
255 if (status == STATUS_SUCCESS) return pstrW->Buffer;
257 if (status == STATUS_BUFFER_OVERFLOW)
258 SetLastError( ERROR_FILENAME_EXCED_RANGE );
260 SetLastError( RtlNtStatusToDosError(status) );
265 /***********************************************************************
268 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
270 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
274 if (srclen < 0) srclen = strlenW( src ) + 1;
276 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
278 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
283 /**************************************************************************
284 * SetFileApisToOEM (KERNEL32.@)
286 VOID WINAPI SetFileApisToOEM(void)
288 oem_file_apis = TRUE;
292 /**************************************************************************
293 * SetFileApisToANSI (KERNEL32.@)
295 VOID WINAPI SetFileApisToANSI(void)
297 oem_file_apis = FALSE;
301 /******************************************************************************
302 * AreFileApisANSI (KERNEL32.@)
304 * Determines if file functions are using ANSI
307 * TRUE: Set of file functions is using ANSI code page
308 * FALSE: Set of file functions is using OEM code page
310 BOOL WINAPI AreFileApisANSI(void)
312 return !oem_file_apis;
316 /**************************************************************************
317 * Operations on file handles *
318 **************************************************************************/
320 /******************************************************************
321 * FILE_ReadWriteApc (internal)
323 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG reserved)
325 LPOVERLAPPED_COMPLETION_ROUTINE cr = apc_user;
327 cr(RtlNtStatusToDosError(io_status->u.Status), io_status->Information, (LPOVERLAPPED)io_status);
331 /***********************************************************************
332 * ReadFileEx (KERNEL32.@)
334 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
335 LPOVERLAPPED overlapped,
336 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
338 LARGE_INTEGER offset;
340 PIO_STATUS_BLOCK io_status;
342 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
346 SetLastError(ERROR_INVALID_PARAMETER);
350 offset.u.LowPart = overlapped->u.s.Offset;
351 offset.u.HighPart = overlapped->u.s.OffsetHigh;
352 io_status = (PIO_STATUS_BLOCK)overlapped;
353 io_status->u.Status = STATUS_PENDING;
354 io_status->Information = 0;
356 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
357 io_status, buffer, bytesToRead, &offset, NULL);
359 if (status && status != STATUS_PENDING)
361 SetLastError( RtlNtStatusToDosError(status) );
368 /***********************************************************************
369 * ReadFileScatter (KERNEL32.@)
371 BOOL WINAPI ReadFileScatter( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
372 LPDWORD reserved, LPOVERLAPPED overlapped )
374 PIO_STATUS_BLOCK io_status;
375 LARGE_INTEGER offset;
378 TRACE( "(%p %p %u %p)\n", file, segments, count, overlapped );
380 offset.u.LowPart = overlapped->u.s.Offset;
381 offset.u.HighPart = overlapped->u.s.OffsetHigh;
382 io_status = (PIO_STATUS_BLOCK)overlapped;
383 io_status->u.Status = STATUS_PENDING;
384 io_status->Information = 0;
386 status = NtReadFileScatter( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
387 if (status) SetLastError( RtlNtStatusToDosError(status) );
392 /***********************************************************************
393 * ReadFile (KERNEL32.@)
395 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
396 LPDWORD bytesRead, LPOVERLAPPED overlapped )
398 LARGE_INTEGER offset;
399 PLARGE_INTEGER poffset = NULL;
400 IO_STATUS_BLOCK iosb;
401 PIO_STATUS_BLOCK io_status = &iosb;
404 LPVOID cvalue = NULL;
406 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToRead,
407 bytesRead, overlapped );
409 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
410 if (!bytesToRead) return TRUE;
412 if (is_console_handle(hFile))
415 if (!ReadConsoleA(hFile, buffer, bytesToRead, &conread, NULL) ||
416 !GetConsoleMode(hFile, &mode))
418 /* ctrl-Z (26) means end of file on window (if at beginning of buffer)
419 * but Unix uses ctrl-D (4), and ctrl-Z is a bad idea on Unix :-/
420 * So map both ctrl-D ctrl-Z to EOF.
422 if ((mode & ENABLE_PROCESSED_INPUT) && conread > 0 &&
423 (((char*)buffer)[0] == 26 || ((char*)buffer)[0] == 4))
427 if (bytesRead) *bytesRead = conread;
431 if (overlapped != NULL)
433 offset.u.LowPart = overlapped->u.s.Offset;
434 offset.u.HighPart = overlapped->u.s.OffsetHigh;
436 hEvent = overlapped->hEvent;
437 io_status = (PIO_STATUS_BLOCK)overlapped;
438 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
440 io_status->u.Status = STATUS_PENDING;
441 io_status->Information = 0;
443 status = NtReadFile(hFile, hEvent, NULL, cvalue, io_status, buffer, bytesToRead, poffset, NULL);
445 if (status == STATUS_PENDING && !overlapped)
447 WaitForSingleObject( hFile, INFINITE );
448 status = io_status->u.Status;
451 if (status != STATUS_PENDING && bytesRead)
452 *bytesRead = io_status->Information;
454 if (status && status != STATUS_END_OF_FILE && status != STATUS_TIMEOUT)
456 SetLastError( RtlNtStatusToDosError(status) );
463 /***********************************************************************
464 * WriteFileEx (KERNEL32.@)
466 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
467 LPOVERLAPPED overlapped,
468 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
470 LARGE_INTEGER offset;
472 PIO_STATUS_BLOCK io_status;
474 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
476 if (overlapped == NULL)
478 SetLastError(ERROR_INVALID_PARAMETER);
481 offset.u.LowPart = overlapped->u.s.Offset;
482 offset.u.HighPart = overlapped->u.s.OffsetHigh;
484 io_status = (PIO_STATUS_BLOCK)overlapped;
485 io_status->u.Status = STATUS_PENDING;
486 io_status->Information = 0;
488 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
489 io_status, buffer, bytesToWrite, &offset, NULL);
491 if (status && status != STATUS_PENDING)
493 SetLastError( RtlNtStatusToDosError(status) );
500 /***********************************************************************
501 * WriteFileGather (KERNEL32.@)
503 BOOL WINAPI WriteFileGather( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
504 LPDWORD reserved, LPOVERLAPPED overlapped )
506 PIO_STATUS_BLOCK io_status;
507 LARGE_INTEGER offset;
510 TRACE( "%p %p %u %p\n", file, segments, count, overlapped );
512 offset.u.LowPart = overlapped->u.s.Offset;
513 offset.u.HighPart = overlapped->u.s.OffsetHigh;
514 io_status = (PIO_STATUS_BLOCK)overlapped;
515 io_status->u.Status = STATUS_PENDING;
516 io_status->Information = 0;
518 status = NtWriteFileGather( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
519 if (status) SetLastError( RtlNtStatusToDosError(status) );
524 /***********************************************************************
525 * WriteFile (KERNEL32.@)
527 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
528 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
530 HANDLE hEvent = NULL;
531 LARGE_INTEGER offset;
532 PLARGE_INTEGER poffset = NULL;
534 IO_STATUS_BLOCK iosb;
535 PIO_STATUS_BLOCK piosb = &iosb;
536 LPVOID cvalue = NULL;
538 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
540 if (is_console_handle(hFile))
541 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
545 offset.u.LowPart = overlapped->u.s.Offset;
546 offset.u.HighPart = overlapped->u.s.OffsetHigh;
548 hEvent = overlapped->hEvent;
549 piosb = (PIO_STATUS_BLOCK)overlapped;
550 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
552 piosb->u.Status = STATUS_PENDING;
553 piosb->Information = 0;
555 status = NtWriteFile(hFile, hEvent, NULL, cvalue, piosb,
556 buffer, bytesToWrite, poffset, NULL);
558 if (status == STATUS_PENDING && !overlapped)
560 WaitForSingleObject( hFile, INFINITE );
561 status = piosb->u.Status;
564 if (status != STATUS_PENDING && bytesWritten)
565 *bytesWritten = piosb->Information;
567 if (status && status != STATUS_TIMEOUT)
569 SetLastError( RtlNtStatusToDosError(status) );
576 /***********************************************************************
577 * GetOverlappedResult (KERNEL32.@)
579 * Check the result of an Asynchronous data transfer from a file.
582 * HANDLE hFile [in] handle of file to check on
583 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
584 * LPDWORD lpTransferred [in/out] number of bytes transferred
585 * BOOL bWait [in] wait for the transfer to complete ?
591 * If successful (and relevant) lpTransferred will hold the number of
592 * bytes transferred during the async operation.
594 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
595 LPDWORD lpTransferred, BOOL bWait)
599 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
601 status = lpOverlapped->Internal;
602 if (status == STATUS_PENDING)
606 SetLastError( ERROR_IO_INCOMPLETE );
610 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : hFile,
611 INFINITE ) == WAIT_FAILED)
613 status = lpOverlapped->Internal;
616 *lpTransferred = lpOverlapped->InternalHigh;
618 if (status) SetLastError( RtlNtStatusToDosError(status) );
622 /***********************************************************************
623 * CancelIoEx (KERNEL32.@)
625 * Cancels pending I/O operations on a file given the overlapped used.
628 * handle [I] File handle.
629 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
633 * Failure: FALSE, check GetLastError().
635 BOOL WINAPI CancelIoEx(HANDLE handle, LPOVERLAPPED lpOverlapped)
637 IO_STATUS_BLOCK io_status;
639 NtCancelIoFileEx(handle, (PIO_STATUS_BLOCK) lpOverlapped, &io_status);
640 if (io_status.u.Status)
642 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
648 /***********************************************************************
649 * CancelIo (KERNEL32.@)
651 * Cancels pending I/O operations initiated by the current thread on a file.
654 * handle [I] File handle.
658 * Failure: FALSE, check GetLastError().
660 BOOL WINAPI CancelIo(HANDLE handle)
662 IO_STATUS_BLOCK io_status;
664 NtCancelIoFile(handle, &io_status);
665 if (io_status.u.Status)
667 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
673 /***********************************************************************
674 * _hread (KERNEL32.@)
676 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
678 return _lread( hFile, buffer, count );
682 /***********************************************************************
683 * _hwrite (KERNEL32.@)
685 * experimentation yields that _lwrite:
686 * o truncates the file at the current position with
688 * o returns 0 on a 0 length write
689 * o works with console handles
692 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
696 TRACE("%d %p %d\n", handle, buffer, count );
700 /* Expand or truncate at current position */
701 if (!SetEndOfFile( LongToHandle(handle) )) return HFILE_ERROR;
704 if (!WriteFile( LongToHandle(handle), buffer, count, &result, NULL ))
710 /***********************************************************************
711 * _lclose (KERNEL32.@)
713 HFILE WINAPI _lclose( HFILE hFile )
715 TRACE("handle %d\n", hFile );
716 return CloseHandle( LongToHandle(hFile) ) ? 0 : HFILE_ERROR;
720 /***********************************************************************
721 * _lcreat (KERNEL32.@)
723 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
727 /* Mask off all flags not explicitly allowed by the doc */
728 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
729 TRACE("%s %02x\n", path, attr );
730 hfile = CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
731 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
732 CREATE_ALWAYS, attr, 0 );
733 return HandleToLong(hfile);
737 /***********************************************************************
738 * _lopen (KERNEL32.@)
740 HFILE WINAPI _lopen( LPCSTR path, INT mode )
744 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
745 hfile = create_file_OF( path, mode & ~OF_CREATE );
746 return HandleToLong(hfile);
749 /***********************************************************************
750 * _lread (KERNEL32.@)
752 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
755 if (!ReadFile( LongToHandle(handle), buffer, count, &result, NULL ))
761 /***********************************************************************
762 * _llseek (KERNEL32.@)
764 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
766 return SetFilePointer( LongToHandle(hFile), lOffset, NULL, nOrigin );
770 /***********************************************************************
771 * _lwrite (KERNEL32.@)
773 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
775 return (UINT)_hwrite( hFile, buffer, (LONG)count );
779 /***********************************************************************
780 * FlushFileBuffers (KERNEL32.@)
782 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
785 IO_STATUS_BLOCK ioblk;
787 if (is_console_handle( hFile ))
789 /* this will fail (as expected) for an output handle */
790 return FlushConsoleInputBuffer( hFile );
792 nts = NtFlushBuffersFile( hFile, &ioblk );
793 if (nts != STATUS_SUCCESS)
795 SetLastError( RtlNtStatusToDosError( nts ) );
803 /***********************************************************************
804 * GetFileType (KERNEL32.@)
806 DWORD WINAPI GetFileType( HANDLE hFile )
808 FILE_FS_DEVICE_INFORMATION info;
812 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
814 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
815 if (status != STATUS_SUCCESS)
817 SetLastError( RtlNtStatusToDosError(status) );
818 return FILE_TYPE_UNKNOWN;
821 switch(info.DeviceType)
823 case FILE_DEVICE_NULL:
824 case FILE_DEVICE_SERIAL_PORT:
825 case FILE_DEVICE_PARALLEL_PORT:
826 case FILE_DEVICE_TAPE:
827 case FILE_DEVICE_UNKNOWN:
828 return FILE_TYPE_CHAR;
829 case FILE_DEVICE_NAMED_PIPE:
830 return FILE_TYPE_PIPE;
832 return FILE_TYPE_DISK;
837 /***********************************************************************
838 * GetFileInformationByHandle (KERNEL32.@)
840 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
842 FILE_ALL_INFORMATION all_info;
846 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
847 if (status == STATUS_BUFFER_OVERFLOW) status = STATUS_SUCCESS;
848 if (status == STATUS_SUCCESS)
850 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
851 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
852 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
853 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
854 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
855 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
856 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
857 info->dwVolumeSerialNumber = 0; /* FIXME */
858 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
859 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
860 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
861 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
862 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
865 SetLastError( RtlNtStatusToDosError(status) );
870 /***********************************************************************
871 * GetFileInformationByHandleEx (KERNEL32.@)
873 BOOL WINAPI GetFileInformationByHandleEx( HANDLE handle, FILE_INFO_BY_HANDLE_CLASS class,
874 LPVOID info, DWORD size )
882 case FileStandardInfo:
884 case FileDispositionInfo:
885 case FileAllocationInfo:
886 case FileEndOfFileInfo:
888 case FileCompressionInfo:
889 case FileAttributeTagInfo:
890 case FileIoPriorityHintInfo:
891 case FileRemoteProtocolInfo:
892 case FileFullDirectoryInfo:
893 case FileFullDirectoryRestartInfo:
894 case FileStorageInfo:
895 case FileAlignmentInfo:
897 case FileIdExtdDirectoryInfo:
898 case FileIdExtdDirectoryRestartInfo:
899 FIXME( "%p, %u, %p, %u\n", handle, class, info, size );
900 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
904 status = NtQueryInformationFile( handle, &io, info, size, FileNameInformation );
905 if (status != STATUS_SUCCESS)
907 SetLastError( RtlNtStatusToDosError( status ) );
912 case FileIdBothDirectoryRestartInfo:
913 case FileIdBothDirectoryInfo:
914 status = NtQueryDirectoryFile( handle, NULL, NULL, NULL, &io, info, size,
915 FileIdBothDirectoryInformation, FALSE, NULL,
916 (class == FileIdBothDirectoryRestartInfo) );
917 if (status != STATUS_SUCCESS)
919 SetLastError( RtlNtStatusToDosError( status ) );
925 SetLastError( ERROR_INVALID_PARAMETER );
931 /***********************************************************************
932 * GetFileSize (KERNEL32.@)
934 * Retrieve the size of a file.
937 * hFile [I] File to retrieve size of.
938 * filesizehigh [O] On return, the high bits of the file size.
941 * Success: The low bits of the file size.
942 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
943 * check GetLastError() for values other than ERROR_SUCCESS.
945 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
948 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
949 if (filesizehigh) *filesizehigh = size.u.HighPart;
950 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
951 return size.u.LowPart;
955 /***********************************************************************
956 * GetFileSizeEx (KERNEL32.@)
958 * Retrieve the size of a file.
961 * hFile [I] File to retrieve size of.
962 * lpFileSIze [O] On return, the size of the file.
966 * Failure: FALSE, check GetLastError().
968 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
970 FILE_STANDARD_INFORMATION info;
974 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileStandardInformation );
975 if (status == STATUS_SUCCESS)
977 *lpFileSize = info.EndOfFile;
980 SetLastError( RtlNtStatusToDosError(status) );
985 /**************************************************************************
986 * SetEndOfFile (KERNEL32.@)
988 * Sets the current position as the end of the file.
991 * hFile [I] File handle.
995 * Failure: FALSE, check GetLastError().
997 BOOL WINAPI SetEndOfFile( HANDLE hFile )
999 FILE_POSITION_INFORMATION pos;
1000 FILE_END_OF_FILE_INFORMATION eof;
1004 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
1005 if (status == STATUS_SUCCESS)
1007 eof.EndOfFile = pos.CurrentByteOffset;
1008 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
1010 if (status == STATUS_SUCCESS) return TRUE;
1011 SetLastError( RtlNtStatusToDosError(status) );
1015 BOOL WINAPI SetFileInformationByHandle( HANDLE file, FILE_INFO_BY_HANDLE_CLASS class, VOID *info, DWORD size )
1017 FIXME("%p %u %p %u - stub\n", file, class, info, size);
1021 /***********************************************************************
1022 * SetFilePointer (KERNEL32.@)
1024 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
1026 LARGE_INTEGER dist, newpos;
1030 dist.u.LowPart = distance;
1031 dist.u.HighPart = *highword;
1033 else dist.QuadPart = distance;
1035 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
1037 if (highword) *highword = newpos.u.HighPart;
1038 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
1039 return newpos.u.LowPart;
1043 /***********************************************************************
1044 * SetFilePointerEx (KERNEL32.@)
1046 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
1047 LARGE_INTEGER *newpos, DWORD method )
1051 FILE_POSITION_INFORMATION info;
1056 pos = distance.QuadPart;
1059 if (NtQueryInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1061 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
1065 FILE_END_OF_FILE_INFORMATION eof;
1066 if (NtQueryInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
1068 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
1072 SetLastError( ERROR_INVALID_PARAMETER );
1078 SetLastError( ERROR_NEGATIVE_SEEK );
1082 info.CurrentByteOffset.QuadPart = pos;
1083 if (NtSetInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1085 if (newpos) newpos->QuadPart = pos;
1089 SetLastError( RtlNtStatusToDosError(io.u.Status) );
1093 /***********************************************************************
1094 * SetFileValidData (KERNEL32.@)
1096 BOOL WINAPI SetFileValidData( HANDLE hFile, LONGLONG ValidDataLength )
1098 FILE_VALID_DATA_LENGTH_INFORMATION info;
1102 info.ValidDataLength.QuadPart = ValidDataLength;
1103 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileValidDataLengthInformation );
1105 if (status == STATUS_SUCCESS) return TRUE;
1106 SetLastError( RtlNtStatusToDosError(status) );
1110 /***********************************************************************
1111 * GetFileTime (KERNEL32.@)
1113 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
1114 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1116 FILE_BASIC_INFORMATION info;
1120 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1121 if (status == STATUS_SUCCESS)
1125 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
1126 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
1128 if (lpLastAccessTime)
1130 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
1131 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
1133 if (lpLastWriteTime)
1135 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
1136 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1140 SetLastError( RtlNtStatusToDosError(status) );
1145 /***********************************************************************
1146 * SetFileTime (KERNEL32.@)
1148 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1149 const FILETIME *atime, const FILETIME *mtime )
1151 FILE_BASIC_INFORMATION info;
1155 memset( &info, 0, sizeof(info) );
1158 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1159 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1163 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1164 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1168 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1169 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1172 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1173 if (status == STATUS_SUCCESS) return TRUE;
1174 SetLastError( RtlNtStatusToDosError(status) );
1179 /**************************************************************************
1180 * LockFile (KERNEL32.@)
1182 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1183 DWORD count_low, DWORD count_high )
1186 LARGE_INTEGER count, offset;
1188 TRACE( "%p %x%08x %x%08x\n",
1189 hFile, offset_high, offset_low, count_high, count_low );
1191 count.u.LowPart = count_low;
1192 count.u.HighPart = count_high;
1193 offset.u.LowPart = offset_low;
1194 offset.u.HighPart = offset_high;
1196 status = NtLockFile( hFile, 0, NULL, NULL,
1197 NULL, &offset, &count, NULL, TRUE, TRUE );
1199 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1204 /**************************************************************************
1205 * LockFileEx [KERNEL32.@]
1207 * Locks a byte range within an open file for shared or exclusive access.
1214 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1216 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1217 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1220 LARGE_INTEGER count, offset;
1221 LPVOID cvalue = NULL;
1225 SetLastError( ERROR_INVALID_PARAMETER );
1229 TRACE( "%p %x%08x %x%08x flags %x\n",
1230 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1231 count_high, count_low, flags );
1233 count.u.LowPart = count_low;
1234 count.u.HighPart = count_high;
1235 offset.u.LowPart = overlapped->u.s.Offset;
1236 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1238 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1240 status = NtLockFile( hFile, overlapped->hEvent, NULL, cvalue,
1241 NULL, &offset, &count, NULL,
1242 flags & LOCKFILE_FAIL_IMMEDIATELY,
1243 flags & LOCKFILE_EXCLUSIVE_LOCK );
1245 if (status) SetLastError( RtlNtStatusToDosError(status) );
1250 /**************************************************************************
1251 * UnlockFile (KERNEL32.@)
1253 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1254 DWORD count_low, DWORD count_high )
1257 LARGE_INTEGER count, offset;
1259 count.u.LowPart = count_low;
1260 count.u.HighPart = count_high;
1261 offset.u.LowPart = offset_low;
1262 offset.u.HighPart = offset_high;
1264 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1265 if (status) SetLastError( RtlNtStatusToDosError(status) );
1270 /**************************************************************************
1271 * UnlockFileEx (KERNEL32.@)
1273 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1274 LPOVERLAPPED overlapped )
1278 SetLastError( ERROR_INVALID_PARAMETER );
1281 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1283 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1287 /*************************************************************************
1288 * SetHandleCount (KERNEL32.@)
1290 UINT WINAPI SetHandleCount( UINT count )
1296 /**************************************************************************
1297 * Operations on file names *
1298 **************************************************************************/
1301 /*************************************************************************
1302 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1304 * Creates or opens an object, and returns a handle that can be used to
1305 * access that object.
1309 * filename [in] pointer to filename to be accessed
1310 * access [in] access mode requested
1311 * sharing [in] share mode
1312 * sa [in] pointer to security attributes
1313 * creation [in] how to create the file
1314 * attributes [in] attributes for newly created file
1315 * template [in] handle to file with extended attributes to copy
1318 * Success: Open handle to specified file
1319 * Failure: INVALID_HANDLE_VALUE
1321 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1322 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1323 DWORD attributes, HANDLE template )
1327 OBJECT_ATTRIBUTES attr;
1328 UNICODE_STRING nameW;
1332 const WCHAR *vxd_name = NULL;
1333 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1334 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1335 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1336 SECURITY_QUALITY_OF_SERVICE qos;
1338 static const UINT nt_disposition[5] =
1340 FILE_CREATE, /* CREATE_NEW */
1341 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1342 FILE_OPEN, /* OPEN_EXISTING */
1343 FILE_OPEN_IF, /* OPEN_ALWAYS */
1344 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1350 if (!filename || !filename[0])
1352 SetLastError( ERROR_PATH_NOT_FOUND );
1353 return INVALID_HANDLE_VALUE;
1356 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1357 (access & GENERIC_READ)?"GENERIC_READ ":"",
1358 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1359 (access & GENERIC_EXECUTE)?"GENERIC_EXECUTE ":"",
1360 (!access)?"QUERY_ACCESS ":"",
1361 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1362 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1363 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1364 creation, attributes);
1366 /* Open a console for CONIN$ or CONOUT$ */
1368 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1370 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle),
1371 creation ? OPEN_EXISTING : 0);
1372 if (ret == INVALID_HANDLE_VALUE) SetLastError(ERROR_INVALID_PARAMETER);
1376 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1378 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1379 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1381 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1382 !strncmpiW( filename + 4, pipeW, 5 ) ||
1383 !strncmpiW( filename + 4, mailslotW, 9 ))
1387 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1389 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1391 else if (GetVersion() & 0x80000000)
1393 vxd_name = filename + 4;
1394 if (!creation) creation = OPEN_EXISTING;
1397 else dosdev = RtlIsDosDeviceName_U( filename );
1401 static const WCHAR conW[] = {'C','O','N'};
1403 if (LOWORD(dosdev) == sizeof(conW) &&
1404 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1406 switch (access & (GENERIC_READ|GENERIC_WRITE))
1409 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1412 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1415 SetLastError( ERROR_FILE_NOT_FOUND );
1416 return INVALID_HANDLE_VALUE;
1421 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1423 SetLastError( ERROR_INVALID_PARAMETER );
1424 return INVALID_HANDLE_VALUE;
1427 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1429 SetLastError( ERROR_PATH_NOT_FOUND );
1430 return INVALID_HANDLE_VALUE;
1433 /* now call NtCreateFile */
1436 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1437 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1439 options |= FILE_NON_DIRECTORY_FILE;
1440 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1442 options |= FILE_DELETE_ON_CLOSE;
1445 if (attributes & FILE_FLAG_NO_BUFFERING)
1446 options |= FILE_NO_INTERMEDIATE_BUFFERING;
1447 if (!(attributes & FILE_FLAG_OVERLAPPED))
1448 options |= FILE_SYNCHRONOUS_IO_NONALERT;
1449 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1450 options |= FILE_RANDOM_ACCESS;
1451 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1453 attr.Length = sizeof(attr);
1454 attr.RootDirectory = 0;
1455 attr.Attributes = OBJ_CASE_INSENSITIVE;
1456 attr.ObjectName = &nameW;
1457 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1458 if (attributes & SECURITY_SQOS_PRESENT)
1460 qos.Length = sizeof(qos);
1461 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
1462 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
1463 qos.EffectiveOnly = (attributes & SECURITY_EFFECTIVE_ONLY) != 0;
1464 attr.SecurityQualityOfService = &qos;
1467 attr.SecurityQualityOfService = NULL;
1469 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1471 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1472 sharing, nt_disposition[creation - CREATE_NEW],
1476 if (vxd_name && vxd_name[0])
1478 static HANDLE (*vxd_open)(LPCWSTR,DWORD,SECURITY_ATTRIBUTES*);
1479 if (!vxd_open) vxd_open = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1480 "__wine_vxd_open" );
1481 if (vxd_open && (ret = vxd_open( vxd_name, access, sa ))) goto done;
1484 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1485 ret = INVALID_HANDLE_VALUE;
1487 /* In the case file creation was rejected due to CREATE_NEW flag
1488 * was specified and file with that name already exists, correct
1489 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1490 * Note: RtlNtStatusToDosError is not the subject to blame here.
1492 if (status == STATUS_OBJECT_NAME_COLLISION)
1493 SetLastError( ERROR_FILE_EXISTS );
1495 SetLastError( RtlNtStatusToDosError(status) );
1499 if ((creation == CREATE_ALWAYS && io.Information == FILE_OVERWRITTEN) ||
1500 (creation == OPEN_ALWAYS && io.Information == FILE_OPENED))
1501 SetLastError( ERROR_ALREADY_EXISTS );
1505 RtlFreeUnicodeString( &nameW );
1508 if (!ret) ret = INVALID_HANDLE_VALUE;
1509 TRACE("returning %p\n", ret);
1515 /*************************************************************************
1516 * CreateFileA (KERNEL32.@)
1520 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1521 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1522 DWORD attributes, HANDLE template)
1526 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1527 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1531 /***********************************************************************
1532 * DeleteFileW (KERNEL32.@)
1537 * path [I] Path to the file to delete.
1541 * Failure: FALSE, check GetLastError().
1543 BOOL WINAPI DeleteFileW( LPCWSTR path )
1545 UNICODE_STRING nameW;
1546 OBJECT_ATTRIBUTES attr;
1551 TRACE("%s\n", debugstr_w(path) );
1553 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1555 SetLastError( ERROR_PATH_NOT_FOUND );
1559 attr.Length = sizeof(attr);
1560 attr.RootDirectory = 0;
1561 attr.Attributes = OBJ_CASE_INSENSITIVE;
1562 attr.ObjectName = &nameW;
1563 attr.SecurityDescriptor = NULL;
1564 attr.SecurityQualityOfService = NULL;
1566 status = NtCreateFile(&hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
1567 &attr, &io, NULL, 0,
1568 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1569 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0);
1570 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1572 RtlFreeUnicodeString( &nameW );
1575 SetLastError( RtlNtStatusToDosError(status) );
1582 /***********************************************************************
1583 * DeleteFileA (KERNEL32.@)
1587 BOOL WINAPI DeleteFileA( LPCSTR path )
1591 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1592 return DeleteFileW( pathW );
1596 /**************************************************************************
1597 * ReplaceFileW (KERNEL32.@)
1598 * ReplaceFile (KERNEL32.@)
1600 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName, LPCWSTR lpReplacementFileName,
1601 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1602 LPVOID lpExclude, LPVOID lpReserved)
1604 UNICODE_STRING nt_replaced_name, nt_replacement_name;
1605 ANSI_STRING unix_replaced_name, unix_replacement_name, unix_backup_name;
1606 HANDLE hReplaced = NULL, hReplacement = NULL, hBackup = NULL;
1607 DWORD error = ERROR_SUCCESS;
1608 UINT replaced_flags;
1612 OBJECT_ATTRIBUTES attr;
1614 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName),
1615 debugstr_w(lpReplacementFileName), debugstr_w(lpBackupFileName),
1616 dwReplaceFlags, lpExclude, lpReserved);
1619 FIXME("Ignoring flags %x\n", dwReplaceFlags);
1621 /* First two arguments are mandatory */
1622 if (!lpReplacedFileName || !lpReplacementFileName)
1624 SetLastError(ERROR_INVALID_PARAMETER);
1628 unix_replaced_name.Buffer = NULL;
1629 unix_replacement_name.Buffer = NULL;
1630 unix_backup_name.Buffer = NULL;
1632 attr.Length = sizeof(attr);
1633 attr.RootDirectory = 0;
1634 attr.Attributes = OBJ_CASE_INSENSITIVE;
1635 attr.ObjectName = NULL;
1636 attr.SecurityDescriptor = NULL;
1637 attr.SecurityQualityOfService = NULL;
1639 /* Open the "replaced" file for reading and writing */
1640 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName, &nt_replaced_name, NULL, NULL)))
1642 error = ERROR_PATH_NOT_FOUND;
1645 replaced_flags = lpBackupFileName ? FILE_OPEN : FILE_OPEN_IF;
1646 attr.ObjectName = &nt_replaced_name;
1647 status = NtOpenFile(&hReplaced, GENERIC_READ|GENERIC_WRITE|DELETE|SYNCHRONIZE,
1649 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1650 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1651 if (status == STATUS_SUCCESS)
1652 status = wine_nt_to_unix_file_name(&nt_replaced_name, &unix_replaced_name, replaced_flags, FALSE);
1653 RtlFreeUnicodeString(&nt_replaced_name);
1654 if (status != STATUS_SUCCESS)
1656 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1657 error = ERROR_FILE_NOT_FOUND;
1659 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1664 * Open the replacement file for reading, writing, and deleting
1665 * (writing and deleting are needed when finished)
1667 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName, &nt_replacement_name, NULL, NULL)))
1669 error = ERROR_PATH_NOT_FOUND;
1672 attr.ObjectName = &nt_replacement_name;
1673 status = NtOpenFile(&hReplacement,
1674 GENERIC_READ|GENERIC_WRITE|DELETE|WRITE_DAC|SYNCHRONIZE,
1676 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1677 if (status == STATUS_SUCCESS)
1678 status = wine_nt_to_unix_file_name(&nt_replacement_name, &unix_replacement_name, FILE_OPEN, FALSE);
1679 RtlFreeUnicodeString(&nt_replacement_name);
1680 if (status != STATUS_SUCCESS)
1682 error = RtlNtStatusToDosError(status);
1686 /* If the user wants a backup then that needs to be performed first */
1687 if (lpBackupFileName)
1689 UNICODE_STRING nt_backup_name;
1690 FILE_BASIC_INFORMATION replaced_info;
1692 /* Obtain the file attributes from the "replaced" file */
1693 status = NtQueryInformationFile(hReplaced, &io, &replaced_info,
1694 sizeof(replaced_info),
1695 FileBasicInformation);
1696 if (status != STATUS_SUCCESS)
1698 error = RtlNtStatusToDosError(status);
1702 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName, &nt_backup_name, NULL, NULL)))
1704 error = ERROR_PATH_NOT_FOUND;
1707 attr.ObjectName = &nt_backup_name;
1708 /* Open the backup with permissions to write over it */
1709 status = NtCreateFile(&hBackup, GENERIC_WRITE,
1710 &attr, &io, NULL, replaced_info.FileAttributes,
1711 FILE_SHARE_WRITE, FILE_OPEN_IF,
1712 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE,
1714 if (status == STATUS_SUCCESS)
1715 status = wine_nt_to_unix_file_name(&nt_backup_name, &unix_backup_name, FILE_OPEN_IF, FALSE);
1716 RtlFreeUnicodeString(&nt_backup_name);
1717 if (status != STATUS_SUCCESS)
1719 error = RtlNtStatusToDosError(status);
1723 /* If an existing backup exists then copy over it */
1724 if (rename(unix_replaced_name.Buffer, unix_backup_name.Buffer) == -1)
1726 error = ERROR_UNABLE_TO_REMOVE_REPLACED; /* is this correct? */
1732 * Now that the backup has been performed (if requested), copy the replacement
1735 if (rename(unix_replacement_name.Buffer, unix_replaced_name.Buffer) == -1)
1737 if (errno == EACCES)
1739 /* Inappropriate permissions on "replaced", rename will fail */
1740 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1743 /* on failure we need to indicate whether a backup was made */
1744 if (!lpBackupFileName)
1745 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT;
1747 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT_2;
1753 /* Perform resource cleanup */
1755 if (hBackup) CloseHandle(hBackup);
1756 if (hReplaced) CloseHandle(hReplaced);
1757 if (hReplacement) CloseHandle(hReplacement);
1758 RtlFreeAnsiString(&unix_backup_name);
1759 RtlFreeAnsiString(&unix_replacement_name);
1760 RtlFreeAnsiString(&unix_replaced_name);
1762 /* If there was an error, set the error code */
1764 SetLastError(error);
1769 /**************************************************************************
1770 * ReplaceFileA (KERNEL32.@)
1772 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1773 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1774 LPVOID lpExclude, LPVOID lpReserved)
1776 WCHAR *replacedW, *replacementW, *backupW = NULL;
1779 /* This function only makes sense when the first two parameters are defined */
1780 if (!lpReplacedFileName || !(replacedW = FILE_name_AtoW( lpReplacedFileName, TRUE )))
1782 SetLastError(ERROR_INVALID_PARAMETER);
1785 if (!lpReplacementFileName || !(replacementW = FILE_name_AtoW( lpReplacementFileName, TRUE )))
1787 HeapFree( GetProcessHeap(), 0, replacedW );
1788 SetLastError(ERROR_INVALID_PARAMETER);
1791 /* The backup parameter, however, is optional */
1792 if (lpBackupFileName)
1794 if (!(backupW = FILE_name_AtoW( lpBackupFileName, TRUE )))
1796 HeapFree( GetProcessHeap(), 0, replacedW );
1797 HeapFree( GetProcessHeap(), 0, replacementW );
1798 SetLastError(ERROR_INVALID_PARAMETER);
1802 ret = ReplaceFileW( replacedW, replacementW, backupW, dwReplaceFlags, lpExclude, lpReserved );
1803 HeapFree( GetProcessHeap(), 0, replacedW );
1804 HeapFree( GetProcessHeap(), 0, replacementW );
1805 HeapFree( GetProcessHeap(), 0, backupW );
1810 /*************************************************************************
1811 * FindFirstFileExW (KERNEL32.@)
1813 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1814 * results as FindExSearchNameMatch
1816 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1817 LPVOID data, FINDEX_SEARCH_OPS search_op,
1818 LPVOID filter, DWORD flags)
1821 FIND_FIRST_INFO *info = NULL;
1822 UNICODE_STRING nt_name;
1823 OBJECT_ATTRIBUTES attr;
1828 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1830 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1833 FIXME("options not implemented 0x%08x 0x%08x\n", search_op, flags );
1834 return INVALID_HANDLE_VALUE;
1836 if (level != FindExInfoStandard)
1838 FIXME("info level %d not implemented\n", level );
1839 return INVALID_HANDLE_VALUE;
1842 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1844 SetLastError( ERROR_PATH_NOT_FOUND );
1845 return INVALID_HANDLE_VALUE;
1848 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1850 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1854 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1856 static const WCHAR dotW[] = {'.',0};
1859 /* we still need to check that the directory can be opened */
1863 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1865 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1868 memcpy( dir, filename, HIWORD(device) );
1869 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1871 RtlFreeUnicodeString( &nt_name );
1872 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1874 HeapFree( GetProcessHeap(), 0, dir );
1875 SetLastError( ERROR_PATH_NOT_FOUND );
1878 HeapFree( GetProcessHeap(), 0, dir );
1879 RtlInitUnicodeString( &info->mask, NULL );
1881 else if (!mask || !*mask)
1883 SetLastError( ERROR_FILE_NOT_FOUND );
1888 if (!RtlCreateUnicodeString( &info->mask, mask ))
1890 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1894 /* truncate dir name before mask */
1896 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1899 /* check if path is the root of the drive */
1900 info->is_root = FALSE;
1901 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1902 if (p[0] && p[1] == ':')
1905 while (*p == '\\') p++;
1906 info->is_root = (*p == 0);
1909 attr.Length = sizeof(attr);
1910 attr.RootDirectory = 0;
1911 attr.Attributes = OBJ_CASE_INSENSITIVE;
1912 attr.ObjectName = &nt_name;
1913 attr.SecurityDescriptor = NULL;
1914 attr.SecurityQualityOfService = NULL;
1916 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1917 FILE_SHARE_READ | FILE_SHARE_WRITE,
1918 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1920 if (status != STATUS_SUCCESS)
1922 RtlFreeUnicodeString( &info->mask );
1923 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1924 SetLastError( ERROR_PATH_NOT_FOUND );
1926 SetLastError( RtlNtStatusToDosError(status) );
1930 RtlInitializeCriticalSection( &info->cs );
1931 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
1932 info->path = nt_name;
1933 info->magic = FIND_FIRST_MAGIC;
1936 info->data_size = 0;
1938 info->search_op = search_op;
1942 WIN32_FIND_DATAW *wfd = data;
1944 memset( wfd, 0, sizeof(*wfd) );
1945 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1946 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1947 CloseHandle( info->handle );
1953 BOOL has_wildcard = strpbrkW( info->mask.Buffer, wildcardsW ) != NULL;
1955 info->data_size = has_wildcard ? 8192 : max_entry_size;
1957 while (info->data_size)
1959 if (!(info->data = HeapAlloc( GetProcessHeap(), 0, info->data_size )))
1962 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1963 return INVALID_HANDLE_VALUE;
1966 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
1967 FileBothDirectoryInformation, FALSE, &info->mask, TRUE );
1971 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1972 return INVALID_HANDLE_VALUE;
1975 if (io.Information < info->data_size - max_entry_size)
1977 info->data_size = 0; /* we read everything */
1979 else if (info->data_size < 1024 * 1024)
1981 HeapFree( GetProcessHeap(), 0, info->data );
1982 info->data_size *= 2;
1987 info->data_len = io.Information;
1988 if (!info->data_size && has_wildcard) /* release unused buffer space */
1989 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, info->data, info->data_len );
1991 if (!FindNextFileW( info, data ))
1993 TRACE( "%s not found\n", debugstr_w(filename) );
1995 SetLastError( ERROR_FILE_NOT_FOUND );
1996 return INVALID_HANDLE_VALUE;
1998 if (!has_wildcard) /* we can't find two files with the same name */
2000 CloseHandle( info->handle );
2001 HeapFree( GetProcessHeap(), 0, info->data );
2009 HeapFree( GetProcessHeap(), 0, info );
2010 RtlFreeUnicodeString( &nt_name );
2011 return INVALID_HANDLE_VALUE;
2015 /*************************************************************************
2016 * FindNextFileW (KERNEL32.@)
2018 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
2020 FIND_FIRST_INFO *info;
2021 FILE_BOTH_DIR_INFORMATION *dir_info;
2024 TRACE("%p %p\n", handle, data);
2026 if (!handle || handle == INVALID_HANDLE_VALUE)
2028 SetLastError( ERROR_INVALID_HANDLE );
2032 if (info->magic != FIND_FIRST_MAGIC)
2034 SetLastError( ERROR_INVALID_HANDLE );
2038 RtlEnterCriticalSection( &info->cs );
2040 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
2043 if (info->data_pos >= info->data_len) /* need to read some more data */
2047 if (info->data_size)
2048 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
2049 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
2051 io.u.Status = STATUS_NO_MORE_FILES;
2055 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
2056 if (io.u.Status == STATUS_NO_MORE_FILES)
2058 CloseHandle( info->handle );
2059 HeapFree( GetProcessHeap(), 0, info->data );
2065 info->data_len = io.Information;
2069 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
2071 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
2072 else info->data_pos = info->data_len;
2074 /* don't return '.' and '..' in the root of the drive */
2077 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
2078 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
2079 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
2082 /* check for dir symlink */
2083 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
2084 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2085 strpbrkW( info->mask.Buffer, wildcardsW ))
2087 if (!check_dir_symlink( info, dir_info )) continue;
2090 data->dwFileAttributes = dir_info->FileAttributes;
2091 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
2092 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
2093 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
2094 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
2095 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
2096 data->dwReserved0 = 0;
2097 data->dwReserved1 = 0;
2099 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
2100 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
2101 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
2102 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
2104 TRACE("returning %s (%s)\n",
2105 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
2111 RtlLeaveCriticalSection( &info->cs );
2116 /*************************************************************************
2117 * FindClose (KERNEL32.@)
2119 BOOL WINAPI FindClose( HANDLE handle )
2121 FIND_FIRST_INFO *info = handle;
2123 if (!handle || handle == INVALID_HANDLE_VALUE)
2125 SetLastError( ERROR_INVALID_HANDLE );
2131 if (info->magic == FIND_FIRST_MAGIC)
2133 RtlEnterCriticalSection( &info->cs );
2134 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
2137 if (info->handle) CloseHandle( info->handle );
2139 RtlFreeUnicodeString( &info->mask );
2140 info->mask.Buffer = NULL;
2141 RtlFreeUnicodeString( &info->path );
2144 HeapFree( GetProcessHeap(), 0, info->data );
2145 RtlLeaveCriticalSection( &info->cs );
2146 info->cs.DebugInfo->Spare[0] = 0;
2147 RtlDeleteCriticalSection( &info->cs );
2148 HeapFree( GetProcessHeap(), 0, info );
2154 WARN("Illegal handle %p\n", handle);
2155 SetLastError( ERROR_INVALID_HANDLE );
2164 /*************************************************************************
2165 * FindFirstFileA (KERNEL32.@)
2167 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
2169 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
2170 FindExSearchNameMatch, NULL, 0);
2173 /*************************************************************************
2174 * FindFirstFileExA (KERNEL32.@)
2176 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
2177 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
2178 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
2181 WIN32_FIND_DATAA *dataA;
2182 WIN32_FIND_DATAW dataW;
2185 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
2187 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
2188 if (handle == INVALID_HANDLE_VALUE) return handle;
2190 dataA = lpFindFileData;
2191 dataA->dwFileAttributes = dataW.dwFileAttributes;
2192 dataA->ftCreationTime = dataW.ftCreationTime;
2193 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
2194 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
2195 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
2196 dataA->nFileSizeLow = dataW.nFileSizeLow;
2197 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
2198 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
2199 sizeof(dataA->cAlternateFileName) );
2204 /*************************************************************************
2205 * FindFirstFileW (KERNEL32.@)
2207 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
2209 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
2210 FindExSearchNameMatch, NULL, 0);
2214 /*************************************************************************
2215 * FindNextFileA (KERNEL32.@)
2217 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
2219 WIN32_FIND_DATAW dataW;
2221 if (!FindNextFileW( handle, &dataW )) return FALSE;
2222 data->dwFileAttributes = dataW.dwFileAttributes;
2223 data->ftCreationTime = dataW.ftCreationTime;
2224 data->ftLastAccessTime = dataW.ftLastAccessTime;
2225 data->ftLastWriteTime = dataW.ftLastWriteTime;
2226 data->nFileSizeHigh = dataW.nFileSizeHigh;
2227 data->nFileSizeLow = dataW.nFileSizeLow;
2228 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
2229 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
2230 sizeof(data->cAlternateFileName) );
2235 /**************************************************************************
2236 * GetFileAttributesW (KERNEL32.@)
2238 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
2240 FILE_BASIC_INFORMATION info;
2241 UNICODE_STRING nt_name;
2242 OBJECT_ATTRIBUTES attr;
2245 TRACE("%s\n", debugstr_w(name));
2247 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2249 SetLastError( ERROR_PATH_NOT_FOUND );
2250 return INVALID_FILE_ATTRIBUTES;
2253 attr.Length = sizeof(attr);
2254 attr.RootDirectory = 0;
2255 attr.Attributes = OBJ_CASE_INSENSITIVE;
2256 attr.ObjectName = &nt_name;
2257 attr.SecurityDescriptor = NULL;
2258 attr.SecurityQualityOfService = NULL;
2260 status = NtQueryAttributesFile( &attr, &info );
2261 RtlFreeUnicodeString( &nt_name );
2263 if (status == STATUS_SUCCESS) return info.FileAttributes;
2265 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2266 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
2268 SetLastError( RtlNtStatusToDosError(status) );
2269 return INVALID_FILE_ATTRIBUTES;
2273 /**************************************************************************
2274 * GetFileAttributesA (KERNEL32.@)
2276 DWORD WINAPI GetFileAttributesA( LPCSTR name )
2280 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
2281 return GetFileAttributesW( nameW );
2285 /**************************************************************************
2286 * SetFileAttributesW (KERNEL32.@)
2288 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
2290 UNICODE_STRING nt_name;
2291 OBJECT_ATTRIBUTES attr;
2296 TRACE("%s %x\n", debugstr_w(name), attributes);
2298 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2300 SetLastError( ERROR_PATH_NOT_FOUND );
2304 attr.Length = sizeof(attr);
2305 attr.RootDirectory = 0;
2306 attr.Attributes = OBJ_CASE_INSENSITIVE;
2307 attr.ObjectName = &nt_name;
2308 attr.SecurityDescriptor = NULL;
2309 attr.SecurityQualityOfService = NULL;
2311 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2312 RtlFreeUnicodeString( &nt_name );
2314 if (status == STATUS_SUCCESS)
2316 FILE_BASIC_INFORMATION info;
2318 memset( &info, 0, sizeof(info) );
2319 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2320 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2324 if (status == STATUS_SUCCESS) return TRUE;
2325 SetLastError( RtlNtStatusToDosError(status) );
2330 /**************************************************************************
2331 * SetFileAttributesA (KERNEL32.@)
2333 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2337 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2338 return SetFileAttributesW( nameW, attributes );
2342 /**************************************************************************
2343 * GetFileAttributesExW (KERNEL32.@)
2345 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2347 FILE_NETWORK_OPEN_INFORMATION info;
2348 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2349 UNICODE_STRING nt_name;
2350 OBJECT_ATTRIBUTES attr;
2353 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2355 if (level != GetFileExInfoStandard)
2357 SetLastError( ERROR_INVALID_PARAMETER );
2361 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2363 SetLastError( ERROR_PATH_NOT_FOUND );
2367 attr.Length = sizeof(attr);
2368 attr.RootDirectory = 0;
2369 attr.Attributes = OBJ_CASE_INSENSITIVE;
2370 attr.ObjectName = &nt_name;
2371 attr.SecurityDescriptor = NULL;
2372 attr.SecurityQualityOfService = NULL;
2374 status = NtQueryFullAttributesFile( &attr, &info );
2375 RtlFreeUnicodeString( &nt_name );
2377 if (status != STATUS_SUCCESS)
2379 SetLastError( RtlNtStatusToDosError(status) );
2383 data->dwFileAttributes = info.FileAttributes;
2384 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2385 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2386 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2387 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2388 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2389 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2390 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2391 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2396 /**************************************************************************
2397 * GetFileAttributesExA (KERNEL32.@)
2399 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2403 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2404 return GetFileAttributesExW( nameW, level, ptr );
2408 /******************************************************************************
2409 * GetCompressedFileSizeW (KERNEL32.@)
2411 * Get the actual number of bytes used on disk.
2414 * Success: Low-order doubleword of number of bytes
2415 * Failure: INVALID_FILE_SIZE
2417 DWORD WINAPI GetCompressedFileSizeW(
2418 LPCWSTR name, /* [in] Pointer to name of file */
2419 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2421 UNICODE_STRING nt_name;
2422 OBJECT_ATTRIBUTES attr;
2426 DWORD ret = INVALID_FILE_SIZE;
2428 TRACE("%s %p\n", debugstr_w(name), size_high);
2430 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2432 SetLastError( ERROR_PATH_NOT_FOUND );
2433 return INVALID_FILE_SIZE;
2436 attr.Length = sizeof(attr);
2437 attr.RootDirectory = 0;
2438 attr.Attributes = OBJ_CASE_INSENSITIVE;
2439 attr.ObjectName = &nt_name;
2440 attr.SecurityDescriptor = NULL;
2441 attr.SecurityQualityOfService = NULL;
2443 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2444 RtlFreeUnicodeString( &nt_name );
2446 if (status == STATUS_SUCCESS)
2448 /* we don't support compressed files, simply return the file size */
2449 ret = GetFileSize( handle, size_high );
2452 else SetLastError( RtlNtStatusToDosError(status) );
2458 /******************************************************************************
2459 * GetCompressedFileSizeA (KERNEL32.@)
2461 * See GetCompressedFileSizeW.
2463 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2467 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2468 return GetCompressedFileSizeW( nameW, size_high );
2472 /***********************************************************************
2473 * OpenVxDHandle (KERNEL32.@)
2475 * This function is supposed to return the corresponding Ring 0
2476 * ("kernel") handle for a Ring 3 handle in Win9x.
2477 * Evidently, Wine will have problems with this. But we try anyway,
2480 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
2482 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
2483 return hHandleRing3;
2487 /****************************************************************************
2488 * DeviceIoControl (KERNEL32.@)
2490 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
2491 LPVOID lpvInBuffer, DWORD cbInBuffer,
2492 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
2493 LPDWORD lpcbBytesReturned,
2494 LPOVERLAPPED lpOverlapped)
2498 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2499 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
2500 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
2502 /* Check if this is a user defined control code for a VxD */
2504 if (HIWORD( dwIoControlCode ) == 0 && (GetVersion() & 0x80000000))
2506 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
2507 static DeviceIoProc (*vxd_get_proc)(HANDLE);
2508 DeviceIoProc proc = NULL;
2510 if (!vxd_get_proc) vxd_get_proc = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2511 "__wine_vxd_get_proc" );
2512 if (vxd_get_proc) proc = vxd_get_proc( hDevice );
2513 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
2514 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
2517 /* Not a VxD, let ntdll handle it */
2521 LPVOID cvalue = ((ULONG_PTR)lpOverlapped->hEvent & 1) ? NULL : lpOverlapped;
2522 lpOverlapped->Internal = STATUS_PENDING;
2523 lpOverlapped->InternalHigh = 0;
2524 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2525 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
2526 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2527 dwIoControlCode, lpvInBuffer, cbInBuffer,
2528 lpvOutBuffer, cbOutBuffer);
2530 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
2531 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2532 dwIoControlCode, lpvInBuffer, cbInBuffer,
2533 lpvOutBuffer, cbOutBuffer);
2534 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
2538 IO_STATUS_BLOCK iosb;
2540 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2541 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
2542 dwIoControlCode, lpvInBuffer, cbInBuffer,
2543 lpvOutBuffer, cbOutBuffer);
2545 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
2546 dwIoControlCode, lpvInBuffer, cbInBuffer,
2547 lpvOutBuffer, cbOutBuffer);
2548 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
2550 if (status) SetLastError( RtlNtStatusToDosError(status) );
2555 /***********************************************************************
2556 * OpenFile (KERNEL32.@)
2558 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2562 WORD filedatetime[2];
2564 if (!ofs) return HFILE_ERROR;
2566 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2567 ((mode & 0x3 )==OF_READ)?"OF_READ":
2568 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2569 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2570 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2571 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2572 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2573 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2574 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2575 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2576 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2577 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2578 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2579 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2580 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2581 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2582 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2583 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2587 ofs->cBytes = sizeof(OFSTRUCT);
2589 if (mode & OF_REOPEN) name = ofs->szPathName;
2591 if (!name) return HFILE_ERROR;
2593 TRACE("%s %04x\n", name, mode );
2595 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2596 Are there any cases where getting the path here is wrong?
2597 Uwe Bonnes 1997 Apr 2 */
2598 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2600 /* OF_PARSE simply fills the structure */
2602 if (mode & OF_PARSE)
2604 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2605 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2609 /* OF_CREATE is completely different from all other options, so
2612 if (mode & OF_CREATE)
2614 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2619 /* Now look for the file */
2621 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2624 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2626 if (mode & OF_DELETE)
2628 if (!DeleteFileA( ofs->szPathName )) goto error;
2629 TRACE("(%s): OF_DELETE return = OK\n", name);
2633 handle = LongToHandle(_lopen( ofs->szPathName, mode ));
2634 if (handle == INVALID_HANDLE_VALUE) goto error;
2636 GetFileTime( handle, NULL, NULL, &filetime );
2637 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2638 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2640 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2642 CloseHandle( handle );
2643 WARN("(%s): OF_VERIFY failed\n", name );
2644 /* FIXME: what error here? */
2645 SetLastError( ERROR_FILE_NOT_FOUND );
2649 ofs->Reserved1 = filedatetime[0];
2650 ofs->Reserved2 = filedatetime[1];
2652 TRACE("(%s): OK, return = %p\n", name, handle );
2653 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2655 CloseHandle( handle );
2658 return HandleToLong(handle);
2660 error: /* We get here if there was an error opening the file */
2661 ofs->nErrCode = GetLastError();
2662 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2667 /***********************************************************************
2668 * OpenFileById (KERNEL32.@)
2670 HANDLE WINAPI OpenFileById( HANDLE handle, LPFILE_ID_DESCRIPTOR id, DWORD access,
2671 DWORD share, LPSECURITY_ATTRIBUTES sec_attr, DWORD flags )
2675 OBJECT_ATTRIBUTES attr;
2678 UNICODE_STRING objectName;
2682 SetLastError( ERROR_INVALID_PARAMETER );
2683 return INVALID_HANDLE_VALUE;
2686 options = FILE_OPEN_BY_FILE_ID;
2687 if (flags & FILE_FLAG_BACKUP_SEMANTICS)
2688 options |= FILE_OPEN_FOR_BACKUP_INTENT;
2690 options |= FILE_NON_DIRECTORY_FILE;
2691 if (flags & FILE_FLAG_NO_BUFFERING) options |= FILE_NO_INTERMEDIATE_BUFFERING;
2692 if (!(flags & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
2693 if (flags & FILE_FLAG_RANDOM_ACCESS) options |= FILE_RANDOM_ACCESS;
2694 flags &= FILE_ATTRIBUTE_VALID_FLAGS;
2696 objectName.Length = sizeof(ULONGLONG);
2697 objectName.Buffer = (WCHAR *)&id->u.FileId;
2698 attr.Length = sizeof(attr);
2699 attr.RootDirectory = handle;
2700 attr.Attributes = 0;
2701 attr.ObjectName = &objectName;
2702 attr.SecurityDescriptor = sec_attr ? sec_attr->lpSecurityDescriptor : NULL;
2703 attr.SecurityQualityOfService = NULL;
2704 if (sec_attr && sec_attr->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
2706 status = NtCreateFile( &result, access, &attr, &io, NULL, flags,
2707 share, OPEN_EXISTING, options, NULL, 0 );
2708 if (status != STATUS_SUCCESS)
2710 SetLastError( RtlNtStatusToDosError( status ) );
2711 return INVALID_HANDLE_VALUE;
2717 /***********************************************************************
2718 * K32EnumDeviceDrivers (KERNEL32.@)
2720 BOOL WINAPI K32EnumDeviceDrivers(void **image_base, DWORD cb, DWORD *needed)
2722 FIXME("(%p, %d, %p): stub\n", image_base, cb, needed);
2730 /***********************************************************************
2731 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2733 DWORD WINAPI K32GetDeviceDriverBaseNameA(void *image_base, LPSTR base_name, DWORD size)
2735 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2737 if (base_name && size)
2738 base_name[0] = '\0';
2743 /***********************************************************************
2744 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2746 DWORD WINAPI K32GetDeviceDriverBaseNameW(void *image_base, LPWSTR base_name, DWORD size)
2748 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2750 if (base_name && size)
2751 base_name[0] = '\0';
2756 /***********************************************************************
2757 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2759 DWORD WINAPI K32GetDeviceDriverFileNameA(void *image_base, LPSTR file_name, DWORD size)
2761 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2763 if (file_name && size)
2764 file_name[0] = '\0';
2769 /***********************************************************************
2770 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2772 DWORD WINAPI K32GetDeviceDriverFileNameW(void *image_base, LPWSTR file_name, DWORD size)
2774 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2776 if (file_name && size)
2777 file_name[0] = '\0';