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>
42 #include "server/request.h"
45 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
46 #define MAP_ANON MAP_ANONYMOUS
49 static void FILE_Destroy( K32OBJ
*obj
);
51 const K32OBJ_OPS FILE_Ops
=
53 FILE_Destroy
/* destroy */
56 struct DOS_FILE_LOCK
{
57 struct DOS_FILE_LOCK
* next
;
61 FILE_OBJECT
* dos_file
;
65 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
67 static DOS_FILE_LOCK
*locks
= NULL
;
68 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
72 /***********************************************************************
77 static void FILE_Destroy( K32OBJ
*ptr
)
79 FILE_OBJECT
*file
= (FILE_OBJECT
*)ptr
;
80 assert( ptr
->type
== K32OBJ_FILE
);
82 DOS_RemoveFileLocks(file
);
84 if (file
->unix_name
) HeapFree( SystemHeap
, 0, file
->unix_name
);
85 ptr
->type
= K32OBJ_UNKNOWN
;
86 HeapFree( SystemHeap
, 0, file
);
90 /***********************************************************************
93 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
94 * be called to release the file.
96 FILE_OBJECT
*FILE_GetFile( HFILE32 handle
, DWORD access
, int *server_handle
)
98 return (FILE_OBJECT
*)HANDLE_GetObjPtr( PROCESS_Current(), handle
,
104 /***********************************************************************
107 * Release a DOS file obtained with FILE_GetFile.
109 void FILE_ReleaseFile( FILE_OBJECT
*file
)
111 K32OBJ_DecCount( &file
->header
);
115 /***********************************************************************
118 * Convert OF_* mode into flags for CreateFile.
120 static void FILE_ConvertOFMode( INT32 mode
, DWORD
*access
, DWORD
*sharing
)
124 case OF_READ
: *access
= GENERIC_READ
; break;
125 case OF_WRITE
: *access
= GENERIC_WRITE
; break;
126 case OF_READWRITE
: *access
= GENERIC_READ
| GENERIC_WRITE
; break;
127 default: *access
= 0; break;
131 case OF_SHARE_EXCLUSIVE
: *sharing
= 0; break;
132 case OF_SHARE_DENY_WRITE
: *sharing
= FILE_SHARE_READ
; break;
133 case OF_SHARE_DENY_READ
: *sharing
= FILE_SHARE_WRITE
; break;
134 case OF_SHARE_DENY_NONE
:
135 case OF_SHARE_COMPAT
:
136 default: *sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
142 /***********************************************************************
146 * oldmode[I] mode how file was first opened
147 * mode[I] mode how the file should get opened
152 * Look what we have to do with the given SHARE modes
154 * Ralph Brown's interrupt list gives following explication, I guess
155 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
157 * FIXME: Validate this function
158 ========from Ralph Brown's list =========
160 Values of DOS file sharing behavior:
161 | Second and subsequent Opens
162 First |Compat Deny Deny Deny Deny
163 Open | All Write Read None
164 |R W RW R W RW R W RW R W RW R W RW
165 - - - - -| - - - - - - - - - - - - - - - - -
166 Compat R |Y Y Y N N N 1 N N N N N 1 N N
167 W |Y Y Y N N N N N N N N N N N N
168 RW|Y Y Y N N N N N N N N N N N N
170 Deny R |C C C N N N N N N N N N N N N
171 All W |C C C N N N N N N N N N N N N
172 RW|C C C N N N N N N N N N N N N
174 Deny R |2 C C N N N Y N N N N N Y N N
175 Write W |C C C N N N N N N Y N N Y N N
176 RW|C C C N N N N N N N N N Y N N
178 Deny R |C C C N N N N Y N N N N N Y N
179 Read W |C C C N N N N N N N Y N N Y N
180 RW|C C C N N N N N N N N N N Y N
182 Deny R |2 C C N N N Y Y Y N N N Y Y Y
183 None W |C C C N N N N N N Y Y Y Y Y Y
184 RW|C C C N N N N N N N N N Y Y Y
185 Legend: Y = open succeeds, N = open fails with error code 05h
186 C = open fails, INT 24 generated
187 1 = open succeeds if file read-only, else fails with error code
188 2 = open succeeds if file read-only, else fails with INT 24
189 ========end of description from Ralph Brown's List =====
190 For every "Y" in the table we return FALSE
191 For every "N" we set the DOS_ERROR and return TRUE
192 For all other cases we barf,set the DOS_ERROR and return TRUE
195 static BOOL32
FILE_ShareDeny( int mode
, int oldmode
)
197 int oldsharemode
= oldmode
& 0x70;
198 int sharemode
= mode
& 0x70;
199 int oldopenmode
= oldmode
& 3;
200 int openmode
= mode
& 3;
202 switch (oldsharemode
)
204 case OF_SHARE_COMPAT
:
205 if (sharemode
== OF_SHARE_COMPAT
) return FALSE
;
206 if (openmode
== OF_READ
) goto test_ro_err05
;
208 case OF_SHARE_EXCLUSIVE
:
209 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
211 case OF_SHARE_DENY_WRITE
:
212 if (openmode
!= OF_READ
)
214 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
219 case OF_SHARE_COMPAT
:
220 if (oldopenmode
== OF_READ
) goto test_ro_int24
;
222 case OF_SHARE_DENY_NONE
:
224 case OF_SHARE_DENY_WRITE
:
225 if (oldopenmode
== OF_READ
) return FALSE
;
226 case OF_SHARE_DENY_READ
:
227 if (oldopenmode
== OF_WRITE
) return FALSE
;
228 case OF_SHARE_EXCLUSIVE
:
233 case OF_SHARE_DENY_READ
:
234 if (openmode
!= OF_WRITE
)
236 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
241 case OF_SHARE_COMPAT
:
243 case OF_SHARE_DENY_NONE
:
245 case OF_SHARE_DENY_WRITE
:
246 if (oldopenmode
== OF_READ
) return FALSE
;
247 case OF_SHARE_DENY_READ
:
248 if (oldopenmode
== OF_WRITE
) return FALSE
;
249 case OF_SHARE_EXCLUSIVE
:
254 case OF_SHARE_DENY_NONE
:
257 case OF_SHARE_COMPAT
:
259 case OF_SHARE_DENY_NONE
:
261 case OF_SHARE_DENY_WRITE
:
262 if (oldopenmode
== OF_READ
) return FALSE
;
263 case OF_SHARE_DENY_READ
:
264 if (oldopenmode
== OF_WRITE
) return FALSE
;
265 case OF_SHARE_EXCLUSIVE
:
270 ERR(file
,"unknown mode\n");
272 ERR(file
,"shouldn't happen\n");
273 ERR(file
,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
277 if (oldmode
== OF_READ
)
281 FIXME(file
,"generate INT24 missing\n");
282 /* Is this the right error? */
283 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
287 if (oldmode
== OF_READ
)
291 TRACE(file
,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode
,mode
);
292 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
298 /***********************************************************************
301 * Set the DOS error code from errno.
303 void FILE_SetDosError(void)
305 int save_errno
= errno
; /* errno gets overwritten by printf */
307 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
311 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
314 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
317 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
322 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
325 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
328 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
331 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
335 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
338 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
342 DOS_ERROR( ER_SeekError
, EC_NotFound
, SA_Ignore
, EL_Disk
);
345 DOS_ERROR( ERROR_DIR_NOT_EMPTY
, EC_Exists
, SA_Ignore
, EL_Disk
);
348 perror( "int21: unknown errno" );
349 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
356 /***********************************************************************
359 * Duplicate a Unix handle into a task handle.
361 HFILE32
FILE_DupUnixHandle( int fd
, DWORD access
)
365 struct create_file_request req
;
366 struct create_file_reply reply
;
368 if ((unix_handle
= dup(fd
)) == -1)
371 return INVALID_HANDLE_VALUE32
;
375 req
.sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
379 CLIENT_SendRequest( REQ_CREATE_FILE
, unix_handle
, 1,
381 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
382 if (reply
.handle
== -1) return INVALID_HANDLE_VALUE32
;
384 if (!(file
= HeapAlloc( SystemHeap
, 0, sizeof(FILE_OBJECT
) )))
386 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
387 CLIENT_CloseHandle( reply
.handle
);
388 return (HFILE32
)NULL
;
390 file
->header
.type
= K32OBJ_FILE
;
391 file
->header
.refcount
= 0;
392 file
->unix_name
= NULL
;
393 return HANDLE_Alloc( PROCESS_Current(), &file
->header
, req
.access
,
394 req
.inherit
, reply
.handle
);
398 /***********************************************************************
401 * Implementation of CreateFile. Takes a Unix path name.
403 HFILE32
FILE_CreateFile( LPCSTR filename
, DWORD access
, DWORD sharing
,
404 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
405 DWORD attributes
, HANDLE32
template )
408 struct create_file_request req
;
409 struct create_file_reply reply
;
412 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
413 req
.sharing
= sharing
;
414 req
.create
= creation
;
415 req
.attrs
= attributes
;
416 CLIENT_SendRequest( REQ_CREATE_FILE
, -1, 2,
418 filename
, strlen(filename
) + 1 );
419 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
421 /* If write access failed, retry without GENERIC_WRITE */
423 if ((reply
.handle
== -1) && !Options
.failReadOnly
&&
424 (access
& GENERIC_WRITE
) && (GetLastError() == ERROR_ACCESS_DENIED
))
426 req
.access
&= ~GENERIC_WRITE
;
427 CLIENT_SendRequest( REQ_CREATE_FILE
, -1, 2,
429 filename
, strlen(filename
) + 1 );
430 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
432 if (reply
.handle
== -1) return INVALID_HANDLE_VALUE32
;
434 /* Now build the FILE_OBJECT */
436 if (!(file
= HeapAlloc( SystemHeap
, 0, sizeof(FILE_OBJECT
) )))
438 SetLastError( ERROR_OUTOFMEMORY
);
439 CLIENT_CloseHandle( reply
.handle
);
440 return (HFILE32
)INVALID_HANDLE_VALUE32
;
442 file
->header
.type
= K32OBJ_FILE
;
443 file
->header
.refcount
= 0;
444 file
->unix_name
= HEAP_strdupA( SystemHeap
, 0, filename
);
445 return HANDLE_Alloc( PROCESS_Current(), &file
->header
, req
.access
,
446 req
.inherit
, reply
.handle
);
450 /*************************************************************************
451 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
453 * Creates or opens an object, and returns a handle that can be used to
454 * access that object.
458 * filename [I] pointer to filename to be accessed
459 * access [I] access mode requested
460 * sharing [I] share mode
461 * sa [I] pointer to security attributes
462 * creation [I] how to create the file
463 * attributes [I] attributes for newly created file
464 * template [I] handle to file with extended attributes to copy
467 * Success: Open handle to specified file
468 * Failure: INVALID_HANDLE_VALUE
471 * Should call SetLastError() on failure.
475 * Doesn't support character devices, pipes, template files, or a
476 * lot of the 'attributes' flags yet.
478 HFILE32 WINAPI
CreateFile32A( LPCSTR filename
, DWORD access
, DWORD sharing
,
479 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
480 DWORD attributes
, HANDLE32
template )
482 DOS_FULL_NAME full_name
;
483 HANDLE32 to_dup
= HFILE_ERROR32
;
487 SetLastError( ERROR_INVALID_PARAMETER
);
488 return HFILE_ERROR32
;
491 /* If the name starts with '\\?\', ignore the first 4 chars. */
492 if (!strncmp(filename
, "\\\\?\\", 4))
495 if (!strncmp(filename
, "UNC\\", 4))
497 FIXME( file
, "UNC name (%s) not supported.\n", filename
);
498 SetLastError( ERROR_PATH_NOT_FOUND
);
499 return HFILE_ERROR32
;
503 if (!strncmp(filename
, "\\\\.\\", 4)) return DEVICE_Open( filename
+4 );
505 /* If the name still starts with '\\', it's a UNC name. */
506 if (!strncmp(filename
, "\\\\", 2))
508 FIXME( file
, "UNC name (%s) not supported.\n", filename
);
509 SetLastError( ERROR_PATH_NOT_FOUND
);
510 return HFILE_ERROR32
;
513 /* If the name is either CONIN$ or CONOUT$, give them duplicated stdin
514 * or stdout, respectively. The lower case version is also allowed. Most likely
515 * this should be a case ignore string compare.
517 if(!strcasecmp(filename
, "CONIN$"))
518 to_dup
= GetStdHandle( STD_INPUT_HANDLE
);
519 else if(!strcasecmp(filename
, "CONOUT$"))
520 to_dup
= GetStdHandle( STD_OUTPUT_HANDLE
);
522 if(to_dup
!= HFILE_ERROR32
)
525 if (!DuplicateHandle( GetCurrentProcess(), to_dup
, GetCurrentProcess(),
526 &handle
, access
, FALSE
, 0 ))
527 handle
= HFILE_ERROR32
;
531 if (DOSFS_GetDevice( filename
))
535 TRACE(file
, "opening device '%s'\n", filename
);
537 if (HFILE_ERROR32
!=(ret
=DOSFS_OpenDevice( filename
, access
)))
540 /* Do not silence this please. It is a critical error. -MM */
541 ERR(file
, "Couldn't open device '%s'!\n",filename
);
542 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
543 return HFILE_ERROR32
;
546 /* check for filename, don't check for last entry if creating */
547 if (!DOSFS_GetFullName( filename
,
548 (creation
== OPEN_EXISTING
) || (creation
== TRUNCATE_EXISTING
), &full_name
))
549 return HFILE_ERROR32
;
551 return FILE_CreateFile( full_name
.long_name
, access
, sharing
,
552 sa
, creation
, attributes
, template );
557 /*************************************************************************
558 * CreateFile32W (KERNEL32.48)
560 HFILE32 WINAPI
CreateFile32W( LPCWSTR filename
, DWORD access
, DWORD sharing
,
561 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
562 DWORD attributes
, HANDLE32
template)
564 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
565 HFILE32 res
= CreateFile32A( afn
, access
, sharing
, sa
, creation
, attributes
, template );
566 HeapFree( GetProcessHeap(), 0, afn
);
571 /***********************************************************************
574 * Fill a file information from a struct stat.
576 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
578 if (S_ISDIR(st
->st_mode
))
579 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
581 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
582 if (!(st
->st_mode
& S_IWUSR
))
583 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
585 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
586 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
587 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
589 info
->dwVolumeSerialNumber
= 0; /* FIXME */
590 info
->nFileSizeHigh
= 0;
591 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
592 info
->nNumberOfLinks
= st
->st_nlink
;
593 info
->nFileIndexHigh
= 0;
594 info
->nFileIndexLow
= st
->st_ino
;
598 /***********************************************************************
601 * Stat a Unix path name. Return TRUE if OK.
603 BOOL32
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
607 if (!unixName
|| !info
) return FALSE
;
609 if (stat( unixName
, &st
) == -1)
614 FILE_FillInfo( &st
, info
);
619 /***********************************************************************
620 * GetFileInformationByHandle (KERNEL32.219)
622 DWORD WINAPI
GetFileInformationByHandle( HFILE32 hFile
,
623 BY_HANDLE_FILE_INFORMATION
*info
)
625 struct get_file_info_request req
;
626 struct get_file_info_reply reply
;
629 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
630 K32OBJ_FILE
, 0 )) == -1)
632 CLIENT_SendRequest( REQ_GET_FILE_INFO
, -1, 1, &req
, sizeof(req
) );
633 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
))
635 DOSFS_UnixTimeToFileTime( reply
.write_time
, &info
->ftCreationTime
, 0 );
636 DOSFS_UnixTimeToFileTime( reply
.write_time
, &info
->ftLastWriteTime
, 0 );
637 DOSFS_UnixTimeToFileTime( reply
.access_time
, &info
->ftLastAccessTime
, 0 );
638 info
->dwFileAttributes
= reply
.attr
;
639 info
->dwVolumeSerialNumber
= reply
.serial
;
640 info
->nFileSizeHigh
= reply
.size_high
;
641 info
->nFileSizeLow
= reply
.size_low
;
642 info
->nNumberOfLinks
= reply
.links
;
643 info
->nFileIndexHigh
= reply
.index_high
;
644 info
->nFileIndexLow
= reply
.index_low
;
649 /**************************************************************************
650 * GetFileAttributes16 (KERNEL.420)
652 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
654 return GetFileAttributes32A( name
);
658 /**************************************************************************
659 * GetFileAttributes32A (KERNEL32.217)
661 DWORD WINAPI
GetFileAttributes32A( LPCSTR name
)
663 DOS_FULL_NAME full_name
;
664 BY_HANDLE_FILE_INFORMATION info
;
666 if (name
== NULL
|| *name
=='\0') return -1;
668 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
669 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
670 return info
.dwFileAttributes
;
674 /**************************************************************************
675 * GetFileAttributes32W (KERNEL32.218)
677 DWORD WINAPI
GetFileAttributes32W( LPCWSTR name
)
679 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
680 DWORD res
= GetFileAttributes32A( nameA
);
681 HeapFree( GetProcessHeap(), 0, nameA
);
686 /***********************************************************************
687 * GetFileSize (KERNEL32.220)
689 DWORD WINAPI
GetFileSize( HFILE32 hFile
, LPDWORD filesizehigh
)
691 BY_HANDLE_FILE_INFORMATION info
;
692 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
693 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
694 return info
.nFileSizeLow
;
698 /***********************************************************************
699 * GetFileTime (KERNEL32.221)
701 BOOL32 WINAPI
GetFileTime( HFILE32 hFile
, FILETIME
*lpCreationTime
,
702 FILETIME
*lpLastAccessTime
,
703 FILETIME
*lpLastWriteTime
)
705 BY_HANDLE_FILE_INFORMATION info
;
706 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
707 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
708 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
709 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
713 /***********************************************************************
714 * CompareFileTime (KERNEL32.28)
716 INT32 WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
718 if (!x
|| !y
) return -1;
720 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
722 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
724 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
726 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
732 /***********************************************************************
735 * dup2() function for DOS handles.
737 HFILE32
FILE_Dup2( HFILE32 hFile1
, HFILE32 hFile2
)
741 TRACE(file
, "FILE_Dup2 for handle %d\n", hFile1
);
742 /* FIXME: should use DuplicateHandle */
743 if (!(file
= FILE_GetFile( hFile1
, 0, NULL
))) return HFILE_ERROR32
;
744 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2
, &file
->header
, 0 ))
745 hFile2
= HFILE_ERROR32
;
746 FILE_ReleaseFile( file
);
751 /***********************************************************************
752 * GetTempFileName16 (KERNEL.97)
754 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
759 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
760 drive
|= DRIVE_GetCurrentDrive() + 'A';
762 if ((drive
& TF_FORCEDRIVE
) &&
763 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
765 drive
&= ~TF_FORCEDRIVE
;
766 WARN(file
, "invalid drive %d specified\n", drive
);
769 if (drive
& TF_FORCEDRIVE
)
770 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
772 GetTempPath32A( 132, temppath
);
773 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
777 /***********************************************************************
778 * GetTempFileName32A (KERNEL32.290)
780 UINT32 WINAPI
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
783 static UINT32 unique_temp
;
784 DOS_FULL_NAME full_name
;
789 if ( !path
|| !prefix
|| !buffer
) return 0;
791 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
792 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
794 strcpy( buffer
, path
);
795 p
= buffer
+ strlen(buffer
);
797 /* add a \, if there isn't one and path is more than just the drive letter ... */
798 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
799 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
802 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
803 sprintf( p
, "%04x.tmp", num
);
805 /* Now try to create it */
811 HFILE32 handle
= CreateFile32A( buffer
, GENERIC_WRITE
, 0, NULL
,
812 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, -1 );
813 if (handle
!= INVALID_HANDLE_VALUE32
)
814 { /* We created it */
815 TRACE(file
, "created %s\n",
817 CloseHandle( handle
);
820 if (DOS_ExtendedError
!= ER_FileExists
)
821 break; /* No need to go on */
823 sprintf( p
, "%04x.tmp", num
);
824 } while (num
!= (unique
& 0xffff));
827 /* Get the full path name */
829 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
831 /* Check if we have write access in the directory */
832 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
833 if (access( full_name
.long_name
, W_OK
) == -1)
834 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
837 TRACE(file
, "returning %s\n", buffer
);
838 return unique
? unique
: num
;
842 /***********************************************************************
843 * GetTempFileName32W (KERNEL32.291)
845 UINT32 WINAPI
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
853 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
854 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
855 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
856 lstrcpyAtoW( buffer
, buffera
);
857 HeapFree( GetProcessHeap(), 0, patha
);
858 HeapFree( GetProcessHeap(), 0, prefixa
);
863 /***********************************************************************
866 * Implementation of OpenFile16() and OpenFile32().
868 static HFILE32
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
,
873 WORD filedatetime
[2];
874 DOS_FULL_NAME full_name
;
875 DWORD access
, sharing
;
878 if (!ofs
) return HFILE_ERROR32
;
880 ofs
->cBytes
= sizeof(OFSTRUCT
);
882 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
885 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
886 return HFILE_ERROR32
;
889 TRACE(file
, "%s %04x\n", name
, mode
);
891 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
892 Are there any cases where getting the path here is wrong?
893 Uwe Bonnes 1997 Apr 2 */
894 if (!GetFullPathName32A( name
, sizeof(ofs
->szPathName
),
895 ofs
->szPathName
, NULL
)) goto error
;
896 FILE_ConvertOFMode( mode
, &access
, &sharing
);
898 /* OF_PARSE simply fills the structure */
902 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
904 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
905 name
, ofs
->szPathName
);
909 /* OF_CREATE is completely different from all other options, so
912 if (mode
& OF_CREATE
)
914 if ((hFileRet
= CreateFile32A( name
, GENERIC_READ
| GENERIC_WRITE
,
915 sharing
, NULL
, CREATE_ALWAYS
,
916 FILE_ATTRIBUTE_NORMAL
, -1 ))== INVALID_HANDLE_VALUE32
)
921 /* If OF_SEARCH is set, ignore the given path */
923 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
925 /* First try the file name as is */
926 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
927 /* Now remove the path */
928 if (name
[0] && (name
[1] == ':')) name
+= 2;
929 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
930 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
931 if (!name
[0]) goto not_found
;
934 /* Now look for the file */
936 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
939 TRACE(file
, "found %s = %s\n",
940 full_name
.long_name
, full_name
.short_name
);
941 lstrcpyn32A( ofs
->szPathName
, full_name
.short_name
,
942 sizeof(ofs
->szPathName
) );
944 if (mode
& OF_SHARE_EXCLUSIVE
)
945 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
946 on the file <tempdir>/_ins0432._mp to determine how
947 far installation has proceeded.
948 _ins0432._mp is an executable and while running the
949 application expects the open with OF_SHARE_ to fail*/
951 As our loader closes the files after loading the executable,
952 we can't find the running executable with FILE_InUse.
953 Perhaps the loader should keep the file open.
954 Recheck against how Win handles that case */
956 char *last
= strrchr(full_name
.long_name
,'/');
958 last
= full_name
.long_name
- 1;
959 if (GetModuleHandle16(last
+1))
961 TRACE(file
,"Denying shared open for %s\n",full_name
.long_name
);
962 return HFILE_ERROR32
;
966 if (mode
& OF_DELETE
)
968 if (unlink( full_name
.long_name
) == -1) goto not_found
;
969 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
973 hFileRet
= FILE_CreateFile( full_name
.long_name
, access
, sharing
,
974 NULL
, OPEN_EXISTING
, 0, -1 );
975 if (hFileRet
== HFILE_ERROR32
) goto not_found
;
977 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
978 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
979 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
981 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
983 CloseHandle( hFileRet
);
984 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
985 /* FIXME: what error here? */
986 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
990 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
992 success
: /* We get here if the open was successful */
993 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
994 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
995 CloseHandle( hFileRet
);
998 not_found
: /* We get here if the file does not exist */
999 WARN(file
, "'%s' not found\n", name
);
1000 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1003 error
: /* We get here if there was an error opening the file */
1004 ofs
->nErrCode
= DOS_ExtendedError
;
1005 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
1006 name
,ofs
->nErrCode
);
1007 return HFILE_ERROR32
;
1011 /***********************************************************************
1012 * OpenFile16 (KERNEL.74)
1014 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
1016 TRACE(file
,"OpenFile16(%s,%i)\n", name
, mode
);
1017 return HFILE32_TO_HFILE16(FILE_DoOpenFile( name
, ofs
, mode
, FALSE
));
1021 /***********************************************************************
1022 * OpenFile32 (KERNEL32.396)
1024 HFILE32 WINAPI
OpenFile32( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
1026 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
1030 /***********************************************************************
1031 * _lclose16 (KERNEL.81)
1033 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1035 TRACE(file
, "handle %d\n", hFile
);
1036 return CloseHandle( HFILE16_TO_HFILE32( hFile
) ) ? 0 : HFILE_ERROR16
;
1040 /***********************************************************************
1041 * _lclose32 (KERNEL32.592)
1043 HFILE32 WINAPI
_lclose32( HFILE32 hFile
)
1045 TRACE(file
, "handle %d\n", hFile
);
1046 return CloseHandle( hFile
) ? 0 : HFILE_ERROR32
;
1050 /***********************************************************************
1051 * ReadFile (KERNEL32.428)
1053 BOOL32 WINAPI
ReadFile( HANDLE32 hFile
, LPVOID buffer
, DWORD bytesToRead
,
1054 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
1056 struct get_read_fd_request req
;
1057 int unix_handle
, result
;
1059 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToRead
);
1061 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
1062 if (!bytesToRead
) return TRUE
;
1064 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1065 K32OBJ_UNKNOWN
, GENERIC_READ
)) == -1)
1067 CLIENT_SendRequest( REQ_GET_READ_FD
, -1, 1, &req
, sizeof(req
) );
1068 CLIENT_WaitReply( NULL
, &unix_handle
, 0 );
1069 if (unix_handle
== -1) return FALSE
;
1070 while ((result
= read( unix_handle
, buffer
, bytesToRead
)) == -1)
1072 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1076 close( unix_handle
);
1077 if (result
== -1) return FALSE
;
1078 if (bytesRead
) *bytesRead
= result
;
1083 /***********************************************************************
1084 * WriteFile (KERNEL32.578)
1086 BOOL32 WINAPI
WriteFile( HANDLE32 hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
1087 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
1089 struct get_write_fd_request req
;
1090 int unix_handle
, result
;
1092 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToWrite
);
1094 if (bytesWritten
) *bytesWritten
= 0; /* Do this before anything else */
1095 if (!bytesToWrite
) return TRUE
;
1097 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1098 K32OBJ_UNKNOWN
, GENERIC_READ
)) == -1)
1100 CLIENT_SendRequest( REQ_GET_WRITE_FD
, -1, 1, &req
, sizeof(req
) );
1101 CLIENT_WaitReply( NULL
, &unix_handle
, 0 );
1102 if (unix_handle
== -1) return FALSE
;
1103 while ((result
= write( unix_handle
, buffer
, bytesToWrite
)) == -1)
1105 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1109 close( unix_handle
);
1110 if (result
== -1) return FALSE
;
1111 if (bytesWritten
) *bytesWritten
= result
;
1116 /***********************************************************************
1119 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1123 TRACE(file
, "%d %08lx %ld\n",
1124 hFile
, (DWORD
)buffer
, count
);
1126 /* Some programs pass a count larger than the allocated buffer */
1127 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1128 if (count
> maxlen
) count
= maxlen
;
1129 return _lread32(HFILE16_TO_HFILE32(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1133 /***********************************************************************
1136 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1138 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1142 /***********************************************************************
1143 * _lread32 (KERNEL32.596)
1145 UINT32 WINAPI
_lread32( HFILE32 handle
, LPVOID buffer
, UINT32 count
)
1148 if (!ReadFile( handle
, buffer
, count
, &result
, NULL
)) return -1;
1153 /***********************************************************************
1154 * _lread16 (KERNEL.82)
1156 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1158 return (UINT16
)_lread32(HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1162 /***********************************************************************
1163 * _lcreat16 (KERNEL.83)
1165 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1167 TRACE(file
, "%s %02x\n", path
, attr
);
1168 return (HFILE16
) HFILE32_TO_HFILE16(_lcreat32( path
, attr
));
1172 /***********************************************************************
1173 * _lcreat32 (KERNEL32.593)
1175 HFILE32 WINAPI
_lcreat32( LPCSTR path
, INT32 attr
)
1177 TRACE(file
, "%s %02x\n", path
, attr
);
1178 return CreateFile32A( path
, GENERIC_READ
| GENERIC_WRITE
,
1179 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1180 CREATE_ALWAYS
, attr
, -1 );
1184 /***********************************************************************
1185 * _lcreat_uniq (Not a Windows API)
1187 HFILE32
_lcreat_uniq( LPCSTR path
, INT32 attr
)
1189 TRACE(file
, "%s %02x\n", path
, attr
);
1190 return CreateFile32A( path
, GENERIC_READ
| GENERIC_WRITE
,
1191 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1192 CREATE_NEW
, attr
, -1 );
1196 /***********************************************************************
1197 * SetFilePointer (KERNEL32.492)
1199 DWORD WINAPI
SetFilePointer( HFILE32 hFile
, LONG distance
, LONG
*highword
,
1202 struct set_file_pointer_request req
;
1203 struct set_file_pointer_reply reply
;
1205 if (highword
&& *highword
)
1207 FIXME(file
, "64-bit offsets not supported yet\n");
1208 SetLastError( ERROR_INVALID_PARAMETER
);
1211 TRACE(file
, "handle %d offset %ld origin %ld\n",
1212 hFile
, distance
, method
);
1214 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1215 K32OBJ_FILE
, 0 )) == -1)
1218 req
.high
= highword
? *highword
: 0;
1219 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1220 req
.whence
= method
;
1221 CLIENT_SendRequest( REQ_SET_FILE_POINTER
, -1, 1, &req
, sizeof(req
) );
1222 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return 0xffffffff;
1224 if (highword
) *highword
= reply
.high
;
1229 /***********************************************************************
1230 * _llseek16 (KERNEL.84)
1233 * Seeking before the start of the file should be allowed for _llseek16,
1234 * but cause subsequent I/O operations to fail (cf. interrupt list)
1237 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1239 return SetFilePointer( HFILE16_TO_HFILE32(hFile
), lOffset
, NULL
, nOrigin
);
1243 /***********************************************************************
1244 * _llseek32 (KERNEL32.594)
1246 LONG WINAPI
_llseek32( HFILE32 hFile
, LONG lOffset
, INT32 nOrigin
)
1248 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1252 /***********************************************************************
1253 * _lopen16 (KERNEL.85)
1255 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1257 return HFILE32_TO_HFILE16(_lopen32( path
, mode
));
1261 /***********************************************************************
1262 * _lopen32 (KERNEL32.595)
1264 HFILE32 WINAPI
_lopen32( LPCSTR path
, INT32 mode
)
1266 DWORD access
, sharing
;
1268 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1269 FILE_ConvertOFMode( mode
, &access
, &sharing
);
1270 return CreateFile32A( path
, access
, sharing
, NULL
, OPEN_EXISTING
, 0, -1 );
1274 /***********************************************************************
1275 * _lwrite16 (KERNEL.86)
1277 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1279 return (UINT16
)_hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, (LONG
)count
);
1282 /***********************************************************************
1283 * _lwrite32 (KERNEL32.761)
1285 UINT32 WINAPI
_lwrite32( HFILE32 hFile
, LPCSTR buffer
, UINT32 count
)
1287 return (UINT32
)_hwrite32( hFile
, buffer
, (LONG
)count
);
1291 /***********************************************************************
1292 * _hread16 (KERNEL.349)
1294 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1296 return _lread32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1300 /***********************************************************************
1301 * _hread32 (KERNEL32.590)
1303 LONG WINAPI
_hread32( HFILE32 hFile
, LPVOID buffer
, LONG count
)
1305 return _lread32( hFile
, buffer
, count
);
1309 /***********************************************************************
1310 * _hwrite16 (KERNEL.350)
1312 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1314 return _hwrite32( HFILE16_TO_HFILE32(hFile
), buffer
, count
);
1318 /***********************************************************************
1319 * _hwrite32 (KERNEL32.591)
1321 * experimentation yields that _lwrite:
1322 * o truncates the file at the current position with
1324 * o returns 0 on a 0 length write
1325 * o works with console handles
1328 LONG WINAPI
_hwrite32( HFILE32 handle
, LPCSTR buffer
, LONG count
)
1332 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1336 /* Expand or truncate at current position */
1337 if (!SetEndOfFile( handle
)) return HFILE_ERROR32
;
1340 if (!WriteFile( handle
, buffer
, count
, &result
, NULL
))
1341 return HFILE_ERROR32
;
1346 /***********************************************************************
1347 * SetHandleCount16 (KERNEL.199)
1349 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1351 HGLOBAL16 hPDB
= GetCurrentPDB();
1352 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1353 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1355 TRACE(file
, "(%d)\n", count
);
1357 if (count
< 20) count
= 20; /* No point in going below 20 */
1358 else if (count
> 254) count
= 254;
1362 if (pdb
->nbFiles
> 20)
1364 memcpy( pdb
->fileHandles
, files
, 20 );
1365 GlobalFree16( pdb
->hFileHandles
);
1366 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1367 GlobalHandleToSel( hPDB
) );
1368 pdb
->hFileHandles
= 0;
1372 else /* More than 20, need a new file handles table */
1375 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1378 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1379 return pdb
->nbFiles
;
1381 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1383 if (count
> pdb
->nbFiles
)
1385 memcpy( newfiles
, files
, pdb
->nbFiles
);
1386 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1388 else memcpy( newfiles
, files
, count
);
1389 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1390 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1391 pdb
->hFileHandles
= newhandle
;
1392 pdb
->nbFiles
= count
;
1394 return pdb
->nbFiles
;
1398 /*************************************************************************
1399 * SetHandleCount32 (KERNEL32.494)
1401 UINT32 WINAPI
SetHandleCount32( UINT32 count
)
1403 return MIN( 256, count
);
1407 /***********************************************************************
1408 * FlushFileBuffers (KERNEL32.133)
1410 BOOL32 WINAPI
FlushFileBuffers( HFILE32 hFile
)
1412 struct flush_file_request req
;
1414 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1415 K32OBJ_FILE
, 0 )) == -1)
1417 CLIENT_SendRequest( REQ_FLUSH_FILE
, -1, 1, &req
, sizeof(req
) );
1418 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1422 /**************************************************************************
1423 * SetEndOfFile (KERNEL32.483)
1425 BOOL32 WINAPI
SetEndOfFile( HFILE32 hFile
)
1427 struct truncate_file_request req
;
1429 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1430 K32OBJ_FILE
, 0 )) == -1)
1432 CLIENT_SendRequest( REQ_TRUNCATE_FILE
, -1, 1, &req
, sizeof(req
) );
1433 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1437 /***********************************************************************
1438 * DeleteFile16 (KERNEL.146)
1440 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1442 return DeleteFile32A( path
);
1446 /***********************************************************************
1447 * DeleteFile32A (KERNEL32.71)
1449 BOOL32 WINAPI
DeleteFile32A( LPCSTR path
)
1451 DOS_FULL_NAME full_name
;
1453 TRACE(file
, "'%s'\n", path
);
1457 ERR(file
, "Empty path passed\n");
1460 if (DOSFS_GetDevice( path
))
1462 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1463 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1467 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1468 if (unlink( full_name
.long_name
) == -1)
1477 /***********************************************************************
1478 * DeleteFile32W (KERNEL32.72)
1480 BOOL32 WINAPI
DeleteFile32W( LPCWSTR path
)
1482 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1483 BOOL32 ret
= DeleteFile32A( xpath
);
1484 HeapFree( GetProcessHeap(), 0, xpath
);
1489 /***********************************************************************
1492 LPVOID
FILE_dommap( int unix_handle
, LPVOID start
,
1493 DWORD size_high
, DWORD size_low
,
1494 DWORD offset_high
, DWORD offset_low
,
1495 int prot
, int flags
)
1501 if (size_high
|| offset_high
)
1502 FIXME(file
, "offsets larger than 4Gb not supported\n");
1504 if (unix_handle
== -1)
1509 static int fdzero
= -1;
1513 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1515 perror( "/dev/zero: open" );
1520 #endif /* MAP_ANON */
1521 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1523 flags
&= ~MAP_SHARED
;
1526 flags
|= MAP_PRIVATE
;
1529 else fd
= unix_handle
;
1531 if ((ret
= mmap( start
, size_low
, prot
,
1532 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1535 /* mmap() failed; if this is because the file offset is not */
1536 /* page-aligned (EINVAL), or because the underlying filesystem */
1537 /* does not support mmap() (ENOEXEC), we do it by hand. */
1539 if (unix_handle
== -1) return ret
;
1540 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1541 if (prot
& PROT_WRITE
)
1543 /* We cannot fake shared write mappings */
1545 if (flags
& MAP_SHARED
) return ret
;
1548 if (!(flags
& MAP_PRIVATE
)) return ret
;
1551 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1552 /* Reserve the memory with an anonymous mmap */
1553 ret
= FILE_dommap( -1, start
, size_high
, size_low
, 0, 0,
1554 PROT_READ
| PROT_WRITE
, flags
);
1555 if (ret
== (LPVOID
)-1) return ret
;
1556 /* Now read in the file */
1557 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1559 FILE_munmap( ret
, size_high
, size_low
);
1562 read( fd
, ret
, size_low
);
1563 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1564 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1569 /***********************************************************************
1572 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1575 FIXME(file
, "offsets larger than 4Gb not supported\n");
1576 return munmap( start
, size_low
);
1580 /***********************************************************************
1581 * GetFileType (KERNEL32.222)
1583 DWORD WINAPI
GetFileType( HFILE32 hFile
)
1585 struct get_file_info_request req
;
1586 struct get_file_info_reply reply
;
1588 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1589 K32OBJ_UNKNOWN
, 0 )) == -1)
1590 return FILE_TYPE_UNKNOWN
;
1591 CLIENT_SendRequest( REQ_GET_FILE_INFO
, -1, 1, &req
, sizeof(req
) );
1592 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
))
1593 return FILE_TYPE_UNKNOWN
;
1598 /**************************************************************************
1599 * MoveFileEx32A (KERNEL32.???)
1601 BOOL32 WINAPI
MoveFileEx32A( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1603 DOS_FULL_NAME full_name1
, full_name2
;
1604 int mode
=0; /* mode == 1: use copy */
1606 TRACE(file
, "(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1608 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1609 if (fn2
) { /* !fn2 means delete fn1 */
1610 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1611 /* Source name and target path are valid */
1612 if ( full_name1
.drive
!= full_name2
.drive
)
1614 /* use copy, if allowed */
1615 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1616 /* FIXME: Use right error code */
1617 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
1622 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1623 /* target exists, check if we may overwrite */
1624 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1625 /* FIXME: Use right error code */
1626 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1630 else /* fn2 == NULL means delete source */
1631 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1632 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1633 WARN(file
, "Illegal flag\n");
1634 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1638 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1639 Perhaps we should queue these command and execute it
1640 when exiting... What about using on_exit(2)
1642 FIXME(file
, "Please delete file '%s' when Wine has finished\n",
1643 full_name1
.long_name
);
1646 else if (unlink( full_name1
.long_name
) == -1)
1651 else return TRUE
; /* successfully deleted */
1653 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1654 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1655 Perhaps we should queue these command and execute it
1656 when exiting... What about using on_exit(2)
1658 FIXME(file
,"Please move existing file '%s' to file '%s'"
1659 "when Wine has finished\n",
1660 full_name1
.long_name
, full_name2
.long_name
);
1664 if (!mode
) /* move the file */
1665 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1671 else /* copy File */
1672 return CopyFile32A(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1676 /**************************************************************************
1677 * MoveFileEx32W (KERNEL32.???)
1679 BOOL32 WINAPI
MoveFileEx32W( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1681 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1682 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1683 BOOL32 res
= MoveFileEx32A( afn1
, afn2
, flag
);
1684 HeapFree( GetProcessHeap(), 0, afn1
);
1685 HeapFree( GetProcessHeap(), 0, afn2
);
1690 /**************************************************************************
1691 * MoveFile32A (KERNEL32.387)
1693 * Move file or directory
1695 BOOL32 WINAPI
MoveFile32A( LPCSTR fn1
, LPCSTR fn2
)
1697 DOS_FULL_NAME full_name1
, full_name2
;
1700 TRACE(file
, "(%s,%s)\n", fn1
, fn2
);
1702 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1703 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1704 /* The new name must not already exist */
1706 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1708 if (full_name1
.drive
== full_name2
.drive
) /* move */
1709 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1716 if (stat( full_name1
.long_name
, &fstat
))
1718 WARN(file
, "Invalid source file %s\n",
1719 full_name1
.long_name
);
1723 if (S_ISDIR(fstat
.st_mode
)) {
1724 /* No Move for directories across file systems */
1725 /* FIXME: Use right error code */
1726 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
,
1731 return CopyFile32A(fn1
, fn2
, TRUE
); /*fail, if exist */
1736 /**************************************************************************
1737 * MoveFile32W (KERNEL32.390)
1739 BOOL32 WINAPI
MoveFile32W( LPCWSTR fn1
, LPCWSTR fn2
)
1741 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1742 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1743 BOOL32 res
= MoveFile32A( afn1
, afn2
);
1744 HeapFree( GetProcessHeap(), 0, afn1
);
1745 HeapFree( GetProcessHeap(), 0, afn2
);
1750 /**************************************************************************
1751 * CopyFile32A (KERNEL32.36)
1753 BOOL32 WINAPI
CopyFile32A( LPCSTR source
, LPCSTR dest
, BOOL32 fail_if_exists
)
1756 BY_HANDLE_FILE_INFORMATION info
;
1762 if ((h1
= _lopen32( source
, OF_READ
)) == HFILE_ERROR32
) return FALSE
;
1763 if (!GetFileInformationByHandle( h1
, &info
))
1768 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1769 if ((h2
= CreateFile32A( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1770 fail_if_exists
? CREATE_NEW
: CREATE_ALWAYS
,
1771 info
.dwFileAttributes
, h1
)) == HFILE_ERROR32
)
1776 while ((count
= _lread32( h1
, buffer
, sizeof(buffer
) )) > 0)
1781 INT32 res
= _lwrite32( h2
, p
, count
);
1782 if (res
<= 0) goto done
;
1795 /**************************************************************************
1796 * CopyFile32W (KERNEL32.37)
1798 BOOL32 WINAPI
CopyFile32W( LPCWSTR source
, LPCWSTR dest
, BOOL32 fail_if_exists
)
1800 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1801 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1802 BOOL32 ret
= CopyFile32A( sourceA
, destA
, fail_if_exists
);
1803 HeapFree( GetProcessHeap(), 0, sourceA
);
1804 HeapFree( GetProcessHeap(), 0, destA
);
1809 /**************************************************************************
1810 * CopyFileEx32A (KERNEL32.858)
1812 * This implementation ignores most of the extra parameters passed-in into
1813 * the "ex" version of the method and calls the CopyFile method.
1814 * It will have to be fixed eventually.
1816 BOOL32 WINAPI
CopyFileEx32A(LPCSTR sourceFilename
,
1817 LPCSTR destFilename
,
1818 LPPROGRESS_ROUTINE progressRoutine
,
1820 LPBOOL32 cancelFlagPointer
,
1823 BOOL32 failIfExists
= FALSE
;
1826 * Interpret the only flag that CopyFile can interpret.
1828 if ( (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0)
1830 failIfExists
= TRUE
;
1833 return CopyFile32A(sourceFilename
, destFilename
, failIfExists
);
1836 /**************************************************************************
1837 * CopyFileEx32W (KERNEL32.859)
1839 BOOL32 WINAPI
CopyFileEx32W(LPCWSTR sourceFilename
,
1840 LPCWSTR destFilename
,
1841 LPPROGRESS_ROUTINE progressRoutine
,
1843 LPBOOL32 cancelFlagPointer
,
1846 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename
);
1847 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename
);
1849 BOOL32 ret
= CopyFileEx32A(sourceA
,
1856 HeapFree( GetProcessHeap(), 0, sourceA
);
1857 HeapFree( GetProcessHeap(), 0, destA
);
1863 /***********************************************************************
1864 * SetFileTime (KERNEL32.650)
1866 BOOL32 WINAPI
SetFileTime( HFILE32 hFile
,
1867 const FILETIME
*lpCreationTime
,
1868 const FILETIME
*lpLastAccessTime
,
1869 const FILETIME
*lpLastWriteTime
)
1871 struct set_file_time_request req
;
1873 if ((req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), hFile
,
1874 K32OBJ_FILE
, GENERIC_WRITE
)) == -1)
1876 if (lpLastAccessTime
)
1877 req
.access_time
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1879 req
.access_time
= 0; /* FIXME */
1880 if (lpLastWriteTime
)
1881 req
.write_time
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1883 req
.write_time
= 0; /* FIXME */
1885 CLIENT_SendRequest( REQ_SET_FILE_TIME
, -1, 1, &req
, sizeof(req
) );
1886 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1889 /* Locks need to be mirrored because unix file locking is based
1890 * on the pid. Inside of wine there can be multiple WINE processes
1891 * that share the same unix pid.
1892 * Read's and writes should check these locks also - not sure
1893 * how critical that is at this point (FIXME).
1896 static BOOL32
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
1898 DOS_FILE_LOCK
*curr
;
1901 processId
= GetCurrentProcessId();
1903 /* check if lock overlaps a current lock for the same file */
1904 for (curr
= locks
; curr
; curr
= curr
->next
) {
1905 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
1906 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
1907 return TRUE
;/* region is identic */
1908 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
1909 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
1910 /* region overlaps */
1916 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
1917 curr
->processId
= GetCurrentProcessId();
1918 curr
->base
= f
->l_start
;
1919 curr
->len
= f
->l_len
;
1920 curr
->unix_name
= HEAP_strdupA( SystemHeap
, 0, file
->unix_name
);
1922 curr
->dos_file
= file
;
1927 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
1930 DOS_FILE_LOCK
**curr
;
1933 processId
= GetCurrentProcessId();
1936 if ((*curr
)->dos_file
== file
) {
1938 *curr
= (*curr
)->next
;
1939 HeapFree( SystemHeap
, 0, rem
->unix_name
);
1940 HeapFree( SystemHeap
, 0, rem
);
1943 curr
= &(*curr
)->next
;
1947 static BOOL32
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
1950 DOS_FILE_LOCK
**curr
;
1953 processId
= GetCurrentProcessId();
1954 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
1955 if ((*curr
)->processId
== processId
&&
1956 (*curr
)->dos_file
== file
&&
1957 (*curr
)->base
== f
->l_start
&&
1958 (*curr
)->len
== f
->l_len
) {
1959 /* this is the same lock */
1961 *curr
= (*curr
)->next
;
1962 HeapFree( SystemHeap
, 0, rem
->unix_name
);
1963 HeapFree( SystemHeap
, 0, rem
);
1967 /* no matching lock found */
1972 /**************************************************************************
1973 * LockFile (KERNEL32.511)
1975 BOOL32 WINAPI
LockFile(
1976 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
1977 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
1982 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
1983 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
1984 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
1986 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
1987 FIXME(file
, "Unimplemented bytes > 32bits\n");
1991 f
.l_start
= dwFileOffsetLow
;
1992 f
.l_len
= nNumberOfBytesToLockLow
;
1993 f
.l_whence
= SEEK_SET
;
1997 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
1999 /* shadow locks internally */
2000 if (!DOS_AddLock(file
, &f
)) {
2001 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2005 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2006 #ifdef USE_UNIX_LOCKS
2007 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2008 if (errno
== EACCES
|| errno
== EAGAIN
) {
2009 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Ignore
, EL_Disk
);
2014 /* remove our internal copy of the lock */
2015 DOS_RemoveLock(file
, &f
);
2023 /**************************************************************************
2024 * UnlockFile (KERNEL32.703)
2026 BOOL32 WINAPI
UnlockFile(
2027 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2028 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2033 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2034 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2035 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2037 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2038 WARN(file
, "Unimplemented bytes > 32bits\n");
2042 f
.l_start
= dwFileOffsetLow
;
2043 f
.l_len
= nNumberOfBytesToUnlockLow
;
2044 f
.l_whence
= SEEK_SET
;
2048 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2050 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2052 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2053 #ifdef USE_UNIX_LOCKS
2054 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2062 /**************************************************************************
2063 * GetFileAttributesEx32A [KERNEL32.874]
2065 BOOL32 WINAPI
GetFileAttributesEx32A(
2066 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2067 LPVOID lpFileInformation
)
2069 DOS_FULL_NAME full_name
;
2070 BY_HANDLE_FILE_INFORMATION info
;
2072 if (lpFileName
== NULL
) return FALSE
;
2073 if (lpFileInformation
== NULL
) return FALSE
;
2075 if (fInfoLevelId
== GetFileExInfoStandard
) {
2076 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2077 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2078 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2079 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2081 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2082 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2083 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2084 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2085 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2086 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2089 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
2097 /**************************************************************************
2098 * GetFileAttributesEx32W [KERNEL32.875]
2100 BOOL32 WINAPI
GetFileAttributesEx32W(
2101 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2102 LPVOID lpFileInformation
)
2104 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2106 GetFileAttributesEx32A( nameA
, fInfoLevelId
, lpFileInformation
);
2107 HeapFree( GetProcessHeap(), 0, nameA
);