progman: Updated Korean translation.
[wine/gsoc_dplay.git] / dlls / kernel / file.c
blob7f0edf3accb723fe1c354adec296a489986fec0d
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include "winerror.h"
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winternl.h"
40 #include "winioctl.h"
41 #include "wincon.h"
42 #include "wine/winbase16.h"
43 #include "kernel_private.h"
45 #include "wine/exception.h"
46 #include "excpt.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
49 #include "thread.h"
50 #include "wine/server.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(file);
54 HANDLE dos_handles[DOS_TABLE_SIZE];
56 /* info structure for FindFirstFile handle */
57 typedef struct
59 DWORD magic; /* magic number */
60 HANDLE handle; /* handle to directory */
61 CRITICAL_SECTION cs; /* crit section protecting this structure */
62 FINDEX_SEARCH_OPS search_op; /* Flags passed to FindFirst. */
63 UNICODE_STRING mask; /* file mask */
64 UNICODE_STRING path; /* NT path used to open the directory */
65 BOOL is_root; /* is directory the root of the drive? */
66 UINT data_pos; /* current position in dir data */
67 UINT data_len; /* length of dir data */
68 BYTE data[8192]; /* directory data */
69 } FIND_FIRST_INFO;
71 #define FIND_FIRST_MAGIC 0xc0ffee11
73 static BOOL oem_file_apis;
76 /***********************************************************************
77 * create_file_OF
79 * Wrapper for CreateFile that takes OF_* mode flags.
81 static HANDLE create_file_OF( LPCSTR path, INT mode )
83 DWORD access, sharing, creation;
85 if (mode & OF_CREATE)
87 creation = CREATE_ALWAYS;
88 access = GENERIC_READ | GENERIC_WRITE;
90 else
92 creation = OPEN_EXISTING;
93 switch(mode & 0x03)
95 case OF_READ: access = GENERIC_READ; break;
96 case OF_WRITE: access = GENERIC_WRITE; break;
97 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
98 default: access = 0; break;
102 switch(mode & 0x70)
104 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
105 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
106 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
107 case OF_SHARE_DENY_NONE:
108 case OF_SHARE_COMPAT:
109 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
111 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
115 /***********************************************************************
116 * check_dir_symlink
118 * Check if a dir symlink should be returned by FindNextFile.
120 static BOOL check_dir_symlink( FIND_FIRST_INFO *info, const FILE_BOTH_DIR_INFORMATION *file_info )
122 UNICODE_STRING str;
123 ANSI_STRING unix_name;
124 struct stat st, parent_st;
125 BOOL ret = TRUE;
126 DWORD len;
128 str.MaximumLength = info->path.Length + sizeof(WCHAR) + file_info->FileNameLength;
129 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength ))) return TRUE;
130 memcpy( str.Buffer, info->path.Buffer, info->path.Length );
131 len = info->path.Length / sizeof(WCHAR);
132 if (!len || str.Buffer[len-1] != '\\') str.Buffer[len++] = '\\';
133 memcpy( str.Buffer + len, file_info->FileName, file_info->FileNameLength );
134 str.Length = len * sizeof(WCHAR) + file_info->FileNameLength;
136 unix_name.Buffer = NULL;
137 if (!wine_nt_to_unix_file_name( &str, &unix_name, OPEN_EXISTING, FALSE ) &&
138 !stat( unix_name.Buffer, &st ))
140 char *p = unix_name.Buffer + unix_name.Length - 1;
142 /* skip trailing slashes */
143 while (p > unix_name.Buffer && *p == '/') p--;
145 while (ret && p > unix_name.Buffer)
147 while (p > unix_name.Buffer && *p != '/') p--;
148 while (p > unix_name.Buffer && *p == '/') p--;
149 p[1] = 0;
150 if (!stat( unix_name.Buffer, &parent_st ) &&
151 parent_st.st_dev == st.st_dev &&
152 parent_st.st_ino == st.st_ino)
154 WARN( "suppressing dir symlink %s pointing to parent %s\n",
155 debugstr_wn( str.Buffer, str.Length/sizeof(WCHAR) ),
156 debugstr_a( unix_name.Buffer ));
157 ret = FALSE;
161 RtlFreeAnsiString( &unix_name );
162 RtlFreeUnicodeString( &str );
163 return ret;
167 /***********************************************************************
168 * FILE_SetDosError
170 * Set the DOS error code from errno.
172 void FILE_SetDosError(void)
174 int save_errno = errno; /* errno gets overwritten by printf */
176 TRACE("errno = %d %s\n", errno, strerror(errno));
177 switch (save_errno)
179 case EAGAIN:
180 SetLastError( ERROR_SHARING_VIOLATION );
181 break;
182 case EBADF:
183 SetLastError( ERROR_INVALID_HANDLE );
184 break;
185 case ENOSPC:
186 SetLastError( ERROR_HANDLE_DISK_FULL );
187 break;
188 case EACCES:
189 case EPERM:
190 case EROFS:
191 SetLastError( ERROR_ACCESS_DENIED );
192 break;
193 case EBUSY:
194 SetLastError( ERROR_LOCK_VIOLATION );
195 break;
196 case ENOENT:
197 SetLastError( ERROR_FILE_NOT_FOUND );
198 break;
199 case EISDIR:
200 SetLastError( ERROR_CANNOT_MAKE );
201 break;
202 case ENFILE:
203 case EMFILE:
204 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
205 break;
206 case EEXIST:
207 SetLastError( ERROR_FILE_EXISTS );
208 break;
209 case EINVAL:
210 case ESPIPE:
211 SetLastError( ERROR_SEEK );
212 break;
213 case ENOTEMPTY:
214 SetLastError( ERROR_DIR_NOT_EMPTY );
215 break;
216 case ENOEXEC:
217 SetLastError( ERROR_BAD_FORMAT );
218 break;
219 case ENOTDIR:
220 SetLastError( ERROR_PATH_NOT_FOUND );
221 break;
222 case EXDEV:
223 SetLastError( ERROR_NOT_SAME_DEVICE );
224 break;
225 default:
226 WARN("unknown file error: %s\n", strerror(save_errno) );
227 SetLastError( ERROR_GEN_FAILURE );
228 break;
230 errno = save_errno;
234 /***********************************************************************
235 * FILE_name_AtoW
237 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
239 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
240 * there is no possibility for the function to do that twice, taking into
241 * account any called function.
243 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
245 ANSI_STRING str;
246 UNICODE_STRING strW, *pstrW;
247 NTSTATUS status;
249 RtlInitAnsiString( &str, name );
250 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
251 if (oem_file_apis)
252 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
253 else
254 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
255 if (status == STATUS_SUCCESS) return pstrW->Buffer;
257 if (status == STATUS_BUFFER_OVERFLOW)
258 SetLastError( ERROR_FILENAME_EXCED_RANGE );
259 else
260 SetLastError( RtlNtStatusToDosError(status) );
261 return NULL;
265 /***********************************************************************
266 * FILE_name_WtoA
268 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
270 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
272 DWORD ret;
274 if (srclen < 0) srclen = strlenW( src ) + 1;
275 if (oem_file_apis)
276 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
277 else
278 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
279 return ret;
283 /**************************************************************************
284 * SetFileApisToOEM (KERNEL32.@)
286 VOID WINAPI SetFileApisToOEM(void)
288 oem_file_apis = TRUE;
292 /**************************************************************************
293 * SetFileApisToANSI (KERNEL32.@)
295 VOID WINAPI SetFileApisToANSI(void)
297 oem_file_apis = FALSE;
301 /******************************************************************************
302 * AreFileApisANSI (KERNEL32.@)
304 * Determines if file functions are using ANSI
306 * RETURNS
307 * TRUE: Set of file functions is using ANSI code page
308 * FALSE: Set of file functions is using OEM code page
310 BOOL WINAPI AreFileApisANSI(void)
312 return !oem_file_apis;
316 /**************************************************************************
317 * Operations on file handles *
318 **************************************************************************/
320 /***********************************************************************
321 * FILE_InitProcessDosHandles
323 * Allocates the default DOS handles for a process. Called either by
324 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
326 static void FILE_InitProcessDosHandles( void )
328 static BOOL init_done /* = FALSE */;
329 HANDLE cp = GetCurrentProcess();
331 if (init_done) return;
332 init_done = TRUE;
333 DuplicateHandle(cp, GetStdHandle(STD_INPUT_HANDLE), cp, &dos_handles[0],
334 0, TRUE, DUPLICATE_SAME_ACCESS);
335 DuplicateHandle(cp, GetStdHandle(STD_OUTPUT_HANDLE), cp, &dos_handles[1],
336 0, TRUE, DUPLICATE_SAME_ACCESS);
337 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[2],
338 0, TRUE, DUPLICATE_SAME_ACCESS);
339 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[3],
340 0, TRUE, DUPLICATE_SAME_ACCESS);
341 DuplicateHandle(cp, GetStdHandle(STD_ERROR_HANDLE), cp, &dos_handles[4],
342 0, TRUE, DUPLICATE_SAME_ACCESS);
346 /******************************************************************
347 * FILE_ReadWriteApc (internal)
349 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG len)
351 LPOVERLAPPED_COMPLETION_ROUTINE cr = (LPOVERLAPPED_COMPLETION_ROUTINE)apc_user;
353 cr(RtlNtStatusToDosError(io_status->u.Status), len, (LPOVERLAPPED)io_status);
357 /***********************************************************************
358 * ReadFileEx (KERNEL32.@)
360 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
361 LPOVERLAPPED overlapped,
362 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
364 LARGE_INTEGER offset;
365 NTSTATUS status;
366 PIO_STATUS_BLOCK io_status;
368 TRACE("(hFile=%p, buffer=%p, bytes=%lu, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
370 if (!overlapped)
372 SetLastError(ERROR_INVALID_PARAMETER);
373 return FALSE;
376 offset.u.LowPart = overlapped->u.s.Offset;
377 offset.u.HighPart = overlapped->u.s.OffsetHigh;
378 io_status = (PIO_STATUS_BLOCK)overlapped;
379 io_status->u.Status = STATUS_PENDING;
381 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
382 io_status, buffer, bytesToRead, &offset, NULL);
384 if (status)
386 SetLastError( RtlNtStatusToDosError(status) );
387 return FALSE;
389 return TRUE;
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;
406 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToRead,
407 bytesRead, overlapped );
409 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
410 if (!bytesToRead) return TRUE;
412 if (is_console_handle(hFile))
413 return ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
415 if (overlapped != NULL)
417 offset.u.LowPart = overlapped->u.s.Offset;
418 offset.u.HighPart = overlapped->u.s.OffsetHigh;
419 poffset = &offset;
420 hEvent = overlapped->hEvent;
421 io_status = (PIO_STATUS_BLOCK)overlapped;
423 io_status->u.Status = STATUS_PENDING;
424 io_status->Information = 0;
426 status = NtReadFile(hFile, hEvent, NULL, NULL, io_status, buffer, bytesToRead, poffset, NULL);
428 if (status != STATUS_PENDING && bytesRead)
429 *bytesRead = io_status->Information;
431 if (status && status != STATUS_END_OF_FILE && status != STATUS_TIMEOUT)
433 SetLastError( RtlNtStatusToDosError(status) );
434 return FALSE;
436 return TRUE;
440 /***********************************************************************
441 * WriteFileEx (KERNEL32.@)
443 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
444 LPOVERLAPPED overlapped,
445 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
447 LARGE_INTEGER offset;
448 NTSTATUS status;
449 PIO_STATUS_BLOCK io_status;
451 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
453 if (overlapped == NULL)
455 SetLastError(ERROR_INVALID_PARAMETER);
456 return FALSE;
458 offset.u.LowPart = overlapped->u.s.Offset;
459 offset.u.HighPart = overlapped->u.s.OffsetHigh;
461 io_status = (PIO_STATUS_BLOCK)overlapped;
462 io_status->u.Status = STATUS_PENDING;
464 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
465 io_status, buffer, bytesToWrite, &offset, NULL);
467 if (status) SetLastError( RtlNtStatusToDosError(status) );
468 return !status;
472 /***********************************************************************
473 * WriteFile (KERNEL32.@)
475 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
476 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
478 HANDLE hEvent = NULL;
479 LARGE_INTEGER offset;
480 PLARGE_INTEGER poffset = NULL;
481 NTSTATUS status;
482 IO_STATUS_BLOCK iosb;
483 PIO_STATUS_BLOCK piosb = &iosb;
485 TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
487 if (is_console_handle(hFile))
488 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
490 if (overlapped)
492 offset.u.LowPart = overlapped->u.s.Offset;
493 offset.u.HighPart = overlapped->u.s.OffsetHigh;
494 poffset = &offset;
495 hEvent = overlapped->hEvent;
496 piosb = (PIO_STATUS_BLOCK)overlapped;
498 piosb->u.Status = STATUS_PENDING;
499 piosb->Information = 0;
501 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
502 buffer, bytesToWrite, poffset, NULL);
504 /* FIXME: NtWriteFile does not always cause page faults, generate them now */
505 if (status == STATUS_INVALID_USER_BUFFER && !IsBadReadPtr( buffer, bytesToWrite ))
507 status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
508 buffer, bytesToWrite, poffset, NULL);
509 if (status != STATUS_INVALID_USER_BUFFER)
510 FIXME("Could not access memory (%p,%ld) at first, now OK. Protected by DIBSection code?\n",
511 buffer, bytesToWrite);
514 if (status != STATUS_PENDING && bytesWritten)
515 *bytesWritten = piosb->Information;
517 if (status && status != STATUS_TIMEOUT)
519 SetLastError( RtlNtStatusToDosError(status) );
520 return FALSE;
522 return TRUE;
526 /***********************************************************************
527 * GetOverlappedResult (KERNEL32.@)
529 * Check the result of an Asynchronous data transfer from a file.
531 * Parameters
532 * HANDLE hFile [in] handle of file to check on
533 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
534 * LPDWORD lpTransferred [in/out] number of bytes transferred
535 * BOOL bWait [in] wait for the transfer to complete ?
537 * RETURNS
538 * TRUE on success
539 * FALSE on failure
541 * If successful (and relevant) lpTransferred will hold the number of
542 * bytes transferred during the async operation.
544 * BUGS
546 * Currently only works for WaitCommEvent, ReadFile, WriteFile
547 * with communications ports.
550 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
551 LPDWORD lpTransferred, BOOL bWait)
553 DWORD r = WAIT_OBJECT_0;
555 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
557 if ( lpOverlapped == NULL )
559 ERR("lpOverlapped was null\n");
560 return FALSE;
562 if ( bWait )
564 if ( lpOverlapped->hEvent )
568 TRACE( "waiting on %p\n", lpOverlapped );
569 r = WaitForSingleObjectEx( lpOverlapped->hEvent, INFINITE, TRUE );
570 TRACE( "wait on %p returned %ld\n", lpOverlapped, r );
571 } while ( r == WAIT_IO_COMPLETION );
573 else
575 /* busy loop */
576 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
577 Sleep( 10 );
580 else if ( lpOverlapped->Internal == STATUS_PENDING )
582 /* Wait in order to give APCs a chance to run. */
583 /* This is cheating, so we must set the event again in case of success -
584 it may be a non-manual reset event. */
587 TRACE( "waiting on %p\n", lpOverlapped );
588 r = WaitForSingleObjectEx( lpOverlapped->hEvent, 0, TRUE );
589 TRACE( "wait on %p returned %ld\n", lpOverlapped, r );
590 } while ( r == WAIT_IO_COMPLETION );
591 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
592 NtSetEvent( lpOverlapped->hEvent, NULL );
594 if ( r == WAIT_FAILED )
596 WARN("wait operation failed\n");
597 return FALSE;
599 if (lpTransferred) *lpTransferred = lpOverlapped->InternalHigh;
601 switch ( lpOverlapped->Internal )
603 case STATUS_SUCCESS:
604 return TRUE;
605 case STATUS_PENDING:
606 SetLastError( ERROR_IO_INCOMPLETE );
607 if ( bWait ) ERR("PENDING status after waiting!\n");
608 return FALSE;
609 default:
610 SetLastError( RtlNtStatusToDosError( lpOverlapped->Internal ) );
611 return FALSE;
615 /***********************************************************************
616 * CancelIo (KERNEL32.@)
618 BOOL WINAPI CancelIo(HANDLE handle)
620 IO_STATUS_BLOCK io_status;
622 NtCancelIoFile(handle, &io_status);
623 if (io_status.u.Status)
625 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
626 return FALSE;
628 return TRUE;
631 /***********************************************************************
632 * _hread (KERNEL32.@)
634 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
636 return _lread( hFile, buffer, count );
640 /***********************************************************************
641 * _hwrite (KERNEL32.@)
643 * experimentation yields that _lwrite:
644 * o truncates the file at the current position with
645 * a 0 len write
646 * o returns 0 on a 0 length write
647 * o works with console handles
650 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
652 DWORD result;
654 TRACE("%d %p %ld\n", handle, buffer, count );
656 if (!count)
658 /* Expand or truncate at current position */
659 if (!SetEndOfFile( (HANDLE)handle )) return HFILE_ERROR;
660 return 0;
662 if (!WriteFile( (HANDLE)handle, buffer, count, &result, NULL ))
663 return HFILE_ERROR;
664 return result;
668 /***********************************************************************
669 * _lclose (KERNEL32.@)
671 HFILE WINAPI _lclose( HFILE hFile )
673 TRACE("handle %d\n", hFile );
674 return CloseHandle( (HANDLE)hFile ) ? 0 : HFILE_ERROR;
678 /***********************************************************************
679 * _lcreat (KERNEL32.@)
681 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
683 /* Mask off all flags not explicitly allowed by the doc */
684 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
685 TRACE("%s %02x\n", path, attr );
686 return (HFILE)CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
687 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
688 CREATE_ALWAYS, attr, 0 );
692 /***********************************************************************
693 * _lopen (KERNEL32.@)
695 HFILE WINAPI _lopen( LPCSTR path, INT mode )
697 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
698 return (HFILE)create_file_OF( path, mode & ~OF_CREATE );
701 /***********************************************************************
702 * _lread (KERNEL32.@)
704 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
706 DWORD result;
707 if (!ReadFile( (HANDLE)handle, buffer, count, &result, NULL ))
708 return HFILE_ERROR;
709 return result;
713 /***********************************************************************
714 * _llseek (KERNEL32.@)
716 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
718 return SetFilePointer( (HANDLE)hFile, lOffset, NULL, nOrigin );
722 /***********************************************************************
723 * _lwrite (KERNEL32.@)
725 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
727 return (UINT)_hwrite( hFile, buffer, (LONG)count );
731 /***********************************************************************
732 * FlushFileBuffers (KERNEL32.@)
734 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
736 NTSTATUS nts;
737 IO_STATUS_BLOCK ioblk;
739 if (is_console_handle( hFile ))
741 /* this will fail (as expected) for an output handle */
742 return FlushConsoleInputBuffer( hFile );
744 nts = NtFlushBuffersFile( hFile, &ioblk );
745 if (nts != STATUS_SUCCESS)
747 SetLastError( RtlNtStatusToDosError( nts ) );
748 return FALSE;
751 return TRUE;
755 /***********************************************************************
756 * GetFileType (KERNEL32.@)
758 DWORD WINAPI GetFileType( HANDLE hFile )
760 FILE_FS_DEVICE_INFORMATION info;
761 IO_STATUS_BLOCK io;
762 NTSTATUS status;
764 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
766 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
767 if (status != STATUS_SUCCESS)
769 SetLastError( RtlNtStatusToDosError(status) );
770 return FILE_TYPE_UNKNOWN;
773 switch(info.DeviceType)
775 case FILE_DEVICE_NULL:
776 case FILE_DEVICE_SERIAL_PORT:
777 case FILE_DEVICE_PARALLEL_PORT:
778 case FILE_DEVICE_TAPE:
779 case FILE_DEVICE_UNKNOWN:
780 return FILE_TYPE_CHAR;
781 case FILE_DEVICE_NAMED_PIPE:
782 return FILE_TYPE_PIPE;
783 default:
784 return FILE_TYPE_DISK;
789 /***********************************************************************
790 * GetFileInformationByHandle (KERNEL32.@)
792 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
794 FILE_ALL_INFORMATION all_info;
795 IO_STATUS_BLOCK io;
796 NTSTATUS status;
798 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
799 if (status == STATUS_SUCCESS)
801 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
802 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
803 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
804 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
805 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
806 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
807 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
808 info->dwVolumeSerialNumber = 0; /* FIXME */
809 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
810 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
811 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
812 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
813 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
814 return TRUE;
816 SetLastError( RtlNtStatusToDosError(status) );
817 return FALSE;
821 /***********************************************************************
822 * GetFileSize (KERNEL32.@)
824 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
826 LARGE_INTEGER size;
827 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
828 if (filesizehigh) *filesizehigh = size.u.HighPart;
829 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
830 return size.u.LowPart;
834 /***********************************************************************
835 * GetFileSizeEx (KERNEL32.@)
837 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
839 FILE_END_OF_FILE_INFORMATION info;
840 IO_STATUS_BLOCK io;
841 NTSTATUS status;
843 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileEndOfFileInformation );
844 if (status == STATUS_SUCCESS)
846 *lpFileSize = info.EndOfFile;
847 return TRUE;
849 SetLastError( RtlNtStatusToDosError(status) );
850 return FALSE;
854 /**************************************************************************
855 * SetEndOfFile (KERNEL32.@)
857 BOOL WINAPI SetEndOfFile( HANDLE hFile )
859 FILE_POSITION_INFORMATION pos;
860 FILE_END_OF_FILE_INFORMATION eof;
861 IO_STATUS_BLOCK io;
862 NTSTATUS status;
864 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
865 if (status == STATUS_SUCCESS)
867 eof.EndOfFile = pos.CurrentByteOffset;
868 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
870 if (status == STATUS_SUCCESS) return TRUE;
871 SetLastError( RtlNtStatusToDosError(status) );
872 return FALSE;
876 /***********************************************************************
877 * SetFilePointer (KERNEL32.@)
879 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
881 LARGE_INTEGER dist, newpos;
883 if (highword)
885 dist.u.LowPart = distance;
886 dist.u.HighPart = *highword;
888 else dist.QuadPart = distance;
890 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
892 if (highword) *highword = newpos.u.HighPart;
893 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
894 return newpos.u.LowPart;
898 /***********************************************************************
899 * SetFilePointerEx (KERNEL32.@)
901 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
902 LARGE_INTEGER *newpos, DWORD method )
904 static const int whence[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
905 BOOL ret = FALSE;
906 NTSTATUS status;
907 int fd;
909 TRACE("handle %p offset %s newpos %p origin %ld\n",
910 hFile, wine_dbgstr_longlong(distance.QuadPart), newpos, method );
912 if (method > FILE_END)
914 SetLastError( ERROR_INVALID_PARAMETER );
915 return ret;
918 if (!(status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
920 off_t pos, res;
922 pos = distance.QuadPart;
923 if ((res = lseek( fd, pos, whence[method] )) == (off_t)-1)
925 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
926 if (((errno == EINVAL) || (errno == EPERM)) && (method != FILE_BEGIN) && (pos < 0))
927 SetLastError( ERROR_NEGATIVE_SEEK );
928 else
929 FILE_SetDosError();
931 else
933 ret = TRUE;
934 if( newpos )
935 newpos->QuadPart = res;
937 wine_server_release_fd( hFile, fd );
939 else SetLastError( RtlNtStatusToDosError(status) );
941 return ret;
944 /***********************************************************************
945 * GetFileTime (KERNEL32.@)
947 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
948 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
950 FILE_BASIC_INFORMATION info;
951 IO_STATUS_BLOCK io;
952 NTSTATUS status;
954 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
955 if (status == STATUS_SUCCESS)
957 if (lpCreationTime)
959 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
960 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
962 if (lpLastAccessTime)
964 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
965 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
967 if (lpLastWriteTime)
969 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
970 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
972 return TRUE;
974 SetLastError( RtlNtStatusToDosError(status) );
975 return FALSE;
979 /***********************************************************************
980 * SetFileTime (KERNEL32.@)
982 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
983 const FILETIME *atime, const FILETIME *mtime )
985 FILE_BASIC_INFORMATION info;
986 IO_STATUS_BLOCK io;
987 NTSTATUS status;
989 memset( &info, 0, sizeof(info) );
990 if (ctime)
992 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
993 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
995 if (atime)
997 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
998 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1000 if (mtime)
1002 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1003 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1006 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1007 if (status == STATUS_SUCCESS) return TRUE;
1008 SetLastError( RtlNtStatusToDosError(status) );
1009 return FALSE;
1013 /**************************************************************************
1014 * LockFile (KERNEL32.@)
1016 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1017 DWORD count_low, DWORD count_high )
1019 NTSTATUS status;
1020 LARGE_INTEGER count, offset;
1022 TRACE( "%p %lx%08lx %lx%08lx\n",
1023 hFile, offset_high, offset_low, count_high, count_low );
1025 count.u.LowPart = count_low;
1026 count.u.HighPart = count_high;
1027 offset.u.LowPart = offset_low;
1028 offset.u.HighPart = offset_high;
1030 status = NtLockFile( hFile, 0, NULL, NULL,
1031 NULL, &offset, &count, NULL, TRUE, TRUE );
1033 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1034 return !status;
1038 /**************************************************************************
1039 * LockFileEx [KERNEL32.@]
1041 * Locks a byte range within an open file for shared or exclusive access.
1043 * RETURNS
1044 * success: TRUE
1045 * failure: FALSE
1047 * NOTES
1048 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1050 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1051 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1053 NTSTATUS status;
1054 LARGE_INTEGER count, offset;
1056 if (reserved)
1058 SetLastError( ERROR_INVALID_PARAMETER );
1059 return FALSE;
1062 TRACE( "%p %lx%08lx %lx%08lx flags %lx\n",
1063 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1064 count_high, count_low, flags );
1066 count.u.LowPart = count_low;
1067 count.u.HighPart = count_high;
1068 offset.u.LowPart = overlapped->u.s.Offset;
1069 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1071 status = NtLockFile( hFile, overlapped->hEvent, NULL, NULL,
1072 NULL, &offset, &count, NULL,
1073 flags & LOCKFILE_FAIL_IMMEDIATELY,
1074 flags & LOCKFILE_EXCLUSIVE_LOCK );
1076 if (status) SetLastError( RtlNtStatusToDosError(status) );
1077 return !status;
1081 /**************************************************************************
1082 * UnlockFile (KERNEL32.@)
1084 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1085 DWORD count_low, DWORD count_high )
1087 NTSTATUS status;
1088 LARGE_INTEGER count, offset;
1090 count.u.LowPart = count_low;
1091 count.u.HighPart = count_high;
1092 offset.u.LowPart = offset_low;
1093 offset.u.HighPart = offset_high;
1095 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1096 if (status) SetLastError( RtlNtStatusToDosError(status) );
1097 return !status;
1101 /**************************************************************************
1102 * UnlockFileEx (KERNEL32.@)
1104 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1105 LPOVERLAPPED overlapped )
1107 if (reserved)
1109 SetLastError( ERROR_INVALID_PARAMETER );
1110 return FALSE;
1112 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1114 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1118 /***********************************************************************
1119 * Win32HandleToDosFileHandle (KERNEL32.21)
1121 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1122 * longer valid after this function (even on failure).
1124 * Note: this is not exactly right, since on Win95 the Win32 handles
1125 * are on top of DOS handles and we do it the other way
1126 * around. Should be good enough though.
1128 HFILE WINAPI Win32HandleToDosFileHandle( HANDLE handle )
1130 int i;
1132 if (!handle || (handle == INVALID_HANDLE_VALUE))
1133 return HFILE_ERROR;
1135 FILE_InitProcessDosHandles();
1136 for (i = 0; i < DOS_TABLE_SIZE; i++)
1137 if (!dos_handles[i])
1139 dos_handles[i] = handle;
1140 TRACE("Got %d for h32 %p\n", i, handle );
1141 return (HFILE)i;
1143 CloseHandle( handle );
1144 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1145 return HFILE_ERROR;
1149 /***********************************************************************
1150 * DosFileHandleToWin32Handle (KERNEL32.20)
1152 * Return the Win32 handle for a DOS handle.
1154 * Note: this is not exactly right, since on Win95 the Win32 handles
1155 * are on top of DOS handles and we do it the other way
1156 * around. Should be good enough though.
1158 HANDLE WINAPI DosFileHandleToWin32Handle( HFILE handle )
1160 HFILE16 hfile = (HFILE16)handle;
1161 if (hfile < 5) FILE_InitProcessDosHandles();
1162 if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
1164 SetLastError( ERROR_INVALID_HANDLE );
1165 return INVALID_HANDLE_VALUE;
1167 return dos_handles[hfile];
1171 /*************************************************************************
1172 * SetHandleCount (KERNEL32.@)
1174 UINT WINAPI SetHandleCount( UINT count )
1176 return min( 256, count );
1180 /***********************************************************************
1181 * DisposeLZ32Handle (KERNEL32.22)
1183 * Note: this is not entirely correct, we should only close the
1184 * 32-bit handle and not the 16-bit one, but we cannot do
1185 * this because of the way our DOS handles are implemented.
1186 * It shouldn't break anything though.
1188 void WINAPI DisposeLZ32Handle( HANDLE handle )
1190 int i;
1192 if (!handle || (handle == INVALID_HANDLE_VALUE)) return;
1194 for (i = 5; i < DOS_TABLE_SIZE; i++)
1195 if (dos_handles[i] == handle)
1197 dos_handles[i] = 0;
1198 CloseHandle( handle );
1199 break;
1203 /**************************************************************************
1204 * Operations on file names *
1205 **************************************************************************/
1208 /*************************************************************************
1209 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1211 * Creates or opens an object, and returns a handle that can be used to
1212 * access that object.
1214 * PARAMS
1216 * filename [in] pointer to filename to be accessed
1217 * access [in] access mode requested
1218 * sharing [in] share mode
1219 * sa [in] pointer to security attributes
1220 * creation [in] how to create the file
1221 * attributes [in] attributes for newly created file
1222 * template [in] handle to file with extended attributes to copy
1224 * RETURNS
1225 * Success: Open handle to specified file
1226 * Failure: INVALID_HANDLE_VALUE
1228 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1229 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1230 DWORD attributes, HANDLE template )
1232 NTSTATUS status;
1233 UINT options;
1234 OBJECT_ATTRIBUTES attr;
1235 UNICODE_STRING nameW;
1236 IO_STATUS_BLOCK io;
1237 HANDLE ret;
1238 DWORD dosdev;
1239 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1240 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1241 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1243 static const UINT nt_disposition[5] =
1245 FILE_CREATE, /* CREATE_NEW */
1246 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1247 FILE_OPEN, /* OPEN_EXISTING */
1248 FILE_OPEN_IF, /* OPEN_ALWAYS */
1249 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1253 /* sanity checks */
1255 if (!filename || !filename[0])
1257 SetLastError( ERROR_PATH_NOT_FOUND );
1258 return INVALID_HANDLE_VALUE;
1261 TRACE("%s %s%s%s%s%s%s creation %ld attributes 0x%lx\n", debugstr_w(filename),
1262 (access & GENERIC_READ)?"GENERIC_READ ":"",
1263 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1264 (!access)?"QUERY_ACCESS ":"",
1265 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1266 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1267 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1268 creation, attributes);
1270 /* Open a console for CONIN$ or CONOUT$ */
1272 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1274 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle), creation);
1275 goto done;
1278 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1280 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1281 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1283 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1284 !strncmpiW( filename + 4, pipeW, 5 ) ||
1285 !strncmpiW( filename + 4, mailslotW, 9 ))
1287 dosdev = 0;
1289 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1291 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1293 else if (!(GetVersion() & 0x80000000))
1295 dosdev = 0;
1297 else if (filename[4])
1299 ret = VXD_Open( filename+4, access, sa );
1300 goto done;
1302 else
1304 SetLastError( ERROR_INVALID_NAME );
1305 return INVALID_HANDLE_VALUE;
1308 else dosdev = RtlIsDosDeviceName_U( filename );
1310 if (dosdev)
1312 static const WCHAR conW[] = {'C','O','N'};
1314 if (LOWORD(dosdev) == sizeof(conW) &&
1315 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1317 switch (access & (GENERIC_READ|GENERIC_WRITE))
1319 case GENERIC_READ:
1320 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), creation);
1321 goto done;
1322 case GENERIC_WRITE:
1323 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), creation);
1324 goto done;
1325 default:
1326 SetLastError( ERROR_FILE_NOT_FOUND );
1327 return INVALID_HANDLE_VALUE;
1332 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1334 SetLastError( ERROR_INVALID_PARAMETER );
1335 return INVALID_HANDLE_VALUE;
1338 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1340 SetLastError( ERROR_PATH_NOT_FOUND );
1341 return INVALID_HANDLE_VALUE;
1344 /* now call NtCreateFile */
1346 options = 0;
1347 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1348 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1349 else
1350 options |= FILE_NON_DIRECTORY_FILE;
1351 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1353 options |= FILE_DELETE_ON_CLOSE;
1354 access |= DELETE;
1356 if (!(attributes & FILE_FLAG_OVERLAPPED))
1357 options |= FILE_SYNCHRONOUS_IO_ALERT;
1358 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1359 options |= FILE_RANDOM_ACCESS;
1360 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1362 attr.Length = sizeof(attr);
1363 attr.RootDirectory = 0;
1364 attr.Attributes = OBJ_CASE_INSENSITIVE;
1365 attr.ObjectName = &nameW;
1366 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1367 attr.SecurityQualityOfService = NULL;
1369 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1371 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1372 sharing, nt_disposition[creation - CREATE_NEW],
1373 options, NULL, 0 );
1374 if (status)
1376 WARN("Unable to create file %s (status %lx)\n", debugstr_w(filename), status);
1377 ret = INVALID_HANDLE_VALUE;
1379 /* In the case file creation was rejected due to CREATE_NEW flag
1380 * was specified and file with that name already exists, correct
1381 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1382 * Note: RtlNtStatusToDosError is not the subject to blame here.
1384 if (status == STATUS_OBJECT_NAME_COLLISION)
1385 SetLastError( ERROR_FILE_EXISTS );
1386 else
1387 SetLastError( RtlNtStatusToDosError(status) );
1389 else SetLastError(0);
1390 RtlFreeUnicodeString( &nameW );
1392 done:
1393 if (!ret) ret = INVALID_HANDLE_VALUE;
1394 TRACE("returning %p\n", ret);
1395 return ret;
1400 /*************************************************************************
1401 * CreateFileA (KERNEL32.@)
1403 * See CreateFileW.
1405 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1406 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1407 DWORD attributes, HANDLE template)
1409 WCHAR *nameW;
1411 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1412 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1416 /***********************************************************************
1417 * DeleteFileW (KERNEL32.@)
1419 BOOL WINAPI DeleteFileW( LPCWSTR path )
1421 UNICODE_STRING nameW;
1422 OBJECT_ATTRIBUTES attr;
1423 NTSTATUS status;
1425 TRACE("%s\n", debugstr_w(path) );
1427 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1429 SetLastError( ERROR_PATH_NOT_FOUND );
1430 return FALSE;
1433 attr.Length = sizeof(attr);
1434 attr.RootDirectory = 0;
1435 attr.Attributes = OBJ_CASE_INSENSITIVE;
1436 attr.ObjectName = &nameW;
1437 attr.SecurityDescriptor = NULL;
1438 attr.SecurityQualityOfService = NULL;
1440 status = NtDeleteFile(&attr);
1441 RtlFreeUnicodeString( &nameW );
1442 if (status)
1444 SetLastError( RtlNtStatusToDosError(status) );
1445 return FALSE;
1447 return TRUE;
1451 /***********************************************************************
1452 * DeleteFileA (KERNEL32.@)
1454 BOOL WINAPI DeleteFileA( LPCSTR path )
1456 WCHAR *pathW;
1458 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1459 return DeleteFileW( pathW );
1463 /**************************************************************************
1464 * ReplaceFileW (KERNEL32.@)
1465 * ReplaceFile (KERNEL32.@)
1467 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName,LPCWSTR lpReplacementFileName,
1468 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1469 LPVOID lpExclude, LPVOID lpReserved)
1471 FIXME("(%s,%s,%s,%08lx,%p,%p) stub\n",debugstr_w(lpReplacedFileName),debugstr_w(lpReplacementFileName),
1472 debugstr_w(lpBackupFileName),dwReplaceFlags,lpExclude,lpReserved);
1473 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1474 return FALSE;
1478 /**************************************************************************
1479 * ReplaceFileA (KERNEL32.@)
1481 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1482 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1483 LPVOID lpExclude, LPVOID lpReserved)
1485 FIXME("(%s,%s,%s,%08lx,%p,%p) stub\n",lpReplacedFileName,lpReplacementFileName,
1486 lpBackupFileName,dwReplaceFlags,lpExclude,lpReserved);
1487 SetLastError(ERROR_UNABLE_TO_MOVE_REPLACEMENT);
1488 return FALSE;
1492 /*************************************************************************
1493 * FindFirstFileExW (KERNEL32.@)
1495 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1496 LPVOID data, FINDEX_SEARCH_OPS search_op,
1497 LPVOID filter, DWORD flags)
1499 WCHAR *mask, *p;
1500 FIND_FIRST_INFO *info = NULL;
1501 UNICODE_STRING nt_name;
1502 OBJECT_ATTRIBUTES attr;
1503 IO_STATUS_BLOCK io;
1504 NTSTATUS status;
1506 TRACE("%s %d %p %d %p %lx\n", debugstr_w(filename), level, data, search_op, filter, flags);
1508 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1509 || flags != 0)
1511 FIXME("options not implemented 0x%08x 0x%08lx\n", search_op, flags );
1512 return INVALID_HANDLE_VALUE;
1514 if (level != FindExInfoStandard)
1516 FIXME("info level %d not implemented\n", level );
1517 return INVALID_HANDLE_VALUE;
1520 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1522 SetLastError( ERROR_PATH_NOT_FOUND );
1523 return INVALID_HANDLE_VALUE;
1526 if (!mask || !*mask)
1528 SetLastError( ERROR_FILE_NOT_FOUND );
1529 goto error;
1532 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1534 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1535 goto error;
1538 if (!RtlCreateUnicodeString( &info->mask, mask ))
1540 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1541 goto error;
1544 /* truncate dir name before mask */
1545 *mask = 0;
1546 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1548 /* check if path is the root of the drive */
1549 info->is_root = FALSE;
1550 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1551 if (p[0] && p[1] == ':')
1553 p += 2;
1554 while (*p == '\\') p++;
1555 info->is_root = (*p == 0);
1558 attr.Length = sizeof(attr);
1559 attr.RootDirectory = 0;
1560 attr.Attributes = OBJ_CASE_INSENSITIVE;
1561 attr.ObjectName = &nt_name;
1562 attr.SecurityDescriptor = NULL;
1563 attr.SecurityQualityOfService = NULL;
1565 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1566 FILE_SHARE_READ | FILE_SHARE_WRITE,
1567 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1569 if (status != STATUS_SUCCESS)
1571 RtlFreeUnicodeString( &info->mask );
1572 SetLastError( RtlNtStatusToDosError(status) );
1573 goto error;
1576 RtlInitializeCriticalSection( &info->cs );
1577 info->path = nt_name;
1578 info->magic = FIND_FIRST_MAGIC;
1579 info->data_pos = 0;
1580 info->data_len = 0;
1581 info->search_op = search_op;
1583 if (!FindNextFileW( (HANDLE)info, data ))
1585 TRACE( "%s not found\n", debugstr_w(filename) );
1586 FindClose( (HANDLE)info );
1587 SetLastError( ERROR_FILE_NOT_FOUND );
1588 return INVALID_HANDLE_VALUE;
1590 return (HANDLE)info;
1592 error:
1593 HeapFree( GetProcessHeap(), 0, info );
1594 RtlFreeUnicodeString( &nt_name );
1595 return INVALID_HANDLE_VALUE;
1599 /*************************************************************************
1600 * FindNextFileW (KERNEL32.@)
1602 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1604 FIND_FIRST_INFO *info;
1605 FILE_BOTH_DIR_INFORMATION *dir_info;
1606 BOOL ret = FALSE;
1608 TRACE("%p %p\n", handle, data);
1610 if (!handle || handle == INVALID_HANDLE_VALUE)
1612 SetLastError( ERROR_INVALID_HANDLE );
1613 return ret;
1615 info = (FIND_FIRST_INFO *)handle;
1616 if (info->magic != FIND_FIRST_MAGIC)
1618 SetLastError( ERROR_INVALID_HANDLE );
1619 return ret;
1622 RtlEnterCriticalSection( &info->cs );
1624 for (;;)
1626 if (info->data_pos >= info->data_len) /* need to read some more data */
1628 IO_STATUS_BLOCK io;
1630 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1631 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
1632 if (io.u.Status)
1634 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1635 break;
1637 info->data_len = io.Information;
1638 info->data_pos = 0;
1641 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
1643 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
1644 else info->data_pos = info->data_len;
1646 /* don't return '.' and '..' in the root of the drive */
1647 if (info->is_root)
1649 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
1650 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
1651 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
1654 /* check for dir symlink */
1655 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
1656 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
1658 if (!check_dir_symlink( info, dir_info )) continue;
1660 if (info->search_op == FindExSearchLimitToDirectories &&
1661 (dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
1662 continue;
1664 data->dwFileAttributes = dir_info->FileAttributes;
1665 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
1666 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
1667 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
1668 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
1669 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
1670 data->dwReserved0 = 0;
1671 data->dwReserved1 = 0;
1673 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
1674 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1675 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
1676 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
1678 TRACE("returning %s (%s)\n",
1679 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
1681 ret = TRUE;
1682 break;
1685 RtlLeaveCriticalSection( &info->cs );
1686 return ret;
1690 /*************************************************************************
1691 * FindClose (KERNEL32.@)
1693 BOOL WINAPI FindClose( HANDLE handle )
1695 FIND_FIRST_INFO *info = (FIND_FIRST_INFO *)handle;
1697 if (!handle || handle == INVALID_HANDLE_VALUE)
1699 SetLastError( ERROR_INVALID_HANDLE );
1700 return FALSE;
1703 __TRY
1705 if (info->magic == FIND_FIRST_MAGIC)
1707 RtlEnterCriticalSection( &info->cs );
1708 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
1710 info->magic = 0;
1711 if (info->handle) CloseHandle( info->handle );
1712 info->handle = 0;
1713 RtlFreeUnicodeString( &info->mask );
1714 info->mask.Buffer = NULL;
1715 RtlFreeUnicodeString( &info->path );
1716 info->data_pos = 0;
1717 info->data_len = 0;
1718 RtlLeaveCriticalSection( &info->cs );
1719 RtlDeleteCriticalSection( &info->cs );
1720 HeapFree( GetProcessHeap(), 0, info );
1724 __EXCEPT_PAGE_FAULT
1726 WARN("Illegal handle %p\n", handle);
1727 SetLastError( ERROR_INVALID_HANDLE );
1728 return FALSE;
1730 __ENDTRY
1732 return TRUE;
1736 /*************************************************************************
1737 * FindFirstFileA (KERNEL32.@)
1739 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
1741 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1742 FindExSearchNameMatch, NULL, 0);
1745 /*************************************************************************
1746 * FindFirstFileExA (KERNEL32.@)
1748 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
1749 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
1750 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
1752 HANDLE handle;
1753 WIN32_FIND_DATAA *dataA;
1754 WIN32_FIND_DATAW dataW;
1755 WCHAR *nameW;
1757 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
1759 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1760 if (handle == INVALID_HANDLE_VALUE) return handle;
1762 dataA = (WIN32_FIND_DATAA *) lpFindFileData;
1763 dataA->dwFileAttributes = dataW.dwFileAttributes;
1764 dataA->ftCreationTime = dataW.ftCreationTime;
1765 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
1766 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
1767 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
1768 dataA->nFileSizeLow = dataW.nFileSizeLow;
1769 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
1770 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
1771 sizeof(dataA->cAlternateFileName) );
1772 return handle;
1776 /*************************************************************************
1777 * FindFirstFileW (KERNEL32.@)
1779 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1781 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1782 FindExSearchNameMatch, NULL, 0);
1786 /*************************************************************************
1787 * FindNextFileA (KERNEL32.@)
1789 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1791 WIN32_FIND_DATAW dataW;
1793 if (!FindNextFileW( handle, &dataW )) return FALSE;
1794 data->dwFileAttributes = dataW.dwFileAttributes;
1795 data->ftCreationTime = dataW.ftCreationTime;
1796 data->ftLastAccessTime = dataW.ftLastAccessTime;
1797 data->ftLastWriteTime = dataW.ftLastWriteTime;
1798 data->nFileSizeHigh = dataW.nFileSizeHigh;
1799 data->nFileSizeLow = dataW.nFileSizeLow;
1800 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
1801 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
1802 sizeof(data->cAlternateFileName) );
1803 return TRUE;
1807 /**************************************************************************
1808 * GetFileAttributesW (KERNEL32.@)
1810 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
1812 FILE_BASIC_INFORMATION info;
1813 UNICODE_STRING nt_name;
1814 OBJECT_ATTRIBUTES attr;
1815 NTSTATUS status;
1817 TRACE("%s\n", debugstr_w(name));
1819 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1821 SetLastError( ERROR_PATH_NOT_FOUND );
1822 return INVALID_FILE_ATTRIBUTES;
1825 attr.Length = sizeof(attr);
1826 attr.RootDirectory = 0;
1827 attr.Attributes = OBJ_CASE_INSENSITIVE;
1828 attr.ObjectName = &nt_name;
1829 attr.SecurityDescriptor = NULL;
1830 attr.SecurityQualityOfService = NULL;
1832 status = NtQueryAttributesFile( &attr, &info );
1833 RtlFreeUnicodeString( &nt_name );
1835 if (status == STATUS_SUCCESS) return info.FileAttributes;
1837 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1838 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
1840 SetLastError( RtlNtStatusToDosError(status) );
1841 return INVALID_FILE_ATTRIBUTES;
1845 /**************************************************************************
1846 * GetFileAttributesA (KERNEL32.@)
1848 DWORD WINAPI GetFileAttributesA( LPCSTR name )
1850 WCHAR *nameW;
1852 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
1853 return GetFileAttributesW( nameW );
1857 /**************************************************************************
1858 * SetFileAttributesW (KERNEL32.@)
1860 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
1862 UNICODE_STRING nt_name;
1863 OBJECT_ATTRIBUTES attr;
1864 IO_STATUS_BLOCK io;
1865 NTSTATUS status;
1866 HANDLE handle;
1868 TRACE("%s %lx\n", debugstr_w(name), attributes);
1870 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1872 SetLastError( ERROR_PATH_NOT_FOUND );
1873 return FALSE;
1876 attr.Length = sizeof(attr);
1877 attr.RootDirectory = 0;
1878 attr.Attributes = OBJ_CASE_INSENSITIVE;
1879 attr.ObjectName = &nt_name;
1880 attr.SecurityDescriptor = NULL;
1881 attr.SecurityQualityOfService = NULL;
1883 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1884 RtlFreeUnicodeString( &nt_name );
1886 if (status == STATUS_SUCCESS)
1888 FILE_BASIC_INFORMATION info;
1890 memset( &info, 0, sizeof(info) );
1891 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
1892 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
1893 NtClose( handle );
1896 if (status == STATUS_SUCCESS) return TRUE;
1897 SetLastError( RtlNtStatusToDosError(status) );
1898 return FALSE;
1902 /**************************************************************************
1903 * SetFileAttributesA (KERNEL32.@)
1905 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
1907 WCHAR *nameW;
1909 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
1910 return SetFileAttributesW( nameW, attributes );
1914 /**************************************************************************
1915 * GetFileAttributesExW (KERNEL32.@)
1917 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
1919 FILE_NETWORK_OPEN_INFORMATION info;
1920 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
1921 UNICODE_STRING nt_name;
1922 OBJECT_ATTRIBUTES attr;
1923 NTSTATUS status;
1925 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
1927 if (level != GetFileExInfoStandard)
1929 SetLastError( ERROR_INVALID_PARAMETER );
1930 return FALSE;
1933 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1935 SetLastError( ERROR_PATH_NOT_FOUND );
1936 return FALSE;
1939 attr.Length = sizeof(attr);
1940 attr.RootDirectory = 0;
1941 attr.Attributes = OBJ_CASE_INSENSITIVE;
1942 attr.ObjectName = &nt_name;
1943 attr.SecurityDescriptor = NULL;
1944 attr.SecurityQualityOfService = NULL;
1946 status = NtQueryFullAttributesFile( &attr, &info );
1947 RtlFreeUnicodeString( &nt_name );
1949 if (status != STATUS_SUCCESS)
1951 SetLastError( RtlNtStatusToDosError(status) );
1952 return FALSE;
1955 data->dwFileAttributes = info.FileAttributes;
1956 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
1957 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
1958 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
1959 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
1960 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
1961 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
1962 data->nFileSizeLow = info.EndOfFile.u.LowPart;
1963 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
1964 return TRUE;
1968 /**************************************************************************
1969 * GetFileAttributesExA (KERNEL32.@)
1971 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
1973 WCHAR *nameW;
1975 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
1976 return GetFileAttributesExW( nameW, level, ptr );
1980 /******************************************************************************
1981 * GetCompressedFileSizeW (KERNEL32.@)
1983 * Get the actual number of bytes used on disk.
1985 * RETURNS
1986 * Success: Low-order doubleword of number of bytes
1987 * Failure: INVALID_FILE_SIZE
1989 DWORD WINAPI GetCompressedFileSizeW(
1990 LPCWSTR name, /* [in] Pointer to name of file */
1991 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
1993 UNICODE_STRING nt_name;
1994 OBJECT_ATTRIBUTES attr;
1995 IO_STATUS_BLOCK io;
1996 NTSTATUS status;
1997 HANDLE handle;
1998 DWORD ret = INVALID_FILE_SIZE;
2000 TRACE("%s %p\n", debugstr_w(name), size_high);
2002 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2004 SetLastError( ERROR_PATH_NOT_FOUND );
2005 return INVALID_FILE_SIZE;
2008 attr.Length = sizeof(attr);
2009 attr.RootDirectory = 0;
2010 attr.Attributes = OBJ_CASE_INSENSITIVE;
2011 attr.ObjectName = &nt_name;
2012 attr.SecurityDescriptor = NULL;
2013 attr.SecurityQualityOfService = NULL;
2015 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2016 RtlFreeUnicodeString( &nt_name );
2018 if (status == STATUS_SUCCESS)
2020 /* we don't support compressed files, simply return the file size */
2021 ret = GetFileSize( handle, size_high );
2022 NtClose( handle );
2024 else SetLastError( RtlNtStatusToDosError(status) );
2026 return ret;
2030 /******************************************************************************
2031 * GetCompressedFileSizeA (KERNEL32.@)
2033 * See GetCompressedFileSizeW.
2035 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2037 WCHAR *nameW;
2039 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2040 return GetCompressedFileSizeW( nameW, size_high );
2044 /***********************************************************************
2045 * OpenFile (KERNEL32.@)
2047 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2049 HANDLE handle;
2050 FILETIME filetime;
2051 WORD filedatetime[2];
2053 if (!ofs) return HFILE_ERROR;
2055 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2056 ((mode & 0x3 )==OF_READ)?"OF_READ":
2057 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2058 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2059 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2060 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2061 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2062 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2063 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2064 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2065 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2066 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2067 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2068 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2069 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2070 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2071 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2072 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2076 ofs->cBytes = sizeof(OFSTRUCT);
2077 ofs->nErrCode = 0;
2078 if (mode & OF_REOPEN) name = ofs->szPathName;
2080 if (!name) return HFILE_ERROR;
2082 TRACE("%s %04x\n", name, mode );
2084 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2085 Are there any cases where getting the path here is wrong?
2086 Uwe Bonnes 1997 Apr 2 */
2087 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2089 /* OF_PARSE simply fills the structure */
2091 if (mode & OF_PARSE)
2093 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2094 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2095 return 0;
2098 /* OF_CREATE is completely different from all other options, so
2099 handle it first */
2101 if (mode & OF_CREATE)
2103 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2104 goto error;
2106 else
2108 /* Now look for the file */
2110 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2111 goto error;
2113 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2115 if (mode & OF_DELETE)
2117 if (!DeleteFileA( ofs->szPathName )) goto error;
2118 TRACE("(%s): OF_DELETE return = OK\n", name);
2119 return TRUE;
2122 handle = (HANDLE)_lopen( ofs->szPathName, mode );
2123 if (handle == INVALID_HANDLE_VALUE) goto error;
2125 GetFileTime( handle, NULL, NULL, &filetime );
2126 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2127 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2129 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2131 CloseHandle( handle );
2132 WARN("(%s): OF_VERIFY failed\n", name );
2133 /* FIXME: what error here? */
2134 SetLastError( ERROR_FILE_NOT_FOUND );
2135 goto error;
2138 ofs->Reserved1 = filedatetime[0];
2139 ofs->Reserved2 = filedatetime[1];
2141 TRACE("(%s): OK, return = %p\n", name, handle );
2142 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2144 CloseHandle( handle );
2145 return TRUE;
2147 else return (HFILE)handle;
2149 error: /* We get here if there was an error opening the file */
2150 ofs->nErrCode = GetLastError();
2151 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2152 return HFILE_ERROR;