d3d10core: Implement ID3D10Multithread.
[wine/multimedia.git] / dlls / kernel32 / file.c
blob2f5dee2855ebd98c6e3744ea3b30d2903eced7b0
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 "ddk/ntddk.h"
44 #include "kernel_private.h"
45 #include "fileapi.h"
47 #include "wine/exception.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(file);
53 /* info structure for FindFirstFile handle */
54 typedef struct
56 DWORD magic; /* magic number */
57 HANDLE handle; /* handle to directory */
58 CRITICAL_SECTION cs; /* crit section protecting this structure */
59 FINDEX_SEARCH_OPS search_op; /* Flags passed to FindFirst. */
60 UNICODE_STRING mask; /* file mask */
61 UNICODE_STRING path; /* NT path used to open the directory */
62 BOOL is_root; /* is directory the root of the drive? */
63 UINT data_pos; /* current position in dir data */
64 UINT data_len; /* length of dir data */
65 UINT data_size; /* size of data buffer, or 0 when everything has been read */
66 BYTE *data; /* directory data */
67 } FIND_FIRST_INFO;
69 #define FIND_FIRST_MAGIC 0xc0ffee11
71 static const UINT max_entry_size = offsetof( FILE_BOTH_DIRECTORY_INFORMATION, FileName[256] );
73 static BOOL oem_file_apis;
75 static const WCHAR wildcardsW[] = { '*','?',0 };
77 /***********************************************************************
78 * create_file_OF
80 * Wrapper for CreateFile that takes OF_* mode flags.
82 static HANDLE create_file_OF( LPCSTR path, INT mode )
84 DWORD access, sharing, creation;
86 if (mode & OF_CREATE)
88 creation = CREATE_ALWAYS;
89 access = GENERIC_READ | GENERIC_WRITE;
91 else
93 creation = OPEN_EXISTING;
94 switch(mode & 0x03)
96 case OF_READ: access = GENERIC_READ; break;
97 case OF_WRITE: access = GENERIC_WRITE; break;
98 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
99 default: access = 0; break;
103 switch(mode & 0x70)
105 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
106 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
107 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
108 case OF_SHARE_DENY_NONE:
109 case OF_SHARE_COMPAT:
110 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
112 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
116 /***********************************************************************
117 * check_dir_symlink
119 * Check if a dir symlink should be returned by FindNextFile.
121 static BOOL check_dir_symlink( FIND_FIRST_INFO *info, const FILE_BOTH_DIR_INFORMATION *file_info )
123 UNICODE_STRING str;
124 ANSI_STRING unix_name;
125 struct stat st, parent_st;
126 BOOL ret = TRUE;
127 DWORD len;
129 str.MaximumLength = info->path.Length + sizeof(WCHAR) + file_info->FileNameLength;
130 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength ))) return TRUE;
131 memcpy( str.Buffer, info->path.Buffer, info->path.Length );
132 len = info->path.Length / sizeof(WCHAR);
133 if (!len || str.Buffer[len-1] != '\\') str.Buffer[len++] = '\\';
134 memcpy( str.Buffer + len, file_info->FileName, file_info->FileNameLength );
135 str.Length = len * sizeof(WCHAR) + file_info->FileNameLength;
137 unix_name.Buffer = NULL;
138 if (!wine_nt_to_unix_file_name( &str, &unix_name, OPEN_EXISTING, FALSE ) &&
139 !stat( unix_name.Buffer, &st ))
141 char *p = unix_name.Buffer + unix_name.Length - 1;
143 /* skip trailing slashes */
144 while (p > unix_name.Buffer && *p == '/') p--;
146 while (ret && p > unix_name.Buffer)
148 while (p > unix_name.Buffer && *p != '/') p--;
149 while (p > unix_name.Buffer && *p == '/') p--;
150 p[1] = 0;
151 if (!stat( unix_name.Buffer, &parent_st ) &&
152 parent_st.st_dev == st.st_dev &&
153 parent_st.st_ino == st.st_ino)
155 WARN( "suppressing dir symlink %s pointing to parent %s\n",
156 debugstr_wn( str.Buffer, str.Length/sizeof(WCHAR) ),
157 debugstr_a( unix_name.Buffer ));
158 ret = FALSE;
162 RtlFreeAnsiString( &unix_name );
163 RtlFreeUnicodeString( &str );
164 return ret;
168 /***********************************************************************
169 * FILE_SetDosError
171 * Set the DOS error code from errno.
173 void FILE_SetDosError(void)
175 int save_errno = errno; /* errno gets overwritten by printf */
177 TRACE("errno = %d %s\n", errno, strerror(errno));
178 switch (save_errno)
180 case EAGAIN:
181 SetLastError( ERROR_SHARING_VIOLATION );
182 break;
183 case EBADF:
184 SetLastError( ERROR_INVALID_HANDLE );
185 break;
186 case ENOSPC:
187 SetLastError( ERROR_HANDLE_DISK_FULL );
188 break;
189 case EACCES:
190 case EPERM:
191 case EROFS:
192 SetLastError( ERROR_ACCESS_DENIED );
193 break;
194 case EBUSY:
195 SetLastError( ERROR_LOCK_VIOLATION );
196 break;
197 case ENOENT:
198 SetLastError( ERROR_FILE_NOT_FOUND );
199 break;
200 case EISDIR:
201 SetLastError( ERROR_CANNOT_MAKE );
202 break;
203 case ENFILE:
204 case EMFILE:
205 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
206 break;
207 case EEXIST:
208 SetLastError( ERROR_FILE_EXISTS );
209 break;
210 case EINVAL:
211 case ESPIPE:
212 SetLastError( ERROR_SEEK );
213 break;
214 case ENOTEMPTY:
215 SetLastError( ERROR_DIR_NOT_EMPTY );
216 break;
217 case ENOEXEC:
218 SetLastError( ERROR_BAD_FORMAT );
219 break;
220 case ENOTDIR:
221 SetLastError( ERROR_PATH_NOT_FOUND );
222 break;
223 case EXDEV:
224 SetLastError( ERROR_NOT_SAME_DEVICE );
225 break;
226 default:
227 WARN("unknown file error: %s\n", strerror(save_errno) );
228 SetLastError( ERROR_GEN_FAILURE );
229 break;
231 errno = save_errno;
235 /***********************************************************************
236 * FILE_name_AtoW
238 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
240 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
241 * there is no possibility for the function to do that twice, taking into
242 * account any called function.
244 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
246 ANSI_STRING str;
247 UNICODE_STRING strW, *pstrW;
248 NTSTATUS status;
250 RtlInitAnsiString( &str, name );
251 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
252 if (oem_file_apis)
253 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
254 else
255 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
256 if (status == STATUS_SUCCESS) return pstrW->Buffer;
258 if (status == STATUS_BUFFER_OVERFLOW)
259 SetLastError( ERROR_FILENAME_EXCED_RANGE );
260 else
261 SetLastError( RtlNtStatusToDosError(status) );
262 return NULL;
266 /***********************************************************************
267 * FILE_name_WtoA
269 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
271 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
273 DWORD ret;
275 if (srclen < 0) srclen = strlenW( src ) + 1;
276 if (oem_file_apis)
277 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
278 else
279 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
280 return ret;
284 /**************************************************************************
285 * SetFileApisToOEM (KERNEL32.@)
287 VOID WINAPI SetFileApisToOEM(void)
289 oem_file_apis = TRUE;
293 /**************************************************************************
294 * SetFileApisToANSI (KERNEL32.@)
296 VOID WINAPI SetFileApisToANSI(void)
298 oem_file_apis = FALSE;
302 /******************************************************************************
303 * AreFileApisANSI (KERNEL32.@)
305 * Determines if file functions are using ANSI
307 * RETURNS
308 * TRUE: Set of file functions is using ANSI code page
309 * FALSE: Set of file functions is using OEM code page
311 BOOL WINAPI AreFileApisANSI(void)
313 return !oem_file_apis;
317 /**************************************************************************
318 * Operations on file handles *
319 **************************************************************************/
321 /******************************************************************
322 * FILE_ReadWriteApc (internal)
324 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG reserved)
326 LPOVERLAPPED_COMPLETION_ROUTINE cr = apc_user;
328 cr(RtlNtStatusToDosError(io_status->u.Status), io_status->Information, (LPOVERLAPPED)io_status);
332 /***********************************************************************
333 * ReadFileEx (KERNEL32.@)
335 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
336 LPOVERLAPPED overlapped,
337 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
339 LARGE_INTEGER offset;
340 NTSTATUS status;
341 PIO_STATUS_BLOCK io_status;
343 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
345 if (!overlapped)
347 SetLastError(ERROR_INVALID_PARAMETER);
348 return FALSE;
351 offset.u.LowPart = overlapped->u.s.Offset;
352 offset.u.HighPart = overlapped->u.s.OffsetHigh;
353 io_status = (PIO_STATUS_BLOCK)overlapped;
354 io_status->u.Status = STATUS_PENDING;
355 io_status->Information = 0;
357 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
358 io_status, buffer, bytesToRead, &offset, NULL);
360 if (status && status != STATUS_PENDING)
362 SetLastError( RtlNtStatusToDosError(status) );
363 return FALSE;
365 return TRUE;
369 /***********************************************************************
370 * ReadFileScatter (KERNEL32.@)
372 BOOL WINAPI ReadFileScatter( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
373 LPDWORD reserved, LPOVERLAPPED overlapped )
375 PIO_STATUS_BLOCK io_status;
376 LARGE_INTEGER offset;
377 NTSTATUS status;
379 TRACE( "(%p %p %u %p)\n", file, segments, count, overlapped );
381 offset.u.LowPart = overlapped->u.s.Offset;
382 offset.u.HighPart = overlapped->u.s.OffsetHigh;
383 io_status = (PIO_STATUS_BLOCK)overlapped;
384 io_status->u.Status = STATUS_PENDING;
385 io_status->Information = 0;
387 status = NtReadFileScatter( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
388 if (status) SetLastError( RtlNtStatusToDosError(status) );
389 return !status;
393 /***********************************************************************
394 * ReadFile (KERNEL32.@)
396 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
397 LPDWORD bytesRead, LPOVERLAPPED overlapped )
399 LARGE_INTEGER offset;
400 PLARGE_INTEGER poffset = NULL;
401 IO_STATUS_BLOCK iosb;
402 PIO_STATUS_BLOCK io_status = &iosb;
403 HANDLE hEvent = 0;
404 NTSTATUS status;
405 LPVOID cvalue = NULL;
407 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToRead,
408 bytesRead, overlapped );
410 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
412 if (is_console_handle(hFile))
414 DWORD conread, mode;
415 if (!ReadConsoleA(hFile, buffer, bytesToRead, &conread, NULL) ||
416 !GetConsoleMode(hFile, &mode))
417 return FALSE;
418 /* ctrl-Z (26) means end of file on window (if at beginning of buffer)
419 * but Unix uses ctrl-D (4), and ctrl-Z is a bad idea on Unix :-/
420 * So map both ctrl-D ctrl-Z to EOF.
422 if ((mode & ENABLE_PROCESSED_INPUT) && conread > 0 &&
423 (((char*)buffer)[0] == 26 || ((char*)buffer)[0] == 4))
425 conread = 0;
427 if (bytesRead) *bytesRead = conread;
428 return TRUE;
431 if (overlapped != NULL)
433 offset.u.LowPart = overlapped->u.s.Offset;
434 offset.u.HighPart = overlapped->u.s.OffsetHigh;
435 poffset = &offset;
436 hEvent = overlapped->hEvent;
437 io_status = (PIO_STATUS_BLOCK)overlapped;
438 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
440 io_status->u.Status = STATUS_PENDING;
441 io_status->Information = 0;
443 status = NtReadFile(hFile, hEvent, NULL, cvalue, io_status, buffer, bytesToRead, poffset, NULL);
445 if (status == STATUS_PENDING && !overlapped)
447 WaitForSingleObject( hFile, INFINITE );
448 status = io_status->u.Status;
451 if (status != STATUS_PENDING && bytesRead)
452 *bytesRead = io_status->Information;
454 if (status == STATUS_END_OF_FILE)
456 if (overlapped != NULL)
458 SetLastError( RtlNtStatusToDosError(status) );
459 return FALSE;
462 else if (status && status != STATUS_TIMEOUT)
464 SetLastError( RtlNtStatusToDosError(status) );
465 return FALSE;
467 return TRUE;
471 /***********************************************************************
472 * WriteFileEx (KERNEL32.@)
474 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
475 LPOVERLAPPED overlapped,
476 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
478 LARGE_INTEGER offset;
479 NTSTATUS status;
480 PIO_STATUS_BLOCK io_status;
482 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
484 if (overlapped == NULL)
486 SetLastError(ERROR_INVALID_PARAMETER);
487 return FALSE;
489 offset.u.LowPart = overlapped->u.s.Offset;
490 offset.u.HighPart = overlapped->u.s.OffsetHigh;
492 io_status = (PIO_STATUS_BLOCK)overlapped;
493 io_status->u.Status = STATUS_PENDING;
494 io_status->Information = 0;
496 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
497 io_status, buffer, bytesToWrite, &offset, NULL);
499 if (status && status != STATUS_PENDING)
501 SetLastError( RtlNtStatusToDosError(status) );
502 return FALSE;
504 return TRUE;
508 /***********************************************************************
509 * WriteFileGather (KERNEL32.@)
511 BOOL WINAPI WriteFileGather( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
512 LPDWORD reserved, LPOVERLAPPED overlapped )
514 PIO_STATUS_BLOCK io_status;
515 LARGE_INTEGER offset;
516 NTSTATUS status;
518 TRACE( "%p %p %u %p\n", file, segments, count, overlapped );
520 offset.u.LowPart = overlapped->u.s.Offset;
521 offset.u.HighPart = overlapped->u.s.OffsetHigh;
522 io_status = (PIO_STATUS_BLOCK)overlapped;
523 io_status->u.Status = STATUS_PENDING;
524 io_status->Information = 0;
526 status = NtWriteFileGather( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
527 if (status) SetLastError( RtlNtStatusToDosError(status) );
528 return !status;
532 /***********************************************************************
533 * WriteFile (KERNEL32.@)
535 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
536 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
538 HANDLE hEvent = NULL;
539 LARGE_INTEGER offset;
540 PLARGE_INTEGER poffset = NULL;
541 NTSTATUS status;
542 IO_STATUS_BLOCK iosb;
543 PIO_STATUS_BLOCK piosb = &iosb;
544 LPVOID cvalue = NULL;
546 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
548 if (is_console_handle(hFile))
549 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
551 if (overlapped)
553 offset.u.LowPart = overlapped->u.s.Offset;
554 offset.u.HighPart = overlapped->u.s.OffsetHigh;
555 poffset = &offset;
556 hEvent = overlapped->hEvent;
557 piosb = (PIO_STATUS_BLOCK)overlapped;
558 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
560 piosb->u.Status = STATUS_PENDING;
561 piosb->Information = 0;
563 status = NtWriteFile(hFile, hEvent, NULL, cvalue, piosb,
564 buffer, bytesToWrite, poffset, NULL);
566 if (status == STATUS_PENDING && !overlapped)
568 WaitForSingleObject( hFile, INFINITE );
569 status = piosb->u.Status;
572 if (status != STATUS_PENDING && bytesWritten)
573 *bytesWritten = piosb->Information;
575 if (status && status != STATUS_TIMEOUT)
577 SetLastError( RtlNtStatusToDosError(status) );
578 return FALSE;
580 return TRUE;
584 /***********************************************************************
585 * GetOverlappedResult (KERNEL32.@)
587 * Check the result of an Asynchronous data transfer from a file.
589 * Parameters
590 * HANDLE hFile [in] handle of file to check on
591 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
592 * LPDWORD lpTransferred [in/out] number of bytes transferred
593 * BOOL bWait [in] wait for the transfer to complete ?
595 * RETURNS
596 * TRUE on success
597 * FALSE on failure
599 * If successful (and relevant) lpTransferred will hold the number of
600 * bytes transferred during the async operation.
602 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
603 LPDWORD lpTransferred, BOOL bWait)
605 NTSTATUS status;
607 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
609 status = lpOverlapped->Internal;
610 if (status == STATUS_PENDING)
612 if (!bWait)
614 SetLastError( ERROR_IO_INCOMPLETE );
615 return FALSE;
618 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : hFile,
619 INFINITE ) == WAIT_FAILED)
620 return FALSE;
621 status = lpOverlapped->Internal;
624 *lpTransferred = lpOverlapped->InternalHigh;
626 if (status) SetLastError( RtlNtStatusToDosError(status) );
627 return !status;
630 /***********************************************************************
631 * CancelIoEx (KERNEL32.@)
633 * Cancels pending I/O operations on a file given the overlapped used.
635 * PARAMS
636 * handle [I] File handle.
637 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
639 * RETURNS
640 * Success: TRUE.
641 * Failure: FALSE, check GetLastError().
643 BOOL WINAPI CancelIoEx(HANDLE handle, LPOVERLAPPED lpOverlapped)
645 IO_STATUS_BLOCK io_status;
647 NtCancelIoFileEx(handle, (PIO_STATUS_BLOCK) lpOverlapped, &io_status);
648 if (io_status.u.Status)
650 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
651 return FALSE;
653 return TRUE;
656 /***********************************************************************
657 * CancelIo (KERNEL32.@)
659 * Cancels pending I/O operations initiated by the current thread on a file.
661 * PARAMS
662 * handle [I] File handle.
664 * RETURNS
665 * Success: TRUE.
666 * Failure: FALSE, check GetLastError().
668 BOOL WINAPI CancelIo(HANDLE handle)
670 IO_STATUS_BLOCK io_status;
672 NtCancelIoFile(handle, &io_status);
673 if (io_status.u.Status)
675 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
676 return FALSE;
678 return TRUE;
681 /***********************************************************************
682 * _hread (KERNEL32.@)
684 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
686 return _lread( hFile, buffer, count );
690 /***********************************************************************
691 * _hwrite (KERNEL32.@)
693 * experimentation yields that _lwrite:
694 * o truncates the file at the current position with
695 * a 0 len write
696 * o returns 0 on a 0 length write
697 * o works with console handles
700 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
702 DWORD result;
704 TRACE("%d %p %d\n", handle, buffer, count );
706 if (!count)
708 /* Expand or truncate at current position */
709 if (!SetEndOfFile( LongToHandle(handle) )) return HFILE_ERROR;
710 return 0;
712 if (!WriteFile( LongToHandle(handle), buffer, count, &result, NULL ))
713 return HFILE_ERROR;
714 return result;
718 /***********************************************************************
719 * _lclose (KERNEL32.@)
721 HFILE WINAPI _lclose( HFILE hFile )
723 TRACE("handle %d\n", hFile );
724 return CloseHandle( LongToHandle(hFile) ) ? 0 : HFILE_ERROR;
728 /***********************************************************************
729 * _lcreat (KERNEL32.@)
731 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
733 HANDLE hfile;
735 /* Mask off all flags not explicitly allowed by the doc */
736 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
737 TRACE("%s %02x\n", path, attr );
738 hfile = CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
739 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
740 CREATE_ALWAYS, attr, 0 );
741 return HandleToLong(hfile);
745 /***********************************************************************
746 * _lopen (KERNEL32.@)
748 HFILE WINAPI _lopen( LPCSTR path, INT mode )
750 HANDLE hfile;
752 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
753 hfile = create_file_OF( path, mode & ~OF_CREATE );
754 return HandleToLong(hfile);
757 /***********************************************************************
758 * _lread (KERNEL32.@)
760 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
762 DWORD result;
763 if (!ReadFile( LongToHandle(handle), buffer, count, &result, NULL ))
764 return HFILE_ERROR;
765 return result;
769 /***********************************************************************
770 * _llseek (KERNEL32.@)
772 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
774 return SetFilePointer( LongToHandle(hFile), lOffset, NULL, nOrigin );
778 /***********************************************************************
779 * _lwrite (KERNEL32.@)
781 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
783 return (UINT)_hwrite( hFile, buffer, (LONG)count );
787 /***********************************************************************
788 * FlushFileBuffers (KERNEL32.@)
790 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
792 NTSTATUS nts;
793 IO_STATUS_BLOCK ioblk;
795 if (is_console_handle( hFile ))
797 /* this will fail (as expected) for an output handle */
798 return FlushConsoleInputBuffer( hFile );
800 nts = NtFlushBuffersFile( hFile, &ioblk );
801 if (nts != STATUS_SUCCESS)
803 SetLastError( RtlNtStatusToDosError( nts ) );
804 return FALSE;
807 return TRUE;
811 /***********************************************************************
812 * GetFileType (KERNEL32.@)
814 DWORD WINAPI GetFileType( HANDLE hFile )
816 FILE_FS_DEVICE_INFORMATION info;
817 IO_STATUS_BLOCK io;
818 NTSTATUS status;
820 if (hFile == (HANDLE)STD_INPUT_HANDLE || hFile == (HANDLE)STD_OUTPUT_HANDLE
821 || hFile == (HANDLE)STD_ERROR_HANDLE)
822 hFile = GetStdHandle((DWORD_PTR)hFile);
824 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
826 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
827 if (status != STATUS_SUCCESS)
829 SetLastError( RtlNtStatusToDosError(status) );
830 return FILE_TYPE_UNKNOWN;
833 switch(info.DeviceType)
835 case FILE_DEVICE_NULL:
836 case FILE_DEVICE_SERIAL_PORT:
837 case FILE_DEVICE_PARALLEL_PORT:
838 case FILE_DEVICE_TAPE:
839 case FILE_DEVICE_UNKNOWN:
840 return FILE_TYPE_CHAR;
841 case FILE_DEVICE_NAMED_PIPE:
842 return FILE_TYPE_PIPE;
843 default:
844 return FILE_TYPE_DISK;
849 /***********************************************************************
850 * GetFileInformationByHandle (KERNEL32.@)
852 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
854 FILE_ALL_INFORMATION all_info;
855 IO_STATUS_BLOCK io;
856 NTSTATUS status;
858 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
859 if (status == STATUS_BUFFER_OVERFLOW) status = STATUS_SUCCESS;
860 if (status == STATUS_SUCCESS)
862 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
863 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
864 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
865 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
866 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
867 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
868 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
869 info->dwVolumeSerialNumber = 0; /* FIXME */
870 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
871 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
872 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
873 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
874 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
875 return TRUE;
877 SetLastError( RtlNtStatusToDosError(status) );
878 return FALSE;
882 /***********************************************************************
883 * GetFileInformationByHandleEx (KERNEL32.@)
885 BOOL WINAPI GetFileInformationByHandleEx( HANDLE handle, FILE_INFO_BY_HANDLE_CLASS class,
886 LPVOID info, DWORD size )
888 NTSTATUS status;
889 IO_STATUS_BLOCK io;
891 switch (class)
893 case FileBasicInfo:
894 case FileStandardInfo:
895 case FileRenameInfo:
896 case FileDispositionInfo:
897 case FileAllocationInfo:
898 case FileEndOfFileInfo:
899 case FileStreamInfo:
900 case FileCompressionInfo:
901 case FileAttributeTagInfo:
902 case FileIoPriorityHintInfo:
903 case FileRemoteProtocolInfo:
904 case FileFullDirectoryInfo:
905 case FileFullDirectoryRestartInfo:
906 case FileStorageInfo:
907 case FileAlignmentInfo:
908 case FileIdInfo:
909 case FileIdExtdDirectoryInfo:
910 case FileIdExtdDirectoryRestartInfo:
911 FIXME( "%p, %u, %p, %u\n", handle, class, info, size );
912 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
913 return FALSE;
915 case FileNameInfo:
916 status = NtQueryInformationFile( handle, &io, info, size, FileNameInformation );
917 if (status != STATUS_SUCCESS)
919 SetLastError( RtlNtStatusToDosError( status ) );
920 return FALSE;
922 return TRUE;
924 case FileIdBothDirectoryRestartInfo:
925 case FileIdBothDirectoryInfo:
926 status = NtQueryDirectoryFile( handle, NULL, NULL, NULL, &io, info, size,
927 FileIdBothDirectoryInformation, FALSE, NULL,
928 (class == FileIdBothDirectoryRestartInfo) );
929 if (status != STATUS_SUCCESS)
931 SetLastError( RtlNtStatusToDosError( status ) );
932 return FALSE;
934 return TRUE;
936 default:
937 SetLastError( ERROR_INVALID_PARAMETER );
938 return FALSE;
943 /***********************************************************************
944 * GetFileSize (KERNEL32.@)
946 * Retrieve the size of a file.
948 * PARAMS
949 * hFile [I] File to retrieve size of.
950 * filesizehigh [O] On return, the high bits of the file size.
952 * RETURNS
953 * Success: The low bits of the file size.
954 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
955 * check GetLastError() for values other than ERROR_SUCCESS.
957 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
959 LARGE_INTEGER size;
960 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
961 if (filesizehigh) *filesizehigh = size.u.HighPart;
962 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
963 return size.u.LowPart;
967 /***********************************************************************
968 * GetFileSizeEx (KERNEL32.@)
970 * Retrieve the size of a file.
972 * PARAMS
973 * hFile [I] File to retrieve size of.
974 * lpFileSIze [O] On return, the size of the file.
976 * RETURNS
977 * Success: TRUE.
978 * Failure: FALSE, check GetLastError().
980 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
982 FILE_STANDARD_INFORMATION info;
983 IO_STATUS_BLOCK io;
984 NTSTATUS status;
986 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileStandardInformation );
987 if (status == STATUS_SUCCESS)
989 *lpFileSize = info.EndOfFile;
990 return TRUE;
992 SetLastError( RtlNtStatusToDosError(status) );
993 return FALSE;
997 /**************************************************************************
998 * SetEndOfFile (KERNEL32.@)
1000 * Sets the current position as the end of the file.
1002 * PARAMS
1003 * hFile [I] File handle.
1005 * RETURNS
1006 * Success: TRUE.
1007 * Failure: FALSE, check GetLastError().
1009 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1011 FILE_POSITION_INFORMATION pos;
1012 FILE_END_OF_FILE_INFORMATION eof;
1013 IO_STATUS_BLOCK io;
1014 NTSTATUS status;
1016 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
1017 if (status == STATUS_SUCCESS)
1019 eof.EndOfFile = pos.CurrentByteOffset;
1020 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
1022 if (status == STATUS_SUCCESS) return TRUE;
1023 SetLastError( RtlNtStatusToDosError(status) );
1024 return FALSE;
1027 BOOL WINAPI SetFileInformationByHandle( HANDLE file, FILE_INFO_BY_HANDLE_CLASS class, VOID *info, DWORD size )
1029 FIXME("%p %u %p %u - stub\n", file, class, info, size);
1030 return FALSE;
1033 /***********************************************************************
1034 * SetFilePointer (KERNEL32.@)
1036 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
1038 LARGE_INTEGER dist, newpos;
1040 if (highword)
1042 dist.u.LowPart = distance;
1043 dist.u.HighPart = *highword;
1045 else dist.QuadPart = distance;
1047 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
1049 if (highword) *highword = newpos.u.HighPart;
1050 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
1051 return newpos.u.LowPart;
1055 /***********************************************************************
1056 * SetFilePointerEx (KERNEL32.@)
1058 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
1059 LARGE_INTEGER *newpos, DWORD method )
1061 LONGLONG pos;
1062 IO_STATUS_BLOCK io;
1063 FILE_POSITION_INFORMATION info;
1065 switch(method)
1067 case FILE_BEGIN:
1068 pos = distance.QuadPart;
1069 break;
1070 case FILE_CURRENT:
1071 if (NtQueryInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1072 goto error;
1073 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
1074 break;
1075 case FILE_END:
1077 FILE_END_OF_FILE_INFORMATION eof;
1078 if (NtQueryInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
1079 goto error;
1080 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
1082 break;
1083 default:
1084 SetLastError( ERROR_INVALID_PARAMETER );
1085 return FALSE;
1088 if (pos < 0)
1090 SetLastError( ERROR_NEGATIVE_SEEK );
1091 return FALSE;
1094 info.CurrentByteOffset.QuadPart = pos;
1095 if (NtSetInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1096 goto error;
1097 if (newpos) newpos->QuadPart = pos;
1098 return TRUE;
1100 error:
1101 SetLastError( RtlNtStatusToDosError(io.u.Status) );
1102 return FALSE;
1105 /***********************************************************************
1106 * SetFileValidData (KERNEL32.@)
1108 BOOL WINAPI SetFileValidData( HANDLE hFile, LONGLONG ValidDataLength )
1110 FILE_VALID_DATA_LENGTH_INFORMATION info;
1111 IO_STATUS_BLOCK io;
1112 NTSTATUS status;
1114 info.ValidDataLength.QuadPart = ValidDataLength;
1115 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileValidDataLengthInformation );
1117 if (status == STATUS_SUCCESS) return TRUE;
1118 SetLastError( RtlNtStatusToDosError(status) );
1119 return FALSE;
1122 /***********************************************************************
1123 * GetFileTime (KERNEL32.@)
1125 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
1126 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1128 FILE_BASIC_INFORMATION info;
1129 IO_STATUS_BLOCK io;
1130 NTSTATUS status;
1132 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1133 if (status == STATUS_SUCCESS)
1135 if (lpCreationTime)
1137 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
1138 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
1140 if (lpLastAccessTime)
1142 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
1143 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
1145 if (lpLastWriteTime)
1147 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
1148 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1150 return TRUE;
1152 SetLastError( RtlNtStatusToDosError(status) );
1153 return FALSE;
1157 /***********************************************************************
1158 * SetFileTime (KERNEL32.@)
1160 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1161 const FILETIME *atime, const FILETIME *mtime )
1163 FILE_BASIC_INFORMATION info;
1164 IO_STATUS_BLOCK io;
1165 NTSTATUS status;
1167 memset( &info, 0, sizeof(info) );
1168 if (ctime)
1170 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1171 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1173 if (atime)
1175 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1176 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1178 if (mtime)
1180 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1181 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1184 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1185 if (status == STATUS_SUCCESS) return TRUE;
1186 SetLastError( RtlNtStatusToDosError(status) );
1187 return FALSE;
1191 /**************************************************************************
1192 * LockFile (KERNEL32.@)
1194 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1195 DWORD count_low, DWORD count_high )
1197 NTSTATUS status;
1198 LARGE_INTEGER count, offset;
1200 TRACE( "%p %x%08x %x%08x\n",
1201 hFile, offset_high, offset_low, count_high, count_low );
1203 count.u.LowPart = count_low;
1204 count.u.HighPart = count_high;
1205 offset.u.LowPart = offset_low;
1206 offset.u.HighPart = offset_high;
1208 status = NtLockFile( hFile, 0, NULL, NULL,
1209 NULL, &offset, &count, NULL, TRUE, TRUE );
1211 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1212 return !status;
1216 /**************************************************************************
1217 * LockFileEx [KERNEL32.@]
1219 * Locks a byte range within an open file for shared or exclusive access.
1221 * RETURNS
1222 * success: TRUE
1223 * failure: FALSE
1225 * NOTES
1226 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1228 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1229 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1231 NTSTATUS status;
1232 LARGE_INTEGER count, offset;
1233 LPVOID cvalue = NULL;
1235 if (reserved)
1237 SetLastError( ERROR_INVALID_PARAMETER );
1238 return FALSE;
1241 TRACE( "%p %x%08x %x%08x flags %x\n",
1242 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1243 count_high, count_low, flags );
1245 count.u.LowPart = count_low;
1246 count.u.HighPart = count_high;
1247 offset.u.LowPart = overlapped->u.s.Offset;
1248 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1250 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1252 status = NtLockFile( hFile, overlapped->hEvent, NULL, cvalue,
1253 NULL, &offset, &count, NULL,
1254 flags & LOCKFILE_FAIL_IMMEDIATELY,
1255 flags & LOCKFILE_EXCLUSIVE_LOCK );
1257 if (status) SetLastError( RtlNtStatusToDosError(status) );
1258 return !status;
1262 /**************************************************************************
1263 * UnlockFile (KERNEL32.@)
1265 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1266 DWORD count_low, DWORD count_high )
1268 NTSTATUS status;
1269 LARGE_INTEGER count, offset;
1271 count.u.LowPart = count_low;
1272 count.u.HighPart = count_high;
1273 offset.u.LowPart = offset_low;
1274 offset.u.HighPart = offset_high;
1276 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1277 if (status) SetLastError( RtlNtStatusToDosError(status) );
1278 return !status;
1282 /**************************************************************************
1283 * UnlockFileEx (KERNEL32.@)
1285 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1286 LPOVERLAPPED overlapped )
1288 if (reserved)
1290 SetLastError( ERROR_INVALID_PARAMETER );
1291 return FALSE;
1293 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1295 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1299 /*************************************************************************
1300 * SetHandleCount (KERNEL32.@)
1302 UINT WINAPI SetHandleCount( UINT count )
1304 return count;
1308 /**************************************************************************
1309 * Operations on file names *
1310 **************************************************************************/
1313 /*************************************************************************
1314 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1316 * Creates or opens an object, and returns a handle that can be used to
1317 * access that object.
1319 * PARAMS
1321 * filename [in] pointer to filename to be accessed
1322 * access [in] access mode requested
1323 * sharing [in] share mode
1324 * sa [in] pointer to security attributes
1325 * creation [in] how to create the file
1326 * attributes [in] attributes for newly created file
1327 * template [in] handle to file with extended attributes to copy
1329 * RETURNS
1330 * Success: Open handle to specified file
1331 * Failure: INVALID_HANDLE_VALUE
1333 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1334 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1335 DWORD attributes, HANDLE template )
1337 NTSTATUS status;
1338 UINT options;
1339 OBJECT_ATTRIBUTES attr;
1340 UNICODE_STRING nameW;
1341 IO_STATUS_BLOCK io;
1342 HANDLE ret;
1343 DWORD dosdev;
1344 const WCHAR *vxd_name = NULL;
1345 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1346 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1347 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1348 SECURITY_QUALITY_OF_SERVICE qos;
1350 static const UINT nt_disposition[5] =
1352 FILE_CREATE, /* CREATE_NEW */
1353 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1354 FILE_OPEN, /* OPEN_EXISTING */
1355 FILE_OPEN_IF, /* OPEN_ALWAYS */
1356 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1360 /* sanity checks */
1362 if (!filename || !filename[0])
1364 SetLastError( ERROR_PATH_NOT_FOUND );
1365 return INVALID_HANDLE_VALUE;
1368 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1369 (access & GENERIC_READ)?"GENERIC_READ ":"",
1370 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1371 (access & GENERIC_EXECUTE)?"GENERIC_EXECUTE ":"",
1372 (!access)?"QUERY_ACCESS ":"",
1373 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1374 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1375 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1376 creation, attributes);
1378 /* Open a console for CONIN$ or CONOUT$ */
1380 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1382 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle),
1383 creation ? OPEN_EXISTING : 0);
1384 if (ret == INVALID_HANDLE_VALUE) SetLastError(ERROR_INVALID_PARAMETER);
1385 goto done;
1388 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1390 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1391 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1393 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1394 !strncmpiW( filename + 4, pipeW, 5 ) ||
1395 !strncmpiW( filename + 4, mailslotW, 9 ))
1397 dosdev = 0;
1399 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1401 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1403 else if (GetVersion() & 0x80000000)
1405 vxd_name = filename + 4;
1406 if (!creation) creation = OPEN_EXISTING;
1409 else dosdev = RtlIsDosDeviceName_U( filename );
1411 if (dosdev)
1413 static const WCHAR conW[] = {'C','O','N'};
1415 if (LOWORD(dosdev) == sizeof(conW) &&
1416 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1418 switch (access & (GENERIC_READ|GENERIC_WRITE))
1420 case GENERIC_READ:
1421 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1422 goto done;
1423 case GENERIC_WRITE:
1424 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1425 goto done;
1426 default:
1427 SetLastError( ERROR_FILE_NOT_FOUND );
1428 return INVALID_HANDLE_VALUE;
1433 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1435 SetLastError( ERROR_INVALID_PARAMETER );
1436 return INVALID_HANDLE_VALUE;
1439 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1441 SetLastError( ERROR_PATH_NOT_FOUND );
1442 return INVALID_HANDLE_VALUE;
1445 /* now call NtCreateFile */
1447 options = 0;
1448 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1449 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1450 else
1451 options |= FILE_NON_DIRECTORY_FILE;
1452 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1454 options |= FILE_DELETE_ON_CLOSE;
1455 access |= DELETE;
1457 if (attributes & FILE_FLAG_NO_BUFFERING)
1458 options |= FILE_NO_INTERMEDIATE_BUFFERING;
1459 if (!(attributes & FILE_FLAG_OVERLAPPED))
1460 options |= FILE_SYNCHRONOUS_IO_NONALERT;
1461 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1462 options |= FILE_RANDOM_ACCESS;
1463 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1465 attr.Length = sizeof(attr);
1466 attr.RootDirectory = 0;
1467 attr.Attributes = OBJ_CASE_INSENSITIVE;
1468 attr.ObjectName = &nameW;
1469 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1470 if (attributes & SECURITY_SQOS_PRESENT)
1472 qos.Length = sizeof(qos);
1473 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
1474 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
1475 qos.EffectiveOnly = (attributes & SECURITY_EFFECTIVE_ONLY) != 0;
1476 attr.SecurityQualityOfService = &qos;
1478 else
1479 attr.SecurityQualityOfService = NULL;
1481 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1483 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1484 sharing, nt_disposition[creation - CREATE_NEW],
1485 options, NULL, 0 );
1486 if (status)
1488 if (vxd_name && vxd_name[0])
1490 static HANDLE (*vxd_open)(LPCWSTR,DWORD,SECURITY_ATTRIBUTES*);
1491 if (!vxd_open) vxd_open = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1492 "__wine_vxd_open" );
1493 if (vxd_open && (ret = vxd_open( vxd_name, access, sa ))) goto done;
1496 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1497 ret = INVALID_HANDLE_VALUE;
1499 /* In the case file creation was rejected due to CREATE_NEW flag
1500 * was specified and file with that name already exists, correct
1501 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1502 * Note: RtlNtStatusToDosError is not the subject to blame here.
1504 if (status == STATUS_OBJECT_NAME_COLLISION)
1505 SetLastError( ERROR_FILE_EXISTS );
1506 else
1507 SetLastError( RtlNtStatusToDosError(status) );
1509 else
1511 if ((creation == CREATE_ALWAYS && io.Information == FILE_OVERWRITTEN) ||
1512 (creation == OPEN_ALWAYS && io.Information == FILE_OPENED))
1513 SetLastError( ERROR_ALREADY_EXISTS );
1514 else
1515 SetLastError( 0 );
1517 RtlFreeUnicodeString( &nameW );
1519 done:
1520 if (!ret) ret = INVALID_HANDLE_VALUE;
1521 TRACE("returning %p\n", ret);
1522 return ret;
1527 /*************************************************************************
1528 * CreateFileA (KERNEL32.@)
1530 * See CreateFileW.
1532 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1533 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1534 DWORD attributes, HANDLE template)
1536 WCHAR *nameW;
1538 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1539 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1542 /*************************************************************************
1543 * CreateFile2 (KERNEL32.@)
1545 HANDLE WINAPI CreateFile2( LPCWSTR filename, DWORD access, DWORD sharing, DWORD creation,
1546 CREATEFILE2_EXTENDED_PARAMETERS *exparams )
1548 LPSECURITY_ATTRIBUTES sa = exparams ? exparams->lpSecurityAttributes : NULL;
1549 DWORD attributes = exparams ? exparams->dwFileAttributes : 0;
1550 HANDLE template = exparams ? exparams->hTemplateFile : NULL;
1552 FIXME("(%s %x %x %x %p), partial stub\n", debugstr_w(filename), access, sharing, creation, exparams);
1554 return CreateFileW( filename, access, sharing, sa, creation, attributes, template );
1557 /***********************************************************************
1558 * DeleteFileW (KERNEL32.@)
1560 * Delete a file.
1562 * PARAMS
1563 * path [I] Path to the file to delete.
1565 * RETURNS
1566 * Success: TRUE.
1567 * Failure: FALSE, check GetLastError().
1569 BOOL WINAPI DeleteFileW( LPCWSTR path )
1571 UNICODE_STRING nameW;
1572 OBJECT_ATTRIBUTES attr;
1573 NTSTATUS status;
1574 HANDLE hFile;
1575 IO_STATUS_BLOCK io;
1577 TRACE("%s\n", debugstr_w(path) );
1579 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1581 SetLastError( ERROR_PATH_NOT_FOUND );
1582 return FALSE;
1585 attr.Length = sizeof(attr);
1586 attr.RootDirectory = 0;
1587 attr.Attributes = OBJ_CASE_INSENSITIVE;
1588 attr.ObjectName = &nameW;
1589 attr.SecurityDescriptor = NULL;
1590 attr.SecurityQualityOfService = NULL;
1592 status = NtCreateFile(&hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
1593 &attr, &io, NULL, 0,
1594 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1595 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0);
1596 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1598 RtlFreeUnicodeString( &nameW );
1599 if (status)
1601 SetLastError( RtlNtStatusToDosError(status) );
1602 return FALSE;
1604 return TRUE;
1608 /***********************************************************************
1609 * DeleteFileA (KERNEL32.@)
1611 * See DeleteFileW.
1613 BOOL WINAPI DeleteFileA( LPCSTR path )
1615 WCHAR *pathW;
1617 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1618 return DeleteFileW( pathW );
1622 /**************************************************************************
1623 * ReplaceFileW (KERNEL32.@)
1624 * ReplaceFile (KERNEL32.@)
1626 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName, LPCWSTR lpReplacementFileName,
1627 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1628 LPVOID lpExclude, LPVOID lpReserved)
1630 UNICODE_STRING nt_replaced_name, nt_replacement_name;
1631 ANSI_STRING unix_replaced_name, unix_replacement_name, unix_backup_name;
1632 HANDLE hReplaced = NULL, hReplacement = NULL, hBackup = NULL;
1633 DWORD error = ERROR_SUCCESS;
1634 UINT replaced_flags;
1635 BOOL ret = FALSE;
1636 NTSTATUS status;
1637 IO_STATUS_BLOCK io;
1638 OBJECT_ATTRIBUTES attr;
1640 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName),
1641 debugstr_w(lpReplacementFileName), debugstr_w(lpBackupFileName),
1642 dwReplaceFlags, lpExclude, lpReserved);
1644 if (dwReplaceFlags)
1645 FIXME("Ignoring flags %x\n", dwReplaceFlags);
1647 /* First two arguments are mandatory */
1648 if (!lpReplacedFileName || !lpReplacementFileName)
1650 SetLastError(ERROR_INVALID_PARAMETER);
1651 return FALSE;
1654 unix_replaced_name.Buffer = NULL;
1655 unix_replacement_name.Buffer = NULL;
1656 unix_backup_name.Buffer = NULL;
1658 attr.Length = sizeof(attr);
1659 attr.RootDirectory = 0;
1660 attr.Attributes = OBJ_CASE_INSENSITIVE;
1661 attr.ObjectName = NULL;
1662 attr.SecurityDescriptor = NULL;
1663 attr.SecurityQualityOfService = NULL;
1665 /* Open the "replaced" file for reading and writing */
1666 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName, &nt_replaced_name, NULL, NULL)))
1668 error = ERROR_PATH_NOT_FOUND;
1669 goto fail;
1671 replaced_flags = lpBackupFileName ? FILE_OPEN : FILE_OPEN_IF;
1672 attr.ObjectName = &nt_replaced_name;
1673 status = NtOpenFile(&hReplaced, GENERIC_READ|GENERIC_WRITE|DELETE|SYNCHRONIZE,
1674 &attr, &io,
1675 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1676 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1677 if (status == STATUS_SUCCESS)
1678 status = wine_nt_to_unix_file_name(&nt_replaced_name, &unix_replaced_name, replaced_flags, FALSE);
1679 RtlFreeUnicodeString(&nt_replaced_name);
1680 if (status != STATUS_SUCCESS)
1682 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1683 error = ERROR_FILE_NOT_FOUND;
1684 else
1685 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1686 goto fail;
1690 * Open the replacement file for reading, writing, and deleting
1691 * (writing and deleting are needed when finished)
1693 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName, &nt_replacement_name, NULL, NULL)))
1695 error = ERROR_PATH_NOT_FOUND;
1696 goto fail;
1698 attr.ObjectName = &nt_replacement_name;
1699 status = NtOpenFile(&hReplacement,
1700 GENERIC_READ|GENERIC_WRITE|DELETE|WRITE_DAC|SYNCHRONIZE,
1701 &attr, &io, 0,
1702 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1703 if (status == STATUS_SUCCESS)
1704 status = wine_nt_to_unix_file_name(&nt_replacement_name, &unix_replacement_name, FILE_OPEN, FALSE);
1705 RtlFreeUnicodeString(&nt_replacement_name);
1706 if (status != STATUS_SUCCESS)
1708 error = RtlNtStatusToDosError(status);
1709 goto fail;
1712 /* If the user wants a backup then that needs to be performed first */
1713 if (lpBackupFileName)
1715 UNICODE_STRING nt_backup_name;
1716 FILE_BASIC_INFORMATION replaced_info;
1718 /* Obtain the file attributes from the "replaced" file */
1719 status = NtQueryInformationFile(hReplaced, &io, &replaced_info,
1720 sizeof(replaced_info),
1721 FileBasicInformation);
1722 if (status != STATUS_SUCCESS)
1724 error = RtlNtStatusToDosError(status);
1725 goto fail;
1728 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName, &nt_backup_name, NULL, NULL)))
1730 error = ERROR_PATH_NOT_FOUND;
1731 goto fail;
1733 attr.ObjectName = &nt_backup_name;
1734 /* Open the backup with permissions to write over it */
1735 status = NtCreateFile(&hBackup, GENERIC_WRITE,
1736 &attr, &io, NULL, replaced_info.FileAttributes,
1737 FILE_SHARE_WRITE, FILE_OPEN_IF,
1738 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE,
1739 NULL, 0);
1740 if (status == STATUS_SUCCESS)
1741 status = wine_nt_to_unix_file_name(&nt_backup_name, &unix_backup_name, FILE_OPEN_IF, FALSE);
1742 RtlFreeUnicodeString(&nt_backup_name);
1743 if (status != STATUS_SUCCESS)
1745 error = RtlNtStatusToDosError(status);
1746 goto fail;
1749 /* If an existing backup exists then copy over it */
1750 if (rename(unix_replaced_name.Buffer, unix_backup_name.Buffer) == -1)
1752 error = ERROR_UNABLE_TO_REMOVE_REPLACED; /* is this correct? */
1753 goto fail;
1758 * Now that the backup has been performed (if requested), copy the replacement
1759 * into place
1761 if (rename(unix_replacement_name.Buffer, unix_replaced_name.Buffer) == -1)
1763 if (errno == EACCES)
1765 /* Inappropriate permissions on "replaced", rename will fail */
1766 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1767 goto fail;
1769 /* on failure we need to indicate whether a backup was made */
1770 if (!lpBackupFileName)
1771 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT;
1772 else
1773 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT_2;
1774 goto fail;
1776 /* Success! */
1777 ret = TRUE;
1779 /* Perform resource cleanup */
1780 fail:
1781 if (hBackup) CloseHandle(hBackup);
1782 if (hReplaced) CloseHandle(hReplaced);
1783 if (hReplacement) CloseHandle(hReplacement);
1784 RtlFreeAnsiString(&unix_backup_name);
1785 RtlFreeAnsiString(&unix_replacement_name);
1786 RtlFreeAnsiString(&unix_replaced_name);
1788 /* If there was an error, set the error code */
1789 if(!ret)
1790 SetLastError(error);
1791 return ret;
1795 /**************************************************************************
1796 * ReplaceFileA (KERNEL32.@)
1798 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1799 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1800 LPVOID lpExclude, LPVOID lpReserved)
1802 WCHAR *replacedW, *replacementW, *backupW = NULL;
1803 BOOL ret;
1805 /* This function only makes sense when the first two parameters are defined */
1806 if (!lpReplacedFileName || !(replacedW = FILE_name_AtoW( lpReplacedFileName, TRUE )))
1808 SetLastError(ERROR_INVALID_PARAMETER);
1809 return FALSE;
1811 if (!lpReplacementFileName || !(replacementW = FILE_name_AtoW( lpReplacementFileName, TRUE )))
1813 HeapFree( GetProcessHeap(), 0, replacedW );
1814 SetLastError(ERROR_INVALID_PARAMETER);
1815 return FALSE;
1817 /* The backup parameter, however, is optional */
1818 if (lpBackupFileName)
1820 if (!(backupW = FILE_name_AtoW( lpBackupFileName, TRUE )))
1822 HeapFree( GetProcessHeap(), 0, replacedW );
1823 HeapFree( GetProcessHeap(), 0, replacementW );
1824 SetLastError(ERROR_INVALID_PARAMETER);
1825 return FALSE;
1828 ret = ReplaceFileW( replacedW, replacementW, backupW, dwReplaceFlags, lpExclude, lpReserved );
1829 HeapFree( GetProcessHeap(), 0, replacedW );
1830 HeapFree( GetProcessHeap(), 0, replacementW );
1831 HeapFree( GetProcessHeap(), 0, backupW );
1832 return ret;
1836 /*************************************************************************
1837 * FindFirstFileExW (KERNEL32.@)
1839 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1840 * results as FindExSearchNameMatch
1842 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1843 LPVOID data, FINDEX_SEARCH_OPS search_op,
1844 LPVOID filter, DWORD flags)
1846 WCHAR *mask, *p;
1847 FIND_FIRST_INFO *info = NULL;
1848 UNICODE_STRING nt_name;
1849 OBJECT_ATTRIBUTES attr;
1850 IO_STATUS_BLOCK io;
1851 NTSTATUS status;
1852 DWORD device = 0;
1854 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1856 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1857 || flags != 0)
1859 FIXME("options not implemented 0x%08x 0x%08x\n", search_op, flags );
1860 return INVALID_HANDLE_VALUE;
1862 if (level != FindExInfoStandard)
1864 FIXME("info level %d not implemented\n", level );
1865 return INVALID_HANDLE_VALUE;
1868 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1870 SetLastError( ERROR_PATH_NOT_FOUND );
1871 return INVALID_HANDLE_VALUE;
1874 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1876 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1877 goto error;
1880 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1882 static const WCHAR dotW[] = {'.',0};
1883 WCHAR *dir = NULL;
1885 /* we still need to check that the directory can be opened */
1887 if (HIWORD(device))
1889 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1891 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1892 goto error;
1894 memcpy( dir, filename, HIWORD(device) );
1895 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1897 RtlFreeUnicodeString( &nt_name );
1898 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1900 HeapFree( GetProcessHeap(), 0, dir );
1901 SetLastError( ERROR_PATH_NOT_FOUND );
1902 goto error;
1904 HeapFree( GetProcessHeap(), 0, dir );
1905 RtlInitUnicodeString( &info->mask, NULL );
1907 else if (!mask || !*mask)
1909 SetLastError( ERROR_FILE_NOT_FOUND );
1910 goto error;
1912 else
1914 if (!RtlCreateUnicodeString( &info->mask, mask ))
1916 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1917 goto error;
1920 /* truncate dir name before mask */
1921 *mask = 0;
1922 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1925 /* check if path is the root of the drive */
1926 info->is_root = FALSE;
1927 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1928 if (p[0] && p[1] == ':')
1930 p += 2;
1931 while (*p == '\\') p++;
1932 info->is_root = (*p == 0);
1935 attr.Length = sizeof(attr);
1936 attr.RootDirectory = 0;
1937 attr.Attributes = OBJ_CASE_INSENSITIVE;
1938 attr.ObjectName = &nt_name;
1939 attr.SecurityDescriptor = NULL;
1940 attr.SecurityQualityOfService = NULL;
1942 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1943 FILE_SHARE_READ | FILE_SHARE_WRITE,
1944 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1946 if (status != STATUS_SUCCESS)
1948 RtlFreeUnicodeString( &info->mask );
1949 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1950 SetLastError( ERROR_PATH_NOT_FOUND );
1951 else
1952 SetLastError( RtlNtStatusToDosError(status) );
1953 goto error;
1956 RtlInitializeCriticalSection( &info->cs );
1957 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
1958 info->path = nt_name;
1959 info->magic = FIND_FIRST_MAGIC;
1960 info->data_pos = 0;
1961 info->data_len = 0;
1962 info->data_size = 0;
1963 info->data = NULL;
1964 info->search_op = search_op;
1966 if (device)
1968 WIN32_FIND_DATAW *wfd = data;
1970 memset( wfd, 0, sizeof(*wfd) );
1971 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1972 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1973 CloseHandle( info->handle );
1974 info->handle = 0;
1976 else
1978 IO_STATUS_BLOCK io;
1979 BOOL has_wildcard = strpbrkW( info->mask.Buffer, wildcardsW ) != NULL;
1981 info->data_size = has_wildcard ? 8192 : max_entry_size * 2;
1983 while (info->data_size)
1985 if (!(info->data = HeapAlloc( GetProcessHeap(), 0, info->data_size )))
1987 FindClose( info );
1988 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1989 return INVALID_HANDLE_VALUE;
1992 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
1993 FileBothDirectoryInformation, FALSE, &info->mask, TRUE );
1994 if (io.u.Status)
1996 FindClose( info );
1997 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1998 return INVALID_HANDLE_VALUE;
2001 if (io.Information < info->data_size - max_entry_size)
2003 info->data_size = 0; /* we read everything */
2005 else if (info->data_size < 1024 * 1024)
2007 HeapFree( GetProcessHeap(), 0, info->data );
2008 info->data_size *= 2;
2010 else break;
2013 info->data_len = io.Information;
2014 if (!info->data_size && has_wildcard) /* release unused buffer space */
2015 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, info->data, info->data_len );
2017 if (!FindNextFileW( info, data ))
2019 TRACE( "%s not found\n", debugstr_w(filename) );
2020 FindClose( info );
2021 SetLastError( ERROR_FILE_NOT_FOUND );
2022 return INVALID_HANDLE_VALUE;
2024 if (!has_wildcard) /* we can't find two files with the same name */
2026 CloseHandle( info->handle );
2027 HeapFree( GetProcessHeap(), 0, info->data );
2028 info->handle = 0;
2029 info->data = NULL;
2032 return info;
2034 error:
2035 HeapFree( GetProcessHeap(), 0, info );
2036 RtlFreeUnicodeString( &nt_name );
2037 return INVALID_HANDLE_VALUE;
2041 /*************************************************************************
2042 * FindNextFileW (KERNEL32.@)
2044 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
2046 FIND_FIRST_INFO *info;
2047 FILE_BOTH_DIR_INFORMATION *dir_info;
2048 BOOL ret = FALSE;
2050 TRACE("%p %p\n", handle, data);
2052 if (!handle || handle == INVALID_HANDLE_VALUE)
2054 SetLastError( ERROR_INVALID_HANDLE );
2055 return ret;
2057 info = handle;
2058 if (info->magic != FIND_FIRST_MAGIC)
2060 SetLastError( ERROR_INVALID_HANDLE );
2061 return ret;
2064 RtlEnterCriticalSection( &info->cs );
2066 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
2067 else for (;;)
2069 if (info->data_pos >= info->data_len) /* need to read some more data */
2071 IO_STATUS_BLOCK io;
2073 if (info->data_size)
2074 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
2075 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
2076 else
2077 io.u.Status = STATUS_NO_MORE_FILES;
2079 if (io.u.Status)
2081 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
2082 if (io.u.Status == STATUS_NO_MORE_FILES)
2084 CloseHandle( info->handle );
2085 HeapFree( GetProcessHeap(), 0, info->data );
2086 info->handle = 0;
2087 info->data = NULL;
2089 break;
2091 info->data_len = io.Information;
2092 info->data_pos = 0;
2095 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
2097 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
2098 else info->data_pos = info->data_len;
2100 /* don't return '.' and '..' in the root of the drive */
2101 if (info->is_root)
2103 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
2104 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
2105 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
2108 /* check for dir symlink */
2109 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
2110 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2111 strpbrkW( info->mask.Buffer, wildcardsW ))
2113 if (!check_dir_symlink( info, dir_info )) continue;
2116 data->dwFileAttributes = dir_info->FileAttributes;
2117 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
2118 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
2119 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
2120 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
2121 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
2122 data->dwReserved0 = 0;
2123 data->dwReserved1 = 0;
2125 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
2126 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
2127 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
2128 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
2130 TRACE("returning %s (%s)\n",
2131 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
2133 ret = TRUE;
2134 break;
2137 RtlLeaveCriticalSection( &info->cs );
2138 return ret;
2142 /*************************************************************************
2143 * FindClose (KERNEL32.@)
2145 BOOL WINAPI FindClose( HANDLE handle )
2147 FIND_FIRST_INFO *info = handle;
2149 if (!handle || handle == INVALID_HANDLE_VALUE)
2151 SetLastError( ERROR_INVALID_HANDLE );
2152 return FALSE;
2155 __TRY
2157 if (info->magic == FIND_FIRST_MAGIC)
2159 RtlEnterCriticalSection( &info->cs );
2160 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
2162 info->magic = 0;
2163 if (info->handle) CloseHandle( info->handle );
2164 info->handle = 0;
2165 RtlFreeUnicodeString( &info->mask );
2166 info->mask.Buffer = NULL;
2167 RtlFreeUnicodeString( &info->path );
2168 info->data_pos = 0;
2169 info->data_len = 0;
2170 HeapFree( GetProcessHeap(), 0, info->data );
2171 RtlLeaveCriticalSection( &info->cs );
2172 info->cs.DebugInfo->Spare[0] = 0;
2173 RtlDeleteCriticalSection( &info->cs );
2174 HeapFree( GetProcessHeap(), 0, info );
2178 __EXCEPT_PAGE_FAULT
2180 WARN("Illegal handle %p\n", handle);
2181 SetLastError( ERROR_INVALID_HANDLE );
2182 return FALSE;
2184 __ENDTRY
2186 return TRUE;
2190 /*************************************************************************
2191 * FindFirstFileA (KERNEL32.@)
2193 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
2195 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
2196 FindExSearchNameMatch, NULL, 0);
2199 /*************************************************************************
2200 * FindFirstFileExA (KERNEL32.@)
2202 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
2203 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
2204 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
2206 HANDLE handle;
2207 WIN32_FIND_DATAA *dataA;
2208 WIN32_FIND_DATAW dataW;
2209 WCHAR *nameW;
2211 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
2213 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
2214 if (handle == INVALID_HANDLE_VALUE) return handle;
2216 dataA = lpFindFileData;
2217 dataA->dwFileAttributes = dataW.dwFileAttributes;
2218 dataA->ftCreationTime = dataW.ftCreationTime;
2219 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
2220 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
2221 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
2222 dataA->nFileSizeLow = dataW.nFileSizeLow;
2223 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
2224 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
2225 sizeof(dataA->cAlternateFileName) );
2226 return handle;
2230 /*************************************************************************
2231 * FindFirstFileW (KERNEL32.@)
2233 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
2235 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
2236 FindExSearchNameMatch, NULL, 0);
2240 /*************************************************************************
2241 * FindNextFileA (KERNEL32.@)
2243 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
2245 WIN32_FIND_DATAW dataW;
2247 if (!FindNextFileW( handle, &dataW )) return FALSE;
2248 data->dwFileAttributes = dataW.dwFileAttributes;
2249 data->ftCreationTime = dataW.ftCreationTime;
2250 data->ftLastAccessTime = dataW.ftLastAccessTime;
2251 data->ftLastWriteTime = dataW.ftLastWriteTime;
2252 data->nFileSizeHigh = dataW.nFileSizeHigh;
2253 data->nFileSizeLow = dataW.nFileSizeLow;
2254 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
2255 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
2256 sizeof(data->cAlternateFileName) );
2257 return TRUE;
2261 /**************************************************************************
2262 * GetFileAttributesW (KERNEL32.@)
2264 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
2266 FILE_BASIC_INFORMATION info;
2267 UNICODE_STRING nt_name;
2268 OBJECT_ATTRIBUTES attr;
2269 NTSTATUS status;
2271 TRACE("%s\n", debugstr_w(name));
2273 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2275 SetLastError( ERROR_PATH_NOT_FOUND );
2276 return INVALID_FILE_ATTRIBUTES;
2279 attr.Length = sizeof(attr);
2280 attr.RootDirectory = 0;
2281 attr.Attributes = OBJ_CASE_INSENSITIVE;
2282 attr.ObjectName = &nt_name;
2283 attr.SecurityDescriptor = NULL;
2284 attr.SecurityQualityOfService = NULL;
2286 status = NtQueryAttributesFile( &attr, &info );
2287 RtlFreeUnicodeString( &nt_name );
2289 if (status == STATUS_SUCCESS) return info.FileAttributes;
2291 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2292 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
2294 SetLastError( RtlNtStatusToDosError(status) );
2295 return INVALID_FILE_ATTRIBUTES;
2299 /**************************************************************************
2300 * GetFileAttributesA (KERNEL32.@)
2302 DWORD WINAPI GetFileAttributesA( LPCSTR name )
2304 WCHAR *nameW;
2306 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
2307 return GetFileAttributesW( nameW );
2311 /**************************************************************************
2312 * SetFileAttributesW (KERNEL32.@)
2314 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
2316 UNICODE_STRING nt_name;
2317 OBJECT_ATTRIBUTES attr;
2318 IO_STATUS_BLOCK io;
2319 NTSTATUS status;
2320 HANDLE handle;
2322 TRACE("%s %x\n", debugstr_w(name), attributes);
2324 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2326 SetLastError( ERROR_PATH_NOT_FOUND );
2327 return FALSE;
2330 attr.Length = sizeof(attr);
2331 attr.RootDirectory = 0;
2332 attr.Attributes = OBJ_CASE_INSENSITIVE;
2333 attr.ObjectName = &nt_name;
2334 attr.SecurityDescriptor = NULL;
2335 attr.SecurityQualityOfService = NULL;
2337 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2338 RtlFreeUnicodeString( &nt_name );
2340 if (status == STATUS_SUCCESS)
2342 FILE_BASIC_INFORMATION info;
2344 memset( &info, 0, sizeof(info) );
2345 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2346 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2347 NtClose( handle );
2350 if (status == STATUS_SUCCESS) return TRUE;
2351 SetLastError( RtlNtStatusToDosError(status) );
2352 return FALSE;
2356 /**************************************************************************
2357 * SetFileAttributesA (KERNEL32.@)
2359 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2361 WCHAR *nameW;
2363 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2364 return SetFileAttributesW( nameW, attributes );
2368 /**************************************************************************
2369 * GetFileAttributesExW (KERNEL32.@)
2371 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2373 FILE_NETWORK_OPEN_INFORMATION info;
2374 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2375 UNICODE_STRING nt_name;
2376 OBJECT_ATTRIBUTES attr;
2377 NTSTATUS status;
2379 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2381 if (level != GetFileExInfoStandard)
2383 SetLastError( ERROR_INVALID_PARAMETER );
2384 return FALSE;
2387 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2389 SetLastError( ERROR_PATH_NOT_FOUND );
2390 return FALSE;
2393 attr.Length = sizeof(attr);
2394 attr.RootDirectory = 0;
2395 attr.Attributes = OBJ_CASE_INSENSITIVE;
2396 attr.ObjectName = &nt_name;
2397 attr.SecurityDescriptor = NULL;
2398 attr.SecurityQualityOfService = NULL;
2400 status = NtQueryFullAttributesFile( &attr, &info );
2401 RtlFreeUnicodeString( &nt_name );
2403 if (status != STATUS_SUCCESS)
2405 SetLastError( RtlNtStatusToDosError(status) );
2406 return FALSE;
2409 data->dwFileAttributes = info.FileAttributes;
2410 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2411 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2412 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2413 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2414 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2415 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2416 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2417 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2418 return TRUE;
2422 /**************************************************************************
2423 * GetFileAttributesExA (KERNEL32.@)
2425 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2427 WCHAR *nameW;
2429 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2430 return GetFileAttributesExW( nameW, level, ptr );
2434 /******************************************************************************
2435 * GetCompressedFileSizeW (KERNEL32.@)
2437 * Get the actual number of bytes used on disk.
2439 * RETURNS
2440 * Success: Low-order doubleword of number of bytes
2441 * Failure: INVALID_FILE_SIZE
2443 DWORD WINAPI GetCompressedFileSizeW(
2444 LPCWSTR name, /* [in] Pointer to name of file */
2445 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2447 UNICODE_STRING nt_name;
2448 OBJECT_ATTRIBUTES attr;
2449 IO_STATUS_BLOCK io;
2450 NTSTATUS status;
2451 HANDLE handle;
2452 DWORD ret = INVALID_FILE_SIZE;
2454 TRACE("%s %p\n", debugstr_w(name), size_high);
2456 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2458 SetLastError( ERROR_PATH_NOT_FOUND );
2459 return INVALID_FILE_SIZE;
2462 attr.Length = sizeof(attr);
2463 attr.RootDirectory = 0;
2464 attr.Attributes = OBJ_CASE_INSENSITIVE;
2465 attr.ObjectName = &nt_name;
2466 attr.SecurityDescriptor = NULL;
2467 attr.SecurityQualityOfService = NULL;
2469 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2470 RtlFreeUnicodeString( &nt_name );
2472 if (status == STATUS_SUCCESS)
2474 /* we don't support compressed files, simply return the file size */
2475 ret = GetFileSize( handle, size_high );
2476 NtClose( handle );
2478 else SetLastError( RtlNtStatusToDosError(status) );
2480 return ret;
2484 /******************************************************************************
2485 * GetCompressedFileSizeA (KERNEL32.@)
2487 * See GetCompressedFileSizeW.
2489 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2491 WCHAR *nameW;
2493 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2494 return GetCompressedFileSizeW( nameW, size_high );
2498 /***********************************************************************
2499 * OpenVxDHandle (KERNEL32.@)
2501 * This function is supposed to return the corresponding Ring 0
2502 * ("kernel") handle for a Ring 3 handle in Win9x.
2503 * Evidently, Wine will have problems with this. But we try anyway,
2504 * maybe it helps...
2506 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
2508 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
2509 return hHandleRing3;
2513 /****************************************************************************
2514 * DeviceIoControl (KERNEL32.@)
2516 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
2517 LPVOID lpvInBuffer, DWORD cbInBuffer,
2518 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
2519 LPDWORD lpcbBytesReturned,
2520 LPOVERLAPPED lpOverlapped)
2522 NTSTATUS status;
2524 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2525 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
2526 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
2528 /* Check if this is a user defined control code for a VxD */
2530 if (HIWORD( dwIoControlCode ) == 0 && (GetVersion() & 0x80000000))
2532 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
2533 static DeviceIoProc (*vxd_get_proc)(HANDLE);
2534 DeviceIoProc proc = NULL;
2536 if (!vxd_get_proc) vxd_get_proc = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2537 "__wine_vxd_get_proc" );
2538 if (vxd_get_proc) proc = vxd_get_proc( hDevice );
2539 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
2540 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
2543 /* Not a VxD, let ntdll handle it */
2545 if (lpOverlapped)
2547 LPVOID cvalue = ((ULONG_PTR)lpOverlapped->hEvent & 1) ? NULL : lpOverlapped;
2548 lpOverlapped->Internal = STATUS_PENDING;
2549 lpOverlapped->InternalHigh = 0;
2550 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2551 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
2552 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2553 dwIoControlCode, lpvInBuffer, cbInBuffer,
2554 lpvOutBuffer, cbOutBuffer);
2555 else
2556 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
2557 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2558 dwIoControlCode, lpvInBuffer, cbInBuffer,
2559 lpvOutBuffer, cbOutBuffer);
2560 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
2562 else
2564 IO_STATUS_BLOCK iosb;
2566 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2567 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
2568 dwIoControlCode, lpvInBuffer, cbInBuffer,
2569 lpvOutBuffer, cbOutBuffer);
2570 else
2571 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
2572 dwIoControlCode, lpvInBuffer, cbInBuffer,
2573 lpvOutBuffer, cbOutBuffer);
2574 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
2576 if (status) SetLastError( RtlNtStatusToDosError(status) );
2577 return !status;
2581 /***********************************************************************
2582 * OpenFile (KERNEL32.@)
2584 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2586 HANDLE handle;
2587 FILETIME filetime;
2588 WORD filedatetime[2];
2590 if (!ofs) return HFILE_ERROR;
2592 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2593 ((mode & 0x3 )==OF_READ)?"OF_READ":
2594 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2595 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2596 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2597 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2598 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2599 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2600 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2601 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2602 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2603 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2604 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2605 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2606 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2607 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2608 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2609 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2613 ofs->cBytes = sizeof(OFSTRUCT);
2614 ofs->nErrCode = 0;
2615 if (mode & OF_REOPEN) name = ofs->szPathName;
2617 if (!name) return HFILE_ERROR;
2619 TRACE("%s %04x\n", name, mode );
2621 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2622 Are there any cases where getting the path here is wrong?
2623 Uwe Bonnes 1997 Apr 2 */
2624 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2626 /* OF_PARSE simply fills the structure */
2628 if (mode & OF_PARSE)
2630 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2631 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2632 return 0;
2635 /* OF_CREATE is completely different from all other options, so
2636 handle it first */
2638 if (mode & OF_CREATE)
2640 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2641 goto error;
2643 else
2645 /* Now look for the file */
2647 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2648 goto error;
2650 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2652 if (mode & OF_DELETE)
2654 if (!DeleteFileA( ofs->szPathName )) goto error;
2655 TRACE("(%s): OF_DELETE return = OK\n", name);
2656 return TRUE;
2659 handle = LongToHandle(_lopen( ofs->szPathName, mode ));
2660 if (handle == INVALID_HANDLE_VALUE) goto error;
2662 GetFileTime( handle, NULL, NULL, &filetime );
2663 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2664 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2666 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2668 CloseHandle( handle );
2669 WARN("(%s): OF_VERIFY failed\n", name );
2670 /* FIXME: what error here? */
2671 SetLastError( ERROR_FILE_NOT_FOUND );
2672 goto error;
2675 ofs->Reserved1 = filedatetime[0];
2676 ofs->Reserved2 = filedatetime[1];
2678 TRACE("(%s): OK, return = %p\n", name, handle );
2679 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2681 CloseHandle( handle );
2682 return TRUE;
2684 return HandleToLong(handle);
2686 error: /* We get here if there was an error opening the file */
2687 ofs->nErrCode = GetLastError();
2688 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2689 return HFILE_ERROR;
2693 /***********************************************************************
2694 * OpenFileById (KERNEL32.@)
2696 HANDLE WINAPI OpenFileById( HANDLE handle, LPFILE_ID_DESCRIPTOR id, DWORD access,
2697 DWORD share, LPSECURITY_ATTRIBUTES sec_attr, DWORD flags )
2699 UINT options;
2700 HANDLE result;
2701 OBJECT_ATTRIBUTES attr;
2702 NTSTATUS status;
2703 IO_STATUS_BLOCK io;
2704 UNICODE_STRING objectName;
2706 if (!id)
2708 SetLastError( ERROR_INVALID_PARAMETER );
2709 return INVALID_HANDLE_VALUE;
2712 options = FILE_OPEN_BY_FILE_ID;
2713 if (flags & FILE_FLAG_BACKUP_SEMANTICS)
2714 options |= FILE_OPEN_FOR_BACKUP_INTENT;
2715 else
2716 options |= FILE_NON_DIRECTORY_FILE;
2717 if (flags & FILE_FLAG_NO_BUFFERING) options |= FILE_NO_INTERMEDIATE_BUFFERING;
2718 if (!(flags & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
2719 if (flags & FILE_FLAG_RANDOM_ACCESS) options |= FILE_RANDOM_ACCESS;
2720 flags &= FILE_ATTRIBUTE_VALID_FLAGS;
2722 objectName.Length = sizeof(ULONGLONG);
2723 objectName.Buffer = (WCHAR *)&id->u.FileId;
2724 attr.Length = sizeof(attr);
2725 attr.RootDirectory = handle;
2726 attr.Attributes = 0;
2727 attr.ObjectName = &objectName;
2728 attr.SecurityDescriptor = sec_attr ? sec_attr->lpSecurityDescriptor : NULL;
2729 attr.SecurityQualityOfService = NULL;
2730 if (sec_attr && sec_attr->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
2732 status = NtCreateFile( &result, access, &attr, &io, NULL, flags,
2733 share, OPEN_EXISTING, options, NULL, 0 );
2734 if (status != STATUS_SUCCESS)
2736 SetLastError( RtlNtStatusToDosError( status ) );
2737 return INVALID_HANDLE_VALUE;
2739 return result;
2743 /***********************************************************************
2744 * K32EnumDeviceDrivers (KERNEL32.@)
2746 BOOL WINAPI K32EnumDeviceDrivers(void **image_base, DWORD cb, DWORD *needed)
2748 FIXME("(%p, %d, %p): stub\n", image_base, cb, needed);
2750 if (needed)
2751 *needed = 0;
2753 return TRUE;
2756 /***********************************************************************
2757 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2759 DWORD WINAPI K32GetDeviceDriverBaseNameA(void *image_base, LPSTR base_name, DWORD size)
2761 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2763 if (base_name && size)
2764 base_name[0] = '\0';
2766 return 0;
2769 /***********************************************************************
2770 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2772 DWORD WINAPI K32GetDeviceDriverBaseNameW(void *image_base, LPWSTR base_name, DWORD size)
2774 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2776 if (base_name && size)
2777 base_name[0] = '\0';
2779 return 0;
2782 /***********************************************************************
2783 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2785 DWORD WINAPI K32GetDeviceDriverFileNameA(void *image_base, LPSTR file_name, DWORD size)
2787 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2789 if (file_name && size)
2790 file_name[0] = '\0';
2792 return 0;
2795 /***********************************************************************
2796 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2798 DWORD WINAPI K32GetDeviceDriverFileNameW(void *image_base, LPWSTR file_name, DWORD size)
2800 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2802 if (file_name && size)
2803 file_name[0] = '\0';
2805 return 0;