2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
14 #include <sys/errno.h>
15 #include <sys/types.h>
35 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
36 #define MAP_ANON MAP_ANONYMOUS
39 static BOOL32
FILE_Read(K32OBJ
*ptr
, LPVOID lpBuffer
, DWORD nNumberOfChars
,
40 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
);
41 static BOOL32
FILE_Write(K32OBJ
*ptr
, LPCVOID lpBuffer
, DWORD nNumberOfChars
,
42 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
);
43 static void FILE_Destroy( K32OBJ
*obj
);
45 const K32OBJ_OPS FILE_Ops
=
47 /* Object cannot be waited upon (FIXME: for now) */
51 NULL
, /* remove_wait */
53 FILE_Write
, /* write */
54 FILE_Destroy
/* destroy */
57 struct DOS_FILE_LOCK
{
58 struct DOS_FILE_LOCK
* next
;
62 FILE_OBJECT
* dos_file
;
66 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
68 static DOS_FILE_LOCK
*locks
= NULL
;
69 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
71 /***********************************************************************
76 HFILE32
FILE_Alloc( FILE_OBJECT
**file
)
79 *file
= HeapAlloc( SystemHeap
, 0, sizeof(FILE_OBJECT
) );
82 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
85 (*file
)->header
.type
= K32OBJ_FILE
;
86 (*file
)->header
.refcount
= 0;
87 (*file
)->unix_handle
= -1;
88 (*file
)->unix_name
= NULL
;
89 (*file
)->type
= FILE_TYPE_DISK
;
92 handle
= HANDLE_Alloc( PROCESS_Current(), &(*file
)->header
,
93 FILE_ALL_ACCESS
| GENERIC_READ
|
94 GENERIC_WRITE
| GENERIC_EXECUTE
/*FIXME*/, TRUE
, -1 );
95 /* If the allocation failed, the object is already destroyed */
96 if (handle
== INVALID_HANDLE_VALUE32
) *file
= NULL
;
101 /* FIXME: lpOverlapped is ignored */
102 static BOOL32
FILE_Read(K32OBJ
*ptr
, LPVOID lpBuffer
, DWORD nNumberOfChars
,
103 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
105 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
108 TRACE(file
, "%p %p %ld\n", ptr
, lpBuffer
,
111 if (nNumberOfChars
== 0) {
112 *lpNumberOfChars
= 0; /* FIXME: does this change */
116 if ( (file
->pos
< 0) || /* workaround, see SetFilePointer */
117 ((result
= read(file
->unix_handle
, lpBuffer
, nNumberOfChars
)) == -1) )
123 *lpNumberOfChars
= result
;
128 * experimentation yields that WriteFile:
129 * o does not truncate on write of 0
130 * o always changes the *lpNumberOfChars to actual number of
132 * o write of 0 nNumberOfChars returns TRUE
134 static BOOL32
FILE_Write(K32OBJ
*ptr
, LPCVOID lpBuffer
, DWORD nNumberOfChars
,
135 LPDWORD lpNumberOfChars
, LPOVERLAPPED lpOverlapped
)
137 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
140 TRACE(file
, "%p %p %ld\n", ptr
, lpBuffer
,
143 *lpNumberOfChars
= 0;
146 * I assume this loop around EAGAIN is here because
147 * win32 doesn't have interrupted system calls
150 if (file
->pos
< 0) { /* workaround, see SetFilePointer */
157 result
= write(file
->unix_handle
, lpBuffer
, nNumberOfChars
);
159 *lpNumberOfChars
= result
;
163 if (errno
!= EINTR
) {
172 /***********************************************************************
175 * Destroy a DOS file.
177 static void FILE_Destroy( K32OBJ
*ptr
)
179 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
180 assert( ptr
->type
== K32OBJ_FILE
);
182 DOS_RemoveFileLocks(file
);
184 if (file
->unix_handle
!= -1) close( file
->unix_handle
);
185 if (file
->unix_name
) HeapFree( SystemHeap
, 0, file
->unix_name
);
186 ptr
->type
= K32OBJ_UNKNOWN
;
187 HeapFree( SystemHeap
, 0, file
);
191 /***********************************************************************
194 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
195 * be called to release the file.
197 FILE_OBJECT
*FILE_GetFile( HFILE32 handle
)
199 return (FILE_OBJECT
*)HANDLE_GetObjPtr( PROCESS_Current(), handle
,
200 K32OBJ_FILE
, 0 /*FIXME*/, NULL
);
204 /***********************************************************************
207 * Release a DOS file obtained with FILE_GetFile.
209 void FILE_ReleaseFile( FILE_OBJECT
*file
)
211 K32OBJ_DecCount( &file
->header
);
215 /***********************************************************************
218 * Return the Unix handle associated to a file handle.
220 int FILE_GetUnixHandle( HFILE32 hFile
)
225 if (!(file
= FILE_GetFile( hFile
))) return -1;
226 ret
= file
->unix_handle
;
227 FILE_ReleaseFile( file
);
232 /***********************************************************************
235 * Set the DOS error code from errno.
237 void FILE_SetDosError(void)
239 int save_errno
= errno
; /* errno gets overwritten by printf */
241 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
245 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
248 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
251 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
256 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
259 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
262 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
265 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
269 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
272 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
276 DOS_ERROR( ER_SeekError
, EC_NotFound
, SA_Ignore
, EL_Disk
);
279 DOS_ERROR( ERROR_DIR_NOT_EMPTY
, EC_Exists
, SA_Ignore
, EL_Disk
);
282 perror( "int21: unknown errno" );
283 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
290 /***********************************************************************
293 * Duplicate a Unix handle into a task handle.
295 HFILE32
FILE_DupUnixHandle( int fd
)
300 if ((handle
= FILE_Alloc( &file
)) != INVALID_HANDLE_VALUE32
)
302 if ((file
->unix_handle
= dup(fd
)) == -1)
305 CloseHandle( handle
);
306 return INVALID_HANDLE_VALUE32
;
313 /***********************************************************************
316 HFILE32
FILE_OpenUnixFile( const char *name
, int mode
)
322 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
323 return INVALID_HANDLE_VALUE32
;
325 if ((file
->unix_handle
= open( name
, mode
, 0666 )) == -1)
327 if (!Options
.failReadOnly
&& (mode
== O_RDWR
))
328 file
->unix_handle
= open( name
, O_RDONLY
);
330 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
333 CloseHandle( handle
);
334 return INVALID_HANDLE_VALUE32
;
336 if (S_ISDIR(st
.st_mode
))
338 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
339 CloseHandle( handle
);
340 return INVALID_HANDLE_VALUE32
;
343 /* File opened OK, now fill the FILE_OBJECT */
345 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, name
);
350 /***********************************************************************
353 HFILE32
FILE_Open( LPCSTR path
, INT32 mode
)
355 DOS_FULL_NAME full_name
;
356 const char *unixName
;
358 TRACE(file
, "'%s' %04x\n", path
, mode
);
360 if (!path
) return HFILE_ERROR32
;
362 if (DOSFS_GetDevice( path
))
366 TRACE(file
, "opening device '%s'\n", path
);
368 if (HFILE_ERROR32
!=(ret
=DOSFS_OpenDevice( path
, mode
)))
371 /* Do not silence this please. It is a critical error. -MM */
372 ERR(file
, "Couldn't open device '%s'!\n",path
);
373 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
374 return HFILE_ERROR32
;
377 else /* check for filename, don't check for last entry if creating */
379 if (!DOSFS_GetFullName( path
, !(mode
& O_CREAT
), &full_name
))
380 return HFILE_ERROR32
;
381 unixName
= full_name
.long_name
;
383 return FILE_OpenUnixFile( unixName
, mode
);
387 /***********************************************************************
390 static HFILE32
FILE_Create( LPCSTR path
, int mode
, int unique
)
394 DOS_FULL_NAME full_name
;
396 TRACE(file
, "'%s' %04x %d\n", path
, mode
, unique
);
398 if (!path
) return INVALID_HANDLE_VALUE32
;
400 if (DOSFS_GetDevice( path
))
402 WARN(file
, "cannot create DOS device '%s'!\n", path
);
403 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
404 return INVALID_HANDLE_VALUE32
;
407 if ((handle
= FILE_Alloc( &file
)) == INVALID_HANDLE_VALUE32
)
408 return INVALID_HANDLE_VALUE32
;
410 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
))
412 CloseHandle( handle
);
413 return INVALID_HANDLE_VALUE32
;
415 if ((file
->unix_handle
= open( full_name
.long_name
,
416 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
420 CloseHandle( handle
);
421 return INVALID_HANDLE_VALUE32
;
424 /* File created OK, now fill the FILE_OBJECT */
426 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, full_name
.long_name
);
431 /***********************************************************************
434 * Fill a file information from a struct stat.
436 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
438 if (S_ISDIR(st
->st_mode
))
439 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
441 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
442 if (!(st
->st_mode
& S_IWUSR
))
443 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
445 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
446 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
447 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
449 info
->dwVolumeSerialNumber
= 0; /* FIXME */
450 info
->nFileSizeHigh
= 0;
451 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
452 info
->nNumberOfLinks
= st
->st_nlink
;
453 info
->nFileIndexHigh
= 0;
454 info
->nFileIndexLow
= st
->st_ino
;
458 /***********************************************************************
461 * Stat a Unix path name. Return TRUE if OK.
463 BOOL32
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
467 if (!unixName
|| !info
) return FALSE
;
469 if (stat( unixName
, &st
) == -1)
474 FILE_FillInfo( &st
, info
);
479 /***********************************************************************
480 * GetFileInformationByHandle (KERNEL32.219)
482 DWORD WINAPI
GetFileInformationByHandle( HFILE32 hFile
,
483 BY_HANDLE_FILE_INFORMATION
*info
)
491 if (!(file
= FILE_GetFile( hFile
))) return 0;
492 if (fstat( file
->unix_handle
, &st
) == -1) FILE_SetDosError();
495 FILE_FillInfo( &st
, info
);
498 FILE_ReleaseFile( file
);
503 /**************************************************************************
504 * GetFileAttributes16 (KERNEL.420)
506 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
508 return GetFileAttributes32A( name
);
512 /**************************************************************************
513 * GetFileAttributes32A (KERNEL32.217)
515 DWORD WINAPI
GetFileAttributes32A( LPCSTR name
)
517 DOS_FULL_NAME full_name
;
518 BY_HANDLE_FILE_INFORMATION info
;
520 if (name
== NULL
|| *name
=='\0') return -1;
522 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
523 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
524 return info
.dwFileAttributes
;
528 /**************************************************************************
529 * GetFileAttributes32W (KERNEL32.218)
531 DWORD WINAPI
GetFileAttributes32W( LPCWSTR name
)
533 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
534 DWORD res
= GetFileAttributes32A( nameA
);
535 HeapFree( GetProcessHeap(), 0, nameA
);
540 /***********************************************************************
541 * GetFileSize (KERNEL32.220)
543 DWORD WINAPI
GetFileSize( HFILE32 hFile
, LPDWORD filesizehigh
)
545 BY_HANDLE_FILE_INFORMATION info
;
546 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
547 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
548 return info
.nFileSizeLow
;
552 /***********************************************************************
553 * GetFileTime (KERNEL32.221)
555 BOOL32 WINAPI
GetFileTime( HFILE32 hFile
, FILETIME
*lpCreationTime
,
556 FILETIME
*lpLastAccessTime
,
557 FILETIME
*lpLastWriteTime
)
559 BY_HANDLE_FILE_INFORMATION info
;
560 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
561 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
562 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
563 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
567 /***********************************************************************
568 * CompareFileTime (KERNEL32.28)
570 INT32 WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
572 if (!x
|| !y
) return -1;
574 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
576 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
578 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
580 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
585 /***********************************************************************
588 * dup() function for DOS handles.
590 HFILE32
FILE_Dup( HFILE32 hFile
)
594 TRACE(file
, "FILE_Dup for handle %d\n", hFile
);
595 if (!DuplicateHandle( GetCurrentProcess(), hFile
, GetCurrentProcess(),
596 &handle
, FILE_ALL_ACCESS
/* FIXME */, FALSE
, 0 ))
597 handle
= HFILE_ERROR32
;
598 TRACE(file
, "FILE_Dup return handle %d\n", handle
);
603 /***********************************************************************
606 * dup2() function for DOS handles.
608 HFILE32
FILE_Dup2( HFILE32 hFile1
, HFILE32 hFile2
)
612 TRACE(file
, "FILE_Dup2 for handle %d\n", hFile1
);
613 /* FIXME: should use DuplicateHandle */
614 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR32
;
615 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2
, &file
->header
, 0 ))
616 hFile2
= HFILE_ERROR32
;
617 FILE_ReleaseFile( file
);
622 /***********************************************************************
623 * GetTempFileName16 (KERNEL.97)
625 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
630 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
631 drive
|= DRIVE_GetCurrentDrive() + 'A';
633 if ((drive
& TF_FORCEDRIVE
) &&
634 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
636 drive
&= ~TF_FORCEDRIVE
;
637 WARN(file
, "invalid drive %d specified\n", drive
);
640 if (drive
& TF_FORCEDRIVE
)
641 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
643 GetTempPath32A( 132, temppath
);
644 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
648 /***********************************************************************
649 * GetTempFileName32A (KERNEL32.290)
651 UINT32 WINAPI
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
654 DOS_FULL_NAME full_name
;
657 UINT32 num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
659 if ( !path
|| !prefix
|| !buffer
) return 0;
661 strcpy( buffer
, path
);
662 p
= buffer
+ strlen(buffer
);
664 /* add a \, if there isn't one and path is more than just the drive letter ... */
665 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
666 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
669 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
670 sprintf( p
, "%04x.tmp", num
);
672 /* Now try to create it */
678 HFILE32 handle
= FILE_Create( buffer
, 0666, TRUE
);
679 if (handle
!= INVALID_HANDLE_VALUE32
)
680 { /* We created it */
681 TRACE(file
, "created %s\n",
683 CloseHandle( handle
);
686 if (DOS_ExtendedError
!= ER_FileExists
)
687 break; /* No need to go on */
689 sprintf( p
, "%04x.tmp", num
);
690 } while (num
!= (unique
& 0xffff));
693 /* Get the full path name */
695 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
697 /* Check if we have write access in the directory */
698 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
699 if (access( full_name
.long_name
, W_OK
) == -1)
700 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
703 TRACE(file
, "returning %s\n", buffer
);
704 return unique
? unique
: num
;
708 /***********************************************************************
709 * GetTempFileName32W (KERNEL32.291)
711 UINT32 WINAPI
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
719 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
720 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
721 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
722 lstrcpyAtoW( buffer
, buffera
);
723 HeapFree( GetProcessHeap(), 0, patha
);
724 HeapFree( GetProcessHeap(), 0, prefixa
);
729 /***********************************************************************
732 * Implementation of OpenFile16() and OpenFile32().
734 static HFILE32
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
,
739 WORD filedatetime
[2];
740 DOS_FULL_NAME full_name
;
744 if (!ofs
) return HFILE_ERROR32
;
747 ofs
->cBytes
= sizeof(OFSTRUCT
);
749 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
752 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
753 return HFILE_ERROR32
;
756 TRACE(file
, "%s %04x\n", name
, mode
);
758 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
759 Are there any cases where getting the path here is wrong?
760 Uwe Bonnes 1997 Apr 2 */
761 if (!GetFullPathName32A( name
, sizeof(ofs
->szPathName
),
762 ofs
->szPathName
, NULL
)) goto error
;
764 /* OF_PARSE simply fills the structure */
768 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
770 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
771 name
, ofs
->szPathName
);
775 /* OF_CREATE is completely different from all other options, so
778 if (mode
& OF_CREATE
)
780 if ((hFileRet
= FILE_Create(name
,0666,FALSE
))== INVALID_HANDLE_VALUE32
)
785 /* If OF_SEARCH is set, ignore the given path */
787 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
789 /* First try the file name as is */
790 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
791 /* Now remove the path */
792 if (name
[0] && (name
[1] == ':')) name
+= 2;
793 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
794 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
795 if (!name
[0]) goto not_found
;
798 /* Now look for the file */
800 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
803 TRACE(file
, "found %s = %s\n",
804 full_name
.long_name
, full_name
.short_name
);
805 lstrcpyn32A( ofs
->szPathName
, full_name
.short_name
,
806 sizeof(ofs
->szPathName
) );
808 if (mode
& OF_SHARE_EXCLUSIVE
)
810 char *last
= strrchr(full_name
.long_name
,'/');
812 last
= full_name
.long_name
- 1;
813 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
814 on the file <tempdir>/_ins0432._mp to determine how
815 far installation has proceeded*/
816 if (GetModuleHandle16(last
+1))
818 TRACE(file
,"Denying shared open for %s\n",full_name
.long_name
);
819 return HFILE_ERROR32
;
821 FIXME(file
,"OF_SHARE_EXCLUSIVE only partial implemented\n");
824 if (mode
& OF_DELETE
)
826 if (unlink( full_name
.long_name
) == -1) goto not_found
;
827 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
834 unixMode
= O_WRONLY
; break;
836 unixMode
= O_RDWR
; break;
839 unixMode
= O_RDONLY
; break;
842 hFileRet
= FILE_OpenUnixFile( full_name
.long_name
, unixMode
);
843 if (hFileRet
== HFILE_ERROR32
) goto not_found
;
844 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
845 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
846 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
848 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
850 CloseHandle( hFileRet
);
851 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
852 /* FIXME: what error here? */
853 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
857 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
859 success
: /* We get here if the open was successful */
860 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
861 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
862 CloseHandle( hFileRet
);
865 not_found
: /* We get here if the file does not exist */
866 WARN(file
, "'%s' not found\n", name
);
867 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
870 error
: /* We get here if there was an error opening the file */
871 ofs
->nErrCode
= DOS_ExtendedError
;
872 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
873 name
,ofs
->nErrCode
);
874 return HFILE_ERROR32
;
878 /***********************************************************************
879 * OpenFile16 (KERNEL.74)
881 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
883 TRACE(file
,"OpenFile16(%s,%i)\n", name
, mode
);
884 return HFILE32_TO_HFILE16(FILE_DoOpenFile( name
, ofs
, mode
, FALSE
));
888 /***********************************************************************
889 * OpenFile32 (KERNEL32.396)
891 HFILE32 WINAPI
OpenFile32( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
893 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
897 /***********************************************************************
898 * _lclose16 (KERNEL.81)
900 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
902 TRACE(file
, "handle %d\n", hFile
);
903 return CloseHandle( HFILE16_TO_HFILE32( hFile
) ) ? 0 : HFILE_ERROR16
;
907 /***********************************************************************
908 * _lclose32 (KERNEL32.592)
910 HFILE32 WINAPI
_lclose32( HFILE32 hFile
)
912 TRACE(file
, "handle %d\n", hFile
);
913 return CloseHandle( hFile
) ? 0 : HFILE_ERROR32
;
917 /***********************************************************************
920 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
924 TRACE(file
, "%d %08lx %ld\n",
925 hFile
, (DWORD
)buffer
, count
);
927 /* Some programs pass a count larger than the allocated buffer */
928 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
929 if (count
> maxlen
) count
= maxlen
;
930 return _lread32(HFILE16_TO_HFILE32(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
934 /***********************************************************************
937 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
939 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
943 /***********************************************************************
944 * _lread32 (KERNEL32.596)
946 UINT32 WINAPI
_lread32( HFILE32 handle
, LPVOID buffer
, UINT32 count
)
950 BOOL32 result
= FALSE
;
952 TRACE( file
, "%d %p %d\n", handle
, buffer
, count
);
953 if (!(ptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
954 K32OBJ_UNKNOWN
, 0, NULL
))) return -1;
955 if (K32OBJ_OPS(ptr
)->read
)
956 result
= K32OBJ_OPS(ptr
)->read(ptr
, buffer
, count
, &numWritten
, NULL
);
957 K32OBJ_DecCount( ptr
);
958 if (!result
) return -1;
959 return (UINT32
)numWritten
;
963 /***********************************************************************
964 * _lread16 (KERNEL.82)
966 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
968 return (UINT16
)_lread32(HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
972 /***********************************************************************
973 * _lcreat16 (KERNEL.83)
975 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
977 int mode
= (attr
& 1) ? 0444 : 0666;
978 TRACE(file
, "%s %02x\n", path
, attr
);
979 return (HFILE16
) HFILE32_TO_HFILE16(FILE_Create( path
, mode
, FALSE
));
983 /***********************************************************************
984 * _lcreat32 (KERNEL32.593)
986 HFILE32 WINAPI
_lcreat32( LPCSTR path
, INT32 attr
)
988 int mode
= (attr
& 1) ? 0444 : 0666;
989 TRACE(file
, "%s %02x\n", path
, attr
);
990 return FILE_Create( path
, mode
, FALSE
);
994 /***********************************************************************
995 * _lcreat_uniq (Not a Windows API)
997 HFILE32
_lcreat_uniq( LPCSTR path
, INT32 attr
)
999 int mode
= (attr
& 1) ? 0444 : 0666;
1000 TRACE(file
, "%s %02x\n", path
, attr
);
1001 return FILE_Create( path
, mode
, TRUE
);
1005 /***********************************************************************
1006 * SetFilePointer (KERNEL32.492)
1008 DWORD WINAPI
SetFilePointer( HFILE32 hFile
, LONG distance
, LONG
*highword
,
1012 DWORD result
= 0xffffffff;
1014 if (highword
&& *highword
)
1016 FIXME(file
, "64-bit offsets not supported yet\n");
1017 SetLastError( ERROR_INVALID_PARAMETER
);
1020 TRACE(file
, "handle %d offset %ld origin %ld\n",
1021 hFile
, distance
, method
);
1023 if (!(file
= FILE_GetFile( hFile
))) return 0xffffffff;
1026 /* the pointer may be positioned before the start of the file;
1027 no error is returned in that case,
1028 but subsequent attempts at I/O will produce errors.
1029 This is not allowed with Unix lseek(),
1030 so we'll need some emulation here */
1034 distance
+= file
->pos
; /* fall through */
1036 if ((result
= lseek(file
->unix_handle
, distance
, SEEK_SET
)) == -1)
1038 if ((INT32
)distance
< 0)
1039 file
->pos
= result
= distance
;
1045 if ((result
= lseek(file
->unix_handle
, distance
, SEEK_END
)) == -1)
1047 if ((INT32
)distance
< 0)
1050 result
= lseek(file
->unix_handle
, 0, SEEK_END
);
1052 /* return to the old pos, as the first lseek failed */
1053 lseek(file
->unix_handle
, file
->pos
, SEEK_END
);
1055 file
->pos
= (result
+= distance
);
1058 ERR(file
, "lseek: unknown error. Please report.\n");
1060 else file
->pos
= result
;
1063 ERR(file
, "Unknown origin %ld !\n", method
);
1069 FILE_ReleaseFile( file
);
1074 /***********************************************************************
1075 * _llseek16 (KERNEL.84)
1077 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1079 return SetFilePointer( HFILE16_TO_HFILE32(hFile
), lOffset
, NULL
, nOrigin
);
1083 /***********************************************************************
1084 * _llseek32 (KERNEL32.594)
1086 LONG WINAPI
_llseek32( HFILE32 hFile
, LONG lOffset
, INT32 nOrigin
)
1088 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1092 /***********************************************************************
1093 * _lopen16 (KERNEL.85)
1095 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1097 return HFILE32_TO_HFILE16(_lopen32( path
, mode
));
1101 /***********************************************************************
1102 * _lopen32 (KERNEL32.595)
1104 HFILE32 WINAPI
_lopen32( LPCSTR path
, INT32 mode
)
1108 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1113 unixMode
= O_WRONLY
;
1120 unixMode
= O_RDONLY
;
1123 return FILE_Open( path
, unixMode
);
1127 /***********************************************************************
1128 * _lwrite16 (KERNEL.86)
1130 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1132 return (UINT16
)_hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1135 /***********************************************************************
1136 * _lwrite32 (KERNEL.86)
1138 UINT32 WINAPI
_lwrite32( HFILE32 hFile
, LPCSTR buffer
, UINT32 count
)
1140 return (UINT32
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1144 /***********************************************************************
1145 * _hread16 (KERNEL.349)
1147 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1149 return _lread32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1153 /***********************************************************************
1154 * _hread32 (KERNEL32.590)
1156 LONG WINAPI
_hread32( HFILE32 hFile
, LPVOID buffer
, LONG count
)
1158 return _lread32( hFile
, buffer
, count
);
1162 /***********************************************************************
1163 * _hwrite16 (KERNEL.350)
1165 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1167 return _hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1171 /***********************************************************************
1172 * _hwrite32 (KERNEL32.591)
1174 * experimenation yields that _lwrite:
1175 * o truncates the file at the current position with
1177 * o returns 0 on a 0 length write
1178 * o works with console handles
1181 LONG WINAPI
_hwrite32( HFILE32 handle
, LPCSTR buffer
, LONG count
)
1185 BOOL32 status
= FALSE
;
1187 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1189 if (count
== 0) { /* Expand or truncate at current position */
1190 FILE_OBJECT
*file
= FILE_GetFile(handle
);
1192 if ( ftruncate(file
->unix_handle
,
1193 lseek( file
->unix_handle
, 0, SEEK_CUR
)) == 0 ) {
1194 FILE_ReleaseFile(file
);
1198 FILE_ReleaseFile(file
);
1199 return HFILE_ERROR32
;
1203 if (!(ioptr
= HANDLE_GetObjPtr( PROCESS_Current(), handle
,
1204 K32OBJ_UNKNOWN
, 0, NULL
)))
1205 return HFILE_ERROR32
;
1206 if (K32OBJ_OPS(ioptr
)->write
)
1207 status
= K32OBJ_OPS(ioptr
)->write(ioptr
, buffer
, count
, &result
, NULL
);
1208 K32OBJ_DecCount( ioptr
);
1209 if (!status
) result
= HFILE_ERROR32
;
1214 /***********************************************************************
1215 * SetHandleCount16 (KERNEL.199)
1217 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1219 HGLOBAL16 hPDB
= GetCurrentPDB();
1220 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1221 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1223 TRACE(file
, "(%d)\n", count
);
1225 if (count
< 20) count
= 20; /* No point in going below 20 */
1226 else if (count
> 254) count
= 254;
1230 if (pdb
->nbFiles
> 20)
1232 memcpy( pdb
->fileHandles
, files
, 20 );
1233 GlobalFree16( pdb
->hFileHandles
);
1234 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1235 GlobalHandleToSel( hPDB
) );
1236 pdb
->hFileHandles
= 0;
1240 else /* More than 20, need a new file handles table */
1243 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1246 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1247 return pdb
->nbFiles
;
1249 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1251 if (count
> pdb
->nbFiles
)
1253 memcpy( newfiles
, files
, pdb
->nbFiles
);
1254 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1256 else memcpy( newfiles
, files
, count
);
1257 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1258 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1259 pdb
->hFileHandles
= newhandle
;
1260 pdb
->nbFiles
= count
;
1262 return pdb
->nbFiles
;
1266 /*************************************************************************
1267 * SetHandleCount32 (KERNEL32.494)
1269 UINT32 WINAPI
SetHandleCount32( UINT32 count
)
1271 return MIN( 256, count
);
1275 /***********************************************************************
1276 * FlushFileBuffers (KERNEL32.133)
1278 BOOL32 WINAPI
FlushFileBuffers( HFILE32 hFile
)
1283 TRACE(file
, "(%d)\n", hFile
);
1284 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1285 if (fsync( file
->unix_handle
) != -1) ret
= TRUE
;
1291 FILE_ReleaseFile( file
);
1296 /**************************************************************************
1297 * SetEndOfFile (KERNEL32.483)
1299 BOOL32 WINAPI
SetEndOfFile( HFILE32 hFile
)
1304 TRACE(file
, "(%d)\n", hFile
);
1305 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1306 if (ftruncate( file
->unix_handle
,
1307 lseek( file
->unix_handle
, 0, SEEK_CUR
) ))
1312 FILE_ReleaseFile( file
);
1317 /***********************************************************************
1318 * DeleteFile16 (KERNEL.146)
1320 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1322 return DeleteFile32A( path
);
1326 /***********************************************************************
1327 * DeleteFile32A (KERNEL32.71)
1329 BOOL32 WINAPI
DeleteFile32A( LPCSTR path
)
1331 DOS_FULL_NAME full_name
;
1333 TRACE(file
, "'%s'\n", path
);
1335 if (DOSFS_GetDevice( path
))
1337 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1338 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1342 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1343 if (unlink( full_name
.long_name
) == -1)
1352 /***********************************************************************
1353 * DeleteFile32W (KERNEL32.72)
1355 BOOL32 WINAPI
DeleteFile32W( LPCWSTR path
)
1357 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1358 BOOL32 ret
= DeleteFile32A( xpath
);
1359 HeapFree( GetProcessHeap(), 0, xpath
);
1364 /***********************************************************************
1367 BOOL32
FILE_SetFileType( HFILE32 hFile
, DWORD type
)
1369 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1370 if (!file
) return FALSE
;
1372 FILE_ReleaseFile( file
);
1377 /***********************************************************************
1380 LPVOID
FILE_mmap( HFILE32 hFile
, LPVOID start
,
1381 DWORD size_high
, DWORD size_low
,
1382 DWORD offset_high
, DWORD offset_low
,
1383 int prot
, int flags
)
1386 FILE_OBJECT
*file
= FILE_GetFile( hFile
);
1387 if (!file
) return (LPVOID
)-1;
1388 ret
= FILE_dommap( file
, start
, size_high
, size_low
,
1389 offset_high
, offset_low
, prot
, flags
);
1390 FILE_ReleaseFile( file
);
1395 /***********************************************************************
1398 LPVOID
FILE_dommap( FILE_OBJECT
*file
, LPVOID start
,
1399 DWORD size_high
, DWORD size_low
,
1400 DWORD offset_high
, DWORD offset_low
,
1401 int prot
, int flags
)
1407 if (size_high
|| offset_high
)
1408 FIXME(file
, "offsets larger than 4Gb not supported\n");
1415 static int fdzero
= -1;
1419 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1421 perror( "/dev/zero: open" );
1426 #endif /* MAP_ANON */
1427 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1429 flags
&= ~MAP_SHARED
;
1432 flags
|= MAP_PRIVATE
;
1435 else fd
= file
->unix_handle
;
1437 if ((ret
= mmap( start
, size_low
, prot
,
1438 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1441 /* mmap() failed; if this is because the file offset is not */
1442 /* page-aligned (EINVAL), or because the underlying filesystem */
1443 /* does not support mmap() (ENOEXEC), we do it by hand. */
1445 if (!file
) return ret
;
1446 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1447 if (prot
& PROT_WRITE
)
1449 /* We cannot fake shared write mappings */
1451 if (flags
& MAP_SHARED
) return ret
;
1454 if (!(flags
& MAP_PRIVATE
)) return ret
;
1457 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1458 /* Reserve the memory with an anonymous mmap */
1459 ret
= FILE_dommap( NULL
, start
, size_high
, size_low
, 0, 0,
1460 PROT_READ
| PROT_WRITE
, flags
);
1461 if (ret
== (LPVOID
)-1) return ret
;
1462 /* Now read in the file */
1463 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1465 FILE_munmap( ret
, size_high
, size_low
);
1468 read( fd
, ret
, size_low
);
1469 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1470 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1475 /***********************************************************************
1478 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1481 FIXME(file
, "offsets larger than 4Gb not supported\n");
1482 return munmap( start
, size_low
);
1486 /***********************************************************************
1487 * GetFileType (KERNEL32.222)
1489 DWORD WINAPI
GetFileType( HFILE32 hFile
)
1491 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1492 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1493 FILE_ReleaseFile( file
);
1498 /**************************************************************************
1499 * MoveFileEx32A (KERNEL32.???)
1501 BOOL32 WINAPI
MoveFileEx32A( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1503 DOS_FULL_NAME full_name1
, full_name2
;
1504 int mode
=0; /* mode == 1: use copy */
1506 TRACE(file
, "(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1508 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1509 if (fn2
) { /* !fn2 means delete fn1 */
1510 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1511 /* Source name and target path are valid */
1512 if ( full_name1
.drive
!= full_name2
.drive
)
1513 /* use copy, if allowed */
1514 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1515 /* FIXME: Use right error code */
1516 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
1520 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1521 /* target exists, check if we may overwrite */
1522 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1523 /* FIXME: Use right error code */
1524 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1528 else /* fn2 == NULL means delete source */
1529 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1530 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1531 WARN(file
, "Illegal flag\n");
1532 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1536 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1537 Perhaps we should queue these command and execute it
1538 when exiting... What about using on_exit(2)
1540 FIXME(file
, "Please delete file '%s' when Wine has finished\n",
1541 full_name1
.long_name
);
1544 else if (unlink( full_name1
.long_name
) == -1)
1549 else return TRUE
; /* successfully deleted */
1551 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1552 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1553 Perhaps we should queue these command and execute it
1554 when exiting... What about using on_exit(2)
1556 FIXME(file
,"Please move existing file '%s' to file '%s'"
1557 "when Wine has finished\n",
1558 full_name1
.long_name
, full_name2
.long_name
);
1562 if (!mode
) /* move the file */
1563 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1569 else /* copy File */
1570 return CopyFile32A(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1574 /**************************************************************************
1575 * MoveFileEx32W (KERNEL32.???)
1577 BOOL32 WINAPI
MoveFileEx32W( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1579 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1580 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1581 BOOL32 res
= MoveFileEx32A( afn1
, afn2
, flag
);
1582 HeapFree( GetProcessHeap(), 0, afn1
);
1583 HeapFree( GetProcessHeap(), 0, afn2
);
1588 /**************************************************************************
1589 * MoveFile32A (KERNEL32.387)
1591 * Move file or directory
1593 BOOL32 WINAPI
MoveFile32A( LPCSTR fn1
, LPCSTR fn2
)
1595 DOS_FULL_NAME full_name1
, full_name2
;
1598 TRACE(file
, "(%s,%s)\n", fn1
, fn2
);
1600 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1601 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1602 /* The new name must not already exist */
1604 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1606 if (full_name1
.drive
== full_name2
.drive
) /* move */
1607 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1614 if (stat( full_name1
.long_name
, &fstat
))
1616 WARN(file
, "Invalid source file %s\n",
1617 full_name1
.long_name
);
1621 if (S_ISDIR(fstat
.st_mode
)) {
1622 /* No Move for directories across file systems */
1623 /* FIXME: Use right error code */
1624 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1629 return CopyFile32A(fn1
, fn2
, TRUE
); /*fail, if exist */
1634 /**************************************************************************
1635 * MoveFile32W (KERNEL32.390)
1637 BOOL32 WINAPI
MoveFile32W( LPCWSTR fn1
, LPCWSTR fn2
)
1639 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1640 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1641 BOOL32 res
= MoveFile32A( afn1
, afn2
);
1642 HeapFree( GetProcessHeap(), 0, afn1
);
1643 HeapFree( GetProcessHeap(), 0, afn2
);
1648 /**************************************************************************
1649 * CopyFile32A (KERNEL32.36)
1651 BOOL32 WINAPI
CopyFile32A( LPCSTR source
, LPCSTR dest
, BOOL32 fail_if_exists
)
1654 BY_HANDLE_FILE_INFORMATION info
;
1660 if ((h1
= _lopen32( source
, OF_READ
)) == HFILE_ERROR32
) return FALSE
;
1661 if (!GetFileInformationByHandle( h1
, &info
))
1666 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1667 if ((h2
= FILE_Create( dest
, mode
, fail_if_exists
)) == HFILE_ERROR32
)
1672 while ((count
= _lread32( h1
, buffer
, sizeof(buffer
) )) > 0)
1677 INT32 res
= _lwrite32( h2
, p
, count
);
1678 if (res
<= 0) goto done
;
1691 /**************************************************************************
1692 * CopyFile32W (KERNEL32.37)
1694 BOOL32 WINAPI
CopyFile32W( LPCWSTR source
, LPCWSTR dest
, BOOL32 fail_if_exists
)
1696 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1697 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1698 BOOL32 ret
= CopyFile32A( sourceA
, destA
, fail_if_exists
);
1699 HeapFree( GetProcessHeap(), 0, sourceA
);
1700 HeapFree( GetProcessHeap(), 0, destA
);
1705 /***********************************************************************
1706 * SetFileTime (KERNEL32.650)
1708 BOOL32 WINAPI
SetFileTime( HFILE32 hFile
,
1709 const FILETIME
*lpCreationTime
,
1710 const FILETIME
*lpLastAccessTime
,
1711 const FILETIME
*lpLastWriteTime
)
1713 FILE_OBJECT
*file
= FILE_GetFile(hFile
);
1714 struct utimbuf utimbuf
;
1716 if (!file
) return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */
1717 TRACE(file
,"('%s',%p,%p,%p)\n",
1723 if (lpLastAccessTime
)
1724 utimbuf
.actime
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1726 utimbuf
.actime
= 0; /* FIXME */
1727 if (lpLastWriteTime
)
1728 utimbuf
.modtime
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1730 utimbuf
.modtime
= 0; /* FIXME */
1731 if (-1==utime(file
->unix_name
,&utimbuf
))
1733 MSG("Couldn't set the time for file '%s'. Insufficient permissions !?\n", file
->unix_name
);
1734 FILE_ReleaseFile( file
);
1738 FILE_ReleaseFile( file
);
1742 /* Locks need to be mirrored because unix file locking is based
1743 * on the pid. Inside of wine there can be multiple WINE processes
1744 * that share the same unix pid.
1745 * Read's and writes should check these locks also - not sure
1746 * how critical that is at this point (FIXME).
1749 static BOOL32
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
1751 DOS_FILE_LOCK
*curr
;
1754 processId
= GetCurrentProcessId();
1756 /* check if lock overlaps a current lock for the same file */
1757 for (curr
= locks
; curr
; curr
= curr
->next
) {
1758 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
1759 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
1760 return TRUE
;/* region is identic */
1761 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
1762 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
1763 /* region overlaps */
1769 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
1770 curr
->processId
= GetCurrentProcessId();
1771 curr
->base
= f
->l_start
;
1772 curr
->len
= f
->l_len
;
1773 curr
->unix_name
= HEAP_strdupA( SystemHeap
, 0, file
->unix_name
);
1775 curr
->dos_file
= file
;
1780 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
1783 DOS_FILE_LOCK
**curr
;
1786 processId
= GetCurrentProcessId();
1789 if ((*curr
)->dos_file
== file
) {
1791 *curr
= (*curr
)->next
;
1792 HeapFree( SystemHeap
, 0, rem
->unix_name
);
1793 HeapFree( SystemHeap
, 0, rem
);
1796 curr
= &(*curr
)->next
;
1800 static BOOL32
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
1803 DOS_FILE_LOCK
**curr
;
1806 processId
= GetCurrentProcessId();
1807 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
1808 if ((*curr
)->processId
== processId
&&
1809 (*curr
)->dos_file
== file
&&
1810 (*curr
)->base
== f
->l_start
&&
1811 (*curr
)->len
== f
->l_len
) {
1812 /* this is the same lock */
1814 *curr
= (*curr
)->next
;
1815 HeapFree( SystemHeap
, 0, rem
->unix_name
);
1816 HeapFree( SystemHeap
, 0, rem
);
1820 /* no matching lock found */
1825 /**************************************************************************
1826 * LockFile (KERNEL32.511)
1828 BOOL32 WINAPI
LockFile(
1829 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
1830 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
1835 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1836 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
1837 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
1839 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
1840 FIXME(file
, "Unimplemented bytes > 32bits\n");
1844 f
.l_start
= dwFileOffsetLow
;
1845 f
.l_len
= nNumberOfBytesToLockLow
;
1846 f
.l_whence
= SEEK_SET
;
1850 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
1852 /* shadow locks internally */
1853 if (!DOS_AddLock(file
, &f
)) {
1854 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
1858 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1859 #ifdef USE_UNIX_LOCKS
1860 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
1861 if (errno
== EACCES
|| errno
== EAGAIN
) {
1862 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
1867 /* remove our internal copy of the lock */
1868 DOS_RemoveLock(file
, &f
);
1876 /**************************************************************************
1877 * UnlockFile (KERNEL32.703)
1879 BOOL32 WINAPI
UnlockFile(
1880 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
1881 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
1886 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1887 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
1888 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
1890 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
1891 WARN(file
, "Unimplemented bytes > 32bits\n");
1895 f
.l_start
= dwFileOffsetLow
;
1896 f
.l_len
= nNumberOfBytesToUnlockLow
;
1897 f
.l_whence
= SEEK_SET
;
1901 if (!(file
= FILE_GetFile(hFile
))) return FALSE
;
1903 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
1905 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
1906 #ifdef USE_UNIX_LOCKS
1907 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
1915 /**************************************************************************
1916 * GetFileAttributesEx32A [KERNEL32.874]
1918 BOOL32 WINAPI
GetFileAttributesEx32A(
1919 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
1920 LPVOID lpFileInformation
)
1922 DOS_FULL_NAME full_name
;
1923 BY_HANDLE_FILE_INFORMATION info
;
1925 if (lpFileName
== NULL
) return FALSE
;
1926 if (lpFileInformation
== NULL
) return FALSE
;
1928 if (fInfoLevelId
== GetFileExInfoStandard
) {
1929 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
1930 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
1931 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
1932 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
1934 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
1935 lpFad
->ftCreationTime
= info
.ftCreationTime
;
1936 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
1937 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
1938 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
1939 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
1942 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
1950 /**************************************************************************
1951 * GetFileAttributesEx32W [KERNEL32.875]
1953 BOOL32 WINAPI
GetFileAttributesEx32W(
1954 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
1955 LPVOID lpFileInformation
)
1957 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
1959 GetFileAttributesEx32A( nameA
, fInfoLevelId
, lpFileInformation
);
1960 HeapFree( GetProcessHeap(), 0, nameA
);