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
38 #include "wine/winbase16.h"
39 #include "kernel_private.h"
41 #include "wine/exception.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 #include "wine/server.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(file);
50 HANDLE dos_handles[DOS_TABLE_SIZE];
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 UNICODE_STRING mask; /* file mask */
59 BOOL is_root; /* is directory the root of the drive? */
60 UINT data_pos; /* current position in dir data */
61 UINT data_len; /* length of dir data */
62 BYTE data[8192]; /* directory data */
65 #define FIND_FIRST_MAGIC 0xc0ffee11
67 static BOOL oem_file_apis;
69 static WINE_EXCEPTION_FILTER(page_fault)
71 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
72 return EXCEPTION_EXECUTE_HANDLER;
73 return EXCEPTION_CONTINUE_SEARCH;
77 /***********************************************************************
80 * Wrapper for CreateFile that takes OF_* mode flags.
82 static HANDLE create_file_OF( LPCSTR path, INT mode )
84 DWORD access, sharing, creation;
88 creation = CREATE_ALWAYS;
89 access = GENERIC_READ | GENERIC_WRITE;
93 creation = OPEN_EXISTING;
96 case OF_READ: access = GENERIC_READ; break;
97 case OF_WRITE: access = GENERIC_WRITE; break;
98 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
99 default: access = 0; break;
105 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
106 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
107 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
108 case OF_SHARE_DENY_NONE:
109 case OF_SHARE_COMPAT:
110 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
112 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
116 /***********************************************************************
119 * Set the DOS error code from errno.
121 void FILE_SetDosError(void)
123 int save_errno = errno; /* errno gets overwritten by printf */
125 TRACE("errno = %d %s\n", errno, strerror(errno));
129 SetLastError( ERROR_SHARING_VIOLATION );
132 SetLastError( ERROR_INVALID_HANDLE );
135 SetLastError( ERROR_HANDLE_DISK_FULL );
140 SetLastError( ERROR_ACCESS_DENIED );
143 SetLastError( ERROR_LOCK_VIOLATION );
146 SetLastError( ERROR_FILE_NOT_FOUND );
149 SetLastError( ERROR_CANNOT_MAKE );
153 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
156 SetLastError( ERROR_FILE_EXISTS );
160 SetLastError( ERROR_SEEK );
163 SetLastError( ERROR_DIR_NOT_EMPTY );
166 SetLastError( ERROR_BAD_FORMAT );
169 SetLastError( ERROR_PATH_NOT_FOUND );
172 SetLastError( ERROR_NOT_SAME_DEVICE );
175 WARN("unknown file error: %s\n", strerror(save_errno) );
176 SetLastError( ERROR_GEN_FAILURE );
183 /***********************************************************************
186 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
188 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
189 * there is no possibility for the function to do that twice, taking into
190 * account any called function.
192 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
195 UNICODE_STRING strW, *pstrW;
198 RtlInitAnsiString( &str, name );
199 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
201 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
203 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
204 if (status == STATUS_SUCCESS) return pstrW->Buffer;
206 if (status == STATUS_BUFFER_OVERFLOW)
207 SetLastError( ERROR_FILENAME_EXCED_RANGE );
209 SetLastError( RtlNtStatusToDosError(status) );
214 /***********************************************************************
217 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
219 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
223 if (srclen < 0) srclen = strlenW( src ) + 1;
225 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
227 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
232 /**************************************************************************
233 * SetFileApisToOEM (KERNEL32.@)
235 VOID WINAPI SetFileApisToOEM(void)
237 oem_file_apis = TRUE;
241 /**************************************************************************
242 * SetFileApisToANSI (KERNEL32.@)
244 VOID WINAPI SetFileApisToANSI(void)
246 oem_file_apis = FALSE;
250 /******************************************************************************
251 * AreFileApisANSI (KERNEL32.@)
253 * Determines if file functions are using ANSI
256 * TRUE: Set of file functions is using ANSI code page
257 * FALSE: Set of file functions is using OEM code page
259 BOOL WINAPI AreFileApisANSI(void)
261 return !oem_file_apis;
265 /**************************************************************************
266 * Operations on file handles *
267 **************************************************************************/
269 /***********************************************************************
270 * FILE_InitProcessDosHandles
272 * Allocates the default DOS handles for a process. Called either by
273 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
275 static void FILE_InitProcessDosHandles( void )
277 static BOOL init_done /* = FALSE */;
278 HANDLE cp = GetCurrentProcess();
280 if (init_done) return;
282 DuplicateHandle(cp, GetStdHandle(STD_INPUT_HANDLE), cp, &dos_handles[0],
283 0, TRUE, DUPLICATE_SAME_ACCESS);
284 DuplicateHandle(cp, GetStdHandle(STD_OUTPUT_HANDLE), cp, &dos_handles[1],
285 0, TRUE, DUPLICATE_SAME_ACCESS);
286 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[2],
287 0, TRUE, DUPLICATE_SAME_ACCESS);
288 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[3],
289 0, TRUE, DUPLICATE_SAME_ACCESS);
290 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[4],
291 0, TRUE, DUPLICATE_SAME_ACCESS);
295 /******************************************************************
296 * FILE_ReadWriteApc (internal)
298 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG len)
300 LPOVERLAPPED_COMPLETION_ROUTINE cr = (LPOVERLAPPED_COMPLETION_ROUTINE)apc_user;
302 cr(RtlNtStatusToDosError(io_status->u.Status), len, (LPOVERLAPPED)io_status);
306 /***********************************************************************
307 * ReadFileEx (KERNEL32.@)
309 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
310 LPOVERLAPPED overlapped,
311 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
313 LARGE_INTEGER offset;
315 PIO_STATUS_BLOCK io_status;
317 TRACE("(hFile=%p, buffer=%p, bytes=%lu, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
321 SetLastError(ERROR_INVALID_PARAMETER);
325 offset.u.LowPart = overlapped->u.s.Offset;
326 offset.u.HighPart = overlapped->u.s.OffsetHigh;
327 io_status = (PIO_STATUS_BLOCK)overlapped;
328 io_status->u.Status = STATUS_PENDING;
330 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
331 io_status, buffer, bytesToRead, &offset, NULL);
335 SetLastError( RtlNtStatusToDosError(status) );
342 /***********************************************************************
343 * ReadFile (KERNEL32.@)
345 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
346 LPDWORD bytesRead, LPOVERLAPPED overlapped )
348 LARGE_INTEGER offset;
349 PLARGE_INTEGER poffset = NULL;
350 IO_STATUS_BLOCK iosb;
351 PIO_STATUS_BLOCK io_status = &iosb;
355 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToRead,
356 bytesRead, overlapped );
358 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
359 if (!bytesToRead) return TRUE;
361 if (is_console_handle(hFile))
362 return ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
364 if (overlapped != NULL)
366 offset.u.LowPart = overlapped->u.s.Offset;
367 offset.u.HighPart = overlapped->u.s.OffsetHigh;
369 hEvent = overlapped->hEvent;
370 io_status = (PIO_STATUS_BLOCK)overlapped;
372 io_status->u.Status = STATUS_PENDING;
373 io_status->Information = 0;
375 status = NtReadFile(hFile, hEvent, NULL, NULL, io_status, buffer, bytesToRead, poffset, NULL);
377 if (status != STATUS_PENDING && bytesRead)
378 *bytesRead = io_status->Information;
380 if (status && status != STATUS_END_OF_FILE)
382 SetLastError( RtlNtStatusToDosError(status) );
389 /***********************************************************************
390 * WriteFileEx (KERNEL32.@)
392 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
393 LPOVERLAPPED overlapped,
394 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
396 LARGE_INTEGER offset;
398 PIO_STATUS_BLOCK io_status;
400 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
402 if (overlapped == NULL)
404 SetLastError(ERROR_INVALID_PARAMETER);
407 offset.u.LowPart = overlapped->u.s.Offset;
408 offset.u.HighPart = overlapped->u.s.OffsetHigh;
410 io_status = (PIO_STATUS_BLOCK)overlapped;
411 io_status->u.Status = STATUS_PENDING;
413 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
414 io_status, buffer, bytesToWrite, &offset, NULL);
416 if (status) SetLastError( RtlNtStatusToDosError(status) );
421 /***********************************************************************
422 * WriteFile (KERNEL32.@)
424 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
425 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
427 HANDLE hEvent = NULL;
428 LARGE_INTEGER offset;
429 PLARGE_INTEGER poffset = NULL;
431 IO_STATUS_BLOCK iosb;
432 PIO_STATUS_BLOCK piosb = &iosb;
434 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
436 if (is_console_handle(hFile))
437 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
441 offset.u.LowPart = overlapped->u.s.Offset;
442 offset.u.HighPart = overlapped->u.s.OffsetHigh;
444 hEvent = overlapped->hEvent;
445 piosb = (PIO_STATUS_BLOCK)overlapped;
447 piosb->u.Status = STATUS_PENDING;
448 piosb->Information = 0;
450 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
451 buffer, bytesToWrite, poffset, NULL);
453 /* FIXME: NtWriteFile does not always cause page faults, generate them now */
454 if (status == STATUS_INVALID_USER_BUFFER && !IsBadReadPtr( buffer, bytesToWrite ))
456 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
457 buffer, bytesToWrite, poffset, NULL);
458 if (status != STATUS_INVALID_USER_BUFFER)
459 FIXME("Could not access memory (%p,%ld) at first, now OK. Protected by DIBSection code?\n",
460 buffer, bytesToWrite);
463 if (status != STATUS_PENDING && bytesWritten)
464 *bytesWritten = piosb->Information;
468 SetLastError( RtlNtStatusToDosError(status) );
475 /***********************************************************************
476 * GetOverlappedResult (KERNEL32.@)
478 * Check the result of an Asynchronous data transfer from a file.
481 * HANDLE hFile [in] handle of file to check on
482 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
483 * LPDWORD lpTransferred [in/out] number of bytes transferred
484 * BOOL bWait [in] wait for the transfer to complete ?
490 * If successful (and relevant) lpTransferred will hold the number of
491 * bytes transferred during the async operation.
495 * Currently only works for WaitCommEvent, ReadFile, WriteFile
496 * with communications ports.
499 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
500 LPDWORD lpTransferred, BOOL bWait)
502 DWORD r = WAIT_OBJECT_0;
504 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
506 if ( lpOverlapped == NULL )
508 ERR("lpOverlapped was null\n");
513 if ( lpOverlapped->hEvent )
517 TRACE( "waiting on %p\n", lpOverlapped );
518 r = WaitForSingleObjectEx( lpOverlapped->hEvent, INFINITE, TRUE );
519 TRACE( "wait on %p returned %ld\n", lpOverlapped, r );
520 } while ( r == WAIT_IO_COMPLETION );
525 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
529 else if ( lpOverlapped->Internal == STATUS_PENDING )
531 /* Wait in order to give APCs a chance to run. */
532 /* This is cheating, so we must set the event again in case of success -
533 it may be a non-manual reset event. */
536 TRACE( "waiting on %p\n", lpOverlapped );
537 r = WaitForSingleObjectEx( lpOverlapped->hEvent, 0, TRUE );
538 TRACE( "wait on %p returned %ld\n", lpOverlapped, r );
539 } while ( r == WAIT_IO_COMPLETION );
540 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
541 NtSetEvent( lpOverlapped->hEvent, NULL );
543 if ( r == WAIT_FAILED )
545 WARN("wait operation failed\n");
548 if (lpTransferred) *lpTransferred = lpOverlapped->InternalHigh;
550 switch ( lpOverlapped->Internal )
555 SetLastError( ERROR_IO_INCOMPLETE );
556 if ( bWait ) ERR("PENDING status after waiting!\n");
559 SetLastError( RtlNtStatusToDosError( lpOverlapped->Internal ) );
564 /***********************************************************************
565 * CancelIo (KERNEL32.@)
567 BOOL WINAPI CancelIo(HANDLE handle)
569 IO_STATUS_BLOCK io_status;
571 NtCancelIoFile(handle, &io_status);
572 if (io_status.u.Status)
574 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
580 /***********************************************************************
581 * _hread (KERNEL32.@)
583 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
585 return _lread( hFile, buffer, count );
589 /***********************************************************************
590 * _hwrite (KERNEL32.@)
592 * experimentation yields that _lwrite:
593 * o truncates the file at the current position with
595 * o returns 0 on a 0 length write
596 * o works with console handles
599 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
603 TRACE("%d %p %ld\n", handle, buffer, count );
607 /* Expand or truncate at current position */
608 if (!SetEndOfFile( (HANDLE)handle )) return HFILE_ERROR;
611 if (!WriteFile( (HANDLE)handle, buffer, count, &result, NULL ))
617 /***********************************************************************
618 * _lclose (KERNEL32.@)
620 HFILE WINAPI _lclose( HFILE hFile )
622 TRACE("handle %d\n", hFile );
623 return CloseHandle( (HANDLE)hFile ) ? 0 : HFILE_ERROR;
627 /***********************************************************************
628 * _lcreat (KERNEL32.@)
630 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
632 /* Mask off all flags not explicitly allowed by the doc */
633 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
634 TRACE("%s %02x\n", path, attr );
635 return (HFILE)CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
636 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
637 CREATE_ALWAYS, attr, 0 );
641 /***********************************************************************
642 * _lopen (KERNEL32.@)
644 HFILE WINAPI _lopen( LPCSTR path, INT mode )
646 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
647 return (HFILE)create_file_OF( path, mode & ~OF_CREATE );
650 /***********************************************************************
651 * _lread (KERNEL32.@)
653 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
656 if (!ReadFile( (HANDLE)handle, buffer, count, &result, NULL ))
662 /***********************************************************************
663 * _llseek (KERNEL32.@)
665 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
667 return SetFilePointer( (HANDLE)hFile, lOffset, NULL, nOrigin );
671 /***********************************************************************
672 * _lwrite (KERNEL32.@)
674 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
676 return (UINT)_hwrite( hFile, buffer, (LONG)count );
680 /***********************************************************************
681 * FlushFileBuffers (KERNEL32.@)
683 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
686 IO_STATUS_BLOCK ioblk;
688 if (is_console_handle( hFile ))
690 /* this will fail (as expected) for an output handle */
691 return FlushConsoleInputBuffer( hFile );
693 nts = NtFlushBuffersFile( hFile, &ioblk );
694 if (nts != STATUS_SUCCESS)
696 SetLastError( RtlNtStatusToDosError( nts ) );
704 /***********************************************************************
705 * GetFileType (KERNEL32.@)
707 DWORD WINAPI GetFileType( HANDLE hFile )
709 FILE_FS_DEVICE_INFORMATION info;
713 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
715 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
716 if (status != STATUS_SUCCESS)
718 SetLastError( RtlNtStatusToDosError(status) );
719 return FILE_TYPE_UNKNOWN;
722 switch(info.DeviceType)
724 case FILE_DEVICE_NULL:
725 case FILE_DEVICE_SERIAL_PORT:
726 case FILE_DEVICE_PARALLEL_PORT:
727 case FILE_DEVICE_UNKNOWN:
728 return FILE_TYPE_CHAR;
729 case FILE_DEVICE_NAMED_PIPE:
730 return FILE_TYPE_PIPE;
732 return FILE_TYPE_DISK;
737 /***********************************************************************
738 * GetFileInformationByHandle (KERNEL32.@)
740 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
742 FILE_ALL_INFORMATION all_info;
746 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
747 if (status == STATUS_SUCCESS)
749 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
750 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
751 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
752 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
753 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
754 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
755 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
756 info->dwVolumeSerialNumber = 0; /* FIXME */
757 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
758 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
759 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
760 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
761 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
764 SetLastError( RtlNtStatusToDosError(status) );
769 /***********************************************************************
770 * GetFileSize (KERNEL32.@)
772 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
775 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
776 if (filesizehigh) *filesizehigh = size.u.HighPart;
777 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
778 return size.u.LowPart;
782 /***********************************************************************
783 * GetFileSizeEx (KERNEL32.@)
785 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
787 FILE_END_OF_FILE_INFORMATION info;
791 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileEndOfFileInformation );
792 if (status == STATUS_SUCCESS)
794 *lpFileSize = info.EndOfFile;
797 SetLastError( RtlNtStatusToDosError(status) );
802 /**************************************************************************
803 * SetEndOfFile (KERNEL32.@)
805 BOOL WINAPI SetEndOfFile( HANDLE hFile )
807 FILE_POSITION_INFORMATION pos;
808 FILE_END_OF_FILE_INFORMATION eof;
812 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
813 if (status == STATUS_SUCCESS)
815 eof.EndOfFile = pos.CurrentByteOffset;
816 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
818 if (status == STATUS_SUCCESS) return TRUE;
819 SetLastError( RtlNtStatusToDosError(status) );
824 /***********************************************************************
825 * SetFilePointer (KERNEL32.@)
827 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
829 LARGE_INTEGER dist, newpos;
833 dist.u.LowPart = distance;
834 dist.u.HighPart = *highword;
836 else dist.QuadPart = distance;
838 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
840 if (highword) *highword = newpos.u.HighPart;
841 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
842 return newpos.u.LowPart;
846 /***********************************************************************
847 * SetFilePointerEx (KERNEL32.@)
849 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
850 LARGE_INTEGER *newpos, DWORD method )
852 static const int whence[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
857 TRACE("handle %p offset %s newpos %p origin %ld\n",
858 hFile, wine_dbgstr_longlong(distance.QuadPart), newpos, method );
860 if (method > FILE_END)
862 SetLastError( ERROR_INVALID_PARAMETER );
866 if (!(status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
870 pos = distance.QuadPart;
871 if ((res = lseek( fd, pos, whence[method] )) == (off_t)-1)
873 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
874 if (((errno == EINVAL) || (errno == EPERM)) && (method != FILE_BEGIN) && (pos < 0))
875 SetLastError( ERROR_NEGATIVE_SEEK );
883 newpos->QuadPart = res;
885 wine_server_release_fd( hFile, fd );
887 else SetLastError( RtlNtStatusToDosError(status) );
892 /***********************************************************************
893 * GetFileTime (KERNEL32.@)
895 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
896 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
898 FILE_BASIC_INFORMATION info;
902 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
903 if (status == STATUS_SUCCESS)
907 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
908 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
910 if (lpLastAccessTime)
912 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
913 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
917 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
918 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
922 SetLastError( RtlNtStatusToDosError(status) );
927 /***********************************************************************
928 * SetFileTime (KERNEL32.@)
930 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
931 const FILETIME *atime, const FILETIME *mtime )
933 FILE_BASIC_INFORMATION info;
937 memset( &info, 0, sizeof(info) );
940 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
941 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
945 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
946 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
950 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
951 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
954 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
955 if (status == STATUS_SUCCESS) return TRUE;
956 SetLastError( RtlNtStatusToDosError(status) );
961 /**************************************************************************
962 * LockFile (KERNEL32.@)
964 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
965 DWORD count_low, DWORD count_high )
968 LARGE_INTEGER count, offset;
970 TRACE( "%p %lx%08lx %lx%08lx\n",
971 hFile, offset_high, offset_low, count_high, count_low );
973 count.u.LowPart = count_low;
974 count.u.HighPart = count_high;
975 offset.u.LowPart = offset_low;
976 offset.u.HighPart = offset_high;
978 status = NtLockFile( hFile, 0, NULL, NULL,
979 NULL, &offset, &count, NULL, TRUE, TRUE );
981 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
986 /**************************************************************************
987 * LockFileEx [KERNEL32.@]
989 * Locks a byte range within an open file for shared or exclusive access.
996 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
998 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
999 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1002 LARGE_INTEGER count, offset;
1006 SetLastError( ERROR_INVALID_PARAMETER );
1010 TRACE( "%p %lx%08lx %lx%08lx flags %lx\n",
1011 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1012 count_high, count_low, flags );
1014 count.u.LowPart = count_low;
1015 count.u.HighPart = count_high;
1016 offset.u.LowPart = overlapped->u.s.Offset;
1017 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1019 status = NtLockFile( hFile, overlapped->hEvent, NULL, NULL,
1020 NULL, &offset, &count, NULL,
1021 flags & LOCKFILE_FAIL_IMMEDIATELY,
1022 flags & LOCKFILE_EXCLUSIVE_LOCK );
1024 if (status) SetLastError( RtlNtStatusToDosError(status) );
1029 /**************************************************************************
1030 * UnlockFile (KERNEL32.@)
1032 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1033 DWORD count_low, DWORD count_high )
1036 LARGE_INTEGER count, offset;
1038 count.u.LowPart = count_low;
1039 count.u.HighPart = count_high;
1040 offset.u.LowPart = offset_low;
1041 offset.u.HighPart = offset_high;
1043 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1044 if (status) SetLastError( RtlNtStatusToDosError(status) );
1049 /**************************************************************************
1050 * UnlockFileEx (KERNEL32.@)
1052 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1053 LPOVERLAPPED overlapped )
1057 SetLastError( ERROR_INVALID_PARAMETER );
1060 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1062 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1066 /***********************************************************************
1067 * Win32HandleToDosFileHandle (KERNEL32.21)
1069 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1070 * longer valid after this function (even on failure).
1072 * Note: this is not exactly right, since on Win95 the Win32 handles
1073 * are on top of DOS handles and we do it the other way
1074 * around. Should be good enough though.
1076 HFILE WINAPI Win32HandleToDosFileHandle( HANDLE handle )
1080 if (!handle || (handle == INVALID_HANDLE_VALUE))
1083 FILE_InitProcessDosHandles();
1084 for (i = 0; i < DOS_TABLE_SIZE; i++)
1085 if (!dos_handles[i])
1087 dos_handles[i] = handle;
1088 TRACE("Got %d for h32 %p\n", i, handle );
1091 CloseHandle( handle );
1092 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1097 /***********************************************************************
1098 * DosFileHandleToWin32Handle (KERNEL32.20)
1100 * Return the Win32 handle for a DOS handle.
1102 * Note: this is not exactly right, since on Win95 the Win32 handles
1103 * are on top of DOS handles and we do it the other way
1104 * around. Should be good enough though.
1106 HANDLE WINAPI DosFileHandleToWin32Handle( HFILE handle )
1108 HFILE16 hfile = (HFILE16)handle;
1109 if (hfile < 5) FILE_InitProcessDosHandles();
1110 if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
1112 SetLastError( ERROR_INVALID_HANDLE );
1113 return INVALID_HANDLE_VALUE;
1115 return dos_handles[hfile];
1119 /*************************************************************************
1120 * SetHandleCount (KERNEL32.@)
1122 UINT WINAPI SetHandleCount( UINT count )
1124 return min( 256, count );
1128 /***********************************************************************
1129 * DisposeLZ32Handle (KERNEL32.22)
1131 * Note: this is not entirely correct, we should only close the
1132 * 32-bit handle and not the 16-bit one, but we cannot do
1133 * this because of the way our DOS handles are implemented.
1134 * It shouldn't break anything though.
1136 void WINAPI DisposeLZ32Handle( HANDLE handle )
1140 if (!handle || (handle == INVALID_HANDLE_VALUE)) return;
1142 for (i = 5; i < DOS_TABLE_SIZE; i++)
1143 if (dos_handles[i] == handle)
1146 CloseHandle( handle );
1151 /**************************************************************************
1152 * Operations on file names *
1153 **************************************************************************/
1156 /*************************************************************************
1157 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1159 * Creates or opens an object, and returns a handle that can be used to
1160 * access that object.
1164 * filename [in] pointer to filename to be accessed
1165 * access [in] access mode requested
1166 * sharing [in] share mode
1167 * sa [in] pointer to security attributes
1168 * creation [in] how to create the file
1169 * attributes [in] attributes for newly created file
1170 * template [in] handle to file with extended attributes to copy
1173 * Success: Open handle to specified file
1174 * Failure: INVALID_HANDLE_VALUE
1176 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1177 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1178 DWORD attributes, HANDLE template )
1182 OBJECT_ATTRIBUTES attr;
1183 UNICODE_STRING nameW;
1187 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1188 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1189 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1191 static const UINT nt_disposition[5] =
1193 FILE_CREATE, /* CREATE_NEW */
1194 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1195 FILE_OPEN, /* OPEN_EXISTING */
1196 FILE_OPEN_IF, /* OPEN_ALWAYS */
1197 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1203 if (!filename || !filename[0])
1205 SetLastError( ERROR_PATH_NOT_FOUND );
1206 return INVALID_HANDLE_VALUE;
1209 TRACE("%s %s%s%s%s%s%s creation %ld attributes 0x%lx\n", debugstr_w(filename),
1210 (access & GENERIC_READ)?"GENERIC_READ ":"",
1211 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1212 (!access)?"QUERY_ACCESS ":"",
1213 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1214 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1215 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1216 creation, attributes);
1218 /* Open a console for CONIN$ or CONOUT$ */
1220 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1222 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle), creation);
1226 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1228 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1229 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1231 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1232 !strncmpiW( filename + 4, pipeW, 5 ) ||
1233 !strncmpiW( filename + 4, mailslotW, 9 ))
1237 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1239 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1241 else if (filename[4])
1243 ret = VXD_Open( filename+4, access, sa );
1248 SetLastError( ERROR_INVALID_NAME );
1249 return INVALID_HANDLE_VALUE;
1252 else dosdev = RtlIsDosDeviceName_U( filename );
1256 static const WCHAR conW[] = {'C','O','N'};
1258 if (LOWORD(dosdev) == sizeof(conW) &&
1259 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1261 switch (access & (GENERIC_READ|GENERIC_WRITE))
1264 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), creation);
1267 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), creation);
1270 SetLastError( ERROR_FILE_NOT_FOUND );
1271 return INVALID_HANDLE_VALUE;
1276 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1278 SetLastError( ERROR_INVALID_PARAMETER );
1279 return INVALID_HANDLE_VALUE;
1282 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1284 SetLastError( ERROR_PATH_NOT_FOUND );
1285 return INVALID_HANDLE_VALUE;
1288 /* now call NtCreateFile */
1291 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1292 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1294 options |= FILE_NON_DIRECTORY_FILE;
1295 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1296 options |= FILE_DELETE_ON_CLOSE;
1297 if (!(attributes & FILE_FLAG_OVERLAPPED))
1298 options |= FILE_SYNCHRONOUS_IO_ALERT;
1299 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1300 options |= FILE_RANDOM_ACCESS;
1301 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1303 attr.Length = sizeof(attr);
1304 attr.RootDirectory = 0;
1305 attr.Attributes = OBJ_CASE_INSENSITIVE;
1306 attr.ObjectName = &nameW;
1307 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1308 attr.SecurityQualityOfService = NULL;
1310 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1312 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1313 sharing, nt_disposition[creation - CREATE_NEW],
1317 WARN("Unable to create file %s (status %lx)\n", debugstr_w(filename), status);
1318 ret = INVALID_HANDLE_VALUE;
1320 /* In the case file creation was rejected due to CREATE_NEW flag
1321 * was specified and file with that name already exists, correct
1322 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1323 * Note: RtlNtStatusToDosError is not the subject to blame here.
1325 if (status == STATUS_OBJECT_NAME_COLLISION)
1326 SetLastError( ERROR_FILE_EXISTS );
1328 SetLastError( RtlNtStatusToDosError(status) );
1330 else SetLastError(0);
1331 RtlFreeUnicodeString( &nameW );
1334 if (!ret) ret = INVALID_HANDLE_VALUE;
1335 TRACE("returning %p\n", ret);
1341 /*************************************************************************
1342 * CreateFileA (KERNEL32.@)
1344 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1345 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1346 DWORD attributes, HANDLE template)
1350 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1351 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1355 /***********************************************************************
1356 * DeleteFileW (KERNEL32.@)
1358 BOOL WINAPI DeleteFileW( LPCWSTR path )
1360 UNICODE_STRING nameW;
1361 OBJECT_ATTRIBUTES attr;
1364 TRACE("%s\n", debugstr_w(path) );
1366 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1368 SetLastError( ERROR_PATH_NOT_FOUND );
1372 attr.Length = sizeof(attr);
1373 attr.RootDirectory = 0;
1374 attr.Attributes = OBJ_CASE_INSENSITIVE;
1375 attr.ObjectName = &nameW;
1376 attr.SecurityDescriptor = NULL;
1377 attr.SecurityQualityOfService = NULL;
1379 status = NtDeleteFile(&attr);
1380 RtlFreeUnicodeString( &nameW );
1383 SetLastError( RtlNtStatusToDosError(status) );
1390 /***********************************************************************
1391 * DeleteFileA (KERNEL32.@)
1393 BOOL WINAPI DeleteFileA( LPCSTR path )
1397 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1398 return DeleteFileW( pathW );
1402 /**************************************************************************
1403 * ReplaceFileW (KERNEL32.@)
1404 * ReplaceFile (KERNEL32.@)
1406 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName,LPCWSTR lpReplacementFileName,
1407 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1408 LPVOID lpExclude, LPVOID lpReserved)
1410 FIXME("(%s,%s,%s,%08lx,%p,%p) stub\n",debugstr_w(lpReplacedFileName),debugstr_w(lpReplacementFileName),
1411 debugstr_w(lpBackupFileName),dwReplaceFlags,lpExclude,lpReserved);
1412 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1417 /**************************************************************************
1418 * ReplaceFileA (KERNEL32.@)
1420 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1421 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1422 LPVOID lpExclude, LPVOID lpReserved)
1424 FIXME("(%s,%s,%s,%08lx,%p,%p) stub\n",lpReplacedFileName,lpReplacementFileName,
1425 lpBackupFileName,dwReplaceFlags,lpExclude,lpReserved);
1426 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1431 /*************************************************************************
1432 * FindFirstFileExW (KERNEL32.@)
1434 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1435 LPVOID data, FINDEX_SEARCH_OPS search_op,
1436 LPVOID filter, DWORD flags)
1439 FIND_FIRST_INFO *info = NULL;
1440 UNICODE_STRING nt_name;
1441 OBJECT_ATTRIBUTES attr;
1445 TRACE("%s %d %p %d %p %lx\n", debugstr_w(filename), level, data, search_op, filter, flags);
1447 if ((search_op != FindExSearchNameMatch) || (flags != 0))
1449 FIXME("options not implemented 0x%08x 0x%08lx\n", search_op, flags );
1450 return INVALID_HANDLE_VALUE;
1452 if (level != FindExInfoStandard)
1454 FIXME("info level %d not implemented\n", level );
1455 return INVALID_HANDLE_VALUE;
1458 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1460 SetLastError( ERROR_PATH_NOT_FOUND );
1461 return INVALID_HANDLE_VALUE;
1464 if (!mask || !*mask)
1466 SetLastError( ERROR_FILE_NOT_FOUND );
1470 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1472 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1476 if (!RtlCreateUnicodeString( &info->mask, mask ))
1478 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1482 /* truncate dir name before mask */
1484 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1486 /* check if path is the root of the drive */
1487 info->is_root = FALSE;
1488 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1489 if (p[0] && p[1] == ':')
1492 while (*p == '\\') p++;
1493 info->is_root = (*p == 0);
1496 attr.Length = sizeof(attr);
1497 attr.RootDirectory = 0;
1498 attr.Attributes = OBJ_CASE_INSENSITIVE;
1499 attr.ObjectName = &nt_name;
1500 attr.SecurityDescriptor = NULL;
1501 attr.SecurityQualityOfService = NULL;
1503 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1504 FILE_SHARE_READ | FILE_SHARE_WRITE,
1505 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1507 if (status != STATUS_SUCCESS)
1509 RtlFreeUnicodeString( &info->mask );
1510 SetLastError( RtlNtStatusToDosError(status) );
1513 RtlFreeUnicodeString( &nt_name );
1515 RtlInitializeCriticalSection( &info->cs );
1516 info->magic = FIND_FIRST_MAGIC;
1520 if (!FindNextFileW( (HANDLE)info, data ))
1522 TRACE( "%s not found\n", debugstr_w(filename) );
1523 FindClose( (HANDLE)info );
1524 SetLastError( ERROR_FILE_NOT_FOUND );
1525 return INVALID_HANDLE_VALUE;
1527 return (HANDLE)info;
1530 HeapFree( GetProcessHeap(), 0, info );
1531 RtlFreeUnicodeString( &nt_name );
1532 return INVALID_HANDLE_VALUE;
1536 /*************************************************************************
1537 * FindNextFileW (KERNEL32.@)
1539 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1541 FIND_FIRST_INFO *info;
1542 FILE_BOTH_DIR_INFORMATION *dir_info;
1545 TRACE("%p %p\n", handle, data);
1547 if (!handle || handle == INVALID_HANDLE_VALUE)
1549 SetLastError( ERROR_INVALID_HANDLE );
1552 info = (FIND_FIRST_INFO *)handle;
1553 if (info->magic != FIND_FIRST_MAGIC)
1555 SetLastError( ERROR_INVALID_HANDLE );
1559 RtlEnterCriticalSection( &info->cs );
1563 if (info->data_pos >= info->data_len) /* need to read some more data */
1567 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1568 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
1571 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1574 info->data_len = io.Information;
1578 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
1580 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
1581 else info->data_pos = info->data_len;
1583 /* don't return '.' and '..' in the root of the drive */
1586 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
1587 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
1588 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
1591 data->dwFileAttributes = dir_info->FileAttributes;
1592 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
1593 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
1594 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
1595 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
1596 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
1597 data->dwReserved0 = 0;
1598 data->dwReserved1 = 0;
1600 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
1601 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1602 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
1603 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
1605 TRACE("returning %s (%s)\n",
1606 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
1612 RtlLeaveCriticalSection( &info->cs );
1617 /*************************************************************************
1618 * FindClose (KERNEL32.@)
1620 BOOL WINAPI FindClose( HANDLE handle )
1622 FIND_FIRST_INFO *info = (FIND_FIRST_INFO *)handle;
1624 if (!handle || handle == INVALID_HANDLE_VALUE)
1626 SetLastError( ERROR_INVALID_HANDLE );
1632 if (info->magic == FIND_FIRST_MAGIC)
1634 RtlEnterCriticalSection( &info->cs );
1635 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
1638 if (info->handle) CloseHandle( info->handle );
1640 RtlFreeUnicodeString( &info->mask );
1641 info->mask.Buffer = NULL;
1644 RtlLeaveCriticalSection( &info->cs );
1645 RtlDeleteCriticalSection( &info->cs );
1646 HeapFree( GetProcessHeap(), 0, info );
1650 __EXCEPT(page_fault)
1652 WARN("Illegal handle %p\n", handle);
1653 SetLastError( ERROR_INVALID_HANDLE );
1662 /*************************************************************************
1663 * FindFirstFileA (KERNEL32.@)
1665 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
1667 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1668 FindExSearchNameMatch, NULL, 0);
1671 /*************************************************************************
1672 * FindFirstFileExA (KERNEL32.@)
1674 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
1675 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
1676 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
1679 WIN32_FIND_DATAA *dataA;
1680 WIN32_FIND_DATAW dataW;
1683 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
1685 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1686 if (handle == INVALID_HANDLE_VALUE) return handle;
1688 dataA = (WIN32_FIND_DATAA *) lpFindFileData;
1689 dataA->dwFileAttributes = dataW.dwFileAttributes;
1690 dataA->ftCreationTime = dataW.ftCreationTime;
1691 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
1692 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
1693 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
1694 dataA->nFileSizeLow = dataW.nFileSizeLow;
1695 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
1696 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
1697 sizeof(dataA->cAlternateFileName) );
1702 /*************************************************************************
1703 * FindFirstFileW (KERNEL32.@)
1705 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1707 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1708 FindExSearchNameMatch, NULL, 0);
1712 /*************************************************************************
1713 * FindNextFileA (KERNEL32.@)
1715 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1717 WIN32_FIND_DATAW dataW;
1719 if (!FindNextFileW( handle, &dataW )) return FALSE;
1720 data->dwFileAttributes = dataW.dwFileAttributes;
1721 data->ftCreationTime = dataW.ftCreationTime;
1722 data->ftLastAccessTime = dataW.ftLastAccessTime;
1723 data->ftLastWriteTime = dataW.ftLastWriteTime;
1724 data->nFileSizeHigh = dataW.nFileSizeHigh;
1725 data->nFileSizeLow = dataW.nFileSizeLow;
1726 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
1727 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
1728 sizeof(data->cAlternateFileName) );
1733 /**************************************************************************
1734 * GetFileAttributesW (KERNEL32.@)
1736 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
1738 FILE_BASIC_INFORMATION info;
1739 UNICODE_STRING nt_name;
1740 OBJECT_ATTRIBUTES attr;
1743 TRACE("%s\n", debugstr_w(name));
1745 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1747 SetLastError( ERROR_PATH_NOT_FOUND );
1748 return INVALID_FILE_ATTRIBUTES;
1751 attr.Length = sizeof(attr);
1752 attr.RootDirectory = 0;
1753 attr.Attributes = OBJ_CASE_INSENSITIVE;
1754 attr.ObjectName = &nt_name;
1755 attr.SecurityDescriptor = NULL;
1756 attr.SecurityQualityOfService = NULL;
1758 status = NtQueryAttributesFile( &attr, &info );
1759 RtlFreeUnicodeString( &nt_name );
1761 if (status == STATUS_SUCCESS) return info.FileAttributes;
1763 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1764 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
1766 SetLastError( RtlNtStatusToDosError(status) );
1767 return INVALID_FILE_ATTRIBUTES;
1771 /**************************************************************************
1772 * GetFileAttributesA (KERNEL32.@)
1774 DWORD WINAPI GetFileAttributesA( LPCSTR name )
1778 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
1779 return GetFileAttributesW( nameW );
1783 /**************************************************************************
1784 * SetFileAttributesW (KERNEL32.@)
1786 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
1788 UNICODE_STRING nt_name;
1789 OBJECT_ATTRIBUTES attr;
1794 TRACE("%s %lx\n", debugstr_w(name), attributes);
1796 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1798 SetLastError( ERROR_PATH_NOT_FOUND );
1802 attr.Length = sizeof(attr);
1803 attr.RootDirectory = 0;
1804 attr.Attributes = OBJ_CASE_INSENSITIVE;
1805 attr.ObjectName = &nt_name;
1806 attr.SecurityDescriptor = NULL;
1807 attr.SecurityQualityOfService = NULL;
1809 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1810 RtlFreeUnicodeString( &nt_name );
1812 if (status == STATUS_SUCCESS)
1814 FILE_BASIC_INFORMATION info;
1816 memset( &info, 0, sizeof(info) );
1817 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
1818 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
1822 if (status == STATUS_SUCCESS) return TRUE;
1823 SetLastError( RtlNtStatusToDosError(status) );
1828 /**************************************************************************
1829 * SetFileAttributesA (KERNEL32.@)
1831 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
1835 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
1836 return SetFileAttributesW( nameW, attributes );
1840 /**************************************************************************
1841 * GetFileAttributesExW (KERNEL32.@)
1843 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
1845 FILE_NETWORK_OPEN_INFORMATION info;
1846 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
1847 UNICODE_STRING nt_name;
1848 OBJECT_ATTRIBUTES attr;
1851 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
1853 if (level != GetFileExInfoStandard)
1855 SetLastError( ERROR_INVALID_PARAMETER );
1859 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1861 SetLastError( ERROR_PATH_NOT_FOUND );
1865 attr.Length = sizeof(attr);
1866 attr.RootDirectory = 0;
1867 attr.Attributes = OBJ_CASE_INSENSITIVE;
1868 attr.ObjectName = &nt_name;
1869 attr.SecurityDescriptor = NULL;
1870 attr.SecurityQualityOfService = NULL;
1872 status = NtQueryFullAttributesFile( &attr, &info );
1873 RtlFreeUnicodeString( &nt_name );
1875 if (status != STATUS_SUCCESS)
1877 SetLastError( RtlNtStatusToDosError(status) );
1881 data->dwFileAttributes = info.FileAttributes;
1882 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
1883 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
1884 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
1885 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
1886 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
1887 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
1888 data->nFileSizeLow = info.EndOfFile.u.LowPart;
1889 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
1894 /**************************************************************************
1895 * GetFileAttributesExA (KERNEL32.@)
1897 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
1901 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
1902 return GetFileAttributesExW( nameW, level, ptr );
1906 /******************************************************************************
1907 * GetCompressedFileSizeW (KERNEL32.@)
1910 * Success: Low-order doubleword of number of bytes
1911 * Failure: INVALID_FILE_SIZE
1913 DWORD WINAPI GetCompressedFileSizeW(
1914 LPCWSTR name, /* [in] Pointer to name of file */
1915 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
1917 UNICODE_STRING nt_name;
1918 OBJECT_ATTRIBUTES attr;
1922 DWORD ret = INVALID_FILE_SIZE;
1924 TRACE("%s %p\n", debugstr_w(name), size_high);
1926 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1928 SetLastError( ERROR_PATH_NOT_FOUND );
1929 return INVALID_FILE_SIZE;
1932 attr.Length = sizeof(attr);
1933 attr.RootDirectory = 0;
1934 attr.Attributes = OBJ_CASE_INSENSITIVE;
1935 attr.ObjectName = &nt_name;
1936 attr.SecurityDescriptor = NULL;
1937 attr.SecurityQualityOfService = NULL;
1939 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1940 RtlFreeUnicodeString( &nt_name );
1942 if (status == STATUS_SUCCESS)
1944 /* we don't support compressed files, simply return the file size */
1945 ret = GetFileSize( handle, size_high );
1948 else SetLastError( RtlNtStatusToDosError(status) );
1954 /******************************************************************************
1955 * GetCompressedFileSizeA (KERNEL32.@)
1957 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
1961 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
1962 return GetCompressedFileSizeW( nameW, size_high );
1966 /***********************************************************************
1967 * OpenFile (KERNEL32.@)
1969 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
1973 WORD filedatetime[2];
1975 if (!ofs) return HFILE_ERROR;
1977 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
1978 ((mode & 0x3 )==OF_READ)?"OF_READ":
1979 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
1980 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
1981 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
1982 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
1983 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
1984 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
1985 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
1986 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
1987 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
1988 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
1989 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
1990 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
1991 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
1992 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
1993 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
1994 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
1998 ofs->cBytes = sizeof(OFSTRUCT);
2000 if (mode & OF_REOPEN) name = ofs->szPathName;
2002 if (!name) return HFILE_ERROR;
2004 TRACE("%s %04x\n", name, mode );
2006 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2007 Are there any cases where getting the path here is wrong?
2008 Uwe Bonnes 1997 Apr 2 */
2009 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2011 /* OF_PARSE simply fills the structure */
2013 if (mode & OF_PARSE)
2015 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2016 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2020 /* OF_CREATE is completely different from all other options, so
2023 if (mode & OF_CREATE)
2025 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2030 /* Now look for the file */
2032 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2035 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2037 if (mode & OF_DELETE)
2039 if (!DeleteFileA( ofs->szPathName )) goto error;
2040 TRACE("(%s): OF_DELETE return = OK\n", name);
2044 handle = (HANDLE)_lopen( ofs->szPathName, mode );
2045 if (handle == INVALID_HANDLE_VALUE) goto error;
2047 GetFileTime( handle, NULL, NULL, &filetime );
2048 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2049 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2051 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2053 CloseHandle( handle );
2054 WARN("(%s): OF_VERIFY failed\n", name );
2055 /* FIXME: what error here? */
2056 SetLastError( ERROR_FILE_NOT_FOUND );
2060 ofs->Reserved1 = filedatetime[0];
2061 ofs->Reserved2 = filedatetime[1];
2063 TRACE("(%s): OK, return = %p\n", name, handle );
2064 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2066 CloseHandle( handle );
2069 else return (HFILE)handle;
2071 error: /* We get here if there was an error opening the file */
2072 ofs->nErrCode = GetLastError();
2073 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );