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>
40 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
41 #define MAP_ANON MAP_ANONYMOUS
44 static BOOL32
FILE_Signaled(K32OBJ
*ptr
, DWORD tid
);
45 static BOOL32
FILE_Satisfied(K32OBJ
*ptr
, DWORD thread_id
);
46 static void FILE_AddWait(K32OBJ
*ptr
, DWORD tid
);
47 static void FILE_RemoveWait(K32OBJ
*ptr
, DWORD thread_id
);
48 static BOOL32
FILE_Read(K32OBJ
*ptr
, LPVOID lpBuffer
, DWORD nNumberOfChars
,
49 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
);
50 static BOOL32
FILE_Write(K32OBJ
*ptr
, LPCVOID lpBuffer
, DWORD nNumberOfChars
,
51 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
);
52 static void FILE_Destroy( K32OBJ
*obj
);
54 const K32OBJ_OPS FILE_Ops
=
56 FILE_Signaled
, /* signaled */
57 FILE_Satisfied
, /* satisfied */
58 FILE_AddWait
, /* add_wait */
59 FILE_RemoveWait
, /* remove_wait */
61 FILE_Write
, /* write */
62 FILE_Destroy
/* destroy */
65 struct DOS_FILE_LOCK
{
66 struct DOS_FILE_LOCK
* next
;
70 FILE_OBJECT
* dos_file
;
74 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
76 static DOS_FILE_LOCK
*locks
= NULL
;
77 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
79 /***********************************************************************
84 HFILE32
FILE_Alloc( FILE_OBJECT
**file
)
87 *file
= HeapAlloc( SystemHeap
, 0, sizeof(FILE_OBJECT
) );
90 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
93 (*file
)->header
.type
= K32OBJ_FILE
;
94 (*file
)->header
.refcount
= 0;
95 (*file
)->unix_handle
= -1;
96 (*file
)->unix_name
= NULL
;
97 (*file
)->type
= FILE_TYPE_DISK
;
101 handle
= HANDLE_Alloc( PROCESS_Current(), &(*file
)->header
,
102 FILE_ALL_ACCESS
| GENERIC_READ
|
103 GENERIC_WRITE
| GENERIC_EXECUTE
/*FIXME*/, TRUE
, -1 );
104 /* If the allocation failed, the object is already destroyed */
105 if (handle
== INVALID_HANDLE_VALUE32
) *file
= NULL
;
109 static BOOL32
FILE_Signaled(K32OBJ
*ptr
, DWORD thread_id
)
111 fd_set fds
,*readfds
= NULL
,*writefds
= NULL
;
113 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
116 FD_SET(file
->unix_handle
,&fds
);
117 if (file
->mode
== OF_READ
) readfds
= &fds
;
118 if (file
->mode
== OF_WRITE
) writefds
= &fds
;
119 if (file
->mode
== OF_READWRITE
) {writefds
= &fds
; readfds
= &fds
;}
122 assert(readfds
|| writefds
);
123 if (select(file
->unix_handle
+1,readfds
,writefds
,NULL
,&tv
)>0)
124 return TRUE
; /* we triggered one fd. Whereever. */
128 static void FILE_AddWait(K32OBJ
*ptr
, DWORD thread_id
)
130 TRACE(file
,"(),stub\n");
134 static void FILE_RemoveWait(K32OBJ
*ptr
, DWORD thread_id
)
136 TRACE(file
,"(),stub\n");
140 static BOOL32
FILE_Satisfied(K32OBJ
*ptr
, DWORD thread_id
)
142 TRACE(file
,"(),stub\n");
146 /* FIXME: lpOverlapped is ignored */
147 static BOOL32
FILE_Read(K32OBJ
*ptr
, LPVOID lpBuffer
, DWORD nNumberOfChars
,
148 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
150 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
153 TRACE(file
, "%p %p %ld\n", ptr
, lpBuffer
,
156 if (nNumberOfChars
== 0) {
157 *lpNumberOfChars
= 0; /* FIXME: does this change */
161 if ( (file
->pos
< 0) || /* workaround, see SetFilePointer */
162 ((result
= read(file
->unix_handle
, lpBuffer
, nNumberOfChars
)) == -1) )
168 *lpNumberOfChars
= result
;
173 * experimentation yields that WriteFile:
174 * o does not truncate on write of 0
175 * o always changes the *lpNumberOfChars to actual number of
177 * o write of 0 nNumberOfChars returns TRUE
179 static BOOL32
FILE_Write(K32OBJ
*ptr
, LPCVOID lpBuffer
, DWORD nNumberOfChars
,
180 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
182 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
185 TRACE(file
, "%p %p %ld\n", ptr
, lpBuffer
,
188 *lpNumberOfChars
= 0;
191 * I assume this loop around EAGAIN is here because
192 * win32 doesn't have interrupted system calls
195 if (file
->pos
< 0) { /* workaround, see SetFilePointer */
202 result
= write(file
->unix_handle
, lpBuffer
, nNumberOfChars
);
204 *lpNumberOfChars
= result
;
208 if (errno
!= EINTR
) {
217 /***********************************************************************
220 * Destroy a DOS file.
222 static void FILE_Destroy( K32OBJ
*ptr
)
224 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
225 assert( ptr
->type
== K32OBJ_FILE
);
227 DOS_RemoveFileLocks(file
);
229 if (file
->unix_handle
!= -1) close( file
->unix_handle
);
230 if (file
->unix_name
) HeapFree( SystemHeap
, 0, file
->unix_name
);
231 ptr
->type
= K32OBJ_UNKNOWN
;
232 HeapFree( SystemHeap
, 0, file
);
236 /***********************************************************************
239 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
240 * be called to release the file.
242 FILE_OBJECT
*FILE_GetFile( HFILE32 handle
)
244 return (FILE_OBJECT
*)HANDLE_GetObjPtr( PROCESS_Current(), handle
,
245 K32OBJ_FILE
, 0 /*FIXME*/, NULL
);
249 /***********************************************************************
252 * Release a DOS file obtained with FILE_GetFile.
254 void FILE_ReleaseFile( FILE_OBJECT
*file
)
256 K32OBJ_DecCount( &file
->header
);
260 /***********************************************************************
263 * Return the Unix handle associated to a file handle.
265 int FILE_GetUnixHandle( HFILE32 hFile
)
270 if (!(file
= FILE_GetFile( hFile
))) return -1;
271 ret
= file
->unix_handle
;
272 FILE_ReleaseFile( file
);
276 /***********************************************************************
284 static int FILE_UnixToDosMode(int unixMode
)
293 dosMode
=OF_READWRITE
;
303 /***********************************************************************
311 static int FILE_DOSToUnixMode(int dosMode
)
317 unixMode
= O_WRONLY
; break;
319 unixMode
= O_RDWR
; break;
322 unixMode
= O_RDONLY
; break;
327 /***********************************************************************
331 * oldmode[I] mode how file was first opened
332 * mode[I] mode how the file should get opened
337 * Look what we have to do with the given SHARE modes
339 * Ralph Brown's interrupt list gives following explication, I guess
340 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
342 * FIXME: Validate this function
343 ========from Ralph Brown's list =========
345 Values of DOS file sharing behavior:
346 | Second and subsequent Opens
347 First |Compat Deny Deny Deny Deny
348 Open | All Write Read None
349 |R W RW R W RW R W RW R W RW R W RW
350 - - - - -| - - - - - - - - - - - - - - - - -
351 Compat R |Y Y Y N N N 1 N N N N N 1 N N
352 W |Y Y Y N N N N N N N N N N N N
353 RW|Y Y Y N N N N N N N N N N N N
355 Deny R |C C C N N N N N N N N N N N N
356 All W |C C C N N N N N N N N N N N N
357 RW|C C C N N N N N N N N N N N N
359 Deny R |2 C C N N N Y N N N N N Y N N
360 Write W |C C C N N N N N N Y N N Y N N
361 RW|C C C N N N N N N N N N Y N N
363 Deny R |C C C N N N N Y N N N N N Y N
364 Read W |C C C N N N N N N N Y N N Y N
365 RW|C C C N N N N N N N N N N Y N
367 Deny R |2 C C N N N Y Y Y N N N Y Y Y
368 None W |C C C N N N N N N Y Y Y Y Y Y
369 RW|C C C N N N N N N N N N Y Y Y
370 Legend: Y = open succeeds, N = open fails with error code 05h
371 C = open fails, INT 24 generated
372 1 = open succeeds if file read-only, else fails with error code
373 2 = open succeeds if file read-only, else fails with INT 24
374 ========end of description from Ralph Brown's List =====
375 For every "Y" in the table we return FALSE
376 For every "N" we set the DOS_ERROR and return TRUE
377 For all other cases we barf,set the DOS_ERROR and return TRUE
380 static BOOL32
FILE_ShareDeny( int mode
, int oldmode
)
382 int oldsharemode
= oldmode
& 0x70;
383 int sharemode
= mode
& 0x70;
384 int oldopenmode
= oldmode
& 3;
385 int openmode
= mode
& 3;
387 switch (oldsharemode
)
389 case OF_SHARE_COMPAT
:
390 if (sharemode
== OF_SHARE_COMPAT
) return FALSE
;
391 if (openmode
== OF_READ
) goto test_ro_err05
;
393 case OF_SHARE_EXCLUSIVE
:
394 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
396 case OF_SHARE_DENY_WRITE
:
397 if (openmode
!= OF_READ
)
399 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
404 case OF_SHARE_COMPAT
:
405 if (oldopenmode
== OF_READ
) goto test_ro_int24
;
407 case OF_SHARE_DENY_NONE
:
409 case OF_SHARE_DENY_WRITE
:
410 if (oldopenmode
== OF_READ
) return FALSE
;
411 case OF_SHARE_DENY_READ
:
412 if (oldopenmode
== OF_WRITE
) return FALSE
;
413 case OF_SHARE_EXCLUSIVE
:
418 case OF_SHARE_DENY_READ
:
419 if (openmode
!= OF_WRITE
)
421 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
426 case OF_SHARE_COMPAT
:
428 case OF_SHARE_DENY_NONE
:
430 case OF_SHARE_DENY_WRITE
:
431 if (oldopenmode
== OF_READ
) return FALSE
;
432 case OF_SHARE_DENY_READ
:
433 if (oldopenmode
== OF_WRITE
) return FALSE
;
434 case OF_SHARE_EXCLUSIVE
:
439 case OF_SHARE_DENY_NONE
:
442 case OF_SHARE_COMPAT
:
444 case OF_SHARE_DENY_NONE
:
446 case OF_SHARE_DENY_WRITE
:
447 if (oldopenmode
== OF_READ
) return FALSE
;
448 case OF_SHARE_DENY_READ
:
449 if (oldopenmode
== OF_WRITE
) return FALSE
;
450 case OF_SHARE_EXCLUSIVE
:
455 ERR(file
,"unknown mode\n");
457 ERR(file
,"shouldn't happen\n");
458 ERR(file
,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
462 if (oldmode
== OF_READ
)
466 FIXME(file
,"generate INT24 missing\n");
467 /* Is this the right error? */
468 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
472 if (oldmode
== OF_READ
)
476 TRACE(file
,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode
,mode
);
477 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
483 /***********************************************************************
486 * Look if the File is in Use For the OF_SHARE_XXX options
489 * name [I]: full unix name of the file that should be opened
490 * mode [O]: mode how the file was first opened
492 * TRUE if the file was opened before
493 * FALSE if we open the file exclusive for this process
495 * Scope of the files we look for is only the current pdb
496 * Could we use /proc/self/? on Linux for this?
497 * Should we use flock? Should we create another structure?
498 * Searching through all files seem quite expensive for me, but
499 * I don't see any other way.
501 * FIXME: Extend scope to the whole Wine process
504 static BOOL32
FILE_InUse(char * name
, int * mode
)
508 HGLOBAL16 hPDB
= GetCurrentPDB();
509 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
512 for (i
=0;i
<pdb
->nbFiles
;i
++)
514 file
=FILE_GetFile( (HFILE32
) i
);
519 TRACE(file
,"got %s at %d\n",file
->unix_name
,i
);
520 if(!lstrcmp32A(file
->unix_name
,name
))
523 FILE_ReleaseFile(file
);
527 FILE_ReleaseFile(file
);
533 /***********************************************************************
536 * Set the DOS error code from errno.
538 void FILE_SetDosError(void)
540 int save_errno
= errno
; /* errno gets overwritten by printf */
542 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
546 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
549 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
552 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
557 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
560 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
563 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
566 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
570 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
573 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
577 DOS_ERROR( ER_SeekError
, EC_NotFound
, SA_Ignore
, EL_Disk
);
580 DOS_ERROR( ERROR_DIR_NOT_EMPTY
, EC_Exists
, SA_Ignore
, EL_Disk
);
583 perror( "int21: unknown errno" );
584 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
591 /***********************************************************************
594 * Duplicate a Unix handle into a task handle.
596 HFILE32
FILE_DupUnixHandle( int fd
)
601 if ((handle
= FILE_Alloc( &file
)) != INVALID_HANDLE_VALUE32
)
603 if ((file
->unix_handle
= dup(fd
)) == -1)
606 CloseHandle( handle
);
607 return INVALID_HANDLE_VALUE32
;
614 /***********************************************************************
617 HFILE32
FILE_OpenUnixFile( const char *name
, int mode
)
623 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
624 return INVALID_HANDLE_VALUE32
;
626 if ((file
->unix_handle
= open( name
, mode
, 0666 )) == -1)
628 if (!Options
.failReadOnly
&& (mode
== O_RDWR
))
629 file
->unix_handle
= open( name
, O_RDONLY
);
631 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
634 CloseHandle( handle
);
635 return INVALID_HANDLE_VALUE32
;
637 if (S_ISDIR(st
.st_mode
))
639 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
640 CloseHandle( handle
);
641 return INVALID_HANDLE_VALUE32
;
644 /* File opened OK, now fill the FILE_OBJECT */
646 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, name
);
651 /***********************************************************************
654 * path[I] name of file to open
655 * mode[I] mode how to open, in unix notation
656 * shareMode[I] the sharing mode in the win OpenFile notation
659 HFILE32
FILE_Open( LPCSTR path
, INT32 mode
, INT32 shareMode
)
661 DOS_FULL_NAME full_name
;
662 const char *unixName
;
663 int oldMode
, dosMode
; /* FIXME: Do we really need unixmode as argument for
667 BOOL32 fileInUse
= FALSE
;
669 TRACE(file
, "'%s' %04x\n", path
, mode
);
671 if (!path
) return HFILE_ERROR32
;
673 if (DOSFS_GetDevice( path
))
677 TRACE(file
, "opening device '%s'\n", path
);
679 if (HFILE_ERROR32
!=(ret
=DOSFS_OpenDevice( path
, mode
)))
682 /* Do not silence this please. It is a critical error. -MM */
683 ERR(file
, "Couldn't open device '%s'!\n",path
);
684 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
685 return HFILE_ERROR32
;
688 else /* check for filename, don't check for last entry if creating */
690 if (!DOSFS_GetFullName( path
, !(mode
& O_CREAT
), &full_name
))
691 return HFILE_ERROR32
;
692 unixName
= full_name
.long_name
;
695 dosMode
= FILE_UnixToDosMode(mode
)| shareMode
;
696 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
699 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
700 if (FILE_ShareDeny(dosMode
,oldMode
)) return HFILE_ERROR32
;
702 hFileRet
= FILE_OpenUnixFile( unixName
, mode
);
703 /* we need to save the mode, but only if it is not in use yet*/
704 if ((hFileRet
) && (!fileInUse
) && ((file
=FILE_GetFile(hFileRet
))))
707 FILE_ReleaseFile(file
);
714 /***********************************************************************
717 static HFILE32
FILE_Create( LPCSTR path
, int mode
, int unique
)
721 DOS_FULL_NAME full_name
;
722 BOOL32 fileInUse
= FALSE
;
723 int oldMode
,dosMode
; /* FIXME: Do we really need unixmode as argument for
726 TRACE(file
, "'%s' %04x %d\n", path
, mode
, unique
);
728 if (!path
) return INVALID_HANDLE_VALUE32
;
730 if (DOSFS_GetDevice( path
))
732 WARN(file
, "cannot create DOS device '%s'!\n", path
);
733 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
734 return INVALID_HANDLE_VALUE32
;
737 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
738 return INVALID_HANDLE_VALUE32
;
740 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
))
742 CloseHandle( handle
);
743 return INVALID_HANDLE_VALUE32
;
746 dosMode
= FILE_UnixToDosMode(mode
);
747 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
750 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
751 if (FILE_ShareDeny(dosMode
,oldMode
)) return INVALID_HANDLE_VALUE32
;
754 if ((file
->unix_handle
= open( full_name
.long_name
,
755 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
759 CloseHandle( handle
);
760 return INVALID_HANDLE_VALUE32
;
763 /* File created OK, now fill the FILE_OBJECT */
765 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, full_name
.long_name
);
766 file
->mode
= dosMode
;
771 /***********************************************************************
774 * Fill a file information from a struct stat.
776 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
778 if (S_ISDIR(st
->st_mode
))
779 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
781 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
782 if (!(st
->st_mode
& S_IWUSR
))
783 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
785 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
786 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
787 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
789 info
->dwVolumeSerialNumber
= 0; /* FIXME */
790 info
->nFileSizeHigh
= 0;
791 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
792 info
->nNumberOfLinks
= st
->st_nlink
;
793 info
->nFileIndexHigh
= 0;
794 info
->nFileIndexLow
= st
->st_ino
;
798 /***********************************************************************
801 * Stat a Unix path name. Return TRUE if OK.
803 BOOL32
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
807 if (!unixName
|| !info
) return FALSE
;
809 if (stat( unixName
, &st
) == -1)
814 FILE_FillInfo( &st
, info
);
819 /***********************************************************************
820 * GetFileInformationByHandle (KERNEL32.219)
822 DWORD WINAPI
GetFileInformationByHandle( HFILE32 hFile
,
823 BY_HANDLE_FILE_INFORMATION
*info
)
831 if (!(file
= FILE_GetFile( hFile
))) return 0;
832 if (fstat( file
->unix_handle
, &st
) == -1) FILE_SetDosError();
835 FILE_FillInfo( &st
, info
);
838 FILE_ReleaseFile( file
);
843 /**************************************************************************
844 * GetFileAttributes16 (KERNEL.420)
846 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
848 return GetFileAttributes32A( name
);
852 /**************************************************************************
853 * GetFileAttributes32A (KERNEL32.217)
855 DWORD WINAPI
GetFileAttributes32A( LPCSTR name
)
857 DOS_FULL_NAME full_name
;
858 BY_HANDLE_FILE_INFORMATION info
;
860 if (name
== NULL
|| *name
=='\0') return -1;
862 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
863 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
864 return info
.dwFileAttributes
;
868 /**************************************************************************
869 * GetFileAttributes32W (KERNEL32.218)
871 DWORD WINAPI
GetFileAttributes32W( LPCWSTR name
)
873 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
874 DWORD res
= GetFileAttributes32A( nameA
);
875 HeapFree( GetProcessHeap(), 0, nameA
);
880 /***********************************************************************
881 * GetFileSize (KERNEL32.220)
883 DWORD WINAPI
GetFileSize( HFILE32 hFile
, LPDWORD filesizehigh
)
885 BY_HANDLE_FILE_INFORMATION info
;
886 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
887 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
888 return info
.nFileSizeLow
;
892 /***********************************************************************
893 * GetFileTime (KERNEL32.221)
895 BOOL32 WINAPI
GetFileTime( HFILE32 hFile
, FILETIME
*lpCreationTime
,
896 FILETIME
*lpLastAccessTime
,
897 FILETIME
*lpLastWriteTime
)
899 BY_HANDLE_FILE_INFORMATION info
;
900 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
901 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
902 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
903 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
907 /***********************************************************************
908 * CompareFileTime (KERNEL32.28)
910 INT32 WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
912 if (!x
|| !y
) return -1;
914 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
916 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
918 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
920 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
925 /***********************************************************************
928 * dup() function for DOS handles.
930 HFILE32
FILE_Dup( HFILE32 hFile
)
934 TRACE(file
, "FILE_Dup for handle %d\n", hFile
);
935 if (!DuplicateHandle( GetCurrentProcess(), hFile
, GetCurrentProcess(),
936 &handle
, FILE_ALL_ACCESS
/* FIXME */, FALSE
, 0 ))
937 handle
= HFILE_ERROR32
;
938 TRACE(file
, "FILE_Dup return handle %d\n", handle
);
943 /***********************************************************************
946 * dup2() function for DOS handles.
948 HFILE32
FILE_Dup2( HFILE32 hFile1
, HFILE32 hFile2
)
952 TRACE(file
, "FILE_Dup2 for handle %d\n", hFile1
);
953 /* FIXME: should use DuplicateHandle */
954 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR32
;
955 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2
, &file
->header
, 0 ))
956 hFile2
= HFILE_ERROR32
;
957 FILE_ReleaseFile( file
);
962 /***********************************************************************
963 * GetTempFileName16 (KERNEL.97)
965 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
970 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
971 drive
|= DRIVE_GetCurrentDrive() + 'A';
973 if ((drive
& TF_FORCEDRIVE
) &&
974 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
976 drive
&= ~TF_FORCEDRIVE
;
977 WARN(file
, "invalid drive %d specified\n", drive
);
980 if (drive
& TF_FORCEDRIVE
)
981 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
983 GetTempPath32A( 132, temppath
);
984 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
988 /***********************************************************************
989 * GetTempFileName32A (KERNEL32.290)
991 UINT32 WINAPI
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
994 static UINT32 unique_temp
;
995 DOS_FULL_NAME full_name
;
1000 if ( !path
|| !prefix
|| !buffer
) return 0;
1002 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
1003 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
1005 strcpy( buffer
, path
);
1006 p
= buffer
+ strlen(buffer
);
1008 /* add a \, if there isn't one and path is more than just the drive letter ... */
1009 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
1010 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
1013 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
1014 sprintf( p
, "%04x.tmp", num
);
1016 /* Now try to create it */
1022 HFILE32 handle
= FILE_Create( buffer
, 0666, TRUE
);
1023 if (handle
!= INVALID_HANDLE_VALUE32
)
1024 { /* We created it */
1025 TRACE(file
, "created %s\n",
1027 CloseHandle( handle
);
1030 if (DOS_ExtendedError
!= ER_FileExists
)
1031 break; /* No need to go on */
1033 sprintf( p
, "%04x.tmp", num
);
1034 } while (num
!= (unique
& 0xffff));
1037 /* Get the full path name */
1039 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
1041 /* Check if we have write access in the directory */
1042 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
1043 if (access( full_name
.long_name
, W_OK
) == -1)
1044 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
1047 TRACE(file
, "returning %s\n", buffer
);
1048 return unique
? unique
: num
;
1052 /***********************************************************************
1053 * GetTempFileName32W (KERNEL32.291)
1055 UINT32 WINAPI
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
1058 LPSTR patha
,prefixa
;
1062 if (!path
) return 0;
1063 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1064 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
1065 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
1066 lstrcpyAtoW( buffer
, buffera
);
1067 HeapFree( GetProcessHeap(), 0, patha
);
1068 HeapFree( GetProcessHeap(), 0, prefixa
);
1073 /***********************************************************************
1076 * Implementation of OpenFile16() and OpenFile32().
1078 static HFILE32
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
,
1083 WORD filedatetime
[2];
1084 DOS_FULL_NAME full_name
;
1086 int unixMode
, oldMode
;
1088 BOOL32 fileInUse
= FALSE
;
1090 if (!ofs
) return HFILE_ERROR32
;
1093 ofs
->cBytes
= sizeof(OFSTRUCT
);
1095 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
1098 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
1099 return HFILE_ERROR32
;
1102 TRACE(file
, "%s %04x\n", name
, mode
);
1104 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
1105 Are there any cases where getting the path here is wrong?
1106 Uwe Bonnes 1997 Apr 2 */
1107 if (!GetFullPathName32A( name
, sizeof(ofs
->szPathName
),
1108 ofs
->szPathName
, NULL
)) goto error
;
1110 /* OF_PARSE simply fills the structure */
1112 if (mode
& OF_PARSE
)
1114 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
1115 != DRIVE_REMOVABLE
);
1116 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
1117 name
, ofs
->szPathName
);
1121 /* OF_CREATE is completely different from all other options, so
1124 if (mode
& OF_CREATE
)
1126 if ((hFileRet
= FILE_Create(name
,0666,FALSE
))== INVALID_HANDLE_VALUE32
)
1131 /* If OF_SEARCH is set, ignore the given path */
1133 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
1135 /* First try the file name as is */
1136 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
1137 /* Now remove the path */
1138 if (name
[0] && (name
[1] == ':')) name
+= 2;
1139 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
1140 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
1141 if (!name
[0]) goto not_found
;
1144 /* Now look for the file */
1146 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
1149 TRACE(file
, "found %s = %s\n",
1150 full_name
.long_name
, full_name
.short_name
);
1151 lstrcpyn32A( ofs
->szPathName
, full_name
.short_name
,
1152 sizeof(ofs
->szPathName
) );
1154 fileInUse
= FILE_InUse(full_name
.long_name
,&oldMode
);
1157 TRACE(file
, "found another instance with mode 0x%02x\n",oldMode
&0x70);
1158 if (FILE_ShareDeny(mode
,oldMode
)) return HFILE_ERROR32
;
1161 if (mode
& OF_SHARE_EXCLUSIVE
)
1162 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
1163 on the file <tempdir>/_ins0432._mp to determine how
1164 far installation has proceeded.
1165 _ins0432._mp is an executable and while running the
1166 application expects the open with OF_SHARE_ to fail*/
1168 As our loader closes the files after loading the executable,
1169 we can't find the running executable with FILE_InUse.
1170 Perhaps the loader should keep the file open.
1171 Recheck against how Win handles that case */
1173 char *last
= strrchr(full_name
.long_name
,'/');
1175 last
= full_name
.long_name
- 1;
1176 if (GetModuleHandle16(last
+1))
1178 TRACE(file
,"Denying shared open for %s\n",full_name
.long_name
);
1179 return HFILE_ERROR32
;
1183 if (mode
& OF_DELETE
)
1185 if (unlink( full_name
.long_name
) == -1) goto not_found
;
1186 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
1190 unixMode
=FILE_DOSToUnixMode(mode
);
1192 hFileRet
= FILE_OpenUnixFile( full_name
.long_name
, unixMode
);
1193 if (hFileRet
== HFILE_ERROR32
) goto not_found
;
1194 /* we need to save the mode, but only if it is not in use yet*/
1195 if( (!fileInUse
) &&(file
=FILE_GetFile(hFileRet
)))
1198 FILE_ReleaseFile(file
);
1201 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
1202 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
1203 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
1205 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
1207 CloseHandle( hFileRet
);
1208 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
1209 /* FIXME: what error here? */
1210 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1214 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
1216 success
: /* We get here if the open was successful */
1217 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
1218 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
1219 CloseHandle( hFileRet
);
1222 not_found
: /* We get here if the file does not exist */
1223 WARN(file
, "'%s' not found\n", name
);
1224 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1227 error
: /* We get here if there was an error opening the file */
1228 ofs
->nErrCode
= DOS_ExtendedError
;
1229 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
1230 name
,ofs
->nErrCode
);
1231 return HFILE_ERROR32
;
1235 /***********************************************************************
1236 * OpenFile16 (KERNEL.74)
1238 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
1240 TRACE(file
,"OpenFile16(%s,%i)\n", name
, mode
);
1241 return HFILE32_TO_HFILE16(FILE_DoOpenFile( name
, ofs
, mode
, FALSE
));
1245 /***********************************************************************
1246 * OpenFile32 (KERNEL32.396)
1248 HFILE32 WINAPI
OpenFile32( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
1250 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
1254 /***********************************************************************
1255 * _lclose16 (KERNEL.81)
1257 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1259 TRACE(file
, "handle %d\n", hFile
);
1260 return CloseHandle( HFILE16_TO_HFILE32( hFile
) ) ? 0 : HFILE_ERROR16
;
1264 /***********************************************************************
1265 * _lclose32 (KERNEL32.592)
1267 HFILE32 WINAPI
_lclose32( HFILE32 hFile
)
1269 TRACE(file
, "handle %d\n", hFile
);
1270 return CloseHandle( hFile
) ? 0 : HFILE_ERROR32
;
1274 /***********************************************************************
1277 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1281 TRACE(file
, "%d %08lx %ld\n",
1282 hFile
, (DWORD
)buffer
, count
);
1284 /* Some programs pass a count larger than the allocated buffer */
1285 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1286 if (count
> maxlen
) count
= maxlen
;
1287 return _lread32(HFILE16_TO_HFILE32(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1291 /***********************************************************************
1294 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1296 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1300 /***********************************************************************
1301 * _lread32 (KERNEL32.596)
1303 UINT32 WINAPI
_lread32( HFILE32 handle
, LPVOID buffer
, UINT32 count
)
1307 BOOL32 result
= FALSE
;
1309 TRACE( file
, "%d %p %d\n", handle
, buffer
, count
);
1310 if (!(ptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
1311 K32OBJ_UNKNOWN
, 0, NULL
))) return -1;
1312 if (K32OBJ_OPS(ptr
)->read
)
1313 result
= K32OBJ_OPS(ptr
)->read(ptr
, buffer
, count
, &numWritten
, NULL
);
1314 K32OBJ_DecCount( ptr
);
1315 if (!result
) return -1;
1316 return (UINT32
)numWritten
;
1320 /***********************************************************************
1321 * _lread16 (KERNEL.82)
1323 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1325 return (UINT16
)_lread32(HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1329 /***********************************************************************
1330 * _lcreat16 (KERNEL.83)
1332 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1334 int mode
= (attr
& 1) ? 0444 : 0666;
1335 TRACE(file
, "%s %02x\n", path
, attr
);
1336 return (HFILE16
) HFILE32_TO_HFILE16(FILE_Create( path
, mode
, FALSE
));
1340 /***********************************************************************
1341 * _lcreat32 (KERNEL32.593)
1343 HFILE32 WINAPI
_lcreat32( LPCSTR path
, INT32 attr
)
1345 int mode
= (attr
& 1) ? 0444 : 0666;
1346 TRACE(file
, "%s %02x\n", path
, attr
);
1347 return FILE_Create( path
, mode
, FALSE
);
1351 /***********************************************************************
1352 * _lcreat_uniq (Not a Windows API)
1354 HFILE32
_lcreat_uniq( LPCSTR path
, INT32 attr
)
1356 int mode
= (attr
& 1) ? 0444 : 0666;
1357 TRACE(file
, "%s %02x\n", path
, attr
);
1358 return FILE_Create( path
, mode
, TRUE
);
1362 /***********************************************************************
1363 * SetFilePointer (KERNEL32.492)
1365 DWORD WINAPI
SetFilePointer( HFILE32 hFile
, LONG distance
, LONG
*highword
,
1369 DWORD result
= 0xffffffff;
1371 if (highword
&& *highword
)
1373 FIXME(file
, "64-bit offsets not supported yet\n");
1374 SetLastError( ERROR_INVALID_PARAMETER
);
1377 TRACE(file
, "handle %d offset %ld origin %ld\n",
1378 hFile
, distance
, method
);
1380 if (!(file
= FILE_GetFile( hFile
))) return 0xffffffff;
1383 /* the pointer may be positioned before the start of the file;
1384 no error is returned in that case,
1385 but subsequent attempts at I/O will produce errors.
1386 This is not allowed with Unix lseek(),
1387 so we'll need some emulation here */
1391 distance
+= file
->pos
; /* fall through */
1393 if ((result
= lseek(file
->unix_handle
, distance
, SEEK_SET
)) == -1)
1395 if ((INT32
)distance
< 0)
1396 file
->pos
= result
= distance
;
1402 if ((result
= lseek(file
->unix_handle
, distance
, SEEK_END
)) == -1)
1404 if ((INT32
)distance
< 0)
1407 result
= lseek(file
->unix_handle
, 0, SEEK_END
);
1409 /* return to the old pos, as the first lseek failed */
1410 lseek(file
->unix_handle
, file
->pos
, SEEK_END
);
1412 file
->pos
= (result
+= distance
);
1415 ERR(file
, "lseek: unknown error. Please report.\n");
1417 else file
->pos
= result
;
1420 ERR(file
, "Unknown origin %ld !\n", method
);
1426 FILE_ReleaseFile( file
);
1431 /***********************************************************************
1432 * _llseek16 (KERNEL.84)
1434 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1436 return SetFilePointer( HFILE16_TO_HFILE32(hFile
), lOffset
, NULL
, nOrigin
);
1440 /***********************************************************************
1441 * _llseek32 (KERNEL32.594)
1443 LONG WINAPI
_llseek32( HFILE32 hFile
, LONG lOffset
, INT32 nOrigin
)
1445 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1449 /***********************************************************************
1450 * _lopen16 (KERNEL.85)
1452 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1454 return HFILE32_TO_HFILE16(_lopen32( path
, mode
));
1458 /***********************************************************************
1459 * _lopen32 (KERNEL32.595)
1461 HFILE32 WINAPI
_lopen32( LPCSTR path
, INT32 mode
)
1465 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1467 unixMode
= FILE_DOSToUnixMode(mode
);
1468 return FILE_Open( path
, unixMode
, (mode
& 0x70));
1472 /***********************************************************************
1473 * _lwrite16 (KERNEL.86)
1475 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1477 return (UINT16
)_hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1480 /***********************************************************************
1481 * _lwrite32 (KERNEL.86)
1483 UINT32 WINAPI
_lwrite32( HFILE32 hFile
, LPCSTR buffer
, UINT32 count
)
1485 return (UINT32
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1489 /***********************************************************************
1490 * _hread16 (KERNEL.349)
1492 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1494 return _lread32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1498 /***********************************************************************
1499 * _hread32 (KERNEL32.590)
1501 LONG WINAPI
_hread32( HFILE32 hFile
, LPVOID buffer
, LONG count
)
1503 return _lread32( hFile
, buffer
, count
);
1507 /***********************************************************************
1508 * _hwrite16 (KERNEL.350)
1510 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1512 return _hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1516 /***********************************************************************
1517 * _hwrite32 (KERNEL32.591)
1519 * experimenation yields that _lwrite:
1520 * o truncates the file at the current position with
1522 * o returns 0 on a 0 length write
1523 * o works with console handles
1526 LONG WINAPI
_hwrite32( HFILE32 handle
, LPCSTR buffer
, LONG count
)
1530 BOOL32 status
= FALSE
;
1532 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1534 if (count
== 0) { /* Expand or truncate at current position */
1535 FILE_OBJECT
*file
= FILE_GetFile(handle
);
1537 if ( ftruncate(file
->unix_handle
,
1538 lseek( file
->unix_handle
, 0, SEEK_CUR
)) == 0 ) {
1539 FILE_ReleaseFile(file
);
1543 FILE_ReleaseFile(file
);
1544 return HFILE_ERROR32
;
1548 if (!(ioptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
1549 K32OBJ_UNKNOWN
, 0, NULL
)))
1550 return HFILE_ERROR32
;
1551 if (K32OBJ_OPS(ioptr
)->write
)
1552 status
= K32OBJ_OPS(ioptr
)->write(ioptr
, buffer
, count
, &result
, NULL
);
1553 K32OBJ_DecCount( ioptr
);
1554 if (!status
) result
= HFILE_ERROR32
;
1559 /***********************************************************************
1560 * SetHandleCount16 (KERNEL.199)
1562 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1564 HGLOBAL16 hPDB
= GetCurrentPDB();
1565 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1566 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1568 TRACE(file
, "(%d)\n", count
);
1570 if (count
< 20) count
= 20; /* No point in going below 20 */
1571 else if (count
> 254) count
= 254;
1575 if (pdb
->nbFiles
> 20)
1577 memcpy( pdb
->fileHandles
, files
, 20 );
1578 GlobalFree16( pdb
->hFileHandles
);
1579 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1580 GlobalHandleToSel( hPDB
) );
1581 pdb
->hFileHandles
= 0;
1585 else /* More than 20, need a new file handles table */
1588 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1591 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1592 return pdb
->nbFiles
;
1594 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1596 if (count
> pdb
->nbFiles
)
1598 memcpy( newfiles
, files
, pdb
->nbFiles
);
1599 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1601 else memcpy( newfiles
, files
, count
);
1602 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1603 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1604 pdb
->hFileHandles
= newhandle
;
1605 pdb
->nbFiles
= count
;
1607 return pdb
->nbFiles
;
1611 /*************************************************************************
1612 * SetHandleCount32 (KERNEL32.494)
1614 UINT32 WINAPI
SetHandleCount32( UINT32 count
)
1616 return MIN( 256, count
);
1620 /***********************************************************************
1621 * FlushFileBuffers (KERNEL32.133)
1623 BOOL32 WINAPI
FlushFileBuffers( HFILE32 hFile
)
1628 TRACE(file
, "(%d)\n", hFile
);
1629 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1630 if (fsync( file
->unix_handle
) != -1) ret
= TRUE
;
1636 FILE_ReleaseFile( file
);
1641 /**************************************************************************
1642 * SetEndOfFile (KERNEL32.483)
1644 BOOL32 WINAPI
SetEndOfFile( HFILE32 hFile
)
1649 TRACE(file
, "(%d)\n", hFile
);
1650 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1651 if (ftruncate( file
->unix_handle
,
1652 lseek( file
->unix_handle
, 0, SEEK_CUR
) ))
1657 FILE_ReleaseFile( file
);
1662 /***********************************************************************
1663 * DeleteFile16 (KERNEL.146)
1665 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1667 return DeleteFile32A( path
);
1671 /***********************************************************************
1672 * DeleteFile32A (KERNEL32.71)
1674 BOOL32 WINAPI
DeleteFile32A( LPCSTR path
)
1676 DOS_FULL_NAME full_name
;
1678 TRACE(file
, "'%s'\n", path
);
1680 if (DOSFS_GetDevice( path
))
1682 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1683 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1687 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1688 if (unlink( full_name
.long_name
) == -1)
1697 /***********************************************************************
1698 * DeleteFile32W (KERNEL32.72)
1700 BOOL32 WINAPI
DeleteFile32W( LPCWSTR path
)
1702 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1703 BOOL32 ret
= DeleteFile32A( xpath
);
1704 HeapFree( GetProcessHeap(), 0, xpath
);
1709 /***********************************************************************
1712 BOOL32
FILE_SetFileType( HFILE32 hFile
, DWORD type
)
1714 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1715 if (!file
) return FALSE
;
1717 FILE_ReleaseFile( file
);
1722 /***********************************************************************
1725 LPVOID
FILE_mmap( HFILE32 hFile
, LPVOID start
,
1726 DWORD size_high
, DWORD size_low
,
1727 DWORD offset_high
, DWORD offset_low
,
1728 int prot
, int flags
)
1731 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1732 if (!file
) return (LPVOID
)-1;
1733 ret
= FILE_dommap( file
, start
, size_high
, size_low
,
1734 offset_high
, offset_low
, prot
, flags
);
1735 FILE_ReleaseFile( file
);
1740 /***********************************************************************
1743 LPVOID
FILE_dommap( FILE_OBJECT
*file
, LPVOID start
,
1744 DWORD size_high
, DWORD size_low
,
1745 DWORD offset_high
, DWORD offset_low
,
1746 int prot
, int flags
)
1752 if (size_high
|| offset_high
)
1753 FIXME(file
, "offsets larger than 4Gb not supported\n");
1760 static int fdzero
= -1;
1764 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1766 perror( "/dev/zero: open" );
1771 #endif /* MAP_ANON */
1772 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1774 flags
&= ~MAP_SHARED
;
1777 flags
|= MAP_PRIVATE
;
1780 else fd
= file
->unix_handle
;
1782 if ((ret
= mmap( start
, size_low
, prot
,
1783 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1786 /* mmap() failed; if this is because the file offset is not */
1787 /* page-aligned (EINVAL), or because the underlying filesystem */
1788 /* does not support mmap() (ENOEXEC), we do it by hand. */
1790 if (!file
) return ret
;
1791 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1792 if (prot
& PROT_WRITE
)
1794 /* We cannot fake shared write mappings */
1796 if (flags
& MAP_SHARED
) return ret
;
1799 if (!(flags
& MAP_PRIVATE
)) return ret
;
1802 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1803 /* Reserve the memory with an anonymous mmap */
1804 ret
= FILE_dommap( NULL
, start
, size_high
, size_low
, 0, 0,
1805 PROT_READ
| PROT_WRITE
, flags
);
1806 if (ret
== (LPVOID
)-1) return ret
;
1807 /* Now read in the file */
1808 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1810 FILE_munmap( ret
, size_high
, size_low
);
1813 read( fd
, ret
, size_low
);
1814 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1815 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1820 /***********************************************************************
1823 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1826 FIXME(file
, "offsets larger than 4Gb not supported\n");
1827 return munmap( start
, size_low
);
1831 /***********************************************************************
1832 * GetFileType (KERNEL32.222)
1834 DWORD WINAPI
GetFileType( HFILE32 hFile
)
1836 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1837 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1838 FILE_ReleaseFile( file
);
1843 /**************************************************************************
1844 * MoveFileEx32A (KERNEL32.???)
1846 BOOL32 WINAPI
MoveFileEx32A( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1848 DOS_FULL_NAME full_name1
, full_name2
;
1849 int mode
=0; /* mode == 1: use copy */
1851 TRACE(file
, "(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1853 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1854 if (fn2
) { /* !fn2 means delete fn1 */
1855 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1856 /* Source name and target path are valid */
1857 if ( full_name1
.drive
!= full_name2
.drive
)
1859 /* use copy, if allowed */
1860 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1861 /* FIXME: Use right error code */
1862 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
1867 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1868 /* target exists, check if we may overwrite */
1869 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1870 /* FIXME: Use right error code */
1871 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1875 else /* fn2 == NULL means delete source */
1876 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1877 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1878 WARN(file
, "Illegal flag\n");
1879 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1883 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1884 Perhaps we should queue these command and execute it
1885 when exiting... What about using on_exit(2)
1887 FIXME(file
, "Please delete file '%s' when Wine has finished\n",
1888 full_name1
.long_name
);
1891 else if (unlink( full_name1
.long_name
) == -1)
1896 else return TRUE
; /* successfully deleted */
1898 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1899 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1900 Perhaps we should queue these command and execute it
1901 when exiting... What about using on_exit(2)
1903 FIXME(file
,"Please move existing file '%s' to file '%s'"
1904 "when Wine has finished\n",
1905 full_name1
.long_name
, full_name2
.long_name
);
1909 if (!mode
) /* move the file */
1910 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1916 else /* copy File */
1917 return CopyFile32A(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1921 /**************************************************************************
1922 * MoveFileEx32W (KERNEL32.???)
1924 BOOL32 WINAPI
MoveFileEx32W( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1926 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1927 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1928 BOOL32 res
= MoveFileEx32A( afn1
, afn2
, flag
);
1929 HeapFree( GetProcessHeap(), 0, afn1
);
1930 HeapFree( GetProcessHeap(), 0, afn2
);
1935 /**************************************************************************
1936 * MoveFile32A (KERNEL32.387)
1938 * Move file or directory
1940 BOOL32 WINAPI
MoveFile32A( LPCSTR fn1
, LPCSTR fn2
)
1942 DOS_FULL_NAME full_name1
, full_name2
;
1945 TRACE(file
, "(%s,%s)\n", fn1
, fn2
);
1947 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1948 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1949 /* The new name must not already exist */
1951 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1953 if (full_name1
.drive
== full_name2
.drive
) /* move */
1954 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1961 if (stat( full_name1
.long_name
, &fstat
))
1963 WARN(file
, "Invalid source file %s\n",
1964 full_name1
.long_name
);
1968 if (S_ISDIR(fstat
.st_mode
)) {
1969 /* No Move for directories across file systems */
1970 /* FIXME: Use right error code */
1971 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1976 return CopyFile32A(fn1
, fn2
, TRUE
); /*fail, if exist */
1981 /**************************************************************************
1982 * MoveFile32W (KERNEL32.390)
1984 BOOL32 WINAPI
MoveFile32W( LPCWSTR fn1
, LPCWSTR fn2
)
1986 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1987 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1988 BOOL32 res
= MoveFile32A( afn1
, afn2
);
1989 HeapFree( GetProcessHeap(), 0, afn1
);
1990 HeapFree( GetProcessHeap(), 0, afn2
);
1995 /**************************************************************************
1996 * CopyFile32A (KERNEL32.36)
1998 BOOL32 WINAPI
CopyFile32A( LPCSTR source
, LPCSTR dest
, BOOL32 fail_if_exists
)
2001 BY_HANDLE_FILE_INFORMATION info
;
2007 if ((h1
= _lopen32( source
, OF_READ
)) == HFILE_ERROR32
) return FALSE
;
2008 if (!GetFileInformationByHandle( h1
, &info
))
2013 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
2014 if ((h2
= FILE_Create( dest
, mode
, fail_if_exists
)) == HFILE_ERROR32
)
2019 while ((count
= _lread32( h1
, buffer
, sizeof(buffer
) )) > 0)
2024 INT32 res
= _lwrite32( h2
, p
, count
);
2025 if (res
<= 0) goto done
;
2038 /**************************************************************************
2039 * CopyFile32W (KERNEL32.37)
2041 BOOL32 WINAPI
CopyFile32W( LPCWSTR source
, LPCWSTR dest
, BOOL32 fail_if_exists
)
2043 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
2044 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
2045 BOOL32 ret
= CopyFile32A( sourceA
, destA
, fail_if_exists
);
2046 HeapFree( GetProcessHeap(), 0, sourceA
);
2047 HeapFree( GetProcessHeap(), 0, destA
);
2052 /**************************************************************************
2053 * CopyFileEx32A (KERNEL32.858)
2055 * This implementation ignores most of the extra parameters passed-in into
2056 * the "ex" version of the method and calls the CopyFile method.
2057 * It will have to be fixed eventually.
2059 BOOL32 WINAPI
CopyFileEx32A(LPCSTR sourceFilename
,
2060 LPCSTR destFilename
,
2061 LPPROGRESS_ROUTINE progressRoutine
,
2063 LPBOOL32 cancelFlagPointer
,
2066 BOOL32 failIfExists
= FALSE
;
2069 * Interpret the only flag that CopyFile can interpret.
2071 if ( (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0)
2073 failIfExists
= TRUE
;
2076 return CopyFile32A(sourceFilename
, destFilename
, failIfExists
);
2079 /**************************************************************************
2080 * CopyFileEx32W (KERNEL32.859)
2082 BOOL32 WINAPI
CopyFileEx32W(LPCWSTR sourceFilename
,
2083 LPCWSTR destFilename
,
2084 LPPROGRESS_ROUTINE progressRoutine
,
2086 LPBOOL32 cancelFlagPointer
,
2089 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename
);
2090 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename
);
2092 BOOL32 ret
= CopyFileEx32A(sourceA
,
2099 HeapFree( GetProcessHeap(), 0, sourceA
);
2100 HeapFree( GetProcessHeap(), 0, destA
);
2106 /***********************************************************************
2107 * SetFileTime (KERNEL32.650)
2109 BOOL32 WINAPI
SetFileTime( HFILE32 hFile
,
2110 const FILETIME
*lpCreationTime
,
2111 const FILETIME
*lpLastAccessTime
,
2112 const FILETIME
*lpLastWriteTime
)
2114 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
2115 struct utimbuf utimbuf
;
2117 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
2118 TRACE(file
,"('%s',%p,%p,%p)\n",
2124 if (lpLastAccessTime
)
2125 utimbuf
.actime
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
2127 utimbuf
.actime
= 0; /* FIXME */
2128 if (lpLastWriteTime
)
2129 utimbuf
.modtime
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
2131 utimbuf
.modtime
= 0; /* FIXME */
2132 if (-1==utime(file
->unix_name
,&utimbuf
))
2134 MSG("Couldn't set the time for file '%s'. Insufficient permissions !?\n", file
->unix_name
);
2135 FILE_ReleaseFile( file
);
2139 FILE_ReleaseFile( file
);
2143 /* Locks need to be mirrored because unix file locking is based
2144 * on the pid. Inside of wine there can be multiple WINE processes
2145 * that share the same unix pid.
2146 * Read's and writes should check these locks also - not sure
2147 * how critical that is at this point (FIXME).
2150 static BOOL32
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
2152 DOS_FILE_LOCK
*curr
;
2155 processId
= GetCurrentProcessId();
2157 /* check if lock overlaps a current lock for the same file */
2158 for (curr
= locks
; curr
; curr
= curr
->next
) {
2159 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
2160 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
2161 return TRUE
;/* region is identic */
2162 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
2163 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
2164 /* region overlaps */
2170 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
2171 curr
->processId
= GetCurrentProcessId();
2172 curr
->base
= f
->l_start
;
2173 curr
->len
= f
->l_len
;
2174 curr
->unix_name
= HEAP_strdupA( SystemHeap
, 0, file
->unix_name
);
2176 curr
->dos_file
= file
;
2181 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
2184 DOS_FILE_LOCK
**curr
;
2187 processId
= GetCurrentProcessId();
2190 if ((*curr
)->dos_file
== file
) {
2192 *curr
= (*curr
)->next
;
2193 HeapFree( SystemHeap
, 0, rem
->unix_name
);
2194 HeapFree( SystemHeap
, 0, rem
);
2197 curr
= &(*curr
)->next
;
2201 static BOOL32
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2204 DOS_FILE_LOCK
**curr
;
2207 processId
= GetCurrentProcessId();
2208 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2209 if ((*curr
)->processId
== processId
&&
2210 (*curr
)->dos_file
== file
&&
2211 (*curr
)->base
== f
->l_start
&&
2212 (*curr
)->len
== f
->l_len
) {
2213 /* this is the same lock */
2215 *curr
= (*curr
)->next
;
2216 HeapFree( SystemHeap
, 0, rem
->unix_name
);
2217 HeapFree( SystemHeap
, 0, rem
);
2221 /* no matching lock found */
2226 /**************************************************************************
2227 * LockFile (KERNEL32.511)
2229 BOOL32 WINAPI
LockFile(
2230 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2231 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2236 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2237 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2238 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2240 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2241 FIXME(file
, "Unimplemented bytes > 32bits\n");
2245 f
.l_start
= dwFileOffsetLow
;
2246 f
.l_len
= nNumberOfBytesToLockLow
;
2247 f
.l_whence
= SEEK_SET
;
2251 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
2253 /* shadow locks internally */
2254 if (!DOS_AddLock(file
, &f
)) {
2255 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2259 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2260 #ifdef USE_UNIX_LOCKS
2261 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2262 if (errno
== EACCES
|| errno
== EAGAIN
) {
2263 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2268 /* remove our internal copy of the lock */
2269 DOS_RemoveLock(file
, &f
);
2277 /**************************************************************************
2278 * UnlockFile (KERNEL32.703)
2280 BOOL32 WINAPI
UnlockFile(
2281 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2282 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2287 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2288 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2289 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2291 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2292 WARN(file
, "Unimplemented bytes > 32bits\n");
2296 f
.l_start
= dwFileOffsetLow
;
2297 f
.l_len
= nNumberOfBytesToUnlockLow
;
2298 f
.l_whence
= SEEK_SET
;
2302 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
2304 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2306 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2307 #ifdef USE_UNIX_LOCKS
2308 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2316 /**************************************************************************
2317 * GetFileAttributesEx32A [KERNEL32.874]
2319 BOOL32 WINAPI
GetFileAttributesEx32A(
2320 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2321 LPVOID lpFileInformation
)
2323 DOS_FULL_NAME full_name
;
2324 BY_HANDLE_FILE_INFORMATION info
;
2326 if (lpFileName
== NULL
) return FALSE
;
2327 if (lpFileInformation
== NULL
) return FALSE
;
2329 if (fInfoLevelId
== GetFileExInfoStandard
) {
2330 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2331 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2332 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2333 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2335 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2336 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2337 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2338 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2339 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2340 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2343 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
2351 /**************************************************************************
2352 * GetFileAttributesEx32W [KERNEL32.875]
2354 BOOL32 WINAPI
GetFileAttributesEx32W(
2355 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2356 LPVOID lpFileInformation
)
2358 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2360 GetFileAttributesEx32A( nameA
, fInfoLevelId
, lpFileInformation
);
2361 HeapFree( GetProcessHeap(), 0, nameA
);