Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / kernel32 / file.c
blob09ae968a807395ba09950371e2ccda5b129cd11b
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"
51 WINE_DEFAULT_DEBUG_CHANNEL(file);
53 HANDLE dos_handles[DOS_TABLE_SIZE];
55 /* info structure for FindFirstFile handle */
56 typedef struct
58 DWORD magic; /* magic number */
59 HANDLE handle; /* handle to directory */
60 CRITICAL_SECTION cs; /* crit section protecting this structure */
61 FINDEX_SEARCH_OPS search_op; /* Flags passed to FindFirst. */
62 UNICODE_STRING mask; /* file mask */
63 UNICODE_STRING path; /* NT path used to open the directory */
64 BOOL is_root; /* is directory the root of the drive? */
65 UINT data_pos; /* current position in dir data */
66 UINT data_len; /* length of dir data */
67 BYTE data[8192]; /* directory data */
68 } FIND_FIRST_INFO;
70 #define FIND_FIRST_MAGIC 0xc0ffee11
72 static BOOL oem_file_apis;
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;
380 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
381 io_status, buffer, bytesToRead, &offset, NULL);
383 if (status)
385 SetLastError( RtlNtStatusToDosError(status) );
386 return FALSE;
388 return TRUE;
392 /***********************************************************************
393 * ReadFile (KERNEL32.@)
395 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
396 LPDWORD bytesRead, LPOVERLAPPED overlapped )
398 LARGE_INTEGER offset;
399 PLARGE_INTEGER poffset = NULL;
400 IO_STATUS_BLOCK iosb;
401 PIO_STATUS_BLOCK io_status = &iosb;
402 HANDLE hEvent = 0;
403 NTSTATUS status;
405 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToRead,
406 bytesRead, overlapped );
408 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
409 if (!bytesToRead) return TRUE;
411 if (is_console_handle(hFile))
412 return ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
414 if (overlapped != NULL)
416 offset.u.LowPart = overlapped->u.s.Offset;
417 offset.u.HighPart = overlapped->u.s.OffsetHigh;
418 poffset = &offset;
419 hEvent = overlapped->hEvent;
420 io_status = (PIO_STATUS_BLOCK)overlapped;
422 io_status->u.Status = STATUS_PENDING;
423 io_status->Information = 0;
425 status = NtReadFile(hFile, hEvent, NULL, NULL, io_status, buffer, bytesToRead, poffset, NULL);
427 if (status != STATUS_PENDING && bytesRead)
428 *bytesRead = io_status->Information;
430 if (status && status != STATUS_END_OF_FILE && status != STATUS_TIMEOUT)
432 SetLastError( RtlNtStatusToDosError(status) );
433 return FALSE;
435 return TRUE;
439 /***********************************************************************
440 * WriteFileEx (KERNEL32.@)
442 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
443 LPOVERLAPPED overlapped,
444 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
446 LARGE_INTEGER offset;
447 NTSTATUS status;
448 PIO_STATUS_BLOCK io_status;
450 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
452 if (overlapped == NULL)
454 SetLastError(ERROR_INVALID_PARAMETER);
455 return FALSE;
457 offset.u.LowPart = overlapped->u.s.Offset;
458 offset.u.HighPart = overlapped->u.s.OffsetHigh;
460 io_status = (PIO_STATUS_BLOCK)overlapped;
461 io_status->u.Status = STATUS_PENDING;
463 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
464 io_status, buffer, bytesToWrite, &offset, NULL);
466 if (status) SetLastError( RtlNtStatusToDosError(status) );
467 return !status;
471 /***********************************************************************
472 * WriteFile (KERNEL32.@)
474 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
475 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
477 HANDLE hEvent = NULL;
478 LARGE_INTEGER offset;
479 PLARGE_INTEGER poffset = NULL;
480 NTSTATUS status;
481 IO_STATUS_BLOCK iosb;
482 PIO_STATUS_BLOCK piosb = &iosb;
484 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
486 if (is_console_handle(hFile))
487 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
489 if (overlapped)
491 offset.u.LowPart = overlapped->u.s.Offset;
492 offset.u.HighPart = overlapped->u.s.OffsetHigh;
493 poffset = &offset;
494 hEvent = overlapped->hEvent;
495 piosb = (PIO_STATUS_BLOCK)overlapped;
497 piosb->u.Status = STATUS_PENDING;
498 piosb->Information = 0;
500 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
501 buffer, bytesToWrite, poffset, NULL);
503 /* FIXME: NtWriteFile does not always cause page faults, generate them now */
504 if (status == STATUS_INVALID_USER_BUFFER && !IsBadReadPtr( buffer, bytesToWrite ))
506 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
507 buffer, bytesToWrite, poffset, NULL);
508 if (status != STATUS_INVALID_USER_BUFFER)
509 FIXME("Could not access memory (%p,%d) at first, now OK. Protected by DIBSection code?\n",
510 buffer, bytesToWrite);
513 if (status != STATUS_PENDING && bytesWritten)
514 *bytesWritten = piosb->Information;
516 if (status && status != STATUS_TIMEOUT)
518 SetLastError( RtlNtStatusToDosError(status) );
519 return FALSE;
521 return TRUE;
525 /***********************************************************************
526 * GetOverlappedResult (KERNEL32.@)
528 * Check the result of an Asynchronous data transfer from a file.
530 * Parameters
531 * HANDLE hFile [in] handle of file to check on
532 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
533 * LPDWORD lpTransferred [in/out] number of bytes transferred
534 * BOOL bWait [in] wait for the transfer to complete ?
536 * RETURNS
537 * TRUE on success
538 * FALSE on failure
540 * If successful (and relevant) lpTransferred will hold the number of
541 * bytes transferred during the async operation.
543 * BUGS
545 * Currently only works for WaitCommEvent, ReadFile, WriteFile
546 * with communications ports.
549 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
550 LPDWORD lpTransferred, BOOL bWait)
552 DWORD r = WAIT_OBJECT_0;
554 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
556 if ( lpOverlapped == NULL )
558 ERR("lpOverlapped was null\n");
559 return FALSE;
561 if ( bWait )
563 if ( lpOverlapped->hEvent )
567 TRACE( "waiting on %p\n", lpOverlapped );
568 r = WaitForSingleObjectEx( lpOverlapped->hEvent, INFINITE, TRUE );
569 TRACE( "wait on %p returned %d\n", lpOverlapped, r );
570 } while ( r == WAIT_IO_COMPLETION );
572 else
574 /* busy loop */
575 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
576 Sleep( 10 );
579 else if ( lpOverlapped->Internal == STATUS_PENDING )
581 /* Wait in order to give APCs a chance to run. */
582 /* This is cheating, so we must set the event again in case of success -
583 it may be a non-manual reset event. */
586 TRACE( "waiting on %p\n", lpOverlapped );
587 r = WaitForSingleObjectEx( lpOverlapped->hEvent, 0, TRUE );
588 TRACE( "wait on %p returned %d\n", lpOverlapped, r );
589 } while ( r == WAIT_IO_COMPLETION );
590 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
591 NtSetEvent( lpOverlapped->hEvent, NULL );
593 if ( r == WAIT_FAILED )
595 WARN("wait operation failed\n");
596 return FALSE;
598 if (lpTransferred) *lpTransferred = lpOverlapped->InternalHigh;
600 switch ( lpOverlapped->Internal )
602 case STATUS_SUCCESS:
603 return TRUE;
604 case STATUS_PENDING:
605 SetLastError( ERROR_IO_INCOMPLETE );
606 if ( bWait ) ERR("PENDING status after waiting!\n");
607 return FALSE;
608 default:
609 SetLastError( RtlNtStatusToDosError( lpOverlapped->Internal ) );
610 return FALSE;
614 /***********************************************************************
615 * CancelIo (KERNEL32.@)
617 * Cancels pending I/O operations initiated by the current thread on a file.
619 * PARAMS
620 * handle [I] File handle.
622 * RETURNS
623 * Success: TRUE.
624 * Failure: FALSE, check GetLastError().
626 BOOL WINAPI CancelIo(HANDLE handle)
628 IO_STATUS_BLOCK io_status;
630 NtCancelIoFile(handle, &io_status);
631 if (io_status.u.Status)
633 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
634 return FALSE;
636 return TRUE;
639 /***********************************************************************
640 * _hread (KERNEL32.@)
642 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
644 return _lread( hFile, buffer, count );
648 /***********************************************************************
649 * _hwrite (KERNEL32.@)
651 * experimentation yields that _lwrite:
652 * o truncates the file at the current position with
653 * a 0 len write
654 * o returns 0 on a 0 length write
655 * o works with console handles
658 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
660 DWORD result;
662 TRACE("%d %p %d\n", handle, buffer, count );
664 if (!count)
666 /* Expand or truncate at current position */
667 if (!SetEndOfFile( (HANDLE)handle )) return HFILE_ERROR;
668 return 0;
670 if (!WriteFile( (HANDLE)handle, buffer, count, &result, NULL ))
671 return HFILE_ERROR;
672 return result;
676 /***********************************************************************
677 * _lclose (KERNEL32.@)
679 HFILE WINAPI _lclose( HFILE hFile )
681 TRACE("handle %d\n", hFile );
682 return CloseHandle( (HANDLE)hFile ) ? 0 : HFILE_ERROR;
686 /***********************************************************************
687 * _lcreat (KERNEL32.@)
689 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
691 /* Mask off all flags not explicitly allowed by the doc */
692 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
693 TRACE("%s %02x\n", path, attr );
694 return (HFILE)CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
695 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
696 CREATE_ALWAYS, attr, 0 );
700 /***********************************************************************
701 * _lopen (KERNEL32.@)
703 HFILE WINAPI _lopen( LPCSTR path, INT mode )
705 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
706 return (HFILE)create_file_OF( path, mode & ~OF_CREATE );
709 /***********************************************************************
710 * _lread (KERNEL32.@)
712 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
714 DWORD result;
715 if (!ReadFile( (HANDLE)handle, buffer, count, &result, NULL ))
716 return HFILE_ERROR;
717 return result;
721 /***********************************************************************
722 * _llseek (KERNEL32.@)
724 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
726 return SetFilePointer( (HANDLE)hFile, lOffset, NULL, nOrigin );
730 /***********************************************************************
731 * _lwrite (KERNEL32.@)
733 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
735 return (UINT)_hwrite( hFile, buffer, (LONG)count );
739 /***********************************************************************
740 * FlushFileBuffers (KERNEL32.@)
742 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
744 NTSTATUS nts;
745 IO_STATUS_BLOCK ioblk;
747 if (is_console_handle( hFile ))
749 /* this will fail (as expected) for an output handle */
750 return FlushConsoleInputBuffer( hFile );
752 nts = NtFlushBuffersFile( hFile, &ioblk );
753 if (nts != STATUS_SUCCESS)
755 SetLastError( RtlNtStatusToDosError( nts ) );
756 return FALSE;
759 return TRUE;
763 /***********************************************************************
764 * GetFileType (KERNEL32.@)
766 DWORD WINAPI GetFileType( HANDLE hFile )
768 FILE_FS_DEVICE_INFORMATION info;
769 IO_STATUS_BLOCK io;
770 NTSTATUS status;
772 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
774 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
775 if (status != STATUS_SUCCESS)
777 SetLastError( RtlNtStatusToDosError(status) );
778 return FILE_TYPE_UNKNOWN;
781 switch(info.DeviceType)
783 case FILE_DEVICE_NULL:
784 case FILE_DEVICE_SERIAL_PORT:
785 case FILE_DEVICE_PARALLEL_PORT:
786 case FILE_DEVICE_TAPE:
787 case FILE_DEVICE_UNKNOWN:
788 return FILE_TYPE_CHAR;
789 case FILE_DEVICE_NAMED_PIPE:
790 return FILE_TYPE_PIPE;
791 default:
792 return FILE_TYPE_DISK;
797 /***********************************************************************
798 * GetFileInformationByHandle (KERNEL32.@)
800 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
802 FILE_ALL_INFORMATION all_info;
803 IO_STATUS_BLOCK io;
804 NTSTATUS status;
806 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
807 if (status == STATUS_SUCCESS)
809 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
810 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
811 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
812 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
813 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
814 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
815 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
816 info->dwVolumeSerialNumber = 0; /* FIXME */
817 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
818 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
819 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
820 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
821 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
822 return TRUE;
824 SetLastError( RtlNtStatusToDosError(status) );
825 return FALSE;
829 /***********************************************************************
830 * GetFileSize (KERNEL32.@)
832 * Retrieve the size of a file.
834 * PARAMS
835 * hFile [I] File to retrieve size of.
836 * filesizehigh [O] On return, the high bits of the file size.
838 * RETURNS
839 * Success: The low bits of the file size.
840 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
841 * check GetLastError() for values other than ERROR_SUCCESS.
843 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
845 LARGE_INTEGER size;
846 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
847 if (filesizehigh) *filesizehigh = size.u.HighPart;
848 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
849 return size.u.LowPart;
853 /***********************************************************************
854 * GetFileSizeEx (KERNEL32.@)
856 * Retrieve the size of a file.
858 * PARAMS
859 * hFile [I] File to retrieve size of.
860 * lpFileSIze [O] On return, the size of the file.
862 * RETURNS
863 * Success: TRUE.
864 * Failure: FALSE, check GetLastError().
866 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
868 FILE_END_OF_FILE_INFORMATION info;
869 IO_STATUS_BLOCK io;
870 NTSTATUS status;
872 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileEndOfFileInformation );
873 if (status == STATUS_SUCCESS)
875 *lpFileSize = info.EndOfFile;
876 return TRUE;
878 SetLastError( RtlNtStatusToDosError(status) );
879 return FALSE;
883 /**************************************************************************
884 * SetEndOfFile (KERNEL32.@)
886 * Sets the current position as the end of the file.
888 * PARAMS
889 * hFile [I] File handle.
891 * RETURNS
892 * Success: TRUE.
893 * Failure: FALSE, check GetLastError().
895 BOOL WINAPI SetEndOfFile( HANDLE hFile )
897 FILE_POSITION_INFORMATION pos;
898 FILE_END_OF_FILE_INFORMATION eof;
899 IO_STATUS_BLOCK io;
900 NTSTATUS status;
902 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
903 if (status == STATUS_SUCCESS)
905 eof.EndOfFile = pos.CurrentByteOffset;
906 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
908 if (status == STATUS_SUCCESS) return TRUE;
909 SetLastError( RtlNtStatusToDosError(status) );
910 return FALSE;
914 /***********************************************************************
915 * SetFilePointer (KERNEL32.@)
917 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
919 LARGE_INTEGER dist, newpos;
921 if (highword)
923 dist.u.LowPart = distance;
924 dist.u.HighPart = *highword;
926 else dist.QuadPart = distance;
928 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
930 if (highword) *highword = newpos.u.HighPart;
931 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
932 return newpos.u.LowPart;
936 /***********************************************************************
937 * SetFilePointerEx (KERNEL32.@)
939 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
940 LARGE_INTEGER *newpos, DWORD method )
942 LONGLONG pos;
943 IO_STATUS_BLOCK io;
944 FILE_POSITION_INFORMATION info;
946 switch(method)
948 case FILE_BEGIN:
949 pos = distance.QuadPart;
950 break;
951 case FILE_CURRENT:
952 if (NtQueryInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
953 goto error;
954 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
955 break;
956 case FILE_END:
958 FILE_END_OF_FILE_INFORMATION eof;
959 if (NtQueryInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
960 goto error;
961 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
963 break;
964 default:
965 SetLastError( ERROR_INVALID_PARAMETER );
966 return FALSE;
969 if (pos < 0)
971 SetLastError( ERROR_NEGATIVE_SEEK );
972 return FALSE;
975 info.CurrentByteOffset.QuadPart = pos;
976 if (NtSetInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
977 goto error;
978 if (newpos) newpos->QuadPart = pos;
979 return TRUE;
981 error:
982 SetLastError( RtlNtStatusToDosError(io.u.Status) );
983 return FALSE;
986 /***********************************************************************
987 * GetFileTime (KERNEL32.@)
989 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
990 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
992 FILE_BASIC_INFORMATION info;
993 IO_STATUS_BLOCK io;
994 NTSTATUS status;
996 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
997 if (status == STATUS_SUCCESS)
999 if (lpCreationTime)
1001 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
1002 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
1004 if (lpLastAccessTime)
1006 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
1007 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
1009 if (lpLastWriteTime)
1011 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
1012 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1014 return TRUE;
1016 SetLastError( RtlNtStatusToDosError(status) );
1017 return FALSE;
1021 /***********************************************************************
1022 * SetFileTime (KERNEL32.@)
1024 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1025 const FILETIME *atime, const FILETIME *mtime )
1027 FILE_BASIC_INFORMATION info;
1028 IO_STATUS_BLOCK io;
1029 NTSTATUS status;
1031 memset( &info, 0, sizeof(info) );
1032 if (ctime)
1034 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1035 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1037 if (atime)
1039 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1040 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1042 if (mtime)
1044 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1045 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1048 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1049 if (status == STATUS_SUCCESS) return TRUE;
1050 SetLastError( RtlNtStatusToDosError(status) );
1051 return FALSE;
1055 /**************************************************************************
1056 * LockFile (KERNEL32.@)
1058 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1059 DWORD count_low, DWORD count_high )
1061 NTSTATUS status;
1062 LARGE_INTEGER count, offset;
1064 TRACE( "%p %x%08x %x%08x\n",
1065 hFile, offset_high, offset_low, count_high, count_low );
1067 count.u.LowPart = count_low;
1068 count.u.HighPart = count_high;
1069 offset.u.LowPart = offset_low;
1070 offset.u.HighPart = offset_high;
1072 status = NtLockFile( hFile, 0, NULL, NULL,
1073 NULL, &offset, &count, NULL, TRUE, TRUE );
1075 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1076 return !status;
1080 /**************************************************************************
1081 * LockFileEx [KERNEL32.@]
1083 * Locks a byte range within an open file for shared or exclusive access.
1085 * RETURNS
1086 * success: TRUE
1087 * failure: FALSE
1089 * NOTES
1090 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1092 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1093 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1095 NTSTATUS status;
1096 LARGE_INTEGER count, offset;
1098 if (reserved)
1100 SetLastError( ERROR_INVALID_PARAMETER );
1101 return FALSE;
1104 TRACE( "%p %x%08x %x%08x flags %x\n",
1105 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1106 count_high, count_low, flags );
1108 count.u.LowPart = count_low;
1109 count.u.HighPart = count_high;
1110 offset.u.LowPart = overlapped->u.s.Offset;
1111 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1113 status = NtLockFile( hFile, overlapped->hEvent, NULL, NULL,
1114 NULL, &offset, &count, NULL,
1115 flags & LOCKFILE_FAIL_IMMEDIATELY,
1116 flags & LOCKFILE_EXCLUSIVE_LOCK );
1118 if (status) SetLastError( RtlNtStatusToDosError(status) );
1119 return !status;
1123 /**************************************************************************
1124 * UnlockFile (KERNEL32.@)
1126 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1127 DWORD count_low, DWORD count_high )
1129 NTSTATUS status;
1130 LARGE_INTEGER count, offset;
1132 count.u.LowPart = count_low;
1133 count.u.HighPart = count_high;
1134 offset.u.LowPart = offset_low;
1135 offset.u.HighPart = offset_high;
1137 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1138 if (status) SetLastError( RtlNtStatusToDosError(status) );
1139 return !status;
1143 /**************************************************************************
1144 * UnlockFileEx (KERNEL32.@)
1146 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1147 LPOVERLAPPED overlapped )
1149 if (reserved)
1151 SetLastError( ERROR_INVALID_PARAMETER );
1152 return FALSE;
1154 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1156 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1160 /***********************************************************************
1161 * Win32HandleToDosFileHandle (KERNEL32.21)
1163 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1164 * longer valid after this function (even on failure).
1166 * Note: this is not exactly right, since on Win95 the Win32 handles
1167 * are on top of DOS handles and we do it the other way
1168 * around. Should be good enough though.
1170 HFILE WINAPI Win32HandleToDosFileHandle( HANDLE handle )
1172 int i;
1174 if (!handle || (handle == INVALID_HANDLE_VALUE))
1175 return HFILE_ERROR;
1177 FILE_InitProcessDosHandles();
1178 for (i = 0; i < DOS_TABLE_SIZE; i++)
1179 if (!dos_handles[i])
1181 dos_handles[i] = handle;
1182 TRACE("Got %d for h32 %p\n", i, handle );
1183 return (HFILE)i;
1185 CloseHandle( handle );
1186 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1187 return HFILE_ERROR;
1191 /***********************************************************************
1192 * DosFileHandleToWin32Handle (KERNEL32.20)
1194 * Return the Win32 handle for a DOS handle.
1196 * Note: this is not exactly right, since on Win95 the Win32 handles
1197 * are on top of DOS handles and we do it the other way
1198 * around. Should be good enough though.
1200 HANDLE WINAPI DosFileHandleToWin32Handle( HFILE handle )
1202 HFILE16 hfile = (HFILE16)handle;
1203 if (hfile < 5) FILE_InitProcessDosHandles();
1204 if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
1206 SetLastError( ERROR_INVALID_HANDLE );
1207 return INVALID_HANDLE_VALUE;
1209 return dos_handles[hfile];
1213 /*************************************************************************
1214 * SetHandleCount (KERNEL32.@)
1216 UINT WINAPI SetHandleCount( UINT count )
1218 return min( 256, count );
1222 /***********************************************************************
1223 * DisposeLZ32Handle (KERNEL32.22)
1225 * Note: this is not entirely correct, we should only close the
1226 * 32-bit handle and not the 16-bit one, but we cannot do
1227 * this because of the way our DOS handles are implemented.
1228 * It shouldn't break anything though.
1230 void WINAPI DisposeLZ32Handle( HANDLE handle )
1232 int i;
1234 if (!handle || (handle == INVALID_HANDLE_VALUE)) return;
1236 for (i = 5; i < DOS_TABLE_SIZE; i++)
1237 if (dos_handles[i] == handle)
1239 dos_handles[i] = 0;
1240 CloseHandle( handle );
1241 break;
1245 /**************************************************************************
1246 * Operations on file names *
1247 **************************************************************************/
1250 /*************************************************************************
1251 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1253 * Creates or opens an object, and returns a handle that can be used to
1254 * access that object.
1256 * PARAMS
1258 * filename [in] pointer to filename to be accessed
1259 * access [in] access mode requested
1260 * sharing [in] share mode
1261 * sa [in] pointer to security attributes
1262 * creation [in] how to create the file
1263 * attributes [in] attributes for newly created file
1264 * template [in] handle to file with extended attributes to copy
1266 * RETURNS
1267 * Success: Open handle to specified file
1268 * Failure: INVALID_HANDLE_VALUE
1270 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1271 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1272 DWORD attributes, HANDLE template )
1274 NTSTATUS status;
1275 UINT options;
1276 OBJECT_ATTRIBUTES attr;
1277 UNICODE_STRING nameW;
1278 IO_STATUS_BLOCK io;
1279 HANDLE ret;
1280 DWORD dosdev;
1281 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1282 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1283 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1284 SECURITY_QUALITY_OF_SERVICE qos;
1286 static const UINT nt_disposition[5] =
1288 FILE_CREATE, /* CREATE_NEW */
1289 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1290 FILE_OPEN, /* OPEN_EXISTING */
1291 FILE_OPEN_IF, /* OPEN_ALWAYS */
1292 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1296 /* sanity checks */
1298 if (!filename || !filename[0])
1300 SetLastError( ERROR_PATH_NOT_FOUND );
1301 return INVALID_HANDLE_VALUE;
1304 TRACE("%s %s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1305 (access & GENERIC_READ)?"GENERIC_READ ":"",
1306 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1307 (!access)?"QUERY_ACCESS ":"",
1308 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1309 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1310 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1311 creation, attributes);
1313 /* Open a console for CONIN$ or CONOUT$ */
1315 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1317 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle), creation);
1318 goto done;
1321 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1323 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1324 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1326 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1327 !strncmpiW( filename + 4, pipeW, 5 ) ||
1328 !strncmpiW( filename + 4, mailslotW, 9 ))
1330 dosdev = 0;
1332 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1334 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1336 else if (!(GetVersion() & 0x80000000))
1338 dosdev = 0;
1340 else if (filename[4])
1342 if (!(GetVersion() & 0x80000000)) /* there are no VxDs on NT */
1343 ret = Device_Open( filename+4, access, sa );
1344 else
1345 ret = VXD_Open( filename+4, access, sa );
1347 goto done;
1349 else
1351 SetLastError( ERROR_INVALID_NAME );
1352 return INVALID_HANDLE_VALUE;
1355 else dosdev = RtlIsDosDeviceName_U( filename );
1357 if (dosdev)
1359 static const WCHAR conW[] = {'C','O','N'};
1361 if (LOWORD(dosdev) == sizeof(conW) &&
1362 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1364 switch (access & (GENERIC_READ|GENERIC_WRITE))
1366 case GENERIC_READ:
1367 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), creation);
1368 goto done;
1369 case GENERIC_WRITE:
1370 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), creation);
1371 goto done;
1372 default:
1373 SetLastError( ERROR_FILE_NOT_FOUND );
1374 return INVALID_HANDLE_VALUE;
1379 if (!creation) creation = OPEN_EXISTING;
1381 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1383 SetLastError( ERROR_INVALID_PARAMETER );
1384 return INVALID_HANDLE_VALUE;
1387 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1389 SetLastError( ERROR_PATH_NOT_FOUND );
1390 return INVALID_HANDLE_VALUE;
1393 /* now call NtCreateFile */
1395 options = 0;
1396 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1397 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1398 else
1399 options |= FILE_NON_DIRECTORY_FILE;
1400 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1402 options |= FILE_DELETE_ON_CLOSE;
1403 access |= DELETE;
1405 if (!(attributes & FILE_FLAG_OVERLAPPED))
1406 options |= FILE_SYNCHRONOUS_IO_ALERT;
1407 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1408 options |= FILE_RANDOM_ACCESS;
1409 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1411 attr.Length = sizeof(attr);
1412 attr.RootDirectory = 0;
1413 attr.Attributes = OBJ_CASE_INSENSITIVE;
1414 attr.ObjectName = &nameW;
1415 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1416 if (attributes & SECURITY_SQOS_PRESENT)
1418 qos.Length = sizeof(qos);
1419 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
1420 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
1421 qos.EffectiveOnly = attributes & SECURITY_EFFECTIVE_ONLY ? TRUE : FALSE;
1422 attr.SecurityQualityOfService = &qos;
1424 else
1425 attr.SecurityQualityOfService = NULL;
1427 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1429 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1430 sharing, nt_disposition[creation - CREATE_NEW],
1431 options, NULL, 0 );
1432 if (status)
1434 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1435 ret = INVALID_HANDLE_VALUE;
1437 /* In the case file creation was rejected due to CREATE_NEW flag
1438 * was specified and file with that name already exists, correct
1439 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1440 * Note: RtlNtStatusToDosError is not the subject to blame here.
1442 if (status == STATUS_OBJECT_NAME_COLLISION)
1443 SetLastError( ERROR_FILE_EXISTS );
1444 else
1445 SetLastError( RtlNtStatusToDosError(status) );
1447 else SetLastError(0);
1448 RtlFreeUnicodeString( &nameW );
1450 done:
1451 if (!ret) ret = INVALID_HANDLE_VALUE;
1452 TRACE("returning %p\n", ret);
1453 return ret;
1458 /*************************************************************************
1459 * CreateFileA (KERNEL32.@)
1461 * See CreateFileW.
1463 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1464 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1465 DWORD attributes, HANDLE template)
1467 WCHAR *nameW;
1469 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1470 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1474 /***********************************************************************
1475 * DeleteFileW (KERNEL32.@)
1477 * Delete a file.
1479 * PARAMS
1480 * path [I] Path to the file to delete.
1482 * RETURNS
1483 * Success: TRUE.
1484 * Failure: FALSE, check GetLastError().
1486 BOOL WINAPI DeleteFileW( LPCWSTR path )
1488 UNICODE_STRING nameW;
1489 OBJECT_ATTRIBUTES attr;
1490 NTSTATUS status;
1492 TRACE("%s\n", debugstr_w(path) );
1494 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1496 SetLastError( ERROR_PATH_NOT_FOUND );
1497 return FALSE;
1500 attr.Length = sizeof(attr);
1501 attr.RootDirectory = 0;
1502 attr.Attributes = OBJ_CASE_INSENSITIVE;
1503 attr.ObjectName = &nameW;
1504 attr.SecurityDescriptor = NULL;
1505 attr.SecurityQualityOfService = NULL;
1507 status = NtDeleteFile(&attr);
1508 RtlFreeUnicodeString( &nameW );
1509 if (status)
1511 SetLastError( RtlNtStatusToDosError(status) );
1512 return FALSE;
1514 return TRUE;
1518 /***********************************************************************
1519 * DeleteFileA (KERNEL32.@)
1521 * See DeleteFileW.
1523 BOOL WINAPI DeleteFileA( LPCSTR path )
1525 WCHAR *pathW;
1527 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1528 return DeleteFileW( pathW );
1532 /**************************************************************************
1533 * ReplaceFileW (KERNEL32.@)
1534 * ReplaceFile (KERNEL32.@)
1536 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName,LPCWSTR lpReplacementFileName,
1537 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1538 LPVOID lpExclude, LPVOID lpReserved)
1540 FIXME("(%s,%s,%s,%08x,%p,%p) stub\n",debugstr_w(lpReplacedFileName),debugstr_w(lpReplacementFileName),
1541 debugstr_w(lpBackupFileName),dwReplaceFlags,lpExclude,lpReserved);
1542 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1543 return FALSE;
1547 /**************************************************************************
1548 * ReplaceFileA (KERNEL32.@)
1550 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1551 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1552 LPVOID lpExclude, LPVOID lpReserved)
1554 FIXME("(%s,%s,%s,%08x,%p,%p) stub\n",lpReplacedFileName,lpReplacementFileName,
1555 lpBackupFileName,dwReplaceFlags,lpExclude,lpReserved);
1556 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1557 return FALSE;
1561 /*************************************************************************
1562 * FindFirstFileExW (KERNEL32.@)
1564 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1565 LPVOID data, FINDEX_SEARCH_OPS search_op,
1566 LPVOID filter, DWORD flags)
1568 static const WCHAR wildcardsW[] = { '*','?',0 };
1569 WCHAR *mask, *p;
1570 FIND_FIRST_INFO *info = NULL;
1571 UNICODE_STRING nt_name;
1572 OBJECT_ATTRIBUTES attr;
1573 IO_STATUS_BLOCK io;
1574 NTSTATUS status;
1575 DWORD device = 0;
1577 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1579 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1580 || flags != 0)
1582 FIXME("options not implemented 0x%08x 0x%08x\n", search_op, flags );
1583 return INVALID_HANDLE_VALUE;
1585 if (level != FindExInfoStandard)
1587 FIXME("info level %d not implemented\n", level );
1588 return INVALID_HANDLE_VALUE;
1591 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1593 SetLastError( ERROR_PATH_NOT_FOUND );
1594 return INVALID_HANDLE_VALUE;
1597 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1599 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1600 goto error;
1603 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1605 static const WCHAR dotW[] = {'.',0};
1606 WCHAR *dir = NULL;
1608 /* we still need to check that the directory can be opened */
1610 if (HIWORD(device))
1612 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1614 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1615 goto error;
1617 memcpy( dir, filename, HIWORD(device) );
1618 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1620 RtlFreeUnicodeString( &nt_name );
1621 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1623 HeapFree( GetProcessHeap(), 0, dir );
1624 SetLastError( ERROR_PATH_NOT_FOUND );
1625 goto error;
1627 HeapFree( GetProcessHeap(), 0, dir );
1628 RtlInitUnicodeString( &info->mask, NULL );
1630 else if (!mask || !*mask)
1632 SetLastError( ERROR_FILE_NOT_FOUND );
1633 goto error;
1635 else
1637 if (!RtlCreateUnicodeString( &info->mask, mask ))
1639 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1640 goto error;
1643 /* truncate dir name before mask */
1644 *mask = 0;
1645 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1648 /* check if path is the root of the drive */
1649 info->is_root = FALSE;
1650 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1651 if (p[0] && p[1] == ':')
1653 p += 2;
1654 while (*p == '\\') p++;
1655 info->is_root = (*p == 0);
1658 attr.Length = sizeof(attr);
1659 attr.RootDirectory = 0;
1660 attr.Attributes = OBJ_CASE_INSENSITIVE;
1661 attr.ObjectName = &nt_name;
1662 attr.SecurityDescriptor = NULL;
1663 attr.SecurityQualityOfService = NULL;
1665 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1666 FILE_SHARE_READ | FILE_SHARE_WRITE,
1667 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1669 if (status != STATUS_SUCCESS)
1671 RtlFreeUnicodeString( &info->mask );
1672 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1673 SetLastError( ERROR_PATH_NOT_FOUND );
1674 else
1675 SetLastError( RtlNtStatusToDosError(status) );
1676 goto error;
1679 RtlInitializeCriticalSection( &info->cs );
1680 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
1681 info->path = nt_name;
1682 info->magic = FIND_FIRST_MAGIC;
1683 info->data_pos = 0;
1684 info->data_len = 0;
1685 info->search_op = search_op;
1687 if (device)
1689 WIN32_FIND_DATAW *wfd = data;
1691 memset( wfd, 0, sizeof(*wfd) );
1692 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1693 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1694 CloseHandle( info->handle );
1695 info->handle = 0;
1697 else
1699 IO_STATUS_BLOCK io;
1701 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1702 FileBothDirectoryInformation, FALSE, &info->mask, TRUE );
1703 if (io.u.Status)
1705 FindClose( info );
1706 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1707 return INVALID_HANDLE_VALUE;
1709 info->data_len = io.Information;
1710 if (!FindNextFileW( info, data ))
1712 TRACE( "%s not found\n", debugstr_w(filename) );
1713 FindClose( info );
1714 SetLastError( ERROR_FILE_NOT_FOUND );
1715 return INVALID_HANDLE_VALUE;
1717 if (!strpbrkW( info->mask.Buffer, wildcardsW ))
1719 /* we can't find two files with the same name */
1720 CloseHandle( info->handle );
1721 info->handle = 0;
1724 return info;
1726 error:
1727 HeapFree( GetProcessHeap(), 0, info );
1728 RtlFreeUnicodeString( &nt_name );
1729 return INVALID_HANDLE_VALUE;
1733 /*************************************************************************
1734 * FindNextFileW (KERNEL32.@)
1736 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1738 FIND_FIRST_INFO *info;
1739 FILE_BOTH_DIR_INFORMATION *dir_info;
1740 BOOL ret = FALSE;
1742 TRACE("%p %p\n", handle, data);
1744 if (!handle || handle == INVALID_HANDLE_VALUE)
1746 SetLastError( ERROR_INVALID_HANDLE );
1747 return ret;
1749 info = (FIND_FIRST_INFO *)handle;
1750 if (info->magic != FIND_FIRST_MAGIC)
1752 SetLastError( ERROR_INVALID_HANDLE );
1753 return ret;
1756 RtlEnterCriticalSection( &info->cs );
1758 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
1759 else for (;;)
1761 if (info->data_pos >= info->data_len) /* need to read some more data */
1763 IO_STATUS_BLOCK io;
1765 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1766 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
1767 if (io.u.Status)
1769 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1770 if (io.u.Status == STATUS_NO_MORE_FILES)
1772 CloseHandle( info->handle );
1773 info->handle = 0;
1775 break;
1777 info->data_len = io.Information;
1778 info->data_pos = 0;
1781 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
1783 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
1784 else info->data_pos = info->data_len;
1786 /* don't return '.' and '..' in the root of the drive */
1787 if (info->is_root)
1789 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
1790 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
1791 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
1794 /* check for dir symlink */
1795 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
1796 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
1798 if (!check_dir_symlink( info, dir_info )) continue;
1800 if (info->search_op == FindExSearchLimitToDirectories &&
1801 (dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
1802 continue;
1804 data->dwFileAttributes = dir_info->FileAttributes;
1805 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
1806 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
1807 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
1808 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
1809 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
1810 data->dwReserved0 = 0;
1811 data->dwReserved1 = 0;
1813 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
1814 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1815 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
1816 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
1818 TRACE("returning %s (%s)\n",
1819 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
1821 ret = TRUE;
1822 break;
1825 RtlLeaveCriticalSection( &info->cs );
1826 return ret;
1830 /*************************************************************************
1831 * FindClose (KERNEL32.@)
1833 BOOL WINAPI FindClose( HANDLE handle )
1835 FIND_FIRST_INFO *info = (FIND_FIRST_INFO *)handle;
1837 if (!handle || handle == INVALID_HANDLE_VALUE)
1839 SetLastError( ERROR_INVALID_HANDLE );
1840 return FALSE;
1843 __TRY
1845 if (info->magic == FIND_FIRST_MAGIC)
1847 RtlEnterCriticalSection( &info->cs );
1848 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
1850 info->magic = 0;
1851 if (info->handle) CloseHandle( info->handle );
1852 info->handle = 0;
1853 RtlFreeUnicodeString( &info->mask );
1854 info->mask.Buffer = NULL;
1855 RtlFreeUnicodeString( &info->path );
1856 info->data_pos = 0;
1857 info->data_len = 0;
1858 RtlLeaveCriticalSection( &info->cs );
1859 info->cs.DebugInfo->Spare[0] = 0;
1860 RtlDeleteCriticalSection( &info->cs );
1861 HeapFree( GetProcessHeap(), 0, info );
1865 __EXCEPT_PAGE_FAULT
1867 WARN("Illegal handle %p\n", handle);
1868 SetLastError( ERROR_INVALID_HANDLE );
1869 return FALSE;
1871 __ENDTRY
1873 return TRUE;
1877 /*************************************************************************
1878 * FindFirstFileA (KERNEL32.@)
1880 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
1882 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1883 FindExSearchNameMatch, NULL, 0);
1886 /*************************************************************************
1887 * FindFirstFileExA (KERNEL32.@)
1889 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
1890 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
1891 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
1893 HANDLE handle;
1894 WIN32_FIND_DATAA *dataA;
1895 WIN32_FIND_DATAW dataW;
1896 WCHAR *nameW;
1898 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
1900 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1901 if (handle == INVALID_HANDLE_VALUE) return handle;
1903 dataA = (WIN32_FIND_DATAA *) lpFindFileData;
1904 dataA->dwFileAttributes = dataW.dwFileAttributes;
1905 dataA->ftCreationTime = dataW.ftCreationTime;
1906 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
1907 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
1908 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
1909 dataA->nFileSizeLow = dataW.nFileSizeLow;
1910 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
1911 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
1912 sizeof(dataA->cAlternateFileName) );
1913 return handle;
1917 /*************************************************************************
1918 * FindFirstFileW (KERNEL32.@)
1920 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1922 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1923 FindExSearchNameMatch, NULL, 0);
1927 /*************************************************************************
1928 * FindNextFileA (KERNEL32.@)
1930 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1932 WIN32_FIND_DATAW dataW;
1934 if (!FindNextFileW( handle, &dataW )) return FALSE;
1935 data->dwFileAttributes = dataW.dwFileAttributes;
1936 data->ftCreationTime = dataW.ftCreationTime;
1937 data->ftLastAccessTime = dataW.ftLastAccessTime;
1938 data->ftLastWriteTime = dataW.ftLastWriteTime;
1939 data->nFileSizeHigh = dataW.nFileSizeHigh;
1940 data->nFileSizeLow = dataW.nFileSizeLow;
1941 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
1942 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
1943 sizeof(data->cAlternateFileName) );
1944 return TRUE;
1948 /**************************************************************************
1949 * GetFileAttributesW (KERNEL32.@)
1951 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
1953 FILE_BASIC_INFORMATION info;
1954 UNICODE_STRING nt_name;
1955 OBJECT_ATTRIBUTES attr;
1956 NTSTATUS status;
1958 TRACE("%s\n", debugstr_w(name));
1960 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1962 SetLastError( ERROR_PATH_NOT_FOUND );
1963 return INVALID_FILE_ATTRIBUTES;
1966 attr.Length = sizeof(attr);
1967 attr.RootDirectory = 0;
1968 attr.Attributes = OBJ_CASE_INSENSITIVE;
1969 attr.ObjectName = &nt_name;
1970 attr.SecurityDescriptor = NULL;
1971 attr.SecurityQualityOfService = NULL;
1973 status = NtQueryAttributesFile( &attr, &info );
1974 RtlFreeUnicodeString( &nt_name );
1976 if (status == STATUS_SUCCESS) return info.FileAttributes;
1978 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1979 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
1981 SetLastError( RtlNtStatusToDosError(status) );
1982 return INVALID_FILE_ATTRIBUTES;
1986 /**************************************************************************
1987 * GetFileAttributesA (KERNEL32.@)
1989 DWORD WINAPI GetFileAttributesA( LPCSTR name )
1991 WCHAR *nameW;
1993 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
1994 return GetFileAttributesW( nameW );
1998 /**************************************************************************
1999 * SetFileAttributesW (KERNEL32.@)
2001 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
2003 UNICODE_STRING nt_name;
2004 OBJECT_ATTRIBUTES attr;
2005 IO_STATUS_BLOCK io;
2006 NTSTATUS status;
2007 HANDLE handle;
2009 TRACE("%s %x\n", debugstr_w(name), attributes);
2011 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2013 SetLastError( ERROR_PATH_NOT_FOUND );
2014 return FALSE;
2017 attr.Length = sizeof(attr);
2018 attr.RootDirectory = 0;
2019 attr.Attributes = OBJ_CASE_INSENSITIVE;
2020 attr.ObjectName = &nt_name;
2021 attr.SecurityDescriptor = NULL;
2022 attr.SecurityQualityOfService = NULL;
2024 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2025 RtlFreeUnicodeString( &nt_name );
2027 if (status == STATUS_SUCCESS)
2029 FILE_BASIC_INFORMATION info;
2031 memset( &info, 0, sizeof(info) );
2032 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2033 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2034 NtClose( handle );
2037 if (status == STATUS_SUCCESS) return TRUE;
2038 SetLastError( RtlNtStatusToDosError(status) );
2039 return FALSE;
2043 /**************************************************************************
2044 * SetFileAttributesA (KERNEL32.@)
2046 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2048 WCHAR *nameW;
2050 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2051 return SetFileAttributesW( nameW, attributes );
2055 /**************************************************************************
2056 * GetFileAttributesExW (KERNEL32.@)
2058 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2060 FILE_NETWORK_OPEN_INFORMATION info;
2061 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2062 UNICODE_STRING nt_name;
2063 OBJECT_ATTRIBUTES attr;
2064 NTSTATUS status;
2066 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2068 if (level != GetFileExInfoStandard)
2070 SetLastError( ERROR_INVALID_PARAMETER );
2071 return FALSE;
2074 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2076 SetLastError( ERROR_PATH_NOT_FOUND );
2077 return FALSE;
2080 attr.Length = sizeof(attr);
2081 attr.RootDirectory = 0;
2082 attr.Attributes = OBJ_CASE_INSENSITIVE;
2083 attr.ObjectName = &nt_name;
2084 attr.SecurityDescriptor = NULL;
2085 attr.SecurityQualityOfService = NULL;
2087 status = NtQueryFullAttributesFile( &attr, &info );
2088 RtlFreeUnicodeString( &nt_name );
2090 if (status != STATUS_SUCCESS)
2092 SetLastError( RtlNtStatusToDosError(status) );
2093 return FALSE;
2096 data->dwFileAttributes = info.FileAttributes;
2097 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2098 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2099 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2100 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2101 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2102 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2103 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2104 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2105 return TRUE;
2109 /**************************************************************************
2110 * GetFileAttributesExA (KERNEL32.@)
2112 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2114 WCHAR *nameW;
2116 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2117 return GetFileAttributesExW( nameW, level, ptr );
2121 /******************************************************************************
2122 * GetCompressedFileSizeW (KERNEL32.@)
2124 * Get the actual number of bytes used on disk.
2126 * RETURNS
2127 * Success: Low-order doubleword of number of bytes
2128 * Failure: INVALID_FILE_SIZE
2130 DWORD WINAPI GetCompressedFileSizeW(
2131 LPCWSTR name, /* [in] Pointer to name of file */
2132 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2134 UNICODE_STRING nt_name;
2135 OBJECT_ATTRIBUTES attr;
2136 IO_STATUS_BLOCK io;
2137 NTSTATUS status;
2138 HANDLE handle;
2139 DWORD ret = INVALID_FILE_SIZE;
2141 TRACE("%s %p\n", debugstr_w(name), size_high);
2143 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2145 SetLastError( ERROR_PATH_NOT_FOUND );
2146 return INVALID_FILE_SIZE;
2149 attr.Length = sizeof(attr);
2150 attr.RootDirectory = 0;
2151 attr.Attributes = OBJ_CASE_INSENSITIVE;
2152 attr.ObjectName = &nt_name;
2153 attr.SecurityDescriptor = NULL;
2154 attr.SecurityQualityOfService = NULL;
2156 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2157 RtlFreeUnicodeString( &nt_name );
2159 if (status == STATUS_SUCCESS)
2161 /* we don't support compressed files, simply return the file size */
2162 ret = GetFileSize( handle, size_high );
2163 NtClose( handle );
2165 else SetLastError( RtlNtStatusToDosError(status) );
2167 return ret;
2171 /******************************************************************************
2172 * GetCompressedFileSizeA (KERNEL32.@)
2174 * See GetCompressedFileSizeW.
2176 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2178 WCHAR *nameW;
2180 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2181 return GetCompressedFileSizeW( nameW, size_high );
2185 /***********************************************************************
2186 * OpenFile (KERNEL32.@)
2188 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2190 HANDLE handle;
2191 FILETIME filetime;
2192 WORD filedatetime[2];
2194 if (!ofs) return HFILE_ERROR;
2196 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2197 ((mode & 0x3 )==OF_READ)?"OF_READ":
2198 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2199 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2200 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2201 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2202 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2203 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2204 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2205 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2206 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2207 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2208 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2209 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2210 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2211 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2212 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2213 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2217 ofs->cBytes = sizeof(OFSTRUCT);
2218 ofs->nErrCode = 0;
2219 if (mode & OF_REOPEN) name = ofs->szPathName;
2221 if (!name) return HFILE_ERROR;
2223 TRACE("%s %04x\n", name, mode );
2225 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2226 Are there any cases where getting the path here is wrong?
2227 Uwe Bonnes 1997 Apr 2 */
2228 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2230 /* OF_PARSE simply fills the structure */
2232 if (mode & OF_PARSE)
2234 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2235 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2236 return 0;
2239 /* OF_CREATE is completely different from all other options, so
2240 handle it first */
2242 if (mode & OF_CREATE)
2244 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2245 goto error;
2247 else
2249 /* Now look for the file */
2251 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2252 goto error;
2254 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2256 if (mode & OF_DELETE)
2258 if (!DeleteFileA( ofs->szPathName )) goto error;
2259 TRACE("(%s): OF_DELETE return = OK\n", name);
2260 return TRUE;
2263 handle = (HANDLE)_lopen( ofs->szPathName, mode );
2264 if (handle == INVALID_HANDLE_VALUE) goto error;
2266 GetFileTime( handle, NULL, NULL, &filetime );
2267 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2268 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2270 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2272 CloseHandle( handle );
2273 WARN("(%s): OF_VERIFY failed\n", name );
2274 /* FIXME: what error here? */
2275 SetLastError( ERROR_FILE_NOT_FOUND );
2276 goto error;
2279 ofs->Reserved1 = filedatetime[0];
2280 ofs->Reserved2 = filedatetime[1];
2282 TRACE("(%s): OK, return = %p\n", name, handle );
2283 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2285 CloseHandle( handle );
2286 return TRUE;
2288 else return (HFILE)handle;
2290 error: /* We get here if there was an error opening the file */
2291 ofs->nErrCode = GetLastError();
2292 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2293 return HFILE_ERROR;