Release 971116
[wine/multimedia.git] / files / file.c
blobf60a115ea2afaabbc4a7d3ae5a939c0ebbdae1e0
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 "stddebug.h"
35 #include "debug.h"
37 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
38 #define MAP_ANON MAP_ANONYMOUS
39 #endif
41 struct DOS_FILE_LOCK {
42 struct DOS_FILE_LOCK * next;
43 DWORD base;
44 DWORD len;
45 DWORD processId;
46 FILE_OBJECT * dos_file;
47 char * unix_name;
50 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
52 static DOS_FILE_LOCK *locks = NULL;
53 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
55 /***********************************************************************
56 * FILE_Alloc
58 * Allocate a file.
60 static HFILE32 FILE_Alloc( FILE_OBJECT **file )
62 HFILE32 handle;
63 *file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) );
64 if (!*file)
66 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
67 return NULL;
69 (*file)->header.type = K32OBJ_FILE;
70 (*file)->header.refcount = 0;
71 (*file)->unix_handle = -1;
72 (*file)->unix_name = NULL;
73 (*file)->type = FILE_TYPE_DISK;
75 handle = PROCESS_AllocHandle( &(*file)->header, 0 );
76 if (handle == INVALID_HANDLE_VALUE32) *file = NULL;
77 return handle;
81 /***********************************************************************
82 * FILE_Destroy
84 * Destroy a DOS file.
86 void FILE_Destroy( K32OBJ *ptr )
88 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
89 assert( ptr->type == K32OBJ_FILE );
91 DOS_RemoveFileLocks(file);
93 if (file->unix_handle != -1) close( file->unix_handle );
94 if (file->unix_name) HeapFree( SystemHeap, 0, file->unix_name );
95 ptr->type = K32OBJ_UNKNOWN;
96 HeapFree( SystemHeap, 0, file );
100 /***********************************************************************
101 * FILE_GetFile
103 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
104 * be called to release the file.
106 static FILE_OBJECT *FILE_GetFile( HFILE32 handle )
108 return (FILE_OBJECT *)PROCESS_GetObjPtr( handle, K32OBJ_FILE );
112 /***********************************************************************
113 * FILE_ReleaseFile
115 * Release a DOS file obtained with FILE_GetFile.
117 static void FILE_ReleaseFile( FILE_OBJECT *file )
119 K32OBJ_DecCount( &file->header );
123 /***********************************************************************
124 * FILE_GetUnixHandle
126 * Return the Unix handle associated to a file handle.
128 int FILE_GetUnixHandle( HFILE32 hFile )
130 FILE_OBJECT *file;
131 int ret;
133 if (!(file = FILE_GetFile( hFile ))) return -1;
134 ret = file->unix_handle;
135 FILE_ReleaseFile( file );
136 return ret;
140 /***********************************************************************
141 * FILE_SetDosError
143 * Set the DOS error code from errno.
145 void FILE_SetDosError(void)
147 int save_errno = errno; /* errno gets overwritten by printf */
149 dprintf_file(stddeb, "FILE_SetDosError: errno = %d\n", errno );
150 switch (save_errno)
152 case EAGAIN:
153 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
154 break;
155 case EBADF:
156 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
157 break;
158 case ENOSPC:
159 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
160 break;
161 case EACCES:
162 case EPERM:
163 case EROFS:
164 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
165 break;
166 case EBUSY:
167 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
168 break;
169 case ENOENT:
170 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
171 break;
172 case EISDIR:
173 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
174 break;
175 case ENFILE:
176 case EMFILE:
177 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
178 break;
179 case EEXIST:
180 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
181 break;
182 default:
183 perror( "int21: unknown errno" );
184 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
185 break;
187 errno = save_errno;
191 /***********************************************************************
192 * FILE_DupUnixHandle
194 * Duplicate a Unix handle into a task handle.
196 HFILE32 FILE_DupUnixHandle( int fd )
198 HFILE32 handle;
199 FILE_OBJECT *file;
201 if ((handle = FILE_Alloc( &file )) != INVALID_HANDLE_VALUE32)
203 if ((file->unix_handle = dup(fd)) == -1)
205 FILE_SetDosError();
206 CloseHandle( handle );
207 return INVALID_HANDLE_VALUE32;
210 return handle;
214 /***********************************************************************
215 * FILE_OpenUnixFile
217 static HFILE32 FILE_OpenUnixFile( const char *name, int mode )
219 HFILE32 handle;
220 FILE_OBJECT *file;
221 struct stat st;
223 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
224 return INVALID_HANDLE_VALUE32;
226 if ((file->unix_handle = open( name, mode, 0666 )) == -1)
228 if (!Options.failReadOnly && (mode == O_RDWR))
229 file->unix_handle = open( name, O_RDONLY );
231 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
233 FILE_SetDosError();
234 CloseHandle( handle );
235 return INVALID_HANDLE_VALUE32;
237 if (S_ISDIR(st.st_mode))
239 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
240 CloseHandle( handle );
241 return INVALID_HANDLE_VALUE32;
244 /* File opened OK, now fill the FILE_OBJECT */
246 file->unix_name = HEAP_strdupA( SystemHeap, 0, name );
247 return handle;
251 /***********************************************************************
252 * FILE_Open
254 HFILE32 FILE_Open( LPCSTR path, INT32 mode )
256 DOS_FULL_NAME full_name;
257 const char *unixName;
259 dprintf_file(stddeb, "FILE_Open: '%s' %04x\n", path, mode );
261 if (!path) return HFILE_ERROR32;
263 if ((unixName = DOSFS_IsDevice( path )) != NULL)
265 dprintf_file( stddeb, "FILE_Open: opening device '%s'\n", unixName );
266 if (!unixName[0]) /* Non-existing device */
268 dprintf_file(stddeb, "FILE_Open: Non-existing device\n");
269 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
270 return HFILE_ERROR32;
273 else /* check for filename, don't check for last entry if creating */
275 if (!DOSFS_GetFullName( path, !(mode & O_CREAT), &full_name ))
276 return HFILE_ERROR32;
277 unixName = full_name.long_name;
279 return FILE_OpenUnixFile( unixName, mode );
283 /***********************************************************************
284 * FILE_Create
286 static HFILE32 FILE_Create( LPCSTR path, int mode, int unique )
288 HFILE32 handle;
289 FILE_OBJECT *file;
290 const char *unixName;
291 DOS_FULL_NAME full_name;
293 dprintf_file(stddeb, "FILE_Create: '%s' %04x %d\n", path, mode, unique );
295 if (!path) return INVALID_HANDLE_VALUE32;
297 if ((unixName = DOSFS_IsDevice( path )) != NULL)
299 dprintf_file(stddeb, "FILE_Create: creating device '%s'!\n", unixName);
300 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
301 return INVALID_HANDLE_VALUE32;
304 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
305 return INVALID_HANDLE_VALUE32;
307 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
309 CloseHandle( handle );
310 return INVALID_HANDLE_VALUE32;
312 if ((file->unix_handle = open( full_name.long_name,
313 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
314 mode )) == -1)
316 FILE_SetDosError();
317 CloseHandle( handle );
318 return INVALID_HANDLE_VALUE32;
321 /* File created OK, now fill the FILE_OBJECT */
323 file->unix_name = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
324 return handle;
328 /***********************************************************************
329 * FILE_FillInfo
331 * Fill a file information from a struct stat.
333 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
335 if (S_ISDIR(st->st_mode))
336 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
337 else
338 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
339 if (!(st->st_mode & S_IWUSR))
340 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
342 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
343 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
344 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
346 info->dwVolumeSerialNumber = 0; /* FIXME */
347 info->nFileSizeHigh = 0;
348 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
349 info->nNumberOfLinks = st->st_nlink;
350 info->nFileIndexHigh = 0;
351 info->nFileIndexLow = st->st_ino;
355 /***********************************************************************
356 * FILE_Stat
358 * Stat a Unix path name. Return TRUE if OK.
360 BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
362 struct stat st;
364 if (!unixName || !info) return FALSE;
366 if (stat( unixName, &st ) == -1)
368 FILE_SetDosError();
369 return FALSE;
371 FILE_FillInfo( &st, info );
372 return TRUE;
376 /***********************************************************************
377 * GetFileInformationByHandle (KERNEL32.219)
379 DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile,
380 BY_HANDLE_FILE_INFORMATION *info )
382 FILE_OBJECT *file;
383 DWORD ret = 0;
384 struct stat st;
386 if (!info) return 0;
388 if (!(file = FILE_GetFile( hFile ))) return 0;
389 if (fstat( file->unix_handle, &st ) == -1) FILE_SetDosError();
390 else
392 FILE_FillInfo( &st, info );
393 ret = 1;
395 FILE_ReleaseFile( file );
396 return ret;
400 /**************************************************************************
401 * GetFileAttributes16 (KERNEL.420)
403 DWORD WINAPI GetFileAttributes16( LPCSTR name )
405 return GetFileAttributes32A( name );
409 /**************************************************************************
410 * GetFileAttributes32A (KERNEL32.217)
412 DWORD WINAPI GetFileAttributes32A( LPCSTR name )
414 DOS_FULL_NAME full_name;
415 BY_HANDLE_FILE_INFORMATION info;
417 if (name == NULL) return -1;
419 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
420 if (!FILE_Stat( full_name.long_name, &info )) return -1;
421 return info.dwFileAttributes;
425 /**************************************************************************
426 * GetFileAttributes32W (KERNEL32.218)
428 DWORD WINAPI GetFileAttributes32W( LPCWSTR name )
430 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
431 DWORD res = GetFileAttributes32A( nameA );
432 HeapFree( GetProcessHeap(), 0, nameA );
433 return res;
437 /***********************************************************************
438 * GetFileSize (KERNEL32.220)
440 DWORD WINAPI GetFileSize( HFILE32 hFile, LPDWORD filesizehigh )
442 BY_HANDLE_FILE_INFORMATION info;
443 if (!GetFileInformationByHandle( hFile, &info )) return 0;
444 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
445 return info.nFileSizeLow;
449 /***********************************************************************
450 * GetFileTime (KERNEL32.221)
452 BOOL32 WINAPI GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
453 FILETIME *lpLastAccessTime,
454 FILETIME *lpLastWriteTime )
456 BY_HANDLE_FILE_INFORMATION info;
457 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
458 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
459 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
460 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
461 return TRUE;
464 /***********************************************************************
465 * CompareFileTime (KERNEL32.28)
467 INT32 WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
469 if (!x || !y) return -1;
471 if (x->dwHighDateTime > y->dwHighDateTime)
472 return 1;
473 if (x->dwHighDateTime < y->dwHighDateTime)
474 return -1;
475 if (x->dwLowDateTime > y->dwLowDateTime)
476 return 1;
477 if (x->dwLowDateTime < y->dwLowDateTime)
478 return -1;
479 return 0;
482 /***********************************************************************
483 * FILE_Dup
485 * dup() function for DOS handles.
487 HFILE32 FILE_Dup( HFILE32 hFile )
489 FILE_OBJECT *file;
490 HFILE32 handle;
492 dprintf_file( stddeb, "FILE_Dup for handle %d\n", hFile );
493 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR32;
494 handle = PROCESS_AllocHandle( &file->header, 0 );
495 FILE_ReleaseFile( file );
496 dprintf_file( stddeb, "FILE_Dup return handle %d\n", handle );
497 return handle;
501 /***********************************************************************
502 * FILE_Dup2
504 * dup2() function for DOS handles.
506 HFILE32 FILE_Dup2( HFILE32 hFile1, HFILE32 hFile2 )
508 FILE_OBJECT *file;
510 dprintf_file( stddeb, "FILE_Dup2 for handle %d\n", hFile1 );
511 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR32;
512 if (!PROCESS_SetObjPtr( hFile2, &file->header, 0 )) hFile2 = HFILE_ERROR32;
513 FILE_ReleaseFile( file );
514 return hFile2;
518 /***********************************************************************
519 * GetTempFileName16 (KERNEL.97)
521 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
522 LPSTR buffer )
524 char temppath[144];
526 if ((drive & TF_FORCEDRIVE) &&
527 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
529 drive &= ~TF_FORCEDRIVE;
530 fprintf( stderr, "Warning: GetTempFileName: invalid drive %d specified\n",
531 drive );
534 if (drive & TF_FORCEDRIVE)
535 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
536 else
538 GetTempPath32A( 132, temppath );
539 strcat( temppath, "\\" );
541 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
545 /***********************************************************************
546 * GetTempFileName32A (KERNEL32.290)
548 UINT32 WINAPI GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
549 LPSTR buffer)
551 DOS_FULL_NAME full_name;
552 int i;
553 LPSTR p;
554 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
556 if ( !path || !prefix || !buffer ) return 0;
558 strcpy( buffer, path );
559 p = buffer + strlen(buffer);
560 /* add a \, if there isn't one ... */
561 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
562 *p++ = '~';
563 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
564 sprintf( p, "%04x.tmp", num );
566 /* Now try to create it */
568 if (!unique)
572 HFILE32 handle = FILE_Create( buffer, 0666, TRUE );
573 if (handle != INVALID_HANDLE_VALUE32)
574 { /* We created it */
575 dprintf_file( stddeb, "GetTempFileName32A: created %s\n",
576 buffer);
577 CloseHandle( handle );
578 break;
580 if (DOS_ExtendedError != ER_FileExists)
581 break; /* No need to go on */
582 num++;
583 sprintf( p, "%04x.tmp", num );
584 } while (num != (unique & 0xffff));
587 /* Get the full path name */
589 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
591 /* Check if we have write access in the directory */
592 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
593 if (access( full_name.long_name, W_OK ) == -1)
594 fprintf( stderr,
595 "Warning: GetTempFileName returns '%s', which doesn't seem to be writeable.\n"
596 "Please check your configuration file if this generates a failure.\n",
597 buffer);
599 dprintf_file( stddeb, "GetTempFileName32A: returning %s\n", buffer );
600 return unique ? unique : num;
604 /***********************************************************************
605 * GetTempFileName32W (KERNEL32.291)
607 UINT32 WINAPI GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
608 LPWSTR buffer )
610 LPSTR patha,prefixa;
611 char buffera[144];
612 UINT32 ret;
614 if (!path) return 0;
615 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
616 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
617 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
618 lstrcpyAtoW( buffer, buffera );
619 HeapFree( GetProcessHeap(), 0, patha );
620 HeapFree( GetProcessHeap(), 0, prefixa );
621 return ret;
625 /***********************************************************************
626 * FILE_DoOpenFile
628 * Implementation of OpenFile16() and OpenFile32().
630 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
631 BOOL32 win32 )
633 HFILE32 hFileRet;
634 FILETIME filetime;
635 WORD filedatetime[2];
636 DOS_FULL_NAME full_name;
637 char *p;
638 int unixMode;
640 if (!ofs) return HFILE_ERROR32;
643 ofs->cBytes = sizeof(OFSTRUCT);
644 ofs->nErrCode = 0;
645 if (mode & OF_REOPEN) name = ofs->szPathName;
647 if (!name) {
648 fprintf(stderr, "ERROR: FILE_DoOpenFile() called with `name' set to NULL ! Please debug.\n");
650 return HFILE_ERROR32;
653 dprintf_file( stddeb, "OpenFile: %s %04x\n", name, mode );
655 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
656 Are there any cases where getting the path here is wrong?
657 Uwe Bonnes 1997 Apr 2 */
658 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
659 ofs->szPathName, NULL )) goto error;
661 /* OF_PARSE simply fills the structure */
663 if (mode & OF_PARSE)
665 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
666 != DRIVE_REMOVABLE);
667 dprintf_file( stddeb, "OpenFile(%s): OF_PARSE, res = '%s'\n",
668 name, ofs->szPathName );
669 return 0;
672 /* OF_CREATE is completely different from all other options, so
673 handle it first */
675 if (mode & OF_CREATE)
677 if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32)
678 goto error;
679 goto success;
682 /* If OF_SEARCH is set, ignore the given path */
684 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
686 /* First try the file name as is */
687 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
688 /* Now remove the path */
689 if (name[0] && (name[1] == ':')) name += 2;
690 if ((p = strrchr( name, '\\' ))) name = p + 1;
691 if ((p = strrchr( name, '/' ))) name = p + 1;
692 if (!name[0]) goto not_found;
695 /* Now look for the file */
697 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
699 found:
700 dprintf_file( stddeb, "OpenFile: found %s = %s\n",
701 full_name.long_name, full_name.short_name );
702 lstrcpyn32A( ofs->szPathName, full_name.short_name,
703 sizeof(ofs->szPathName) );
705 if (mode & OF_DELETE)
707 if (unlink( full_name.long_name ) == -1) goto not_found;
708 dprintf_file( stddeb, "OpenFile(%s): OF_DELETE return = OK\n", name);
709 return 1;
712 switch(mode & 3)
714 case OF_WRITE:
715 unixMode = O_WRONLY; break;
716 case OF_READWRITE:
717 unixMode = O_RDWR; break;
718 case OF_READ:
719 default:
720 unixMode = O_RDONLY; break;
723 hFileRet = FILE_OpenUnixFile( full_name.long_name, unixMode );
724 if (hFileRet == HFILE_ERROR32) goto not_found;
725 GetFileTime( hFileRet, NULL, NULL, &filetime );
726 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
727 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
729 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
731 CloseHandle( hFileRet );
732 dprintf_file( stddeb, "OpenFile(%s): OF_VERIFY failed\n", name );
733 /* FIXME: what error here? */
734 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
735 goto error;
738 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
740 success: /* We get here if the open was successful */
741 dprintf_file( stddeb, "OpenFile(%s): OK, return = %d\n", name, hFileRet );
742 if (mode & OF_EXIST) /* Return the handle, but close it first */
743 CloseHandle( hFileRet );
744 return hFileRet;
746 not_found: /* We get here if the file does not exist */
747 dprintf_file( stddeb, "OpenFile: '%s' not found\n", name );
748 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
749 /* fall through */
751 error: /* We get here if there was an error opening the file */
752 ofs->nErrCode = DOS_ExtendedError;
753 dprintf_file( stddeb, "OpenFile(%s): return = HFILE_ERROR error= %d\n",
754 name,ofs->nErrCode );
755 return HFILE_ERROR32;
759 /***********************************************************************
760 * OpenFile16 (KERNEL.74)
762 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
764 return FILE_DoOpenFile( name, ofs, mode, FALSE );
768 /***********************************************************************
769 * OpenFile32 (KERNEL32.396)
771 HFILE32 WINAPI OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
773 return FILE_DoOpenFile( name, ofs, mode, TRUE );
777 /***********************************************************************
778 * _lclose16 (KERNEL.81)
780 HFILE16 WINAPI _lclose16( HFILE16 hFile )
782 dprintf_file( stddeb, "_lclose16: handle %d\n", hFile );
783 return CloseHandle( hFile ) ? 0 : HFILE_ERROR16;
787 /***********************************************************************
788 * _lclose32 (KERNEL32.592)
790 HFILE32 WINAPI _lclose32( HFILE32 hFile )
792 dprintf_file( stddeb, "_lclose32: handle %d\n", hFile );
793 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
797 /***********************************************************************
798 * WIN16_hread
800 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
802 LONG maxlen;
804 dprintf_file( stddeb, "WIN16_hread: %d %08lx %ld\n",
805 hFile, (DWORD)buffer, count );
807 /* Some programs pass a count larger than the allocated buffer */
808 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
809 if (count > maxlen) count = maxlen;
810 return _lread32( hFile, PTR_SEG_TO_LIN(buffer), count );
814 /***********************************************************************
815 * WIN16_lread
817 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
819 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
823 /***********************************************************************
824 * _lread32 (KERNEL32.596)
826 UINT32 WINAPI _lread32( HFILE32 hFile, LPVOID buffer, UINT32 count )
828 FILE_OBJECT *file;
829 UINT32 result;
831 dprintf_file( stddeb, "_lread32: %d %p %d\n", hFile, buffer, count );
832 if (!(file = FILE_GetFile( hFile ))) return -1;
833 if (!count) result = 0;
834 else if ((result = read( file->unix_handle, buffer, count )) == -1)
835 FILE_SetDosError();
836 FILE_ReleaseFile( file );
837 return result;
841 /***********************************************************************
842 * _lread16 (KERNEL.82)
844 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
846 return (UINT16)_lread32( hFile, buffer, (LONG)count );
850 /***********************************************************************
851 * _lcreat16 (KERNEL.83)
853 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
855 int mode = (attr & 1) ? 0444 : 0666;
856 dprintf_file( stddeb, "_lcreat16: %s %02x\n", path, attr );
857 return (HFILE16)FILE_Create( path, mode, FALSE );
861 /***********************************************************************
862 * _lcreat32 (KERNEL32.593)
864 HFILE32 WINAPI _lcreat32( LPCSTR path, INT32 attr )
866 int mode = (attr & 1) ? 0444 : 0666;
867 dprintf_file( stddeb, "_lcreat32: %s %02x\n", path, attr );
868 return FILE_Create( path, mode, FALSE );
872 /***********************************************************************
873 * _lcreat_uniq (Not a Windows API)
875 HFILE32 _lcreat_uniq( LPCSTR path, INT32 attr )
877 int mode = (attr & 1) ? 0444 : 0666;
878 dprintf_file( stddeb, "_lcreat_uniq: %s %02x\n", path, attr );
879 return FILE_Create( path, mode, TRUE );
883 /***********************************************************************
884 * SetFilePointer (KERNEL32.492)
886 DWORD WINAPI SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
887 DWORD method )
889 FILE_OBJECT *file;
890 int origin, result;
892 if (highword && *highword)
894 fprintf( stderr, "SetFilePointer: 64-bit offsets not supported yet\n");
895 SetLastError( ERROR_INVALID_PARAMETER );
896 return 0xffffffff;
898 dprintf_file( stddeb, "SetFilePointer: handle %d offset %ld origin %ld\n",
899 hFile, distance, method );
901 if (!(file = FILE_GetFile( hFile ))) return 0xffffffff;
902 switch(method)
904 case FILE_CURRENT: origin = SEEK_CUR; break;
905 case FILE_END: origin = SEEK_END; break;
906 default: origin = SEEK_SET; break;
909 if ((result = lseek( file->unix_handle, distance, origin )) == -1)
910 FILE_SetDosError();
911 FILE_ReleaseFile( file );
912 return (DWORD)result;
916 /***********************************************************************
917 * _llseek16 (KERNEL.84)
919 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
921 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
925 /***********************************************************************
926 * _llseek32 (KERNEL32.594)
928 LONG WINAPI _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
930 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
934 /***********************************************************************
935 * _lopen16 (KERNEL.85)
937 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
939 return _lopen32( path, mode );
943 /***********************************************************************
944 * _lopen32 (KERNEL32.595)
946 HFILE32 WINAPI _lopen32( LPCSTR path, INT32 mode )
948 INT32 unixMode;
950 dprintf_file(stddeb, "_lopen32('%s',%04x)\n", path, mode );
952 switch(mode & 3)
954 case OF_WRITE:
955 unixMode = O_WRONLY;
956 break;
957 case OF_READWRITE:
958 unixMode = O_RDWR;
959 break;
960 case OF_READ:
961 default:
962 unixMode = O_RDONLY;
963 break;
965 return FILE_Open( path, unixMode );
969 /***********************************************************************
970 * _lwrite16 (KERNEL.86)
972 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
974 return (UINT16)_hwrite32( hFile, buffer, (LONG)count );
977 /***********************************************************************
978 * _lwrite32 (KERNEL.86)
980 UINT32 WINAPI _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
982 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
986 /***********************************************************************
987 * _hread16 (KERNEL.349)
989 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
991 return _lread32( hFile, buffer, count );
995 /***********************************************************************
996 * _hread32 (KERNEL32.590)
998 LONG WINAPI _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
1000 return _lread32( hFile, buffer, count );
1004 /***********************************************************************
1005 * _hwrite16 (KERNEL.350)
1007 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1009 return _hwrite32( hFile, buffer, count );
1013 /***********************************************************************
1014 * _hwrite32 (KERNEL32.591)
1016 LONG WINAPI _hwrite32( HFILE32 hFile, LPCSTR buffer, LONG count )
1018 FILE_OBJECT *file;
1019 LONG result;
1021 dprintf_file( stddeb, "_hwrite32: %d %p %ld\n", hFile, buffer, count );
1023 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR32;
1024 if (count == 0) /* Expand or truncate at current position */
1025 result = ftruncate( file->unix_handle,
1026 lseek( file->unix_handle, 0, SEEK_CUR ) );
1027 else for (;;)
1029 result = write( file->unix_handle, buffer, count );
1030 if (result != -1) break;
1031 if (errno != EINTR)
1033 FILE_SetDosError();
1034 break;
1038 FILE_ReleaseFile( file );
1039 return result;
1043 /***********************************************************************
1044 * SetHandleCount16 (KERNEL.199)
1046 UINT16 WINAPI SetHandleCount16( UINT16 count )
1048 HGLOBAL16 hPDB = GetCurrentPDB();
1049 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1050 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1052 dprintf_file( stddeb, "SetHandleCount16(%d)\n", count );
1054 if (count < 20) count = 20; /* No point in going below 20 */
1055 else if (count > 254) count = 254;
1057 if (count == 20)
1059 if (pdb->nbFiles > 20)
1061 memcpy( pdb->fileHandles, files, 20 );
1062 GlobalFree16( pdb->hFileHandles );
1063 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1064 GlobalHandleToSel( hPDB ) );
1065 pdb->hFileHandles = 0;
1066 pdb->nbFiles = 20;
1069 else /* More than 20, need a new file handles table */
1071 BYTE *newfiles;
1072 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1073 if (!newhandle)
1075 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1076 return pdb->nbFiles;
1078 newfiles = (BYTE *)GlobalLock16( newhandle );
1080 if (count > pdb->nbFiles)
1082 memcpy( newfiles, files, pdb->nbFiles );
1083 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1085 else memcpy( newfiles, files, count );
1086 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1087 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1088 pdb->hFileHandles = newhandle;
1089 pdb->nbFiles = count;
1091 return pdb->nbFiles;
1095 /*************************************************************************
1096 * SetHandleCount32 (KERNEL32.494)
1098 UINT32 WINAPI SetHandleCount32( UINT32 count )
1100 return MIN( 256, count );
1104 /***********************************************************************
1105 * FlushFileBuffers (KERNEL32.133)
1107 BOOL32 WINAPI FlushFileBuffers( HFILE32 hFile )
1109 FILE_OBJECT *file;
1110 BOOL32 ret;
1112 dprintf_file( stddeb, "FlushFileBuffers(%d)\n", hFile );
1113 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1114 if (fsync( file->unix_handle ) != -1) ret = TRUE;
1115 else
1117 FILE_SetDosError();
1118 ret = FALSE;
1120 FILE_ReleaseFile( file );
1121 return ret;
1125 /**************************************************************************
1126 * SetEndOfFile (KERNEL32.483)
1128 BOOL32 WINAPI SetEndOfFile( HFILE32 hFile )
1130 FILE_OBJECT *file;
1131 BOOL32 ret = TRUE;
1133 dprintf_file( stddeb, "SetEndOfFile(%d)\n", hFile );
1134 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1135 if (ftruncate( file->unix_handle,
1136 lseek( file->unix_handle, 0, SEEK_CUR ) ))
1138 FILE_SetDosError();
1139 ret = FALSE;
1141 FILE_ReleaseFile( file );
1142 return ret;
1146 /***********************************************************************
1147 * DeleteFile16 (KERNEL.146)
1149 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1151 return DeleteFile32A( path );
1155 /***********************************************************************
1156 * DeleteFile32A (KERNEL32.71)
1158 BOOL32 WINAPI DeleteFile32A( LPCSTR path )
1160 DOS_FULL_NAME full_name;
1161 const char *unixName;
1163 dprintf_file(stddeb, "DeleteFile: '%s'\n", path );
1165 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1167 dprintf_file(stddeb, "DeleteFile: removing device '%s'!\n", unixName);
1168 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1169 return FALSE;
1172 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1173 if (unlink( full_name.long_name ) == -1)
1175 FILE_SetDosError();
1176 return FALSE;
1178 return TRUE;
1182 /***********************************************************************
1183 * DeleteFile32W (KERNEL32.72)
1185 BOOL32 WINAPI DeleteFile32W( LPCWSTR path )
1187 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1188 BOOL32 ret = RemoveDirectory32A( xpath );
1189 HeapFree( GetProcessHeap(), 0, xpath );
1190 return ret;
1194 /***********************************************************************
1195 * FILE_SetFileType
1197 BOOL32 FILE_SetFileType( HFILE32 hFile, DWORD type )
1199 FILE_OBJECT *file = FILE_GetFile( hFile );
1200 if (!file) return FALSE;
1201 file->type = type;
1202 FILE_ReleaseFile( file );
1203 return TRUE;
1207 /***********************************************************************
1208 * FILE_mmap
1210 LPVOID FILE_mmap( HFILE32 hFile, LPVOID start,
1211 DWORD size_high, DWORD size_low,
1212 DWORD offset_high, DWORD offset_low,
1213 int prot, int flags )
1215 LPVOID ret;
1216 FILE_OBJECT *file = FILE_GetFile( hFile );
1217 if (!file) return (LPVOID)-1;
1218 ret = FILE_dommap( file, start, size_high, size_low,
1219 offset_high, offset_low, prot, flags );
1220 FILE_ReleaseFile( file );
1221 return ret;
1225 /***********************************************************************
1226 * FILE_dommap
1228 LPVOID FILE_dommap( FILE_OBJECT *file, LPVOID start,
1229 DWORD size_high, DWORD size_low,
1230 DWORD offset_high, DWORD offset_low,
1231 int prot, int flags )
1233 int fd = -1;
1234 int pos;
1235 LPVOID ret;
1237 if (size_high || offset_high)
1238 fprintf( stderr, "FILE_mmap: offsets larger than 4Gb not supported\n");
1240 if (!file)
1242 #ifdef MAP_ANON
1243 flags |= MAP_ANON;
1244 #else
1245 static int fdzero = -1;
1247 if (fdzero == -1)
1249 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1251 perror( "/dev/zero: open" );
1252 exit(1);
1255 fd = fdzero;
1256 #endif /* MAP_ANON */
1257 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1258 #ifdef MAP_SHARED
1259 flags &= ~MAP_SHARED;
1260 #endif
1261 #ifdef MAP_PRIVATE
1262 flags |= MAP_PRIVATE;
1263 #endif
1265 else fd = file->unix_handle;
1267 if ((ret = mmap( start, size_low, prot,
1268 flags, fd, offset_low )) != (LPVOID)-1)
1269 return ret;
1271 /* mmap() failed; if this is because the file offset is not */
1272 /* page-aligned (EINVAL), or because the underlying filesystem */
1273 /* does not support mmap() (ENOEXEC), we do it by hand. */
1275 if (!file) return ret;
1276 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1277 if (prot & PROT_WRITE)
1279 /* We cannot fake shared write mappings */
1280 #ifdef MAP_SHARED
1281 if (flags & MAP_SHARED) return ret;
1282 #endif
1283 #ifdef MAP_PRIVATE
1284 if (!(flags & MAP_PRIVATE)) return ret;
1285 #endif
1287 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1288 /* Reserve the memory with an anonymous mmap */
1289 ret = FILE_dommap( NULL, start, size_high, size_low, 0, 0,
1290 PROT_READ | PROT_WRITE, flags );
1291 if (ret == (LPVOID)-1) return ret;
1292 /* Now read in the file */
1293 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1295 FILE_munmap( ret, size_high, size_low );
1296 return (LPVOID)-1;
1298 read( fd, ret, size_low );
1299 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1300 mprotect( ret, size_low, prot ); /* Set the right protection */
1301 return ret;
1305 /***********************************************************************
1306 * FILE_munmap
1308 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1310 if (size_high)
1311 fprintf( stderr, "FILE_munmap: offsets larger than 4Gb not supported\n");
1312 return munmap( start, size_low );
1316 /***********************************************************************
1317 * GetFileType (KERNEL32.222)
1319 DWORD WINAPI GetFileType( HFILE32 hFile )
1321 FILE_OBJECT *file = FILE_GetFile(hFile);
1322 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1323 FILE_ReleaseFile( file );
1324 return file->type;
1328 /**************************************************************************
1329 * MoveFileEx32A (KERNEL32.???)
1331 BOOL32 WINAPI MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1333 DOS_FULL_NAME full_name1, full_name2;
1334 int mode=0; /* mode == 1: use copy */
1336 dprintf_file( stddeb, "MoveFileEx32A(%s,%s,%04lx)\n", fn1, fn2, flag);
1338 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1339 if (fn2) { /* !fn2 means delete fn1 */
1340 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1341 /* Source name and target path are valid */
1342 if ( full_name1.drive != full_name2.drive)
1343 /* use copy, if allowed */
1344 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1345 /* FIXME: Use right error code */
1346 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
1347 return FALSE;
1349 else mode =1;
1350 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1351 /* target exists, check if we may overwrite */
1352 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1353 /* FIXME: Use right error code */
1354 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1355 return FALSE;
1358 else /* fn2 == NULL means delete source */
1359 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1360 if (flag & MOVEFILE_COPY_ALLOWED) {
1361 fprintf( stderr,
1362 "MoveFileEx32A: Illegal flag\n");
1363 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1364 EL_Unknown );
1365 return FALSE;
1367 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1368 Perhaps we should queue these command and execute it
1369 when exiting... What about using on_exit(2)
1371 fprintf( stderr,"MoveFileEx32A: Please delete file %s\n",
1372 full_name1.long_name);
1373 fprintf( stderr," when Wine has finished\n");
1374 fprintf( stderr," like \"rm %s\"\n",
1375 full_name1.long_name);
1376 return TRUE;
1378 else if (unlink( full_name1.long_name ) == -1)
1380 FILE_SetDosError();
1381 return FALSE;
1383 else return TRUE; /* successfully deleted */
1385 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1386 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1387 Perhaps we should queue these command and execute it
1388 when exiting... What about using on_exit(2)
1390 fprintf( stderr,"MoveFileEx32A: Please move existing file %s\n"
1391 ,full_name1.long_name);
1392 fprintf( stderr," to file %s\n"
1393 ,full_name2.long_name);
1394 fprintf( stderr," when Wine has finished\n");
1395 fprintf( stderr," like \" mv %s %s\"\n",
1396 full_name1.long_name,full_name2.long_name);
1397 return TRUE;
1400 if (!mode) /* move the file */
1401 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1403 FILE_SetDosError();
1404 return FALSE;
1406 else return TRUE;
1407 else /* copy File */
1408 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1412 /**************************************************************************
1413 * MoveFileEx32W (KERNEL32.???)
1415 BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1417 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1418 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1419 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1420 HeapFree( GetProcessHeap(), 0, afn1 );
1421 HeapFree( GetProcessHeap(), 0, afn2 );
1422 return res;
1426 /**************************************************************************
1427 * MoveFile32A (KERNEL32.387)
1429 * Move file or directory
1431 BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1433 DOS_FULL_NAME full_name1, full_name2;
1434 struct stat fstat;
1436 dprintf_file( stddeb, "MoveFile32A(%s,%s)\n", fn1, fn2 );
1438 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1439 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1440 /* The new name must not already exist */
1441 return FALSE;
1442 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1444 if (full_name1.drive == full_name2.drive) /* move */
1445 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1447 FILE_SetDosError();
1448 return FALSE;
1450 else return TRUE;
1451 else /*copy */ {
1452 if (stat( full_name1.long_name, &fstat ))
1454 dprintf_file( stddeb, "Invalid source file %s\n",
1455 full_name1.long_name);
1456 FILE_SetDosError();
1457 return FALSE;
1459 if (S_ISDIR(fstat.st_mode)) {
1460 /* No Move for directories across file systems */
1461 /* FIXME: Use right error code */
1462 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1463 EL_Unknown );
1464 return FALSE;
1466 else
1467 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1472 /**************************************************************************
1473 * MoveFile32W (KERNEL32.390)
1475 BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1477 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1478 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1479 BOOL32 res = MoveFile32A( afn1, afn2 );
1480 HeapFree( GetProcessHeap(), 0, afn1 );
1481 HeapFree( GetProcessHeap(), 0, afn2 );
1482 return res;
1486 /**************************************************************************
1487 * CopyFile32A (KERNEL32.36)
1489 BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1491 HFILE32 h1, h2;
1492 BY_HANDLE_FILE_INFORMATION info;
1493 UINT32 count;
1494 BOOL32 ret = FALSE;
1495 int mode;
1496 char buffer[2048];
1498 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1499 if (!GetFileInformationByHandle( h1, &info ))
1501 CloseHandle( h1 );
1502 return FALSE;
1504 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1505 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1507 CloseHandle( h1 );
1508 return FALSE;
1510 while ((count = _lread32( h2, buffer, sizeof(buffer) )) > 0)
1512 char *p = buffer;
1513 while (count > 0)
1515 INT32 res = _lwrite32( h2, p, count );
1516 if (res <= 0) goto done;
1517 p += res;
1518 count -= res;
1521 ret = TRUE;
1522 done:
1523 CloseHandle( h1 );
1524 CloseHandle( h2 );
1525 return ret;
1529 /**************************************************************************
1530 * CopyFile32W (KERNEL32.37)
1532 BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
1534 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1535 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1536 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1537 HeapFree( GetProcessHeap(), 0, sourceA );
1538 HeapFree( GetProcessHeap(), 0, destA );
1539 return ret;
1543 /***********************************************************************
1544 * SetFileTime (KERNEL32.493)
1546 BOOL32 WINAPI SetFileTime( HFILE32 hFile,
1547 const FILETIME *lpCreationTime,
1548 const FILETIME *lpLastAccessTime,
1549 const FILETIME *lpLastWriteTime )
1551 FILE_OBJECT *file = FILE_GetFile(hFile);
1552 struct utimbuf utimbuf;
1554 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1555 dprintf_file(stddeb,"SetFileTime(%s,%p,%p,%p)\n",
1556 file->unix_name,
1557 lpCreationTime,
1558 lpLastAccessTime,
1559 lpLastWriteTime
1561 if (lpLastAccessTime)
1562 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1563 else
1564 utimbuf.actime = 0; /* FIXME */
1565 if (lpLastWriteTime)
1566 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1567 else
1568 utimbuf.modtime = 0; /* FIXME */
1569 if (-1==utime(file->unix_name,&utimbuf))
1571 FILE_ReleaseFile( file );
1572 FILE_SetDosError();
1573 return FALSE;
1575 FILE_ReleaseFile( file );
1576 return TRUE;
1579 /* Locks need to be mirrored because unix file locking is based
1580 * on the pid. Inside of wine there can be multiple WINE processes
1581 * that share the same unix pid.
1582 * Read's and writes should check these locks also - not sure
1583 * how critical that is at this point (FIXME).
1586 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
1588 DOS_FILE_LOCK *curr;
1589 DWORD processId;
1591 processId = GetCurrentProcessId();
1593 /* check if lock overlaps a current lock for the same file */
1594 for (curr = locks; curr; curr = curr->next) {
1595 if (strcmp(curr->unix_name, file->unix_name) == 0) {
1596 if ((f->l_start < (curr->base + curr->len)) &&
1597 ((f->l_start + f->l_len) > curr->base)) {
1598 /* region overlaps */
1599 return FALSE;
1604 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
1605 curr->processId = GetCurrentProcessId();
1606 curr->base = f->l_start;
1607 curr->len = f->l_len;
1608 curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);
1609 curr->next = locks;
1610 curr->dos_file = file;
1611 locks = curr;
1612 return TRUE;
1615 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
1617 DWORD processId;
1618 DOS_FILE_LOCK **curr;
1619 DOS_FILE_LOCK *rem;
1621 processId = GetCurrentProcessId();
1622 curr = &locks;
1623 while (*curr) {
1624 if ((*curr)->dos_file == file) {
1625 rem = *curr;
1626 *curr = (*curr)->next;
1627 HeapFree( SystemHeap, 0, rem->unix_name );
1628 HeapFree( SystemHeap, 0, rem );
1630 else
1631 curr = &(*curr)->next;
1635 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
1637 DWORD processId;
1638 DOS_FILE_LOCK **curr;
1639 DOS_FILE_LOCK *rem;
1641 processId = GetCurrentProcessId();
1642 for (curr = &locks; *curr; curr = &(*curr)->next) {
1643 if ((*curr)->processId == processId &&
1644 (*curr)->dos_file == file &&
1645 (*curr)->base == f->l_start &&
1646 (*curr)->len == f->l_len) {
1647 /* this is the same lock */
1648 rem = *curr;
1649 *curr = (*curr)->next;
1650 HeapFree( SystemHeap, 0, rem->unix_name );
1651 HeapFree( SystemHeap, 0, rem );
1652 return TRUE;
1655 /* no matching lock found */
1656 return FALSE;
1660 /**************************************************************************
1661 * LockFile (KERNEL32.511)
1663 BOOL32 WINAPI LockFile(
1664 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1665 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
1667 struct flock f;
1668 FILE_OBJECT *file;
1670 dprintf_file(stddeb, "LockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1671 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1672 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
1674 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
1675 dprintf_file(stddeb, "LockFile32: Unimplemented bytes > 32bits\n");
1676 return FALSE;
1679 f.l_start = dwFileOffsetLow;
1680 f.l_len = nNumberOfBytesToLockLow;
1681 f.l_whence = SEEK_SET;
1682 f.l_pid = 0;
1683 f.l_type = F_WRLCK;
1685 if (!(file = FILE_GetFile(hFile))) return FALSE;
1687 /* shadow locks internally */
1688 if (!DOS_AddLock(file, &f)) {
1689 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1690 return FALSE;
1693 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1694 #ifdef USE_UNIX_LOCKS
1695 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1696 if (errno == EACCES || errno == EAGAIN) {
1697 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1699 else {
1700 FILE_SetDosError();
1702 /* remove our internal copy of the lock */
1703 DOS_RemoveLock(file, &f);
1704 return FALSE;
1706 #endif
1707 return TRUE;
1711 /**************************************************************************
1712 * UnlockFile (KERNEL32.703)
1714 BOOL32 WINAPI UnlockFile(
1715 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1716 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
1718 FILE_OBJECT *file;
1719 struct flock f;
1721 dprintf_file(stddeb, "UnlockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1722 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1723 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
1725 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
1726 dprintf_file(stddeb, "UnlockFile32: Unimplemented bytes > 32bits\n");
1727 return FALSE;
1730 f.l_start = dwFileOffsetLow;
1731 f.l_len = nNumberOfBytesToUnlockLow;
1732 f.l_whence = SEEK_SET;
1733 f.l_pid = 0;
1734 f.l_type = F_UNLCK;
1736 if (!(file = FILE_GetFile(hFile))) return FALSE;
1738 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
1740 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1741 #ifdef USE_UNIX_LOCKS
1742 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1743 FILE_SetDosError();
1744 return FALSE;
1746 #endif
1747 return TRUE;