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 DEFAULT_DEBUG_CHANNEL(file
)
51 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
52 #define MAP_ANON MAP_ANONYMOUS
55 /* Size of per-process table of DOS handles */
56 #define DOS_TABLE_SIZE 256
59 /***********************************************************************
62 * Convert OF_* mode into flags for CreateFile.
64 static void FILE_ConvertOFMode( INT mode
, DWORD
*access
, DWORD
*sharing
)
68 case OF_READ
: *access
= GENERIC_READ
; break;
69 case OF_WRITE
: *access
= GENERIC_WRITE
; break;
70 case OF_READWRITE
: *access
= GENERIC_READ
| GENERIC_WRITE
; break;
71 default: *access
= 0; break;
75 case OF_SHARE_EXCLUSIVE
: *sharing
= 0; break;
76 case OF_SHARE_DENY_WRITE
: *sharing
= FILE_SHARE_READ
; break;
77 case OF_SHARE_DENY_READ
: *sharing
= FILE_SHARE_WRITE
; break;
78 case OF_SHARE_DENY_NONE
:
80 default: *sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
86 /***********************************************************************
90 * oldmode[I] mode how file was first opened
91 * mode[I] mode how the file should get opened
96 * Look what we have to do with the given SHARE modes
98 * Ralph Brown's interrupt list gives following explication, I guess
99 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
101 * FIXME: Validate this function
102 ========from Ralph Brown's list =========
104 Values of DOS file sharing behavior:
105 | Second and subsequent Opens
106 First |Compat Deny Deny Deny Deny
107 Open | All Write Read None
108 |R W RW R W RW R W RW R W RW R W RW
109 - - - - -| - - - - - - - - - - - - - - - - -
110 Compat R |Y Y Y N N N 1 N N N N N 1 N N
111 W |Y Y Y N N N N N N N N N N N N
112 RW|Y Y Y N N N N N N N N N N N N
114 Deny R |C C C N N N N N N N N N N N N
115 All W |C C C N N N N N N N N N N N N
116 RW|C C C N N N N N N N N N N N N
118 Deny R |2 C C N N N Y N N N N N Y N N
119 Write W |C C C N N N N N N Y N N Y N N
120 RW|C C C N N N N N N N N N Y N N
122 Deny R |C C C N N N N Y N N N N N Y N
123 Read W |C C C N N N N N N N Y N N Y N
124 RW|C C C N N N N N N N N N N Y N
126 Deny R |2 C C N N N Y Y Y N N N Y Y Y
127 None W |C C C N N N N N N Y Y Y Y Y Y
128 RW|C C C N N N N N N N N N Y Y Y
129 Legend: Y = open succeeds, N = open fails with error code 05h
130 C = open fails, INT 24 generated
131 1 = open succeeds if file read-only, else fails with error code
132 2 = open succeeds if file read-only, else fails with INT 24
133 ========end of description from Ralph Brown's List =====
134 For every "Y" in the table we return FALSE
135 For every "N" we set the DOS_ERROR and return TRUE
136 For all other cases we barf,set the DOS_ERROR and return TRUE
139 static BOOL
FILE_ShareDeny( int mode
, int oldmode
)
141 int oldsharemode
= oldmode
& 0x70;
142 int sharemode
= mode
& 0x70;
143 int oldopenmode
= oldmode
& 3;
144 int openmode
= mode
& 3;
146 switch (oldsharemode
)
148 case OF_SHARE_COMPAT
:
149 if (sharemode
== OF_SHARE_COMPAT
) return FALSE
;
150 if (openmode
== OF_READ
) goto test_ro_err05
;
152 case OF_SHARE_EXCLUSIVE
:
153 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
155 case OF_SHARE_DENY_WRITE
:
156 if (openmode
!= OF_READ
)
158 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
163 case OF_SHARE_COMPAT
:
164 if (oldopenmode
== OF_READ
) goto test_ro_int24
;
166 case OF_SHARE_DENY_NONE
:
168 case OF_SHARE_DENY_WRITE
:
169 if (oldopenmode
== OF_READ
) return FALSE
;
170 case OF_SHARE_DENY_READ
:
171 if (oldopenmode
== OF_WRITE
) return FALSE
;
172 case OF_SHARE_EXCLUSIVE
:
177 case OF_SHARE_DENY_READ
:
178 if (openmode
!= OF_WRITE
)
180 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
185 case OF_SHARE_COMPAT
:
187 case OF_SHARE_DENY_NONE
:
189 case OF_SHARE_DENY_WRITE
:
190 if (oldopenmode
== OF_READ
) return FALSE
;
191 case OF_SHARE_DENY_READ
:
192 if (oldopenmode
== OF_WRITE
) return FALSE
;
193 case OF_SHARE_EXCLUSIVE
:
198 case OF_SHARE_DENY_NONE
:
201 case OF_SHARE_COMPAT
:
203 case OF_SHARE_DENY_NONE
:
205 case OF_SHARE_DENY_WRITE
:
206 if (oldopenmode
== OF_READ
) return FALSE
;
207 case OF_SHARE_DENY_READ
:
208 if (oldopenmode
== OF_WRITE
) return FALSE
;
209 case OF_SHARE_EXCLUSIVE
:
214 ERR(file
,"unknown mode\n");
216 ERR(file
,"shouldn't happen\n");
217 ERR(file
,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
221 if (oldmode
== OF_READ
)
225 FIXME(file
,"generate INT24 missing\n");
226 /* Is this the right error? */
227 SetLastError( ERROR_ACCESS_DENIED
);
231 if (oldmode
== OF_READ
)
235 TRACE(file
,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode
,mode
);
236 SetLastError( ERROR_ACCESS_DENIED
);
242 /***********************************************************************
245 * Set the DOS error code from errno.
247 void FILE_SetDosError(void)
249 int save_errno
= errno
; /* errno gets overwritten by printf */
251 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
255 SetLastError( ERROR_SHARING_VIOLATION
);
258 SetLastError( ERROR_INVALID_HANDLE
);
261 SetLastError( ERROR_HANDLE_DISK_FULL
);
266 SetLastError( ERROR_ACCESS_DENIED
);
269 SetLastError( ERROR_LOCK_VIOLATION
);
272 SetLastError( ERROR_FILE_NOT_FOUND
);
275 SetLastError( ERROR_CANNOT_MAKE
);
279 SetLastError( ERROR_NO_MORE_FILES
);
282 SetLastError( ERROR_FILE_EXISTS
);
286 SetLastError( ERROR_SEEK
);
289 SetLastError( ERROR_DIR_NOT_EMPTY
);
292 perror( "int21: unknown errno" );
293 SetLastError( ERROR_GEN_FAILURE
);
300 /***********************************************************************
303 * Duplicate a Unix handle into a task handle.
305 HFILE
FILE_DupUnixHandle( int fd
, DWORD access
)
308 struct create_file_request req
;
309 struct create_file_reply reply
;
311 if ((unix_handle
= dup(fd
)) == -1)
314 return INVALID_HANDLE_VALUE
;
318 req
.sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
322 CLIENT_SendRequest( REQ_CREATE_FILE
, unix_handle
, 1,
324 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
329 /***********************************************************************
332 * Implementation of CreateFile. Takes a Unix path name.
334 HFILE
FILE_CreateFile( LPCSTR filename
, DWORD access
, DWORD sharing
,
335 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
336 DWORD attributes
, HANDLE
template )
338 struct create_file_request req
;
339 struct create_file_reply reply
;
342 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
343 req
.sharing
= sharing
;
344 req
.create
= creation
;
345 req
.attrs
= attributes
;
346 CLIENT_SendRequest( REQ_CREATE_FILE
, -1, 2,
348 filename
, strlen(filename
) + 1 );
350 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
352 /* If write access failed, retry without GENERIC_WRITE */
354 if ((reply
.handle
== -1) && !Options
.failReadOnly
&&
355 (access
& GENERIC_WRITE
))
357 DWORD lasterror
= GetLastError();
358 if ((lasterror
== ERROR_ACCESS_DENIED
) ||
359 (lasterror
== ERROR_WRITE_PROTECT
))
361 req
.access
&= ~GENERIC_WRITE
;
362 CLIENT_SendRequest( REQ_CREATE_FILE
, -1, 2,
364 filename
, strlen(filename
) + 1 );
366 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
373 /***********************************************************************
376 * Same as FILE_CreateFile but for a device
378 HFILE
FILE_CreateDevice( int client_id
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
380 struct create_device_request req
;
381 struct create_device_reply reply
;
384 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
386 CLIENT_SendRequest( REQ_CREATE_DEVICE
, -1, 1, &req
, sizeof(req
) );
388 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
393 /*************************************************************************
394 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
396 * Creates or opens an object, and returns a handle that can be used to
397 * access that object.
401 * filename [I] pointer to filename to be accessed
402 * access [I] access mode requested
403 * sharing [I] share mode
404 * sa [I] pointer to security attributes
405 * creation [I] how to create the file
406 * attributes [I] attributes for newly created file
407 * template [I] handle to file with extended attributes to copy
410 * Success: Open handle to specified file
411 * Failure: INVALID_HANDLE_VALUE
414 * Should call SetLastError() on failure.
418 * Doesn't support character devices, pipes, template files, or a
419 * lot of the 'attributes' flags yet.
421 HFILE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
422 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
423 DWORD attributes
, HANDLE
template )
425 DOS_FULL_NAME full_name
;
429 SetLastError( ERROR_INVALID_PARAMETER
);
433 /* If the name starts with '\\?\', ignore the first 4 chars. */
434 if (!strncmp(filename
, "\\\\?\\", 4))
437 if (!strncmp(filename
, "UNC\\", 4))
439 FIXME( file
, "UNC name (%s) not supported.\n", filename
);
440 SetLastError( ERROR_PATH_NOT_FOUND
);
445 if (!strncmp(filename
, "\\\\.\\", 4))
446 return DEVICE_Open( filename
+4, access
, sa
);
448 /* If the name still starts with '\\', it's a UNC name. */
449 if (!strncmp(filename
, "\\\\", 2))
451 FIXME( file
, "UNC name (%s) not supported.\n", filename
);
452 SetLastError( ERROR_PATH_NOT_FOUND
);
456 /* Open a console for CONIN$ or CONOUT$ */
457 if (!lstrcmpiA(filename
, "CONIN$")) return CONSOLE_OpenHandle( FALSE
, access
, sa
);
458 if (!lstrcmpiA(filename
, "CONOUT$")) return CONSOLE_OpenHandle( TRUE
, access
, sa
);
460 if (DOSFS_GetDevice( filename
))
464 TRACE(file
, "opening device '%s'\n", filename
);
466 if (HFILE_ERROR
!=(ret
=DOSFS_OpenDevice( filename
, access
)))
469 /* Do not silence this please. It is a critical error. -MM */
470 ERR(file
, "Couldn't open device '%s'!\n",filename
);
471 SetLastError( ERROR_FILE_NOT_FOUND
);
475 /* check for filename, don't check for last entry if creating */
476 if (!DOSFS_GetFullName( filename
,
477 (creation
== OPEN_EXISTING
) || (creation
== TRUNCATE_EXISTING
), &full_name
))
480 return FILE_CreateFile( full_name
.long_name
, access
, sharing
,
481 sa
, creation
, attributes
, template );
486 /*************************************************************************
487 * CreateFile32W (KERNEL32.48)
489 HFILE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
490 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
491 DWORD attributes
, HANDLE
template)
493 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
494 HFILE res
= CreateFileA( afn
, access
, sharing
, sa
, creation
, attributes
, template );
495 HeapFree( GetProcessHeap(), 0, afn
);
500 /***********************************************************************
503 * Fill a file information from a struct stat.
505 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
507 if (S_ISDIR(st
->st_mode
))
508 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
510 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
511 if (!(st
->st_mode
& S_IWUSR
))
512 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
514 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
515 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
516 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
518 info
->dwVolumeSerialNumber
= 0; /* FIXME */
519 info
->nFileSizeHigh
= 0;
520 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
521 info
->nNumberOfLinks
= st
->st_nlink
;
522 info
->nFileIndexHigh
= 0;
523 info
->nFileIndexLow
= st
->st_ino
;
527 /***********************************************************************
530 * Stat a Unix path name. Return TRUE if OK.
532 BOOL
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
536 if (!unixName
|| !info
) return FALSE
;
538 if (stat( unixName
, &st
) == -1)
543 FILE_FillInfo( &st
, info
);
548 /***********************************************************************
549 * GetFileInformationByHandle (KERNEL32.219)
551 DWORD WINAPI
GetFileInformationByHandle( HFILE hFile
,
552 BY_HANDLE_FILE_INFORMATION
*info
)
554 struct get_file_info_request req
;
555 struct get_file_info_reply reply
;
559 CLIENT_SendRequest( REQ_GET_FILE_INFO
, -1, 1, &req
, sizeof(req
) );
560 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
))
562 DOSFS_UnixTimeToFileTime( reply
.write_time
, &info
->ftCreationTime
, 0 );
563 DOSFS_UnixTimeToFileTime( reply
.write_time
, &info
->ftLastWriteTime
, 0 );
564 DOSFS_UnixTimeToFileTime( reply
.access_time
, &info
->ftLastAccessTime
, 0 );
565 info
->dwFileAttributes
= reply
.attr
;
566 info
->dwVolumeSerialNumber
= reply
.serial
;
567 info
->nFileSizeHigh
= reply
.size_high
;
568 info
->nFileSizeLow
= reply
.size_low
;
569 info
->nNumberOfLinks
= reply
.links
;
570 info
->nFileIndexHigh
= reply
.index_high
;
571 info
->nFileIndexLow
= reply
.index_low
;
576 /**************************************************************************
577 * GetFileAttributes16 (KERNEL.420)
579 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
581 return GetFileAttributesA( name
);
585 /**************************************************************************
586 * GetFileAttributes32A (KERNEL32.217)
588 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
590 DOS_FULL_NAME full_name
;
591 BY_HANDLE_FILE_INFORMATION info
;
593 if (name
== NULL
|| *name
=='\0') return -1;
595 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
596 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
597 return info
.dwFileAttributes
;
601 /**************************************************************************
602 * GetFileAttributes32W (KERNEL32.218)
604 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
606 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
607 DWORD res
= GetFileAttributesA( nameA
);
608 HeapFree( GetProcessHeap(), 0, nameA
);
613 /***********************************************************************
614 * GetFileSize (KERNEL32.220)
616 DWORD WINAPI
GetFileSize( HFILE hFile
, LPDWORD filesizehigh
)
618 BY_HANDLE_FILE_INFORMATION info
;
619 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
620 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
621 return info
.nFileSizeLow
;
625 /***********************************************************************
626 * GetFileTime (KERNEL32.221)
628 BOOL WINAPI
GetFileTime( HFILE hFile
, FILETIME
*lpCreationTime
,
629 FILETIME
*lpLastAccessTime
,
630 FILETIME
*lpLastWriteTime
)
632 BY_HANDLE_FILE_INFORMATION info
;
633 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
634 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
635 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
636 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
640 /***********************************************************************
641 * CompareFileTime (KERNEL32.28)
643 INT WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
645 if (!x
|| !y
) return -1;
647 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
649 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
651 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
653 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
659 /***********************************************************************
660 * GetTempFileName16 (KERNEL.97)
662 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
667 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
668 drive
|= DRIVE_GetCurrentDrive() + 'A';
670 if ((drive
& TF_FORCEDRIVE
) &&
671 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
673 drive
&= ~TF_FORCEDRIVE
;
674 WARN(file
, "invalid drive %d specified\n", drive
);
677 if (drive
& TF_FORCEDRIVE
)
678 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
680 GetTempPathA( 132, temppath
);
681 return (UINT16
)GetTempFileNameA( temppath
, prefix
, unique
, buffer
);
685 /***********************************************************************
686 * GetTempFileName32A (KERNEL32.290)
688 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
,
691 static UINT unique_temp
;
692 DOS_FULL_NAME full_name
;
697 if ( !path
|| !prefix
|| !buffer
) return 0;
699 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
700 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
702 strcpy( buffer
, path
);
703 p
= buffer
+ strlen(buffer
);
705 /* add a \, if there isn't one and path is more than just the drive letter ... */
706 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
707 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
710 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
711 sprintf( p
, "%04x.tmp", num
);
713 /* Now try to create it */
719 HFILE handle
= CreateFileA( buffer
, GENERIC_WRITE
, 0, NULL
,
720 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, -1 );
721 if (handle
!= INVALID_HANDLE_VALUE
)
722 { /* We created it */
723 TRACE(file
, "created %s\n",
725 CloseHandle( handle
);
728 if (GetLastError() != ERROR_FILE_EXISTS
)
729 break; /* No need to go on */
731 sprintf( p
, "%04x.tmp", num
);
732 } while (num
!= (unique
& 0xffff));
735 /* Get the full path name */
737 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
739 /* Check if we have write access in the directory */
740 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
741 if (access( full_name
.long_name
, W_OK
) == -1)
742 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
745 TRACE(file
, "returning %s\n", buffer
);
746 return unique
? unique
: num
;
750 /***********************************************************************
751 * GetTempFileName32W (KERNEL32.291)
753 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
,
761 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
762 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
763 ret
= GetTempFileNameA( patha
, prefixa
, unique
, buffera
);
764 lstrcpyAtoW( buffer
, buffera
);
765 HeapFree( GetProcessHeap(), 0, patha
);
766 HeapFree( GetProcessHeap(), 0, prefixa
);
771 /***********************************************************************
774 * Implementation of OpenFile16() and OpenFile32().
776 static HFILE
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
,
781 WORD filedatetime
[2];
782 DOS_FULL_NAME full_name
;
783 DWORD access
, sharing
;
786 if (!ofs
) return HFILE_ERROR
;
788 ofs
->cBytes
= sizeof(OFSTRUCT
);
790 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
793 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
797 TRACE(file
, "%s %04x\n", name
, mode
);
799 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
800 Are there any cases where getting the path here is wrong?
801 Uwe Bonnes 1997 Apr 2 */
802 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
),
803 ofs
->szPathName
, NULL
)) goto error
;
804 FILE_ConvertOFMode( mode
, &access
, &sharing
);
806 /* OF_PARSE simply fills the structure */
810 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
812 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
813 name
, ofs
->szPathName
);
817 /* OF_CREATE is completely different from all other options, so
820 if (mode
& OF_CREATE
)
822 if ((hFileRet
= CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
823 sharing
, NULL
, CREATE_ALWAYS
,
824 FILE_ATTRIBUTE_NORMAL
, -1 ))== INVALID_HANDLE_VALUE
)
829 /* If OF_SEARCH is set, ignore the given path */
831 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
833 /* First try the file name as is */
834 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
835 /* Now remove the path */
836 if (name
[0] && (name
[1] == ':')) name
+= 2;
837 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
838 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
839 if (!name
[0]) goto not_found
;
842 /* Now look for the file */
844 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
847 TRACE(file
, "found %s = %s\n",
848 full_name
.long_name
, full_name
.short_name
);
849 lstrcpynA( ofs
->szPathName
, full_name
.short_name
,
850 sizeof(ofs
->szPathName
) );
852 if (mode
& OF_SHARE_EXCLUSIVE
)
853 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
854 on the file <tempdir>/_ins0432._mp to determine how
855 far installation has proceeded.
856 _ins0432._mp is an executable and while running the
857 application expects the open with OF_SHARE_ to fail*/
859 As our loader closes the files after loading the executable,
860 we can't find the running executable with FILE_InUse.
861 Perhaps the loader should keep the file open.
862 Recheck against how Win handles that case */
864 char *last
= strrchr(full_name
.long_name
,'/');
866 last
= full_name
.long_name
- 1;
867 if (GetModuleHandle16(last
+1))
869 TRACE(file
,"Denying shared open for %s\n",full_name
.long_name
);
874 if (mode
& OF_DELETE
)
876 if (unlink( full_name
.long_name
) == -1) goto not_found
;
877 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
881 hFileRet
= FILE_CreateFile( full_name
.long_name
, access
, sharing
,
882 NULL
, OPEN_EXISTING
, 0, -1 );
883 if (hFileRet
== HFILE_ERROR
) goto not_found
;
885 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
886 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
887 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
889 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
891 CloseHandle( hFileRet
);
892 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
893 /* FIXME: what error here? */
894 SetLastError( ERROR_FILE_NOT_FOUND
);
898 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
900 success
: /* We get here if the open was successful */
901 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
904 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
905 CloseHandle( hFileRet
);
909 hFileRet
= FILE_AllocDosHandle( hFileRet
);
910 if (hFileRet
== HFILE_ERROR16
) goto error
;
911 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
912 _lclose16( hFileRet
);
916 not_found
: /* We get here if the file does not exist */
917 WARN(file
, "'%s' not found\n", name
);
918 SetLastError( ERROR_FILE_NOT_FOUND
);
921 error
: /* We get here if there was an error opening the file */
922 ofs
->nErrCode
= GetLastError();
923 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
924 name
,ofs
->nErrCode
);
929 /***********************************************************************
930 * OpenFile16 (KERNEL.74)
932 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
934 return FILE_DoOpenFile( name
, ofs
, mode
, FALSE
);
938 /***********************************************************************
939 * OpenFile32 (KERNEL32.396)
941 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
943 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
947 /***********************************************************************
948 * FILE_InitProcessDosHandles
950 * Allocates the default DOS handles for a process. Called either by
951 * AllocDosHandle below or by the DOSVM stuff.
953 BOOL
FILE_InitProcessDosHandles( void ) {
956 if (!(ptr
= HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
,
957 sizeof(*ptr
) * DOS_TABLE_SIZE
)))
959 PROCESS_Current()->dos_handles
= ptr
;
960 ptr
[0] = GetStdHandle(STD_INPUT_HANDLE
);
961 ptr
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
962 ptr
[2] = GetStdHandle(STD_ERROR_HANDLE
);
963 ptr
[3] = GetStdHandle(STD_ERROR_HANDLE
);
964 ptr
[4] = GetStdHandle(STD_ERROR_HANDLE
);
968 /***********************************************************************
969 * FILE_AllocDosHandle
971 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
972 * longer valid after this function (even on failure).
974 HFILE16
FILE_AllocDosHandle( HANDLE handle
)
977 HANDLE
*ptr
= PROCESS_Current()->dos_handles
;
979 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
980 return INVALID_HANDLE_VALUE16
;
983 if (!FILE_InitProcessDosHandles())
985 ptr
= PROCESS_Current()->dos_handles
;
988 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++, ptr
++)
992 TRACE( file
, "Got %d for h32 %d\n", i
, handle
);
996 CloseHandle( handle
);
997 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
998 return INVALID_HANDLE_VALUE16
;
1002 /***********************************************************************
1005 * Return the Win32 handle for a DOS handle.
1007 HANDLE
FILE_GetHandle( HFILE16 hfile
)
1009 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1010 if ((hfile
>= DOS_TABLE_SIZE
) || !table
|| !table
[hfile
])
1012 SetLastError( ERROR_INVALID_HANDLE
);
1013 return INVALID_HANDLE_VALUE
;
1015 return table
[hfile
];
1019 /***********************************************************************
1022 * dup2() function for DOS handles.
1024 HFILE16
FILE_Dup2( HFILE16 hFile1
, HFILE16 hFile2
)
1026 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1029 if ((hFile1
>= DOS_TABLE_SIZE
) || (hFile2
>= DOS_TABLE_SIZE
) ||
1030 !table
|| !table
[hFile1
])
1032 SetLastError( ERROR_INVALID_HANDLE
);
1033 return HFILE_ERROR16
;
1037 FIXME( file
, "stdio handle closed, need proper conversion\n" );
1038 SetLastError( ERROR_INVALID_HANDLE
);
1039 return HFILE_ERROR16
;
1041 if (!DuplicateHandle( GetCurrentProcess(), table
[hFile1
],
1042 GetCurrentProcess(), &new_handle
,
1043 0, FALSE
, DUPLICATE_SAME_ACCESS
))
1044 return HFILE_ERROR16
;
1045 if (table
[hFile2
]) CloseHandle( table
[hFile2
] );
1046 table
[hFile2
] = new_handle
;
1051 /***********************************************************************
1052 * _lclose16 (KERNEL.81)
1054 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1056 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1060 FIXME( file
, "stdio handle closed, need proper conversion\n" );
1061 SetLastError( ERROR_INVALID_HANDLE
);
1062 return HFILE_ERROR16
;
1064 if ((hFile
>= DOS_TABLE_SIZE
) || !table
|| !table
[hFile
])
1066 SetLastError( ERROR_INVALID_HANDLE
);
1067 return HFILE_ERROR16
;
1069 TRACE( file
, "%d (handle32=%d)\n", hFile
, table
[hFile
] );
1070 CloseHandle( table
[hFile
] );
1076 /***********************************************************************
1077 * _lclose32 (KERNEL32.592)
1079 HFILE WINAPI
_lclose( HFILE hFile
)
1081 TRACE(file
, "handle %d\n", hFile
);
1082 return CloseHandle( hFile
) ? 0 : HFILE_ERROR
;
1086 /***********************************************************************
1087 * ReadFile (KERNEL32.428)
1089 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
1090 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
1092 struct get_read_fd_request req
;
1093 int unix_handle
, result
;
1095 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToRead
);
1097 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
1098 if (!bytesToRead
) return TRUE
;
1101 CLIENT_SendRequest( REQ_GET_READ_FD
, -1, 1, &req
, sizeof(req
) );
1102 CLIENT_WaitReply( NULL
, &unix_handle
, 0 );
1103 if (unix_handle
== -1) return FALSE
;
1104 while ((result
= read( unix_handle
, buffer
, bytesToRead
)) == -1)
1106 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1107 if ((errno
== EFAULT
) && VIRTUAL_HandleFault( buffer
)) continue;
1111 close( unix_handle
);
1112 if (result
== -1) return FALSE
;
1113 if (bytesRead
) *bytesRead
= result
;
1118 /***********************************************************************
1119 * WriteFile (KERNEL32.578)
1121 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
1122 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
1124 struct get_write_fd_request req
;
1125 int unix_handle
, result
;
1127 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToWrite
);
1129 if (bytesWritten
) *bytesWritten
= 0; /* Do this before anything else */
1130 if (!bytesToWrite
) return TRUE
;
1133 CLIENT_SendRequest( REQ_GET_WRITE_FD
, -1, 1, &req
, sizeof(req
) );
1134 CLIENT_WaitReply( NULL
, &unix_handle
, 0 );
1135 if (unix_handle
== -1) return FALSE
;
1136 while ((result
= write( unix_handle
, buffer
, bytesToWrite
)) == -1)
1138 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1139 if ((errno
== EFAULT
) && VIRTUAL_HandleFault( buffer
)) continue;
1143 close( unix_handle
);
1144 if (result
== -1) return FALSE
;
1145 if (bytesWritten
) *bytesWritten
= result
;
1150 /***********************************************************************
1153 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1157 TRACE(file
, "%d %08lx %ld\n",
1158 hFile
, (DWORD
)buffer
, count
);
1160 /* Some programs pass a count larger than the allocated buffer */
1161 maxlen
= GetSelectorLimit16( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1162 if (count
> maxlen
) count
= maxlen
;
1163 return _lread(FILE_GetHandle(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1167 /***********************************************************************
1170 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1172 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1176 /***********************************************************************
1177 * _lread32 (KERNEL32.596)
1179 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
1182 if (!ReadFile( handle
, buffer
, count
, &result
, NULL
)) return -1;
1187 /***********************************************************************
1188 * _lread16 (KERNEL.82)
1190 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1192 return (UINT16
)_lread(FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1196 /***********************************************************************
1197 * _lcreat16 (KERNEL.83)
1199 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1201 return FILE_AllocDosHandle( _lcreat( path
, attr
) );
1205 /***********************************************************************
1206 * _lcreat (KERNEL32.593)
1208 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
1210 /* Mask off all flags not explicitly allowed by the doc */
1211 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
1212 TRACE(file
, "%s %02x\n", path
, attr
);
1213 return CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
1214 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1215 CREATE_ALWAYS
, attr
, -1 );
1219 /***********************************************************************
1220 * SetFilePointer (KERNEL32.492)
1222 DWORD WINAPI
SetFilePointer( HFILE hFile
, LONG distance
, LONG
*highword
,
1225 struct set_file_pointer_request req
;
1226 struct set_file_pointer_reply reply
;
1228 if (highword
&& *highword
)
1230 FIXME(file
, "64-bit offsets not supported yet\n");
1231 SetLastError( ERROR_INVALID_PARAMETER
);
1234 TRACE(file
, "handle %d offset %ld origin %ld\n",
1235 hFile
, distance
, method
);
1239 req
.high
= highword
? *highword
: 0;
1240 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1241 req
.whence
= method
;
1242 CLIENT_SendRequest( REQ_SET_FILE_POINTER
, -1, 1, &req
, sizeof(req
) );
1243 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return 0xffffffff;
1245 if (highword
) *highword
= reply
.high
;
1250 /***********************************************************************
1251 * _llseek16 (KERNEL.84)
1254 * Seeking before the start of the file should be allowed for _llseek16,
1255 * but cause subsequent I/O operations to fail (cf. interrupt list)
1258 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1260 return SetFilePointer( FILE_GetHandle(hFile
), lOffset
, NULL
, nOrigin
);
1264 /***********************************************************************
1265 * _llseek32 (KERNEL32.594)
1267 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
1269 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1273 /***********************************************************************
1274 * _lopen16 (KERNEL.85)
1276 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1278 return FILE_AllocDosHandle( _lopen( path
, mode
) );
1282 /***********************************************************************
1283 * _lopen32 (KERNEL32.595)
1285 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
1287 DWORD access
, sharing
;
1289 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1290 FILE_ConvertOFMode( mode
, &access
, &sharing
);
1291 return CreateFileA( path
, access
, sharing
, NULL
, OPEN_EXISTING
, 0, -1 );
1295 /***********************************************************************
1296 * _lwrite16 (KERNEL.86)
1298 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1300 return (UINT16
)_hwrite( FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1303 /***********************************************************************
1304 * _lwrite32 (KERNEL32.761)
1306 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
1308 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
1312 /***********************************************************************
1313 * _hread16 (KERNEL.349)
1315 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1317 return _lread( FILE_GetHandle(hFile
), buffer
, count
);
1321 /***********************************************************************
1322 * _hread32 (KERNEL32.590)
1324 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
1326 return _lread( hFile
, buffer
, count
);
1330 /***********************************************************************
1331 * _hwrite16 (KERNEL.350)
1333 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1335 return _hwrite( FILE_GetHandle(hFile
), buffer
, count
);
1339 /***********************************************************************
1340 * _hwrite32 (KERNEL32.591)
1342 * experimentation yields that _lwrite:
1343 * o truncates the file at the current position with
1345 * o returns 0 on a 0 length write
1346 * o works with console handles
1349 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
1353 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1357 /* Expand or truncate at current position */
1358 if (!SetEndOfFile( handle
)) return HFILE_ERROR
;
1361 if (!WriteFile( handle
, buffer
, count
, &result
, NULL
))
1367 /***********************************************************************
1368 * SetHandleCount16 (KERNEL.199)
1370 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1372 HGLOBAL16 hPDB
= GetCurrentPDB16();
1373 PDB16
*pdb
= (PDB16
*)GlobalLock16( hPDB
);
1374 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1376 TRACE(file
, "(%d)\n", count
);
1378 if (count
< 20) count
= 20; /* No point in going below 20 */
1379 else if (count
> 254) count
= 254;
1383 if (pdb
->nbFiles
> 20)
1385 memcpy( pdb
->fileHandles
, files
, 20 );
1386 GlobalFree16( pdb
->hFileHandles
);
1387 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1388 GlobalHandleToSel16( hPDB
) );
1389 pdb
->hFileHandles
= 0;
1393 else /* More than 20, need a new file handles table */
1396 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1399 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1400 return pdb
->nbFiles
;
1402 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1404 if (count
> pdb
->nbFiles
)
1406 memcpy( newfiles
, files
, pdb
->nbFiles
);
1407 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1409 else memcpy( newfiles
, files
, count
);
1410 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1411 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1412 pdb
->hFileHandles
= newhandle
;
1413 pdb
->nbFiles
= count
;
1415 return pdb
->nbFiles
;
1419 /*************************************************************************
1420 * SetHandleCount32 (KERNEL32.494)
1422 UINT WINAPI
SetHandleCount( UINT count
)
1424 return MIN( 256, count
);
1428 /***********************************************************************
1429 * FlushFileBuffers (KERNEL32.133)
1431 BOOL WINAPI
FlushFileBuffers( HFILE hFile
)
1433 struct flush_file_request req
;
1436 CLIENT_SendRequest( REQ_FLUSH_FILE
, -1, 1, &req
, sizeof(req
) );
1437 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1441 /**************************************************************************
1442 * SetEndOfFile (KERNEL32.483)
1444 BOOL WINAPI
SetEndOfFile( HFILE hFile
)
1446 struct truncate_file_request req
;
1449 CLIENT_SendRequest( REQ_TRUNCATE_FILE
, -1, 1, &req
, sizeof(req
) );
1450 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1454 /***********************************************************************
1455 * DeleteFile16 (KERNEL.146)
1457 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1459 return DeleteFileA( path
);
1463 /***********************************************************************
1464 * DeleteFile32A (KERNEL32.71)
1466 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1468 DOS_FULL_NAME full_name
;
1470 TRACE(file
, "'%s'\n", path
);
1474 ERR(file
, "Empty path passed\n");
1477 if (DOSFS_GetDevice( path
))
1479 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1480 SetLastError( ERROR_FILE_NOT_FOUND
);
1484 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1485 if (unlink( full_name
.long_name
) == -1)
1494 /***********************************************************************
1495 * DeleteFile32W (KERNEL32.72)
1497 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1499 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1500 BOOL ret
= DeleteFileA( xpath
);
1501 HeapFree( GetProcessHeap(), 0, xpath
);
1506 /***********************************************************************
1509 LPVOID
FILE_dommap( int unix_handle
, LPVOID start
,
1510 DWORD size_high
, DWORD size_low
,
1511 DWORD offset_high
, DWORD offset_low
,
1512 int prot
, int flags
)
1518 if (size_high
|| offset_high
)
1519 FIXME(file
, "offsets larger than 4Gb not supported\n");
1521 if (unix_handle
== -1)
1526 static int fdzero
= -1;
1530 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1532 perror( "/dev/zero: open" );
1537 #endif /* MAP_ANON */
1538 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1540 flags
&= ~MAP_SHARED
;
1543 flags
|= MAP_PRIVATE
;
1546 else fd
= unix_handle
;
1548 if ((ret
= mmap( start
, size_low
, prot
,
1549 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1552 /* mmap() failed; if this is because the file offset is not */
1553 /* page-aligned (EINVAL), or because the underlying filesystem */
1554 /* does not support mmap() (ENOEXEC), we do it by hand. */
1556 if (unix_handle
== -1) return ret
;
1557 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1558 if (prot
& PROT_WRITE
)
1560 /* We cannot fake shared write mappings */
1562 if (flags
& MAP_SHARED
) return ret
;
1565 if (!(flags
& MAP_PRIVATE
)) return ret
;
1568 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1569 /* Reserve the memory with an anonymous mmap */
1570 ret
= FILE_dommap( -1, start
, size_high
, size_low
, 0, 0,
1571 PROT_READ
| PROT_WRITE
, flags
);
1572 if (ret
== (LPVOID
)-1) return ret
;
1573 /* Now read in the file */
1574 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1576 FILE_munmap( ret
, size_high
, size_low
);
1579 read( fd
, ret
, size_low
);
1580 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1581 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1586 /***********************************************************************
1589 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1592 FIXME(file
, "offsets larger than 4Gb not supported\n");
1593 return munmap( start
, size_low
);
1597 /***********************************************************************
1598 * GetFileType (KERNEL32.222)
1600 DWORD WINAPI
GetFileType( HFILE hFile
)
1602 struct get_file_info_request req
;
1603 struct get_file_info_reply reply
;
1606 CLIENT_SendRequest( REQ_GET_FILE_INFO
, -1, 1, &req
, sizeof(req
) );
1607 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
))
1608 return FILE_TYPE_UNKNOWN
;
1613 /**************************************************************************
1614 * MoveFileEx32A (KERNEL32.???)
1616 BOOL WINAPI
MoveFileExA( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1618 DOS_FULL_NAME full_name1
, full_name2
;
1619 int mode
=0; /* mode == 1: use copy */
1621 TRACE(file
, "(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1623 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1624 if (fn2
) { /* !fn2 means delete fn1 */
1625 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1626 /* Source name and target path are valid */
1627 if ( full_name1
.drive
!= full_name2
.drive
)
1629 /* use copy, if allowed */
1630 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1631 /* FIXME: Use right error code */
1632 SetLastError( ERROR_FILE_EXISTS
);
1637 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1638 /* target exists, check if we may overwrite */
1639 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1640 /* FIXME: Use right error code */
1641 SetLastError( ERROR_ACCESS_DENIED
);
1645 else /* fn2 == NULL means delete source */
1646 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1647 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1648 WARN(file
, "Illegal flag\n");
1649 SetLastError( ERROR_GEN_FAILURE
);
1652 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1653 Perhaps we should queue these command and execute it
1654 when exiting... What about using on_exit(2)
1656 FIXME(file
, "Please delete file '%s' when Wine has finished\n",
1657 full_name1
.long_name
);
1660 else if (unlink( full_name1
.long_name
) == -1)
1665 else return TRUE
; /* successfully deleted */
1667 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1668 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1669 Perhaps we should queue these command and execute it
1670 when exiting... What about using on_exit(2)
1672 FIXME(file
,"Please move existing file '%s' to file '%s'"
1673 "when Wine has finished\n",
1674 full_name1
.long_name
, full_name2
.long_name
);
1678 if (!mode
) /* move the file */
1679 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1685 else /* copy File */
1686 return CopyFileA(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1690 /**************************************************************************
1691 * MoveFileEx32W (KERNEL32.???)
1693 BOOL WINAPI
MoveFileExW( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1695 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1696 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1697 BOOL res
= MoveFileExA( afn1
, afn2
, flag
);
1698 HeapFree( GetProcessHeap(), 0, afn1
);
1699 HeapFree( GetProcessHeap(), 0, afn2
);
1704 /**************************************************************************
1705 * MoveFile32A (KERNEL32.387)
1707 * Move file or directory
1709 BOOL WINAPI
MoveFileA( LPCSTR fn1
, LPCSTR fn2
)
1711 DOS_FULL_NAME full_name1
, full_name2
;
1714 TRACE(file
, "(%s,%s)\n", fn1
, fn2
);
1716 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1717 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1718 /* The new name must not already exist */
1720 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1722 if (full_name1
.drive
== full_name2
.drive
) /* move */
1723 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1730 if (stat( full_name1
.long_name
, &fstat
))
1732 WARN(file
, "Invalid source file %s\n",
1733 full_name1
.long_name
);
1737 if (S_ISDIR(fstat
.st_mode
)) {
1738 /* No Move for directories across file systems */
1739 /* FIXME: Use right error code */
1740 SetLastError( ERROR_GEN_FAILURE
);
1744 return CopyFileA(fn1
, fn2
, TRUE
); /*fail, if exist */
1749 /**************************************************************************
1750 * MoveFile32W (KERNEL32.390)
1752 BOOL WINAPI
MoveFileW( LPCWSTR fn1
, LPCWSTR fn2
)
1754 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1755 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1756 BOOL res
= MoveFileA( afn1
, afn2
);
1757 HeapFree( GetProcessHeap(), 0, afn1
);
1758 HeapFree( GetProcessHeap(), 0, afn2
);
1763 /**************************************************************************
1764 * CopyFile32A (KERNEL32.36)
1766 BOOL WINAPI
CopyFileA( LPCSTR source
, LPCSTR dest
, BOOL fail_if_exists
)
1769 BY_HANDLE_FILE_INFORMATION info
;
1775 if ((h1
= _lopen( source
, OF_READ
)) == HFILE_ERROR
) return FALSE
;
1776 if (!GetFileInformationByHandle( h1
, &info
))
1781 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1782 if ((h2
= CreateFileA( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1783 fail_if_exists
? CREATE_NEW
: CREATE_ALWAYS
,
1784 info
.dwFileAttributes
, h1
)) == HFILE_ERROR
)
1789 while ((count
= _lread( h1
, buffer
, sizeof(buffer
) )) > 0)
1794 INT res
= _lwrite( h2
, p
, count
);
1795 if (res
<= 0) goto done
;
1808 /**************************************************************************
1809 * CopyFile32W (KERNEL32.37)
1811 BOOL WINAPI
CopyFileW( LPCWSTR source
, LPCWSTR dest
, BOOL fail_if_exists
)
1813 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1814 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1815 BOOL ret
= CopyFileA( sourceA
, destA
, fail_if_exists
);
1816 HeapFree( GetProcessHeap(), 0, sourceA
);
1817 HeapFree( GetProcessHeap(), 0, destA
);
1822 /**************************************************************************
1823 * CopyFileEx32A (KERNEL32.858)
1825 * This implementation ignores most of the extra parameters passed-in into
1826 * the "ex" version of the method and calls the CopyFile method.
1827 * It will have to be fixed eventually.
1829 BOOL WINAPI
CopyFileExA(LPCSTR sourceFilename
,
1830 LPCSTR destFilename
,
1831 LPPROGRESS_ROUTINE progressRoutine
,
1833 LPBOOL cancelFlagPointer
,
1836 BOOL failIfExists
= FALSE
;
1839 * Interpret the only flag that CopyFile can interpret.
1841 if ( (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0)
1843 failIfExists
= TRUE
;
1846 return CopyFileA(sourceFilename
, destFilename
, failIfExists
);
1849 /**************************************************************************
1850 * CopyFileEx32W (KERNEL32.859)
1852 BOOL WINAPI
CopyFileExW(LPCWSTR sourceFilename
,
1853 LPCWSTR destFilename
,
1854 LPPROGRESS_ROUTINE progressRoutine
,
1856 LPBOOL cancelFlagPointer
,
1859 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename
);
1860 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename
);
1862 BOOL ret
= CopyFileExA(sourceA
,
1869 HeapFree( GetProcessHeap(), 0, sourceA
);
1870 HeapFree( GetProcessHeap(), 0, destA
);
1876 /***********************************************************************
1877 * SetFileTime (KERNEL32.650)
1879 BOOL WINAPI
SetFileTime( HFILE hFile
,
1880 const FILETIME
*lpCreationTime
,
1881 const FILETIME
*lpLastAccessTime
,
1882 const FILETIME
*lpLastWriteTime
)
1884 struct set_file_time_request req
;
1887 if (lpLastAccessTime
)
1888 req
.access_time
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1890 req
.access_time
= 0; /* FIXME */
1891 if (lpLastWriteTime
)
1892 req
.write_time
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1894 req
.write_time
= 0; /* FIXME */
1896 CLIENT_SendRequest( REQ_SET_FILE_TIME
, -1, 1, &req
, sizeof(req
) );
1897 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1901 /**************************************************************************
1902 * LockFile (KERNEL32.511)
1904 BOOL WINAPI
LockFile( HFILE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1905 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
)
1907 struct lock_file_request req
;
1910 req
.offset_low
= dwFileOffsetLow
;
1911 req
.offset_high
= dwFileOffsetHigh
;
1912 req
.count_low
= nNumberOfBytesToLockLow
;
1913 req
.count_high
= nNumberOfBytesToLockHigh
;
1914 CLIENT_SendRequest( REQ_LOCK_FILE
, -1, 1, &req
, sizeof(req
) );
1915 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1919 /**************************************************************************
1920 * UnlockFile (KERNEL32.703)
1922 BOOL WINAPI
UnlockFile( HFILE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1923 DWORD nNumberOfBytesToUnlockLow
, DWORD nNumberOfBytesToUnlockHigh
)
1925 struct unlock_file_request req
;
1928 req
.offset_low
= dwFileOffsetLow
;
1929 req
.offset_high
= dwFileOffsetHigh
;
1930 req
.count_low
= nNumberOfBytesToUnlockLow
;
1931 req
.count_high
= nNumberOfBytesToUnlockHigh
;
1932 CLIENT_SendRequest( REQ_UNLOCK_FILE
, -1, 1, &req
, sizeof(req
) );
1933 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1939 struct DOS_FILE_LOCK
{
1940 struct DOS_FILE_LOCK
* next
;
1944 FILE_OBJECT
* dos_file
;
1945 /* char * unix_name;*/
1948 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
1950 static DOS_FILE_LOCK
*locks
= NULL
;
1951 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
1954 /* Locks need to be mirrored because unix file locking is based
1955 * on the pid. Inside of wine there can be multiple WINE processes
1956 * that share the same unix pid.
1957 * Read's and writes should check these locks also - not sure
1958 * how critical that is at this point (FIXME).
1961 static BOOL
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
1963 DOS_FILE_LOCK
*curr
;
1966 processId
= GetCurrentProcessId();
1968 /* check if lock overlaps a current lock for the same file */
1970 for (curr
= locks
; curr
; curr
= curr
->next
) {
1971 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
1972 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
1973 return TRUE
;/* region is identic */
1974 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
1975 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
1976 /* region overlaps */
1983 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
1984 curr
->processId
= GetCurrentProcessId();
1985 curr
->base
= f
->l_start
;
1986 curr
->len
= f
->l_len
;
1987 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
1989 curr
->dos_file
= file
;
1994 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
1997 DOS_FILE_LOCK
**curr
;
2000 processId
= GetCurrentProcessId();
2003 if ((*curr
)->dos_file
== file
) {
2005 *curr
= (*curr
)->next
;
2006 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2007 HeapFree( SystemHeap
, 0, rem
);
2010 curr
= &(*curr
)->next
;
2014 static BOOL
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2017 DOS_FILE_LOCK
**curr
;
2020 processId
= GetCurrentProcessId();
2021 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2022 if ((*curr
)->processId
== processId
&&
2023 (*curr
)->dos_file
== file
&&
2024 (*curr
)->base
== f
->l_start
&&
2025 (*curr
)->len
== f
->l_len
) {
2026 /* this is the same lock */
2028 *curr
= (*curr
)->next
;
2029 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2030 HeapFree( SystemHeap
, 0, rem
);
2034 /* no matching lock found */
2039 /**************************************************************************
2040 * LockFile (KERNEL32.511)
2042 BOOL WINAPI
LockFile(
2043 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2044 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2049 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2050 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2051 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2053 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2054 FIXME(file
, "Unimplemented bytes > 32bits\n");
2058 f
.l_start
= dwFileOffsetLow
;
2059 f
.l_len
= nNumberOfBytesToLockLow
;
2060 f
.l_whence
= SEEK_SET
;
2064 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2066 /* shadow locks internally */
2067 if (!DOS_AddLock(file
, &f
)) {
2068 SetLastError( ERROR_LOCK_VIOLATION
);
2072 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2073 #ifdef USE_UNIX_LOCKS
2074 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2075 if (errno
== EACCES
|| errno
== EAGAIN
) {
2076 SetLastError( ERROR_LOCK_VIOLATION
);
2081 /* remove our internal copy of the lock */
2082 DOS_RemoveLock(file
, &f
);
2090 /**************************************************************************
2091 * UnlockFile (KERNEL32.703)
2093 BOOL WINAPI
UnlockFile(
2094 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2095 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2100 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2101 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2102 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2104 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2105 WARN(file
, "Unimplemented bytes > 32bits\n");
2109 f
.l_start
= dwFileOffsetLow
;
2110 f
.l_len
= nNumberOfBytesToUnlockLow
;
2111 f
.l_whence
= SEEK_SET
;
2115 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2117 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2119 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2120 #ifdef USE_UNIX_LOCKS
2121 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2130 /**************************************************************************
2131 * GetFileAttributesEx32A [KERNEL32.874]
2133 BOOL WINAPI
GetFileAttributesExA(
2134 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2135 LPVOID lpFileInformation
)
2137 DOS_FULL_NAME full_name
;
2138 BY_HANDLE_FILE_INFORMATION info
;
2140 if (lpFileName
== NULL
) return FALSE
;
2141 if (lpFileInformation
== NULL
) return FALSE
;
2143 if (fInfoLevelId
== GetFileExInfoStandard
) {
2144 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2145 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2146 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2147 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2149 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2150 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2151 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2152 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2153 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2154 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2157 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
2165 /**************************************************************************
2166 * GetFileAttributesEx32W [KERNEL32.875]
2168 BOOL WINAPI
GetFileAttributesExW(
2169 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2170 LPVOID lpFileInformation
)
2172 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2174 GetFileAttributesExA( nameA
, fInfoLevelId
, lpFileInformation
);
2175 HeapFree( GetProcessHeap(), 0, nameA
);