2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
14 #include <sys/errno.h>
15 #include <sys/types.h>
35 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
36 #define MAP_ANON MAP_ANONYMOUS
39 static BOOL32
FILE_Read(K32OBJ
*ptr
, LPVOID lpBuffer
, DWORD nNumberOfChars
,
40 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
);
41 static BOOL32
FILE_Write(K32OBJ
*ptr
, LPCVOID lpBuffer
, DWORD nNumberOfChars
,
42 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
);
43 static void FILE_Destroy( K32OBJ
*obj
);
45 const K32OBJ_OPS FILE_Ops
=
47 /* Object cannot be waited upon (FIXME: for now) */
51 NULL
, /* remove_wait */
53 FILE_Write
, /* write */
54 FILE_Destroy
/* destroy */
57 struct DOS_FILE_LOCK
{
58 struct DOS_FILE_LOCK
* next
;
62 FILE_OBJECT
* dos_file
;
66 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
68 static DOS_FILE_LOCK
*locks
= NULL
;
69 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
71 /***********************************************************************
76 HFILE32
FILE_Alloc( FILE_OBJECT
**file
)
79 *file
= HeapAlloc( SystemHeap
, 0, sizeof(FILE_OBJECT
) );
82 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
85 (*file
)->header
.type
= K32OBJ_FILE
;
86 (*file
)->header
.refcount
= 0;
87 (*file
)->unix_handle
= -1;
88 (*file
)->unix_name
= NULL
;
89 (*file
)->type
= FILE_TYPE_DISK
;
91 handle
= HANDLE_Alloc( PROCESS_Current(), &(*file
)->header
,
92 FILE_ALL_ACCESS
| GENERIC_READ
|
93 GENERIC_WRITE
| GENERIC_EXECUTE
/*FIXME*/, TRUE
);
94 /* If the allocation failed, the object is already destroyed */
95 if (handle
== INVALID_HANDLE_VALUE32
) *file
= NULL
;
100 /* FIXME: lpOverlapped is ignored */
101 static BOOL32
FILE_Read(K32OBJ
*ptr
, LPVOID lpBuffer
, DWORD nNumberOfChars
,
102 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
104 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
107 TRACE(file
, "%p %p %ld\n", ptr
, lpBuffer
,
110 if (nNumberOfChars
== 0) {
111 *lpNumberOfChars
= 0; /* FIXME: does this change */
115 if ((result
= read(file
->unix_handle
, lpBuffer
, nNumberOfChars
)) == -1)
120 *lpNumberOfChars
= result
;
125 * experimentation yields that WriteFile:
126 * o does not truncate on write of 0
127 * o always changes the *lpNumberOfChars to actual number of
129 * o write of 0 nNumberOfChars returns TRUE
131 static BOOL32
FILE_Write(K32OBJ
*ptr
, LPCVOID lpBuffer
, DWORD nNumberOfChars
,
132 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
134 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
137 TRACE(file
, "%p %p %ld\n", ptr
, lpBuffer
,
140 *lpNumberOfChars
= 0;
143 * I assume this loop around EAGAIN is here because
144 * win32 doesn't have interrupted system calls
149 result
= write(file
->unix_handle
, lpBuffer
, nNumberOfChars
);
151 *lpNumberOfChars
= result
;
154 if (errno
!= EINTR
) {
163 /***********************************************************************
166 * Destroy a DOS file.
168 static void FILE_Destroy( K32OBJ
*ptr
)
170 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
171 assert( ptr
->type
== K32OBJ_FILE
);
173 DOS_RemoveFileLocks(file
);
175 if (file
->unix_handle
!= -1) close( file
->unix_handle
);
176 if (file
->unix_name
) HeapFree( SystemHeap
, 0, file
->unix_name
);
177 ptr
->type
= K32OBJ_UNKNOWN
;
178 HeapFree( SystemHeap
, 0, file
);
182 /***********************************************************************
185 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
186 * be called to release the file.
188 FILE_OBJECT
*FILE_GetFile( HFILE32 handle
)
190 return (FILE_OBJECT
*)HANDLE_GetObjPtr( PROCESS_Current(), handle
,
191 K32OBJ_FILE
, 0 /*FIXME*/ );
195 /***********************************************************************
198 * Release a DOS file obtained with FILE_GetFile.
200 void FILE_ReleaseFile( FILE_OBJECT
*file
)
202 K32OBJ_DecCount( &file
->header
);
206 /***********************************************************************
209 * Return the Unix handle associated to a file handle.
211 int FILE_GetUnixHandle( HFILE32 hFile
)
216 if (!(file
= FILE_GetFile( hFile
))) return -1;
217 ret
= file
->unix_handle
;
218 FILE_ReleaseFile( file
);
223 /***********************************************************************
226 * Set the DOS error code from errno.
228 void FILE_SetDosError(void)
230 int save_errno
= errno
; /* errno gets overwritten by printf */
232 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
236 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
239 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
242 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
247 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
250 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
253 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
256 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
260 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
263 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
267 DOS_ERROR( ER_SeekError
, EC_NotFound
, SA_Ignore
, EL_Disk
);
270 DOS_ERROR( ERROR_DIR_NOT_EMPTY
, EC_Exists
, SA_Ignore
, EL_Disk
);
273 perror( "int21: unknown errno" );
274 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
281 /***********************************************************************
284 * Duplicate a Unix handle into a task handle.
286 HFILE32
FILE_DupUnixHandle( int fd
)
291 if ((handle
= FILE_Alloc( &file
)) != INVALID_HANDLE_VALUE32
)
293 if ((file
->unix_handle
= dup(fd
)) == -1)
296 CloseHandle( handle
);
297 return INVALID_HANDLE_VALUE32
;
304 /***********************************************************************
307 HFILE32
FILE_OpenUnixFile( const char *name
, int mode
)
313 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
314 return INVALID_HANDLE_VALUE32
;
316 if ((file
->unix_handle
= open( name
, mode
, 0666 )) == -1)
318 if (!Options
.failReadOnly
&& (mode
== O_RDWR
))
319 file
->unix_handle
= open( name
, O_RDONLY
);
321 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
324 CloseHandle( handle
);
325 return INVALID_HANDLE_VALUE32
;
327 if (S_ISDIR(st
.st_mode
))
329 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
330 CloseHandle( handle
);
331 return INVALID_HANDLE_VALUE32
;
334 /* File opened OK, now fill the FILE_OBJECT */
336 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, name
);
341 /***********************************************************************
344 HFILE32
FILE_Open( LPCSTR path
, INT32 mode
)
346 DOS_FULL_NAME full_name
;
347 const char *unixName
;
349 TRACE(file
, "'%s' %04x\n", path
, mode
);
351 if (!path
) return HFILE_ERROR32
;
353 if (DOSFS_GetDevice( path
))
357 TRACE(file
, "opening device '%s'\n", path
);
359 if (HFILE_ERROR32
!=(ret
=DOSFS_OpenDevice( path
, mode
)))
362 /* Do not silence this please. It is a critical error. -MM */
363 ERR(file
, "Couldn't open device '%s'!\n",path
);
364 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
365 return HFILE_ERROR32
;
368 else /* check for filename, don't check for last entry if creating */
370 if (!DOSFS_GetFullName( path
, !(mode
& O_CREAT
), &full_name
))
371 return HFILE_ERROR32
;
372 unixName
= full_name
.long_name
;
374 return FILE_OpenUnixFile( unixName
, mode
);
378 /***********************************************************************
381 static HFILE32
FILE_Create( LPCSTR path
, int mode
, int unique
)
385 DOS_FULL_NAME full_name
;
387 TRACE(file
, "'%s' %04x %d\n", path
, mode
, unique
);
389 if (!path
) return INVALID_HANDLE_VALUE32
;
391 if (DOSFS_GetDevice( path
))
393 WARN(file
, "cannot create DOS device '%s'!\n", path
);
394 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
395 return INVALID_HANDLE_VALUE32
;
398 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
399 return INVALID_HANDLE_VALUE32
;
401 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
))
403 CloseHandle( handle
);
404 return INVALID_HANDLE_VALUE32
;
406 if ((file
->unix_handle
= open( full_name
.long_name
,
407 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
411 CloseHandle( handle
);
412 return INVALID_HANDLE_VALUE32
;
415 /* File created OK, now fill the FILE_OBJECT */
417 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, full_name
.long_name
);
422 /***********************************************************************
425 * Fill a file information from a struct stat.
427 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
429 if (S_ISDIR(st
->st_mode
))
430 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
432 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
433 if (!(st
->st_mode
& S_IWUSR
))
434 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
436 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
437 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
438 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
440 info
->dwVolumeSerialNumber
= 0; /* FIXME */
441 info
->nFileSizeHigh
= 0;
442 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
443 info
->nNumberOfLinks
= st
->st_nlink
;
444 info
->nFileIndexHigh
= 0;
445 info
->nFileIndexLow
= st
->st_ino
;
449 /***********************************************************************
452 * Stat a Unix path name. Return TRUE if OK.
454 BOOL32
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
458 if (!unixName
|| !info
) return FALSE
;
460 if (stat( unixName
, &st
) == -1)
465 FILE_FillInfo( &st
, info
);
470 /***********************************************************************
471 * GetFileInformationByHandle (KERNEL32.219)
473 DWORD WINAPI
GetFileInformationByHandle( HFILE32 hFile
,
474 BY_HANDLE_FILE_INFORMATION
*info
)
482 if (!(file
= FILE_GetFile( hFile
))) return 0;
483 if (fstat( file
->unix_handle
, &st
) == -1) FILE_SetDosError();
486 FILE_FillInfo( &st
, info
);
489 FILE_ReleaseFile( file
);
494 /**************************************************************************
495 * GetFileAttributes16 (KERNEL.420)
497 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
499 return GetFileAttributes32A( name
);
503 /**************************************************************************
504 * GetFileAttributes32A (KERNEL32.217)
506 DWORD WINAPI
GetFileAttributes32A( LPCSTR name
)
508 DOS_FULL_NAME full_name
;
509 BY_HANDLE_FILE_INFORMATION info
;
511 if (name
== NULL
) return -1;
513 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
514 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
515 return info
.dwFileAttributes
;
519 /**************************************************************************
520 * GetFileAttributes32W (KERNEL32.218)
522 DWORD WINAPI
GetFileAttributes32W( LPCWSTR name
)
524 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
525 DWORD res
= GetFileAttributes32A( nameA
);
526 HeapFree( GetProcessHeap(), 0, nameA
);
531 /***********************************************************************
532 * GetFileSize (KERNEL32.220)
534 DWORD WINAPI
GetFileSize( HFILE32 hFile
, LPDWORD filesizehigh
)
536 BY_HANDLE_FILE_INFORMATION info
;
537 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
538 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
539 return info
.nFileSizeLow
;
543 /***********************************************************************
544 * GetFileTime (KERNEL32.221)
546 BOOL32 WINAPI
GetFileTime( HFILE32 hFile
, FILETIME
*lpCreationTime
,
547 FILETIME
*lpLastAccessTime
,
548 FILETIME
*lpLastWriteTime
)
550 BY_HANDLE_FILE_INFORMATION info
;
551 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
552 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
553 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
554 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
558 /***********************************************************************
559 * CompareFileTime (KERNEL32.28)
561 INT32 WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
563 if (!x
|| !y
) return -1;
565 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
567 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
569 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
571 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
576 /***********************************************************************
579 * dup() function for DOS handles.
581 HFILE32
FILE_Dup( HFILE32 hFile
)
585 TRACE(file
, "FILE_Dup for handle %d\n", hFile
);
586 if (!DuplicateHandle( GetCurrentProcess(), hFile
, GetCurrentProcess(),
587 &handle
, FILE_ALL_ACCESS
/* FIXME */, FALSE
, 0 ))
588 handle
= HFILE_ERROR32
;
589 TRACE(file
, "FILE_Dup return handle %d\n", handle
);
594 /***********************************************************************
597 * dup2() function for DOS handles.
599 HFILE32
FILE_Dup2( HFILE32 hFile1
, HFILE32 hFile2
)
603 TRACE(file
, "FILE_Dup2 for handle %d\n", hFile1
);
604 /* FIXME: should use DuplicateHandle */
605 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR32
;
606 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2
, &file
->header
, 0 ))
607 hFile2
= HFILE_ERROR32
;
608 FILE_ReleaseFile( file
);
613 /***********************************************************************
614 * GetTempFileName16 (KERNEL.97)
616 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
621 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
622 drive
|= DRIVE_GetCurrentDrive() + 'A';
624 if ((drive
& TF_FORCEDRIVE
) &&
625 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
627 drive
&= ~TF_FORCEDRIVE
;
628 WARN(file
, "invalid drive %d specified\n", drive
);
631 if (drive
& TF_FORCEDRIVE
)
632 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
634 GetTempPath32A( 132, temppath
);
635 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
639 /***********************************************************************
640 * GetTempFileName32A (KERNEL32.290)
642 UINT32 WINAPI
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
645 DOS_FULL_NAME full_name
;
648 UINT32 num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
650 if ( !path
|| !prefix
|| !buffer
) return 0;
652 strcpy( buffer
, path
);
653 p
= buffer
+ strlen(buffer
);
655 /* add a \, if there isn't one and path is more than just the drive letter ... */
656 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
657 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
660 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
661 sprintf( p
, "%04x.tmp", num
);
663 /* Now try to create it */
669 HFILE32 handle
= FILE_Create( buffer
, 0666, TRUE
);
670 if (handle
!= INVALID_HANDLE_VALUE32
)
671 { /* We created it */
672 TRACE(file
, "created %s\n",
674 CloseHandle( handle
);
677 if (DOS_ExtendedError
!= ER_FileExists
)
678 break; /* No need to go on */
680 sprintf( p
, "%04x.tmp", num
);
681 } while (num
!= (unique
& 0xffff));
684 /* Get the full path name */
686 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
688 /* Check if we have write access in the directory */
689 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
690 if (access( full_name
.long_name
, W_OK
) == -1)
691 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
694 TRACE(file
, "returning %s\n", buffer
);
695 return unique
? unique
: num
;
699 /***********************************************************************
700 * GetTempFileName32W (KERNEL32.291)
702 UINT32 WINAPI
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
710 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
711 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
712 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
713 lstrcpyAtoW( buffer
, buffera
);
714 HeapFree( GetProcessHeap(), 0, patha
);
715 HeapFree( GetProcessHeap(), 0, prefixa
);
720 /***********************************************************************
723 * Implementation of OpenFile16() and OpenFile32().
725 static HFILE32
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
,
730 WORD filedatetime
[2];
731 DOS_FULL_NAME full_name
;
735 if (!ofs
) return HFILE_ERROR32
;
738 ofs
->cBytes
= sizeof(OFSTRUCT
);
740 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
743 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
744 return HFILE_ERROR32
;
747 TRACE(file
, "%s %04x\n", name
, mode
);
749 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
750 Are there any cases where getting the path here is wrong?
751 Uwe Bonnes 1997 Apr 2 */
752 if (!GetFullPathName32A( name
, sizeof(ofs
->szPathName
),
753 ofs
->szPathName
, NULL
)) goto error
;
755 /* OF_PARSE simply fills the structure */
759 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
761 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
762 name
, ofs
->szPathName
);
766 /* OF_CREATE is completely different from all other options, so
769 if (mode
& OF_CREATE
)
771 if ((hFileRet
= FILE_Create(name
,0666,FALSE
))== INVALID_HANDLE_VALUE32
)
776 /* If OF_SEARCH is set, ignore the given path */
778 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
780 /* First try the file name as is */
781 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
782 /* Now remove the path */
783 if (name
[0] && (name
[1] == ':')) name
+= 2;
784 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
785 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
786 if (!name
[0]) goto not_found
;
789 /* Now look for the file */
791 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
794 TRACE(file
, "found %s = %s\n",
795 full_name
.long_name
, full_name
.short_name
);
796 lstrcpyn32A( ofs
->szPathName
, full_name
.short_name
,
797 sizeof(ofs
->szPathName
) );
799 if (mode
& OF_DELETE
)
801 if (unlink( full_name
.long_name
) == -1) goto not_found
;
802 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
809 unixMode
= O_WRONLY
; break;
811 unixMode
= O_RDWR
; break;
814 unixMode
= O_RDONLY
; break;
817 hFileRet
= FILE_OpenUnixFile( full_name
.long_name
, unixMode
);
818 if (hFileRet
== HFILE_ERROR32
) goto not_found
;
819 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
820 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
821 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
823 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
825 CloseHandle( hFileRet
);
826 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
827 /* FIXME: what error here? */
828 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
832 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
834 success
: /* We get here if the open was successful */
835 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
836 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
837 CloseHandle( hFileRet
);
840 not_found
: /* We get here if the file does not exist */
841 WARN(file
, "'%s' not found\n", name
);
842 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
845 error
: /* We get here if there was an error opening the file */
846 ofs
->nErrCode
= DOS_ExtendedError
;
847 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
848 name
,ofs
->nErrCode
);
849 return HFILE_ERROR32
;
853 /***********************************************************************
854 * OpenFile16 (KERNEL.74)
856 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
858 return FILE_DoOpenFile( name
, ofs
, mode
, FALSE
);
862 /***********************************************************************
863 * OpenFile32 (KERNEL32.396)
865 HFILE32 WINAPI
OpenFile32( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
867 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
871 /***********************************************************************
872 * _lclose16 (KERNEL.81)
874 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
876 TRACE(file
, "handle %d\n", hFile
);
877 return CloseHandle( hFile
) ? 0 : HFILE_ERROR16
;
881 /***********************************************************************
882 * _lclose32 (KERNEL32.592)
884 HFILE32 WINAPI
_lclose32( HFILE32 hFile
)
886 TRACE(file
, "handle %d\n", hFile
);
887 return CloseHandle( hFile
) ? 0 : HFILE_ERROR32
;
891 /***********************************************************************
894 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
898 TRACE(file
, "%d %08lx %ld\n",
899 hFile
, (DWORD
)buffer
, count
);
901 /* Some programs pass a count larger than the allocated buffer */
902 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
903 if (count
> maxlen
) count
= maxlen
;
904 return _lread32( hFile
, PTR_SEG_TO_LIN(buffer
), count
);
908 /***********************************************************************
911 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
913 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
917 /***********************************************************************
918 * _lread32 (KERNEL32.596)
920 UINT32 WINAPI
_lread32( HFILE32 handle
, LPVOID buffer
, UINT32 count
)
924 BOOL32 result
= FALSE
;
926 TRACE( file
, "%d %p %d\n", handle
, buffer
, count
);
927 if (!(ptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
928 K32OBJ_UNKNOWN
, 0))) return -1;
929 if (K32OBJ_OPS(ptr
)->read
)
930 result
= K32OBJ_OPS(ptr
)->read(ptr
, buffer
, count
, &numWritten
, NULL
);
931 K32OBJ_DecCount( ptr
);
932 if (!result
) return -1;
933 return (UINT32
)numWritten
;
937 /***********************************************************************
938 * _lread16 (KERNEL.82)
940 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
942 return (UINT16
)_lread32( hFile
, buffer
, (LONG
)count
);
946 /***********************************************************************
947 * _lcreat16 (KERNEL.83)
949 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
951 int mode
= (attr
& 1) ? 0444 : 0666;
952 TRACE(file
, "%s %02x\n", path
, attr
);
953 return (HFILE16
)FILE_Create( path
, mode
, FALSE
);
957 /***********************************************************************
958 * _lcreat32 (KERNEL32.593)
960 HFILE32 WINAPI
_lcreat32( LPCSTR path
, INT32 attr
)
962 int mode
= (attr
& 1) ? 0444 : 0666;
963 TRACE(file
, "%s %02x\n", path
, attr
);
964 return FILE_Create( path
, mode
, FALSE
);
968 /***********************************************************************
969 * _lcreat_uniq (Not a Windows API)
971 HFILE32
_lcreat_uniq( LPCSTR path
, INT32 attr
)
973 int mode
= (attr
& 1) ? 0444 : 0666;
974 TRACE(file
, "%s %02x\n", path
, attr
);
975 return FILE_Create( path
, mode
, TRUE
);
979 /***********************************************************************
980 * SetFilePointer (KERNEL32.492)
982 DWORD WINAPI
SetFilePointer( HFILE32 hFile
, LONG distance
, LONG
*highword
,
988 if (highword
&& *highword
)
990 FIXME(file
, "64-bit offsets not supported yet\n");
991 SetLastError( ERROR_INVALID_PARAMETER
);
994 TRACE(file
, "handle %d offset %ld origin %ld\n",
995 hFile
, distance
, method
);
997 if (!(file
= FILE_GetFile( hFile
))) return 0xffffffff;
1000 case FILE_CURRENT
: origin
= SEEK_CUR
; break;
1001 case FILE_END
: origin
= SEEK_END
; break;
1002 default: origin
= SEEK_SET
; break;
1005 if ((result
= lseek( file
->unix_handle
, distance
, origin
)) == -1)
1007 /* care for this pathological case:
1008 SetFilePointer(00000006,ffffffff,00000000,00000002)
1009 ret=0062ab4a fs=01c7 */
1010 if ((distance
!= -1))
1015 FILE_ReleaseFile( file
);
1016 return (DWORD
)result
;
1020 /***********************************************************************
1021 * _llseek16 (KERNEL.84)
1023 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1025 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1029 /***********************************************************************
1030 * _llseek32 (KERNEL32.594)
1032 LONG WINAPI
_llseek32( HFILE32 hFile
, LONG lOffset
, INT32 nOrigin
)
1034 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1038 /***********************************************************************
1039 * _lopen16 (KERNEL.85)
1041 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1043 return _lopen32( path
, mode
);
1047 /***********************************************************************
1048 * _lopen32 (KERNEL32.595)
1050 HFILE32 WINAPI
_lopen32( LPCSTR path
, INT32 mode
)
1054 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1059 unixMode
= O_WRONLY
;
1066 unixMode
= O_RDONLY
;
1069 return FILE_Open( path
, unixMode
);
1073 /***********************************************************************
1074 * _lwrite16 (KERNEL.86)
1076 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1078 return (UINT16
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1081 /***********************************************************************
1082 * _lwrite32 (KERNEL.86)
1084 UINT32 WINAPI
_lwrite32( HFILE32 hFile
, LPCSTR buffer
, UINT32 count
)
1086 return (UINT32
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1090 /***********************************************************************
1091 * _hread16 (KERNEL.349)
1093 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1095 return _lread32( hFile
, buffer
, count
);
1099 /***********************************************************************
1100 * _hread32 (KERNEL32.590)
1102 LONG WINAPI
_hread32( HFILE32 hFile
, LPVOID buffer
, LONG count
)
1104 return _lread32( hFile
, buffer
, count
);
1108 /***********************************************************************
1109 * _hwrite16 (KERNEL.350)
1111 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1113 return _hwrite32( hFile
, buffer
, count
);
1117 /***********************************************************************
1118 * _hwrite32 (KERNEL32.591)
1120 * experimenation yields that _lwrite:
1121 * o truncates the file at the current position with
1123 * o returns 0 on a 0 length write
1124 * o works with console handles
1127 LONG WINAPI
_hwrite32( HFILE32 handle
, LPCSTR buffer
, LONG count
)
1131 BOOL32 status
= FALSE
;
1133 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1135 if (count
== 0) { /* Expand or truncate at current position */
1136 FILE_OBJECT
*file
= FILE_GetFile(handle
);
1138 if ( ftruncate(file
->unix_handle
,
1139 lseek( file
->unix_handle
, 0, SEEK_CUR
)) == 0 ) {
1140 FILE_ReleaseFile(file
);
1144 FILE_ReleaseFile(file
);
1145 return HFILE_ERROR32
;
1149 if (!(ioptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
1150 K32OBJ_UNKNOWN
, 0 )))
1151 return HFILE_ERROR32
;
1152 if (K32OBJ_OPS(ioptr
)->write
)
1153 status
= K32OBJ_OPS(ioptr
)->write(ioptr
, buffer
, count
, &result
, NULL
);
1154 K32OBJ_DecCount( ioptr
);
1155 if (!status
) result
= HFILE_ERROR32
;
1160 /***********************************************************************
1161 * SetHandleCount16 (KERNEL.199)
1163 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1165 HGLOBAL16 hPDB
= GetCurrentPDB();
1166 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1167 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1169 TRACE(file
, "(%d)\n", count
);
1171 if (count
< 20) count
= 20; /* No point in going below 20 */
1172 else if (count
> 254) count
= 254;
1176 if (pdb
->nbFiles
> 20)
1178 memcpy( pdb
->fileHandles
, files
, 20 );
1179 GlobalFree16( pdb
->hFileHandles
);
1180 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1181 GlobalHandleToSel( hPDB
) );
1182 pdb
->hFileHandles
= 0;
1186 else /* More than 20, need a new file handles table */
1189 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1192 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1193 return pdb
->nbFiles
;
1195 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1197 if (count
> pdb
->nbFiles
)
1199 memcpy( newfiles
, files
, pdb
->nbFiles
);
1200 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1202 else memcpy( newfiles
, files
, count
);
1203 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1204 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1205 pdb
->hFileHandles
= newhandle
;
1206 pdb
->nbFiles
= count
;
1208 return pdb
->nbFiles
;
1212 /*************************************************************************
1213 * SetHandleCount32 (KERNEL32.494)
1215 UINT32 WINAPI
SetHandleCount32( UINT32 count
)
1217 return MIN( 256, count
);
1221 /***********************************************************************
1222 * FlushFileBuffers (KERNEL32.133)
1224 BOOL32 WINAPI
FlushFileBuffers( HFILE32 hFile
)
1229 TRACE(file
, "(%d)\n", hFile
);
1230 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1231 if (fsync( file
->unix_handle
) != -1) ret
= TRUE
;
1237 FILE_ReleaseFile( file
);
1242 /**************************************************************************
1243 * SetEndOfFile (KERNEL32.483)
1245 BOOL32 WINAPI
SetEndOfFile( HFILE32 hFile
)
1250 TRACE(file
, "(%d)\n", hFile
);
1251 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1252 if (ftruncate( file
->unix_handle
,
1253 lseek( file
->unix_handle
, 0, SEEK_CUR
) ))
1258 FILE_ReleaseFile( file
);
1263 /***********************************************************************
1264 * DeleteFile16 (KERNEL.146)
1266 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1268 return DeleteFile32A( path
);
1272 /***********************************************************************
1273 * DeleteFile32A (KERNEL32.71)
1275 BOOL32 WINAPI
DeleteFile32A( LPCSTR path
)
1277 DOS_FULL_NAME full_name
;
1279 TRACE(file
, "'%s'\n", path
);
1281 if (DOSFS_GetDevice( path
))
1283 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1284 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1288 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1289 if (unlink( full_name
.long_name
) == -1)
1298 /***********************************************************************
1299 * DeleteFile32W (KERNEL32.72)
1301 BOOL32 WINAPI
DeleteFile32W( LPCWSTR path
)
1303 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1304 BOOL32 ret
= DeleteFile32A( xpath
);
1305 HeapFree( GetProcessHeap(), 0, xpath
);
1310 /***********************************************************************
1313 BOOL32
FILE_SetFileType( HFILE32 hFile
, DWORD type
)
1315 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1316 if (!file
) return FALSE
;
1318 FILE_ReleaseFile( file
);
1323 /***********************************************************************
1326 LPVOID
FILE_mmap( HFILE32 hFile
, LPVOID start
,
1327 DWORD size_high
, DWORD size_low
,
1328 DWORD offset_high
, DWORD offset_low
,
1329 int prot
, int flags
)
1332 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1333 if (!file
) return (LPVOID
)-1;
1334 ret
= FILE_dommap( file
, start
, size_high
, size_low
,
1335 offset_high
, offset_low
, prot
, flags
);
1336 FILE_ReleaseFile( file
);
1341 /***********************************************************************
1344 LPVOID
FILE_dommap( FILE_OBJECT
*file
, LPVOID start
,
1345 DWORD size_high
, DWORD size_low
,
1346 DWORD offset_high
, DWORD offset_low
,
1347 int prot
, int flags
)
1353 if (size_high
|| offset_high
)
1354 FIXME(file
, "offsets larger than 4Gb not supported\n");
1361 static int fdzero
= -1;
1365 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1367 perror( "/dev/zero: open" );
1372 #endif /* MAP_ANON */
1373 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1375 flags
&= ~MAP_SHARED
;
1378 flags
|= MAP_PRIVATE
;
1381 else fd
= file
->unix_handle
;
1383 if ((ret
= mmap( start
, size_low
, prot
,
1384 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1387 /* mmap() failed; if this is because the file offset is not */
1388 /* page-aligned (EINVAL), or because the underlying filesystem */
1389 /* does not support mmap() (ENOEXEC), we do it by hand. */
1391 if (!file
) return ret
;
1392 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1393 if (prot
& PROT_WRITE
)
1395 /* We cannot fake shared write mappings */
1397 if (flags
& MAP_SHARED
) return ret
;
1400 if (!(flags
& MAP_PRIVATE
)) return ret
;
1403 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1404 /* Reserve the memory with an anonymous mmap */
1405 ret
= FILE_dommap( NULL
, start
, size_high
, size_low
, 0, 0,
1406 PROT_READ
| PROT_WRITE
, flags
);
1407 if (ret
== (LPVOID
)-1) return ret
;
1408 /* Now read in the file */
1409 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1411 FILE_munmap( ret
, size_high
, size_low
);
1414 read( fd
, ret
, size_low
);
1415 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1416 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1421 /***********************************************************************
1424 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1427 FIXME(file
, "offsets larger than 4Gb not supported\n");
1428 return munmap( start
, size_low
);
1432 /***********************************************************************
1433 * GetFileType (KERNEL32.222)
1435 DWORD WINAPI
GetFileType( HFILE32 hFile
)
1437 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1438 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1439 FILE_ReleaseFile( file
);
1444 /**************************************************************************
1445 * MoveFileEx32A (KERNEL32.???)
1447 BOOL32 WINAPI
MoveFileEx32A( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1449 DOS_FULL_NAME full_name1
, full_name2
;
1450 int mode
=0; /* mode == 1: use copy */
1452 TRACE(file
, "(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1454 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1455 if (fn2
) { /* !fn2 means delete fn1 */
1456 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1457 /* Source name and target path are valid */
1458 if ( full_name1
.drive
!= full_name2
.drive
)
1459 /* use copy, if allowed */
1460 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1461 /* FIXME: Use right error code */
1462 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
1466 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1467 /* target exists, check if we may overwrite */
1468 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1469 /* FIXME: Use right error code */
1470 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1474 else /* fn2 == NULL means delete source */
1475 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1476 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1477 WARN(file
, "Illegal flag\n");
1478 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1482 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1483 Perhaps we should queue these command and execute it
1484 when exiting... What about using on_exit(2)
1486 FIXME(file
, "Please delete file '%s' when Wine has finished\n",
1487 full_name1
.long_name
);
1490 else if (unlink( full_name1
.long_name
) == -1)
1495 else return TRUE
; /* successfully deleted */
1497 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1498 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1499 Perhaps we should queue these command and execute it
1500 when exiting... What about using on_exit(2)
1502 FIXME(file
,"Please move existing file '%s' to file '%s'"
1503 "when Wine has finished\n",
1504 full_name1
.long_name
, full_name2
.long_name
);
1508 if (!mode
) /* move the file */
1509 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1515 else /* copy File */
1516 return CopyFile32A(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1520 /**************************************************************************
1521 * MoveFileEx32W (KERNEL32.???)
1523 BOOL32 WINAPI
MoveFileEx32W( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1525 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1526 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1527 BOOL32 res
= MoveFileEx32A( afn1
, afn2
, flag
);
1528 HeapFree( GetProcessHeap(), 0, afn1
);
1529 HeapFree( GetProcessHeap(), 0, afn2
);
1534 /**************************************************************************
1535 * MoveFile32A (KERNEL32.387)
1537 * Move file or directory
1539 BOOL32 WINAPI
MoveFile32A( LPCSTR fn1
, LPCSTR fn2
)
1541 DOS_FULL_NAME full_name1
, full_name2
;
1544 TRACE(file
, "(%s,%s)\n", fn1
, fn2
);
1546 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1547 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1548 /* The new name must not already exist */
1550 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1552 if (full_name1
.drive
== full_name2
.drive
) /* move */
1553 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1560 if (stat( full_name1
.long_name
, &fstat
))
1562 WARN(file
, "Invalid source file %s\n",
1563 full_name1
.long_name
);
1567 if (S_ISDIR(fstat
.st_mode
)) {
1568 /* No Move for directories across file systems */
1569 /* FIXME: Use right error code */
1570 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1575 return CopyFile32A(fn1
, fn2
, TRUE
); /*fail, if exist */
1580 /**************************************************************************
1581 * MoveFile32W (KERNEL32.390)
1583 BOOL32 WINAPI
MoveFile32W( LPCWSTR fn1
, LPCWSTR fn2
)
1585 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1586 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1587 BOOL32 res
= MoveFile32A( afn1
, afn2
);
1588 HeapFree( GetProcessHeap(), 0, afn1
);
1589 HeapFree( GetProcessHeap(), 0, afn2
);
1594 /**************************************************************************
1595 * CopyFile32A (KERNEL32.36)
1597 BOOL32 WINAPI
CopyFile32A( LPCSTR source
, LPCSTR dest
, BOOL32 fail_if_exists
)
1600 BY_HANDLE_FILE_INFORMATION info
;
1606 if ((h1
= _lopen32( source
, OF_READ
)) == HFILE_ERROR32
) return FALSE
;
1607 if (!GetFileInformationByHandle( h1
, &info
))
1612 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1613 if ((h2
= FILE_Create( dest
, mode
, fail_if_exists
)) == HFILE_ERROR32
)
1618 while ((count
= _lread32( h1
, buffer
, sizeof(buffer
) )) > 0)
1623 INT32 res
= _lwrite32( h2
, p
, count
);
1624 if (res
<= 0) goto done
;
1637 /**************************************************************************
1638 * CopyFile32W (KERNEL32.37)
1640 BOOL32 WINAPI
CopyFile32W( LPCWSTR source
, LPCWSTR dest
, BOOL32 fail_if_exists
)
1642 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1643 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1644 BOOL32 ret
= CopyFile32A( sourceA
, destA
, fail_if_exists
);
1645 HeapFree( GetProcessHeap(), 0, sourceA
);
1646 HeapFree( GetProcessHeap(), 0, destA
);
1651 /***********************************************************************
1652 * SetFileTime (KERNEL32.493)
1654 BOOL32 WINAPI
SetFileTime( HFILE32 hFile
,
1655 const FILETIME
*lpCreationTime
,
1656 const FILETIME
*lpLastAccessTime
,
1657 const FILETIME
*lpLastWriteTime
)
1659 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1660 struct utimbuf utimbuf
;
1662 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1663 TRACE(file
,"(%s,%p,%p,%p)\n",
1669 if (lpLastAccessTime
)
1670 utimbuf
.actime
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1672 utimbuf
.actime
= 0; /* FIXME */
1673 if (lpLastWriteTime
)
1674 utimbuf
.modtime
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1676 utimbuf
.modtime
= 0; /* FIXME */
1677 if (-1==utime(file
->unix_name
,&utimbuf
))
1679 FILE_ReleaseFile( file
);
1683 FILE_ReleaseFile( file
);
1687 /* Locks need to be mirrored because unix file locking is based
1688 * on the pid. Inside of wine there can be multiple WINE processes
1689 * that share the same unix pid.
1690 * Read's and writes should check these locks also - not sure
1691 * how critical that is at this point (FIXME).
1694 static BOOL32
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
1696 DOS_FILE_LOCK
*curr
;
1699 processId
= GetCurrentProcessId();
1701 /* check if lock overlaps a current lock for the same file */
1702 for (curr
= locks
; curr
; curr
= curr
->next
) {
1703 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
1704 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
1705 return TRUE
;/* region is identic */
1706 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
1707 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
1708 /* region overlaps */
1714 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
1715 curr
->processId
= GetCurrentProcessId();
1716 curr
->base
= f
->l_start
;
1717 curr
->len
= f
->l_len
;
1718 curr
->unix_name
= HEAP_strdupA( SystemHeap
, 0, file
->unix_name
);
1720 curr
->dos_file
= file
;
1725 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
1728 DOS_FILE_LOCK
**curr
;
1731 processId
= GetCurrentProcessId();
1734 if ((*curr
)->dos_file
== file
) {
1736 *curr
= (*curr
)->next
;
1737 HeapFree( SystemHeap
, 0, rem
->unix_name
);
1738 HeapFree( SystemHeap
, 0, rem
);
1741 curr
= &(*curr
)->next
;
1745 static BOOL32
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
1748 DOS_FILE_LOCK
**curr
;
1751 processId
= GetCurrentProcessId();
1752 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
1753 if ((*curr
)->processId
== processId
&&
1754 (*curr
)->dos_file
== file
&&
1755 (*curr
)->base
== f
->l_start
&&
1756 (*curr
)->len
== f
->l_len
) {
1757 /* this is the same lock */
1759 *curr
= (*curr
)->next
;
1760 HeapFree( SystemHeap
, 0, rem
->unix_name
);
1761 HeapFree( SystemHeap
, 0, rem
);
1765 /* no matching lock found */
1770 /**************************************************************************
1771 * LockFile (KERNEL32.511)
1773 BOOL32 WINAPI
LockFile(
1774 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
1775 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
1780 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1781 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
1782 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
1784 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
1785 FIXME(file
, "Unimplemented bytes > 32bits\n");
1789 f
.l_start
= dwFileOffsetLow
;
1790 f
.l_len
= nNumberOfBytesToLockLow
;
1791 f
.l_whence
= SEEK_SET
;
1795 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
1797 /* shadow locks internally */
1798 if (!DOS_AddLock(file
, &f
)) {
1799 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
1803 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1804 #ifdef USE_UNIX_LOCKS
1805 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
1806 if (errno
== EACCES
|| errno
== EAGAIN
) {
1807 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
1812 /* remove our internal copy of the lock */
1813 DOS_RemoveLock(file
, &f
);
1821 /**************************************************************************
1822 * UnlockFile (KERNEL32.703)
1824 BOOL32 WINAPI
UnlockFile(
1825 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
1826 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
1831 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1832 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
1833 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
1835 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
1836 WARN(file
, "Unimplemented bytes > 32bits\n");
1840 f
.l_start
= dwFileOffsetLow
;
1841 f
.l_len
= nNumberOfBytesToUnlockLow
;
1842 f
.l_whence
= SEEK_SET
;
1846 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
1848 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
1850 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1851 #ifdef USE_UNIX_LOCKS
1852 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
1860 /**************************************************************************
1861 * GetFileAttributesEx32A [KERNEL32.874]
1863 BOOL32 WINAPI
GetFileAttributesEx32A(
1864 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
1865 LPVOID lpFileInformation
)
1867 DOS_FULL_NAME full_name
;
1868 BY_HANDLE_FILE_INFORMATION info
;
1870 if (lpFileName
== NULL
) return FALSE
;
1871 if (lpFileInformation
== NULL
) return FALSE
;
1873 if (fInfoLevelId
== GetFileExInfoStandard
) {
1874 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
1875 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
1876 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
1877 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
1879 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
1880 lpFad
->ftCreationTime
= info
.ftCreationTime
;
1881 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
1882 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
1883 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
1884 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
1887 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
1895 /**************************************************************************
1896 * GetFileAttributesEx32W [KERNEL32.875]
1898 BOOL32 WINAPI
GetFileAttributesEx32W(
1899 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
1900 LPVOID lpFileInformation
)
1902 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
1904 GetFileAttributesEx32A( nameA
, fInfoLevelId
, lpFileInformation
);
1905 HeapFree( GetProcessHeap(), 0, nameA
);