Release 980301
[wine.git] / files / file.c
blobe1de21814d055666c49213fd8a7b894d4b677824
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 <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/errno.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/mman.h>
19 #include <time.h>
20 #include <unistd.h>
21 #include <utime.h>
23 #include "windows.h"
24 #include "winerror.h"
25 #include "drive.h"
26 #include "file.h"
27 #include "global.h"
28 #include "heap.h"
29 #include "msdos.h"
30 #include "options.h"
31 #include "ldt.h"
32 #include "process.h"
33 #include "task.h"
34 #include "debug.h"
36 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
37 #define MAP_ANON MAP_ANONYMOUS
38 #endif
40 static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
41 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped);
42 static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars,
43 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped);
44 static void FILE_Destroy( K32OBJ *obj );
46 const K32OBJ_OPS FILE_Ops =
48 /* Object cannot be waited upon (FIXME: for now) */
49 NULL, /* signaled */
50 NULL, /* satisfied */
51 NULL, /* add_wait */
52 NULL, /* remove_wait */
53 FILE_Read, /* read */
54 FILE_Write, /* write */
55 FILE_Destroy /* destroy */
58 struct DOS_FILE_LOCK {
59 struct DOS_FILE_LOCK * next;
60 DWORD base;
61 DWORD len;
62 DWORD processId;
63 FILE_OBJECT * dos_file;
64 char * unix_name;
67 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
69 static DOS_FILE_LOCK *locks = NULL;
70 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
72 /***********************************************************************
73 * FILE_Alloc
75 * Allocate a file.
77 static HFILE32 FILE_Alloc( FILE_OBJECT **file )
79 HFILE32 handle;
80 *file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) );
81 if (!*file)
83 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
84 return NULL;
86 (*file)->header.type = K32OBJ_FILE;
87 (*file)->header.refcount = 0;
88 (*file)->unix_handle = -1;
89 (*file)->unix_name = NULL;
90 (*file)->type = FILE_TYPE_DISK;
92 handle = HANDLE_Alloc( &(*file)->header, FILE_ALL_ACCESS | GENERIC_READ |
93 GENERIC_WRITE | GENERIC_EXECUTE /*FIXME*/, FALSE );
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 dprintf_info(file, "FILE_Read: %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 dprintf_info(file, "FILE_Write: %p %p %ld\n", ptr, lpBuffer,
138 nNumberOfChars);
140 *lpNumberOfChars = 0;
142 /* FIXME: there was a loop here before that
143 * retried writes after EAGAIN, why??? -- I assume
144 * it is because win32 doesn't have interrupted
145 * system calls
148 for (;;)
150 result = write(file->unix_handle, lpBuffer, nNumberOfChars);
151 if (result != -1) {
152 *lpNumberOfChars = result;
153 return TRUE;
155 if (errno != EINTR) {
156 FILE_SetDosError();
157 return FALSE;
164 /***********************************************************************
165 * FILE_Destroy
167 * Destroy a DOS file.
169 static void FILE_Destroy( K32OBJ *ptr )
171 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
172 assert( ptr->type == K32OBJ_FILE );
174 DOS_RemoveFileLocks(file);
176 if (file->unix_handle != -1) close( file->unix_handle );
177 if (file->unix_name) HeapFree( SystemHeap, 0, file->unix_name );
178 ptr->type = K32OBJ_UNKNOWN;
179 HeapFree( SystemHeap, 0, file );
183 /***********************************************************************
184 * FILE_GetFile
186 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
187 * be called to release the file.
189 static FILE_OBJECT *FILE_GetFile( HFILE32 handle )
191 return (FILE_OBJECT *)HANDLE_GetObjPtr( handle, K32OBJ_FILE, 0 /*FIXME*/ );
195 /***********************************************************************
196 * FILE_ReleaseFile
198 * Release a DOS file obtained with FILE_GetFile.
200 static 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 dprintf_info(file, "FILE_SetDosError: 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 DOS_ERROR( ER_SeekError, EC_NotFound, SA_Ignore, EL_Disk );
267 break;
268 case ENOTEMPTY:
269 DOS_ERROR( ERROR_DIR_NOT_EMPTY, EC_Exists, SA_Ignore, EL_Disk );
270 break;
271 default:
272 perror( "int21: unknown errno" );
273 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
274 break;
276 errno = save_errno;
280 /***********************************************************************
281 * FILE_DupUnixHandle
283 * Duplicate a Unix handle into a task handle.
285 HFILE32 FILE_DupUnixHandle( int fd )
287 HFILE32 handle;
288 FILE_OBJECT *file;
290 if ((handle = FILE_Alloc( &file )) != INVALID_HANDLE_VALUE32)
292 if ((file->unix_handle = dup(fd)) == -1)
294 FILE_SetDosError();
295 CloseHandle( handle );
296 return INVALID_HANDLE_VALUE32;
299 return handle;
303 /***********************************************************************
304 * FILE_OpenUnixFile
306 HFILE32 FILE_OpenUnixFile( const char *name, int mode )
308 HFILE32 handle;
309 FILE_OBJECT *file;
310 struct stat st;
312 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
313 return INVALID_HANDLE_VALUE32;
315 if ((file->unix_handle = open( name, mode, 0666 )) == -1)
317 if (!Options.failReadOnly && (mode == O_RDWR))
318 file->unix_handle = open( name, O_RDONLY );
320 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
322 FILE_SetDosError();
323 CloseHandle( handle );
324 return INVALID_HANDLE_VALUE32;
326 if (S_ISDIR(st.st_mode))
328 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
329 CloseHandle( handle );
330 return INVALID_HANDLE_VALUE32;
333 /* File opened OK, now fill the FILE_OBJECT */
335 file->unix_name = HEAP_strdupA( SystemHeap, 0, name );
336 return handle;
340 /***********************************************************************
341 * FILE_Open
343 HFILE32 FILE_Open( LPCSTR path, INT32 mode )
345 DOS_FULL_NAME full_name;
346 const char *unixName;
348 dprintf_info(file, "FILE_Open: '%s' %04x\n", path, mode );
350 if (!path) return HFILE_ERROR32;
352 if (DOSFS_IsDevice( path ))
354 HFILE32 ret;
356 dprintf_info(file, "FILE_Open: opening device '%s'\n", path );
358 if (HFILE_ERROR32!=(ret=DOSFS_OpenDevice( path, mode )))
359 return ret;
361 /* Do not silence this please. It is a critical error. -MM */
362 fprintf(stderr, "FILE_Open: Couldn't open device '%s'!\n",path);
363 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
364 return HFILE_ERROR32;
367 else /* check for filename, don't check for last entry if creating */
369 if (!DOSFS_GetFullName( path, !(mode & O_CREAT), &full_name ))
370 return HFILE_ERROR32;
371 unixName = full_name.long_name;
373 return FILE_OpenUnixFile( unixName, mode );
377 /***********************************************************************
378 * FILE_Create
380 static HFILE32 FILE_Create( LPCSTR path, int mode, int unique )
382 HFILE32 handle;
383 FILE_OBJECT *file;
384 DOS_FULL_NAME full_name;
386 dprintf_info(file, "FILE_Create: '%s' %04x %d\n", path, mode, unique );
388 if (!path) return INVALID_HANDLE_VALUE32;
390 if (DOSFS_IsDevice( path ))
392 fprintf(stderr, "FILE_Create: cannot create DOS device '%s'!\n", path);
393 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
394 return INVALID_HANDLE_VALUE32;
397 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
398 return INVALID_HANDLE_VALUE32;
400 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
402 CloseHandle( handle );
403 return INVALID_HANDLE_VALUE32;
405 if ((file->unix_handle = open( full_name.long_name,
406 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
407 mode )) == -1)
409 FILE_SetDosError();
410 CloseHandle( handle );
411 return INVALID_HANDLE_VALUE32;
414 /* File created OK, now fill the FILE_OBJECT */
416 file->unix_name = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
417 return handle;
421 /***********************************************************************
422 * FILE_FillInfo
424 * Fill a file information from a struct stat.
426 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
428 if (S_ISDIR(st->st_mode))
429 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
430 else
431 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
432 if (!(st->st_mode & S_IWUSR))
433 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
435 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
436 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
437 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
439 info->dwVolumeSerialNumber = 0; /* FIXME */
440 info->nFileSizeHigh = 0;
441 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
442 info->nNumberOfLinks = st->st_nlink;
443 info->nFileIndexHigh = 0;
444 info->nFileIndexLow = st->st_ino;
448 /***********************************************************************
449 * FILE_Stat
451 * Stat a Unix path name. Return TRUE if OK.
453 BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
455 struct stat st;
457 if (!unixName || !info) return FALSE;
459 if (stat( unixName, &st ) == -1)
461 FILE_SetDosError();
462 return FALSE;
464 FILE_FillInfo( &st, info );
465 return TRUE;
469 /***********************************************************************
470 * GetFileInformationByHandle (KERNEL32.219)
472 DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile,
473 BY_HANDLE_FILE_INFORMATION *info )
475 FILE_OBJECT *file;
476 DWORD ret = 0;
477 struct stat st;
479 if (!info) return 0;
481 if (!(file = FILE_GetFile( hFile ))) return 0;
482 if (fstat( file->unix_handle, &st ) == -1) FILE_SetDosError();
483 else
485 FILE_FillInfo( &st, info );
486 ret = 1;
488 FILE_ReleaseFile( file );
489 return ret;
493 /**************************************************************************
494 * GetFileAttributes16 (KERNEL.420)
496 DWORD WINAPI GetFileAttributes16( LPCSTR name )
498 return GetFileAttributes32A( name );
502 /**************************************************************************
503 * GetFileAttributes32A (KERNEL32.217)
505 DWORD WINAPI GetFileAttributes32A( LPCSTR name )
507 DOS_FULL_NAME full_name;
508 BY_HANDLE_FILE_INFORMATION info;
510 if (name == NULL) return -1;
512 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
513 if (!FILE_Stat( full_name.long_name, &info )) return -1;
514 return info.dwFileAttributes;
518 /**************************************************************************
519 * GetFileAttributes32W (KERNEL32.218)
521 DWORD WINAPI GetFileAttributes32W( LPCWSTR name )
523 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
524 DWORD res = GetFileAttributes32A( nameA );
525 HeapFree( GetProcessHeap(), 0, nameA );
526 return res;
530 /***********************************************************************
531 * GetFileSize (KERNEL32.220)
533 DWORD WINAPI GetFileSize( HFILE32 hFile, LPDWORD filesizehigh )
535 BY_HANDLE_FILE_INFORMATION info;
536 if (!GetFileInformationByHandle( hFile, &info )) return 0;
537 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
538 return info.nFileSizeLow;
542 /***********************************************************************
543 * GetFileTime (KERNEL32.221)
545 BOOL32 WINAPI GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
546 FILETIME *lpLastAccessTime,
547 FILETIME *lpLastWriteTime )
549 BY_HANDLE_FILE_INFORMATION info;
550 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
551 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
552 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
553 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
554 return TRUE;
557 /***********************************************************************
558 * CompareFileTime (KERNEL32.28)
560 INT32 WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
562 if (!x || !y) return -1;
564 if (x->dwHighDateTime > y->dwHighDateTime)
565 return 1;
566 if (x->dwHighDateTime < y->dwHighDateTime)
567 return -1;
568 if (x->dwLowDateTime > y->dwLowDateTime)
569 return 1;
570 if (x->dwLowDateTime < y->dwLowDateTime)
571 return -1;
572 return 0;
575 /***********************************************************************
576 * FILE_Dup
578 * dup() function for DOS handles.
580 HFILE32 FILE_Dup( HFILE32 hFile )
582 FILE_OBJECT *file;
583 HFILE32 handle;
585 dprintf_info(file, "FILE_Dup for handle %d\n", hFile );
586 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR32;
587 handle = HANDLE_Alloc( &file->header, FILE_ALL_ACCESS /*FIXME*/, FALSE );
588 FILE_ReleaseFile( file );
589 dprintf_info(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 dprintf_info(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( hFile2, &file->header, 0 )) hFile2 = HFILE_ERROR32;
607 FILE_ReleaseFile( file );
608 return hFile2;
612 /***********************************************************************
613 * GetTempFileName16 (KERNEL.97)
615 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
616 LPSTR buffer )
618 char temppath[144];
620 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
621 drive |= DRIVE_GetCurrentDrive() + 'A';
623 if ((drive & TF_FORCEDRIVE) &&
624 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
626 drive &= ~TF_FORCEDRIVE;
627 fprintf( stderr, "Warning: GetTempFileName: invalid drive %d specified\n",
628 drive );
631 if (drive & TF_FORCEDRIVE)
632 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
633 else
635 GetTempPath32A( 132, temppath );
636 strcat( temppath, "\\" );
638 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
642 /***********************************************************************
643 * GetTempFileName32A (KERNEL32.290)
645 UINT32 WINAPI GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
646 LPSTR buffer)
648 DOS_FULL_NAME full_name;
649 int i;
650 LPSTR p;
651 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
653 if ( !path || !prefix || !buffer ) return 0;
655 strcpy( buffer, path );
656 p = buffer + strlen(buffer);
658 /* add a \, if there isn't one and path is more than just the drive letter ... */
659 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
660 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
662 *p++ = '~';
663 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
664 sprintf( p, "%04x.tmp", num );
666 /* Now try to create it */
668 if (!unique)
672 HFILE32 handle = FILE_Create( buffer, 0666, TRUE );
673 if (handle != INVALID_HANDLE_VALUE32)
674 { /* We created it */
675 dprintf_info(file, "GetTempFileName32A: created %s\n",
676 buffer);
677 CloseHandle( handle );
678 break;
680 if (DOS_ExtendedError != ER_FileExists)
681 break; /* No need to go on */
682 num++;
683 sprintf( p, "%04x.tmp", num );
684 } while (num != (unique & 0xffff));
687 /* Get the full path name */
689 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
691 /* Check if we have write access in the directory */
692 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
693 if (access( full_name.long_name, W_OK ) == -1)
694 fprintf( stderr,
695 "Warning: GetTempFileName returns '%s', which doesn't seem to be writeable.\n"
696 "Please check your configuration file if this generates a failure.\n",
697 buffer);
699 dprintf_info(file, "GetTempFileName32A: returning %s\n", buffer );
700 return unique ? unique : num;
704 /***********************************************************************
705 * GetTempFileName32W (KERNEL32.291)
707 UINT32 WINAPI GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
708 LPWSTR buffer )
710 LPSTR patha,prefixa;
711 char buffera[144];
712 UINT32 ret;
714 if (!path) return 0;
715 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
716 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
717 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
718 lstrcpyAtoW( buffer, buffera );
719 HeapFree( GetProcessHeap(), 0, patha );
720 HeapFree( GetProcessHeap(), 0, prefixa );
721 return ret;
725 /***********************************************************************
726 * FILE_DoOpenFile
728 * Implementation of OpenFile16() and OpenFile32().
730 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
731 BOOL32 win32 )
733 HFILE32 hFileRet;
734 FILETIME filetime;
735 WORD filedatetime[2];
736 DOS_FULL_NAME full_name;
737 char *p;
738 int unixMode;
740 if (!ofs) return HFILE_ERROR32;
743 ofs->cBytes = sizeof(OFSTRUCT);
744 ofs->nErrCode = 0;
745 if (mode & OF_REOPEN) name = ofs->szPathName;
747 if (!name) {
748 fprintf(stderr, "ERROR: FILE_DoOpenFile() called with `name' set to NULL ! Please debug.\n");
750 return HFILE_ERROR32;
753 dprintf_info(file, "OpenFile: %s %04x\n", name, mode );
755 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
756 Are there any cases where getting the path here is wrong?
757 Uwe Bonnes 1997 Apr 2 */
758 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
759 ofs->szPathName, NULL )) goto error;
761 /* OF_PARSE simply fills the structure */
763 if (mode & OF_PARSE)
765 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
766 != DRIVE_REMOVABLE);
767 dprintf_info(file, "OpenFile(%s): OF_PARSE, res = '%s'\n",
768 name, ofs->szPathName );
769 return 0;
772 /* OF_CREATE is completely different from all other options, so
773 handle it first */
775 if (mode & OF_CREATE)
777 if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32)
778 goto error;
779 goto success;
782 /* If OF_SEARCH is set, ignore the given path */
784 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
786 /* First try the file name as is */
787 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
788 /* Now remove the path */
789 if (name[0] && (name[1] == ':')) name += 2;
790 if ((p = strrchr( name, '\\' ))) name = p + 1;
791 if ((p = strrchr( name, '/' ))) name = p + 1;
792 if (!name[0]) goto not_found;
795 /* Now look for the file */
797 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
799 found:
800 dprintf_info(file, "OpenFile: found %s = %s\n",
801 full_name.long_name, full_name.short_name );
802 lstrcpyn32A( ofs->szPathName, full_name.short_name,
803 sizeof(ofs->szPathName) );
805 if (mode & OF_DELETE)
807 if (unlink( full_name.long_name ) == -1) goto not_found;
808 dprintf_info(file, "OpenFile(%s): OF_DELETE return = OK\n", name);
809 return 1;
812 switch(mode & 3)
814 case OF_WRITE:
815 unixMode = O_WRONLY; break;
816 case OF_READWRITE:
817 unixMode = O_RDWR; break;
818 case OF_READ:
819 default:
820 unixMode = O_RDONLY; break;
823 hFileRet = FILE_OpenUnixFile( full_name.long_name, unixMode );
824 if (hFileRet == HFILE_ERROR32) goto not_found;
825 GetFileTime( hFileRet, NULL, NULL, &filetime );
826 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
827 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
829 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
831 CloseHandle( hFileRet );
832 dprintf_warn(file, "OpenFile(%s): OF_VERIFY failed\n", name );
833 /* FIXME: what error here? */
834 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
835 goto error;
838 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
840 success: /* We get here if the open was successful */
841 dprintf_info(file, "OpenFile(%s): OK, return = %d\n", name, hFileRet );
842 if (mode & OF_EXIST) /* Return the handle, but close it first */
843 CloseHandle( hFileRet );
844 return hFileRet;
846 not_found: /* We get here if the file does not exist */
847 dprintf_warn(file, "OpenFile: '%s' not found\n", name );
848 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
849 /* fall through */
851 error: /* We get here if there was an error opening the file */
852 ofs->nErrCode = DOS_ExtendedError;
853 dprintf_warn(file, "OpenFile(%s): return = HFILE_ERROR error= %d\n",
854 name,ofs->nErrCode );
855 return HFILE_ERROR32;
859 /***********************************************************************
860 * OpenFile16 (KERNEL.74)
862 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
864 return FILE_DoOpenFile( name, ofs, mode, FALSE );
868 /***********************************************************************
869 * OpenFile32 (KERNEL32.396)
871 HFILE32 WINAPI OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
873 return FILE_DoOpenFile( name, ofs, mode, TRUE );
877 /***********************************************************************
878 * _lclose16 (KERNEL.81)
880 HFILE16 WINAPI _lclose16( HFILE16 hFile )
882 dprintf_info(file, "_lclose16: handle %d\n", hFile );
883 return CloseHandle( hFile ) ? 0 : HFILE_ERROR16;
887 /***********************************************************************
888 * _lclose32 (KERNEL32.592)
890 HFILE32 WINAPI _lclose32( HFILE32 hFile )
892 dprintf_info(file, "_lclose32: handle %d\n", hFile );
893 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
897 /***********************************************************************
898 * WIN16_hread
900 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
902 LONG maxlen;
904 dprintf_info(file, "WIN16_hread: %d %08lx %ld\n",
905 hFile, (DWORD)buffer, count );
907 /* Some programs pass a count larger than the allocated buffer */
908 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
909 if (count > maxlen) count = maxlen;
910 return _lread32( hFile, PTR_SEG_TO_LIN(buffer), count );
914 /***********************************************************************
915 * WIN16_lread
917 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
919 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
923 /***********************************************************************
924 * _lread32 (KERNEL32.596)
926 UINT32 WINAPI _lread32( HFILE32 handle, LPVOID buffer, UINT32 count )
928 K32OBJ *ptr;
929 DWORD numWritten;
930 BOOL32 result = FALSE;
932 dprintf_info( file, "_lread32: %d %p %d\n", handle, buffer, count);
933 if (!(ptr = HANDLE_GetObjPtr( handle, K32OBJ_UNKNOWN, 0))) return -1;
934 if (K32OBJ_OPS(ptr)->read)
935 result = K32OBJ_OPS(ptr)->read(ptr, buffer, count, &numWritten, NULL);
936 K32OBJ_DecCount( ptr );
937 if (!result) return -1;
938 return (UINT32)numWritten;
942 /***********************************************************************
943 * _lread16 (KERNEL.82)
945 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
947 return (UINT16)_lread32( hFile, buffer, (LONG)count );
951 /***********************************************************************
952 * _lcreat16 (KERNEL.83)
954 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
956 int mode = (attr & 1) ? 0444 : 0666;
957 dprintf_info(file, "_lcreat16: %s %02x\n", path, attr );
958 return (HFILE16)FILE_Create( path, mode, FALSE );
962 /***********************************************************************
963 * _lcreat32 (KERNEL32.593)
965 HFILE32 WINAPI _lcreat32( LPCSTR path, INT32 attr )
967 int mode = (attr & 1) ? 0444 : 0666;
968 dprintf_info(file, "_lcreat32: %s %02x\n", path, attr );
969 return FILE_Create( path, mode, FALSE );
973 /***********************************************************************
974 * _lcreat_uniq (Not a Windows API)
976 HFILE32 _lcreat_uniq( LPCSTR path, INT32 attr )
978 int mode = (attr & 1) ? 0444 : 0666;
979 dprintf_info(file, "_lcreat_uniq: %s %02x\n", path, attr );
980 return FILE_Create( path, mode, TRUE );
984 /***********************************************************************
985 * SetFilePointer (KERNEL32.492)
987 DWORD WINAPI SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
988 DWORD method )
990 FILE_OBJECT *file;
991 int origin, result;
993 if (highword && *highword)
995 fprintf( stderr, "SetFilePointer: 64-bit offsets not supported yet\n");
996 SetLastError( ERROR_INVALID_PARAMETER );
997 return 0xffffffff;
999 dprintf_info(file, "SetFilePointer: handle %d offset %ld origin %ld\n",
1000 hFile, distance, method );
1002 if (!(file = FILE_GetFile( hFile ))) return 0xffffffff;
1003 switch(method)
1005 case FILE_CURRENT: origin = SEEK_CUR; break;
1006 case FILE_END: origin = SEEK_END; break;
1007 default: origin = SEEK_SET; break;
1010 if ((result = lseek( file->unix_handle, distance, origin )) == -1)
1011 FILE_SetDosError();
1012 FILE_ReleaseFile( file );
1013 return (DWORD)result;
1017 /***********************************************************************
1018 * _llseek16 (KERNEL.84)
1020 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1022 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1026 /***********************************************************************
1027 * _llseek32 (KERNEL32.594)
1029 LONG WINAPI _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
1031 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1035 /***********************************************************************
1036 * _lopen16 (KERNEL.85)
1038 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1040 return _lopen32( path, mode );
1044 /***********************************************************************
1045 * _lopen32 (KERNEL32.595)
1047 HFILE32 WINAPI _lopen32( LPCSTR path, INT32 mode )
1049 INT32 unixMode;
1051 dprintf_info(file, "_lopen32('%s',%04x)\n", path, mode );
1053 switch(mode & 3)
1055 case OF_WRITE:
1056 unixMode = O_WRONLY;
1057 break;
1058 case OF_READWRITE:
1059 unixMode = O_RDWR;
1060 break;
1061 case OF_READ:
1062 default:
1063 unixMode = O_RDONLY;
1064 break;
1066 return FILE_Open( path, unixMode );
1070 /***********************************************************************
1071 * _lwrite16 (KERNEL.86)
1073 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1075 return (UINT16)_hwrite32( hFile, buffer, (LONG)count );
1078 /***********************************************************************
1079 * _lwrite32 (KERNEL.86)
1081 UINT32 WINAPI _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
1083 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
1087 /***********************************************************************
1088 * _hread16 (KERNEL.349)
1090 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1092 return _lread32( hFile, buffer, count );
1096 /***********************************************************************
1097 * _hread32 (KERNEL32.590)
1099 LONG WINAPI _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
1101 return _lread32( hFile, buffer, count );
1105 /***********************************************************************
1106 * _hwrite16 (KERNEL.350)
1108 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1110 return _hwrite32( hFile, buffer, count );
1114 /***********************************************************************
1115 * _hwrite32 (KERNEL32.591)
1117 * experimenation yields that _lwrite:
1118 * o truncates the file at the current position with
1119 * a 0 len write
1120 * o returns 0 on a 0 length write
1121 * o works with console handles
1124 LONG WINAPI _hwrite32( HFILE32 handle, LPCSTR buffer, LONG count )
1126 K32OBJ *ioptr;
1127 DWORD result;
1128 BOOL32 status = FALSE;
1130 dprintf_info(file, "_hwrite32: %d %p %ld\n", handle, buffer, count );
1132 if (count == 0) { /* Expand or truncate at current position */
1133 FILE_OBJECT *file = FILE_GetFile(handle);
1135 if ( ftruncate(file->unix_handle,
1136 lseek( file->unix_handle, 0, SEEK_CUR)) == 0 ) {
1137 FILE_ReleaseFile(file);
1138 return 0;
1139 } else {
1140 FILE_SetDosError();
1141 FILE_ReleaseFile(file);
1142 return HFILE_ERROR32;
1146 if (!(ioptr = HANDLE_GetObjPtr( handle, K32OBJ_UNKNOWN, 0 )))
1147 return HFILE_ERROR32;
1148 if (K32OBJ_OPS(ioptr)->write)
1149 status = K32OBJ_OPS(ioptr)->write(ioptr, buffer, count, &result, NULL);
1150 K32OBJ_DecCount( ioptr );
1151 if (!status) result = HFILE_ERROR32;
1152 return result;
1156 /***********************************************************************
1157 * SetHandleCount16 (KERNEL.199)
1159 UINT16 WINAPI SetHandleCount16( UINT16 count )
1161 HGLOBAL16 hPDB = GetCurrentPDB();
1162 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1163 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1165 dprintf_info(file, "SetHandleCount16(%d)\n", count );
1167 if (count < 20) count = 20; /* No point in going below 20 */
1168 else if (count > 254) count = 254;
1170 if (count == 20)
1172 if (pdb->nbFiles > 20)
1174 memcpy( pdb->fileHandles, files, 20 );
1175 GlobalFree16( pdb->hFileHandles );
1176 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1177 GlobalHandleToSel( hPDB ) );
1178 pdb->hFileHandles = 0;
1179 pdb->nbFiles = 20;
1182 else /* More than 20, need a new file handles table */
1184 BYTE *newfiles;
1185 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1186 if (!newhandle)
1188 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1189 return pdb->nbFiles;
1191 newfiles = (BYTE *)GlobalLock16( newhandle );
1193 if (count > pdb->nbFiles)
1195 memcpy( newfiles, files, pdb->nbFiles );
1196 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1198 else memcpy( newfiles, files, count );
1199 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1200 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1201 pdb->hFileHandles = newhandle;
1202 pdb->nbFiles = count;
1204 return pdb->nbFiles;
1208 /*************************************************************************
1209 * SetHandleCount32 (KERNEL32.494)
1211 UINT32 WINAPI SetHandleCount32( UINT32 count )
1213 return MIN( 256, count );
1217 /***********************************************************************
1218 * FlushFileBuffers (KERNEL32.133)
1220 BOOL32 WINAPI FlushFileBuffers( HFILE32 hFile )
1222 FILE_OBJECT *file;
1223 BOOL32 ret;
1225 dprintf_info(file, "FlushFileBuffers(%d)\n", hFile );
1226 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1227 if (fsync( file->unix_handle ) != -1) ret = TRUE;
1228 else
1230 FILE_SetDosError();
1231 ret = FALSE;
1233 FILE_ReleaseFile( file );
1234 return ret;
1238 /**************************************************************************
1239 * SetEndOfFile (KERNEL32.483)
1241 BOOL32 WINAPI SetEndOfFile( HFILE32 hFile )
1243 FILE_OBJECT *file;
1244 BOOL32 ret = TRUE;
1246 dprintf_info(file, "SetEndOfFile(%d)\n", hFile );
1247 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1248 if (ftruncate( file->unix_handle,
1249 lseek( file->unix_handle, 0, SEEK_CUR ) ))
1251 FILE_SetDosError();
1252 ret = FALSE;
1254 FILE_ReleaseFile( file );
1255 return ret;
1259 /***********************************************************************
1260 * DeleteFile16 (KERNEL.146)
1262 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1264 return DeleteFile32A( path );
1268 /***********************************************************************
1269 * DeleteFile32A (KERNEL32.71)
1271 BOOL32 WINAPI DeleteFile32A( LPCSTR path )
1273 DOS_FULL_NAME full_name;
1275 dprintf_info(file, "DeleteFile: '%s'\n", path );
1277 if (DOSFS_IsDevice( path ))
1279 fprintf(stderr, "DeleteFile: cannot remove DOS device '%s'!\n", path);
1280 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1281 return FALSE;
1284 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1285 if (unlink( full_name.long_name ) == -1)
1287 FILE_SetDosError();
1288 return FALSE;
1290 return TRUE;
1294 /***********************************************************************
1295 * DeleteFile32W (KERNEL32.72)
1297 BOOL32 WINAPI DeleteFile32W( LPCWSTR path )
1299 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1300 BOOL32 ret = DeleteFile32A( xpath );
1301 HeapFree( GetProcessHeap(), 0, xpath );
1302 return ret;
1306 /***********************************************************************
1307 * FILE_SetFileType
1309 BOOL32 FILE_SetFileType( HFILE32 hFile, DWORD type )
1311 FILE_OBJECT *file = FILE_GetFile( hFile );
1312 if (!file) return FALSE;
1313 file->type = type;
1314 FILE_ReleaseFile( file );
1315 return TRUE;
1319 /***********************************************************************
1320 * FILE_mmap
1322 LPVOID FILE_mmap( HFILE32 hFile, LPVOID start,
1323 DWORD size_high, DWORD size_low,
1324 DWORD offset_high, DWORD offset_low,
1325 int prot, int flags )
1327 LPVOID ret;
1328 FILE_OBJECT *file = FILE_GetFile( hFile );
1329 if (!file) return (LPVOID)-1;
1330 ret = FILE_dommap( file, start, size_high, size_low,
1331 offset_high, offset_low, prot, flags );
1332 FILE_ReleaseFile( file );
1333 return ret;
1337 /***********************************************************************
1338 * FILE_dommap
1340 LPVOID FILE_dommap( FILE_OBJECT *file, LPVOID start,
1341 DWORD size_high, DWORD size_low,
1342 DWORD offset_high, DWORD offset_low,
1343 int prot, int flags )
1345 int fd = -1;
1346 int pos;
1347 LPVOID ret;
1349 if (size_high || offset_high)
1350 fprintf( stderr, "FILE_mmap: offsets larger than 4Gb not supported\n");
1352 if (!file)
1354 #ifdef MAP_ANON
1355 flags |= MAP_ANON;
1356 #else
1357 static int fdzero = -1;
1359 if (fdzero == -1)
1361 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1363 perror( "/dev/zero: open" );
1364 exit(1);
1367 fd = fdzero;
1368 #endif /* MAP_ANON */
1369 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1370 #ifdef MAP_SHARED
1371 flags &= ~MAP_SHARED;
1372 #endif
1373 #ifdef MAP_PRIVATE
1374 flags |= MAP_PRIVATE;
1375 #endif
1377 else fd = file->unix_handle;
1379 if ((ret = mmap( start, size_low, prot,
1380 flags, fd, offset_low )) != (LPVOID)-1)
1381 return ret;
1383 /* mmap() failed; if this is because the file offset is not */
1384 /* page-aligned (EINVAL), or because the underlying filesystem */
1385 /* does not support mmap() (ENOEXEC), we do it by hand. */
1387 if (!file) return ret;
1388 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1389 if (prot & PROT_WRITE)
1391 /* We cannot fake shared write mappings */
1392 #ifdef MAP_SHARED
1393 if (flags & MAP_SHARED) return ret;
1394 #endif
1395 #ifdef MAP_PRIVATE
1396 if (!(flags & MAP_PRIVATE)) return ret;
1397 #endif
1399 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1400 /* Reserve the memory with an anonymous mmap */
1401 ret = FILE_dommap( NULL, start, size_high, size_low, 0, 0,
1402 PROT_READ | PROT_WRITE, flags );
1403 if (ret == (LPVOID)-1) return ret;
1404 /* Now read in the file */
1405 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1407 FILE_munmap( ret, size_high, size_low );
1408 return (LPVOID)-1;
1410 read( fd, ret, size_low );
1411 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1412 mprotect( ret, size_low, prot ); /* Set the right protection */
1413 return ret;
1417 /***********************************************************************
1418 * FILE_munmap
1420 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1422 if (size_high)
1423 fprintf( stderr, "FILE_munmap: offsets larger than 4Gb not supported\n");
1424 return munmap( start, size_low );
1428 /***********************************************************************
1429 * GetFileType (KERNEL32.222)
1431 DWORD WINAPI GetFileType( HFILE32 hFile )
1433 FILE_OBJECT *file = FILE_GetFile(hFile);
1434 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1435 FILE_ReleaseFile( file );
1436 return file->type;
1440 /**************************************************************************
1441 * MoveFileEx32A (KERNEL32.???)
1443 BOOL32 WINAPI MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1445 DOS_FULL_NAME full_name1, full_name2;
1446 int mode=0; /* mode == 1: use copy */
1448 dprintf_info(file, "MoveFileEx32A(%s,%s,%04lx)\n", fn1, fn2, flag);
1450 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1451 if (fn2) { /* !fn2 means delete fn1 */
1452 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1453 /* Source name and target path are valid */
1454 if ( full_name1.drive != full_name2.drive)
1455 /* use copy, if allowed */
1456 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1457 /* FIXME: Use right error code */
1458 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
1459 return FALSE;
1461 else mode =1;
1462 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1463 /* target exists, check if we may overwrite */
1464 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1465 /* FIXME: Use right error code */
1466 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1467 return FALSE;
1470 else /* fn2 == NULL means delete source */
1471 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1472 if (flag & MOVEFILE_COPY_ALLOWED) {
1473 fprintf( stderr,
1474 "MoveFileEx32A: Illegal flag\n");
1475 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1476 EL_Unknown );
1477 return FALSE;
1479 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1480 Perhaps we should queue these command and execute it
1481 when exiting... What about using on_exit(2)
1483 fprintf( stderr,"MoveFileEx32A: Please delete file %s\n",
1484 full_name1.long_name);
1485 fprintf( stderr," when Wine has finished\n");
1486 fprintf( stderr," like \"rm %s\"\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 fprintf( stderr,"MoveFileEx32A: Please move existing file %s\n"
1503 ,full_name1.long_name);
1504 fprintf( stderr," to file %s\n"
1505 ,full_name2.long_name);
1506 fprintf( stderr," when Wine has finished\n");
1507 fprintf( stderr," like \" mv %s %s\"\n",
1508 full_name1.long_name,full_name2.long_name);
1509 return TRUE;
1512 if (!mode) /* move the file */
1513 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1515 FILE_SetDosError();
1516 return FALSE;
1518 else return TRUE;
1519 else /* copy File */
1520 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1524 /**************************************************************************
1525 * MoveFileEx32W (KERNEL32.???)
1527 BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1529 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1530 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1531 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1532 HeapFree( GetProcessHeap(), 0, afn1 );
1533 HeapFree( GetProcessHeap(), 0, afn2 );
1534 return res;
1538 /**************************************************************************
1539 * MoveFile32A (KERNEL32.387)
1541 * Move file or directory
1543 BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1545 DOS_FULL_NAME full_name1, full_name2;
1546 struct stat fstat;
1548 dprintf_info(file, "MoveFile32A(%s,%s)\n", fn1, fn2 );
1550 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1551 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1552 /* The new name must not already exist */
1553 return FALSE;
1554 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1556 if (full_name1.drive == full_name2.drive) /* move */
1557 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1559 FILE_SetDosError();
1560 return FALSE;
1562 else return TRUE;
1563 else /*copy */ {
1564 if (stat( full_name1.long_name, &fstat ))
1566 dprintf_warn(file, "Invalid source file %s\n",
1567 full_name1.long_name);
1568 FILE_SetDosError();
1569 return FALSE;
1571 if (S_ISDIR(fstat.st_mode)) {
1572 /* No Move for directories across file systems */
1573 /* FIXME: Use right error code */
1574 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1575 EL_Unknown );
1576 return FALSE;
1578 else
1579 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1584 /**************************************************************************
1585 * MoveFile32W (KERNEL32.390)
1587 BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1589 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1590 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1591 BOOL32 res = MoveFile32A( afn1, afn2 );
1592 HeapFree( GetProcessHeap(), 0, afn1 );
1593 HeapFree( GetProcessHeap(), 0, afn2 );
1594 return res;
1598 /**************************************************************************
1599 * CopyFile32A (KERNEL32.36)
1601 BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1603 HFILE32 h1, h2;
1604 BY_HANDLE_FILE_INFORMATION info;
1605 UINT32 count;
1606 BOOL32 ret = FALSE;
1607 int mode;
1608 char buffer[2048];
1610 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1611 if (!GetFileInformationByHandle( h1, &info ))
1613 CloseHandle( h1 );
1614 return FALSE;
1616 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1617 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1619 CloseHandle( h1 );
1620 return FALSE;
1622 while ((count = _lread32( h2, buffer, sizeof(buffer) )) > 0)
1624 char *p = buffer;
1625 while (count > 0)
1627 INT32 res = _lwrite32( h2, p, count );
1628 if (res <= 0) goto done;
1629 p += res;
1630 count -= res;
1633 ret = TRUE;
1634 done:
1635 CloseHandle( h1 );
1636 CloseHandle( h2 );
1637 return ret;
1641 /**************************************************************************
1642 * CopyFile32W (KERNEL32.37)
1644 BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
1646 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1647 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1648 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1649 HeapFree( GetProcessHeap(), 0, sourceA );
1650 HeapFree( GetProcessHeap(), 0, destA );
1651 return ret;
1655 /***********************************************************************
1656 * SetFileTime (KERNEL32.493)
1658 BOOL32 WINAPI SetFileTime( HFILE32 hFile,
1659 const FILETIME *lpCreationTime,
1660 const FILETIME *lpLastAccessTime,
1661 const FILETIME *lpLastWriteTime )
1663 FILE_OBJECT *file = FILE_GetFile(hFile);
1664 struct utimbuf utimbuf;
1666 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1667 dprintf_info(file,"SetFileTime(%s,%p,%p,%p)\n",
1668 file->unix_name,
1669 lpCreationTime,
1670 lpLastAccessTime,
1671 lpLastWriteTime
1673 if (lpLastAccessTime)
1674 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1675 else
1676 utimbuf.actime = 0; /* FIXME */
1677 if (lpLastWriteTime)
1678 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1679 else
1680 utimbuf.modtime = 0; /* FIXME */
1681 if (-1==utime(file->unix_name,&utimbuf))
1683 FILE_ReleaseFile( file );
1684 FILE_SetDosError();
1685 return FALSE;
1687 FILE_ReleaseFile( file );
1688 return TRUE;
1691 /* Locks need to be mirrored because unix file locking is based
1692 * on the pid. Inside of wine there can be multiple WINE processes
1693 * that share the same unix pid.
1694 * Read's and writes should check these locks also - not sure
1695 * how critical that is at this point (FIXME).
1698 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
1700 DOS_FILE_LOCK *curr;
1701 DWORD processId;
1703 processId = GetCurrentProcessId();
1705 /* check if lock overlaps a current lock for the same file */
1706 for (curr = locks; curr; curr = curr->next) {
1707 if (strcmp(curr->unix_name, file->unix_name) == 0) {
1708 if ((f->l_start == curr->base) && (f->l_len == curr->len))
1709 return TRUE;/* region is identic */
1710 if ((f->l_start < (curr->base + curr->len)) &&
1711 ((f->l_start + f->l_len) > curr->base)) {
1712 /* region overlaps */
1713 return FALSE;
1718 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
1719 curr->processId = GetCurrentProcessId();
1720 curr->base = f->l_start;
1721 curr->len = f->l_len;
1722 curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);
1723 curr->next = locks;
1724 curr->dos_file = file;
1725 locks = curr;
1726 return TRUE;
1729 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
1731 DWORD processId;
1732 DOS_FILE_LOCK **curr;
1733 DOS_FILE_LOCK *rem;
1735 processId = GetCurrentProcessId();
1736 curr = &locks;
1737 while (*curr) {
1738 if ((*curr)->dos_file == file) {
1739 rem = *curr;
1740 *curr = (*curr)->next;
1741 HeapFree( SystemHeap, 0, rem->unix_name );
1742 HeapFree( SystemHeap, 0, rem );
1744 else
1745 curr = &(*curr)->next;
1749 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
1751 DWORD processId;
1752 DOS_FILE_LOCK **curr;
1753 DOS_FILE_LOCK *rem;
1755 processId = GetCurrentProcessId();
1756 for (curr = &locks; *curr; curr = &(*curr)->next) {
1757 if ((*curr)->processId == processId &&
1758 (*curr)->dos_file == file &&
1759 (*curr)->base == f->l_start &&
1760 (*curr)->len == f->l_len) {
1761 /* this is the same lock */
1762 rem = *curr;
1763 *curr = (*curr)->next;
1764 HeapFree( SystemHeap, 0, rem->unix_name );
1765 HeapFree( SystemHeap, 0, rem );
1766 return TRUE;
1769 /* no matching lock found */
1770 return FALSE;
1774 /**************************************************************************
1775 * LockFile (KERNEL32.511)
1777 BOOL32 WINAPI LockFile(
1778 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1779 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
1781 struct flock f;
1782 FILE_OBJECT *file;
1784 dprintf_info(file, "LockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1785 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1786 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
1788 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
1789 dprintf_fixme(file, "LockFile32: Unimplemented bytes > 32bits\n");
1790 return FALSE;
1793 f.l_start = dwFileOffsetLow;
1794 f.l_len = nNumberOfBytesToLockLow;
1795 f.l_whence = SEEK_SET;
1796 f.l_pid = 0;
1797 f.l_type = F_WRLCK;
1799 if (!(file = FILE_GetFile(hFile))) return FALSE;
1801 /* shadow locks internally */
1802 if (!DOS_AddLock(file, &f)) {
1803 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1804 return FALSE;
1807 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1808 #ifdef USE_UNIX_LOCKS
1809 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1810 if (errno == EACCES || errno == EAGAIN) {
1811 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1813 else {
1814 FILE_SetDosError();
1816 /* remove our internal copy of the lock */
1817 DOS_RemoveLock(file, &f);
1818 return FALSE;
1820 #endif
1821 return TRUE;
1825 /**************************************************************************
1826 * UnlockFile (KERNEL32.703)
1828 BOOL32 WINAPI UnlockFile(
1829 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1830 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
1832 FILE_OBJECT *file;
1833 struct flock f;
1835 dprintf_info(file, "UnlockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1836 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1837 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
1839 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
1840 dprintf_warn(file, "UnlockFile32: Unimplemented bytes > 32bits\n");
1841 return FALSE;
1844 f.l_start = dwFileOffsetLow;
1845 f.l_len = nNumberOfBytesToUnlockLow;
1846 f.l_whence = SEEK_SET;
1847 f.l_pid = 0;
1848 f.l_type = F_UNLCK;
1850 if (!(file = FILE_GetFile(hFile))) return FALSE;
1852 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
1854 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1855 #ifdef USE_UNIX_LOCKS
1856 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1857 FILE_SetDosError();
1858 return FALSE;
1860 #endif
1861 return TRUE;