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