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 BYTE data[8192]; /* directory data */
67 #define FIND_FIRST_MAGIC 0xc0ffee11
69 static BOOL oem_file_apis;
71 static const WCHAR wildcardsW[] = { '*','?',0 };
73 /***********************************************************************
76 * Wrapper for CreateFile that takes OF_* mode flags.
78 static HANDLE create_file_OF( LPCSTR path, INT mode )
80 DWORD access, sharing, creation;
84 creation = CREATE_ALWAYS;
85 access = GENERIC_READ | GENERIC_WRITE;
89 creation = OPEN_EXISTING;
92 case OF_READ: access = GENERIC_READ; break;
93 case OF_WRITE: access = GENERIC_WRITE; break;
94 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
95 default: access = 0; break;
101 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
102 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
103 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
104 case OF_SHARE_DENY_NONE:
105 case OF_SHARE_COMPAT:
106 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
108 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
112 /***********************************************************************
115 * Check if a dir symlink should be returned by FindNextFile.
117 static BOOL check_dir_symlink( FIND_FIRST_INFO *info, const FILE_BOTH_DIR_INFORMATION *file_info )
120 ANSI_STRING unix_name;
121 struct stat st, parent_st;
125 str.MaximumLength = info->path.Length + sizeof(WCHAR) + file_info->FileNameLength;
126 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength ))) return TRUE;
127 memcpy( str.Buffer, info->path.Buffer, info->path.Length );
128 len = info->path.Length / sizeof(WCHAR);
129 if (!len || str.Buffer[len-1] != '\\') str.Buffer[len++] = '\\';
130 memcpy( str.Buffer + len, file_info->FileName, file_info->FileNameLength );
131 str.Length = len * sizeof(WCHAR) + file_info->FileNameLength;
133 unix_name.Buffer = NULL;
134 if (!wine_nt_to_unix_file_name( &str, &unix_name, OPEN_EXISTING, FALSE ) &&
135 !stat( unix_name.Buffer, &st ))
137 char *p = unix_name.Buffer + unix_name.Length - 1;
139 /* skip trailing slashes */
140 while (p > unix_name.Buffer && *p == '/') p--;
142 while (ret && p > unix_name.Buffer)
144 while (p > unix_name.Buffer && *p != '/') p--;
145 while (p > unix_name.Buffer && *p == '/') p--;
147 if (!stat( unix_name.Buffer, &parent_st ) &&
148 parent_st.st_dev == st.st_dev &&
149 parent_st.st_ino == st.st_ino)
151 WARN( "suppressing dir symlink %s pointing to parent %s\n",
152 debugstr_wn( str.Buffer, str.Length/sizeof(WCHAR) ),
153 debugstr_a( unix_name.Buffer ));
158 RtlFreeAnsiString( &unix_name );
159 RtlFreeUnicodeString( &str );
164 /***********************************************************************
167 * Set the DOS error code from errno.
169 void FILE_SetDosError(void)
171 int save_errno = errno; /* errno gets overwritten by printf */
173 TRACE("errno = %d %s\n", errno, strerror(errno));
177 SetLastError( ERROR_SHARING_VIOLATION );
180 SetLastError( ERROR_INVALID_HANDLE );
183 SetLastError( ERROR_HANDLE_DISK_FULL );
188 SetLastError( ERROR_ACCESS_DENIED );
191 SetLastError( ERROR_LOCK_VIOLATION );
194 SetLastError( ERROR_FILE_NOT_FOUND );
197 SetLastError( ERROR_CANNOT_MAKE );
201 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
204 SetLastError( ERROR_FILE_EXISTS );
208 SetLastError( ERROR_SEEK );
211 SetLastError( ERROR_DIR_NOT_EMPTY );
214 SetLastError( ERROR_BAD_FORMAT );
217 SetLastError( ERROR_PATH_NOT_FOUND );
220 SetLastError( ERROR_NOT_SAME_DEVICE );
223 WARN("unknown file error: %s\n", strerror(save_errno) );
224 SetLastError( ERROR_GEN_FAILURE );
231 /***********************************************************************
234 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
236 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
237 * there is no possibility for the function to do that twice, taking into
238 * account any called function.
240 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
243 UNICODE_STRING strW, *pstrW;
246 RtlInitAnsiString( &str, name );
247 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
249 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
251 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
252 if (status == STATUS_SUCCESS) return pstrW->Buffer;
254 if (status == STATUS_BUFFER_OVERFLOW)
255 SetLastError( ERROR_FILENAME_EXCED_RANGE );
257 SetLastError( RtlNtStatusToDosError(status) );
262 /***********************************************************************
265 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
267 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
271 if (srclen < 0) srclen = strlenW( src ) + 1;
273 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
275 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
280 /**************************************************************************
281 * SetFileApisToOEM (KERNEL32.@)
283 VOID WINAPI SetFileApisToOEM(void)
285 oem_file_apis = TRUE;
289 /**************************************************************************
290 * SetFileApisToANSI (KERNEL32.@)
292 VOID WINAPI SetFileApisToANSI(void)
294 oem_file_apis = FALSE;
298 /******************************************************************************
299 * AreFileApisANSI (KERNEL32.@)
301 * Determines if file functions are using ANSI
304 * TRUE: Set of file functions is using ANSI code page
305 * FALSE: Set of file functions is using OEM code page
307 BOOL WINAPI AreFileApisANSI(void)
309 return !oem_file_apis;
313 /**************************************************************************
314 * Operations on file handles *
315 **************************************************************************/
317 /******************************************************************
318 * FILE_ReadWriteApc (internal)
320 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG reserved)
322 LPOVERLAPPED_COMPLETION_ROUTINE cr = apc_user;
324 cr(RtlNtStatusToDosError(io_status->u.Status), io_status->Information, (LPOVERLAPPED)io_status);
328 /***********************************************************************
329 * ReadFileEx (KERNEL32.@)
331 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
332 LPOVERLAPPED overlapped,
333 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
335 LARGE_INTEGER offset;
337 PIO_STATUS_BLOCK io_status;
339 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
343 SetLastError(ERROR_INVALID_PARAMETER);
347 offset.u.LowPart = overlapped->u.s.Offset;
348 offset.u.HighPart = overlapped->u.s.OffsetHigh;
349 io_status = (PIO_STATUS_BLOCK)overlapped;
350 io_status->u.Status = STATUS_PENDING;
351 io_status->Information = 0;
353 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
354 io_status, buffer, bytesToRead, &offset, NULL);
356 if (status && status != STATUS_PENDING)
358 SetLastError( RtlNtStatusToDosError(status) );
365 /***********************************************************************
366 * ReadFileScatter (KERNEL32.@)
368 BOOL WINAPI ReadFileScatter( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
369 LPDWORD reserved, LPOVERLAPPED overlapped )
371 PIO_STATUS_BLOCK io_status;
372 LARGE_INTEGER offset;
375 TRACE( "(%p %p %u %p)\n", file, segments, count, overlapped );
377 offset.u.LowPart = overlapped->u.s.Offset;
378 offset.u.HighPart = overlapped->u.s.OffsetHigh;
379 io_status = (PIO_STATUS_BLOCK)overlapped;
380 io_status->u.Status = STATUS_PENDING;
381 io_status->Information = 0;
383 status = NtReadFileScatter( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
384 if (status) SetLastError( RtlNtStatusToDosError(status) );
389 /***********************************************************************
390 * ReadFile (KERNEL32.@)
392 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
393 LPDWORD bytesRead, LPOVERLAPPED overlapped )
395 LARGE_INTEGER offset;
396 PLARGE_INTEGER poffset = NULL;
397 IO_STATUS_BLOCK iosb;
398 PIO_STATUS_BLOCK io_status = &iosb;
401 LPVOID cvalue = NULL;
403 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToRead,
404 bytesRead, overlapped );
406 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
407 if (!bytesToRead) return TRUE;
409 if (is_console_handle(hFile))
412 if (!ReadConsoleA(hFile, buffer, bytesToRead, &conread, NULL) ||
413 !GetConsoleMode(hFile, &mode))
415 /* ctrl-Z (26) means end of file on window (if at beginning of buffer)
416 * but Unix uses ctrl-D (4), and ctrl-Z is a bad idea on Unix :-/
417 * So map both ctrl-D ctrl-Z to EOF.
419 if ((mode & ENABLE_PROCESSED_INPUT) && conread > 0 &&
420 (((char*)buffer)[0] == 26 || ((char*)buffer)[0] == 4))
424 if (bytesRead) *bytesRead = conread;
428 if (overlapped != NULL)
430 offset.u.LowPart = overlapped->u.s.Offset;
431 offset.u.HighPart = overlapped->u.s.OffsetHigh;
433 hEvent = overlapped->hEvent;
434 io_status = (PIO_STATUS_BLOCK)overlapped;
435 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
437 io_status->u.Status = STATUS_PENDING;
438 io_status->Information = 0;
440 status = NtReadFile(hFile, hEvent, NULL, cvalue, io_status, buffer, bytesToRead, poffset, NULL);
442 if (status == STATUS_PENDING && !overlapped)
444 WaitForSingleObject( hFile, INFINITE );
445 status = io_status->u.Status;
448 if (status != STATUS_PENDING && bytesRead)
449 *bytesRead = io_status->Information;
451 if (status && status != STATUS_END_OF_FILE && status != STATUS_TIMEOUT)
453 SetLastError( RtlNtStatusToDosError(status) );
460 /***********************************************************************
461 * WriteFileEx (KERNEL32.@)
463 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
464 LPOVERLAPPED overlapped,
465 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
467 LARGE_INTEGER offset;
469 PIO_STATUS_BLOCK io_status;
471 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
473 if (overlapped == NULL)
475 SetLastError(ERROR_INVALID_PARAMETER);
478 offset.u.LowPart = overlapped->u.s.Offset;
479 offset.u.HighPart = overlapped->u.s.OffsetHigh;
481 io_status = (PIO_STATUS_BLOCK)overlapped;
482 io_status->u.Status = STATUS_PENDING;
483 io_status->Information = 0;
485 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
486 io_status, buffer, bytesToWrite, &offset, NULL);
488 if (status && status != STATUS_PENDING)
490 SetLastError( RtlNtStatusToDosError(status) );
497 /***********************************************************************
498 * WriteFileGather (KERNEL32.@)
500 BOOL WINAPI WriteFileGather( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
501 LPDWORD reserved, LPOVERLAPPED overlapped )
503 PIO_STATUS_BLOCK io_status;
504 LARGE_INTEGER offset;
507 TRACE( "%p %p %u %p\n", file, segments, count, overlapped );
509 offset.u.LowPart = overlapped->u.s.Offset;
510 offset.u.HighPart = overlapped->u.s.OffsetHigh;
511 io_status = (PIO_STATUS_BLOCK)overlapped;
512 io_status->u.Status = STATUS_PENDING;
513 io_status->Information = 0;
515 status = NtWriteFileGather( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
516 if (status) SetLastError( RtlNtStatusToDosError(status) );
521 /***********************************************************************
522 * WriteFile (KERNEL32.@)
524 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
525 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
527 HANDLE hEvent = NULL;
528 LARGE_INTEGER offset;
529 PLARGE_INTEGER poffset = NULL;
531 IO_STATUS_BLOCK iosb;
532 PIO_STATUS_BLOCK piosb = &iosb;
533 LPVOID cvalue = NULL;
535 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
537 if (is_console_handle(hFile))
538 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
542 offset.u.LowPart = overlapped->u.s.Offset;
543 offset.u.HighPart = overlapped->u.s.OffsetHigh;
545 hEvent = overlapped->hEvent;
546 piosb = (PIO_STATUS_BLOCK)overlapped;
547 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
549 piosb->u.Status = STATUS_PENDING;
550 piosb->Information = 0;
552 status = NtWriteFile(hFile, hEvent, NULL, cvalue, piosb,
553 buffer, bytesToWrite, poffset, NULL);
555 if (status == STATUS_PENDING && !overlapped)
557 WaitForSingleObject( hFile, INFINITE );
558 status = piosb->u.Status;
561 if (status != STATUS_PENDING && bytesWritten)
562 *bytesWritten = piosb->Information;
564 if (status && status != STATUS_TIMEOUT)
566 SetLastError( RtlNtStatusToDosError(status) );
573 /***********************************************************************
574 * GetOverlappedResult (KERNEL32.@)
576 * Check the result of an Asynchronous data transfer from a file.
579 * HANDLE hFile [in] handle of file to check on
580 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
581 * LPDWORD lpTransferred [in/out] number of bytes transferred
582 * BOOL bWait [in] wait for the transfer to complete ?
588 * If successful (and relevant) lpTransferred will hold the number of
589 * bytes transferred during the async operation.
591 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
592 LPDWORD lpTransferred, BOOL bWait)
596 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
598 status = lpOverlapped->Internal;
599 if (status == STATUS_PENDING)
603 SetLastError( ERROR_IO_INCOMPLETE );
607 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : hFile,
608 INFINITE ) == WAIT_FAILED)
610 status = lpOverlapped->Internal;
613 *lpTransferred = lpOverlapped->InternalHigh;
615 if (status) SetLastError( RtlNtStatusToDosError(status) );
619 /***********************************************************************
620 * CancelIoEx (KERNEL32.@)
622 * Cancels pending I/O operations on a file given the overlapped used.
625 * handle [I] File handle.
626 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
630 * Failure: FALSE, check GetLastError().
632 BOOL WINAPI CancelIoEx(HANDLE handle, LPOVERLAPPED lpOverlapped)
634 IO_STATUS_BLOCK io_status;
636 NtCancelIoFileEx(handle, (PIO_STATUS_BLOCK) lpOverlapped, &io_status);
637 if (io_status.u.Status)
639 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
645 /***********************************************************************
646 * CancelIo (KERNEL32.@)
648 * Cancels pending I/O operations initiated by the current thread on a file.
651 * handle [I] File handle.
655 * Failure: FALSE, check GetLastError().
657 BOOL WINAPI CancelIo(HANDLE handle)
659 IO_STATUS_BLOCK io_status;
661 NtCancelIoFile(handle, &io_status);
662 if (io_status.u.Status)
664 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
670 /***********************************************************************
671 * _hread (KERNEL32.@)
673 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
675 return _lread( hFile, buffer, count );
679 /***********************************************************************
680 * _hwrite (KERNEL32.@)
682 * experimentation yields that _lwrite:
683 * o truncates the file at the current position with
685 * o returns 0 on a 0 length write
686 * o works with console handles
689 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
693 TRACE("%d %p %d\n", handle, buffer, count );
697 /* Expand or truncate at current position */
698 if (!SetEndOfFile( LongToHandle(handle) )) return HFILE_ERROR;
701 if (!WriteFile( LongToHandle(handle), buffer, count, &result, NULL ))
707 /***********************************************************************
708 * _lclose (KERNEL32.@)
710 HFILE WINAPI _lclose( HFILE hFile )
712 TRACE("handle %d\n", hFile );
713 return CloseHandle( LongToHandle(hFile) ) ? 0 : HFILE_ERROR;
717 /***********************************************************************
718 * _lcreat (KERNEL32.@)
720 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
724 /* Mask off all flags not explicitly allowed by the doc */
725 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
726 TRACE("%s %02x\n", path, attr );
727 hfile = CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
728 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
729 CREATE_ALWAYS, attr, 0 );
730 return HandleToLong(hfile);
734 /***********************************************************************
735 * _lopen (KERNEL32.@)
737 HFILE WINAPI _lopen( LPCSTR path, INT mode )
741 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
742 hfile = create_file_OF( path, mode & ~OF_CREATE );
743 return HandleToLong(hfile);
746 /***********************************************************************
747 * _lread (KERNEL32.@)
749 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
752 if (!ReadFile( LongToHandle(handle), buffer, count, &result, NULL ))
758 /***********************************************************************
759 * _llseek (KERNEL32.@)
761 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
763 return SetFilePointer( LongToHandle(hFile), lOffset, NULL, nOrigin );
767 /***********************************************************************
768 * _lwrite (KERNEL32.@)
770 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
772 return (UINT)_hwrite( hFile, buffer, (LONG)count );
776 /***********************************************************************
777 * FlushFileBuffers (KERNEL32.@)
779 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
782 IO_STATUS_BLOCK ioblk;
784 if (is_console_handle( hFile ))
786 /* this will fail (as expected) for an output handle */
787 return FlushConsoleInputBuffer( hFile );
789 nts = NtFlushBuffersFile( hFile, &ioblk );
790 if (nts != STATUS_SUCCESS)
792 SetLastError( RtlNtStatusToDosError( nts ) );
800 /***********************************************************************
801 * GetFileType (KERNEL32.@)
803 DWORD WINAPI GetFileType( HANDLE hFile )
805 FILE_FS_DEVICE_INFORMATION info;
809 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
811 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
812 if (status != STATUS_SUCCESS)
814 SetLastError( RtlNtStatusToDosError(status) );
815 return FILE_TYPE_UNKNOWN;
818 switch(info.DeviceType)
820 case FILE_DEVICE_NULL:
821 case FILE_DEVICE_SERIAL_PORT:
822 case FILE_DEVICE_PARALLEL_PORT:
823 case FILE_DEVICE_TAPE:
824 case FILE_DEVICE_UNKNOWN:
825 return FILE_TYPE_CHAR;
826 case FILE_DEVICE_NAMED_PIPE:
827 return FILE_TYPE_PIPE;
829 return FILE_TYPE_DISK;
834 /***********************************************************************
835 * GetFileInformationByHandle (KERNEL32.@)
837 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
839 FILE_ALL_INFORMATION all_info;
843 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
844 if (status == STATUS_BUFFER_OVERFLOW) status = STATUS_SUCCESS;
845 if (status == STATUS_SUCCESS)
847 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
848 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
849 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
850 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
851 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
852 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
853 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
854 info->dwVolumeSerialNumber = 0; /* FIXME */
855 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
856 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
857 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
858 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
859 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
862 SetLastError( RtlNtStatusToDosError(status) );
867 /***********************************************************************
868 * GetFileInformationByHandleEx (KERNEL32.@)
870 BOOL WINAPI GetFileInformationByHandleEx( HANDLE handle, FILE_INFO_BY_HANDLE_CLASS class,
871 LPVOID info, DWORD size )
879 case FileStandardInfo:
881 case FileDispositionInfo:
882 case FileAllocationInfo:
883 case FileEndOfFileInfo:
885 case FileCompressionInfo:
886 case FileAttributeTagInfo:
887 case FileIoPriorityHintInfo:
888 case FileRemoteProtocolInfo:
889 case FileFullDirectoryInfo:
890 case FileFullDirectoryRestartInfo:
891 case FileStorageInfo:
892 case FileAlignmentInfo:
894 case FileIdExtdDirectoryInfo:
895 case FileIdExtdDirectoryRestartInfo:
896 FIXME( "%p, %u, %p, %u\n", handle, class, info, size );
897 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
901 status = NtQueryInformationFile( handle, &io, info, size, FileNameInformation );
902 if (status != STATUS_SUCCESS)
904 SetLastError( RtlNtStatusToDosError( status ) );
909 case FileIdBothDirectoryRestartInfo:
910 case FileIdBothDirectoryInfo:
911 status = NtQueryDirectoryFile( handle, NULL, NULL, NULL, &io, info, size,
912 FileIdBothDirectoryInformation, FALSE, NULL,
913 (class == FileIdBothDirectoryRestartInfo) );
914 if (status != STATUS_SUCCESS)
916 SetLastError( RtlNtStatusToDosError( status ) );
922 SetLastError( ERROR_INVALID_PARAMETER );
928 /***********************************************************************
929 * GetFileSize (KERNEL32.@)
931 * Retrieve the size of a file.
934 * hFile [I] File to retrieve size of.
935 * filesizehigh [O] On return, the high bits of the file size.
938 * Success: The low bits of the file size.
939 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
940 * check GetLastError() for values other than ERROR_SUCCESS.
942 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
945 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
946 if (filesizehigh) *filesizehigh = size.u.HighPart;
947 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
948 return size.u.LowPart;
952 /***********************************************************************
953 * GetFileSizeEx (KERNEL32.@)
955 * Retrieve the size of a file.
958 * hFile [I] File to retrieve size of.
959 * lpFileSIze [O] On return, the size of the file.
963 * Failure: FALSE, check GetLastError().
965 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
967 FILE_STANDARD_INFORMATION info;
971 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileStandardInformation );
972 if (status == STATUS_SUCCESS)
974 *lpFileSize = info.EndOfFile;
977 SetLastError( RtlNtStatusToDosError(status) );
982 /**************************************************************************
983 * SetEndOfFile (KERNEL32.@)
985 * Sets the current position as the end of the file.
988 * hFile [I] File handle.
992 * Failure: FALSE, check GetLastError().
994 BOOL WINAPI SetEndOfFile( HANDLE hFile )
996 FILE_POSITION_INFORMATION pos;
997 FILE_END_OF_FILE_INFORMATION eof;
1001 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
1002 if (status == STATUS_SUCCESS)
1004 eof.EndOfFile = pos.CurrentByteOffset;
1005 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
1007 if (status == STATUS_SUCCESS) return TRUE;
1008 SetLastError( RtlNtStatusToDosError(status) );
1013 /***********************************************************************
1014 * SetFilePointer (KERNEL32.@)
1016 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
1018 LARGE_INTEGER dist, newpos;
1022 dist.u.LowPart = distance;
1023 dist.u.HighPart = *highword;
1025 else dist.QuadPart = distance;
1027 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
1029 if (highword) *highword = newpos.u.HighPart;
1030 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
1031 return newpos.u.LowPart;
1035 /***********************************************************************
1036 * SetFilePointerEx (KERNEL32.@)
1038 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
1039 LARGE_INTEGER *newpos, DWORD method )
1043 FILE_POSITION_INFORMATION info;
1048 pos = distance.QuadPart;
1051 if (NtQueryInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1053 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
1057 FILE_END_OF_FILE_INFORMATION eof;
1058 if (NtQueryInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
1060 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
1064 SetLastError( ERROR_INVALID_PARAMETER );
1070 SetLastError( ERROR_NEGATIVE_SEEK );
1074 info.CurrentByteOffset.QuadPart = pos;
1075 if (NtSetInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1077 if (newpos) newpos->QuadPart = pos;
1081 SetLastError( RtlNtStatusToDosError(io.u.Status) );
1085 /***********************************************************************
1086 * SetFileValidData (KERNEL32.@)
1088 BOOL WINAPI SetFileValidData( HANDLE hFile, LONGLONG ValidDataLength )
1090 FILE_VALID_DATA_LENGTH_INFORMATION info;
1094 info.ValidDataLength.QuadPart = ValidDataLength;
1095 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileValidDataLengthInformation );
1097 if (status == STATUS_SUCCESS) return TRUE;
1098 SetLastError( RtlNtStatusToDosError(status) );
1102 /***********************************************************************
1103 * GetFileTime (KERNEL32.@)
1105 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
1106 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1108 FILE_BASIC_INFORMATION info;
1112 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1113 if (status == STATUS_SUCCESS)
1117 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
1118 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
1120 if (lpLastAccessTime)
1122 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
1123 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
1125 if (lpLastWriteTime)
1127 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
1128 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1132 SetLastError( RtlNtStatusToDosError(status) );
1137 /***********************************************************************
1138 * SetFileTime (KERNEL32.@)
1140 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1141 const FILETIME *atime, const FILETIME *mtime )
1143 FILE_BASIC_INFORMATION info;
1147 memset( &info, 0, sizeof(info) );
1150 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1151 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1155 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1156 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1160 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1161 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1164 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1165 if (status == STATUS_SUCCESS) return TRUE;
1166 SetLastError( RtlNtStatusToDosError(status) );
1171 /**************************************************************************
1172 * LockFile (KERNEL32.@)
1174 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1175 DWORD count_low, DWORD count_high )
1178 LARGE_INTEGER count, offset;
1180 TRACE( "%p %x%08x %x%08x\n",
1181 hFile, offset_high, offset_low, count_high, count_low );
1183 count.u.LowPart = count_low;
1184 count.u.HighPart = count_high;
1185 offset.u.LowPart = offset_low;
1186 offset.u.HighPart = offset_high;
1188 status = NtLockFile( hFile, 0, NULL, NULL,
1189 NULL, &offset, &count, NULL, TRUE, TRUE );
1191 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1196 /**************************************************************************
1197 * LockFileEx [KERNEL32.@]
1199 * Locks a byte range within an open file for shared or exclusive access.
1206 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1208 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1209 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1212 LARGE_INTEGER count, offset;
1213 LPVOID cvalue = NULL;
1217 SetLastError( ERROR_INVALID_PARAMETER );
1221 TRACE( "%p %x%08x %x%08x flags %x\n",
1222 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1223 count_high, count_low, flags );
1225 count.u.LowPart = count_low;
1226 count.u.HighPart = count_high;
1227 offset.u.LowPart = overlapped->u.s.Offset;
1228 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1230 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1232 status = NtLockFile( hFile, overlapped->hEvent, NULL, cvalue,
1233 NULL, &offset, &count, NULL,
1234 flags & LOCKFILE_FAIL_IMMEDIATELY,
1235 flags & LOCKFILE_EXCLUSIVE_LOCK );
1237 if (status) SetLastError( RtlNtStatusToDosError(status) );
1242 /**************************************************************************
1243 * UnlockFile (KERNEL32.@)
1245 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1246 DWORD count_low, DWORD count_high )
1249 LARGE_INTEGER count, offset;
1251 count.u.LowPart = count_low;
1252 count.u.HighPart = count_high;
1253 offset.u.LowPart = offset_low;
1254 offset.u.HighPart = offset_high;
1256 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1257 if (status) SetLastError( RtlNtStatusToDosError(status) );
1262 /**************************************************************************
1263 * UnlockFileEx (KERNEL32.@)
1265 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1266 LPOVERLAPPED overlapped )
1270 SetLastError( ERROR_INVALID_PARAMETER );
1273 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1275 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1279 /*************************************************************************
1280 * SetHandleCount (KERNEL32.@)
1282 UINT WINAPI SetHandleCount( UINT count )
1288 /**************************************************************************
1289 * Operations on file names *
1290 **************************************************************************/
1293 /*************************************************************************
1294 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1296 * Creates or opens an object, and returns a handle that can be used to
1297 * access that object.
1301 * filename [in] pointer to filename to be accessed
1302 * access [in] access mode requested
1303 * sharing [in] share mode
1304 * sa [in] pointer to security attributes
1305 * creation [in] how to create the file
1306 * attributes [in] attributes for newly created file
1307 * template [in] handle to file with extended attributes to copy
1310 * Success: Open handle to specified file
1311 * Failure: INVALID_HANDLE_VALUE
1313 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1314 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1315 DWORD attributes, HANDLE template )
1319 OBJECT_ATTRIBUTES attr;
1320 UNICODE_STRING nameW;
1324 const WCHAR *vxd_name = NULL;
1325 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1326 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1327 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1328 SECURITY_QUALITY_OF_SERVICE qos;
1330 static const UINT nt_disposition[5] =
1332 FILE_CREATE, /* CREATE_NEW */
1333 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1334 FILE_OPEN, /* OPEN_EXISTING */
1335 FILE_OPEN_IF, /* OPEN_ALWAYS */
1336 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1342 if (!filename || !filename[0])
1344 SetLastError( ERROR_PATH_NOT_FOUND );
1345 return INVALID_HANDLE_VALUE;
1348 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1349 (access & GENERIC_READ)?"GENERIC_READ ":"",
1350 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1351 (access & GENERIC_EXECUTE)?"GENERIC_EXECUTE ":"",
1352 (!access)?"QUERY_ACCESS ":"",
1353 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1354 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1355 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1356 creation, attributes);
1358 /* Open a console for CONIN$ or CONOUT$ */
1360 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1362 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle),
1363 creation ? OPEN_EXISTING : 0);
1364 if (ret == INVALID_HANDLE_VALUE) SetLastError(ERROR_INVALID_PARAMETER);
1368 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1370 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1371 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1373 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1374 !strncmpiW( filename + 4, pipeW, 5 ) ||
1375 !strncmpiW( filename + 4, mailslotW, 9 ))
1379 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1381 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1383 else if (GetVersion() & 0x80000000)
1385 vxd_name = filename + 4;
1386 if (!creation) creation = OPEN_EXISTING;
1389 else dosdev = RtlIsDosDeviceName_U( filename );
1393 static const WCHAR conW[] = {'C','O','N'};
1395 if (LOWORD(dosdev) == sizeof(conW) &&
1396 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1398 switch (access & (GENERIC_READ|GENERIC_WRITE))
1401 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1404 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1407 SetLastError( ERROR_FILE_NOT_FOUND );
1408 return INVALID_HANDLE_VALUE;
1413 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1415 SetLastError( ERROR_INVALID_PARAMETER );
1416 return INVALID_HANDLE_VALUE;
1419 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1421 SetLastError( ERROR_PATH_NOT_FOUND );
1422 return INVALID_HANDLE_VALUE;
1425 /* now call NtCreateFile */
1428 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1429 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1431 options |= FILE_NON_DIRECTORY_FILE;
1432 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1434 options |= FILE_DELETE_ON_CLOSE;
1437 if (attributes & FILE_FLAG_NO_BUFFERING)
1438 options |= FILE_NO_INTERMEDIATE_BUFFERING;
1439 if (!(attributes & FILE_FLAG_OVERLAPPED))
1440 options |= FILE_SYNCHRONOUS_IO_NONALERT;
1441 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1442 options |= FILE_RANDOM_ACCESS;
1443 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1445 attr.Length = sizeof(attr);
1446 attr.RootDirectory = 0;
1447 attr.Attributes = OBJ_CASE_INSENSITIVE;
1448 attr.ObjectName = &nameW;
1449 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1450 if (attributes & SECURITY_SQOS_PRESENT)
1452 qos.Length = sizeof(qos);
1453 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
1454 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
1455 qos.EffectiveOnly = (attributes & SECURITY_EFFECTIVE_ONLY) != 0;
1456 attr.SecurityQualityOfService = &qos;
1459 attr.SecurityQualityOfService = NULL;
1461 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1463 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1464 sharing, nt_disposition[creation - CREATE_NEW],
1468 if (vxd_name && vxd_name[0])
1470 static HANDLE (*vxd_open)(LPCWSTR,DWORD,SECURITY_ATTRIBUTES*);
1471 if (!vxd_open) vxd_open = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1472 "__wine_vxd_open" );
1473 if (vxd_open && (ret = vxd_open( vxd_name, access, sa ))) goto done;
1476 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1477 ret = INVALID_HANDLE_VALUE;
1479 /* In the case file creation was rejected due to CREATE_NEW flag
1480 * was specified and file with that name already exists, correct
1481 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1482 * Note: RtlNtStatusToDosError is not the subject to blame here.
1484 if (status == STATUS_OBJECT_NAME_COLLISION)
1485 SetLastError( ERROR_FILE_EXISTS );
1487 SetLastError( RtlNtStatusToDosError(status) );
1491 if ((creation == CREATE_ALWAYS && io.Information == FILE_OVERWRITTEN) ||
1492 (creation == OPEN_ALWAYS && io.Information == FILE_OPENED))
1493 SetLastError( ERROR_ALREADY_EXISTS );
1497 RtlFreeUnicodeString( &nameW );
1500 if (!ret) ret = INVALID_HANDLE_VALUE;
1501 TRACE("returning %p\n", ret);
1507 /*************************************************************************
1508 * CreateFileA (KERNEL32.@)
1512 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1513 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1514 DWORD attributes, HANDLE template)
1518 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1519 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1523 /***********************************************************************
1524 * DeleteFileW (KERNEL32.@)
1529 * path [I] Path to the file to delete.
1533 * Failure: FALSE, check GetLastError().
1535 BOOL WINAPI DeleteFileW( LPCWSTR path )
1537 UNICODE_STRING nameW;
1538 OBJECT_ATTRIBUTES attr;
1543 TRACE("%s\n", debugstr_w(path) );
1545 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1547 SetLastError( ERROR_PATH_NOT_FOUND );
1551 attr.Length = sizeof(attr);
1552 attr.RootDirectory = 0;
1553 attr.Attributes = OBJ_CASE_INSENSITIVE;
1554 attr.ObjectName = &nameW;
1555 attr.SecurityDescriptor = NULL;
1556 attr.SecurityQualityOfService = NULL;
1558 status = NtCreateFile(&hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
1559 &attr, &io, NULL, 0,
1560 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1561 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0);
1562 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1564 RtlFreeUnicodeString( &nameW );
1567 SetLastError( RtlNtStatusToDosError(status) );
1574 /***********************************************************************
1575 * DeleteFileA (KERNEL32.@)
1579 BOOL WINAPI DeleteFileA( LPCSTR path )
1583 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1584 return DeleteFileW( pathW );
1588 /**************************************************************************
1589 * ReplaceFileW (KERNEL32.@)
1590 * ReplaceFile (KERNEL32.@)
1592 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName, LPCWSTR lpReplacementFileName,
1593 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1594 LPVOID lpExclude, LPVOID lpReserved)
1596 UNICODE_STRING nt_replaced_name, nt_replacement_name;
1597 ANSI_STRING unix_replaced_name, unix_replacement_name, unix_backup_name;
1598 HANDLE hReplaced = NULL, hReplacement = NULL, hBackup = NULL;
1599 DWORD error = ERROR_SUCCESS;
1600 UINT replaced_flags;
1604 OBJECT_ATTRIBUTES attr;
1606 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName),
1607 debugstr_w(lpReplacementFileName), debugstr_w(lpBackupFileName),
1608 dwReplaceFlags, lpExclude, lpReserved);
1611 FIXME("Ignoring flags %x\n", dwReplaceFlags);
1613 /* First two arguments are mandatory */
1614 if (!lpReplacedFileName || !lpReplacementFileName)
1616 SetLastError(ERROR_INVALID_PARAMETER);
1620 unix_replaced_name.Buffer = NULL;
1621 unix_replacement_name.Buffer = NULL;
1622 unix_backup_name.Buffer = NULL;
1624 attr.Length = sizeof(attr);
1625 attr.RootDirectory = 0;
1626 attr.Attributes = OBJ_CASE_INSENSITIVE;
1627 attr.ObjectName = NULL;
1628 attr.SecurityDescriptor = NULL;
1629 attr.SecurityQualityOfService = NULL;
1631 /* Open the "replaced" file for reading and writing */
1632 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName, &nt_replaced_name, NULL, NULL)))
1634 error = ERROR_PATH_NOT_FOUND;
1637 replaced_flags = lpBackupFileName ? FILE_OPEN : FILE_OPEN_IF;
1638 attr.ObjectName = &nt_replaced_name;
1639 status = NtOpenFile(&hReplaced, GENERIC_READ|GENERIC_WRITE|DELETE|SYNCHRONIZE,
1641 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1642 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1643 if (status == STATUS_SUCCESS)
1644 status = wine_nt_to_unix_file_name(&nt_replaced_name, &unix_replaced_name, replaced_flags, FALSE);
1645 RtlFreeUnicodeString(&nt_replaced_name);
1646 if (status != STATUS_SUCCESS)
1648 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1649 error = ERROR_FILE_NOT_FOUND;
1651 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1656 * Open the replacement file for reading, writing, and deleting
1657 * (writing and deleting are needed when finished)
1659 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName, &nt_replacement_name, NULL, NULL)))
1661 error = ERROR_PATH_NOT_FOUND;
1664 attr.ObjectName = &nt_replacement_name;
1665 status = NtOpenFile(&hReplacement,
1666 GENERIC_READ|GENERIC_WRITE|DELETE|WRITE_DAC|SYNCHRONIZE,
1668 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1669 if (status == STATUS_SUCCESS)
1670 status = wine_nt_to_unix_file_name(&nt_replacement_name, &unix_replacement_name, FILE_OPEN, FALSE);
1671 RtlFreeUnicodeString(&nt_replacement_name);
1672 if (status != STATUS_SUCCESS)
1674 error = RtlNtStatusToDosError(status);
1678 /* If the user wants a backup then that needs to be performed first */
1679 if (lpBackupFileName)
1681 UNICODE_STRING nt_backup_name;
1682 FILE_BASIC_INFORMATION replaced_info;
1684 /* Obtain the file attributes from the "replaced" file */
1685 status = NtQueryInformationFile(hReplaced, &io, &replaced_info,
1686 sizeof(replaced_info),
1687 FileBasicInformation);
1688 if (status != STATUS_SUCCESS)
1690 error = RtlNtStatusToDosError(status);
1694 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName, &nt_backup_name, NULL, NULL)))
1696 error = ERROR_PATH_NOT_FOUND;
1699 attr.ObjectName = &nt_backup_name;
1700 /* Open the backup with permissions to write over it */
1701 status = NtCreateFile(&hBackup, GENERIC_WRITE,
1702 &attr, &io, NULL, replaced_info.FileAttributes,
1703 FILE_SHARE_WRITE, FILE_OPEN_IF,
1704 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE,
1706 if (status == STATUS_SUCCESS)
1707 status = wine_nt_to_unix_file_name(&nt_backup_name, &unix_backup_name, FILE_OPEN_IF, FALSE);
1708 RtlFreeUnicodeString(&nt_backup_name);
1709 if (status != STATUS_SUCCESS)
1711 error = RtlNtStatusToDosError(status);
1715 /* If an existing backup exists then copy over it */
1716 if (rename(unix_replaced_name.Buffer, unix_backup_name.Buffer) == -1)
1718 error = ERROR_UNABLE_TO_REMOVE_REPLACED; /* is this correct? */
1724 * Now that the backup has been performed (if requested), copy the replacement
1727 if (rename(unix_replacement_name.Buffer, unix_replaced_name.Buffer) == -1)
1729 if (errno == EACCES)
1731 /* Inappropriate permissions on "replaced", rename will fail */
1732 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1735 /* on failure we need to indicate whether a backup was made */
1736 if (!lpBackupFileName)
1737 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT;
1739 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT_2;
1745 /* Perform resource cleanup */
1747 if (hBackup) CloseHandle(hBackup);
1748 if (hReplaced) CloseHandle(hReplaced);
1749 if (hReplacement) CloseHandle(hReplacement);
1750 RtlFreeAnsiString(&unix_backup_name);
1751 RtlFreeAnsiString(&unix_replacement_name);
1752 RtlFreeAnsiString(&unix_replaced_name);
1754 /* If there was an error, set the error code */
1756 SetLastError(error);
1761 /**************************************************************************
1762 * ReplaceFileA (KERNEL32.@)
1764 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1765 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1766 LPVOID lpExclude, LPVOID lpReserved)
1768 WCHAR *replacedW, *replacementW, *backupW = NULL;
1771 /* This function only makes sense when the first two parameters are defined */
1772 if (!lpReplacedFileName || !(replacedW = FILE_name_AtoW( lpReplacedFileName, TRUE )))
1774 SetLastError(ERROR_INVALID_PARAMETER);
1777 if (!lpReplacementFileName || !(replacementW = FILE_name_AtoW( lpReplacementFileName, TRUE )))
1779 HeapFree( GetProcessHeap(), 0, replacedW );
1780 SetLastError(ERROR_INVALID_PARAMETER);
1783 /* The backup parameter, however, is optional */
1784 if (lpBackupFileName)
1786 if (!(backupW = FILE_name_AtoW( lpBackupFileName, TRUE )))
1788 HeapFree( GetProcessHeap(), 0, replacedW );
1789 HeapFree( GetProcessHeap(), 0, replacementW );
1790 SetLastError(ERROR_INVALID_PARAMETER);
1794 ret = ReplaceFileW( replacedW, replacementW, backupW, dwReplaceFlags, lpExclude, lpReserved );
1795 HeapFree( GetProcessHeap(), 0, replacedW );
1796 HeapFree( GetProcessHeap(), 0, replacementW );
1797 HeapFree( GetProcessHeap(), 0, backupW );
1802 /*************************************************************************
1803 * FindFirstFileExW (KERNEL32.@)
1805 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1806 * results as FindExSearchNameMatch
1808 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1809 LPVOID data, FINDEX_SEARCH_OPS search_op,
1810 LPVOID filter, DWORD flags)
1813 FIND_FIRST_INFO *info = NULL;
1814 UNICODE_STRING nt_name;
1815 OBJECT_ATTRIBUTES attr;
1820 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1822 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1825 FIXME("options not implemented 0x%08x 0x%08x\n", search_op, flags );
1826 return INVALID_HANDLE_VALUE;
1828 if (level != FindExInfoStandard)
1830 FIXME("info level %d not implemented\n", level );
1831 return INVALID_HANDLE_VALUE;
1834 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1836 SetLastError( ERROR_PATH_NOT_FOUND );
1837 return INVALID_HANDLE_VALUE;
1840 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1842 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1846 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1848 static const WCHAR dotW[] = {'.',0};
1851 /* we still need to check that the directory can be opened */
1855 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1857 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1860 memcpy( dir, filename, HIWORD(device) );
1861 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1863 RtlFreeUnicodeString( &nt_name );
1864 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1866 HeapFree( GetProcessHeap(), 0, dir );
1867 SetLastError( ERROR_PATH_NOT_FOUND );
1870 HeapFree( GetProcessHeap(), 0, dir );
1871 RtlInitUnicodeString( &info->mask, NULL );
1873 else if (!mask || !*mask)
1875 SetLastError( ERROR_FILE_NOT_FOUND );
1880 if (!RtlCreateUnicodeString( &info->mask, mask ))
1882 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1886 /* truncate dir name before mask */
1888 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1891 /* check if path is the root of the drive */
1892 info->is_root = FALSE;
1893 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1894 if (p[0] && p[1] == ':')
1897 while (*p == '\\') p++;
1898 info->is_root = (*p == 0);
1901 attr.Length = sizeof(attr);
1902 attr.RootDirectory = 0;
1903 attr.Attributes = OBJ_CASE_INSENSITIVE;
1904 attr.ObjectName = &nt_name;
1905 attr.SecurityDescriptor = NULL;
1906 attr.SecurityQualityOfService = NULL;
1908 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1909 FILE_SHARE_READ | FILE_SHARE_WRITE,
1910 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1912 if (status != STATUS_SUCCESS)
1914 RtlFreeUnicodeString( &info->mask );
1915 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1916 SetLastError( ERROR_PATH_NOT_FOUND );
1918 SetLastError( RtlNtStatusToDosError(status) );
1922 RtlInitializeCriticalSection( &info->cs );
1923 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
1924 info->path = nt_name;
1925 info->magic = FIND_FIRST_MAGIC;
1928 info->search_op = search_op;
1932 WIN32_FIND_DATAW *wfd = data;
1934 memset( wfd, 0, sizeof(*wfd) );
1935 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1936 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1937 CloseHandle( info->handle );
1944 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1945 FileBothDirectoryInformation, FALSE, &info->mask, TRUE );
1949 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1950 return INVALID_HANDLE_VALUE;
1952 info->data_len = io.Information;
1953 if (!FindNextFileW( info, data ))
1955 TRACE( "%s not found\n", debugstr_w(filename) );
1957 SetLastError( ERROR_FILE_NOT_FOUND );
1958 return INVALID_HANDLE_VALUE;
1960 if (!strpbrkW( info->mask.Buffer, wildcardsW ))
1962 /* we can't find two files with the same name */
1963 CloseHandle( info->handle );
1970 HeapFree( GetProcessHeap(), 0, info );
1971 RtlFreeUnicodeString( &nt_name );
1972 return INVALID_HANDLE_VALUE;
1976 /*************************************************************************
1977 * FindNextFileW (KERNEL32.@)
1979 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1981 FIND_FIRST_INFO *info;
1982 FILE_BOTH_DIR_INFORMATION *dir_info;
1985 TRACE("%p %p\n", handle, data);
1987 if (!handle || handle == INVALID_HANDLE_VALUE)
1989 SetLastError( ERROR_INVALID_HANDLE );
1993 if (info->magic != FIND_FIRST_MAGIC)
1995 SetLastError( ERROR_INVALID_HANDLE );
1999 RtlEnterCriticalSection( &info->cs );
2001 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
2004 if (info->data_pos >= info->data_len) /* need to read some more data */
2008 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
2009 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
2012 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
2013 if (io.u.Status == STATUS_NO_MORE_FILES)
2015 CloseHandle( info->handle );
2020 info->data_len = io.Information;
2024 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
2026 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
2027 else info->data_pos = info->data_len;
2029 /* don't return '.' and '..' in the root of the drive */
2032 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
2033 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
2034 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
2037 /* check for dir symlink */
2038 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
2039 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2040 strpbrkW( info->mask.Buffer, wildcardsW ))
2042 if (!check_dir_symlink( info, dir_info )) continue;
2045 data->dwFileAttributes = dir_info->FileAttributes;
2046 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
2047 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
2048 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
2049 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
2050 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
2051 data->dwReserved0 = 0;
2052 data->dwReserved1 = 0;
2054 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
2055 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
2056 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
2057 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
2059 TRACE("returning %s (%s)\n",
2060 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
2066 RtlLeaveCriticalSection( &info->cs );
2071 /*************************************************************************
2072 * FindClose (KERNEL32.@)
2074 BOOL WINAPI FindClose( HANDLE handle )
2076 FIND_FIRST_INFO *info = handle;
2078 if (!handle || handle == INVALID_HANDLE_VALUE)
2080 SetLastError( ERROR_INVALID_HANDLE );
2086 if (info->magic == FIND_FIRST_MAGIC)
2088 RtlEnterCriticalSection( &info->cs );
2089 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
2092 if (info->handle) CloseHandle( info->handle );
2094 RtlFreeUnicodeString( &info->mask );
2095 info->mask.Buffer = NULL;
2096 RtlFreeUnicodeString( &info->path );
2099 RtlLeaveCriticalSection( &info->cs );
2100 info->cs.DebugInfo->Spare[0] = 0;
2101 RtlDeleteCriticalSection( &info->cs );
2102 HeapFree( GetProcessHeap(), 0, info );
2108 WARN("Illegal handle %p\n", handle);
2109 SetLastError( ERROR_INVALID_HANDLE );
2118 /*************************************************************************
2119 * FindFirstFileA (KERNEL32.@)
2121 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
2123 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
2124 FindExSearchNameMatch, NULL, 0);
2127 /*************************************************************************
2128 * FindFirstFileExA (KERNEL32.@)
2130 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
2131 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
2132 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
2135 WIN32_FIND_DATAA *dataA;
2136 WIN32_FIND_DATAW dataW;
2139 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
2141 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
2142 if (handle == INVALID_HANDLE_VALUE) return handle;
2144 dataA = lpFindFileData;
2145 dataA->dwFileAttributes = dataW.dwFileAttributes;
2146 dataA->ftCreationTime = dataW.ftCreationTime;
2147 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
2148 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
2149 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
2150 dataA->nFileSizeLow = dataW.nFileSizeLow;
2151 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
2152 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
2153 sizeof(dataA->cAlternateFileName) );
2158 /*************************************************************************
2159 * FindFirstFileW (KERNEL32.@)
2161 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
2163 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
2164 FindExSearchNameMatch, NULL, 0);
2168 /*************************************************************************
2169 * FindNextFileA (KERNEL32.@)
2171 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
2173 WIN32_FIND_DATAW dataW;
2175 if (!FindNextFileW( handle, &dataW )) return FALSE;
2176 data->dwFileAttributes = dataW.dwFileAttributes;
2177 data->ftCreationTime = dataW.ftCreationTime;
2178 data->ftLastAccessTime = dataW.ftLastAccessTime;
2179 data->ftLastWriteTime = dataW.ftLastWriteTime;
2180 data->nFileSizeHigh = dataW.nFileSizeHigh;
2181 data->nFileSizeLow = dataW.nFileSizeLow;
2182 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
2183 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
2184 sizeof(data->cAlternateFileName) );
2189 /**************************************************************************
2190 * GetFileAttributesW (KERNEL32.@)
2192 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
2194 FILE_BASIC_INFORMATION info;
2195 UNICODE_STRING nt_name;
2196 OBJECT_ATTRIBUTES attr;
2199 TRACE("%s\n", debugstr_w(name));
2201 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2203 SetLastError( ERROR_PATH_NOT_FOUND );
2204 return INVALID_FILE_ATTRIBUTES;
2207 attr.Length = sizeof(attr);
2208 attr.RootDirectory = 0;
2209 attr.Attributes = OBJ_CASE_INSENSITIVE;
2210 attr.ObjectName = &nt_name;
2211 attr.SecurityDescriptor = NULL;
2212 attr.SecurityQualityOfService = NULL;
2214 status = NtQueryAttributesFile( &attr, &info );
2215 RtlFreeUnicodeString( &nt_name );
2217 if (status == STATUS_SUCCESS) return info.FileAttributes;
2219 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2220 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
2222 SetLastError( RtlNtStatusToDosError(status) );
2223 return INVALID_FILE_ATTRIBUTES;
2227 /**************************************************************************
2228 * GetFileAttributesA (KERNEL32.@)
2230 DWORD WINAPI GetFileAttributesA( LPCSTR name )
2234 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
2235 return GetFileAttributesW( nameW );
2239 /**************************************************************************
2240 * SetFileAttributesW (KERNEL32.@)
2242 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
2244 UNICODE_STRING nt_name;
2245 OBJECT_ATTRIBUTES attr;
2250 TRACE("%s %x\n", debugstr_w(name), attributes);
2252 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2254 SetLastError( ERROR_PATH_NOT_FOUND );
2258 attr.Length = sizeof(attr);
2259 attr.RootDirectory = 0;
2260 attr.Attributes = OBJ_CASE_INSENSITIVE;
2261 attr.ObjectName = &nt_name;
2262 attr.SecurityDescriptor = NULL;
2263 attr.SecurityQualityOfService = NULL;
2265 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2266 RtlFreeUnicodeString( &nt_name );
2268 if (status == STATUS_SUCCESS)
2270 FILE_BASIC_INFORMATION info;
2272 memset( &info, 0, sizeof(info) );
2273 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2274 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2278 if (status == STATUS_SUCCESS) return TRUE;
2279 SetLastError( RtlNtStatusToDosError(status) );
2284 /**************************************************************************
2285 * SetFileAttributesA (KERNEL32.@)
2287 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2291 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2292 return SetFileAttributesW( nameW, attributes );
2296 /**************************************************************************
2297 * GetFileAttributesExW (KERNEL32.@)
2299 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2301 FILE_NETWORK_OPEN_INFORMATION info;
2302 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2303 UNICODE_STRING nt_name;
2304 OBJECT_ATTRIBUTES attr;
2307 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2309 if (level != GetFileExInfoStandard)
2311 SetLastError( ERROR_INVALID_PARAMETER );
2315 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2317 SetLastError( ERROR_PATH_NOT_FOUND );
2321 attr.Length = sizeof(attr);
2322 attr.RootDirectory = 0;
2323 attr.Attributes = OBJ_CASE_INSENSITIVE;
2324 attr.ObjectName = &nt_name;
2325 attr.SecurityDescriptor = NULL;
2326 attr.SecurityQualityOfService = NULL;
2328 status = NtQueryFullAttributesFile( &attr, &info );
2329 RtlFreeUnicodeString( &nt_name );
2331 if (status != STATUS_SUCCESS)
2333 SetLastError( RtlNtStatusToDosError(status) );
2337 data->dwFileAttributes = info.FileAttributes;
2338 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2339 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2340 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2341 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2342 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2343 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2344 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2345 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2350 /**************************************************************************
2351 * GetFileAttributesExA (KERNEL32.@)
2353 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2357 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2358 return GetFileAttributesExW( nameW, level, ptr );
2362 /******************************************************************************
2363 * GetCompressedFileSizeW (KERNEL32.@)
2365 * Get the actual number of bytes used on disk.
2368 * Success: Low-order doubleword of number of bytes
2369 * Failure: INVALID_FILE_SIZE
2371 DWORD WINAPI GetCompressedFileSizeW(
2372 LPCWSTR name, /* [in] Pointer to name of file */
2373 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2375 UNICODE_STRING nt_name;
2376 OBJECT_ATTRIBUTES attr;
2380 DWORD ret = INVALID_FILE_SIZE;
2382 TRACE("%s %p\n", debugstr_w(name), size_high);
2384 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2386 SetLastError( ERROR_PATH_NOT_FOUND );
2387 return INVALID_FILE_SIZE;
2390 attr.Length = sizeof(attr);
2391 attr.RootDirectory = 0;
2392 attr.Attributes = OBJ_CASE_INSENSITIVE;
2393 attr.ObjectName = &nt_name;
2394 attr.SecurityDescriptor = NULL;
2395 attr.SecurityQualityOfService = NULL;
2397 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2398 RtlFreeUnicodeString( &nt_name );
2400 if (status == STATUS_SUCCESS)
2402 /* we don't support compressed files, simply return the file size */
2403 ret = GetFileSize( handle, size_high );
2406 else SetLastError( RtlNtStatusToDosError(status) );
2412 /******************************************************************************
2413 * GetCompressedFileSizeA (KERNEL32.@)
2415 * See GetCompressedFileSizeW.
2417 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2421 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2422 return GetCompressedFileSizeW( nameW, size_high );
2426 /***********************************************************************
2427 * OpenVxDHandle (KERNEL32.@)
2429 * This function is supposed to return the corresponding Ring 0
2430 * ("kernel") handle for a Ring 3 handle in Win9x.
2431 * Evidently, Wine will have problems with this. But we try anyway,
2434 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
2436 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
2437 return hHandleRing3;
2441 /****************************************************************************
2442 * DeviceIoControl (KERNEL32.@)
2444 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
2445 LPVOID lpvInBuffer, DWORD cbInBuffer,
2446 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
2447 LPDWORD lpcbBytesReturned,
2448 LPOVERLAPPED lpOverlapped)
2452 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2453 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
2454 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
2456 /* Check if this is a user defined control code for a VxD */
2458 if (HIWORD( dwIoControlCode ) == 0 && (GetVersion() & 0x80000000))
2460 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
2461 static DeviceIoProc (*vxd_get_proc)(HANDLE);
2462 DeviceIoProc proc = NULL;
2464 if (!vxd_get_proc) vxd_get_proc = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2465 "__wine_vxd_get_proc" );
2466 if (vxd_get_proc) proc = vxd_get_proc( hDevice );
2467 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
2468 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
2471 /* Not a VxD, let ntdll handle it */
2475 LPVOID cvalue = ((ULONG_PTR)lpOverlapped->hEvent & 1) ? NULL : lpOverlapped;
2476 lpOverlapped->Internal = STATUS_PENDING;
2477 lpOverlapped->InternalHigh = 0;
2478 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2479 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
2480 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2481 dwIoControlCode, lpvInBuffer, cbInBuffer,
2482 lpvOutBuffer, cbOutBuffer);
2484 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
2485 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2486 dwIoControlCode, lpvInBuffer, cbInBuffer,
2487 lpvOutBuffer, cbOutBuffer);
2488 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
2492 IO_STATUS_BLOCK iosb;
2494 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2495 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
2496 dwIoControlCode, lpvInBuffer, cbInBuffer,
2497 lpvOutBuffer, cbOutBuffer);
2499 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
2500 dwIoControlCode, lpvInBuffer, cbInBuffer,
2501 lpvOutBuffer, cbOutBuffer);
2502 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
2504 if (status) SetLastError( RtlNtStatusToDosError(status) );
2509 /***********************************************************************
2510 * OpenFile (KERNEL32.@)
2512 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2516 WORD filedatetime[2];
2518 if (!ofs) return HFILE_ERROR;
2520 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2521 ((mode & 0x3 )==OF_READ)?"OF_READ":
2522 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2523 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2524 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2525 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2526 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2527 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2528 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2529 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2530 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2531 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2532 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2533 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2534 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2535 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2536 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2537 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2541 ofs->cBytes = sizeof(OFSTRUCT);
2543 if (mode & OF_REOPEN) name = ofs->szPathName;
2545 if (!name) return HFILE_ERROR;
2547 TRACE("%s %04x\n", name, mode );
2549 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2550 Are there any cases where getting the path here is wrong?
2551 Uwe Bonnes 1997 Apr 2 */
2552 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2554 /* OF_PARSE simply fills the structure */
2556 if (mode & OF_PARSE)
2558 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2559 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2563 /* OF_CREATE is completely different from all other options, so
2566 if (mode & OF_CREATE)
2568 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2573 /* Now look for the file */
2575 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2578 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2580 if (mode & OF_DELETE)
2582 if (!DeleteFileA( ofs->szPathName )) goto error;
2583 TRACE("(%s): OF_DELETE return = OK\n", name);
2587 handle = LongToHandle(_lopen( ofs->szPathName, mode ));
2588 if (handle == INVALID_HANDLE_VALUE) goto error;
2590 GetFileTime( handle, NULL, NULL, &filetime );
2591 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2592 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2594 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2596 CloseHandle( handle );
2597 WARN("(%s): OF_VERIFY failed\n", name );
2598 /* FIXME: what error here? */
2599 SetLastError( ERROR_FILE_NOT_FOUND );
2603 ofs->Reserved1 = filedatetime[0];
2604 ofs->Reserved2 = filedatetime[1];
2606 TRACE("(%s): OK, return = %p\n", name, handle );
2607 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2609 CloseHandle( handle );
2612 return HandleToLong(handle);
2614 error: /* We get here if there was an error opening the file */
2615 ofs->nErrCode = GetLastError();
2616 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2621 /***********************************************************************
2622 * OpenFileById (KERNEL32.@)
2624 HANDLE WINAPI OpenFileById( HANDLE handle, LPFILE_ID_DESCRIPTOR id, DWORD access,
2625 DWORD share, LPSECURITY_ATTRIBUTES sec_attr, DWORD flags )
2629 OBJECT_ATTRIBUTES attr;
2632 UNICODE_STRING objectName;
2636 SetLastError( ERROR_INVALID_PARAMETER );
2637 return INVALID_HANDLE_VALUE;
2640 options = FILE_OPEN_BY_FILE_ID;
2641 if (flags & FILE_FLAG_BACKUP_SEMANTICS)
2642 options |= FILE_OPEN_FOR_BACKUP_INTENT;
2644 options |= FILE_NON_DIRECTORY_FILE;
2645 if (flags & FILE_FLAG_NO_BUFFERING) options |= FILE_NO_INTERMEDIATE_BUFFERING;
2646 if (!(flags & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
2647 if (flags & FILE_FLAG_RANDOM_ACCESS) options |= FILE_RANDOM_ACCESS;
2648 flags &= FILE_ATTRIBUTE_VALID_FLAGS;
2650 objectName.Length = sizeof(ULONGLONG);
2651 objectName.Buffer = (WCHAR *)&id->u.FileId;
2652 attr.Length = sizeof(attr);
2653 attr.RootDirectory = handle;
2654 attr.Attributes = 0;
2655 attr.ObjectName = &objectName;
2656 attr.SecurityDescriptor = sec_attr ? sec_attr->lpSecurityDescriptor : NULL;
2657 attr.SecurityQualityOfService = NULL;
2658 if (sec_attr && sec_attr->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
2660 status = NtCreateFile( &result, access, &attr, &io, NULL, flags,
2661 share, OPEN_EXISTING, options, NULL, 0 );
2662 if (status != STATUS_SUCCESS)
2664 SetLastError( RtlNtStatusToDosError( status ) );
2665 return INVALID_HANDLE_VALUE;
2671 /***********************************************************************
2672 * K32EnumDeviceDrivers (KERNEL32.@)
2674 BOOL WINAPI K32EnumDeviceDrivers(void **image_base, DWORD cb, DWORD *needed)
2676 FIXME("(%p, %d, %p): stub\n", image_base, cb, needed);
2684 /***********************************************************************
2685 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2687 DWORD WINAPI K32GetDeviceDriverBaseNameA(void *image_base, LPSTR base_name, DWORD size)
2689 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2691 if (base_name && size)
2692 base_name[0] = '\0';
2697 /***********************************************************************
2698 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2700 DWORD WINAPI K32GetDeviceDriverBaseNameW(void *image_base, LPWSTR base_name, DWORD size)
2702 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2704 if (base_name && size)
2705 base_name[0] = '\0';
2710 /***********************************************************************
2711 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2713 DWORD WINAPI K32GetDeviceDriverFileNameA(void *image_base, LPSTR file_name, DWORD size)
2715 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2717 if (file_name && size)
2718 file_name[0] = '\0';
2723 /***********************************************************************
2724 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2726 DWORD WINAPI K32GetDeviceDriverFileNameW(void *image_base, LPWSTR file_name, DWORD size)
2728 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2730 if (file_name && size)
2731 file_name[0] = '\0';