2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
15 #include <sys/errno.h>
16 #include <sys/types.h>
37 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
38 #define MAP_ANON MAP_ANONYMOUS
41 struct DOS_FILE_LOCK {
42 struct DOS_FILE_LOCK * next;
46 FILE_OBJECT * dos_file;
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 /***********************************************************************
60 static HFILE32 FILE_Alloc( FILE_OBJECT **file )
63 *file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) );
66 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
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;
81 /***********************************************************************
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 /***********************************************************************
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 /***********************************************************************
115 * Release a DOS file obtained with FILE_GetFile.
117 static void FILE_ReleaseFile( FILE_OBJECT *file )
119 K32OBJ_DecCount( &file->header );
123 /***********************************************************************
126 * Return the Unix handle associated to a file handle.
128 int FILE_GetUnixHandle( HFILE32 hFile )
133 if (!(file = FILE_GetFile( hFile ))) return -1;
134 ret = file->unix_handle;
135 FILE_ReleaseFile( file );
140 /***********************************************************************
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 );
153 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
156 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
159 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
164 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
167 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
170 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
173 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
177 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
180 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
183 perror( "int21: unknown errno" );
184 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
191 /***********************************************************************
194 * Duplicate a Unix handle into a task handle.
196 HFILE32 FILE_DupUnixHandle( int fd )
201 if ((handle = FILE_Alloc( &file )) != INVALID_HANDLE_VALUE32)
203 if ((file->unix_handle = dup(fd)) == -1)
206 CloseHandle( handle );
207 return INVALID_HANDLE_VALUE32;
214 /***********************************************************************
217 static HFILE32 FILE_OpenUnixFile( const char *name, int mode )
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))
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 );
251 /***********************************************************************
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 /***********************************************************************
286 static HFILE32 FILE_Create( LPCSTR path, int mode, int unique )
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),
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 );
328 /***********************************************************************
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;
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 /***********************************************************************
358 * Stat a Unix path name. Return TRUE if OK.
360 BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
364 if (!unixName || !info) return FALSE;
366 if (stat( unixName, &st ) == -1)
371 FILE_FillInfo( &st, info );
376 /***********************************************************************
377 * GetFileInformationByHandle (KERNEL32.219)
379 DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile,
380 BY_HANDLE_FILE_INFORMATION *info )
388 if (!(file = FILE_GetFile( hFile ))) return 0;
389 if (fstat( file->unix_handle, &st ) == -1) FILE_SetDosError();
392 FILE_FillInfo( &st, info );
395 FILE_ReleaseFile( file );
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 );
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;
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)
473 if (x->dwHighDateTime < y->dwHighDateTime)
475 if (x->dwLowDateTime > y->dwLowDateTime)
477 if (x->dwLowDateTime < y->dwLowDateTime)
482 /***********************************************************************
485 * dup() function for DOS handles.
487 HFILE32 FILE_Dup( HFILE32 hFile )
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 );
501 /***********************************************************************
504 * dup2() function for DOS handles.
506 HFILE32 FILE_Dup2( HFILE32 hFile1, HFILE32 hFile2 )
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 );
518 /***********************************************************************
519 * GetTempFileName16 (KERNEL.97)
521 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
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",
534 if (drive & TF_FORCEDRIVE)
535 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
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,
551 DOS_FULL_NAME full_name;
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++ = '\\';
563 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
564 sprintf( p, "%04x.tmp", num );
566 /* Now try to create it */
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",
577 CloseHandle( handle );
580 if (DOS_ExtendedError != ER_FileExists)
581 break; /* No need to go on */
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)
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",
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,
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 );
625 /***********************************************************************
628 * Implementation of OpenFile16() and OpenFile32().
630 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
635 WORD filedatetime[2];
636 DOS_FULL_NAME full_name;
640 if (!ofs) return HFILE_ERROR32;
643 ofs->cBytes = sizeof(OFSTRUCT);
645 if (mode & OF_REOPEN) name = ofs->szPathName;
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 */
665 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
667 dprintf_file( stddeb, "OpenFile(%s): OF_PARSE, res = '%s'\n",
668 name, ofs->szPathName );
672 /* OF_CREATE is completely different from all other options, so
675 if (mode & OF_CREATE)
677 if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32)
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;
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);
715 unixMode = O_WRONLY; break;
717 unixMode = O_RDWR; break;
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 );
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 );
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 );
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 /***********************************************************************
800 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
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 /***********************************************************************
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 )
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)
836 FILE_ReleaseFile( file );
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,
892 if (highword && *highword)
894 fprintf( stderr, "SetFilePointer: 64-bit offsets not supported yet\n");
895 SetLastError( ERROR_INVALID_PARAMETER );
898 dprintf_file( stddeb, "SetFilePointer: handle %d offset %ld origin %ld\n",
899 hFile, distance, method );
901 if (!(file = FILE_GetFile( hFile ))) return 0xffffffff;
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)
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 )
950 dprintf_file(stddeb, "_lopen32('%s',%04x)\n", path, mode );
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 )
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 ) );
1029 result = write( file->unix_handle, buffer, count );
1030 if (result != -1) break;
1038 FILE_ReleaseFile( file );
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;
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;
1069 else /* More than 20, need a new file handles table */
1072 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
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 )
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;
1120 FILE_ReleaseFile( file );
1125 /**************************************************************************
1126 * SetEndOfFile (KERNEL32.483)
1128 BOOL32 WINAPI SetEndOfFile( HFILE32 hFile )
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 ) ))
1141 FILE_ReleaseFile( file );
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 );
1172 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1173 if (unlink( full_name.long_name ) == -1)
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 );
1194 /***********************************************************************
1197 BOOL32 FILE_SetFileType( HFILE32 hFile, DWORD type )
1199 FILE_OBJECT *file = FILE_GetFile( hFile );
1200 if (!file) return FALSE;
1202 FILE_ReleaseFile( file );
1207 /***********************************************************************
1210 LPVOID FILE_mmap( FILE_OBJECT *file, LPVOID start,
1211 DWORD size_high, DWORD size_low,
1212 DWORD offset_high, DWORD offset_low,
1213 int prot, int flags )
1217 if (size_high || offset_high)
1218 fprintf( stderr, "FILE_mmap: offsets larger than 4Gb not supported\n");
1225 static int fdzero = -1;
1229 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1231 perror( "/dev/zero: open" );
1236 #endif /* MAP_ANON */
1237 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1239 flags &= ~MAP_SHARED;
1242 flags |= MAP_PRIVATE;
1245 else fd = file->unix_handle;
1247 return mmap( start, size_low, prot, flags, fd, offset_low );
1251 /***********************************************************************
1252 * GetFileType (KERNEL32.222)
1254 DWORD WINAPI GetFileType( HFILE32 hFile )
1256 FILE_OBJECT *file = FILE_GetFile(hFile);
1257 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1258 FILE_ReleaseFile( file );
1263 /**************************************************************************
1264 * MoveFileEx32A (KERNEL32.???)
1268 BOOL32 WINAPI MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1270 DOS_FULL_NAME full_name1, full_name2;
1271 int mode=0; /* mode == 1: use copy */
1273 dprintf_file( stddeb, "MoveFileEx32A(%s,%s,%04lx)\n", fn1, fn2, flag);
1275 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1276 if (fn2) { /* !fn2 means delete fn1 */
1277 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1278 /* Source name and target path are valid */
1279 if ( full_name1.drive != full_name2.drive)
1280 /* use copy, if allowed */
1281 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1282 /* FIXME: Use right error code */
1283 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
1287 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1288 /* target exists, check if we may overwrite */
1289 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1290 /* FIXME: Use right error code */
1291 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1295 else /* fn2 == NULL means delete source */
1296 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1297 if (flag & MOVEFILE_COPY_ALLOWED) {
1299 "MoveFileEx32A: Illegal flag\n");
1300 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1304 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1305 Perhaps we should queue these command and execute it
1306 when exiting... What about using on_exit(2)
1308 fprintf( stderr,"MoveFileEx32A: Please delete file %s\n",
1309 full_name1.long_name);
1310 fprintf( stderr," when Wine has finished\n");
1311 fprintf( stderr," like \"rm %s\"\n",
1312 full_name1.long_name);
1315 else if (unlink( full_name1.long_name ) == -1)
1320 else return TRUE; /* successfully deleted */
1322 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1323 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1324 Perhaps we should queue these command and execute it
1325 when exiting... What about using on_exit(2)
1327 fprintf( stderr,"MoveFileEx32A: Please move existing file %s\n"
1328 ,full_name1.long_name);
1329 fprintf( stderr," to file %s\n"
1330 ,full_name2.long_name);
1331 fprintf( stderr," when Wine has finished\n");
1332 fprintf( stderr," like \" mv %s %s\"\n",
1333 full_name1.long_name,full_name2.long_name);
1337 if (!mode) /* move the file */
1338 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1344 else /* copy File */
1345 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1349 /**************************************************************************
1350 * MoveFileEx32W (KERNEL32.???)
1352 BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1354 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1355 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1356 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1357 HeapFree( GetProcessHeap(), 0, afn1 );
1358 HeapFree( GetProcessHeap(), 0, afn2 );
1363 /**************************************************************************
1364 * MoveFile32A (KERNEL32.387)
1366 * Move file or directory
1368 BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1370 DOS_FULL_NAME full_name1, full_name2;
1373 dprintf_file( stddeb, "MoveFile32A(%s,%s)\n", fn1, fn2 );
1375 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1376 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1377 /* The new name must not already exist */
1379 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1381 if (full_name1.drive == full_name2.drive) /* move */
1382 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1389 if (stat( full_name1.long_name, &fstat ))
1391 dprintf_file( stddeb, "Invalid source file %s\n",
1392 full_name1.long_name);
1396 if (S_ISDIR(fstat.st_mode)) {
1397 /* No Move for directories across file systems */
1398 /* FIXME: Use right error code */
1399 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1404 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1409 /**************************************************************************
1410 * MoveFile32W (KERNEL32.390)
1412 BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1414 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1415 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1416 BOOL32 res = MoveFile32A( afn1, afn2 );
1417 HeapFree( GetProcessHeap(), 0, afn1 );
1418 HeapFree( GetProcessHeap(), 0, afn2 );
1423 /**************************************************************************
1424 * CopyFile32A (KERNEL32.36)
1426 BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1429 BY_HANDLE_FILE_INFORMATION info;
1435 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1436 if (!GetFileInformationByHandle( h1, &info ))
1441 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1442 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1447 while ((count = _lread32( h2, buffer, sizeof(buffer) )) > 0)
1452 INT32 res = _lwrite32( h2, p, count );
1453 if (res <= 0) goto done;
1466 /**************************************************************************
1467 * CopyFile32W (KERNEL32.37)
1469 BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
1471 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1472 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1473 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1474 HeapFree( GetProcessHeap(), 0, sourceA );
1475 HeapFree( GetProcessHeap(), 0, destA );
1480 /***********************************************************************
1481 * SetFileTime (KERNEL32.493)
1483 BOOL32 WINAPI SetFileTime( HFILE32 hFile,
1484 const FILETIME *lpCreationTime,
1485 const FILETIME *lpLastAccessTime,
1486 const FILETIME *lpLastWriteTime )
1488 FILE_OBJECT *file = FILE_GetFile(hFile);
1489 struct utimbuf utimbuf;
1491 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1492 dprintf_file(stddeb,"SetFileTime(%s,%p,%p,%p)\n",
1498 if (lpLastAccessTime)
1499 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1501 utimbuf.actime = 0; /* FIXME */
1502 if (lpLastWriteTime)
1503 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1505 utimbuf.modtime = 0; /* FIXME */
1506 if (-1==utime(file->unix_name,&utimbuf))
1508 FILE_ReleaseFile( file );
1512 FILE_ReleaseFile( file );
1516 /* Locks need to be mirrored because unix file locking is based
1517 * on the pid. Inside of wine there can be multiple WINE processes
1518 * that share the same unix pid.
1519 * Read's and writes should check these locks also - not sure
1520 * how critical that is at this point (FIXME).
1523 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
1525 DOS_FILE_LOCK *curr;
1528 processId = GetCurrentProcessId();
1530 /* check if lock overlaps a current lock for the same file */
1531 for (curr = locks; curr; curr = curr->next) {
1532 if (strcmp(curr->unix_name, file->unix_name) == 0) {
1533 if ((f->l_start < (curr->base + curr->len)) &&
1534 ((f->l_start + f->l_len) > curr->base)) {
1535 /* region overlaps */
1541 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
1542 curr->processId = GetCurrentProcessId();
1543 curr->base = f->l_start;
1544 curr->len = f->l_len;
1545 curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);
1547 curr->dos_file = file;
1552 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
1555 DOS_FILE_LOCK **curr;
1558 processId = GetCurrentProcessId();
1561 if ((*curr)->dos_file == file) {
1563 *curr = (*curr)->next;
1564 HeapFree( SystemHeap, 0, rem->unix_name );
1565 HeapFree( SystemHeap, 0, rem );
1568 curr = &(*curr)->next;
1572 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
1575 DOS_FILE_LOCK **curr;
1578 processId = GetCurrentProcessId();
1579 for (curr = &locks; *curr; curr = &(*curr)->next) {
1580 if ((*curr)->processId == processId &&
1581 (*curr)->dos_file == file &&
1582 (*curr)->base == f->l_start &&
1583 (*curr)->len == f->l_len) {
1584 /* this is the same lock */
1586 *curr = (*curr)->next;
1587 HeapFree( SystemHeap, 0, rem->unix_name );
1588 HeapFree( SystemHeap, 0, rem );
1592 /* no matching lock found */
1597 /**************************************************************************
1598 * LockFile (KERNEL32.511)
1600 BOOL32 WINAPI LockFile(
1601 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1602 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
1607 dprintf_file(stddeb, "LockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1608 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1609 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
1611 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
1612 dprintf_file(stddeb, "LockFile32: Unimplemented bytes > 32bits\n");
1616 f.l_start = dwFileOffsetLow;
1617 f.l_len = nNumberOfBytesToLockLow;
1618 f.l_whence = SEEK_SET;
1622 if (!(file = FILE_GetFile(hFile))) return FALSE;
1624 /* shadow locks internally */
1625 if (!DOS_AddLock(file, &f)) {
1626 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1630 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1631 #ifdef USE_UNIX_LOCKS
1632 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
1633 if (errno == EACCES || errno == EAGAIN) {
1634 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
1639 /* remove our internal copy of the lock */
1640 DOS_RemoveLock(file, &f);
1648 /**************************************************************************
1649 * UnlockFile (KERNEL32.703)
1651 BOOL32 WINAPI UnlockFile(
1652 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
1653 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
1658 dprintf_file(stddeb, "UnlockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1659 hFile, dwFileOffsetLow, dwFileOffsetHigh,
1660 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
1662 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
1663 dprintf_file(stddeb, "UnlockFile32: Unimplemented bytes > 32bits\n");
1667 f.l_start = dwFileOffsetLow;
1668 f.l_len = nNumberOfBytesToUnlockLow;
1669 f.l_whence = SEEK_SET;
1673 if (!(file = FILE_GetFile(hFile))) return FALSE;
1675 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
1677 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1678 #ifdef USE_UNIX_LOCKS
1679 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {