1 /*
2 * File handling functions
3 *
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 2008 Jeff Zaroyko
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "config.h"
24 #include "wine/port.h"
25
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
32
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 "wine/winbase16.h"
44 #include "kernel_private.h"
45
46 #include "wine/exception.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(file);
51
52 /* info structure for FindFirstFile handle */
53 typedef struct
54 {
55 DWORD magic; /* magic number */
56 HANDLE handle; /* handle to directory */
57 CRITICAL_SECTION cs; /* crit section protecting this structure */
58 FINDEX_SEARCH_OPS search_op; /* Flags passed to FindFirst. */
59 UNICODE_STRING mask; /* file mask */
60 UNICODE_STRING path; /* NT path used to open the directory */
61 BOOL is_root; /* is directory the root of the drive? */
62 UINT data_pos; /* current position in dir data */
63 UINT data_len; /* length of dir data */
64 BYTE data[8192]; /* directory data */
65 } FIND_FIRST_INFO;
66
67 #define FIND_FIRST_MAGIC 0xc0ffee11
68
69 static BOOL oem_file_apis;
70
71 static const WCHAR wildcardsW[] = { '*','?',0 };
72
73 /***********************************************************************
74 * create_file_OF
75 *
76 * Wrapper for CreateFile that takes OF_* mode flags.
77 */
78 static HANDLE create_file_OF( LPCSTR path, INT mode )
79 {
80 DWORD access, sharing, creation;
81
82 if (mode & OF_CREATE)
83 {
84 creation = CREATE_ALWAYS;
85 access = GENERIC_READ | GENERIC_WRITE;
86 }
87 else
88 {
89 creation = OPEN_EXISTING;
90 switch(mode & 0x03)
91 {
92 case OF_READ: access = GENERIC_READ; break;
93 case OF_WRITE: access = GENERIC_WRITE; break;
94 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
95 default: access = 0; break;
96 }
97 }
98
99 switch(mode & 0x70)
100 {
101 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
102 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
103 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
104 case OF_SHARE_DENY_NONE:
105 case OF_SHARE_COMPAT:
106 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
107 }
108 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
109 }
110
111
112 /***********************************************************************
113 * check_dir_symlink
114 *
115 * Check if a dir symlink should be returned by FindNextFile.
116 */
117 static BOOL check_dir_symlink( FIND_FIRST_INFO *info, const FILE_BOTH_DIR_INFORMATION *file_info )
118 {
119 UNICODE_STRING str;
120 ANSI_STRING unix_name;
121 struct stat st, parent_st;
122 BOOL ret = TRUE;
123 DWORD len;
124
125 str.MaximumLength = info->path.Length + sizeof(WCHAR) + file_info->FileNameLength;
126 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength ))) return TRUE;
127 memcpy( str.Buffer, info->path.Buffer, info->path.Length );
128 len = info->path.Length / sizeof(WCHAR);
129 if (!len || str.Buffer[len-1] != '\\') str.Buffer[len++] = '\\';
130 memcpy( str.Buffer + len, file_info->FileName, file_info->FileNameLength );
131 str.Length = len * sizeof(WCHAR) + file_info->FileNameLength;
132
133 unix_name.Buffer = NULL;
134 if (!wine_nt_to_unix_file_name( &str, &unix_name, OPEN_EXISTING, FALSE ) &&
135 !stat( unix_name.Buffer, &st ))
136 {
137 char *p = unix_name.Buffer + unix_name.Length - 1;
138
139 /* skip trailing slashes */
140 while (p > unix_name.Buffer && *p == '/') p--;
141
142 while (ret && p > unix_name.Buffer)
143 {
144 while (p > unix_name.Buffer && *p != '/') p--;
145 while (p > unix_name.Buffer && *p == '/') p--;
146 p[1] = 0;
147 if (!stat( unix_name.Buffer, &parent_st ) &&
148 parent_st.st_dev == st.st_dev &&
149 parent_st.st_ino == st.st_ino)
150 {
151 WARN( "suppressing dir symlink %s pointing to parent %s\n",
152 debugstr_wn( str.Buffer, str.Length/sizeof(WCHAR) ),
153 debugstr_a( unix_name.Buffer ));
154 ret = FALSE;
155 }
156 }
157 }
158 RtlFreeAnsiString( &unix_name );
159 RtlFreeUnicodeString( &str );
160 return ret;
161 }
162
163
164 /***********************************************************************
165 * FILE_SetDosError
166 *
167 * Set the DOS error code from errno.
168 */
169 void FILE_SetDosError(void)
170 {
171 int save_errno = errno; /* errno gets overwritten by printf */
172
173 TRACE("errno = %d %s\n", errno, strerror(errno));
174 switch (save_errno)
175 {
176 case EAGAIN:
177 SetLastError( ERROR_SHARING_VIOLATION );
178 break;
179 case EBADF:
180 SetLastError( ERROR_INVALID_HANDLE );
181 break;
182 case ENOSPC:
183 SetLastError( ERROR_HANDLE_DISK_FULL );
184 break;
185 case EACCES:
186 case EPERM:
187 case EROFS:
188 SetLastError( ERROR_ACCESS_DENIED );
189 break;
190 case EBUSY:
191 SetLastError( ERROR_LOCK_VIOLATION );
192 break;
193 case ENOENT:
194 SetLastError( ERROR_FILE_NOT_FOUND );
195 break;
196 case EISDIR:
197 SetLastError( ERROR_CANNOT_MAKE );
198 break;
199 case ENFILE:
200 case EMFILE:
201 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
202 break;
203 case EEXIST:
204 SetLastError( ERROR_FILE_EXISTS );
205 break;
206 case EINVAL:
207 case ESPIPE:
208 SetLastError( ERROR_SEEK );
209 break;
210 case ENOTEMPTY:
211 SetLastError( ERROR_DIR_NOT_EMPTY );
212 break;
213 case ENOEXEC:
214 SetLastError( ERROR_BAD_FORMAT );
215 break;
216 case ENOTDIR:
217 SetLastError( ERROR_PATH_NOT_FOUND );
218 break;
219 case EXDEV:
220 SetLastError( ERROR_NOT_SAME_DEVICE );
221 break;
222 default:
223 WARN("unknown file error: %s\n", strerror(save_errno) );
224 SetLastError( ERROR_GEN_FAILURE );
225 break;
226 }
227 errno = save_errno;
228 }
229
230
231 /***********************************************************************
232 * FILE_name_AtoW
233 *
234 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
235 *
236 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
237 * there is no possibility for the function to do that twice, taking into
238 * account any called function.
239 */
240 WCHAR *FILE_name_AtoW( LPCSTR name, BOOL alloc )
241 {
242 ANSI_STRING str;
243 UNICODE_STRING strW, *pstrW;
244 NTSTATUS status;
245
246 RtlInitAnsiString( &str, name );
247 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
248 if (oem_file_apis)
249 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
250 else
251 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
252 if (status == STATUS_SUCCESS) return pstrW->Buffer;
253
254 if (status == STATUS_BUFFER_OVERFLOW)
255 SetLastError( ERROR_FILENAME_EXCED_RANGE );
256 else
257 SetLastError( RtlNtStatusToDosError(status) );
258 return NULL;
259 }
260
261
262 /***********************************************************************
263 * FILE_name_WtoA
264 *
265 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
266 */
267 DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
268 {
269 DWORD ret;
270
271 if (srclen < 0) srclen = strlenW( src ) + 1;
272 if (oem_file_apis)
273 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
274 else
275 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
276 return ret;
277 }
278
279
280 /**************************************************************************
281 * SetFileApisToOEM (KERNEL32.@)
282 */
283 VOID WINAPI SetFileApisToOEM(void)
284 {
285 oem_file_apis = TRUE;
286 }
287
288
289 /**************************************************************************
290 * SetFileApisToANSI (KERNEL32.@)
291 */
292 VOID WINAPI SetFileApisToANSI(void)
293 {
294 oem_file_apis = FALSE;
295 }
296
297
298 /******************************************************************************
299 * AreFileApisANSI (KERNEL32.@)
300 *
301 * Determines if file functions are using ANSI
302 *
303 * RETURNS
304 * TRUE: Set of file functions is using ANSI code page
305 * FALSE: Set of file functions is using OEM code page
306 */
307 BOOL WINAPI AreFileApisANSI(void)
308 {
309 return !oem_file_apis;
310 }
311
312
313 /**************************************************************************
314 * Operations on file handles *
315 **************************************************************************/
316
317 /******************************************************************
318 * FILE_ReadWriteApc (internal)
319 */
320 static void WINAPI FILE_ReadWriteApc(void* apc_user, PIO_STATUS_BLOCK io_status, ULONG reserved)
321 {
322 LPOVERLAPPED_COMPLETION_ROUTINE cr = apc_user;
323
324 cr(RtlNtStatusToDosError(io_status->u.Status), io_status->Information, (LPOVERLAPPED)io_status);
325 }
326
327
328 /***********************************************************************
329 * ReadFileEx (KERNEL32.@)
330 */
331 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
332 LPOVERLAPPED overlapped,
333 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
334 {
335 LARGE_INTEGER offset;
336 NTSTATUS status;
337 PIO_STATUS_BLOCK io_status;
338
339 TRACE("(hFile=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n", hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
340
341 if (!overlapped)
342 {
343 SetLastError(ERROR_INVALID_PARAMETER);
344 return FALSE;
345 }
346
347 offset.u.LowPart = overlapped->u.s.Offset;
348 offset.u.HighPart = overlapped->u.s.OffsetHigh;
349 io_status = (PIO_STATUS_BLOCK)overlapped;
350 io_status->u.Status = STATUS_PENDING;
351 io_status->Information = 0;
352
353 status = NtReadFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
354 io_status, buffer, bytesToRead, &offset, NULL);
355
356 if (status)
357 {
358 SetLastError( RtlNtStatusToDosError(status) );
359 return FALSE;
360 }
361 return TRUE;
362 }
363
364
365 /***********************************************************************
366 * ReadFileScatter (KERNEL32.@)
367 */
368 BOOL WINAPI ReadFileScatter( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
369 LPDWORD reserved, LPOVERLAPPED overlapped )
370 {
371 PIO_STATUS_BLOCK io_status;
372 LARGE_INTEGER offset;
373 NTSTATUS status;
374
375 TRACE( "(%p %p %u %p)\n", file, segments, count, overlapped );
376
377 offset.u.LowPart = overlapped->u.s.Offset;
378 offset.u.HighPart = overlapped->u.s.OffsetHigh;
379 io_status = (PIO_STATUS_BLOCK)overlapped;
380 io_status->u.Status = STATUS_PENDING;
381 io_status->Information = 0;
382
383 status = NtReadFileScatter( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
384 if (status) SetLastError( RtlNtStatusToDosError(status) );
385 return !status;
386 }
387
388
389 /***********************************************************************
390 * ReadFile (KERNEL32.@)
391 */
392 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
393 LPDWORD bytesRead, LPOVERLAPPED overlapped )
394 {
395 LARGE_INTEGER offset;
396 PLARGE_INTEGER poffset = NULL;
397 IO_STATUS_BLOCK iosb;
398 PIO_STATUS_BLOCK io_status = &iosb;
399 HANDLE hEvent = 0;
400 NTSTATUS status;
401 LPVOID cvalue = NULL;
402
403 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToRead,
404 bytesRead, overlapped );
405
406 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
407 if (!bytesToRead) return TRUE;
408
409 if (is_console_handle(hFile))
410 return ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
411
412 if (overlapped != NULL)
413 {
414 offset.u.LowPart = overlapped->u.s.Offset;
415 offset.u.HighPart = overlapped->u.s.OffsetHigh;
416 poffset = &offset;
417 hEvent = overlapped->hEvent;
418 io_status = (PIO_STATUS_BLOCK)overlapped;
419 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
420 }
421 io_status->u.Status = STATUS_PENDING;
422 io_status->Information = 0;
423
424 status = NtReadFile(hFile, hEvent, NULL, cvalue, io_status, buffer, bytesToRead, poffset, NULL);
425
426 if (status == STATUS_PENDING && !overlapped)
427 {
428 WaitForSingleObject( hFile, INFINITE );
429 status = io_status->u.Status;
430 }
431
432 if (status != STATUS_PENDING && bytesRead)
433 *bytesRead = io_status->Information;
434
435 if (status && status != STATUS_END_OF_FILE && status != STATUS_TIMEOUT)
436 {
437 SetLastError( RtlNtStatusToDosError(status) );
438 return FALSE;
439 }
440 return TRUE;
441 }
442
443
444 /***********************************************************************
445 * WriteFileEx (KERNEL32.@)
446 */
447 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
448 LPOVERLAPPED overlapped,
449 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
450 {
451 LARGE_INTEGER offset;
452 NTSTATUS status;
453 PIO_STATUS_BLOCK io_status;
454
455 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
456
457 if (overlapped == NULL)
458 {
459 SetLastError(ERROR_INVALID_PARAMETER);
460 return FALSE;
461 }
462 offset.u.LowPart = overlapped->u.s.Offset;
463 offset.u.HighPart = overlapped->u.s.OffsetHigh;
464
465 io_status = (PIO_STATUS_BLOCK)overlapped;
466 io_status->u.Status = STATUS_PENDING;
467 io_status->Information = 0;
468
469 status = NtWriteFile(hFile, NULL, FILE_ReadWriteApc, lpCompletionRoutine,
470 io_status, buffer, bytesToWrite, &offset, NULL);
471
472 if (status) SetLastError( RtlNtStatusToDosError(status) );
473 return !status;
474 }
475
476
477 /***********************************************************************
478 * WriteFileGather (KERNEL32.@)
479 */
480 BOOL WINAPI WriteFileGather( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
481 LPDWORD reserved, LPOVERLAPPED overlapped )
482 {
483 PIO_STATUS_BLOCK io_status;
484 LARGE_INTEGER offset;
485 NTSTATUS status;
486
487 TRACE( "%p %p %u %p\n", file, segments, count, overlapped );
488
489 offset.u.LowPart = overlapped->u.s.Offset;
490 offset.u.HighPart = overlapped->u.s.OffsetHigh;
491 io_status = (PIO_STATUS_BLOCK)overlapped;
492 io_status->u.Status = STATUS_PENDING;
493 io_status->Information = 0;
494
495 status = NtWriteFileGather( file, NULL, NULL, NULL, io_status, segments, count, &offset, NULL );
496 if (status) SetLastError( RtlNtStatusToDosError(status) );
497 return !status;
498 }
499
500
501 /***********************************************************************
502 * WriteFile (KERNEL32.@)
503 */
504 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
505 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
506 {
507 HANDLE hEvent = NULL;
508 LARGE_INTEGER offset;
509 PLARGE_INTEGER poffset = NULL;
510 NTSTATUS status;
511 IO_STATUS_BLOCK iosb;
512 PIO_STATUS_BLOCK piosb = &iosb;
513 LPVOID cvalue = NULL;
514
515 TRACE("%p %p %d %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
516
517 if (is_console_handle(hFile))
518 return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
519
520 if (overlapped)
521 {
522 offset.u.LowPart = overlapped->u.s.Offset;
523 offset.u.HighPart = overlapped->u.s.OffsetHigh;
524 poffset = &offset;
525 hEvent = overlapped->hEvent;
526 piosb = (PIO_STATUS_BLOCK)overlapped;
527 if (((ULONG_PTR)hEvent & 1) == 0) cvalue = overlapped;
528 }
529 piosb->u.Status = STATUS_PENDING;
530 piosb->Information = 0;
531
532 status = NtWriteFile(hFile, hEvent, NULL, cvalue, piosb,
533 buffer, bytesToWrite, poffset, NULL);
534
535 if (status == STATUS_PENDING && !overlapped)
536 {
537 WaitForSingleObject( hFile, INFINITE );
538 status = piosb->u.Status;
539 }
540
541 if (status != STATUS_PENDING && bytesWritten)
542 *bytesWritten = piosb->Information;
543
544 if (status && status != STATUS_TIMEOUT)
545 {
546 SetLastError( RtlNtStatusToDosError(status) );
547 return FALSE;
548 }
549 return TRUE;
550 }
551
552
553 /***********************************************************************
554 * GetOverlappedResult (KERNEL32.@)
555 *
556 * Check the result of an Asynchronous data transfer from a file.
557 *
558 * Parameters
559 * HANDLE hFile [in] handle of file to check on
560 * LPOVERLAPPED lpOverlapped [in/out] pointer to overlapped
561 * LPDWORD lpTransferred [in/out] number of bytes transferred
562 * BOOL bWait [in] wait for the transfer to complete ?
563 *
564 * RETURNS
565 * TRUE on success
566 * FALSE on failure
567 *
568 * If successful (and relevant) lpTransferred will hold the number of
569 * bytes transferred during the async operation.
570 */
571 BOOL WINAPI GetOverlappedResult(HANDLE hFile, LPOVERLAPPED lpOverlapped,
572 LPDWORD lpTransferred, BOOL bWait)
573 {
574 NTSTATUS status;
575
576 TRACE( "(%p %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait );
577
578 status = lpOverlapped->Internal;
579 if (status == STATUS_PENDING)
580 {
581 if (!bWait)
582 {
583 SetLastError( ERROR_IO_INCOMPLETE );
584 return FALSE;
585 }
586
587 if (WaitForSingleObject( lpOverlapped->hEvent ? lpOverlapped->hEvent : hFile,
588 INFINITE ) == WAIT_FAILED)
589 return FALSE;
590 status = lpOverlapped->Internal;
591 }
592
593 *lpTransferred = lpOverlapped->InternalHigh;
594
595 if (status) SetLastError( RtlNtStatusToDosError(status) );
596 return !status;
597 }
598
599 /***********************************************************************
600 * CancelIoEx (KERNEL32.@)
601 *
602 * Cancels pending I/O operations on a file given the overlapped used.
603 *
604 * PARAMS
605 * handle [I] File handle.
606 * lpOverlapped [I,OPT] pointer to overlapped (if null, cancel all)
607 *
608 * RETURNS
609 * Success: TRUE.
610 * Failure: FALSE, check GetLastError().
611 */
612 BOOL WINAPI CancelIoEx(HANDLE handle, LPOVERLAPPED lpOverlapped)
613 {
614 IO_STATUS_BLOCK io_status;
615
616 NtCancelIoFileEx(handle, (PIO_STATUS_BLOCK) lpOverlapped, &io_status);
617 if (io_status.u.Status)
618 {
619 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
620 return FALSE;
621 }
622 return TRUE;
623 }
624
625 /***********************************************************************
626 * CancelIo (KERNEL32.@)
627 *
628 * Cancels pending I/O operations initiated by the current thread on a file.
629 *
630 * PARAMS
631 * handle [I] File handle.
632 *
633 * RETURNS
634 * Success: TRUE.
635 * Failure: FALSE, check GetLastError().
636 */
637 BOOL WINAPI CancelIo(HANDLE handle)
638 {
639 IO_STATUS_BLOCK io_status;
640
641 NtCancelIoFile(handle, &io_status);
642 if (io_status.u.Status)
643 {
644 SetLastError( RtlNtStatusToDosError( io_status.u.Status ) );
645 return FALSE;
646 }
647 return TRUE;
648 }
649
650 /***********************************************************************
651 * _hread (KERNEL32.@)
652 */
653 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
654 {
655 return _lread( hFile, buffer, count );
656 }
657
658
659 /***********************************************************************
660 * _hwrite (KERNEL32.@)
661 *
662 * experimentation yields that _lwrite:
663 * o truncates the file at the current position with
664 * a 0 len write
665 * o returns 0 on a 0 length write
666 * o works with console handles
667 *
668 */
669 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
670 {
671 DWORD result;
672
673 TRACE("%d %p %d\n", handle, buffer, count );
674
675 if (!count)
676 {
677 /* Expand or truncate at current position */
678 if (!SetEndOfFile( LongToHandle(handle) )) return HFILE_ERROR;
679 return 0;
680 }
681 if (!WriteFile( LongToHandle(handle), buffer, count, &result, NULL ))
682 return HFILE_ERROR;
683 return result;
684 }
685
686
687 /***********************************************************************
688 * _lclose (KERNEL32.@)
689 */
690 HFILE WINAPI _lclose( HFILE hFile )
691 {
692 TRACE("handle %d\n", hFile );
693 return CloseHandle( LongToHandle(hFile) ) ? 0 : HFILE_ERROR;
694 }
695
696
697 /***********************************************************************
698 * _lcreat (KERNEL32.@)
699 */
700 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
701 {
702 HANDLE hfile;
703
704 /* Mask off all flags not explicitly allowed by the doc */
705 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
706 TRACE("%s %02x\n", path, attr );
707 hfile = CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
708 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
709 CREATE_ALWAYS, attr, 0 );
710 return HandleToLong(hfile);
711 }
712
713
714 /***********************************************************************
715 * _lopen (KERNEL32.@)
716 */
717 HFILE WINAPI _lopen( LPCSTR path, INT mode )
718 {
719 HANDLE hfile;
720
721 TRACE("(%s,%04x)\n", debugstr_a(path), mode );
722 hfile = create_file_OF( path, mode & ~OF_CREATE );
723 return HandleToLong(hfile);
724 }
725
726 /***********************************************************************
727 * _lread (KERNEL32.@)
728 */
729 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
730 {
731 DWORD result;
732 if (!ReadFile( LongToHandle(handle), buffer, count, &result, NULL ))
733 return HFILE_ERROR;
734 return result;
735 }
736
737
738 /***********************************************************************
739 * _llseek (KERNEL32.@)
740 */
741 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
742 {
743 return SetFilePointer( LongToHandle(hFile), lOffset, NULL, nOrigin );
744 }
745
746
747 /***********************************************************************
748 * _lwrite (KERNEL32.@)
749 */
750 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
751 {
752 return (UINT)_hwrite( hFile, buffer, (LONG)count );
753 }
754
755
756 /***********************************************************************
757 * FlushFileBuffers (KERNEL32.@)
758 */
759 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
760 {
761 NTSTATUS nts;
762 IO_STATUS_BLOCK ioblk;
763
764 if (is_console_handle( hFile ))
765 {
766 /* this will fail (as expected) for an output handle */
767 return FlushConsoleInputBuffer( hFile );
768 }
769 nts = NtFlushBuffersFile( hFile, &ioblk );
770 if (nts != STATUS_SUCCESS)
771 {
772 SetLastError( RtlNtStatusToDosError( nts ) );
773 return FALSE;
774 }
775
776 return TRUE;
777 }
778
779
780 /***********************************************************************
781 * GetFileType (KERNEL32.@)
782 */
783 DWORD WINAPI GetFileType( HANDLE hFile )
784 {
785 FILE_FS_DEVICE_INFORMATION info;
786 IO_STATUS_BLOCK io;
787 NTSTATUS status;
788
789 if (is_console_handle( hFile )) return FILE_TYPE_CHAR;
790
791 status = NtQueryVolumeInformationFile( hFile, &io, &info, sizeof(info), FileFsDeviceInformation );
792 if (status != STATUS_SUCCESS)
793 {
794 SetLastError( RtlNtStatusToDosError(status) );
795 return FILE_TYPE_UNKNOWN;
796 }
797
798 switch(info.DeviceType)
799 {
800 case FILE_DEVICE_NULL:
801 case FILE_DEVICE_SERIAL_PORT:
802 case FILE_DEVICE_PARALLEL_PORT:
803 case FILE_DEVICE_TAPE:
804 case FILE_DEVICE_UNKNOWN:
805 return FILE_TYPE_CHAR;
806 case FILE_DEVICE_NAMED_PIPE:
807 return FILE_TYPE_PIPE;
808 default:
809 return FILE_TYPE_DISK;
810 }
811 }
812
813
814 /***********************************************************************
815 * GetFileInformationByHandle (KERNEL32.@)
816 */
817 BOOL WINAPI GetFileInformationByHandle( HANDLE hFile, BY_HANDLE_FILE_INFORMATION *info )
818 {
819 FILE_ALL_INFORMATION all_info;
820 IO_STATUS_BLOCK io;
821 NTSTATUS status;
822
823 status = NtQueryInformationFile( hFile, &io, &all_info, sizeof(all_info), FileAllInformation );
824 if (status == STATUS_SUCCESS)
825 {
826 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
827 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
828 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
829 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
830 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
831 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
832 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
833 info->dwVolumeSerialNumber = 0; /* FIXME */
834 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
835 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
836 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
837 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
838 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
839 return TRUE;
840 }
841 SetLastError( RtlNtStatusToDosError(status) );
842 return FALSE;
843 }
844
845
846 /***********************************************************************
847 * GetFileSize (KERNEL32.@)
848 *
849 * Retrieve the size of a file.
850 *
851 * PARAMS
852 * hFile [I] File to retrieve size of.
853 * filesizehigh [O] On return, the high bits of the file size.
854 *
855 * RETURNS
856 * Success: The low bits of the file size.
857 * Failure: INVALID_FILE_SIZE. As this is could also be a success value,
858 * check GetLastError() for values other than ERROR_SUCCESS.
859 */
860 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
861 {
862 LARGE_INTEGER size;
863 if (!GetFileSizeEx( hFile, &size )) return INVALID_FILE_SIZE;
864 if (filesizehigh) *filesizehigh = size.u.HighPart;
865 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError(0);
866 return size.u.LowPart;
867 }
868
869
870 /***********************************************************************
871 * GetFileSizeEx (KERNEL32.@)
872 *
873 * Retrieve the size of a file.
874 *
875 * PARAMS
876 * hFile [I] File to retrieve size of.
877 * lpFileSIze [O] On return, the size of the file.
878 *
879 * RETURNS
880 * Success: TRUE.
881 * Failure: FALSE, check GetLastError().
882 */
883 BOOL WINAPI GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize )
884 {
885 FILE_STANDARD_INFORMATION info;
886 IO_STATUS_BLOCK io;
887 NTSTATUS status;
888
889 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileStandardInformation );
890 if (status == STATUS_SUCCESS)
891 {
892 *lpFileSize = info.EndOfFile;
893 return TRUE;
894 }
895 SetLastError( RtlNtStatusToDosError(status) );
896 return FALSE;
897 }
898
899
900 /**************************************************************************
901 * SetEndOfFile (KERNEL32.@)
902 *
903 * Sets the current position as the end of the file.
904 *
905 * PARAMS
906 * hFile [I] File handle.
907 *
908 * RETURNS
909 * Success: TRUE.
910 * Failure: FALSE, check GetLastError().
911 */
912 BOOL WINAPI SetEndOfFile( HANDLE hFile )
913 {
914 FILE_POSITION_INFORMATION pos;
915 FILE_END_OF_FILE_INFORMATION eof;
916 IO_STATUS_BLOCK io;
917 NTSTATUS status;
918
919 status = NtQueryInformationFile( hFile, &io, &pos, sizeof(pos), FilePositionInformation );
920 if (status == STATUS_SUCCESS)
921 {
922 eof.EndOfFile = pos.CurrentByteOffset;
923 status = NtSetInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation );
924 }
925 if (status == STATUS_SUCCESS) return TRUE;
926 SetLastError( RtlNtStatusToDosError(status) );
927 return FALSE;
928 }
929
930
931 /***********************************************************************
932 * SetFilePointer (KERNEL32.@)
933 */
934 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword, DWORD method )
935 {
936 LARGE_INTEGER dist, newpos;
937
938 if (highword)
939 {
940 dist.u.LowPart = distance;
941 dist.u.HighPart = *highword;
942 }
943 else dist.QuadPart = distance;
944
945 if (!SetFilePointerEx( hFile, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
946
947 if (highword) *highword = newpos.u.HighPart;
948 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
949 return newpos.u.LowPart;
950 }
951
952
953 /***********************************************************************
954 * SetFilePointerEx (KERNEL32.@)
955 */
956 BOOL WINAPI SetFilePointerEx( HANDLE hFile, LARGE_INTEGER distance,
957 LARGE_INTEGER *newpos, DWORD method )
958 {
959 LONGLONG pos;
960 IO_STATUS_BLOCK io;
961 FILE_POSITION_INFORMATION info;
962
963 switch(method)
964 {
965 case FILE_BEGIN:
966 pos = distance.QuadPart;
967 break;
968 case FILE_CURRENT:
969 if (NtQueryInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
970 goto error;
971 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
972 break;
973 case FILE_END:
974 {
975 FILE_END_OF_FILE_INFORMATION eof;
976 if (NtQueryInformationFile( hFile, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
977 goto error;
978 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
979 }
980 break;
981 default:
982 SetLastError( ERROR_INVALID_PARAMETER );
983 return FALSE;
984 }
985
986 if (pos < 0)
987 {
988 SetLastError( ERROR_NEGATIVE_SEEK );
989 return FALSE;
990 }
991
992 info.CurrentByteOffset.QuadPart = pos;
993 if (NtSetInformationFile( hFile, &io, &info, sizeof(info), FilePositionInformation ))
994 goto error;
995 if (newpos) newpos->QuadPart = pos;
996 return TRUE;
997
998 error:
999 SetLastError( RtlNtStatusToDosError(io.u.Status) );
1000 return FALSE;
1001 }
1002
1003 /***********************************************************************
1004 * GetFileTime (KERNEL32.@)
1005 */
1006 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
1007 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1008 {
1009 FILE_BASIC_INFORMATION info;
1010 IO_STATUS_BLOCK io;
1011 NTSTATUS status;
1012
1013 status = NtQueryInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1014 if (status == STATUS_SUCCESS)
1015 {
1016 if (lpCreationTime)
1017 {
1018 lpCreationTime->dwHighDateTime = info.CreationTime.u.HighPart;
1019 lpCreationTime->dwLowDateTime = info.CreationTime.u.LowPart;
1020 }
1021 if (lpLastAccessTime)
1022 {
1023 lpLastAccessTime->dwHighDateTime = info.LastAccessTime.u.HighPart;
1024 lpLastAccessTime->dwLowDateTime = info.LastAccessTime.u.LowPart;
1025 }
1026 if (lpLastWriteTime)
1027 {
1028 lpLastWriteTime->dwHighDateTime = info.LastWriteTime.u.HighPart;
1029 lpLastWriteTime->dwLowDateTime = info.LastWriteTime.u.LowPart;
1030 }
1031 return TRUE;
1032 }
1033 SetLastError( RtlNtStatusToDosError(status) );
1034 return FALSE;
1035 }
1036
1037
1038 /***********************************************************************
1039 * SetFileTime (KERNEL32.@)
1040 */
1041 BOOL WINAPI SetFileTime( HANDLE hFile, const FILETIME *ctime,
1042 const FILETIME *atime, const FILETIME *mtime )
1043 {
1044 FILE_BASIC_INFORMATION info;
1045 IO_STATUS_BLOCK io;
1046 NTSTATUS status;
1047
1048 memset( &info, 0, sizeof(info) );
1049 if (ctime)
1050 {
1051 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
1052 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
1053 }
1054 if (atime)
1055 {
1056 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
1057 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
1058 }
1059 if (mtime)
1060 {
1061 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
1062 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
1063 }
1064
1065 status = NtSetInformationFile( hFile, &io, &info, sizeof(info), FileBasicInformation );
1066 if (status == STATUS_SUCCESS) return TRUE;
1067 SetLastError( RtlNtStatusToDosError(status) );
1068 return FALSE;
1069 }
1070
1071
1072 /**************************************************************************
1073 * LockFile (KERNEL32.@)
1074 */
1075 BOOL WINAPI LockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1076 DWORD count_low, DWORD count_high )
1077 {
1078 NTSTATUS status;
1079 LARGE_INTEGER count, offset;
1080
1081 TRACE( "%p %x%08x %x%08x\n",
1082 hFile, offset_high, offset_low, count_high, count_low );
1083
1084 count.u.LowPart = count_low;
1085 count.u.HighPart = count_high;
1086 offset.u.LowPart = offset_low;
1087 offset.u.HighPart = offset_high;
1088
1089 status = NtLockFile( hFile, 0, NULL, NULL,
1090 NULL, &offset, &count, NULL, TRUE, TRUE );
1091
1092 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1093 return !status;
1094 }
1095
1096
1097 /**************************************************************************
1098 * LockFileEx [KERNEL32.@]
1099 *
1100 * Locks a byte range within an open file for shared or exclusive access.
1101 *
1102 * RETURNS
1103 * success: TRUE
1104 * failure: FALSE
1105 *
1106 * NOTES
1107 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1108 */
1109 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1110 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
1111 {
1112 NTSTATUS status;
1113 LARGE_INTEGER count, offset;
1114 LPVOID cvalue = NULL;
1115
1116 if (reserved)
1117 {
1118 SetLastError( ERROR_INVALID_PARAMETER );
1119 return FALSE;
1120 }
1121
1122 TRACE( "%p %x%08x %x%08x flags %x\n",
1123 hFile, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset,
1124 count_high, count_low, flags );
1125
1126 count.u.LowPart = count_low;
1127 count.u.HighPart = count_high;
1128 offset.u.LowPart = overlapped->u.s.Offset;
1129 offset.u.HighPart = overlapped->u.s.OffsetHigh;
1130
1131 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1132
1133 status = NtLockFile( hFile, overlapped->hEvent, NULL, cvalue,
1134 NULL, &offset, &count, NULL,
1135 flags & LOCKFILE_FAIL_IMMEDIATELY,
1136 flags & LOCKFILE_EXCLUSIVE_LOCK );
1137
1138 if (status) SetLastError( RtlNtStatusToDosError(status) );
1139 return !status;
1140 }
1141
1142
1143 /**************************************************************************
1144 * UnlockFile (KERNEL32.@)
1145 */
1146 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD offset_low, DWORD offset_high,
1147 DWORD count_low, DWORD count_high )
1148 {
1149 NTSTATUS status;
1150 LARGE_INTEGER count, offset;
1151
1152 count.u.LowPart = count_low;
1153 count.u.HighPart = count_high;
1154 offset.u.LowPart = offset_low;
1155 offset.u.HighPart = offset_high;
1156
1157 status = NtUnlockFile( hFile, NULL, &offset, &count, NULL);
1158 if (status) SetLastError( RtlNtStatusToDosError(status) );
1159 return !status;
1160 }
1161
1162
1163 /**************************************************************************
1164 * UnlockFileEx (KERNEL32.@)
1165 */
1166 BOOL WINAPI UnlockFileEx( HANDLE hFile, DWORD reserved, DWORD count_low, DWORD count_high,
1167 LPOVERLAPPED overlapped )
1168 {
1169 if (reserved)
1170 {
1171 SetLastError( ERROR_INVALID_PARAMETER );
1172 return FALSE;
1173 }
1174 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
1175
1176 return UnlockFile( hFile, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
1177 }
1178
1179
1180 /*************************************************************************
1181 * SetHandleCount (KERNEL32.@)
1182 */
1183 UINT WINAPI SetHandleCount( UINT count )
1184 {
1185 return min( 256, count );
1186 }
1187
1188
1189 /**************************************************************************
1190 * Operations on file names *
1191 **************************************************************************/
1192
1193
1194 /*************************************************************************
1195 * CreateFileW [KERNEL32.@] Creates or opens a file or other object
1196 *
1197 * Creates or opens an object, and returns a handle that can be used to
1198 * access that object.
1199 *
1200 * PARAMS
1201 *
1202 * filename [in] pointer to filename to be accessed
1203 * access [in] access mode requested
1204 * sharing [in] share mode
1205 * sa [in] pointer to security attributes
1206 * creation [in] how to create the file
1207 * attributes [in] attributes for newly created file
1208 * template [in] handle to file with extended attributes to copy
1209 *
1210 * RETURNS
1211 * Success: Open handle to specified file
1212 * Failure: INVALID_HANDLE_VALUE
1213 */
1214 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
1215 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1216 DWORD attributes, HANDLE template )
1217 {
1218 NTSTATUS status;
1219 UINT options;
1220 OBJECT_ATTRIBUTES attr;
1221 UNICODE_STRING nameW;
1222 IO_STATUS_BLOCK io;
1223 HANDLE ret;
1224 DWORD dosdev;
1225 const WCHAR *vxd_name = NULL;
1226 static const WCHAR bkslashes_with_dotW[] = {'\\','\\','.','\\',0};
1227 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
1228 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
1229 SECURITY_QUALITY_OF_SERVICE qos;
1230
1231 static const UINT nt_disposition[5] =
1232 {
1233 FILE_CREATE, /* CREATE_NEW */
1234 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
1235 FILE_OPEN, /* OPEN_EXISTING */
1236 FILE_OPEN_IF, /* OPEN_ALWAYS */
1237 FILE_OVERWRITE /* TRUNCATE_EXISTING */
1238 };
1239
1240
1241 /* sanity checks */
1242
1243 if (!filename || !filename[0])
1244 {
1245 SetLastError( ERROR_PATH_NOT_FOUND );
1246 return INVALID_HANDLE_VALUE;
1247 }
1248
1249 TRACE("%s %s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
1250 (access & GENERIC_READ)?"GENERIC_READ ":"",
1251 (access & GENERIC_WRITE)?"GENERIC_WRITE ":"",
1252 (!access)?"QUERY_ACCESS ":"",
1253 (sharing & FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
1254 (sharing & FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
1255 (sharing & FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
1256 creation, attributes);
1257
1258 /* Open a console for CONIN$ or CONOUT$ */
1259
1260 if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
1261 {
1262 ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle), creation);
1263 goto done;
1264 }
1265
1266 if (!strncmpW(filename, bkslashes_with_dotW, 4))
1267 {
1268 static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
1269 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
1270
1271 if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
1272 !strncmpiW( filename + 4, pipeW, 5 ) ||
1273 !strncmpiW( filename + 4, mailslotW, 9 ))
1274 {
1275 dosdev = 0;
1276 }
1277 else if ((dosdev = RtlIsDosDeviceName_U( filename + 4 )))
1278 {
1279 dosdev += MAKELONG( 0, 4*sizeof(WCHAR) ); /* adjust position to start of filename */
1280 }
1281 else if (GetVersion() & 0x80000000)
1282 {
1283 vxd_name = filename + 4;
1284 }
1285 }
1286 else dosdev = RtlIsDosDeviceName_U( filename );
1287
1288 if (dosdev)
1289 {
1290 static const WCHAR conW[] = {'C','O','N'};
1291
1292 if (LOWORD(dosdev) == sizeof(conW) &&
1293 !memicmpW( filename + HIWORD(dosdev)/sizeof(WCHAR), conW, sizeof(conW)/sizeof(WCHAR)))
1294 {
1295 switch (access & (GENERIC_READ|GENERIC_WRITE))
1296 {
1297 case GENERIC_READ:
1298 ret = OpenConsoleW(coninW, access, (sa && sa->bInheritHandle), creation);
1299 goto done;
1300 case GENERIC_WRITE:
1301 ret = OpenConsoleW(conoutW, access, (sa && sa->bInheritHandle), creation);
1302 goto done;
1303 default:
1304 SetLastError( ERROR_FILE_NOT_FOUND );
1305 return INVALID_HANDLE_VALUE;
1306 }
1307 }
1308 }
1309
1310 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
1311 {
1312 SetLastError( ERROR_INVALID_PARAMETER );
1313 return INVALID_HANDLE_VALUE;
1314 }
1315
1316 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
1317 {
1318 SetLastError( ERROR_PATH_NOT_FOUND );
1319 return INVALID_HANDLE_VALUE;
1320 }
1321
1322 /* now call NtCreateFile */
1323
1324 options = 0;
1325 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
1326 options |= FILE_OPEN_FOR_BACKUP_INTENT;
1327 else
1328 options |= FILE_NON_DIRECTORY_FILE;
1329 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
1330 {
1331 options |= FILE_DELETE_ON_CLOSE;
1332 access |= DELETE;
1333 }
1334 if (attributes & FILE_FLAG_NO_BUFFERING)
1335 options |= FILE_NO_INTERMEDIATE_BUFFERING;
1336 if (!(attributes & FILE_FLAG_OVERLAPPED))
1337 options |= FILE_SYNCHRONOUS_IO_ALERT;
1338 if (attributes & FILE_FLAG_RANDOM_ACCESS)
1339 options |= FILE_RANDOM_ACCESS;
1340 attributes &= FILE_ATTRIBUTE_VALID_FLAGS;
1341
1342 attr.Length = sizeof(attr);
1343 attr.RootDirectory = 0;
1344 attr.Attributes = OBJ_CASE_INSENSITIVE;
1345 attr.ObjectName = &nameW;
1346 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1347 if (attributes & SECURITY_SQOS_PRESENT)
1348 {
1349 qos.Length = sizeof(qos);
1350 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
1351 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
1352 qos.EffectiveOnly = attributes & SECURITY_EFFECTIVE_ONLY ? TRUE : FALSE;
1353 attr.SecurityQualityOfService = &qos;
1354 }
1355 else
1356 attr.SecurityQualityOfService = NULL;
1357
1358 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
1359
1360 status = NtCreateFile( &ret, access, &attr, &io, NULL, attributes,
1361 sharing, nt_disposition[creation - CREATE_NEW],
1362 options, NULL, 0 );
1363 if (status)
1364 {
1365 if (vxd_name && vxd_name[0] && (ret = VXD_Open( vxd_name, access, sa ))) goto done;
1366
1367 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
1368 ret = INVALID_HANDLE_VALUE;
1369
1370 /* In the case file creation was rejected due to CREATE_NEW flag
1371 * was specified and file with that name already exists, correct
1372 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
1373 * Note: RtlNtStatusToDosError is not the subject to blame here.
1374 */
1375 if (status == STATUS_OBJECT_NAME_COLLISION)
1376 SetLastError( ERROR_FILE_EXISTS );
1377 else
1378 SetLastError( RtlNtStatusToDosError(status) );
1379 }
1380 else
1381 {
1382 if ((creation == CREATE_ALWAYS && io.Information == FILE_OVERWRITTEN) ||
1383 (creation == OPEN_ALWAYS && io.Information == FILE_OPENED))
1384 SetLastError( ERROR_ALREADY_EXISTS );
1385 else
1386 SetLastError( 0 );
1387 }
1388 RtlFreeUnicodeString( &nameW );
1389
1390 done:
1391 if (!ret) ret = INVALID_HANDLE_VALUE;
1392 TRACE("returning %p\n", ret);
1393 return ret;
1394 }
1395
1396
1397
1398 /*************************************************************************
1399 * CreateFileA (KERNEL32.@)
1400 *
1401 * See CreateFileW.
1402 */
1403 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
1404 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1405 DWORD attributes, HANDLE template)
1406 {
1407 WCHAR *nameW;
1408
1409 if (!(nameW = FILE_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1410 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
1411 }
1412
1413
1414 /***********************************************************************
1415 * DeleteFileW (KERNEL32.@)
1416 *
1417 * Delete a file.
1418 *
1419 * PARAMS
1420 * path [I] Path to the file to delete.
1421 *
1422 * RETURNS
1423 * Success: TRUE.
1424 * Failure: FALSE, check GetLastError().
1425 */
1426 BOOL WINAPI DeleteFileW( LPCWSTR path )
1427 {
1428 UNICODE_STRING nameW;
1429 OBJECT_ATTRIBUTES attr;
1430 NTSTATUS status;
1431 HANDLE hFile;
1432 IO_STATUS_BLOCK io;
1433
1434 TRACE("%s\n", debugstr_w(path) );
1435
1436 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
1437 {
1438 SetLastError( ERROR_PATH_NOT_FOUND );
1439 return FALSE;
1440 }
1441
1442 attr.Length = sizeof(attr);
1443 attr.RootDirectory = 0;
1444 attr.Attributes = OBJ_CASE_INSENSITIVE;
1445 attr.ObjectName = &nameW;
1446 attr.SecurityDescriptor = NULL;
1447 attr.SecurityQualityOfService = NULL;
1448
1449 status = NtCreateFile(&hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
1450 &attr, &io, NULL, 0,
1451 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1452 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0);
1453 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1454
1455 RtlFreeUnicodeString( &nameW );
1456 if (status)
1457 {
1458 SetLastError( RtlNtStatusToDosError(status) );
1459 return FALSE;
1460 }
1461 return TRUE;
1462 }
1463
1464
1465 /***********************************************************************
1466 * DeleteFileA (KERNEL32.@)
1467 *
1468 * See DeleteFileW.
1469 */
1470 BOOL WINAPI DeleteFileA( LPCSTR path )
1471 {
1472 WCHAR *pathW;
1473
1474 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1475 return DeleteFileW( pathW );
1476 }
1477
1478
1479 /**************************************************************************
1480 * ReplaceFileW (KERNEL32.@)
1481 * ReplaceFile (KERNEL32.@)
1482 */
1483 BOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName, LPCWSTR lpReplacementFileName,
1484 LPCWSTR lpBackupFileName, DWORD dwReplaceFlags,
1485 LPVOID lpExclude, LPVOID lpReserved)
1486 {
1487 UNICODE_STRING nt_replaced_name, nt_replacement_name;
1488 ANSI_STRING unix_replaced_name, unix_replacement_name, unix_backup_name;
1489 HANDLE hReplaced = NULL, hReplacement = NULL, hBackup = NULL;
1490 DWORD error = ERROR_SUCCESS;
1491 UINT replaced_flags;
1492 BOOL ret = FALSE;
1493 NTSTATUS status;
1494 IO_STATUS_BLOCK io;
1495 OBJECT_ATTRIBUTES attr;
1496
1497 if (dwReplaceFlags)
1498 FIXME("Ignoring flags %x\n", dwReplaceFlags);
1499
1500 /* First two arguments are mandatory */
1501 if (!lpReplacedFileName || !lpReplacementFileName)
1502 {
1503 SetLastError(ERROR_INVALID_PARAMETER);
1504 return FALSE;
1505 }
1506
1507 unix_replaced_name.Buffer = NULL;
1508 unix_replacement_name.Buffer = NULL;
1509 unix_backup_name.Buffer = NULL;
1510
1511 attr.Length = sizeof(attr);
1512 attr.RootDirectory = 0;
1513 attr.Attributes = OBJ_CASE_INSENSITIVE;
1514 attr.ObjectName = NULL;
1515 attr.SecurityDescriptor = NULL;
1516 attr.SecurityQualityOfService = NULL;
1517
1518 /* Open the "replaced" file for reading and writing */
1519 if (!(RtlDosPathNameToNtPathName_U(lpReplacedFileName, &nt_replaced_name, NULL, NULL)))
1520 {
1521 error = ERROR_PATH_NOT_FOUND;
1522 goto fail;
1523 }
1524 replaced_flags = lpBackupFileName ? FILE_OPEN : FILE_OPEN_IF;
1525 attr.ObjectName = &nt_replaced_name;
1526 status = NtOpenFile(&hReplaced, GENERIC_READ|GENERIC_WRITE|DELETE|SYNCHRONIZE,
1527 &attr, &io,
1528 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1529 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1530 if (status == STATUS_SUCCESS)
1531 status = wine_nt_to_unix_file_name(&nt_replaced_name, &unix_replaced_name, replaced_flags, FALSE);
1532 RtlFreeUnicodeString(&nt_replaced_name);
1533 if (status != STATUS_SUCCESS)
1534 {
1535 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1536 error = ERROR_FILE_NOT_FOUND;
1537 else
1538 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1539 goto fail;
1540 }
1541
1542 /*
1543 * Open the replacement file for reading, writing, and deleting
1544 * (writing and deleting are needed when finished)
1545 */
1546 if (!(RtlDosPathNameToNtPathName_U(lpReplacementFileName, &nt_replacement_name, NULL, NULL)))
1547 {
1548 error = ERROR_PATH_NOT_FOUND;
1549 goto fail;
1550 }
1551 attr.ObjectName = &nt_replacement_name;
1552 status = NtOpenFile(&hReplacement,
1553 GENERIC_READ|GENERIC_WRITE|DELETE|WRITE_DAC|SYNCHRONIZE,
1554 &attr, &io, 0,
1555 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE);
1556 if (status == STATUS_SUCCESS)
1557 status = wine_nt_to_unix_file_name(&nt_replacement_name, &unix_replacement_name, FILE_OPEN, FALSE);
1558 RtlFreeUnicodeString(&nt_replacement_name);
1559 if (status != STATUS_SUCCESS)
1560 {
1561 error = RtlNtStatusToDosError(status);
1562 goto fail;
1563 }
1564
1565 /* If the user wants a backup then that needs to be performed first */
1566 if (lpBackupFileName)
1567 {
1568 UNICODE_STRING nt_backup_name;
1569 FILE_BASIC_INFORMATION replaced_info;
1570
1571 /* Obtain the file attributes from the "replaced" file */
1572 status = NtQueryInformationFile(hReplaced, &io, &replaced_info,
1573 sizeof(replaced_info),
1574 FileBasicInformation);
1575 if (status != STATUS_SUCCESS)
1576 {
1577 error = RtlNtStatusToDosError(status);
1578 goto fail;
1579 }
1580
1581 if (!(RtlDosPathNameToNtPathName_U(lpBackupFileName, &nt_backup_name, NULL, NULL)))
1582 {
1583 error = ERROR_PATH_NOT_FOUND;
1584 goto fail;
1585 }
1586 attr.ObjectName = &nt_backup_name;
1587 /* Open the backup with permissions to write over it */
1588 status = NtCreateFile(&hBackup, GENERIC_WRITE,
1589 &attr, &io, NULL, replaced_info.FileAttributes,
1590 FILE_SHARE_WRITE, FILE_OPEN_IF,
1591 FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE,
1592 NULL, 0);
1593 if (status == STATUS_SUCCESS)
1594 status = wine_nt_to_unix_file_name(&nt_backup_name, &unix_backup_name, FILE_OPEN_IF, FALSE);
1595 if (status != STATUS_SUCCESS)
1596 {
1597 error = RtlNtStatusToDosError(status);
1598 goto fail;
1599 }
1600
1601 /* If an existing backup exists then copy over it */
1602 if (rename(unix_replaced_name.Buffer, unix_backup_name.Buffer) == -1)
1603 {
1604 error = ERROR_UNABLE_TO_REMOVE_REPLACED; /* is this correct? */
1605 goto fail;
1606 }
1607 }
1608
1609 /*
1610 * Now that the backup has been performed (if requested), copy the replacement
1611 * into place
1612 */
1613 if (rename(unix_replacement_name.Buffer, unix_replaced_name.Buffer) == -1)
1614 {
1615 if (errno == EACCES)
1616 {
1617 /* Inappropriate permissions on "replaced", rename will fail */
1618 error = ERROR_UNABLE_TO_REMOVE_REPLACED;
1619 goto fail;
1620 }
1621 /* on failure we need to indicate whether a backup was made */
1622 if (!lpBackupFileName)
1623 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT;
1624 else
1625 error = ERROR_UNABLE_TO_MOVE_REPLACEMENT_2;
1626 goto fail;
1627 }
1628 /* Success! */
1629 ret = TRUE;
1630
1631 /* Perform resource cleanup */
1632 fail:
1633 if (hBackup) CloseHandle(hBackup);
1634 if (hReplaced) CloseHandle(hReplaced);
1635 if (hReplacement) CloseHandle(hReplacement);
1636 RtlFreeAnsiString(&unix_backup_name);
1637 RtlFreeAnsiString(&unix_replacement_name);
1638 RtlFreeAnsiString(&unix_replaced_name);
1639
1640 /* If there was an error, set the error code */
1641 if(!ret)
1642 SetLastError(error);
1643 return ret;
1644 }
1645
1646
1647 /**************************************************************************
1648 * ReplaceFileA (KERNEL32.@)
1649 */
1650 BOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,
1651 LPCSTR lpBackupFileName, DWORD dwReplaceFlags,
1652 LPVOID lpExclude, LPVOID lpReserved)
1653 {
1654 WCHAR *replacedW, *replacementW, *backupW = NULL;
1655 BOOL ret;
1656
1657 /* This function only makes sense when the first two parameters are defined */
1658 if (!lpReplacedFileName || !(replacedW = FILE_name_AtoW( lpReplacedFileName, TRUE )))
1659 {
1660 SetLastError(ERROR_INVALID_PARAMETER);
1661 return FALSE;
1662 }
1663 if (!lpReplacementFileName || !(replacementW = FILE_name_AtoW( lpReplacementFileName, TRUE )))
1664 {
1665 HeapFree( GetProcessHeap(), 0, replacedW );
1666 SetLastError(ERROR_INVALID_PARAMETER);
1667 return FALSE;
1668 }
1669 /* The backup parameter, however, is optional */
1670 if (lpBackupFileName)
1671 {
1672 if (!(backupW = FILE_name_AtoW( lpBackupFileName, TRUE )))
1673 {
1674 HeapFree( GetProcessHeap(), 0, replacedW );
1675 HeapFree( GetProcessHeap(), 0, replacementW );
1676 SetLastError(ERROR_INVALID_PARAMETER);
1677 return FALSE;
1678 }
1679 }
1680 ret = ReplaceFileW( replacedW, replacementW, backupW, dwReplaceFlags, lpExclude, lpReserved );
1681 HeapFree( GetProcessHeap(), 0, replacedW );
1682 HeapFree( GetProcessHeap(), 0, replacementW );
1683 HeapFree( GetProcessHeap(), 0, backupW );
1684 return ret;
1685 }
1686
1687
1688 /*************************************************************************
1689 * FindFirstFileExW (KERNEL32.@)
1690 *
1691 * NOTE: The FindExSearchLimitToDirectories is ignored - it gives the same
1692 * results as FindExSearchNameMatch
1693 */
1694 HANDLE WINAPI FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1695 LPVOID data, FINDEX_SEARCH_OPS search_op,
1696 LPVOID filter, DWORD flags)
1697 {
1698 WCHAR *mask, *p;
1699 FIND_FIRST_INFO *info = NULL;
1700 UNICODE_STRING nt_name;
1701 OBJECT_ATTRIBUTES attr;
1702 IO_STATUS_BLOCK io;
1703 NTSTATUS status;
1704 DWORD device = 0;
1705
1706 TRACE("%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags);
1707
1708 if ((search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1709 || flags != 0)
1710 {
1711 FIXME("options not implemented 0x%08x 0x%08x\n", search_op, flags );
1712 return INVALID_HANDLE_VALUE;
1713 }
1714 if (level != FindExInfoStandard)
1715 {
1716 FIXME("info level %d not implemented\n", level );
1717 return INVALID_HANDLE_VALUE;
1718 }
1719
1720 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1721 {
1722 SetLastError( ERROR_PATH_NOT_FOUND );
1723 return INVALID_HANDLE_VALUE;
1724 }
1725
1726 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info))))
1727 {
1728 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1729 goto error;
1730 }
1731
1732 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1733 {
1734 static const WCHAR dotW[] = {'.',0};
1735 WCHAR *dir = NULL;
1736
1737 /* we still need to check that the directory can be opened */
1738
1739 if (HIWORD(device))
1740 {
1741 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1742 {
1743 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1744 goto error;
1745 }
1746 memcpy( dir, filename, HIWORD(device) );
1747 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1748 }
1749 RtlFreeUnicodeString( &nt_name );
1750 if (!RtlDosPathNameToNtPathName_U( dir ? dir : dotW, &nt_name, &mask, NULL ))
1751 {
1752 HeapFree( GetProcessHeap(), 0, dir );
1753 SetLastError( ERROR_PATH_NOT_FOUND );
1754 goto error;
1755 }
1756 HeapFree( GetProcessHeap(), 0, dir );
1757 RtlInitUnicodeString( &info->mask, NULL );
1758 }
1759 else if (!mask || !*mask)
1760 {
1761 SetLastError( ERROR_FILE_NOT_FOUND );
1762 goto error;
1763 }
1764 else
1765 {
1766 if (!RtlCreateUnicodeString( &info->mask, mask ))
1767 {
1768 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1769 goto error;
1770 }
1771
1772 /* truncate dir name before mask */
1773 *mask = 0;
1774 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1775 }
1776
1777 /* check if path is the root of the drive */
1778 info->is_root = FALSE;
1779 p = nt_name.Buffer + 4; /* skip \??\ prefix */
1780 if (p[0] && p[1] == ':')
1781 {
1782 p += 2;
1783 while (*p == '\\') p++;
1784 info->is_root = (*p == 0);
1785 }
1786
1787 attr.Length = sizeof(attr);
1788 attr.RootDirectory = 0;
1789 attr.Attributes = OBJ_CASE_INSENSITIVE;
1790 attr.ObjectName = &nt_name;
1791 attr.SecurityDescriptor = NULL;
1792 attr.SecurityQualityOfService = NULL;
1793
1794 status = NtOpenFile( &info->handle, GENERIC_READ, &attr, &io,
1795 FILE_SHARE_READ | FILE_SHARE_WRITE,
1796 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1797
1798 if (status != STATUS_SUCCESS)
1799 {
1800 RtlFreeUnicodeString( &info->mask );
1801 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1802 SetLastError( ERROR_PATH_NOT_FOUND );
1803 else
1804 SetLastError( RtlNtStatusToDosError(status) );
1805 goto error;
1806 }
1807
1808 RtlInitializeCriticalSection( &info->cs );
1809 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
1810 info->path = nt_name;
1811 info->magic = FIND_FIRST_MAGIC;
1812 info->data_pos = 0;
1813 info->data_len = 0;
1814 info->search_op = search_op;
1815
1816 if (device)
1817 {
1818 WIN32_FIND_DATAW *wfd = data;
1819
1820 memset( wfd, 0, sizeof(*wfd) );
1821 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1822 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1823 CloseHandle( info->handle );
1824 info->handle = 0;
1825 }
1826 else
1827 {
1828 IO_STATUS_BLOCK io;
1829
1830 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1831 FileBothDirectoryInformation, FALSE, &info->mask, TRUE );
1832 if (io.u.Status)
1833 {
1834 FindClose( info );
1835 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1836 return INVALID_HANDLE_VALUE;
1837 }
1838 info->data_len = io.Information;
1839 if (!FindNextFileW( info, data ))
1840 {
1841 TRACE( "%s not found\n", debugstr_w(filename) );
1842 FindClose( info );
1843 SetLastError( ERROR_FILE_NOT_FOUND );
1844 return INVALID_HANDLE_VALUE;
1845 }
1846 if (!strpbrkW( info->mask.Buffer, wildcardsW ))
1847 {
1848 /* we can't find two files with the same name */
1849 CloseHandle( info->handle );
1850 info->handle = 0;
1851 }
1852 }
1853 return info;
1854
1855 error:
1856 HeapFree( GetProcessHeap(), 0, info );
1857 RtlFreeUnicodeString( &nt_name );
1858 return INVALID_HANDLE_VALUE;
1859 }
1860
1861
1862 /*************************************************************************
1863 * FindNextFileW (KERNEL32.@)
1864 */
1865 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1866 {
1867 FIND_FIRST_INFO *info;
1868 FILE_BOTH_DIR_INFORMATION *dir_info;
1869 BOOL ret = FALSE;
1870
1871 TRACE("%p %p\n", handle, data);
1872
1873 if (!handle || handle == INVALID_HANDLE_VALUE)
1874 {
1875 SetLastError( ERROR_INVALID_HANDLE );
1876 return ret;
1877 }
1878 info = handle;
1879 if (info->magic != FIND_FIRST_MAGIC)
1880 {
1881 SetLastError( ERROR_INVALID_HANDLE );
1882 return ret;
1883 }
1884
1885 RtlEnterCriticalSection( &info->cs );
1886
1887 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
1888 else for (;;)
1889 {
1890 if (info->data_pos >= info->data_len) /* need to read some more data */
1891 {
1892 IO_STATUS_BLOCK io;
1893
1894 NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, sizeof(info->data),
1895 FileBothDirectoryInformation, FALSE, &info->mask, FALSE );
1896 if (io.u.Status)
1897 {
1898 SetLastError( RtlNtStatusToDosError( io.u.Status ) );
1899 if (io.u.Status == STATUS_NO_MORE_FILES)
1900 {
1901 CloseHandle( info->handle );
1902 info->handle = 0;
1903 }
1904 break;
1905 }
1906 info->data_len = io.Information;
1907 info->data_pos = 0;
1908 }
1909
1910 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
1911
1912 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
1913 else info->data_pos = info->data_len;
1914
1915 /* don't return '.' and '..' in the root of the drive */
1916 if (info->is_root)
1917 {
1918 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
1919 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
1920 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
1921 }
1922
1923 /* check for dir symlink */
1924 if ((dir_info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
1925 (dir_info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
1926 strpbrkW( info->mask.Buffer, wildcardsW ))
1927 {
1928 if (!check_dir_symlink( info, dir_info )) continue;
1929 }
1930
1931 data->dwFileAttributes = dir_info->FileAttributes;
1932 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
1933 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
1934 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
1935 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
1936 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
1937 data->dwReserved0 = 0;
1938 data->dwReserved1 = 0;
1939
1940 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
1941 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1942 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
1943 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
1944
1945 TRACE("returning %s (%s)\n",
1946 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
1947
1948 ret = TRUE;
1949 break;
1950 }
1951
1952 RtlLeaveCriticalSection( &info->cs );
1953 return ret;
1954 }
1955
1956
1957 /*************************************************************************
1958 * FindClose (KERNEL32.@)
1959 */
1960 BOOL WINAPI FindClose( HANDLE handle )
1961 {
1962 FIND_FIRST_INFO *info = handle;
1963
1964 if (!handle || handle == INVALID_HANDLE_VALUE)
1965 {
1966 SetLastError( ERROR_INVALID_HANDLE );
1967 return FALSE;
1968 }
1969
1970 __TRY
1971 {
1972 if (info->magic == FIND_FIRST_MAGIC)
1973 {
1974 RtlEnterCriticalSection( &info->cs );
1975 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
1976 {
1977 info->magic = 0;
1978 if (info->handle) CloseHandle( info->handle );
1979 info->handle = 0;
1980 RtlFreeUnicodeString( &info->mask );
1981 info->mask.Buffer = NULL;
1982 RtlFreeUnicodeString( &info->path );
1983 info->data_pos = 0;
1984 info->data_len = 0;
1985 RtlLeaveCriticalSection( &info->cs );
1986 info->cs.DebugInfo->Spare[0] = 0;
1987 RtlDeleteCriticalSection( &info->cs );
1988 HeapFree( GetProcessHeap(), 0, info );
1989 }
1990 }
1991 }
1992 __EXCEPT_PAGE_FAULT
1993 {
1994 WARN("Illegal handle %p\n", handle);
1995 SetLastError( ERROR_INVALID_HANDLE );
1996 return FALSE;
1997 }
1998 __ENDTRY
1999
2000 return TRUE;
2001 }
2002
2003
2004 /*************************************************************************
2005 * FindFirstFileA (KERNEL32.@)
2006 */
2007 HANDLE WINAPI FindFirstFileA( LPCSTR lpFileName, WIN32_FIND_DATAA *lpFindData )
2008 {
2009 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
2010 FindExSearchNameMatch, NULL, 0);
2011 }
2012
2013 /*************************************************************************
2014 * FindFirstFileExA (KERNEL32.@)
2015 */
2016 HANDLE WINAPI FindFirstFileExA( LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
2017 LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp,
2018 LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
2019 {
2020 HANDLE handle;
2021 WIN32_FIND_DATAA *dataA;
2022 WIN32_FIND_DATAW dataW;
2023 WCHAR *nameW;
2024
2025 if (!(nameW = FILE_name_AtoW( lpFileName, FALSE ))) return INVALID_HANDLE_VALUE;
2026
2027 handle = FindFirstFileExW(nameW, fInfoLevelId, &dataW, fSearchOp, lpSearchFilter, dwAdditionalFlags);
2028 if (handle == INVALID_HANDLE_VALUE) return handle;
2029
2030 dataA = lpFindFileData;
2031 dataA->dwFileAttributes = dataW.dwFileAttributes;
2032 dataA->ftCreationTime = dataW.ftCreationTime;
2033 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
2034 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
2035 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
2036 dataA->nFileSizeLow = dataW.nFileSizeLow;
2037 FILE_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
2038 FILE_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
2039 sizeof(dataA->cAlternateFileName) );
2040 return handle;
2041 }
2042
2043
2044 /*************************************************************************
2045 * FindFirstFileW (KERNEL32.@)
2046 */
2047 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
2048 {
2049 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
2050 FindExSearchNameMatch, NULL, 0);
2051 }
2052
2053
2054 /*************************************************************************
2055 * FindNextFileA (KERNEL32.@)
2056 */
2057 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
2058 {
2059 WIN32_FIND_DATAW dataW;
2060
2061 if (!FindNextFileW( handle, &dataW )) return FALSE;
2062 data->dwFileAttributes = dataW.dwFileAttributes;
2063 data->ftCreationTime = dataW.ftCreationTime;
2064 data->ftLastAccessTime = dataW.ftLastAccessTime;
2065 data->ftLastWriteTime = dataW.ftLastWriteTime;
2066 data->nFileSizeHigh = dataW.nFileSizeHigh;
2067 data->nFileSizeLow = dataW.nFileSizeLow;
2068 FILE_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
2069 FILE_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
2070 sizeof(data->cAlternateFileName) );
2071 return TRUE;
2072 }
2073
2074
2075 /**************************************************************************
2076 * GetFileAttributesW (KERNEL32.@)
2077 */
2078 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
2079 {
2080 FILE_BASIC_INFORMATION info;
2081 UNICODE_STRING nt_name;
2082 OBJECT_ATTRIBUTES attr;
2083 NTSTATUS status;
2084
2085 TRACE("%s\n", debugstr_w(name));
2086
2087 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2088 {
2089 SetLastError( ERROR_PATH_NOT_FOUND );
2090 return INVALID_FILE_ATTRIBUTES;
2091 }
2092
2093 attr.Length = sizeof(attr);
2094 attr.RootDirectory = 0;
2095 attr.Attributes = OBJ_CASE_INSENSITIVE;
2096 attr.ObjectName = &nt_name;
2097 attr.SecurityDescriptor = NULL;
2098 attr.SecurityQualityOfService = NULL;
2099
2100 status = NtQueryAttributesFile( &attr, &info );
2101 RtlFreeUnicodeString( &nt_name );
2102
2103 if (status == STATUS_SUCCESS) return info.FileAttributes;
2104
2105 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
2106 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
2107
2108 SetLastError( RtlNtStatusToDosError(status) );
2109 return INVALID_FILE_ATTRIBUTES;
2110 }
2111
2112
2113 /**************************************************************************
2114 * GetFileAttributesA (KERNEL32.@)
2115 */
2116 DWORD WINAPI GetFileAttributesA( LPCSTR name )
2117 {
2118 WCHAR *nameW;
2119
2120 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
2121 return GetFileAttributesW( nameW );
2122 }
2123
2124
2125 /**************************************************************************
2126 * SetFileAttributesW (KERNEL32.@)
2127 */
2128 BOOL WINAPI SetFileAttributesW( LPCWSTR name, DWORD attributes )
2129 {
2130 UNICODE_STRING nt_name;
2131 OBJECT_ATTRIBUTES attr;
2132 IO_STATUS_BLOCK io;
2133 NTSTATUS status;
2134 HANDLE handle;
2135
2136 TRACE("%s %x\n", debugstr_w(name), attributes);
2137
2138 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2139 {
2140 SetLastError( ERROR_PATH_NOT_FOUND );
2141 return FALSE;
2142 }
2143
2144 attr.Length = sizeof(attr);
2145 attr.RootDirectory = 0;
2146 attr.Attributes = OBJ_CASE_INSENSITIVE;
2147 attr.ObjectName = &nt_name;
2148 attr.SecurityDescriptor = NULL;
2149 attr.SecurityQualityOfService = NULL;
2150
2151 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2152 RtlFreeUnicodeString( &nt_name );
2153
2154 if (status == STATUS_SUCCESS)
2155 {
2156 FILE_BASIC_INFORMATION info;
2157
2158 memset( &info, 0, sizeof(info) );
2159 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2160 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2161 NtClose( handle );
2162 }
2163
2164 if (status == STATUS_SUCCESS) return TRUE;
2165 SetLastError( RtlNtStatusToDosError(status) );
2166 return FALSE;
2167 }
2168
2169
2170 /**************************************************************************
2171 * SetFileAttributesA (KERNEL32.@)
2172 */
2173 BOOL WINAPI SetFileAttributesA( LPCSTR name, DWORD attributes )
2174 {
2175 WCHAR *nameW;
2176
2177 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2178 return SetFileAttributesW( nameW, attributes );
2179 }
2180
2181
2182 /**************************************************************************
2183 * GetFileAttributesExW (KERNEL32.@)
2184 */
2185 BOOL WINAPI GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2186 {
2187 FILE_NETWORK_OPEN_INFORMATION info;
2188 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
2189 UNICODE_STRING nt_name;
2190 OBJECT_ATTRIBUTES attr;
2191 NTSTATUS status;
2192
2193 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
2194
2195 if (level != GetFileExInfoStandard)
2196 {
2197 SetLastError( ERROR_INVALID_PARAMETER );
2198 return FALSE;
2199 }
2200
2201 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2202 {
2203 SetLastError( ERROR_PATH_NOT_FOUND );
2204 return FALSE;
2205 }
2206
2207 attr.Length = sizeof(attr);
2208 attr.RootDirectory = 0;
2209 attr.Attributes = OBJ_CASE_INSENSITIVE;
2210 attr.ObjectName = &nt_name;
2211 attr.SecurityDescriptor = NULL;
2212 attr.SecurityQualityOfService = NULL;
2213
2214 status = NtQueryFullAttributesFile( &attr, &info );
2215 RtlFreeUnicodeString( &nt_name );
2216
2217 if (status != STATUS_SUCCESS)
2218 {
2219 SetLastError( RtlNtStatusToDosError(status) );
2220 return FALSE;
2221 }
2222
2223 data->dwFileAttributes = info.FileAttributes;
2224 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
2225 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
2226 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
2227 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
2228 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
2229 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
2230 data->nFileSizeLow = info.EndOfFile.u.LowPart;
2231 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
2232 return TRUE;
2233 }
2234
2235
2236 /**************************************************************************
2237 * GetFileAttributesExA (KERNEL32.@)
2238 */
2239 BOOL WINAPI GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID ptr )
2240 {
2241 WCHAR *nameW;
2242
2243 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2244 return GetFileAttributesExW( nameW, level, ptr );
2245 }
2246
2247
2248 /******************************************************************************
2249 * GetCompressedFileSizeW (KERNEL32.@)
2250 *
2251 * Get the actual number of bytes used on disk.
2252 *
2253 * RETURNS
2254 * Success: Low-order doubleword of number of bytes
2255 * Failure: INVALID_FILE_SIZE
2256 */
2257 DWORD WINAPI GetCompressedFileSizeW(
2258 LPCWSTR name, /* [in] Pointer to name of file */
2259 LPDWORD size_high ) /* [out] Receives high-order doubleword of size */
2260 {
2261 UNICODE_STRING nt_name;
2262 OBJECT_ATTRIBUTES attr;
2263 IO_STATUS_BLOCK io;
2264 NTSTATUS status;
2265 HANDLE handle;
2266 DWORD ret = INVALID_FILE_SIZE;
2267
2268 TRACE("%s %p\n", debugstr_w(name), size_high);
2269
2270 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2271 {
2272 SetLastError( ERROR_PATH_NOT_FOUND );
2273 return INVALID_FILE_SIZE;
2274 }
2275
2276 attr.Length = sizeof(attr);
2277 attr.RootDirectory = 0;
2278 attr.Attributes = OBJ_CASE_INSENSITIVE;
2279 attr.ObjectName = &nt_name;
2280 attr.SecurityDescriptor = NULL;
2281 attr.SecurityQualityOfService = NULL;
2282
2283 status = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2284 RtlFreeUnicodeString( &nt_name );
2285
2286 if (status == STATUS_SUCCESS)
2287 {
2288 /* we don't support compressed files, simply return the file size */
2289 ret = GetFileSize( handle, size_high );
2290 NtClose( handle );
2291 }
2292 else SetLastError( RtlNtStatusToDosError(status) );
2293
2294 return ret;
2295 }
2296
2297
2298 /******************************************************************************
2299 * GetCompressedFileSizeA (KERNEL32.@)
2300 *
2301 * See GetCompressedFileSizeW.
2302 */
2303 DWORD WINAPI GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
2304 {
2305 WCHAR *nameW;
2306
2307 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
2308 return GetCompressedFileSizeW( nameW, size_high );
2309 }
2310
2311
2312 /***********************************************************************
2313 * OpenVxDHandle (KERNEL32.@)
2314 *
2315 * This function is supposed to return the corresponding Ring 0
2316 * ("kernel") handle for a Ring 3 handle in Win9x.
2317 * Evidently, Wine will have problems with this. But we try anyway,
2318 * maybe it helps...
2319 */
2320 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
2321 {
2322 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
2323 return hHandleRing3;
2324 }
2325
2326
2327 /****************************************************************************
2328 * DeviceIoControl (KERNEL32.@)
2329 */
2330 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
2331 LPVOID lpvInBuffer, DWORD cbInBuffer,
2332 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
2333 LPDWORD lpcbBytesReturned,
2334 LPOVERLAPPED lpOverlapped)
2335 {
2336 NTSTATUS status;
2337
2338 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
2339 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
2340 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
2341
2342 /* Check if this is a user defined control code for a VxD */
2343
2344 if (HIWORD( dwIoControlCode ) == 0 && (GetVersion() & 0x80000000))
2345 {
2346 DeviceIoProc proc = VXD_get_proc( hDevice );
2347 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
2348 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
2349 }
2350
2351 /* Not a VxD, let ntdll handle it */
2352
2353 if (lpOverlapped)
2354 {
2355 LPVOID cvalue = ((ULONG_PTR)lpOverlapped->hEvent & 1) ? NULL : lpOverlapped;
2356 lpOverlapped->Internal = STATUS_PENDING;
2357 lpOverlapped->InternalHigh = 0;
2358 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2359 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
2360 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2361 dwIoControlCode, lpvInBuffer, cbInBuffer,
2362 lpvOutBuffer, cbOutBuffer);
2363 else
2364 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
2365 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
2366 dwIoControlCode, lpvInBuffer, cbInBuffer,
2367 lpvOutBuffer, cbOutBuffer);
2368 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
2369 }
2370 else
2371 {
2372 IO_STATUS_BLOCK iosb;
2373
2374 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
2375 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
2376 dwIoControlCode, lpvInBuffer, cbInBuffer,
2377 lpvOutBuffer, cbOutBuffer);
2378 else
2379 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
2380 dwIoControlCode, lpvInBuffer, cbInBuffer,
2381 lpvOutBuffer, cbOutBuffer);
2382 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
2383 }
2384 if (status) SetLastError( RtlNtStatusToDosError(status) );
2385 return !status;
2386 }
2387
2388
2389 /***********************************************************************
2390 * OpenFile (KERNEL32.@)
2391 */
2392 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
2393 {
2394 HANDLE handle;
2395 FILETIME filetime;
2396 WORD filedatetime[2];
2397
2398 if (!ofs) return HFILE_ERROR;
2399
2400 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
2401 ((mode & 0x3 )==OF_READ)?"OF_READ":
2402 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
2403 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
2404 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
2405 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
2406 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
2407 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
2408 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
2409 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
2410 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
2411 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
2412 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
2413 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
2414 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
2415 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
2416 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
2417 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
2418 );
2419
2420
2421 ofs->cBytes = sizeof(OFSTRUCT);
2422 ofs->nErrCode = 0;
2423 if (mode & OF_REOPEN) name = ofs->szPathName;
2424
2425 if (!name) return HFILE_ERROR;
2426
2427 TRACE("%s %04x\n", name, mode );
2428
2429 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
2430 Are there any cases where getting the path here is wrong?
2431 Uwe Bonnes 1997 Apr 2 */
2432 if (!GetFullPathNameA( name, sizeof(ofs->szPathName), ofs->szPathName, NULL )) goto error;
2433
2434 /* OF_PARSE simply fills the structure */
2435
2436 if (mode & OF_PARSE)
2437 {
2438 ofs->fFixedDisk = (GetDriveTypeA( ofs->szPathName ) != DRIVE_REMOVABLE);
2439 TRACE("(%s): OF_PARSE, res = '%s'\n", name, ofs->szPathName );
2440 return 0;
2441 }
2442
2443 /* OF_CREATE is completely different from all other options, so
2444 handle it first */
2445
2446 if (mode & OF_CREATE)
2447 {
2448 if ((handle = create_file_OF( name, mode )) == INVALID_HANDLE_VALUE)
2449 goto error;
2450 }
2451 else
2452 {
2453 /* Now look for the file */
2454
2455 if (!SearchPathA( NULL, name, NULL, sizeof(ofs->szPathName), ofs->szPathName, NULL ))
2456 goto error;
2457
2458 TRACE("found %s\n", debugstr_a(ofs->szPathName) );
2459
2460 if (mode & OF_DELETE)
2461 {
2462 if (!DeleteFileA( ofs->szPathName )) goto error;
2463 TRACE("(%s): OF_DELETE return = OK\n", name);
2464 return TRUE;
2465 }
2466
2467 handle = LongToHandle(_lopen( ofs->szPathName, mode ));
2468 if (handle == INVALID_HANDLE_VALUE) goto error;
2469
2470 GetFileTime( handle, NULL, NULL, &filetime );
2471 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
2472 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
2473 {
2474 if (ofs->Reserved1 != filedatetime[0] || ofs->Reserved2 != filedatetime[1] )
2475 {
2476 CloseHandle( handle );
2477 WARN("(%s): OF_VERIFY failed\n", name );
2478 /* FIXME: what error here? */
2479 SetLastError( ERROR_FILE_NOT_FOUND );
2480 goto error;
2481 }
2482 }
2483 ofs->Reserved1 = filedatetime[0];
2484 ofs->Reserved2 = filedatetime[1];
2485 }
2486 TRACE("(%s): OK, return = %p\n", name, handle );
2487 if (mode & OF_EXIST) /* Return TRUE instead of a handle */
2488 {
2489 CloseHandle( handle );
2490 return TRUE;
2491 }
2492 return HandleToLong(handle);
2493
2494 error: /* We get here if there was an error opening the file */
2495 ofs->nErrCode = GetLastError();
2496 WARN("(%s): return = HFILE_ERROR error= %d\n", name,ofs->nErrCode );
2497 return HFILE_ERROR;
2498 }
2499
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.