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>
30 #include "wine/winbase16.h"
31 #include "wine/winestring.h"
46 #include "server/request.h"
49 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
50 #define MAP_ANON MAP_ANONYMOUS
53 /* Size of per-process table of DOS handles */
54 #define DOS_TABLE_SIZE 256
57 /***********************************************************************
60 * Convert OF_* mode into flags for CreateFile.
62 static void FILE_ConvertOFMode( INT mode
, DWORD
*access
, DWORD
*sharing
)
66 case OF_READ
: *access
= GENERIC_READ
; break;
67 case OF_WRITE
: *access
= GENERIC_WRITE
; break;
68 case OF_READWRITE
: *access
= GENERIC_READ
| GENERIC_WRITE
; break;
69 default: *access
= 0; break;
73 case OF_SHARE_EXCLUSIVE
: *sharing
= 0; break;
74 case OF_SHARE_DENY_WRITE
: *sharing
= FILE_SHARE_READ
; break;
75 case OF_SHARE_DENY_READ
: *sharing
= FILE_SHARE_WRITE
; break;
76 case OF_SHARE_DENY_NONE
:
78 default: *sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
84 /***********************************************************************
88 * oldmode[I] mode how file was first opened
89 * mode[I] mode how the file should get opened
94 * Look what we have to do with the given SHARE modes
96 * Ralph Brown's interrupt list gives following explication, I guess
97 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
99 * FIXME: Validate this function
100 ========from Ralph Brown's list =========
102 Values of DOS file sharing behavior:
103 | Second and subsequent Opens
104 First |Compat Deny Deny Deny Deny
105 Open | All Write Read None
106 |R W RW R W RW R W RW R W RW R W RW
107 - - - - -| - - - - - - - - - - - - - - - - -
108 Compat R |Y Y Y N N N 1 N N N N N 1 N N
109 W |Y Y Y N N N N N N N N N N N N
110 RW|Y Y Y N N N N N N N N N N N N
112 Deny R |C C C N N N N N N N N N N N N
113 All W |C C C N N N N N N N N N N N N
114 RW|C C C N N N N N N N N N N N N
116 Deny R |2 C C N N N Y N N N N N Y N N
117 Write W |C C C N N N N N N Y N N Y N N
118 RW|C C C N N N N N N N N N Y N N
120 Deny R |C C C N N N N Y N N N N N Y N
121 Read W |C C C N N N N N N N Y N N Y N
122 RW|C C C N N N N N N N N N N Y N
124 Deny R |2 C C N N N Y Y Y N N N Y Y Y
125 None W |C C C N N N N N N Y Y Y Y Y Y
126 RW|C C C N N N N N N N N N Y Y Y
127 Legend: Y = open succeeds, N = open fails with error code 05h
128 C = open fails, INT 24 generated
129 1 = open succeeds if file read-only, else fails with error code
130 2 = open succeeds if file read-only, else fails with INT 24
131 ========end of description from Ralph Brown's List =====
132 For every "Y" in the table we return FALSE
133 For every "N" we set the DOS_ERROR and return TRUE
134 For all other cases we barf,set the DOS_ERROR and return TRUE
137 static BOOL
FILE_ShareDeny( int mode
, int oldmode
)
139 int oldsharemode
= oldmode
& 0x70;
140 int sharemode
= mode
& 0x70;
141 int oldopenmode
= oldmode
& 3;
142 int openmode
= mode
& 3;
144 switch (oldsharemode
)
146 case OF_SHARE_COMPAT
:
147 if (sharemode
== OF_SHARE_COMPAT
) return FALSE
;
148 if (openmode
== OF_READ
) goto test_ro_err05
;
150 case OF_SHARE_EXCLUSIVE
:
151 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
153 case OF_SHARE_DENY_WRITE
:
154 if (openmode
!= OF_READ
)
156 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
161 case OF_SHARE_COMPAT
:
162 if (oldopenmode
== OF_READ
) goto test_ro_int24
;
164 case OF_SHARE_DENY_NONE
:
166 case OF_SHARE_DENY_WRITE
:
167 if (oldopenmode
== OF_READ
) return FALSE
;
168 case OF_SHARE_DENY_READ
:
169 if (oldopenmode
== OF_WRITE
) return FALSE
;
170 case OF_SHARE_EXCLUSIVE
:
175 case OF_SHARE_DENY_READ
:
176 if (openmode
!= OF_WRITE
)
178 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
183 case OF_SHARE_COMPAT
:
185 case OF_SHARE_DENY_NONE
:
187 case OF_SHARE_DENY_WRITE
:
188 if (oldopenmode
== OF_READ
) return FALSE
;
189 case OF_SHARE_DENY_READ
:
190 if (oldopenmode
== OF_WRITE
) return FALSE
;
191 case OF_SHARE_EXCLUSIVE
:
196 case OF_SHARE_DENY_NONE
:
199 case OF_SHARE_COMPAT
:
201 case OF_SHARE_DENY_NONE
:
203 case OF_SHARE_DENY_WRITE
:
204 if (oldopenmode
== OF_READ
) return FALSE
;
205 case OF_SHARE_DENY_READ
:
206 if (oldopenmode
== OF_WRITE
) return FALSE
;
207 case OF_SHARE_EXCLUSIVE
:
212 ERR(file
,"unknown mode\n");
214 ERR(file
,"shouldn't happen\n");
215 ERR(file
,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
219 if (oldmode
== OF_READ
)
223 FIXME(file
,"generate INT24 missing\n");
224 /* Is this the right error? */
225 SetLastError( ERROR_ACCESS_DENIED
);
229 if (oldmode
== OF_READ
)
233 TRACE(file
,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode
,mode
);
234 SetLastError( ERROR_ACCESS_DENIED
);
240 /***********************************************************************
243 * Set the DOS error code from errno.
245 void FILE_SetDosError(void)
247 int save_errno
= errno
; /* errno gets overwritten by printf */
249 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
253 SetLastError( ERROR_SHARING_VIOLATION
);
256 SetLastError( ERROR_INVALID_HANDLE
);
259 SetLastError( ERROR_HANDLE_DISK_FULL
);
264 SetLastError( ERROR_ACCESS_DENIED
);
267 SetLastError( ERROR_LOCK_VIOLATION
);
270 SetLastError( ERROR_FILE_NOT_FOUND
);
273 SetLastError( ERROR_CANNOT_MAKE
);
277 SetLastError( ERROR_NO_MORE_FILES
);
280 SetLastError( ERROR_FILE_EXISTS
);
284 SetLastError( ERROR_SEEK
);
287 SetLastError( ERROR_DIR_NOT_EMPTY
);
290 perror( "int21: unknown errno" );
291 SetLastError( ERROR_GEN_FAILURE
);
298 /***********************************************************************
301 * Duplicate a Unix handle into a task handle.
303 HFILE
FILE_DupUnixHandle( int fd
, DWORD access
)
306 struct create_file_request req
;
307 struct create_file_reply reply
;
309 if ((unix_handle
= dup(fd
)) == -1)
312 return INVALID_HANDLE_VALUE
;
316 req
.sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
320 CLIENT_SendRequest( REQ_CREATE_FILE
, unix_handle
, 1,
322 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
327 /***********************************************************************
330 * Implementation of CreateFile. Takes a Unix path name.
332 HFILE
FILE_CreateFile( LPCSTR filename
, DWORD access
, DWORD sharing
,
333 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
334 DWORD attributes
, HANDLE
template )
336 struct create_file_request req
;
337 struct create_file_reply reply
;
340 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
341 req
.sharing
= sharing
;
342 req
.create
= creation
;
343 req
.attrs
= attributes
;
344 CLIENT_SendRequest( REQ_CREATE_FILE
, -1, 2,
346 filename
, strlen(filename
) + 1 );
348 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
350 /* If write access failed, retry without GENERIC_WRITE */
352 if ((reply
.handle
== -1) && !Options
.failReadOnly
&&
353 (access
& GENERIC_WRITE
))
355 DWORD lasterror
= GetLastError();
356 if ((lasterror
== ERROR_ACCESS_DENIED
) ||
357 (lasterror
== ERROR_WRITE_PROTECT
))
359 req
.access
&= ~GENERIC_WRITE
;
360 CLIENT_SendRequest( REQ_CREATE_FILE
, -1, 2,
362 filename
, strlen(filename
) + 1 );
364 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
371 /***********************************************************************
374 * Same as FILE_CreateFile but for a device
376 HFILE
FILE_CreateDevice( int client_id
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
378 struct create_device_request req
;
379 struct create_device_reply reply
;
382 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
384 CLIENT_SendRequest( REQ_CREATE_DEVICE
, -1, 1, &req
, sizeof(req
) );
386 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
391 /*************************************************************************
392 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
394 * Creates or opens an object, and returns a handle that can be used to
395 * access that object.
399 * filename [I] pointer to filename to be accessed
400 * access [I] access mode requested
401 * sharing [I] share mode
402 * sa [I] pointer to security attributes
403 * creation [I] how to create the file
404 * attributes [I] attributes for newly created file
405 * template [I] handle to file with extended attributes to copy
408 * Success: Open handle to specified file
409 * Failure: INVALID_HANDLE_VALUE
412 * Should call SetLastError() on failure.
416 * Doesn't support character devices, pipes, template files, or a
417 * lot of the 'attributes' flags yet.
419 HFILE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
420 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
421 DWORD attributes
, HANDLE
template )
423 DOS_FULL_NAME full_name
;
427 SetLastError( ERROR_INVALID_PARAMETER
);
431 /* If the name starts with '\\?\', ignore the first 4 chars. */
432 if (!strncmp(filename
, "\\\\?\\", 4))
435 if (!strncmp(filename
, "UNC\\", 4))
437 FIXME( file
, "UNC name (%s) not supported.\n", filename
);
438 SetLastError( ERROR_PATH_NOT_FOUND
);
443 if (!strncmp(filename
, "\\\\.\\", 4))
444 return DEVICE_Open( filename
+4, access
, sa
);
446 /* If the name still starts with '\\', it's a UNC name. */
447 if (!strncmp(filename
, "\\\\", 2))
449 FIXME( file
, "UNC name (%s) not supported.\n", filename
);
450 SetLastError( ERROR_PATH_NOT_FOUND
);
454 /* Open a console for CONIN$ or CONOUT$ */
455 if (!lstrcmpiA(filename
, "CONIN$")) return CONSOLE_OpenHandle( FALSE
, access
, sa
);
456 if (!lstrcmpiA(filename
, "CONOUT$")) return CONSOLE_OpenHandle( TRUE
, access
, sa
);
458 if (DOSFS_GetDevice( filename
))
462 TRACE(file
, "opening device '%s'\n", filename
);
464 if (HFILE_ERROR
!=(ret
=DOSFS_OpenDevice( filename
, access
)))
467 /* Do not silence this please. It is a critical error. -MM */
468 ERR(file
, "Couldn't open device '%s'!\n",filename
);
469 SetLastError( ERROR_FILE_NOT_FOUND
);
473 /* check for filename, don't check for last entry if creating */
474 if (!DOSFS_GetFullName( filename
,
475 (creation
== OPEN_EXISTING
) || (creation
== TRUNCATE_EXISTING
), &full_name
))
478 return FILE_CreateFile( full_name
.long_name
, access
, sharing
,
479 sa
, creation
, attributes
, template );
484 /*************************************************************************
485 * CreateFile32W (KERNEL32.48)
487 HFILE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
488 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
489 DWORD attributes
, HANDLE
template)
491 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
492 HFILE res
= CreateFileA( afn
, access
, sharing
, sa
, creation
, attributes
, template );
493 HeapFree( GetProcessHeap(), 0, afn
);
498 /***********************************************************************
501 * Fill a file information from a struct stat.
503 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
505 if (S_ISDIR(st
->st_mode
))
506 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
508 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
509 if (!(st
->st_mode
& S_IWUSR
))
510 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
512 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
513 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
514 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
516 info
->dwVolumeSerialNumber
= 0; /* FIXME */
517 info
->nFileSizeHigh
= 0;
518 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
519 info
->nNumberOfLinks
= st
->st_nlink
;
520 info
->nFileIndexHigh
= 0;
521 info
->nFileIndexLow
= st
->st_ino
;
525 /***********************************************************************
528 * Stat a Unix path name. Return TRUE if OK.
530 BOOL
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
534 if (!unixName
|| !info
) return FALSE
;
536 if (stat( unixName
, &st
) == -1)
541 FILE_FillInfo( &st
, info
);
546 /***********************************************************************
547 * GetFileInformationByHandle (KERNEL32.219)
549 DWORD WINAPI
GetFileInformationByHandle( HFILE hFile
,
550 BY_HANDLE_FILE_INFORMATION
*info
)
552 struct get_file_info_request req
;
553 struct get_file_info_reply reply
;
557 CLIENT_SendRequest( REQ_GET_FILE_INFO
, -1, 1, &req
, sizeof(req
) );
558 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
))
560 DOSFS_UnixTimeToFileTime( reply
.write_time
, &info
->ftCreationTime
, 0 );
561 DOSFS_UnixTimeToFileTime( reply
.write_time
, &info
->ftLastWriteTime
, 0 );
562 DOSFS_UnixTimeToFileTime( reply
.access_time
, &info
->ftLastAccessTime
, 0 );
563 info
->dwFileAttributes
= reply
.attr
;
564 info
->dwVolumeSerialNumber
= reply
.serial
;
565 info
->nFileSizeHigh
= reply
.size_high
;
566 info
->nFileSizeLow
= reply
.size_low
;
567 info
->nNumberOfLinks
= reply
.links
;
568 info
->nFileIndexHigh
= reply
.index_high
;
569 info
->nFileIndexLow
= reply
.index_low
;
574 /**************************************************************************
575 * GetFileAttributes16 (KERNEL.420)
577 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
579 return GetFileAttributesA( name
);
583 /**************************************************************************
584 * GetFileAttributes32A (KERNEL32.217)
586 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
588 DOS_FULL_NAME full_name
;
589 BY_HANDLE_FILE_INFORMATION info
;
591 if (name
== NULL
|| *name
=='\0') return -1;
593 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
594 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
595 return info
.dwFileAttributes
;
599 /**************************************************************************
600 * GetFileAttributes32W (KERNEL32.218)
602 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
604 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
605 DWORD res
= GetFileAttributesA( nameA
);
606 HeapFree( GetProcessHeap(), 0, nameA
);
611 /***********************************************************************
612 * GetFileSize (KERNEL32.220)
614 DWORD WINAPI
GetFileSize( HFILE hFile
, LPDWORD filesizehigh
)
616 BY_HANDLE_FILE_INFORMATION info
;
617 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
618 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
619 return info
.nFileSizeLow
;
623 /***********************************************************************
624 * GetFileTime (KERNEL32.221)
626 BOOL WINAPI
GetFileTime( HFILE hFile
, FILETIME
*lpCreationTime
,
627 FILETIME
*lpLastAccessTime
,
628 FILETIME
*lpLastWriteTime
)
630 BY_HANDLE_FILE_INFORMATION info
;
631 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
632 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
633 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
634 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
638 /***********************************************************************
639 * CompareFileTime (KERNEL32.28)
641 INT WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
643 if (!x
|| !y
) return -1;
645 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
647 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
649 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
651 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
657 /***********************************************************************
658 * GetTempFileName16 (KERNEL.97)
660 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
665 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
666 drive
|= DRIVE_GetCurrentDrive() + 'A';
668 if ((drive
& TF_FORCEDRIVE
) &&
669 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
671 drive
&= ~TF_FORCEDRIVE
;
672 WARN(file
, "invalid drive %d specified\n", drive
);
675 if (drive
& TF_FORCEDRIVE
)
676 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
678 GetTempPathA( 132, temppath
);
679 return (UINT16
)GetTempFileNameA( temppath
, prefix
, unique
, buffer
);
683 /***********************************************************************
684 * GetTempFileName32A (KERNEL32.290)
686 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
,
689 static UINT unique_temp
;
690 DOS_FULL_NAME full_name
;
695 if ( !path
|| !prefix
|| !buffer
) return 0;
697 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
698 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
700 strcpy( buffer
, path
);
701 p
= buffer
+ strlen(buffer
);
703 /* add a \, if there isn't one and path is more than just the drive letter ... */
704 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
705 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
708 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
709 sprintf( p
, "%04x.tmp", num
);
711 /* Now try to create it */
717 HFILE handle
= CreateFileA( buffer
, GENERIC_WRITE
, 0, NULL
,
718 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, -1 );
719 if (handle
!= INVALID_HANDLE_VALUE
)
720 { /* We created it */
721 TRACE(file
, "created %s\n",
723 CloseHandle( handle
);
726 if (GetLastError() != ERROR_FILE_EXISTS
)
727 break; /* No need to go on */
729 sprintf( p
, "%04x.tmp", num
);
730 } while (num
!= (unique
& 0xffff));
733 /* Get the full path name */
735 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
737 /* Check if we have write access in the directory */
738 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
739 if (access( full_name
.long_name
, W_OK
) == -1)
740 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
743 TRACE(file
, "returning %s\n", buffer
);
744 return unique
? unique
: num
;
748 /***********************************************************************
749 * GetTempFileName32W (KERNEL32.291)
751 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
,
759 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
760 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
761 ret
= GetTempFileNameA( patha
, prefixa
, unique
, buffera
);
762 lstrcpyAtoW( buffer
, buffera
);
763 HeapFree( GetProcessHeap(), 0, patha
);
764 HeapFree( GetProcessHeap(), 0, prefixa
);
769 /***********************************************************************
772 * Implementation of OpenFile16() and OpenFile32().
774 static HFILE
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
,
779 WORD filedatetime
[2];
780 DOS_FULL_NAME full_name
;
781 DWORD access
, sharing
;
784 if (!ofs
) return HFILE_ERROR
;
786 ofs
->cBytes
= sizeof(OFSTRUCT
);
788 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
791 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
795 TRACE(file
, "%s %04x\n", name
, mode
);
797 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
798 Are there any cases where getting the path here is wrong?
799 Uwe Bonnes 1997 Apr 2 */
800 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
),
801 ofs
->szPathName
, NULL
)) goto error
;
802 FILE_ConvertOFMode( mode
, &access
, &sharing
);
804 /* OF_PARSE simply fills the structure */
808 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
810 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
811 name
, ofs
->szPathName
);
815 /* OF_CREATE is completely different from all other options, so
818 if (mode
& OF_CREATE
)
820 if ((hFileRet
= CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
821 sharing
, NULL
, CREATE_ALWAYS
,
822 FILE_ATTRIBUTE_NORMAL
, -1 ))== INVALID_HANDLE_VALUE
)
827 /* If OF_SEARCH is set, ignore the given path */
829 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
831 /* First try the file name as is */
832 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
833 /* Now remove the path */
834 if (name
[0] && (name
[1] == ':')) name
+= 2;
835 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
836 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
837 if (!name
[0]) goto not_found
;
840 /* Now look for the file */
842 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
845 TRACE(file
, "found %s = %s\n",
846 full_name
.long_name
, full_name
.short_name
);
847 lstrcpynA( ofs
->szPathName
, full_name
.short_name
,
848 sizeof(ofs
->szPathName
) );
850 if (mode
& OF_SHARE_EXCLUSIVE
)
851 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
852 on the file <tempdir>/_ins0432._mp to determine how
853 far installation has proceeded.
854 _ins0432._mp is an executable and while running the
855 application expects the open with OF_SHARE_ to fail*/
857 As our loader closes the files after loading the executable,
858 we can't find the running executable with FILE_InUse.
859 Perhaps the loader should keep the file open.
860 Recheck against how Win handles that case */
862 char *last
= strrchr(full_name
.long_name
,'/');
864 last
= full_name
.long_name
- 1;
865 if (GetModuleHandle16(last
+1))
867 TRACE(file
,"Denying shared open for %s\n",full_name
.long_name
);
872 if (mode
& OF_DELETE
)
874 if (unlink( full_name
.long_name
) == -1) goto not_found
;
875 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
879 hFileRet
= FILE_CreateFile( full_name
.long_name
, access
, sharing
,
880 NULL
, OPEN_EXISTING
, 0, -1 );
881 if (hFileRet
== HFILE_ERROR
) goto not_found
;
883 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
884 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
885 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
887 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
889 CloseHandle( hFileRet
);
890 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
891 /* FIXME: what error here? */
892 SetLastError( ERROR_FILE_NOT_FOUND
);
896 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
898 success
: /* We get here if the open was successful */
899 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
902 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
903 CloseHandle( hFileRet
);
907 hFileRet
= FILE_AllocDosHandle( hFileRet
);
908 if (hFileRet
== HFILE_ERROR16
) goto error
;
909 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
910 _lclose16( hFileRet
);
914 not_found
: /* We get here if the file does not exist */
915 WARN(file
, "'%s' not found\n", name
);
916 SetLastError( ERROR_FILE_NOT_FOUND
);
919 error
: /* We get here if there was an error opening the file */
920 ofs
->nErrCode
= GetLastError();
921 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
922 name
,ofs
->nErrCode
);
927 /***********************************************************************
928 * OpenFile16 (KERNEL.74)
930 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
932 return FILE_DoOpenFile( name
, ofs
, mode
, FALSE
);
936 /***********************************************************************
937 * OpenFile32 (KERNEL32.396)
939 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
941 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
945 /***********************************************************************
946 * FILE_InitProcessDosHandles
948 * Allocates the default DOS handles for a process. Called either by
949 * AllocDosHandle below or by the DOSVM stuff.
951 BOOL
FILE_InitProcessDosHandles( void ) {
954 if (!(ptr
= HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
,
955 sizeof(*ptr
) * DOS_TABLE_SIZE
)))
957 PROCESS_Current()->dos_handles
= ptr
;
958 ptr
[0] = GetStdHandle(STD_INPUT_HANDLE
);
959 ptr
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
960 ptr
[2] = GetStdHandle(STD_ERROR_HANDLE
);
961 ptr
[3] = GetStdHandle(STD_ERROR_HANDLE
);
962 ptr
[4] = GetStdHandle(STD_ERROR_HANDLE
);
966 /***********************************************************************
967 * FILE_AllocDosHandle
969 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
970 * longer valid after this function (even on failure).
972 HFILE16
FILE_AllocDosHandle( HANDLE handle
)
975 HANDLE
*ptr
= PROCESS_Current()->dos_handles
;
977 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
978 return INVALID_HANDLE_VALUE16
;
981 if (!FILE_InitProcessDosHandles())
983 ptr
= PROCESS_Current()->dos_handles
;
986 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++, ptr
++)
990 TRACE( file
, "Got %d for h32 %d\n", i
, handle
);
994 CloseHandle( handle
);
995 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
996 return INVALID_HANDLE_VALUE16
;
1000 /***********************************************************************
1003 * Return the Win32 handle for a DOS handle.
1005 HANDLE
FILE_GetHandle( HFILE16 hfile
)
1007 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1008 if ((hfile
>= DOS_TABLE_SIZE
) || !table
|| !table
[hfile
])
1010 SetLastError( ERROR_INVALID_HANDLE
);
1011 return INVALID_HANDLE_VALUE
;
1013 return table
[hfile
];
1017 /***********************************************************************
1020 * dup2() function for DOS handles.
1022 HFILE16
FILE_Dup2( HFILE16 hFile1
, HFILE16 hFile2
)
1024 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1027 if ((hFile1
>= DOS_TABLE_SIZE
) || (hFile2
>= DOS_TABLE_SIZE
) ||
1028 !table
|| !table
[hFile1
])
1030 SetLastError( ERROR_INVALID_HANDLE
);
1031 return HFILE_ERROR16
;
1035 FIXME( file
, "stdio handle closed, need proper conversion\n" );
1036 SetLastError( ERROR_INVALID_HANDLE
);
1037 return HFILE_ERROR16
;
1039 if (!DuplicateHandle( GetCurrentProcess(), table
[hFile1
],
1040 GetCurrentProcess(), &new_handle
,
1041 0, FALSE
, DUPLICATE_SAME_ACCESS
))
1042 return HFILE_ERROR16
;
1043 if (table
[hFile2
]) CloseHandle( table
[hFile2
] );
1044 table
[hFile2
] = new_handle
;
1049 /***********************************************************************
1050 * _lclose16 (KERNEL.81)
1052 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1054 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1058 FIXME( file
, "stdio handle closed, need proper conversion\n" );
1059 SetLastError( ERROR_INVALID_HANDLE
);
1060 return HFILE_ERROR16
;
1062 if ((hFile
>= DOS_TABLE_SIZE
) || !table
|| !table
[hFile
])
1064 SetLastError( ERROR_INVALID_HANDLE
);
1065 return HFILE_ERROR16
;
1067 TRACE( file
, "%d (handle32=%d)\n", hFile
, table
[hFile
] );
1068 CloseHandle( table
[hFile
] );
1074 /***********************************************************************
1075 * _lclose32 (KERNEL32.592)
1077 HFILE WINAPI
_lclose( HFILE hFile
)
1079 TRACE(file
, "handle %d\n", hFile
);
1080 return CloseHandle( hFile
) ? 0 : HFILE_ERROR
;
1084 /***********************************************************************
1085 * ReadFile (KERNEL32.428)
1087 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
1088 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
1090 struct get_read_fd_request req
;
1091 int unix_handle
, result
;
1093 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToRead
);
1095 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
1096 if (!bytesToRead
) return TRUE
;
1099 CLIENT_SendRequest( REQ_GET_READ_FD
, -1, 1, &req
, sizeof(req
) );
1100 CLIENT_WaitReply( NULL
, &unix_handle
, 0 );
1101 if (unix_handle
== -1) return FALSE
;
1102 while ((result
= read( unix_handle
, buffer
, bytesToRead
)) == -1)
1104 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1105 if ((errno
== EFAULT
) && VIRTUAL_HandleFault( buffer
)) continue;
1109 close( unix_handle
);
1110 if (result
== -1) return FALSE
;
1111 if (bytesRead
) *bytesRead
= result
;
1116 /***********************************************************************
1117 * WriteFile (KERNEL32.578)
1119 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
1120 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
1122 struct get_write_fd_request req
;
1123 int unix_handle
, result
;
1125 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToWrite
);
1127 if (bytesWritten
) *bytesWritten
= 0; /* Do this before anything else */
1128 if (!bytesToWrite
) return TRUE
;
1131 CLIENT_SendRequest( REQ_GET_WRITE_FD
, -1, 1, &req
, sizeof(req
) );
1132 CLIENT_WaitReply( NULL
, &unix_handle
, 0 );
1133 if (unix_handle
== -1) return FALSE
;
1134 while ((result
= write( unix_handle
, buffer
, bytesToWrite
)) == -1)
1136 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1137 if ((errno
== EFAULT
) && VIRTUAL_HandleFault( buffer
)) continue;
1141 close( unix_handle
);
1142 if (result
== -1) return FALSE
;
1143 if (bytesWritten
) *bytesWritten
= result
;
1148 /***********************************************************************
1151 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1155 TRACE(file
, "%d %08lx %ld\n",
1156 hFile
, (DWORD
)buffer
, count
);
1158 /* Some programs pass a count larger than the allocated buffer */
1159 maxlen
= GetSelectorLimit16( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1160 if (count
> maxlen
) count
= maxlen
;
1161 return _lread(FILE_GetHandle(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1165 /***********************************************************************
1168 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1170 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1174 /***********************************************************************
1175 * _lread32 (KERNEL32.596)
1177 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
1180 if (!ReadFile( handle
, buffer
, count
, &result
, NULL
)) return -1;
1185 /***********************************************************************
1186 * _lread16 (KERNEL.82)
1188 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1190 return (UINT16
)_lread(FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1194 /***********************************************************************
1195 * _lcreat16 (KERNEL.83)
1197 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1199 TRACE(file
, "%s %02x\n", path
, attr
);
1200 return FILE_AllocDosHandle( _lcreat( path
, attr
) );
1204 /***********************************************************************
1205 * _lcreat32 (KERNEL32.593)
1207 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
1209 TRACE(file
, "%s %02x\n", path
, attr
);
1210 return CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
1211 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1212 CREATE_ALWAYS
, attr
, -1 );
1216 /***********************************************************************
1217 * _lcreat16_uniq (Not a Windows API)
1219 HFILE16
_lcreat16_uniq( LPCSTR path
, INT attr
)
1221 TRACE(file
, "%s %02x\n", path
, attr
);
1222 return FILE_AllocDosHandle( CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
1223 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1224 CREATE_NEW
, attr
, -1 ));
1228 /***********************************************************************
1229 * SetFilePointer (KERNEL32.492)
1231 DWORD WINAPI
SetFilePointer( HFILE hFile
, LONG distance
, LONG
*highword
,
1234 struct set_file_pointer_request req
;
1235 struct set_file_pointer_reply reply
;
1237 if (highword
&& *highword
)
1239 FIXME(file
, "64-bit offsets not supported yet\n");
1240 SetLastError( ERROR_INVALID_PARAMETER
);
1243 TRACE(file
, "handle %d offset %ld origin %ld\n",
1244 hFile
, distance
, method
);
1248 req
.high
= highword
? *highword
: 0;
1249 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1250 req
.whence
= method
;
1251 CLIENT_SendRequest( REQ_SET_FILE_POINTER
, -1, 1, &req
, sizeof(req
) );
1252 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return 0xffffffff;
1254 if (highword
) *highword
= reply
.high
;
1259 /***********************************************************************
1260 * _llseek16 (KERNEL.84)
1263 * Seeking before the start of the file should be allowed for _llseek16,
1264 * but cause subsequent I/O operations to fail (cf. interrupt list)
1267 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1269 return SetFilePointer( FILE_GetHandle(hFile
), lOffset
, NULL
, nOrigin
);
1273 /***********************************************************************
1274 * _llseek32 (KERNEL32.594)
1276 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
1278 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1282 /***********************************************************************
1283 * _lopen16 (KERNEL.85)
1285 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1287 return FILE_AllocDosHandle( _lopen( path
, mode
) );
1291 /***********************************************************************
1292 * _lopen32 (KERNEL32.595)
1294 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
1296 DWORD access
, sharing
;
1298 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1299 FILE_ConvertOFMode( mode
, &access
, &sharing
);
1300 return CreateFileA( path
, access
, sharing
, NULL
, OPEN_EXISTING
, 0, -1 );
1304 /***********************************************************************
1305 * _lwrite16 (KERNEL.86)
1307 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1309 return (UINT16
)_hwrite( FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1312 /***********************************************************************
1313 * _lwrite32 (KERNEL32.761)
1315 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
1317 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
1321 /***********************************************************************
1322 * _hread16 (KERNEL.349)
1324 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1326 return _lread( FILE_GetHandle(hFile
), buffer
, count
);
1330 /***********************************************************************
1331 * _hread32 (KERNEL32.590)
1333 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
1335 return _lread( hFile
, buffer
, count
);
1339 /***********************************************************************
1340 * _hwrite16 (KERNEL.350)
1342 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1344 return _hwrite( FILE_GetHandle(hFile
), buffer
, count
);
1348 /***********************************************************************
1349 * _hwrite32 (KERNEL32.591)
1351 * experimentation yields that _lwrite:
1352 * o truncates the file at the current position with
1354 * o returns 0 on a 0 length write
1355 * o works with console handles
1358 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
1362 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1366 /* Expand or truncate at current position */
1367 if (!SetEndOfFile( handle
)) return HFILE_ERROR
;
1370 if (!WriteFile( handle
, buffer
, count
, &result
, NULL
))
1376 /***********************************************************************
1377 * SetHandleCount16 (KERNEL.199)
1379 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1381 HGLOBAL16 hPDB
= GetCurrentPDB16();
1382 PDB16
*pdb
= (PDB16
*)GlobalLock16( hPDB
);
1383 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1385 TRACE(file
, "(%d)\n", count
);
1387 if (count
< 20) count
= 20; /* No point in going below 20 */
1388 else if (count
> 254) count
= 254;
1392 if (pdb
->nbFiles
> 20)
1394 memcpy( pdb
->fileHandles
, files
, 20 );
1395 GlobalFree16( pdb
->hFileHandles
);
1396 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1397 GlobalHandleToSel16( hPDB
) );
1398 pdb
->hFileHandles
= 0;
1402 else /* More than 20, need a new file handles table */
1405 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1408 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1409 return pdb
->nbFiles
;
1411 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1413 if (count
> pdb
->nbFiles
)
1415 memcpy( newfiles
, files
, pdb
->nbFiles
);
1416 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1418 else memcpy( newfiles
, files
, count
);
1419 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1420 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1421 pdb
->hFileHandles
= newhandle
;
1422 pdb
->nbFiles
= count
;
1424 return pdb
->nbFiles
;
1428 /*************************************************************************
1429 * SetHandleCount32 (KERNEL32.494)
1431 UINT WINAPI
SetHandleCount( UINT count
)
1433 return MIN( 256, count
);
1437 /***********************************************************************
1438 * FlushFileBuffers (KERNEL32.133)
1440 BOOL WINAPI
FlushFileBuffers( HFILE hFile
)
1442 struct flush_file_request req
;
1445 CLIENT_SendRequest( REQ_FLUSH_FILE
, -1, 1, &req
, sizeof(req
) );
1446 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1450 /**************************************************************************
1451 * SetEndOfFile (KERNEL32.483)
1453 BOOL WINAPI
SetEndOfFile( HFILE hFile
)
1455 struct truncate_file_request req
;
1458 CLIENT_SendRequest( REQ_TRUNCATE_FILE
, -1, 1, &req
, sizeof(req
) );
1459 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1463 /***********************************************************************
1464 * DeleteFile16 (KERNEL.146)
1466 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1468 return DeleteFileA( path
);
1472 /***********************************************************************
1473 * DeleteFile32A (KERNEL32.71)
1475 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1477 DOS_FULL_NAME full_name
;
1479 TRACE(file
, "'%s'\n", path
);
1483 ERR(file
, "Empty path passed\n");
1486 if (DOSFS_GetDevice( path
))
1488 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1489 SetLastError( ERROR_FILE_NOT_FOUND
);
1493 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1494 if (unlink( full_name
.long_name
) == -1)
1503 /***********************************************************************
1504 * DeleteFile32W (KERNEL32.72)
1506 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1508 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1509 BOOL ret
= DeleteFileA( xpath
);
1510 HeapFree( GetProcessHeap(), 0, xpath
);
1515 /***********************************************************************
1518 LPVOID
FILE_dommap( int unix_handle
, LPVOID start
,
1519 DWORD size_high
, DWORD size_low
,
1520 DWORD offset_high
, DWORD offset_low
,
1521 int prot
, int flags
)
1527 if (size_high
|| offset_high
)
1528 FIXME(file
, "offsets larger than 4Gb not supported\n");
1530 if (unix_handle
== -1)
1535 static int fdzero
= -1;
1539 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1541 perror( "/dev/zero: open" );
1546 #endif /* MAP_ANON */
1547 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1549 flags
&= ~MAP_SHARED
;
1552 flags
|= MAP_PRIVATE
;
1555 else fd
= unix_handle
;
1557 if ((ret
= mmap( start
, size_low
, prot
,
1558 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1561 /* mmap() failed; if this is because the file offset is not */
1562 /* page-aligned (EINVAL), or because the underlying filesystem */
1563 /* does not support mmap() (ENOEXEC), we do it by hand. */
1565 if (unix_handle
== -1) return ret
;
1566 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1567 if (prot
& PROT_WRITE
)
1569 /* We cannot fake shared write mappings */
1571 if (flags
& MAP_SHARED
) return ret
;
1574 if (!(flags
& MAP_PRIVATE
)) return ret
;
1577 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1578 /* Reserve the memory with an anonymous mmap */
1579 ret
= FILE_dommap( -1, start
, size_high
, size_low
, 0, 0,
1580 PROT_READ
| PROT_WRITE
, flags
);
1581 if (ret
== (LPVOID
)-1) return ret
;
1582 /* Now read in the file */
1583 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1585 FILE_munmap( ret
, size_high
, size_low
);
1588 read( fd
, ret
, size_low
);
1589 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1590 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1595 /***********************************************************************
1598 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1601 FIXME(file
, "offsets larger than 4Gb not supported\n");
1602 return munmap( start
, size_low
);
1606 /***********************************************************************
1607 * GetFileType (KERNEL32.222)
1609 DWORD WINAPI
GetFileType( HFILE hFile
)
1611 struct get_file_info_request req
;
1612 struct get_file_info_reply reply
;
1615 CLIENT_SendRequest( REQ_GET_FILE_INFO
, -1, 1, &req
, sizeof(req
) );
1616 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
))
1617 return FILE_TYPE_UNKNOWN
;
1622 /**************************************************************************
1623 * MoveFileEx32A (KERNEL32.???)
1625 BOOL WINAPI
MoveFileExA( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1627 DOS_FULL_NAME full_name1
, full_name2
;
1628 int mode
=0; /* mode == 1: use copy */
1630 TRACE(file
, "(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1632 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1633 if (fn2
) { /* !fn2 means delete fn1 */
1634 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1635 /* Source name and target path are valid */
1636 if ( full_name1
.drive
!= full_name2
.drive
)
1638 /* use copy, if allowed */
1639 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1640 /* FIXME: Use right error code */
1641 SetLastError( ERROR_FILE_EXISTS
);
1646 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1647 /* target exists, check if we may overwrite */
1648 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1649 /* FIXME: Use right error code */
1650 SetLastError( ERROR_ACCESS_DENIED
);
1654 else /* fn2 == NULL means delete source */
1655 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1656 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1657 WARN(file
, "Illegal flag\n");
1658 SetLastError( ERROR_GEN_FAILURE
);
1661 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1662 Perhaps we should queue these command and execute it
1663 when exiting... What about using on_exit(2)
1665 FIXME(file
, "Please delete file '%s' when Wine has finished\n",
1666 full_name1
.long_name
);
1669 else if (unlink( full_name1
.long_name
) == -1)
1674 else return TRUE
; /* successfully deleted */
1676 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1677 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1678 Perhaps we should queue these command and execute it
1679 when exiting... What about using on_exit(2)
1681 FIXME(file
,"Please move existing file '%s' to file '%s'"
1682 "when Wine has finished\n",
1683 full_name1
.long_name
, full_name2
.long_name
);
1687 if (!mode
) /* move the file */
1688 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1694 else /* copy File */
1695 return CopyFileA(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1699 /**************************************************************************
1700 * MoveFileEx32W (KERNEL32.???)
1702 BOOL WINAPI
MoveFileExW( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1704 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1705 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1706 BOOL res
= MoveFileExA( afn1
, afn2
, flag
);
1707 HeapFree( GetProcessHeap(), 0, afn1
);
1708 HeapFree( GetProcessHeap(), 0, afn2
);
1713 /**************************************************************************
1714 * MoveFile32A (KERNEL32.387)
1716 * Move file or directory
1718 BOOL WINAPI
MoveFileA( LPCSTR fn1
, LPCSTR fn2
)
1720 DOS_FULL_NAME full_name1
, full_name2
;
1723 TRACE(file
, "(%s,%s)\n", fn1
, fn2
);
1725 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1726 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1727 /* The new name must not already exist */
1729 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1731 if (full_name1
.drive
== full_name2
.drive
) /* move */
1732 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1739 if (stat( full_name1
.long_name
, &fstat
))
1741 WARN(file
, "Invalid source file %s\n",
1742 full_name1
.long_name
);
1746 if (S_ISDIR(fstat
.st_mode
)) {
1747 /* No Move for directories across file systems */
1748 /* FIXME: Use right error code */
1749 SetLastError( ERROR_GEN_FAILURE
);
1753 return CopyFileA(fn1
, fn2
, TRUE
); /*fail, if exist */
1758 /**************************************************************************
1759 * MoveFile32W (KERNEL32.390)
1761 BOOL WINAPI
MoveFileW( LPCWSTR fn1
, LPCWSTR fn2
)
1763 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1764 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1765 BOOL res
= MoveFileA( afn1
, afn2
);
1766 HeapFree( GetProcessHeap(), 0, afn1
);
1767 HeapFree( GetProcessHeap(), 0, afn2
);
1772 /**************************************************************************
1773 * CopyFile32A (KERNEL32.36)
1775 BOOL WINAPI
CopyFileA( LPCSTR source
, LPCSTR dest
, BOOL fail_if_exists
)
1778 BY_HANDLE_FILE_INFORMATION info
;
1784 if ((h1
= _lopen( source
, OF_READ
)) == HFILE_ERROR
) return FALSE
;
1785 if (!GetFileInformationByHandle( h1
, &info
))
1790 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1791 if ((h2
= CreateFileA( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1792 fail_if_exists
? CREATE_NEW
: CREATE_ALWAYS
,
1793 info
.dwFileAttributes
, h1
)) == HFILE_ERROR
)
1798 while ((count
= _lread( h1
, buffer
, sizeof(buffer
) )) > 0)
1803 INT res
= _lwrite( h2
, p
, count
);
1804 if (res
<= 0) goto done
;
1817 /**************************************************************************
1818 * CopyFile32W (KERNEL32.37)
1820 BOOL WINAPI
CopyFileW( LPCWSTR source
, LPCWSTR dest
, BOOL fail_if_exists
)
1822 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1823 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1824 BOOL ret
= CopyFileA( sourceA
, destA
, fail_if_exists
);
1825 HeapFree( GetProcessHeap(), 0, sourceA
);
1826 HeapFree( GetProcessHeap(), 0, destA
);
1831 /**************************************************************************
1832 * CopyFileEx32A (KERNEL32.858)
1834 * This implementation ignores most of the extra parameters passed-in into
1835 * the "ex" version of the method and calls the CopyFile method.
1836 * It will have to be fixed eventually.
1838 BOOL WINAPI
CopyFileExA(LPCSTR sourceFilename
,
1839 LPCSTR destFilename
,
1840 LPPROGRESS_ROUTINE progressRoutine
,
1842 LPBOOL cancelFlagPointer
,
1845 BOOL failIfExists
= FALSE
;
1848 * Interpret the only flag that CopyFile can interpret.
1850 if ( (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0)
1852 failIfExists
= TRUE
;
1855 return CopyFileA(sourceFilename
, destFilename
, failIfExists
);
1858 /**************************************************************************
1859 * CopyFileEx32W (KERNEL32.859)
1861 BOOL WINAPI
CopyFileExW(LPCWSTR sourceFilename
,
1862 LPCWSTR destFilename
,
1863 LPPROGRESS_ROUTINE progressRoutine
,
1865 LPBOOL cancelFlagPointer
,
1868 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename
);
1869 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename
);
1871 BOOL ret
= CopyFileExA(sourceA
,
1878 HeapFree( GetProcessHeap(), 0, sourceA
);
1879 HeapFree( GetProcessHeap(), 0, destA
);
1885 /***********************************************************************
1886 * SetFileTime (KERNEL32.650)
1888 BOOL WINAPI
SetFileTime( HFILE hFile
,
1889 const FILETIME
*lpCreationTime
,
1890 const FILETIME
*lpLastAccessTime
,
1891 const FILETIME
*lpLastWriteTime
)
1893 struct set_file_time_request req
;
1896 if (lpLastAccessTime
)
1897 req
.access_time
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1899 req
.access_time
= 0; /* FIXME */
1900 if (lpLastWriteTime
)
1901 req
.write_time
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1903 req
.write_time
= 0; /* FIXME */
1905 CLIENT_SendRequest( REQ_SET_FILE_TIME
, -1, 1, &req
, sizeof(req
) );
1906 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1910 /**************************************************************************
1911 * LockFile (KERNEL32.511)
1913 BOOL WINAPI
LockFile( HFILE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1914 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
)
1916 struct lock_file_request req
;
1919 req
.offset_low
= dwFileOffsetLow
;
1920 req
.offset_high
= dwFileOffsetHigh
;
1921 req
.count_low
= nNumberOfBytesToLockLow
;
1922 req
.count_high
= nNumberOfBytesToLockHigh
;
1923 CLIENT_SendRequest( REQ_LOCK_FILE
, -1, 1, &req
, sizeof(req
) );
1924 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1928 /**************************************************************************
1929 * UnlockFile (KERNEL32.703)
1931 BOOL WINAPI
UnlockFile( HFILE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1932 DWORD nNumberOfBytesToUnlockLow
, DWORD nNumberOfBytesToUnlockHigh
)
1934 struct unlock_file_request req
;
1937 req
.offset_low
= dwFileOffsetLow
;
1938 req
.offset_high
= dwFileOffsetHigh
;
1939 req
.count_low
= nNumberOfBytesToUnlockLow
;
1940 req
.count_high
= nNumberOfBytesToUnlockHigh
;
1941 CLIENT_SendRequest( REQ_UNLOCK_FILE
, -1, 1, &req
, sizeof(req
) );
1942 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1948 struct DOS_FILE_LOCK
{
1949 struct DOS_FILE_LOCK
* next
;
1953 FILE_OBJECT
* dos_file
;
1954 /* char * unix_name;*/
1957 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
1959 static DOS_FILE_LOCK
*locks
= NULL
;
1960 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
1963 /* Locks need to be mirrored because unix file locking is based
1964 * on the pid. Inside of wine there can be multiple WINE processes
1965 * that share the same unix pid.
1966 * Read's and writes should check these locks also - not sure
1967 * how critical that is at this point (FIXME).
1970 static BOOL
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
1972 DOS_FILE_LOCK
*curr
;
1975 processId
= GetCurrentProcessId();
1977 /* check if lock overlaps a current lock for the same file */
1979 for (curr
= locks
; curr
; curr
= curr
->next
) {
1980 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
1981 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
1982 return TRUE
;/* region is identic */
1983 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
1984 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
1985 /* region overlaps */
1992 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
1993 curr
->processId
= GetCurrentProcessId();
1994 curr
->base
= f
->l_start
;
1995 curr
->len
= f
->l_len
;
1996 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
1998 curr
->dos_file
= file
;
2003 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
2006 DOS_FILE_LOCK
**curr
;
2009 processId
= GetCurrentProcessId();
2012 if ((*curr
)->dos_file
== file
) {
2014 *curr
= (*curr
)->next
;
2015 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2016 HeapFree( SystemHeap
, 0, rem
);
2019 curr
= &(*curr
)->next
;
2023 static BOOL
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2026 DOS_FILE_LOCK
**curr
;
2029 processId
= GetCurrentProcessId();
2030 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2031 if ((*curr
)->processId
== processId
&&
2032 (*curr
)->dos_file
== file
&&
2033 (*curr
)->base
== f
->l_start
&&
2034 (*curr
)->len
== f
->l_len
) {
2035 /* this is the same lock */
2037 *curr
= (*curr
)->next
;
2038 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2039 HeapFree( SystemHeap
, 0, rem
);
2043 /* no matching lock found */
2048 /**************************************************************************
2049 * LockFile (KERNEL32.511)
2051 BOOL WINAPI
LockFile(
2052 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2053 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2058 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2059 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2060 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2062 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2063 FIXME(file
, "Unimplemented bytes > 32bits\n");
2067 f
.l_start
= dwFileOffsetLow
;
2068 f
.l_len
= nNumberOfBytesToLockLow
;
2069 f
.l_whence
= SEEK_SET
;
2073 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2075 /* shadow locks internally */
2076 if (!DOS_AddLock(file
, &f
)) {
2077 SetLastError( ERROR_LOCK_VIOLATION
);
2081 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2082 #ifdef USE_UNIX_LOCKS
2083 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2084 if (errno
== EACCES
|| errno
== EAGAIN
) {
2085 SetLastError( ERROR_LOCK_VIOLATION
);
2090 /* remove our internal copy of the lock */
2091 DOS_RemoveLock(file
, &f
);
2099 /**************************************************************************
2100 * UnlockFile (KERNEL32.703)
2102 BOOL WINAPI
UnlockFile(
2103 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2104 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2109 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2110 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2111 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2113 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2114 WARN(file
, "Unimplemented bytes > 32bits\n");
2118 f
.l_start
= dwFileOffsetLow
;
2119 f
.l_len
= nNumberOfBytesToUnlockLow
;
2120 f
.l_whence
= SEEK_SET
;
2124 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2126 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2128 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2129 #ifdef USE_UNIX_LOCKS
2130 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2139 /**************************************************************************
2140 * GetFileAttributesEx32A [KERNEL32.874]
2142 BOOL WINAPI
GetFileAttributesExA(
2143 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2144 LPVOID lpFileInformation
)
2146 DOS_FULL_NAME full_name
;
2147 BY_HANDLE_FILE_INFORMATION info
;
2149 if (lpFileName
== NULL
) return FALSE
;
2150 if (lpFileInformation
== NULL
) return FALSE
;
2152 if (fInfoLevelId
== GetFileExInfoStandard
) {
2153 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2154 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2155 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2156 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2158 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2159 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2160 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2161 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2162 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2163 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2166 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
2174 /**************************************************************************
2175 * GetFileAttributesEx32W [KERNEL32.875]
2177 BOOL WINAPI
GetFileAttributesExW(
2178 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2179 LPVOID lpFileInformation
)
2181 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2183 GetFileAttributesExA( nameA
, fInfoLevelId
, lpFileInformation
);
2184 HeapFree( GetProcessHeap(), 0, nameA
);