Release 970804
[wine/multimedia.git] / files / file.c
blob742d518e1f948388b547dc6003c928d410ad835a
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 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 GetFileAttributes16( LPCSTR name )
405 return GetFileAttributes32A( name );
409 /**************************************************************************
410 * GetFileAttributes32A (KERNEL32.217)
412 DWORD 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 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 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 GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
453 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
455 BY_HANDLE_FILE_INFORMATION info;
456 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
457 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
458 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
459 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
460 return TRUE;
463 /***********************************************************************
464 * CompareFileTime (KERNEL32.28)
466 INT32 CompareFileTime( LPFILETIME x, LPFILETIME y )
468 if (!x || !y) return -1;
470 if (x->dwHighDateTime > y->dwHighDateTime)
471 return 1;
472 if (x->dwHighDateTime < y->dwHighDateTime)
473 return -1;
474 if (x->dwLowDateTime > y->dwLowDateTime)
475 return 1;
476 if (x->dwLowDateTime < y->dwLowDateTime)
477 return -1;
478 return 0;
481 /***********************************************************************
482 * FILE_Dup
484 * dup() function for DOS handles.
486 HFILE32 FILE_Dup( HFILE32 hFile )
488 FILE_OBJECT *file;
489 HFILE32 handle;
491 dprintf_file( stddeb, "FILE_Dup for handle %d\n", hFile );
492 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR32;
493 handle = PROCESS_AllocHandle( &file->header, 0 );
494 FILE_ReleaseFile( file );
495 dprintf_file( stddeb, "FILE_Dup return handle %d\n", handle );
496 return handle;
500 /***********************************************************************
501 * FILE_Dup2
503 * dup2() function for DOS handles.
505 HFILE32 FILE_Dup2( HFILE32 hFile1, HFILE32 hFile2 )
507 FILE_OBJECT *file;
509 dprintf_file( stddeb, "FILE_Dup2 for handle %d\n", hFile1 );
510 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR32;
511 if (!PROCESS_SetObjPtr( hFile2, &file->header, 0 )) hFile2 = HFILE_ERROR32;
512 FILE_ReleaseFile( file );
513 return hFile2;
517 /***********************************************************************
518 * GetTempFileName16 (KERNEL.97)
520 UINT16 GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
521 LPSTR buffer )
523 char temppath[144];
525 if ((drive & TF_FORCEDRIVE) &&
526 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
528 drive &= ~TF_FORCEDRIVE;
529 fprintf( stderr, "Warning: GetTempFileName: invalid drive %d specified\n",
530 drive );
533 if (drive & TF_FORCEDRIVE)
534 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
535 else
537 GetTempPath32A( 132, temppath );
538 strcat( temppath, "\\" );
540 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
544 /***********************************************************************
545 * GetTempFileName32A (KERNEL32.290)
547 UINT32 GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
548 LPSTR buffer)
550 DOS_FULL_NAME full_name;
551 int i;
552 LPSTR p;
553 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
555 if ( !path || !prefix || !buffer ) return 0;
557 strcpy( buffer, path );
558 p = buffer + strlen(buffer);
559 /* add a \, if there isn't one ... */
560 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
561 *p++ = '~';
562 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
563 sprintf( p, "%04x.tmp", num );
565 /* Now try to create it */
567 if (!unique)
571 HFILE32 handle = FILE_Create( buffer, 0666, TRUE );
572 if (handle != INVALID_HANDLE_VALUE32)
573 { /* We created it */
574 dprintf_file( stddeb, "GetTempFileName32A: created %s\n",
575 buffer);
576 CloseHandle( handle );
577 break;
579 if (DOS_ExtendedError != ER_FileExists)
580 break; /* No need to go on */
581 num++;
582 sprintf( p, "%04x.tmp", num );
583 } while (num != (unique & 0xffff));
586 /* Get the full path name */
588 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
590 /* Check if we have write access in the directory */
591 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
592 if (access( full_name.long_name, W_OK ) == -1)
593 fprintf( stderr,
594 "Warning: GetTempFileName returns '%s', which doesn't seem to be writeable.\n"
595 "Please check your configuration file if this generates a failure.\n",
596 buffer);
598 dprintf_file( stddeb, "GetTempFileName32A: returning %s\n", buffer );
599 return unique ? unique : num;
603 /***********************************************************************
604 * GetTempFileName32W (KERNEL32.291)
606 UINT32 GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
607 LPWSTR buffer )
609 LPSTR patha,prefixa;
610 char buffera[144];
611 UINT32 ret;
613 if (!path) return 0;
614 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
615 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
616 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
617 lstrcpyAtoW( buffer, buffera );
618 HeapFree( GetProcessHeap(), 0, patha );
619 HeapFree( GetProcessHeap(), 0, prefixa );
620 return ret;
624 /***********************************************************************
625 * FILE_DoOpenFile
627 * Implementation of OpenFile16() and OpenFile32().
629 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
630 BOOL32 win32 )
632 HFILE32 hFileRet;
633 FILETIME filetime;
634 WORD filedatetime[2];
635 DOS_FULL_NAME full_name;
636 char *p;
637 int unixMode;
639 if (!ofs) return HFILE_ERROR32;
642 ofs->cBytes = sizeof(OFSTRUCT);
643 ofs->nErrCode = 0;
644 if (mode & OF_REOPEN) name = ofs->szPathName;
646 if (!name) {
647 fprintf(stderr, "ERROR: FILE_DoOpenFile() called with `name' set to NULL ! Please debug.\n");
649 return HFILE_ERROR32;
652 dprintf_file( stddeb, "OpenFile: %s %04x\n", name, mode );
654 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
655 Are there any cases where getting the path here is wrong?
656 Uwe Bonnes 1997 Apr 2 */
657 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
658 ofs->szPathName, NULL )) goto error;
660 /* OF_PARSE simply fills the structure */
662 if (mode & OF_PARSE)
664 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
665 != DRIVE_REMOVABLE);
666 dprintf_file( stddeb, "OpenFile(%s): OF_PARSE, res = '%s'\n",
667 name, ofs->szPathName );
668 return 0;
671 /* OF_CREATE is completely different from all other options, so
672 handle it first */
674 if (mode & OF_CREATE)
676 if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32)
677 goto error;
678 goto success;
681 /* If OF_SEARCH is set, ignore the given path */
683 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
685 /* First try the file name as is */
686 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
687 /* Now remove the path */
688 if (name[0] && (name[1] == ':')) name += 2;
689 if ((p = strrchr( name, '\\' ))) name = p + 1;
690 if ((p = strrchr( name, '/' ))) name = p + 1;
691 if (!name[0]) goto not_found;
694 /* Now look for the file */
696 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
698 found:
699 dprintf_file( stddeb, "OpenFile: found %s = %s\n",
700 full_name.long_name, full_name.short_name );
701 lstrcpyn32A( ofs->szPathName, full_name.short_name,
702 sizeof(ofs->szPathName) );
704 if (mode & OF_DELETE)
706 if (unlink( full_name.long_name ) == -1) goto not_found;
707 dprintf_file( stddeb, "OpenFile(%s): OF_DELETE return = OK\n", name);
708 return 1;
711 switch(mode & 3)
713 case OF_WRITE:
714 unixMode = O_WRONLY; break;
715 case OF_READWRITE:
716 unixMode = O_RDWR; break;
717 case OF_READ:
718 default:
719 unixMode = O_RDONLY; break;
722 hFileRet = FILE_OpenUnixFile( full_name.long_name, unixMode );
723 if (hFileRet == HFILE_ERROR32) goto not_found;
724 GetFileTime( hFileRet, NULL, NULL, &filetime );
725 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
726 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
728 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
730 CloseHandle( hFileRet );
731 dprintf_file( stddeb, "OpenFile(%s): OF_VERIFY failed\n", name );
732 /* FIXME: what error here? */
733 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
734 goto error;
737 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
739 success: /* We get here if the open was successful */
740 dprintf_file( stddeb, "OpenFile(%s): OK, return = %d\n", name, hFileRet );
741 if (mode & OF_EXIST) /* Return the handle, but close it first */
742 CloseHandle( hFileRet );
743 return hFileRet;
745 not_found: /* We get here if the file does not exist */
746 dprintf_file( stddeb, "OpenFile: '%s' not found\n", name );
747 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
748 /* fall through */
750 error: /* We get here if there was an error opening the file */
751 ofs->nErrCode = DOS_ExtendedError;
752 dprintf_file( stddeb, "OpenFile(%s): return = HFILE_ERROR error= %d\n",
753 name,ofs->nErrCode );
754 return HFILE_ERROR32;
758 /***********************************************************************
759 * OpenFile16 (KERNEL.74)
761 HFILE16 OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
763 return FILE_DoOpenFile( name, ofs, mode, FALSE );
767 /***********************************************************************
768 * OpenFile32 (KERNEL32.396)
770 HFILE32 OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
772 return FILE_DoOpenFile( name, ofs, mode, TRUE );
776 /***********************************************************************
777 * _lclose16 (KERNEL.81)
779 HFILE16 _lclose16( HFILE16 hFile )
781 dprintf_file( stddeb, "_lclose16: handle %d\n", hFile );
782 return CloseHandle( hFile ) ? 0 : HFILE_ERROR16;
786 /***********************************************************************
787 * _lclose32 (KERNEL32.592)
789 HFILE32 _lclose32( HFILE32 hFile )
791 dprintf_file( stddeb, "_lclose32: handle %d\n", hFile );
792 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
796 /***********************************************************************
797 * WIN16_hread
799 LONG WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
801 LONG maxlen;
803 dprintf_file( stddeb, "WIN16_hread: %d %08lx %ld\n",
804 hFile, (DWORD)buffer, count );
806 /* Some programs pass a count larger than the allocated buffer */
807 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
808 if (count > maxlen) count = maxlen;
809 return _lread32( hFile, PTR_SEG_TO_LIN(buffer), count );
813 /***********************************************************************
814 * WIN16_lread
816 UINT16 WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
818 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
822 /***********************************************************************
823 * _lread32 (KERNEL32.596)
825 UINT32 _lread32( HFILE32 hFile, LPVOID buffer, UINT32 count )
827 FILE_OBJECT *file;
828 UINT32 result;
830 dprintf_file( stddeb, "_lread32: %d %p %d\n", hFile, buffer, count );
831 if (!(file = FILE_GetFile( hFile ))) return -1;
832 if (!count) result = 0;
833 else if ((result = read( file->unix_handle, buffer, count )) == -1)
834 FILE_SetDosError();
835 FILE_ReleaseFile( file );
836 return result;
840 /***********************************************************************
841 * _lread16 (KERNEL.82)
843 UINT16 _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
845 return (UINT16)_lread32( hFile, buffer, (LONG)count );
849 /***********************************************************************
850 * _lcreat16 (KERNEL.83)
852 HFILE16 _lcreat16( LPCSTR path, INT16 attr )
854 int mode = (attr & 1) ? 0444 : 0666;
855 dprintf_file( stddeb, "_lcreat16: %s %02x\n", path, attr );
856 return (HFILE16)FILE_Create( path, mode, FALSE );
860 /***********************************************************************
861 * _lcreat32 (KERNEL32.593)
863 HFILE32 _lcreat32( LPCSTR path, INT32 attr )
865 int mode = (attr & 1) ? 0444 : 0666;
866 dprintf_file( stddeb, "_lcreat32: %s %02x\n", path, attr );
867 return FILE_Create( path, mode, FALSE );
871 /***********************************************************************
872 * _lcreat_uniq (Not a Windows API)
874 HFILE32 _lcreat_uniq( LPCSTR path, INT32 attr )
876 int mode = (attr & 1) ? 0444 : 0666;
877 dprintf_file( stddeb, "_lcreat_uniq: %s %02x\n", path, attr );
878 return FILE_Create( path, mode, TRUE );
882 /***********************************************************************
883 * SetFilePointer (KERNEL32.492)
885 DWORD SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
886 DWORD method )
888 FILE_OBJECT *file;
889 int origin, result;
891 if (highword && *highword)
893 fprintf( stderr, "SetFilePointer: 64-bit offsets not supported yet\n");
894 SetLastError( ERROR_INVALID_PARAMETER );
895 return 0xffffffff;
897 dprintf_file( stddeb, "SetFilePointer: handle %d offset %ld origin %ld\n",
898 hFile, distance, method );
900 if (!(file = FILE_GetFile( hFile ))) return 0xffffffff;
901 switch(method)
903 case FILE_CURRENT: origin = SEEK_CUR; break;
904 case FILE_END: origin = SEEK_END; break;
905 default: origin = SEEK_SET; break;
908 if ((result = lseek( file->unix_handle, distance, origin )) == -1)
909 FILE_SetDosError();
910 FILE_ReleaseFile( file );
911 return (DWORD)result;
915 /***********************************************************************
916 * _llseek16 (KERNEL.84)
918 LONG _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
920 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
924 /***********************************************************************
925 * _llseek32 (KERNEL32.594)
927 LONG _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
929 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
933 /***********************************************************************
934 * _lopen16 (KERNEL.85)
936 HFILE16 _lopen16( LPCSTR path, INT16 mode )
938 return _lopen32( path, mode );
942 /***********************************************************************
943 * _lopen32 (KERNEL32.595)
945 HFILE32 _lopen32( LPCSTR path, INT32 mode )
947 INT32 unixMode;
949 dprintf_file(stddeb, "_lopen32('%s',%04x)\n", path, mode );
951 switch(mode & 3)
953 case OF_WRITE:
954 unixMode = O_WRONLY;
955 break;
956 case OF_READWRITE:
957 unixMode = O_RDWR;
958 break;
959 case OF_READ:
960 default:
961 unixMode = O_RDONLY;
962 break;
964 return FILE_Open( path, unixMode );
968 /***********************************************************************
969 * _lwrite16 (KERNEL.86)
971 UINT16 _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
973 return (UINT16)_hwrite32( hFile, buffer, (LONG)count );
976 /***********************************************************************
977 * _lwrite32 (KERNEL.86)
979 UINT32 _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
981 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
985 /***********************************************************************
986 * _hread16 (KERNEL.349)
988 LONG _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
990 return _lread32( hFile, buffer, count );
994 /***********************************************************************
995 * _hread32 (KERNEL32.590)
997 LONG _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
999 return _lread32( hFile, buffer, count );
1003 /***********************************************************************
1004 * _hwrite16 (KERNEL.350)
1006 LONG _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1008 return _hwrite32( hFile, buffer, count );
1012 /***********************************************************************
1013 * _hwrite32 (KERNEL32.591)
1015 LONG _hwrite32( HFILE32 hFile, LPCSTR buffer, LONG count )
1017 FILE_OBJECT *file;
1018 LONG result;
1020 dprintf_file( stddeb, "_hwrite32: %d %p %ld\n", hFile, buffer, count );
1022 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR32;
1023 if (count == 0) /* Expand or truncate at current position */
1024 result = ftruncate( file->unix_handle,
1025 lseek( file->unix_handle, 0, SEEK_CUR ) );
1026 else for (;;)
1028 result = write( file->unix_handle, buffer, count );
1029 if (result != -1) break;
1030 if (errno != EINTR)
1032 FILE_SetDosError();
1033 break;
1037 FILE_ReleaseFile( file );
1038 return result;
1042 /***********************************************************************
1043 * SetHandleCount16 (KERNEL.199)
1045 UINT16 SetHandleCount16( UINT16 count )
1047 HGLOBAL16 hPDB = GetCurrentPDB();
1048 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1049 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1051 dprintf_file( stddeb, "SetHandleCount16(%d)\n", count );
1053 if (count < 20) count = 20; /* No point in going below 20 */
1054 else if (count > 254) count = 254;
1056 if (count == 20)
1058 if (pdb->nbFiles > 20)
1060 memcpy( pdb->fileHandles, files, 20 );
1061 GlobalFree16( pdb->hFileHandles );
1062 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1063 GlobalHandleToSel( hPDB ) );
1064 pdb->hFileHandles = 0;
1065 pdb->nbFiles = 20;
1068 else /* More than 20, need a new file handles table */
1070 BYTE *newfiles;
1071 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1072 if (!newhandle)
1074 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1075 return pdb->nbFiles;
1077 newfiles = (BYTE *)GlobalLock16( newhandle );
1079 if (count > pdb->nbFiles)
1081 memcpy( newfiles, files, pdb->nbFiles );
1082 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1084 else memcpy( newfiles, files, count );
1085 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1086 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1087 pdb->hFileHandles = newhandle;
1088 pdb->nbFiles = count;
1090 return pdb->nbFiles;
1094 /*************************************************************************
1095 * SetHandleCount32 (KERNEL32.494)
1097 UINT32 SetHandleCount32( UINT32 count )
1099 return MIN( 256, count );
1103 /***********************************************************************
1104 * FlushFileBuffers (KERNEL32.133)
1106 BOOL32 FlushFileBuffers( HFILE32 hFile )
1108 FILE_OBJECT *file;
1109 BOOL32 ret;
1111 dprintf_file( stddeb, "FlushFileBuffers(%d)\n", hFile );
1112 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1113 if (fsync( file->unix_handle ) != -1) ret = TRUE;
1114 else
1116 FILE_SetDosError();
1117 ret = FALSE;
1119 FILE_ReleaseFile( file );
1120 return ret;
1124 /**************************************************************************
1125 * SetEndOfFile (KERNEL32.483)
1127 BOOL32 SetEndOfFile( HFILE32 hFile )
1129 FILE_OBJECT *file;
1130 BOOL32 ret = TRUE;
1132 dprintf_file( stddeb, "SetEndOfFile(%d)\n", hFile );
1133 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1134 if (ftruncate( file->unix_handle,
1135 lseek( file->unix_handle, 0, SEEK_CUR ) ))
1137 FILE_SetDosError();
1138 ret = FALSE;
1140 FILE_ReleaseFile( file );
1141 return ret;
1145 /***********************************************************************
1146 * DeleteFile16 (KERNEL.146)
1148 BOOL16 DeleteFile16( LPCSTR path )
1150 return DeleteFile32A( path );
1154 /***********************************************************************
1155 * DeleteFile32A (KERNEL32.71)
1157 BOOL32 DeleteFile32A( LPCSTR path )
1159 DOS_FULL_NAME full_name;
1160 const char *unixName;
1162 dprintf_file(stddeb, "DeleteFile: '%s'\n", path );
1164 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1166 dprintf_file(stddeb, "DeleteFile: removing device '%s'!\n", unixName);
1167 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1168 return FALSE;
1171 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1172 if (unlink( full_name.long_name ) == -1)
1174 FILE_SetDosError();
1175 return FALSE;
1177 return TRUE;
1181 /***********************************************************************
1182 * DeleteFile32W (KERNEL32.72)
1184 BOOL32 DeleteFile32W( LPCWSTR path )
1186 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1187 BOOL32 ret = RemoveDirectory32A( xpath );
1188 HeapFree( GetProcessHeap(), 0, xpath );
1189 return ret;
1193 /***********************************************************************
1194 * FILE_SetFileType
1196 BOOL32 FILE_SetFileType( HFILE32 hFile, DWORD type )
1198 FILE_OBJECT *file = FILE_GetFile( hFile );
1199 if (!file) return FALSE;
1200 file->type = type;
1201 FILE_ReleaseFile( file );
1202 return TRUE;
1206 /***********************************************************************
1207 * FILE_mmap
1209 LPVOID FILE_mmap( FILE_OBJECT *file, LPVOID start,
1210 DWORD size_high, DWORD size_low,
1211 DWORD offset_high, DWORD offset_low,
1212 int prot, int flags )
1214 int fd = -1;
1216 if (size_high || offset_high)
1217 fprintf( stderr, "FILE_mmap: offsets larger than 4Gb not supported\n");
1219 if (!file)
1221 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1222 #ifdef MAP_SHARED
1223 flags &= ~MAP_SHARED;
1224 #endif
1225 #ifdef MAP_PRIVATE
1226 flags |= MAP_PRIVATE;
1227 #endif
1228 #ifdef MAP_ANON
1229 flags |= MAP_ANON;
1230 #else
1231 static int fdzero = -1;
1233 if (fdzero == -1)
1235 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1237 perror( "/dev/zero: open" );
1238 exit(1);
1241 fd = fdzero;
1242 #endif /* MAP_ANON */
1244 else fd = file->unix_handle;
1246 return mmap( start, size_low, prot, flags, fd, offset_low );
1250 /***********************************************************************
1251 * GetFileType (KERNEL32.222)
1253 DWORD GetFileType( HFILE32 hFile )
1255 FILE_OBJECT *file = FILE_GetFile(hFile);
1256 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1257 FILE_ReleaseFile( file );
1258 return file->type;
1262 /**************************************************************************
1263 * MoveFileEx32A (KERNEL32.???)
1267 BOOL32 MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1269 DOS_FULL_NAME full_name1, full_name2;
1270 int mode=0; /* mode == 1: use copy */
1272 dprintf_file( stddeb, "MoveFileEx32A(%s,%s,%04lx)\n", fn1, fn2, flag);
1274 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1275 if (fn2) { /* !fn2 means delete fn1 */
1276 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1277 /* Source name and target path are valid */
1278 if ( full_name1.drive != full_name2.drive)
1279 /* use copy, if allowed */
1280 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1281 /* FIXME: Use right error code */
1282 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
1283 return FALSE;
1285 else mode =1;
1286 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1287 /* target exists, check if we may overwrite */
1288 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1289 /* FIXME: Use right error code */
1290 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1291 return FALSE;
1294 else /* fn2 == NULL means delete source */
1295 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1296 if (flag & MOVEFILE_COPY_ALLOWED) {
1297 fprintf( stderr,
1298 "MoveFileEx32A: Illegal flag\n");
1299 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1300 EL_Unknown );
1301 return FALSE;
1303 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1304 Perhaps we should queue these command and execute it
1305 when exiting... What about using on_exit(2)
1307 fprintf( stderr,"MoveFileEx32A: Please delete file %s\n",
1308 full_name1.long_name);
1309 fprintf( stderr," when Wine has finished\n");
1310 fprintf( stderr," like \"rm %s\"\n",
1311 full_name1.long_name);
1312 return TRUE;
1314 else if (unlink( full_name1.long_name ) == -1)
1316 FILE_SetDosError();
1317 return FALSE;
1319 else return TRUE; /* successfully deleted */
1321 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1322 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1323 Perhaps we should queue these command and execute it
1324 when exiting... What about using on_exit(2)
1326 fprintf( stderr,"MoveFileEx32A: Please move existing file %s\n"
1327 ,full_name1.long_name);
1328 fprintf( stderr," to file %s\n"
1329 ,full_name2.long_name);
1330 fprintf( stderr," when Wine has finished\n");
1331 fprintf( stderr," like \" mv %s %s\"\n",
1332 full_name1.long_name,full_name2.long_name);
1333 return TRUE;
1336 if (!mode) /* move the file */
1337 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1339 FILE_SetDosError();
1340 return FALSE;
1342 else return TRUE;
1343 else /* copy File */
1344 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1348 /**************************************************************************
1349 * MoveFileEx32W (KERNEL32.???)
1351 BOOL32 MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1353 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1354 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1355 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1356 HeapFree( GetProcessHeap(), 0, afn1 );
1357 HeapFree( GetProcessHeap(), 0, afn2 );
1358 return res;
1362 /**************************************************************************
1363 * MoveFile32A (KERNEL32.387)
1365 * Move file or directory
1367 BOOL32 MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1369 DOS_FULL_NAME full_name1, full_name2;
1370 struct stat fstat;
1372 dprintf_file( stddeb, "MoveFile32A(%s,%s)\n", fn1, fn2 );
1374 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1375 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1376 /* The new name must not already exist */
1377 return FALSE;
1378 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1380 if (full_name1.drive == full_name2.drive) /* move */
1381 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1383 FILE_SetDosError();
1384 return FALSE;
1386 else return TRUE;
1387 else /*copy */ {
1388 if (stat( full_name1.long_name, &fstat ))
1390 dprintf_file( stddeb, "Invalid source file %s\n",
1391 full_name1.long_name);
1392 FILE_SetDosError();
1393 return FALSE;
1395 if (S_ISDIR(fstat.st_mode)) {
1396 /* No Move for directories across file systems */
1397 /* FIXME: Use right error code */
1398 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1399 EL_Unknown );
1400 return FALSE;
1402 else
1403 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1408 /**************************************************************************
1409 * MoveFile32W (KERNEL32.390)
1411 BOOL32 MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1413 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1414 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1415 BOOL32 res = MoveFile32A( afn1, afn2 );
1416 HeapFree( GetProcessHeap(), 0, afn1 );
1417 HeapFree( GetProcessHeap(), 0, afn2 );
1418 return res;
1422 /**************************************************************************
1423 * CopyFile32A (KERNEL32.36)
1425 BOOL32 CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1427 HFILE32 h1, h2;
1428 BY_HANDLE_FILE_INFORMATION info;
1429 UINT32 count;
1430 BOOL32 ret = FALSE;
1431 int mode;
1432 char buffer[2048];
1434 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1435 if (!GetFileInformationByHandle( h1, &info ))
1437 CloseHandle( h1 );
1438 return FALSE;
1440 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1441 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1443 CloseHandle( h1 );
1444 return FALSE;
1446 while ((count = _lread32( h2, buffer, sizeof(buffer) )) > 0)
1448 char *p = buffer;
1449 while (count > 0)
1451 INT32 res = _lwrite32( h2, p, count );
1452 if (res <= 0) goto done;
1453 p += res;
1454 count -= res;
1457 ret = TRUE;
1458 done:
1459 CloseHandle( h1 );
1460 CloseHandle( h2 );
1461 return ret;
1465 /**************************************************************************
1466 * CopyFile32W (KERNEL32.37)
1468 BOOL32 CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists )
1470 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1471 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1472 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1473 HeapFree( GetProcessHeap(), 0, sourceA );
1474 HeapFree( GetProcessHeap(), 0, destA );
1475 return ret;
1479 /***********************************************************************
1480 * SetFileTime (KERNEL32.493)
1482 BOOL32 SetFileTime( HFILE32 hFile,
1483 const FILETIME *lpCreationTime,
1484 const FILETIME *lpLastAccessTime,
1485 const FILETIME *lpLastWriteTime )
1487 FILE_OBJECT *file = FILE_GetFile(hFile);
1488 struct utimbuf utimbuf;
1490 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1491 dprintf_file(stddeb,"SetFileTime(%s,%p,%p,%p)\n",
1492 file->unix_name,
1493 lpCreationTime,
1494 lpLastAccessTime,
1495 lpLastWriteTime
1497 if (lpLastAccessTime)
1498 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1499 else
1500 utimbuf.actime = 0; /* FIXME */
1501 if (lpLastWriteTime)
1502 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1503 else
1504 utimbuf.modtime = 0; /* FIXME */
1505 if (-1==utime(file->unix_name,&utimbuf))
1507 FILE_ReleaseFile( file );
1508 FILE_SetDosError();
1509 return FALSE;
1511 FILE_ReleaseFile( file );
1512 return TRUE;
1515 /* Locks need to be mirrored because unix file locking is based
1516 * on the pid. Inside of wine there can be multiple WINE processes
1517 * that share the same unix pid.
1518 * Read's and writes should check these locks also - not sure
1519 * how critical that is at this point (FIXME).
1522 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
1524 DOS_FILE_LOCK *curr;
1525 DWORD processId;
1527 processId = GetCurrentProcessId();
1529 /* check if lock overlaps a current lock for the same file */
1530 for (curr = locks; curr; curr = curr->next) {
1531 if (strcmp(curr->unix_name, file->unix_name) == 0) {
1532 if ((f->l_start < (curr->base + curr->len)) &&
1533 ((f->l_start + f->l_len) > curr->base)) {
1534 /* region overlaps */
1535 return FALSE;
1540 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
1541 curr->processId = GetCurrentProcessId();
1542 curr->base = f->l_start;
1543 curr->len = f->l_len;
1544 curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);
1545 curr->next = locks;
1546 curr->dos_file = file;
1547 locks = curr;
1548 return TRUE;
1551 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
1553 DWORD processId;
1554 DOS_FILE_LOCK **curr;
1555 DOS_FILE_LOCK *rem;
1557 processId = GetCurrentProcessId();
1558 curr = &locks;
1559 while (*curr) {
1560 if ((*curr)->dos_file == file) {
1561 rem = *curr;
1562 *curr = (*curr)->next;
1563 HeapFree( SystemHeap, 0, rem->unix_name );
1564 HeapFree( SystemHeap, 0, rem );
1566 else
1567 curr = &(*curr)->next;
1571 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
1573 DWORD processId;
1574 DOS_FILE_LOCK **curr;
1575 DOS_FILE_LOCK *rem;
1577 processId = GetCurrentProcessId();
1578 for (curr = &locks; *curr; curr = &(*curr)->next) {
1579 if ((*curr)->processId == processId &&
1580 (*curr)->dos_file == file &&
1581 (*curr)->base == f->l_start &&
1582 (*curr)->len == f->l_len) {
1583 /* this is the same lock */
1584 rem = *curr;
1585 *curr = (*curr)->next;
1586 HeapFree( SystemHeap, 0, rem->unix_name );
1587 HeapFree( SystemHeap, 0, rem );
1588 return TRUE;
1591 /* no matching lock found */
1592 return FALSE;
1596 /**************************************************************************
1597 * LockFile (KERNEL32.511)
1599 BOOL32 LockFile(
1600 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1601 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
1603 struct flock f;
1604 FILE_OBJECT *file;
1606 dprintf_file(stddeb, "LockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1607 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1608 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
1610 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
1611 dprintf_file(stddeb, "LockFile32: Unimplemented bytes > 32bits\n");
1612 return FALSE;
1615 f.l_start = dwFileOffsetLow;
1616 f.l_len = nNumberOfBytesToLockLow;
1617 f.l_whence = SEEK_SET;
1618 f.l_pid = 0;
1619 f.l_type = F_WRLCK;
1621 if (!(file = FILE_GetFile(hFile))) return FALSE;
1623 /* shadow locks internally */
1624 if (!DOS_AddLock(file, &f)) {
1625 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1626 return FALSE;
1629 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1630 #ifdef USE_UNIX_LOCKS
1631 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1632 if (errno == EACCES || errno == EAGAIN) {
1633 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1635 else {
1636 FILE_SetDosError();
1638 /* remove our internal copy of the lock */
1639 DOS_RemoveLock(file, &f);
1640 return FALSE;
1642 #endif
1643 return TRUE;
1647 /**************************************************************************
1648 * UnlockFile (KERNEL32.703)
1650 BOOL32 UnlockFile(
1651 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1652 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
1654 FILE_OBJECT *file;
1655 struct flock f;
1657 dprintf_file(stddeb, "UnlockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1658 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1659 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
1661 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
1662 dprintf_file(stddeb, "UnlockFile32: Unimplemented bytes > 32bits\n");
1663 return FALSE;
1666 f.l_start = dwFileOffsetLow;
1667 f.l_len = nNumberOfBytesToUnlockLow;
1668 f.l_whence = SEEK_SET;
1669 f.l_pid = 0;
1670 f.l_type = F_UNLCK;
1672 if (!(file = FILE_GetFile(hFile))) return FALSE;
1674 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
1676 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1677 #ifdef USE_UNIX_LOCKS
1678 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1679 FILE_SetDosError();
1680 return FALSE;
1682 #endif
1683 return TRUE;