kernel32: Improve stub for SetFileInformationByHandle.
[wine/multimedia.git] / dlls / kernel32 / file.c
blob9cdcd8d8c795f0d9e3431a3e2329be2c80635412
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 FINDEX_INFO_LEVELS level; /* Level passed to FindFirst */
61 UNICODE_STRING mask; /* file mask */
62 UNICODE_STRING path; /* NT path used to open the directory */
63 BOOL is_root; /* is directory the root of the drive? */
64 UINT data_pos; /* current position in dir data */
65 UINT data_len; /* length of dir data */
66 UINT data_size; /* size of data buffer, or 0 when everything has been read */
67 BYTE *data; /* directory data */
68 } FIND_FIRST_INFO;
70 #define FIND_FIRST_MAGIC 0xc0ffee11
72 static const UINT max_entry_size = offsetof( FILE_BOTH_DIRECTORY_INFORMATION, FileName[256] );
74 static BOOL oem_file_apis;
76 static const WCHAR wildcardsW[] = { '*','?',0 };
78 /***********************************************************************
79 * create_file_OF
81 * Wrapper for CreateFile that takes OF_* mode flags.
83 static HANDLE create_file_OF( LPCSTR path, INT mode )
85 DWORD access, sharing, creation;
87 if (mode & OF_CREATE)
89 creation = CREATE_ALWAYS;
90 access = GENERIC_READ | GENERIC_WRITE;
92 else
94 creation = OPEN_EXISTING;
95 switch(mode & 0x03)
97 case OF_READ: access = GENERIC_READ; break;
98 case OF_WRITE: access = GENERIC_WRITE; break;
99 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
100 default: access = 0; break;
104 switch(mode & 0x70)
106 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
107 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
108 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
109 case OF_SHARE_DENY_NONE:
110 case OF_SHARE_COMPAT:
111 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
113 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
117 /***********************************************************************
118 * check_dir_symlink
120 * Check if a dir symlink should be returned by FindNextFile.
122 static BOOL check_dir_symlink( FIND_FIRST_INFO *info, const FILE_BOTH_DIR_INFORMATION *file_info )
124 UNICODE_STRING str;
125 ANSI_STRING unix_name;
126 struct stat st, parent_st;
127 BOOL ret = TRUE;
128 DWORD len;
130 str.MaximumLength = info->path.Length + sizeof(WCHAR) + file_info->FileNameLength;
131 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength ))) return TRUE;
132 memcpy( str.Buffer, info->path.Buffer, info->path.Length );
133 len = info->path.Length / sizeof(WCHAR);
134 if (!len || str.Buffer[len-1] != '\\') str.Buffer[len++] = '\\';
135 memcpy( str.Buffer + len, file_info->FileName, file_info->FileNameLength );
136 str.Length = len * sizeof(WCHAR) + file_info->FileNameLength;
138 unix_name.Buffer = NULL;
139 if (!wine_nt_to_unix_file_name( &str, &unix_name, OPEN_EXISTING, FALSE ) &&
140 !stat( unix_name.Buffer, &st ))
142 char *p = unix_name.Buffer + unix_name.Length - 1;
144 /* skip trailing slashes */
145 while (p > unix_name.Buffer && *p == '/') p--;
147 while (ret && p > unix_name.Buffer)
149 while (p > unix_name.Buffer && *p != '/') p--;
150 while (p > unix_name.Buffer && *p == '/') p--;
151 p[1] = 0;
152 if (!stat( unix_name.Buffer, &parent_st ) &&
153 parent_st.st_dev == st.st_dev &&
154 parent_st.st_ino == st.st_ino)
156 WARN( "suppressing dir symlink %s pointing to parent %s\n",
157 debugstr_wn( str.Buffer, str.Length/sizeof(WCHAR) ),
158 debugstr_a( unix_name.Buffer ));
159 ret = FALSE;
163 RtlFreeAnsiString( &unix_name );
164 RtlFreeUnicodeString( &str );
165 return ret;
169 /***********************************************************************
170 * FILE_SetDosError
172 * Set the DOS error code from errno.
174 void FILE_SetDosError(void)
176 int save_errno = errno; /* errno gets overwritten by printf */
178 TRACE("errno = %d %s\n", errno, strerror(errno));
179 switch (save_errno)
181 case EAGAIN:
182 SetLastError( ERROR_SHARING_VIOLATION );
183 break;
184 case EBADF:
185 SetLastError( ERROR_INVALID_HANDLE );
186 break;
187 case ENOSPC:
188 SetLastError( ERROR_HANDLE_DISK_FULL );
189 break;
190 case EACCES:
191 case EPERM:
192 case EROFS:
193 SetLastError( ERROR_ACCESS_DENIED );
194 break;
195 case EBUSY:
196 SetLastError( ERROR_LOCK_VIOLATION );
197 break;
198 case ENOENT:
199 SetLastError( ERROR_FILE_NOT_FOUND );
200 break;
201 case EISDIR:
202 SetLastError( ERROR_CANNOT_MAKE );
203 break;
204 case ENFILE:
205 case EMFILE:
206 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
207 break;
208 case EEXIST:
209 SetLastError( ERROR_FILE_EXISTS );
210 break;
211 case EINVAL:
212 case ESPIPE:
213 SetLastError( ERROR_SEEK );
214 break;
215 case ENOTEMPTY:
216 SetLastError( ERROR_DIR_NOT_EMPTY );
217 break;
218 case ENOEXEC:
219 SetLastError( ERROR_BAD_FORMAT );
220 break;
221 case ENOTDIR:
222 SetLastError( ERROR_PATH_NOT_FOUND );
223 break;
224 case EXDEV:
225 SetLastError( ERROR_NOT_SAME_DEVICE );
226 break;
227 default:
228 WARN("unknown file error: %s\n", strerror(save_errno) );
229 SetLastError( ERROR_GEN_FAILURE );
230 break;
232 errno = save_errno;
236 /***********************************************************************
237 * FILE_name_AtoW
239 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
241 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
242 * there is no possibility for the function to do that twice, taking into
243 * account any called function.
245 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
247 ANSI_STRING str;
248 UNICODE_STRING strW, *pstrW;
249 NTSTATUS status;
251 RtlInitAnsiString( &str, name );
252 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
253 if (oem_file_apis)
254 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
255 else
256 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
257 if (status == STATUS_SUCCESS) return pstrW->Buffer;
259 if (status == STATUS_BUFFER_OVERFLOW)
260 SetLastError( ERROR_FILENAME_EXCED_RANGE );
261 else
262 SetLastError( RtlNtStatusToDosError(status) );
263 return NULL;
267 /***********************************************************************
268 * FILE_name_WtoA
270 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
272 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
274 DWORD ret;
276 if (srclen < 0) srclen = strlenW( src ) + 1;
277 if (oem_file_apis)
278 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
279 else
280 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
281 return ret;
285 /**************************************************************************
286 * SetFileApisToOEM (KERNEL32.@)
288 VOID WINAPI SetFileApisToOEM(void)
290 oem_file_apis = TRUE;
294 /**************************************************************************
295 * SetFileApisToANSI (KERNEL32.@)
297 VOID WINAPI SetFileApisToANSI(void)
299 oem_file_apis = FALSE;
303 /******************************************************************************
304 * AreFileApisANSI (KERNEL32.@)
306 * Determines if file functions are using ANSI
308 * RETURNS
309 * TRUE: Set of file functions is using ANSI code page
310 * FALSE: Set of file functions is using OEM code page
312 BOOL WINAPI AreFileApisANSI(void)
314 return !oem_file_apis;
318 /**************************************************************************
319 * Operations on file handles *
320 **************************************************************************/
322 /******************************************************************
323 * FILE_ReadWriteApc (internal)
325 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG reserved)
327 LPOVERLAPPED_COMPLETION_ROUTINE cr = apc_user;
329 cr(RtlNtStatusToDosError(io_status->u.Status), io_status->Information, (LPOVERLAPPED)io_status);
333 /***********************************************************************
334 * ReadFileEx (KERNEL32.@)
336 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
337 LPOVERLAPPED overlapped,
338 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
340 LARGE_INTEGER offset;
341 NTSTATUS status;
342 PIO_STATUS_BLOCK io_status;
344 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
346 if (!overlapped)
348 SetLastError(ERROR_INVALID_PARAMETER);
349 return FALSE;
352 offset.u.LowPart = overlapped->u.s.Offset;
353 offset.u.HighPart = overlapped->u.s.OffsetHigh;
354 io_status = (PIO_STATUS_BLOCK)overlapped;
355 io_status->u.Status = STATUS_PENDING;
356 io_status->Information = 0;
358 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
359 io_status, buffer, bytesToRead, &offset, NULL);
361 if (status && status != STATUS_PENDING)
363 SetLastError( RtlNtStatusToDosError(status) );
364 return FALSE;
366 return TRUE;
370 /***********************************************************************
371 * ReadFileScatter (KERNEL32.@)
373 BOOL WINAPI ReadFileScatter( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
374 LPDWORD reserved, LPOVERLAPPED overlapped )
376 PIO_STATUS_BLOCK io_status;
377 LARGE_INTEGER offset;
378 void *cvalue = NULL;
379 NTSTATUS status;
381 TRACE( "(%p %p %u %p)\n", file, segments, count, overlapped );
383 offset.u.LowPart = overlapped->u.s.Offset;
384 offset.u.HighPart = overlapped->u.s.OffsetHigh;
385 if (!((ULONG_PTR)overlapped->hEvent & 1)) cvalue = overlapped;
386 io_status = (PIO_STATUS_BLOCK)overlapped;
387 io_status->u.Status = STATUS_PENDING;
388 io_status->Information = 0;
390 status = NtReadFileScatter( file, overlapped->hEvent, NULL, cvalue, io_status,
391 segments, count, &offset, NULL );
392 if (status) SetLastError( RtlNtStatusToDosError(status) );
393 return !status;
397 /***********************************************************************
398 * ReadFile (KERNEL32.@)
400 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
401 LPDWORD bytesRead, LPOVERLAPPED overlapped )
403 LARGE_INTEGER offset;
404 PLARGE_INTEGER poffset = NULL;
405 IO_STATUS_BLOCK iosb;
406 PIO_STATUS_BLOCK io_status = &iosb;
407 HANDLE hEvent = 0;
408 NTSTATUS status;
409 LPVOID cvalue = NULL;
411 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToRead,
412 bytesRead, overlapped );
414 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
416 if (is_console_handle(hFile))
418 DWORD conread, mode;
419 if (!ReadConsoleA(hFile, buffer, bytesToRead, &conread, NULL) ||
420 !GetConsoleMode(hFile, &mode))
421 return FALSE;
422 /* ctrl-Z (26) means end of file on window (if at beginning of buffer)
423 * but Unix uses ctrl-D (4), and ctrl-Z is a bad idea on Unix :-/
424 * So map both ctrl-D ctrl-Z to EOF.
426 if ((mode & ENABLE_PROCESSED_INPUT) && conread > 0 &&
427 (((char*)buffer)[0] == 26 || ((char*)buffer)[0] == 4))
429 conread = 0;
431 if (bytesRead) *bytesRead = conread;
432 return TRUE;
435 if (overlapped != NULL)
437 offset.u.LowPart = overlapped->u.s.Offset;
438 offset.u.HighPart = overlapped->u.s.OffsetHigh;
439 poffset = &offset;
440 hEvent = overlapped->hEvent;
441 io_status = (PIO_STATUS_BLOCK)overlapped;
442 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
444 io_status->u.Status = STATUS_PENDING;
445 io_status->Information = 0;
447 status = NtReadFile(hFile, hEvent, NULL, cvalue, io_status, buffer, bytesToRead, poffset, NULL);
449 if (status == STATUS_PENDING && !overlapped)
451 WaitForSingleObject( hFile, INFINITE );
452 status = io_status->u.Status;
455 if (status != STATUS_PENDING && bytesRead)
456 *bytesRead = io_status->Information;
458 if (status == STATUS_END_OF_FILE)
460 if (overlapped != NULL)
462 SetLastError( RtlNtStatusToDosError(status) );
463 return FALSE;
466 else if (status && status != STATUS_TIMEOUT)
468 SetLastError( RtlNtStatusToDosError(status) );
469 return FALSE;
471 return TRUE;
475 /***********************************************************************
476 * WriteFileEx (KERNEL32.@)
478 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
479 LPOVERLAPPED overlapped,
480 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
482 LARGE_INTEGER offset;
483 NTSTATUS status;
484 PIO_STATUS_BLOCK io_status;
486 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
488 if (overlapped == NULL)
490 SetLastError(ERROR_INVALID_PARAMETER);
491 return FALSE;
493 offset.u.LowPart = overlapped->u.s.Offset;
494 offset.u.HighPart = overlapped->u.s.OffsetHigh;
496 io_status = (PIO_STATUS_BLOCK)overlapped;
497 io_status->u.Status = STATUS_PENDING;
498 io_status->Information = 0;
500 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
501 io_status, buffer, bytesToWrite, &offset, NULL);
503 if (status && status != STATUS_PENDING)
505 SetLastError( RtlNtStatusToDosError(status) );
506 return FALSE;
508 return TRUE;
512 /***********************************************************************
513 * WriteFileGather (KERNEL32.@)
515 BOOL WINAPI WriteFileGather( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
516 LPDWORD reserved, LPOVERLAPPED overlapped )
518 PIO_STATUS_BLOCK io_status;
519 LARGE_INTEGER offset;
520 void *cvalue = NULL;
521 NTSTATUS status;
523 TRACE( "%p %p %u %p\n", file, segments, count, overlapped );
525 offset.u.LowPart = overlapped->u.s.Offset;
526 offset.u.HighPart = overlapped->u.s.OffsetHigh;
527 if (!((ULONG_PTR)overlapped->hEvent & 1)) cvalue = overlapped;
528 io_status = (PIO_STATUS_BLOCK)overlapped;
529 io_status->u.Status = STATUS_PENDING;
530 io_status->Information = 0;
532 status = NtWriteFileGather( file, overlapped->hEvent, NULL, cvalue, io_status,
533 segments, count, &offset, NULL );
534 if (status) SetLastError( RtlNtStatusToDosError(status) );
535 return !status;
539 /***********************************************************************
540 * WriteFile (KERNEL32.@)
542 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
543 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
545 HANDLE hEvent = NULL;
546 LARGE_INTEGER offset;
547 PLARGE_INTEGER poffset = NULL;
548 NTSTATUS status;
549 IO_STATUS_BLOCK iosb;
550 PIO_STATUS_BLOCK piosb = &iosb;
551 LPVOID cvalue = NULL;
553 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
555 if (is_console_handle(hFile))
556 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
558 if (overlapped)
560 offset.u.LowPart = overlapped->u.s.Offset;
561 offset.u.HighPart = overlapped->u.s.OffsetHigh;
562 poffset = &offset;
563 hEvent = overlapped->hEvent;
564 piosb = (PIO_STATUS_BLOCK)overlapped;
565 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
567 piosb->u.Status = STATUS_PENDING;
568 piosb->Information = 0;
570 status = NtWriteFile(hFile, hEvent, NULL, cvalue, piosb,
571 buffer, bytesToWrite, poffset, NULL);
573 if (status == STATUS_PENDING && !overlapped)
575 WaitForSingleObject( hFile, INFINITE );
576 status = piosb->u.Status;
579 if (status != STATUS_PENDING && bytesWritten)
580 *bytesWritten = piosb->Information;
582 if (status && status != STATUS_TIMEOUT)
584 SetLastError( RtlNtStatusToDosError(status) );
585 return FALSE;
587 return TRUE;
591 /***********************************************************************
592 * GetOverlappedResult (KERNEL32.@)
594 * Check the result of an Asynchronous data transfer from a file.
596 * Parameters
597 * HANDLE hFile [in] handle of file to check on
598 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
599 * LPDWORD lpTransferred [in/out] number of bytes transferred
600 * BOOL bWait [in] wait for the transfer to complete ?
602 * RETURNS
603 * TRUE on success
604 * FALSE on failure
606 * If successful (and relevant) lpTransferred will hold the number of
607 * bytes transferred during the async operation.
609 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
610 LPDWORD lpTransferred, BOOL bWait)
612 NTSTATUS status;
614 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
616 status = lpOverlapped->Internal;
617 if (status == STATUS_PENDING)
619 if (!bWait)
621 SetLastError( ERROR_IO_INCOMPLETE );
622 return FALSE;
625 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : hFile,
626 INFINITE ) == WAIT_FAILED)
627 return FALSE;
628 status = lpOverlapped->Internal;
631 *lpTransferred = lpOverlapped->InternalHigh;
633 if (status) SetLastError( RtlNtStatusToDosError(status) );
634 return !status;
637 /***********************************************************************
638 * CancelIoEx (KERNEL32.@)
640 * Cancels pending I/O operations on a file given the overlapped used.
642 * PARAMS
643 * handle [I] File handle.
644 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
646 * RETURNS
647 * Success: TRUE.
648 * Failure: FALSE, check GetLastError().
650 BOOL WINAPI CancelIoEx(HANDLE handle, LPOVERLAPPED lpOverlapped)
652 IO_STATUS_BLOCK io_status;
654 NtCancelIoFileEx(handle, (PIO_STATUS_BLOCK) lpOverlapped, &io_status);
655 if (io_status.u.Status)
657 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
658 return FALSE;
660 return TRUE;
663 /***********************************************************************
664 * CancelIo (KERNEL32.@)
666 * Cancels pending I/O operations initiated by the current thread on a file.
668 * PARAMS
669 * handle [I] File handle.
671 * RETURNS
672 * Success: TRUE.
673 * Failure: FALSE, check GetLastError().
675 BOOL WINAPI CancelIo(HANDLE handle)
677 IO_STATUS_BLOCK io_status;
679 NtCancelIoFile(handle, &io_status);
680 if (io_status.u.Status)
682 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
683 return FALSE;
685 return TRUE;
688 /***********************************************************************
689 * _hread (KERNEL32.@)
691 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
693 return _lread( hFile, buffer, count );
697 /***********************************************************************
698 * _hwrite (KERNEL32.@)
700 * experimentation yields that _lwrite:
701 * o truncates the file at the current position with
702 * a 0 len write
703 * o returns 0 on a 0 length write
704 * o works with console handles
707 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
709 DWORD result;
711 TRACE("%d %p %d\n", handle, buffer, count );
713 if (!count)
715 /* Expand or truncate at current position */
716 if (!SetEndOfFile( LongToHandle(handle) )) return HFILE_ERROR;
717 return 0;
719 if (!WriteFile( LongToHandle(handle), buffer, count, &result, NULL ))
720 return HFILE_ERROR;
721 return result;
725 /***********************************************************************
726 * _lclose (KERNEL32.@)
728 HFILE WINAPI _lclose( HFILE hFile )
730 TRACE("handle %d\n", hFile );
731 return CloseHandle( LongToHandle(hFile) ) ? 0 : HFILE_ERROR;
735 /***********************************************************************
736 * _lcreat (KERNEL32.@)
738 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
740 HANDLE hfile;
742 /* Mask off all flags not explicitly allowed by the doc */
743 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
744 TRACE("%s %02x\n", path, attr );
745 hfile = CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
746 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
747 CREATE_ALWAYS, attr, 0 );
748 return HandleToLong(hfile);
752 /***********************************************************************
753 * _lopen (KERNEL32.@)
755 HFILE WINAPI _lopen( LPCSTR path, INT mode )
757 HANDLE hfile;
759 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
760 hfile = create_file_OF( path, mode & ~OF_CREATE );
761 return HandleToLong(hfile);
764 /***********************************************************************
765 * _lread (KERNEL32.@)
767 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
769 DWORD result;
770 if (!ReadFile( LongToHandle(handle), buffer, count, &result, NULL ))
771 return HFILE_ERROR;
772 return result;
776 /***********************************************************************
777 * _llseek (KERNEL32.@)
779 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
781 return SetFilePointer( LongToHandle(hFile), lOffset, NULL, nOrigin );
785 /***********************************************************************
786 * _lwrite (KERNEL32.@)
788 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
790 return (UINT)_hwrite( hFile, buffer, (LONG)count );
794 /***********************************************************************
795 * FlushFileBuffers (KERNEL32.@)
797 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
799 NTSTATUS nts;
800 IO_STATUS_BLOCK ioblk;
802 if (is_console_handle( hFile ))
804 /* this will fail (as expected) for an output handle */
805 return FlushConsoleInputBuffer( hFile );
807 nts = NtFlushBuffersFile( hFile, &ioblk );
808 if (nts != STATUS_SUCCESS)
810 SetLastError( RtlNtStatusToDosError( nts ) );
811 return FALSE;
814 return TRUE;
818 /***********************************************************************
819 * GetFileType (KERNEL32.@)
821 DWORD WINAPI GetFileType( HANDLE hFile )
823 FILE_FS_DEVICE_INFORMATION info;
824 IO_STATUS_BLOCK io;
825 NTSTATUS status;
827 if (hFile == (HANDLE)STD_INPUT_HANDLE || hFile == (HANDLE)STD_OUTPUT_HANDLE
828 || hFile == (HANDLE)STD_ERROR_HANDLE)
829 hFile = GetStdHandle((DWORD_PTR)hFile);
831 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
833 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
834 if (status != STATUS_SUCCESS)
836 SetLastError( RtlNtStatusToDosError(status) );
837 return FILE_TYPE_UNKNOWN;
840 switch(info.DeviceType)
842 case FILE_DEVICE_NULL:
843 case FILE_DEVICE_SERIAL_PORT:
844 case FILE_DEVICE_PARALLEL_PORT:
845 case FILE_DEVICE_TAPE:
846 case FILE_DEVICE_UNKNOWN:
847 return FILE_TYPE_CHAR;
848 case FILE_DEVICE_NAMED_PIPE:
849 return FILE_TYPE_PIPE;
850 default:
851 return FILE_TYPE_DISK;
856 /***********************************************************************
857 * GetFileInformationByHandle (KERNEL32.@)
859 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
861 FILE_ALL_INFORMATION all_info;
862 IO_STATUS_BLOCK io;
863 NTSTATUS status;
865 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
866 if (status == STATUS_BUFFER_OVERFLOW) status = STATUS_SUCCESS;
867 if (status == STATUS_SUCCESS)
869 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
870 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
871 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
872 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
873 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
874 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
875 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
876 info->dwVolumeSerialNumber = 0; /* FIXME */
877 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
878 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
879 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
880 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
881 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
882 return TRUE;
884 SetLastError( RtlNtStatusToDosError(status) );
885 return FALSE;
889 /***********************************************************************
890 * GetFileInformationByHandleEx (KERNEL32.@)
892 BOOL WINAPI GetFileInformationByHandleEx( HANDLE handle, FILE_INFO_BY_HANDLE_CLASS class,
893 LPVOID info, DWORD size )
895 NTSTATUS status;
896 IO_STATUS_BLOCK io;
898 switch (class)
900 case FileStreamInfo:
901 case FileCompressionInfo:
902 case FileAttributeTagInfo:
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 FileBasicInfo:
916 status = NtQueryInformationFile( handle, &io, info, size, FileBasicInformation );
917 break;
919 case FileStandardInfo:
920 status = NtQueryInformationFile( handle, &io, info, size, FileStandardInformation );
921 break;
923 case FileNameInfo:
924 status = NtQueryInformationFile( handle, &io, info, size, FileNameInformation );
925 break;
927 case FileIdBothDirectoryRestartInfo:
928 case FileIdBothDirectoryInfo:
929 status = NtQueryDirectoryFile( handle, NULL, NULL, NULL, &io, info, size,
930 FileIdBothDirectoryInformation, FALSE, NULL,
931 (class == FileIdBothDirectoryRestartInfo) );
932 break;
934 case FileRenameInfo:
935 case FileDispositionInfo:
936 case FileAllocationInfo:
937 case FileIoPriorityHintInfo:
938 case FileEndOfFileInfo:
939 default:
940 SetLastError( ERROR_INVALID_PARAMETER );
941 return FALSE;
944 if (status != STATUS_SUCCESS)
946 SetLastError( RtlNtStatusToDosError( status ) );
947 return FALSE;
949 return TRUE;
953 /***********************************************************************
954 * GetFileSize (KERNEL32.@)
956 * Retrieve the size of a file.
958 * PARAMS
959 * hFile [I] File to retrieve size of.
960 * filesizehigh [O] On return, the high bits of the file size.
962 * RETURNS
963 * Success: The low bits of the file size.
964 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
965 * check GetLastError() for values other than ERROR_SUCCESS.
967 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
969 LARGE_INTEGER size;
970 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
971 if (filesizehigh) *filesizehigh = size.u.HighPart;
972 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
973 return size.u.LowPart;
977 /***********************************************************************
978 * GetFileSizeEx (KERNEL32.@)
980 * Retrieve the size of a file.
982 * PARAMS
983 * hFile [I] File to retrieve size of.
984 * lpFileSIze [O] On return, the size of the file.
986 * RETURNS
987 * Success: TRUE.
988 * Failure: FALSE, check GetLastError().
990 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
992 FILE_STANDARD_INFORMATION info;
993 IO_STATUS_BLOCK io;
994 NTSTATUS status;
996 if (is_console_handle( hFile ))
998 SetLastError( ERROR_INVALID_HANDLE );
999 return FALSE;
1002 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileStandardInformation );
1003 if (status == STATUS_SUCCESS)
1005 *lpFileSize = info.EndOfFile;
1006 return TRUE;
1008 SetLastError( RtlNtStatusToDosError(status) );
1009 return FALSE;
1013 /**************************************************************************
1014 * SetEndOfFile (KERNEL32.@)
1016 * Sets the current position as the end of the file.
1018 * PARAMS
1019 * hFile [I] File handle.
1021 * RETURNS
1022 * Success: TRUE.
1023 * Failure: FALSE, check GetLastError().
1025 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1027 FILE_POSITION_INFORMATION pos;
1028 FILE_END_OF_FILE_INFORMATION eof;
1029 IO_STATUS_BLOCK io;
1030 NTSTATUS status;
1032 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
1033 if (status == STATUS_SUCCESS)
1035 eof.EndOfFile = pos.CurrentByteOffset;
1036 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
1038 if (status == STATUS_SUCCESS) return TRUE;
1039 SetLastError( RtlNtStatusToDosError(status) );
1040 return FALSE;
1044 /***********************************************************************
1045 * SetFileInformationByHandle (KERNEL32.@)
1047 BOOL WINAPI SetFileInformationByHandle( HANDLE file, FILE_INFO_BY_HANDLE_CLASS class, VOID *info, DWORD size )
1049 TRACE( "%p %u %p %u\n", file, class, info, size );
1051 switch (class)
1053 case FileBasicInfo:
1054 case FileNameInfo:
1055 case FileRenameInfo:
1056 case FileDispositionInfo:
1057 case FileAllocationInfo:
1058 case FileEndOfFileInfo:
1059 case FileStreamInfo:
1060 case FileIdBothDirectoryInfo:
1061 case FileIdBothDirectoryRestartInfo:
1062 case FileIoPriorityHintInfo:
1063 case FileFullDirectoryInfo:
1064 case FileFullDirectoryRestartInfo:
1065 case FileStorageInfo:
1066 case FileAlignmentInfo:
1067 case FileIdInfo:
1068 case FileIdExtdDirectoryInfo:
1069 case FileIdExtdDirectoryRestartInfo:
1070 FIXME( "%p, %u, %p, %u\n", file, class, info, size );
1071 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1072 return FALSE;
1074 case FileStandardInfo:
1075 case FileCompressionInfo:
1076 case FileAttributeTagInfo:
1077 case FileRemoteProtocolInfo:
1078 default:
1079 SetLastError( ERROR_INVALID_PARAMETER );
1080 return FALSE;
1083 return TRUE;
1087 /***********************************************************************
1088 * SetFilePointer (KERNEL32.@)
1090 DWORD WINAPI DECLSPEC_HOTPATCH SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
1092 LARGE_INTEGER dist, newpos;
1094 if (highword)
1096 dist.u.LowPart = distance;
1097 dist.u.HighPart = *highword;
1099 else dist.QuadPart = distance;
1101 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
1103 if (highword) *highword = newpos.u.HighPart;
1104 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
1105 return newpos.u.LowPart;
1109 /***********************************************************************
1110 * SetFilePointerEx (KERNEL32.@)
1112 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
1113 LARGE_INTEGER *newpos, DWORD method )
1115 LONGLONG pos;
1116 IO_STATUS_BLOCK io;
1117 FILE_POSITION_INFORMATION info;
1119 switch(method)
1121 case FILE_BEGIN:
1122 pos = distance.QuadPart;
1123 break;
1124 case FILE_CURRENT:
1125 if (NtQueryInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1126 goto error;
1127 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
1128 break;
1129 case FILE_END:
1131 FILE_END_OF_FILE_INFORMATION eof;
1132 if (NtQueryInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
1133 goto error;
1134 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
1136 break;
1137 default:
1138 SetLastError( ERROR_INVALID_PARAMETER );
1139 return FALSE;
1142 if (pos < 0)
1144 SetLastError( ERROR_NEGATIVE_SEEK );
1145 return FALSE;
1148 info.CurrentByteOffset.QuadPart = pos;
1149 if (NtSetInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
1150 goto error;
1151 if (newpos) newpos->QuadPart = pos;
1152 return TRUE;
1154 error:
1155 SetLastError( RtlNtStatusToDosError(io.u.Status) );
1156 return FALSE;
1159 /***********************************************************************
1160 * SetFileValidData (KERNEL32.@)
1162 BOOL WINAPI SetFileValidData( HANDLE hFile, LONGLONG ValidDataLength )
1164 FILE_VALID_DATA_LENGTH_INFORMATION info;
1165 IO_STATUS_BLOCK io;
1166 NTSTATUS status;
1168 info.ValidDataLength.QuadPart = ValidDataLength;
1169 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileValidDataLengthInformation );
1171 if (status == STATUS_SUCCESS) return TRUE;
1172 SetLastError( RtlNtStatusToDosError(status) );
1173 return FALSE;
1176 /***********************************************************************
1177 * GetFileTime (KERNEL32.@)
1179 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
1180 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1182 FILE_BASIC_INFORMATION info;
1183 IO_STATUS_BLOCK io;
1184 NTSTATUS status;
1186 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1187 if (status == STATUS_SUCCESS)
1189 if (lpCreationTime)
1191 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
1192 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
1194 if (lpLastAccessTime)
1196 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
1197 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
1199 if (lpLastWriteTime)
1201 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
1202 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1204 return TRUE;
1206 SetLastError( RtlNtStatusToDosError(status) );
1207 return FALSE;
1211 /***********************************************************************
1212 * SetFileTime (KERNEL32.@)
1214 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1215 const FILETIME *atime, const FILETIME *mtime )
1217 FILE_BASIC_INFORMATION info;
1218 IO_STATUS_BLOCK io;
1219 NTSTATUS status;
1221 memset( &info, 0, sizeof(info) );
1222 if (ctime)
1224 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1225 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1227 if (atime)
1229 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1230 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1232 if (mtime)
1234 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1235 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1238 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1239 if (status == STATUS_SUCCESS) return TRUE;
1240 SetLastError( RtlNtStatusToDosError(status) );
1241 return FALSE;
1245 /**************************************************************************
1246 * LockFile (KERNEL32.@)
1248 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1249 DWORD count_low, DWORD count_high )
1251 NTSTATUS status;
1252 LARGE_INTEGER count, offset;
1254 TRACE( "%p %x%08x %x%08x\n",
1255 hFile, offset_high, offset_low, count_high, count_low );
1257 count.u.LowPart = count_low;
1258 count.u.HighPart = count_high;
1259 offset.u.LowPart = offset_low;
1260 offset.u.HighPart = offset_high;
1262 status = NtLockFile( hFile, 0, NULL, NULL,
1263 NULL, &offset, &count, NULL, TRUE, TRUE );
1265 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1266 return !status;
1270 /**************************************************************************
1271 * LockFileEx [KERNEL32.@]
1273 * Locks a byte range within an open file for shared or exclusive access.
1275 * RETURNS
1276 * success: TRUE
1277 * failure: FALSE
1279 * NOTES
1280 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1282 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1283 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1285 NTSTATUS status;
1286 LARGE_INTEGER count, offset;
1287 LPVOID cvalue = NULL;
1289 if (reserved)
1291 SetLastError( ERROR_INVALID_PARAMETER );
1292 return FALSE;
1295 TRACE( "%p %x%08x %x%08x flags %x\n",
1296 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1297 count_high, count_low, flags );
1299 count.u.LowPart = count_low;
1300 count.u.HighPart = count_high;
1301 offset.u.LowPart = overlapped->u.s.Offset;
1302 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1304 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1306 status = NtLockFile( hFile, overlapped->hEvent, NULL, cvalue,
1307 NULL, &offset, &count, NULL,
1308 flags & LOCKFILE_FAIL_IMMEDIATELY,
1309 flags & LOCKFILE_EXCLUSIVE_LOCK );
1311 if (status) SetLastError( RtlNtStatusToDosError(status) );
1312 return !status;
1316 /**************************************************************************
1317 * UnlockFile (KERNEL32.@)
1319 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1320 DWORD count_low, DWORD count_high )
1322 NTSTATUS status;
1323 LARGE_INTEGER count, offset;
1325 count.u.LowPart = count_low;
1326 count.u.HighPart = count_high;
1327 offset.u.LowPart = offset_low;
1328 offset.u.HighPart = offset_high;
1330 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1331 if (status) SetLastError( RtlNtStatusToDosError(status) );
1332 return !status;
1336 /**************************************************************************
1337 * UnlockFileEx (KERNEL32.@)
1339 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1340 LPOVERLAPPED overlapped )
1342 if (reserved)
1344 SetLastError( ERROR_INVALID_PARAMETER );
1345 return FALSE;
1347 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1349 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1353 /*************************************************************************
1354 * SetHandleCount (KERNEL32.@)
1356 UINT WINAPI SetHandleCount( UINT count )
1358 return count;
1362 /**************************************************************************
1363 * Operations on file names *
1364 **************************************************************************/
1367 /*************************************************************************
1368 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1370 * Creates or opens an object, and returns a handle that can be used to
1371 * access that object.
1373 * PARAMS
1375 * filename [in] pointer to filename to be accessed
1376 * access [in] access mode requested
1377 * sharing [in] share mode
1378 * sa [in] pointer to security attributes
1379 * creation [in] how to create the file
1380 * attributes [in] attributes for newly created file
1381 * template [in] handle to file with extended attributes to copy
1383 * RETURNS
1384 * Success: Open handle to specified file
1385 * Failure: INVALID_HANDLE_VALUE
1387 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1388 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1389 DWORD attributes, HANDLE template )
1391 NTSTATUS status;
1392 UINT options;
1393 OBJECT_ATTRIBUTES attr;
1394 UNICODE_STRING nameW;
1395 IO_STATUS_BLOCK io;
1396 HANDLE ret;
1397 DWORD dosdev;
1398 const WCHAR *vxd_name = NULL;
1399 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1400 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1401 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1402 SECURITY_QUALITY_OF_SERVICE qos;
1404 static const UINT nt_disposition[5] =
1406 FILE_CREATE, /* CREATE_NEW */
1407 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1408 FILE_OPEN, /* OPEN_EXISTING */
1409 FILE_OPEN_IF, /* OPEN_ALWAYS */
1410 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1414 /* sanity checks */
1416 if (!filename || !filename[0])
1418 SetLastError( ERROR_PATH_NOT_FOUND );
1419 return INVALID_HANDLE_VALUE;
1422 TRACE("%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1423 (access & GENERIC_READ)?"GENERIC_READ ":"",
1424 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1425 (access & GENERIC_EXECUTE)?"GENERIC_EXECUTE ":"",
1426 (!access)?"QUERY_ACCESS ":"",
1427 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1428 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1429 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1430 creation, attributes);
1432 /* Open a console for CONIN$ or CONOUT$ */
1434 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1436 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle),
1437 creation ? OPEN_EXISTING : 0);
1438 if (ret == INVALID_HANDLE_VALUE) SetLastError(ERROR_INVALID_PARAMETER);
1439 goto done;
1442 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1444 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1445 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1447 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1448 !strncmpiW( filename + 4, pipeW, 5 ) ||
1449 !strncmpiW( filename + 4, mailslotW, 9 ))
1451 dosdev = 0;
1453 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1455 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1457 else if (GetVersion() & 0x80000000)
1459 vxd_name = filename + 4;
1460 if (!creation) creation = OPEN_EXISTING;
1463 else dosdev = RtlIsDosDeviceName_U( filename );
1465 if (dosdev)
1467 static const WCHAR conW[] = {'C','O','N'};
1469 if (LOWORD(dosdev) == sizeof(conW) &&
1470 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1472 switch (access & (GENERIC_READ|GENERIC_WRITE))
1474 case GENERIC_READ:
1475 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1476 goto done;
1477 case GENERIC_WRITE:
1478 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), OPEN_EXISTING);
1479 goto done;
1480 default:
1481 SetLastError( ERROR_FILE_NOT_FOUND );
1482 return INVALID_HANDLE_VALUE;
1487 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1489 SetLastError( ERROR_INVALID_PARAMETER );
1490 return INVALID_HANDLE_VALUE;
1493 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1495 SetLastError( ERROR_PATH_NOT_FOUND );
1496 return INVALID_HANDLE_VALUE;
1499 /* now call NtCreateFile */
1501 options = 0;
1502 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1503 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1504 else
1505 options |= FILE_NON_DIRECTORY_FILE;
1506 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1508 options |= FILE_DELETE_ON_CLOSE;
1509 access |= DELETE;
1511 if (attributes & FILE_FLAG_NO_BUFFERING)
1512 options |= FILE_NO_INTERMEDIATE_BUFFERING;
1513 if (!(attributes & FILE_FLAG_OVERLAPPED))
1514 options |= FILE_SYNCHRONOUS_IO_NONALERT;
1515 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1516 options |= FILE_RANDOM_ACCESS;
1517 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1519 attr.Length = sizeof(attr);
1520 attr.RootDirectory = 0;
1521 attr.Attributes = OBJ_CASE_INSENSITIVE;
1522 attr.ObjectName = &nameW;
1523 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1524 if (attributes & SECURITY_SQOS_PRESENT)
1526 qos.Length = sizeof(qos);
1527 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
1528 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
1529 qos.EffectiveOnly = (attributes & SECURITY_EFFECTIVE_ONLY) != 0;
1530 attr.SecurityQualityOfService = &qos;
1532 else
1533 attr.SecurityQualityOfService = NULL;
1535 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1537 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1538 sharing, nt_disposition[creation - CREATE_NEW],
1539 options, NULL, 0 );
1540 if (status)
1542 if (vxd_name && vxd_name[0])
1544 static HANDLE (*vxd_open)(LPCWSTR,DWORD,SECURITY_ATTRIBUTES*);
1545 if (!vxd_open) vxd_open = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
1546 "__wine_vxd_open" );
1547 if (vxd_open && (ret = vxd_open( vxd_name, access, sa ))) goto done;
1550 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1551 ret = INVALID_HANDLE_VALUE;
1553 /* In the case file creation was rejected due to CREATE_NEW flag
1554 * was specified and file with that name already exists, correct
1555 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1556 * Note: RtlNtStatusToDosError is not the subject to blame here.
1558 if (status == STATUS_OBJECT_NAME_COLLISION)
1559 SetLastError( ERROR_FILE_EXISTS );
1560 else
1561 SetLastError( RtlNtStatusToDosError(status) );
1563 else
1565 if ((creation == CREATE_ALWAYS && io.Information == FILE_OVERWRITTEN) ||
1566 (creation == OPEN_ALWAYS && io.Information == FILE_OPENED))
1567 SetLastError( ERROR_ALREADY_EXISTS );
1568 else
1569 SetLastError( 0 );
1571 RtlFreeUnicodeString( &nameW );
1573 done:
1574 if (!ret) ret = INVALID_HANDLE_VALUE;
1575 TRACE("returning %p\n", ret);
1576 return ret;
1581 /*************************************************************************
1582 * CreateFileA (KERNEL32.@)
1584 * See CreateFileW.
1586 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1587 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1588 DWORD attributes, HANDLE template)
1590 WCHAR *nameW;
1592 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1593 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1596 /*************************************************************************
1597 * CreateFile2 (KERNEL32.@)
1599 HANDLE WINAPI CreateFile2( LPCWSTR filename, DWORD access, DWORD sharing, DWORD creation,
1600 CREATEFILE2_EXTENDED_PARAMETERS *exparams )
1602 LPSECURITY_ATTRIBUTES sa = exparams ? exparams->lpSecurityAttributes : NULL;
1603 DWORD attributes = exparams ? exparams->dwFileAttributes : 0;
1604 HANDLE template = exparams ? exparams->hTemplateFile : NULL;
1606 FIXME("(%s %x %x %x %p), partial stub\n", debugstr_w(filename), access, sharing, creation, exparams);
1608 return CreateFileW( filename, access, sharing, sa, creation, attributes, template );
1611 /***********************************************************************
1612 * DeleteFileW (KERNEL32.@)
1614 * Delete a file.
1616 * PARAMS
1617 * path [I] Path to the file to delete.
1619 * RETURNS
1620 * Success: TRUE.
1621 * Failure: FALSE, check GetLastError().
1623 BOOL WINAPI DeleteFileW( LPCWSTR path )
1625 UNICODE_STRING nameW;
1626 OBJECT_ATTRIBUTES attr;
1627 NTSTATUS status;
1628 HANDLE hFile;
1629 IO_STATUS_BLOCK io;
1631 TRACE("%s\n", debugstr_w(path) );
1633 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1635 SetLastError( ERROR_PATH_NOT_FOUND );
1636 return FALSE;
1639 attr.Length = sizeof(attr);
1640 attr.RootDirectory = 0;
1641 attr.Attributes = OBJ_CASE_INSENSITIVE;
1642 attr.ObjectName = &nameW;
1643 attr.SecurityDescriptor = NULL;
1644 attr.SecurityQualityOfService = NULL;
1646 status = NtCreateFile(&hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
1647 &attr, &io, NULL, 0,
1648 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1649 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0);
1650 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1652 RtlFreeUnicodeString( &nameW );
1653 if (status)
1655 SetLastError( RtlNtStatusToDosError(status) );
1656 return FALSE;
1658 return TRUE;
1662 /***********************************************************************
1663 * DeleteFileA (KERNEL32.@)
1665 * See DeleteFileW.
1667 BOOL WINAPI DeleteFileA( LPCSTR path )
1669 WCHAR *pathW;
1671 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1672 return DeleteFileW( pathW );
1676 /**************************************************************************
1677 * ReplaceFileW (KERNEL32.@)
1678 * ReplaceFile (KERNEL32.@)
1680 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName, LPCWSTR lpReplacementFileName,
1681 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1682 LPVOID lpExclude, LPVOID lpReserved)
1684 UNICODE_STRING nt_replaced_name, nt_replacement_name;
1685 ANSI_STRING unix_replaced_name, unix_replacement_name, unix_backup_name;
1686 HANDLE hReplaced = NULL, hReplacement = NULL, hBackup = NULL;
1687 DWORD error = ERROR_SUCCESS;
1688 UINT replaced_flags;
1689 BOOL ret = FALSE;
1690 NTSTATUS status;
1691 IO_STATUS_BLOCK io;
1692 OBJECT_ATTRIBUTES attr;
1694 TRACE("%s %s %s 0x%08x %p %p\n", debugstr_w(lpReplacedFileName),
1695 debugstr_w(lpReplacementFileName), debugstr_w(lpBackupFileName),
1696 dwReplaceFlags, lpExclude, lpReserved);
1698 if (dwReplaceFlags)
1699 FIXME("Ignoring flags %x\n", dwReplaceFlags);
1701 /* First two arguments are mandatory */
1702 if (!lpReplacedFileName || !lpReplacementFileName)
1704 SetLastError(ERROR_INVALID_PARAMETER);
1705 return FALSE;
1708 unix_replaced_name.Buffer = NULL;
1709 unix_replacement_name.Buffer = NULL;
1710 unix_backup_name.Buffer = NULL;
1712 attr.Length = sizeof(attr);
1713 attr.RootDirectory = 0;
1714 attr.Attributes = OBJ_CASE_INSENSITIVE;
1715 attr.ObjectName = NULL;
1716 attr.SecurityDescriptor = NULL;
1717 attr.SecurityQualityOfService = NULL;
1719 /* Open the "replaced" file for reading and writing */
1720 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName, &nt_replaced_name, NULL, NULL)))
1722 error = ERROR_PATH_NOT_FOUND;
1723 goto fail;
1725 replaced_flags = lpBackupFileName ? FILE_OPEN : FILE_OPEN_IF;
1726 attr.ObjectName = &nt_replaced_name;
1727 status = NtOpenFile(&hReplaced, GENERIC_READ|GENERIC_WRITE|DELETE|SYNCHRONIZE,
1728 &attr, &io,
1729 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1730 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1731 if (status == STATUS_SUCCESS)
1732 status = wine_nt_to_unix_file_name(&nt_replaced_name, &unix_replaced_name, replaced_flags, FALSE);
1733 RtlFreeUnicodeString(&nt_replaced_name);
1734 if (status != STATUS_SUCCESS)
1736 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1737 error = ERROR_FILE_NOT_FOUND;
1738 else
1739 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1740 goto fail;
1744 * Open the replacement file for reading, writing, and deleting
1745 * (writing and deleting are needed when finished)
1747 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName, &nt_replacement_name, NULL, NULL)))
1749 error = ERROR_PATH_NOT_FOUND;
1750 goto fail;
1752 attr.ObjectName = &nt_replacement_name;
1753 status = NtOpenFile(&hReplacement,
1754 GENERIC_READ|GENERIC_WRITE|DELETE|WRITE_DAC|SYNCHRONIZE,
1755 &attr, &io, 0,
1756 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1757 if (status == STATUS_SUCCESS)
1758 status = wine_nt_to_unix_file_name(&nt_replacement_name, &unix_replacement_name, FILE_OPEN, FALSE);
1759 RtlFreeUnicodeString(&nt_replacement_name);
1760 if (status != STATUS_SUCCESS)
1762 error = RtlNtStatusToDosError(status);
1763 goto fail;
1766 /* If the user wants a backup then that needs to be performed first */
1767 if (lpBackupFileName)
1769 UNICODE_STRING nt_backup_name;
1770 FILE_BASIC_INFORMATION replaced_info;
1772 /* Obtain the file attributes from the "replaced" file */
1773 status = NtQueryInformationFile(hReplaced, &io, &replaced_info,
1774 sizeof(replaced_info),
1775 FileBasicInformation);
1776 if (status != STATUS_SUCCESS)
1778 error = RtlNtStatusToDosError(status);
1779 goto fail;
1782 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName, &nt_backup_name, NULL, NULL)))
1784 error = ERROR_PATH_NOT_FOUND;
1785 goto fail;
1787 attr.ObjectName = &nt_backup_name;
1788 /* Open the backup with permissions to write over it */
1789 status = NtCreateFile(&hBackup, GENERIC_WRITE,
1790 &attr, &io, NULL, replaced_info.FileAttributes,
1791 FILE_SHARE_WRITE, FILE_OPEN_IF,
1792 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE,
1793 NULL, 0);
1794 if (status == STATUS_SUCCESS)
1795 status = wine_nt_to_unix_file_name(&nt_backup_name, &unix_backup_name, FILE_OPEN_IF, FALSE);
1796 RtlFreeUnicodeString(&nt_backup_name);
1797 if (status != STATUS_SUCCESS)
1799 error = RtlNtStatusToDosError(status);
1800 goto fail;
1803 /* If an existing backup exists then copy over it */
1804 if (rename(unix_replaced_name.Buffer, unix_backup_name.Buffer) == -1)
1806 error = ERROR_UNABLE_TO_REMOVE_REPLACED; /* is this correct? */
1807 goto fail;
1812 * Now that the backup has been performed (if requested), copy the replacement
1813 * into place
1815 if (rename(unix_replacement_name.Buffer, unix_replaced_name.Buffer) == -1)
1817 if (errno == EACCES)
1819 /* Inappropriate permissions on "replaced", rename will fail */
1820 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1821 goto fail;
1823 /* on failure we need to indicate whether a backup was made */
1824 if (!lpBackupFileName)
1825 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT;
1826 else
1827 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT_2;
1828 goto fail;
1830 /* Success! */
1831 ret = TRUE;
1833 /* Perform resource cleanup */
1834 fail:
1835 if (hBackup) CloseHandle(hBackup);
1836 if (hReplaced) CloseHandle(hReplaced);
1837 if (hReplacement) CloseHandle(hReplacement);
1838 RtlFreeAnsiString(&unix_backup_name);
1839 RtlFreeAnsiString(&unix_replacement_name);
1840 RtlFreeAnsiString(&unix_replaced_name);
1842 /* If there was an error, set the error code */
1843 if(!ret)
1844 SetLastError(error);
1845 return ret;
1849 /**************************************************************************
1850 * ReplaceFileA (KERNEL32.@)
1852 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1853 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1854 LPVOID lpExclude, LPVOID lpReserved)
1856 WCHAR *replacedW, *replacementW, *backupW = NULL;
1857 BOOL ret;
1859 /* This function only makes sense when the first two parameters are defined */
1860 if (!lpReplacedFileName || !(replacedW = FILE_name_AtoW( lpReplacedFileName, TRUE )))
1862 SetLastError(ERROR_INVALID_PARAMETER);
1863 return FALSE;
1865 if (!lpReplacementFileName || !(replacementW = FILE_name_AtoW( lpReplacementFileName, TRUE )))
1867 HeapFree( GetProcessHeap(), 0, replacedW );
1868 SetLastError(ERROR_INVALID_PARAMETER);
1869 return FALSE;
1871 /* The backup parameter, however, is optional */
1872 if (lpBackupFileName)
1874 if (!(backupW = FILE_name_AtoW( lpBackupFileName, TRUE )))
1876 HeapFree( GetProcessHeap(), 0, replacedW );
1877 HeapFree( GetProcessHeap(), 0, replacementW );
1878 SetLastError(ERROR_INVALID_PARAMETER);
1879 return FALSE;
1882 ret = ReplaceFileW( replacedW, replacementW, backupW, dwReplaceFlags, lpExclude, lpReserved );
1883 HeapFree( GetProcessHeap(), 0, replacedW );
1884 HeapFree( GetProcessHeap(), 0, replacementW );
1885 HeapFree( GetProcessHeap(), 0, backupW );
1886 return ret;
1890 /*************************************************************************
1891 * FindFirstFileExW (KERNEL32.@)
1893 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1894 * results as FindExSearchNameMatch
1896 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1897 LPVOID data, FINDEX_SEARCH_OPS search_op,
1898 LPVOID filter, DWORD flags)
1900 WCHAR *mask, *p;
1901 FIND_FIRST_INFO *info = NULL;
1902 UNICODE_STRING nt_name;
1903 OBJECT_ATTRIBUTES attr;
1904 IO_STATUS_BLOCK io;
1905 NTSTATUS status;
1906 DWORD device = 0;
1908 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1910 if (flags != 0)
1912 FIXME("flags not implemented 0x%08x\n", flags );
1914 if (search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1916 FIXME("search_op not implemented 0x%08x\n", search_op);
1917 SetLastError( ERROR_INVALID_PARAMETER );
1918 return INVALID_HANDLE_VALUE;
1920 if (level != FindExInfoStandard && level != FindExInfoBasic)
1922 FIXME("info level %d not implemented\n", level );
1923 SetLastError( ERROR_INVALID_PARAMETER );
1924 return INVALID_HANDLE_VALUE;
1927 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1929 SetLastError( ERROR_PATH_NOT_FOUND );
1930 return INVALID_HANDLE_VALUE;
1933 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1935 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1936 goto error;
1939 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1941 static const WCHAR dotW[] = {'.',0};
1942 WCHAR *dir = NULL;
1944 /* we still need to check that the directory can be opened */
1946 if (HIWORD(device))
1948 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1950 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1951 goto error;
1953 memcpy( dir, filename, HIWORD(device) );
1954 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1956 RtlFreeUnicodeString( &nt_name );
1957 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1959 HeapFree( GetProcessHeap(), 0, dir );
1960 SetLastError( ERROR_PATH_NOT_FOUND );
1961 goto error;
1963 HeapFree( GetProcessHeap(), 0, dir );
1964 RtlInitUnicodeString( &info->mask, NULL );
1966 else if (!mask || !*mask)
1968 SetLastError( ERROR_FILE_NOT_FOUND );
1969 goto error;
1971 else
1973 if (!RtlCreateUnicodeString( &info->mask, mask ))
1975 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1976 goto error;
1979 /* truncate dir name before mask */
1980 *mask = 0;
1981 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1984 /* check if path is the root of the drive */
1985 info->is_root = FALSE;
1986 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1987 if (p[0] && p[1] == ':')
1989 p += 2;
1990 while (*p == '\\') p++;
1991 info->is_root = (*p == 0);
1994 attr.Length = sizeof(attr);
1995 attr.RootDirectory = 0;
1996 attr.Attributes = OBJ_CASE_INSENSITIVE;
1997 attr.ObjectName = &nt_name;
1998 attr.SecurityDescriptor = NULL;
1999 attr.SecurityQualityOfService = NULL;
2001 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
2002 FILE_SHARE_READ | FILE_SHARE_WRITE,
2003 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
2005 if (status != STATUS_SUCCESS)
2007 RtlFreeUnicodeString( &info->mask );
2008 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
2009 SetLastError( ERROR_PATH_NOT_FOUND );
2010 else
2011 SetLastError( RtlNtStatusToDosError(status) );
2012 goto error;
2015 RtlInitializeCriticalSection( &info->cs );
2016 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
2017 info->path = nt_name;
2018 info->magic = FIND_FIRST_MAGIC;
2019 info->data_pos = 0;
2020 info->data_len = 0;
2021 info->data_size = 0;
2022 info->data = NULL;
2023 info->search_op = search_op;
2024 info->level = level;
2026 if (device)
2028 WIN32_FIND_DATAW *wfd = data;
2030 memset( wfd, 0, sizeof(*wfd) );
2031 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
2032 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
2033 CloseHandle( info->handle );
2034 info->handle = 0;
2036 else
2038 IO_STATUS_BLOCK io;
2039 BOOL has_wildcard = strpbrkW( info->mask.Buffer, wildcardsW ) != NULL;
2041 info->data_size = has_wildcard ? 8192 : max_entry_size * 2;
2043 while (info->data_size)
2045 if (!(info->data = HeapAlloc( GetProcessHeap(), 0, info->data_size )))
2047 FindClose( info );
2048 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2049 return INVALID_HANDLE_VALUE;
2052 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
2053 FileBothDirectoryInformation, FALSE, &info->mask, TRUE );
2054 if (io.u.Status)
2056 FindClose( info );
2057 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
2058 return INVALID_HANDLE_VALUE;
2061 if (io.Information < info->data_size - max_entry_size)
2063 info->data_size = 0; /* we read everything */
2065 else if (info->data_size < 1024 * 1024)
2067 HeapFree( GetProcessHeap(), 0, info->data );
2068 info->data_size *= 2;
2070 else break;
2073 info->data_len = io.Information;
2074 if (!info->data_size && has_wildcard) /* release unused buffer space */
2075 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, info->data, info->data_len );
2077 if (!FindNextFileW( info, data ))
2079 TRACE( "%s not found\n", debugstr_w(filename) );
2080 FindClose( info );
2081 SetLastError( ERROR_FILE_NOT_FOUND );
2082 return INVALID_HANDLE_VALUE;
2084 if (!has_wildcard) /* we can't find two files with the same name */
2086 CloseHandle( info->handle );
2087 HeapFree( GetProcessHeap(), 0, info->data );
2088 info->handle = 0;
2089 info->data = NULL;
2092 return info;
2094 error:
2095 HeapFree( GetProcessHeap(), 0, info );
2096 RtlFreeUnicodeString( &nt_name );
2097 return INVALID_HANDLE_VALUE;
2101 /*************************************************************************
2102 * FindNextFileW (KERNEL32.@)
2104 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
2106 FIND_FIRST_INFO *info;
2107 FILE_BOTH_DIR_INFORMATION *dir_info;
2108 BOOL ret = FALSE;
2110 TRACE("%p %p\n", handle, data);
2112 if (!handle || handle == INVALID_HANDLE_VALUE)
2114 SetLastError( ERROR_INVALID_HANDLE );
2115 return ret;
2117 info = handle;
2118 if (info->magic != FIND_FIRST_MAGIC)
2120 SetLastError( ERROR_INVALID_HANDLE );
2121 return ret;
2124 RtlEnterCriticalSection( &info->cs );
2126 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
2127 else for (;;)
2129 if (info->data_pos >= info->data_len) /* need to read some more data */
2131 IO_STATUS_BLOCK io;
2133 if (info->data_size)
2134 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
2135 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
2136 else
2137 io.u.Status = STATUS_NO_MORE_FILES;
2139 if (io.u.Status)
2141 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
2142 if (io.u.Status == STATUS_NO_MORE_FILES)
2144 CloseHandle( info->handle );
2145 HeapFree( GetProcessHeap(), 0, info->data );
2146 info->handle = 0;
2147 info->data = NULL;
2149 break;
2151 info->data_len = io.Information;
2152 info->data_pos = 0;
2155 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
2157 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
2158 else info->data_pos = info->data_len;
2160 /* don't return '.' and '..' in the root of the drive */
2161 if (info->is_root)
2163 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
2164 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
2165 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
2168 /* check for dir symlink */
2169 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
2170 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2171 strpbrkW( info->mask.Buffer, wildcardsW ))
2173 if (!check_dir_symlink( info, dir_info )) continue;
2176 data->dwFileAttributes = dir_info->FileAttributes;
2177 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
2178 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
2179 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
2180 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
2181 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
2182 data->dwReserved0 = 0;
2183 data->dwReserved1 = 0;
2185 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
2186 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
2188 if (info->level != FindExInfoBasic)
2190 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
2191 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
2193 else
2194 data->cAlternateFileName[0] = 0;
2196 TRACE("returning %s (%s)\n",
2197 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
2199 ret = TRUE;
2200 break;
2203 RtlLeaveCriticalSection( &info->cs );
2204 return ret;
2208 /*************************************************************************
2209 * FindClose (KERNEL32.@)
2211 BOOL WINAPI FindClose( HANDLE handle )
2213 FIND_FIRST_INFO *info = handle;
2215 if (!handle || handle == INVALID_HANDLE_VALUE)
2217 SetLastError( ERROR_INVALID_HANDLE );
2218 return FALSE;
2221 __TRY
2223 if (info->magic == FIND_FIRST_MAGIC)
2225 RtlEnterCriticalSection( &info->cs );
2226 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
2228 info->magic = 0;
2229 if (info->handle) CloseHandle( info->handle );
2230 info->handle = 0;
2231 RtlFreeUnicodeString( &info->mask );
2232 info->mask.Buffer = NULL;
2233 RtlFreeUnicodeString( &info->path );
2234 info->data_pos = 0;
2235 info->data_len = 0;
2236 HeapFree( GetProcessHeap(), 0, info->data );
2237 RtlLeaveCriticalSection( &info->cs );
2238 info->cs.DebugInfo->Spare[0] = 0;
2239 RtlDeleteCriticalSection( &info->cs );
2240 HeapFree( GetProcessHeap(), 0, info );
2244 __EXCEPT_PAGE_FAULT
2246 WARN("Illegal handle %p\n", handle);
2247 SetLastError( ERROR_INVALID_HANDLE );
2248 return FALSE;
2250 __ENDTRY
2252 return TRUE;
2256 /*************************************************************************
2257 * FindFirstFileA (KERNEL32.@)
2259 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
2261 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
2262 FindExSearchNameMatch, NULL, 0);
2265 /*************************************************************************
2266 * FindFirstFileExA (KERNEL32.@)
2268 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
2269 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
2270 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
2272 HANDLE handle;
2273 WIN32_FIND_DATAA *dataA;
2274 WIN32_FIND_DATAW dataW;
2275 WCHAR *nameW;
2277 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
2279 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
2280 if (handle == INVALID_HANDLE_VALUE) return handle;
2282 dataA = lpFindFileData;
2283 dataA->dwFileAttributes = dataW.dwFileAttributes;
2284 dataA->ftCreationTime = dataW.ftCreationTime;
2285 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
2286 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
2287 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
2288 dataA->nFileSizeLow = dataW.nFileSizeLow;
2289 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
2290 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
2291 sizeof(dataA->cAlternateFileName) );
2292 return handle;
2296 /*************************************************************************
2297 * FindFirstFileW (KERNEL32.@)
2299 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
2301 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
2302 FindExSearchNameMatch, NULL, 0);
2306 /*************************************************************************
2307 * FindNextFileA (KERNEL32.@)
2309 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
2311 WIN32_FIND_DATAW dataW;
2313 if (!FindNextFileW( handle, &dataW )) return FALSE;
2314 data->dwFileAttributes = dataW.dwFileAttributes;
2315 data->ftCreationTime = dataW.ftCreationTime;
2316 data->ftLastAccessTime = dataW.ftLastAccessTime;
2317 data->ftLastWriteTime = dataW.ftLastWriteTime;
2318 data->nFileSizeHigh = dataW.nFileSizeHigh;
2319 data->nFileSizeLow = dataW.nFileSizeLow;
2320 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
2321 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
2322 sizeof(data->cAlternateFileName) );
2323 return TRUE;
2327 /**************************************************************************
2328 * GetFileAttributesW (KERNEL32.@)
2330 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
2332 FILE_BASIC_INFORMATION info;
2333 UNICODE_STRING nt_name;
2334 OBJECT_ATTRIBUTES attr;
2335 NTSTATUS status;
2337 TRACE("%s\n", debugstr_w(name));
2339 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2341 SetLastError( ERROR_PATH_NOT_FOUND );
2342 return INVALID_FILE_ATTRIBUTES;
2345 attr.Length = sizeof(attr);
2346 attr.RootDirectory = 0;
2347 attr.Attributes = OBJ_CASE_INSENSITIVE;
2348 attr.ObjectName = &nt_name;
2349 attr.SecurityDescriptor = NULL;
2350 attr.SecurityQualityOfService = NULL;
2352 status = NtQueryAttributesFile( &attr, &info );
2353 RtlFreeUnicodeString( &nt_name );
2355 if (status == STATUS_SUCCESS) return info.FileAttributes;
2357 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2358 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
2360 SetLastError( RtlNtStatusToDosError(status) );
2361 return INVALID_FILE_ATTRIBUTES;
2365 /**************************************************************************
2366 * GetFileAttributesA (KERNEL32.@)
2368 DWORD WINAPI GetFileAttributesA( LPCSTR name )
2370 WCHAR *nameW;
2372 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
2373 return GetFileAttributesW( nameW );
2377 /**************************************************************************
2378 * SetFileAttributesW (KERNEL32.@)
2380 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
2382 UNICODE_STRING nt_name;
2383 OBJECT_ATTRIBUTES attr;
2384 IO_STATUS_BLOCK io;
2385 NTSTATUS status;
2386 HANDLE handle;
2388 TRACE("%s %x\n", debugstr_w(name), attributes);
2390 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2392 SetLastError( ERROR_PATH_NOT_FOUND );
2393 return FALSE;
2396 attr.Length = sizeof(attr);
2397 attr.RootDirectory = 0;
2398 attr.Attributes = OBJ_CASE_INSENSITIVE;
2399 attr.ObjectName = &nt_name;
2400 attr.SecurityDescriptor = NULL;
2401 attr.SecurityQualityOfService = NULL;
2403 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2404 RtlFreeUnicodeString( &nt_name );
2406 if (status == STATUS_SUCCESS)
2408 FILE_BASIC_INFORMATION info;
2410 memset( &info, 0, sizeof(info) );
2411 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2412 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2413 NtClose( handle );
2416 if (status == STATUS_SUCCESS) return TRUE;
2417 SetLastError( RtlNtStatusToDosError(status) );
2418 return FALSE;
2422 /**************************************************************************
2423 * SetFileAttributesA (KERNEL32.@)
2425 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2427 WCHAR *nameW;
2429 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2430 return SetFileAttributesW( nameW, attributes );
2434 /**************************************************************************
2435 * GetFileAttributesExW (KERNEL32.@)
2437 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2439 FILE_NETWORK_OPEN_INFORMATION info;
2440 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2441 UNICODE_STRING nt_name;
2442 OBJECT_ATTRIBUTES attr;
2443 NTSTATUS status;
2445 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2447 if (level != GetFileExInfoStandard)
2449 SetLastError( ERROR_INVALID_PARAMETER );
2450 return FALSE;
2453 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2455 SetLastError( ERROR_PATH_NOT_FOUND );
2456 return FALSE;
2459 attr.Length = sizeof(attr);
2460 attr.RootDirectory = 0;
2461 attr.Attributes = OBJ_CASE_INSENSITIVE;
2462 attr.ObjectName = &nt_name;
2463 attr.SecurityDescriptor = NULL;
2464 attr.SecurityQualityOfService = NULL;
2466 status = NtQueryFullAttributesFile( &attr, &info );
2467 RtlFreeUnicodeString( &nt_name );
2469 if (status != STATUS_SUCCESS)
2471 SetLastError( RtlNtStatusToDosError(status) );
2472 return FALSE;
2475 data->dwFileAttributes = info.FileAttributes;
2476 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2477 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2478 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2479 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2480 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2481 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2482 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2483 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2484 return TRUE;
2488 /**************************************************************************
2489 * GetFileAttributesExA (KERNEL32.@)
2491 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2493 WCHAR *nameW;
2495 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2496 return GetFileAttributesExW( nameW, level, ptr );
2500 /******************************************************************************
2501 * GetCompressedFileSizeW (KERNEL32.@)
2503 * Get the actual number of bytes used on disk.
2505 * RETURNS
2506 * Success: Low-order doubleword of number of bytes
2507 * Failure: INVALID_FILE_SIZE
2509 DWORD WINAPI GetCompressedFileSizeW(
2510 LPCWSTR name, /* [in] Pointer to name of file */
2511 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2513 UNICODE_STRING nt_name;
2514 OBJECT_ATTRIBUTES attr;
2515 IO_STATUS_BLOCK io;
2516 NTSTATUS status;
2517 HANDLE handle;
2518 DWORD ret = INVALID_FILE_SIZE;
2520 TRACE("%s %p\n", debugstr_w(name), size_high);
2522 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2524 SetLastError( ERROR_PATH_NOT_FOUND );
2525 return INVALID_FILE_SIZE;
2528 attr.Length = sizeof(attr);
2529 attr.RootDirectory = 0;
2530 attr.Attributes = OBJ_CASE_INSENSITIVE;
2531 attr.ObjectName = &nt_name;
2532 attr.SecurityDescriptor = NULL;
2533 attr.SecurityQualityOfService = NULL;
2535 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2536 RtlFreeUnicodeString( &nt_name );
2538 if (status == STATUS_SUCCESS)
2540 /* we don't support compressed files, simply return the file size */
2541 ret = GetFileSize( handle, size_high );
2542 NtClose( handle );
2544 else SetLastError( RtlNtStatusToDosError(status) );
2546 return ret;
2550 /******************************************************************************
2551 * GetCompressedFileSizeA (KERNEL32.@)
2553 * See GetCompressedFileSizeW.
2555 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2557 WCHAR *nameW;
2559 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2560 return GetCompressedFileSizeW( nameW, size_high );
2564 /***********************************************************************
2565 * OpenVxDHandle (KERNEL32.@)
2567 * This function is supposed to return the corresponding Ring 0
2568 * ("kernel") handle for a Ring 3 handle in Win9x.
2569 * Evidently, Wine will have problems with this. But we try anyway,
2570 * maybe it helps...
2572 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
2574 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
2575 return hHandleRing3;
2579 /****************************************************************************
2580 * DeviceIoControl (KERNEL32.@)
2582 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
2583 LPVOID lpvInBuffer, DWORD cbInBuffer,
2584 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
2585 LPDWORD lpcbBytesReturned,
2586 LPOVERLAPPED lpOverlapped)
2588 NTSTATUS status;
2590 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2591 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
2592 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
2594 /* Check if this is a user defined control code for a VxD */
2596 if (HIWORD( dwIoControlCode ) == 0 && (GetVersion() & 0x80000000))
2598 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
2599 static DeviceIoProc (*vxd_get_proc)(HANDLE);
2600 DeviceIoProc proc = NULL;
2602 if (!vxd_get_proc) vxd_get_proc = (void *)GetProcAddress( GetModuleHandleA("krnl386.exe16"),
2603 "__wine_vxd_get_proc" );
2604 if (vxd_get_proc) proc = vxd_get_proc( hDevice );
2605 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
2606 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
2609 /* Not a VxD, let ntdll handle it */
2611 if (lpOverlapped)
2613 LPVOID cvalue = ((ULONG_PTR)lpOverlapped->hEvent & 1) ? NULL : lpOverlapped;
2614 lpOverlapped->Internal = STATUS_PENDING;
2615 lpOverlapped->InternalHigh = 0;
2616 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2617 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
2618 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2619 dwIoControlCode, lpvInBuffer, cbInBuffer,
2620 lpvOutBuffer, cbOutBuffer);
2621 else
2622 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
2623 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2624 dwIoControlCode, lpvInBuffer, cbInBuffer,
2625 lpvOutBuffer, cbOutBuffer);
2626 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
2628 else
2630 IO_STATUS_BLOCK iosb;
2632 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2633 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
2634 dwIoControlCode, lpvInBuffer, cbInBuffer,
2635 lpvOutBuffer, cbOutBuffer);
2636 else
2637 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
2638 dwIoControlCode, lpvInBuffer, cbInBuffer,
2639 lpvOutBuffer, cbOutBuffer);
2640 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
2642 if (status) SetLastError( RtlNtStatusToDosError(status) );
2643 return !status;
2647 /***********************************************************************
2648 * OpenFile (KERNEL32.@)
2650 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2652 HANDLE handle;
2653 FILETIME filetime;
2654 WORD filedatetime[2];
2656 if (!ofs) return HFILE_ERROR;
2658 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2659 ((mode & 0x3 )==OF_READ)?"OF_READ":
2660 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2661 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2662 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2663 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2664 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2665 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2666 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2667 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2668 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2669 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2670 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2671 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2672 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2673 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2674 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2675 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2679 ofs->cBytes = sizeof(OFSTRUCT);
2680 ofs->nErrCode = 0;
2681 if (mode & OF_REOPEN) name = ofs->szPathName;
2683 if (!name) return HFILE_ERROR;
2685 TRACE("%s %04x\n", name, mode );
2687 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2688 Are there any cases where getting the path here is wrong?
2689 Uwe Bonnes 1997 Apr 2 */
2690 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2692 /* OF_PARSE simply fills the structure */
2694 if (mode & OF_PARSE)
2696 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2697 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2698 return 0;
2701 /* OF_CREATE is completely different from all other options, so
2702 handle it first */
2704 if (mode & OF_CREATE)
2706 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2707 goto error;
2709 else
2711 /* Now look for the file */
2713 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2714 goto error;
2716 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2718 if (mode & OF_DELETE)
2720 if (!DeleteFileA( ofs->szPathName )) goto error;
2721 TRACE("(%s): OF_DELETE return = OK\n", name);
2722 return TRUE;
2725 handle = LongToHandle(_lopen( ofs->szPathName, mode ));
2726 if (handle == INVALID_HANDLE_VALUE) goto error;
2728 GetFileTime( handle, NULL, NULL, &filetime );
2729 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2730 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2732 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2734 CloseHandle( handle );
2735 WARN("(%s): OF_VERIFY failed\n", name );
2736 /* FIXME: what error here? */
2737 SetLastError( ERROR_FILE_NOT_FOUND );
2738 goto error;
2741 ofs->Reserved1 = filedatetime[0];
2742 ofs->Reserved2 = filedatetime[1];
2744 TRACE("(%s): OK, return = %p\n", name, handle );
2745 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2747 CloseHandle( handle );
2748 return TRUE;
2750 return HandleToLong(handle);
2752 error: /* We get here if there was an error opening the file */
2753 ofs->nErrCode = GetLastError();
2754 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2755 return HFILE_ERROR;
2759 /***********************************************************************
2760 * OpenFileById (KERNEL32.@)
2762 HANDLE WINAPI OpenFileById( HANDLE handle, LPFILE_ID_DESCRIPTOR id, DWORD access,
2763 DWORD share, LPSECURITY_ATTRIBUTES sec_attr, DWORD flags )
2765 UINT options;
2766 HANDLE result;
2767 OBJECT_ATTRIBUTES attr;
2768 NTSTATUS status;
2769 IO_STATUS_BLOCK io;
2770 UNICODE_STRING objectName;
2772 if (!id)
2774 SetLastError( ERROR_INVALID_PARAMETER );
2775 return INVALID_HANDLE_VALUE;
2778 options = FILE_OPEN_BY_FILE_ID;
2779 if (flags & FILE_FLAG_BACKUP_SEMANTICS)
2780 options |= FILE_OPEN_FOR_BACKUP_INTENT;
2781 else
2782 options |= FILE_NON_DIRECTORY_FILE;
2783 if (flags & FILE_FLAG_NO_BUFFERING) options |= FILE_NO_INTERMEDIATE_BUFFERING;
2784 if (!(flags & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
2785 if (flags & FILE_FLAG_RANDOM_ACCESS) options |= FILE_RANDOM_ACCESS;
2786 flags &= FILE_ATTRIBUTE_VALID_FLAGS;
2788 objectName.Length = sizeof(ULONGLONG);
2789 objectName.Buffer = (WCHAR *)&id->u.FileId;
2790 attr.Length = sizeof(attr);
2791 attr.RootDirectory = handle;
2792 attr.Attributes = 0;
2793 attr.ObjectName = &objectName;
2794 attr.SecurityDescriptor = sec_attr ? sec_attr->lpSecurityDescriptor : NULL;
2795 attr.SecurityQualityOfService = NULL;
2796 if (sec_attr && sec_attr->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
2798 status = NtCreateFile( &result, access, &attr, &io, NULL, flags,
2799 share, OPEN_EXISTING, options, NULL, 0 );
2800 if (status != STATUS_SUCCESS)
2802 SetLastError( RtlNtStatusToDosError( status ) );
2803 return INVALID_HANDLE_VALUE;
2805 return result;
2809 /***********************************************************************
2810 * K32EnumDeviceDrivers (KERNEL32.@)
2812 BOOL WINAPI K32EnumDeviceDrivers(void **image_base, DWORD cb, DWORD *needed)
2814 FIXME("(%p, %d, %p): stub\n", image_base, cb, needed);
2816 if (needed)
2817 *needed = 0;
2819 return TRUE;
2822 /***********************************************************************
2823 * K32GetDeviceDriverBaseNameA (KERNEL32.@)
2825 DWORD WINAPI K32GetDeviceDriverBaseNameA(void *image_base, LPSTR base_name, DWORD size)
2827 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2829 if (base_name && size)
2830 base_name[0] = '\0';
2832 return 0;
2835 /***********************************************************************
2836 * K32GetDeviceDriverBaseNameW (KERNEL32.@)
2838 DWORD WINAPI K32GetDeviceDriverBaseNameW(void *image_base, LPWSTR base_name, DWORD size)
2840 FIXME("(%p, %p, %d): stub\n", image_base, base_name, size);
2842 if (base_name && size)
2843 base_name[0] = '\0';
2845 return 0;
2848 /***********************************************************************
2849 * K32GetDeviceDriverFileNameA (KERNEL32.@)
2851 DWORD WINAPI K32GetDeviceDriverFileNameA(void *image_base, LPSTR file_name, DWORD size)
2853 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2855 if (file_name && size)
2856 file_name[0] = '\0';
2858 return 0;
2861 /***********************************************************************
2862 * K32GetDeviceDriverFileNameW (KERNEL32.@)
2864 DWORD WINAPI K32GetDeviceDriverFileNameW(void *image_base, LPWSTR file_name, DWORD size)
2866 FIXME("(%p, %p, %d): stub\n", image_base, file_name, size);
2868 if (file_name && size)
2869 file_name[0] = '\0';
2871 return 0;