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