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 FIXME(file
,"test if file is RO missing\n");
421 FIXME(file
,"generate INT24 missing\n");
422 /* Is this the right error? */
423 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
427 FIXME(file
,"test if file is RO missing\n");
430 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
436 /***********************************************************************
439 * Look if the File is in Use For the OF_SHARE_XXX options
442 * name [I]: full unix name of the file that should be opened
443 * mode [O]: mode how the file was first opened
445 * TRUE if the file was opened before
446 * FALSE if we open the file exclusive for this process
448 * Scope of the files we look for is only the current pdb
449 * Could we use /proc/self/? on Linux for this?
450 * Should we use flock? Should we create another structure?
451 * Searching through all files seem quite expensive for me, but
452 * I don't see any other way.
454 * FIXME: Extend scope to the whole Wine process
457 static BOOL32
FILE_InUse(char * name
, int * mode
)
461 HGLOBAL16 hPDB
= GetCurrentPDB();
462 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
465 for (i
=0;i
<pdb
->nbFiles
;i
++)
467 file
=FILE_GetFile( (HFILE32
) i
);
472 TRACE(file
,"got %s at %d\n",file
->unix_name
,i
);
473 if(!lstrcmp32A(file
->unix_name
,name
))
476 FILE_ReleaseFile(file
);
479 FILE_ReleaseFile(file
);
486 /***********************************************************************
489 * Set the DOS error code from errno.
491 void FILE_SetDosError(void)
493 int save_errno
= errno
; /* errno gets overwritten by printf */
495 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
499 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
502 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
505 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
510 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
513 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
516 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
519 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
523 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
526 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
530 DOS_ERROR( ER_SeekError
, EC_NotFound
, SA_Ignore
, EL_Disk
);
533 DOS_ERROR( ERROR_DIR_NOT_EMPTY
, EC_Exists
, SA_Ignore
, EL_Disk
);
536 perror( "int21: unknown errno" );
537 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
544 /***********************************************************************
547 * Duplicate a Unix handle into a task handle.
549 HFILE32
FILE_DupUnixHandle( int fd
)
554 if ((handle
= FILE_Alloc( &file
)) != INVALID_HANDLE_VALUE32
)
556 if ((file
->unix_handle
= dup(fd
)) == -1)
559 CloseHandle( handle
);
560 return INVALID_HANDLE_VALUE32
;
567 /***********************************************************************
570 HFILE32
FILE_OpenUnixFile( const char *name
, int mode
)
576 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
577 return INVALID_HANDLE_VALUE32
;
579 if ((file
->unix_handle
= open( name
, mode
, 0666 )) == -1)
581 if (!Options
.failReadOnly
&& (mode
== O_RDWR
))
582 file
->unix_handle
= open( name
, O_RDONLY
);
584 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
587 CloseHandle( handle
);
588 return INVALID_HANDLE_VALUE32
;
590 if (S_ISDIR(st
.st_mode
))
592 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
593 CloseHandle( handle
);
594 return INVALID_HANDLE_VALUE32
;
597 /* File opened OK, now fill the FILE_OBJECT */
599 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, name
);
604 /***********************************************************************
607 * path[I] name of file to open
608 * mode[I] mode how to open, in unix notation
609 * shareMode[I] the sharing mode in the win OpenFile notation
612 HFILE32
FILE_Open( LPCSTR path
, INT32 mode
, INT32 shareMode
)
614 DOS_FULL_NAME full_name
;
615 const char *unixName
;
616 int oldMode
, dosMode
; /* FIXME: Do we really need unixmode as argument for
620 BOOL32 fileInUse
= FALSE
;
622 TRACE(file
, "'%s' %04x\n", path
, mode
);
624 if (!path
) return HFILE_ERROR32
;
626 if (DOSFS_GetDevice( path
))
630 TRACE(file
, "opening device '%s'\n", path
);
632 if (HFILE_ERROR32
!=(ret
=DOSFS_OpenDevice( path
, mode
)))
635 /* Do not silence this please. It is a critical error. -MM */
636 ERR(file
, "Couldn't open device '%s'!\n",path
);
637 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
638 return HFILE_ERROR32
;
641 else /* check for filename, don't check for last entry if creating */
643 if (!DOSFS_GetFullName( path
, !(mode
& O_CREAT
), &full_name
))
644 return HFILE_ERROR32
;
645 unixName
= full_name
.long_name
;
648 dosMode
= FILE_UnixToDosMode(mode
)| shareMode
;
649 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
652 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
653 if (FILE_ShareDeny(dosMode
,oldMode
)) return HFILE_ERROR32
;
655 hFileRet
= FILE_OpenUnixFile( unixName
, mode
);
656 /* we need to save the mode, but only if it is not in use yet*/
657 if ((hFileRet
) && (!fileInUse
) && ((file
=FILE_GetFile(hFileRet
))))
660 FILE_ReleaseFile(file
);
667 /***********************************************************************
670 static HFILE32
FILE_Create( LPCSTR path
, int mode
, int unique
)
674 DOS_FULL_NAME full_name
;
675 BOOL32 fileInUse
= FALSE
;
676 int oldMode
,dosMode
; /* FIXME: Do we really need unixmode as argument for
679 TRACE(file
, "'%s' %04x %d\n", path
, mode
, unique
);
681 if (!path
) return INVALID_HANDLE_VALUE32
;
683 if (DOSFS_GetDevice( path
))
685 WARN(file
, "cannot create DOS device '%s'!\n", path
);
686 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
687 return INVALID_HANDLE_VALUE32
;
690 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
691 return INVALID_HANDLE_VALUE32
;
693 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
))
695 CloseHandle( handle
);
696 return INVALID_HANDLE_VALUE32
;
699 dosMode
= FILE_UnixToDosMode(mode
);
700 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
703 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
704 if (FILE_ShareDeny(dosMode
,oldMode
)) return INVALID_HANDLE_VALUE32
;
707 if ((file
->unix_handle
= open( full_name
.long_name
,
708 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
712 CloseHandle( handle
);
713 return INVALID_HANDLE_VALUE32
;
716 /* File created OK, now fill the FILE_OBJECT */
718 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, full_name
.long_name
);
719 file
->mode
= dosMode
;
724 /***********************************************************************
727 * Fill a file information from a struct stat.
729 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
731 if (S_ISDIR(st
->st_mode
))
732 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
734 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
735 if (!(st
->st_mode
& S_IWUSR
))
736 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
738 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
739 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
740 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
742 info
->dwVolumeSerialNumber
= 0; /* FIXME */
743 info
->nFileSizeHigh
= 0;
744 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
745 info
->nNumberOfLinks
= st
->st_nlink
;
746 info
->nFileIndexHigh
= 0;
747 info
->nFileIndexLow
= st
->st_ino
;
751 /***********************************************************************
754 * Stat a Unix path name. Return TRUE if OK.
756 BOOL32
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
760 if (!unixName
|| !info
) return FALSE
;
762 if (stat( unixName
, &st
) == -1)
767 FILE_FillInfo( &st
, info
);
772 /***********************************************************************
773 * GetFileInformationByHandle (KERNEL32.219)
775 DWORD WINAPI
GetFileInformationByHandle( HFILE32 hFile
,
776 BY_HANDLE_FILE_INFORMATION
*info
)
784 if (!(file
= FILE_GetFile( hFile
))) return 0;
785 if (fstat( file
->unix_handle
, &st
) == -1) FILE_SetDosError();
788 FILE_FillInfo( &st
, info
);
791 FILE_ReleaseFile( file
);
796 /**************************************************************************
797 * GetFileAttributes16 (KERNEL.420)
799 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
801 return GetFileAttributes32A( name
);
805 /**************************************************************************
806 * GetFileAttributes32A (KERNEL32.217)
808 DWORD WINAPI
GetFileAttributes32A( LPCSTR name
)
810 DOS_FULL_NAME full_name
;
811 BY_HANDLE_FILE_INFORMATION info
;
813 if (name
== NULL
|| *name
=='\0') return -1;
815 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
816 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
817 return info
.dwFileAttributes
;
821 /**************************************************************************
822 * GetFileAttributes32W (KERNEL32.218)
824 DWORD WINAPI
GetFileAttributes32W( LPCWSTR name
)
826 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
827 DWORD res
= GetFileAttributes32A( nameA
);
828 HeapFree( GetProcessHeap(), 0, nameA
);
833 /***********************************************************************
834 * GetFileSize (KERNEL32.220)
836 DWORD WINAPI
GetFileSize( HFILE32 hFile
, LPDWORD filesizehigh
)
838 BY_HANDLE_FILE_INFORMATION info
;
839 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
840 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
841 return info
.nFileSizeLow
;
845 /***********************************************************************
846 * GetFileTime (KERNEL32.221)
848 BOOL32 WINAPI
GetFileTime( HFILE32 hFile
, FILETIME
*lpCreationTime
,
849 FILETIME
*lpLastAccessTime
,
850 FILETIME
*lpLastWriteTime
)
852 BY_HANDLE_FILE_INFORMATION info
;
853 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
854 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
855 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
856 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
860 /***********************************************************************
861 * CompareFileTime (KERNEL32.28)
863 INT32 WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
865 if (!x
|| !y
) return -1;
867 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
869 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
871 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
873 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
878 /***********************************************************************
881 * dup() function for DOS handles.
883 HFILE32
FILE_Dup( HFILE32 hFile
)
887 TRACE(file
, "FILE_Dup for handle %d\n", hFile
);
888 if (!DuplicateHandle( GetCurrentProcess(), hFile
, GetCurrentProcess(),
889 &handle
, FILE_ALL_ACCESS
/* FIXME */, FALSE
, 0 ))
890 handle
= HFILE_ERROR32
;
891 TRACE(file
, "FILE_Dup return handle %d\n", handle
);
896 /***********************************************************************
899 * dup2() function for DOS handles.
901 HFILE32
FILE_Dup2( HFILE32 hFile1
, HFILE32 hFile2
)
905 TRACE(file
, "FILE_Dup2 for handle %d\n", hFile1
);
906 /* FIXME: should use DuplicateHandle */
907 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR32
;
908 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2
, &file
->header
, 0 ))
909 hFile2
= HFILE_ERROR32
;
910 FILE_ReleaseFile( file
);
915 /***********************************************************************
916 * GetTempFileName16 (KERNEL.97)
918 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
923 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
924 drive
|= DRIVE_GetCurrentDrive() + 'A';
926 if ((drive
& TF_FORCEDRIVE
) &&
927 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
929 drive
&= ~TF_FORCEDRIVE
;
930 WARN(file
, "invalid drive %d specified\n", drive
);
933 if (drive
& TF_FORCEDRIVE
)
934 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
936 GetTempPath32A( 132, temppath
);
937 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
941 /***********************************************************************
942 * GetTempFileName32A (KERNEL32.290)
944 UINT32 WINAPI
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
947 DOS_FULL_NAME full_name
;
950 UINT32 num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
952 if ( !path
|| !prefix
|| !buffer
) return 0;
954 strcpy( buffer
, path
);
955 p
= buffer
+ strlen(buffer
);
957 /* add a \, if there isn't one and path is more than just the drive letter ... */
958 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
959 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
962 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
963 sprintf( p
, "%04x.tmp", num
);
965 /* Now try to create it */
971 HFILE32 handle
= FILE_Create( buffer
, 0666, TRUE
);
972 if (handle
!= INVALID_HANDLE_VALUE32
)
973 { /* We created it */
974 TRACE(file
, "created %s\n",
976 CloseHandle( handle
);
979 if (DOS_ExtendedError
!= ER_FileExists
)
980 break; /* No need to go on */
982 sprintf( p
, "%04x.tmp", num
);
983 } while (num
!= (unique
& 0xffff));
986 /* Get the full path name */
988 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
990 /* Check if we have write access in the directory */
991 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
992 if (access( full_name
.long_name
, W_OK
) == -1)
993 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
996 TRACE(file
, "returning %s\n", buffer
);
997 return unique
? unique
: num
;
1001 /***********************************************************************
1002 * GetTempFileName32W (KERNEL32.291)
1004 UINT32 WINAPI
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
1007 LPSTR patha
,prefixa
;
1011 if (!path
) return 0;
1012 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1013 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
1014 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
1015 lstrcpyAtoW( buffer
, buffera
);
1016 HeapFree( GetProcessHeap(), 0, patha
);
1017 HeapFree( GetProcessHeap(), 0, prefixa
);
1022 /***********************************************************************
1025 * Implementation of OpenFile16() and OpenFile32().
1027 static HFILE32
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
,
1032 WORD filedatetime
[2];
1033 DOS_FULL_NAME full_name
;
1035 int unixMode
, oldMode
;
1037 BOOL32 fileInUse
= FALSE
;
1039 if (!ofs
) return HFILE_ERROR32
;
1042 ofs
->cBytes
= sizeof(OFSTRUCT
);
1044 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
1047 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
1048 return HFILE_ERROR32
;
1051 TRACE(file
, "%s %04x\n", name
, mode
);
1053 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
1054 Are there any cases where getting the path here is wrong?
1055 Uwe Bonnes 1997 Apr 2 */
1056 if (!GetFullPathName32A( name
, sizeof(ofs
->szPathName
),
1057 ofs
->szPathName
, NULL
)) goto error
;
1059 /* OF_PARSE simply fills the structure */
1061 if (mode
& OF_PARSE
)
1063 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
1064 != DRIVE_REMOVABLE
);
1065 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
1066 name
, ofs
->szPathName
);
1070 /* OF_CREATE is completely different from all other options, so
1073 if (mode
& OF_CREATE
)
1075 if ((hFileRet
= FILE_Create(name
,0666,FALSE
))== INVALID_HANDLE_VALUE32
)
1080 /* If OF_SEARCH is set, ignore the given path */
1082 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
1084 /* First try the file name as is */
1085 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
1086 /* Now remove the path */
1087 if (name
[0] && (name
[1] == ':')) name
+= 2;
1088 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
1089 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
1090 if (!name
[0]) goto not_found
;
1093 /* Now look for the file */
1095 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
1098 TRACE(file
, "found %s = %s\n",
1099 full_name
.long_name
, full_name
.short_name
);
1100 lstrcpyn32A( ofs
->szPathName
, full_name
.short_name
,
1101 sizeof(ofs
->szPathName
) );
1103 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
1106 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
1107 if (FILE_ShareDeny(mode
,oldMode
)) return HFILE_ERROR32
;
1110 if (mode
& OF_SHARE_EXCLUSIVE
)
1111 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
1112 on the file <tempdir>/_ins0432._mp to determine how
1113 far installation has proceeded.
1114 _ins0432._mp is an executable and while running the
1115 application expects the open with OF_SHARE_ to fail*/
1117 As our loader closes the files after loading the executable,
1118 we can't find the running executable with FILE_InUse.
1119 Perhaps the loader should keep the file open.
1120 Recheck against how Win handles that case */
1122 char *last
= strrchr(full_name
.long_name
,'/');
1124 last
= full_name
.long_name
- 1;
1125 if (GetModuleHandle16(last
+1))
1127 TRACE(file
,"Denying shared open for %s\n",full_name
.long_name
);
1128 return HFILE_ERROR32
;
1132 if (mode
& OF_DELETE
)
1134 if (unlink( full_name
.long_name
) == -1) goto not_found
;
1135 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
1139 unixMode
=FILE_DOSToUnixMode(mode
);
1141 hFileRet
= FILE_OpenUnixFile( full_name
.long_name
, unixMode
);
1142 if (hFileRet
== HFILE_ERROR32
) goto not_found
;
1143 /* we need to save the mode, but only if it is not in use yet*/
1144 if( (!fileInUse
) &&(file
=FILE_GetFile(hFileRet
)))
1147 FILE_ReleaseFile(file
);
1150 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
1151 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
1152 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
1154 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
1156 CloseHandle( hFileRet
);
1157 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
1158 /* FIXME: what error here? */
1159 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1163 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
1165 success
: /* We get here if the open was successful */
1166 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
1167 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
1168 CloseHandle( hFileRet
);
1171 not_found
: /* We get here if the file does not exist */
1172 WARN(file
, "'%s' not found\n", name
);
1173 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1176 error
: /* We get here if there was an error opening the file */
1177 ofs
->nErrCode
= DOS_ExtendedError
;
1178 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
1179 name
,ofs
->nErrCode
);
1180 return HFILE_ERROR32
;
1184 /***********************************************************************
1185 * OpenFile16 (KERNEL.74)
1187 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
1189 TRACE(file
,"OpenFile16(%s,%i)\n", name
, mode
);
1190 return HFILE32_TO_HFILE16(FILE_DoOpenFile( name
, ofs
, mode
, FALSE
));
1194 /***********************************************************************
1195 * OpenFile32 (KERNEL32.396)
1197 HFILE32 WINAPI
OpenFile32( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
1199 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
1203 /***********************************************************************
1204 * _lclose16 (KERNEL.81)
1206 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1208 TRACE(file
, "handle %d\n", hFile
);
1209 return CloseHandle( HFILE16_TO_HFILE32( hFile
) ) ? 0 : HFILE_ERROR16
;
1213 /***********************************************************************
1214 * _lclose32 (KERNEL32.592)
1216 HFILE32 WINAPI
_lclose32( HFILE32 hFile
)
1218 TRACE(file
, "handle %d\n", hFile
);
1219 return CloseHandle( hFile
) ? 0 : HFILE_ERROR32
;
1223 /***********************************************************************
1226 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1230 TRACE(file
, "%d %08lx %ld\n",
1231 hFile
, (DWORD
)buffer
, count
);
1233 /* Some programs pass a count larger than the allocated buffer */
1234 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1235 if (count
> maxlen
) count
= maxlen
;
1236 return _lread32(HFILE16_TO_HFILE32(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1240 /***********************************************************************
1243 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1245 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1249 /***********************************************************************
1250 * _lread32 (KERNEL32.596)
1252 UINT32 WINAPI
_lread32( HFILE32 handle
, LPVOID buffer
, UINT32 count
)
1256 BOOL32 result
= FALSE
;
1258 TRACE( file
, "%d %p %d\n", handle
, buffer
, count
);
1259 if (!(ptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
1260 K32OBJ_UNKNOWN
, 0, NULL
))) return -1;
1261 if (K32OBJ_OPS(ptr
)->read
)
1262 result
= K32OBJ_OPS(ptr
)->read(ptr
, buffer
, count
, &numWritten
, NULL
);
1263 K32OBJ_DecCount( ptr
);
1264 if (!result
) return -1;
1265 return (UINT32
)numWritten
;
1269 /***********************************************************************
1270 * _lread16 (KERNEL.82)
1272 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1274 return (UINT16
)_lread32(HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1278 /***********************************************************************
1279 * _lcreat16 (KERNEL.83)
1281 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1283 int mode
= (attr
& 1) ? 0444 : 0666;
1284 TRACE(file
, "%s %02x\n", path
, attr
);
1285 return (HFILE16
) HFILE32_TO_HFILE16(FILE_Create( path
, mode
, FALSE
));
1289 /***********************************************************************
1290 * _lcreat32 (KERNEL32.593)
1292 HFILE32 WINAPI
_lcreat32( LPCSTR path
, INT32 attr
)
1294 int mode
= (attr
& 1) ? 0444 : 0666;
1295 TRACE(file
, "%s %02x\n", path
, attr
);
1296 return FILE_Create( path
, mode
, FALSE
);
1300 /***********************************************************************
1301 * _lcreat_uniq (Not a Windows API)
1303 HFILE32
_lcreat_uniq( LPCSTR path
, INT32 attr
)
1305 int mode
= (attr
& 1) ? 0444 : 0666;
1306 TRACE(file
, "%s %02x\n", path
, attr
);
1307 return FILE_Create( path
, mode
, TRUE
);
1311 /***********************************************************************
1312 * SetFilePointer (KERNEL32.492)
1314 DWORD WINAPI
SetFilePointer( HFILE32 hFile
, LONG distance
, LONG
*highword
,
1318 DWORD result
= 0xffffffff;
1320 if (highword
&& *highword
)
1322 FIXME(file
, "64-bit offsets not supported yet\n");
1323 SetLastError( ERROR_INVALID_PARAMETER
);
1326 TRACE(file
, "handle %d offset %ld origin %ld\n",
1327 hFile
, distance
, method
);
1329 if (!(file
= FILE_GetFile( hFile
))) return 0xffffffff;
1332 /* the pointer may be positioned before the start of the file;
1333 no error is returned in that case,
1334 but subsequent attempts at I/O will produce errors.
1335 This is not allowed with Unix lseek(),
1336 so we'll need some emulation here */
1340 distance
+= file
->pos
; /* fall through */
1342 if ((result
= lseek(file
->unix_handle
, distance
, SEEK_SET
)) == -1)
1344 if ((INT32
)distance
< 0)
1345 file
->pos
= result
= distance
;
1351 if ((result
= lseek(file
->unix_handle
, distance
, SEEK_END
)) == -1)
1353 if ((INT32
)distance
< 0)
1356 result
= lseek(file
->unix_handle
, 0, SEEK_END
);
1358 /* return to the old pos, as the first lseek failed */
1359 lseek(file
->unix_handle
, file
->pos
, SEEK_END
);
1361 file
->pos
= (result
+= distance
);
1364 ERR(file
, "lseek: unknown error. Please report.\n");
1366 else file
->pos
= result
;
1369 ERR(file
, "Unknown origin %ld !\n", method
);
1375 FILE_ReleaseFile( file
);
1380 /***********************************************************************
1381 * _llseek16 (KERNEL.84)
1383 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1385 return SetFilePointer( HFILE16_TO_HFILE32(hFile
), lOffset
, NULL
, nOrigin
);
1389 /***********************************************************************
1390 * _llseek32 (KERNEL32.594)
1392 LONG WINAPI
_llseek32( HFILE32 hFile
, LONG lOffset
, INT32 nOrigin
)
1394 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1398 /***********************************************************************
1399 * _lopen16 (KERNEL.85)
1401 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1403 return HFILE32_TO_HFILE16(_lopen32( path
, mode
));
1407 /***********************************************************************
1408 * _lopen32 (KERNEL32.595)
1410 HFILE32 WINAPI
_lopen32( LPCSTR path
, INT32 mode
)
1414 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1416 unixMode
= FILE_DOSToUnixMode(mode
);
1417 return FILE_Open( path
, unixMode
, (mode
& 0x70));
1421 /***********************************************************************
1422 * _lwrite16 (KERNEL.86)
1424 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1426 return (UINT16
)_hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1429 /***********************************************************************
1430 * _lwrite32 (KERNEL.86)
1432 UINT32 WINAPI
_lwrite32( HFILE32 hFile
, LPCSTR buffer
, UINT32 count
)
1434 return (UINT32
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1438 /***********************************************************************
1439 * _hread16 (KERNEL.349)
1441 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1443 return _lread32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1447 /***********************************************************************
1448 * _hread32 (KERNEL32.590)
1450 LONG WINAPI
_hread32( HFILE32 hFile
, LPVOID buffer
, LONG count
)
1452 return _lread32( hFile
, buffer
, count
);
1456 /***********************************************************************
1457 * _hwrite16 (KERNEL.350)
1459 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1461 return _hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1465 /***********************************************************************
1466 * _hwrite32 (KERNEL32.591)
1468 * experimenation yields that _lwrite:
1469 * o truncates the file at the current position with
1471 * o returns 0 on a 0 length write
1472 * o works with console handles
1475 LONG WINAPI
_hwrite32( HFILE32 handle
, LPCSTR buffer
, LONG count
)
1479 BOOL32 status
= FALSE
;
1481 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1483 if (count
== 0) { /* Expand or truncate at current position */
1484 FILE_OBJECT
*file
= FILE_GetFile(handle
);
1486 if ( ftruncate(file
->unix_handle
,
1487 lseek( file
->unix_handle
, 0, SEEK_CUR
)) == 0 ) {
1488 FILE_ReleaseFile(file
);
1492 FILE_ReleaseFile(file
);
1493 return HFILE_ERROR32
;
1497 if (!(ioptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
1498 K32OBJ_UNKNOWN
, 0, NULL
)))
1499 return HFILE_ERROR32
;
1500 if (K32OBJ_OPS(ioptr
)->write
)
1501 status
= K32OBJ_OPS(ioptr
)->write(ioptr
, buffer
, count
, &result
, NULL
);
1502 K32OBJ_DecCount( ioptr
);
1503 if (!status
) result
= HFILE_ERROR32
;
1508 /***********************************************************************
1509 * SetHandleCount16 (KERNEL.199)
1511 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1513 HGLOBAL16 hPDB
= GetCurrentPDB();
1514 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1515 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1517 TRACE(file
, "(%d)\n", count
);
1519 if (count
< 20) count
= 20; /* No point in going below 20 */
1520 else if (count
> 254) count
= 254;
1524 if (pdb
->nbFiles
> 20)
1526 memcpy( pdb
->fileHandles
, files
, 20 );
1527 GlobalFree16( pdb
->hFileHandles
);
1528 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1529 GlobalHandleToSel( hPDB
) );
1530 pdb
->hFileHandles
= 0;
1534 else /* More than 20, need a new file handles table */
1537 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1540 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1541 return pdb
->nbFiles
;
1543 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1545 if (count
> pdb
->nbFiles
)
1547 memcpy( newfiles
, files
, pdb
->nbFiles
);
1548 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1550 else memcpy( newfiles
, files
, count
);
1551 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1552 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1553 pdb
->hFileHandles
= newhandle
;
1554 pdb
->nbFiles
= count
;
1556 return pdb
->nbFiles
;
1560 /*************************************************************************
1561 * SetHandleCount32 (KERNEL32.494)
1563 UINT32 WINAPI
SetHandleCount32( UINT32 count
)
1565 return MIN( 256, count
);
1569 /***********************************************************************
1570 * FlushFileBuffers (KERNEL32.133)
1572 BOOL32 WINAPI
FlushFileBuffers( HFILE32 hFile
)
1577 TRACE(file
, "(%d)\n", hFile
);
1578 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1579 if (fsync( file
->unix_handle
) != -1) ret
= TRUE
;
1585 FILE_ReleaseFile( file
);
1590 /**************************************************************************
1591 * SetEndOfFile (KERNEL32.483)
1593 BOOL32 WINAPI
SetEndOfFile( HFILE32 hFile
)
1598 TRACE(file
, "(%d)\n", hFile
);
1599 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1600 if (ftruncate( file
->unix_handle
,
1601 lseek( file
->unix_handle
, 0, SEEK_CUR
) ))
1606 FILE_ReleaseFile( file
);
1611 /***********************************************************************
1612 * DeleteFile16 (KERNEL.146)
1614 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1616 return DeleteFile32A( path
);
1620 /***********************************************************************
1621 * DeleteFile32A (KERNEL32.71)
1623 BOOL32 WINAPI
DeleteFile32A( LPCSTR path
)
1625 DOS_FULL_NAME full_name
;
1627 TRACE(file
, "'%s'\n", path
);
1629 if (DOSFS_GetDevice( path
))
1631 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1632 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1636 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1637 if (unlink( full_name
.long_name
) == -1)
1646 /***********************************************************************
1647 * DeleteFile32W (KERNEL32.72)
1649 BOOL32 WINAPI
DeleteFile32W( LPCWSTR path
)
1651 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1652 BOOL32 ret
= DeleteFile32A( xpath
);
1653 HeapFree( GetProcessHeap(), 0, xpath
);
1658 /***********************************************************************
1661 BOOL32
FILE_SetFileType( HFILE32 hFile
, DWORD type
)
1663 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1664 if (!file
) return FALSE
;
1666 FILE_ReleaseFile( file
);
1671 /***********************************************************************
1674 LPVOID
FILE_mmap( HFILE32 hFile
, LPVOID start
,
1675 DWORD size_high
, DWORD size_low
,
1676 DWORD offset_high
, DWORD offset_low
,
1677 int prot
, int flags
)
1680 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1681 if (!file
) return (LPVOID
)-1;
1682 ret
= FILE_dommap( file
, start
, size_high
, size_low
,
1683 offset_high
, offset_low
, prot
, flags
);
1684 FILE_ReleaseFile( file
);
1689 /***********************************************************************
1692 LPVOID
FILE_dommap( FILE_OBJECT
*file
, LPVOID start
,
1693 DWORD size_high
, DWORD size_low
,
1694 DWORD offset_high
, DWORD offset_low
,
1695 int prot
, int flags
)
1701 if (size_high
|| offset_high
)
1702 FIXME(file
, "offsets larger than 4Gb not supported\n");
1709 static int fdzero
= -1;
1713 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1715 perror( "/dev/zero: open" );
1720 #endif /* MAP_ANON */
1721 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1723 flags
&= ~MAP_SHARED
;
1726 flags
|= MAP_PRIVATE
;
1729 else fd
= file
->unix_handle
;
1731 if ((ret
= mmap( start
, size_low
, prot
,
1732 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1735 /* mmap() failed; if this is because the file offset is not */
1736 /* page-aligned (EINVAL), or because the underlying filesystem */
1737 /* does not support mmap() (ENOEXEC), we do it by hand. */
1739 if (!file
) return ret
;
1740 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1741 if (prot
& PROT_WRITE
)
1743 /* We cannot fake shared write mappings */
1745 if (flags
& MAP_SHARED
) return ret
;
1748 if (!(flags
& MAP_PRIVATE
)) return ret
;
1751 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1752 /* Reserve the memory with an anonymous mmap */
1753 ret
= FILE_dommap( NULL
, start
, size_high
, size_low
, 0, 0,
1754 PROT_READ
| PROT_WRITE
, flags
);
1755 if (ret
== (LPVOID
)-1) return ret
;
1756 /* Now read in the file */
1757 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1759 FILE_munmap( ret
, size_high
, size_low
);
1762 read( fd
, ret
, size_low
);
1763 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1764 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1769 /***********************************************************************
1772 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1775 FIXME(file
, "offsets larger than 4Gb not supported\n");
1776 return munmap( start
, size_low
);
1780 /***********************************************************************
1781 * GetFileType (KERNEL32.222)
1783 DWORD WINAPI
GetFileType( HFILE32 hFile
)
1785 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1786 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1787 FILE_ReleaseFile( file
);
1792 /**************************************************************************
1793 * MoveFileEx32A (KERNEL32.???)
1795 BOOL32 WINAPI
MoveFileEx32A( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1797 DOS_FULL_NAME full_name1
, full_name2
;
1798 int mode
=0; /* mode == 1: use copy */
1800 TRACE(file
, "(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1802 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1803 if (fn2
) { /* !fn2 means delete fn1 */
1804 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1805 /* Source name and target path are valid */
1806 if ( full_name1
.drive
!= full_name2
.drive
)
1807 /* use copy, if allowed */
1808 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1809 /* FIXME: Use right error code */
1810 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
1814 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1815 /* target exists, check if we may overwrite */
1816 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1817 /* FIXME: Use right error code */
1818 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1822 else /* fn2 == NULL means delete source */
1823 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1824 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1825 WARN(file
, "Illegal flag\n");
1826 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1830 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1831 Perhaps we should queue these command and execute it
1832 when exiting... What about using on_exit(2)
1834 FIXME(file
, "Please delete file '%s' when Wine has finished\n",
1835 full_name1
.long_name
);
1838 else if (unlink( full_name1
.long_name
) == -1)
1843 else return TRUE
; /* successfully deleted */
1845 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1846 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1847 Perhaps we should queue these command and execute it
1848 when exiting... What about using on_exit(2)
1850 FIXME(file
,"Please move existing file '%s' to file '%s'"
1851 "when Wine has finished\n",
1852 full_name1
.long_name
, full_name2
.long_name
);
1856 if (!mode
) /* move the file */
1857 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1863 else /* copy File */
1864 return CopyFile32A(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1868 /**************************************************************************
1869 * MoveFileEx32W (KERNEL32.???)
1871 BOOL32 WINAPI
MoveFileEx32W( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1873 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1874 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1875 BOOL32 res
= MoveFileEx32A( afn1
, afn2
, flag
);
1876 HeapFree( GetProcessHeap(), 0, afn1
);
1877 HeapFree( GetProcessHeap(), 0, afn2
);
1882 /**************************************************************************
1883 * MoveFile32A (KERNEL32.387)
1885 * Move file or directory
1887 BOOL32 WINAPI
MoveFile32A( LPCSTR fn1
, LPCSTR fn2
)
1889 DOS_FULL_NAME full_name1
, full_name2
;
1892 TRACE(file
, "(%s,%s)\n", fn1
, fn2
);
1894 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1895 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1896 /* The new name must not already exist */
1898 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1900 if (full_name1
.drive
== full_name2
.drive
) /* move */
1901 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1908 if (stat( full_name1
.long_name
, &fstat
))
1910 WARN(file
, "Invalid source file %s\n",
1911 full_name1
.long_name
);
1915 if (S_ISDIR(fstat
.st_mode
)) {
1916 /* No Move for directories across file systems */
1917 /* FIXME: Use right error code */
1918 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1923 return CopyFile32A(fn1
, fn2
, TRUE
); /*fail, if exist */
1928 /**************************************************************************
1929 * MoveFile32W (KERNEL32.390)
1931 BOOL32 WINAPI
MoveFile32W( LPCWSTR fn1
, LPCWSTR fn2
)
1933 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1934 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1935 BOOL32 res
= MoveFile32A( afn1
, afn2
);
1936 HeapFree( GetProcessHeap(), 0, afn1
);
1937 HeapFree( GetProcessHeap(), 0, afn2
);
1942 /**************************************************************************
1943 * CopyFile32A (KERNEL32.36)
1945 BOOL32 WINAPI
CopyFile32A( LPCSTR source
, LPCSTR dest
, BOOL32 fail_if_exists
)
1948 BY_HANDLE_FILE_INFORMATION info
;
1954 if ((h1
= _lopen32( source
, OF_READ
)) == HFILE_ERROR32
) return FALSE
;
1955 if (!GetFileInformationByHandle( h1
, &info
))
1960 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1961 if ((h2
= FILE_Create( dest
, mode
, fail_if_exists
)) == HFILE_ERROR32
)
1966 while ((count
= _lread32( h1
, buffer
, sizeof(buffer
) )) > 0)
1971 INT32 res
= _lwrite32( h2
, p
, count
);
1972 if (res
<= 0) goto done
;
1985 /**************************************************************************
1986 * CopyFile32W (KERNEL32.37)
1988 BOOL32 WINAPI
CopyFile32W( LPCWSTR source
, LPCWSTR dest
, BOOL32 fail_if_exists
)
1990 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1991 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1992 BOOL32 ret
= CopyFile32A( sourceA
, destA
, fail_if_exists
);
1993 HeapFree( GetProcessHeap(), 0, sourceA
);
1994 HeapFree( GetProcessHeap(), 0, destA
);
1999 /***********************************************************************
2000 * SetFileTime (KERNEL32.650)
2002 BOOL32 WINAPI
SetFileTime( HFILE32 hFile
,
2003 const FILETIME
*lpCreationTime
,
2004 const FILETIME
*lpLastAccessTime
,
2005 const FILETIME
*lpLastWriteTime
)
2007 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
2008 struct utimbuf utimbuf
;
2010 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
2011 TRACE(file
,"('%s',%p,%p,%p)\n",
2017 if (lpLastAccessTime
)
2018 utimbuf
.actime
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
2020 utimbuf
.actime
= 0; /* FIXME */
2021 if (lpLastWriteTime
)
2022 utimbuf
.modtime
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
2024 utimbuf
.modtime
= 0; /* FIXME */
2025 if (-1==utime(file
->unix_name
,&utimbuf
))
2027 MSG("Couldn't set the time for file '%s'. Insufficient permissions !?\n", file
->unix_name
);
2028 FILE_ReleaseFile( file
);
2032 FILE_ReleaseFile( file
);
2036 /* Locks need to be mirrored because unix file locking is based
2037 * on the pid. Inside of wine there can be multiple WINE processes
2038 * that share the same unix pid.
2039 * Read's and writes should check these locks also - not sure
2040 * how critical that is at this point (FIXME).
2043 static BOOL32
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
2045 DOS_FILE_LOCK
*curr
;
2048 processId
= GetCurrentProcessId();
2050 /* check if lock overlaps a current lock for the same file */
2051 for (curr
= locks
; curr
; curr
= curr
->next
) {
2052 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
2053 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
2054 return TRUE
;/* region is identic */
2055 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
2056 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
2057 /* region overlaps */
2063 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
2064 curr
->processId
= GetCurrentProcessId();
2065 curr
->base
= f
->l_start
;
2066 curr
->len
= f
->l_len
;
2067 curr
->unix_name
= HEAP_strdupA( SystemHeap
, 0, file
->unix_name
);
2069 curr
->dos_file
= file
;
2074 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
2077 DOS_FILE_LOCK
**curr
;
2080 processId
= GetCurrentProcessId();
2083 if ((*curr
)->dos_file
== file
) {
2085 *curr
= (*curr
)->next
;
2086 HeapFree( SystemHeap
, 0, rem
->unix_name
);
2087 HeapFree( SystemHeap
, 0, rem
);
2090 curr
= &(*curr
)->next
;
2094 static BOOL32
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2097 DOS_FILE_LOCK
**curr
;
2100 processId
= GetCurrentProcessId();
2101 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2102 if ((*curr
)->processId
== processId
&&
2103 (*curr
)->dos_file
== file
&&
2104 (*curr
)->base
== f
->l_start
&&
2105 (*curr
)->len
== f
->l_len
) {
2106 /* this is the same lock */
2108 *curr
= (*curr
)->next
;
2109 HeapFree( SystemHeap
, 0, rem
->unix_name
);
2110 HeapFree( SystemHeap
, 0, rem
);
2114 /* no matching lock found */
2119 /**************************************************************************
2120 * LockFile (KERNEL32.511)
2122 BOOL32 WINAPI
LockFile(
2123 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2124 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2129 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2130 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2131 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2133 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2134 FIXME(file
, "Unimplemented bytes > 32bits\n");
2138 f
.l_start
= dwFileOffsetLow
;
2139 f
.l_len
= nNumberOfBytesToLockLow
;
2140 f
.l_whence
= SEEK_SET
;
2144 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
2146 /* shadow locks internally */
2147 if (!DOS_AddLock(file
, &f
)) {
2148 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2152 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2153 #ifdef USE_UNIX_LOCKS
2154 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2155 if (errno
== EACCES
|| errno
== EAGAIN
) {
2156 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2161 /* remove our internal copy of the lock */
2162 DOS_RemoveLock(file
, &f
);
2170 /**************************************************************************
2171 * UnlockFile (KERNEL32.703)
2173 BOOL32 WINAPI
UnlockFile(
2174 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2175 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2180 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2181 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2182 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2184 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2185 WARN(file
, "Unimplemented bytes > 32bits\n");
2189 f
.l_start
= dwFileOffsetLow
;
2190 f
.l_len
= nNumberOfBytesToUnlockLow
;
2191 f
.l_whence
= SEEK_SET
;
2195 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
2197 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2199 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2200 #ifdef USE_UNIX_LOCKS
2201 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2209 /**************************************************************************
2210 * GetFileAttributesEx32A [KERNEL32.874]
2212 BOOL32 WINAPI
GetFileAttributesEx32A(
2213 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2214 LPVOID lpFileInformation
)
2216 DOS_FULL_NAME full_name
;
2217 BY_HANDLE_FILE_INFORMATION info
;
2219 if (lpFileName
== NULL
) return FALSE
;
2220 if (lpFileInformation
== NULL
) return FALSE
;
2222 if (fInfoLevelId
== GetFileExInfoStandard
) {
2223 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2224 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2225 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2226 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2228 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2229 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2230 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2231 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2232 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2233 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2236 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
2244 /**************************************************************************
2245 * GetFileAttributesEx32W [KERNEL32.875]
2247 BOOL32 WINAPI
GetFileAttributesEx32W(
2248 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2249 LPVOID lpFileInformation
)
2251 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2253 GetFileAttributesEx32A( nameA
, fInfoLevelId
, lpFileInformation
);
2254 HeapFree( GetProcessHeap(), 0, nameA
);