2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
8 * Fix the CopyFileEx methods to implement the "extented" functionality.
9 * Right now, they simply call the CopyFile method.
18 #include <sys/errno.h>
19 #include <sys/types.h>
41 #include "server/request.h"
44 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
45 #define MAP_ANON MAP_ANONYMOUS
48 static void FILE_Destroy( K32OBJ
*obj
);
50 const K32OBJ_OPS FILE_Ops
=
52 FILE_Destroy
/* destroy */
55 struct DOS_FILE_LOCK
{
56 struct DOS_FILE_LOCK
* next
;
60 FILE_OBJECT
* dos_file
;
64 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
66 static DOS_FILE_LOCK
*locks
= NULL
;
67 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
69 /***********************************************************************
72 * Allocate a file. The unix_handle is closed.
74 HFILE32
FILE_Alloc( FILE_OBJECT
**file
, int unix_handle
, const char *unix_name
)
77 struct create_file_request req
;
78 struct create_file_reply reply
;
80 req
.access
= FILE_ALL_ACCESS
| GENERIC_READ
|
81 GENERIC_WRITE
| GENERIC_EXECUTE
; /* FIXME */
82 req
.inherit
= 1; /* FIXME */
83 CLIENT_SendRequest( REQ_CREATE_FILE
, unix_handle
, 1, &req
, sizeof(req
) );
84 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
85 if (reply
.handle
== -1) return INVALID_HANDLE_VALUE32
;
87 *file
= HeapAlloc( SystemHeap
, 0, sizeof(FILE_OBJECT
) );
90 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
91 CLIENT_CloseHandle( reply
.handle
);
94 (*file
)->header
.type
= K32OBJ_FILE
;
95 (*file
)->header
.refcount
= 0;
96 (*file
)->unix_name
= unix_name
? HEAP_strdupA( SystemHeap
, 0, unix_name
) : NULL
;
97 (*file
)->type
= FILE_TYPE_DISK
;
100 handle
= HANDLE_Alloc( PROCESS_Current(), &(*file
)->header
, req
.access
,
101 req
.inherit
, reply
.handle
);
102 /* If the allocation failed, the object is already destroyed */
103 if (handle
== INVALID_HANDLE_VALUE32
) *file
= NULL
;
108 /***********************************************************************
111 * Destroy a DOS file.
113 static void FILE_Destroy( K32OBJ
*ptr
)
115 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
116 assert( ptr
->type
== K32OBJ_FILE
);
118 DOS_RemoveFileLocks(file
);
120 if (file
->unix_name
) HeapFree( SystemHeap
, 0, file
->unix_name
);
121 ptr
->type
= K32OBJ_UNKNOWN
;
122 HeapFree( SystemHeap
, 0, file
);
126 /***********************************************************************
129 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
130 * be called to release the file.
132 FILE_OBJECT
*FILE_GetFile( HFILE32 handle
, DWORD access
, int *server_handle
)
134 return (FILE_OBJECT
*)HANDLE_GetObjPtr( PROCESS_Current(), handle
,
140 /***********************************************************************
143 * Release a DOS file obtained with FILE_GetFile.
145 void FILE_ReleaseFile( FILE_OBJECT
*file
)
147 K32OBJ_DecCount( &file
->header
);
151 /***********************************************************************
159 static int FILE_UnixToDosMode(int unixMode
)
168 dosMode
=OF_READWRITE
;
178 /***********************************************************************
186 static int FILE_DOSToUnixMode(int dosMode
)
192 unixMode
= O_WRONLY
; break;
194 unixMode
= O_RDWR
; break;
197 unixMode
= O_RDONLY
; break;
202 /***********************************************************************
206 * oldmode[I] mode how file was first opened
207 * mode[I] mode how the file should get opened
212 * Look what we have to do with the given SHARE modes
214 * Ralph Brown's interrupt list gives following explication, I guess
215 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
217 * FIXME: Validate this function
218 ========from Ralph Brown's list =========
220 Values of DOS file sharing behavior:
221 | Second and subsequent Opens
222 First |Compat Deny Deny Deny Deny
223 Open | All Write Read None
224 |R W RW R W RW R W RW R W RW R W RW
225 - - - - -| - - - - - - - - - - - - - - - - -
226 Compat R |Y Y Y N N N 1 N N N N N 1 N N
227 W |Y Y Y N N N N N N N N N N N N
228 RW|Y Y Y N N N N N N N N N N N N
230 Deny R |C C C N N N N N N N N N N N N
231 All W |C C C N N N N N N N N N N N N
232 RW|C C C N N N N N N N N N N N N
234 Deny R |2 C C N N N Y N N N N N Y N N
235 Write W |C C C N N N N N N Y N N Y N N
236 RW|C C C N N N N N N N N N Y N N
238 Deny R |C C C N N N N Y N N N N N Y N
239 Read W |C C C N N N N N N N Y N N Y N
240 RW|C C C N N N N N N N N N N Y N
242 Deny R |2 C C N N N Y Y Y N N N Y Y Y
243 None W |C C C N N N N N N Y Y Y Y Y Y
244 RW|C C C N N N N N N N N N Y Y Y
245 Legend: Y = open succeeds, N = open fails with error code 05h
246 C = open fails, INT 24 generated
247 1 = open succeeds if file read-only, else fails with error code
248 2 = open succeeds if file read-only, else fails with INT 24
249 ========end of description from Ralph Brown's List =====
250 For every "Y" in the table we return FALSE
251 For every "N" we set the DOS_ERROR and return TRUE
252 For all other cases we barf,set the DOS_ERROR and return TRUE
255 static BOOL32
FILE_ShareDeny( int mode
, int oldmode
)
257 int oldsharemode
= oldmode
& 0x70;
258 int sharemode
= mode
& 0x70;
259 int oldopenmode
= oldmode
& 3;
260 int openmode
= mode
& 3;
262 switch (oldsharemode
)
264 case OF_SHARE_COMPAT
:
265 if (sharemode
== OF_SHARE_COMPAT
) return FALSE
;
266 if (openmode
== OF_READ
) goto test_ro_err05
;
268 case OF_SHARE_EXCLUSIVE
:
269 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
271 case OF_SHARE_DENY_WRITE
:
272 if (openmode
!= OF_READ
)
274 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
279 case OF_SHARE_COMPAT
:
280 if (oldopenmode
== OF_READ
) goto test_ro_int24
;
282 case OF_SHARE_DENY_NONE
:
284 case OF_SHARE_DENY_WRITE
:
285 if (oldopenmode
== OF_READ
) return FALSE
;
286 case OF_SHARE_DENY_READ
:
287 if (oldopenmode
== OF_WRITE
) return FALSE
;
288 case OF_SHARE_EXCLUSIVE
:
293 case OF_SHARE_DENY_READ
:
294 if (openmode
!= OF_WRITE
)
296 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
301 case OF_SHARE_COMPAT
:
303 case OF_SHARE_DENY_NONE
:
305 case OF_SHARE_DENY_WRITE
:
306 if (oldopenmode
== OF_READ
) return FALSE
;
307 case OF_SHARE_DENY_READ
:
308 if (oldopenmode
== OF_WRITE
) return FALSE
;
309 case OF_SHARE_EXCLUSIVE
:
314 case OF_SHARE_DENY_NONE
:
317 case OF_SHARE_COMPAT
:
319 case OF_SHARE_DENY_NONE
:
321 case OF_SHARE_DENY_WRITE
:
322 if (oldopenmode
== OF_READ
) return FALSE
;
323 case OF_SHARE_DENY_READ
:
324 if (oldopenmode
== OF_WRITE
) return FALSE
;
325 case OF_SHARE_EXCLUSIVE
:
330 ERR(file
,"unknown mode\n");
332 ERR(file
,"shouldn't happen\n");
333 ERR(file
,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
337 if (oldmode
== OF_READ
)
341 FIXME(file
,"generate INT24 missing\n");
342 /* Is this the right error? */
343 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
347 if (oldmode
== OF_READ
)
351 TRACE(file
,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode
,mode
);
352 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
358 /***********************************************************************
361 * Look if the File is in Use For the OF_SHARE_XXX options
364 * name [I]: full unix name of the file that should be opened
365 * mode [O]: mode how the file was first opened
367 * TRUE if the file was opened before
368 * FALSE if we open the file exclusive for this process
370 * Scope of the files we look for is only the current pdb
371 * Could we use /proc/self/? on Linux for this?
372 * Should we use flock? Should we create another structure?
373 * Searching through all files seem quite expensive for me, but
374 * I don't see any other way.
376 * FIXME: Extend scope to the whole Wine process
379 static BOOL32
FILE_InUse(char * name
, int * mode
)
383 HGLOBAL16 hPDB
= GetCurrentPDB();
384 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
387 for (i
=0;i
<pdb
->nbFiles
;i
++)
389 file
=FILE_GetFile( (HFILE32
)i
, 0, NULL
);
394 TRACE(file
,"got %s at %d\n",file
->unix_name
,i
);
395 if(!lstrcmp32A(file
->unix_name
,name
))
398 FILE_ReleaseFile(file
);
402 FILE_ReleaseFile(file
);
408 /***********************************************************************
411 * Set the DOS error code from errno.
413 void FILE_SetDosError(void)
415 int save_errno
= errno
; /* errno gets overwritten by printf */
417 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
421 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
424 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
427 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
432 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
435 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
438 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
441 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
445 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
448 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
452 DOS_ERROR( ER_SeekError
, EC_NotFound
, SA_Ignore
, EL_Disk
);
455 DOS_ERROR( ERROR_DIR_NOT_EMPTY
, EC_Exists
, SA_Ignore
, EL_Disk
);
458 perror( "int21: unknown errno" );
459 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
466 /***********************************************************************
469 * Duplicate a Unix handle into a task handle.
471 HFILE32
FILE_DupUnixHandle( int fd
)
476 if ((unix_handle
= dup(fd
)) == -1)
479 return INVALID_HANDLE_VALUE32
;
481 return FILE_Alloc( &file
, unix_handle
, NULL
);
485 /***********************************************************************
488 HFILE32
FILE_OpenUnixFile( const char *name
, int mode
)
494 if ((unix_handle
= open( name
, mode
, 0666 )) == -1)
496 if (!Options
.failReadOnly
&& (mode
== O_RDWR
))
497 unix_handle
= open( name
, O_RDONLY
);
499 if ((unix_handle
== -1) || (fstat( unix_handle
, &st
) == -1))
502 return INVALID_HANDLE_VALUE32
;
504 if (S_ISDIR(st
.st_mode
))
506 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
507 close( unix_handle
);
508 return INVALID_HANDLE_VALUE32
;
511 /* File opened OK, now allocate a handle */
513 return FILE_Alloc( &file
, unix_handle
, name
);
517 /***********************************************************************
520 * path[I] name of file to open
521 * mode[I] mode how to open, in unix notation
522 * shareMode[I] the sharing mode in the win OpenFile notation
525 HFILE32
FILE_Open( LPCSTR path
, INT32 mode
, INT32 shareMode
)
527 DOS_FULL_NAME full_name
;
528 const char *unixName
;
529 int oldMode
, dosMode
; /* FIXME: Do we really need unixmode as argument for
533 BOOL32 fileInUse
= FALSE
;
535 TRACE(file
, "'%s' %04x\n", path
, mode
);
537 if (!path
) return HFILE_ERROR32
;
539 if (DOSFS_GetDevice( path
))
543 TRACE(file
, "opening device '%s'\n", path
);
545 if (HFILE_ERROR32
!=(ret
=DOSFS_OpenDevice( path
, mode
)))
548 /* Do not silence this please. It is a critical error. -MM */
549 ERR(file
, "Couldn't open device '%s'!\n",path
);
550 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
551 return HFILE_ERROR32
;
554 else /* check for filename, don't check for last entry if creating */
556 if (!DOSFS_GetFullName( path
, !(mode
& O_CREAT
), &full_name
))
557 return HFILE_ERROR32
;
558 unixName
= full_name
.long_name
;
561 dosMode
= FILE_UnixToDosMode(mode
)| shareMode
;
562 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
565 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
566 if (FILE_ShareDeny(dosMode
,oldMode
)) return HFILE_ERROR32
;
568 hFileRet
= FILE_OpenUnixFile( unixName
, mode
);
569 /* we need to save the mode, but only if it is not in use yet*/
570 if ((hFileRet
) && (!fileInUse
) && ((file
=FILE_GetFile(hFileRet
, 0, NULL
))))
573 FILE_ReleaseFile(file
);
580 /***********************************************************************
583 static HFILE32
FILE_Create( LPCSTR path
, int mode
, int unique
)
588 DOS_FULL_NAME full_name
;
589 BOOL32 fileInUse
= FALSE
;
590 int oldMode
,dosMode
; /* FIXME: Do we really need unixmode as argument for
593 TRACE(file
, "'%s' %04x %d\n", path
, mode
, unique
);
595 if (!path
) return INVALID_HANDLE_VALUE32
;
597 if (DOSFS_GetDevice( path
))
599 WARN(file
, "cannot create DOS device '%s'!\n", path
);
600 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
601 return INVALID_HANDLE_VALUE32
;
604 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return INVALID_HANDLE_VALUE32
;
606 dosMode
= FILE_UnixToDosMode(mode
);
607 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
610 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
611 if (FILE_ShareDeny(dosMode
,oldMode
)) return INVALID_HANDLE_VALUE32
;
614 if ((unix_handle
= open( full_name
.long_name
,
615 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
619 return INVALID_HANDLE_VALUE32
;
622 /* File created OK, now fill the FILE_OBJECT */
624 if ((handle
= FILE_Alloc( &file
, unix_handle
,
625 full_name
.long_name
)) == INVALID_HANDLE_VALUE32
)
626 return INVALID_HANDLE_VALUE32
;
627 file
->mode
= dosMode
;
632 /***********************************************************************
635 * Fill a file information from a struct stat.
637 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
639 if (S_ISDIR(st
->st_mode
))
640 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
642 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
643 if (!(st
->st_mode
& S_IWUSR
))
644 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
646 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
647 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
648 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
650 info
->dwVolumeSerialNumber
= 0; /* FIXME */
651 info
->nFileSizeHigh
= 0;
652 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
653 info
->nNumberOfLinks
= st
->st_nlink
;
654 info
->nFileIndexHigh
= 0;
655 info
->nFileIndexLow
= st
->st_ino
;
659 /***********************************************************************
662 * Stat a Unix path name. Return TRUE if OK.
664 BOOL32
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
668 if (!unixName
|| !info
) return FALSE
;
670 if (stat( unixName
, &st
) == -1)
675 FILE_FillInfo( &st
, info
);
680 /***********************************************************************
681 * GetFileInformationByHandle (KERNEL32.219)
683 DWORD WINAPI
GetFileInformationByHandle( HFILE32 hFile
,
684 BY_HANDLE_FILE_INFORMATION
*info
)
686 struct get_file_info_request req
;
687 struct get_file_info_reply reply
;
690 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
691 K32OBJ_FILE
, 0 )) == -1)
693 CLIENT_SendRequest( REQ_GET_FILE_INFO
, -1, 1, &req
, sizeof(req
) );
694 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
))
696 DOSFS_UnixTimeToFileTime( reply
.write_time
, &info
->ftCreationTime
, 0 );
697 DOSFS_UnixTimeToFileTime( reply
.write_time
, &info
->ftLastWriteTime
, 0 );
698 DOSFS_UnixTimeToFileTime( reply
.access_time
, &info
->ftLastAccessTime
, 0 );
699 info
->dwFileAttributes
= reply
.attr
;
700 info
->dwVolumeSerialNumber
= reply
.serial
;
701 info
->nFileSizeHigh
= reply
.size_high
;
702 info
->nFileSizeLow
= reply
.size_low
;
703 info
->nNumberOfLinks
= reply
.links
;
704 info
->nFileIndexHigh
= reply
.index_high
;
705 info
->nFileIndexLow
= reply
.index_low
;
710 /**************************************************************************
711 * GetFileAttributes16 (KERNEL.420)
713 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
715 return GetFileAttributes32A( name
);
719 /**************************************************************************
720 * GetFileAttributes32A (KERNEL32.217)
722 DWORD WINAPI
GetFileAttributes32A( LPCSTR name
)
724 DOS_FULL_NAME full_name
;
725 BY_HANDLE_FILE_INFORMATION info
;
727 if (name
== NULL
|| *name
=='\0') return -1;
729 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
730 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
731 return info
.dwFileAttributes
;
735 /**************************************************************************
736 * GetFileAttributes32W (KERNEL32.218)
738 DWORD WINAPI
GetFileAttributes32W( LPCWSTR name
)
740 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
741 DWORD res
= GetFileAttributes32A( nameA
);
742 HeapFree( GetProcessHeap(), 0, nameA
);
747 /***********************************************************************
748 * GetFileSize (KERNEL32.220)
750 DWORD WINAPI
GetFileSize( HFILE32 hFile
, LPDWORD filesizehigh
)
752 BY_HANDLE_FILE_INFORMATION info
;
753 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
754 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
755 return info
.nFileSizeLow
;
759 /***********************************************************************
760 * GetFileTime (KERNEL32.221)
762 BOOL32 WINAPI
GetFileTime( HFILE32 hFile
, FILETIME
*lpCreationTime
,
763 FILETIME
*lpLastAccessTime
,
764 FILETIME
*lpLastWriteTime
)
766 BY_HANDLE_FILE_INFORMATION info
;
767 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
768 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
769 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
770 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
774 /***********************************************************************
775 * CompareFileTime (KERNEL32.28)
777 INT32 WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
779 if (!x
|| !y
) return -1;
781 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
783 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
785 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
787 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
792 /***********************************************************************
795 * dup() function for DOS handles.
797 HFILE32
FILE_Dup( HFILE32 hFile
)
801 TRACE(file
, "FILE_Dup for handle %d\n", hFile
);
802 if (!DuplicateHandle( GetCurrentProcess(), hFile
, GetCurrentProcess(),
803 &handle
, FILE_ALL_ACCESS
/* FIXME */, FALSE
, 0 ))
804 handle
= HFILE_ERROR32
;
805 TRACE(file
, "FILE_Dup return handle %d\n", handle
);
810 /***********************************************************************
813 * dup2() function for DOS handles.
815 HFILE32
FILE_Dup2( HFILE32 hFile1
, HFILE32 hFile2
)
819 TRACE(file
, "FILE_Dup2 for handle %d\n", hFile1
);
820 /* FIXME: should use DuplicateHandle */
821 if (!(file
= FILE_GetFile( hFile1
, 0, NULL
))) return HFILE_ERROR32
;
822 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2
, &file
->header
, 0 ))
823 hFile2
= HFILE_ERROR32
;
824 FILE_ReleaseFile( file
);
829 /***********************************************************************
830 * GetTempFileName16 (KERNEL.97)
832 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
837 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
838 drive
|= DRIVE_GetCurrentDrive() + 'A';
840 if ((drive
& TF_FORCEDRIVE
) &&
841 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
843 drive
&= ~TF_FORCEDRIVE
;
844 WARN(file
, "invalid drive %d specified\n", drive
);
847 if (drive
& TF_FORCEDRIVE
)
848 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
850 GetTempPath32A( 132, temppath
);
851 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
855 /***********************************************************************
856 * GetTempFileName32A (KERNEL32.290)
858 UINT32 WINAPI
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
861 static UINT32 unique_temp
;
862 DOS_FULL_NAME full_name
;
867 if ( !path
|| !prefix
|| !buffer
) return 0;
869 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
870 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
872 strcpy( buffer
, path
);
873 p
= buffer
+ strlen(buffer
);
875 /* add a \, if there isn't one and path is more than just the drive letter ... */
876 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
877 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
880 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
881 sprintf( p
, "%04x.tmp", num
);
883 /* Now try to create it */
889 HFILE32 handle
= FILE_Create( buffer
, 0666, TRUE
);
890 if (handle
!= INVALID_HANDLE_VALUE32
)
891 { /* We created it */
892 TRACE(file
, "created %s\n",
894 CloseHandle( handle
);
897 if (DOS_ExtendedError
!= ER_FileExists
)
898 break; /* No need to go on */
900 sprintf( p
, "%04x.tmp", num
);
901 } while (num
!= (unique
& 0xffff));
904 /* Get the full path name */
906 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
908 /* Check if we have write access in the directory */
909 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
910 if (access( full_name
.long_name
, W_OK
) == -1)
911 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
914 TRACE(file
, "returning %s\n", buffer
);
915 return unique
? unique
: num
;
919 /***********************************************************************
920 * GetTempFileName32W (KERNEL32.291)
922 UINT32 WINAPI
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
930 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
931 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
932 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
933 lstrcpyAtoW( buffer
, buffera
);
934 HeapFree( GetProcessHeap(), 0, patha
);
935 HeapFree( GetProcessHeap(), 0, prefixa
);
940 /***********************************************************************
943 * Implementation of OpenFile16() and OpenFile32().
945 static HFILE32
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
,
950 WORD filedatetime
[2];
951 DOS_FULL_NAME full_name
;
953 int unixMode
, oldMode
;
955 BOOL32 fileInUse
= FALSE
;
957 if (!ofs
) return HFILE_ERROR32
;
960 ofs
->cBytes
= sizeof(OFSTRUCT
);
962 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
965 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
966 return HFILE_ERROR32
;
969 TRACE(file
, "%s %04x\n", name
, mode
);
971 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
972 Are there any cases where getting the path here is wrong?
973 Uwe Bonnes 1997 Apr 2 */
974 if (!GetFullPathName32A( name
, sizeof(ofs
->szPathName
),
975 ofs
->szPathName
, NULL
)) goto error
;
977 /* OF_PARSE simply fills the structure */
981 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
983 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
984 name
, ofs
->szPathName
);
988 /* OF_CREATE is completely different from all other options, so
991 if (mode
& OF_CREATE
)
993 if ((hFileRet
= FILE_Create(name
,0666,FALSE
))== INVALID_HANDLE_VALUE32
)
998 /* If OF_SEARCH is set, ignore the given path */
1000 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
1002 /* First try the file name as is */
1003 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
1004 /* Now remove the path */
1005 if (name
[0] && (name
[1] == ':')) name
+= 2;
1006 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
1007 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
1008 if (!name
[0]) goto not_found
;
1011 /* Now look for the file */
1013 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
1016 TRACE(file
, "found %s = %s\n",
1017 full_name
.long_name
, full_name
.short_name
);
1018 lstrcpyn32A( ofs
->szPathName
, full_name
.short_name
,
1019 sizeof(ofs
->szPathName
) );
1021 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
1024 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
1025 if (FILE_ShareDeny(mode
,oldMode
)) return HFILE_ERROR32
;
1028 if (mode
& OF_SHARE_EXCLUSIVE
)
1029 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
1030 on the file <tempdir>/_ins0432._mp to determine how
1031 far installation has proceeded.
1032 _ins0432._mp is an executable and while running the
1033 application expects the open with OF_SHARE_ to fail*/
1035 As our loader closes the files after loading the executable,
1036 we can't find the running executable with FILE_InUse.
1037 Perhaps the loader should keep the file open.
1038 Recheck against how Win handles that case */
1040 char *last
= strrchr(full_name
.long_name
,'/');
1042 last
= full_name
.long_name
- 1;
1043 if (GetModuleHandle16(last
+1))
1045 TRACE(file
,"Denying shared open for %s\n",full_name
.long_name
);
1046 return HFILE_ERROR32
;
1050 if (mode
& OF_DELETE
)
1052 if (unlink( full_name
.long_name
) == -1) goto not_found
;
1053 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
1057 unixMode
=FILE_DOSToUnixMode(mode
);
1059 hFileRet
= FILE_OpenUnixFile( full_name
.long_name
, unixMode
);
1060 if (hFileRet
== HFILE_ERROR32
) goto not_found
;
1061 /* we need to save the mode, but only if it is not in use yet*/
1062 if( (!fileInUse
) &&(file
=FILE_GetFile(hFileRet
,0,NULL
)))
1065 FILE_ReleaseFile(file
);
1068 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
1069 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
1070 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
1072 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
1074 CloseHandle( hFileRet
);
1075 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
1076 /* FIXME: what error here? */
1077 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1081 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
1083 success
: /* We get here if the open was successful */
1084 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
1085 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
1086 CloseHandle( hFileRet
);
1089 not_found
: /* We get here if the file does not exist */
1090 WARN(file
, "'%s' not found\n", name
);
1091 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1094 error
: /* We get here if there was an error opening the file */
1095 ofs
->nErrCode
= DOS_ExtendedError
;
1096 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
1097 name
,ofs
->nErrCode
);
1098 return HFILE_ERROR32
;
1102 /***********************************************************************
1103 * OpenFile16 (KERNEL.74)
1105 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
1107 TRACE(file
,"OpenFile16(%s,%i)\n", name
, mode
);
1108 return HFILE32_TO_HFILE16(FILE_DoOpenFile( name
, ofs
, mode
, FALSE
));
1112 /***********************************************************************
1113 * OpenFile32 (KERNEL32.396)
1115 HFILE32 WINAPI
OpenFile32( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
1117 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
1121 /***********************************************************************
1122 * _lclose16 (KERNEL.81)
1124 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1126 TRACE(file
, "handle %d\n", hFile
);
1127 return CloseHandle( HFILE16_TO_HFILE32( hFile
) ) ? 0 : HFILE_ERROR16
;
1131 /***********************************************************************
1132 * _lclose32 (KERNEL32.592)
1134 HFILE32 WINAPI
_lclose32( HFILE32 hFile
)
1136 TRACE(file
, "handle %d\n", hFile
);
1137 return CloseHandle( hFile
) ? 0 : HFILE_ERROR32
;
1141 /***********************************************************************
1142 * ReadFile (KERNEL32.428)
1144 BOOL32 WINAPI
ReadFile( HANDLE32 hFile
, LPVOID buffer
, DWORD bytesToRead
,
1145 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
1147 struct get_read_fd_request req
;
1148 int unix_handle
, result
;
1150 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToRead
);
1152 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
1153 if (!bytesToRead
) return TRUE
;
1155 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1156 K32OBJ_UNKNOWN
, GENERIC_READ
)) == -1)
1158 CLIENT_SendRequest( REQ_GET_READ_FD
, -1, 1, &req
, sizeof(req
) );
1159 CLIENT_WaitReply( NULL
, &unix_handle
, 0 );
1160 if (unix_handle
== -1) return FALSE
;
1161 while ((result
= read( unix_handle
, buffer
, bytesToRead
)) == -1)
1163 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1167 close( unix_handle
);
1168 if (result
== -1) return FALSE
;
1169 if (bytesRead
) *bytesRead
= result
;
1174 /***********************************************************************
1175 * WriteFile (KERNEL32.578)
1177 BOOL32 WINAPI
WriteFile( HANDLE32 hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
1178 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
1180 struct get_write_fd_request req
;
1181 int unix_handle
, result
;
1183 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToWrite
);
1185 if (bytesWritten
) *bytesWritten
= 0; /* Do this before anything else */
1186 if (!bytesToWrite
) return TRUE
;
1188 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1189 K32OBJ_UNKNOWN
, GENERIC_READ
)) == -1)
1191 CLIENT_SendRequest( REQ_GET_WRITE_FD
, -1, 1, &req
, sizeof(req
) );
1192 CLIENT_WaitReply( NULL
, &unix_handle
, 0 );
1193 if (unix_handle
== -1) return FALSE
;
1194 while ((result
= write( unix_handle
, buffer
, bytesToWrite
)) == -1)
1196 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1200 close( unix_handle
);
1201 if (result
== -1) return FALSE
;
1202 if (bytesWritten
) *bytesWritten
= result
;
1207 /***********************************************************************
1210 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1214 TRACE(file
, "%d %08lx %ld\n",
1215 hFile
, (DWORD
)buffer
, count
);
1217 /* Some programs pass a count larger than the allocated buffer */
1218 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1219 if (count
> maxlen
) count
= maxlen
;
1220 return _lread32(HFILE16_TO_HFILE32(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1224 /***********************************************************************
1227 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1229 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1233 /***********************************************************************
1234 * _lread32 (KERNEL32.596)
1236 UINT32 WINAPI
_lread32( HFILE32 handle
, LPVOID buffer
, UINT32 count
)
1239 if (!ReadFile( handle
, buffer
, count
, &result
, NULL
)) return -1;
1244 /***********************************************************************
1245 * _lread16 (KERNEL.82)
1247 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1249 return (UINT16
)_lread32(HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1253 /***********************************************************************
1254 * _lcreat16 (KERNEL.83)
1256 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1258 int mode
= (attr
& 1) ? 0444 : 0666;
1259 TRACE(file
, "%s %02x\n", path
, attr
);
1260 return (HFILE16
) HFILE32_TO_HFILE16(FILE_Create( path
, mode
, FALSE
));
1264 /***********************************************************************
1265 * _lcreat32 (KERNEL32.593)
1267 HFILE32 WINAPI
_lcreat32( LPCSTR path
, INT32 attr
)
1269 int mode
= (attr
& 1) ? 0444 : 0666;
1270 TRACE(file
, "%s %02x\n", path
, attr
);
1271 return FILE_Create( path
, mode
, FALSE
);
1275 /***********************************************************************
1276 * _lcreat_uniq (Not a Windows API)
1278 HFILE32
_lcreat_uniq( LPCSTR path
, INT32 attr
)
1280 int mode
= (attr
& 1) ? 0444 : 0666;
1281 TRACE(file
, "%s %02x\n", path
, attr
);
1282 return FILE_Create( path
, mode
, TRUE
);
1286 /***********************************************************************
1287 * SetFilePointer (KERNEL32.492)
1289 DWORD WINAPI
SetFilePointer( HFILE32 hFile
, LONG distance
, LONG
*highword
,
1292 struct set_file_pointer_request req
;
1293 struct set_file_pointer_reply reply
;
1295 if (highword
&& *highword
)
1297 FIXME(file
, "64-bit offsets not supported yet\n");
1298 SetLastError( ERROR_INVALID_PARAMETER
);
1301 TRACE(file
, "handle %d offset %ld origin %ld\n",
1302 hFile
, distance
, method
);
1304 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1305 K32OBJ_FILE
, 0 )) == -1)
1308 req
.high
= highword
? *highword
: 0;
1309 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1310 req
.whence
= method
;
1311 CLIENT_SendRequest( REQ_SET_FILE_POINTER
, -1, 1, &req
, sizeof(req
) );
1312 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return 0xffffffff;
1314 if (highword
) *highword
= reply
.high
;
1319 /***********************************************************************
1320 * _llseek16 (KERNEL.84)
1323 * Seeking before the start of the file should be allowed for _llseek16,
1324 * but cause subsequent I/O operations to fail (cf. interrupt list)
1327 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1329 return SetFilePointer( HFILE16_TO_HFILE32(hFile
), lOffset
, NULL
, nOrigin
);
1333 /***********************************************************************
1334 * _llseek32 (KERNEL32.594)
1336 LONG WINAPI
_llseek32( HFILE32 hFile
, LONG lOffset
, INT32 nOrigin
)
1338 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1342 /***********************************************************************
1343 * _lopen16 (KERNEL.85)
1345 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1347 return HFILE32_TO_HFILE16(_lopen32( path
, mode
));
1351 /***********************************************************************
1352 * _lopen32 (KERNEL32.595)
1354 HFILE32 WINAPI
_lopen32( LPCSTR path
, INT32 mode
)
1358 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1360 unixMode
= FILE_DOSToUnixMode(mode
);
1361 return FILE_Open( path
, unixMode
, (mode
& 0x70));
1365 /***********************************************************************
1366 * _lwrite16 (KERNEL.86)
1368 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1370 return (UINT16
)_hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1373 /***********************************************************************
1374 * _lwrite32 (KERNEL32.761)
1376 UINT32 WINAPI
_lwrite32( HFILE32 hFile
, LPCSTR buffer
, UINT32 count
)
1378 return (UINT32
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1382 /***********************************************************************
1383 * _hread16 (KERNEL.349)
1385 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1387 return _lread32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1391 /***********************************************************************
1392 * _hread32 (KERNEL32.590)
1394 LONG WINAPI
_hread32( HFILE32 hFile
, LPVOID buffer
, LONG count
)
1396 return _lread32( hFile
, buffer
, count
);
1400 /***********************************************************************
1401 * _hwrite16 (KERNEL.350)
1403 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1405 return _hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1409 /***********************************************************************
1410 * _hwrite32 (KERNEL32.591)
1412 * experimentation yields that _lwrite:
1413 * o truncates the file at the current position with
1415 * o returns 0 on a 0 length write
1416 * o works with console handles
1419 LONG WINAPI
_hwrite32( HFILE32 handle
, LPCSTR buffer
, LONG count
)
1423 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1427 /* Expand or truncate at current position */
1428 if (!SetEndOfFile( handle
)) return HFILE_ERROR32
;
1431 if (!WriteFile( handle
, buffer
, count
, &result
, NULL
))
1432 return HFILE_ERROR32
;
1437 /***********************************************************************
1438 * SetHandleCount16 (KERNEL.199)
1440 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1442 HGLOBAL16 hPDB
= GetCurrentPDB();
1443 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1444 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1446 TRACE(file
, "(%d)\n", count
);
1448 if (count
< 20) count
= 20; /* No point in going below 20 */
1449 else if (count
> 254) count
= 254;
1453 if (pdb
->nbFiles
> 20)
1455 memcpy( pdb
->fileHandles
, files
, 20 );
1456 GlobalFree16( pdb
->hFileHandles
);
1457 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1458 GlobalHandleToSel( hPDB
) );
1459 pdb
->hFileHandles
= 0;
1463 else /* More than 20, need a new file handles table */
1466 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1469 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1470 return pdb
->nbFiles
;
1472 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1474 if (count
> pdb
->nbFiles
)
1476 memcpy( newfiles
, files
, pdb
->nbFiles
);
1477 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1479 else memcpy( newfiles
, files
, count
);
1480 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1481 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1482 pdb
->hFileHandles
= newhandle
;
1483 pdb
->nbFiles
= count
;
1485 return pdb
->nbFiles
;
1489 /*************************************************************************
1490 * SetHandleCount32 (KERNEL32.494)
1492 UINT32 WINAPI
SetHandleCount32( UINT32 count
)
1494 return MIN( 256, count
);
1498 /***********************************************************************
1499 * FlushFileBuffers (KERNEL32.133)
1501 BOOL32 WINAPI
FlushFileBuffers( HFILE32 hFile
)
1503 struct flush_file_request req
;
1505 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1506 K32OBJ_FILE
, 0 )) == -1)
1508 CLIENT_SendRequest( REQ_FLUSH_FILE
, -1, 1, &req
, sizeof(req
) );
1509 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1513 /**************************************************************************
1514 * SetEndOfFile (KERNEL32.483)
1516 BOOL32 WINAPI
SetEndOfFile( HFILE32 hFile
)
1518 struct truncate_file_request req
;
1520 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1521 K32OBJ_FILE
, 0 )) == -1)
1523 CLIENT_SendRequest( REQ_TRUNCATE_FILE
, -1, 1, &req
, sizeof(req
) );
1524 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1528 /***********************************************************************
1529 * DeleteFile16 (KERNEL.146)
1531 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1533 return DeleteFile32A( path
);
1537 /***********************************************************************
1538 * DeleteFile32A (KERNEL32.71)
1540 BOOL32 WINAPI
DeleteFile32A( LPCSTR path
)
1542 DOS_FULL_NAME full_name
;
1544 TRACE(file
, "'%s'\n", path
);
1546 if (DOSFS_GetDevice( path
))
1548 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1549 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1553 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1554 if (unlink( full_name
.long_name
) == -1)
1563 /***********************************************************************
1564 * DeleteFile32W (KERNEL32.72)
1566 BOOL32 WINAPI
DeleteFile32W( LPCWSTR path
)
1568 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1569 BOOL32 ret
= DeleteFile32A( xpath
);
1570 HeapFree( GetProcessHeap(), 0, xpath
);
1575 /***********************************************************************
1578 BOOL32
FILE_SetFileType( HFILE32 hFile
, DWORD type
)
1580 FILE_OBJECT
*file
= FILE_GetFile( hFile
, 0, NULL
);
1581 if (!file
) return FALSE
;
1583 FILE_ReleaseFile( file
);
1588 /***********************************************************************
1591 LPVOID
FILE_dommap( int unix_handle
, LPVOID start
,
1592 DWORD size_high
, DWORD size_low
,
1593 DWORD offset_high
, DWORD offset_low
,
1594 int prot
, int flags
)
1600 if (size_high
|| offset_high
)
1601 FIXME(file
, "offsets larger than 4Gb not supported\n");
1603 if (unix_handle
== -1)
1608 static int fdzero
= -1;
1612 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1614 perror( "/dev/zero: open" );
1619 #endif /* MAP_ANON */
1620 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1622 flags
&= ~MAP_SHARED
;
1625 flags
|= MAP_PRIVATE
;
1628 else fd
= unix_handle
;
1630 if ((ret
= mmap( start
, size_low
, prot
,
1631 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1634 /* mmap() failed; if this is because the file offset is not */
1635 /* page-aligned (EINVAL), or because the underlying filesystem */
1636 /* does not support mmap() (ENOEXEC), we do it by hand. */
1638 if (unix_handle
== -1) return ret
;
1639 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1640 if (prot
& PROT_WRITE
)
1642 /* We cannot fake shared write mappings */
1644 if (flags
& MAP_SHARED
) return ret
;
1647 if (!(flags
& MAP_PRIVATE
)) return ret
;
1650 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1651 /* Reserve the memory with an anonymous mmap */
1652 ret
= FILE_dommap( -1, start
, size_high
, size_low
, 0, 0,
1653 PROT_READ
| PROT_WRITE
, flags
);
1654 if (ret
== (LPVOID
)-1) return ret
;
1655 /* Now read in the file */
1656 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1658 FILE_munmap( ret
, size_high
, size_low
);
1661 read( fd
, ret
, size_low
);
1662 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1663 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1668 /***********************************************************************
1671 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1674 FIXME(file
, "offsets larger than 4Gb not supported\n");
1675 return munmap( start
, size_low
);
1679 /***********************************************************************
1680 * GetFileType (KERNEL32.222)
1682 DWORD WINAPI
GetFileType( HFILE32 hFile
)
1684 FILE_OBJECT
*file
= FILE_GetFile(hFile
, 0, NULL
);
1685 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1686 FILE_ReleaseFile( file
);
1691 /**************************************************************************
1692 * MoveFileEx32A (KERNEL32.???)
1694 BOOL32 WINAPI
MoveFileEx32A( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1696 DOS_FULL_NAME full_name1
, full_name2
;
1697 int mode
=0; /* mode == 1: use copy */
1699 TRACE(file
, "(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1701 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1702 if (fn2
) { /* !fn2 means delete fn1 */
1703 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1704 /* Source name and target path are valid */
1705 if ( full_name1
.drive
!= full_name2
.drive
)
1707 /* use copy, if allowed */
1708 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1709 /* FIXME: Use right error code */
1710 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
1715 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1716 /* target exists, check if we may overwrite */
1717 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1718 /* FIXME: Use right error code */
1719 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1723 else /* fn2 == NULL means delete source */
1724 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1725 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1726 WARN(file
, "Illegal flag\n");
1727 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1731 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1732 Perhaps we should queue these command and execute it
1733 when exiting... What about using on_exit(2)
1735 FIXME(file
, "Please delete file '%s' when Wine has finished\n",
1736 full_name1
.long_name
);
1739 else if (unlink( full_name1
.long_name
) == -1)
1744 else return TRUE
; /* successfully deleted */
1746 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1747 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1748 Perhaps we should queue these command and execute it
1749 when exiting... What about using on_exit(2)
1751 FIXME(file
,"Please move existing file '%s' to file '%s'"
1752 "when Wine has finished\n",
1753 full_name1
.long_name
, full_name2
.long_name
);
1757 if (!mode
) /* move the file */
1758 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1764 else /* copy File */
1765 return CopyFile32A(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1769 /**************************************************************************
1770 * MoveFileEx32W (KERNEL32.???)
1772 BOOL32 WINAPI
MoveFileEx32W( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1774 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1775 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1776 BOOL32 res
= MoveFileEx32A( afn1
, afn2
, flag
);
1777 HeapFree( GetProcessHeap(), 0, afn1
);
1778 HeapFree( GetProcessHeap(), 0, afn2
);
1783 /**************************************************************************
1784 * MoveFile32A (KERNEL32.387)
1786 * Move file or directory
1788 BOOL32 WINAPI
MoveFile32A( LPCSTR fn1
, LPCSTR fn2
)
1790 DOS_FULL_NAME full_name1
, full_name2
;
1793 TRACE(file
, "(%s,%s)\n", fn1
, fn2
);
1795 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1796 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1797 /* The new name must not already exist */
1799 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1801 if (full_name1
.drive
== full_name2
.drive
) /* move */
1802 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1809 if (stat( full_name1
.long_name
, &fstat
))
1811 WARN(file
, "Invalid source file %s\n",
1812 full_name1
.long_name
);
1816 if (S_ISDIR(fstat
.st_mode
)) {
1817 /* No Move for directories across file systems */
1818 /* FIXME: Use right error code */
1819 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1824 return CopyFile32A(fn1
, fn2
, TRUE
); /*fail, if exist */
1829 /**************************************************************************
1830 * MoveFile32W (KERNEL32.390)
1832 BOOL32 WINAPI
MoveFile32W( LPCWSTR fn1
, LPCWSTR fn2
)
1834 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1835 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1836 BOOL32 res
= MoveFile32A( afn1
, afn2
);
1837 HeapFree( GetProcessHeap(), 0, afn1
);
1838 HeapFree( GetProcessHeap(), 0, afn2
);
1843 /**************************************************************************
1844 * CopyFile32A (KERNEL32.36)
1846 BOOL32 WINAPI
CopyFile32A( LPCSTR source
, LPCSTR dest
, BOOL32 fail_if_exists
)
1849 BY_HANDLE_FILE_INFORMATION info
;
1855 if ((h1
= _lopen32( source
, OF_READ
)) == HFILE_ERROR32
) return FALSE
;
1856 if (!GetFileInformationByHandle( h1
, &info
))
1861 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1862 if ((h2
= FILE_Create( dest
, mode
, fail_if_exists
)) == HFILE_ERROR32
)
1867 while ((count
= _lread32( h1
, buffer
, sizeof(buffer
) )) > 0)
1872 INT32 res
= _lwrite32( h2
, p
, count
);
1873 if (res
<= 0) goto done
;
1886 /**************************************************************************
1887 * CopyFile32W (KERNEL32.37)
1889 BOOL32 WINAPI
CopyFile32W( LPCWSTR source
, LPCWSTR dest
, BOOL32 fail_if_exists
)
1891 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1892 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1893 BOOL32 ret
= CopyFile32A( sourceA
, destA
, fail_if_exists
);
1894 HeapFree( GetProcessHeap(), 0, sourceA
);
1895 HeapFree( GetProcessHeap(), 0, destA
);
1900 /**************************************************************************
1901 * CopyFileEx32A (KERNEL32.858)
1903 * This implementation ignores most of the extra parameters passed-in into
1904 * the "ex" version of the method and calls the CopyFile method.
1905 * It will have to be fixed eventually.
1907 BOOL32 WINAPI
CopyFileEx32A(LPCSTR sourceFilename
,
1908 LPCSTR destFilename
,
1909 LPPROGRESS_ROUTINE progressRoutine
,
1911 LPBOOL32 cancelFlagPointer
,
1914 BOOL32 failIfExists
= FALSE
;
1917 * Interpret the only flag that CopyFile can interpret.
1919 if ( (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0)
1921 failIfExists
= TRUE
;
1924 return CopyFile32A(sourceFilename
, destFilename
, failIfExists
);
1927 /**************************************************************************
1928 * CopyFileEx32W (KERNEL32.859)
1930 BOOL32 WINAPI
CopyFileEx32W(LPCWSTR sourceFilename
,
1931 LPCWSTR destFilename
,
1932 LPPROGRESS_ROUTINE progressRoutine
,
1934 LPBOOL32 cancelFlagPointer
,
1937 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename
);
1938 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename
);
1940 BOOL32 ret
= CopyFileEx32A(sourceA
,
1947 HeapFree( GetProcessHeap(), 0, sourceA
);
1948 HeapFree( GetProcessHeap(), 0, destA
);
1954 /***********************************************************************
1955 * SetFileTime (KERNEL32.650)
1957 BOOL32 WINAPI
SetFileTime( HFILE32 hFile
,
1958 const FILETIME
*lpCreationTime
,
1959 const FILETIME
*lpLastAccessTime
,
1960 const FILETIME
*lpLastWriteTime
)
1962 FILE_OBJECT
*file
= FILE_GetFile(hFile
, 0, NULL
);
1963 struct utimbuf utimbuf
;
1965 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1966 TRACE(file
,"('%s',%p,%p,%p)\n",
1972 if (lpLastAccessTime
)
1973 utimbuf
.actime
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1975 utimbuf
.actime
= 0; /* FIXME */
1976 if (lpLastWriteTime
)
1977 utimbuf
.modtime
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1979 utimbuf
.modtime
= 0; /* FIXME */
1980 if (-1==utime(file
->unix_name
,&utimbuf
))
1982 MSG("Couldn't set the time for file '%s'. Insufficient permissions !?\n", file
->unix_name
);
1983 FILE_ReleaseFile( file
);
1987 FILE_ReleaseFile( file
);
1991 /* Locks need to be mirrored because unix file locking is based
1992 * on the pid. Inside of wine there can be multiple WINE processes
1993 * that share the same unix pid.
1994 * Read's and writes should check these locks also - not sure
1995 * how critical that is at this point (FIXME).
1998 static BOOL32
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
2000 DOS_FILE_LOCK
*curr
;
2003 processId
= GetCurrentProcessId();
2005 /* check if lock overlaps a current lock for the same file */
2006 for (curr
= locks
; curr
; curr
= curr
->next
) {
2007 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
2008 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
2009 return TRUE
;/* region is identic */
2010 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
2011 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
2012 /* region overlaps */
2018 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
2019 curr
->processId
= GetCurrentProcessId();
2020 curr
->base
= f
->l_start
;
2021 curr
->len
= f
->l_len
;
2022 curr
->unix_name
= HEAP_strdupA( SystemHeap
, 0, file
->unix_name
);
2024 curr
->dos_file
= file
;
2029 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
2032 DOS_FILE_LOCK
**curr
;
2035 processId
= GetCurrentProcessId();
2038 if ((*curr
)->dos_file
== file
) {
2040 *curr
= (*curr
)->next
;
2041 HeapFree( SystemHeap
, 0, rem
->unix_name
);
2042 HeapFree( SystemHeap
, 0, rem
);
2045 curr
= &(*curr
)->next
;
2049 static BOOL32
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2052 DOS_FILE_LOCK
**curr
;
2055 processId
= GetCurrentProcessId();
2056 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2057 if ((*curr
)->processId
== processId
&&
2058 (*curr
)->dos_file
== file
&&
2059 (*curr
)->base
== f
->l_start
&&
2060 (*curr
)->len
== f
->l_len
) {
2061 /* this is the same lock */
2063 *curr
= (*curr
)->next
;
2064 HeapFree( SystemHeap
, 0, rem
->unix_name
);
2065 HeapFree( SystemHeap
, 0, rem
);
2069 /* no matching lock found */
2074 /**************************************************************************
2075 * LockFile (KERNEL32.511)
2077 BOOL32 WINAPI
LockFile(
2078 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2079 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2084 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2085 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2086 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2088 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2089 FIXME(file
, "Unimplemented bytes > 32bits\n");
2093 f
.l_start
= dwFileOffsetLow
;
2094 f
.l_len
= nNumberOfBytesToLockLow
;
2095 f
.l_whence
= SEEK_SET
;
2099 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2101 /* shadow locks internally */
2102 if (!DOS_AddLock(file
, &f
)) {
2103 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2107 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2108 #ifdef USE_UNIX_LOCKS
2109 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2110 if (errno
== EACCES
|| errno
== EAGAIN
) {
2111 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2116 /* remove our internal copy of the lock */
2117 DOS_RemoveLock(file
, &f
);
2125 /**************************************************************************
2126 * UnlockFile (KERNEL32.703)
2128 BOOL32 WINAPI
UnlockFile(
2129 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2130 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2135 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2136 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2137 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2139 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2140 WARN(file
, "Unimplemented bytes > 32bits\n");
2144 f
.l_start
= dwFileOffsetLow
;
2145 f
.l_len
= nNumberOfBytesToUnlockLow
;
2146 f
.l_whence
= SEEK_SET
;
2150 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2152 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2154 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2155 #ifdef USE_UNIX_LOCKS
2156 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2164 /**************************************************************************
2165 * GetFileAttributesEx32A [KERNEL32.874]
2167 BOOL32 WINAPI
GetFileAttributesEx32A(
2168 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2169 LPVOID lpFileInformation
)
2171 DOS_FULL_NAME full_name
;
2172 BY_HANDLE_FILE_INFORMATION info
;
2174 if (lpFileName
== NULL
) return FALSE
;
2175 if (lpFileInformation
== NULL
) return FALSE
;
2177 if (fInfoLevelId
== GetFileExInfoStandard
) {
2178 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2179 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2180 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2181 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2183 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2184 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2185 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2186 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2187 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2188 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2191 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
2199 /**************************************************************************
2200 * GetFileAttributesEx32W [KERNEL32.875]
2202 BOOL32 WINAPI
GetFileAttributesEx32W(
2203 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2204 LPVOID lpFileInformation
)
2206 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2208 GetFileAttributesEx32A( nameA
, fInfoLevelId
, lpFileInformation
);
2209 HeapFree( GetProcessHeap(), 0, nameA
);