kernel32: Fixed behavior of FindFirstFile for DOS devices.
[wine/wine-kai.git] / dlls / kernel32 / file.c
blob8f8890d2136b4e65f33c902f67a040732024a59e
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include "winerror.h"
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winternl.h"
40 #include "winioctl.h"
41 #include "wincon.h"
42 #include "wine/winbase16.h"
43 #include "kernel_private.h"
45 #include "wine/exception.h"
46 #include "excpt.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
49 #include "thread.h"
50 #include "wine/server.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(file);
54 HANDLE dos_handles[DOS_TABLE_SIZE];
56 /* info structure for FindFirstFile handle */
57 typedef struct
59 DWORD magic; /* magic number */
60 HANDLE handle; /* handle to directory */
61 CRITICAL_SECTION cs; /* crit section protecting this structure */
62 FINDEX_SEARCH_OPS search_op; /* Flags passed to FindFirst. */
63 UNICODE_STRING mask; /* file mask */
64 UNICODE_STRING path; /* NT path used to open the directory */
65 BOOL is_root; /* is directory the root of the drive? */
66 UINT data_pos; /* current position in dir data */
67 UINT data_len; /* length of dir data */
68 BYTE data[8192]; /* directory data */
69 } FIND_FIRST_INFO;
71 #define FIND_FIRST_MAGIC 0xc0ffee11
73 static BOOL oem_file_apis;
76 /***********************************************************************
77 * create_file_OF
79 * Wrapper for CreateFile that takes OF_* mode flags.
81 static HANDLE create_file_OF( LPCSTR path, INT mode )
83 DWORD access, sharing, creation;
85 if (mode & OF_CREATE)
87 creation = CREATE_ALWAYS;
88 access = GENERIC_READ | GENERIC_WRITE;
90 else
92 creation = OPEN_EXISTING;
93 switch(mode & 0x03)
95 case OF_READ: access = GENERIC_READ; break;
96 case OF_WRITE: access = GENERIC_WRITE; break;
97 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
98 default: access = 0; break;
102 switch(mode & 0x70)
104 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
105 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
106 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
107 case OF_SHARE_DENY_NONE:
108 case OF_SHARE_COMPAT:
109 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
111 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
115 /***********************************************************************
116 * check_dir_symlink
118 * Check if a dir symlink should be returned by FindNextFile.
120 static BOOL check_dir_symlink( FIND_FIRST_INFO *info, const FILE_BOTH_DIR_INFORMATION *file_info )
122 UNICODE_STRING str;
123 ANSI_STRING unix_name;
124 struct stat st, parent_st;
125 BOOL ret = TRUE;
126 DWORD len;
128 str.MaximumLength = info->path.Length + sizeof(WCHAR) + file_info->FileNameLength;
129 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength ))) return TRUE;
130 memcpy( str.Buffer, info->path.Buffer, info->path.Length );
131 len = info->path.Length / sizeof(WCHAR);
132 if (!len || str.Buffer[len-1] != '\\') str.Buffer[len++] = '\\';
133 memcpy( str.Buffer + len, file_info->FileName, file_info->FileNameLength );
134 str.Length = len * sizeof(WCHAR) + file_info->FileNameLength;
136 unix_name.Buffer = NULL;
137 if (!wine_nt_to_unix_file_name( &str, &unix_name, OPEN_EXISTING, FALSE ) &&
138 !stat( unix_name.Buffer, &st ))
140 char *p = unix_name.Buffer + unix_name.Length - 1;
142 /* skip trailing slashes */
143 while (p > unix_name.Buffer && *p == '/') p--;
145 while (ret && p > unix_name.Buffer)
147 while (p > unix_name.Buffer && *p != '/') p--;
148 while (p > unix_name.Buffer && *p == '/') p--;
149 p[1] = 0;
150 if (!stat( unix_name.Buffer, &parent_st ) &&
151 parent_st.st_dev == st.st_dev &&
152 parent_st.st_ino == st.st_ino)
154 WARN( "suppressing dir symlink %s pointing to parent %s\n",
155 debugstr_wn( str.Buffer, str.Length/sizeof(WCHAR) ),
156 debugstr_a( unix_name.Buffer ));
157 ret = FALSE;
161 RtlFreeAnsiString( &unix_name );
162 RtlFreeUnicodeString( &str );
163 return ret;
167 /***********************************************************************
168 * FILE_SetDosError
170 * Set the DOS error code from errno.
172 void FILE_SetDosError(void)
174 int save_errno = errno; /* errno gets overwritten by printf */
176 TRACE("errno = %d %s\n", errno, strerror(errno));
177 switch (save_errno)
179 case EAGAIN:
180 SetLastError( ERROR_SHARING_VIOLATION );
181 break;
182 case EBADF:
183 SetLastError( ERROR_INVALID_HANDLE );
184 break;
185 case ENOSPC:
186 SetLastError( ERROR_HANDLE_DISK_FULL );
187 break;
188 case EACCES:
189 case EPERM:
190 case EROFS:
191 SetLastError( ERROR_ACCESS_DENIED );
192 break;
193 case EBUSY:
194 SetLastError( ERROR_LOCK_VIOLATION );
195 break;
196 case ENOENT:
197 SetLastError( ERROR_FILE_NOT_FOUND );
198 break;
199 case EISDIR:
200 SetLastError( ERROR_CANNOT_MAKE );
201 break;
202 case ENFILE:
203 case EMFILE:
204 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
205 break;
206 case EEXIST:
207 SetLastError( ERROR_FILE_EXISTS );
208 break;
209 case EINVAL:
210 case ESPIPE:
211 SetLastError( ERROR_SEEK );
212 break;
213 case ENOTEMPTY:
214 SetLastError( ERROR_DIR_NOT_EMPTY );
215 break;
216 case ENOEXEC:
217 SetLastError( ERROR_BAD_FORMAT );
218 break;
219 case ENOTDIR:
220 SetLastError( ERROR_PATH_NOT_FOUND );
221 break;
222 case EXDEV:
223 SetLastError( ERROR_NOT_SAME_DEVICE );
224 break;
225 default:
226 WARN("unknown file error: %s\n", strerror(save_errno) );
227 SetLastError( ERROR_GEN_FAILURE );
228 break;
230 errno = save_errno;
234 /***********************************************************************
235 * FILE_name_AtoW
237 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
239 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
240 * there is no possibility for the function to do that twice, taking into
241 * account any called function.
243 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
245 ANSI_STRING str;
246 UNICODE_STRING strW, *pstrW;
247 NTSTATUS status;
249 RtlInitAnsiString( &str, name );
250 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
251 if (oem_file_apis)
252 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
253 else
254 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
255 if (status == STATUS_SUCCESS) return pstrW->Buffer;
257 if (status == STATUS_BUFFER_OVERFLOW)
258 SetLastError( ERROR_FILENAME_EXCED_RANGE );
259 else
260 SetLastError( RtlNtStatusToDosError(status) );
261 return NULL;
265 /***********************************************************************
266 * FILE_name_WtoA
268 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
270 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
272 DWORD ret;
274 if (srclen < 0) srclen = strlenW( src ) + 1;
275 if (oem_file_apis)
276 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
277 else
278 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
279 return ret;
283 /**************************************************************************
284 * SetFileApisToOEM (KERNEL32.@)
286 VOID WINAPI SetFileApisToOEM(void)
288 oem_file_apis = TRUE;
292 /**************************************************************************
293 * SetFileApisToANSI (KERNEL32.@)
295 VOID WINAPI SetFileApisToANSI(void)
297 oem_file_apis = FALSE;
301 /******************************************************************************
302 * AreFileApisANSI (KERNEL32.@)
304 * Determines if file functions are using ANSI
306 * RETURNS
307 * TRUE: Set of file functions is using ANSI code page
308 * FALSE: Set of file functions is using OEM code page
310 BOOL WINAPI AreFileApisANSI(void)
312 return !oem_file_apis;
316 /**************************************************************************
317 * Operations on file handles *
318 **************************************************************************/
320 /***********************************************************************
321 * FILE_InitProcessDosHandles
323 * Allocates the default DOS handles for a process. Called either by
324 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
326 static void FILE_InitProcessDosHandles( void )
328 static BOOL init_done /* = FALSE */;
329 HANDLE cp = GetCurrentProcess();
331 if (init_done) return;
332 init_done = TRUE;
333 DuplicateHandle(cp, GetStdHandle(STD_INPUT_HANDLE), cp, &dos_handles[0],
334 0, TRUE, DUPLICATE_SAME_ACCESS);
335 DuplicateHandle(cp, GetStdHandle(STD_OUTPUT_HANDLE), cp, &dos_handles[1],
336 0, TRUE, DUPLICATE_SAME_ACCESS);
337 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[2],
338 0, TRUE, DUPLICATE_SAME_ACCESS);
339 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[3],
340 0, TRUE, DUPLICATE_SAME_ACCESS);
341 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[4],
342 0, TRUE, DUPLICATE_SAME_ACCESS);
346 /******************************************************************
347 * FILE_ReadWriteApc (internal)
349 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG len)
351 LPOVERLAPPED_COMPLETION_ROUTINE cr = (LPOVERLAPPED_COMPLETION_ROUTINE)apc_user;
353 cr(RtlNtStatusToDosError(io_status->u.Status), len, (LPOVERLAPPED)io_status);
357 /***********************************************************************
358 * ReadFileEx (KERNEL32.@)
360 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
361 LPOVERLAPPED overlapped,
362 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
364 LARGE_INTEGER offset;
365 NTSTATUS status;
366 PIO_STATUS_BLOCK io_status;
368 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
370 if (!overlapped)
372 SetLastError(ERROR_INVALID_PARAMETER);
373 return FALSE;
376 offset.u.LowPart = overlapped->u.s.Offset;
377 offset.u.HighPart = overlapped->u.s.OffsetHigh;
378 io_status = (PIO_STATUS_BLOCK)overlapped;
379 io_status->u.Status = STATUS_PENDING;
381 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
382 io_status, buffer, bytesToRead, &offset, NULL);
384 if (status)
386 SetLastError( RtlNtStatusToDosError(status) );
387 return FALSE;
389 return TRUE;
393 /***********************************************************************
394 * ReadFile (KERNEL32.@)
396 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
397 LPDWORD bytesRead, LPOVERLAPPED overlapped )
399 LARGE_INTEGER offset;
400 PLARGE_INTEGER poffset = NULL;
401 IO_STATUS_BLOCK iosb;
402 PIO_STATUS_BLOCK io_status = &iosb;
403 HANDLE hEvent = 0;
404 NTSTATUS status;
406 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToRead,
407 bytesRead, overlapped );
409 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
410 if (!bytesToRead) return TRUE;
412 if (is_console_handle(hFile))
413 return ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
415 if (overlapped != NULL)
417 offset.u.LowPart = overlapped->u.s.Offset;
418 offset.u.HighPart = overlapped->u.s.OffsetHigh;
419 poffset = &offset;
420 hEvent = overlapped->hEvent;
421 io_status = (PIO_STATUS_BLOCK)overlapped;
423 io_status->u.Status = STATUS_PENDING;
424 io_status->Information = 0;
426 status = NtReadFile(hFile, hEvent, NULL, NULL, io_status, buffer, bytesToRead, poffset, NULL);
428 if (status != STATUS_PENDING && bytesRead)
429 *bytesRead = io_status->Information;
431 if (status && status != STATUS_END_OF_FILE && status != STATUS_TIMEOUT)
433 SetLastError( RtlNtStatusToDosError(status) );
434 return FALSE;
436 return TRUE;
440 /***********************************************************************
441 * WriteFileEx (KERNEL32.@)
443 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
444 LPOVERLAPPED overlapped,
445 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
447 LARGE_INTEGER offset;
448 NTSTATUS status;
449 PIO_STATUS_BLOCK io_status;
451 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
453 if (overlapped == NULL)
455 SetLastError(ERROR_INVALID_PARAMETER);
456 return FALSE;
458 offset.u.LowPart = overlapped->u.s.Offset;
459 offset.u.HighPart = overlapped->u.s.OffsetHigh;
461 io_status = (PIO_STATUS_BLOCK)overlapped;
462 io_status->u.Status = STATUS_PENDING;
464 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
465 io_status, buffer, bytesToWrite, &offset, NULL);
467 if (status) SetLastError( RtlNtStatusToDosError(status) );
468 return !status;
472 /***********************************************************************
473 * WriteFile (KERNEL32.@)
475 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
476 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
478 HANDLE hEvent = NULL;
479 LARGE_INTEGER offset;
480 PLARGE_INTEGER poffset = NULL;
481 NTSTATUS status;
482 IO_STATUS_BLOCK iosb;
483 PIO_STATUS_BLOCK piosb = &iosb;
485 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
487 if (is_console_handle(hFile))
488 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
490 if (overlapped)
492 offset.u.LowPart = overlapped->u.s.Offset;
493 offset.u.HighPart = overlapped->u.s.OffsetHigh;
494 poffset = &offset;
495 hEvent = overlapped->hEvent;
496 piosb = (PIO_STATUS_BLOCK)overlapped;
498 piosb->u.Status = STATUS_PENDING;
499 piosb->Information = 0;
501 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
502 buffer, bytesToWrite, poffset, NULL);
504 /* FIXME: NtWriteFile does not always cause page faults, generate them now */
505 if (status == STATUS_INVALID_USER_BUFFER && !IsBadReadPtr( buffer, bytesToWrite ))
507 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
508 buffer, bytesToWrite, poffset, NULL);
509 if (status != STATUS_INVALID_USER_BUFFER)
510 FIXME("Could not access memory (%p,%d) at first, now OK. Protected by DIBSection code?\n",
511 buffer, bytesToWrite);
514 if (status != STATUS_PENDING && bytesWritten)
515 *bytesWritten = piosb->Information;
517 if (status && status != STATUS_TIMEOUT)
519 SetLastError( RtlNtStatusToDosError(status) );
520 return FALSE;
522 return TRUE;
526 /***********************************************************************
527 * GetOverlappedResult (KERNEL32.@)
529 * Check the result of an Asynchronous data transfer from a file.
531 * Parameters
532 * HANDLE hFile [in] handle of file to check on
533 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
534 * LPDWORD lpTransferred [in/out] number of bytes transferred
535 * BOOL bWait [in] wait for the transfer to complete ?
537 * RETURNS
538 * TRUE on success
539 * FALSE on failure
541 * If successful (and relevant) lpTransferred will hold the number of
542 * bytes transferred during the async operation.
544 * BUGS
546 * Currently only works for WaitCommEvent, ReadFile, WriteFile
547 * with communications ports.
550 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
551 LPDWORD lpTransferred, BOOL bWait)
553 DWORD r = WAIT_OBJECT_0;
555 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
557 if ( lpOverlapped == NULL )
559 ERR("lpOverlapped was null\n");
560 return FALSE;
562 if ( bWait )
564 if ( lpOverlapped->hEvent )
568 TRACE( "waiting on %p\n", lpOverlapped );
569 r = WaitForSingleObjectEx( lpOverlapped->hEvent, INFINITE, TRUE );
570 TRACE( "wait on %p returned %d\n", lpOverlapped, r );
571 } while ( r == WAIT_IO_COMPLETION );
573 else
575 /* busy loop */
576 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
577 Sleep( 10 );
580 else if ( lpOverlapped->Internal == STATUS_PENDING )
582 /* Wait in order to give APCs a chance to run. */
583 /* This is cheating, so we must set the event again in case of success -
584 it may be a non-manual reset event. */
587 TRACE( "waiting on %p\n", lpOverlapped );
588 r = WaitForSingleObjectEx( lpOverlapped->hEvent, 0, TRUE );
589 TRACE( "wait on %p returned %d\n", lpOverlapped, r );
590 } while ( r == WAIT_IO_COMPLETION );
591 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
592 NtSetEvent( lpOverlapped->hEvent, NULL );
594 if ( r == WAIT_FAILED )
596 WARN("wait operation failed\n");
597 return FALSE;
599 if (lpTransferred) *lpTransferred = lpOverlapped->InternalHigh;
601 switch ( lpOverlapped->Internal )
603 case STATUS_SUCCESS:
604 return TRUE;
605 case STATUS_PENDING:
606 SetLastError( ERROR_IO_INCOMPLETE );
607 if ( bWait ) ERR("PENDING status after waiting!\n");
608 return FALSE;
609 default:
610 SetLastError( RtlNtStatusToDosError( lpOverlapped->Internal ) );
611 return FALSE;
615 /***********************************************************************
616 * CancelIo (KERNEL32.@)
618 * Cancels pending I/O operations initiated by the current thread on a file.
620 * PARAMS
621 * handle [I] File handle.
623 * RETURNS
624 * Success: TRUE.
625 * Failure: FALSE, check GetLastError().
627 BOOL WINAPI CancelIo(HANDLE handle)
629 IO_STATUS_BLOCK io_status;
631 NtCancelIoFile(handle, &io_status);
632 if (io_status.u.Status)
634 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
635 return FALSE;
637 return TRUE;
640 /***********************************************************************
641 * _hread (KERNEL32.@)
643 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
645 return _lread( hFile, buffer, count );
649 /***********************************************************************
650 * _hwrite (KERNEL32.@)
652 * experimentation yields that _lwrite:
653 * o truncates the file at the current position with
654 * a 0 len write
655 * o returns 0 on a 0 length write
656 * o works with console handles
659 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
661 DWORD result;
663 TRACE("%d %p %d\n", handle, buffer, count );
665 if (!count)
667 /* Expand or truncate at current position */
668 if (!SetEndOfFile( (HANDLE)handle )) return HFILE_ERROR;
669 return 0;
671 if (!WriteFile( (HANDLE)handle, buffer, count, &result, NULL ))
672 return HFILE_ERROR;
673 return result;
677 /***********************************************************************
678 * _lclose (KERNEL32.@)
680 HFILE WINAPI _lclose( HFILE hFile )
682 TRACE("handle %d\n", hFile );
683 return CloseHandle( (HANDLE)hFile ) ? 0 : HFILE_ERROR;
687 /***********************************************************************
688 * _lcreat (KERNEL32.@)
690 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
692 /* Mask off all flags not explicitly allowed by the doc */
693 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
694 TRACE("%s %02x\n", path, attr );
695 return (HFILE)CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
696 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
697 CREATE_ALWAYS, attr, 0 );
701 /***********************************************************************
702 * _lopen (KERNEL32.@)
704 HFILE WINAPI _lopen( LPCSTR path, INT mode )
706 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
707 return (HFILE)create_file_OF( path, mode & ~OF_CREATE );
710 /***********************************************************************
711 * _lread (KERNEL32.@)
713 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
715 DWORD result;
716 if (!ReadFile( (HANDLE)handle, buffer, count, &result, NULL ))
717 return HFILE_ERROR;
718 return result;
722 /***********************************************************************
723 * _llseek (KERNEL32.@)
725 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
727 return SetFilePointer( (HANDLE)hFile, lOffset, NULL, nOrigin );
731 /***********************************************************************
732 * _lwrite (KERNEL32.@)
734 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
736 return (UINT)_hwrite( hFile, buffer, (LONG)count );
740 /***********************************************************************
741 * FlushFileBuffers (KERNEL32.@)
743 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
745 NTSTATUS nts;
746 IO_STATUS_BLOCK ioblk;
748 if (is_console_handle( hFile ))
750 /* this will fail (as expected) for an output handle */
751 return FlushConsoleInputBuffer( hFile );
753 nts = NtFlushBuffersFile( hFile, &ioblk );
754 if (nts != STATUS_SUCCESS)
756 SetLastError( RtlNtStatusToDosError( nts ) );
757 return FALSE;
760 return TRUE;
764 /***********************************************************************
765 * GetFileType (KERNEL32.@)
767 DWORD WINAPI GetFileType( HANDLE hFile )
769 FILE_FS_DEVICE_INFORMATION info;
770 IO_STATUS_BLOCK io;
771 NTSTATUS status;
773 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
775 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
776 if (status != STATUS_SUCCESS)
778 SetLastError( RtlNtStatusToDosError(status) );
779 return FILE_TYPE_UNKNOWN;
782 switch(info.DeviceType)
784 case FILE_DEVICE_NULL:
785 case FILE_DEVICE_SERIAL_PORT:
786 case FILE_DEVICE_PARALLEL_PORT:
787 case FILE_DEVICE_TAPE:
788 case FILE_DEVICE_UNKNOWN:
789 return FILE_TYPE_CHAR;
790 case FILE_DEVICE_NAMED_PIPE:
791 return FILE_TYPE_PIPE;
792 default:
793 return FILE_TYPE_DISK;
798 /***********************************************************************
799 * GetFileInformationByHandle (KERNEL32.@)
801 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
803 FILE_ALL_INFORMATION all_info;
804 IO_STATUS_BLOCK io;
805 NTSTATUS status;
807 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
808 if (status == STATUS_SUCCESS)
810 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
811 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
812 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
813 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
814 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
815 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
816 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
817 info->dwVolumeSerialNumber = 0; /* FIXME */
818 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
819 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
820 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
821 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
822 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
823 return TRUE;
825 SetLastError( RtlNtStatusToDosError(status) );
826 return FALSE;
830 /***********************************************************************
831 * GetFileSize (KERNEL32.@)
833 * Retrieve the size of a file.
835 * PARAMS
836 * hFile [I] File to retrieve size of.
837 * filesizehigh [O] On return, the high bits of the file size.
839 * RETURNS
840 * Success: The low bits of the file size.
841 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
842 * check GetLastError() for values other than ERROR_SUCCESS.
844 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
846 LARGE_INTEGER size;
847 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
848 if (filesizehigh) *filesizehigh = size.u.HighPart;
849 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
850 return size.u.LowPart;
854 /***********************************************************************
855 * GetFileSizeEx (KERNEL32.@)
857 * Retrieve the size of a file.
859 * PARAMS
860 * hFile [I] File to retrieve size of.
861 * lpFileSIze [O] On return, the size of the file.
863 * RETURNS
864 * Success: TRUE.
865 * Failure: FALSE, check GetLastError().
867 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
869 FILE_END_OF_FILE_INFORMATION info;
870 IO_STATUS_BLOCK io;
871 NTSTATUS status;
873 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileEndOfFileInformation );
874 if (status == STATUS_SUCCESS)
876 *lpFileSize = info.EndOfFile;
877 return TRUE;
879 SetLastError( RtlNtStatusToDosError(status) );
880 return FALSE;
884 /**************************************************************************
885 * SetEndOfFile (KERNEL32.@)
887 * Sets the current position as the end of the file.
889 * PARAMS
890 * hFile [I] File handle.
892 * RETURNS
893 * Success: TRUE.
894 * Failure: FALSE, check GetLastError().
896 BOOL WINAPI SetEndOfFile( HANDLE hFile )
898 FILE_POSITION_INFORMATION pos;
899 FILE_END_OF_FILE_INFORMATION eof;
900 IO_STATUS_BLOCK io;
901 NTSTATUS status;
903 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
904 if (status == STATUS_SUCCESS)
906 eof.EndOfFile = pos.CurrentByteOffset;
907 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
909 if (status == STATUS_SUCCESS) return TRUE;
910 SetLastError( RtlNtStatusToDosError(status) );
911 return FALSE;
915 /***********************************************************************
916 * SetFilePointer (KERNEL32.@)
918 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
920 LARGE_INTEGER dist, newpos;
922 if (highword)
924 dist.u.LowPart = distance;
925 dist.u.HighPart = *highword;
927 else dist.QuadPart = distance;
929 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
931 if (highword) *highword = newpos.u.HighPart;
932 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
933 return newpos.u.LowPart;
937 /***********************************************************************
938 * SetFilePointerEx (KERNEL32.@)
940 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
941 LARGE_INTEGER *newpos, DWORD method )
943 static const int whence[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
944 BOOL ret = FALSE;
945 NTSTATUS status;
946 int fd;
948 TRACE("handle %p offset %s newpos %p origin %d\n",
949 hFile, wine_dbgstr_longlong(distance.QuadPart), newpos, method );
951 if (method > FILE_END)
953 SetLastError( ERROR_INVALID_PARAMETER );
954 return ret;
957 if (!(status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
959 off_t pos, res;
961 pos = distance.QuadPart;
962 if ((res = lseek( fd, pos, whence[method] )) == (off_t)-1)
964 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
965 if (((errno == EINVAL) || (errno == EPERM)) && (method != FILE_BEGIN) && (pos < 0))
966 SetLastError( ERROR_NEGATIVE_SEEK );
967 else
968 FILE_SetDosError();
970 else
972 ret = TRUE;
973 if( newpos )
974 newpos->QuadPart = res;
976 wine_server_release_fd( hFile, fd );
978 else SetLastError( RtlNtStatusToDosError(status) );
980 return ret;
983 /***********************************************************************
984 * GetFileTime (KERNEL32.@)
986 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
987 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
989 FILE_BASIC_INFORMATION info;
990 IO_STATUS_BLOCK io;
991 NTSTATUS status;
993 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
994 if (status == STATUS_SUCCESS)
996 if (lpCreationTime)
998 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
999 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
1001 if (lpLastAccessTime)
1003 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
1004 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
1006 if (lpLastWriteTime)
1008 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
1009 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1011 return TRUE;
1013 SetLastError( RtlNtStatusToDosError(status) );
1014 return FALSE;
1018 /***********************************************************************
1019 * SetFileTime (KERNEL32.@)
1021 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1022 const FILETIME *atime, const FILETIME *mtime )
1024 FILE_BASIC_INFORMATION info;
1025 IO_STATUS_BLOCK io;
1026 NTSTATUS status;
1028 memset( &info, 0, sizeof(info) );
1029 if (ctime)
1031 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1032 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1034 if (atime)
1036 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1037 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1039 if (mtime)
1041 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1042 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1045 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1046 if (status == STATUS_SUCCESS) return TRUE;
1047 SetLastError( RtlNtStatusToDosError(status) );
1048 return FALSE;
1052 /**************************************************************************
1053 * LockFile (KERNEL32.@)
1055 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1056 DWORD count_low, DWORD count_high )
1058 NTSTATUS status;
1059 LARGE_INTEGER count, offset;
1061 TRACE( "%p %x%08x %x%08x\n",
1062 hFile, offset_high, offset_low, count_high, count_low );
1064 count.u.LowPart = count_low;
1065 count.u.HighPart = count_high;
1066 offset.u.LowPart = offset_low;
1067 offset.u.HighPart = offset_high;
1069 status = NtLockFile( hFile, 0, NULL, NULL,
1070 NULL, &offset, &count, NULL, TRUE, TRUE );
1072 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1073 return !status;
1077 /**************************************************************************
1078 * LockFileEx [KERNEL32.@]
1080 * Locks a byte range within an open file for shared or exclusive access.
1082 * RETURNS
1083 * success: TRUE
1084 * failure: FALSE
1086 * NOTES
1087 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1089 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1090 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1092 NTSTATUS status;
1093 LARGE_INTEGER count, offset;
1095 if (reserved)
1097 SetLastError( ERROR_INVALID_PARAMETER );
1098 return FALSE;
1101 TRACE( "%p %x%08x %x%08x flags %x\n",
1102 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1103 count_high, count_low, flags );
1105 count.u.LowPart = count_low;
1106 count.u.HighPart = count_high;
1107 offset.u.LowPart = overlapped->u.s.Offset;
1108 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1110 status = NtLockFile( hFile, overlapped->hEvent, NULL, NULL,
1111 NULL, &offset, &count, NULL,
1112 flags & LOCKFILE_FAIL_IMMEDIATELY,
1113 flags & LOCKFILE_EXCLUSIVE_LOCK );
1115 if (status) SetLastError( RtlNtStatusToDosError(status) );
1116 return !status;
1120 /**************************************************************************
1121 * UnlockFile (KERNEL32.@)
1123 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1124 DWORD count_low, DWORD count_high )
1126 NTSTATUS status;
1127 LARGE_INTEGER count, offset;
1129 count.u.LowPart = count_low;
1130 count.u.HighPart = count_high;
1131 offset.u.LowPart = offset_low;
1132 offset.u.HighPart = offset_high;
1134 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1135 if (status) SetLastError( RtlNtStatusToDosError(status) );
1136 return !status;
1140 /**************************************************************************
1141 * UnlockFileEx (KERNEL32.@)
1143 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1144 LPOVERLAPPED overlapped )
1146 if (reserved)
1148 SetLastError( ERROR_INVALID_PARAMETER );
1149 return FALSE;
1151 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1153 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1157 /***********************************************************************
1158 * Win32HandleToDosFileHandle (KERNEL32.21)
1160 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1161 * longer valid after this function (even on failure).
1163 * Note: this is not exactly right, since on Win95 the Win32 handles
1164 * are on top of DOS handles and we do it the other way
1165 * around. Should be good enough though.
1167 HFILE WINAPI Win32HandleToDosFileHandle( HANDLE handle )
1169 int i;
1171 if (!handle || (handle == INVALID_HANDLE_VALUE))
1172 return HFILE_ERROR;
1174 FILE_InitProcessDosHandles();
1175 for (i = 0; i < DOS_TABLE_SIZE; i++)
1176 if (!dos_handles[i])
1178 dos_handles[i] = handle;
1179 TRACE("Got %d for h32 %p\n", i, handle );
1180 return (HFILE)i;
1182 CloseHandle( handle );
1183 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1184 return HFILE_ERROR;
1188 /***********************************************************************
1189 * DosFileHandleToWin32Handle (KERNEL32.20)
1191 * Return the Win32 handle for a DOS handle.
1193 * Note: this is not exactly right, since on Win95 the Win32 handles
1194 * are on top of DOS handles and we do it the other way
1195 * around. Should be good enough though.
1197 HANDLE WINAPI DosFileHandleToWin32Handle( HFILE handle )
1199 HFILE16 hfile = (HFILE16)handle;
1200 if (hfile < 5) FILE_InitProcessDosHandles();
1201 if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
1203 SetLastError( ERROR_INVALID_HANDLE );
1204 return INVALID_HANDLE_VALUE;
1206 return dos_handles[hfile];
1210 /*************************************************************************
1211 * SetHandleCount (KERNEL32.@)
1213 UINT WINAPI SetHandleCount( UINT count )
1215 return min( 256, count );
1219 /***********************************************************************
1220 * DisposeLZ32Handle (KERNEL32.22)
1222 * Note: this is not entirely correct, we should only close the
1223 * 32-bit handle and not the 16-bit one, but we cannot do
1224 * this because of the way our DOS handles are implemented.
1225 * It shouldn't break anything though.
1227 void WINAPI DisposeLZ32Handle( HANDLE handle )
1229 int i;
1231 if (!handle || (handle == INVALID_HANDLE_VALUE)) return;
1233 for (i = 5; i < DOS_TABLE_SIZE; i++)
1234 if (dos_handles[i] == handle)
1236 dos_handles[i] = 0;
1237 CloseHandle( handle );
1238 break;
1242 /**************************************************************************
1243 * Operations on file names *
1244 **************************************************************************/
1247 /*************************************************************************
1248 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1250 * Creates or opens an object, and returns a handle that can be used to
1251 * access that object.
1253 * PARAMS
1255 * filename [in] pointer to filename to be accessed
1256 * access [in] access mode requested
1257 * sharing [in] share mode
1258 * sa [in] pointer to security attributes
1259 * creation [in] how to create the file
1260 * attributes [in] attributes for newly created file
1261 * template [in] handle to file with extended attributes to copy
1263 * RETURNS
1264 * Success: Open handle to specified file
1265 * Failure: INVALID_HANDLE_VALUE
1267 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1268 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1269 DWORD attributes, HANDLE template )
1271 NTSTATUS status;
1272 UINT options;
1273 OBJECT_ATTRIBUTES attr;
1274 UNICODE_STRING nameW;
1275 IO_STATUS_BLOCK io;
1276 HANDLE ret;
1277 DWORD dosdev;
1278 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1279 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1280 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1282 static const UINT nt_disposition[5] =
1284 FILE_CREATE, /* CREATE_NEW */
1285 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1286 FILE_OPEN, /* OPEN_EXISTING */
1287 FILE_OPEN_IF, /* OPEN_ALWAYS */
1288 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1292 /* sanity checks */
1294 if (!filename || !filename[0])
1296 SetLastError( ERROR_PATH_NOT_FOUND );
1297 return INVALID_HANDLE_VALUE;
1300 TRACE("%s %s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1301 (access & GENERIC_READ)?"GENERIC_READ ":"",
1302 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1303 (!access)?"QUERY_ACCESS ":"",
1304 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1305 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1306 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1307 creation, attributes);
1309 /* Open a console for CONIN$ or CONOUT$ */
1311 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1313 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle), creation);
1314 goto done;
1317 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1319 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1320 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1322 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1323 !strncmpiW( filename + 4, pipeW, 5 ) ||
1324 !strncmpiW( filename + 4, mailslotW, 9 ))
1326 dosdev = 0;
1328 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1330 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1332 else if (!(GetVersion() & 0x80000000))
1334 dosdev = 0;
1336 else if (filename[4])
1338 ret = VXD_Open( filename+4, access, sa );
1339 goto done;
1341 else
1343 SetLastError( ERROR_INVALID_NAME );
1344 return INVALID_HANDLE_VALUE;
1347 else dosdev = RtlIsDosDeviceName_U( filename );
1349 if (dosdev)
1351 static const WCHAR conW[] = {'C','O','N'};
1353 if (LOWORD(dosdev) == sizeof(conW) &&
1354 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1356 switch (access & (GENERIC_READ|GENERIC_WRITE))
1358 case GENERIC_READ:
1359 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), creation);
1360 goto done;
1361 case GENERIC_WRITE:
1362 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), creation);
1363 goto done;
1364 default:
1365 SetLastError( ERROR_FILE_NOT_FOUND );
1366 return INVALID_HANDLE_VALUE;
1371 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1373 SetLastError( ERROR_INVALID_PARAMETER );
1374 return INVALID_HANDLE_VALUE;
1377 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1379 SetLastError( ERROR_PATH_NOT_FOUND );
1380 return INVALID_HANDLE_VALUE;
1383 /* now call NtCreateFile */
1385 options = 0;
1386 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1387 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1388 else
1389 options |= FILE_NON_DIRECTORY_FILE;
1390 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1392 options |= FILE_DELETE_ON_CLOSE;
1393 access |= DELETE;
1395 if (!(attributes & FILE_FLAG_OVERLAPPED))
1396 options |= FILE_SYNCHRONOUS_IO_ALERT;
1397 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1398 options |= FILE_RANDOM_ACCESS;
1399 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1401 attr.Length = sizeof(attr);
1402 attr.RootDirectory = 0;
1403 attr.Attributes = OBJ_CASE_INSENSITIVE;
1404 attr.ObjectName = &nameW;
1405 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1406 attr.SecurityQualityOfService = NULL;
1408 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1410 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1411 sharing, nt_disposition[creation - CREATE_NEW],
1412 options, NULL, 0 );
1413 if (status)
1415 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1416 ret = INVALID_HANDLE_VALUE;
1418 /* In the case file creation was rejected due to CREATE_NEW flag
1419 * was specified and file with that name already exists, correct
1420 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1421 * Note: RtlNtStatusToDosError is not the subject to blame here.
1423 if (status == STATUS_OBJECT_NAME_COLLISION)
1424 SetLastError( ERROR_FILE_EXISTS );
1425 else
1426 SetLastError( RtlNtStatusToDosError(status) );
1428 else SetLastError(0);
1429 RtlFreeUnicodeString( &nameW );
1431 done:
1432 if (!ret) ret = INVALID_HANDLE_VALUE;
1433 TRACE("returning %p\n", ret);
1434 return ret;
1439 /*************************************************************************
1440 * CreateFileA (KERNEL32.@)
1442 * See CreateFileW.
1444 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1445 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1446 DWORD attributes, HANDLE template)
1448 WCHAR *nameW;
1450 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1451 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1455 /***********************************************************************
1456 * DeleteFileW (KERNEL32.@)
1458 * Delete a file.
1460 * PARAMS
1461 * path [I] Path to the file to delete.
1463 * RETURNS
1464 * Success: TRUE.
1465 * Failure: FALSE, check GetLastError().
1467 BOOL WINAPI DeleteFileW( LPCWSTR path )
1469 UNICODE_STRING nameW;
1470 OBJECT_ATTRIBUTES attr;
1471 NTSTATUS status;
1473 TRACE("%s\n", debugstr_w(path) );
1475 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1477 SetLastError( ERROR_PATH_NOT_FOUND );
1478 return FALSE;
1481 attr.Length = sizeof(attr);
1482 attr.RootDirectory = 0;
1483 attr.Attributes = OBJ_CASE_INSENSITIVE;
1484 attr.ObjectName = &nameW;
1485 attr.SecurityDescriptor = NULL;
1486 attr.SecurityQualityOfService = NULL;
1488 status = NtDeleteFile(&attr);
1489 RtlFreeUnicodeString( &nameW );
1490 if (status)
1492 SetLastError( RtlNtStatusToDosError(status) );
1493 return FALSE;
1495 return TRUE;
1499 /***********************************************************************
1500 * DeleteFileA (KERNEL32.@)
1502 * See DeleteFileW.
1504 BOOL WINAPI DeleteFileA( LPCSTR path )
1506 WCHAR *pathW;
1508 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1509 return DeleteFileW( pathW );
1513 /**************************************************************************
1514 * ReplaceFileW (KERNEL32.@)
1515 * ReplaceFile (KERNEL32.@)
1517 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName,LPCWSTR lpReplacementFileName,
1518 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1519 LPVOID lpExclude, LPVOID lpReserved)
1521 FIXME("(%s,%s,%s,%08x,%p,%p) stub\n",debugstr_w(lpReplacedFileName),debugstr_w(lpReplacementFileName),
1522 debugstr_w(lpBackupFileName),dwReplaceFlags,lpExclude,lpReserved);
1523 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1524 return FALSE;
1528 /**************************************************************************
1529 * ReplaceFileA (KERNEL32.@)
1531 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1532 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1533 LPVOID lpExclude, LPVOID lpReserved)
1535 FIXME("(%s,%s,%s,%08x,%p,%p) stub\n",lpReplacedFileName,lpReplacementFileName,
1536 lpBackupFileName,dwReplaceFlags,lpExclude,lpReserved);
1537 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1538 return FALSE;
1542 /*************************************************************************
1543 * FindFirstFileExW (KERNEL32.@)
1545 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1546 LPVOID data, FINDEX_SEARCH_OPS search_op,
1547 LPVOID filter, DWORD flags)
1549 static const WCHAR wildcardsW[] = { '*','?',0 };
1550 WCHAR *mask, *p;
1551 FIND_FIRST_INFO *info = NULL;
1552 UNICODE_STRING nt_name;
1553 OBJECT_ATTRIBUTES attr;
1554 IO_STATUS_BLOCK io;
1555 NTSTATUS status;
1556 DWORD device = 0;
1558 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1560 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1561 || flags != 0)
1563 FIXME("options not implemented 0x%08x 0x%08x\n", search_op, flags );
1564 return INVALID_HANDLE_VALUE;
1566 if (level != FindExInfoStandard)
1568 FIXME("info level %d not implemented\n", level );
1569 return INVALID_HANDLE_VALUE;
1572 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1574 SetLastError( ERROR_PATH_NOT_FOUND );
1575 return INVALID_HANDLE_VALUE;
1578 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1580 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1581 goto error;
1584 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1586 static const WCHAR dotW[] = {'.',0};
1587 WCHAR *dir = NULL;
1589 /* we still need to check that the directory can be opened */
1591 if (HIWORD(device))
1593 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1595 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1596 goto error;
1598 memcpy( dir, filename, HIWORD(device) );
1599 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1601 RtlFreeUnicodeString( &nt_name );
1602 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1604 HeapFree( GetProcessHeap(), 0, dir );
1605 SetLastError( ERROR_PATH_NOT_FOUND );
1606 goto error;
1608 HeapFree( GetProcessHeap(), 0, dir );
1609 RtlInitUnicodeString( &info->mask, NULL );
1611 else if (!mask || !*mask)
1613 SetLastError( ERROR_FILE_NOT_FOUND );
1614 goto error;
1616 else
1618 if (!RtlCreateUnicodeString( &info->mask, mask ))
1620 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1621 goto error;
1624 /* truncate dir name before mask */
1625 *mask = 0;
1626 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1629 /* check if path is the root of the drive */
1630 info->is_root = FALSE;
1631 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1632 if (p[0] && p[1] == ':')
1634 p += 2;
1635 while (*p == '\\') p++;
1636 info->is_root = (*p == 0);
1639 attr.Length = sizeof(attr);
1640 attr.RootDirectory = 0;
1641 attr.Attributes = OBJ_CASE_INSENSITIVE;
1642 attr.ObjectName = &nt_name;
1643 attr.SecurityDescriptor = NULL;
1644 attr.SecurityQualityOfService = NULL;
1646 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1647 FILE_SHARE_READ | FILE_SHARE_WRITE,
1648 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1650 if (status != STATUS_SUCCESS)
1652 RtlFreeUnicodeString( &info->mask );
1653 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1654 SetLastError( ERROR_PATH_NOT_FOUND );
1655 else
1656 SetLastError( RtlNtStatusToDosError(status) );
1657 goto error;
1660 RtlInitializeCriticalSection( &info->cs );
1661 info->path = nt_name;
1662 info->magic = FIND_FIRST_MAGIC;
1663 info->data_pos = 0;
1664 info->data_len = 0;
1665 info->search_op = search_op;
1667 if (device)
1669 WIN32_FIND_DATAW *wfd = data;
1671 memset( wfd, 0, sizeof(*wfd) );
1672 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1673 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1674 CloseHandle( info->handle );
1675 info->handle = 0;
1677 else if (!FindNextFileW( (HANDLE)info, data ))
1679 TRACE( "%s not found\n", debugstr_w(filename) );
1680 FindClose( (HANDLE)info );
1681 SetLastError( ERROR_FILE_NOT_FOUND );
1682 return INVALID_HANDLE_VALUE;
1684 else if (!strpbrkW( info->mask.Buffer, wildcardsW ))
1686 /* we can't find two files with the same name */
1687 CloseHandle( info->handle );
1688 info->handle = 0;
1690 return (HANDLE)info;
1692 error:
1693 HeapFree( GetProcessHeap(), 0, info );
1694 RtlFreeUnicodeString( &nt_name );
1695 return INVALID_HANDLE_VALUE;
1699 /*************************************************************************
1700 * FindNextFileW (KERNEL32.@)
1702 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1704 FIND_FIRST_INFO *info;
1705 FILE_BOTH_DIR_INFORMATION *dir_info;
1706 BOOL ret = FALSE;
1708 TRACE("%p %p\n", handle, data);
1710 if (!handle || handle == INVALID_HANDLE_VALUE)
1712 SetLastError( ERROR_INVALID_HANDLE );
1713 return ret;
1715 info = (FIND_FIRST_INFO *)handle;
1716 if (info->magic != FIND_FIRST_MAGIC)
1718 SetLastError( ERROR_INVALID_HANDLE );
1719 return ret;
1722 RtlEnterCriticalSection( &info->cs );
1724 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
1725 else for (;;)
1727 if (info->data_pos >= info->data_len) /* need to read some more data */
1729 IO_STATUS_BLOCK io;
1731 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1732 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
1733 if (io.u.Status)
1735 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1736 if (io.u.Status == STATUS_NO_MORE_FILES)
1738 CloseHandle( info->handle );
1739 info->handle = 0;
1741 break;
1743 info->data_len = io.Information;
1744 info->data_pos = 0;
1747 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
1749 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
1750 else info->data_pos = info->data_len;
1752 /* don't return '.' and '..' in the root of the drive */
1753 if (info->is_root)
1755 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
1756 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
1757 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
1760 /* check for dir symlink */
1761 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
1762 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
1764 if (!check_dir_symlink( info, dir_info )) continue;
1766 if (info->search_op == FindExSearchLimitToDirectories &&
1767 (dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
1768 continue;
1770 data->dwFileAttributes = dir_info->FileAttributes;
1771 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
1772 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
1773 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
1774 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
1775 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
1776 data->dwReserved0 = 0;
1777 data->dwReserved1 = 0;
1779 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
1780 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1781 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
1782 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
1784 TRACE("returning %s (%s)\n",
1785 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
1787 ret = TRUE;
1788 break;
1791 RtlLeaveCriticalSection( &info->cs );
1792 return ret;
1796 /*************************************************************************
1797 * FindClose (KERNEL32.@)
1799 BOOL WINAPI FindClose( HANDLE handle )
1801 FIND_FIRST_INFO *info = (FIND_FIRST_INFO *)handle;
1803 if (!handle || handle == INVALID_HANDLE_VALUE)
1805 SetLastError( ERROR_INVALID_HANDLE );
1806 return FALSE;
1809 __TRY
1811 if (info->magic == FIND_FIRST_MAGIC)
1813 RtlEnterCriticalSection( &info->cs );
1814 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
1816 info->magic = 0;
1817 if (info->handle) CloseHandle( info->handle );
1818 info->handle = 0;
1819 RtlFreeUnicodeString( &info->mask );
1820 info->mask.Buffer = NULL;
1821 RtlFreeUnicodeString( &info->path );
1822 info->data_pos = 0;
1823 info->data_len = 0;
1824 RtlLeaveCriticalSection( &info->cs );
1825 RtlDeleteCriticalSection( &info->cs );
1826 HeapFree( GetProcessHeap(), 0, info );
1830 __EXCEPT_PAGE_FAULT
1832 WARN("Illegal handle %p\n", handle);
1833 SetLastError( ERROR_INVALID_HANDLE );
1834 return FALSE;
1836 __ENDTRY
1838 return TRUE;
1842 /*************************************************************************
1843 * FindFirstFileA (KERNEL32.@)
1845 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
1847 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1848 FindExSearchNameMatch, NULL, 0);
1851 /*************************************************************************
1852 * FindFirstFileExA (KERNEL32.@)
1854 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
1855 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
1856 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
1858 HANDLE handle;
1859 WIN32_FIND_DATAA *dataA;
1860 WIN32_FIND_DATAW dataW;
1861 WCHAR *nameW;
1863 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
1865 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1866 if (handle == INVALID_HANDLE_VALUE) return handle;
1868 dataA = (WIN32_FIND_DATAA *) lpFindFileData;
1869 dataA->dwFileAttributes = dataW.dwFileAttributes;
1870 dataA->ftCreationTime = dataW.ftCreationTime;
1871 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
1872 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
1873 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
1874 dataA->nFileSizeLow = dataW.nFileSizeLow;
1875 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
1876 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
1877 sizeof(dataA->cAlternateFileName) );
1878 return handle;
1882 /*************************************************************************
1883 * FindFirstFileW (KERNEL32.@)
1885 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1887 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1888 FindExSearchNameMatch, NULL, 0);
1892 /*************************************************************************
1893 * FindNextFileA (KERNEL32.@)
1895 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1897 WIN32_FIND_DATAW dataW;
1899 if (!FindNextFileW( handle, &dataW )) return FALSE;
1900 data->dwFileAttributes = dataW.dwFileAttributes;
1901 data->ftCreationTime = dataW.ftCreationTime;
1902 data->ftLastAccessTime = dataW.ftLastAccessTime;
1903 data->ftLastWriteTime = dataW.ftLastWriteTime;
1904 data->nFileSizeHigh = dataW.nFileSizeHigh;
1905 data->nFileSizeLow = dataW.nFileSizeLow;
1906 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
1907 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
1908 sizeof(data->cAlternateFileName) );
1909 return TRUE;
1913 /**************************************************************************
1914 * GetFileAttributesW (KERNEL32.@)
1916 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
1918 FILE_BASIC_INFORMATION info;
1919 UNICODE_STRING nt_name;
1920 OBJECT_ATTRIBUTES attr;
1921 NTSTATUS status;
1923 TRACE("%s\n", debugstr_w(name));
1925 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1927 SetLastError( ERROR_PATH_NOT_FOUND );
1928 return INVALID_FILE_ATTRIBUTES;
1931 attr.Length = sizeof(attr);
1932 attr.RootDirectory = 0;
1933 attr.Attributes = OBJ_CASE_INSENSITIVE;
1934 attr.ObjectName = &nt_name;
1935 attr.SecurityDescriptor = NULL;
1936 attr.SecurityQualityOfService = NULL;
1938 status = NtQueryAttributesFile( &attr, &info );
1939 RtlFreeUnicodeString( &nt_name );
1941 if (status == STATUS_SUCCESS) return info.FileAttributes;
1943 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1944 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
1946 SetLastError( RtlNtStatusToDosError(status) );
1947 return INVALID_FILE_ATTRIBUTES;
1951 /**************************************************************************
1952 * GetFileAttributesA (KERNEL32.@)
1954 DWORD WINAPI GetFileAttributesA( LPCSTR name )
1956 WCHAR *nameW;
1958 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
1959 return GetFileAttributesW( nameW );
1963 /**************************************************************************
1964 * SetFileAttributesW (KERNEL32.@)
1966 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
1968 UNICODE_STRING nt_name;
1969 OBJECT_ATTRIBUTES attr;
1970 IO_STATUS_BLOCK io;
1971 NTSTATUS status;
1972 HANDLE handle;
1974 TRACE("%s %x\n", debugstr_w(name), attributes);
1976 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1978 SetLastError( ERROR_PATH_NOT_FOUND );
1979 return FALSE;
1982 attr.Length = sizeof(attr);
1983 attr.RootDirectory = 0;
1984 attr.Attributes = OBJ_CASE_INSENSITIVE;
1985 attr.ObjectName = &nt_name;
1986 attr.SecurityDescriptor = NULL;
1987 attr.SecurityQualityOfService = NULL;
1989 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1990 RtlFreeUnicodeString( &nt_name );
1992 if (status == STATUS_SUCCESS)
1994 FILE_BASIC_INFORMATION info;
1996 memset( &info, 0, sizeof(info) );
1997 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
1998 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
1999 NtClose( handle );
2002 if (status == STATUS_SUCCESS) return TRUE;
2003 SetLastError( RtlNtStatusToDosError(status) );
2004 return FALSE;
2008 /**************************************************************************
2009 * SetFileAttributesA (KERNEL32.@)
2011 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2013 WCHAR *nameW;
2015 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2016 return SetFileAttributesW( nameW, attributes );
2020 /**************************************************************************
2021 * GetFileAttributesExW (KERNEL32.@)
2023 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2025 FILE_NETWORK_OPEN_INFORMATION info;
2026 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2027 UNICODE_STRING nt_name;
2028 OBJECT_ATTRIBUTES attr;
2029 NTSTATUS status;
2031 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2033 if (level != GetFileExInfoStandard)
2035 SetLastError( ERROR_INVALID_PARAMETER );
2036 return FALSE;
2039 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2041 SetLastError( ERROR_PATH_NOT_FOUND );
2042 return FALSE;
2045 attr.Length = sizeof(attr);
2046 attr.RootDirectory = 0;
2047 attr.Attributes = OBJ_CASE_INSENSITIVE;
2048 attr.ObjectName = &nt_name;
2049 attr.SecurityDescriptor = NULL;
2050 attr.SecurityQualityOfService = NULL;
2052 status = NtQueryFullAttributesFile( &attr, &info );
2053 RtlFreeUnicodeString( &nt_name );
2055 if (status != STATUS_SUCCESS)
2057 SetLastError( RtlNtStatusToDosError(status) );
2058 return FALSE;
2061 data->dwFileAttributes = info.FileAttributes;
2062 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2063 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2064 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2065 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2066 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2067 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2068 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2069 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2070 return TRUE;
2074 /**************************************************************************
2075 * GetFileAttributesExA (KERNEL32.@)
2077 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2079 WCHAR *nameW;
2081 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2082 return GetFileAttributesExW( nameW, level, ptr );
2086 /******************************************************************************
2087 * GetCompressedFileSizeW (KERNEL32.@)
2089 * Get the actual number of bytes used on disk.
2091 * RETURNS
2092 * Success: Low-order doubleword of number of bytes
2093 * Failure: INVALID_FILE_SIZE
2095 DWORD WINAPI GetCompressedFileSizeW(
2096 LPCWSTR name, /* [in] Pointer to name of file */
2097 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2099 UNICODE_STRING nt_name;
2100 OBJECT_ATTRIBUTES attr;
2101 IO_STATUS_BLOCK io;
2102 NTSTATUS status;
2103 HANDLE handle;
2104 DWORD ret = INVALID_FILE_SIZE;
2106 TRACE("%s %p\n", debugstr_w(name), size_high);
2108 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2110 SetLastError( ERROR_PATH_NOT_FOUND );
2111 return INVALID_FILE_SIZE;
2114 attr.Length = sizeof(attr);
2115 attr.RootDirectory = 0;
2116 attr.Attributes = OBJ_CASE_INSENSITIVE;
2117 attr.ObjectName = &nt_name;
2118 attr.SecurityDescriptor = NULL;
2119 attr.SecurityQualityOfService = NULL;
2121 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2122 RtlFreeUnicodeString( &nt_name );
2124 if (status == STATUS_SUCCESS)
2126 /* we don't support compressed files, simply return the file size */
2127 ret = GetFileSize( handle, size_high );
2128 NtClose( handle );
2130 else SetLastError( RtlNtStatusToDosError(status) );
2132 return ret;
2136 /******************************************************************************
2137 * GetCompressedFileSizeA (KERNEL32.@)
2139 * See GetCompressedFileSizeW.
2141 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2143 WCHAR *nameW;
2145 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2146 return GetCompressedFileSizeW( nameW, size_high );
2150 /***********************************************************************
2151 * OpenFile (KERNEL32.@)
2153 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2155 HANDLE handle;
2156 FILETIME filetime;
2157 WORD filedatetime[2];
2159 if (!ofs) return HFILE_ERROR;
2161 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2162 ((mode & 0x3 )==OF_READ)?"OF_READ":
2163 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2164 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2165 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2166 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2167 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2168 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2169 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2170 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2171 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2172 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2173 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2174 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2175 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2176 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2177 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2178 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2182 ofs->cBytes = sizeof(OFSTRUCT);
2183 ofs->nErrCode = 0;
2184 if (mode & OF_REOPEN) name = ofs->szPathName;
2186 if (!name) return HFILE_ERROR;
2188 TRACE("%s %04x\n", name, mode );
2190 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2191 Are there any cases where getting the path here is wrong?
2192 Uwe Bonnes 1997 Apr 2 */
2193 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2195 /* OF_PARSE simply fills the structure */
2197 if (mode & OF_PARSE)
2199 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2200 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2201 return 0;
2204 /* OF_CREATE is completely different from all other options, so
2205 handle it first */
2207 if (mode & OF_CREATE)
2209 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2210 goto error;
2212 else
2214 /* Now look for the file */
2216 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2217 goto error;
2219 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2221 if (mode & OF_DELETE)
2223 if (!DeleteFileA( ofs->szPathName )) goto error;
2224 TRACE("(%s): OF_DELETE return = OK\n", name);
2225 return TRUE;
2228 handle = (HANDLE)_lopen( ofs->szPathName, mode );
2229 if (handle == INVALID_HANDLE_VALUE) goto error;
2231 GetFileTime( handle, NULL, NULL, &filetime );
2232 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2233 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2235 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2237 CloseHandle( handle );
2238 WARN("(%s): OF_VERIFY failed\n", name );
2239 /* FIXME: what error here? */
2240 SetLastError( ERROR_FILE_NOT_FOUND );
2241 goto error;
2244 ofs->Reserved1 = filedatetime[0];
2245 ofs->Reserved2 = filedatetime[1];
2247 TRACE("(%s): OK, return = %p\n", name, handle );
2248 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2250 CloseHandle( handle );
2251 return TRUE;
2253 else return (HFILE)handle;
2255 error: /* We get here if there was an error opening the file */
2256 ofs->nErrCode = GetLastError();
2257 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2258 return HFILE_ERROR;