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
;
93 handle
= HANDLE_Alloc( PROCESS_Current(), &(*file
)->header
,
94 FILE_ALL_ACCESS
| GENERIC_READ
|
95 GENERIC_WRITE
| GENERIC_EXECUTE
/*FIXME*/, TRUE
, -1 );
96 /* If the allocation failed, the object is already destroyed */
97 if (handle
== INVALID_HANDLE_VALUE32
) *file
= NULL
;
102 /* FIXME: lpOverlapped is ignored */
103 static BOOL32
FILE_Read(K32OBJ
*ptr
, LPVOID lpBuffer
, DWORD nNumberOfChars
,
104 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
106 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
109 TRACE(file
, "%p %p %ld\n", ptr
, lpBuffer
,
112 if (nNumberOfChars
== 0) {
113 *lpNumberOfChars
= 0; /* FIXME: does this change */
117 if ( (file
->pos
< 0) || /* workaround, see SetFilePointer */
118 ((result
= read(file
->unix_handle
, lpBuffer
, nNumberOfChars
)) == -1) )
124 *lpNumberOfChars
= result
;
129 * experimentation yields that WriteFile:
130 * o does not truncate on write of 0
131 * o always changes the *lpNumberOfChars to actual number of
133 * o write of 0 nNumberOfChars returns TRUE
135 static BOOL32
FILE_Write(K32OBJ
*ptr
, LPCVOID lpBuffer
, DWORD nNumberOfChars
,
136 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
138 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
141 TRACE(file
, "%p %p %ld\n", ptr
, lpBuffer
,
144 *lpNumberOfChars
= 0;
147 * I assume this loop around EAGAIN is here because
148 * win32 doesn't have interrupted system calls
151 if (file
->pos
< 0) { /* workaround, see SetFilePointer */
158 result
= write(file
->unix_handle
, lpBuffer
, nNumberOfChars
);
160 *lpNumberOfChars
= result
;
164 if (errno
!= EINTR
) {
173 /***********************************************************************
176 * Destroy a DOS file.
178 static void FILE_Destroy( K32OBJ
*ptr
)
180 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
181 assert( ptr
->type
== K32OBJ_FILE
);
183 DOS_RemoveFileLocks(file
);
185 if (file
->unix_handle
!= -1) close( file
->unix_handle
);
186 if (file
->unix_name
) HeapFree( SystemHeap
, 0, file
->unix_name
);
187 ptr
->type
= K32OBJ_UNKNOWN
;
188 HeapFree( SystemHeap
, 0, file
);
192 /***********************************************************************
195 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
196 * be called to release the file.
198 FILE_OBJECT
*FILE_GetFile( HFILE32 handle
)
200 return (FILE_OBJECT
*)HANDLE_GetObjPtr( PROCESS_Current(), handle
,
201 K32OBJ_FILE
, 0 /*FIXME*/, NULL
);
205 /***********************************************************************
208 * Release a DOS file obtained with FILE_GetFile.
210 void FILE_ReleaseFile( FILE_OBJECT
*file
)
212 K32OBJ_DecCount( &file
->header
);
216 /***********************************************************************
219 * Return the Unix handle associated to a file handle.
221 int FILE_GetUnixHandle( HFILE32 hFile
)
226 if (!(file
= FILE_GetFile( hFile
))) return -1;
227 ret
= file
->unix_handle
;
228 FILE_ReleaseFile( file
);
232 /***********************************************************************
240 static int FILE_UnixToDosMode(int unixMode
)
249 dosMode
=OF_READWRITE
;
259 /***********************************************************************
267 static int FILE_DOSToUnixMode(int dosMode
)
273 unixMode
= O_WRONLY
; break;
275 unixMode
= O_RDWR
; break;
278 unixMode
= O_RDONLY
; break;
283 /***********************************************************************
287 * oldmode[I] mode how file was first opened
288 * mode[I] mode how the file should get opened
293 * Look what we have to do with the given SHARE modes
295 * Ralph Brown's interrupt list gives following explication, I guess
296 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
298 * FIXME: Validate this function
299 ========from Ralph Brown's list =========
301 Values of DOS file sharing behavior:
302 | Second and subsequent Opens
303 First |Compat Deny Deny Deny Deny
304 Open | All Write Read None
305 |R W RW R W RW R W RW R W RW R W RW
306 - - - - -| - - - - - - - - - - - - - - - - -
307 Compat R |Y Y Y N N N 1 N N N N N 1 N N
308 W |Y Y Y N N N N N N N N N N N N
309 RW|Y Y Y N N N N N N N N N N N N
311 Deny R |C C C N N N N N N N N N N N N
312 All W |C C C N N N N N N N N N N N N
313 RW|C C C N N N N N N N N N N N N
315 Deny R |2 C C N N N Y N N N N N Y N N
316 Write W |C C C N N N N N N Y N N Y N N
317 RW|C C C N N N N N N N N N Y N N
319 Deny R |C C C N N N N Y N N N N N Y N
320 Read W |C C C N N N N N N N Y N N Y N
321 RW|C C C N N N N N N N N N N Y N
323 Deny R |2 C C N N N Y Y Y N N N Y Y Y
324 None W |C C C N N N N N N Y Y Y Y Y Y
325 RW|C C C N N N N N N N N N Y Y Y
326 Legend: Y = open succeeds, N = open fails with error code 05h
327 C = open fails, INT 24 generated
328 1 = open succeeds if file read-only, else fails with error code
329 2 = open succeeds if file read-only, else fails with INT 24
330 ========end of description from Ralph Brown's List =====
331 For every "Y" in the table we return FALSE
332 For every "N" we set the DOS_ERROR and return TRUE
333 For all other cases we barf,set the DOS_ERROR and return TRUE
336 static BOOL32
FILE_ShareDeny( int mode
, int oldmode
)
338 int oldsharemode
= oldmode
& 0x70;
339 int sharemode
= mode
& 0x70;
340 int oldopenmode
= oldmode
& 3;
341 int openmode
= mode
& 3;
343 switch (oldsharemode
)
345 case OF_SHARE_COMPAT
:
346 if (sharemode
== OF_SHARE_COMPAT
) return FALSE
;
347 if (openmode
== OF_READ
) goto test_ro_err05
;
349 case OF_SHARE_EXCLUSIVE
:
350 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
352 case OF_SHARE_DENY_WRITE
:
353 if (openmode
!= OF_READ
)
355 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
360 case OF_SHARE_COMPAT
:
361 if (oldopenmode
== OF_READ
) goto test_ro_int24
;
363 case OF_SHARE_DENY_NONE
:
365 case OF_SHARE_DENY_WRITE
:
366 if (oldopenmode
== OF_READ
) return FALSE
;
367 case OF_SHARE_DENY_READ
:
368 if (oldopenmode
== OF_WRITE
) return FALSE
;
369 case OF_SHARE_EXCLUSIVE
:
374 case OF_SHARE_DENY_READ
:
375 if (openmode
!= OF_WRITE
)
377 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
382 case OF_SHARE_COMPAT
:
384 case OF_SHARE_DENY_NONE
:
386 case OF_SHARE_DENY_WRITE
:
387 if (oldopenmode
== OF_READ
) return FALSE
;
388 case OF_SHARE_DENY_READ
:
389 if (oldopenmode
== OF_WRITE
) return FALSE
;
390 case OF_SHARE_EXCLUSIVE
:
395 case OF_SHARE_DENY_NONE
:
398 case OF_SHARE_COMPAT
:
400 case OF_SHARE_DENY_NONE
:
402 case OF_SHARE_DENY_WRITE
:
403 if (oldopenmode
== OF_READ
) return FALSE
;
404 case OF_SHARE_DENY_READ
:
405 if (oldopenmode
== OF_WRITE
) return FALSE
;
406 case OF_SHARE_EXCLUSIVE
:
411 ERR(file
,"unknown mode\n");
413 ERR(file
,"shouldn't happen\n");
414 ERR(file
,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
418 if (oldmode
== OF_READ
)
422 FIXME(file
,"generate INT24 missing\n");
423 /* Is this the right error? */
424 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
428 if (oldmode
== OF_READ
)
432 TRACE(file
,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode
,mode
);
433 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
439 /***********************************************************************
442 * Look if the File is in Use For the OF_SHARE_XXX options
445 * name [I]: full unix name of the file that should be opened
446 * mode [O]: mode how the file was first opened
448 * TRUE if the file was opened before
449 * FALSE if we open the file exclusive for this process
451 * Scope of the files we look for is only the current pdb
452 * Could we use /proc/self/? on Linux for this?
453 * Should we use flock? Should we create another structure?
454 * Searching through all files seem quite expensive for me, but
455 * I don't see any other way.
457 * FIXME: Extend scope to the whole Wine process
460 static BOOL32
FILE_InUse(char * name
, int * mode
)
464 HGLOBAL16 hPDB
= GetCurrentPDB();
465 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
468 for (i
=0;i
<pdb
->nbFiles
;i
++)
470 file
=FILE_GetFile( (HFILE32
) i
);
475 TRACE(file
,"got %s at %d\n",file
->unix_name
,i
);
476 if(!lstrcmp32A(file
->unix_name
,name
))
479 FILE_ReleaseFile(file
);
483 FILE_ReleaseFile(file
);
489 /***********************************************************************
492 * Set the DOS error code from errno.
494 void FILE_SetDosError(void)
496 int save_errno
= errno
; /* errno gets overwritten by printf */
498 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
502 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
505 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
508 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
513 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
516 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
519 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
522 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
526 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
529 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
533 DOS_ERROR( ER_SeekError
, EC_NotFound
, SA_Ignore
, EL_Disk
);
536 DOS_ERROR( ERROR_DIR_NOT_EMPTY
, EC_Exists
, SA_Ignore
, EL_Disk
);
539 perror( "int21: unknown errno" );
540 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
547 /***********************************************************************
550 * Duplicate a Unix handle into a task handle.
552 HFILE32
FILE_DupUnixHandle( int fd
)
557 if ((handle
= FILE_Alloc( &file
)) != INVALID_HANDLE_VALUE32
)
559 if ((file
->unix_handle
= dup(fd
)) == -1)
562 CloseHandle( handle
);
563 return INVALID_HANDLE_VALUE32
;
570 /***********************************************************************
573 HFILE32
FILE_OpenUnixFile( const char *name
, int mode
)
579 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
580 return INVALID_HANDLE_VALUE32
;
582 if ((file
->unix_handle
= open( name
, mode
, 0666 )) == -1)
584 if (!Options
.failReadOnly
&& (mode
== O_RDWR
))
585 file
->unix_handle
= open( name
, O_RDONLY
);
587 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
590 CloseHandle( handle
);
591 return INVALID_HANDLE_VALUE32
;
593 if (S_ISDIR(st
.st_mode
))
595 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
596 CloseHandle( handle
);
597 return INVALID_HANDLE_VALUE32
;
600 /* File opened OK, now fill the FILE_OBJECT */
602 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, name
);
607 /***********************************************************************
610 * path[I] name of file to open
611 * mode[I] mode how to open, in unix notation
612 * shareMode[I] the sharing mode in the win OpenFile notation
615 HFILE32
FILE_Open( LPCSTR path
, INT32 mode
, INT32 shareMode
)
617 DOS_FULL_NAME full_name
;
618 const char *unixName
;
619 int oldMode
, dosMode
; /* FIXME: Do we really need unixmode as argument for
623 BOOL32 fileInUse
= FALSE
;
625 TRACE(file
, "'%s' %04x\n", path
, mode
);
627 if (!path
) return HFILE_ERROR32
;
629 if (DOSFS_GetDevice( path
))
633 TRACE(file
, "opening device '%s'\n", path
);
635 if (HFILE_ERROR32
!=(ret
=DOSFS_OpenDevice( path
, mode
)))
638 /* Do not silence this please. It is a critical error. -MM */
639 ERR(file
, "Couldn't open device '%s'!\n",path
);
640 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
641 return HFILE_ERROR32
;
644 else /* check for filename, don't check for last entry if creating */
646 if (!DOSFS_GetFullName( path
, !(mode
& O_CREAT
), &full_name
))
647 return HFILE_ERROR32
;
648 unixName
= full_name
.long_name
;
651 dosMode
= FILE_UnixToDosMode(mode
)| shareMode
;
652 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
655 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
656 if (FILE_ShareDeny(dosMode
,oldMode
)) return HFILE_ERROR32
;
658 hFileRet
= FILE_OpenUnixFile( unixName
, mode
);
659 /* we need to save the mode, but only if it is not in use yet*/
660 if ((hFileRet
) && (!fileInUse
) && ((file
=FILE_GetFile(hFileRet
))))
663 FILE_ReleaseFile(file
);
670 /***********************************************************************
673 static HFILE32
FILE_Create( LPCSTR path
, int mode
, int unique
)
677 DOS_FULL_NAME full_name
;
678 BOOL32 fileInUse
= FALSE
;
679 int oldMode
,dosMode
; /* FIXME: Do we really need unixmode as argument for
682 TRACE(file
, "'%s' %04x %d\n", path
, mode
, unique
);
684 if (!path
) return INVALID_HANDLE_VALUE32
;
686 if (DOSFS_GetDevice( path
))
688 WARN(file
, "cannot create DOS device '%s'!\n", path
);
689 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
690 return INVALID_HANDLE_VALUE32
;
693 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
694 return INVALID_HANDLE_VALUE32
;
696 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
))
698 CloseHandle( handle
);
699 return INVALID_HANDLE_VALUE32
;
702 dosMode
= FILE_UnixToDosMode(mode
);
703 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
706 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
707 if (FILE_ShareDeny(dosMode
,oldMode
)) return INVALID_HANDLE_VALUE32
;
710 if ((file
->unix_handle
= open( full_name
.long_name
,
711 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
715 CloseHandle( handle
);
716 return INVALID_HANDLE_VALUE32
;
719 /* File created OK, now fill the FILE_OBJECT */
721 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, full_name
.long_name
);
722 file
->mode
= dosMode
;
727 /***********************************************************************
730 * Fill a file information from a struct stat.
732 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
734 if (S_ISDIR(st
->st_mode
))
735 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
737 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
738 if (!(st
->st_mode
& S_IWUSR
))
739 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
741 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
742 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
743 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
745 info
->dwVolumeSerialNumber
= 0; /* FIXME */
746 info
->nFileSizeHigh
= 0;
747 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
748 info
->nNumberOfLinks
= st
->st_nlink
;
749 info
->nFileIndexHigh
= 0;
750 info
->nFileIndexLow
= st
->st_ino
;
754 /***********************************************************************
757 * Stat a Unix path name. Return TRUE if OK.
759 BOOL32
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
763 if (!unixName
|| !info
) return FALSE
;
765 if (stat( unixName
, &st
) == -1)
770 FILE_FillInfo( &st
, info
);
775 /***********************************************************************
776 * GetFileInformationByHandle (KERNEL32.219)
778 DWORD WINAPI
GetFileInformationByHandle( HFILE32 hFile
,
779 BY_HANDLE_FILE_INFORMATION
*info
)
787 if (!(file
= FILE_GetFile( hFile
))) return 0;
788 if (fstat( file
->unix_handle
, &st
) == -1) FILE_SetDosError();
791 FILE_FillInfo( &st
, info
);
794 FILE_ReleaseFile( file
);
799 /**************************************************************************
800 * GetFileAttributes16 (KERNEL.420)
802 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
804 return GetFileAttributes32A( name
);
808 /**************************************************************************
809 * GetFileAttributes32A (KERNEL32.217)
811 DWORD WINAPI
GetFileAttributes32A( LPCSTR name
)
813 DOS_FULL_NAME full_name
;
814 BY_HANDLE_FILE_INFORMATION info
;
816 if (name
== NULL
|| *name
=='\0') return -1;
818 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
819 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
820 return info
.dwFileAttributes
;
824 /**************************************************************************
825 * GetFileAttributes32W (KERNEL32.218)
827 DWORD WINAPI
GetFileAttributes32W( LPCWSTR name
)
829 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
830 DWORD res
= GetFileAttributes32A( nameA
);
831 HeapFree( GetProcessHeap(), 0, nameA
);
836 /***********************************************************************
837 * GetFileSize (KERNEL32.220)
839 DWORD WINAPI
GetFileSize( HFILE32 hFile
, LPDWORD filesizehigh
)
841 BY_HANDLE_FILE_INFORMATION info
;
842 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
843 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
844 return info
.nFileSizeLow
;
848 /***********************************************************************
849 * GetFileTime (KERNEL32.221)
851 BOOL32 WINAPI
GetFileTime( HFILE32 hFile
, FILETIME
*lpCreationTime
,
852 FILETIME
*lpLastAccessTime
,
853 FILETIME
*lpLastWriteTime
)
855 BY_HANDLE_FILE_INFORMATION info
;
856 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
857 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
858 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
859 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
863 /***********************************************************************
864 * CompareFileTime (KERNEL32.28)
866 INT32 WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
868 if (!x
|| !y
) return -1;
870 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
872 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
874 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
876 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
881 /***********************************************************************
884 * dup() function for DOS handles.
886 HFILE32
FILE_Dup( HFILE32 hFile
)
890 TRACE(file
, "FILE_Dup for handle %d\n", hFile
);
891 if (!DuplicateHandle( GetCurrentProcess(), hFile
, GetCurrentProcess(),
892 &handle
, FILE_ALL_ACCESS
/* FIXME */, FALSE
, 0 ))
893 handle
= HFILE_ERROR32
;
894 TRACE(file
, "FILE_Dup return handle %d\n", handle
);
899 /***********************************************************************
902 * dup2() function for DOS handles.
904 HFILE32
FILE_Dup2( HFILE32 hFile1
, HFILE32 hFile2
)
908 TRACE(file
, "FILE_Dup2 for handle %d\n", hFile1
);
909 /* FIXME: should use DuplicateHandle */
910 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR32
;
911 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2
, &file
->header
, 0 ))
912 hFile2
= HFILE_ERROR32
;
913 FILE_ReleaseFile( file
);
918 /***********************************************************************
919 * GetTempFileName16 (KERNEL.97)
921 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
926 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
927 drive
|= DRIVE_GetCurrentDrive() + 'A';
929 if ((drive
& TF_FORCEDRIVE
) &&
930 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
932 drive
&= ~TF_FORCEDRIVE
;
933 WARN(file
, "invalid drive %d specified\n", drive
);
936 if (drive
& TF_FORCEDRIVE
)
937 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
939 GetTempPath32A( 132, temppath
);
940 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
944 /***********************************************************************
945 * GetTempFileName32A (KERNEL32.290)
947 UINT32 WINAPI
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
950 static UINT32 unique_temp
;
951 DOS_FULL_NAME full_name
;
956 if ( !path
|| !prefix
|| !buffer
) return 0;
958 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
959 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
961 strcpy( buffer
, path
);
962 p
= buffer
+ strlen(buffer
);
964 /* add a \, if there isn't one and path is more than just the drive letter ... */
965 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
966 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
969 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
970 sprintf( p
, "%04x.tmp", num
);
972 /* Now try to create it */
978 HFILE32 handle
= FILE_Create( buffer
, 0666, TRUE
);
979 if (handle
!= INVALID_HANDLE_VALUE32
)
980 { /* We created it */
981 TRACE(file
, "created %s\n",
983 CloseHandle( handle
);
986 if (DOS_ExtendedError
!= ER_FileExists
)
987 break; /* No need to go on */
989 sprintf( p
, "%04x.tmp", num
);
990 } while (num
!= (unique
& 0xffff));
993 /* Get the full path name */
995 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
997 /* Check if we have write access in the directory */
998 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
999 if (access( full_name
.long_name
, W_OK
) == -1)
1000 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
1003 TRACE(file
, "returning %s\n", buffer
);
1004 return unique
? unique
: num
;
1008 /***********************************************************************
1009 * GetTempFileName32W (KERNEL32.291)
1011 UINT32 WINAPI
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
1014 LPSTR patha
,prefixa
;
1018 if (!path
) return 0;
1019 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1020 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
1021 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
1022 lstrcpyAtoW( buffer
, buffera
);
1023 HeapFree( GetProcessHeap(), 0, patha
);
1024 HeapFree( GetProcessHeap(), 0, prefixa
);
1029 /***********************************************************************
1032 * Implementation of OpenFile16() and OpenFile32().
1034 static HFILE32
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
,
1039 WORD filedatetime
[2];
1040 DOS_FULL_NAME full_name
;
1042 int unixMode
, oldMode
;
1044 BOOL32 fileInUse
= FALSE
;
1046 if (!ofs
) return HFILE_ERROR32
;
1049 ofs
->cBytes
= sizeof(OFSTRUCT
);
1051 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
1054 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
1055 return HFILE_ERROR32
;
1058 TRACE(file
, "%s %04x\n", name
, mode
);
1060 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
1061 Are there any cases where getting the path here is wrong?
1062 Uwe Bonnes 1997 Apr 2 */
1063 if (!GetFullPathName32A( name
, sizeof(ofs
->szPathName
),
1064 ofs
->szPathName
, NULL
)) goto error
;
1066 /* OF_PARSE simply fills the structure */
1068 if (mode
& OF_PARSE
)
1070 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
1071 != DRIVE_REMOVABLE
);
1072 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
1073 name
, ofs
->szPathName
);
1077 /* OF_CREATE is completely different from all other options, so
1080 if (mode
& OF_CREATE
)
1082 if ((hFileRet
= FILE_Create(name
,0666,FALSE
))== INVALID_HANDLE_VALUE32
)
1087 /* If OF_SEARCH is set, ignore the given path */
1089 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
1091 /* First try the file name as is */
1092 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
1093 /* Now remove the path */
1094 if (name
[0] && (name
[1] == ':')) name
+= 2;
1095 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
1096 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
1097 if (!name
[0]) goto not_found
;
1100 /* Now look for the file */
1102 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
1105 TRACE(file
, "found %s = %s\n",
1106 full_name
.long_name
, full_name
.short_name
);
1107 lstrcpyn32A( ofs
->szPathName
, full_name
.short_name
,
1108 sizeof(ofs
->szPathName
) );
1110 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
1113 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
1114 if (FILE_ShareDeny(mode
,oldMode
)) return HFILE_ERROR32
;
1117 if (mode
& OF_SHARE_EXCLUSIVE
)
1118 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
1119 on the file <tempdir>/_ins0432._mp to determine how
1120 far installation has proceeded.
1121 _ins0432._mp is an executable and while running the
1122 application expects the open with OF_SHARE_ to fail*/
1124 As our loader closes the files after loading the executable,
1125 we can't find the running executable with FILE_InUse.
1126 Perhaps the loader should keep the file open.
1127 Recheck against how Win handles that case */
1129 char *last
= strrchr(full_name
.long_name
,'/');
1131 last
= full_name
.long_name
- 1;
1132 if (GetModuleHandle16(last
+1))
1134 TRACE(file
,"Denying shared open for %s\n",full_name
.long_name
);
1135 return HFILE_ERROR32
;
1139 if (mode
& OF_DELETE
)
1141 if (unlink( full_name
.long_name
) == -1) goto not_found
;
1142 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
1146 unixMode
=FILE_DOSToUnixMode(mode
);
1148 hFileRet
= FILE_OpenUnixFile( full_name
.long_name
, unixMode
);
1149 if (hFileRet
== HFILE_ERROR32
) goto not_found
;
1150 /* we need to save the mode, but only if it is not in use yet*/
1151 if( (!fileInUse
) &&(file
=FILE_GetFile(hFileRet
)))
1154 FILE_ReleaseFile(file
);
1157 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
1158 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
1159 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
1161 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
1163 CloseHandle( hFileRet
);
1164 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
1165 /* FIXME: what error here? */
1166 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1170 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
1172 success
: /* We get here if the open was successful */
1173 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
1174 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
1175 CloseHandle( hFileRet
);
1178 not_found
: /* We get here if the file does not exist */
1179 WARN(file
, "'%s' not found\n", name
);
1180 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1183 error
: /* We get here if there was an error opening the file */
1184 ofs
->nErrCode
= DOS_ExtendedError
;
1185 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
1186 name
,ofs
->nErrCode
);
1187 return HFILE_ERROR32
;
1191 /***********************************************************************
1192 * OpenFile16 (KERNEL.74)
1194 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
1196 TRACE(file
,"OpenFile16(%s,%i)\n", name
, mode
);
1197 return HFILE32_TO_HFILE16(FILE_DoOpenFile( name
, ofs
, mode
, FALSE
));
1201 /***********************************************************************
1202 * OpenFile32 (KERNEL32.396)
1204 HFILE32 WINAPI
OpenFile32( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
1206 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
1210 /***********************************************************************
1211 * _lclose16 (KERNEL.81)
1213 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1215 TRACE(file
, "handle %d\n", hFile
);
1216 return CloseHandle( HFILE16_TO_HFILE32( hFile
) ) ? 0 : HFILE_ERROR16
;
1220 /***********************************************************************
1221 * _lclose32 (KERNEL32.592)
1223 HFILE32 WINAPI
_lclose32( HFILE32 hFile
)
1225 TRACE(file
, "handle %d\n", hFile
);
1226 return CloseHandle( hFile
) ? 0 : HFILE_ERROR32
;
1230 /***********************************************************************
1233 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1237 TRACE(file
, "%d %08lx %ld\n",
1238 hFile
, (DWORD
)buffer
, count
);
1240 /* Some programs pass a count larger than the allocated buffer */
1241 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1242 if (count
> maxlen
) count
= maxlen
;
1243 return _lread32(HFILE16_TO_HFILE32(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1247 /***********************************************************************
1250 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1252 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1256 /***********************************************************************
1257 * _lread32 (KERNEL32.596)
1259 UINT32 WINAPI
_lread32( HFILE32 handle
, LPVOID buffer
, UINT32 count
)
1263 BOOL32 result
= FALSE
;
1265 TRACE( file
, "%d %p %d\n", handle
, buffer
, count
);
1266 if (!(ptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
1267 K32OBJ_UNKNOWN
, 0, NULL
))) return -1;
1268 if (K32OBJ_OPS(ptr
)->read
)
1269 result
= K32OBJ_OPS(ptr
)->read(ptr
, buffer
, count
, &numWritten
, NULL
);
1270 K32OBJ_DecCount( ptr
);
1271 if (!result
) return -1;
1272 return (UINT32
)numWritten
;
1276 /***********************************************************************
1277 * _lread16 (KERNEL.82)
1279 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1281 return (UINT16
)_lread32(HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1285 /***********************************************************************
1286 * _lcreat16 (KERNEL.83)
1288 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1290 int mode
= (attr
& 1) ? 0444 : 0666;
1291 TRACE(file
, "%s %02x\n", path
, attr
);
1292 return (HFILE16
) HFILE32_TO_HFILE16(FILE_Create( path
, mode
, FALSE
));
1296 /***********************************************************************
1297 * _lcreat32 (KERNEL32.593)
1299 HFILE32 WINAPI
_lcreat32( LPCSTR path
, INT32 attr
)
1301 int mode
= (attr
& 1) ? 0444 : 0666;
1302 TRACE(file
, "%s %02x\n", path
, attr
);
1303 return FILE_Create( path
, mode
, FALSE
);
1307 /***********************************************************************
1308 * _lcreat_uniq (Not a Windows API)
1310 HFILE32
_lcreat_uniq( LPCSTR path
, INT32 attr
)
1312 int mode
= (attr
& 1) ? 0444 : 0666;
1313 TRACE(file
, "%s %02x\n", path
, attr
);
1314 return FILE_Create( path
, mode
, TRUE
);
1318 /***********************************************************************
1319 * SetFilePointer (KERNEL32.492)
1321 DWORD WINAPI
SetFilePointer( HFILE32 hFile
, LONG distance
, LONG
*highword
,
1325 DWORD result
= 0xffffffff;
1327 if (highword
&& *highword
)
1329 FIXME(file
, "64-bit offsets not supported yet\n");
1330 SetLastError( ERROR_INVALID_PARAMETER
);
1333 TRACE(file
, "handle %d offset %ld origin %ld\n",
1334 hFile
, distance
, method
);
1336 if (!(file
= FILE_GetFile( hFile
))) return 0xffffffff;
1339 /* the pointer may be positioned before the start of the file;
1340 no error is returned in that case,
1341 but subsequent attempts at I/O will produce errors.
1342 This is not allowed with Unix lseek(),
1343 so we'll need some emulation here */
1347 distance
+= file
->pos
; /* fall through */
1349 if ((result
= lseek(file
->unix_handle
, distance
, SEEK_SET
)) == -1)
1351 if ((INT32
)distance
< 0)
1352 file
->pos
= result
= distance
;
1358 if ((result
= lseek(file
->unix_handle
, distance
, SEEK_END
)) == -1)
1360 if ((INT32
)distance
< 0)
1363 result
= lseek(file
->unix_handle
, 0, SEEK_END
);
1365 /* return to the old pos, as the first lseek failed */
1366 lseek(file
->unix_handle
, file
->pos
, SEEK_END
);
1368 file
->pos
= (result
+= distance
);
1371 ERR(file
, "lseek: unknown error. Please report.\n");
1373 else file
->pos
= result
;
1376 ERR(file
, "Unknown origin %ld !\n", method
);
1382 FILE_ReleaseFile( file
);
1387 /***********************************************************************
1388 * _llseek16 (KERNEL.84)
1390 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1392 return SetFilePointer( HFILE16_TO_HFILE32(hFile
), lOffset
, NULL
, nOrigin
);
1396 /***********************************************************************
1397 * _llseek32 (KERNEL32.594)
1399 LONG WINAPI
_llseek32( HFILE32 hFile
, LONG lOffset
, INT32 nOrigin
)
1401 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1405 /***********************************************************************
1406 * _lopen16 (KERNEL.85)
1408 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1410 return HFILE32_TO_HFILE16(_lopen32( path
, mode
));
1414 /***********************************************************************
1415 * _lopen32 (KERNEL32.595)
1417 HFILE32 WINAPI
_lopen32( LPCSTR path
, INT32 mode
)
1421 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1423 unixMode
= FILE_DOSToUnixMode(mode
);
1424 return FILE_Open( path
, unixMode
, (mode
& 0x70));
1428 /***********************************************************************
1429 * _lwrite16 (KERNEL.86)
1431 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1433 return (UINT16
)_hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1436 /***********************************************************************
1437 * _lwrite32 (KERNEL.86)
1439 UINT32 WINAPI
_lwrite32( HFILE32 hFile
, LPCSTR buffer
, UINT32 count
)
1441 return (UINT32
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1445 /***********************************************************************
1446 * _hread16 (KERNEL.349)
1448 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1450 return _lread32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1454 /***********************************************************************
1455 * _hread32 (KERNEL32.590)
1457 LONG WINAPI
_hread32( HFILE32 hFile
, LPVOID buffer
, LONG count
)
1459 return _lread32( hFile
, buffer
, count
);
1463 /***********************************************************************
1464 * _hwrite16 (KERNEL.350)
1466 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1468 return _hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1472 /***********************************************************************
1473 * _hwrite32 (KERNEL32.591)
1475 * experimenation yields that _lwrite:
1476 * o truncates the file at the current position with
1478 * o returns 0 on a 0 length write
1479 * o works with console handles
1482 LONG WINAPI
_hwrite32( HFILE32 handle
, LPCSTR buffer
, LONG count
)
1486 BOOL32 status
= FALSE
;
1488 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1490 if (count
== 0) { /* Expand or truncate at current position */
1491 FILE_OBJECT
*file
= FILE_GetFile(handle
);
1493 if ( ftruncate(file
->unix_handle
,
1494 lseek( file
->unix_handle
, 0, SEEK_CUR
)) == 0 ) {
1495 FILE_ReleaseFile(file
);
1499 FILE_ReleaseFile(file
);
1500 return HFILE_ERROR32
;
1504 if (!(ioptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
1505 K32OBJ_UNKNOWN
, 0, NULL
)))
1506 return HFILE_ERROR32
;
1507 if (K32OBJ_OPS(ioptr
)->write
)
1508 status
= K32OBJ_OPS(ioptr
)->write(ioptr
, buffer
, count
, &result
, NULL
);
1509 K32OBJ_DecCount( ioptr
);
1510 if (!status
) result
= HFILE_ERROR32
;
1515 /***********************************************************************
1516 * SetHandleCount16 (KERNEL.199)
1518 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1520 HGLOBAL16 hPDB
= GetCurrentPDB();
1521 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1522 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1524 TRACE(file
, "(%d)\n", count
);
1526 if (count
< 20) count
= 20; /* No point in going below 20 */
1527 else if (count
> 254) count
= 254;
1531 if (pdb
->nbFiles
> 20)
1533 memcpy( pdb
->fileHandles
, files
, 20 );
1534 GlobalFree16( pdb
->hFileHandles
);
1535 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1536 GlobalHandleToSel( hPDB
) );
1537 pdb
->hFileHandles
= 0;
1541 else /* More than 20, need a new file handles table */
1544 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1547 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1548 return pdb
->nbFiles
;
1550 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1552 if (count
> pdb
->nbFiles
)
1554 memcpy( newfiles
, files
, pdb
->nbFiles
);
1555 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1557 else memcpy( newfiles
, files
, count
);
1558 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1559 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1560 pdb
->hFileHandles
= newhandle
;
1561 pdb
->nbFiles
= count
;
1563 return pdb
->nbFiles
;
1567 /*************************************************************************
1568 * SetHandleCount32 (KERNEL32.494)
1570 UINT32 WINAPI
SetHandleCount32( UINT32 count
)
1572 return MIN( 256, count
);
1576 /***********************************************************************
1577 * FlushFileBuffers (KERNEL32.133)
1579 BOOL32 WINAPI
FlushFileBuffers( HFILE32 hFile
)
1584 TRACE(file
, "(%d)\n", hFile
);
1585 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1586 if (fsync( file
->unix_handle
) != -1) ret
= TRUE
;
1592 FILE_ReleaseFile( file
);
1597 /**************************************************************************
1598 * SetEndOfFile (KERNEL32.483)
1600 BOOL32 WINAPI
SetEndOfFile( HFILE32 hFile
)
1605 TRACE(file
, "(%d)\n", hFile
);
1606 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1607 if (ftruncate( file
->unix_handle
,
1608 lseek( file
->unix_handle
, 0, SEEK_CUR
) ))
1613 FILE_ReleaseFile( file
);
1618 /***********************************************************************
1619 * DeleteFile16 (KERNEL.146)
1621 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1623 return DeleteFile32A( path
);
1627 /***********************************************************************
1628 * DeleteFile32A (KERNEL32.71)
1630 BOOL32 WINAPI
DeleteFile32A( LPCSTR path
)
1632 DOS_FULL_NAME full_name
;
1634 TRACE(file
, "'%s'\n", path
);
1636 if (DOSFS_GetDevice( path
))
1638 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1639 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1643 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1644 if (unlink( full_name
.long_name
) == -1)
1653 /***********************************************************************
1654 * DeleteFile32W (KERNEL32.72)
1656 BOOL32 WINAPI
DeleteFile32W( LPCWSTR path
)
1658 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1659 BOOL32 ret
= DeleteFile32A( xpath
);
1660 HeapFree( GetProcessHeap(), 0, xpath
);
1665 /***********************************************************************
1668 BOOL32
FILE_SetFileType( HFILE32 hFile
, DWORD type
)
1670 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1671 if (!file
) return FALSE
;
1673 FILE_ReleaseFile( file
);
1678 /***********************************************************************
1681 LPVOID
FILE_mmap( HFILE32 hFile
, LPVOID start
,
1682 DWORD size_high
, DWORD size_low
,
1683 DWORD offset_high
, DWORD offset_low
,
1684 int prot
, int flags
)
1687 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1688 if (!file
) return (LPVOID
)-1;
1689 ret
= FILE_dommap( file
, start
, size_high
, size_low
,
1690 offset_high
, offset_low
, prot
, flags
);
1691 FILE_ReleaseFile( file
);
1696 /***********************************************************************
1699 LPVOID
FILE_dommap( FILE_OBJECT
*file
, LPVOID start
,
1700 DWORD size_high
, DWORD size_low
,
1701 DWORD offset_high
, DWORD offset_low
,
1702 int prot
, int flags
)
1708 if (size_high
|| offset_high
)
1709 FIXME(file
, "offsets larger than 4Gb not supported\n");
1716 static int fdzero
= -1;
1720 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1722 perror( "/dev/zero: open" );
1727 #endif /* MAP_ANON */
1728 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1730 flags
&= ~MAP_SHARED
;
1733 flags
|= MAP_PRIVATE
;
1736 else fd
= file
->unix_handle
;
1738 if ((ret
= mmap( start
, size_low
, prot
,
1739 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1742 /* mmap() failed; if this is because the file offset is not */
1743 /* page-aligned (EINVAL), or because the underlying filesystem */
1744 /* does not support mmap() (ENOEXEC), we do it by hand. */
1746 if (!file
) return ret
;
1747 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1748 if (prot
& PROT_WRITE
)
1750 /* We cannot fake shared write mappings */
1752 if (flags
& MAP_SHARED
) return ret
;
1755 if (!(flags
& MAP_PRIVATE
)) return ret
;
1758 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1759 /* Reserve the memory with an anonymous mmap */
1760 ret
= FILE_dommap( NULL
, start
, size_high
, size_low
, 0, 0,
1761 PROT_READ
| PROT_WRITE
, flags
);
1762 if (ret
== (LPVOID
)-1) return ret
;
1763 /* Now read in the file */
1764 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1766 FILE_munmap( ret
, size_high
, size_low
);
1769 read( fd
, ret
, size_low
);
1770 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1771 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1776 /***********************************************************************
1779 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1782 FIXME(file
, "offsets larger than 4Gb not supported\n");
1783 return munmap( start
, size_low
);
1787 /***********************************************************************
1788 * GetFileType (KERNEL32.222)
1790 DWORD WINAPI
GetFileType( HFILE32 hFile
)
1792 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1793 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1794 FILE_ReleaseFile( file
);
1799 /**************************************************************************
1800 * MoveFileEx32A (KERNEL32.???)
1802 BOOL32 WINAPI
MoveFileEx32A( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1804 DOS_FULL_NAME full_name1
, full_name2
;
1805 int mode
=0; /* mode == 1: use copy */
1807 TRACE(file
, "(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1809 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1810 if (fn2
) { /* !fn2 means delete fn1 */
1811 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1812 /* Source name and target path are valid */
1813 if ( full_name1
.drive
!= full_name2
.drive
)
1815 /* use copy, if allowed */
1816 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1817 /* FIXME: Use right error code */
1818 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
1823 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1824 /* target exists, check if we may overwrite */
1825 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1826 /* FIXME: Use right error code */
1827 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1831 else /* fn2 == NULL means delete source */
1832 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1833 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1834 WARN(file
, "Illegal flag\n");
1835 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1839 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1840 Perhaps we should queue these command and execute it
1841 when exiting... What about using on_exit(2)
1843 FIXME(file
, "Please delete file '%s' when Wine has finished\n",
1844 full_name1
.long_name
);
1847 else if (unlink( full_name1
.long_name
) == -1)
1852 else return TRUE
; /* successfully deleted */
1854 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1855 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1856 Perhaps we should queue these command and execute it
1857 when exiting... What about using on_exit(2)
1859 FIXME(file
,"Please move existing file '%s' to file '%s'"
1860 "when Wine has finished\n",
1861 full_name1
.long_name
, full_name2
.long_name
);
1865 if (!mode
) /* move the file */
1866 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1872 else /* copy File */
1873 return CopyFile32A(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1877 /**************************************************************************
1878 * MoveFileEx32W (KERNEL32.???)
1880 BOOL32 WINAPI
MoveFileEx32W( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1882 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1883 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1884 BOOL32 res
= MoveFileEx32A( afn1
, afn2
, flag
);
1885 HeapFree( GetProcessHeap(), 0, afn1
);
1886 HeapFree( GetProcessHeap(), 0, afn2
);
1891 /**************************************************************************
1892 * MoveFile32A (KERNEL32.387)
1894 * Move file or directory
1896 BOOL32 WINAPI
MoveFile32A( LPCSTR fn1
, LPCSTR fn2
)
1898 DOS_FULL_NAME full_name1
, full_name2
;
1901 TRACE(file
, "(%s,%s)\n", fn1
, fn2
);
1903 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1904 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1905 /* The new name must not already exist */
1907 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1909 if (full_name1
.drive
== full_name2
.drive
) /* move */
1910 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1917 if (stat( full_name1
.long_name
, &fstat
))
1919 WARN(file
, "Invalid source file %s\n",
1920 full_name1
.long_name
);
1924 if (S_ISDIR(fstat
.st_mode
)) {
1925 /* No Move for directories across file systems */
1926 /* FIXME: Use right error code */
1927 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1932 return CopyFile32A(fn1
, fn2
, TRUE
); /*fail, if exist */
1937 /**************************************************************************
1938 * MoveFile32W (KERNEL32.390)
1940 BOOL32 WINAPI
MoveFile32W( LPCWSTR fn1
, LPCWSTR fn2
)
1942 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1943 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1944 BOOL32 res
= MoveFile32A( afn1
, afn2
);
1945 HeapFree( GetProcessHeap(), 0, afn1
);
1946 HeapFree( GetProcessHeap(), 0, afn2
);
1951 /**************************************************************************
1952 * CopyFile32A (KERNEL32.36)
1954 BOOL32 WINAPI
CopyFile32A( LPCSTR source
, LPCSTR dest
, BOOL32 fail_if_exists
)
1957 BY_HANDLE_FILE_INFORMATION info
;
1963 if ((h1
= _lopen32( source
, OF_READ
)) == HFILE_ERROR32
) return FALSE
;
1964 if (!GetFileInformationByHandle( h1
, &info
))
1969 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1970 if ((h2
= FILE_Create( dest
, mode
, fail_if_exists
)) == HFILE_ERROR32
)
1975 while ((count
= _lread32( h1
, buffer
, sizeof(buffer
) )) > 0)
1980 INT32 res
= _lwrite32( h2
, p
, count
);
1981 if (res
<= 0) goto done
;
1994 /**************************************************************************
1995 * CopyFile32W (KERNEL32.37)
1997 BOOL32 WINAPI
CopyFile32W( LPCWSTR source
, LPCWSTR dest
, BOOL32 fail_if_exists
)
1999 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
2000 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
2001 BOOL32 ret
= CopyFile32A( sourceA
, destA
, fail_if_exists
);
2002 HeapFree( GetProcessHeap(), 0, sourceA
);
2003 HeapFree( GetProcessHeap(), 0, destA
);
2008 /***********************************************************************
2009 * SetFileTime (KERNEL32.650)
2011 BOOL32 WINAPI
SetFileTime( HFILE32 hFile
,
2012 const FILETIME
*lpCreationTime
,
2013 const FILETIME
*lpLastAccessTime
,
2014 const FILETIME
*lpLastWriteTime
)
2016 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
2017 struct utimbuf utimbuf
;
2019 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
2020 TRACE(file
,"('%s',%p,%p,%p)\n",
2026 if (lpLastAccessTime
)
2027 utimbuf
.actime
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
2029 utimbuf
.actime
= 0; /* FIXME */
2030 if (lpLastWriteTime
)
2031 utimbuf
.modtime
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
2033 utimbuf
.modtime
= 0; /* FIXME */
2034 if (-1==utime(file
->unix_name
,&utimbuf
))
2036 MSG("Couldn't set the time for file '%s'. Insufficient permissions !?\n", file
->unix_name
);
2037 FILE_ReleaseFile( file
);
2041 FILE_ReleaseFile( file
);
2045 /* Locks need to be mirrored because unix file locking is based
2046 * on the pid. Inside of wine there can be multiple WINE processes
2047 * that share the same unix pid.
2048 * Read's and writes should check these locks also - not sure
2049 * how critical that is at this point (FIXME).
2052 static BOOL32
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
2054 DOS_FILE_LOCK
*curr
;
2057 processId
= GetCurrentProcessId();
2059 /* check if lock overlaps a current lock for the same file */
2060 for (curr
= locks
; curr
; curr
= curr
->next
) {
2061 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
2062 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
2063 return TRUE
;/* region is identic */
2064 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
2065 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
2066 /* region overlaps */
2072 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
2073 curr
->processId
= GetCurrentProcessId();
2074 curr
->base
= f
->l_start
;
2075 curr
->len
= f
->l_len
;
2076 curr
->unix_name
= HEAP_strdupA( SystemHeap
, 0, file
->unix_name
);
2078 curr
->dos_file
= file
;
2083 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
2086 DOS_FILE_LOCK
**curr
;
2089 processId
= GetCurrentProcessId();
2092 if ((*curr
)->dos_file
== file
) {
2094 *curr
= (*curr
)->next
;
2095 HeapFree( SystemHeap
, 0, rem
->unix_name
);
2096 HeapFree( SystemHeap
, 0, rem
);
2099 curr
= &(*curr
)->next
;
2103 static BOOL32
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2106 DOS_FILE_LOCK
**curr
;
2109 processId
= GetCurrentProcessId();
2110 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2111 if ((*curr
)->processId
== processId
&&
2112 (*curr
)->dos_file
== file
&&
2113 (*curr
)->base
== f
->l_start
&&
2114 (*curr
)->len
== f
->l_len
) {
2115 /* this is the same lock */
2117 *curr
= (*curr
)->next
;
2118 HeapFree( SystemHeap
, 0, rem
->unix_name
);
2119 HeapFree( SystemHeap
, 0, rem
);
2123 /* no matching lock found */
2128 /**************************************************************************
2129 * LockFile (KERNEL32.511)
2131 BOOL32 WINAPI
LockFile(
2132 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2133 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2138 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2139 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2140 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2142 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2143 FIXME(file
, "Unimplemented bytes > 32bits\n");
2147 f
.l_start
= dwFileOffsetLow
;
2148 f
.l_len
= nNumberOfBytesToLockLow
;
2149 f
.l_whence
= SEEK_SET
;
2153 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
2155 /* shadow locks internally */
2156 if (!DOS_AddLock(file
, &f
)) {
2157 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2161 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2162 #ifdef USE_UNIX_LOCKS
2163 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2164 if (errno
== EACCES
|| errno
== EAGAIN
) {
2165 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2170 /* remove our internal copy of the lock */
2171 DOS_RemoveLock(file
, &f
);
2179 /**************************************************************************
2180 * UnlockFile (KERNEL32.703)
2182 BOOL32 WINAPI
UnlockFile(
2183 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2184 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2189 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2190 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2191 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2193 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2194 WARN(file
, "Unimplemented bytes > 32bits\n");
2198 f
.l_start
= dwFileOffsetLow
;
2199 f
.l_len
= nNumberOfBytesToUnlockLow
;
2200 f
.l_whence
= SEEK_SET
;
2204 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
2206 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2208 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2209 #ifdef USE_UNIX_LOCKS
2210 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2218 /**************************************************************************
2219 * GetFileAttributesEx32A [KERNEL32.874]
2221 BOOL32 WINAPI
GetFileAttributesEx32A(
2222 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2223 LPVOID lpFileInformation
)
2225 DOS_FULL_NAME full_name
;
2226 BY_HANDLE_FILE_INFORMATION info
;
2228 if (lpFileName
== NULL
) return FALSE
;
2229 if (lpFileInformation
== NULL
) return FALSE
;
2231 if (fInfoLevelId
== GetFileExInfoStandard
) {
2232 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2233 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2234 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2235 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2237 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2238 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2239 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2240 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2241 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2242 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2245 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
2253 /**************************************************************************
2254 * GetFileAttributesEx32W [KERNEL32.875]
2256 BOOL32 WINAPI
GetFileAttributesEx32W(
2257 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2258 LPVOID lpFileInformation
)
2260 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2262 GetFileAttributesEx32A( nameA
, fInfoLevelId
, lpFileInformation
);
2263 HeapFree( GetProcessHeap(), 0, nameA
);