change default iSmCaptionWidth to 12
[wine/kumbayo.git] / dlls / kernel32 / file.c
blob4e05c2842111d1454025a1596f98bd0fb88ee530
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 "wine/unicode.h"
47 #include "wine/debug.h"
48 #include "thread.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(file);
52 HANDLE dos_handles[DOS_TABLE_SIZE];
54 /* info structure for FindFirstFile handle */
55 typedef struct
57 DWORD magic; /* magic number */
58 HANDLE handle; /* handle to directory */
59 CRITICAL_SECTION cs; /* crit section protecting this structure */
60 FINDEX_SEARCH_OPS search_op; /* Flags passed to FindFirst. */
61 UNICODE_STRING mask; /* file mask */
62 UNICODE_STRING path; /* NT path used to open the directory */
63 BOOL is_root; /* is directory the root of the drive? */
64 UINT data_pos; /* current position in dir data */
65 UINT data_len; /* length of dir data */
66 BYTE data[8192]; /* directory data */
67 } FIND_FIRST_INFO;
69 #define FIND_FIRST_MAGIC 0xc0ffee11
71 static BOOL oem_file_apis;
73 static const WCHAR wildcardsW[] = { '*','?',0 };
75 /***********************************************************************
76 * create_file_OF
78 * Wrapper for CreateFile that takes OF_* mode flags.
80 static HANDLE create_file_OF( LPCSTR path, INT mode )
82 DWORD access, sharing, creation;
84 if (mode & OF_CREATE)
86 creation = CREATE_ALWAYS;
87 access = GENERIC_READ | GENERIC_WRITE;
89 else
91 creation = OPEN_EXISTING;
92 switch(mode & 0x03)
94 case OF_READ: access = GENERIC_READ; break;
95 case OF_WRITE: access = GENERIC_WRITE; break;
96 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
97 default: access = 0; break;
101 switch(mode & 0x70)
103 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
104 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
105 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
106 case OF_SHARE_DENY_NONE:
107 case OF_SHARE_COMPAT:
108 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
110 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
114 /***********************************************************************
115 * check_dir_symlink
117 * Check if a dir symlink should be returned by FindNextFile.
119 static BOOL check_dir_symlink( FIND_FIRST_INFO *info, const FILE_BOTH_DIR_INFORMATION *file_info )
121 UNICODE_STRING str;
122 ANSI_STRING unix_name;
123 struct stat st, parent_st;
124 BOOL ret = TRUE;
125 DWORD len;
127 str.MaximumLength = info->path.Length + sizeof(WCHAR) + file_info->FileNameLength;
128 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength ))) return TRUE;
129 memcpy( str.Buffer, info->path.Buffer, info->path.Length );
130 len = info->path.Length / sizeof(WCHAR);
131 if (!len || str.Buffer[len-1] != '\\') str.Buffer[len++] = '\\';
132 memcpy( str.Buffer + len, file_info->FileName, file_info->FileNameLength );
133 str.Length = len * sizeof(WCHAR) + file_info->FileNameLength;
135 unix_name.Buffer = NULL;
136 if (!wine_nt_to_unix_file_name( &str, &unix_name, OPEN_EXISTING, FALSE ) &&
137 !stat( unix_name.Buffer, &st ))
139 char *p = unix_name.Buffer + unix_name.Length - 1;
141 /* skip trailing slashes */
142 while (p > unix_name.Buffer && *p == '/') p--;
144 while (ret && p > unix_name.Buffer)
146 while (p > unix_name.Buffer && *p != '/') p--;
147 while (p > unix_name.Buffer && *p == '/') p--;
148 p[1] = 0;
149 if (!stat( unix_name.Buffer, &parent_st ) &&
150 parent_st.st_dev == st.st_dev &&
151 parent_st.st_ino == st.st_ino)
153 WARN( "suppressing dir symlink %s pointing to parent %s\n",
154 debugstr_wn( str.Buffer, str.Length/sizeof(WCHAR) ),
155 debugstr_a( unix_name.Buffer ));
156 ret = FALSE;
160 RtlFreeAnsiString( &unix_name );
161 RtlFreeUnicodeString( &str );
162 return ret;
166 /***********************************************************************
167 * FILE_SetDosError
169 * Set the DOS error code from errno.
171 void FILE_SetDosError(void)
173 int save_errno = errno; /* errno gets overwritten by printf */
175 TRACE("errno = %d %s\n", errno, strerror(errno));
176 switch (save_errno)
178 case EAGAIN:
179 SetLastError( ERROR_SHARING_VIOLATION );
180 break;
181 case EBADF:
182 SetLastError( ERROR_INVALID_HANDLE );
183 break;
184 case ENOSPC:
185 SetLastError( ERROR_HANDLE_DISK_FULL );
186 break;
187 case EACCES:
188 case EPERM:
189 case EROFS:
190 SetLastError( ERROR_ACCESS_DENIED );
191 break;
192 case EBUSY:
193 SetLastError( ERROR_LOCK_VIOLATION );
194 break;
195 case ENOENT:
196 SetLastError( ERROR_FILE_NOT_FOUND );
197 break;
198 case EISDIR:
199 SetLastError( ERROR_CANNOT_MAKE );
200 break;
201 case ENFILE:
202 case EMFILE:
203 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
204 break;
205 case EEXIST:
206 SetLastError( ERROR_FILE_EXISTS );
207 break;
208 case EINVAL:
209 case ESPIPE:
210 SetLastError( ERROR_SEEK );
211 break;
212 case ENOTEMPTY:
213 SetLastError( ERROR_DIR_NOT_EMPTY );
214 break;
215 case ENOEXEC:
216 SetLastError( ERROR_BAD_FORMAT );
217 break;
218 case ENOTDIR:
219 SetLastError( ERROR_PATH_NOT_FOUND );
220 break;
221 case EXDEV:
222 SetLastError( ERROR_NOT_SAME_DEVICE );
223 break;
224 default:
225 WARN("unknown file error: %s\n", strerror(save_errno) );
226 SetLastError( ERROR_GEN_FAILURE );
227 break;
229 errno = save_errno;
233 /***********************************************************************
234 * FILE_name_AtoW
236 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
238 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
239 * there is no possibility for the function to do that twice, taking into
240 * account any called function.
242 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
244 ANSI_STRING str;
245 UNICODE_STRING strW, *pstrW;
246 NTSTATUS status;
248 RtlInitAnsiString( &str, name );
249 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
250 if (oem_file_apis)
251 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
252 else
253 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
254 if (status == STATUS_SUCCESS) return pstrW->Buffer;
256 if (status == STATUS_BUFFER_OVERFLOW)
257 SetLastError( ERROR_FILENAME_EXCED_RANGE );
258 else
259 SetLastError( RtlNtStatusToDosError(status) );
260 return NULL;
264 /***********************************************************************
265 * FILE_name_WtoA
267 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
269 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
271 DWORD ret;
273 if (srclen < 0) srclen = strlenW( src ) + 1;
274 if (oem_file_apis)
275 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
276 else
277 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
278 return ret;
282 /**************************************************************************
283 * SetFileApisToOEM (KERNEL32.@)
285 VOID WINAPI SetFileApisToOEM(void)
287 oem_file_apis = TRUE;
291 /**************************************************************************
292 * SetFileApisToANSI (KERNEL32.@)
294 VOID WINAPI SetFileApisToANSI(void)
296 oem_file_apis = FALSE;
300 /******************************************************************************
301 * AreFileApisANSI (KERNEL32.@)
303 * Determines if file functions are using ANSI
305 * RETURNS
306 * TRUE: Set of file functions is using ANSI code page
307 * FALSE: Set of file functions is using OEM code page
309 BOOL WINAPI AreFileApisANSI(void)
311 return !oem_file_apis;
315 /**************************************************************************
316 * Operations on file handles *
317 **************************************************************************/
319 /***********************************************************************
320 * FILE_InitProcessDosHandles
322 * Allocates the default DOS handles for a process. Called either by
323 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
325 static void FILE_InitProcessDosHandles( void )
327 static BOOL init_done /* = FALSE */;
328 HANDLE cp = GetCurrentProcess();
330 if (init_done) return;
331 init_done = TRUE;
332 DuplicateHandle(cp, GetStdHandle(STD_INPUT_HANDLE), cp, &dos_handles[0],
333 0, TRUE, DUPLICATE_SAME_ACCESS);
334 DuplicateHandle(cp, GetStdHandle(STD_OUTPUT_HANDLE), cp, &dos_handles[1],
335 0, TRUE, DUPLICATE_SAME_ACCESS);
336 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[2],
337 0, TRUE, DUPLICATE_SAME_ACCESS);
338 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[3],
339 0, TRUE, DUPLICATE_SAME_ACCESS);
340 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[4],
341 0, TRUE, DUPLICATE_SAME_ACCESS);
345 /******************************************************************
346 * FILE_ReadWriteApc (internal)
348 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG reserved)
350 LPOVERLAPPED_COMPLETION_ROUTINE cr = (LPOVERLAPPED_COMPLETION_ROUTINE)apc_user;
352 cr(RtlNtStatusToDosError(io_status->u.Status), io_status->Information, (LPOVERLAPPED)io_status);
356 /***********************************************************************
357 * ReadFileEx (KERNEL32.@)
359 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
360 LPOVERLAPPED overlapped,
361 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
363 LARGE_INTEGER offset;
364 NTSTATUS status;
365 PIO_STATUS_BLOCK io_status;
367 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
369 if (!overlapped)
371 SetLastError(ERROR_INVALID_PARAMETER);
372 return FALSE;
375 offset.u.LowPart = overlapped->u.s.Offset;
376 offset.u.HighPart = overlapped->u.s.OffsetHigh;
377 io_status = (PIO_STATUS_BLOCK)overlapped;
378 io_status->u.Status = STATUS_PENDING;
379 io_status->Information = 0;
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;
405 LPVOID cvalue = NULL;
407 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToRead,
408 bytesRead, overlapped );
410 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
411 if (!bytesToRead) return TRUE;
413 if (is_console_handle(hFile))
414 return ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
416 if (overlapped != NULL)
418 offset.u.LowPart = overlapped->u.s.Offset;
419 offset.u.HighPart = overlapped->u.s.OffsetHigh;
420 poffset = &offset;
421 hEvent = overlapped->hEvent;
422 io_status = (PIO_STATUS_BLOCK)overlapped;
423 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
425 io_status->u.Status = STATUS_PENDING;
426 io_status->Information = 0;
428 status = NtReadFile(hFile, hEvent, NULL, cvalue, io_status, buffer, bytesToRead, poffset, NULL);
430 if (status == STATUS_PENDING && !overlapped)
432 WaitForSingleObject( hFile, INFINITE );
433 status = io_status->u.Status;
436 if (status != STATUS_PENDING && bytesRead)
437 *bytesRead = io_status->Information;
439 if (status && status != STATUS_END_OF_FILE && status != STATUS_TIMEOUT)
441 SetLastError( RtlNtStatusToDosError(status) );
442 return FALSE;
444 return TRUE;
448 /***********************************************************************
449 * WriteFileEx (KERNEL32.@)
451 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
452 LPOVERLAPPED overlapped,
453 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
455 LARGE_INTEGER offset;
456 NTSTATUS status;
457 PIO_STATUS_BLOCK io_status;
459 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
461 if (overlapped == NULL)
463 SetLastError(ERROR_INVALID_PARAMETER);
464 return FALSE;
466 offset.u.LowPart = overlapped->u.s.Offset;
467 offset.u.HighPart = overlapped->u.s.OffsetHigh;
469 io_status = (PIO_STATUS_BLOCK)overlapped;
470 io_status->u.Status = STATUS_PENDING;
471 io_status->Information = 0;
473 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
474 io_status, buffer, bytesToWrite, &offset, NULL);
476 if (status) SetLastError( RtlNtStatusToDosError(status) );
477 return !status;
481 /***********************************************************************
482 * WriteFile (KERNEL32.@)
484 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
485 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
487 HANDLE hEvent = NULL;
488 LARGE_INTEGER offset;
489 PLARGE_INTEGER poffset = NULL;
490 NTSTATUS status;
491 IO_STATUS_BLOCK iosb;
492 PIO_STATUS_BLOCK piosb = &iosb;
493 LPVOID cvalue = NULL;
495 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
497 if (is_console_handle(hFile))
498 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
500 if (overlapped)
502 offset.u.LowPart = overlapped->u.s.Offset;
503 offset.u.HighPart = overlapped->u.s.OffsetHigh;
504 poffset = &offset;
505 hEvent = overlapped->hEvent;
506 piosb = (PIO_STATUS_BLOCK)overlapped;
507 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
509 piosb->u.Status = STATUS_PENDING;
510 piosb->Information = 0;
512 status = NtWriteFile(hFile, hEvent, NULL, cvalue, piosb,
513 buffer, bytesToWrite, poffset, NULL);
515 /* FIXME: NtWriteFile does not always cause page faults, generate them now */
516 if (status == STATUS_INVALID_USER_BUFFER && !IsBadReadPtr( buffer, bytesToWrite ))
518 status = NtWriteFile(hFile, hEvent, NULL, cvalue, piosb,
519 buffer, bytesToWrite, poffset, NULL);
520 if (status != STATUS_INVALID_USER_BUFFER)
521 FIXME("Could not access memory (%p,%d) at first, now OK. Protected by DIBSection code?\n",
522 buffer, bytesToWrite);
525 if (status == STATUS_PENDING && !overlapped)
527 WaitForSingleObject( hFile, INFINITE );
528 status = piosb->u.Status;
531 if (status != STATUS_PENDING && bytesWritten)
532 *bytesWritten = piosb->Information;
534 if (status && status != STATUS_TIMEOUT)
536 SetLastError( RtlNtStatusToDosError(status) );
537 return FALSE;
539 return TRUE;
543 /***********************************************************************
544 * GetOverlappedResult (KERNEL32.@)
546 * Check the result of an Asynchronous data transfer from a file.
548 * Parameters
549 * HANDLE hFile [in] handle of file to check on
550 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
551 * LPDWORD lpTransferred [in/out] number of bytes transferred
552 * BOOL bWait [in] wait for the transfer to complete ?
554 * RETURNS
555 * TRUE on success
556 * FALSE on failure
558 * If successful (and relevant) lpTransferred will hold the number of
559 * bytes transferred during the async operation.
561 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
562 LPDWORD lpTransferred, BOOL bWait)
564 NTSTATUS status;
566 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
568 if ( lpOverlapped == NULL )
570 ERR("lpOverlapped was null\n");
571 return FALSE;
574 status = lpOverlapped->Internal;
575 if (status == STATUS_PENDING)
577 if (!bWait)
579 SetLastError( ERROR_IO_INCOMPLETE );
580 return FALSE;
583 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : hFile,
584 INFINITE ) == WAIT_FAILED)
585 return FALSE;
586 status = lpOverlapped->Internal;
589 if (lpTransferred) *lpTransferred = lpOverlapped->InternalHigh;
591 if (status) SetLastError( RtlNtStatusToDosError(status) );
592 return !status;
595 /***********************************************************************
596 * CancelIo (KERNEL32.@)
598 * Cancels pending I/O operations initiated by the current thread on a file.
600 * PARAMS
601 * handle [I] File handle.
603 * RETURNS
604 * Success: TRUE.
605 * Failure: FALSE, check GetLastError().
607 BOOL WINAPI CancelIo(HANDLE handle)
609 IO_STATUS_BLOCK io_status;
611 NtCancelIoFile(handle, &io_status);
612 if (io_status.u.Status)
614 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
615 return FALSE;
617 return TRUE;
620 /***********************************************************************
621 * _hread (KERNEL32.@)
623 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
625 return _lread( hFile, buffer, count );
629 /***********************************************************************
630 * _hwrite (KERNEL32.@)
632 * experimentation yields that _lwrite:
633 * o truncates the file at the current position with
634 * a 0 len write
635 * o returns 0 on a 0 length write
636 * o works with console handles
639 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
641 DWORD result;
643 TRACE("%d %p %d\n", handle, buffer, count );
645 if (!count)
647 /* Expand or truncate at current position */
648 if (!SetEndOfFile( LongToHandle(handle) )) return HFILE_ERROR;
649 return 0;
651 if (!WriteFile( LongToHandle(handle), buffer, count, &result, NULL ))
652 return HFILE_ERROR;
653 return result;
657 /***********************************************************************
658 * _lclose (KERNEL32.@)
660 HFILE WINAPI _lclose( HFILE hFile )
662 TRACE("handle %d\n", hFile );
663 return CloseHandle( LongToHandle(hFile) ) ? 0 : HFILE_ERROR;
667 /***********************************************************************
668 * _lcreat (KERNEL32.@)
670 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
672 HANDLE hfile;
674 /* Mask off all flags not explicitly allowed by the doc */
675 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
676 TRACE("%s %02x\n", path, attr );
677 hfile = CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
678 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
679 CREATE_ALWAYS, attr, 0 );
680 return HandleToLong(hfile);
684 /***********************************************************************
685 * _lopen (KERNEL32.@)
687 HFILE WINAPI _lopen( LPCSTR path, INT mode )
689 HANDLE hfile;
691 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
692 hfile = create_file_OF( path, mode & ~OF_CREATE );
693 return HandleToLong(hfile);
696 /***********************************************************************
697 * _lread (KERNEL32.@)
699 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
701 DWORD result;
702 if (!ReadFile( LongToHandle(handle), buffer, count, &result, NULL ))
703 return HFILE_ERROR;
704 return result;
708 /***********************************************************************
709 * _llseek (KERNEL32.@)
711 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
713 return SetFilePointer( LongToHandle(hFile), lOffset, NULL, nOrigin );
717 /***********************************************************************
718 * _lwrite (KERNEL32.@)
720 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
722 return (UINT)_hwrite( hFile, buffer, (LONG)count );
726 /***********************************************************************
727 * FlushFileBuffers (KERNEL32.@)
729 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
731 NTSTATUS nts;
732 IO_STATUS_BLOCK ioblk;
734 if (is_console_handle( hFile ))
736 /* this will fail (as expected) for an output handle */
737 return FlushConsoleInputBuffer( hFile );
739 nts = NtFlushBuffersFile( hFile, &ioblk );
740 if (nts != STATUS_SUCCESS)
742 SetLastError( RtlNtStatusToDosError( nts ) );
743 return FALSE;
746 return TRUE;
750 /***********************************************************************
751 * GetFileType (KERNEL32.@)
753 DWORD WINAPI GetFileType( HANDLE hFile )
755 FILE_FS_DEVICE_INFORMATION info;
756 IO_STATUS_BLOCK io;
757 NTSTATUS status;
759 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
761 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
762 if (status != STATUS_SUCCESS)
764 SetLastError( RtlNtStatusToDosError(status) );
765 return FILE_TYPE_UNKNOWN;
768 switch(info.DeviceType)
770 case FILE_DEVICE_NULL:
771 case FILE_DEVICE_SERIAL_PORT:
772 case FILE_DEVICE_PARALLEL_PORT:
773 case FILE_DEVICE_TAPE:
774 case FILE_DEVICE_UNKNOWN:
775 return FILE_TYPE_CHAR;
776 case FILE_DEVICE_NAMED_PIPE:
777 return FILE_TYPE_PIPE;
778 default:
779 return FILE_TYPE_DISK;
784 /***********************************************************************
785 * GetFileInformationByHandle (KERNEL32.@)
787 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
789 FILE_ALL_INFORMATION all_info;
790 IO_STATUS_BLOCK io;
791 NTSTATUS status;
793 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
794 if (status == STATUS_SUCCESS)
796 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
797 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
798 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
799 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
800 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
801 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
802 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
803 info->dwVolumeSerialNumber = 0; /* FIXME */
804 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
805 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
806 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
807 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
808 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
809 return TRUE;
811 SetLastError( RtlNtStatusToDosError(status) );
812 return FALSE;
816 /***********************************************************************
817 * GetFileSize (KERNEL32.@)
819 * Retrieve the size of a file.
821 * PARAMS
822 * hFile [I] File to retrieve size of.
823 * filesizehigh [O] On return, the high bits of the file size.
825 * RETURNS
826 * Success: The low bits of the file size.
827 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
828 * check GetLastError() for values other than ERROR_SUCCESS.
830 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
832 LARGE_INTEGER size;
833 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
834 if (filesizehigh) *filesizehigh = size.u.HighPart;
835 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
836 return size.u.LowPart;
840 /***********************************************************************
841 * GetFileSizeEx (KERNEL32.@)
843 * Retrieve the size of a file.
845 * PARAMS
846 * hFile [I] File to retrieve size of.
847 * lpFileSIze [O] On return, the size of the file.
849 * RETURNS
850 * Success: TRUE.
851 * Failure: FALSE, check GetLastError().
853 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
855 FILE_END_OF_FILE_INFORMATION info;
856 IO_STATUS_BLOCK io;
857 NTSTATUS status;
859 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileEndOfFileInformation );
860 if (status == STATUS_SUCCESS)
862 *lpFileSize = info.EndOfFile;
863 return TRUE;
865 SetLastError( RtlNtStatusToDosError(status) );
866 return FALSE;
870 /**************************************************************************
871 * SetEndOfFile (KERNEL32.@)
873 * Sets the current position as the end of the file.
875 * PARAMS
876 * hFile [I] File handle.
878 * RETURNS
879 * Success: TRUE.
880 * Failure: FALSE, check GetLastError().
882 BOOL WINAPI SetEndOfFile( HANDLE hFile )
884 FILE_POSITION_INFORMATION pos;
885 FILE_END_OF_FILE_INFORMATION eof;
886 IO_STATUS_BLOCK io;
887 NTSTATUS status;
889 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
890 if (status == STATUS_SUCCESS)
892 eof.EndOfFile = pos.CurrentByteOffset;
893 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
895 if (status == STATUS_SUCCESS) return TRUE;
896 SetLastError( RtlNtStatusToDosError(status) );
897 return FALSE;
901 /***********************************************************************
902 * SetFilePointer (KERNEL32.@)
904 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
906 LARGE_INTEGER dist, newpos;
908 if (highword)
910 dist.u.LowPart = distance;
911 dist.u.HighPart = *highword;
913 else dist.QuadPart = distance;
915 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
917 if (highword) *highword = newpos.u.HighPart;
918 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
919 return newpos.u.LowPart;
923 /***********************************************************************
924 * SetFilePointerEx (KERNEL32.@)
926 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
927 LARGE_INTEGER *newpos, DWORD method )
929 LONGLONG pos;
930 IO_STATUS_BLOCK io;
931 FILE_POSITION_INFORMATION info;
933 switch(method)
935 case FILE_BEGIN:
936 pos = distance.QuadPart;
937 break;
938 case FILE_CURRENT:
939 if (NtQueryInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
940 goto error;
941 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
942 break;
943 case FILE_END:
945 FILE_END_OF_FILE_INFORMATION eof;
946 if (NtQueryInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
947 goto error;
948 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
950 break;
951 default:
952 SetLastError( ERROR_INVALID_PARAMETER );
953 return FALSE;
956 if (pos < 0)
958 SetLastError( ERROR_NEGATIVE_SEEK );
959 return FALSE;
962 info.CurrentByteOffset.QuadPart = pos;
963 if (NtSetInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
964 goto error;
965 if (newpos) newpos->QuadPart = pos;
966 return TRUE;
968 error:
969 SetLastError( RtlNtStatusToDosError(io.u.Status) );
970 return FALSE;
973 /***********************************************************************
974 * GetFileTime (KERNEL32.@)
976 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
977 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
979 FILE_BASIC_INFORMATION info;
980 IO_STATUS_BLOCK io;
981 NTSTATUS status;
983 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
984 if (status == STATUS_SUCCESS)
986 if (lpCreationTime)
988 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
989 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
991 if (lpLastAccessTime)
993 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
994 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
996 if (lpLastWriteTime)
998 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
999 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1001 return TRUE;
1003 SetLastError( RtlNtStatusToDosError(status) );
1004 return FALSE;
1008 /***********************************************************************
1009 * SetFileTime (KERNEL32.@)
1011 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1012 const FILETIME *atime, const FILETIME *mtime )
1014 FILE_BASIC_INFORMATION info;
1015 IO_STATUS_BLOCK io;
1016 NTSTATUS status;
1018 memset( &info, 0, sizeof(info) );
1019 if (ctime)
1021 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1022 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1024 if (atime)
1026 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1027 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1029 if (mtime)
1031 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1032 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1035 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1036 if (status == STATUS_SUCCESS) return TRUE;
1037 SetLastError( RtlNtStatusToDosError(status) );
1038 return FALSE;
1042 /**************************************************************************
1043 * LockFile (KERNEL32.@)
1045 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1046 DWORD count_low, DWORD count_high )
1048 NTSTATUS status;
1049 LARGE_INTEGER count, offset;
1051 TRACE( "%p %x%08x %x%08x\n",
1052 hFile, offset_high, offset_low, count_high, count_low );
1054 count.u.LowPart = count_low;
1055 count.u.HighPart = count_high;
1056 offset.u.LowPart = offset_low;
1057 offset.u.HighPart = offset_high;
1059 status = NtLockFile( hFile, 0, NULL, NULL,
1060 NULL, &offset, &count, NULL, TRUE, TRUE );
1062 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1063 return !status;
1067 /**************************************************************************
1068 * LockFileEx [KERNEL32.@]
1070 * Locks a byte range within an open file for shared or exclusive access.
1072 * RETURNS
1073 * success: TRUE
1074 * failure: FALSE
1076 * NOTES
1077 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1079 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1080 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1082 NTSTATUS status;
1083 LARGE_INTEGER count, offset;
1084 LPVOID cvalue = NULL;
1086 if (reserved)
1088 SetLastError( ERROR_INVALID_PARAMETER );
1089 return FALSE;
1092 TRACE( "%p %x%08x %x%08x flags %x\n",
1093 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1094 count_high, count_low, flags );
1096 count.u.LowPart = count_low;
1097 count.u.HighPart = count_high;
1098 offset.u.LowPart = overlapped->u.s.Offset;
1099 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1101 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1103 status = NtLockFile( hFile, overlapped->hEvent, NULL, cvalue,
1104 NULL, &offset, &count, NULL,
1105 flags & LOCKFILE_FAIL_IMMEDIATELY,
1106 flags & LOCKFILE_EXCLUSIVE_LOCK );
1108 if (status) SetLastError( RtlNtStatusToDosError(status) );
1109 return !status;
1113 /**************************************************************************
1114 * UnlockFile (KERNEL32.@)
1116 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1117 DWORD count_low, DWORD count_high )
1119 NTSTATUS status;
1120 LARGE_INTEGER count, offset;
1122 count.u.LowPart = count_low;
1123 count.u.HighPart = count_high;
1124 offset.u.LowPart = offset_low;
1125 offset.u.HighPart = offset_high;
1127 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1128 if (status) SetLastError( RtlNtStatusToDosError(status) );
1129 return !status;
1133 /**************************************************************************
1134 * UnlockFileEx (KERNEL32.@)
1136 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1137 LPOVERLAPPED overlapped )
1139 if (reserved)
1141 SetLastError( ERROR_INVALID_PARAMETER );
1142 return FALSE;
1144 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1146 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1150 /***********************************************************************
1151 * Win32HandleToDosFileHandle (KERNEL32.21)
1153 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1154 * longer valid after this function (even on failure).
1156 * Note: this is not exactly right, since on Win95 the Win32 handles
1157 * are on top of DOS handles and we do it the other way
1158 * around. Should be good enough though.
1160 HFILE WINAPI Win32HandleToDosFileHandle( HANDLE handle )
1162 int i;
1164 if (!handle || (handle == INVALID_HANDLE_VALUE))
1165 return HFILE_ERROR;
1167 FILE_InitProcessDosHandles();
1168 for (i = 0; i < DOS_TABLE_SIZE; i++)
1169 if (!dos_handles[i])
1171 dos_handles[i] = handle;
1172 TRACE("Got %d for h32 %p\n", i, handle );
1173 return (HFILE)i;
1175 CloseHandle( handle );
1176 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1177 return HFILE_ERROR;
1181 /***********************************************************************
1182 * DosFileHandleToWin32Handle (KERNEL32.20)
1184 * Return the Win32 handle for a DOS handle.
1186 * Note: this is not exactly right, since on Win95 the Win32 handles
1187 * are on top of DOS handles and we do it the other way
1188 * around. Should be good enough though.
1190 HANDLE WINAPI DosFileHandleToWin32Handle( HFILE handle )
1192 HFILE16 hfile = (HFILE16)handle;
1193 if (hfile < 5) FILE_InitProcessDosHandles();
1194 if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
1196 SetLastError( ERROR_INVALID_HANDLE );
1197 return INVALID_HANDLE_VALUE;
1199 return dos_handles[hfile];
1203 /*************************************************************************
1204 * SetHandleCount (KERNEL32.@)
1206 UINT WINAPI SetHandleCount( UINT count )
1208 return min( 256, count );
1212 /***********************************************************************
1213 * DisposeLZ32Handle (KERNEL32.22)
1215 * Note: this is not entirely correct, we should only close the
1216 * 32-bit handle and not the 16-bit one, but we cannot do
1217 * this because of the way our DOS handles are implemented.
1218 * It shouldn't break anything though.
1220 void WINAPI DisposeLZ32Handle( HANDLE handle )
1222 int i;
1224 if (!handle || (handle == INVALID_HANDLE_VALUE)) return;
1226 for (i = 5; i < DOS_TABLE_SIZE; i++)
1227 if (dos_handles[i] == handle)
1229 dos_handles[i] = 0;
1230 CloseHandle( handle );
1231 break;
1235 /**************************************************************************
1236 * Operations on file names *
1237 **************************************************************************/
1240 /*************************************************************************
1241 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1243 * Creates or opens an object, and returns a handle that can be used to
1244 * access that object.
1246 * PARAMS
1248 * filename [in] pointer to filename to be accessed
1249 * access [in] access mode requested
1250 * sharing [in] share mode
1251 * sa [in] pointer to security attributes
1252 * creation [in] how to create the file
1253 * attributes [in] attributes for newly created file
1254 * template [in] handle to file with extended attributes to copy
1256 * RETURNS
1257 * Success: Open handle to specified file
1258 * Failure: INVALID_HANDLE_VALUE
1260 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1261 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1262 DWORD attributes, HANDLE template )
1264 NTSTATUS status;
1265 UINT options;
1266 OBJECT_ATTRIBUTES attr;
1267 UNICODE_STRING nameW;
1268 IO_STATUS_BLOCK io;
1269 HANDLE ret;
1270 DWORD dosdev;
1271 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1272 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1273 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1274 SECURITY_QUALITY_OF_SERVICE qos;
1276 static const UINT nt_disposition[5] =
1278 FILE_CREATE, /* CREATE_NEW */
1279 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1280 FILE_OPEN, /* OPEN_EXISTING */
1281 FILE_OPEN_IF, /* OPEN_ALWAYS */
1282 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1286 /* sanity checks */
1288 if (!filename || !filename[0])
1290 SetLastError( ERROR_PATH_NOT_FOUND );
1291 return INVALID_HANDLE_VALUE;
1294 TRACE("%s %s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1295 (access & GENERIC_READ)?"GENERIC_READ ":"",
1296 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1297 (!access)?"QUERY_ACCESS ":"",
1298 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1299 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1300 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1301 creation, attributes);
1303 /* Open a console for CONIN$ or CONOUT$ */
1305 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1307 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle), creation);
1308 goto done;
1311 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1313 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1314 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1316 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1317 !strncmpiW( filename + 4, pipeW, 5 ) ||
1318 !strncmpiW( filename + 4, mailslotW, 9 ))
1320 dosdev = 0;
1322 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1324 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1326 else if (!(GetVersion() & 0x80000000))
1328 dosdev = 0;
1330 else if (filename[4])
1332 ret = VXD_Open( filename+4, access, sa );
1333 goto done;
1335 else
1337 SetLastError( ERROR_INVALID_NAME );
1338 return INVALID_HANDLE_VALUE;
1341 else dosdev = RtlIsDosDeviceName_U( filename );
1343 if (dosdev)
1345 static const WCHAR conW[] = {'C','O','N'};
1347 if (LOWORD(dosdev) == sizeof(conW) &&
1348 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1350 switch (access & (GENERIC_READ|GENERIC_WRITE))
1352 case GENERIC_READ:
1353 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), creation);
1354 goto done;
1355 case GENERIC_WRITE:
1356 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), creation);
1357 goto done;
1358 default:
1359 SetLastError( ERROR_FILE_NOT_FOUND );
1360 return INVALID_HANDLE_VALUE;
1365 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1367 SetLastError( ERROR_INVALID_PARAMETER );
1368 return INVALID_HANDLE_VALUE;
1371 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1373 SetLastError( ERROR_PATH_NOT_FOUND );
1374 return INVALID_HANDLE_VALUE;
1377 /* now call NtCreateFile */
1379 options = 0;
1380 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1381 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1382 else
1383 options |= FILE_NON_DIRECTORY_FILE;
1384 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1386 options |= FILE_DELETE_ON_CLOSE;
1387 access |= DELETE;
1389 if (!(attributes & FILE_FLAG_OVERLAPPED))
1390 options |= FILE_SYNCHRONOUS_IO_ALERT;
1391 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1392 options |= FILE_RANDOM_ACCESS;
1393 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1395 attr.Length = sizeof(attr);
1396 attr.RootDirectory = 0;
1397 attr.Attributes = OBJ_CASE_INSENSITIVE;
1398 attr.ObjectName = &nameW;
1399 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1400 if (attributes & SECURITY_SQOS_PRESENT)
1402 qos.Length = sizeof(qos);
1403 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
1404 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
1405 qos.EffectiveOnly = attributes & SECURITY_EFFECTIVE_ONLY ? TRUE : FALSE;
1406 attr.SecurityQualityOfService = &qos;
1408 else
1409 attr.SecurityQualityOfService = NULL;
1411 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1413 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1414 sharing, nt_disposition[creation - CREATE_NEW],
1415 options, NULL, 0 );
1416 if (status)
1418 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1419 ret = INVALID_HANDLE_VALUE;
1421 /* In the case file creation was rejected due to CREATE_NEW flag
1422 * was specified and file with that name already exists, correct
1423 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1424 * Note: RtlNtStatusToDosError is not the subject to blame here.
1426 if (status == STATUS_OBJECT_NAME_COLLISION)
1427 SetLastError( ERROR_FILE_EXISTS );
1428 else
1429 SetLastError( RtlNtStatusToDosError(status) );
1431 else
1433 if ((creation == CREATE_ALWAYS && io.Information == FILE_OVERWRITTEN) ||
1434 (creation == OPEN_ALWAYS && io.Information == FILE_OPENED))
1435 SetLastError( ERROR_ALREADY_EXISTS );
1436 else
1437 SetLastError( 0 );
1439 RtlFreeUnicodeString( &nameW );
1441 done:
1442 if (!ret) ret = INVALID_HANDLE_VALUE;
1443 TRACE("returning %p\n", ret);
1444 return ret;
1449 /*************************************************************************
1450 * CreateFileA (KERNEL32.@)
1452 * See CreateFileW.
1454 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1455 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1456 DWORD attributes, HANDLE template)
1458 WCHAR *nameW;
1460 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1461 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1465 /***********************************************************************
1466 * DeleteFileW (KERNEL32.@)
1468 * Delete a file.
1470 * PARAMS
1471 * path [I] Path to the file to delete.
1473 * RETURNS
1474 * Success: TRUE.
1475 * Failure: FALSE, check GetLastError().
1477 BOOL WINAPI DeleteFileW( LPCWSTR path )
1479 UNICODE_STRING nameW;
1480 OBJECT_ATTRIBUTES attr;
1481 NTSTATUS status;
1483 TRACE("%s\n", debugstr_w(path) );
1485 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1487 SetLastError( ERROR_PATH_NOT_FOUND );
1488 return FALSE;
1491 attr.Length = sizeof(attr);
1492 attr.RootDirectory = 0;
1493 attr.Attributes = OBJ_CASE_INSENSITIVE;
1494 attr.ObjectName = &nameW;
1495 attr.SecurityDescriptor = NULL;
1496 attr.SecurityQualityOfService = NULL;
1498 status = NtDeleteFile(&attr);
1499 RtlFreeUnicodeString( &nameW );
1500 if (status)
1502 SetLastError( RtlNtStatusToDosError(status) );
1503 return FALSE;
1505 return TRUE;
1509 /***********************************************************************
1510 * DeleteFileA (KERNEL32.@)
1512 * See DeleteFileW.
1514 BOOL WINAPI DeleteFileA( LPCSTR path )
1516 WCHAR *pathW;
1518 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1519 return DeleteFileW( pathW );
1523 /**************************************************************************
1524 * ReplaceFileW (KERNEL32.@)
1525 * ReplaceFile (KERNEL32.@)
1527 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName,LPCWSTR lpReplacementFileName,
1528 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1529 LPVOID lpExclude, LPVOID lpReserved)
1531 FIXME("(%s,%s,%s,%08x,%p,%p) stub\n",debugstr_w(lpReplacedFileName),debugstr_w(lpReplacementFileName),
1532 debugstr_w(lpBackupFileName),dwReplaceFlags,lpExclude,lpReserved);
1533 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1534 return FALSE;
1538 /**************************************************************************
1539 * ReplaceFileA (KERNEL32.@)
1541 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1542 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1543 LPVOID lpExclude, LPVOID lpReserved)
1545 FIXME("(%s,%s,%s,%08x,%p,%p) stub\n",lpReplacedFileName,lpReplacementFileName,
1546 lpBackupFileName,dwReplaceFlags,lpExclude,lpReserved);
1547 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1548 return FALSE;
1552 /*************************************************************************
1553 * FindFirstFileExW (KERNEL32.@)
1555 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1556 * results as FindExSearchNameMatch
1558 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1559 LPVOID data, FINDEX_SEARCH_OPS search_op,
1560 LPVOID filter, DWORD flags)
1562 WCHAR *mask, *p;
1563 FIND_FIRST_INFO *info = NULL;
1564 UNICODE_STRING nt_name;
1565 OBJECT_ATTRIBUTES attr;
1566 IO_STATUS_BLOCK io;
1567 NTSTATUS status;
1568 DWORD device = 0;
1570 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1572 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1573 || flags != 0)
1575 FIXME("options not implemented 0x%08x 0x%08x\n", search_op, flags );
1576 return INVALID_HANDLE_VALUE;
1578 if (level != FindExInfoStandard)
1580 FIXME("info level %d not implemented\n", level );
1581 return INVALID_HANDLE_VALUE;
1584 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1586 SetLastError( ERROR_PATH_NOT_FOUND );
1587 return INVALID_HANDLE_VALUE;
1590 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1592 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1593 goto error;
1596 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1598 static const WCHAR dotW[] = {'.',0};
1599 WCHAR *dir = NULL;
1601 /* we still need to check that the directory can be opened */
1603 if (HIWORD(device))
1605 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1607 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1608 goto error;
1610 memcpy( dir, filename, HIWORD(device) );
1611 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1613 RtlFreeUnicodeString( &nt_name );
1614 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1616 HeapFree( GetProcessHeap(), 0, dir );
1617 SetLastError( ERROR_PATH_NOT_FOUND );
1618 goto error;
1620 HeapFree( GetProcessHeap(), 0, dir );
1621 RtlInitUnicodeString( &info->mask, NULL );
1623 else if (!mask || !*mask)
1625 SetLastError( ERROR_FILE_NOT_FOUND );
1626 goto error;
1628 else
1630 if (!RtlCreateUnicodeString( &info->mask, mask ))
1632 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1633 goto error;
1636 /* truncate dir name before mask */
1637 *mask = 0;
1638 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1641 /* check if path is the root of the drive */
1642 info->is_root = FALSE;
1643 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1644 if (p[0] && p[1] == ':')
1646 p += 2;
1647 while (*p == '\\') p++;
1648 info->is_root = (*p == 0);
1651 attr.Length = sizeof(attr);
1652 attr.RootDirectory = 0;
1653 attr.Attributes = OBJ_CASE_INSENSITIVE;
1654 attr.ObjectName = &nt_name;
1655 attr.SecurityDescriptor = NULL;
1656 attr.SecurityQualityOfService = NULL;
1658 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1659 FILE_SHARE_READ | FILE_SHARE_WRITE,
1660 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1662 if (status != STATUS_SUCCESS)
1664 RtlFreeUnicodeString( &info->mask );
1665 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1666 SetLastError( ERROR_PATH_NOT_FOUND );
1667 else
1668 SetLastError( RtlNtStatusToDosError(status) );
1669 goto error;
1672 RtlInitializeCriticalSection( &info->cs );
1673 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
1674 info->path = nt_name;
1675 info->magic = FIND_FIRST_MAGIC;
1676 info->data_pos = 0;
1677 info->data_len = 0;
1678 info->search_op = search_op;
1680 if (device)
1682 WIN32_FIND_DATAW *wfd = data;
1684 memset( wfd, 0, sizeof(*wfd) );
1685 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1686 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1687 CloseHandle( info->handle );
1688 info->handle = 0;
1690 else
1692 IO_STATUS_BLOCK io;
1694 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1695 FileBothDirectoryInformation, FALSE, &info->mask, TRUE );
1696 if (io.u.Status)
1698 FindClose( info );
1699 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1700 return INVALID_HANDLE_VALUE;
1702 info->data_len = io.Information;
1703 if (!FindNextFileW( info, data ))
1705 TRACE( "%s not found\n", debugstr_w(filename) );
1706 FindClose( info );
1707 SetLastError( ERROR_FILE_NOT_FOUND );
1708 return INVALID_HANDLE_VALUE;
1710 if (!strpbrkW( info->mask.Buffer, wildcardsW ))
1712 /* we can't find two files with the same name */
1713 CloseHandle( info->handle );
1714 info->handle = 0;
1717 return info;
1719 error:
1720 HeapFree( GetProcessHeap(), 0, info );
1721 RtlFreeUnicodeString( &nt_name );
1722 return INVALID_HANDLE_VALUE;
1726 /*************************************************************************
1727 * FindNextFileW (KERNEL32.@)
1729 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1731 FIND_FIRST_INFO *info;
1732 FILE_BOTH_DIR_INFORMATION *dir_info;
1733 BOOL ret = FALSE;
1735 TRACE("%p %p\n", handle, data);
1737 if (!handle || handle == INVALID_HANDLE_VALUE)
1739 SetLastError( ERROR_INVALID_HANDLE );
1740 return ret;
1742 info = (FIND_FIRST_INFO *)handle;
1743 if (info->magic != FIND_FIRST_MAGIC)
1745 SetLastError( ERROR_INVALID_HANDLE );
1746 return ret;
1749 RtlEnterCriticalSection( &info->cs );
1751 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
1752 else for (;;)
1754 if (info->data_pos >= info->data_len) /* need to read some more data */
1756 IO_STATUS_BLOCK io;
1758 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1759 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
1760 if (io.u.Status)
1762 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1763 if (io.u.Status == STATUS_NO_MORE_FILES)
1765 CloseHandle( info->handle );
1766 info->handle = 0;
1768 break;
1770 info->data_len = io.Information;
1771 info->data_pos = 0;
1774 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
1776 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
1777 else info->data_pos = info->data_len;
1779 /* don't return '.' and '..' in the root of the drive */
1780 if (info->is_root)
1782 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
1783 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
1784 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
1787 /* check for dir symlink */
1788 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
1789 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
1790 strpbrkW( info->mask.Buffer, wildcardsW ))
1792 if (!check_dir_symlink( info, dir_info )) continue;
1795 data->dwFileAttributes = dir_info->FileAttributes;
1796 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
1797 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
1798 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
1799 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
1800 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
1801 data->dwReserved0 = 0;
1802 data->dwReserved1 = 0;
1804 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
1805 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1806 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
1807 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
1809 TRACE("returning %s (%s)\n",
1810 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
1812 ret = TRUE;
1813 break;
1816 RtlLeaveCriticalSection( &info->cs );
1817 return ret;
1821 /*************************************************************************
1822 * FindClose (KERNEL32.@)
1824 BOOL WINAPI FindClose( HANDLE handle )
1826 FIND_FIRST_INFO *info = (FIND_FIRST_INFO *)handle;
1828 if (!handle || handle == INVALID_HANDLE_VALUE)
1830 SetLastError( ERROR_INVALID_HANDLE );
1831 return FALSE;
1834 __TRY
1836 if (info->magic == FIND_FIRST_MAGIC)
1838 RtlEnterCriticalSection( &info->cs );
1839 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
1841 info->magic = 0;
1842 if (info->handle) CloseHandle( info->handle );
1843 info->handle = 0;
1844 RtlFreeUnicodeString( &info->mask );
1845 info->mask.Buffer = NULL;
1846 RtlFreeUnicodeString( &info->path );
1847 info->data_pos = 0;
1848 info->data_len = 0;
1849 RtlLeaveCriticalSection( &info->cs );
1850 info->cs.DebugInfo->Spare[0] = 0;
1851 RtlDeleteCriticalSection( &info->cs );
1852 HeapFree( GetProcessHeap(), 0, info );
1856 __EXCEPT_PAGE_FAULT
1858 WARN("Illegal handle %p\n", handle);
1859 SetLastError( ERROR_INVALID_HANDLE );
1860 return FALSE;
1862 __ENDTRY
1864 return TRUE;
1868 /*************************************************************************
1869 * FindFirstFileA (KERNEL32.@)
1871 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
1873 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1874 FindExSearchNameMatch, NULL, 0);
1877 /*************************************************************************
1878 * FindFirstFileExA (KERNEL32.@)
1880 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
1881 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
1882 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
1884 HANDLE handle;
1885 WIN32_FIND_DATAA *dataA;
1886 WIN32_FIND_DATAW dataW;
1887 WCHAR *nameW;
1889 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
1891 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1892 if (handle == INVALID_HANDLE_VALUE) return handle;
1894 dataA = (WIN32_FIND_DATAA *) lpFindFileData;
1895 dataA->dwFileAttributes = dataW.dwFileAttributes;
1896 dataA->ftCreationTime = dataW.ftCreationTime;
1897 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
1898 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
1899 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
1900 dataA->nFileSizeLow = dataW.nFileSizeLow;
1901 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
1902 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
1903 sizeof(dataA->cAlternateFileName) );
1904 return handle;
1908 /*************************************************************************
1909 * FindFirstFileW (KERNEL32.@)
1911 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1913 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1914 FindExSearchNameMatch, NULL, 0);
1918 /*************************************************************************
1919 * FindNextFileA (KERNEL32.@)
1921 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1923 WIN32_FIND_DATAW dataW;
1925 if (!FindNextFileW( handle, &dataW )) return FALSE;
1926 data->dwFileAttributes = dataW.dwFileAttributes;
1927 data->ftCreationTime = dataW.ftCreationTime;
1928 data->ftLastAccessTime = dataW.ftLastAccessTime;
1929 data->ftLastWriteTime = dataW.ftLastWriteTime;
1930 data->nFileSizeHigh = dataW.nFileSizeHigh;
1931 data->nFileSizeLow = dataW.nFileSizeLow;
1932 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
1933 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
1934 sizeof(data->cAlternateFileName) );
1935 return TRUE;
1939 /**************************************************************************
1940 * GetFileAttributesW (KERNEL32.@)
1942 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
1944 FILE_BASIC_INFORMATION info;
1945 UNICODE_STRING nt_name;
1946 OBJECT_ATTRIBUTES attr;
1947 NTSTATUS status;
1949 TRACE("%s\n", debugstr_w(name));
1951 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1953 SetLastError( ERROR_PATH_NOT_FOUND );
1954 return INVALID_FILE_ATTRIBUTES;
1957 attr.Length = sizeof(attr);
1958 attr.RootDirectory = 0;
1959 attr.Attributes = OBJ_CASE_INSENSITIVE;
1960 attr.ObjectName = &nt_name;
1961 attr.SecurityDescriptor = NULL;
1962 attr.SecurityQualityOfService = NULL;
1964 status = NtQueryAttributesFile( &attr, &info );
1965 RtlFreeUnicodeString( &nt_name );
1967 if (status == STATUS_SUCCESS) return info.FileAttributes;
1969 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1970 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
1972 SetLastError( RtlNtStatusToDosError(status) );
1973 return INVALID_FILE_ATTRIBUTES;
1977 /**************************************************************************
1978 * GetFileAttributesA (KERNEL32.@)
1980 DWORD WINAPI GetFileAttributesA( LPCSTR name )
1982 WCHAR *nameW;
1984 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
1985 return GetFileAttributesW( nameW );
1989 /**************************************************************************
1990 * SetFileAttributesW (KERNEL32.@)
1992 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
1994 UNICODE_STRING nt_name;
1995 OBJECT_ATTRIBUTES attr;
1996 IO_STATUS_BLOCK io;
1997 NTSTATUS status;
1998 HANDLE handle;
2000 TRACE("%s %x\n", debugstr_w(name), attributes);
2002 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2004 SetLastError( ERROR_PATH_NOT_FOUND );
2005 return FALSE;
2008 attr.Length = sizeof(attr);
2009 attr.RootDirectory = 0;
2010 attr.Attributes = OBJ_CASE_INSENSITIVE;
2011 attr.ObjectName = &nt_name;
2012 attr.SecurityDescriptor = NULL;
2013 attr.SecurityQualityOfService = NULL;
2015 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2016 RtlFreeUnicodeString( &nt_name );
2018 if (status == STATUS_SUCCESS)
2020 FILE_BASIC_INFORMATION info;
2022 memset( &info, 0, sizeof(info) );
2023 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2024 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2025 NtClose( handle );
2028 if (status == STATUS_SUCCESS) return TRUE;
2029 SetLastError( RtlNtStatusToDosError(status) );
2030 return FALSE;
2034 /**************************************************************************
2035 * SetFileAttributesA (KERNEL32.@)
2037 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2039 WCHAR *nameW;
2041 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2042 return SetFileAttributesW( nameW, attributes );
2046 /**************************************************************************
2047 * GetFileAttributesExW (KERNEL32.@)
2049 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2051 FILE_NETWORK_OPEN_INFORMATION info;
2052 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2053 UNICODE_STRING nt_name;
2054 OBJECT_ATTRIBUTES attr;
2055 NTSTATUS status;
2057 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2059 if (level != GetFileExInfoStandard)
2061 SetLastError( ERROR_INVALID_PARAMETER );
2062 return FALSE;
2065 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2067 SetLastError( ERROR_PATH_NOT_FOUND );
2068 return FALSE;
2071 attr.Length = sizeof(attr);
2072 attr.RootDirectory = 0;
2073 attr.Attributes = OBJ_CASE_INSENSITIVE;
2074 attr.ObjectName = &nt_name;
2075 attr.SecurityDescriptor = NULL;
2076 attr.SecurityQualityOfService = NULL;
2078 status = NtQueryFullAttributesFile( &attr, &info );
2079 RtlFreeUnicodeString( &nt_name );
2081 if (status != STATUS_SUCCESS)
2083 SetLastError( RtlNtStatusToDosError(status) );
2084 return FALSE;
2087 data->dwFileAttributes = info.FileAttributes;
2088 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2089 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2090 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2091 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2092 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2093 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2094 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2095 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2096 return TRUE;
2100 /**************************************************************************
2101 * GetFileAttributesExA (KERNEL32.@)
2103 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2105 WCHAR *nameW;
2107 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2108 return GetFileAttributesExW( nameW, level, ptr );
2112 /******************************************************************************
2113 * GetCompressedFileSizeW (KERNEL32.@)
2115 * Get the actual number of bytes used on disk.
2117 * RETURNS
2118 * Success: Low-order doubleword of number of bytes
2119 * Failure: INVALID_FILE_SIZE
2121 DWORD WINAPI GetCompressedFileSizeW(
2122 LPCWSTR name, /* [in] Pointer to name of file */
2123 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2125 UNICODE_STRING nt_name;
2126 OBJECT_ATTRIBUTES attr;
2127 IO_STATUS_BLOCK io;
2128 NTSTATUS status;
2129 HANDLE handle;
2130 DWORD ret = INVALID_FILE_SIZE;
2132 TRACE("%s %p\n", debugstr_w(name), size_high);
2134 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2136 SetLastError( ERROR_PATH_NOT_FOUND );
2137 return INVALID_FILE_SIZE;
2140 attr.Length = sizeof(attr);
2141 attr.RootDirectory = 0;
2142 attr.Attributes = OBJ_CASE_INSENSITIVE;
2143 attr.ObjectName = &nt_name;
2144 attr.SecurityDescriptor = NULL;
2145 attr.SecurityQualityOfService = NULL;
2147 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2148 RtlFreeUnicodeString( &nt_name );
2150 if (status == STATUS_SUCCESS)
2152 /* we don't support compressed files, simply return the file size */
2153 ret = GetFileSize( handle, size_high );
2154 NtClose( handle );
2156 else SetLastError( RtlNtStatusToDosError(status) );
2158 return ret;
2162 /******************************************************************************
2163 * GetCompressedFileSizeA (KERNEL32.@)
2165 * See GetCompressedFileSizeW.
2167 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2169 WCHAR *nameW;
2171 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2172 return GetCompressedFileSizeW( nameW, size_high );
2176 /***********************************************************************
2177 * OpenFile (KERNEL32.@)
2179 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2181 HANDLE handle;
2182 FILETIME filetime;
2183 WORD filedatetime[2];
2185 if (!ofs) return HFILE_ERROR;
2187 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2188 ((mode & 0x3 )==OF_READ)?"OF_READ":
2189 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2190 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2191 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2192 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2193 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2194 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2195 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2196 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2197 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2198 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2199 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2200 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2201 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2202 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2203 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2204 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2208 ofs->cBytes = sizeof(OFSTRUCT);
2209 ofs->nErrCode = 0;
2210 if (mode & OF_REOPEN) name = ofs->szPathName;
2212 if (!name) return HFILE_ERROR;
2214 TRACE("%s %04x\n", name, mode );
2216 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2217 Are there any cases where getting the path here is wrong?
2218 Uwe Bonnes 1997 Apr 2 */
2219 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2221 /* OF_PARSE simply fills the structure */
2223 if (mode & OF_PARSE)
2225 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2226 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2227 return 0;
2230 /* OF_CREATE is completely different from all other options, so
2231 handle it first */
2233 if (mode & OF_CREATE)
2235 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2236 goto error;
2238 else
2240 /* Now look for the file */
2242 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2243 goto error;
2245 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2247 if (mode & OF_DELETE)
2249 if (!DeleteFileA( ofs->szPathName )) goto error;
2250 TRACE("(%s): OF_DELETE return = OK\n", name);
2251 return TRUE;
2254 handle = LongToHandle(_lopen( ofs->szPathName, mode ));
2255 if (handle == INVALID_HANDLE_VALUE) goto error;
2257 GetFileTime( handle, NULL, NULL, &filetime );
2258 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2259 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2261 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2263 CloseHandle( handle );
2264 WARN("(%s): OF_VERIFY failed\n", name );
2265 /* FIXME: what error here? */
2266 SetLastError( ERROR_FILE_NOT_FOUND );
2267 goto error;
2270 ofs->Reserved1 = filedatetime[0];
2271 ofs->Reserved2 = filedatetime[1];
2273 TRACE("(%s): OK, return = %p\n", name, handle );
2274 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2276 CloseHandle( handle );
2277 return TRUE;
2279 return HandleToLong(handle);
2281 error: /* We get here if there was an error opening the file */
2282 ofs->nErrCode = GetLastError();
2283 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2284 return HFILE_ERROR;