2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
15 #include <sys/errno.h>
16 #include <sys/types.h>
36 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
37 #define MAP_ANON MAP_ANONYMOUS
40 static BOOL32
FILE_Read(K32OBJ
*ptr
, LPVOID lpBuffer
, DWORD nNumberOfChars
,
41 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
);
42 static BOOL32
FILE_Write(K32OBJ
*ptr
, LPCVOID lpBuffer
, DWORD nNumberOfChars
,
43 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
);
44 static void FILE_Destroy( K32OBJ
*obj
);
46 const K32OBJ_OPS FILE_Ops
=
48 /* Object cannot be waited upon (FIXME: for now) */
52 NULL
, /* remove_wait */
54 FILE_Write
, /* write */
55 FILE_Destroy
/* destroy */
58 struct DOS_FILE_LOCK
{
59 struct DOS_FILE_LOCK
* next
;
63 FILE_OBJECT
* dos_file
;
67 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
69 static DOS_FILE_LOCK
*locks
= NULL
;
70 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
72 /***********************************************************************
77 static HFILE32
FILE_Alloc( FILE_OBJECT
**file
)
80 *file
= HeapAlloc( SystemHeap
, 0, sizeof(FILE_OBJECT
) );
83 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
86 (*file
)->header
.type
= K32OBJ_FILE
;
87 (*file
)->header
.refcount
= 0;
88 (*file
)->unix_handle
= -1;
89 (*file
)->unix_name
= NULL
;
90 (*file
)->type
= FILE_TYPE_DISK
;
92 handle
= HANDLE_Alloc( &(*file
)->header
, FILE_ALL_ACCESS
| GENERIC_READ
|
93 GENERIC_WRITE
| GENERIC_EXECUTE
/*FIXME*/, FALSE
);
94 /* If the allocation failed, the object is already destroyed */
95 if (handle
== INVALID_HANDLE_VALUE32
) *file
= NULL
;
100 /* FIXME: lpOverlapped is ignored */
101 static BOOL32
FILE_Read(K32OBJ
*ptr
, LPVOID lpBuffer
, DWORD nNumberOfChars
,
102 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
104 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
107 dprintf_info(file
, "FILE_Read: %p %p %ld\n", ptr
, lpBuffer
,
110 if (nNumberOfChars
== 0) {
111 *lpNumberOfChars
= 0; /* FIXME: does this change */
115 if ((result
= read(file
->unix_handle
, lpBuffer
, nNumberOfChars
)) == -1)
120 *lpNumberOfChars
= result
;
125 * experimentation yields that WriteFile:
126 * o does not truncate on write of 0
127 * o always changes the *lpNumberOfChars to actual number of
129 * o write of 0 nNumberOfChars returns TRUE
131 static BOOL32
FILE_Write(K32OBJ
*ptr
, LPCVOID lpBuffer
, DWORD nNumberOfChars
,
132 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
134 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
137 dprintf_info(file
, "FILE_Write: %p %p %ld\n", ptr
, lpBuffer
,
140 *lpNumberOfChars
= 0;
142 /* FIXME: there was a loop here before that
143 * retried writes after EAGAIN, why??? -- I assume
144 * it is because win32 doesn't have interrupted
150 result
= write(file
->unix_handle
, lpBuffer
, nNumberOfChars
);
152 *lpNumberOfChars
= result
;
155 if (errno
!= EINTR
) {
164 /***********************************************************************
167 * Destroy a DOS file.
169 static void FILE_Destroy( K32OBJ
*ptr
)
171 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
172 assert( ptr
->type
== K32OBJ_FILE
);
174 DOS_RemoveFileLocks(file
);
176 if (file
->unix_handle
!= -1) close( file
->unix_handle
);
177 if (file
->unix_name
) HeapFree( SystemHeap
, 0, file
->unix_name
);
178 ptr
->type
= K32OBJ_UNKNOWN
;
179 HeapFree( SystemHeap
, 0, file
);
183 /***********************************************************************
186 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
187 * be called to release the file.
189 static FILE_OBJECT
*FILE_GetFile( HFILE32 handle
)
191 return (FILE_OBJECT
*)HANDLE_GetObjPtr( handle
, K32OBJ_FILE
, 0 /*FIXME*/ );
195 /***********************************************************************
198 * Release a DOS file obtained with FILE_GetFile.
200 static void FILE_ReleaseFile( FILE_OBJECT
*file
)
202 K32OBJ_DecCount( &file
->header
);
206 /***********************************************************************
209 * Return the Unix handle associated to a file handle.
211 int FILE_GetUnixHandle( HFILE32 hFile
)
216 if (!(file
= FILE_GetFile( hFile
))) return -1;
217 ret
= file
->unix_handle
;
218 FILE_ReleaseFile( file
);
223 /***********************************************************************
226 * Set the DOS error code from errno.
228 void FILE_SetDosError(void)
230 int save_errno
= errno
; /* errno gets overwritten by printf */
232 dprintf_info(file
, "FILE_SetDosError: errno = %d %s\n", errno
, strerror(errno
));
236 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
239 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
242 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
247 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
250 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
253 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
256 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
260 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
263 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
266 DOS_ERROR( ER_SeekError
, EC_NotFound
, SA_Ignore
, EL_Disk
);
269 DOS_ERROR( ERROR_DIR_NOT_EMPTY
, EC_Exists
, SA_Ignore
, EL_Disk
);
272 perror( "int21: unknown errno" );
273 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
280 /***********************************************************************
283 * Duplicate a Unix handle into a task handle.
285 HFILE32
FILE_DupUnixHandle( int fd
)
290 if ((handle
= FILE_Alloc( &file
)) != INVALID_HANDLE_VALUE32
)
292 if ((file
->unix_handle
= dup(fd
)) == -1)
295 CloseHandle( handle
);
296 return INVALID_HANDLE_VALUE32
;
303 /***********************************************************************
306 HFILE32
FILE_OpenUnixFile( const char *name
, int mode
)
312 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
313 return INVALID_HANDLE_VALUE32
;
315 if ((file
->unix_handle
= open( name
, mode
, 0666 )) == -1)
317 if (!Options
.failReadOnly
&& (mode
== O_RDWR
))
318 file
->unix_handle
= open( name
, O_RDONLY
);
320 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
323 CloseHandle( handle
);
324 return INVALID_HANDLE_VALUE32
;
326 if (S_ISDIR(st
.st_mode
))
328 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
329 CloseHandle( handle
);
330 return INVALID_HANDLE_VALUE32
;
333 /* File opened OK, now fill the FILE_OBJECT */
335 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, name
);
340 /***********************************************************************
343 HFILE32
FILE_Open( LPCSTR path
, INT32 mode
)
345 DOS_FULL_NAME full_name
;
346 const char *unixName
;
348 dprintf_info(file
, "FILE_Open: '%s' %04x\n", path
, mode
);
350 if (!path
) return HFILE_ERROR32
;
352 if (DOSFS_IsDevice( path
))
356 dprintf_info(file
, "FILE_Open: opening device '%s'\n", path
);
358 if (HFILE_ERROR32
!=(ret
=DOSFS_OpenDevice( path
, mode
)))
361 /* Do not silence this please. It is a critical error. -MM */
362 fprintf(stderr
, "FILE_Open: Couldn't open device '%s'!\n",path
);
363 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
364 return HFILE_ERROR32
;
367 else /* check for filename, don't check for last entry if creating */
369 if (!DOSFS_GetFullName( path
, !(mode
& O_CREAT
), &full_name
))
370 return HFILE_ERROR32
;
371 unixName
= full_name
.long_name
;
373 return FILE_OpenUnixFile( unixName
, mode
);
377 /***********************************************************************
380 static HFILE32
FILE_Create( LPCSTR path
, int mode
, int unique
)
384 DOS_FULL_NAME full_name
;
386 dprintf_info(file
, "FILE_Create: '%s' %04x %d\n", path
, mode
, unique
);
388 if (!path
) return INVALID_HANDLE_VALUE32
;
390 if (DOSFS_IsDevice( path
))
392 fprintf(stderr
, "FILE_Create: cannot create DOS device '%s'!\n", path
);
393 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
394 return INVALID_HANDLE_VALUE32
;
397 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
398 return INVALID_HANDLE_VALUE32
;
400 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
))
402 CloseHandle( handle
);
403 return INVALID_HANDLE_VALUE32
;
405 if ((file
->unix_handle
= open( full_name
.long_name
,
406 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
410 CloseHandle( handle
);
411 return INVALID_HANDLE_VALUE32
;
414 /* File created OK, now fill the FILE_OBJECT */
416 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, full_name
.long_name
);
421 /***********************************************************************
424 * Fill a file information from a struct stat.
426 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
428 if (S_ISDIR(st
->st_mode
))
429 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
431 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
432 if (!(st
->st_mode
& S_IWUSR
))
433 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
435 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
436 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
437 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
439 info
->dwVolumeSerialNumber
= 0; /* FIXME */
440 info
->nFileSizeHigh
= 0;
441 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
442 info
->nNumberOfLinks
= st
->st_nlink
;
443 info
->nFileIndexHigh
= 0;
444 info
->nFileIndexLow
= st
->st_ino
;
448 /***********************************************************************
451 * Stat a Unix path name. Return TRUE if OK.
453 BOOL32
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
457 if (!unixName
|| !info
) return FALSE
;
459 if (stat( unixName
, &st
) == -1)
464 FILE_FillInfo( &st
, info
);
469 /***********************************************************************
470 * GetFileInformationByHandle (KERNEL32.219)
472 DWORD WINAPI
GetFileInformationByHandle( HFILE32 hFile
,
473 BY_HANDLE_FILE_INFORMATION
*info
)
481 if (!(file
= FILE_GetFile( hFile
))) return 0;
482 if (fstat( file
->unix_handle
, &st
) == -1) FILE_SetDosError();
485 FILE_FillInfo( &st
, info
);
488 FILE_ReleaseFile( file
);
493 /**************************************************************************
494 * GetFileAttributes16 (KERNEL.420)
496 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
498 return GetFileAttributes32A( name
);
502 /**************************************************************************
503 * GetFileAttributes32A (KERNEL32.217)
505 DWORD WINAPI
GetFileAttributes32A( LPCSTR name
)
507 DOS_FULL_NAME full_name
;
508 BY_HANDLE_FILE_INFORMATION info
;
510 if (name
== NULL
) return -1;
512 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
513 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
514 return info
.dwFileAttributes
;
518 /**************************************************************************
519 * GetFileAttributes32W (KERNEL32.218)
521 DWORD WINAPI
GetFileAttributes32W( LPCWSTR name
)
523 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
524 DWORD res
= GetFileAttributes32A( nameA
);
525 HeapFree( GetProcessHeap(), 0, nameA
);
530 /***********************************************************************
531 * GetFileSize (KERNEL32.220)
533 DWORD WINAPI
GetFileSize( HFILE32 hFile
, LPDWORD filesizehigh
)
535 BY_HANDLE_FILE_INFORMATION info
;
536 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
537 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
538 return info
.nFileSizeLow
;
542 /***********************************************************************
543 * GetFileTime (KERNEL32.221)
545 BOOL32 WINAPI
GetFileTime( HFILE32 hFile
, FILETIME
*lpCreationTime
,
546 FILETIME
*lpLastAccessTime
,
547 FILETIME
*lpLastWriteTime
)
549 BY_HANDLE_FILE_INFORMATION info
;
550 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
551 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
552 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
553 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
557 /***********************************************************************
558 * CompareFileTime (KERNEL32.28)
560 INT32 WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
562 if (!x
|| !y
) return -1;
564 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
566 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
568 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
570 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
575 /***********************************************************************
578 * dup() function for DOS handles.
580 HFILE32
FILE_Dup( HFILE32 hFile
)
585 dprintf_info(file
, "FILE_Dup for handle %d\n", hFile
);
586 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR32
;
587 handle
= HANDLE_Alloc( &file
->header
, FILE_ALL_ACCESS
/*FIXME*/, FALSE
);
588 FILE_ReleaseFile( file
);
589 dprintf_info(file
, "FILE_Dup return handle %d\n", handle
);
594 /***********************************************************************
597 * dup2() function for DOS handles.
599 HFILE32
FILE_Dup2( HFILE32 hFile1
, HFILE32 hFile2
)
603 dprintf_info(file
, "FILE_Dup2 for handle %d\n", hFile1
);
604 /* FIXME: should use DuplicateHandle */
605 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR32
;
606 if (!HANDLE_SetObjPtr( hFile2
, &file
->header
, 0 )) hFile2
= HFILE_ERROR32
;
607 FILE_ReleaseFile( file
);
612 /***********************************************************************
613 * GetTempFileName16 (KERNEL.97)
615 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
620 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
621 drive
|= DRIVE_GetCurrentDrive() + 'A';
623 if ((drive
& TF_FORCEDRIVE
) &&
624 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
626 drive
&= ~TF_FORCEDRIVE
;
627 fprintf( stderr
, "Warning: GetTempFileName: invalid drive %d specified\n",
631 if (drive
& TF_FORCEDRIVE
)
632 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
635 GetTempPath32A( 132, temppath
);
636 strcat( temppath
, "\\" );
638 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
642 /***********************************************************************
643 * GetTempFileName32A (KERNEL32.290)
645 UINT32 WINAPI
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
648 DOS_FULL_NAME full_name
;
651 UINT32 num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
653 if ( !path
|| !prefix
|| !buffer
) return 0;
655 strcpy( buffer
, path
);
656 p
= buffer
+ strlen(buffer
);
658 /* add a \, if there isn't one and path is more than just the drive letter ... */
659 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
660 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
663 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
664 sprintf( p
, "%04x.tmp", num
);
666 /* Now try to create it */
672 HFILE32 handle
= FILE_Create( buffer
, 0666, TRUE
);
673 if (handle
!= INVALID_HANDLE_VALUE32
)
674 { /* We created it */
675 dprintf_info(file
, "GetTempFileName32A: created %s\n",
677 CloseHandle( handle
);
680 if (DOS_ExtendedError
!= ER_FileExists
)
681 break; /* No need to go on */
683 sprintf( p
, "%04x.tmp", num
);
684 } while (num
!= (unique
& 0xffff));
687 /* Get the full path name */
689 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
691 /* Check if we have write access in the directory */
692 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
693 if (access( full_name
.long_name
, W_OK
) == -1)
695 "Warning: GetTempFileName returns '%s', which doesn't seem to be writeable.\n"
696 "Please check your configuration file if this generates a failure.\n",
699 dprintf_info(file
, "GetTempFileName32A: returning %s\n", buffer
);
700 return unique
? unique
: num
;
704 /***********************************************************************
705 * GetTempFileName32W (KERNEL32.291)
707 UINT32 WINAPI
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
715 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
716 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
717 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
718 lstrcpyAtoW( buffer
, buffera
);
719 HeapFree( GetProcessHeap(), 0, patha
);
720 HeapFree( GetProcessHeap(), 0, prefixa
);
725 /***********************************************************************
728 * Implementation of OpenFile16() and OpenFile32().
730 static HFILE32
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
,
735 WORD filedatetime
[2];
736 DOS_FULL_NAME full_name
;
740 if (!ofs
) return HFILE_ERROR32
;
743 ofs
->cBytes
= sizeof(OFSTRUCT
);
745 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
748 fprintf(stderr
, "ERROR: FILE_DoOpenFile() called with `name' set to NULL ! Please debug.\n");
750 return HFILE_ERROR32
;
753 dprintf_info(file
, "OpenFile: %s %04x\n", name
, mode
);
755 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
756 Are there any cases where getting the path here is wrong?
757 Uwe Bonnes 1997 Apr 2 */
758 if (!GetFullPathName32A( name
, sizeof(ofs
->szPathName
),
759 ofs
->szPathName
, NULL
)) goto error
;
761 /* OF_PARSE simply fills the structure */
765 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
767 dprintf_info(file
, "OpenFile(%s): OF_PARSE, res = '%s'\n",
768 name
, ofs
->szPathName
);
772 /* OF_CREATE is completely different from all other options, so
775 if (mode
& OF_CREATE
)
777 if ((hFileRet
= FILE_Create(name
,0666,FALSE
))== INVALID_HANDLE_VALUE32
)
782 /* If OF_SEARCH is set, ignore the given path */
784 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
786 /* First try the file name as is */
787 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
788 /* Now remove the path */
789 if (name
[0] && (name
[1] == ':')) name
+= 2;
790 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
791 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
792 if (!name
[0]) goto not_found
;
795 /* Now look for the file */
797 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
800 dprintf_info(file
, "OpenFile: found %s = %s\n",
801 full_name
.long_name
, full_name
.short_name
);
802 lstrcpyn32A( ofs
->szPathName
, full_name
.short_name
,
803 sizeof(ofs
->szPathName
) );
805 if (mode
& OF_DELETE
)
807 if (unlink( full_name
.long_name
) == -1) goto not_found
;
808 dprintf_info(file
, "OpenFile(%s): OF_DELETE return = OK\n", name
);
815 unixMode
= O_WRONLY
; break;
817 unixMode
= O_RDWR
; break;
820 unixMode
= O_RDONLY
; break;
823 hFileRet
= FILE_OpenUnixFile( full_name
.long_name
, unixMode
);
824 if (hFileRet
== HFILE_ERROR32
) goto not_found
;
825 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
826 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
827 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
829 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
831 CloseHandle( hFileRet
);
832 dprintf_warn(file
, "OpenFile(%s): OF_VERIFY failed\n", name
);
833 /* FIXME: what error here? */
834 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
838 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
840 success
: /* We get here if the open was successful */
841 dprintf_info(file
, "OpenFile(%s): OK, return = %d\n", name
, hFileRet
);
842 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
843 CloseHandle( hFileRet
);
846 not_found
: /* We get here if the file does not exist */
847 dprintf_warn(file
, "OpenFile: '%s' not found\n", name
);
848 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
851 error
: /* We get here if there was an error opening the file */
852 ofs
->nErrCode
= DOS_ExtendedError
;
853 dprintf_warn(file
, "OpenFile(%s): return = HFILE_ERROR error= %d\n",
854 name
,ofs
->nErrCode
);
855 return HFILE_ERROR32
;
859 /***********************************************************************
860 * OpenFile16 (KERNEL.74)
862 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
864 return FILE_DoOpenFile( name
, ofs
, mode
, FALSE
);
868 /***********************************************************************
869 * OpenFile32 (KERNEL32.396)
871 HFILE32 WINAPI
OpenFile32( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
873 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
877 /***********************************************************************
878 * _lclose16 (KERNEL.81)
880 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
882 dprintf_info(file
, "_lclose16: handle %d\n", hFile
);
883 return CloseHandle( hFile
) ? 0 : HFILE_ERROR16
;
887 /***********************************************************************
888 * _lclose32 (KERNEL32.592)
890 HFILE32 WINAPI
_lclose32( HFILE32 hFile
)
892 dprintf_info(file
, "_lclose32: handle %d\n", hFile
);
893 return CloseHandle( hFile
) ? 0 : HFILE_ERROR32
;
897 /***********************************************************************
900 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
904 dprintf_info(file
, "WIN16_hread: %d %08lx %ld\n",
905 hFile
, (DWORD
)buffer
, count
);
907 /* Some programs pass a count larger than the allocated buffer */
908 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
909 if (count
> maxlen
) count
= maxlen
;
910 return _lread32( hFile
, PTR_SEG_TO_LIN(buffer
), count
);
914 /***********************************************************************
917 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
919 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
923 /***********************************************************************
924 * _lread32 (KERNEL32.596)
926 UINT32 WINAPI
_lread32( HFILE32 handle
, LPVOID buffer
, UINT32 count
)
930 BOOL32 result
= FALSE
;
932 dprintf_info( file
, "_lread32: %d %p %d\n", handle
, buffer
, count
);
933 if (!(ptr
= HANDLE_GetObjPtr( handle
, K32OBJ_UNKNOWN
, 0))) return -1;
934 if (K32OBJ_OPS(ptr
)->read
)
935 result
= K32OBJ_OPS(ptr
)->read(ptr
, buffer
, count
, &numWritten
, NULL
);
936 K32OBJ_DecCount( ptr
);
937 if (!result
) return -1;
938 return (UINT32
)numWritten
;
942 /***********************************************************************
943 * _lread16 (KERNEL.82)
945 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
947 return (UINT16
)_lread32( hFile
, buffer
, (LONG
)count
);
951 /***********************************************************************
952 * _lcreat16 (KERNEL.83)
954 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
956 int mode
= (attr
& 1) ? 0444 : 0666;
957 dprintf_info(file
, "_lcreat16: %s %02x\n", path
, attr
);
958 return (HFILE16
)FILE_Create( path
, mode
, FALSE
);
962 /***********************************************************************
963 * _lcreat32 (KERNEL32.593)
965 HFILE32 WINAPI
_lcreat32( LPCSTR path
, INT32 attr
)
967 int mode
= (attr
& 1) ? 0444 : 0666;
968 dprintf_info(file
, "_lcreat32: %s %02x\n", path
, attr
);
969 return FILE_Create( path
, mode
, FALSE
);
973 /***********************************************************************
974 * _lcreat_uniq (Not a Windows API)
976 HFILE32
_lcreat_uniq( LPCSTR path
, INT32 attr
)
978 int mode
= (attr
& 1) ? 0444 : 0666;
979 dprintf_info(file
, "_lcreat_uniq: %s %02x\n", path
, attr
);
980 return FILE_Create( path
, mode
, TRUE
);
984 /***********************************************************************
985 * SetFilePointer (KERNEL32.492)
987 DWORD WINAPI
SetFilePointer( HFILE32 hFile
, LONG distance
, LONG
*highword
,
993 if (highword
&& *highword
)
995 fprintf( stderr
, "SetFilePointer: 64-bit offsets not supported yet\n");
996 SetLastError( ERROR_INVALID_PARAMETER
);
999 dprintf_info(file
, "SetFilePointer: handle %d offset %ld origin %ld\n",
1000 hFile
, distance
, method
);
1002 if (!(file
= FILE_GetFile( hFile
))) return 0xffffffff;
1005 case FILE_CURRENT
: origin
= SEEK_CUR
; break;
1006 case FILE_END
: origin
= SEEK_END
; break;
1007 default: origin
= SEEK_SET
; break;
1010 if ((result
= lseek( file
->unix_handle
, distance
, origin
)) == -1)
1012 FILE_ReleaseFile( file
);
1013 return (DWORD
)result
;
1017 /***********************************************************************
1018 * _llseek16 (KERNEL.84)
1020 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1022 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1026 /***********************************************************************
1027 * _llseek32 (KERNEL32.594)
1029 LONG WINAPI
_llseek32( HFILE32 hFile
, LONG lOffset
, INT32 nOrigin
)
1031 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1035 /***********************************************************************
1036 * _lopen16 (KERNEL.85)
1038 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1040 return _lopen32( path
, mode
);
1044 /***********************************************************************
1045 * _lopen32 (KERNEL32.595)
1047 HFILE32 WINAPI
_lopen32( LPCSTR path
, INT32 mode
)
1051 dprintf_info(file
, "_lopen32('%s',%04x)\n", path
, mode
);
1056 unixMode
= O_WRONLY
;
1063 unixMode
= O_RDONLY
;
1066 return FILE_Open( path
, unixMode
);
1070 /***********************************************************************
1071 * _lwrite16 (KERNEL.86)
1073 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1075 return (UINT16
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1078 /***********************************************************************
1079 * _lwrite32 (KERNEL.86)
1081 UINT32 WINAPI
_lwrite32( HFILE32 hFile
, LPCSTR buffer
, UINT32 count
)
1083 return (UINT32
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1087 /***********************************************************************
1088 * _hread16 (KERNEL.349)
1090 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1092 return _lread32( hFile
, buffer
, count
);
1096 /***********************************************************************
1097 * _hread32 (KERNEL32.590)
1099 LONG WINAPI
_hread32( HFILE32 hFile
, LPVOID buffer
, LONG count
)
1101 return _lread32( hFile
, buffer
, count
);
1105 /***********************************************************************
1106 * _hwrite16 (KERNEL.350)
1108 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1110 return _hwrite32( hFile
, buffer
, count
);
1114 /***********************************************************************
1115 * _hwrite32 (KERNEL32.591)
1117 * experimenation yields that _lwrite:
1118 * o truncates the file at the current position with
1120 * o returns 0 on a 0 length write
1121 * o works with console handles
1124 LONG WINAPI
_hwrite32( HFILE32 handle
, LPCSTR buffer
, LONG count
)
1128 BOOL32 status
= FALSE
;
1130 dprintf_info(file
, "_hwrite32: %d %p %ld\n", handle
, buffer
, count
);
1132 if (count
== 0) { /* Expand or truncate at current position */
1133 FILE_OBJECT
*file
= FILE_GetFile(handle
);
1135 if ( ftruncate(file
->unix_handle
,
1136 lseek( file
->unix_handle
, 0, SEEK_CUR
)) == 0 ) {
1137 FILE_ReleaseFile(file
);
1141 FILE_ReleaseFile(file
);
1142 return HFILE_ERROR32
;
1146 if (!(ioptr
= HANDLE_GetObjPtr( handle
, K32OBJ_UNKNOWN
, 0 )))
1147 return HFILE_ERROR32
;
1148 if (K32OBJ_OPS(ioptr
)->write
)
1149 status
= K32OBJ_OPS(ioptr
)->write(ioptr
, buffer
, count
, &result
, NULL
);
1150 K32OBJ_DecCount( ioptr
);
1151 if (!status
) result
= HFILE_ERROR32
;
1156 /***********************************************************************
1157 * SetHandleCount16 (KERNEL.199)
1159 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1161 HGLOBAL16 hPDB
= GetCurrentPDB();
1162 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1163 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1165 dprintf_info(file
, "SetHandleCount16(%d)\n", count
);
1167 if (count
< 20) count
= 20; /* No point in going below 20 */
1168 else if (count
> 254) count
= 254;
1172 if (pdb
->nbFiles
> 20)
1174 memcpy( pdb
->fileHandles
, files
, 20 );
1175 GlobalFree16( pdb
->hFileHandles
);
1176 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1177 GlobalHandleToSel( hPDB
) );
1178 pdb
->hFileHandles
= 0;
1182 else /* More than 20, need a new file handles table */
1185 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1188 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1189 return pdb
->nbFiles
;
1191 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1193 if (count
> pdb
->nbFiles
)
1195 memcpy( newfiles
, files
, pdb
->nbFiles
);
1196 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1198 else memcpy( newfiles
, files
, count
);
1199 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1200 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1201 pdb
->hFileHandles
= newhandle
;
1202 pdb
->nbFiles
= count
;
1204 return pdb
->nbFiles
;
1208 /*************************************************************************
1209 * SetHandleCount32 (KERNEL32.494)
1211 UINT32 WINAPI
SetHandleCount32( UINT32 count
)
1213 return MIN( 256, count
);
1217 /***********************************************************************
1218 * FlushFileBuffers (KERNEL32.133)
1220 BOOL32 WINAPI
FlushFileBuffers( HFILE32 hFile
)
1225 dprintf_info(file
, "FlushFileBuffers(%d)\n", hFile
);
1226 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1227 if (fsync( file
->unix_handle
) != -1) ret
= TRUE
;
1233 FILE_ReleaseFile( file
);
1238 /**************************************************************************
1239 * SetEndOfFile (KERNEL32.483)
1241 BOOL32 WINAPI
SetEndOfFile( HFILE32 hFile
)
1246 dprintf_info(file
, "SetEndOfFile(%d)\n", hFile
);
1247 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1248 if (ftruncate( file
->unix_handle
,
1249 lseek( file
->unix_handle
, 0, SEEK_CUR
) ))
1254 FILE_ReleaseFile( file
);
1259 /***********************************************************************
1260 * DeleteFile16 (KERNEL.146)
1262 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1264 return DeleteFile32A( path
);
1268 /***********************************************************************
1269 * DeleteFile32A (KERNEL32.71)
1271 BOOL32 WINAPI
DeleteFile32A( LPCSTR path
)
1273 DOS_FULL_NAME full_name
;
1275 dprintf_info(file
, "DeleteFile: '%s'\n", path
);
1277 if (DOSFS_IsDevice( path
))
1279 fprintf(stderr
, "DeleteFile: cannot remove DOS device '%s'!\n", path
);
1280 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1284 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1285 if (unlink( full_name
.long_name
) == -1)
1294 /***********************************************************************
1295 * DeleteFile32W (KERNEL32.72)
1297 BOOL32 WINAPI
DeleteFile32W( LPCWSTR path
)
1299 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1300 BOOL32 ret
= DeleteFile32A( xpath
);
1301 HeapFree( GetProcessHeap(), 0, xpath
);
1306 /***********************************************************************
1309 BOOL32
FILE_SetFileType( HFILE32 hFile
, DWORD type
)
1311 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1312 if (!file
) return FALSE
;
1314 FILE_ReleaseFile( file
);
1319 /***********************************************************************
1322 LPVOID
FILE_mmap( HFILE32 hFile
, LPVOID start
,
1323 DWORD size_high
, DWORD size_low
,
1324 DWORD offset_high
, DWORD offset_low
,
1325 int prot
, int flags
)
1328 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1329 if (!file
) return (LPVOID
)-1;
1330 ret
= FILE_dommap( file
, start
, size_high
, size_low
,
1331 offset_high
, offset_low
, prot
, flags
);
1332 FILE_ReleaseFile( file
);
1337 /***********************************************************************
1340 LPVOID
FILE_dommap( FILE_OBJECT
*file
, LPVOID start
,
1341 DWORD size_high
, DWORD size_low
,
1342 DWORD offset_high
, DWORD offset_low
,
1343 int prot
, int flags
)
1349 if (size_high
|| offset_high
)
1350 fprintf( stderr
, "FILE_mmap: offsets larger than 4Gb not supported\n");
1357 static int fdzero
= -1;
1361 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1363 perror( "/dev/zero: open" );
1368 #endif /* MAP_ANON */
1369 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1371 flags
&= ~MAP_SHARED
;
1374 flags
|= MAP_PRIVATE
;
1377 else fd
= file
->unix_handle
;
1379 if ((ret
= mmap( start
, size_low
, prot
,
1380 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1383 /* mmap() failed; if this is because the file offset is not */
1384 /* page-aligned (EINVAL), or because the underlying filesystem */
1385 /* does not support mmap() (ENOEXEC), we do it by hand. */
1387 if (!file
) return ret
;
1388 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1389 if (prot
& PROT_WRITE
)
1391 /* We cannot fake shared write mappings */
1393 if (flags
& MAP_SHARED
) return ret
;
1396 if (!(flags
& MAP_PRIVATE
)) return ret
;
1399 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1400 /* Reserve the memory with an anonymous mmap */
1401 ret
= FILE_dommap( NULL
, start
, size_high
, size_low
, 0, 0,
1402 PROT_READ
| PROT_WRITE
, flags
);
1403 if (ret
== (LPVOID
)-1) return ret
;
1404 /* Now read in the file */
1405 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1407 FILE_munmap( ret
, size_high
, size_low
);
1410 read( fd
, ret
, size_low
);
1411 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1412 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1417 /***********************************************************************
1420 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1423 fprintf( stderr
, "FILE_munmap: offsets larger than 4Gb not supported\n");
1424 return munmap( start
, size_low
);
1428 /***********************************************************************
1429 * GetFileType (KERNEL32.222)
1431 DWORD WINAPI
GetFileType( HFILE32 hFile
)
1433 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1434 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1435 FILE_ReleaseFile( file
);
1440 /**************************************************************************
1441 * MoveFileEx32A (KERNEL32.???)
1443 BOOL32 WINAPI
MoveFileEx32A( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1445 DOS_FULL_NAME full_name1
, full_name2
;
1446 int mode
=0; /* mode == 1: use copy */
1448 dprintf_info(file
, "MoveFileEx32A(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1450 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1451 if (fn2
) { /* !fn2 means delete fn1 */
1452 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1453 /* Source name and target path are valid */
1454 if ( full_name1
.drive
!= full_name2
.drive
)
1455 /* use copy, if allowed */
1456 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1457 /* FIXME: Use right error code */
1458 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
1462 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1463 /* target exists, check if we may overwrite */
1464 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1465 /* FIXME: Use right error code */
1466 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1470 else /* fn2 == NULL means delete source */
1471 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1472 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1474 "MoveFileEx32A: Illegal flag\n");
1475 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1479 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1480 Perhaps we should queue these command and execute it
1481 when exiting... What about using on_exit(2)
1483 fprintf( stderr
,"MoveFileEx32A: Please delete file %s\n",
1484 full_name1
.long_name
);
1485 fprintf( stderr
," when Wine has finished\n");
1486 fprintf( stderr
," like \"rm %s\"\n",
1487 full_name1
.long_name
);
1490 else if (unlink( full_name1
.long_name
) == -1)
1495 else return TRUE
; /* successfully deleted */
1497 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1498 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1499 Perhaps we should queue these command and execute it
1500 when exiting... What about using on_exit(2)
1502 fprintf( stderr
,"MoveFileEx32A: Please move existing file %s\n"
1503 ,full_name1
.long_name
);
1504 fprintf( stderr
," to file %s\n"
1505 ,full_name2
.long_name
);
1506 fprintf( stderr
," when Wine has finished\n");
1507 fprintf( stderr
," like \" mv %s %s\"\n",
1508 full_name1
.long_name
,full_name2
.long_name
);
1512 if (!mode
) /* move the file */
1513 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1519 else /* copy File */
1520 return CopyFile32A(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1524 /**************************************************************************
1525 * MoveFileEx32W (KERNEL32.???)
1527 BOOL32 WINAPI
MoveFileEx32W( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1529 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1530 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1531 BOOL32 res
= MoveFileEx32A( afn1
, afn2
, flag
);
1532 HeapFree( GetProcessHeap(), 0, afn1
);
1533 HeapFree( GetProcessHeap(), 0, afn2
);
1538 /**************************************************************************
1539 * MoveFile32A (KERNEL32.387)
1541 * Move file or directory
1543 BOOL32 WINAPI
MoveFile32A( LPCSTR fn1
, LPCSTR fn2
)
1545 DOS_FULL_NAME full_name1
, full_name2
;
1548 dprintf_info(file
, "MoveFile32A(%s,%s)\n", fn1
, fn2
);
1550 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1551 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1552 /* The new name must not already exist */
1554 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1556 if (full_name1
.drive
== full_name2
.drive
) /* move */
1557 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1564 if (stat( full_name1
.long_name
, &fstat
))
1566 dprintf_warn(file
, "Invalid source file %s\n",
1567 full_name1
.long_name
);
1571 if (S_ISDIR(fstat
.st_mode
)) {
1572 /* No Move for directories across file systems */
1573 /* FIXME: Use right error code */
1574 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1579 return CopyFile32A(fn1
, fn2
, TRUE
); /*fail, if exist */
1584 /**************************************************************************
1585 * MoveFile32W (KERNEL32.390)
1587 BOOL32 WINAPI
MoveFile32W( LPCWSTR fn1
, LPCWSTR fn2
)
1589 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1590 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1591 BOOL32 res
= MoveFile32A( afn1
, afn2
);
1592 HeapFree( GetProcessHeap(), 0, afn1
);
1593 HeapFree( GetProcessHeap(), 0, afn2
);
1598 /**************************************************************************
1599 * CopyFile32A (KERNEL32.36)
1601 BOOL32 WINAPI
CopyFile32A( LPCSTR source
, LPCSTR dest
, BOOL32 fail_if_exists
)
1604 BY_HANDLE_FILE_INFORMATION info
;
1610 if ((h1
= _lopen32( source
, OF_READ
)) == HFILE_ERROR32
) return FALSE
;
1611 if (!GetFileInformationByHandle( h1
, &info
))
1616 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1617 if ((h2
= FILE_Create( dest
, mode
, fail_if_exists
)) == HFILE_ERROR32
)
1622 while ((count
= _lread32( h2
, buffer
, sizeof(buffer
) )) > 0)
1627 INT32 res
= _lwrite32( h2
, p
, count
);
1628 if (res
<= 0) goto done
;
1641 /**************************************************************************
1642 * CopyFile32W (KERNEL32.37)
1644 BOOL32 WINAPI
CopyFile32W( LPCWSTR source
, LPCWSTR dest
, BOOL32 fail_if_exists
)
1646 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1647 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1648 BOOL32 ret
= CopyFile32A( sourceA
, destA
, fail_if_exists
);
1649 HeapFree( GetProcessHeap(), 0, sourceA
);
1650 HeapFree( GetProcessHeap(), 0, destA
);
1655 /***********************************************************************
1656 * SetFileTime (KERNEL32.493)
1658 BOOL32 WINAPI
SetFileTime( HFILE32 hFile
,
1659 const FILETIME
*lpCreationTime
,
1660 const FILETIME
*lpLastAccessTime
,
1661 const FILETIME
*lpLastWriteTime
)
1663 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1664 struct utimbuf utimbuf
;
1666 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1667 dprintf_info(file
,"SetFileTime(%s,%p,%p,%p)\n",
1673 if (lpLastAccessTime
)
1674 utimbuf
.actime
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1676 utimbuf
.actime
= 0; /* FIXME */
1677 if (lpLastWriteTime
)
1678 utimbuf
.modtime
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1680 utimbuf
.modtime
= 0; /* FIXME */
1681 if (-1==utime(file
->unix_name
,&utimbuf
))
1683 FILE_ReleaseFile( file
);
1687 FILE_ReleaseFile( file
);
1691 /* Locks need to be mirrored because unix file locking is based
1692 * on the pid. Inside of wine there can be multiple WINE processes
1693 * that share the same unix pid.
1694 * Read's and writes should check these locks also - not sure
1695 * how critical that is at this point (FIXME).
1698 static BOOL32
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
1700 DOS_FILE_LOCK
*curr
;
1703 processId
= GetCurrentProcessId();
1705 /* check if lock overlaps a current lock for the same file */
1706 for (curr
= locks
; curr
; curr
= curr
->next
) {
1707 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
1708 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
1709 return TRUE
;/* region is identic */
1710 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
1711 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
1712 /* region overlaps */
1718 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
1719 curr
->processId
= GetCurrentProcessId();
1720 curr
->base
= f
->l_start
;
1721 curr
->len
= f
->l_len
;
1722 curr
->unix_name
= HEAP_strdupA( SystemHeap
, 0, file
->unix_name
);
1724 curr
->dos_file
= file
;
1729 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
1732 DOS_FILE_LOCK
**curr
;
1735 processId
= GetCurrentProcessId();
1738 if ((*curr
)->dos_file
== file
) {
1740 *curr
= (*curr
)->next
;
1741 HeapFree( SystemHeap
, 0, rem
->unix_name
);
1742 HeapFree( SystemHeap
, 0, rem
);
1745 curr
= &(*curr
)->next
;
1749 static BOOL32
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
1752 DOS_FILE_LOCK
**curr
;
1755 processId
= GetCurrentProcessId();
1756 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
1757 if ((*curr
)->processId
== processId
&&
1758 (*curr
)->dos_file
== file
&&
1759 (*curr
)->base
== f
->l_start
&&
1760 (*curr
)->len
== f
->l_len
) {
1761 /* this is the same lock */
1763 *curr
= (*curr
)->next
;
1764 HeapFree( SystemHeap
, 0, rem
->unix_name
);
1765 HeapFree( SystemHeap
, 0, rem
);
1769 /* no matching lock found */
1774 /**************************************************************************
1775 * LockFile (KERNEL32.511)
1777 BOOL32 WINAPI
LockFile(
1778 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
1779 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
1784 dprintf_info(file
, "LockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1785 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
1786 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
1788 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
1789 dprintf_fixme(file
, "LockFile32: Unimplemented bytes > 32bits\n");
1793 f
.l_start
= dwFileOffsetLow
;
1794 f
.l_len
= nNumberOfBytesToLockLow
;
1795 f
.l_whence
= SEEK_SET
;
1799 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
1801 /* shadow locks internally */
1802 if (!DOS_AddLock(file
, &f
)) {
1803 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
1807 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1808 #ifdef USE_UNIX_LOCKS
1809 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
1810 if (errno
== EACCES
|| errno
== EAGAIN
) {
1811 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
1816 /* remove our internal copy of the lock */
1817 DOS_RemoveLock(file
, &f
);
1825 /**************************************************************************
1826 * UnlockFile (KERNEL32.703)
1828 BOOL32 WINAPI
UnlockFile(
1829 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
1830 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
1835 dprintf_info(file
, "UnlockFile32: handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1836 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
1837 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
1839 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
1840 dprintf_warn(file
, "UnlockFile32: Unimplemented bytes > 32bits\n");
1844 f
.l_start
= dwFileOffsetLow
;
1845 f
.l_len
= nNumberOfBytesToUnlockLow
;
1846 f
.l_whence
= SEEK_SET
;
1850 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
1852 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
1854 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1855 #ifdef USE_UNIX_LOCKS
1856 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {