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
42 /***********************************************************************
47 static HFILE32
FILE_Alloc( FILE_OBJECT
**file
)
50 *file
= HeapAlloc( SystemHeap
, 0, sizeof(FILE_OBJECT
) );
53 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
56 (*file
)->header
.type
= K32OBJ_FILE
;
57 (*file
)->header
.refcount
= 0;
58 (*file
)->unix_handle
= -1;
59 (*file
)->unix_name
= NULL
;
60 (*file
)->type
= FILE_TYPE_DISK
;
62 handle
= PROCESS_AllocHandle( &(*file
)->header
, 0 );
63 if (handle
== INVALID_HANDLE_VALUE32
) *file
= NULL
;
68 /***********************************************************************
73 void FILE_Destroy( K32OBJ
*ptr
)
75 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
76 assert( ptr
->type
== K32OBJ_FILE
);
78 if (file
->unix_handle
!= -1) close( file
->unix_handle
);
79 if (file
->unix_name
) HeapFree( SystemHeap
, 0, file
->unix_name
);
80 ptr
->type
= K32OBJ_UNKNOWN
;
81 HeapFree( SystemHeap
, 0, file
);
85 /***********************************************************************
88 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
89 * be called to release the file.
91 static FILE_OBJECT
*FILE_GetFile( HFILE32 handle
)
93 return (FILE_OBJECT
*)PROCESS_GetObjPtr( handle
, K32OBJ_FILE
);
97 /***********************************************************************
100 * Release a DOS file obtained with FILE_GetFile.
102 static void FILE_ReleaseFile( FILE_OBJECT
*file
)
104 K32OBJ_DecCount( &file
->header
);
108 /***********************************************************************
111 * Return the Unix handle associated to a file handle.
113 int FILE_GetUnixHandle( HFILE32 hFile
)
118 if (!(file
= FILE_GetFile( hFile
))) return -1;
119 ret
= file
->unix_handle
;
120 FILE_ReleaseFile( file
);
125 /***********************************************************************
128 * Set the DOS error code from errno.
130 void FILE_SetDosError(void)
132 dprintf_file(stddeb
, "FILE_SetDosError: errno = %d\n", errno
);
136 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
139 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
142 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
147 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
150 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
153 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
156 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
160 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
163 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
166 perror( "int21: unknown errno" );
167 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
173 /***********************************************************************
176 * Duplicate a Unix handle into a task handle.
178 HFILE32
FILE_DupUnixHandle( int fd
)
183 if ((handle
= FILE_Alloc( &file
)) != INVALID_HANDLE_VALUE32
)
185 if ((file
->unix_handle
= dup(fd
)) == -1)
188 CloseHandle( handle
);
189 return INVALID_HANDLE_VALUE32
;
196 /***********************************************************************
199 static HFILE32
FILE_OpenUnixFile( const char *name
, int mode
)
205 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
206 return INVALID_HANDLE_VALUE32
;
208 if ((file
->unix_handle
= open( name
, mode
, 0666 )) == -1)
210 if (!Options
.failReadOnly
&& (mode
== O_RDWR
))
211 file
->unix_handle
= open( name
, O_RDONLY
);
213 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
216 CloseHandle( handle
);
217 return INVALID_HANDLE_VALUE32
;
219 if (S_ISDIR(st
.st_mode
))
221 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
222 CloseHandle( handle
);
223 return INVALID_HANDLE_VALUE32
;
226 /* File opened OK, now fill the FILE_OBJECT */
228 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, name
);
233 /***********************************************************************
236 HFILE32
FILE_Open( LPCSTR path
, INT32 mode
)
238 DOS_FULL_NAME full_name
;
239 const char *unixName
;
241 dprintf_file(stddeb
, "FILE_Open: '%s' %04x\n", path
, mode
);
242 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
244 dprintf_file( stddeb
, "FILE_Open: opening device '%s'\n", unixName
);
245 if (!unixName
[0]) /* Non-existing device */
247 dprintf_file(stddeb
, "FILE_Open: Non-existing device\n");
248 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
249 return HFILE_ERROR32
;
252 else /* check for filename, don't check for last entry if creating */
254 if (!DOSFS_GetFullName( path
, !(mode
& O_CREAT
), &full_name
))
255 return HFILE_ERROR32
;
256 unixName
= full_name
.long_name
;
258 return FILE_OpenUnixFile( unixName
, mode
);
262 /***********************************************************************
265 static HFILE32
FILE_Create( LPCSTR path
, int mode
, int unique
)
269 const char *unixName
;
270 DOS_FULL_NAME full_name
;
272 dprintf_file(stddeb
, "FILE_Create: '%s' %04x %d\n", path
, mode
, unique
);
274 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
276 dprintf_file(stddeb
, "FILE_Create: creating device '%s'!\n", unixName
);
277 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
278 return INVALID_HANDLE_VALUE32
;
281 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
282 return INVALID_HANDLE_VALUE32
;
284 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
))
286 CloseHandle( handle
);
287 return INVALID_HANDLE_VALUE32
;
289 if ((file
->unix_handle
= open( full_name
.long_name
,
290 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
294 CloseHandle( handle
);
295 return INVALID_HANDLE_VALUE32
;
298 /* File created OK, now fill the FILE_OBJECT */
300 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, full_name
.long_name
);
305 /***********************************************************************
308 * Fill a file information from a struct stat.
310 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
312 if (S_ISDIR(st
->st_mode
))
313 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
315 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
316 if (!(st
->st_mode
& S_IWUSR
))
317 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
319 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
320 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
321 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
323 info
->dwVolumeSerialNumber
= 0; /* FIXME */
324 info
->nFileSizeHigh
= 0;
325 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
326 info
->nNumberOfLinks
= st
->st_nlink
;
327 info
->nFileIndexHigh
= 0;
328 info
->nFileIndexLow
= st
->st_ino
;
332 /***********************************************************************
335 * Stat a Unix path name. Return TRUE if OK.
337 BOOL32
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
341 if (stat( unixName
, &st
) == -1)
346 FILE_FillInfo( &st
, info
);
351 /***********************************************************************
352 * GetFileInformationByHandle (KERNEL32.219)
354 DWORD
GetFileInformationByHandle( HFILE32 hFile
,
355 BY_HANDLE_FILE_INFORMATION
*info
)
361 if (!(file
= FILE_GetFile( hFile
))) return 0;
362 if (fstat( file
->unix_handle
, &st
) == -1) FILE_SetDosError();
365 FILE_FillInfo( &st
, info
);
368 FILE_ReleaseFile( file
);
373 /**************************************************************************
374 * GetFileAttributes16 (KERNEL.420)
376 DWORD
GetFileAttributes16( LPCSTR name
)
378 return GetFileAttributes32A( name
);
382 /**************************************************************************
383 * GetFileAttributes32A (KERNEL32.217)
385 DWORD
GetFileAttributes32A( LPCSTR name
)
387 DOS_FULL_NAME full_name
;
388 BY_HANDLE_FILE_INFORMATION info
;
390 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
391 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
392 return info
.dwFileAttributes
;
396 /**************************************************************************
397 * GetFileAttributes32W (KERNEL32.218)
399 DWORD
GetFileAttributes32W( LPCWSTR name
)
401 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
402 DWORD res
= GetFileAttributes32A( nameA
);
403 HeapFree( GetProcessHeap(), 0, nameA
);
408 /***********************************************************************
409 * GetFileSize (KERNEL32.220)
411 DWORD
GetFileSize( HFILE32 hFile
, LPDWORD filesizehigh
)
413 BY_HANDLE_FILE_INFORMATION info
;
414 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
415 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
416 return info
.nFileSizeLow
;
420 /***********************************************************************
421 * GetFileTime (KERNEL32.221)
423 BOOL32
GetFileTime( HFILE32 hFile
, FILETIME
*lpCreationTime
,
424 FILETIME
*lpLastAccessTime
, FILETIME
*lpLastWriteTime
)
426 BY_HANDLE_FILE_INFORMATION info
;
427 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
428 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
429 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
430 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
434 /***********************************************************************
435 * CompareFileTime (KERNEL32.28)
437 INT32
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
439 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
441 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
443 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
445 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
450 /***********************************************************************
453 * dup() function for DOS handles.
455 HFILE32
FILE_Dup( HFILE32 hFile
)
460 dprintf_file( stddeb
, "FILE_Dup for handle %d\n", hFile
);
461 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR32
;
462 handle
= PROCESS_AllocHandle( &file
->header
, 0 );
463 FILE_ReleaseFile( file
);
464 dprintf_file( stddeb
, "FILE_Dup return handle %d\n", handle
);
469 /***********************************************************************
472 * dup2() function for DOS handles.
474 HFILE32
FILE_Dup2( HFILE32 hFile1
, HFILE32 hFile2
)
478 dprintf_file( stddeb
, "FILE_Dup2 for handle %d\n", hFile1
);
479 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR32
;
480 if (!PROCESS_SetObjPtr( hFile2
, &file
->header
, 0 )) hFile2
= HFILE_ERROR32
;
481 FILE_ReleaseFile( file
);
486 /***********************************************************************
487 * GetTempFileName16 (KERNEL.97)
489 UINT16
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
494 if ((drive
& TF_FORCEDRIVE
) &&
495 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
497 drive
&= ~TF_FORCEDRIVE
;
498 fprintf( stderr
, "Warning: GetTempFileName: invalid drive %d specified\n",
502 if (drive
& TF_FORCEDRIVE
)
503 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
506 GetTempPath32A( 132, temppath
);
507 strcat( temppath
, "\\" );
509 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
513 /***********************************************************************
514 * GetTempFileName32A (KERNEL32.290)
516 UINT32
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
519 DOS_FULL_NAME full_name
;
522 UINT32 num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
525 strcpy( buffer
, path
);
526 p
= buffer
+ strlen(buffer
);
527 /* add a \, if there isn't one ... */
528 if ((p
== buffer
) || (p
[-1] != '\\')) *p
++ = '\\';
530 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
531 sprintf( p
, "%04x.tmp", num
);
533 /* Now try to create it */
539 HFILE32 handle
= FILE_Create( buffer
, 0666, TRUE
);
540 if (handle
!= INVALID_HANDLE_VALUE32
)
541 { /* We created it */
542 dprintf_file( stddeb
, "GetTempFileName32A: created %s\n",
544 CloseHandle( handle
);
547 if (DOS_ExtendedError
!= ER_FileExists
)
548 break; /* No need to go on */
550 sprintf( p
, "%04x.tmp", num
);
551 } while (num
!= (unique
& 0xffff));
554 /* Get the full path name */
556 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
558 /* Check if we have write access in the directory */
559 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
560 if (access( full_name
.long_name
, W_OK
) == -1)
562 "Warning: GetTempFileName returns '%s', which doesn't seem to be writeable.\n"
563 "Please check your configuration file if this generates a failure.\n",
566 dprintf_file( stddeb
, "GetTempFileName32A: returning %s\n", buffer
);
567 return unique
? unique
: num
;
571 /***********************************************************************
572 * GetTempFileName32W (KERNEL32.291)
574 UINT32
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
582 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
583 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
584 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
585 lstrcpyAtoW( buffer
, buffera
);
586 HeapFree( GetProcessHeap(), 0, patha
);
587 HeapFree( GetProcessHeap(), 0, prefixa
);
592 /***********************************************************************
595 * Implementation of OpenFile16() and OpenFile32().
597 static HFILE32
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
,
602 WORD filedatetime
[2];
603 DOS_FULL_NAME full_name
;
607 ofs
->cBytes
= sizeof(OFSTRUCT
);
609 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
610 dprintf_file( stddeb
, "OpenFile: %s %04x\n", name
, mode
);
612 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
613 Are there any cases where getting the path here is wrong?
614 Uwe Bonnes 1997 Apr 2 */
615 if (!GetFullPathName32A( name
, sizeof(ofs
->szPathName
),
616 ofs
->szPathName
, NULL
)) goto error
;
618 /* OF_PARSE simply fills the structure */
622 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
624 dprintf_file( stddeb
, "OpenFile(%s): OF_PARSE, res = '%s'\n",
625 name
, ofs
->szPathName
);
629 /* OF_CREATE is completely different from all other options, so
632 if (mode
& OF_CREATE
)
634 if ((hFileRet
= FILE_Create(name
,0666,FALSE
))== INVALID_HANDLE_VALUE32
)
639 /* If OF_SEARCH is set, ignore the given path */
641 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
643 /* First try the file name as is */
644 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
645 /* Now remove the path */
646 if (name
[0] && (name
[1] == ':')) name
+= 2;
647 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
648 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
649 if (!name
[0]) goto not_found
;
652 /* Now look for the file */
654 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
657 dprintf_file( stddeb
, "OpenFile: found %s = %s\n",
658 full_name
.long_name
, full_name
.short_name
);
659 lstrcpyn32A( ofs
->szPathName
, full_name
.short_name
,
660 sizeof(ofs
->szPathName
) );
662 if (mode
& OF_DELETE
)
664 if (unlink( full_name
.long_name
) == -1) goto not_found
;
665 dprintf_file( stddeb
, "OpenFile(%s): OF_DELETE return = OK\n", name
);
672 unixMode
= O_WRONLY
; break;
674 unixMode
= O_RDWR
; break;
677 unixMode
= O_RDONLY
; break;
680 hFileRet
= FILE_OpenUnixFile( full_name
.long_name
, unixMode
);
681 if (hFileRet
== HFILE_ERROR32
) goto not_found
;
682 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
683 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
684 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
686 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
688 CloseHandle( hFileRet
);
689 dprintf_file( stddeb
, "OpenFile(%s): OF_VERIFY failed\n", name
);
690 /* FIXME: what error here? */
691 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
695 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
697 success
: /* We get here if the open was successful */
698 dprintf_file( stddeb
, "OpenFile(%s): OK, return = %d\n", name
, hFileRet
);
699 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
700 CloseHandle( hFileRet
);
703 not_found
: /* We get here if the file does not exist */
704 dprintf_file( stddeb
, "OpenFile: '%s' not found\n", name
);
705 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
708 error
: /* We get here if there was an error opening the file */
709 ofs
->nErrCode
= DOS_ExtendedError
;
710 dprintf_file( stddeb
, "OpenFile(%s): return = HFILE_ERROR error= %d\n",
711 name
,ofs
->nErrCode
);
712 return HFILE_ERROR32
;
716 /***********************************************************************
717 * OpenFile16 (KERNEL.74)
719 HFILE16
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
721 return FILE_DoOpenFile( name
, ofs
, mode
, FALSE
);
725 /***********************************************************************
726 * OpenFile32 (KERNEL32.396)
728 HFILE32
OpenFile32( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
730 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
734 /***********************************************************************
735 * _lclose16 (KERNEL.81)
737 HFILE16
_lclose16( HFILE16 hFile
)
739 dprintf_file( stddeb
, "_lclose16: handle %d\n", hFile
);
740 return CloseHandle( hFile
) ? 0 : HFILE_ERROR16
;
744 /***********************************************************************
745 * _lclose32 (KERNEL32.592)
747 HFILE32
_lclose32( HFILE32 hFile
)
749 dprintf_file( stddeb
, "_lclose32: handle %d\n", hFile
);
750 return CloseHandle( hFile
) ? 0 : HFILE_ERROR32
;
754 /***********************************************************************
757 LONG
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
761 dprintf_file( stddeb
, "WIN16_hread: %d %08lx %ld\n",
762 hFile
, (DWORD
)buffer
, count
);
764 /* Some programs pass a count larger than the allocated buffer */
765 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
766 if (count
> maxlen
) count
= maxlen
;
767 return _lread32( hFile
, PTR_SEG_TO_LIN(buffer
), count
);
771 /***********************************************************************
774 UINT16
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
776 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
780 /***********************************************************************
781 * _lread32 (KERNEL32.596)
783 UINT32
_lread32( HFILE32 hFile
, LPVOID buffer
, UINT32 count
)
788 dprintf_file( stddeb
, "_lread32: %d %p %d\n", hFile
, buffer
, count
);
789 if (!(file
= FILE_GetFile( hFile
))) return -1;
790 if (!count
) result
= 0;
791 else if ((result
= read( file
->unix_handle
, buffer
, count
)) == -1)
793 FILE_ReleaseFile( file
);
798 /***********************************************************************
799 * _lread16 (KERNEL.82)
801 UINT16
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
803 return (UINT16
)_lread32( hFile
, buffer
, (LONG
)count
);
807 /***********************************************************************
808 * _lcreat16 (KERNEL.83)
810 HFILE16
_lcreat16( LPCSTR path
, INT16 attr
)
812 int mode
= (attr
& 1) ? 0444 : 0666;
813 dprintf_file( stddeb
, "_lcreat16: %s %02x\n", path
, attr
);
814 return (HFILE16
)FILE_Create( path
, mode
, FALSE
);
818 /***********************************************************************
819 * _lcreat32 (KERNEL32.593)
821 HFILE32
_lcreat32( LPCSTR path
, INT32 attr
)
823 int mode
= (attr
& 1) ? 0444 : 0666;
824 dprintf_file( stddeb
, "_lcreat32: %s %02x\n", path
, attr
);
825 return FILE_Create( path
, mode
, FALSE
);
829 /***********************************************************************
830 * _lcreat_uniq (Not a Windows API)
832 HFILE32
_lcreat_uniq( LPCSTR path
, INT32 attr
)
834 int mode
= (attr
& 1) ? 0444 : 0666;
835 dprintf_file( stddeb
, "_lcreat_uniq: %s %02x\n", path
, attr
);
836 return FILE_Create( path
, mode
, TRUE
);
840 /***********************************************************************
841 * SetFilePointer (KERNEL32.492)
843 DWORD
SetFilePointer( HFILE32 hFile
, LONG distance
, LONG
*highword
,
849 if (highword
&& *highword
)
851 fprintf( stderr
, "SetFilePointer: 64-bit offsets not supported yet\n");
852 SetLastError( ERROR_INVALID_PARAMETER
);
855 dprintf_file( stddeb
, "SetFilePointer: handle %d offset %ld origin %ld\n",
856 hFile
, distance
, method
);
858 if (!(file
= FILE_GetFile( hFile
))) return 0xffffffff;
861 case FILE_CURRENT
: origin
= SEEK_CUR
; break;
862 case FILE_END
: origin
= SEEK_END
; break;
863 default: origin
= SEEK_SET
; break;
866 if ((result
= lseek( file
->unix_handle
, distance
, origin
)) == -1)
868 FILE_ReleaseFile( file
);
869 return (DWORD
)result
;
873 /***********************************************************************
874 * _llseek16 (KERNEL.84)
876 LONG
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
878 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
882 /***********************************************************************
883 * _llseek32 (KERNEL32.594)
885 LONG
_llseek32( HFILE32 hFile
, LONG lOffset
, INT32 nOrigin
)
887 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
891 /***********************************************************************
892 * _lopen16 (KERNEL.85)
894 HFILE16
_lopen16( LPCSTR path
, INT16 mode
)
896 return _lopen32( path
, mode
);
900 /***********************************************************************
901 * _lopen32 (KERNEL32.595)
903 HFILE32
_lopen32( LPCSTR path
, INT32 mode
)
907 dprintf_file(stddeb
, "_lopen32('%s',%04x)\n", path
, mode
);
912 unixMode
= O_WRONLY
| O_TRUNC
;
922 return FILE_Open( path
, unixMode
);
926 /***********************************************************************
927 * _lwrite16 (KERNEL.86)
929 UINT16
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
931 return (UINT16
)_hwrite32( hFile
, buffer
, (LONG
)count
);
934 /***********************************************************************
935 * _lwrite32 (KERNEL.86)
937 UINT32
_lwrite32( HFILE32 hFile
, LPCSTR buffer
, UINT32 count
)
939 return (UINT32
)_hwrite32( hFile
, buffer
, (LONG
)count
);
943 /***********************************************************************
944 * _hread16 (KERNEL.349)
946 LONG
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
948 return _lread32( hFile
, buffer
, count
);
952 /***********************************************************************
953 * _hread32 (KERNEL32.590)
955 LONG
_hread32( HFILE32 hFile
, LPVOID buffer
, LONG count
)
957 return _lread32( hFile
, buffer
, count
);
961 /***********************************************************************
962 * _hwrite16 (KERNEL.350)
964 LONG
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
966 return _hwrite32( hFile
, buffer
, count
);
970 /***********************************************************************
971 * _hwrite32 (KERNEL32.591)
973 LONG
_hwrite32( HFILE32 hFile
, LPCSTR buffer
, LONG count
)
978 dprintf_file( stddeb
, "_hwrite32: %d %p %ld\n", hFile
, buffer
, count
);
980 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR32
;
981 if (count
== 0) /* Expand or truncate at current position */
982 result
= ftruncate( file
->unix_handle
,
983 lseek( file
->unix_handle
, 0, SEEK_CUR
) );
985 result
= write( file
->unix_handle
, buffer
, count
);
987 if (result
== -1) FILE_SetDosError();
988 FILE_ReleaseFile( file
);
993 /***********************************************************************
994 * SetHandleCount16 (KERNEL.199)
996 UINT16
SetHandleCount16( UINT16 count
)
998 HGLOBAL16 hPDB
= GetCurrentPDB();
999 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1000 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1002 dprintf_file( stddeb
, "SetHandleCount16(%d)\n", count
);
1004 if (count
< 20) count
= 20; /* No point in going below 20 */
1005 else if (count
> 254) count
= 254;
1009 if (pdb
->nbFiles
> 20)
1011 memcpy( pdb
->fileHandles
, files
, 20 );
1012 GlobalFree16( pdb
->hFileHandles
);
1013 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1014 GlobalHandleToSel( hPDB
) );
1015 pdb
->hFileHandles
= 0;
1019 else /* More than 20, need a new file handles table */
1022 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1025 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1026 return pdb
->nbFiles
;
1028 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1030 if (count
> pdb
->nbFiles
)
1032 memcpy( newfiles
, files
, pdb
->nbFiles
);
1033 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1035 else memcpy( newfiles
, files
, count
);
1036 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1037 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1038 pdb
->hFileHandles
= newhandle
;
1039 pdb
->nbFiles
= count
;
1041 return pdb
->nbFiles
;
1045 /*************************************************************************
1046 * SetHandleCount32 (KERNEL32.494)
1048 UINT32
SetHandleCount32( UINT32 count
)
1050 return MIN( 256, count
);
1054 /***********************************************************************
1055 * FlushFileBuffers (KERNEL32.133)
1057 BOOL32
FlushFileBuffers( HFILE32 hFile
)
1062 dprintf_file( stddeb
, "FlushFileBuffers(%d)\n", hFile
);
1063 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1064 if (fsync( file
->unix_handle
) != -1) ret
= TRUE
;
1070 FILE_ReleaseFile( file
);
1075 /**************************************************************************
1076 * SetEndOfFile (KERNEL32.483)
1078 BOOL32
SetEndOfFile( HFILE32 hFile
)
1083 dprintf_file( stddeb
, "SetEndOfFile(%d)\n", hFile
);
1084 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1085 if (ftruncate( file
->unix_handle
,
1086 lseek( file
->unix_handle
, 0, SEEK_CUR
) ))
1091 FILE_ReleaseFile( file
);
1096 /***********************************************************************
1097 * DeleteFile16 (KERNEL.146)
1099 BOOL16
DeleteFile16( LPCSTR path
)
1101 return DeleteFile32A( path
);
1105 /***********************************************************************
1106 * DeleteFile32A (KERNEL32.71)
1108 BOOL32
DeleteFile32A( LPCSTR path
)
1110 DOS_FULL_NAME full_name
;
1111 const char *unixName
;
1113 dprintf_file(stddeb
, "DeleteFile: '%s'\n", path
);
1115 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
1117 dprintf_file(stddeb
, "DeleteFile: removing device '%s'!\n", unixName
);
1118 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1122 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1123 if (unlink( full_name
.long_name
) == -1)
1132 /***********************************************************************
1133 * DeleteFile32W (KERNEL32.72)
1135 BOOL32
DeleteFile32W( LPCWSTR path
)
1137 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1138 BOOL32 ret
= RemoveDirectory32A( xpath
);
1139 HeapFree( GetProcessHeap(), 0, xpath
);
1144 /***********************************************************************
1147 BOOL32
FILE_SetFileType( HFILE32 hFile
, DWORD type
)
1149 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1150 if (!file
) return FALSE
;
1152 FILE_ReleaseFile( file
);
1157 /***********************************************************************
1160 LPVOID
FILE_mmap( FILE_OBJECT
*file
, LPVOID start
,
1161 DWORD size_high
, DWORD size_low
,
1162 DWORD offset_high
, DWORD offset_low
,
1163 int prot
, int flags
)
1167 if (size_high
|| offset_high
)
1168 fprintf( stderr
, "FILE_mmap: offsets larger than 4Gb not supported\n");
1172 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1173 flags
&= ~MAP_SHARED
;
1174 flags
|= MAP_PRIVATE
;
1178 static int fdzero
= -1;
1182 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1184 perror( "/dev/zero: open" );
1189 #endif /* MAP_ANON */
1191 else fd
= file
->unix_handle
;
1193 return mmap( start
, size_low
, prot
, flags
, fd
, offset_low
);
1197 /***********************************************************************
1198 * GetFileType (KERNEL32.222)
1200 DWORD
GetFileType( HFILE32 hFile
)
1202 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1203 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1204 FILE_ReleaseFile( file
);
1209 /**************************************************************************
1210 * MoveFile32A (KERNEL32.387)
1212 BOOL32
MoveFile32A( LPCSTR fn1
, LPCSTR fn2
)
1214 DOS_FULL_NAME full_name1
, full_name2
;
1216 dprintf_file( stddeb
, "MoveFile32A(%s,%s)\n", fn1
, fn2
);
1218 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1219 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1220 /* FIXME: should not replace an existing file */
1221 /* FIXME: should handle renaming across devices */
1222 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1231 /**************************************************************************
1232 * MoveFile32W (KERNEL32.390)
1234 BOOL32
MoveFile32W( LPCWSTR fn1
, LPCWSTR fn2
)
1236 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1237 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1238 BOOL32 res
= MoveFile32A( afn1
, afn2
);
1239 HeapFree( GetProcessHeap(), 0, afn1
);
1240 HeapFree( GetProcessHeap(), 0, afn2
);
1245 /**************************************************************************
1246 * CopyFile32A (KERNEL32.36)
1248 BOOL32
CopyFile32A( LPCSTR source
, LPCSTR dest
, BOOL32 fail_if_exists
)
1251 BY_HANDLE_FILE_INFORMATION info
;
1257 if ((h1
= _lopen32( source
, OF_READ
)) == HFILE_ERROR32
) return FALSE
;
1258 if (!GetFileInformationByHandle( h1
, &info
))
1263 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1264 if ((h2
= FILE_Create( dest
, mode
, fail_if_exists
)) == HFILE_ERROR32
)
1269 while ((count
= _lread32( h2
, buffer
, sizeof(buffer
) )) > 0)
1274 INT32 res
= _lwrite32( h2
, p
, count
);
1275 if (res
<= 0) goto done
;
1288 /**************************************************************************
1289 * CopyFile32W (KERNEL32.37)
1291 BOOL32
CopyFile32W( LPCWSTR source
, LPCWSTR dest
, BOOL32 fail_if_exists
)
1293 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1294 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1295 BOOL32 ret
= CopyFile32A( sourceA
, destA
, fail_if_exists
);
1296 HeapFree( GetProcessHeap(), 0, sourceA
);
1297 HeapFree( GetProcessHeap(), 0, destA
);
1302 /***********************************************************************
1303 * SetFileTime (KERNEL32.493)
1305 BOOL32
SetFileTime( HFILE32 hFile
,
1306 const FILETIME
*lpCreationTime
,
1307 const FILETIME
*lpLastAccessTime
,
1308 const FILETIME
*lpLastWriteTime
)
1310 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1311 struct utimbuf utimbuf
;
1313 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1314 dprintf_file(stddeb
,"SetFileTime(%s,%p,%p,%p)\n",
1320 if (lpLastAccessTime
)
1321 utimbuf
.actime
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1323 utimbuf
.actime
= 0; /* FIXME */
1324 if (lpLastWriteTime
)
1325 utimbuf
.modtime
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1327 utimbuf
.modtime
= 0; /* FIXME */
1328 if (-1==utime(file
->unix_name
,&utimbuf
))
1330 FILE_ReleaseFile( file
);
1334 FILE_ReleaseFile( file
);