Fixed a simple bug in the implementation of the ShellView objects.
[wine/multimedia.git] / files / file.c
blob8936215c967cabd1db9e27fa920b139d3570bd60
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
7 * TODO:
8 * Fix the CopyFileEx methods to implement the "extented" functionality.
9 * Right now, they simply call the CopyFile method.
12 #include <assert.h>
13 #include <ctype.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/errno.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/time.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <utime.h>
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/winbase16.h"
31 #include "wine/winestring.h"
32 #include "drive.h"
33 #include "device.h"
34 #include "file.h"
35 #include "global.h"
36 #include "heap.h"
37 #include "msdos.h"
38 #include "options.h"
39 #include "ldt.h"
40 #include "process.h"
41 #include "task.h"
42 #include "async.h"
43 #include "wincon.h"
44 #include "debug.h"
46 #include "server/request.h"
47 #include "server.h"
49 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
50 #define MAP_ANON MAP_ANONYMOUS
51 #endif
53 /* Size of per-process table of DOS handles */
54 #define DOS_TABLE_SIZE 256
57 /***********************************************************************
58 * FILE_ConvertOFMode
60 * Convert OF_* mode into flags for CreateFile.
62 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
64 switch(mode & 0x03)
66 case OF_READ: *access = GENERIC_READ; break;
67 case OF_WRITE: *access = GENERIC_WRITE; break;
68 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
69 default: *access = 0; break;
71 switch(mode & 0x70)
73 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
74 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
75 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
76 case OF_SHARE_DENY_NONE:
77 case OF_SHARE_COMPAT:
78 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
83 #if 0
84 /***********************************************************************
85 * FILE_ShareDeny
87 * PARAMS
88 * oldmode[I] mode how file was first opened
89 * mode[I] mode how the file should get opened
90 * RETURNS
91 * TRUE: deny open
92 * FALSE: allow open
94 * Look what we have to do with the given SHARE modes
96 * Ralph Brown's interrupt list gives following explication, I guess
97 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
99 * FIXME: Validate this function
100 ========from Ralph Brown's list =========
101 (Table 0750)
102 Values of DOS file sharing behavior:
103 | Second and subsequent Opens
104 First |Compat Deny Deny Deny Deny
105 Open | All Write Read None
106 |R W RW R W RW R W RW R W RW R W RW
107 - - - - -| - - - - - - - - - - - - - - - - -
108 Compat R |Y Y Y N N N 1 N N N N N 1 N N
109 W |Y Y Y N N N N N N N N N N N N
110 RW|Y Y Y N N N N N N N N N N N N
111 - - - - -|
112 Deny R |C C C N N N N N N N N N N N N
113 All W |C C C N N N N N N N N N N N N
114 RW|C C C N N N N N N N N N N N N
115 - - - - -|
116 Deny R |2 C C N N N Y N N N N N Y N N
117 Write W |C C C N N N N N N Y N N Y N N
118 RW|C C C N N N N N N N N N Y N N
119 - - - - -|
120 Deny R |C C C N N N N Y N N N N N Y N
121 Read W |C C C N N N N N N N Y N N Y N
122 RW|C C C N N N N N N N N N N Y N
123 - - - - -|
124 Deny R |2 C C N N N Y Y Y N N N Y Y Y
125 None W |C C C N N N N N N Y Y Y Y Y Y
126 RW|C C C N N N N N N N N N Y Y Y
127 Legend: Y = open succeeds, N = open fails with error code 05h
128 C = open fails, INT 24 generated
129 1 = open succeeds if file read-only, else fails with error code
130 2 = open succeeds if file read-only, else fails with INT 24
131 ========end of description from Ralph Brown's List =====
132 For every "Y" in the table we return FALSE
133 For every "N" we set the DOS_ERROR and return TRUE
134 For all other cases we barf,set the DOS_ERROR and return TRUE
137 static BOOL FILE_ShareDeny( int mode, int oldmode)
139 int oldsharemode = oldmode & 0x70;
140 int sharemode = mode & 0x70;
141 int oldopenmode = oldmode & 3;
142 int openmode = mode & 3;
144 switch (oldsharemode)
146 case OF_SHARE_COMPAT:
147 if (sharemode == OF_SHARE_COMPAT) return FALSE;
148 if (openmode == OF_READ) goto test_ro_err05 ;
149 goto fail_error05;
150 case OF_SHARE_EXCLUSIVE:
151 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
152 goto fail_error05;
153 case OF_SHARE_DENY_WRITE:
154 if (openmode != OF_READ)
156 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
157 goto fail_error05;
159 switch (sharemode)
161 case OF_SHARE_COMPAT:
162 if (oldopenmode == OF_READ) goto test_ro_int24 ;
163 goto fail_int24;
164 case OF_SHARE_DENY_NONE :
165 return FALSE;
166 case OF_SHARE_DENY_WRITE :
167 if (oldopenmode == OF_READ) return FALSE;
168 case OF_SHARE_DENY_READ :
169 if (oldopenmode == OF_WRITE) return FALSE;
170 case OF_SHARE_EXCLUSIVE:
171 default:
172 goto fail_error05;
174 break;
175 case OF_SHARE_DENY_READ:
176 if (openmode != OF_WRITE)
178 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
179 goto fail_error05;
181 switch (sharemode)
183 case OF_SHARE_COMPAT:
184 goto fail_int24;
185 case OF_SHARE_DENY_NONE :
186 return FALSE;
187 case OF_SHARE_DENY_WRITE :
188 if (oldopenmode == OF_READ) return FALSE;
189 case OF_SHARE_DENY_READ :
190 if (oldopenmode == OF_WRITE) return FALSE;
191 case OF_SHARE_EXCLUSIVE:
192 default:
193 goto fail_error05;
195 break;
196 case OF_SHARE_DENY_NONE:
197 switch (sharemode)
199 case OF_SHARE_COMPAT:
200 goto fail_int24;
201 case OF_SHARE_DENY_NONE :
202 return FALSE;
203 case OF_SHARE_DENY_WRITE :
204 if (oldopenmode == OF_READ) return FALSE;
205 case OF_SHARE_DENY_READ :
206 if (oldopenmode == OF_WRITE) return FALSE;
207 case OF_SHARE_EXCLUSIVE:
208 default:
209 goto fail_error05;
211 default:
212 ERR(file,"unknown mode\n");
214 ERR(file,"shouldn't happen\n");
215 ERR(file,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
216 return TRUE;
218 test_ro_int24:
219 if (oldmode == OF_READ)
220 return FALSE;
221 /* Fall through */
222 fail_int24:
223 FIXME(file,"generate INT24 missing\n");
224 /* Is this the right error? */
225 SetLastError( ERROR_ACCESS_DENIED );
226 return TRUE;
228 test_ro_err05:
229 if (oldmode == OF_READ)
230 return FALSE;
231 /* fall through */
232 fail_error05:
233 TRACE(file,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
234 SetLastError( ERROR_ACCESS_DENIED );
235 return TRUE;
237 #endif
240 /***********************************************************************
241 * FILE_SetDosError
243 * Set the DOS error code from errno.
245 void FILE_SetDosError(void)
247 int save_errno = errno; /* errno gets overwritten by printf */
249 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
250 switch (save_errno)
252 case EAGAIN:
253 SetLastError( ERROR_SHARING_VIOLATION );
254 break;
255 case EBADF:
256 SetLastError( ERROR_INVALID_HANDLE );
257 break;
258 case ENOSPC:
259 SetLastError( ERROR_HANDLE_DISK_FULL );
260 break;
261 case EACCES:
262 case EPERM:
263 case EROFS:
264 SetLastError( ERROR_ACCESS_DENIED );
265 break;
266 case EBUSY:
267 SetLastError( ERROR_LOCK_VIOLATION );
268 break;
269 case ENOENT:
270 SetLastError( ERROR_FILE_NOT_FOUND );
271 break;
272 case EISDIR:
273 SetLastError( ERROR_CANNOT_MAKE );
274 break;
275 case ENFILE:
276 case EMFILE:
277 SetLastError( ERROR_NO_MORE_FILES );
278 break;
279 case EEXIST:
280 SetLastError( ERROR_FILE_EXISTS );
281 break;
282 case EINVAL:
283 case ESPIPE:
284 SetLastError( ERROR_SEEK );
285 break;
286 case ENOTEMPTY:
287 SetLastError( ERROR_DIR_NOT_EMPTY );
288 break;
289 default:
290 perror( "int21: unknown errno" );
291 SetLastError( ERROR_GEN_FAILURE );
292 break;
294 errno = save_errno;
298 /***********************************************************************
299 * FILE_DupUnixHandle
301 * Duplicate a Unix handle into a task handle.
303 HFILE FILE_DupUnixHandle( int fd, DWORD access )
305 int unix_handle;
306 struct create_file_request req;
307 struct create_file_reply reply;
309 if ((unix_handle = dup(fd)) == -1)
311 FILE_SetDosError();
312 return INVALID_HANDLE_VALUE;
314 req.access = access;
315 req.inherit = 1;
316 req.sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
317 req.create = 0;
318 req.attrs = 0;
320 CLIENT_SendRequest( REQ_CREATE_FILE, unix_handle, 1,
321 &req, sizeof(req) );
322 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
323 return reply.handle;
327 /***********************************************************************
328 * FILE_CreateFile
330 * Implementation of CreateFile. Takes a Unix path name.
332 HFILE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
333 LPSECURITY_ATTRIBUTES sa, DWORD creation,
334 DWORD attributes, HANDLE template )
336 struct create_file_request req;
337 struct create_file_reply reply;
339 req.access = access;
340 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
341 req.sharing = sharing;
342 req.create = creation;
343 req.attrs = attributes;
344 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
345 &req, sizeof(req),
346 filename, strlen(filename) + 1 );
347 SetLastError(0);
348 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
350 /* If write access failed, retry without GENERIC_WRITE */
352 if ((reply.handle == -1) && !Options.failReadOnly &&
353 (access & GENERIC_WRITE))
355 DWORD lasterror = GetLastError();
356 if ((lasterror == ERROR_ACCESS_DENIED) ||
357 (lasterror == ERROR_WRITE_PROTECT))
359 req.access &= ~GENERIC_WRITE;
360 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
361 &req, sizeof(req),
362 filename, strlen(filename) + 1 );
363 SetLastError(0);
364 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
367 return reply.handle;
371 /***********************************************************************
372 * FILE_CreateDevice
374 * Same as FILE_CreateFile but for a device
376 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
378 struct create_device_request req;
379 struct create_device_reply reply;
381 req.access = access;
382 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
383 req.id = client_id;
384 CLIENT_SendRequest( REQ_CREATE_DEVICE, -1, 1, &req, sizeof(req) );
385 SetLastError(0);
386 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
387 return reply.handle;
391 /*************************************************************************
392 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
394 * Creates or opens an object, and returns a handle that can be used to
395 * access that object.
397 * PARAMS
399 * filename [I] pointer to filename to be accessed
400 * access [I] access mode requested
401 * sharing [I] share mode
402 * sa [I] pointer to security attributes
403 * creation [I] how to create the file
404 * attributes [I] attributes for newly created file
405 * template [I] handle to file with extended attributes to copy
407 * RETURNS
408 * Success: Open handle to specified file
409 * Failure: INVALID_HANDLE_VALUE
411 * NOTES
412 * Should call SetLastError() on failure.
414 * BUGS
416 * Doesn't support character devices, pipes, template files, or a
417 * lot of the 'attributes' flags yet.
419 HFILE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
420 LPSECURITY_ATTRIBUTES sa, DWORD creation,
421 DWORD attributes, HANDLE template )
423 DOS_FULL_NAME full_name;
425 if (!filename)
427 SetLastError( ERROR_INVALID_PARAMETER );
428 return HFILE_ERROR;
431 /* If the name starts with '\\?\', ignore the first 4 chars. */
432 if (!strncmp(filename, "\\\\?\\", 4))
434 filename += 4;
435 if (!strncmp(filename, "UNC\\", 4))
437 FIXME( file, "UNC name (%s) not supported.\n", filename );
438 SetLastError( ERROR_PATH_NOT_FOUND );
439 return HFILE_ERROR;
443 if (!strncmp(filename, "\\\\.\\", 4))
444 return DEVICE_Open( filename+4, access, sa );
446 /* If the name still starts with '\\', it's a UNC name. */
447 if (!strncmp(filename, "\\\\", 2))
449 FIXME( file, "UNC name (%s) not supported.\n", filename );
450 SetLastError( ERROR_PATH_NOT_FOUND );
451 return HFILE_ERROR;
454 /* Open a console for CONIN$ or CONOUT$ */
455 if (!lstrcmpiA(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
456 if (!lstrcmpiA(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
458 if (DOSFS_GetDevice( filename ))
460 HFILE ret;
462 TRACE(file, "opening device '%s'\n", filename );
464 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
465 return ret;
467 /* Do not silence this please. It is a critical error. -MM */
468 ERR(file, "Couldn't open device '%s'!\n",filename);
469 SetLastError( ERROR_FILE_NOT_FOUND );
470 return HFILE_ERROR;
473 /* check for filename, don't check for last entry if creating */
474 if (!DOSFS_GetFullName( filename,
475 (creation == OPEN_EXISTING) || (creation == TRUNCATE_EXISTING), &full_name ))
476 return HFILE_ERROR;
478 return FILE_CreateFile( full_name.long_name, access, sharing,
479 sa, creation, attributes, template );
484 /*************************************************************************
485 * CreateFile32W (KERNEL32.48)
487 HFILE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
488 LPSECURITY_ATTRIBUTES sa, DWORD creation,
489 DWORD attributes, HANDLE template)
491 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
492 HFILE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
493 HeapFree( GetProcessHeap(), 0, afn );
494 return res;
498 /***********************************************************************
499 * FILE_FillInfo
501 * Fill a file information from a struct stat.
503 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
505 if (S_ISDIR(st->st_mode))
506 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
507 else
508 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
509 if (!(st->st_mode & S_IWUSR))
510 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
512 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
513 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
514 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
516 info->dwVolumeSerialNumber = 0; /* FIXME */
517 info->nFileSizeHigh = 0;
518 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
519 info->nNumberOfLinks = st->st_nlink;
520 info->nFileIndexHigh = 0;
521 info->nFileIndexLow = st->st_ino;
525 /***********************************************************************
526 * FILE_Stat
528 * Stat a Unix path name. Return TRUE if OK.
530 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
532 struct stat st;
534 if (!unixName || !info) return FALSE;
536 if (stat( unixName, &st ) == -1)
538 FILE_SetDosError();
539 return FALSE;
541 FILE_FillInfo( &st, info );
542 return TRUE;
546 /***********************************************************************
547 * GetFileInformationByHandle (KERNEL32.219)
549 DWORD WINAPI GetFileInformationByHandle( HFILE hFile,
550 BY_HANDLE_FILE_INFORMATION *info )
552 struct get_file_info_request req;
553 struct get_file_info_reply reply;
555 if (!info) return 0;
556 req.handle = hFile;
557 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
558 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
559 return 0;
560 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftCreationTime, 0 );
561 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftLastWriteTime, 0 );
562 DOSFS_UnixTimeToFileTime( reply.access_time, &info->ftLastAccessTime, 0 );
563 info->dwFileAttributes = reply.attr;
564 info->dwVolumeSerialNumber = reply.serial;
565 info->nFileSizeHigh = reply.size_high;
566 info->nFileSizeLow = reply.size_low;
567 info->nNumberOfLinks = reply.links;
568 info->nFileIndexHigh = reply.index_high;
569 info->nFileIndexLow = reply.index_low;
570 return 1;
574 /**************************************************************************
575 * GetFileAttributes16 (KERNEL.420)
577 DWORD WINAPI GetFileAttributes16( LPCSTR name )
579 return GetFileAttributesA( name );
583 /**************************************************************************
584 * GetFileAttributes32A (KERNEL32.217)
586 DWORD WINAPI GetFileAttributesA( LPCSTR name )
588 DOS_FULL_NAME full_name;
589 BY_HANDLE_FILE_INFORMATION info;
591 if (name == NULL || *name=='\0') return -1;
593 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
594 if (!FILE_Stat( full_name.long_name, &info )) return -1;
595 return info.dwFileAttributes;
599 /**************************************************************************
600 * GetFileAttributes32W (KERNEL32.218)
602 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
604 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
605 DWORD res = GetFileAttributesA( nameA );
606 HeapFree( GetProcessHeap(), 0, nameA );
607 return res;
611 /***********************************************************************
612 * GetFileSize (KERNEL32.220)
614 DWORD WINAPI GetFileSize( HFILE hFile, LPDWORD filesizehigh )
616 BY_HANDLE_FILE_INFORMATION info;
617 if (!GetFileInformationByHandle( hFile, &info )) return 0;
618 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
619 return info.nFileSizeLow;
623 /***********************************************************************
624 * GetFileTime (KERNEL32.221)
626 BOOL WINAPI GetFileTime( HFILE hFile, FILETIME *lpCreationTime,
627 FILETIME *lpLastAccessTime,
628 FILETIME *lpLastWriteTime )
630 BY_HANDLE_FILE_INFORMATION info;
631 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
632 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
633 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
634 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
635 return TRUE;
638 /***********************************************************************
639 * CompareFileTime (KERNEL32.28)
641 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
643 if (!x || !y) return -1;
645 if (x->dwHighDateTime > y->dwHighDateTime)
646 return 1;
647 if (x->dwHighDateTime < y->dwHighDateTime)
648 return -1;
649 if (x->dwLowDateTime > y->dwLowDateTime)
650 return 1;
651 if (x->dwLowDateTime < y->dwLowDateTime)
652 return -1;
653 return 0;
657 /***********************************************************************
658 * GetTempFileName16 (KERNEL.97)
660 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
661 LPSTR buffer )
663 char temppath[144];
665 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
666 drive |= DRIVE_GetCurrentDrive() + 'A';
668 if ((drive & TF_FORCEDRIVE) &&
669 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
671 drive &= ~TF_FORCEDRIVE;
672 WARN(file, "invalid drive %d specified\n", drive );
675 if (drive & TF_FORCEDRIVE)
676 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
677 else
678 GetTempPathA( 132, temppath );
679 return (UINT16)GetTempFileNameA( temppath, prefix, unique, buffer );
683 /***********************************************************************
684 * GetTempFileName32A (KERNEL32.290)
686 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
687 LPSTR buffer)
689 static UINT unique_temp;
690 DOS_FULL_NAME full_name;
691 int i;
692 LPSTR p;
693 UINT num;
695 if ( !path || !prefix || !buffer ) return 0;
697 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
698 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
700 strcpy( buffer, path );
701 p = buffer + strlen(buffer);
703 /* add a \, if there isn't one and path is more than just the drive letter ... */
704 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
705 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
707 *p++ = '~';
708 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
709 sprintf( p, "%04x.tmp", num );
711 /* Now try to create it */
713 if (!unique)
717 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
718 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
719 if (handle != INVALID_HANDLE_VALUE)
720 { /* We created it */
721 TRACE(file, "created %s\n",
722 buffer);
723 CloseHandle( handle );
724 break;
726 if (GetLastError() != ERROR_FILE_EXISTS)
727 break; /* No need to go on */
728 num++;
729 sprintf( p, "%04x.tmp", num );
730 } while (num != (unique & 0xffff));
733 /* Get the full path name */
735 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
737 /* Check if we have write access in the directory */
738 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
739 if (access( full_name.long_name, W_OK ) == -1)
740 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
741 buffer);
743 TRACE(file, "returning %s\n", buffer );
744 return unique ? unique : num;
748 /***********************************************************************
749 * GetTempFileName32W (KERNEL32.291)
751 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
752 LPWSTR buffer )
754 LPSTR patha,prefixa;
755 char buffera[144];
756 UINT ret;
758 if (!path) return 0;
759 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
760 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
761 ret = GetTempFileNameA( patha, prefixa, unique, buffera );
762 lstrcpyAtoW( buffer, buffera );
763 HeapFree( GetProcessHeap(), 0, patha );
764 HeapFree( GetProcessHeap(), 0, prefixa );
765 return ret;
769 /***********************************************************************
770 * FILE_DoOpenFile
772 * Implementation of OpenFile16() and OpenFile32().
774 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
775 BOOL win32 )
777 HFILE hFileRet;
778 FILETIME filetime;
779 WORD filedatetime[2];
780 DOS_FULL_NAME full_name;
781 DWORD access, sharing;
782 char *p;
784 if (!ofs) return HFILE_ERROR;
786 ofs->cBytes = sizeof(OFSTRUCT);
787 ofs->nErrCode = 0;
788 if (mode & OF_REOPEN) name = ofs->szPathName;
790 if (!name) {
791 ERR(file, "called with `name' set to NULL ! Please debug.\n");
792 return HFILE_ERROR;
795 TRACE(file, "%s %04x\n", name, mode );
797 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
798 Are there any cases where getting the path here is wrong?
799 Uwe Bonnes 1997 Apr 2 */
800 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
801 ofs->szPathName, NULL )) goto error;
802 FILE_ConvertOFMode( mode, &access, &sharing );
804 /* OF_PARSE simply fills the structure */
806 if (mode & OF_PARSE)
808 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
809 != DRIVE_REMOVABLE);
810 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
811 name, ofs->szPathName );
812 return 0;
815 /* OF_CREATE is completely different from all other options, so
816 handle it first */
818 if (mode & OF_CREATE)
820 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
821 sharing, NULL, CREATE_ALWAYS,
822 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
823 goto error;
824 goto success;
827 /* If OF_SEARCH is set, ignore the given path */
829 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
831 /* First try the file name as is */
832 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
833 /* Now remove the path */
834 if (name[0] && (name[1] == ':')) name += 2;
835 if ((p = strrchr( name, '\\' ))) name = p + 1;
836 if ((p = strrchr( name, '/' ))) name = p + 1;
837 if (!name[0]) goto not_found;
840 /* Now look for the file */
842 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
844 found:
845 TRACE(file, "found %s = %s\n",
846 full_name.long_name, full_name.short_name );
847 lstrcpynA( ofs->szPathName, full_name.short_name,
848 sizeof(ofs->szPathName) );
850 if (mode & OF_SHARE_EXCLUSIVE)
851 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
852 on the file <tempdir>/_ins0432._mp to determine how
853 far installation has proceeded.
854 _ins0432._mp is an executable and while running the
855 application expects the open with OF_SHARE_ to fail*/
856 /* Probable FIXME:
857 As our loader closes the files after loading the executable,
858 we can't find the running executable with FILE_InUse.
859 Perhaps the loader should keep the file open.
860 Recheck against how Win handles that case */
862 char *last = strrchr(full_name.long_name,'/');
863 if (!last)
864 last = full_name.long_name - 1;
865 if (GetModuleHandle16(last+1))
867 TRACE(file,"Denying shared open for %s\n",full_name.long_name);
868 return HFILE_ERROR;
872 if (mode & OF_DELETE)
874 if (unlink( full_name.long_name ) == -1) goto not_found;
875 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
876 return 1;
879 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
880 NULL, OPEN_EXISTING, 0, -1 );
881 if (hFileRet == HFILE_ERROR) goto not_found;
883 GetFileTime( hFileRet, NULL, NULL, &filetime );
884 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
885 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
887 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
889 CloseHandle( hFileRet );
890 WARN(file, "(%s): OF_VERIFY failed\n", name );
891 /* FIXME: what error here? */
892 SetLastError( ERROR_FILE_NOT_FOUND );
893 goto error;
896 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
898 success: /* We get here if the open was successful */
899 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
900 if (win32)
902 if (mode & OF_EXIST) /* Return the handle, but close it first */
903 CloseHandle( hFileRet );
905 else
907 hFileRet = FILE_AllocDosHandle( hFileRet );
908 if (hFileRet == HFILE_ERROR16) goto error;
909 if (mode & OF_EXIST) /* Return the handle, but close it first */
910 _lclose16( hFileRet );
912 return hFileRet;
914 not_found: /* We get here if the file does not exist */
915 WARN(file, "'%s' not found\n", name );
916 SetLastError( ERROR_FILE_NOT_FOUND );
917 /* fall through */
919 error: /* We get here if there was an error opening the file */
920 ofs->nErrCode = GetLastError();
921 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
922 name,ofs->nErrCode );
923 return HFILE_ERROR;
927 /***********************************************************************
928 * OpenFile16 (KERNEL.74)
930 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
932 return FILE_DoOpenFile( name, ofs, mode, FALSE );
936 /***********************************************************************
937 * OpenFile32 (KERNEL32.396)
939 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
941 return FILE_DoOpenFile( name, ofs, mode, TRUE );
945 /***********************************************************************
946 * FILE_InitProcessDosHandles
948 * Allocates the default DOS handles for a process. Called either by
949 * AllocDosHandle below or by the DOSVM stuff.
951 BOOL FILE_InitProcessDosHandles( void ) {
952 HANDLE *ptr;
954 if (!(ptr = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY,
955 sizeof(*ptr) * DOS_TABLE_SIZE )))
956 return FALSE;
957 PROCESS_Current()->dos_handles = ptr;
958 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
959 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
960 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
961 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
962 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
963 return TRUE;
966 /***********************************************************************
967 * FILE_AllocDosHandle
969 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
970 * longer valid after this function (even on failure).
972 HFILE16 FILE_AllocDosHandle( HANDLE handle )
974 int i;
975 HANDLE *ptr = PROCESS_Current()->dos_handles;
977 if (!handle || (handle == INVALID_HANDLE_VALUE))
978 return INVALID_HANDLE_VALUE16;
980 if (!ptr) {
981 if (!FILE_InitProcessDosHandles())
982 goto error;
983 ptr = PROCESS_Current()->dos_handles;
986 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
987 if (!*ptr)
989 *ptr = handle;
990 TRACE( file, "Got %d for h32 %d\n", i, handle );
991 return i;
993 error:
994 CloseHandle( handle );
995 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
996 return INVALID_HANDLE_VALUE16;
1000 /***********************************************************************
1001 * FILE_GetHandle32
1003 * Return the Win32 handle for a DOS handle.
1005 HANDLE FILE_GetHandle( HFILE16 hfile )
1007 HANDLE *table = PROCESS_Current()->dos_handles;
1008 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1010 SetLastError( ERROR_INVALID_HANDLE );
1011 return INVALID_HANDLE_VALUE;
1013 return table[hfile];
1017 /***********************************************************************
1018 * FILE_Dup2
1020 * dup2() function for DOS handles.
1022 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1024 HANDLE *table = PROCESS_Current()->dos_handles;
1025 HANDLE new_handle;
1027 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1028 !table || !table[hFile1])
1030 SetLastError( ERROR_INVALID_HANDLE );
1031 return HFILE_ERROR16;
1033 if (hFile2 < 5)
1035 FIXME( file, "stdio handle closed, need proper conversion\n" );
1036 SetLastError( ERROR_INVALID_HANDLE );
1037 return HFILE_ERROR16;
1039 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1040 GetCurrentProcess(), &new_handle,
1041 0, FALSE, DUPLICATE_SAME_ACCESS ))
1042 return HFILE_ERROR16;
1043 if (table[hFile2]) CloseHandle( table[hFile2] );
1044 table[hFile2] = new_handle;
1045 return hFile2;
1049 /***********************************************************************
1050 * _lclose16 (KERNEL.81)
1052 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1054 HANDLE *table = PROCESS_Current()->dos_handles;
1056 if (hFile < 5)
1058 FIXME( file, "stdio handle closed, need proper conversion\n" );
1059 SetLastError( ERROR_INVALID_HANDLE );
1060 return HFILE_ERROR16;
1062 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1064 SetLastError( ERROR_INVALID_HANDLE );
1065 return HFILE_ERROR16;
1067 TRACE( file, "%d (handle32=%d)\n", hFile, table[hFile] );
1068 CloseHandle( table[hFile] );
1069 table[hFile] = 0;
1070 return 0;
1074 /***********************************************************************
1075 * _lclose32 (KERNEL32.592)
1077 HFILE WINAPI _lclose( HFILE hFile )
1079 TRACE(file, "handle %d\n", hFile );
1080 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1084 /***********************************************************************
1085 * ReadFile (KERNEL32.428)
1087 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1088 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1090 struct get_read_fd_request req;
1091 int unix_handle, result;
1093 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToRead );
1095 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1096 if (!bytesToRead) return TRUE;
1098 req.handle = hFile;
1099 CLIENT_SendRequest( REQ_GET_READ_FD, -1, 1, &req, sizeof(req) );
1100 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1101 if (unix_handle == -1) return FALSE;
1102 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1104 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1105 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1106 FILE_SetDosError();
1107 break;
1109 close( unix_handle );
1110 if (result == -1) return FALSE;
1111 if (bytesRead) *bytesRead = result;
1112 return TRUE;
1116 /***********************************************************************
1117 * WriteFile (KERNEL32.578)
1119 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1120 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1122 struct get_write_fd_request req;
1123 int unix_handle, result;
1125 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToWrite );
1127 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1128 if (!bytesToWrite) return TRUE;
1130 req.handle = hFile;
1131 CLIENT_SendRequest( REQ_GET_WRITE_FD, -1, 1, &req, sizeof(req) );
1132 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1133 if (unix_handle == -1) return FALSE;
1134 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1136 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1137 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1138 FILE_SetDosError();
1139 break;
1141 close( unix_handle );
1142 if (result == -1) return FALSE;
1143 if (bytesWritten) *bytesWritten = result;
1144 return TRUE;
1148 /***********************************************************************
1149 * WIN16_hread
1151 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1153 LONG maxlen;
1155 TRACE(file, "%d %08lx %ld\n",
1156 hFile, (DWORD)buffer, count );
1158 /* Some programs pass a count larger than the allocated buffer */
1159 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1160 if (count > maxlen) count = maxlen;
1161 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1165 /***********************************************************************
1166 * WIN16_lread
1168 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1170 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1174 /***********************************************************************
1175 * _lread32 (KERNEL32.596)
1177 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1179 DWORD result;
1180 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1181 return result;
1185 /***********************************************************************
1186 * _lread16 (KERNEL.82)
1188 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1190 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1194 /***********************************************************************
1195 * _lcreat16 (KERNEL.83)
1197 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1199 TRACE(file, "%s %02x\n", path, attr );
1200 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1204 /***********************************************************************
1205 * _lcreat32 (KERNEL32.593)
1207 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1209 TRACE(file, "%s %02x\n", path, attr );
1210 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1211 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1212 CREATE_ALWAYS, attr, -1 );
1216 /***********************************************************************
1217 * _lcreat16_uniq (Not a Windows API)
1219 HFILE16 _lcreat16_uniq( LPCSTR path, INT attr )
1221 TRACE(file, "%s %02x\n", path, attr );
1222 return FILE_AllocDosHandle( CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1223 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1224 CREATE_NEW, attr, -1 ));
1228 /***********************************************************************
1229 * SetFilePointer (KERNEL32.492)
1231 DWORD WINAPI SetFilePointer( HFILE hFile, LONG distance, LONG *highword,
1232 DWORD method )
1234 struct set_file_pointer_request req;
1235 struct set_file_pointer_reply reply;
1237 if (highword && *highword)
1239 FIXME(file, "64-bit offsets not supported yet\n");
1240 SetLastError( ERROR_INVALID_PARAMETER );
1241 return 0xffffffff;
1243 TRACE(file, "handle %d offset %ld origin %ld\n",
1244 hFile, distance, method );
1246 req.handle = hFile;
1247 req.low = distance;
1248 req.high = highword ? *highword : 0;
1249 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1250 req.whence = method;
1251 CLIENT_SendRequest( REQ_SET_FILE_POINTER, -1, 1, &req, sizeof(req) );
1252 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
1253 SetLastError( 0 );
1254 if (highword) *highword = reply.high;
1255 return reply.low;
1259 /***********************************************************************
1260 * _llseek16 (KERNEL.84)
1262 * FIXME:
1263 * Seeking before the start of the file should be allowed for _llseek16,
1264 * but cause subsequent I/O operations to fail (cf. interrupt list)
1267 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1269 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1273 /***********************************************************************
1274 * _llseek32 (KERNEL32.594)
1276 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1278 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1282 /***********************************************************************
1283 * _lopen16 (KERNEL.85)
1285 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1287 return FILE_AllocDosHandle( _lopen( path, mode ) );
1291 /***********************************************************************
1292 * _lopen32 (KERNEL32.595)
1294 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1296 DWORD access, sharing;
1298 TRACE(file, "('%s',%04x)\n", path, mode );
1299 FILE_ConvertOFMode( mode, &access, &sharing );
1300 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1304 /***********************************************************************
1305 * _lwrite16 (KERNEL.86)
1307 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1309 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1312 /***********************************************************************
1313 * _lwrite32 (KERNEL32.761)
1315 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1317 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1321 /***********************************************************************
1322 * _hread16 (KERNEL.349)
1324 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1326 return _lread( FILE_GetHandle(hFile), buffer, count );
1330 /***********************************************************************
1331 * _hread32 (KERNEL32.590)
1333 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1335 return _lread( hFile, buffer, count );
1339 /***********************************************************************
1340 * _hwrite16 (KERNEL.350)
1342 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1344 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1348 /***********************************************************************
1349 * _hwrite32 (KERNEL32.591)
1351 * experimentation yields that _lwrite:
1352 * o truncates the file at the current position with
1353 * a 0 len write
1354 * o returns 0 on a 0 length write
1355 * o works with console handles
1358 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1360 DWORD result;
1362 TRACE(file, "%d %p %ld\n", handle, buffer, count );
1364 if (!count)
1366 /* Expand or truncate at current position */
1367 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1368 return 0;
1370 if (!WriteFile( handle, buffer, count, &result, NULL ))
1371 return HFILE_ERROR;
1372 return result;
1376 /***********************************************************************
1377 * SetHandleCount16 (KERNEL.199)
1379 UINT16 WINAPI SetHandleCount16( UINT16 count )
1381 HGLOBAL16 hPDB = GetCurrentPDB16();
1382 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1383 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1385 TRACE(file, "(%d)\n", count );
1387 if (count < 20) count = 20; /* No point in going below 20 */
1388 else if (count > 254) count = 254;
1390 if (count == 20)
1392 if (pdb->nbFiles > 20)
1394 memcpy( pdb->fileHandles, files, 20 );
1395 GlobalFree16( pdb->hFileHandles );
1396 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1397 GlobalHandleToSel16( hPDB ) );
1398 pdb->hFileHandles = 0;
1399 pdb->nbFiles = 20;
1402 else /* More than 20, need a new file handles table */
1404 BYTE *newfiles;
1405 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1406 if (!newhandle)
1408 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1409 return pdb->nbFiles;
1411 newfiles = (BYTE *)GlobalLock16( newhandle );
1413 if (count > pdb->nbFiles)
1415 memcpy( newfiles, files, pdb->nbFiles );
1416 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1418 else memcpy( newfiles, files, count );
1419 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1420 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1421 pdb->hFileHandles = newhandle;
1422 pdb->nbFiles = count;
1424 return pdb->nbFiles;
1428 /*************************************************************************
1429 * SetHandleCount32 (KERNEL32.494)
1431 UINT WINAPI SetHandleCount( UINT count )
1433 return MIN( 256, count );
1437 /***********************************************************************
1438 * FlushFileBuffers (KERNEL32.133)
1440 BOOL WINAPI FlushFileBuffers( HFILE hFile )
1442 struct flush_file_request req;
1444 req.handle = hFile;
1445 CLIENT_SendRequest( REQ_FLUSH_FILE, -1, 1, &req, sizeof(req) );
1446 return !CLIENT_WaitReply( NULL, NULL, 0 );
1450 /**************************************************************************
1451 * SetEndOfFile (KERNEL32.483)
1453 BOOL WINAPI SetEndOfFile( HFILE hFile )
1455 struct truncate_file_request req;
1457 req.handle = hFile;
1458 CLIENT_SendRequest( REQ_TRUNCATE_FILE, -1, 1, &req, sizeof(req) );
1459 return !CLIENT_WaitReply( NULL, NULL, 0 );
1463 /***********************************************************************
1464 * DeleteFile16 (KERNEL.146)
1466 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1468 return DeleteFileA( path );
1472 /***********************************************************************
1473 * DeleteFile32A (KERNEL32.71)
1475 BOOL WINAPI DeleteFileA( LPCSTR path )
1477 DOS_FULL_NAME full_name;
1479 TRACE(file, "'%s'\n", path );
1481 if (!*path)
1483 ERR(file, "Empty path passed\n");
1484 return FALSE;
1486 if (DOSFS_GetDevice( path ))
1488 WARN(file, "cannot remove DOS device '%s'!\n", path);
1489 SetLastError( ERROR_FILE_NOT_FOUND );
1490 return FALSE;
1493 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1494 if (unlink( full_name.long_name ) == -1)
1496 FILE_SetDosError();
1497 return FALSE;
1499 return TRUE;
1503 /***********************************************************************
1504 * DeleteFile32W (KERNEL32.72)
1506 BOOL WINAPI DeleteFileW( LPCWSTR path )
1508 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1509 BOOL ret = DeleteFileA( xpath );
1510 HeapFree( GetProcessHeap(), 0, xpath );
1511 return ret;
1515 /***********************************************************************
1516 * FILE_dommap
1518 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1519 DWORD size_high, DWORD size_low,
1520 DWORD offset_high, DWORD offset_low,
1521 int prot, int flags )
1523 int fd = -1;
1524 int pos;
1525 LPVOID ret;
1527 if (size_high || offset_high)
1528 FIXME(file, "offsets larger than 4Gb not supported\n");
1530 if (unix_handle == -1)
1532 #ifdef MAP_ANON
1533 flags |= MAP_ANON;
1534 #else
1535 static int fdzero = -1;
1537 if (fdzero == -1)
1539 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1541 perror( "/dev/zero: open" );
1542 exit(1);
1545 fd = fdzero;
1546 #endif /* MAP_ANON */
1547 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1548 #ifdef MAP_SHARED
1549 flags &= ~MAP_SHARED;
1550 #endif
1551 #ifdef MAP_PRIVATE
1552 flags |= MAP_PRIVATE;
1553 #endif
1555 else fd = unix_handle;
1557 if ((ret = mmap( start, size_low, prot,
1558 flags, fd, offset_low )) != (LPVOID)-1)
1559 return ret;
1561 /* mmap() failed; if this is because the file offset is not */
1562 /* page-aligned (EINVAL), or because the underlying filesystem */
1563 /* does not support mmap() (ENOEXEC), we do it by hand. */
1565 if (unix_handle == -1) return ret;
1566 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1567 if (prot & PROT_WRITE)
1569 /* We cannot fake shared write mappings */
1570 #ifdef MAP_SHARED
1571 if (flags & MAP_SHARED) return ret;
1572 #endif
1573 #ifdef MAP_PRIVATE
1574 if (!(flags & MAP_PRIVATE)) return ret;
1575 #endif
1577 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1578 /* Reserve the memory with an anonymous mmap */
1579 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1580 PROT_READ | PROT_WRITE, flags );
1581 if (ret == (LPVOID)-1) return ret;
1582 /* Now read in the file */
1583 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1585 FILE_munmap( ret, size_high, size_low );
1586 return (LPVOID)-1;
1588 read( fd, ret, size_low );
1589 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1590 mprotect( ret, size_low, prot ); /* Set the right protection */
1591 return ret;
1595 /***********************************************************************
1596 * FILE_munmap
1598 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1600 if (size_high)
1601 FIXME(file, "offsets larger than 4Gb not supported\n");
1602 return munmap( start, size_low );
1606 /***********************************************************************
1607 * GetFileType (KERNEL32.222)
1609 DWORD WINAPI GetFileType( HFILE hFile )
1611 struct get_file_info_request req;
1612 struct get_file_info_reply reply;
1614 req.handle = hFile;
1615 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
1616 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
1617 return FILE_TYPE_UNKNOWN;
1618 return reply.type;
1622 /**************************************************************************
1623 * MoveFileEx32A (KERNEL32.???)
1625 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1627 DOS_FULL_NAME full_name1, full_name2;
1628 int mode=0; /* mode == 1: use copy */
1630 TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag);
1632 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1633 if (fn2) { /* !fn2 means delete fn1 */
1634 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1635 /* Source name and target path are valid */
1636 if ( full_name1.drive != full_name2.drive)
1638 /* use copy, if allowed */
1639 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1640 /* FIXME: Use right error code */
1641 SetLastError( ERROR_FILE_EXISTS );
1642 return FALSE;
1644 else mode =1;
1646 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1647 /* target exists, check if we may overwrite */
1648 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1649 /* FIXME: Use right error code */
1650 SetLastError( ERROR_ACCESS_DENIED );
1651 return FALSE;
1654 else /* fn2 == NULL means delete source */
1655 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1656 if (flag & MOVEFILE_COPY_ALLOWED) {
1657 WARN(file, "Illegal flag\n");
1658 SetLastError( ERROR_GEN_FAILURE );
1659 return FALSE;
1661 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1662 Perhaps we should queue these command and execute it
1663 when exiting... What about using on_exit(2)
1665 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1666 full_name1.long_name);
1667 return TRUE;
1669 else if (unlink( full_name1.long_name ) == -1)
1671 FILE_SetDosError();
1672 return FALSE;
1674 else return TRUE; /* successfully deleted */
1676 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1677 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1678 Perhaps we should queue these command and execute it
1679 when exiting... What about using on_exit(2)
1681 FIXME(file,"Please move existing file '%s' to file '%s'"
1682 "when Wine has finished\n",
1683 full_name1.long_name, full_name2.long_name);
1684 return TRUE;
1687 if (!mode) /* move the file */
1688 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1690 FILE_SetDosError();
1691 return FALSE;
1693 else return TRUE;
1694 else /* copy File */
1695 return CopyFileA(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1699 /**************************************************************************
1700 * MoveFileEx32W (KERNEL32.???)
1702 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1704 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1705 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1706 BOOL res = MoveFileExA( afn1, afn2, flag );
1707 HeapFree( GetProcessHeap(), 0, afn1 );
1708 HeapFree( GetProcessHeap(), 0, afn2 );
1709 return res;
1713 /**************************************************************************
1714 * MoveFile32A (KERNEL32.387)
1716 * Move file or directory
1718 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1720 DOS_FULL_NAME full_name1, full_name2;
1721 struct stat fstat;
1723 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1725 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1726 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1727 /* The new name must not already exist */
1728 return FALSE;
1729 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1731 if (full_name1.drive == full_name2.drive) /* move */
1732 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1734 FILE_SetDosError();
1735 return FALSE;
1737 else return TRUE;
1738 else /*copy */ {
1739 if (stat( full_name1.long_name, &fstat ))
1741 WARN(file, "Invalid source file %s\n",
1742 full_name1.long_name);
1743 FILE_SetDosError();
1744 return FALSE;
1746 if (S_ISDIR(fstat.st_mode)) {
1747 /* No Move for directories across file systems */
1748 /* FIXME: Use right error code */
1749 SetLastError( ERROR_GEN_FAILURE );
1750 return FALSE;
1752 else
1753 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1758 /**************************************************************************
1759 * MoveFile32W (KERNEL32.390)
1761 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1763 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1764 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1765 BOOL res = MoveFileA( afn1, afn2 );
1766 HeapFree( GetProcessHeap(), 0, afn1 );
1767 HeapFree( GetProcessHeap(), 0, afn2 );
1768 return res;
1772 /**************************************************************************
1773 * CopyFile32A (KERNEL32.36)
1775 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1777 HFILE h1, h2;
1778 BY_HANDLE_FILE_INFORMATION info;
1779 UINT count;
1780 BOOL ret = FALSE;
1781 int mode;
1782 char buffer[2048];
1784 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1785 if (!GetFileInformationByHandle( h1, &info ))
1787 CloseHandle( h1 );
1788 return FALSE;
1790 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1791 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1792 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1793 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1795 CloseHandle( h1 );
1796 return FALSE;
1798 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1800 char *p = buffer;
1801 while (count > 0)
1803 INT res = _lwrite( h2, p, count );
1804 if (res <= 0) goto done;
1805 p += res;
1806 count -= res;
1809 ret = TRUE;
1810 done:
1811 CloseHandle( h1 );
1812 CloseHandle( h2 );
1813 return ret;
1817 /**************************************************************************
1818 * CopyFile32W (KERNEL32.37)
1820 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1822 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1823 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1824 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1825 HeapFree( GetProcessHeap(), 0, sourceA );
1826 HeapFree( GetProcessHeap(), 0, destA );
1827 return ret;
1831 /**************************************************************************
1832 * CopyFileEx32A (KERNEL32.858)
1834 * This implementation ignores most of the extra parameters passed-in into
1835 * the "ex" version of the method and calls the CopyFile method.
1836 * It will have to be fixed eventually.
1838 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1839 LPCSTR destFilename,
1840 LPPROGRESS_ROUTINE progressRoutine,
1841 LPVOID appData,
1842 LPBOOL cancelFlagPointer,
1843 DWORD copyFlags)
1845 BOOL failIfExists = FALSE;
1848 * Interpret the only flag that CopyFile can interpret.
1850 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1852 failIfExists = TRUE;
1855 return CopyFileA(sourceFilename, destFilename, failIfExists);
1858 /**************************************************************************
1859 * CopyFileEx32W (KERNEL32.859)
1861 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1862 LPCWSTR destFilename,
1863 LPPROGRESS_ROUTINE progressRoutine,
1864 LPVOID appData,
1865 LPBOOL cancelFlagPointer,
1866 DWORD copyFlags)
1868 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1869 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1871 BOOL ret = CopyFileExA(sourceA,
1872 destA,
1873 progressRoutine,
1874 appData,
1875 cancelFlagPointer,
1876 copyFlags);
1878 HeapFree( GetProcessHeap(), 0, sourceA );
1879 HeapFree( GetProcessHeap(), 0, destA );
1881 return ret;
1885 /***********************************************************************
1886 * SetFileTime (KERNEL32.650)
1888 BOOL WINAPI SetFileTime( HFILE hFile,
1889 const FILETIME *lpCreationTime,
1890 const FILETIME *lpLastAccessTime,
1891 const FILETIME *lpLastWriteTime )
1893 struct set_file_time_request req;
1895 req.handle = hFile;
1896 if (lpLastAccessTime)
1897 req.access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1898 else
1899 req.access_time = 0; /* FIXME */
1900 if (lpLastWriteTime)
1901 req.write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1902 else
1903 req.write_time = 0; /* FIXME */
1905 CLIENT_SendRequest( REQ_SET_FILE_TIME, -1, 1, &req, sizeof(req) );
1906 return !CLIENT_WaitReply( NULL, NULL, 0 );
1910 /**************************************************************************
1911 * LockFile (KERNEL32.511)
1913 BOOL WINAPI LockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1914 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1916 struct lock_file_request req;
1918 req.handle = hFile;
1919 req.offset_low = dwFileOffsetLow;
1920 req.offset_high = dwFileOffsetHigh;
1921 req.count_low = nNumberOfBytesToLockLow;
1922 req.count_high = nNumberOfBytesToLockHigh;
1923 CLIENT_SendRequest( REQ_LOCK_FILE, -1, 1, &req, sizeof(req) );
1924 return !CLIENT_WaitReply( NULL, NULL, 0 );
1928 /**************************************************************************
1929 * UnlockFile (KERNEL32.703)
1931 BOOL WINAPI UnlockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1932 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1934 struct unlock_file_request req;
1936 req.handle = hFile;
1937 req.offset_low = dwFileOffsetLow;
1938 req.offset_high = dwFileOffsetHigh;
1939 req.count_low = nNumberOfBytesToUnlockLow;
1940 req.count_high = nNumberOfBytesToUnlockHigh;
1941 CLIENT_SendRequest( REQ_UNLOCK_FILE, -1, 1, &req, sizeof(req) );
1942 return !CLIENT_WaitReply( NULL, NULL, 0 );
1946 #if 0
1948 struct DOS_FILE_LOCK {
1949 struct DOS_FILE_LOCK * next;
1950 DWORD base;
1951 DWORD len;
1952 DWORD processId;
1953 FILE_OBJECT * dos_file;
1954 /* char * unix_name;*/
1957 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
1959 static DOS_FILE_LOCK *locks = NULL;
1960 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
1963 /* Locks need to be mirrored because unix file locking is based
1964 * on the pid. Inside of wine there can be multiple WINE processes
1965 * that share the same unix pid.
1966 * Read's and writes should check these locks also - not sure
1967 * how critical that is at this point (FIXME).
1970 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
1972 DOS_FILE_LOCK *curr;
1973 DWORD processId;
1975 processId = GetCurrentProcessId();
1977 /* check if lock overlaps a current lock for the same file */
1978 #if 0
1979 for (curr = locks; curr; curr = curr->next) {
1980 if (strcmp(curr->unix_name, file->unix_name) == 0) {
1981 if ((f->l_start == curr->base) && (f->l_len == curr->len))
1982 return TRUE;/* region is identic */
1983 if ((f->l_start < (curr->base + curr->len)) &&
1984 ((f->l_start + f->l_len) > curr->base)) {
1985 /* region overlaps */
1986 return FALSE;
1990 #endif
1992 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
1993 curr->processId = GetCurrentProcessId();
1994 curr->base = f->l_start;
1995 curr->len = f->l_len;
1996 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
1997 curr->next = locks;
1998 curr->dos_file = file;
1999 locks = curr;
2000 return TRUE;
2003 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2005 DWORD processId;
2006 DOS_FILE_LOCK **curr;
2007 DOS_FILE_LOCK *rem;
2009 processId = GetCurrentProcessId();
2010 curr = &locks;
2011 while (*curr) {
2012 if ((*curr)->dos_file == file) {
2013 rem = *curr;
2014 *curr = (*curr)->next;
2015 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2016 HeapFree( SystemHeap, 0, rem );
2018 else
2019 curr = &(*curr)->next;
2023 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2025 DWORD processId;
2026 DOS_FILE_LOCK **curr;
2027 DOS_FILE_LOCK *rem;
2029 processId = GetCurrentProcessId();
2030 for (curr = &locks; *curr; curr = &(*curr)->next) {
2031 if ((*curr)->processId == processId &&
2032 (*curr)->dos_file == file &&
2033 (*curr)->base == f->l_start &&
2034 (*curr)->len == f->l_len) {
2035 /* this is the same lock */
2036 rem = *curr;
2037 *curr = (*curr)->next;
2038 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2039 HeapFree( SystemHeap, 0, rem );
2040 return TRUE;
2043 /* no matching lock found */
2044 return FALSE;
2048 /**************************************************************************
2049 * LockFile (KERNEL32.511)
2051 BOOL WINAPI LockFile(
2052 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2053 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2055 struct flock f;
2056 FILE_OBJECT *file;
2058 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2059 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2060 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2062 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2063 FIXME(file, "Unimplemented bytes > 32bits\n");
2064 return FALSE;
2067 f.l_start = dwFileOffsetLow;
2068 f.l_len = nNumberOfBytesToLockLow;
2069 f.l_whence = SEEK_SET;
2070 f.l_pid = 0;
2071 f.l_type = F_WRLCK;
2073 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2075 /* shadow locks internally */
2076 if (!DOS_AddLock(file, &f)) {
2077 SetLastError( ERROR_LOCK_VIOLATION );
2078 return FALSE;
2081 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2082 #ifdef USE_UNIX_LOCKS
2083 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2084 if (errno == EACCES || errno == EAGAIN) {
2085 SetLastError( ERROR_LOCK_VIOLATION );
2087 else {
2088 FILE_SetDosError();
2090 /* remove our internal copy of the lock */
2091 DOS_RemoveLock(file, &f);
2092 return FALSE;
2094 #endif
2095 return TRUE;
2099 /**************************************************************************
2100 * UnlockFile (KERNEL32.703)
2102 BOOL WINAPI UnlockFile(
2103 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2104 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2106 FILE_OBJECT *file;
2107 struct flock f;
2109 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2110 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2111 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2113 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2114 WARN(file, "Unimplemented bytes > 32bits\n");
2115 return FALSE;
2118 f.l_start = dwFileOffsetLow;
2119 f.l_len = nNumberOfBytesToUnlockLow;
2120 f.l_whence = SEEK_SET;
2121 f.l_pid = 0;
2122 f.l_type = F_UNLCK;
2124 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2126 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2128 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2129 #ifdef USE_UNIX_LOCKS
2130 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2131 FILE_SetDosError();
2132 return FALSE;
2134 #endif
2135 return TRUE;
2137 #endif
2139 /**************************************************************************
2140 * GetFileAttributesEx32A [KERNEL32.874]
2142 BOOL WINAPI GetFileAttributesExA(
2143 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2144 LPVOID lpFileInformation)
2146 DOS_FULL_NAME full_name;
2147 BY_HANDLE_FILE_INFORMATION info;
2149 if (lpFileName == NULL) return FALSE;
2150 if (lpFileInformation == NULL) return FALSE;
2152 if (fInfoLevelId == GetFileExInfoStandard) {
2153 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2154 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2155 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2156 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2158 lpFad->dwFileAttributes = info.dwFileAttributes;
2159 lpFad->ftCreationTime = info.ftCreationTime;
2160 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2161 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2162 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2163 lpFad->nFileSizeLow = info.nFileSizeLow;
2165 else {
2166 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
2167 return FALSE;
2170 return TRUE;
2174 /**************************************************************************
2175 * GetFileAttributesEx32W [KERNEL32.875]
2177 BOOL WINAPI GetFileAttributesExW(
2178 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2179 LPVOID lpFileInformation)
2181 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2182 BOOL res =
2183 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2184 HeapFree( GetProcessHeap(), 0, nameA );
2185 return res;