Release 980413
[wine/multimedia.git] / files / file.c
blob721f11ad57c25d839a5f2dcad827b0595c502fc0
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( PROCESS_Current(), &(*file)->header,
93 FILE_ALL_ACCESS | GENERIC_READ |
94 GENERIC_WRITE | GENERIC_EXECUTE /*FIXME*/, TRUE );
95 /* If the allocation failed, the object is already destroyed */
96 if (handle == INVALID_HANDLE_VALUE32) *file = NULL;
97 return handle;
101 /* FIXME: lpOverlapped is ignored */
102 static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
103 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
105 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
106 int result;
108 TRACE(file, "%p %p %ld\n", ptr, lpBuffer,
109 nNumberOfChars);
111 if (nNumberOfChars == 0) {
112 *lpNumberOfChars = 0; /* FIXME: does this change */
113 return TRUE;
116 if ((result = read(file->unix_handle, lpBuffer, nNumberOfChars)) == -1)
118 FILE_SetDosError();
119 return FALSE;
121 *lpNumberOfChars = result;
122 return TRUE;
126 * experimentation yields that WriteFile:
127 * o does not truncate on write of 0
128 * o always changes the *lpNumberOfChars to actual number of
129 * characters written
130 * o write of 0 nNumberOfChars returns TRUE
132 static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars,
133 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
135 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
136 int result;
138 TRACE(file, "%p %p %ld\n", ptr, lpBuffer,
139 nNumberOfChars);
141 *lpNumberOfChars = 0;
144 * I assume this loop around EAGAIN is here because
145 * win32 doesn't have interrupted 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( PROCESS_Current(), handle,
192 K32OBJ_FILE, 0 /*FIXME*/ );
196 /***********************************************************************
197 * FILE_ReleaseFile
199 * Release a DOS file obtained with FILE_GetFile.
201 static void FILE_ReleaseFile( FILE_OBJECT *file )
203 K32OBJ_DecCount( &file->header );
207 /***********************************************************************
208 * FILE_GetUnixHandle
210 * Return the Unix handle associated to a file handle.
212 int FILE_GetUnixHandle( HFILE32 hFile )
214 FILE_OBJECT *file;
215 int ret;
217 if (!(file = FILE_GetFile( hFile ))) return -1;
218 ret = file->unix_handle;
219 FILE_ReleaseFile( file );
220 return ret;
224 /***********************************************************************
225 * FILE_SetDosError
227 * Set the DOS error code from errno.
229 void FILE_SetDosError(void)
231 int save_errno = errno; /* errno gets overwritten by printf */
233 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
234 switch (save_errno)
236 case EAGAIN:
237 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
238 break;
239 case EBADF:
240 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
241 break;
242 case ENOSPC:
243 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
244 break;
245 case EACCES:
246 case EPERM:
247 case EROFS:
248 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
249 break;
250 case EBUSY:
251 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
252 break;
253 case ENOENT:
254 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
255 break;
256 case EISDIR:
257 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
258 break;
259 case ENFILE:
260 case EMFILE:
261 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
262 break;
263 case EEXIST:
264 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
265 break;
266 case EINVAL:
267 case ESPIPE:
268 DOS_ERROR( ER_SeekError, EC_NotFound, SA_Ignore, EL_Disk );
269 break;
270 case ENOTEMPTY:
271 DOS_ERROR( ERROR_DIR_NOT_EMPTY, EC_Exists, SA_Ignore, EL_Disk );
272 break;
273 default:
274 perror( "int21: unknown errno" );
275 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
276 break;
278 errno = save_errno;
282 /***********************************************************************
283 * FILE_DupUnixHandle
285 * Duplicate a Unix handle into a task handle.
287 HFILE32 FILE_DupUnixHandle( int fd )
289 HFILE32 handle;
290 FILE_OBJECT *file;
292 if ((handle = FILE_Alloc( &file )) != INVALID_HANDLE_VALUE32)
294 if ((file->unix_handle = dup(fd)) == -1)
296 FILE_SetDosError();
297 CloseHandle( handle );
298 return INVALID_HANDLE_VALUE32;
301 return handle;
305 /***********************************************************************
306 * FILE_OpenUnixFile
308 HFILE32 FILE_OpenUnixFile( const char *name, int mode )
310 HFILE32 handle;
311 FILE_OBJECT *file;
312 struct stat st;
314 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
315 return INVALID_HANDLE_VALUE32;
317 if ((file->unix_handle = open( name, mode, 0666 )) == -1)
319 if (!Options.failReadOnly && (mode == O_RDWR))
320 file->unix_handle = open( name, O_RDONLY );
322 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
324 FILE_SetDosError();
325 CloseHandle( handle );
326 return INVALID_HANDLE_VALUE32;
328 if (S_ISDIR(st.st_mode))
330 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
331 CloseHandle( handle );
332 return INVALID_HANDLE_VALUE32;
335 /* File opened OK, now fill the FILE_OBJECT */
337 file->unix_name = HEAP_strdupA( SystemHeap, 0, name );
338 return handle;
342 /***********************************************************************
343 * FILE_Open
345 HFILE32 FILE_Open( LPCSTR path, INT32 mode )
347 DOS_FULL_NAME full_name;
348 const char *unixName;
350 TRACE(file, "'%s' %04x\n", path, mode );
352 if (!path) return HFILE_ERROR32;
354 if (DOSFS_IsDevice( path ))
356 HFILE32 ret;
358 TRACE(file, "opening device '%s'\n", path );
360 if (HFILE_ERROR32!=(ret=DOSFS_OpenDevice( path, mode )))
361 return ret;
363 /* Do not silence this please. It is a critical error. -MM */
364 ERR(file, "Couldn't open device '%s'!\n",path);
365 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
366 return HFILE_ERROR32;
369 else /* check for filename, don't check for last entry if creating */
371 if (!DOSFS_GetFullName( path, !(mode & O_CREAT), &full_name ))
372 return HFILE_ERROR32;
373 unixName = full_name.long_name;
375 return FILE_OpenUnixFile( unixName, mode );
379 /***********************************************************************
380 * FILE_Create
382 static HFILE32 FILE_Create( LPCSTR path, int mode, int unique )
384 HFILE32 handle;
385 FILE_OBJECT *file;
386 DOS_FULL_NAME full_name;
388 TRACE(file, "'%s' %04x %d\n", path, mode, unique );
390 if (!path) return INVALID_HANDLE_VALUE32;
392 if (DOSFS_IsDevice( path ))
394 WARN(file, "cannot create DOS device '%s'!\n", path);
395 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
396 return INVALID_HANDLE_VALUE32;
399 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
400 return INVALID_HANDLE_VALUE32;
402 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
404 CloseHandle( handle );
405 return INVALID_HANDLE_VALUE32;
407 if ((file->unix_handle = open( full_name.long_name,
408 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
409 mode )) == -1)
411 FILE_SetDosError();
412 CloseHandle( handle );
413 return INVALID_HANDLE_VALUE32;
416 /* File created OK, now fill the FILE_OBJECT */
418 file->unix_name = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
419 return handle;
423 /***********************************************************************
424 * FILE_FillInfo
426 * Fill a file information from a struct stat.
428 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
430 if (S_ISDIR(st->st_mode))
431 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
432 else
433 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
434 if (!(st->st_mode & S_IWUSR))
435 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
437 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
438 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
439 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
441 info->dwVolumeSerialNumber = 0; /* FIXME */
442 info->nFileSizeHigh = 0;
443 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
444 info->nNumberOfLinks = st->st_nlink;
445 info->nFileIndexHigh = 0;
446 info->nFileIndexLow = st->st_ino;
450 /***********************************************************************
451 * FILE_Stat
453 * Stat a Unix path name. Return TRUE if OK.
455 BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
457 struct stat st;
459 if (!unixName || !info) return FALSE;
461 if (stat( unixName, &st ) == -1)
463 FILE_SetDosError();
464 return FALSE;
466 FILE_FillInfo( &st, info );
467 return TRUE;
471 /***********************************************************************
472 * GetFileInformationByHandle (KERNEL32.219)
474 DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile,
475 BY_HANDLE_FILE_INFORMATION *info )
477 FILE_OBJECT *file;
478 DWORD ret = 0;
479 struct stat st;
481 if (!info) return 0;
483 if (!(file = FILE_GetFile( hFile ))) return 0;
484 if (fstat( file->unix_handle, &st ) == -1) FILE_SetDosError();
485 else
487 FILE_FillInfo( &st, info );
488 ret = 1;
490 FILE_ReleaseFile( file );
491 return ret;
495 /**************************************************************************
496 * GetFileAttributes16 (KERNEL.420)
498 DWORD WINAPI GetFileAttributes16( LPCSTR name )
500 return GetFileAttributes32A( name );
504 /**************************************************************************
505 * GetFileAttributes32A (KERNEL32.217)
507 DWORD WINAPI GetFileAttributes32A( LPCSTR name )
509 DOS_FULL_NAME full_name;
510 BY_HANDLE_FILE_INFORMATION info;
512 if (name == NULL) return -1;
514 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
515 if (!FILE_Stat( full_name.long_name, &info )) return -1;
516 return info.dwFileAttributes;
520 /**************************************************************************
521 * GetFileAttributes32W (KERNEL32.218)
523 DWORD WINAPI GetFileAttributes32W( LPCWSTR name )
525 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
526 DWORD res = GetFileAttributes32A( nameA );
527 HeapFree( GetProcessHeap(), 0, nameA );
528 return res;
532 /***********************************************************************
533 * GetFileSize (KERNEL32.220)
535 DWORD WINAPI GetFileSize( HFILE32 hFile, LPDWORD filesizehigh )
537 BY_HANDLE_FILE_INFORMATION info;
538 if (!GetFileInformationByHandle( hFile, &info )) return 0;
539 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
540 return info.nFileSizeLow;
544 /***********************************************************************
545 * GetFileTime (KERNEL32.221)
547 BOOL32 WINAPI GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
548 FILETIME *lpLastAccessTime,
549 FILETIME *lpLastWriteTime )
551 BY_HANDLE_FILE_INFORMATION info;
552 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
553 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
554 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
555 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
556 return TRUE;
559 /***********************************************************************
560 * CompareFileTime (KERNEL32.28)
562 INT32 WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
564 if (!x || !y) return -1;
566 if (x->dwHighDateTime > y->dwHighDateTime)
567 return 1;
568 if (x->dwHighDateTime < y->dwHighDateTime)
569 return -1;
570 if (x->dwLowDateTime > y->dwLowDateTime)
571 return 1;
572 if (x->dwLowDateTime < y->dwLowDateTime)
573 return -1;
574 return 0;
577 /***********************************************************************
578 * FILE_Dup
580 * dup() function for DOS handles.
582 HFILE32 FILE_Dup( HFILE32 hFile )
584 HFILE32 handle;
586 TRACE(file, "FILE_Dup for handle %d\n", hFile );
587 if (!DuplicateHandle( GetCurrentProcess(), hFile, GetCurrentProcess(),
588 &handle, FILE_ALL_ACCESS /* FIXME */, FALSE, 0 ))
589 handle = HFILE_ERROR32;
590 TRACE(file, "FILE_Dup return handle %d\n", handle );
591 return handle;
595 /***********************************************************************
596 * FILE_Dup2
598 * dup2() function for DOS handles.
600 HFILE32 FILE_Dup2( HFILE32 hFile1, HFILE32 hFile2 )
602 FILE_OBJECT *file;
604 TRACE(file, "FILE_Dup2 for handle %d\n", hFile1 );
605 /* FIXME: should use DuplicateHandle */
606 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR32;
607 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2, &file->header, 0 ))
608 hFile2 = HFILE_ERROR32;
609 FILE_ReleaseFile( file );
610 return hFile2;
614 /***********************************************************************
615 * GetTempFileName16 (KERNEL.97)
617 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
618 LPSTR buffer )
620 char temppath[144];
622 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
623 drive |= DRIVE_GetCurrentDrive() + 'A';
625 if ((drive & TF_FORCEDRIVE) &&
626 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
628 drive &= ~TF_FORCEDRIVE;
629 WARN(file, "invalid drive %d specified\n", drive );
632 if (drive & TF_FORCEDRIVE)
633 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
634 else
636 GetTempPath32A( 132, temppath );
637 strcat( temppath, "\\" );
639 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
643 /***********************************************************************
644 * GetTempFileName32A (KERNEL32.290)
646 UINT32 WINAPI GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
647 LPSTR buffer)
649 DOS_FULL_NAME full_name;
650 int i;
651 LPSTR p;
652 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
654 if ( !path || !prefix || !buffer ) return 0;
656 strcpy( buffer, path );
657 p = buffer + strlen(buffer);
659 /* add a \, if there isn't one and path is more than just the drive letter ... */
660 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
661 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
663 *p++ = '~';
664 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
665 sprintf( p, "%04x.tmp", num );
667 /* Now try to create it */
669 if (!unique)
673 HFILE32 handle = FILE_Create( buffer, 0666, TRUE );
674 if (handle != INVALID_HANDLE_VALUE32)
675 { /* We created it */
676 TRACE(file, "created %s\n",
677 buffer);
678 CloseHandle( handle );
679 break;
681 if (DOS_ExtendedError != ER_FileExists)
682 break; /* No need to go on */
683 num++;
684 sprintf( p, "%04x.tmp", num );
685 } while (num != (unique & 0xffff));
688 /* Get the full path name */
690 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
692 /* Check if we have write access in the directory */
693 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
694 if (access( full_name.long_name, W_OK ) == -1)
695 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
696 buffer);
698 TRACE(file, "returning %s\n", buffer );
699 return unique ? unique : num;
703 /***********************************************************************
704 * GetTempFileName32W (KERNEL32.291)
706 UINT32 WINAPI GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
707 LPWSTR buffer )
709 LPSTR patha,prefixa;
710 char buffera[144];
711 UINT32 ret;
713 if (!path) return 0;
714 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
715 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
716 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
717 lstrcpyAtoW( buffer, buffera );
718 HeapFree( GetProcessHeap(), 0, patha );
719 HeapFree( GetProcessHeap(), 0, prefixa );
720 return ret;
724 /***********************************************************************
725 * FILE_DoOpenFile
727 * Implementation of OpenFile16() and OpenFile32().
729 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
730 BOOL32 win32 )
732 HFILE32 hFileRet;
733 FILETIME filetime;
734 WORD filedatetime[2];
735 DOS_FULL_NAME full_name;
736 char *p;
737 int unixMode;
739 if (!ofs) return HFILE_ERROR32;
742 ofs->cBytes = sizeof(OFSTRUCT);
743 ofs->nErrCode = 0;
744 if (mode & OF_REOPEN) name = ofs->szPathName;
746 if (!name) {
747 ERR(file, "called with `name' set to NULL ! Please debug.\n");
748 return HFILE_ERROR32;
751 TRACE(file, "%s %04x\n", name, mode );
753 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
754 Are there any cases where getting the path here is wrong?
755 Uwe Bonnes 1997 Apr 2 */
756 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
757 ofs->szPathName, NULL )) goto error;
759 /* OF_PARSE simply fills the structure */
761 if (mode & OF_PARSE)
763 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
764 != DRIVE_REMOVABLE);
765 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
766 name, ofs->szPathName );
767 return 0;
770 /* OF_CREATE is completely different from all other options, so
771 handle it first */
773 if (mode & OF_CREATE)
775 if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32)
776 goto error;
777 goto success;
780 /* If OF_SEARCH is set, ignore the given path */
782 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
784 /* First try the file name as is */
785 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
786 /* Now remove the path */
787 if (name[0] && (name[1] == ':')) name += 2;
788 if ((p = strrchr( name, '\\' ))) name = p + 1;
789 if ((p = strrchr( name, '/' ))) name = p + 1;
790 if (!name[0]) goto not_found;
793 /* Now look for the file */
795 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
797 found:
798 TRACE(file, "found %s = %s\n",
799 full_name.long_name, full_name.short_name );
800 lstrcpyn32A( ofs->szPathName, full_name.short_name,
801 sizeof(ofs->szPathName) );
803 if (mode & OF_DELETE)
805 if (unlink( full_name.long_name ) == -1) goto not_found;
806 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
807 return 1;
810 switch(mode & 3)
812 case OF_WRITE:
813 unixMode = O_WRONLY; break;
814 case OF_READWRITE:
815 unixMode = O_RDWR; break;
816 case OF_READ:
817 default:
818 unixMode = O_RDONLY; break;
821 hFileRet = FILE_OpenUnixFile( full_name.long_name, unixMode );
822 if (hFileRet == HFILE_ERROR32) goto not_found;
823 GetFileTime( hFileRet, NULL, NULL, &filetime );
824 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
825 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
827 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
829 CloseHandle( hFileRet );
830 WARN(file, "(%s): OF_VERIFY failed\n", name );
831 /* FIXME: what error here? */
832 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
833 goto error;
836 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
838 success: /* We get here if the open was successful */
839 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
840 if (mode & OF_EXIST) /* Return the handle, but close it first */
841 CloseHandle( hFileRet );
842 return hFileRet;
844 not_found: /* We get here if the file does not exist */
845 WARN(file, "'%s' not found\n", name );
846 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
847 /* fall through */
849 error: /* We get here if there was an error opening the file */
850 ofs->nErrCode = DOS_ExtendedError;
851 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
852 name,ofs->nErrCode );
853 return HFILE_ERROR32;
857 /***********************************************************************
858 * OpenFile16 (KERNEL.74)
860 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
862 return FILE_DoOpenFile( name, ofs, mode, FALSE );
866 /***********************************************************************
867 * OpenFile32 (KERNEL32.396)
869 HFILE32 WINAPI OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
871 return FILE_DoOpenFile( name, ofs, mode, TRUE );
875 /***********************************************************************
876 * _lclose16 (KERNEL.81)
878 HFILE16 WINAPI _lclose16( HFILE16 hFile )
880 TRACE(file, "handle %d\n", hFile );
881 return CloseHandle( hFile ) ? 0 : HFILE_ERROR16;
885 /***********************************************************************
886 * _lclose32 (KERNEL32.592)
888 HFILE32 WINAPI _lclose32( HFILE32 hFile )
890 TRACE(file, "handle %d\n", hFile );
891 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
895 /***********************************************************************
896 * WIN16_hread
898 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
900 LONG maxlen;
902 TRACE(file, "%d %08lx %ld\n",
903 hFile, (DWORD)buffer, count );
905 /* Some programs pass a count larger than the allocated buffer */
906 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
907 if (count > maxlen) count = maxlen;
908 return _lread32( hFile, PTR_SEG_TO_LIN(buffer), count );
912 /***********************************************************************
913 * WIN16_lread
915 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
917 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
921 /***********************************************************************
922 * _lread32 (KERNEL32.596)
924 UINT32 WINAPI _lread32( HFILE32 handle, LPVOID buffer, UINT32 count )
926 K32OBJ *ptr;
927 DWORD numWritten;
928 BOOL32 result = FALSE;
930 TRACE( file, "%d %p %d\n", handle, buffer, count);
931 if (!(ptr = HANDLE_GetObjPtr( PROCESS_Current(), handle,
932 K32OBJ_UNKNOWN, 0))) return -1;
933 if (K32OBJ_OPS(ptr)->read)
934 result = K32OBJ_OPS(ptr)->read(ptr, buffer, count, &numWritten, NULL);
935 K32OBJ_DecCount( ptr );
936 if (!result) return -1;
937 return (UINT32)numWritten;
941 /***********************************************************************
942 * _lread16 (KERNEL.82)
944 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
946 return (UINT16)_lread32( hFile, buffer, (LONG)count );
950 /***********************************************************************
951 * _lcreat16 (KERNEL.83)
953 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
955 int mode = (attr & 1) ? 0444 : 0666;
956 TRACE(file, "%s %02x\n", path, attr );
957 return (HFILE16)FILE_Create( path, mode, FALSE );
961 /***********************************************************************
962 * _lcreat32 (KERNEL32.593)
964 HFILE32 WINAPI _lcreat32( LPCSTR path, INT32 attr )
966 int mode = (attr & 1) ? 0444 : 0666;
967 TRACE(file, "%s %02x\n", path, attr );
968 return FILE_Create( path, mode, FALSE );
972 /***********************************************************************
973 * _lcreat_uniq (Not a Windows API)
975 HFILE32 _lcreat_uniq( LPCSTR path, INT32 attr )
977 int mode = (attr & 1) ? 0444 : 0666;
978 TRACE(file, "%s %02x\n", path, attr );
979 return FILE_Create( path, mode, TRUE );
983 /***********************************************************************
984 * SetFilePointer (KERNEL32.492)
986 DWORD WINAPI SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
987 DWORD method )
989 FILE_OBJECT *file;
990 int origin, result;
992 if (highword && *highword)
994 FIXME(file, "64-bit offsets not supported yet\n");
995 SetLastError( ERROR_INVALID_PARAMETER );
996 return 0xffffffff;
998 TRACE(file, "handle %d offset %ld origin %ld\n",
999 hFile, distance, method );
1001 if (!(file = FILE_GetFile( hFile ))) return 0xffffffff;
1002 switch(method)
1004 case FILE_CURRENT: origin = SEEK_CUR; break;
1005 case FILE_END: origin = SEEK_END; break;
1006 default: origin = SEEK_SET; break;
1009 if ((result = lseek( file->unix_handle, distance, origin )) == -1)
1010 FILE_SetDosError();
1011 FILE_ReleaseFile( file );
1012 return (DWORD)result;
1016 /***********************************************************************
1017 * _llseek16 (KERNEL.84)
1019 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1021 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1025 /***********************************************************************
1026 * _llseek32 (KERNEL32.594)
1028 LONG WINAPI _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
1030 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1034 /***********************************************************************
1035 * _lopen16 (KERNEL.85)
1037 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1039 return _lopen32( path, mode );
1043 /***********************************************************************
1044 * _lopen32 (KERNEL32.595)
1046 HFILE32 WINAPI _lopen32( LPCSTR path, INT32 mode )
1048 INT32 unixMode;
1050 TRACE(file, "('%s',%04x)\n", path, mode );
1052 switch(mode & 3)
1054 case OF_WRITE:
1055 unixMode = O_WRONLY;
1056 break;
1057 case OF_READWRITE:
1058 unixMode = O_RDWR;
1059 break;
1060 case OF_READ:
1061 default:
1062 unixMode = O_RDONLY;
1063 break;
1065 return FILE_Open( path, unixMode );
1069 /***********************************************************************
1070 * _lwrite16 (KERNEL.86)
1072 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1074 return (UINT16)_hwrite32( hFile, buffer, (LONG)count );
1077 /***********************************************************************
1078 * _lwrite32 (KERNEL.86)
1080 UINT32 WINAPI _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
1082 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
1086 /***********************************************************************
1087 * _hread16 (KERNEL.349)
1089 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1091 return _lread32( hFile, buffer, count );
1095 /***********************************************************************
1096 * _hread32 (KERNEL32.590)
1098 LONG WINAPI _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
1100 return _lread32( hFile, buffer, count );
1104 /***********************************************************************
1105 * _hwrite16 (KERNEL.350)
1107 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1109 return _hwrite32( hFile, buffer, count );
1113 /***********************************************************************
1114 * _hwrite32 (KERNEL32.591)
1116 * experimenation yields that _lwrite:
1117 * o truncates the file at the current position with
1118 * a 0 len write
1119 * o returns 0 on a 0 length write
1120 * o works with console handles
1123 LONG WINAPI _hwrite32( HFILE32 handle, LPCSTR buffer, LONG count )
1125 K32OBJ *ioptr;
1126 DWORD result;
1127 BOOL32 status = FALSE;
1129 TRACE(file, "%d %p %ld\n", handle, buffer, count );
1131 if (count == 0) { /* Expand or truncate at current position */
1132 FILE_OBJECT *file = FILE_GetFile(handle);
1134 if ( ftruncate(file->unix_handle,
1135 lseek( file->unix_handle, 0, SEEK_CUR)) == 0 ) {
1136 FILE_ReleaseFile(file);
1137 return 0;
1138 } else {
1139 FILE_SetDosError();
1140 FILE_ReleaseFile(file);
1141 return HFILE_ERROR32;
1145 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), handle,
1146 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 TRACE(file, "(%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 TRACE(file, "(%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 TRACE(file, "(%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 TRACE(file, "'%s'\n", path );
1277 if (DOSFS_IsDevice( path ))
1279 WARN(file, "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 FIXME(file, "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 FIXME(file, "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 TRACE(file, "(%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 WARN(file, "Illegal flag\n");
1474 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1475 EL_Unknown );
1476 return FALSE;
1478 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1479 Perhaps we should queue these command and execute it
1480 when exiting... What about using on_exit(2)
1482 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1483 full_name1.long_name);
1484 return TRUE;
1486 else if (unlink( full_name1.long_name ) == -1)
1488 FILE_SetDosError();
1489 return FALSE;
1491 else return TRUE; /* successfully deleted */
1493 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1494 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1495 Perhaps we should queue these command and execute it
1496 when exiting... What about using on_exit(2)
1498 FIXME(file,"Please move existing file '%s' to file '%s'"
1499 "when Wine has finished\n",
1500 full_name1.long_name, full_name2.long_name);
1501 return TRUE;
1504 if (!mode) /* move the file */
1505 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1507 FILE_SetDosError();
1508 return FALSE;
1510 else return TRUE;
1511 else /* copy File */
1512 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1516 /**************************************************************************
1517 * MoveFileEx32W (KERNEL32.???)
1519 BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1521 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1522 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1523 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1524 HeapFree( GetProcessHeap(), 0, afn1 );
1525 HeapFree( GetProcessHeap(), 0, afn2 );
1526 return res;
1530 /**************************************************************************
1531 * MoveFile32A (KERNEL32.387)
1533 * Move file or directory
1535 BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1537 DOS_FULL_NAME full_name1, full_name2;
1538 struct stat fstat;
1540 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1542 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1543 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1544 /* The new name must not already exist */
1545 return FALSE;
1546 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1548 if (full_name1.drive == full_name2.drive) /* move */
1549 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1551 FILE_SetDosError();
1552 return FALSE;
1554 else return TRUE;
1555 else /*copy */ {
1556 if (stat( full_name1.long_name, &fstat ))
1558 WARN(file, "Invalid source file %s\n",
1559 full_name1.long_name);
1560 FILE_SetDosError();
1561 return FALSE;
1563 if (S_ISDIR(fstat.st_mode)) {
1564 /* No Move for directories across file systems */
1565 /* FIXME: Use right error code */
1566 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1567 EL_Unknown );
1568 return FALSE;
1570 else
1571 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1576 /**************************************************************************
1577 * MoveFile32W (KERNEL32.390)
1579 BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1581 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1582 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1583 BOOL32 res = MoveFile32A( afn1, afn2 );
1584 HeapFree( GetProcessHeap(), 0, afn1 );
1585 HeapFree( GetProcessHeap(), 0, afn2 );
1586 return res;
1590 /**************************************************************************
1591 * CopyFile32A (KERNEL32.36)
1593 BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1595 HFILE32 h1, h2;
1596 BY_HANDLE_FILE_INFORMATION info;
1597 UINT32 count;
1598 BOOL32 ret = FALSE;
1599 int mode;
1600 char buffer[2048];
1602 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1603 if (!GetFileInformationByHandle( h1, &info ))
1605 CloseHandle( h1 );
1606 return FALSE;
1608 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1609 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1611 CloseHandle( h1 );
1612 return FALSE;
1614 while ((count = _lread32( h2, buffer, sizeof(buffer) )) > 0)
1616 char *p = buffer;
1617 while (count > 0)
1619 INT32 res = _lwrite32( h2, p, count );
1620 if (res <= 0) goto done;
1621 p += res;
1622 count -= res;
1625 ret = TRUE;
1626 done:
1627 CloseHandle( h1 );
1628 CloseHandle( h2 );
1629 return ret;
1633 /**************************************************************************
1634 * CopyFile32W (KERNEL32.37)
1636 BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
1638 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1639 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1640 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1641 HeapFree( GetProcessHeap(), 0, sourceA );
1642 HeapFree( GetProcessHeap(), 0, destA );
1643 return ret;
1647 /***********************************************************************
1648 * SetFileTime (KERNEL32.493)
1650 BOOL32 WINAPI SetFileTime( HFILE32 hFile,
1651 const FILETIME *lpCreationTime,
1652 const FILETIME *lpLastAccessTime,
1653 const FILETIME *lpLastWriteTime )
1655 FILE_OBJECT *file = FILE_GetFile(hFile);
1656 struct utimbuf utimbuf;
1658 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1659 TRACE(file,"(%s,%p,%p,%p)\n",
1660 file->unix_name,
1661 lpCreationTime,
1662 lpLastAccessTime,
1663 lpLastWriteTime
1665 if (lpLastAccessTime)
1666 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1667 else
1668 utimbuf.actime = 0; /* FIXME */
1669 if (lpLastWriteTime)
1670 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1671 else
1672 utimbuf.modtime = 0; /* FIXME */
1673 if (-1==utime(file->unix_name,&utimbuf))
1675 FILE_ReleaseFile( file );
1676 FILE_SetDosError();
1677 return FALSE;
1679 FILE_ReleaseFile( file );
1680 return TRUE;
1683 /* Locks need to be mirrored because unix file locking is based
1684 * on the pid. Inside of wine there can be multiple WINE processes
1685 * that share the same unix pid.
1686 * Read's and writes should check these locks also - not sure
1687 * how critical that is at this point (FIXME).
1690 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
1692 DOS_FILE_LOCK *curr;
1693 DWORD processId;
1695 processId = GetCurrentProcessId();
1697 /* check if lock overlaps a current lock for the same file */
1698 for (curr = locks; curr; curr = curr->next) {
1699 if (strcmp(curr->unix_name, file->unix_name) == 0) {
1700 if ((f->l_start == curr->base) && (f->l_len == curr->len))
1701 return TRUE;/* region is identic */
1702 if ((f->l_start < (curr->base + curr->len)) &&
1703 ((f->l_start + f->l_len) > curr->base)) {
1704 /* region overlaps */
1705 return FALSE;
1710 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
1711 curr->processId = GetCurrentProcessId();
1712 curr->base = f->l_start;
1713 curr->len = f->l_len;
1714 curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);
1715 curr->next = locks;
1716 curr->dos_file = file;
1717 locks = curr;
1718 return TRUE;
1721 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
1723 DWORD processId;
1724 DOS_FILE_LOCK **curr;
1725 DOS_FILE_LOCK *rem;
1727 processId = GetCurrentProcessId();
1728 curr = &locks;
1729 while (*curr) {
1730 if ((*curr)->dos_file == file) {
1731 rem = *curr;
1732 *curr = (*curr)->next;
1733 HeapFree( SystemHeap, 0, rem->unix_name );
1734 HeapFree( SystemHeap, 0, rem );
1736 else
1737 curr = &(*curr)->next;
1741 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
1743 DWORD processId;
1744 DOS_FILE_LOCK **curr;
1745 DOS_FILE_LOCK *rem;
1747 processId = GetCurrentProcessId();
1748 for (curr = &locks; *curr; curr = &(*curr)->next) {
1749 if ((*curr)->processId == processId &&
1750 (*curr)->dos_file == file &&
1751 (*curr)->base == f->l_start &&
1752 (*curr)->len == f->l_len) {
1753 /* this is the same lock */
1754 rem = *curr;
1755 *curr = (*curr)->next;
1756 HeapFree( SystemHeap, 0, rem->unix_name );
1757 HeapFree( SystemHeap, 0, rem );
1758 return TRUE;
1761 /* no matching lock found */
1762 return FALSE;
1766 /**************************************************************************
1767 * LockFile (KERNEL32.511)
1769 BOOL32 WINAPI LockFile(
1770 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1771 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
1773 struct flock f;
1774 FILE_OBJECT *file;
1776 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1777 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1778 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
1780 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
1781 FIXME(file, "Unimplemented bytes > 32bits\n");
1782 return FALSE;
1785 f.l_start = dwFileOffsetLow;
1786 f.l_len = nNumberOfBytesToLockLow;
1787 f.l_whence = SEEK_SET;
1788 f.l_pid = 0;
1789 f.l_type = F_WRLCK;
1791 if (!(file = FILE_GetFile(hFile))) return FALSE;
1793 /* shadow locks internally */
1794 if (!DOS_AddLock(file, &f)) {
1795 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1796 return FALSE;
1799 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1800 #ifdef USE_UNIX_LOCKS
1801 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1802 if (errno == EACCES || errno == EAGAIN) {
1803 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1805 else {
1806 FILE_SetDosError();
1808 /* remove our internal copy of the lock */
1809 DOS_RemoveLock(file, &f);
1810 return FALSE;
1812 #endif
1813 return TRUE;
1817 /**************************************************************************
1818 * UnlockFile (KERNEL32.703)
1820 BOOL32 WINAPI UnlockFile(
1821 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1822 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
1824 FILE_OBJECT *file;
1825 struct flock f;
1827 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1828 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1829 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
1831 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
1832 WARN(file, "Unimplemented bytes > 32bits\n");
1833 return FALSE;
1836 f.l_start = dwFileOffsetLow;
1837 f.l_len = nNumberOfBytesToUnlockLow;
1838 f.l_whence = SEEK_SET;
1839 f.l_pid = 0;
1840 f.l_type = F_UNLCK;
1842 if (!(file = FILE_GetFile(hFile))) return FALSE;
1844 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
1846 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1847 #ifdef USE_UNIX_LOCKS
1848 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1849 FILE_SetDosError();
1850 return FALSE;
1852 #endif
1853 return TRUE;