wined3d: Use a common function for parsing SM4 source and destination parameters.
[wine.git] / dlls / kernel32 / file.c
blob41abe49e30b91d6cee0451c1d7cfdeeb9408238a
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 2008 Jeff Zaroyko
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
35 #include "winerror.h"
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winternl.h"
41 #include "winioctl.h"
42 #include "wincon.h"
43 #include "kernel_private.h"
45 #include "wine/exception.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(file);
51 /* info structure for FindFirstFile handle */
52 typedef struct
54 DWORD magic; /* magic number */
55 HANDLE handle; /* handle to directory */
56 CRITICAL_SECTION cs; /* crit section protecting this structure */
57 FINDEX_SEARCH_OPS search_op; /* Flags passed to FindFirst. */
58 UNICODE_STRING mask; /* file mask */
59 UNICODE_STRING path; /* NT path used to open the directory */
60 BOOL is_root; /* is directory the root of the drive? */
61 UINT data_pos; /* current position in dir data */
62 UINT data_len; /* length of dir data */
63 BYTE data[8192]; /* directory data */
64 } FIND_FIRST_INFO;
66 #define FIND_FIRST_MAGIC 0xc0ffee11
68 static BOOL oem_file_apis;
70 static const WCHAR wildcardsW[] = { '*','?',0 };
72 /***********************************************************************
73 * create_file_OF
75 * Wrapper for CreateFile that takes OF_* mode flags.
77 static HANDLE create_file_OF( LPCSTR path, INT mode )
79 DWORD access, sharing, creation;
81 if (mode & OF_CREATE)
83 creation = CREATE_ALWAYS;
84 access = GENERIC_READ | GENERIC_WRITE;
86 else
88 creation = OPEN_EXISTING;
89 switch(mode & 0x03)
91 case OF_READ: access = GENERIC_READ; break;
92 case OF_WRITE: access = GENERIC_WRITE; break;
93 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
94 default: access = 0; break;
98 switch(mode & 0x70)
100 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
101 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
102 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
103 case OF_SHARE_DENY_NONE:
104 case OF_SHARE_COMPAT:
105 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
107 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
111 /***********************************************************************
112 * check_dir_symlink
114 * Check if a dir symlink should be returned by FindNextFile.
116 static BOOL check_dir_symlink( FIND_FIRST_INFO *info, const FILE_BOTH_DIR_INFORMATION *file_info )
118 UNICODE_STRING str;
119 ANSI_STRING unix_name;
120 struct stat st, parent_st;
121 BOOL ret = TRUE;
122 DWORD len;
124 str.MaximumLength = info->path.Length + sizeof(WCHAR) + file_info->FileNameLength;
125 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength ))) return TRUE;
126 memcpy( str.Buffer, info->path.Buffer, info->path.Length );
127 len = info->path.Length / sizeof(WCHAR);
128 if (!len || str.Buffer[len-1] != '\\') str.Buffer[len++] = '\\';
129 memcpy( str.Buffer + len, file_info->FileName, file_info->FileNameLength );
130 str.Length = len * sizeof(WCHAR) + file_info->FileNameLength;
132 unix_name.Buffer = NULL;
133 if (!wine_nt_to_unix_file_name( &str, &unix_name, OPEN_EXISTING, FALSE ) &&
134 !stat( unix_name.Buffer, &st ))
136 char *p = unix_name.Buffer + unix_name.Length - 1;
138 /* skip trailing slashes */
139 while (p > unix_name.Buffer && *p == '/') p--;
141 while (ret && p > unix_name.Buffer)
143 while (p > unix_name.Buffer && *p != '/') p--;
144 while (p > unix_name.Buffer && *p == '/') p--;
145 p[1] = 0;
146 if (!stat( unix_name.Buffer, &parent_st ) &&
147 parent_st.st_dev == st.st_dev &&
148 parent_st.st_ino == st.st_ino)
150 WARN( "suppressing dir symlink %s pointing to parent %s\n",
151 debugstr_wn( str.Buffer, str.Length/sizeof(WCHAR) ),
152 debugstr_a( unix_name.Buffer ));
153 ret = FALSE;
157 RtlFreeAnsiString( &unix_name );
158 RtlFreeUnicodeString( &str );
159 return ret;
163 /***********************************************************************
164 * FILE_SetDosError
166 * Set the DOS error code from errno.
168 void FILE_SetDosError(void)
170 int save_errno = errno; /* errno gets overwritten by printf */
172 TRACE("errno = %d %s\n", errno, strerror(errno));
173 switch (save_errno)
175 case EAGAIN:
176 SetLastError( ERROR_SHARING_VIOLATION );
177 break;
178 case EBADF:
179 SetLastError( ERROR_INVALID_HANDLE );
180 break;
181 case ENOSPC:
182 SetLastError( ERROR_HANDLE_DISK_FULL );
183 break;
184 case EACCES:
185 case EPERM:
186 case EROFS:
187 SetLastError( ERROR_ACCESS_DENIED );
188 break;
189 case EBUSY:
190 SetLastError( ERROR_LOCK_VIOLATION );
191 break;
192 case ENOENT:
193 SetLastError( ERROR_FILE_NOT_FOUND );
194 break;
195 case EISDIR:
196 SetLastError( ERROR_CANNOT_MAKE );
197 break;
198 case ENFILE:
199 case EMFILE:
200 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
201 break;
202 case EEXIST:
203 SetLastError( ERROR_FILE_EXISTS );
204 break;
205 case EINVAL:
206 case ESPIPE:
207 SetLastError( ERROR_SEEK );
208 break;
209 case ENOTEMPTY:
210 SetLastError( ERROR_DIR_NOT_EMPTY );
211 break;
212 case ENOEXEC:
213 SetLastError( ERROR_BAD_FORMAT );
214 break;
215 case ENOTDIR:
216 SetLastError( ERROR_PATH_NOT_FOUND );
217 break;
218 case EXDEV:
219 SetLastError( ERROR_NOT_SAME_DEVICE );
220 break;
221 default:
222 WARN("unknown file error: %s\n", strerror(save_errno) );
223 SetLastError( ERROR_GEN_FAILURE );
224 break;
226 errno = save_errno;
230 /***********************************************************************
231 * FILE_name_AtoW
233 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
235 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
236 * there is no possibility for the function to do that twice, taking into
237 * account any called function.
239 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
241 ANSI_STRING str;
242 UNICODE_STRING strW, *pstrW;
243 NTSTATUS status;
245 RtlInitAnsiString( &str, name );
246 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
247 if (oem_file_apis)
248 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
249 else
250 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
251 if (status == STATUS_SUCCESS) return pstrW->Buffer;
253 if (status == STATUS_BUFFER_OVERFLOW)
254 SetLastError( ERROR_FILENAME_EXCED_RANGE );
255 else
256 SetLastError( RtlNtStatusToDosError(status) );
257 return NULL;
261 /***********************************************************************
262 * FILE_name_WtoA
264 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
266 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
268 DWORD ret;
270 if (srclen < 0) srclen = strlenW( src ) + 1;
271 if (oem_file_apis)
272 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
273 else
274 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
275 return ret;
279 /**************************************************************************
280 * SetFileApisToOEM (KERNEL32.@)
282 VOID WINAPI SetFileApisToOEM(void)
284 oem_file_apis = TRUE;
288 /**************************************************************************
289 * SetFileApisToANSI (KERNEL32.@)
291 VOID WINAPI SetFileApisToANSI(void)
293 oem_file_apis = FALSE;
297 /******************************************************************************
298 * AreFileApisANSI (KERNEL32.@)
300 * Determines if file functions are using ANSI
302 * RETURNS
303 * TRUE: Set of file functions is using ANSI code page
304 * FALSE: Set of file functions is using OEM code page
306 BOOL WINAPI AreFileApisANSI(void)
308 return !oem_file_apis;
312 /**************************************************************************
313 * Operations on file handles *
314 **************************************************************************/
316 /******************************************************************
317 * FILE_ReadWriteApc (internal)
319 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG reserved)
321 LPOVERLAPPED_COMPLETION_ROUTINE cr = apc_user;
323 cr(RtlNtStatusToDosError(io_status->u.Status), io_status->Information, (LPOVERLAPPED)io_status);
327 /***********************************************************************
328 * ReadFileEx (KERNEL32.@)
330 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
331 LPOVERLAPPED overlapped,
332 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
334 LARGE_INTEGER offset;
335 NTSTATUS status;
336 PIO_STATUS_BLOCK io_status;
338 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
340 if (!overlapped)
342 SetLastError(ERROR_INVALID_PARAMETER);
343 return FALSE;
346 offset.u.LowPart = overlapped->u.s.Offset;
347 offset.u.HighPart = overlapped->u.s.OffsetHigh;
348 io_status = (PIO_STATUS_BLOCK)overlapped;
349 io_status->u.Status = STATUS_PENDING;
350 io_status->Information = 0;
352 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
353 io_status, buffer, bytesToRead, &offset, NULL);
355 if (status && status != STATUS_PENDING)
357 SetLastError( RtlNtStatusToDosError(status) );
358 return FALSE;
360 return TRUE;
364 /***********************************************************************
365 * ReadFileScatter (KERNEL32.@)
367 BOOL WINAPI ReadFileScatter( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
368 LPDWORD reserved, LPOVERLAPPED overlapped )
370 PIO_STATUS_BLOCK io_status;
371 LARGE_INTEGER offset;
372 NTSTATUS status;
374 TRACE( "(%p %p %u %p)\n", file, segments, count, overlapped );
376 offset.u.LowPart = overlapped->u.s.Offset;
377 offset.u.HighPart = overlapped->u.s.OffsetHigh;
378 io_status = (PIO_STATUS_BLOCK)overlapped;
379 io_status->u.Status = STATUS_PENDING;
380 io_status->Information = 0;
382 status = NtReadFileScatter( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
383 if (status) SetLastError( RtlNtStatusToDosError(status) );
384 return !status;
388 /***********************************************************************
389 * ReadFile (KERNEL32.@)
391 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
392 LPDWORD bytesRead, LPOVERLAPPED overlapped )
394 LARGE_INTEGER offset;
395 PLARGE_INTEGER poffset = NULL;
396 IO_STATUS_BLOCK iosb;
397 PIO_STATUS_BLOCK io_status = &iosb;
398 HANDLE hEvent = 0;
399 NTSTATUS status;
400 LPVOID cvalue = NULL;
402 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToRead,
403 bytesRead, overlapped );
405 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
406 if (!bytesToRead) return TRUE;
408 if (is_console_handle(hFile))
410 DWORD conread, mode;
411 if (!ReadConsoleA(hFile, buffer, bytesToRead, &conread, NULL) ||
412 !GetConsoleMode(hFile, &mode))
413 return FALSE;
414 /* ctrl-Z (26) means end of file on window (if at beginning of buffer)
415 * but Unix uses ctrl-D (4), and ctrl-Z is a bad idea on Unix :-/
416 * So map both ctrl-D ctrl-Z to EOF.
418 if ((mode & ENABLE_PROCESSED_INPUT) && conread > 0 &&
419 (((char*)buffer)[0] == 26 || ((char*)buffer)[0] == 4))
421 conread = 0;
423 if (bytesRead) *bytesRead = conread;
424 return TRUE;
427 if (overlapped != NULL)
429 offset.u.LowPart = overlapped->u.s.Offset;
430 offset.u.HighPart = overlapped->u.s.OffsetHigh;
431 poffset = &offset;
432 hEvent = overlapped->hEvent;
433 io_status = (PIO_STATUS_BLOCK)overlapped;
434 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
436 io_status->u.Status = STATUS_PENDING;
437 io_status->Information = 0;
439 status = NtReadFile(hFile, hEvent, NULL, cvalue, io_status, buffer, bytesToRead, poffset, NULL);
441 if (status == STATUS_PENDING && !overlapped)
443 WaitForSingleObject( hFile, INFINITE );
444 status = io_status->u.Status;
447 if (status != STATUS_PENDING && bytesRead)
448 *bytesRead = io_status->Information;
450 if (status && status != STATUS_END_OF_FILE && status != STATUS_TIMEOUT)
452 SetLastError( RtlNtStatusToDosError(status) );
453 return FALSE;
455 return TRUE;
459 /***********************************************************************
460 * WriteFileEx (KERNEL32.@)
462 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
463 LPOVERLAPPED overlapped,
464 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
466 LARGE_INTEGER offset;
467 NTSTATUS status;
468 PIO_STATUS_BLOCK io_status;
470 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
472 if (overlapped == NULL)
474 SetLastError(ERROR_INVALID_PARAMETER);
475 return FALSE;
477 offset.u.LowPart = overlapped->u.s.Offset;
478 offset.u.HighPart = overlapped->u.s.OffsetHigh;
480 io_status = (PIO_STATUS_BLOCK)overlapped;
481 io_status->u.Status = STATUS_PENDING;
482 io_status->Information = 0;
484 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
485 io_status, buffer, bytesToWrite, &offset, NULL);
487 if (status && status != STATUS_PENDING)
489 SetLastError( RtlNtStatusToDosError(status) );
490 return FALSE;
492 return TRUE;
496 /***********************************************************************
497 * WriteFileGather (KERNEL32.@)
499 BOOL WINAPI WriteFileGather( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
500 LPDWORD reserved, LPOVERLAPPED overlapped )
502 PIO_STATUS_BLOCK io_status;
503 LARGE_INTEGER offset;
504 NTSTATUS status;
506 TRACE( "%p %p %u %p\n", file, segments, count, overlapped );
508 offset.u.LowPart = overlapped->u.s.Offset;
509 offset.u.HighPart = overlapped->u.s.OffsetHigh;
510 io_status = (PIO_STATUS_BLOCK)overlapped;
511 io_status->u.Status = STATUS_PENDING;
512 io_status->Information = 0;
514 status = NtWriteFileGather( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
515 if (status) SetLastError( RtlNtStatusToDosError(status) );
516 return !status;
520 /***********************************************************************
521 * WriteFile (KERNEL32.@)
523 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
524 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
526 HANDLE hEvent = NULL;
527 LARGE_INTEGER offset;
528 PLARGE_INTEGER poffset = NULL;
529 NTSTATUS status;
530 IO_STATUS_BLOCK iosb;
531 PIO_STATUS_BLOCK piosb = &iosb;
532 LPVOID cvalue = NULL;
534 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
536 if (is_console_handle(hFile))
537 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
539 if (overlapped)
541 offset.u.LowPart = overlapped->u.s.Offset;
542 offset.u.HighPart = overlapped->u.s.OffsetHigh;
543 poffset = &offset;
544 hEvent = overlapped->hEvent;
545 piosb = (PIO_STATUS_BLOCK)overlapped;
546 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
548 piosb->u.Status = STATUS_PENDING;
549 piosb->Information = 0;
551 status = NtWriteFile(hFile, hEvent, NULL, cvalue, piosb,
552 buffer, bytesToWrite, poffset, NULL);
554 if (status == STATUS_PENDING && !overlapped)
556 WaitForSingleObject( hFile, INFINITE );
557 status = piosb->u.Status;
560 if (status != STATUS_PENDING && bytesWritten)
561 *bytesWritten = piosb->Information;
563 if (status && status != STATUS_TIMEOUT)
565 SetLastError( RtlNtStatusToDosError(status) );
566 return FALSE;
568 return TRUE;
572 /***********************************************************************
573 * GetOverlappedResult (KERNEL32.@)
575 * Check the result of an Asynchronous data transfer from a file.
577 * Parameters
578 * HANDLE hFile [in] handle of file to check on
579 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
580 * LPDWORD lpTransferred [in/out] number of bytes transferred
581 * BOOL bWait [in] wait for the transfer to complete ?
583 * RETURNS
584 * TRUE on success
585 * FALSE on failure
587 * If successful (and relevant) lpTransferred will hold the number of
588 * bytes transferred during the async operation.
590 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
591 LPDWORD lpTransferred, BOOL bWait)
593 NTSTATUS status;
595 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
597 status = lpOverlapped->Internal;
598 if (status == STATUS_PENDING)
600 if (!bWait)
602 SetLastError( ERROR_IO_INCOMPLETE );
603 return FALSE;
606 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : hFile,
607 INFINITE ) == WAIT_FAILED)
608 return FALSE;
609 status = lpOverlapped->Internal;
612 *lpTransferred = lpOverlapped->InternalHigh;
614 if (status) SetLastError( RtlNtStatusToDosError(status) );
615 return !status;
618 /***********************************************************************
619 * CancelIoEx (KERNEL32.@)
621 * Cancels pending I/O operations on a file given the overlapped used.
623 * PARAMS
624 * handle [I] File handle.
625 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
627 * RETURNS
628 * Success: TRUE.
629 * Failure: FALSE, check GetLastError().
631 BOOL WINAPI CancelIoEx(HANDLE handle, LPOVERLAPPED lpOverlapped)
633 IO_STATUS_BLOCK io_status;
635 NtCancelIoFileEx(handle, (PIO_STATUS_BLOCK) lpOverlapped, &io_status);
636 if (io_status.u.Status)
638 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
639 return FALSE;
641 return TRUE;
644 /***********************************************************************
645 * CancelIo (KERNEL32.@)
647 * Cancels pending I/O operations initiated by the current thread on a file.
649 * PARAMS
650 * handle [I] File handle.
652 * RETURNS
653 * Success: TRUE.
654 * Failure: FALSE, check GetLastError().
656 BOOL WINAPI CancelIo(HANDLE handle)
658 IO_STATUS_BLOCK io_status;
660 NtCancelIoFile(handle, &io_status);
661 if (io_status.u.Status)
663 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
664 return FALSE;
666 return TRUE;
669 /***********************************************************************
670 * _hread (KERNEL32.@)
672 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
674 return _lread( hFile, buffer, count );
678 /***********************************************************************
679 * _hwrite (KERNEL32.@)
681 * experimentation yields that _lwrite:
682 * o truncates the file at the current position with
683 * a 0 len write
684 * o returns 0 on a 0 length write
685 * o works with console handles
688 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
690 DWORD result;
692 TRACE("%d %p %d\n", handle, buffer, count );
694 if (!count)
696 /* Expand or truncate at current position */
697 if (!SetEndOfFile( LongToHandle(handle) )) return HFILE_ERROR;
698 return 0;
700 if (!WriteFile( LongToHandle(handle), buffer, count, &result, NULL ))
701 return HFILE_ERROR;
702 return result;
706 /***********************************************************************
707 * _lclose (KERNEL32.@)
709 HFILE WINAPI _lclose( HFILE hFile )
711 TRACE("handle %d\n", hFile );
712 return CloseHandle( LongToHandle(hFile) ) ? 0 : HFILE_ERROR;
716 /***********************************************************************
717 * _lcreat (KERNEL32.@)
719 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
721 HANDLE hfile;
723 /* Mask off all flags not explicitly allowed by the doc */
724 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
725 TRACE("%s %02x\n", path, attr );
726 hfile = CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
727 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
728 CREATE_ALWAYS, attr, 0 );
729 return HandleToLong(hfile);
733 /***********************************************************************
734 * _lopen (KERNEL32.@)
736 HFILE WINAPI _lopen( LPCSTR path, INT mode )
738 HANDLE hfile;
740 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
741 hfile = create_file_OF( path, mode & ~OF_CREATE );
742 return HandleToLong(hfile);
745 /***********************************************************************
746 * _lread (KERNEL32.@)
748 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
750 DWORD result;
751 if (!ReadFile( LongToHandle(handle), buffer, count, &result, NULL ))
752 return HFILE_ERROR;
753 return result;
757 /***********************************************************************
758 * _llseek (KERNEL32.@)
760 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
762 return SetFilePointer( LongToHandle(hFile), lOffset, NULL, nOrigin );
766 /***********************************************************************
767 * _lwrite (KERNEL32.@)
769 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
771 return (UINT)_hwrite( hFile, buffer, (LONG)count );
775 /***********************************************************************
776 * FlushFileBuffers (KERNEL32.@)
778 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
780 NTSTATUS nts;
781 IO_STATUS_BLOCK ioblk;
783 if (is_console_handle( hFile ))
785 /* this will fail (as expected) for an output handle */
786 return FlushConsoleInputBuffer( hFile );
788 nts = NtFlushBuffersFile( hFile, &ioblk );
789 if (nts != STATUS_SUCCESS)
791 SetLastError( RtlNtStatusToDosError( nts ) );
792 return FALSE;
795 return TRUE;
799 /***********************************************************************
800 * GetFileType (KERNEL32.@)
802 DWORD WINAPI GetFileType( HANDLE hFile )
804 FILE_FS_DEVICE_INFORMATION info;
805 IO_STATUS_BLOCK io;
806 NTSTATUS status;
808 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
810 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
811 if (status != STATUS_SUCCESS)
813 SetLastError( RtlNtStatusToDosError(status) );
814 return FILE_TYPE_UNKNOWN;
817 switch(info.DeviceType)
819 case FILE_DEVICE_NULL:
820 case FILE_DEVICE_SERIAL_PORT:
821 case FILE_DEVICE_PARALLEL_PORT:
822 case FILE_DEVICE_TAPE:
823 case FILE_DEVICE_UNKNOWN:
824 return FILE_TYPE_CHAR;
825 case FILE_DEVICE_NAMED_PIPE:
826 return FILE_TYPE_PIPE;
827 default:
828 return FILE_TYPE_DISK;
833 /***********************************************************************
834 * GetFileInformationByHandle (KERNEL32.@)
836 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
838 FILE_ALL_INFORMATION all_info;
839 IO_STATUS_BLOCK io;
840 NTSTATUS status;
842 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
843 if (status == STATUS_BUFFER_OVERFLOW) status = STATUS_SUCCESS;
844 if (status == STATUS_SUCCESS)
846 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
847 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
848 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
849 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
850 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
851 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
852 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
853 info->dwVolumeSerialNumber = 0; /* FIXME */
854 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
855 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
856 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
857 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
858 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
859 return TRUE;
861 SetLastError( RtlNtStatusToDosError(status) );
862 return FALSE;
866 /***********************************************************************
867 * GetFileInformationByHandleEx (KERNEL32.@)
869 BOOL WINAPI GetFileInformationByHandleEx( HANDLE handle, FILE_INFO_BY_HANDLE_CLASS class,
870 LPVOID info, DWORD size )
872 NTSTATUS status;
873 IO_STATUS_BLOCK io;
875 switch (class)
877 case FileBasicInfo:
878 case FileStandardInfo:
879 case FileNameInfo:
880 case FileRenameInfo:
881 case FileDispositionInfo:
882 case FileAllocationInfo:
883 case FileEndOfFileInfo:
884 case FileStreamInfo:
885 case FileCompressionInfo:
886 case FileAttributeTagInfo:
887 case FileIoPriorityHintInfo:
888 case FileRemoteProtocolInfo:
889 case FileFullDirectoryInfo:
890 case FileFullDirectoryRestartInfo:
891 case FileStorageInfo:
892 case FileAlignmentInfo:
893 case FileIdInfo:
894 case FileIdExtdDirectoryInfo:
895 case FileIdExtdDirectoryRestartInfo:
896 FIXME( "%p, %u, %p, %u\n", handle, class, info, size );
897 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
898 return FALSE;
900 case FileIdBothDirectoryRestartInfo:
901 case FileIdBothDirectoryInfo:
902 status = NtQueryDirectoryFile( handle, NULL, NULL, NULL, &io, info, size,
903 FileIdBothDirectoryInformation, FALSE, NULL,
904 (class == FileIdBothDirectoryRestartInfo) );
905 if (status != STATUS_SUCCESS)
907 SetLastError( RtlNtStatusToDosError( status ) );
908 return FALSE;
910 return TRUE;
912 default:
913 SetLastError( ERROR_INVALID_PARAMETER );
914 return FALSE;
919 /***********************************************************************
920 * GetFileSize (KERNEL32.@)
922 * Retrieve the size of a file.
924 * PARAMS
925 * hFile [I] File to retrieve size of.
926 * filesizehigh [O] On return, the high bits of the file size.
928 * RETURNS
929 * Success: The low bits of the file size.
930 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
931 * check GetLastError() for values other than ERROR_SUCCESS.
933 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
935 LARGE_INTEGER size;
936 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
937 if (filesizehigh) *filesizehigh = size.u.HighPart;
938 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
939 return size.u.LowPart;
943 /***********************************************************************
944 * GetFileSizeEx (KERNEL32.@)
946 * Retrieve the size of a file.
948 * PARAMS
949 * hFile [I] File to retrieve size of.
950 * lpFileSIze [O] On return, the size of the file.
952 * RETURNS
953 * Success: TRUE.
954 * Failure: FALSE, check GetLastError().
956 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
958 FILE_STANDARD_INFORMATION info;
959 IO_STATUS_BLOCK io;
960 NTSTATUS status;
962 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileStandardInformation );
963 if (status == STATUS_SUCCESS)
965 *lpFileSize = info.EndOfFile;
966 return TRUE;
968 SetLastError( RtlNtStatusToDosError(status) );
969 return FALSE;
973 /**************************************************************************
974 * SetEndOfFile (KERNEL32.@)
976 * Sets the current position as the end of the file.
978 * PARAMS
979 * hFile [I] File handle.
981 * RETURNS
982 * Success: TRUE.
983 * Failure: FALSE, check GetLastError().
985 BOOL WINAPI SetEndOfFile( HANDLE hFile )
987 FILE_POSITION_INFORMATION pos;
988 FILE_END_OF_FILE_INFORMATION eof;
989 IO_STATUS_BLOCK io;
990 NTSTATUS status;
992 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
993 if (status == STATUS_SUCCESS)
995 eof.EndOfFile = pos.CurrentByteOffset;
996 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
998 if (status == STATUS_SUCCESS) return TRUE;
999 SetLastError( RtlNtStatusToDosError(status) );
1000 return FALSE;
1004 /***********************************************************************
1005 * SetFilePointer (KERNEL32.@)
1007 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
1009 LARGE_INTEGER dist, newpos;
1011 if (highword)
1013 dist.u.LowPart = distance;
1014 dist.u.HighPart = *highword;
1016 else dist.QuadPart = distance;
1018 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
1020 if (highword) *highword = newpos.u.HighPart;
1021 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
1022 return newpos.u.LowPart;
1026 /***********************************************************************
1027 * SetFilePointerEx (KERNEL32.@)
1029 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
1030 LARGE_INTEGER *newpos, DWORD method )
1032 LONGLONG pos;
1033 IO_STATUS_BLOCK io;
1034 FILE_POSITION_INFORMATION info;
1036 switch(method)
1038 case FILE_BEGIN:
1039 pos = distance.QuadPart;
1040 break;
1041 case FILE_CURRENT:
1042 if (NtQueryInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1043 goto error;
1044 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
1045 break;
1046 case FILE_END:
1048 FILE_END_OF_FILE_INFORMATION eof;
1049 if (NtQueryInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
1050 goto error;
1051 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
1053 break;
1054 default:
1055 SetLastError( ERROR_INVALID_PARAMETER );
1056 return FALSE;
1059 if (pos < 0)
1061 SetLastError( ERROR_NEGATIVE_SEEK );
1062 return FALSE;
1065 info.CurrentByteOffset.QuadPart = pos;
1066 if (NtSetInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1067 goto error;
1068 if (newpos) newpos->QuadPart = pos;
1069 return TRUE;
1071 error:
1072 SetLastError( RtlNtStatusToDosError(io.u.Status) );
1073 return FALSE;
1076 /***********************************************************************
1077 * SetFileValidData (KERNEL32.@)
1079 BOOL WINAPI SetFileValidData( HANDLE hFile, LONGLONG ValidDataLength )
1081 FIXME("stub: %p, %s\n", hFile, wine_dbgstr_longlong(ValidDataLength));
1082 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1083 return FALSE;
1086 /***********************************************************************
1087 * GetFileTime (KERNEL32.@)
1089 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
1090 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1092 FILE_BASIC_INFORMATION info;
1093 IO_STATUS_BLOCK io;
1094 NTSTATUS status;
1096 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1097 if (status == STATUS_SUCCESS)
1099 if (lpCreationTime)
1101 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
1102 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
1104 if (lpLastAccessTime)
1106 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
1107 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
1109 if (lpLastWriteTime)
1111 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
1112 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1114 return TRUE;
1116 SetLastError( RtlNtStatusToDosError(status) );
1117 return FALSE;
1121 /***********************************************************************
1122 * SetFileTime (KERNEL32.@)
1124 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1125 const FILETIME *atime, const FILETIME *mtime )
1127 FILE_BASIC_INFORMATION info;
1128 IO_STATUS_BLOCK io;
1129 NTSTATUS status;
1131 memset( &info, 0, sizeof(info) );
1132 if (ctime)
1134 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1135 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1137 if (atime)
1139 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1140 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1142 if (mtime)
1144 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1145 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1148 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1149 if (status == STATUS_SUCCESS) return TRUE;
1150 SetLastError( RtlNtStatusToDosError(status) );
1151 return FALSE;
1155 /**************************************************************************
1156 * LockFile (KERNEL32.@)
1158 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1159 DWORD count_low, DWORD count_high )
1161 NTSTATUS status;
1162 LARGE_INTEGER count, offset;
1164 TRACE( "%p %x%08x %x%08x\n",
1165 hFile, offset_high, offset_low, count_high, count_low );
1167 count.u.LowPart = count_low;
1168 count.u.HighPart = count_high;
1169 offset.u.LowPart = offset_low;
1170 offset.u.HighPart = offset_high;
1172 status = NtLockFile( hFile, 0, NULL, NULL,
1173 NULL, &offset, &count, NULL, TRUE, TRUE );
1175 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1176 return !status;
1180 /**************************************************************************
1181 * LockFileEx [KERNEL32.@]
1183 * Locks a byte range within an open file for shared or exclusive access.
1185 * RETURNS
1186 * success: TRUE
1187 * failure: FALSE
1189 * NOTES
1190 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1192 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1193 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1195 NTSTATUS status;
1196 LARGE_INTEGER count, offset;
1197 LPVOID cvalue = NULL;
1199 if (reserved)
1201 SetLastError( ERROR_INVALID_PARAMETER );
1202 return FALSE;
1205 TRACE( "%p %x%08x %x%08x flags %x\n",
1206 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1207 count_high, count_low, flags );
1209 count.u.LowPart = count_low;
1210 count.u.HighPart = count_high;
1211 offset.u.LowPart = overlapped->u.s.Offset;
1212 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1214 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1216 status = NtLockFile( hFile, overlapped->hEvent, NULL, cvalue,
1217 NULL, &offset, &count, NULL,
1218 flags & LOCKFILE_FAIL_IMMEDIATELY,
1219 flags & LOCKFILE_EXCLUSIVE_LOCK );
1221 if (status) SetLastError( RtlNtStatusToDosError(status) );
1222 return !status;
1226 /**************************************************************************
1227 * UnlockFile (KERNEL32.@)
1229 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1230 DWORD count_low, DWORD count_high )
1232 NTSTATUS status;
1233 LARGE_INTEGER count, offset;
1235 count.u.LowPart = count_low;
1236 count.u.HighPart = count_high;
1237 offset.u.LowPart = offset_low;
1238 offset.u.HighPart = offset_high;
1240 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1241 if (status) SetLastError( RtlNtStatusToDosError(status) );
1242 return !status;
1246 /**************************************************************************
1247 * UnlockFileEx (KERNEL32.@)
1249 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1250 LPOVERLAPPED overlapped )
1252 if (reserved)
1254 SetLastError( ERROR_INVALID_PARAMETER );
1255 return FALSE;
1257 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1259 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1263 /*************************************************************************
1264 * SetHandleCount (KERNEL32.@)
1266 UINT WINAPI SetHandleCount( UINT count )
1268 return count;
1272 /**************************************************************************
1273 * Operations on file names *
1274 **************************************************************************/
1277 /*************************************************************************
1278 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1280 * Creates or opens an object, and returns a handle that can be used to
1281 * access that object.
1283 * PARAMS
1285 * filename [in] pointer to filename to be accessed
1286 * access [in] access mode requested
1287 * sharing [in] share mode
1288 * sa [in] pointer to security attributes
1289 * creation [in] how to create the file
1290 * attributes [in] attributes for newly created file
1291 * template [in] handle to file with extended attributes to copy
1293 * RETURNS
1294 * Success: Open handle to specified file
1295 * Failure: INVALID_HANDLE_VALUE
1297 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1298 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1299 DWORD attributes, HANDLE template )
1301 NTSTATUS status;
1302 UINT options;
1303 OBJECT_ATTRIBUTES attr;
1304 UNICODE_STRING nameW;
1305 IO_STATUS_BLOCK io;
1306 HANDLE ret;
1307 DWORD dosdev;
1308 const WCHAR *vxd_name = NULL;
1309 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1310 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1311 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1312 SECURITY_QUALITY_OF_SERVICE qos;
1314 static const UINT nt_disposition[5] =
1316 FILE_CREATE, /* CREATE_NEW */
1317 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1318 FILE_OPEN, /* OPEN_EXISTING */
1319 FILE_OPEN_IF, /* OPEN_ALWAYS */
1320 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1324 /* sanity checks */
1326 if (!filename || !filename[0])
1328 SetLastError( ERROR_PATH_NOT_FOUND );
1329 return INVALID_HANDLE_VALUE;
1332 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1333 (access & GENERIC_READ)?"GENERIC_READ ":"",
1334 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1335 (access & GENERIC_EXECUTE)?"GENERIC_EXECUTE ":"",
1336 (!access)?"QUERY_ACCESS ":"",
1337 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1338 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1339 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1340 creation, attributes);
1342 /* Open a console for CONIN$ or CONOUT$ */
1344 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1346 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle),
1347 creation ? OPEN_EXISTING : 0);
1348 if (ret == INVALID_HANDLE_VALUE) SetLastError(ERROR_INVALID_PARAMETER);
1349 goto done;
1352 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1354 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1355 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1357 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1358 !strncmpiW( filename + 4, pipeW, 5 ) ||
1359 !strncmpiW( filename + 4, mailslotW, 9 ))
1361 dosdev = 0;
1363 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1365 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1367 else if (GetVersion() & 0x80000000)
1369 vxd_name = filename + 4;
1370 if (!creation) creation = OPEN_EXISTING;
1373 else dosdev = RtlIsDosDeviceName_U( filename );
1375 if (dosdev)
1377 static const WCHAR conW[] = {'C','O','N'};
1379 if (LOWORD(dosdev) == sizeof(conW) &&
1380 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1382 switch (access & (GENERIC_READ|GENERIC_WRITE))
1384 case GENERIC_READ:
1385 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1386 goto done;
1387 case GENERIC_WRITE:
1388 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1389 goto done;
1390 default:
1391 SetLastError( ERROR_FILE_NOT_FOUND );
1392 return INVALID_HANDLE_VALUE;
1397 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1399 SetLastError( ERROR_INVALID_PARAMETER );
1400 return INVALID_HANDLE_VALUE;
1403 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1405 SetLastError( ERROR_PATH_NOT_FOUND );
1406 return INVALID_HANDLE_VALUE;
1409 /* now call NtCreateFile */
1411 options = 0;
1412 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1413 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1414 else
1415 options |= FILE_NON_DIRECTORY_FILE;
1416 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1418 options |= FILE_DELETE_ON_CLOSE;
1419 access |= DELETE;
1421 if (attributes & FILE_FLAG_NO_BUFFERING)
1422 options |= FILE_NO_INTERMEDIATE_BUFFERING;
1423 if (!(attributes & FILE_FLAG_OVERLAPPED))
1424 options |= FILE_SYNCHRONOUS_IO_NONALERT;
1425 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1426 options |= FILE_RANDOM_ACCESS;
1427 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1429 attr.Length = sizeof(attr);
1430 attr.RootDirectory = 0;
1431 attr.Attributes = OBJ_CASE_INSENSITIVE;
1432 attr.ObjectName = &nameW;
1433 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1434 if (attributes & SECURITY_SQOS_PRESENT)
1436 qos.Length = sizeof(qos);
1437 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
1438 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
1439 qos.EffectiveOnly = (attributes & SECURITY_EFFECTIVE_ONLY) != 0;
1440 attr.SecurityQualityOfService = &qos;
1442 else
1443 attr.SecurityQualityOfService = NULL;
1445 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1447 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1448 sharing, nt_disposition[creation - CREATE_NEW],
1449 options, NULL, 0 );
1450 if (status)
1452 if (vxd_name && vxd_name[0])
1454 static HANDLE (*vxd_open)(LPCWSTR,DWORD,SECURITY_ATTRIBUTES*);
1455 if (!vxd_open) vxd_open = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1456 "__wine_vxd_open" );
1457 if (vxd_open && (ret = vxd_open( vxd_name, access, sa ))) goto done;
1460 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1461 ret = INVALID_HANDLE_VALUE;
1463 /* In the case file creation was rejected due to CREATE_NEW flag
1464 * was specified and file with that name already exists, correct
1465 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1466 * Note: RtlNtStatusToDosError is not the subject to blame here.
1468 if (status == STATUS_OBJECT_NAME_COLLISION)
1469 SetLastError( ERROR_FILE_EXISTS );
1470 else
1471 SetLastError( RtlNtStatusToDosError(status) );
1473 else
1475 if ((creation == CREATE_ALWAYS && io.Information == FILE_OVERWRITTEN) ||
1476 (creation == OPEN_ALWAYS && io.Information == FILE_OPENED))
1477 SetLastError( ERROR_ALREADY_EXISTS );
1478 else
1479 SetLastError( 0 );
1481 RtlFreeUnicodeString( &nameW );
1483 done:
1484 if (!ret) ret = INVALID_HANDLE_VALUE;
1485 TRACE("returning %p\n", ret);
1486 return ret;
1491 /*************************************************************************
1492 * CreateFileA (KERNEL32.@)
1494 * See CreateFileW.
1496 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1497 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1498 DWORD attributes, HANDLE template)
1500 WCHAR *nameW;
1502 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1503 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1507 /***********************************************************************
1508 * DeleteFileW (KERNEL32.@)
1510 * Delete a file.
1512 * PARAMS
1513 * path [I] Path to the file to delete.
1515 * RETURNS
1516 * Success: TRUE.
1517 * Failure: FALSE, check GetLastError().
1519 BOOL WINAPI DeleteFileW( LPCWSTR path )
1521 UNICODE_STRING nameW;
1522 OBJECT_ATTRIBUTES attr;
1523 NTSTATUS status;
1524 HANDLE hFile;
1525 IO_STATUS_BLOCK io;
1527 TRACE("%s\n", debugstr_w(path) );
1529 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1531 SetLastError( ERROR_PATH_NOT_FOUND );
1532 return FALSE;
1535 attr.Length = sizeof(attr);
1536 attr.RootDirectory = 0;
1537 attr.Attributes = OBJ_CASE_INSENSITIVE;
1538 attr.ObjectName = &nameW;
1539 attr.SecurityDescriptor = NULL;
1540 attr.SecurityQualityOfService = NULL;
1542 status = NtCreateFile(&hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
1543 &attr, &io, NULL, 0,
1544 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1545 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0);
1546 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1548 RtlFreeUnicodeString( &nameW );
1549 if (status)
1551 SetLastError( RtlNtStatusToDosError(status) );
1552 return FALSE;
1554 return TRUE;
1558 /***********************************************************************
1559 * DeleteFileA (KERNEL32.@)
1561 * See DeleteFileW.
1563 BOOL WINAPI DeleteFileA( LPCSTR path )
1565 WCHAR *pathW;
1567 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1568 return DeleteFileW( pathW );
1572 /**************************************************************************
1573 * ReplaceFileW (KERNEL32.@)
1574 * ReplaceFile (KERNEL32.@)
1576 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName, LPCWSTR lpReplacementFileName,
1577 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1578 LPVOID lpExclude, LPVOID lpReserved)
1580 UNICODE_STRING nt_replaced_name, nt_replacement_name;
1581 ANSI_STRING unix_replaced_name, unix_replacement_name, unix_backup_name;
1582 HANDLE hReplaced = NULL, hReplacement = NULL, hBackup = NULL;
1583 DWORD error = ERROR_SUCCESS;
1584 UINT replaced_flags;
1585 BOOL ret = FALSE;
1586 NTSTATUS status;
1587 IO_STATUS_BLOCK io;
1588 OBJECT_ATTRIBUTES attr;
1590 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName),
1591 debugstr_w(lpReplacementFileName), debugstr_w(lpBackupFileName),
1592 dwReplaceFlags, lpExclude, lpReserved);
1594 if (dwReplaceFlags)
1595 FIXME("Ignoring flags %x\n", dwReplaceFlags);
1597 /* First two arguments are mandatory */
1598 if (!lpReplacedFileName || !lpReplacementFileName)
1600 SetLastError(ERROR_INVALID_PARAMETER);
1601 return FALSE;
1604 unix_replaced_name.Buffer = NULL;
1605 unix_replacement_name.Buffer = NULL;
1606 unix_backup_name.Buffer = NULL;
1608 attr.Length = sizeof(attr);
1609 attr.RootDirectory = 0;
1610 attr.Attributes = OBJ_CASE_INSENSITIVE;
1611 attr.ObjectName = NULL;
1612 attr.SecurityDescriptor = NULL;
1613 attr.SecurityQualityOfService = NULL;
1615 /* Open the "replaced" file for reading and writing */
1616 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName, &nt_replaced_name, NULL, NULL)))
1618 error = ERROR_PATH_NOT_FOUND;
1619 goto fail;
1621 replaced_flags = lpBackupFileName ? FILE_OPEN : FILE_OPEN_IF;
1622 attr.ObjectName = &nt_replaced_name;
1623 status = NtOpenFile(&hReplaced, GENERIC_READ|GENERIC_WRITE|DELETE|SYNCHRONIZE,
1624 &attr, &io,
1625 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1626 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1627 if (status == STATUS_SUCCESS)
1628 status = wine_nt_to_unix_file_name(&nt_replaced_name, &unix_replaced_name, replaced_flags, FALSE);
1629 RtlFreeUnicodeString(&nt_replaced_name);
1630 if (status != STATUS_SUCCESS)
1632 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1633 error = ERROR_FILE_NOT_FOUND;
1634 else
1635 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1636 goto fail;
1640 * Open the replacement file for reading, writing, and deleting
1641 * (writing and deleting are needed when finished)
1643 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName, &nt_replacement_name, NULL, NULL)))
1645 error = ERROR_PATH_NOT_FOUND;
1646 goto fail;
1648 attr.ObjectName = &nt_replacement_name;
1649 status = NtOpenFile(&hReplacement,
1650 GENERIC_READ|GENERIC_WRITE|DELETE|WRITE_DAC|SYNCHRONIZE,
1651 &attr, &io, 0,
1652 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1653 if (status == STATUS_SUCCESS)
1654 status = wine_nt_to_unix_file_name(&nt_replacement_name, &unix_replacement_name, FILE_OPEN, FALSE);
1655 RtlFreeUnicodeString(&nt_replacement_name);
1656 if (status != STATUS_SUCCESS)
1658 error = RtlNtStatusToDosError(status);
1659 goto fail;
1662 /* If the user wants a backup then that needs to be performed first */
1663 if (lpBackupFileName)
1665 UNICODE_STRING nt_backup_name;
1666 FILE_BASIC_INFORMATION replaced_info;
1668 /* Obtain the file attributes from the "replaced" file */
1669 status = NtQueryInformationFile(hReplaced, &io, &replaced_info,
1670 sizeof(replaced_info),
1671 FileBasicInformation);
1672 if (status != STATUS_SUCCESS)
1674 error = RtlNtStatusToDosError(status);
1675 goto fail;
1678 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName, &nt_backup_name, NULL, NULL)))
1680 error = ERROR_PATH_NOT_FOUND;
1681 goto fail;
1683 attr.ObjectName = &nt_backup_name;
1684 /* Open the backup with permissions to write over it */
1685 status = NtCreateFile(&hBackup, GENERIC_WRITE,
1686 &attr, &io, NULL, replaced_info.FileAttributes,
1687 FILE_SHARE_WRITE, FILE_OPEN_IF,
1688 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE,
1689 NULL, 0);
1690 if (status == STATUS_SUCCESS)
1691 status = wine_nt_to_unix_file_name(&nt_backup_name, &unix_backup_name, FILE_OPEN_IF, FALSE);
1692 RtlFreeUnicodeString(&nt_backup_name);
1693 if (status != STATUS_SUCCESS)
1695 error = RtlNtStatusToDosError(status);
1696 goto fail;
1699 /* If an existing backup exists then copy over it */
1700 if (rename(unix_replaced_name.Buffer, unix_backup_name.Buffer) == -1)
1702 error = ERROR_UNABLE_TO_REMOVE_REPLACED; /* is this correct? */
1703 goto fail;
1708 * Now that the backup has been performed (if requested), copy the replacement
1709 * into place
1711 if (rename(unix_replacement_name.Buffer, unix_replaced_name.Buffer) == -1)
1713 if (errno == EACCES)
1715 /* Inappropriate permissions on "replaced", rename will fail */
1716 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1717 goto fail;
1719 /* on failure we need to indicate whether a backup was made */
1720 if (!lpBackupFileName)
1721 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT;
1722 else
1723 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT_2;
1724 goto fail;
1726 /* Success! */
1727 ret = TRUE;
1729 /* Perform resource cleanup */
1730 fail:
1731 if (hBackup) CloseHandle(hBackup);
1732 if (hReplaced) CloseHandle(hReplaced);
1733 if (hReplacement) CloseHandle(hReplacement);
1734 RtlFreeAnsiString(&unix_backup_name);
1735 RtlFreeAnsiString(&unix_replacement_name);
1736 RtlFreeAnsiString(&unix_replaced_name);
1738 /* If there was an error, set the error code */
1739 if(!ret)
1740 SetLastError(error);
1741 return ret;
1745 /**************************************************************************
1746 * ReplaceFileA (KERNEL32.@)
1748 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1749 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1750 LPVOID lpExclude, LPVOID lpReserved)
1752 WCHAR *replacedW, *replacementW, *backupW = NULL;
1753 BOOL ret;
1755 /* This function only makes sense when the first two parameters are defined */
1756 if (!lpReplacedFileName || !(replacedW = FILE_name_AtoW( lpReplacedFileName, TRUE )))
1758 SetLastError(ERROR_INVALID_PARAMETER);
1759 return FALSE;
1761 if (!lpReplacementFileName || !(replacementW = FILE_name_AtoW( lpReplacementFileName, TRUE )))
1763 HeapFree( GetProcessHeap(), 0, replacedW );
1764 SetLastError(ERROR_INVALID_PARAMETER);
1765 return FALSE;
1767 /* The backup parameter, however, is optional */
1768 if (lpBackupFileName)
1770 if (!(backupW = FILE_name_AtoW( lpBackupFileName, TRUE )))
1772 HeapFree( GetProcessHeap(), 0, replacedW );
1773 HeapFree( GetProcessHeap(), 0, replacementW );
1774 SetLastError(ERROR_INVALID_PARAMETER);
1775 return FALSE;
1778 ret = ReplaceFileW( replacedW, replacementW, backupW, dwReplaceFlags, lpExclude, lpReserved );
1779 HeapFree( GetProcessHeap(), 0, replacedW );
1780 HeapFree( GetProcessHeap(), 0, replacementW );
1781 HeapFree( GetProcessHeap(), 0, backupW );
1782 return ret;
1786 /*************************************************************************
1787 * FindFirstFileExW (KERNEL32.@)
1789 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1790 * results as FindExSearchNameMatch
1792 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1793 LPVOID data, FINDEX_SEARCH_OPS search_op,
1794 LPVOID filter, DWORD flags)
1796 WCHAR *mask, *p;
1797 FIND_FIRST_INFO *info = NULL;
1798 UNICODE_STRING nt_name;
1799 OBJECT_ATTRIBUTES attr;
1800 IO_STATUS_BLOCK io;
1801 NTSTATUS status;
1802 DWORD device = 0;
1804 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1806 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1807 || flags != 0)
1809 FIXME("options not implemented 0x%08x 0x%08x\n", search_op, flags );
1810 return INVALID_HANDLE_VALUE;
1812 if (level != FindExInfoStandard)
1814 FIXME("info level %d not implemented\n", level );
1815 return INVALID_HANDLE_VALUE;
1818 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1820 SetLastError( ERROR_PATH_NOT_FOUND );
1821 return INVALID_HANDLE_VALUE;
1824 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1826 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1827 goto error;
1830 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1832 static const WCHAR dotW[] = {'.',0};
1833 WCHAR *dir = NULL;
1835 /* we still need to check that the directory can be opened */
1837 if (HIWORD(device))
1839 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1841 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1842 goto error;
1844 memcpy( dir, filename, HIWORD(device) );
1845 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1847 RtlFreeUnicodeString( &nt_name );
1848 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1850 HeapFree( GetProcessHeap(), 0, dir );
1851 SetLastError( ERROR_PATH_NOT_FOUND );
1852 goto error;
1854 HeapFree( GetProcessHeap(), 0, dir );
1855 RtlInitUnicodeString( &info->mask, NULL );
1857 else if (!mask || !*mask)
1859 SetLastError( ERROR_FILE_NOT_FOUND );
1860 goto error;
1862 else
1864 if (!RtlCreateUnicodeString( &info->mask, mask ))
1866 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1867 goto error;
1870 /* truncate dir name before mask */
1871 *mask = 0;
1872 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1875 /* check if path is the root of the drive */
1876 info->is_root = FALSE;
1877 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1878 if (p[0] && p[1] == ':')
1880 p += 2;
1881 while (*p == '\\') p++;
1882 info->is_root = (*p == 0);
1885 attr.Length = sizeof(attr);
1886 attr.RootDirectory = 0;
1887 attr.Attributes = OBJ_CASE_INSENSITIVE;
1888 attr.ObjectName = &nt_name;
1889 attr.SecurityDescriptor = NULL;
1890 attr.SecurityQualityOfService = NULL;
1892 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1893 FILE_SHARE_READ | FILE_SHARE_WRITE,
1894 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1896 if (status != STATUS_SUCCESS)
1898 RtlFreeUnicodeString( &info->mask );
1899 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1900 SetLastError( ERROR_PATH_NOT_FOUND );
1901 else
1902 SetLastError( RtlNtStatusToDosError(status) );
1903 goto error;
1906 RtlInitializeCriticalSection( &info->cs );
1907 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
1908 info->path = nt_name;
1909 info->magic = FIND_FIRST_MAGIC;
1910 info->data_pos = 0;
1911 info->data_len = 0;
1912 info->search_op = search_op;
1914 if (device)
1916 WIN32_FIND_DATAW *wfd = data;
1918 memset( wfd, 0, sizeof(*wfd) );
1919 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1920 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1921 CloseHandle( info->handle );
1922 info->handle = 0;
1924 else
1926 IO_STATUS_BLOCK io;
1928 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1929 FileBothDirectoryInformation, FALSE, &info->mask, TRUE );
1930 if (io.u.Status)
1932 FindClose( info );
1933 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1934 return INVALID_HANDLE_VALUE;
1936 info->data_len = io.Information;
1937 if (!FindNextFileW( info, data ))
1939 TRACE( "%s not found\n", debugstr_w(filename) );
1940 FindClose( info );
1941 SetLastError( ERROR_FILE_NOT_FOUND );
1942 return INVALID_HANDLE_VALUE;
1944 if (!strpbrkW( info->mask.Buffer, wildcardsW ))
1946 /* we can't find two files with the same name */
1947 CloseHandle( info->handle );
1948 info->handle = 0;
1951 return info;
1953 error:
1954 HeapFree( GetProcessHeap(), 0, info );
1955 RtlFreeUnicodeString( &nt_name );
1956 return INVALID_HANDLE_VALUE;
1960 /*************************************************************************
1961 * FindNextFileW (KERNEL32.@)
1963 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1965 FIND_FIRST_INFO *info;
1966 FILE_BOTH_DIR_INFORMATION *dir_info;
1967 BOOL ret = FALSE;
1969 TRACE("%p %p\n", handle, data);
1971 if (!handle || handle == INVALID_HANDLE_VALUE)
1973 SetLastError( ERROR_INVALID_HANDLE );
1974 return ret;
1976 info = handle;
1977 if (info->magic != FIND_FIRST_MAGIC)
1979 SetLastError( ERROR_INVALID_HANDLE );
1980 return ret;
1983 RtlEnterCriticalSection( &info->cs );
1985 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
1986 else for (;;)
1988 if (info->data_pos >= info->data_len) /* need to read some more data */
1990 IO_STATUS_BLOCK io;
1992 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1993 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
1994 if (io.u.Status)
1996 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1997 if (io.u.Status == STATUS_NO_MORE_FILES)
1999 CloseHandle( info->handle );
2000 info->handle = 0;
2002 break;
2004 info->data_len = io.Information;
2005 info->data_pos = 0;
2008 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
2010 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
2011 else info->data_pos = info->data_len;
2013 /* don't return '.' and '..' in the root of the drive */
2014 if (info->is_root)
2016 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
2017 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
2018 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
2021 /* check for dir symlink */
2022 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
2023 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2024 strpbrkW( info->mask.Buffer, wildcardsW ))
2026 if (!check_dir_symlink( info, dir_info )) continue;
2029 data->dwFileAttributes = dir_info->FileAttributes;
2030 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
2031 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
2032 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
2033 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
2034 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
2035 data->dwReserved0 = 0;
2036 data->dwReserved1 = 0;
2038 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
2039 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
2040 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
2041 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
2043 TRACE("returning %s (%s)\n",
2044 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
2046 ret = TRUE;
2047 break;
2050 RtlLeaveCriticalSection( &info->cs );
2051 return ret;
2055 /*************************************************************************
2056 * FindClose (KERNEL32.@)
2058 BOOL WINAPI FindClose( HANDLE handle )
2060 FIND_FIRST_INFO *info = handle;
2062 if (!handle || handle == INVALID_HANDLE_VALUE)
2064 SetLastError( ERROR_INVALID_HANDLE );
2065 return FALSE;
2068 __TRY
2070 if (info->magic == FIND_FIRST_MAGIC)
2072 RtlEnterCriticalSection( &info->cs );
2073 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
2075 info->magic = 0;
2076 if (info->handle) CloseHandle( info->handle );
2077 info->handle = 0;
2078 RtlFreeUnicodeString( &info->mask );
2079 info->mask.Buffer = NULL;
2080 RtlFreeUnicodeString( &info->path );
2081 info->data_pos = 0;
2082 info->data_len = 0;
2083 RtlLeaveCriticalSection( &info->cs );
2084 info->cs.DebugInfo->Spare[0] = 0;
2085 RtlDeleteCriticalSection( &info->cs );
2086 HeapFree( GetProcessHeap(), 0, info );
2090 __EXCEPT_PAGE_FAULT
2092 WARN("Illegal handle %p\n", handle);
2093 SetLastError( ERROR_INVALID_HANDLE );
2094 return FALSE;
2096 __ENDTRY
2098 return TRUE;
2102 /*************************************************************************
2103 * FindFirstFileA (KERNEL32.@)
2105 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
2107 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
2108 FindExSearchNameMatch, NULL, 0);
2111 /*************************************************************************
2112 * FindFirstFileExA (KERNEL32.@)
2114 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
2115 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
2116 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
2118 HANDLE handle;
2119 WIN32_FIND_DATAA *dataA;
2120 WIN32_FIND_DATAW dataW;
2121 WCHAR *nameW;
2123 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
2125 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
2126 if (handle == INVALID_HANDLE_VALUE) return handle;
2128 dataA = lpFindFileData;
2129 dataA->dwFileAttributes = dataW.dwFileAttributes;
2130 dataA->ftCreationTime = dataW.ftCreationTime;
2131 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
2132 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
2133 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
2134 dataA->nFileSizeLow = dataW.nFileSizeLow;
2135 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
2136 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
2137 sizeof(dataA->cAlternateFileName) );
2138 return handle;
2142 /*************************************************************************
2143 * FindFirstFileW (KERNEL32.@)
2145 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
2147 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
2148 FindExSearchNameMatch, NULL, 0);
2152 /*************************************************************************
2153 * FindNextFileA (KERNEL32.@)
2155 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
2157 WIN32_FIND_DATAW dataW;
2159 if (!FindNextFileW( handle, &dataW )) return FALSE;
2160 data->dwFileAttributes = dataW.dwFileAttributes;
2161 data->ftCreationTime = dataW.ftCreationTime;
2162 data->ftLastAccessTime = dataW.ftLastAccessTime;
2163 data->ftLastWriteTime = dataW.ftLastWriteTime;
2164 data->nFileSizeHigh = dataW.nFileSizeHigh;
2165 data->nFileSizeLow = dataW.nFileSizeLow;
2166 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
2167 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
2168 sizeof(data->cAlternateFileName) );
2169 return TRUE;
2173 /**************************************************************************
2174 * GetFileAttributesW (KERNEL32.@)
2176 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
2178 FILE_BASIC_INFORMATION info;
2179 UNICODE_STRING nt_name;
2180 OBJECT_ATTRIBUTES attr;
2181 NTSTATUS status;
2183 TRACE("%s\n", debugstr_w(name));
2185 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2187 SetLastError( ERROR_PATH_NOT_FOUND );
2188 return INVALID_FILE_ATTRIBUTES;
2191 attr.Length = sizeof(attr);
2192 attr.RootDirectory = 0;
2193 attr.Attributes = OBJ_CASE_INSENSITIVE;
2194 attr.ObjectName = &nt_name;
2195 attr.SecurityDescriptor = NULL;
2196 attr.SecurityQualityOfService = NULL;
2198 status = NtQueryAttributesFile( &attr, &info );
2199 RtlFreeUnicodeString( &nt_name );
2201 if (status == STATUS_SUCCESS) return info.FileAttributes;
2203 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2204 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
2206 SetLastError( RtlNtStatusToDosError(status) );
2207 return INVALID_FILE_ATTRIBUTES;
2211 /**************************************************************************
2212 * GetFileAttributesA (KERNEL32.@)
2214 DWORD WINAPI GetFileAttributesA( LPCSTR name )
2216 WCHAR *nameW;
2218 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
2219 return GetFileAttributesW( nameW );
2223 /**************************************************************************
2224 * SetFileAttributesW (KERNEL32.@)
2226 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
2228 UNICODE_STRING nt_name;
2229 OBJECT_ATTRIBUTES attr;
2230 IO_STATUS_BLOCK io;
2231 NTSTATUS status;
2232 HANDLE handle;
2234 TRACE("%s %x\n", debugstr_w(name), attributes);
2236 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2238 SetLastError( ERROR_PATH_NOT_FOUND );
2239 return FALSE;
2242 attr.Length = sizeof(attr);
2243 attr.RootDirectory = 0;
2244 attr.Attributes = OBJ_CASE_INSENSITIVE;
2245 attr.ObjectName = &nt_name;
2246 attr.SecurityDescriptor = NULL;
2247 attr.SecurityQualityOfService = NULL;
2249 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2250 RtlFreeUnicodeString( &nt_name );
2252 if (status == STATUS_SUCCESS)
2254 FILE_BASIC_INFORMATION info;
2256 memset( &info, 0, sizeof(info) );
2257 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2258 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2259 NtClose( handle );
2262 if (status == STATUS_SUCCESS) return TRUE;
2263 SetLastError( RtlNtStatusToDosError(status) );
2264 return FALSE;
2268 /**************************************************************************
2269 * SetFileAttributesA (KERNEL32.@)
2271 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2273 WCHAR *nameW;
2275 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2276 return SetFileAttributesW( nameW, attributes );
2280 /**************************************************************************
2281 * GetFileAttributesExW (KERNEL32.@)
2283 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2285 FILE_NETWORK_OPEN_INFORMATION info;
2286 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2287 UNICODE_STRING nt_name;
2288 OBJECT_ATTRIBUTES attr;
2289 NTSTATUS status;
2291 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2293 if (level != GetFileExInfoStandard)
2295 SetLastError( ERROR_INVALID_PARAMETER );
2296 return FALSE;
2299 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2301 SetLastError( ERROR_PATH_NOT_FOUND );
2302 return FALSE;
2305 attr.Length = sizeof(attr);
2306 attr.RootDirectory = 0;
2307 attr.Attributes = OBJ_CASE_INSENSITIVE;
2308 attr.ObjectName = &nt_name;
2309 attr.SecurityDescriptor = NULL;
2310 attr.SecurityQualityOfService = NULL;
2312 status = NtQueryFullAttributesFile( &attr, &info );
2313 RtlFreeUnicodeString( &nt_name );
2315 if (status != STATUS_SUCCESS)
2317 SetLastError( RtlNtStatusToDosError(status) );
2318 return FALSE;
2321 data->dwFileAttributes = info.FileAttributes;
2322 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2323 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2324 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2325 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2326 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2327 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2328 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2329 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2330 return TRUE;
2334 /**************************************************************************
2335 * GetFileAttributesExA (KERNEL32.@)
2337 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2339 WCHAR *nameW;
2341 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2342 return GetFileAttributesExW( nameW, level, ptr );
2346 /******************************************************************************
2347 * GetCompressedFileSizeW (KERNEL32.@)
2349 * Get the actual number of bytes used on disk.
2351 * RETURNS
2352 * Success: Low-order doubleword of number of bytes
2353 * Failure: INVALID_FILE_SIZE
2355 DWORD WINAPI GetCompressedFileSizeW(
2356 LPCWSTR name, /* [in] Pointer to name of file */
2357 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2359 UNICODE_STRING nt_name;
2360 OBJECT_ATTRIBUTES attr;
2361 IO_STATUS_BLOCK io;
2362 NTSTATUS status;
2363 HANDLE handle;
2364 DWORD ret = INVALID_FILE_SIZE;
2366 TRACE("%s %p\n", debugstr_w(name), size_high);
2368 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2370 SetLastError( ERROR_PATH_NOT_FOUND );
2371 return INVALID_FILE_SIZE;
2374 attr.Length = sizeof(attr);
2375 attr.RootDirectory = 0;
2376 attr.Attributes = OBJ_CASE_INSENSITIVE;
2377 attr.ObjectName = &nt_name;
2378 attr.SecurityDescriptor = NULL;
2379 attr.SecurityQualityOfService = NULL;
2381 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2382 RtlFreeUnicodeString( &nt_name );
2384 if (status == STATUS_SUCCESS)
2386 /* we don't support compressed files, simply return the file size */
2387 ret = GetFileSize( handle, size_high );
2388 NtClose( handle );
2390 else SetLastError( RtlNtStatusToDosError(status) );
2392 return ret;
2396 /******************************************************************************
2397 * GetCompressedFileSizeA (KERNEL32.@)
2399 * See GetCompressedFileSizeW.
2401 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2403 WCHAR *nameW;
2405 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2406 return GetCompressedFileSizeW( nameW, size_high );
2410 /***********************************************************************
2411 * OpenVxDHandle (KERNEL32.@)
2413 * This function is supposed to return the corresponding Ring 0
2414 * ("kernel") handle for a Ring 3 handle in Win9x.
2415 * Evidently, Wine will have problems with this. But we try anyway,
2416 * maybe it helps...
2418 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
2420 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
2421 return hHandleRing3;
2425 /****************************************************************************
2426 * DeviceIoControl (KERNEL32.@)
2428 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
2429 LPVOID lpvInBuffer, DWORD cbInBuffer,
2430 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
2431 LPDWORD lpcbBytesReturned,
2432 LPOVERLAPPED lpOverlapped)
2434 NTSTATUS status;
2436 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2437 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
2438 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
2440 /* Check if this is a user defined control code for a VxD */
2442 if (HIWORD( dwIoControlCode ) == 0 && (GetVersion() & 0x80000000))
2444 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
2445 static DeviceIoProc (*vxd_get_proc)(HANDLE);
2446 DeviceIoProc proc = NULL;
2448 if (!vxd_get_proc) vxd_get_proc = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2449 "__wine_vxd_get_proc" );
2450 if (vxd_get_proc) proc = vxd_get_proc( hDevice );
2451 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
2452 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
2455 /* Not a VxD, let ntdll handle it */
2457 if (lpOverlapped)
2459 LPVOID cvalue = ((ULONG_PTR)lpOverlapped->hEvent & 1) ? NULL : lpOverlapped;
2460 lpOverlapped->Internal = STATUS_PENDING;
2461 lpOverlapped->InternalHigh = 0;
2462 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2463 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
2464 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2465 dwIoControlCode, lpvInBuffer, cbInBuffer,
2466 lpvOutBuffer, cbOutBuffer);
2467 else
2468 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
2469 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2470 dwIoControlCode, lpvInBuffer, cbInBuffer,
2471 lpvOutBuffer, cbOutBuffer);
2472 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
2474 else
2476 IO_STATUS_BLOCK iosb;
2478 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2479 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
2480 dwIoControlCode, lpvInBuffer, cbInBuffer,
2481 lpvOutBuffer, cbOutBuffer);
2482 else
2483 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
2484 dwIoControlCode, lpvInBuffer, cbInBuffer,
2485 lpvOutBuffer, cbOutBuffer);
2486 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
2488 if (status) SetLastError( RtlNtStatusToDosError(status) );
2489 return !status;
2493 /***********************************************************************
2494 * OpenFile (KERNEL32.@)
2496 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2498 HANDLE handle;
2499 FILETIME filetime;
2500 WORD filedatetime[2];
2502 if (!ofs) return HFILE_ERROR;
2504 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2505 ((mode & 0x3 )==OF_READ)?"OF_READ":
2506 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2507 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2508 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2509 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2510 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2511 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2512 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2513 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2514 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2515 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2516 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2517 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2518 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2519 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2520 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2521 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2525 ofs->cBytes = sizeof(OFSTRUCT);
2526 ofs->nErrCode = 0;
2527 if (mode & OF_REOPEN) name = ofs->szPathName;
2529 if (!name) return HFILE_ERROR;
2531 TRACE("%s %04x\n", name, mode );
2533 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2534 Are there any cases where getting the path here is wrong?
2535 Uwe Bonnes 1997 Apr 2 */
2536 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2538 /* OF_PARSE simply fills the structure */
2540 if (mode & OF_PARSE)
2542 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2543 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2544 return 0;
2547 /* OF_CREATE is completely different from all other options, so
2548 handle it first */
2550 if (mode & OF_CREATE)
2552 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2553 goto error;
2555 else
2557 /* Now look for the file */
2559 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2560 goto error;
2562 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2564 if (mode & OF_DELETE)
2566 if (!DeleteFileA( ofs->szPathName )) goto error;
2567 TRACE("(%s): OF_DELETE return = OK\n", name);
2568 return TRUE;
2571 handle = LongToHandle(_lopen( ofs->szPathName, mode ));
2572 if (handle == INVALID_HANDLE_VALUE) goto error;
2574 GetFileTime( handle, NULL, NULL, &filetime );
2575 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2576 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2578 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2580 CloseHandle( handle );
2581 WARN("(%s): OF_VERIFY failed\n", name );
2582 /* FIXME: what error here? */
2583 SetLastError( ERROR_FILE_NOT_FOUND );
2584 goto error;
2587 ofs->Reserved1 = filedatetime[0];
2588 ofs->Reserved2 = filedatetime[1];
2590 TRACE("(%s): OK, return = %p\n", name, handle );
2591 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2593 CloseHandle( handle );
2594 return TRUE;
2596 return HandleToLong(handle);
2598 error: /* We get here if there was an error opening the file */
2599 ofs->nErrCode = GetLastError();
2600 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2601 return HFILE_ERROR;
2605 /***********************************************************************
2606 * OpenFileById (KERNEL32.@)
2608 HANDLE WINAPI OpenFileById( HANDLE handle, LPFILE_ID_DESCRIPTOR id, DWORD access,
2609 DWORD share, LPSECURITY_ATTRIBUTES sec_attr, DWORD flags )
2611 UINT options;
2612 HANDLE result;
2613 OBJECT_ATTRIBUTES attr;
2614 NTSTATUS status;
2615 IO_STATUS_BLOCK io;
2616 UNICODE_STRING objectName;
2618 if (!id)
2620 SetLastError( ERROR_INVALID_PARAMETER );
2621 return INVALID_HANDLE_VALUE;
2624 options = FILE_OPEN_BY_FILE_ID;
2625 if (flags & FILE_FLAG_BACKUP_SEMANTICS)
2626 options |= FILE_OPEN_FOR_BACKUP_INTENT;
2627 else
2628 options |= FILE_NON_DIRECTORY_FILE;
2629 if (flags & FILE_FLAG_NO_BUFFERING) options |= FILE_NO_INTERMEDIATE_BUFFERING;
2630 if (!(flags & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
2631 if (flags & FILE_FLAG_RANDOM_ACCESS) options |= FILE_RANDOM_ACCESS;
2632 flags &= FILE_ATTRIBUTE_VALID_FLAGS;
2634 objectName.Length = sizeof(ULONGLONG);
2635 objectName.Buffer = (WCHAR *)&id->u.FileId;
2636 attr.Length = sizeof(attr);
2637 attr.RootDirectory = handle;
2638 attr.Attributes = 0;
2639 attr.ObjectName = &objectName;
2640 attr.SecurityDescriptor = sec_attr ? sec_attr->lpSecurityDescriptor : NULL;
2641 attr.SecurityQualityOfService = NULL;
2642 if (sec_attr && sec_attr->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
2644 status = NtCreateFile( &result, access, &attr, &io, NULL, flags,
2645 share, OPEN_EXISTING, options, NULL, 0 );
2646 if (status != STATUS_SUCCESS)
2648 SetLastError( RtlNtStatusToDosError( status ) );
2649 return INVALID_HANDLE_VALUE;
2651 return result;
2655 /***********************************************************************
2656 * K32EnumDeviceDrivers (KERNEL32.@)
2658 BOOL WINAPI K32EnumDeviceDrivers(void **image_base, DWORD cb, DWORD *needed)
2660 FIXME("(%p, %d, %p): stub\n", image_base, cb, needed);
2662 if (needed)
2663 *needed = 0;
2665 return TRUE;
2668 /***********************************************************************
2669 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2671 DWORD WINAPI K32GetDeviceDriverBaseNameA(void *image_base, LPSTR base_name, DWORD size)
2673 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2675 if (base_name && size)
2676 base_name[0] = '\0';
2678 return 0;
2681 /***********************************************************************
2682 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2684 DWORD WINAPI K32GetDeviceDriverBaseNameW(void *image_base, LPWSTR base_name, DWORD size)
2686 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2688 if (base_name && size)
2689 base_name[0] = '\0';
2691 return 0;
2694 /***********************************************************************
2695 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2697 DWORD WINAPI K32GetDeviceDriverFileNameA(void *image_base, LPSTR file_name, DWORD size)
2699 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2701 if (file_name && size)
2702 file_name[0] = '\0';
2704 return 0;
2707 /***********************************************************************
2708 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2710 DWORD WINAPI K32GetDeviceDriverFileNameW(void *image_base, LPWSTR file_name, DWORD size)
2712 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2714 if (file_name && size)
2715 file_name[0] = '\0';
2717 return 0;