vbscript: Support multibyte characters in Global_Chr.
[wine.git] / dlls / kernel32 / file.c
blob9bcd9e9e6a5add3e4d9beaf3a1bbf2a5686ff20d
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 if (is_console_handle( hFile ))
988 SetLastError( ERROR_INVALID_HANDLE );
989 return FALSE;
992 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileStandardInformation );
993 if (status == STATUS_SUCCESS)
995 *lpFileSize = info.EndOfFile;
996 return TRUE;
998 SetLastError( RtlNtStatusToDosError(status) );
999 return FALSE;
1003 /**************************************************************************
1004 * SetEndOfFile (KERNEL32.@)
1006 * Sets the current position as the end of the file.
1008 * PARAMS
1009 * hFile [I] File handle.
1011 * RETURNS
1012 * Success: TRUE.
1013 * Failure: FALSE, check GetLastError().
1015 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1017 FILE_POSITION_INFORMATION pos;
1018 FILE_END_OF_FILE_INFORMATION eof;
1019 IO_STATUS_BLOCK io;
1020 NTSTATUS status;
1022 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
1023 if (status == STATUS_SUCCESS)
1025 eof.EndOfFile = pos.CurrentByteOffset;
1026 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
1028 if (status == STATUS_SUCCESS) return TRUE;
1029 SetLastError( RtlNtStatusToDosError(status) );
1030 return FALSE;
1033 BOOL WINAPI SetFileInformationByHandle( HANDLE file, FILE_INFO_BY_HANDLE_CLASS class, VOID *info, DWORD size )
1035 FIXME("%p %u %p %u - stub\n", file, class, info, size);
1036 return FALSE;
1039 /***********************************************************************
1040 * SetFilePointer (KERNEL32.@)
1042 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
1044 LARGE_INTEGER dist, newpos;
1046 if (highword)
1048 dist.u.LowPart = distance;
1049 dist.u.HighPart = *highword;
1051 else dist.QuadPart = distance;
1053 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
1055 if (highword) *highword = newpos.u.HighPart;
1056 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
1057 return newpos.u.LowPart;
1061 /***********************************************************************
1062 * SetFilePointerEx (KERNEL32.@)
1064 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
1065 LARGE_INTEGER *newpos, DWORD method )
1067 LONGLONG pos;
1068 IO_STATUS_BLOCK io;
1069 FILE_POSITION_INFORMATION info;
1071 switch(method)
1073 case FILE_BEGIN:
1074 pos = distance.QuadPart;
1075 break;
1076 case FILE_CURRENT:
1077 if (NtQueryInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1078 goto error;
1079 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
1080 break;
1081 case FILE_END:
1083 FILE_END_OF_FILE_INFORMATION eof;
1084 if (NtQueryInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
1085 goto error;
1086 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
1088 break;
1089 default:
1090 SetLastError( ERROR_INVALID_PARAMETER );
1091 return FALSE;
1094 if (pos < 0)
1096 SetLastError( ERROR_NEGATIVE_SEEK );
1097 return FALSE;
1100 info.CurrentByteOffset.QuadPart = pos;
1101 if (NtSetInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1102 goto error;
1103 if (newpos) newpos->QuadPart = pos;
1104 return TRUE;
1106 error:
1107 SetLastError( RtlNtStatusToDosError(io.u.Status) );
1108 return FALSE;
1111 /***********************************************************************
1112 * SetFileValidData (KERNEL32.@)
1114 BOOL WINAPI SetFileValidData( HANDLE hFile, LONGLONG ValidDataLength )
1116 FILE_VALID_DATA_LENGTH_INFORMATION info;
1117 IO_STATUS_BLOCK io;
1118 NTSTATUS status;
1120 info.ValidDataLength.QuadPart = ValidDataLength;
1121 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileValidDataLengthInformation );
1123 if (status == STATUS_SUCCESS) return TRUE;
1124 SetLastError( RtlNtStatusToDosError(status) );
1125 return FALSE;
1128 /***********************************************************************
1129 * GetFileTime (KERNEL32.@)
1131 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
1132 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1134 FILE_BASIC_INFORMATION info;
1135 IO_STATUS_BLOCK io;
1136 NTSTATUS status;
1138 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1139 if (status == STATUS_SUCCESS)
1141 if (lpCreationTime)
1143 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
1144 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
1146 if (lpLastAccessTime)
1148 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
1149 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
1151 if (lpLastWriteTime)
1153 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
1154 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1156 return TRUE;
1158 SetLastError( RtlNtStatusToDosError(status) );
1159 return FALSE;
1163 /***********************************************************************
1164 * SetFileTime (KERNEL32.@)
1166 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1167 const FILETIME *atime, const FILETIME *mtime )
1169 FILE_BASIC_INFORMATION info;
1170 IO_STATUS_BLOCK io;
1171 NTSTATUS status;
1173 memset( &info, 0, sizeof(info) );
1174 if (ctime)
1176 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1177 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1179 if (atime)
1181 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1182 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1184 if (mtime)
1186 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1187 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1190 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1191 if (status == STATUS_SUCCESS) return TRUE;
1192 SetLastError( RtlNtStatusToDosError(status) );
1193 return FALSE;
1197 /**************************************************************************
1198 * LockFile (KERNEL32.@)
1200 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1201 DWORD count_low, DWORD count_high )
1203 NTSTATUS status;
1204 LARGE_INTEGER count, offset;
1206 TRACE( "%p %x%08x %x%08x\n",
1207 hFile, offset_high, offset_low, count_high, count_low );
1209 count.u.LowPart = count_low;
1210 count.u.HighPart = count_high;
1211 offset.u.LowPart = offset_low;
1212 offset.u.HighPart = offset_high;
1214 status = NtLockFile( hFile, 0, NULL, NULL,
1215 NULL, &offset, &count, NULL, TRUE, TRUE );
1217 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1218 return !status;
1222 /**************************************************************************
1223 * LockFileEx [KERNEL32.@]
1225 * Locks a byte range within an open file for shared or exclusive access.
1227 * RETURNS
1228 * success: TRUE
1229 * failure: FALSE
1231 * NOTES
1232 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1234 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1235 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1237 NTSTATUS status;
1238 LARGE_INTEGER count, offset;
1239 LPVOID cvalue = NULL;
1241 if (reserved)
1243 SetLastError( ERROR_INVALID_PARAMETER );
1244 return FALSE;
1247 TRACE( "%p %x%08x %x%08x flags %x\n",
1248 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1249 count_high, count_low, flags );
1251 count.u.LowPart = count_low;
1252 count.u.HighPart = count_high;
1253 offset.u.LowPart = overlapped->u.s.Offset;
1254 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1256 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1258 status = NtLockFile( hFile, overlapped->hEvent, NULL, cvalue,
1259 NULL, &offset, &count, NULL,
1260 flags & LOCKFILE_FAIL_IMMEDIATELY,
1261 flags & LOCKFILE_EXCLUSIVE_LOCK );
1263 if (status) SetLastError( RtlNtStatusToDosError(status) );
1264 return !status;
1268 /**************************************************************************
1269 * UnlockFile (KERNEL32.@)
1271 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1272 DWORD count_low, DWORD count_high )
1274 NTSTATUS status;
1275 LARGE_INTEGER count, offset;
1277 count.u.LowPart = count_low;
1278 count.u.HighPart = count_high;
1279 offset.u.LowPart = offset_low;
1280 offset.u.HighPart = offset_high;
1282 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1283 if (status) SetLastError( RtlNtStatusToDosError(status) );
1284 return !status;
1288 /**************************************************************************
1289 * UnlockFileEx (KERNEL32.@)
1291 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1292 LPOVERLAPPED overlapped )
1294 if (reserved)
1296 SetLastError( ERROR_INVALID_PARAMETER );
1297 return FALSE;
1299 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1301 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1305 /*************************************************************************
1306 * SetHandleCount (KERNEL32.@)
1308 UINT WINAPI SetHandleCount( UINT count )
1310 return count;
1314 /**************************************************************************
1315 * Operations on file names *
1316 **************************************************************************/
1319 /*************************************************************************
1320 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1322 * Creates or opens an object, and returns a handle that can be used to
1323 * access that object.
1325 * PARAMS
1327 * filename [in] pointer to filename to be accessed
1328 * access [in] access mode requested
1329 * sharing [in] share mode
1330 * sa [in] pointer to security attributes
1331 * creation [in] how to create the file
1332 * attributes [in] attributes for newly created file
1333 * template [in] handle to file with extended attributes to copy
1335 * RETURNS
1336 * Success: Open handle to specified file
1337 * Failure: INVALID_HANDLE_VALUE
1339 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1340 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1341 DWORD attributes, HANDLE template )
1343 NTSTATUS status;
1344 UINT options;
1345 OBJECT_ATTRIBUTES attr;
1346 UNICODE_STRING nameW;
1347 IO_STATUS_BLOCK io;
1348 HANDLE ret;
1349 DWORD dosdev;
1350 const WCHAR *vxd_name = NULL;
1351 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1352 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1353 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1354 SECURITY_QUALITY_OF_SERVICE qos;
1356 static const UINT nt_disposition[5] =
1358 FILE_CREATE, /* CREATE_NEW */
1359 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1360 FILE_OPEN, /* OPEN_EXISTING */
1361 FILE_OPEN_IF, /* OPEN_ALWAYS */
1362 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1366 /* sanity checks */
1368 if (!filename || !filename[0])
1370 SetLastError( ERROR_PATH_NOT_FOUND );
1371 return INVALID_HANDLE_VALUE;
1374 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1375 (access & GENERIC_READ)?"GENERIC_READ ":"",
1376 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1377 (access & GENERIC_EXECUTE)?"GENERIC_EXECUTE ":"",
1378 (!access)?"QUERY_ACCESS ":"",
1379 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1380 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1381 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1382 creation, attributes);
1384 /* Open a console for CONIN$ or CONOUT$ */
1386 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1388 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle),
1389 creation ? OPEN_EXISTING : 0);
1390 if (ret == INVALID_HANDLE_VALUE) SetLastError(ERROR_INVALID_PARAMETER);
1391 goto done;
1394 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1396 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1397 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1399 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1400 !strncmpiW( filename + 4, pipeW, 5 ) ||
1401 !strncmpiW( filename + 4, mailslotW, 9 ))
1403 dosdev = 0;
1405 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1407 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1409 else if (GetVersion() & 0x80000000)
1411 vxd_name = filename + 4;
1412 if (!creation) creation = OPEN_EXISTING;
1415 else dosdev = RtlIsDosDeviceName_U( filename );
1417 if (dosdev)
1419 static const WCHAR conW[] = {'C','O','N'};
1421 if (LOWORD(dosdev) == sizeof(conW) &&
1422 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1424 switch (access & (GENERIC_READ|GENERIC_WRITE))
1426 case GENERIC_READ:
1427 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1428 goto done;
1429 case GENERIC_WRITE:
1430 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1431 goto done;
1432 default:
1433 SetLastError( ERROR_FILE_NOT_FOUND );
1434 return INVALID_HANDLE_VALUE;
1439 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1441 SetLastError( ERROR_INVALID_PARAMETER );
1442 return INVALID_HANDLE_VALUE;
1445 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1447 SetLastError( ERROR_PATH_NOT_FOUND );
1448 return INVALID_HANDLE_VALUE;
1451 /* now call NtCreateFile */
1453 options = 0;
1454 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1455 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1456 else
1457 options |= FILE_NON_DIRECTORY_FILE;
1458 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1460 options |= FILE_DELETE_ON_CLOSE;
1461 access |= DELETE;
1463 if (attributes & FILE_FLAG_NO_BUFFERING)
1464 options |= FILE_NO_INTERMEDIATE_BUFFERING;
1465 if (!(attributes & FILE_FLAG_OVERLAPPED))
1466 options |= FILE_SYNCHRONOUS_IO_NONALERT;
1467 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1468 options |= FILE_RANDOM_ACCESS;
1469 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1471 attr.Length = sizeof(attr);
1472 attr.RootDirectory = 0;
1473 attr.Attributes = OBJ_CASE_INSENSITIVE;
1474 attr.ObjectName = &nameW;
1475 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1476 if (attributes & SECURITY_SQOS_PRESENT)
1478 qos.Length = sizeof(qos);
1479 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
1480 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
1481 qos.EffectiveOnly = (attributes & SECURITY_EFFECTIVE_ONLY) != 0;
1482 attr.SecurityQualityOfService = &qos;
1484 else
1485 attr.SecurityQualityOfService = NULL;
1487 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1489 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1490 sharing, nt_disposition[creation - CREATE_NEW],
1491 options, NULL, 0 );
1492 if (status)
1494 if (vxd_name && vxd_name[0])
1496 static HANDLE (*vxd_open)(LPCWSTR,DWORD,SECURITY_ATTRIBUTES*);
1497 if (!vxd_open) vxd_open = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1498 "__wine_vxd_open" );
1499 if (vxd_open && (ret = vxd_open( vxd_name, access, sa ))) goto done;
1502 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1503 ret = INVALID_HANDLE_VALUE;
1505 /* In the case file creation was rejected due to CREATE_NEW flag
1506 * was specified and file with that name already exists, correct
1507 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1508 * Note: RtlNtStatusToDosError is not the subject to blame here.
1510 if (status == STATUS_OBJECT_NAME_COLLISION)
1511 SetLastError( ERROR_FILE_EXISTS );
1512 else
1513 SetLastError( RtlNtStatusToDosError(status) );
1515 else
1517 if ((creation == CREATE_ALWAYS && io.Information == FILE_OVERWRITTEN) ||
1518 (creation == OPEN_ALWAYS && io.Information == FILE_OPENED))
1519 SetLastError( ERROR_ALREADY_EXISTS );
1520 else
1521 SetLastError( 0 );
1523 RtlFreeUnicodeString( &nameW );
1525 done:
1526 if (!ret) ret = INVALID_HANDLE_VALUE;
1527 TRACE("returning %p\n", ret);
1528 return ret;
1533 /*************************************************************************
1534 * CreateFileA (KERNEL32.@)
1536 * See CreateFileW.
1538 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1539 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1540 DWORD attributes, HANDLE template)
1542 WCHAR *nameW;
1544 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1545 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1548 /*************************************************************************
1549 * CreateFile2 (KERNEL32.@)
1551 HANDLE WINAPI CreateFile2( LPCWSTR filename, DWORD access, DWORD sharing, DWORD creation,
1552 CREATEFILE2_EXTENDED_PARAMETERS *exparams )
1554 LPSECURITY_ATTRIBUTES sa = exparams ? exparams->lpSecurityAttributes : NULL;
1555 DWORD attributes = exparams ? exparams->dwFileAttributes : 0;
1556 HANDLE template = exparams ? exparams->hTemplateFile : NULL;
1558 FIXME("(%s %x %x %x %p), partial stub\n", debugstr_w(filename), access, sharing, creation, exparams);
1560 return CreateFileW( filename, access, sharing, sa, creation, attributes, template );
1563 /***********************************************************************
1564 * DeleteFileW (KERNEL32.@)
1566 * Delete a file.
1568 * PARAMS
1569 * path [I] Path to the file to delete.
1571 * RETURNS
1572 * Success: TRUE.
1573 * Failure: FALSE, check GetLastError().
1575 BOOL WINAPI DeleteFileW( LPCWSTR path )
1577 UNICODE_STRING nameW;
1578 OBJECT_ATTRIBUTES attr;
1579 NTSTATUS status;
1580 HANDLE hFile;
1581 IO_STATUS_BLOCK io;
1583 TRACE("%s\n", debugstr_w(path) );
1585 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1587 SetLastError( ERROR_PATH_NOT_FOUND );
1588 return FALSE;
1591 attr.Length = sizeof(attr);
1592 attr.RootDirectory = 0;
1593 attr.Attributes = OBJ_CASE_INSENSITIVE;
1594 attr.ObjectName = &nameW;
1595 attr.SecurityDescriptor = NULL;
1596 attr.SecurityQualityOfService = NULL;
1598 status = NtCreateFile(&hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
1599 &attr, &io, NULL, 0,
1600 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1601 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0);
1602 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1604 RtlFreeUnicodeString( &nameW );
1605 if (status)
1607 SetLastError( RtlNtStatusToDosError(status) );
1608 return FALSE;
1610 return TRUE;
1614 /***********************************************************************
1615 * DeleteFileA (KERNEL32.@)
1617 * See DeleteFileW.
1619 BOOL WINAPI DeleteFileA( LPCSTR path )
1621 WCHAR *pathW;
1623 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1624 return DeleteFileW( pathW );
1628 /**************************************************************************
1629 * ReplaceFileW (KERNEL32.@)
1630 * ReplaceFile (KERNEL32.@)
1632 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName, LPCWSTR lpReplacementFileName,
1633 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1634 LPVOID lpExclude, LPVOID lpReserved)
1636 UNICODE_STRING nt_replaced_name, nt_replacement_name;
1637 ANSI_STRING unix_replaced_name, unix_replacement_name, unix_backup_name;
1638 HANDLE hReplaced = NULL, hReplacement = NULL, hBackup = NULL;
1639 DWORD error = ERROR_SUCCESS;
1640 UINT replaced_flags;
1641 BOOL ret = FALSE;
1642 NTSTATUS status;
1643 IO_STATUS_BLOCK io;
1644 OBJECT_ATTRIBUTES attr;
1646 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName),
1647 debugstr_w(lpReplacementFileName), debugstr_w(lpBackupFileName),
1648 dwReplaceFlags, lpExclude, lpReserved);
1650 if (dwReplaceFlags)
1651 FIXME("Ignoring flags %x\n", dwReplaceFlags);
1653 /* First two arguments are mandatory */
1654 if (!lpReplacedFileName || !lpReplacementFileName)
1656 SetLastError(ERROR_INVALID_PARAMETER);
1657 return FALSE;
1660 unix_replaced_name.Buffer = NULL;
1661 unix_replacement_name.Buffer = NULL;
1662 unix_backup_name.Buffer = NULL;
1664 attr.Length = sizeof(attr);
1665 attr.RootDirectory = 0;
1666 attr.Attributes = OBJ_CASE_INSENSITIVE;
1667 attr.ObjectName = NULL;
1668 attr.SecurityDescriptor = NULL;
1669 attr.SecurityQualityOfService = NULL;
1671 /* Open the "replaced" file for reading and writing */
1672 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName, &nt_replaced_name, NULL, NULL)))
1674 error = ERROR_PATH_NOT_FOUND;
1675 goto fail;
1677 replaced_flags = lpBackupFileName ? FILE_OPEN : FILE_OPEN_IF;
1678 attr.ObjectName = &nt_replaced_name;
1679 status = NtOpenFile(&hReplaced, GENERIC_READ|GENERIC_WRITE|DELETE|SYNCHRONIZE,
1680 &attr, &io,
1681 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1682 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1683 if (status == STATUS_SUCCESS)
1684 status = wine_nt_to_unix_file_name(&nt_replaced_name, &unix_replaced_name, replaced_flags, FALSE);
1685 RtlFreeUnicodeString(&nt_replaced_name);
1686 if (status != STATUS_SUCCESS)
1688 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1689 error = ERROR_FILE_NOT_FOUND;
1690 else
1691 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1692 goto fail;
1696 * Open the replacement file for reading, writing, and deleting
1697 * (writing and deleting are needed when finished)
1699 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName, &nt_replacement_name, NULL, NULL)))
1701 error = ERROR_PATH_NOT_FOUND;
1702 goto fail;
1704 attr.ObjectName = &nt_replacement_name;
1705 status = NtOpenFile(&hReplacement,
1706 GENERIC_READ|GENERIC_WRITE|DELETE|WRITE_DAC|SYNCHRONIZE,
1707 &attr, &io, 0,
1708 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1709 if (status == STATUS_SUCCESS)
1710 status = wine_nt_to_unix_file_name(&nt_replacement_name, &unix_replacement_name, FILE_OPEN, FALSE);
1711 RtlFreeUnicodeString(&nt_replacement_name);
1712 if (status != STATUS_SUCCESS)
1714 error = RtlNtStatusToDosError(status);
1715 goto fail;
1718 /* If the user wants a backup then that needs to be performed first */
1719 if (lpBackupFileName)
1721 UNICODE_STRING nt_backup_name;
1722 FILE_BASIC_INFORMATION replaced_info;
1724 /* Obtain the file attributes from the "replaced" file */
1725 status = NtQueryInformationFile(hReplaced, &io, &replaced_info,
1726 sizeof(replaced_info),
1727 FileBasicInformation);
1728 if (status != STATUS_SUCCESS)
1730 error = RtlNtStatusToDosError(status);
1731 goto fail;
1734 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName, &nt_backup_name, NULL, NULL)))
1736 error = ERROR_PATH_NOT_FOUND;
1737 goto fail;
1739 attr.ObjectName = &nt_backup_name;
1740 /* Open the backup with permissions to write over it */
1741 status = NtCreateFile(&hBackup, GENERIC_WRITE,
1742 &attr, &io, NULL, replaced_info.FileAttributes,
1743 FILE_SHARE_WRITE, FILE_OPEN_IF,
1744 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE,
1745 NULL, 0);
1746 if (status == STATUS_SUCCESS)
1747 status = wine_nt_to_unix_file_name(&nt_backup_name, &unix_backup_name, FILE_OPEN_IF, FALSE);
1748 RtlFreeUnicodeString(&nt_backup_name);
1749 if (status != STATUS_SUCCESS)
1751 error = RtlNtStatusToDosError(status);
1752 goto fail;
1755 /* If an existing backup exists then copy over it */
1756 if (rename(unix_replaced_name.Buffer, unix_backup_name.Buffer) == -1)
1758 error = ERROR_UNABLE_TO_REMOVE_REPLACED; /* is this correct? */
1759 goto fail;
1764 * Now that the backup has been performed (if requested), copy the replacement
1765 * into place
1767 if (rename(unix_replacement_name.Buffer, unix_replaced_name.Buffer) == -1)
1769 if (errno == EACCES)
1771 /* Inappropriate permissions on "replaced", rename will fail */
1772 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1773 goto fail;
1775 /* on failure we need to indicate whether a backup was made */
1776 if (!lpBackupFileName)
1777 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT;
1778 else
1779 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT_2;
1780 goto fail;
1782 /* Success! */
1783 ret = TRUE;
1785 /* Perform resource cleanup */
1786 fail:
1787 if (hBackup) CloseHandle(hBackup);
1788 if (hReplaced) CloseHandle(hReplaced);
1789 if (hReplacement) CloseHandle(hReplacement);
1790 RtlFreeAnsiString(&unix_backup_name);
1791 RtlFreeAnsiString(&unix_replacement_name);
1792 RtlFreeAnsiString(&unix_replaced_name);
1794 /* If there was an error, set the error code */
1795 if(!ret)
1796 SetLastError(error);
1797 return ret;
1801 /**************************************************************************
1802 * ReplaceFileA (KERNEL32.@)
1804 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1805 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1806 LPVOID lpExclude, LPVOID lpReserved)
1808 WCHAR *replacedW, *replacementW, *backupW = NULL;
1809 BOOL ret;
1811 /* This function only makes sense when the first two parameters are defined */
1812 if (!lpReplacedFileName || !(replacedW = FILE_name_AtoW( lpReplacedFileName, TRUE )))
1814 SetLastError(ERROR_INVALID_PARAMETER);
1815 return FALSE;
1817 if (!lpReplacementFileName || !(replacementW = FILE_name_AtoW( lpReplacementFileName, TRUE )))
1819 HeapFree( GetProcessHeap(), 0, replacedW );
1820 SetLastError(ERROR_INVALID_PARAMETER);
1821 return FALSE;
1823 /* The backup parameter, however, is optional */
1824 if (lpBackupFileName)
1826 if (!(backupW = FILE_name_AtoW( lpBackupFileName, TRUE )))
1828 HeapFree( GetProcessHeap(), 0, replacedW );
1829 HeapFree( GetProcessHeap(), 0, replacementW );
1830 SetLastError(ERROR_INVALID_PARAMETER);
1831 return FALSE;
1834 ret = ReplaceFileW( replacedW, replacementW, backupW, dwReplaceFlags, lpExclude, lpReserved );
1835 HeapFree( GetProcessHeap(), 0, replacedW );
1836 HeapFree( GetProcessHeap(), 0, replacementW );
1837 HeapFree( GetProcessHeap(), 0, backupW );
1838 return ret;
1842 /*************************************************************************
1843 * FindFirstFileExW (KERNEL32.@)
1845 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1846 * results as FindExSearchNameMatch
1848 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1849 LPVOID data, FINDEX_SEARCH_OPS search_op,
1850 LPVOID filter, DWORD flags)
1852 WCHAR *mask, *p;
1853 FIND_FIRST_INFO *info = NULL;
1854 UNICODE_STRING nt_name;
1855 OBJECT_ATTRIBUTES attr;
1856 IO_STATUS_BLOCK io;
1857 NTSTATUS status;
1858 DWORD device = 0;
1860 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1862 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1863 || flags != 0)
1865 FIXME("options not implemented 0x%08x 0x%08x\n", search_op, flags );
1866 return INVALID_HANDLE_VALUE;
1868 if (level != FindExInfoStandard)
1870 FIXME("info level %d not implemented\n", level );
1871 return INVALID_HANDLE_VALUE;
1874 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1876 SetLastError( ERROR_PATH_NOT_FOUND );
1877 return INVALID_HANDLE_VALUE;
1880 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1882 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1883 goto error;
1886 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1888 static const WCHAR dotW[] = {'.',0};
1889 WCHAR *dir = NULL;
1891 /* we still need to check that the directory can be opened */
1893 if (HIWORD(device))
1895 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1897 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1898 goto error;
1900 memcpy( dir, filename, HIWORD(device) );
1901 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1903 RtlFreeUnicodeString( &nt_name );
1904 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1906 HeapFree( GetProcessHeap(), 0, dir );
1907 SetLastError( ERROR_PATH_NOT_FOUND );
1908 goto error;
1910 HeapFree( GetProcessHeap(), 0, dir );
1911 RtlInitUnicodeString( &info->mask, NULL );
1913 else if (!mask || !*mask)
1915 SetLastError( ERROR_FILE_NOT_FOUND );
1916 goto error;
1918 else
1920 if (!RtlCreateUnicodeString( &info->mask, mask ))
1922 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1923 goto error;
1926 /* truncate dir name before mask */
1927 *mask = 0;
1928 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1931 /* check if path is the root of the drive */
1932 info->is_root = FALSE;
1933 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1934 if (p[0] && p[1] == ':')
1936 p += 2;
1937 while (*p == '\\') p++;
1938 info->is_root = (*p == 0);
1941 attr.Length = sizeof(attr);
1942 attr.RootDirectory = 0;
1943 attr.Attributes = OBJ_CASE_INSENSITIVE;
1944 attr.ObjectName = &nt_name;
1945 attr.SecurityDescriptor = NULL;
1946 attr.SecurityQualityOfService = NULL;
1948 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1949 FILE_SHARE_READ | FILE_SHARE_WRITE,
1950 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1952 if (status != STATUS_SUCCESS)
1954 RtlFreeUnicodeString( &info->mask );
1955 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1956 SetLastError( ERROR_PATH_NOT_FOUND );
1957 else
1958 SetLastError( RtlNtStatusToDosError(status) );
1959 goto error;
1962 RtlInitializeCriticalSection( &info->cs );
1963 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
1964 info->path = nt_name;
1965 info->magic = FIND_FIRST_MAGIC;
1966 info->data_pos = 0;
1967 info->data_len = 0;
1968 info->data_size = 0;
1969 info->data = NULL;
1970 info->search_op = search_op;
1972 if (device)
1974 WIN32_FIND_DATAW *wfd = data;
1976 memset( wfd, 0, sizeof(*wfd) );
1977 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1978 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1979 CloseHandle( info->handle );
1980 info->handle = 0;
1982 else
1984 IO_STATUS_BLOCK io;
1985 BOOL has_wildcard = strpbrkW( info->mask.Buffer, wildcardsW ) != NULL;
1987 info->data_size = has_wildcard ? 8192 : max_entry_size * 2;
1989 while (info->data_size)
1991 if (!(info->data = HeapAlloc( GetProcessHeap(), 0, info->data_size )))
1993 FindClose( info );
1994 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1995 return INVALID_HANDLE_VALUE;
1998 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
1999 FileBothDirectoryInformation, FALSE, &info->mask, TRUE );
2000 if (io.u.Status)
2002 FindClose( info );
2003 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
2004 return INVALID_HANDLE_VALUE;
2007 if (io.Information < info->data_size - max_entry_size)
2009 info->data_size = 0; /* we read everything */
2011 else if (info->data_size < 1024 * 1024)
2013 HeapFree( GetProcessHeap(), 0, info->data );
2014 info->data_size *= 2;
2016 else break;
2019 info->data_len = io.Information;
2020 if (!info->data_size && has_wildcard) /* release unused buffer space */
2021 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, info->data, info->data_len );
2023 if (!FindNextFileW( info, data ))
2025 TRACE( "%s not found\n", debugstr_w(filename) );
2026 FindClose( info );
2027 SetLastError( ERROR_FILE_NOT_FOUND );
2028 return INVALID_HANDLE_VALUE;
2030 if (!has_wildcard) /* we can't find two files with the same name */
2032 CloseHandle( info->handle );
2033 HeapFree( GetProcessHeap(), 0, info->data );
2034 info->handle = 0;
2035 info->data = NULL;
2038 return info;
2040 error:
2041 HeapFree( GetProcessHeap(), 0, info );
2042 RtlFreeUnicodeString( &nt_name );
2043 return INVALID_HANDLE_VALUE;
2047 /*************************************************************************
2048 * FindNextFileW (KERNEL32.@)
2050 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
2052 FIND_FIRST_INFO *info;
2053 FILE_BOTH_DIR_INFORMATION *dir_info;
2054 BOOL ret = FALSE;
2056 TRACE("%p %p\n", handle, data);
2058 if (!handle || handle == INVALID_HANDLE_VALUE)
2060 SetLastError( ERROR_INVALID_HANDLE );
2061 return ret;
2063 info = handle;
2064 if (info->magic != FIND_FIRST_MAGIC)
2066 SetLastError( ERROR_INVALID_HANDLE );
2067 return ret;
2070 RtlEnterCriticalSection( &info->cs );
2072 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
2073 else for (;;)
2075 if (info->data_pos >= info->data_len) /* need to read some more data */
2077 IO_STATUS_BLOCK io;
2079 if (info->data_size)
2080 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
2081 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
2082 else
2083 io.u.Status = STATUS_NO_MORE_FILES;
2085 if (io.u.Status)
2087 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
2088 if (io.u.Status == STATUS_NO_MORE_FILES)
2090 CloseHandle( info->handle );
2091 HeapFree( GetProcessHeap(), 0, info->data );
2092 info->handle = 0;
2093 info->data = NULL;
2095 break;
2097 info->data_len = io.Information;
2098 info->data_pos = 0;
2101 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
2103 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
2104 else info->data_pos = info->data_len;
2106 /* don't return '.' and '..' in the root of the drive */
2107 if (info->is_root)
2109 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
2110 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
2111 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
2114 /* check for dir symlink */
2115 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
2116 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2117 strpbrkW( info->mask.Buffer, wildcardsW ))
2119 if (!check_dir_symlink( info, dir_info )) continue;
2122 data->dwFileAttributes = dir_info->FileAttributes;
2123 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
2124 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
2125 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
2126 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
2127 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
2128 data->dwReserved0 = 0;
2129 data->dwReserved1 = 0;
2131 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
2132 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
2133 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
2134 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
2136 TRACE("returning %s (%s)\n",
2137 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
2139 ret = TRUE;
2140 break;
2143 RtlLeaveCriticalSection( &info->cs );
2144 return ret;
2148 /*************************************************************************
2149 * FindClose (KERNEL32.@)
2151 BOOL WINAPI FindClose( HANDLE handle )
2153 FIND_FIRST_INFO *info = handle;
2155 if (!handle || handle == INVALID_HANDLE_VALUE)
2157 SetLastError( ERROR_INVALID_HANDLE );
2158 return FALSE;
2161 __TRY
2163 if (info->magic == FIND_FIRST_MAGIC)
2165 RtlEnterCriticalSection( &info->cs );
2166 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
2168 info->magic = 0;
2169 if (info->handle) CloseHandle( info->handle );
2170 info->handle = 0;
2171 RtlFreeUnicodeString( &info->mask );
2172 info->mask.Buffer = NULL;
2173 RtlFreeUnicodeString( &info->path );
2174 info->data_pos = 0;
2175 info->data_len = 0;
2176 HeapFree( GetProcessHeap(), 0, info->data );
2177 RtlLeaveCriticalSection( &info->cs );
2178 info->cs.DebugInfo->Spare[0] = 0;
2179 RtlDeleteCriticalSection( &info->cs );
2180 HeapFree( GetProcessHeap(), 0, info );
2184 __EXCEPT_PAGE_FAULT
2186 WARN("Illegal handle %p\n", handle);
2187 SetLastError( ERROR_INVALID_HANDLE );
2188 return FALSE;
2190 __ENDTRY
2192 return TRUE;
2196 /*************************************************************************
2197 * FindFirstFileA (KERNEL32.@)
2199 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
2201 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
2202 FindExSearchNameMatch, NULL, 0);
2205 /*************************************************************************
2206 * FindFirstFileExA (KERNEL32.@)
2208 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
2209 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
2210 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
2212 HANDLE handle;
2213 WIN32_FIND_DATAA *dataA;
2214 WIN32_FIND_DATAW dataW;
2215 WCHAR *nameW;
2217 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
2219 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
2220 if (handle == INVALID_HANDLE_VALUE) return handle;
2222 dataA = lpFindFileData;
2223 dataA->dwFileAttributes = dataW.dwFileAttributes;
2224 dataA->ftCreationTime = dataW.ftCreationTime;
2225 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
2226 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
2227 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
2228 dataA->nFileSizeLow = dataW.nFileSizeLow;
2229 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
2230 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
2231 sizeof(dataA->cAlternateFileName) );
2232 return handle;
2236 /*************************************************************************
2237 * FindFirstFileW (KERNEL32.@)
2239 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
2241 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
2242 FindExSearchNameMatch, NULL, 0);
2246 /*************************************************************************
2247 * FindNextFileA (KERNEL32.@)
2249 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
2251 WIN32_FIND_DATAW dataW;
2253 if (!FindNextFileW( handle, &dataW )) return FALSE;
2254 data->dwFileAttributes = dataW.dwFileAttributes;
2255 data->ftCreationTime = dataW.ftCreationTime;
2256 data->ftLastAccessTime = dataW.ftLastAccessTime;
2257 data->ftLastWriteTime = dataW.ftLastWriteTime;
2258 data->nFileSizeHigh = dataW.nFileSizeHigh;
2259 data->nFileSizeLow = dataW.nFileSizeLow;
2260 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
2261 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
2262 sizeof(data->cAlternateFileName) );
2263 return TRUE;
2267 /**************************************************************************
2268 * GetFileAttributesW (KERNEL32.@)
2270 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
2272 FILE_BASIC_INFORMATION info;
2273 UNICODE_STRING nt_name;
2274 OBJECT_ATTRIBUTES attr;
2275 NTSTATUS status;
2277 TRACE("%s\n", debugstr_w(name));
2279 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2281 SetLastError( ERROR_PATH_NOT_FOUND );
2282 return INVALID_FILE_ATTRIBUTES;
2285 attr.Length = sizeof(attr);
2286 attr.RootDirectory = 0;
2287 attr.Attributes = OBJ_CASE_INSENSITIVE;
2288 attr.ObjectName = &nt_name;
2289 attr.SecurityDescriptor = NULL;
2290 attr.SecurityQualityOfService = NULL;
2292 status = NtQueryAttributesFile( &attr, &info );
2293 RtlFreeUnicodeString( &nt_name );
2295 if (status == STATUS_SUCCESS) return info.FileAttributes;
2297 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2298 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
2300 SetLastError( RtlNtStatusToDosError(status) );
2301 return INVALID_FILE_ATTRIBUTES;
2305 /**************************************************************************
2306 * GetFileAttributesA (KERNEL32.@)
2308 DWORD WINAPI GetFileAttributesA( LPCSTR name )
2310 WCHAR *nameW;
2312 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
2313 return GetFileAttributesW( nameW );
2317 /**************************************************************************
2318 * SetFileAttributesW (KERNEL32.@)
2320 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
2322 UNICODE_STRING nt_name;
2323 OBJECT_ATTRIBUTES attr;
2324 IO_STATUS_BLOCK io;
2325 NTSTATUS status;
2326 HANDLE handle;
2328 TRACE("%s %x\n", debugstr_w(name), attributes);
2330 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2332 SetLastError( ERROR_PATH_NOT_FOUND );
2333 return FALSE;
2336 attr.Length = sizeof(attr);
2337 attr.RootDirectory = 0;
2338 attr.Attributes = OBJ_CASE_INSENSITIVE;
2339 attr.ObjectName = &nt_name;
2340 attr.SecurityDescriptor = NULL;
2341 attr.SecurityQualityOfService = NULL;
2343 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2344 RtlFreeUnicodeString( &nt_name );
2346 if (status == STATUS_SUCCESS)
2348 FILE_BASIC_INFORMATION info;
2350 memset( &info, 0, sizeof(info) );
2351 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2352 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2353 NtClose( handle );
2356 if (status == STATUS_SUCCESS) return TRUE;
2357 SetLastError( RtlNtStatusToDosError(status) );
2358 return FALSE;
2362 /**************************************************************************
2363 * SetFileAttributesA (KERNEL32.@)
2365 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2367 WCHAR *nameW;
2369 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2370 return SetFileAttributesW( nameW, attributes );
2374 /**************************************************************************
2375 * GetFileAttributesExW (KERNEL32.@)
2377 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2379 FILE_NETWORK_OPEN_INFORMATION info;
2380 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2381 UNICODE_STRING nt_name;
2382 OBJECT_ATTRIBUTES attr;
2383 NTSTATUS status;
2385 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2387 if (level != GetFileExInfoStandard)
2389 SetLastError( ERROR_INVALID_PARAMETER );
2390 return FALSE;
2393 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2395 SetLastError( ERROR_PATH_NOT_FOUND );
2396 return FALSE;
2399 attr.Length = sizeof(attr);
2400 attr.RootDirectory = 0;
2401 attr.Attributes = OBJ_CASE_INSENSITIVE;
2402 attr.ObjectName = &nt_name;
2403 attr.SecurityDescriptor = NULL;
2404 attr.SecurityQualityOfService = NULL;
2406 status = NtQueryFullAttributesFile( &attr, &info );
2407 RtlFreeUnicodeString( &nt_name );
2409 if (status != STATUS_SUCCESS)
2411 SetLastError( RtlNtStatusToDosError(status) );
2412 return FALSE;
2415 data->dwFileAttributes = info.FileAttributes;
2416 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2417 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2418 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2419 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2420 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2421 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2422 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2423 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2424 return TRUE;
2428 /**************************************************************************
2429 * GetFileAttributesExA (KERNEL32.@)
2431 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2433 WCHAR *nameW;
2435 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2436 return GetFileAttributesExW( nameW, level, ptr );
2440 /******************************************************************************
2441 * GetCompressedFileSizeW (KERNEL32.@)
2443 * Get the actual number of bytes used on disk.
2445 * RETURNS
2446 * Success: Low-order doubleword of number of bytes
2447 * Failure: INVALID_FILE_SIZE
2449 DWORD WINAPI GetCompressedFileSizeW(
2450 LPCWSTR name, /* [in] Pointer to name of file */
2451 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2453 UNICODE_STRING nt_name;
2454 OBJECT_ATTRIBUTES attr;
2455 IO_STATUS_BLOCK io;
2456 NTSTATUS status;
2457 HANDLE handle;
2458 DWORD ret = INVALID_FILE_SIZE;
2460 TRACE("%s %p\n", debugstr_w(name), size_high);
2462 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2464 SetLastError( ERROR_PATH_NOT_FOUND );
2465 return INVALID_FILE_SIZE;
2468 attr.Length = sizeof(attr);
2469 attr.RootDirectory = 0;
2470 attr.Attributes = OBJ_CASE_INSENSITIVE;
2471 attr.ObjectName = &nt_name;
2472 attr.SecurityDescriptor = NULL;
2473 attr.SecurityQualityOfService = NULL;
2475 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2476 RtlFreeUnicodeString( &nt_name );
2478 if (status == STATUS_SUCCESS)
2480 /* we don't support compressed files, simply return the file size */
2481 ret = GetFileSize( handle, size_high );
2482 NtClose( handle );
2484 else SetLastError( RtlNtStatusToDosError(status) );
2486 return ret;
2490 /******************************************************************************
2491 * GetCompressedFileSizeA (KERNEL32.@)
2493 * See GetCompressedFileSizeW.
2495 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2497 WCHAR *nameW;
2499 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2500 return GetCompressedFileSizeW( nameW, size_high );
2504 /***********************************************************************
2505 * OpenVxDHandle (KERNEL32.@)
2507 * This function is supposed to return the corresponding Ring 0
2508 * ("kernel") handle for a Ring 3 handle in Win9x.
2509 * Evidently, Wine will have problems with this. But we try anyway,
2510 * maybe it helps...
2512 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
2514 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
2515 return hHandleRing3;
2519 /****************************************************************************
2520 * DeviceIoControl (KERNEL32.@)
2522 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
2523 LPVOID lpvInBuffer, DWORD cbInBuffer,
2524 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
2525 LPDWORD lpcbBytesReturned,
2526 LPOVERLAPPED lpOverlapped)
2528 NTSTATUS status;
2530 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2531 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
2532 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
2534 /* Check if this is a user defined control code for a VxD */
2536 if (HIWORD( dwIoControlCode ) == 0 && (GetVersion() & 0x80000000))
2538 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
2539 static DeviceIoProc (*vxd_get_proc)(HANDLE);
2540 DeviceIoProc proc = NULL;
2542 if (!vxd_get_proc) vxd_get_proc = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2543 "__wine_vxd_get_proc" );
2544 if (vxd_get_proc) proc = vxd_get_proc( hDevice );
2545 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
2546 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
2549 /* Not a VxD, let ntdll handle it */
2551 if (lpOverlapped)
2553 LPVOID cvalue = ((ULONG_PTR)lpOverlapped->hEvent & 1) ? NULL : lpOverlapped;
2554 lpOverlapped->Internal = STATUS_PENDING;
2555 lpOverlapped->InternalHigh = 0;
2556 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2557 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
2558 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2559 dwIoControlCode, lpvInBuffer, cbInBuffer,
2560 lpvOutBuffer, cbOutBuffer);
2561 else
2562 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
2563 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2564 dwIoControlCode, lpvInBuffer, cbInBuffer,
2565 lpvOutBuffer, cbOutBuffer);
2566 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
2568 else
2570 IO_STATUS_BLOCK iosb;
2572 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2573 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
2574 dwIoControlCode, lpvInBuffer, cbInBuffer,
2575 lpvOutBuffer, cbOutBuffer);
2576 else
2577 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
2578 dwIoControlCode, lpvInBuffer, cbInBuffer,
2579 lpvOutBuffer, cbOutBuffer);
2580 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
2582 if (status) SetLastError( RtlNtStatusToDosError(status) );
2583 return !status;
2587 /***********************************************************************
2588 * OpenFile (KERNEL32.@)
2590 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2592 HANDLE handle;
2593 FILETIME filetime;
2594 WORD filedatetime[2];
2596 if (!ofs) return HFILE_ERROR;
2598 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2599 ((mode & 0x3 )==OF_READ)?"OF_READ":
2600 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2601 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2602 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2603 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2604 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2605 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2606 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2607 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2608 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2609 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2610 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2611 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2612 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2613 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2614 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2615 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2619 ofs->cBytes = sizeof(OFSTRUCT);
2620 ofs->nErrCode = 0;
2621 if (mode & OF_REOPEN) name = ofs->szPathName;
2623 if (!name) return HFILE_ERROR;
2625 TRACE("%s %04x\n", name, mode );
2627 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2628 Are there any cases where getting the path here is wrong?
2629 Uwe Bonnes 1997 Apr 2 */
2630 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2632 /* OF_PARSE simply fills the structure */
2634 if (mode & OF_PARSE)
2636 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2637 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2638 return 0;
2641 /* OF_CREATE is completely different from all other options, so
2642 handle it first */
2644 if (mode & OF_CREATE)
2646 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2647 goto error;
2649 else
2651 /* Now look for the file */
2653 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2654 goto error;
2656 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2658 if (mode & OF_DELETE)
2660 if (!DeleteFileA( ofs->szPathName )) goto error;
2661 TRACE("(%s): OF_DELETE return = OK\n", name);
2662 return TRUE;
2665 handle = LongToHandle(_lopen( ofs->szPathName, mode ));
2666 if (handle == INVALID_HANDLE_VALUE) goto error;
2668 GetFileTime( handle, NULL, NULL, &filetime );
2669 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2670 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2672 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2674 CloseHandle( handle );
2675 WARN("(%s): OF_VERIFY failed\n", name );
2676 /* FIXME: what error here? */
2677 SetLastError( ERROR_FILE_NOT_FOUND );
2678 goto error;
2681 ofs->Reserved1 = filedatetime[0];
2682 ofs->Reserved2 = filedatetime[1];
2684 TRACE("(%s): OK, return = %p\n", name, handle );
2685 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2687 CloseHandle( handle );
2688 return TRUE;
2690 return HandleToLong(handle);
2692 error: /* We get here if there was an error opening the file */
2693 ofs->nErrCode = GetLastError();
2694 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2695 return HFILE_ERROR;
2699 /***********************************************************************
2700 * OpenFileById (KERNEL32.@)
2702 HANDLE WINAPI OpenFileById( HANDLE handle, LPFILE_ID_DESCRIPTOR id, DWORD access,
2703 DWORD share, LPSECURITY_ATTRIBUTES sec_attr, DWORD flags )
2705 UINT options;
2706 HANDLE result;
2707 OBJECT_ATTRIBUTES attr;
2708 NTSTATUS status;
2709 IO_STATUS_BLOCK io;
2710 UNICODE_STRING objectName;
2712 if (!id)
2714 SetLastError( ERROR_INVALID_PARAMETER );
2715 return INVALID_HANDLE_VALUE;
2718 options = FILE_OPEN_BY_FILE_ID;
2719 if (flags & FILE_FLAG_BACKUP_SEMANTICS)
2720 options |= FILE_OPEN_FOR_BACKUP_INTENT;
2721 else
2722 options |= FILE_NON_DIRECTORY_FILE;
2723 if (flags & FILE_FLAG_NO_BUFFERING) options |= FILE_NO_INTERMEDIATE_BUFFERING;
2724 if (!(flags & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
2725 if (flags & FILE_FLAG_RANDOM_ACCESS) options |= FILE_RANDOM_ACCESS;
2726 flags &= FILE_ATTRIBUTE_VALID_FLAGS;
2728 objectName.Length = sizeof(ULONGLONG);
2729 objectName.Buffer = (WCHAR *)&id->u.FileId;
2730 attr.Length = sizeof(attr);
2731 attr.RootDirectory = handle;
2732 attr.Attributes = 0;
2733 attr.ObjectName = &objectName;
2734 attr.SecurityDescriptor = sec_attr ? sec_attr->lpSecurityDescriptor : NULL;
2735 attr.SecurityQualityOfService = NULL;
2736 if (sec_attr && sec_attr->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
2738 status = NtCreateFile( &result, access, &attr, &io, NULL, flags,
2739 share, OPEN_EXISTING, options, NULL, 0 );
2740 if (status != STATUS_SUCCESS)
2742 SetLastError( RtlNtStatusToDosError( status ) );
2743 return INVALID_HANDLE_VALUE;
2745 return result;
2749 /***********************************************************************
2750 * K32EnumDeviceDrivers (KERNEL32.@)
2752 BOOL WINAPI K32EnumDeviceDrivers(void **image_base, DWORD cb, DWORD *needed)
2754 FIXME("(%p, %d, %p): stub\n", image_base, cb, needed);
2756 if (needed)
2757 *needed = 0;
2759 return TRUE;
2762 /***********************************************************************
2763 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2765 DWORD WINAPI K32GetDeviceDriverBaseNameA(void *image_base, LPSTR base_name, DWORD size)
2767 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2769 if (base_name && size)
2770 base_name[0] = '\0';
2772 return 0;
2775 /***********************************************************************
2776 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2778 DWORD WINAPI K32GetDeviceDriverBaseNameW(void *image_base, LPWSTR base_name, DWORD size)
2780 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2782 if (base_name && size)
2783 base_name[0] = '\0';
2785 return 0;
2788 /***********************************************************************
2789 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2791 DWORD WINAPI K32GetDeviceDriverFileNameA(void *image_base, LPSTR file_name, DWORD size)
2793 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2795 if (file_name && size)
2796 file_name[0] = '\0';
2798 return 0;
2801 /***********************************************************************
2802 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2804 DWORD WINAPI K32GetDeviceDriverFileNameW(void *image_base, LPWSTR file_name, DWORD size)
2806 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2808 if (file_name && size)
2809 file_name[0] = '\0';
2811 return 0;