2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
39 #include "wine/winbase16.h"
40 #include "kernel_private.h"
42 #include "wine/exception.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 #include "wine/server.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(file);
51 HANDLE dos_handles[DOS_TABLE_SIZE];
53 /* info structure for FindFirstFile handle */
56 DWORD magic; /* magic number */
57 HANDLE handle; /* handle to directory */
58 CRITICAL_SECTION cs; /* crit section protecting this structure */
59 UNICODE_STRING mask; /* file mask */
60 BOOL is_root; /* is directory the root of the drive? */
61 UINT data_pos; /* current position in dir data */
62 UINT data_len; /* length of dir data */
63 BYTE data[8192]; /* directory data */
66 #define FIND_FIRST_MAGIC 0xc0ffee11
68 static BOOL oem_file_apis;
70 static WINE_EXCEPTION_FILTER(page_fault)
72 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
73 return EXCEPTION_EXECUTE_HANDLER;
74 return EXCEPTION_CONTINUE_SEARCH;
78 /***********************************************************************
81 * Wrapper for CreateFile that takes OF_* mode flags.
83 static HANDLE create_file_OF( LPCSTR path, INT mode )
85 DWORD access, sharing, creation;
89 creation = CREATE_ALWAYS;
90 access = GENERIC_READ | GENERIC_WRITE;
94 creation = OPEN_EXISTING;
97 case OF_READ: access = GENERIC_READ; break;
98 case OF_WRITE: access = GENERIC_WRITE; break;
99 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
100 default: access = 0; break;
106 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
107 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
108 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
109 case OF_SHARE_DENY_NONE:
110 case OF_SHARE_COMPAT:
111 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
113 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
117 /***********************************************************************
120 * Set the DOS error code from errno.
122 void FILE_SetDosError(void)
124 int save_errno = errno; /* errno gets overwritten by printf */
126 TRACE("errno = %d %s\n", errno, strerror(errno));
130 SetLastError( ERROR_SHARING_VIOLATION );
133 SetLastError( ERROR_INVALID_HANDLE );
136 SetLastError( ERROR_HANDLE_DISK_FULL );
141 SetLastError( ERROR_ACCESS_DENIED );
144 SetLastError( ERROR_LOCK_VIOLATION );
147 SetLastError( ERROR_FILE_NOT_FOUND );
150 SetLastError( ERROR_CANNOT_MAKE );
154 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
157 SetLastError( ERROR_FILE_EXISTS );
161 SetLastError( ERROR_SEEK );
164 SetLastError( ERROR_DIR_NOT_EMPTY );
167 SetLastError( ERROR_BAD_FORMAT );
170 SetLastError( ERROR_PATH_NOT_FOUND );
173 SetLastError( ERROR_NOT_SAME_DEVICE );
176 WARN("unknown file error: %s\n", strerror(save_errno) );
177 SetLastError( ERROR_GEN_FAILURE );
184 /***********************************************************************
187 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
189 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
190 * there is no possibility for the function to do that twice, taking into
191 * account any called function.
193 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
196 UNICODE_STRING strW, *pstrW;
199 RtlInitAnsiString( &str, name );
200 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
202 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
204 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
205 if (status == STATUS_SUCCESS) return pstrW->Buffer;
207 if (status == STATUS_BUFFER_OVERFLOW)
208 SetLastError( ERROR_FILENAME_EXCED_RANGE );
210 SetLastError( RtlNtStatusToDosError(status) );
215 /***********************************************************************
218 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
220 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
224 if (srclen < 0) srclen = strlenW( src ) + 1;
226 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
228 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
233 /**************************************************************************
234 * SetFileApisToOEM (KERNEL32.@)
236 VOID WINAPI SetFileApisToOEM(void)
238 oem_file_apis = TRUE;
242 /**************************************************************************
243 * SetFileApisToANSI (KERNEL32.@)
245 VOID WINAPI SetFileApisToANSI(void)
247 oem_file_apis = FALSE;
251 /******************************************************************************
252 * AreFileApisANSI (KERNEL32.@)
254 * Determines if file functions are using ANSI
257 * TRUE: Set of file functions is using ANSI code page
258 * FALSE: Set of file functions is using OEM code page
260 BOOL WINAPI AreFileApisANSI(void)
262 return !oem_file_apis;
266 /**************************************************************************
267 * Operations on file handles *
268 **************************************************************************/
270 /***********************************************************************
271 * FILE_InitProcessDosHandles
273 * Allocates the default DOS handles for a process. Called either by
274 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
276 static void FILE_InitProcessDosHandles( void )
278 static BOOL init_done /* = FALSE */;
279 HANDLE cp = GetCurrentProcess();
281 if (init_done) return;
283 DuplicateHandle(cp, GetStdHandle(STD_INPUT_HANDLE), cp, &dos_handles[0],
284 0, TRUE, DUPLICATE_SAME_ACCESS);
285 DuplicateHandle(cp, GetStdHandle(STD_OUTPUT_HANDLE), cp, &dos_handles[1],
286 0, TRUE, DUPLICATE_SAME_ACCESS);
287 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[2],
288 0, TRUE, DUPLICATE_SAME_ACCESS);
289 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[3],
290 0, TRUE, DUPLICATE_SAME_ACCESS);
291 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[4],
292 0, TRUE, DUPLICATE_SAME_ACCESS);
296 /******************************************************************
297 * FILE_ReadWriteApc (internal)
299 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG len)
301 LPOVERLAPPED_COMPLETION_ROUTINE cr = (LPOVERLAPPED_COMPLETION_ROUTINE)apc_user;
303 cr(RtlNtStatusToDosError(io_status->u.Status), len, (LPOVERLAPPED)io_status);
307 /***********************************************************************
308 * ReadFileEx (KERNEL32.@)
310 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
311 LPOVERLAPPED overlapped,
312 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
314 LARGE_INTEGER offset;
316 PIO_STATUS_BLOCK io_status;
318 TRACE("(hFile=%p, buffer=%p, bytes=%lu, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
322 SetLastError(ERROR_INVALID_PARAMETER);
326 offset.u.LowPart = overlapped->Offset;
327 offset.u.HighPart = overlapped->OffsetHigh;
328 io_status = (PIO_STATUS_BLOCK)overlapped;
329 io_status->u.Status = STATUS_PENDING;
331 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
332 io_status, buffer, bytesToRead, &offset, NULL);
336 SetLastError( RtlNtStatusToDosError(status) );
343 /***********************************************************************
344 * ReadFile (KERNEL32.@)
346 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
347 LPDWORD bytesRead, LPOVERLAPPED overlapped )
349 LARGE_INTEGER offset;
350 PLARGE_INTEGER poffset = NULL;
351 IO_STATUS_BLOCK iosb;
352 PIO_STATUS_BLOCK io_status = &iosb;
356 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToRead,
357 bytesRead, overlapped );
359 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
360 if (!bytesToRead) return TRUE;
362 if (is_console_handle(hFile))
363 return ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
365 if (overlapped != NULL)
367 offset.u.LowPart = overlapped->Offset;
368 offset.u.HighPart = overlapped->OffsetHigh;
370 hEvent = overlapped->hEvent;
371 io_status = (PIO_STATUS_BLOCK)overlapped;
373 io_status->u.Status = STATUS_PENDING;
374 io_status->Information = 0;
376 status = NtReadFile(hFile, hEvent, NULL, NULL, io_status, buffer, bytesToRead, poffset, NULL);
378 if (status != STATUS_PENDING && bytesRead)
379 *bytesRead = io_status->Information;
381 if (status && status != STATUS_END_OF_FILE)
383 SetLastError( RtlNtStatusToDosError(status) );
390 /***********************************************************************
391 * WriteFileEx (KERNEL32.@)
393 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
394 LPOVERLAPPED overlapped,
395 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
397 LARGE_INTEGER offset;
399 PIO_STATUS_BLOCK io_status;
401 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
403 if (overlapped == NULL)
405 SetLastError(ERROR_INVALID_PARAMETER);
408 offset.u.LowPart = overlapped->Offset;
409 offset.u.HighPart = overlapped->OffsetHigh;
411 io_status = (PIO_STATUS_BLOCK)overlapped;
412 io_status->u.Status = STATUS_PENDING;
414 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
415 io_status, buffer, bytesToWrite, &offset, NULL);
417 if (status) SetLastError( RtlNtStatusToDosError(status) );
422 /***********************************************************************
423 * WriteFile (KERNEL32.@)
425 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
426 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
428 HANDLE hEvent = NULL;
429 LARGE_INTEGER offset;
430 PLARGE_INTEGER poffset = NULL;
432 IO_STATUS_BLOCK iosb;
433 PIO_STATUS_BLOCK piosb = &iosb;
435 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
437 if (is_console_handle(hFile))
438 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
442 offset.u.LowPart = overlapped->Offset;
443 offset.u.HighPart = overlapped->OffsetHigh;
445 hEvent = overlapped->hEvent;
446 piosb = (PIO_STATUS_BLOCK)overlapped;
448 piosb->u.Status = STATUS_PENDING;
449 piosb->Information = 0;
451 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
452 buffer, bytesToWrite, poffset, NULL);
454 if (status != STATUS_PENDING && bytesWritten)
455 *bytesWritten = piosb->Information;
459 SetLastError( RtlNtStatusToDosError(status) );
466 /***********************************************************************
467 * GetOverlappedResult (KERNEL32.@)
469 * Check the result of an Asynchronous data transfer from a file.
472 * HANDLE hFile [in] handle of file to check on
473 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
474 * LPDWORD lpTransferred [in/out] number of bytes transferred
475 * BOOL bWait [in] wait for the transfer to complete ?
481 * If successful (and relevant) lpTransferred will hold the number of
482 * bytes transferred during the async operation.
486 * Currently only works for WaitCommEvent, ReadFile, WriteFile
487 * with communications ports.
490 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
491 LPDWORD lpTransferred, BOOL bWait)
493 DWORD r = WAIT_OBJECT_0;
495 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
497 if ( lpOverlapped == NULL )
499 ERR("lpOverlapped was null\n");
504 if ( lpOverlapped->hEvent )
508 TRACE( "waiting on %p\n", lpOverlapped );
509 r = WaitForSingleObjectEx( lpOverlapped->hEvent, INFINITE, TRUE );
510 TRACE( "wait on %p returned %ld\n", lpOverlapped, r );
511 } while ( r == WAIT_IO_COMPLETION );
516 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
520 else if ( lpOverlapped->Internal == STATUS_PENDING )
522 /* Wait in order to give APCs a chance to run. */
523 /* This is cheating, so we must set the event again in case of success -
524 it may be a non-manual reset event. */
527 TRACE( "waiting on %p\n", lpOverlapped );
528 r = WaitForSingleObjectEx( lpOverlapped->hEvent, 0, TRUE );
529 TRACE( "wait on %p returned %ld\n", lpOverlapped, r );
530 } while ( r == WAIT_IO_COMPLETION );
531 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
532 NtSetEvent( lpOverlapped->hEvent, NULL );
534 if ( r == WAIT_FAILED )
536 WARN("wait operation failed\n");
539 if (lpTransferred) *lpTransferred = lpOverlapped->InternalHigh;
541 switch ( lpOverlapped->Internal )
546 SetLastError( ERROR_IO_INCOMPLETE );
547 if ( bWait ) ERR("PENDING status after waiting!\n");
550 SetLastError( RtlNtStatusToDosError( lpOverlapped->Internal ) );
555 /***********************************************************************
556 * CancelIo (KERNEL32.@)
558 BOOL WINAPI CancelIo(HANDLE handle)
560 IO_STATUS_BLOCK io_status;
562 NtCancelIoFile(handle, &io_status);
563 if (io_status.u.Status)
565 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
571 /***********************************************************************
572 * _hread (KERNEL32.@)
574 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
576 return _lread( hFile, buffer, count );
580 /***********************************************************************
581 * _hwrite (KERNEL32.@)
583 * experimentation yields that _lwrite:
584 * o truncates the file at the current position with
586 * o returns 0 on a 0 length write
587 * o works with console handles
590 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
594 TRACE("%d %p %ld\n", handle, buffer, count );
598 /* Expand or truncate at current position */
599 if (!SetEndOfFile( (HANDLE)handle )) return HFILE_ERROR;
602 if (!WriteFile( (HANDLE)handle, buffer, count, &result, NULL ))
608 /***********************************************************************
609 * _lclose (KERNEL32.@)
611 HFILE WINAPI _lclose( HFILE hFile )
613 TRACE("handle %d\n", hFile );
614 return CloseHandle( (HANDLE)hFile ) ? 0 : HFILE_ERROR;
618 /***********************************************************************
619 * _lcreat (KERNEL32.@)
621 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
623 /* Mask off all flags not explicitly allowed by the doc */
624 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
625 TRACE("%s %02x\n", path, attr );
626 return (HFILE)CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
627 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
628 CREATE_ALWAYS, attr, 0 );
632 /***********************************************************************
633 * _lopen (KERNEL32.@)
635 HFILE WINAPI _lopen( LPCSTR path, INT mode )
637 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
638 return (HFILE)create_file_OF( path, mode & ~OF_CREATE );
641 /***********************************************************************
642 * _lread (KERNEL32.@)
644 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
647 if (!ReadFile( (HANDLE)handle, buffer, count, &result, NULL ))
653 /***********************************************************************
654 * _llseek (KERNEL32.@)
656 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
658 return SetFilePointer( (HANDLE)hFile, lOffset, NULL, nOrigin );
662 /***********************************************************************
663 * _lwrite (KERNEL32.@)
665 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
667 return (UINT)_hwrite( hFile, buffer, (LONG)count );
671 /***********************************************************************
672 * FlushFileBuffers (KERNEL32.@)
674 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
677 IO_STATUS_BLOCK ioblk;
679 if (is_console_handle( hFile ))
681 /* this will fail (as expected) for an output handle */
682 return FlushConsoleInputBuffer( hFile );
684 nts = NtFlushBuffersFile( hFile, &ioblk );
685 if (nts != STATUS_SUCCESS)
687 SetLastError( RtlNtStatusToDosError( nts ) );
695 /***********************************************************************
696 * GetFileType (KERNEL32.@)
698 DWORD WINAPI GetFileType( HANDLE hFile )
700 FILE_FS_DEVICE_INFORMATION info;
704 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
706 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
707 if (status != STATUS_SUCCESS)
709 SetLastError( RtlNtStatusToDosError(status) );
710 return FILE_TYPE_UNKNOWN;
713 switch(info.DeviceType)
715 case FILE_DEVICE_NULL:
716 case FILE_DEVICE_SERIAL_PORT:
717 case FILE_DEVICE_PARALLEL_PORT:
718 case FILE_DEVICE_UNKNOWN:
719 return FILE_TYPE_CHAR;
720 case FILE_DEVICE_NAMED_PIPE:
721 return FILE_TYPE_PIPE;
723 return FILE_TYPE_DISK;
728 /***********************************************************************
729 * GetFileInformationByHandle (KERNEL32.@)
731 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
733 FILE_ALL_INFORMATION all_info;
737 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
738 if (status == STATUS_SUCCESS)
740 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
741 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
742 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
743 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
744 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
745 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
746 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
747 info->dwVolumeSerialNumber = 0; /* FIXME */
748 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
749 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
750 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
751 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
752 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
755 SetLastError( RtlNtStatusToDosError(status) );
760 /***********************************************************************
761 * GetFileSize (KERNEL32.@)
763 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
766 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
767 if (filesizehigh) *filesizehigh = size.u.HighPart;
768 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
769 return size.u.LowPart;
773 /***********************************************************************
774 * GetFileSizeEx (KERNEL32.@)
776 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
778 FILE_END_OF_FILE_INFORMATION info;
782 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileEndOfFileInformation );
783 if (status == STATUS_SUCCESS)
785 *lpFileSize = info.EndOfFile;
788 SetLastError( RtlNtStatusToDosError(status) );
793 /**************************************************************************
794 * SetEndOfFile (KERNEL32.@)
796 BOOL WINAPI SetEndOfFile( HANDLE hFile )
798 FILE_POSITION_INFORMATION pos;
799 FILE_END_OF_FILE_INFORMATION eof;
803 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
804 if (status == STATUS_SUCCESS)
806 eof.EndOfFile = pos.CurrentByteOffset;
807 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
809 if (status == STATUS_SUCCESS) return TRUE;
810 SetLastError( RtlNtStatusToDosError(status) );
815 /***********************************************************************
816 * SetFilePointer (KERNEL32.@)
818 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
820 LARGE_INTEGER dist, newpos;
824 dist.u.LowPart = distance;
825 dist.u.HighPart = *highword;
827 else dist.QuadPart = distance;
829 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
831 if (highword) *highword = newpos.u.HighPart;
832 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
833 return newpos.u.LowPart;
837 /***********************************************************************
838 * SetFilePointerEx (KERNEL32.@)
840 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
841 LARGE_INTEGER *newpos, DWORD method )
843 static const int whence[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
848 TRACE("handle %p offset %s newpos %p origin %ld\n",
849 hFile, wine_dbgstr_longlong(distance.QuadPart), newpos, method );
851 if (method > FILE_END)
853 SetLastError( ERROR_INVALID_PARAMETER );
857 if (!(status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
861 pos = distance.QuadPart;
862 if ((res = lseek( fd, pos, whence[method] )) == (off_t)-1)
864 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
865 if (((errno == EINVAL) || (errno == EPERM)) && (method != FILE_BEGIN) && (pos < 0))
866 SetLastError( ERROR_NEGATIVE_SEEK );
874 newpos->QuadPart = res;
876 wine_server_release_fd( hFile, fd );
878 else SetLastError( RtlNtStatusToDosError(status) );
883 /***********************************************************************
884 * GetFileTime (KERNEL32.@)
886 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
887 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
889 FILE_BASIC_INFORMATION info;
893 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
894 if (status == STATUS_SUCCESS)
898 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
899 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
901 if (lpLastAccessTime)
903 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
904 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
908 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
909 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
913 SetLastError( RtlNtStatusToDosError(status) );
918 /***********************************************************************
919 * SetFileTime (KERNEL32.@)
921 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
922 const FILETIME *atime, const FILETIME *mtime )
924 FILE_BASIC_INFORMATION info;
928 memset( &info, 0, sizeof(info) );
931 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
932 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
936 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
937 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
941 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
942 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
945 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
946 if (status == STATUS_SUCCESS) return TRUE;
947 SetLastError( RtlNtStatusToDosError(status) );
952 /**************************************************************************
953 * LockFile (KERNEL32.@)
955 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
956 DWORD count_low, DWORD count_high )
959 LARGE_INTEGER count, offset;
961 TRACE( "%p %lx%08lx %lx%08lx\n",
962 hFile, offset_high, offset_low, count_high, count_low );
964 count.u.LowPart = count_low;
965 count.u.HighPart = count_high;
966 offset.u.LowPart = offset_low;
967 offset.u.HighPart = offset_high;
969 status = NtLockFile( hFile, 0, NULL, NULL,
970 NULL, &offset, &count, NULL, TRUE, TRUE );
972 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
977 /**************************************************************************
978 * LockFileEx [KERNEL32.@]
980 * Locks a byte range within an open file for shared or exclusive access.
987 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
989 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
990 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
993 LARGE_INTEGER count, offset;
997 SetLastError( ERROR_INVALID_PARAMETER );
1001 TRACE( "%p %lx%08lx %lx%08lx flags %lx\n",
1002 hFile, overlapped->OffsetHigh, overlapped->Offset,
1003 count_high, count_low, flags );
1005 count.u.LowPart = count_low;
1006 count.u.HighPart = count_high;
1007 offset.u.LowPart = overlapped->Offset;
1008 offset.u.HighPart = overlapped->OffsetHigh;
1010 status = NtLockFile( hFile, overlapped->hEvent, NULL, NULL,
1011 NULL, &offset, &count, NULL,
1012 flags & LOCKFILE_FAIL_IMMEDIATELY,
1013 flags & LOCKFILE_EXCLUSIVE_LOCK );
1015 if (status) SetLastError( RtlNtStatusToDosError(status) );
1020 /**************************************************************************
1021 * UnlockFile (KERNEL32.@)
1023 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1024 DWORD count_low, DWORD count_high )
1027 LARGE_INTEGER count, offset;
1029 count.u.LowPart = count_low;
1030 count.u.HighPart = count_high;
1031 offset.u.LowPart = offset_low;
1032 offset.u.HighPart = offset_high;
1034 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1035 if (status) SetLastError( RtlNtStatusToDosError(status) );
1040 /**************************************************************************
1041 * UnlockFileEx (KERNEL32.@)
1043 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1044 LPOVERLAPPED overlapped )
1048 SetLastError( ERROR_INVALID_PARAMETER );
1051 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1053 return UnlockFile( hFile, overlapped->Offset, overlapped->OffsetHigh, count_low, count_high );
1057 /***********************************************************************
1058 * Win32HandleToDosFileHandle (KERNEL32.21)
1060 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1061 * longer valid after this function (even on failure).
1063 * Note: this is not exactly right, since on Win95 the Win32 handles
1064 * are on top of DOS handles and we do it the other way
1065 * around. Should be good enough though.
1067 HFILE WINAPI Win32HandleToDosFileHandle( HANDLE handle )
1071 if (!handle || (handle == INVALID_HANDLE_VALUE))
1074 FILE_InitProcessDosHandles();
1075 for (i = 0; i < DOS_TABLE_SIZE; i++)
1076 if (!dos_handles[i])
1078 dos_handles[i] = handle;
1079 TRACE("Got %d for h32 %p\n", i, handle );
1082 CloseHandle( handle );
1083 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1088 /***********************************************************************
1089 * DosFileHandleToWin32Handle (KERNEL32.20)
1091 * Return the Win32 handle for a DOS handle.
1093 * Note: this is not exactly right, since on Win95 the Win32 handles
1094 * are on top of DOS handles and we do it the other way
1095 * around. Should be good enough though.
1097 HANDLE WINAPI DosFileHandleToWin32Handle( HFILE handle )
1099 HFILE16 hfile = (HFILE16)handle;
1100 if (hfile < 5) FILE_InitProcessDosHandles();
1101 if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
1103 SetLastError( ERROR_INVALID_HANDLE );
1104 return INVALID_HANDLE_VALUE;
1106 return dos_handles[hfile];
1110 /*************************************************************************
1111 * SetHandleCount (KERNEL32.@)
1113 UINT WINAPI SetHandleCount( UINT count )
1115 return min( 256, count );
1119 /***********************************************************************
1120 * DisposeLZ32Handle (KERNEL32.22)
1122 * Note: this is not entirely correct, we should only close the
1123 * 32-bit handle and not the 16-bit one, but we cannot do
1124 * this because of the way our DOS handles are implemented.
1125 * It shouldn't break anything though.
1127 void WINAPI DisposeLZ32Handle( HANDLE handle )
1131 if (!handle || (handle == INVALID_HANDLE_VALUE)) return;
1133 for (i = 5; i < DOS_TABLE_SIZE; i++)
1134 if (dos_handles[i] == handle)
1137 CloseHandle( handle );
1142 /**************************************************************************
1143 * Operations on file names *
1144 **************************************************************************/
1147 /*************************************************************************
1148 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1150 * Creates or opens an object, and returns a handle that can be used to
1151 * access that object.
1155 * filename [in] pointer to filename to be accessed
1156 * access [in] access mode requested
1157 * sharing [in] share mode
1158 * sa [in] pointer to security attributes
1159 * creation [in] how to create the file
1160 * attributes [in] attributes for newly created file
1161 * template [in] handle to file with extended attributes to copy
1164 * Success: Open handle to specified file
1165 * Failure: INVALID_HANDLE_VALUE
1167 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1168 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1169 DWORD attributes, HANDLE template )
1173 OBJECT_ATTRIBUTES attr;
1174 UNICODE_STRING nameW;
1178 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1179 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1180 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1182 static const UINT nt_disposition[5] =
1184 FILE_CREATE, /* CREATE_NEW */
1185 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1186 FILE_OPEN, /* OPEN_EXISTING */
1187 FILE_OPEN_IF, /* OPEN_ALWAYS */
1188 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1194 if (!filename || !filename[0])
1196 SetLastError( ERROR_PATH_NOT_FOUND );
1197 return INVALID_HANDLE_VALUE;
1200 TRACE("%s %s%s%s%s%s%s creation %ld attributes 0x%lx\n", debugstr_w(filename),
1201 (access & GENERIC_READ)?"GENERIC_READ ":"",
1202 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1203 (!access)?"QUERY_ACCESS ":"",
1204 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1205 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1206 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1207 creation, attributes);
1209 /* Open a console for CONIN$ or CONOUT$ */
1211 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1213 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle), creation);
1217 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1219 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1221 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1222 !strncmpiW( filename + 4, pipeW, 5 ))
1226 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1228 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1230 else if (filename[4])
1232 ret = VXD_Open( filename+4, access, sa );
1237 SetLastError( ERROR_INVALID_NAME );
1238 return INVALID_HANDLE_VALUE;
1241 else dosdev = RtlIsDosDeviceName_U( filename );
1245 static const WCHAR conW[] = {'C','O','N'};
1247 if (LOWORD(dosdev) == sizeof(conW) &&
1248 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1250 switch (access & (GENERIC_READ|GENERIC_WRITE))
1253 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), creation);
1256 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), creation);
1259 SetLastError( ERROR_FILE_NOT_FOUND );
1260 return INVALID_HANDLE_VALUE;
1265 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1267 SetLastError( ERROR_INVALID_PARAMETER );
1268 return INVALID_HANDLE_VALUE;
1271 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1273 SetLastError( ERROR_PATH_NOT_FOUND );
1274 return INVALID_HANDLE_VALUE;
1277 /* now call NtCreateFile */
1280 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1281 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1283 options |= FILE_NON_DIRECTORY_FILE;
1284 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1285 options |= FILE_DELETE_ON_CLOSE;
1286 if (!(attributes & FILE_FLAG_OVERLAPPED))
1287 options |= FILE_SYNCHRONOUS_IO_ALERT;
1288 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1289 options |= FILE_RANDOM_ACCESS;
1290 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1292 attr.Length = sizeof(attr);
1293 attr.RootDirectory = 0;
1294 attr.Attributes = OBJ_CASE_INSENSITIVE;
1295 attr.ObjectName = &nameW;
1296 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1297 attr.SecurityQualityOfService = NULL;
1299 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1301 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1302 sharing, nt_disposition[creation - CREATE_NEW],
1306 WARN("Unable to create file %s (status %lx)\n", debugstr_w(filename), status);
1307 ret = INVALID_HANDLE_VALUE;
1309 /* In the case file creation was rejected due to CREATE_NEW flag
1310 * was specified and file with that name already exists, correct
1311 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1312 * Note: RtlNtStatusToDosError is not the subject to blame here.
1314 if (status == STATUS_OBJECT_NAME_COLLISION)
1315 SetLastError( ERROR_FILE_EXISTS );
1317 SetLastError( RtlNtStatusToDosError(status) );
1319 else SetLastError(0);
1320 RtlFreeUnicodeString( &nameW );
1323 if (!ret) ret = INVALID_HANDLE_VALUE;
1324 TRACE("returning %p\n", ret);
1330 /*************************************************************************
1331 * CreateFileA (KERNEL32.@)
1333 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1334 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1335 DWORD attributes, HANDLE template)
1339 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1340 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1344 /***********************************************************************
1345 * DeleteFileW (KERNEL32.@)
1347 BOOL WINAPI DeleteFileW( LPCWSTR path )
1349 UNICODE_STRING nameW;
1350 OBJECT_ATTRIBUTES attr;
1353 TRACE("%s\n", debugstr_w(path) );
1355 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1357 SetLastError( ERROR_PATH_NOT_FOUND );
1361 attr.Length = sizeof(attr);
1362 attr.RootDirectory = 0;
1363 attr.Attributes = OBJ_CASE_INSENSITIVE;
1364 attr.ObjectName = &nameW;
1365 attr.SecurityDescriptor = NULL;
1366 attr.SecurityQualityOfService = NULL;
1368 status = NtDeleteFile(&attr);
1369 RtlFreeUnicodeString( &nameW );
1372 SetLastError( RtlNtStatusToDosError(status) );
1379 /***********************************************************************
1380 * DeleteFileA (KERNEL32.@)
1382 BOOL WINAPI DeleteFileA( LPCSTR path )
1386 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1387 return DeleteFileW( pathW );
1391 /**************************************************************************
1392 * ReplaceFileW (KERNEL32.@)
1393 * ReplaceFile (KERNEL32.@)
1395 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName,LPCWSTR lpReplacementFileName,
1396 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1397 LPVOID lpExclude, LPVOID lpReserved)
1399 FIXME("(%s,%s,%s,%08lx,%p,%p) stub\n",debugstr_w(lpReplacedFileName),debugstr_w(lpReplacementFileName),
1400 debugstr_w(lpBackupFileName),dwReplaceFlags,lpExclude,lpReserved);
1401 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1406 /**************************************************************************
1407 * ReplaceFileA (KERNEL32.@)
1409 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1410 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1411 LPVOID lpExclude, LPVOID lpReserved)
1413 FIXME("(%s,%s,%s,%08lx,%p,%p) stub\n",lpReplacedFileName,lpReplacementFileName,
1414 lpBackupFileName,dwReplaceFlags,lpExclude,lpReserved);
1415 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1420 /*************************************************************************
1421 * FindFirstFileExW (KERNEL32.@)
1423 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1424 LPVOID data, FINDEX_SEARCH_OPS search_op,
1425 LPVOID filter, DWORD flags)
1428 FIND_FIRST_INFO *info = NULL;
1429 UNICODE_STRING nt_name;
1430 OBJECT_ATTRIBUTES attr;
1434 TRACE("%s %d %p %d %p %lx\n", debugstr_w(filename), level, data, search_op, filter, flags);
1436 if ((search_op != FindExSearchNameMatch) || (flags != 0))
1438 FIXME("options not implemented 0x%08x 0x%08lx\n", search_op, flags );
1439 return INVALID_HANDLE_VALUE;
1441 if (level != FindExInfoStandard)
1443 FIXME("info level %d not implemented\n", level );
1444 return INVALID_HANDLE_VALUE;
1447 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1449 SetLastError( ERROR_PATH_NOT_FOUND );
1450 return INVALID_HANDLE_VALUE;
1453 if (!mask || !*mask)
1455 SetLastError( ERROR_FILE_NOT_FOUND );
1459 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1461 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1465 if (!RtlCreateUnicodeString( &info->mask, mask ))
1467 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1471 /* truncate dir name before mask */
1473 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1475 /* check if path is the root of the drive */
1476 info->is_root = FALSE;
1477 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1478 if (p[0] && p[1] == ':')
1481 while (*p == '\\') p++;
1482 info->is_root = (*p == 0);
1485 attr.Length = sizeof(attr);
1486 attr.RootDirectory = 0;
1487 attr.Attributes = OBJ_CASE_INSENSITIVE;
1488 attr.ObjectName = &nt_name;
1489 attr.SecurityDescriptor = NULL;
1490 attr.SecurityQualityOfService = NULL;
1492 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1493 FILE_SHARE_READ | FILE_SHARE_WRITE,
1494 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1496 if (status != STATUS_SUCCESS)
1498 RtlFreeUnicodeString( &info->mask );
1499 SetLastError( RtlNtStatusToDosError(status) );
1502 RtlFreeUnicodeString( &nt_name );
1504 RtlInitializeCriticalSection( &info->cs );
1505 info->magic = FIND_FIRST_MAGIC;
1509 if (!FindNextFileW( (HANDLE)info, data ))
1511 TRACE( "%s not found\n", debugstr_w(filename) );
1512 FindClose( (HANDLE)info );
1513 SetLastError( ERROR_FILE_NOT_FOUND );
1514 return INVALID_HANDLE_VALUE;
1516 return (HANDLE)info;
1519 HeapFree( GetProcessHeap(), 0, info );
1520 RtlFreeUnicodeString( &nt_name );
1521 return INVALID_HANDLE_VALUE;
1525 /*************************************************************************
1526 * FindNextFileW (KERNEL32.@)
1528 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1530 FIND_FIRST_INFO *info;
1531 FILE_BOTH_DIR_INFORMATION *dir_info;
1534 TRACE("%p %p\n", handle, data);
1536 if (!handle || handle == INVALID_HANDLE_VALUE)
1538 SetLastError( ERROR_INVALID_HANDLE );
1541 info = (FIND_FIRST_INFO *)handle;
1542 if (info->magic != FIND_FIRST_MAGIC)
1544 SetLastError( ERROR_INVALID_HANDLE );
1548 RtlEnterCriticalSection( &info->cs );
1552 if (info->data_pos >= info->data_len) /* need to read some more data */
1556 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1557 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
1560 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1563 info->data_len = io.Information;
1567 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
1569 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
1570 else info->data_pos = info->data_len;
1572 /* don't return '.' and '..' in the root of the drive */
1575 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
1576 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
1577 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
1580 data->dwFileAttributes = dir_info->FileAttributes;
1581 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
1582 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
1583 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
1584 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
1585 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
1586 data->dwReserved0 = 0;
1587 data->dwReserved1 = 0;
1589 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
1590 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1591 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
1592 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
1594 TRACE("returning %s (%s)\n",
1595 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
1601 RtlLeaveCriticalSection( &info->cs );
1606 /*************************************************************************
1607 * FindClose (KERNEL32.@)
1609 BOOL WINAPI FindClose( HANDLE handle )
1611 FIND_FIRST_INFO *info = (FIND_FIRST_INFO *)handle;
1613 if (!handle || handle == INVALID_HANDLE_VALUE)
1615 SetLastError( ERROR_INVALID_HANDLE );
1621 if (info->magic == FIND_FIRST_MAGIC)
1623 RtlEnterCriticalSection( &info->cs );
1624 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
1627 if (info->handle) CloseHandle( info->handle );
1629 RtlFreeUnicodeString( &info->mask );
1630 info->mask.Buffer = NULL;
1633 RtlLeaveCriticalSection( &info->cs );
1634 RtlDeleteCriticalSection( &info->cs );
1635 HeapFree( GetProcessHeap(), 0, info );
1639 __EXCEPT(page_fault)
1641 WARN("Illegal handle %p\n", handle);
1642 SetLastError( ERROR_INVALID_HANDLE );
1651 /*************************************************************************
1652 * FindFirstFileA (KERNEL32.@)
1654 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
1656 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1657 FindExSearchNameMatch, NULL, 0);
1660 /*************************************************************************
1661 * FindFirstFileExA (KERNEL32.@)
1663 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
1664 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
1665 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
1668 WIN32_FIND_DATAA *dataA;
1669 WIN32_FIND_DATAW dataW;
1672 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
1674 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1675 if (handle == INVALID_HANDLE_VALUE) return handle;
1677 dataA = (WIN32_FIND_DATAA *) lpFindFileData;
1678 dataA->dwFileAttributes = dataW.dwFileAttributes;
1679 dataA->ftCreationTime = dataW.ftCreationTime;
1680 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
1681 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
1682 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
1683 dataA->nFileSizeLow = dataW.nFileSizeLow;
1684 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
1685 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
1686 sizeof(dataA->cAlternateFileName) );
1691 /*************************************************************************
1692 * FindFirstFileW (KERNEL32.@)
1694 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1696 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1697 FindExSearchNameMatch, NULL, 0);
1701 /*************************************************************************
1702 * FindNextFileA (KERNEL32.@)
1704 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1706 WIN32_FIND_DATAW dataW;
1708 if (!FindNextFileW( handle, &dataW )) return FALSE;
1709 data->dwFileAttributes = dataW.dwFileAttributes;
1710 data->ftCreationTime = dataW.ftCreationTime;
1711 data->ftLastAccessTime = dataW.ftLastAccessTime;
1712 data->ftLastWriteTime = dataW.ftLastWriteTime;
1713 data->nFileSizeHigh = dataW.nFileSizeHigh;
1714 data->nFileSizeLow = dataW.nFileSizeLow;
1715 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
1716 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
1717 sizeof(data->cAlternateFileName) );
1722 /**************************************************************************
1723 * GetFileAttributesW (KERNEL32.@)
1725 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
1727 FILE_BASIC_INFORMATION info;
1728 UNICODE_STRING nt_name;
1729 OBJECT_ATTRIBUTES attr;
1732 TRACE("%s\n", debugstr_w(name));
1734 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1736 SetLastError( ERROR_PATH_NOT_FOUND );
1737 return INVALID_FILE_ATTRIBUTES;
1740 attr.Length = sizeof(attr);
1741 attr.RootDirectory = 0;
1742 attr.Attributes = OBJ_CASE_INSENSITIVE;
1743 attr.ObjectName = &nt_name;
1744 attr.SecurityDescriptor = NULL;
1745 attr.SecurityQualityOfService = NULL;
1747 status = NtQueryAttributesFile( &attr, &info );
1748 RtlFreeUnicodeString( &nt_name );
1750 if (status == STATUS_SUCCESS) return info.FileAttributes;
1752 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1753 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
1755 SetLastError( RtlNtStatusToDosError(status) );
1756 return INVALID_FILE_ATTRIBUTES;
1760 /**************************************************************************
1761 * GetFileAttributesA (KERNEL32.@)
1763 DWORD WINAPI GetFileAttributesA( LPCSTR name )
1767 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
1768 return GetFileAttributesW( nameW );
1772 /**************************************************************************
1773 * SetFileAttributesW (KERNEL32.@)
1775 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
1777 UNICODE_STRING nt_name;
1778 OBJECT_ATTRIBUTES attr;
1783 TRACE("%s %lx\n", debugstr_w(name), attributes);
1785 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1787 SetLastError( ERROR_PATH_NOT_FOUND );
1791 attr.Length = sizeof(attr);
1792 attr.RootDirectory = 0;
1793 attr.Attributes = OBJ_CASE_INSENSITIVE;
1794 attr.ObjectName = &nt_name;
1795 attr.SecurityDescriptor = NULL;
1796 attr.SecurityQualityOfService = NULL;
1798 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1799 RtlFreeUnicodeString( &nt_name );
1801 if (status == STATUS_SUCCESS)
1803 FILE_BASIC_INFORMATION info;
1805 memset( &info, 0, sizeof(info) );
1806 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
1807 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
1811 if (status == STATUS_SUCCESS) return TRUE;
1812 SetLastError( RtlNtStatusToDosError(status) );
1817 /**************************************************************************
1818 * SetFileAttributesA (KERNEL32.@)
1820 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
1824 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
1825 return SetFileAttributesW( nameW, attributes );
1829 /**************************************************************************
1830 * GetFileAttributesExW (KERNEL32.@)
1832 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
1834 FILE_NETWORK_OPEN_INFORMATION info;
1835 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
1836 UNICODE_STRING nt_name;
1837 OBJECT_ATTRIBUTES attr;
1840 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
1842 if (level != GetFileExInfoStandard)
1844 SetLastError( ERROR_INVALID_PARAMETER );
1848 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1850 SetLastError( ERROR_PATH_NOT_FOUND );
1854 attr.Length = sizeof(attr);
1855 attr.RootDirectory = 0;
1856 attr.Attributes = OBJ_CASE_INSENSITIVE;
1857 attr.ObjectName = &nt_name;
1858 attr.SecurityDescriptor = NULL;
1859 attr.SecurityQualityOfService = NULL;
1861 status = NtQueryFullAttributesFile( &attr, &info );
1862 RtlFreeUnicodeString( &nt_name );
1864 if (status != STATUS_SUCCESS)
1866 SetLastError( RtlNtStatusToDosError(status) );
1870 data->dwFileAttributes = info.FileAttributes;
1871 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
1872 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
1873 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
1874 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
1875 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
1876 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
1877 data->nFileSizeLow = info.EndOfFile.u.LowPart;
1878 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
1883 /**************************************************************************
1884 * GetFileAttributesExA (KERNEL32.@)
1886 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
1890 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
1891 return GetFileAttributesExW( nameW, level, ptr );
1895 /******************************************************************************
1896 * GetCompressedFileSizeW (KERNEL32.@)
1899 * Success: Low-order doubleword of number of bytes
1900 * Failure: INVALID_FILE_SIZE
1902 DWORD WINAPI GetCompressedFileSizeW(
1903 LPCWSTR name, /* [in] Pointer to name of file */
1904 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
1906 UNICODE_STRING nt_name;
1907 OBJECT_ATTRIBUTES attr;
1911 DWORD ret = INVALID_FILE_SIZE;
1913 TRACE("%s %p\n", debugstr_w(name), size_high);
1915 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1917 SetLastError( ERROR_PATH_NOT_FOUND );
1918 return INVALID_FILE_SIZE;
1921 attr.Length = sizeof(attr);
1922 attr.RootDirectory = 0;
1923 attr.Attributes = OBJ_CASE_INSENSITIVE;
1924 attr.ObjectName = &nt_name;
1925 attr.SecurityDescriptor = NULL;
1926 attr.SecurityQualityOfService = NULL;
1928 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1929 RtlFreeUnicodeString( &nt_name );
1931 if (status == STATUS_SUCCESS)
1933 /* we don't support compressed files, simply return the file size */
1934 ret = GetFileSize( handle, size_high );
1937 else SetLastError( RtlNtStatusToDosError(status) );
1943 /******************************************************************************
1944 * GetCompressedFileSizeA (KERNEL32.@)
1946 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
1950 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
1951 return GetCompressedFileSizeW( nameW, size_high );
1955 /***********************************************************************
1956 * OpenFile (KERNEL32.@)
1958 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
1962 WORD filedatetime[2];
1964 if (!ofs) return HFILE_ERROR;
1966 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
1967 ((mode & 0x3 )==OF_READ)?"OF_READ":
1968 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
1969 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
1970 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
1971 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
1972 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
1973 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
1974 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
1975 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
1976 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
1977 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
1978 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
1979 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
1980 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
1981 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
1982 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
1983 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
1987 ofs->cBytes = sizeof(OFSTRUCT);
1989 if (mode & OF_REOPEN) name = ofs->szPathName;
1991 if (!name) return HFILE_ERROR;
1993 TRACE("%s %04x\n", name, mode );
1995 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
1996 Are there any cases where getting the path here is wrong?
1997 Uwe Bonnes 1997 Apr 2 */
1998 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2000 /* OF_PARSE simply fills the structure */
2002 if (mode & OF_PARSE)
2004 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2005 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2009 /* OF_CREATE is completely different from all other options, so
2012 if (mode & OF_CREATE)
2014 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2019 /* Now look for the file */
2021 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2024 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2026 if (mode & OF_DELETE)
2028 if (!DeleteFileA( ofs->szPathName )) goto error;
2029 TRACE("(%s): OF_DELETE return = OK\n", name);
2033 handle = (HANDLE)_lopen( ofs->szPathName, mode );
2034 if (handle == INVALID_HANDLE_VALUE) goto error;
2036 GetFileTime( handle, NULL, NULL, &filetime );
2037 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2038 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2040 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2042 CloseHandle( handle );
2043 WARN("(%s): OF_VERIFY failed\n", name );
2044 /* FIXME: what error here? */
2045 SetLastError( ERROR_FILE_NOT_FOUND );
2049 ofs->Reserved1 = filedatetime[0];
2050 ofs->Reserved2 = filedatetime[1];
2052 TRACE("(%s): OK, return = %p\n", name, handle );
2053 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2055 CloseHandle( handle );
2058 else return (HFILE)handle;
2060 error: /* We get here if there was an error opening the file */
2061 ofs->nErrCode = GetLastError();
2062 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );