Release 980726
[wine/testsucceed.git] / files / file.c
blobd9ac977620259e12fd80d048c79603bb0d5b2532
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <assert.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/mman.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include <utime.h>
22 #include "windows.h"
23 #include "winerror.h"
24 #include "drive.h"
25 #include "file.h"
26 #include "global.h"
27 #include "heap.h"
28 #include "msdos.h"
29 #include "options.h"
30 #include "ldt.h"
31 #include "process.h"
32 #include "task.h"
33 #include "debug.h"
35 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
36 #define MAP_ANON MAP_ANONYMOUS
37 #endif
39 static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
40 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped);
41 static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars,
42 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped);
43 static void FILE_Destroy( K32OBJ *obj );
45 const K32OBJ_OPS FILE_Ops =
47 /* Object cannot be waited upon (FIXME: for now) */
48 NULL, /* signaled */
49 NULL, /* satisfied */
50 NULL, /* add_wait */
51 NULL, /* remove_wait */
52 FILE_Read, /* read */
53 FILE_Write, /* write */
54 FILE_Destroy /* destroy */
57 struct DOS_FILE_LOCK {
58 struct DOS_FILE_LOCK * next;
59 DWORD base;
60 DWORD len;
61 DWORD processId;
62 FILE_OBJECT * dos_file;
63 char * unix_name;
66 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
68 static DOS_FILE_LOCK *locks = NULL;
69 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
71 /***********************************************************************
72 * FILE_Alloc
74 * Allocate a file.
76 HFILE32 FILE_Alloc( FILE_OBJECT **file )
78 HFILE32 handle;
79 *file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) );
80 if (!*file)
82 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
83 return (HFILE32)NULL;
85 (*file)->header.type = K32OBJ_FILE;
86 (*file)->header.refcount = 0;
87 (*file)->unix_handle = -1;
88 (*file)->unix_name = NULL;
89 (*file)->type = FILE_TYPE_DISK;
91 handle = HANDLE_Alloc( PROCESS_Current(), &(*file)->header,
92 FILE_ALL_ACCESS | GENERIC_READ |
93 GENERIC_WRITE | GENERIC_EXECUTE /*FIXME*/, TRUE );
94 /* If the allocation failed, the object is already destroyed */
95 if (handle == INVALID_HANDLE_VALUE32) *file = NULL;
96 return handle;
100 /* FIXME: lpOverlapped is ignored */
101 static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
102 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
104 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
105 int result;
107 TRACE(file, "%p %p %ld\n", ptr, lpBuffer,
108 nNumberOfChars);
110 if (nNumberOfChars == 0) {
111 *lpNumberOfChars = 0; /* FIXME: does this change */
112 return TRUE;
115 if ((result = read(file->unix_handle, lpBuffer, nNumberOfChars)) == -1)
117 FILE_SetDosError();
118 return FALSE;
120 *lpNumberOfChars = result;
121 return TRUE;
125 * experimentation yields that WriteFile:
126 * o does not truncate on write of 0
127 * o always changes the *lpNumberOfChars to actual number of
128 * characters written
129 * o write of 0 nNumberOfChars returns TRUE
131 static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars,
132 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
134 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
135 int result;
137 TRACE(file, "%p %p %ld\n", ptr, lpBuffer,
138 nNumberOfChars);
140 *lpNumberOfChars = 0;
143 * I assume this loop around EAGAIN is here because
144 * win32 doesn't have interrupted system calls
147 for (;;)
149 result = write(file->unix_handle, lpBuffer, nNumberOfChars);
150 if (result != -1) {
151 *lpNumberOfChars = result;
152 return TRUE;
154 if (errno != EINTR) {
155 FILE_SetDosError();
156 return FALSE;
163 /***********************************************************************
164 * FILE_Destroy
166 * Destroy a DOS file.
168 static void FILE_Destroy( K32OBJ *ptr )
170 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
171 assert( ptr->type == K32OBJ_FILE );
173 DOS_RemoveFileLocks(file);
175 if (file->unix_handle != -1) close( file->unix_handle );
176 if (file->unix_name) HeapFree( SystemHeap, 0, file->unix_name );
177 ptr->type = K32OBJ_UNKNOWN;
178 HeapFree( SystemHeap, 0, file );
182 /***********************************************************************
183 * FILE_GetFile
185 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
186 * be called to release the file.
188 FILE_OBJECT *FILE_GetFile( HFILE32 handle )
190 return (FILE_OBJECT *)HANDLE_GetObjPtr( PROCESS_Current(), handle,
191 K32OBJ_FILE, 0 /*FIXME*/ );
195 /***********************************************************************
196 * FILE_ReleaseFile
198 * Release a DOS file obtained with FILE_GetFile.
200 void FILE_ReleaseFile( FILE_OBJECT *file )
202 K32OBJ_DecCount( &file->header );
206 /***********************************************************************
207 * FILE_GetUnixHandle
209 * Return the Unix handle associated to a file handle.
211 int FILE_GetUnixHandle( HFILE32 hFile )
213 FILE_OBJECT *file;
214 int ret;
216 if (!(file = FILE_GetFile( hFile ))) return -1;
217 ret = file->unix_handle;
218 FILE_ReleaseFile( file );
219 return ret;
223 /***********************************************************************
224 * FILE_SetDosError
226 * Set the DOS error code from errno.
228 void FILE_SetDosError(void)
230 int save_errno = errno; /* errno gets overwritten by printf */
232 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
233 switch (save_errno)
235 case EAGAIN:
236 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
237 break;
238 case EBADF:
239 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
240 break;
241 case ENOSPC:
242 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
243 break;
244 case EACCES:
245 case EPERM:
246 case EROFS:
247 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
248 break;
249 case EBUSY:
250 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
251 break;
252 case ENOENT:
253 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
254 break;
255 case EISDIR:
256 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
257 break;
258 case ENFILE:
259 case EMFILE:
260 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
261 break;
262 case EEXIST:
263 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
264 break;
265 case EINVAL:
266 case ESPIPE:
267 DOS_ERROR( ER_SeekError, EC_NotFound, SA_Ignore, EL_Disk );
268 break;
269 case ENOTEMPTY:
270 DOS_ERROR( ERROR_DIR_NOT_EMPTY, EC_Exists, SA_Ignore, EL_Disk );
271 break;
272 default:
273 perror( "int21: unknown errno" );
274 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
275 break;
277 errno = save_errno;
281 /***********************************************************************
282 * FILE_DupUnixHandle
284 * Duplicate a Unix handle into a task handle.
286 HFILE32 FILE_DupUnixHandle( int fd )
288 HFILE32 handle;
289 FILE_OBJECT *file;
291 if ((handle = FILE_Alloc( &file )) != INVALID_HANDLE_VALUE32)
293 if ((file->unix_handle = dup(fd)) == -1)
295 FILE_SetDosError();
296 CloseHandle( handle );
297 return INVALID_HANDLE_VALUE32;
300 return handle;
304 /***********************************************************************
305 * FILE_OpenUnixFile
307 HFILE32 FILE_OpenUnixFile( const char *name, int mode )
309 HFILE32 handle;
310 FILE_OBJECT *file;
311 struct stat st;
313 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
314 return INVALID_HANDLE_VALUE32;
316 if ((file->unix_handle = open( name, mode, 0666 )) == -1)
318 if (!Options.failReadOnly && (mode == O_RDWR))
319 file->unix_handle = open( name, O_RDONLY );
321 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
323 FILE_SetDosError();
324 CloseHandle( handle );
325 return INVALID_HANDLE_VALUE32;
327 if (S_ISDIR(st.st_mode))
329 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
330 CloseHandle( handle );
331 return INVALID_HANDLE_VALUE32;
334 /* File opened OK, now fill the FILE_OBJECT */
336 file->unix_name = HEAP_strdupA( SystemHeap, 0, name );
337 return handle;
341 /***********************************************************************
342 * FILE_Open
344 HFILE32 FILE_Open( LPCSTR path, INT32 mode )
346 DOS_FULL_NAME full_name;
347 const char *unixName;
349 TRACE(file, "'%s' %04x\n", path, mode );
351 if (!path) return HFILE_ERROR32;
353 if (DOSFS_GetDevice( path ))
355 HFILE32 ret;
357 TRACE(file, "opening device '%s'\n", path );
359 if (HFILE_ERROR32!=(ret=DOSFS_OpenDevice( path, mode )))
360 return ret;
362 /* Do not silence this please. It is a critical error. -MM */
363 ERR(file, "Couldn't open device '%s'!\n",path);
364 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
365 return HFILE_ERROR32;
368 else /* check for filename, don't check for last entry if creating */
370 if (!DOSFS_GetFullName( path, !(mode & O_CREAT), &full_name ))
371 return HFILE_ERROR32;
372 unixName = full_name.long_name;
374 return FILE_OpenUnixFile( unixName, mode );
378 /***********************************************************************
379 * FILE_Create
381 static HFILE32 FILE_Create( LPCSTR path, int mode, int unique )
383 HFILE32 handle;
384 FILE_OBJECT *file;
385 DOS_FULL_NAME full_name;
387 TRACE(file, "'%s' %04x %d\n", path, mode, unique );
389 if (!path) return INVALID_HANDLE_VALUE32;
391 if (DOSFS_GetDevice( path ))
393 WARN(file, "cannot create DOS device '%s'!\n", path);
394 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
395 return INVALID_HANDLE_VALUE32;
398 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
399 return INVALID_HANDLE_VALUE32;
401 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
403 CloseHandle( handle );
404 return INVALID_HANDLE_VALUE32;
406 if ((file->unix_handle = open( full_name.long_name,
407 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
408 mode )) == -1)
410 FILE_SetDosError();
411 CloseHandle( handle );
412 return INVALID_HANDLE_VALUE32;
415 /* File created OK, now fill the FILE_OBJECT */
417 file->unix_name = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
418 return handle;
422 /***********************************************************************
423 * FILE_FillInfo
425 * Fill a file information from a struct stat.
427 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
429 if (S_ISDIR(st->st_mode))
430 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
431 else
432 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
433 if (!(st->st_mode & S_IWUSR))
434 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
436 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
437 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
438 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
440 info->dwVolumeSerialNumber = 0; /* FIXME */
441 info->nFileSizeHigh = 0;
442 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
443 info->nNumberOfLinks = st->st_nlink;
444 info->nFileIndexHigh = 0;
445 info->nFileIndexLow = st->st_ino;
449 /***********************************************************************
450 * FILE_Stat
452 * Stat a Unix path name. Return TRUE if OK.
454 BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
456 struct stat st;
458 if (!unixName || !info) return FALSE;
460 if (stat( unixName, &st ) == -1)
462 FILE_SetDosError();
463 return FALSE;
465 FILE_FillInfo( &st, info );
466 return TRUE;
470 /***********************************************************************
471 * GetFileInformationByHandle (KERNEL32.219)
473 DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile,
474 BY_HANDLE_FILE_INFORMATION *info )
476 FILE_OBJECT *file;
477 DWORD ret = 0;
478 struct stat st;
480 if (!info) return 0;
482 if (!(file = FILE_GetFile( hFile ))) return 0;
483 if (fstat( file->unix_handle, &st ) == -1) FILE_SetDosError();
484 else
486 FILE_FillInfo( &st, info );
487 ret = 1;
489 FILE_ReleaseFile( file );
490 return ret;
494 /**************************************************************************
495 * GetFileAttributes16 (KERNEL.420)
497 DWORD WINAPI GetFileAttributes16( LPCSTR name )
499 return GetFileAttributes32A( name );
503 /**************************************************************************
504 * GetFileAttributes32A (KERNEL32.217)
506 DWORD WINAPI GetFileAttributes32A( LPCSTR name )
508 DOS_FULL_NAME full_name;
509 BY_HANDLE_FILE_INFORMATION info;
511 if (name == NULL) return -1;
513 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
514 if (!FILE_Stat( full_name.long_name, &info )) return -1;
515 return info.dwFileAttributes;
519 /**************************************************************************
520 * GetFileAttributes32W (KERNEL32.218)
522 DWORD WINAPI GetFileAttributes32W( LPCWSTR name )
524 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
525 DWORD res = GetFileAttributes32A( nameA );
526 HeapFree( GetProcessHeap(), 0, nameA );
527 return res;
531 /***********************************************************************
532 * GetFileSize (KERNEL32.220)
534 DWORD WINAPI GetFileSize( HFILE32 hFile, LPDWORD filesizehigh )
536 BY_HANDLE_FILE_INFORMATION info;
537 if (!GetFileInformationByHandle( hFile, &info )) return 0;
538 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
539 return info.nFileSizeLow;
543 /***********************************************************************
544 * GetFileTime (KERNEL32.221)
546 BOOL32 WINAPI GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
547 FILETIME *lpLastAccessTime,
548 FILETIME *lpLastWriteTime )
550 BY_HANDLE_FILE_INFORMATION info;
551 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
552 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
553 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
554 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
555 return TRUE;
558 /***********************************************************************
559 * CompareFileTime (KERNEL32.28)
561 INT32 WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
563 if (!x || !y) return -1;
565 if (x->dwHighDateTime > y->dwHighDateTime)
566 return 1;
567 if (x->dwHighDateTime < y->dwHighDateTime)
568 return -1;
569 if (x->dwLowDateTime > y->dwLowDateTime)
570 return 1;
571 if (x->dwLowDateTime < y->dwLowDateTime)
572 return -1;
573 return 0;
576 /***********************************************************************
577 * FILE_Dup
579 * dup() function for DOS handles.
581 HFILE32 FILE_Dup( HFILE32 hFile )
583 HFILE32 handle;
585 TRACE(file, "FILE_Dup for handle %d\n", hFile );
586 if (!DuplicateHandle( GetCurrentProcess(), hFile, GetCurrentProcess(),
587 &handle, FILE_ALL_ACCESS /* FIXME */, FALSE, 0 ))
588 handle = HFILE_ERROR32;
589 TRACE(file, "FILE_Dup return handle %d\n", handle );
590 return handle;
594 /***********************************************************************
595 * FILE_Dup2
597 * dup2() function for DOS handles.
599 HFILE32 FILE_Dup2( HFILE32 hFile1, HFILE32 hFile2 )
601 FILE_OBJECT *file;
603 TRACE(file, "FILE_Dup2 for handle %d\n", hFile1 );
604 /* FIXME: should use DuplicateHandle */
605 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR32;
606 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2, &file->header, 0 ))
607 hFile2 = HFILE_ERROR32;
608 FILE_ReleaseFile( file );
609 return hFile2;
613 /***********************************************************************
614 * GetTempFileName16 (KERNEL.97)
616 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
617 LPSTR buffer )
619 char temppath[144];
621 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
622 drive |= DRIVE_GetCurrentDrive() + 'A';
624 if ((drive & TF_FORCEDRIVE) &&
625 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
627 drive &= ~TF_FORCEDRIVE;
628 WARN(file, "invalid drive %d specified\n", drive );
631 if (drive & TF_FORCEDRIVE)
632 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
633 else
634 GetTempPath32A( 132, temppath );
635 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
639 /***********************************************************************
640 * GetTempFileName32A (KERNEL32.290)
642 UINT32 WINAPI GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
643 LPSTR buffer)
645 DOS_FULL_NAME full_name;
646 int i;
647 LPSTR p;
648 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
650 if ( !path || !prefix || !buffer ) return 0;
652 strcpy( buffer, path );
653 p = buffer + strlen(buffer);
655 /* add a \, if there isn't one and path is more than just the drive letter ... */
656 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
657 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
659 *p++ = '~';
660 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
661 sprintf( p, "%04x.tmp", num );
663 /* Now try to create it */
665 if (!unique)
669 HFILE32 handle = FILE_Create( buffer, 0666, TRUE );
670 if (handle != INVALID_HANDLE_VALUE32)
671 { /* We created it */
672 TRACE(file, "created %s\n",
673 buffer);
674 CloseHandle( handle );
675 break;
677 if (DOS_ExtendedError != ER_FileExists)
678 break; /* No need to go on */
679 num++;
680 sprintf( p, "%04x.tmp", num );
681 } while (num != (unique & 0xffff));
684 /* Get the full path name */
686 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
688 /* Check if we have write access in the directory */
689 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
690 if (access( full_name.long_name, W_OK ) == -1)
691 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
692 buffer);
694 TRACE(file, "returning %s\n", buffer );
695 return unique ? unique : num;
699 /***********************************************************************
700 * GetTempFileName32W (KERNEL32.291)
702 UINT32 WINAPI GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
703 LPWSTR buffer )
705 LPSTR patha,prefixa;
706 char buffera[144];
707 UINT32 ret;
709 if (!path) return 0;
710 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
711 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
712 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
713 lstrcpyAtoW( buffer, buffera );
714 HeapFree( GetProcessHeap(), 0, patha );
715 HeapFree( GetProcessHeap(), 0, prefixa );
716 return ret;
720 /***********************************************************************
721 * FILE_DoOpenFile
723 * Implementation of OpenFile16() and OpenFile32().
725 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
726 BOOL32 win32 )
728 HFILE32 hFileRet;
729 FILETIME filetime;
730 WORD filedatetime[2];
731 DOS_FULL_NAME full_name;
732 char *p;
733 int unixMode;
735 if (!ofs) return HFILE_ERROR32;
738 ofs->cBytes = sizeof(OFSTRUCT);
739 ofs->nErrCode = 0;
740 if (mode & OF_REOPEN) name = ofs->szPathName;
742 if (!name) {
743 ERR(file, "called with `name' set to NULL ! Please debug.\n");
744 return HFILE_ERROR32;
747 TRACE(file, "%s %04x\n", name, mode );
749 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
750 Are there any cases where getting the path here is wrong?
751 Uwe Bonnes 1997 Apr 2 */
752 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
753 ofs->szPathName, NULL )) goto error;
755 /* OF_PARSE simply fills the structure */
757 if (mode & OF_PARSE)
759 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
760 != DRIVE_REMOVABLE);
761 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
762 name, ofs->szPathName );
763 return 0;
766 /* OF_CREATE is completely different from all other options, so
767 handle it first */
769 if (mode & OF_CREATE)
771 if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32)
772 goto error;
773 goto success;
776 /* If OF_SEARCH is set, ignore the given path */
778 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
780 /* First try the file name as is */
781 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
782 /* Now remove the path */
783 if (name[0] && (name[1] == ':')) name += 2;
784 if ((p = strrchr( name, '\\' ))) name = p + 1;
785 if ((p = strrchr( name, '/' ))) name = p + 1;
786 if (!name[0]) goto not_found;
789 /* Now look for the file */
791 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
793 found:
794 TRACE(file, "found %s = %s\n",
795 full_name.long_name, full_name.short_name );
796 lstrcpyn32A( ofs->szPathName, full_name.short_name,
797 sizeof(ofs->szPathName) );
799 if (mode & OF_DELETE)
801 if (unlink( full_name.long_name ) == -1) goto not_found;
802 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
803 return 1;
806 switch(mode & 3)
808 case OF_WRITE:
809 unixMode = O_WRONLY; break;
810 case OF_READWRITE:
811 unixMode = O_RDWR; break;
812 case OF_READ:
813 default:
814 unixMode = O_RDONLY; break;
817 hFileRet = FILE_OpenUnixFile( full_name.long_name, unixMode );
818 if (hFileRet == HFILE_ERROR32) goto not_found;
819 GetFileTime( hFileRet, NULL, NULL, &filetime );
820 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
821 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
823 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
825 CloseHandle( hFileRet );
826 WARN(file, "(%s): OF_VERIFY failed\n", name );
827 /* FIXME: what error here? */
828 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
829 goto error;
832 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
834 success: /* We get here if the open was successful */
835 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
836 if (mode & OF_EXIST) /* Return the handle, but close it first */
837 CloseHandle( hFileRet );
838 return hFileRet;
840 not_found: /* We get here if the file does not exist */
841 WARN(file, "'%s' not found\n", name );
842 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
843 /* fall through */
845 error: /* We get here if there was an error opening the file */
846 ofs->nErrCode = DOS_ExtendedError;
847 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
848 name,ofs->nErrCode );
849 return HFILE_ERROR32;
853 /***********************************************************************
854 * OpenFile16 (KERNEL.74)
856 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
858 return FILE_DoOpenFile( name, ofs, mode, FALSE );
862 /***********************************************************************
863 * OpenFile32 (KERNEL32.396)
865 HFILE32 WINAPI OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
867 return FILE_DoOpenFile( name, ofs, mode, TRUE );
871 /***********************************************************************
872 * _lclose16 (KERNEL.81)
874 HFILE16 WINAPI _lclose16( HFILE16 hFile )
876 TRACE(file, "handle %d\n", hFile );
877 return CloseHandle( hFile ) ? 0 : HFILE_ERROR16;
881 /***********************************************************************
882 * _lclose32 (KERNEL32.592)
884 HFILE32 WINAPI _lclose32( HFILE32 hFile )
886 TRACE(file, "handle %d\n", hFile );
887 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
891 /***********************************************************************
892 * WIN16_hread
894 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
896 LONG maxlen;
898 TRACE(file, "%d %08lx %ld\n",
899 hFile, (DWORD)buffer, count );
901 /* Some programs pass a count larger than the allocated buffer */
902 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
903 if (count > maxlen) count = maxlen;
904 return _lread32( hFile, PTR_SEG_TO_LIN(buffer), count );
908 /***********************************************************************
909 * WIN16_lread
911 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
913 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
917 /***********************************************************************
918 * _lread32 (KERNEL32.596)
920 UINT32 WINAPI _lread32( HFILE32 handle, LPVOID buffer, UINT32 count )
922 K32OBJ *ptr;
923 DWORD numWritten;
924 BOOL32 result = FALSE;
926 TRACE( file, "%d %p %d\n", handle, buffer, count);
927 if (!(ptr = HANDLE_GetObjPtr( PROCESS_Current(), handle,
928 K32OBJ_UNKNOWN, 0))) return -1;
929 if (K32OBJ_OPS(ptr)->read)
930 result = K32OBJ_OPS(ptr)->read(ptr, buffer, count, &numWritten, NULL);
931 K32OBJ_DecCount( ptr );
932 if (!result) return -1;
933 return (UINT32)numWritten;
937 /***********************************************************************
938 * _lread16 (KERNEL.82)
940 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
942 return (UINT16)_lread32( hFile, buffer, (LONG)count );
946 /***********************************************************************
947 * _lcreat16 (KERNEL.83)
949 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
951 int mode = (attr & 1) ? 0444 : 0666;
952 TRACE(file, "%s %02x\n", path, attr );
953 return (HFILE16)FILE_Create( path, mode, FALSE );
957 /***********************************************************************
958 * _lcreat32 (KERNEL32.593)
960 HFILE32 WINAPI _lcreat32( LPCSTR path, INT32 attr )
962 int mode = (attr & 1) ? 0444 : 0666;
963 TRACE(file, "%s %02x\n", path, attr );
964 return FILE_Create( path, mode, FALSE );
968 /***********************************************************************
969 * _lcreat_uniq (Not a Windows API)
971 HFILE32 _lcreat_uniq( LPCSTR path, INT32 attr )
973 int mode = (attr & 1) ? 0444 : 0666;
974 TRACE(file, "%s %02x\n", path, attr );
975 return FILE_Create( path, mode, TRUE );
979 /***********************************************************************
980 * SetFilePointer (KERNEL32.492)
982 DWORD WINAPI SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
983 DWORD method )
985 FILE_OBJECT *file;
986 int origin, result;
988 if (highword && *highword)
990 FIXME(file, "64-bit offsets not supported yet\n");
991 SetLastError( ERROR_INVALID_PARAMETER );
992 return 0xffffffff;
994 TRACE(file, "handle %d offset %ld origin %ld\n",
995 hFile, distance, method );
997 if (!(file = FILE_GetFile( hFile ))) return 0xffffffff;
998 switch(method)
1000 case FILE_CURRENT: origin = SEEK_CUR; break;
1001 case FILE_END: origin = SEEK_END; break;
1002 default: origin = SEEK_SET; break;
1005 if ((result = lseek( file->unix_handle, distance, origin )) == -1)
1007 /* care for this pathological case:
1008 SetFilePointer(00000006,ffffffff,00000000,00000002)
1009 ret=0062ab4a fs=01c7 */
1010 if ((distance != -1))
1011 FILE_SetDosError();
1012 else
1013 result = 0;
1015 FILE_ReleaseFile( file );
1016 return (DWORD)result;
1020 /***********************************************************************
1021 * _llseek16 (KERNEL.84)
1023 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1025 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1029 /***********************************************************************
1030 * _llseek32 (KERNEL32.594)
1032 LONG WINAPI _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
1034 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1038 /***********************************************************************
1039 * _lopen16 (KERNEL.85)
1041 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1043 return _lopen32( path, mode );
1047 /***********************************************************************
1048 * _lopen32 (KERNEL32.595)
1050 HFILE32 WINAPI _lopen32( LPCSTR path, INT32 mode )
1052 INT32 unixMode;
1054 TRACE(file, "('%s',%04x)\n", path, mode );
1056 switch(mode & 3)
1058 case OF_WRITE:
1059 unixMode = O_WRONLY;
1060 break;
1061 case OF_READWRITE:
1062 unixMode = O_RDWR;
1063 break;
1064 case OF_READ:
1065 default:
1066 unixMode = O_RDONLY;
1067 break;
1069 return FILE_Open( path, unixMode );
1073 /***********************************************************************
1074 * _lwrite16 (KERNEL.86)
1076 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1078 return (UINT16)_hwrite32( hFile, buffer, (LONG)count );
1081 /***********************************************************************
1082 * _lwrite32 (KERNEL.86)
1084 UINT32 WINAPI _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
1086 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
1090 /***********************************************************************
1091 * _hread16 (KERNEL.349)
1093 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1095 return _lread32( hFile, buffer, count );
1099 /***********************************************************************
1100 * _hread32 (KERNEL32.590)
1102 LONG WINAPI _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
1104 return _lread32( hFile, buffer, count );
1108 /***********************************************************************
1109 * _hwrite16 (KERNEL.350)
1111 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1113 return _hwrite32( hFile, buffer, count );
1117 /***********************************************************************
1118 * _hwrite32 (KERNEL32.591)
1120 * experimenation yields that _lwrite:
1121 * o truncates the file at the current position with
1122 * a 0 len write
1123 * o returns 0 on a 0 length write
1124 * o works with console handles
1127 LONG WINAPI _hwrite32( HFILE32 handle, LPCSTR buffer, LONG count )
1129 K32OBJ *ioptr;
1130 DWORD result;
1131 BOOL32 status = FALSE;
1133 TRACE(file, "%d %p %ld\n", handle, buffer, count );
1135 if (count == 0) { /* Expand or truncate at current position */
1136 FILE_OBJECT *file = FILE_GetFile(handle);
1138 if ( ftruncate(file->unix_handle,
1139 lseek( file->unix_handle, 0, SEEK_CUR)) == 0 ) {
1140 FILE_ReleaseFile(file);
1141 return 0;
1142 } else {
1143 FILE_SetDosError();
1144 FILE_ReleaseFile(file);
1145 return HFILE_ERROR32;
1149 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), handle,
1150 K32OBJ_UNKNOWN, 0 )))
1151 return HFILE_ERROR32;
1152 if (K32OBJ_OPS(ioptr)->write)
1153 status = K32OBJ_OPS(ioptr)->write(ioptr, buffer, count, &result, NULL);
1154 K32OBJ_DecCount( ioptr );
1155 if (!status) result = HFILE_ERROR32;
1156 return result;
1160 /***********************************************************************
1161 * SetHandleCount16 (KERNEL.199)
1163 UINT16 WINAPI SetHandleCount16( UINT16 count )
1165 HGLOBAL16 hPDB = GetCurrentPDB();
1166 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1167 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1169 TRACE(file, "(%d)\n", count );
1171 if (count < 20) count = 20; /* No point in going below 20 */
1172 else if (count > 254) count = 254;
1174 if (count == 20)
1176 if (pdb->nbFiles > 20)
1178 memcpy( pdb->fileHandles, files, 20 );
1179 GlobalFree16( pdb->hFileHandles );
1180 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1181 GlobalHandleToSel( hPDB ) );
1182 pdb->hFileHandles = 0;
1183 pdb->nbFiles = 20;
1186 else /* More than 20, need a new file handles table */
1188 BYTE *newfiles;
1189 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1190 if (!newhandle)
1192 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1193 return pdb->nbFiles;
1195 newfiles = (BYTE *)GlobalLock16( newhandle );
1197 if (count > pdb->nbFiles)
1199 memcpy( newfiles, files, pdb->nbFiles );
1200 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1202 else memcpy( newfiles, files, count );
1203 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1204 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1205 pdb->hFileHandles = newhandle;
1206 pdb->nbFiles = count;
1208 return pdb->nbFiles;
1212 /*************************************************************************
1213 * SetHandleCount32 (KERNEL32.494)
1215 UINT32 WINAPI SetHandleCount32( UINT32 count )
1217 return MIN( 256, count );
1221 /***********************************************************************
1222 * FlushFileBuffers (KERNEL32.133)
1224 BOOL32 WINAPI FlushFileBuffers( HFILE32 hFile )
1226 FILE_OBJECT *file;
1227 BOOL32 ret;
1229 TRACE(file, "(%d)\n", hFile );
1230 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1231 if (fsync( file->unix_handle ) != -1) ret = TRUE;
1232 else
1234 FILE_SetDosError();
1235 ret = FALSE;
1237 FILE_ReleaseFile( file );
1238 return ret;
1242 /**************************************************************************
1243 * SetEndOfFile (KERNEL32.483)
1245 BOOL32 WINAPI SetEndOfFile( HFILE32 hFile )
1247 FILE_OBJECT *file;
1248 BOOL32 ret = TRUE;
1250 TRACE(file, "(%d)\n", hFile );
1251 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1252 if (ftruncate( file->unix_handle,
1253 lseek( file->unix_handle, 0, SEEK_CUR ) ))
1255 FILE_SetDosError();
1256 ret = FALSE;
1258 FILE_ReleaseFile( file );
1259 return ret;
1263 /***********************************************************************
1264 * DeleteFile16 (KERNEL.146)
1266 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1268 return DeleteFile32A( path );
1272 /***********************************************************************
1273 * DeleteFile32A (KERNEL32.71)
1275 BOOL32 WINAPI DeleteFile32A( LPCSTR path )
1277 DOS_FULL_NAME full_name;
1279 TRACE(file, "'%s'\n", path );
1281 if (DOSFS_GetDevice( path ))
1283 WARN(file, "cannot remove DOS device '%s'!\n", path);
1284 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1285 return FALSE;
1288 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1289 if (unlink( full_name.long_name ) == -1)
1291 FILE_SetDosError();
1292 return FALSE;
1294 return TRUE;
1298 /***********************************************************************
1299 * DeleteFile32W (KERNEL32.72)
1301 BOOL32 WINAPI DeleteFile32W( LPCWSTR path )
1303 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1304 BOOL32 ret = DeleteFile32A( xpath );
1305 HeapFree( GetProcessHeap(), 0, xpath );
1306 return ret;
1310 /***********************************************************************
1311 * FILE_SetFileType
1313 BOOL32 FILE_SetFileType( HFILE32 hFile, DWORD type )
1315 FILE_OBJECT *file = FILE_GetFile( hFile );
1316 if (!file) return FALSE;
1317 file->type = type;
1318 FILE_ReleaseFile( file );
1319 return TRUE;
1323 /***********************************************************************
1324 * FILE_mmap
1326 LPVOID FILE_mmap( HFILE32 hFile, LPVOID start,
1327 DWORD size_high, DWORD size_low,
1328 DWORD offset_high, DWORD offset_low,
1329 int prot, int flags )
1331 LPVOID ret;
1332 FILE_OBJECT *file = FILE_GetFile( hFile );
1333 if (!file) return (LPVOID)-1;
1334 ret = FILE_dommap( file, start, size_high, size_low,
1335 offset_high, offset_low, prot, flags );
1336 FILE_ReleaseFile( file );
1337 return ret;
1341 /***********************************************************************
1342 * FILE_dommap
1344 LPVOID FILE_dommap( FILE_OBJECT *file, LPVOID start,
1345 DWORD size_high, DWORD size_low,
1346 DWORD offset_high, DWORD offset_low,
1347 int prot, int flags )
1349 int fd = -1;
1350 int pos;
1351 LPVOID ret;
1353 if (size_high || offset_high)
1354 FIXME(file, "offsets larger than 4Gb not supported\n");
1356 if (!file)
1358 #ifdef MAP_ANON
1359 flags |= MAP_ANON;
1360 #else
1361 static int fdzero = -1;
1363 if (fdzero == -1)
1365 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1367 perror( "/dev/zero: open" );
1368 exit(1);
1371 fd = fdzero;
1372 #endif /* MAP_ANON */
1373 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1374 #ifdef MAP_SHARED
1375 flags &= ~MAP_SHARED;
1376 #endif
1377 #ifdef MAP_PRIVATE
1378 flags |= MAP_PRIVATE;
1379 #endif
1381 else fd = file->unix_handle;
1383 if ((ret = mmap( start, size_low, prot,
1384 flags, fd, offset_low )) != (LPVOID)-1)
1385 return ret;
1387 /* mmap() failed; if this is because the file offset is not */
1388 /* page-aligned (EINVAL), or because the underlying filesystem */
1389 /* does not support mmap() (ENOEXEC), we do it by hand. */
1391 if (!file) return ret;
1392 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1393 if (prot & PROT_WRITE)
1395 /* We cannot fake shared write mappings */
1396 #ifdef MAP_SHARED
1397 if (flags & MAP_SHARED) return ret;
1398 #endif
1399 #ifdef MAP_PRIVATE
1400 if (!(flags & MAP_PRIVATE)) return ret;
1401 #endif
1403 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1404 /* Reserve the memory with an anonymous mmap */
1405 ret = FILE_dommap( NULL, start, size_high, size_low, 0, 0,
1406 PROT_READ | PROT_WRITE, flags );
1407 if (ret == (LPVOID)-1) return ret;
1408 /* Now read in the file */
1409 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1411 FILE_munmap( ret, size_high, size_low );
1412 return (LPVOID)-1;
1414 read( fd, ret, size_low );
1415 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1416 mprotect( ret, size_low, prot ); /* Set the right protection */
1417 return ret;
1421 /***********************************************************************
1422 * FILE_munmap
1424 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1426 if (size_high)
1427 FIXME(file, "offsets larger than 4Gb not supported\n");
1428 return munmap( start, size_low );
1432 /***********************************************************************
1433 * GetFileType (KERNEL32.222)
1435 DWORD WINAPI GetFileType( HFILE32 hFile )
1437 FILE_OBJECT *file = FILE_GetFile(hFile);
1438 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1439 FILE_ReleaseFile( file );
1440 return file->type;
1444 /**************************************************************************
1445 * MoveFileEx32A (KERNEL32.???)
1447 BOOL32 WINAPI MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1449 DOS_FULL_NAME full_name1, full_name2;
1450 int mode=0; /* mode == 1: use copy */
1452 TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag);
1454 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1455 if (fn2) { /* !fn2 means delete fn1 */
1456 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1457 /* Source name and target path are valid */
1458 if ( full_name1.drive != full_name2.drive)
1459 /* use copy, if allowed */
1460 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1461 /* FIXME: Use right error code */
1462 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
1463 return FALSE;
1465 else mode =1;
1466 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1467 /* target exists, check if we may overwrite */
1468 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1469 /* FIXME: Use right error code */
1470 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1471 return FALSE;
1474 else /* fn2 == NULL means delete source */
1475 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1476 if (flag & MOVEFILE_COPY_ALLOWED) {
1477 WARN(file, "Illegal flag\n");
1478 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1479 EL_Unknown );
1480 return FALSE;
1482 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1483 Perhaps we should queue these command and execute it
1484 when exiting... What about using on_exit(2)
1486 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1487 full_name1.long_name);
1488 return TRUE;
1490 else if (unlink( full_name1.long_name ) == -1)
1492 FILE_SetDosError();
1493 return FALSE;
1495 else return TRUE; /* successfully deleted */
1497 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1498 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1499 Perhaps we should queue these command and execute it
1500 when exiting... What about using on_exit(2)
1502 FIXME(file,"Please move existing file '%s' to file '%s'"
1503 "when Wine has finished\n",
1504 full_name1.long_name, full_name2.long_name);
1505 return TRUE;
1508 if (!mode) /* move the file */
1509 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1511 FILE_SetDosError();
1512 return FALSE;
1514 else return TRUE;
1515 else /* copy File */
1516 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1520 /**************************************************************************
1521 * MoveFileEx32W (KERNEL32.???)
1523 BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1525 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1526 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1527 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1528 HeapFree( GetProcessHeap(), 0, afn1 );
1529 HeapFree( GetProcessHeap(), 0, afn2 );
1530 return res;
1534 /**************************************************************************
1535 * MoveFile32A (KERNEL32.387)
1537 * Move file or directory
1539 BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1541 DOS_FULL_NAME full_name1, full_name2;
1542 struct stat fstat;
1544 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1546 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1547 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1548 /* The new name must not already exist */
1549 return FALSE;
1550 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1552 if (full_name1.drive == full_name2.drive) /* move */
1553 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1555 FILE_SetDosError();
1556 return FALSE;
1558 else return TRUE;
1559 else /*copy */ {
1560 if (stat( full_name1.long_name, &fstat ))
1562 WARN(file, "Invalid source file %s\n",
1563 full_name1.long_name);
1564 FILE_SetDosError();
1565 return FALSE;
1567 if (S_ISDIR(fstat.st_mode)) {
1568 /* No Move for directories across file systems */
1569 /* FIXME: Use right error code */
1570 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1571 EL_Unknown );
1572 return FALSE;
1574 else
1575 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1580 /**************************************************************************
1581 * MoveFile32W (KERNEL32.390)
1583 BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1585 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1586 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1587 BOOL32 res = MoveFile32A( afn1, afn2 );
1588 HeapFree( GetProcessHeap(), 0, afn1 );
1589 HeapFree( GetProcessHeap(), 0, afn2 );
1590 return res;
1594 /**************************************************************************
1595 * CopyFile32A (KERNEL32.36)
1597 BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1599 HFILE32 h1, h2;
1600 BY_HANDLE_FILE_INFORMATION info;
1601 UINT32 count;
1602 BOOL32 ret = FALSE;
1603 int mode;
1604 char buffer[2048];
1606 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1607 if (!GetFileInformationByHandle( h1, &info ))
1609 CloseHandle( h1 );
1610 return FALSE;
1612 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1613 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1615 CloseHandle( h1 );
1616 return FALSE;
1618 while ((count = _lread32( h1, buffer, sizeof(buffer) )) > 0)
1620 char *p = buffer;
1621 while (count > 0)
1623 INT32 res = _lwrite32( h2, p, count );
1624 if (res <= 0) goto done;
1625 p += res;
1626 count -= res;
1629 ret = TRUE;
1630 done:
1631 CloseHandle( h1 );
1632 CloseHandle( h2 );
1633 return ret;
1637 /**************************************************************************
1638 * CopyFile32W (KERNEL32.37)
1640 BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
1642 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1643 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1644 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1645 HeapFree( GetProcessHeap(), 0, sourceA );
1646 HeapFree( GetProcessHeap(), 0, destA );
1647 return ret;
1651 /***********************************************************************
1652 * SetFileTime (KERNEL32.493)
1654 BOOL32 WINAPI SetFileTime( HFILE32 hFile,
1655 const FILETIME *lpCreationTime,
1656 const FILETIME *lpLastAccessTime,
1657 const FILETIME *lpLastWriteTime )
1659 FILE_OBJECT *file = FILE_GetFile(hFile);
1660 struct utimbuf utimbuf;
1662 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1663 TRACE(file,"(%s,%p,%p,%p)\n",
1664 file->unix_name,
1665 lpCreationTime,
1666 lpLastAccessTime,
1667 lpLastWriteTime
1669 if (lpLastAccessTime)
1670 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1671 else
1672 utimbuf.actime = 0; /* FIXME */
1673 if (lpLastWriteTime)
1674 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1675 else
1676 utimbuf.modtime = 0; /* FIXME */
1677 if (-1==utime(file->unix_name,&utimbuf))
1679 FILE_ReleaseFile( file );
1680 FILE_SetDosError();
1681 return FALSE;
1683 FILE_ReleaseFile( file );
1684 return TRUE;
1687 /* Locks need to be mirrored because unix file locking is based
1688 * on the pid. Inside of wine there can be multiple WINE processes
1689 * that share the same unix pid.
1690 * Read's and writes should check these locks also - not sure
1691 * how critical that is at this point (FIXME).
1694 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
1696 DOS_FILE_LOCK *curr;
1697 DWORD processId;
1699 processId = GetCurrentProcessId();
1701 /* check if lock overlaps a current lock for the same file */
1702 for (curr = locks; curr; curr = curr->next) {
1703 if (strcmp(curr->unix_name, file->unix_name) == 0) {
1704 if ((f->l_start == curr->base) && (f->l_len == curr->len))
1705 return TRUE;/* region is identic */
1706 if ((f->l_start < (curr->base + curr->len)) &&
1707 ((f->l_start + f->l_len) > curr->base)) {
1708 /* region overlaps */
1709 return FALSE;
1714 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
1715 curr->processId = GetCurrentProcessId();
1716 curr->base = f->l_start;
1717 curr->len = f->l_len;
1718 curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);
1719 curr->next = locks;
1720 curr->dos_file = file;
1721 locks = curr;
1722 return TRUE;
1725 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
1727 DWORD processId;
1728 DOS_FILE_LOCK **curr;
1729 DOS_FILE_LOCK *rem;
1731 processId = GetCurrentProcessId();
1732 curr = &locks;
1733 while (*curr) {
1734 if ((*curr)->dos_file == file) {
1735 rem = *curr;
1736 *curr = (*curr)->next;
1737 HeapFree( SystemHeap, 0, rem->unix_name );
1738 HeapFree( SystemHeap, 0, rem );
1740 else
1741 curr = &(*curr)->next;
1745 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
1747 DWORD processId;
1748 DOS_FILE_LOCK **curr;
1749 DOS_FILE_LOCK *rem;
1751 processId = GetCurrentProcessId();
1752 for (curr = &locks; *curr; curr = &(*curr)->next) {
1753 if ((*curr)->processId == processId &&
1754 (*curr)->dos_file == file &&
1755 (*curr)->base == f->l_start &&
1756 (*curr)->len == f->l_len) {
1757 /* this is the same lock */
1758 rem = *curr;
1759 *curr = (*curr)->next;
1760 HeapFree( SystemHeap, 0, rem->unix_name );
1761 HeapFree( SystemHeap, 0, rem );
1762 return TRUE;
1765 /* no matching lock found */
1766 return FALSE;
1770 /**************************************************************************
1771 * LockFile (KERNEL32.511)
1773 BOOL32 WINAPI LockFile(
1774 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1775 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
1777 struct flock f;
1778 FILE_OBJECT *file;
1780 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1781 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1782 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
1784 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
1785 FIXME(file, "Unimplemented bytes > 32bits\n");
1786 return FALSE;
1789 f.l_start = dwFileOffsetLow;
1790 f.l_len = nNumberOfBytesToLockLow;
1791 f.l_whence = SEEK_SET;
1792 f.l_pid = 0;
1793 f.l_type = F_WRLCK;
1795 if (!(file = FILE_GetFile(hFile))) return FALSE;
1797 /* shadow locks internally */
1798 if (!DOS_AddLock(file, &f)) {
1799 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1800 return FALSE;
1803 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1804 #ifdef USE_UNIX_LOCKS
1805 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1806 if (errno == EACCES || errno == EAGAIN) {
1807 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1809 else {
1810 FILE_SetDosError();
1812 /* remove our internal copy of the lock */
1813 DOS_RemoveLock(file, &f);
1814 return FALSE;
1816 #endif
1817 return TRUE;
1821 /**************************************************************************
1822 * UnlockFile (KERNEL32.703)
1824 BOOL32 WINAPI UnlockFile(
1825 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1826 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
1828 FILE_OBJECT *file;
1829 struct flock f;
1831 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1832 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1833 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
1835 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
1836 WARN(file, "Unimplemented bytes > 32bits\n");
1837 return FALSE;
1840 f.l_start = dwFileOffsetLow;
1841 f.l_len = nNumberOfBytesToUnlockLow;
1842 f.l_whence = SEEK_SET;
1843 f.l_pid = 0;
1844 f.l_type = F_UNLCK;
1846 if (!(file = FILE_GetFile(hFile))) return FALSE;
1848 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
1850 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1851 #ifdef USE_UNIX_LOCKS
1852 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1853 FILE_SetDosError();
1854 return FALSE;
1856 #endif
1857 return TRUE;
1860 /**************************************************************************
1861 * GetFileAttributesEx32A [KERNEL32.874]
1863 BOOL32 WINAPI GetFileAttributesEx32A(
1864 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
1865 LPVOID lpFileInformation)
1867 DOS_FULL_NAME full_name;
1868 BY_HANDLE_FILE_INFORMATION info;
1870 if (lpFileName == NULL) return FALSE;
1871 if (lpFileInformation == NULL) return FALSE;
1873 if (fInfoLevelId == GetFileExInfoStandard) {
1874 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
1875 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
1876 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
1877 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
1879 lpFad->dwFileAttributes = info.dwFileAttributes;
1880 lpFad->ftCreationTime = info.ftCreationTime;
1881 lpFad->ftLastAccessTime = info.ftLastAccessTime;
1882 lpFad->ftLastWriteTime = info.ftLastWriteTime;
1883 lpFad->nFileSizeHigh = info.nFileSizeHigh;
1884 lpFad->nFileSizeLow = info.nFileSizeLow;
1886 else {
1887 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
1888 return FALSE;
1891 return TRUE;
1895 /**************************************************************************
1896 * GetFileAttributesEx32W [KERNEL32.875]
1898 BOOL32 WINAPI GetFileAttributesEx32W(
1899 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
1900 LPVOID lpFileInformation)
1902 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
1903 BOOL32 res =
1904 GetFileAttributesEx32A( nameA, fInfoLevelId, lpFileInformation);
1905 HeapFree( GetProcessHeap(), 0, nameA );
1906 return res;