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"
48 DEFAULT_DEBUG_CHANNEL(file
)
50 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
51 #define MAP_ANON MAP_ANONYMOUS
54 /* Size of per-process table of DOS handles */
55 #define DOS_TABLE_SIZE 256
58 /***********************************************************************
61 * Convert OF_* mode into flags for CreateFile.
63 static void FILE_ConvertOFMode( INT mode
, DWORD
*access
, DWORD
*sharing
)
67 case OF_READ
: *access
= GENERIC_READ
; break;
68 case OF_WRITE
: *access
= GENERIC_WRITE
; break;
69 case OF_READWRITE
: *access
= GENERIC_READ
| GENERIC_WRITE
; break;
70 default: *access
= 0; break;
74 case OF_SHARE_EXCLUSIVE
: *sharing
= 0; break;
75 case OF_SHARE_DENY_WRITE
: *sharing
= FILE_SHARE_READ
; break;
76 case OF_SHARE_DENY_READ
: *sharing
= FILE_SHARE_WRITE
; break;
77 case OF_SHARE_DENY_NONE
:
79 default: *sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
85 /***********************************************************************
89 * oldmode[I] mode how file was first opened
90 * mode[I] mode how the file should get opened
95 * Look what we have to do with the given SHARE modes
97 * Ralph Brown's interrupt list gives following explication, I guess
98 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
100 * FIXME: Validate this function
101 ========from Ralph Brown's list =========
103 Values of DOS file sharing behavior:
104 | Second and subsequent Opens
105 First |Compat Deny Deny Deny Deny
106 Open | All Write Read None
107 |R W RW R W RW R W RW R W RW R W RW
108 - - - - -| - - - - - - - - - - - - - - - - -
109 Compat R |Y Y Y N N N 1 N N N N N 1 N N
110 W |Y Y Y N N N N N N N N N N N N
111 RW|Y Y Y N N N N N N N N N N N N
113 Deny R |C C C N N N N N N N N N N N N
114 All W |C C C N N N N N N N N N N N N
115 RW|C C C N N N N N N N N N N N N
117 Deny R |2 C C N N N Y N N N N N Y N N
118 Write W |C C C N N N N N N Y N N Y N N
119 RW|C C C N N N N N N N N N Y N N
121 Deny R |C C C N N N N Y N N N N N Y N
122 Read W |C C C N N N N N N N Y N N Y N
123 RW|C C C N N N N N N N N N N Y N
125 Deny R |2 C C N N N Y Y Y N N N Y Y Y
126 None W |C C C N N N N N N Y Y Y Y Y Y
127 RW|C C C N N N N N N N N N Y Y Y
128 Legend: Y = open succeeds, N = open fails with error code 05h
129 C = open fails, INT 24 generated
130 1 = open succeeds if file read-only, else fails with error code
131 2 = open succeeds if file read-only, else fails with INT 24
132 ========end of description from Ralph Brown's List =====
133 For every "Y" in the table we return FALSE
134 For every "N" we set the DOS_ERROR and return TRUE
135 For all other cases we barf,set the DOS_ERROR and return TRUE
138 static BOOL
FILE_ShareDeny( int mode
, int oldmode
)
140 int oldsharemode
= oldmode
& 0x70;
141 int sharemode
= mode
& 0x70;
142 int oldopenmode
= oldmode
& 3;
143 int openmode
= mode
& 3;
145 switch (oldsharemode
)
147 case OF_SHARE_COMPAT
:
148 if (sharemode
== OF_SHARE_COMPAT
) return FALSE
;
149 if (openmode
== OF_READ
) goto test_ro_err05
;
151 case OF_SHARE_EXCLUSIVE
:
152 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
154 case OF_SHARE_DENY_WRITE
:
155 if (openmode
!= OF_READ
)
157 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
162 case OF_SHARE_COMPAT
:
163 if (oldopenmode
== OF_READ
) goto test_ro_int24
;
165 case OF_SHARE_DENY_NONE
:
167 case OF_SHARE_DENY_WRITE
:
168 if (oldopenmode
== OF_READ
) return FALSE
;
169 case OF_SHARE_DENY_READ
:
170 if (oldopenmode
== OF_WRITE
) return FALSE
;
171 case OF_SHARE_EXCLUSIVE
:
176 case OF_SHARE_DENY_READ
:
177 if (openmode
!= OF_WRITE
)
179 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
184 case OF_SHARE_COMPAT
:
186 case OF_SHARE_DENY_NONE
:
188 case OF_SHARE_DENY_WRITE
:
189 if (oldopenmode
== OF_READ
) return FALSE
;
190 case OF_SHARE_DENY_READ
:
191 if (oldopenmode
== OF_WRITE
) return FALSE
;
192 case OF_SHARE_EXCLUSIVE
:
197 case OF_SHARE_DENY_NONE
:
200 case OF_SHARE_COMPAT
:
202 case OF_SHARE_DENY_NONE
:
204 case OF_SHARE_DENY_WRITE
:
205 if (oldopenmode
== OF_READ
) return FALSE
;
206 case OF_SHARE_DENY_READ
:
207 if (oldopenmode
== OF_WRITE
) return FALSE
;
208 case OF_SHARE_EXCLUSIVE
:
213 ERR(file
,"unknown mode\n");
215 ERR(file
,"shouldn't happen\n");
216 ERR(file
,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
220 if (oldmode
== OF_READ
)
224 FIXME(file
,"generate INT24 missing\n");
225 /* Is this the right error? */
226 SetLastError( ERROR_ACCESS_DENIED
);
230 if (oldmode
== OF_READ
)
234 TRACE(file
,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode
,mode
);
235 SetLastError( ERROR_ACCESS_DENIED
);
241 /***********************************************************************
244 * Set the DOS error code from errno.
246 void FILE_SetDosError(void)
248 int save_errno
= errno
; /* errno gets overwritten by printf */
250 TRACE(file
, "errno = %d %s\n", errno
, strerror(errno
));
254 SetLastError( ERROR_SHARING_VIOLATION
);
257 SetLastError( ERROR_INVALID_HANDLE
);
260 SetLastError( ERROR_HANDLE_DISK_FULL
);
265 SetLastError( ERROR_ACCESS_DENIED
);
268 SetLastError( ERROR_LOCK_VIOLATION
);
271 SetLastError( ERROR_FILE_NOT_FOUND
);
274 SetLastError( ERROR_CANNOT_MAKE
);
278 SetLastError( ERROR_NO_MORE_FILES
);
281 SetLastError( ERROR_FILE_EXISTS
);
285 SetLastError( ERROR_SEEK
);
288 SetLastError( ERROR_DIR_NOT_EMPTY
);
291 perror( "int21: unknown errno" );
292 SetLastError( ERROR_GEN_FAILURE
);
299 /***********************************************************************
302 * Duplicate a Unix handle into a task handle.
304 HFILE
FILE_DupUnixHandle( int fd
, DWORD access
)
307 struct alloc_file_handle_request
*req
= get_req_buffer();
309 if ((unix_handle
= dup(fd
)) == -1)
312 return INVALID_HANDLE_VALUE
;
314 req
->access
= access
;
315 server_call_fd( REQ_ALLOC_FILE_HANDLE
, unix_handle
, NULL
);
320 /***********************************************************************
323 * Implementation of CreateFile. Takes a Unix path name.
325 HFILE
FILE_CreateFile( LPCSTR filename
, DWORD access
, DWORD sharing
,
326 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
327 DWORD attributes
, HANDLE
template )
329 struct create_file_request
*req
= get_req_buffer();
331 req
->access
= access
;
332 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
333 req
->sharing
= sharing
;
334 req
->create
= creation
;
335 req
->attrs
= attributes
;
336 lstrcpynA( req
->name
, filename
, server_remaining(req
->name
) );
338 server_call( REQ_CREATE_FILE
);
340 /* If write access failed, retry without GENERIC_WRITE */
342 if ((req
->handle
== -1) && !Options
.failReadOnly
&&
343 (access
& GENERIC_WRITE
))
345 DWORD lasterror
= GetLastError();
346 if ((lasterror
== ERROR_ACCESS_DENIED
) || (lasterror
== ERROR_WRITE_PROTECT
))
347 return FILE_CreateFile( filename
, access
& ~GENERIC_WRITE
, sharing
,
348 sa
, creation
, attributes
, template );
354 /***********************************************************************
357 * Same as FILE_CreateFile but for a device
359 HFILE
FILE_CreateDevice( int client_id
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
361 struct create_device_request
*req
= get_req_buffer();
363 req
->access
= access
;
364 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
367 server_call( REQ_CREATE_DEVICE
);
372 /*************************************************************************
373 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
375 * Creates or opens an object, and returns a handle that can be used to
376 * access that object.
380 * filename [I] pointer to filename to be accessed
381 * access [I] access mode requested
382 * sharing [I] share mode
383 * sa [I] pointer to security attributes
384 * creation [I] how to create the file
385 * attributes [I] attributes for newly created file
386 * template [I] handle to file with extended attributes to copy
389 * Success: Open handle to specified file
390 * Failure: INVALID_HANDLE_VALUE
393 * Should call SetLastError() on failure.
397 * Doesn't support character devices, pipes, template files, or a
398 * lot of the 'attributes' flags yet.
400 HFILE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
401 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
402 DWORD attributes
, HANDLE
template )
404 DOS_FULL_NAME full_name
;
408 SetLastError( ERROR_INVALID_PARAMETER
);
411 TRACE(file
,"%s %s%s%s%s%s%s%s\n",filename
,
412 ((access
& GENERIC_READ
)==GENERIC_READ
)?"GENERIC_READ ":"",
413 ((access
& GENERIC_WRITE
)==GENERIC_WRITE
)?"GENERIC_WRITE ":"",
414 (!access
)?"QUERY_ACCESS ":"",
415 ((sharing
& FILE_SHARE_READ
)==FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
416 ((sharing
& FILE_SHARE_WRITE
)==FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
417 ((sharing
& FILE_SHARE_DELETE
)==FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
418 (creation
==CREATE_NEW
)?"CREATE_NEW":
419 (creation
==CREATE_ALWAYS
)?"CREATE_ALWAYS ":
420 (creation
==OPEN_EXISTING
)?"OPEN_EXISTING ":
421 (creation
==OPEN_ALWAYS
)?"OPEN_ALWAYS ":
422 (creation
==TRUNCATE_EXISTING
)?"TRUNCATE_EXISTING ":"");
424 /* If the name starts with '\\?\', ignore the first 4 chars. */
425 if (!strncmp(filename
, "\\\\?\\", 4))
428 if (!strncmp(filename
, "UNC\\", 4))
430 FIXME( file
, "UNC name (%s) not supported.\n", filename
);
431 SetLastError( ERROR_PATH_NOT_FOUND
);
436 if (!strncmp(filename
, "\\\\.\\", 4))
437 return DEVICE_Open( filename
+4, access
, sa
);
439 /* If the name still starts with '\\', it's a UNC name. */
440 if (!strncmp(filename
, "\\\\", 2))
442 FIXME( file
, "UNC name (%s) not supported.\n", filename
);
443 SetLastError( ERROR_PATH_NOT_FOUND
);
447 /* Open a console for CONIN$ or CONOUT$ */
448 if (!lstrcmpiA(filename
, "CONIN$")) return CONSOLE_OpenHandle( FALSE
, access
, sa
);
449 if (!lstrcmpiA(filename
, "CONOUT$")) return CONSOLE_OpenHandle( TRUE
, access
, sa
);
451 if (DOSFS_GetDevice( filename
))
455 TRACE(file
, "opening device '%s'\n", filename
);
457 if (HFILE_ERROR
!=(ret
=DOSFS_OpenDevice( filename
, access
)))
460 /* Do not silence this please. It is a critical error. -MM */
461 ERR(file
, "Couldn't open device '%s'!\n",filename
);
462 SetLastError( ERROR_FILE_NOT_FOUND
);
466 /* check for filename, don't check for last entry if creating */
467 if (!DOSFS_GetFullName( filename
,
468 (creation
== OPEN_EXISTING
) || (creation
== TRUNCATE_EXISTING
), &full_name
))
471 return FILE_CreateFile( full_name
.long_name
, access
, sharing
,
472 sa
, creation
, attributes
, template );
477 /*************************************************************************
478 * CreateFile32W (KERNEL32.48)
480 HFILE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
481 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
482 DWORD attributes
, HANDLE
template)
484 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
485 HFILE res
= CreateFileA( afn
, access
, sharing
, sa
, creation
, attributes
, template );
486 HeapFree( GetProcessHeap(), 0, afn
);
491 /***********************************************************************
494 * Fill a file information from a struct stat.
496 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
498 if (S_ISDIR(st
->st_mode
))
499 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
501 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
502 if (!(st
->st_mode
& S_IWUSR
))
503 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
505 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftCreationTime
, 0 );
506 DOSFS_UnixTimeToFileTime( st
->st_mtime
, &info
->ftLastWriteTime
, 0 );
507 DOSFS_UnixTimeToFileTime( st
->st_atime
, &info
->ftLastAccessTime
, 0 );
509 info
->dwVolumeSerialNumber
= 0; /* FIXME */
510 info
->nFileSizeHigh
= 0;
511 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
512 info
->nNumberOfLinks
= st
->st_nlink
;
513 info
->nFileIndexHigh
= 0;
514 info
->nFileIndexLow
= st
->st_ino
;
518 /***********************************************************************
521 * Stat a Unix path name. Return TRUE if OK.
523 BOOL
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
527 if (!unixName
|| !info
) return FALSE
;
529 if (stat( unixName
, &st
) == -1)
534 FILE_FillInfo( &st
, info
);
539 /***********************************************************************
540 * GetFileInformationByHandle (KERNEL32.219)
542 DWORD WINAPI
GetFileInformationByHandle( HFILE hFile
,
543 BY_HANDLE_FILE_INFORMATION
*info
)
545 struct get_file_info_request
*req
= get_req_buffer();
549 if (server_call( REQ_GET_FILE_INFO
)) return 0;
550 DOSFS_UnixTimeToFileTime( req
->write_time
, &info
->ftCreationTime
, 0 );
551 DOSFS_UnixTimeToFileTime( req
->write_time
, &info
->ftLastWriteTime
, 0 );
552 DOSFS_UnixTimeToFileTime( req
->access_time
, &info
->ftLastAccessTime
, 0 );
553 info
->dwFileAttributes
= req
->attr
;
554 info
->dwVolumeSerialNumber
= req
->serial
;
555 info
->nFileSizeHigh
= req
->size_high
;
556 info
->nFileSizeLow
= req
->size_low
;
557 info
->nNumberOfLinks
= req
->links
;
558 info
->nFileIndexHigh
= req
->index_high
;
559 info
->nFileIndexLow
= req
->index_low
;
564 /**************************************************************************
565 * GetFileAttributes16 (KERNEL.420)
567 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
569 return GetFileAttributesA( name
);
573 /**************************************************************************
574 * GetFileAttributes32A (KERNEL32.217)
576 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
578 DOS_FULL_NAME full_name
;
579 BY_HANDLE_FILE_INFORMATION info
;
581 if (name
== NULL
|| *name
=='\0') return -1;
583 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
584 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
585 return info
.dwFileAttributes
;
589 /**************************************************************************
590 * GetFileAttributes32W (KERNEL32.218)
592 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
594 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
595 DWORD res
= GetFileAttributesA( nameA
);
596 HeapFree( GetProcessHeap(), 0, nameA
);
601 /***********************************************************************
602 * GetFileSize (KERNEL32.220)
604 DWORD WINAPI
GetFileSize( HFILE hFile
, LPDWORD filesizehigh
)
606 BY_HANDLE_FILE_INFORMATION info
;
607 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
608 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
609 return info
.nFileSizeLow
;
613 /***********************************************************************
614 * GetFileTime (KERNEL32.221)
616 BOOL WINAPI
GetFileTime( HFILE hFile
, FILETIME
*lpCreationTime
,
617 FILETIME
*lpLastAccessTime
,
618 FILETIME
*lpLastWriteTime
)
620 BY_HANDLE_FILE_INFORMATION info
;
621 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
622 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
623 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
624 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
628 /***********************************************************************
629 * CompareFileTime (KERNEL32.28)
631 INT WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
633 if (!x
|| !y
) return -1;
635 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
637 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
639 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
641 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
647 /***********************************************************************
648 * GetTempFileName16 (KERNEL.97)
650 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
655 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
656 drive
|= DRIVE_GetCurrentDrive() + 'A';
658 if ((drive
& TF_FORCEDRIVE
) &&
659 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
661 drive
&= ~TF_FORCEDRIVE
;
662 WARN(file
, "invalid drive %d specified\n", drive
);
665 if (drive
& TF_FORCEDRIVE
)
666 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
668 GetTempPathA( 132, temppath
);
669 return (UINT16
)GetTempFileNameA( temppath
, prefix
, unique
, buffer
);
673 /***********************************************************************
674 * GetTempFileName32A (KERNEL32.290)
676 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
,
679 static UINT unique_temp
;
680 DOS_FULL_NAME full_name
;
685 if ( !path
|| !prefix
|| !buffer
) return 0;
687 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
688 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
690 strcpy( buffer
, path
);
691 p
= buffer
+ strlen(buffer
);
693 /* add a \, if there isn't one and path is more than just the drive letter ... */
694 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
695 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
698 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
699 sprintf( p
, "%04x.tmp", num
);
701 /* Now try to create it */
707 HFILE handle
= CreateFileA( buffer
, GENERIC_WRITE
, 0, NULL
,
708 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, -1 );
709 if (handle
!= INVALID_HANDLE_VALUE
)
710 { /* We created it */
711 TRACE(file
, "created %s\n",
713 CloseHandle( handle
);
716 if (GetLastError() != ERROR_FILE_EXISTS
)
717 break; /* No need to go on */
719 sprintf( p
, "%04x.tmp", num
);
720 } while (num
!= (unique
& 0xffff));
723 /* Get the full path name */
725 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
727 /* Check if we have write access in the directory */
728 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
729 if (access( full_name
.long_name
, W_OK
) == -1)
730 WARN(file
, "returns '%s', which doesn't seem to be writeable.\n",
733 TRACE(file
, "returning %s\n", buffer
);
734 return unique
? unique
: num
;
738 /***********************************************************************
739 * GetTempFileName32W (KERNEL32.291)
741 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
,
749 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
750 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
751 ret
= GetTempFileNameA( patha
, prefixa
, unique
, buffera
);
752 lstrcpyAtoW( buffer
, buffera
);
753 HeapFree( GetProcessHeap(), 0, patha
);
754 HeapFree( GetProcessHeap(), 0, prefixa
);
759 /***********************************************************************
762 * Implementation of OpenFile16() and OpenFile32().
764 static HFILE
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
,
769 WORD filedatetime
[2];
770 DOS_FULL_NAME full_name
;
771 DWORD access
, sharing
;
774 if (!ofs
) return HFILE_ERROR
;
776 TRACE(file
,"%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
777 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
778 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
779 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
780 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
781 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
782 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
783 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
784 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
785 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
786 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
787 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
788 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
789 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
790 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
791 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
792 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
793 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
797 ofs
->cBytes
= sizeof(OFSTRUCT
);
799 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
802 ERR(file
, "called with `name' set to NULL ! Please debug.\n");
806 TRACE(file
, "%s %04x\n", name
, mode
);
808 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
809 Are there any cases where getting the path here is wrong?
810 Uwe Bonnes 1997 Apr 2 */
811 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
),
812 ofs
->szPathName
, NULL
)) goto error
;
813 FILE_ConvertOFMode( mode
, &access
, &sharing
);
815 /* OF_PARSE simply fills the structure */
819 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
821 TRACE(file
, "(%s): OF_PARSE, res = '%s'\n",
822 name
, ofs
->szPathName
);
826 /* OF_CREATE is completely different from all other options, so
829 if (mode
& OF_CREATE
)
831 if ((hFileRet
= CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
832 sharing
, NULL
, CREATE_ALWAYS
,
833 FILE_ATTRIBUTE_NORMAL
, -1 ))== INVALID_HANDLE_VALUE
)
838 /* If OF_SEARCH is set, ignore the given path */
840 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
842 /* First try the file name as is */
843 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
844 /* Now remove the path */
845 if (name
[0] && (name
[1] == ':')) name
+= 2;
846 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
847 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
848 if (!name
[0]) goto not_found
;
851 /* Now look for the file */
853 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
856 TRACE(file
, "found %s = %s\n",
857 full_name
.long_name
, full_name
.short_name
);
858 lstrcpynA( ofs
->szPathName
, full_name
.short_name
,
859 sizeof(ofs
->szPathName
) );
861 if (mode
& OF_SHARE_EXCLUSIVE
)
862 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
863 on the file <tempdir>/_ins0432._mp to determine how
864 far installation has proceeded.
865 _ins0432._mp is an executable and while running the
866 application expects the open with OF_SHARE_ to fail*/
868 As our loader closes the files after loading the executable,
869 we can't find the running executable with FILE_InUse.
870 Perhaps the loader should keep the file open.
871 Recheck against how Win handles that case */
873 char *last
= strrchr(full_name
.long_name
,'/');
875 last
= full_name
.long_name
- 1;
876 if (GetModuleHandle16(last
+1))
878 TRACE(file
,"Denying shared open for %s\n",full_name
.long_name
);
883 if (mode
& OF_DELETE
)
885 if (unlink( full_name
.long_name
) == -1) goto not_found
;
886 TRACE(file
, "(%s): OF_DELETE return = OK\n", name
);
890 hFileRet
= FILE_CreateFile( full_name
.long_name
, access
, sharing
,
891 NULL
, OPEN_EXISTING
, 0, -1 );
892 if (hFileRet
== HFILE_ERROR
) goto not_found
;
894 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
895 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
896 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
898 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
900 CloseHandle( hFileRet
);
901 WARN(file
, "(%s): OF_VERIFY failed\n", name
);
902 /* FIXME: what error here? */
903 SetLastError( ERROR_FILE_NOT_FOUND
);
907 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
909 success
: /* We get here if the open was successful */
910 TRACE(file
, "(%s): OK, return = %d\n", name
, hFileRet
);
913 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
914 CloseHandle( hFileRet
);
918 hFileRet
= FILE_AllocDosHandle( hFileRet
);
919 if (hFileRet
== HFILE_ERROR16
) goto error
;
920 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
921 _lclose16( hFileRet
);
925 not_found
: /* We get here if the file does not exist */
926 WARN(file
, "'%s' not found\n", name
);
927 SetLastError( ERROR_FILE_NOT_FOUND
);
930 error
: /* We get here if there was an error opening the file */
931 ofs
->nErrCode
= GetLastError();
932 WARN(file
, "(%s): return = HFILE_ERROR error= %d\n",
933 name
,ofs
->nErrCode
);
938 /***********************************************************************
939 * OpenFile16 (KERNEL.74)
941 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
943 return FILE_DoOpenFile( name
, ofs
, mode
, FALSE
);
947 /***********************************************************************
948 * OpenFile32 (KERNEL32.396)
950 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
952 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
956 /***********************************************************************
957 * FILE_InitProcessDosHandles
959 * Allocates the default DOS handles for a process. Called either by
960 * AllocDosHandle below or by the DOSVM stuff.
962 BOOL
FILE_InitProcessDosHandles( void ) {
965 if (!(ptr
= HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
,
966 sizeof(*ptr
) * DOS_TABLE_SIZE
)))
968 PROCESS_Current()->dos_handles
= ptr
;
969 ptr
[0] = GetStdHandle(STD_INPUT_HANDLE
);
970 ptr
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
971 ptr
[2] = GetStdHandle(STD_ERROR_HANDLE
);
972 ptr
[3] = GetStdHandle(STD_ERROR_HANDLE
);
973 ptr
[4] = GetStdHandle(STD_ERROR_HANDLE
);
977 /***********************************************************************
978 * FILE_AllocDosHandle
980 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
981 * longer valid after this function (even on failure).
983 HFILE16
FILE_AllocDosHandle( HANDLE handle
)
986 HANDLE
*ptr
= PROCESS_Current()->dos_handles
;
988 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
989 return INVALID_HANDLE_VALUE16
;
992 if (!FILE_InitProcessDosHandles())
994 ptr
= PROCESS_Current()->dos_handles
;
997 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++, ptr
++)
1001 TRACE( file
, "Got %d for h32 %d\n", i
, handle
);
1005 CloseHandle( handle
);
1006 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
1007 return INVALID_HANDLE_VALUE16
;
1011 /***********************************************************************
1014 * Return the Win32 handle for a DOS handle.
1016 HANDLE
FILE_GetHandle( HFILE16 hfile
)
1018 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1019 if ((hfile
>= DOS_TABLE_SIZE
) || !table
|| !table
[hfile
])
1021 SetLastError( ERROR_INVALID_HANDLE
);
1022 return INVALID_HANDLE_VALUE
;
1024 return table
[hfile
];
1028 /***********************************************************************
1031 * dup2() function for DOS handles.
1033 HFILE16
FILE_Dup2( HFILE16 hFile1
, HFILE16 hFile2
)
1035 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1038 if ((hFile1
>= DOS_TABLE_SIZE
) || (hFile2
>= DOS_TABLE_SIZE
) ||
1039 !table
|| !table
[hFile1
])
1041 SetLastError( ERROR_INVALID_HANDLE
);
1042 return HFILE_ERROR16
;
1046 FIXME( file
, "stdio handle closed, need proper conversion\n" );
1047 SetLastError( ERROR_INVALID_HANDLE
);
1048 return HFILE_ERROR16
;
1050 if (!DuplicateHandle( GetCurrentProcess(), table
[hFile1
],
1051 GetCurrentProcess(), &new_handle
,
1052 0, FALSE
, DUPLICATE_SAME_ACCESS
))
1053 return HFILE_ERROR16
;
1054 if (table
[hFile2
]) CloseHandle( table
[hFile2
] );
1055 table
[hFile2
] = new_handle
;
1060 /***********************************************************************
1061 * _lclose16 (KERNEL.81)
1063 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1065 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1069 FIXME( file
, "stdio handle closed, need proper conversion\n" );
1070 SetLastError( ERROR_INVALID_HANDLE
);
1071 return HFILE_ERROR16
;
1073 if ((hFile
>= DOS_TABLE_SIZE
) || !table
|| !table
[hFile
])
1075 SetLastError( ERROR_INVALID_HANDLE
);
1076 return HFILE_ERROR16
;
1078 TRACE( file
, "%d (handle32=%d)\n", hFile
, table
[hFile
] );
1079 CloseHandle( table
[hFile
] );
1085 /***********************************************************************
1086 * _lclose32 (KERNEL32.592)
1088 HFILE WINAPI
_lclose( HFILE hFile
)
1090 TRACE(file
, "handle %d\n", hFile
);
1091 return CloseHandle( hFile
) ? 0 : HFILE_ERROR
;
1095 /***********************************************************************
1096 * ReadFile (KERNEL32.428)
1098 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
1099 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
1101 struct get_read_fd_request
*req
= get_req_buffer();
1102 int unix_handle
, result
;
1104 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToRead
);
1106 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
1107 if (!bytesToRead
) return TRUE
;
1109 req
->handle
= hFile
;
1110 server_call_fd( REQ_GET_READ_FD
, -1, &unix_handle
);
1111 if (unix_handle
== -1) return FALSE
;
1112 while ((result
= read( unix_handle
, buffer
, bytesToRead
)) == -1)
1114 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1115 if ((errno
== EFAULT
) && VIRTUAL_HandleFault( buffer
)) continue;
1119 close( unix_handle
);
1120 if (result
== -1) return FALSE
;
1121 if (bytesRead
) *bytesRead
= result
;
1126 /***********************************************************************
1127 * WriteFile (KERNEL32.578)
1129 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
1130 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
1132 struct get_write_fd_request
*req
= get_req_buffer();
1133 int unix_handle
, result
;
1135 TRACE(file
, "%d %p %ld\n", hFile
, buffer
, bytesToWrite
);
1137 if (bytesWritten
) *bytesWritten
= 0; /* Do this before anything else */
1138 if (!bytesToWrite
) return TRUE
;
1140 req
->handle
= hFile
;
1141 server_call_fd( REQ_GET_WRITE_FD
, -1, &unix_handle
);
1142 if (unix_handle
== -1) return FALSE
;
1143 while ((result
= write( unix_handle
, buffer
, bytesToWrite
)) == -1)
1145 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1146 if ((errno
== EFAULT
) && VIRTUAL_HandleFault( buffer
)) continue;
1147 if (errno
== ENOSPC
)
1148 SetLastError( ERROR_DISK_FULL
);
1153 close( unix_handle
);
1154 if (result
== -1) return FALSE
;
1155 if (bytesWritten
) *bytesWritten
= result
;
1160 /***********************************************************************
1163 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1167 TRACE(file
, "%d %08lx %ld\n",
1168 hFile
, (DWORD
)buffer
, count
);
1170 /* Some programs pass a count larger than the allocated buffer */
1171 maxlen
= GetSelectorLimit16( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1172 if (count
> maxlen
) count
= maxlen
;
1173 return _lread(FILE_GetHandle(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1177 /***********************************************************************
1180 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1182 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1186 /***********************************************************************
1187 * _lread32 (KERNEL32.596)
1189 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
1192 if (!ReadFile( handle
, buffer
, count
, &result
, NULL
)) return -1;
1197 /***********************************************************************
1198 * _lread16 (KERNEL.82)
1200 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1202 return (UINT16
)_lread(FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1206 /***********************************************************************
1207 * _lcreat16 (KERNEL.83)
1209 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1211 return FILE_AllocDosHandle( _lcreat( path
, attr
) );
1215 /***********************************************************************
1216 * _lcreat (KERNEL32.593)
1218 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
1220 /* Mask off all flags not explicitly allowed by the doc */
1221 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
1222 TRACE(file
, "%s %02x\n", path
, attr
);
1223 return CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
1224 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1225 CREATE_ALWAYS
, attr
, -1 );
1229 /***********************************************************************
1230 * SetFilePointer (KERNEL32.492)
1232 DWORD WINAPI
SetFilePointer( HFILE hFile
, LONG distance
, LONG
*highword
,
1235 struct set_file_pointer_request
*req
= get_req_buffer();
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
);
1246 req
->handle
= hFile
;
1247 req
->low
= distance
;
1248 req
->high
= highword
? *highword
: 0;
1249 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1250 req
->whence
= method
;
1252 if (server_call( REQ_SET_FILE_POINTER
)) return 0xffffffff;
1253 if (highword
) *highword
= req
->new_high
;
1254 return req
->new_low
;
1258 /***********************************************************************
1259 * _llseek16 (KERNEL.84)
1262 * Seeking before the start of the file should be allowed for _llseek16,
1263 * but cause subsequent I/O operations to fail (cf. interrupt list)
1266 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1268 return SetFilePointer( FILE_GetHandle(hFile
), lOffset
, NULL
, nOrigin
);
1272 /***********************************************************************
1273 * _llseek32 (KERNEL32.594)
1275 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
1277 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1281 /***********************************************************************
1282 * _lopen16 (KERNEL.85)
1284 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1286 return FILE_AllocDosHandle( _lopen( path
, mode
) );
1290 /***********************************************************************
1291 * _lopen32 (KERNEL32.595)
1293 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
1295 DWORD access
, sharing
;
1297 TRACE(file
, "('%s',%04x)\n", path
, mode
);
1298 FILE_ConvertOFMode( mode
, &access
, &sharing
);
1299 return CreateFileA( path
, access
, sharing
, NULL
, OPEN_EXISTING
, 0, -1 );
1303 /***********************************************************************
1304 * _lwrite16 (KERNEL.86)
1306 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1308 return (UINT16
)_hwrite( FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1311 /***********************************************************************
1312 * _lwrite32 (KERNEL32.761)
1314 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
1316 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
1320 /***********************************************************************
1321 * _hread16 (KERNEL.349)
1323 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1325 return _lread( FILE_GetHandle(hFile
), buffer
, count
);
1329 /***********************************************************************
1330 * _hread32 (KERNEL32.590)
1332 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
1334 return _lread( hFile
, buffer
, count
);
1338 /***********************************************************************
1339 * _hwrite16 (KERNEL.350)
1341 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1343 return _hwrite( FILE_GetHandle(hFile
), buffer
, count
);
1347 /***********************************************************************
1348 * _hwrite32 (KERNEL32.591)
1350 * experimentation yields that _lwrite:
1351 * o truncates the file at the current position with
1353 * o returns 0 on a 0 length write
1354 * o works with console handles
1357 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
1361 TRACE(file
, "%d %p %ld\n", handle
, buffer
, count
);
1365 /* Expand or truncate at current position */
1366 if (!SetEndOfFile( handle
)) return HFILE_ERROR
;
1369 if (!WriteFile( handle
, buffer
, count
, &result
, NULL
))
1375 /***********************************************************************
1376 * SetHandleCount16 (KERNEL.199)
1378 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1380 HGLOBAL16 hPDB
= GetCurrentPDB16();
1381 PDB16
*pdb
= (PDB16
*)GlobalLock16( hPDB
);
1382 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1384 TRACE(file
, "(%d)\n", count
);
1386 if (count
< 20) count
= 20; /* No point in going below 20 */
1387 else if (count
> 254) count
= 254;
1391 if (pdb
->nbFiles
> 20)
1393 memcpy( pdb
->fileHandles
, files
, 20 );
1394 GlobalFree16( pdb
->hFileHandles
);
1395 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1396 GlobalHandleToSel16( hPDB
) );
1397 pdb
->hFileHandles
= 0;
1401 else /* More than 20, need a new file handles table */
1404 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1407 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1408 return pdb
->nbFiles
;
1410 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1412 if (count
> pdb
->nbFiles
)
1414 memcpy( newfiles
, files
, pdb
->nbFiles
);
1415 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1417 else memcpy( newfiles
, files
, count
);
1418 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1419 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1420 pdb
->hFileHandles
= newhandle
;
1421 pdb
->nbFiles
= count
;
1423 return pdb
->nbFiles
;
1427 /*************************************************************************
1428 * SetHandleCount32 (KERNEL32.494)
1430 UINT WINAPI
SetHandleCount( UINT count
)
1432 return MIN( 256, count
);
1436 /***********************************************************************
1437 * FlushFileBuffers (KERNEL32.133)
1439 BOOL WINAPI
FlushFileBuffers( HFILE hFile
)
1441 struct flush_file_request
*req
= get_req_buffer();
1442 req
->handle
= hFile
;
1443 return !server_call( REQ_FLUSH_FILE
);
1447 /**************************************************************************
1448 * SetEndOfFile (KERNEL32.483)
1450 BOOL WINAPI
SetEndOfFile( HFILE hFile
)
1452 struct truncate_file_request
*req
= get_req_buffer();
1453 req
->handle
= hFile
;
1454 return !server_call( REQ_TRUNCATE_FILE
);
1458 /***********************************************************************
1459 * DeleteFile16 (KERNEL.146)
1461 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1463 return DeleteFileA( path
);
1467 /***********************************************************************
1468 * DeleteFile32A (KERNEL32.71)
1470 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1472 DOS_FULL_NAME full_name
;
1474 TRACE(file
, "'%s'\n", path
);
1478 ERR(file
, "Empty path passed\n");
1481 if (DOSFS_GetDevice( path
))
1483 WARN(file
, "cannot remove DOS device '%s'!\n", path
);
1484 SetLastError( ERROR_FILE_NOT_FOUND
);
1488 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1489 if (unlink( full_name
.long_name
) == -1)
1498 /***********************************************************************
1499 * DeleteFile32W (KERNEL32.72)
1501 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1503 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1504 BOOL ret
= DeleteFileA( xpath
);
1505 HeapFree( GetProcessHeap(), 0, xpath
);
1510 /***********************************************************************
1513 LPVOID
FILE_dommap( int unix_handle
, LPVOID start
,
1514 DWORD size_high
, DWORD size_low
,
1515 DWORD offset_high
, DWORD offset_low
,
1516 int prot
, int flags
)
1522 if (size_high
|| offset_high
)
1523 FIXME(file
, "offsets larger than 4Gb not supported\n");
1525 if (unix_handle
== -1)
1530 static int fdzero
= -1;
1534 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1536 perror( "/dev/zero: open" );
1541 #endif /* MAP_ANON */
1542 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1544 flags
&= ~MAP_SHARED
;
1547 flags
|= MAP_PRIVATE
;
1550 else fd
= unix_handle
;
1552 if ((ret
= mmap( start
, size_low
, prot
,
1553 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1556 /* mmap() failed; if this is because the file offset is not */
1557 /* page-aligned (EINVAL), or because the underlying filesystem */
1558 /* does not support mmap() (ENOEXEC), we do it by hand. */
1560 if (unix_handle
== -1) return ret
;
1561 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1562 if (prot
& PROT_WRITE
)
1564 /* We cannot fake shared write mappings */
1566 if (flags
& MAP_SHARED
) return ret
;
1569 if (!(flags
& MAP_PRIVATE
)) return ret
;
1572 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1573 /* Reserve the memory with an anonymous mmap */
1574 ret
= FILE_dommap( -1, start
, size_high
, size_low
, 0, 0,
1575 PROT_READ
| PROT_WRITE
, flags
);
1576 if (ret
== (LPVOID
)-1) return ret
;
1577 /* Now read in the file */
1578 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1580 FILE_munmap( ret
, size_high
, size_low
);
1583 read( fd
, ret
, size_low
);
1584 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1585 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1590 /***********************************************************************
1593 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1596 FIXME(file
, "offsets larger than 4Gb not supported\n");
1597 return munmap( start
, size_low
);
1601 /***********************************************************************
1602 * GetFileType (KERNEL32.222)
1604 DWORD WINAPI
GetFileType( HFILE hFile
)
1606 struct get_file_info_request
*req
= get_req_buffer();
1607 req
->handle
= hFile
;
1608 if (server_call( REQ_GET_FILE_INFO
)) 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
= get_req_buffer();
1886 req
->handle
= hFile
;
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 */
1895 return !server_call( REQ_SET_FILE_TIME
);
1899 /**************************************************************************
1900 * LockFile (KERNEL32.511)
1902 BOOL WINAPI
LockFile( HFILE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1903 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
)
1905 struct lock_file_request
*req
= get_req_buffer();
1907 req
->handle
= hFile
;
1908 req
->offset_low
= dwFileOffsetLow
;
1909 req
->offset_high
= dwFileOffsetHigh
;
1910 req
->count_low
= nNumberOfBytesToLockLow
;
1911 req
->count_high
= nNumberOfBytesToLockHigh
;
1912 return !server_call( REQ_LOCK_FILE
);
1915 /**************************************************************************
1916 * LockFileEx [KERNEL32.512]
1918 * Locks a byte range within an open file for shared or exclusive access.
1925 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1927 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1928 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
,
1929 LPOVERLAPPED pOverlapped
)
1931 FIXME(file
, "hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1932 hFile
, flags
, reserved
, nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
,
1935 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1938 ERR(file
, "reserved == %ld: Supposed to be 0??\n", reserved
);
1939 SetLastError(ERROR_INVALID_PARAMETER
);
1946 /**************************************************************************
1947 * UnlockFile (KERNEL32.703)
1949 BOOL WINAPI
UnlockFile( HFILE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1950 DWORD nNumberOfBytesToUnlockLow
, DWORD nNumberOfBytesToUnlockHigh
)
1952 struct unlock_file_request
*req
= get_req_buffer();
1954 req
->handle
= hFile
;
1955 req
->offset_low
= dwFileOffsetLow
;
1956 req
->offset_high
= dwFileOffsetHigh
;
1957 req
->count_low
= nNumberOfBytesToUnlockLow
;
1958 req
->count_high
= nNumberOfBytesToUnlockHigh
;
1959 return !server_call( REQ_UNLOCK_FILE
);
1965 struct DOS_FILE_LOCK
{
1966 struct DOS_FILE_LOCK
* next
;
1970 FILE_OBJECT
* dos_file
;
1971 /* char * unix_name;*/
1974 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
1976 static DOS_FILE_LOCK
*locks
= NULL
;
1977 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
1980 /* Locks need to be mirrored because unix file locking is based
1981 * on the pid. Inside of wine there can be multiple WINE processes
1982 * that share the same unix pid.
1983 * Read's and writes should check these locks also - not sure
1984 * how critical that is at this point (FIXME).
1987 static BOOL
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
1989 DOS_FILE_LOCK
*curr
;
1992 processId
= GetCurrentProcessId();
1994 /* check if lock overlaps a current lock for the same file */
1996 for (curr
= locks
; curr
; curr
= curr
->next
) {
1997 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
1998 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
1999 return TRUE
;/* region is identic */
2000 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
2001 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
2002 /* region overlaps */
2009 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
2010 curr
->processId
= GetCurrentProcessId();
2011 curr
->base
= f
->l_start
;
2012 curr
->len
= f
->l_len
;
2013 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
2015 curr
->dos_file
= file
;
2020 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
2023 DOS_FILE_LOCK
**curr
;
2026 processId
= GetCurrentProcessId();
2029 if ((*curr
)->dos_file
== file
) {
2031 *curr
= (*curr
)->next
;
2032 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2033 HeapFree( SystemHeap
, 0, rem
);
2036 curr
= &(*curr
)->next
;
2040 static BOOL
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2043 DOS_FILE_LOCK
**curr
;
2046 processId
= GetCurrentProcessId();
2047 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2048 if ((*curr
)->processId
== processId
&&
2049 (*curr
)->dos_file
== file
&&
2050 (*curr
)->base
== f
->l_start
&&
2051 (*curr
)->len
== f
->l_len
) {
2052 /* this is the same lock */
2054 *curr
= (*curr
)->next
;
2055 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2056 HeapFree( SystemHeap
, 0, rem
);
2060 /* no matching lock found */
2065 /**************************************************************************
2066 * LockFile (KERNEL32.511)
2068 BOOL WINAPI
LockFile(
2069 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2070 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2075 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2076 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2077 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2079 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2080 FIXME(file
, "Unimplemented bytes > 32bits\n");
2084 f
.l_start
= dwFileOffsetLow
;
2085 f
.l_len
= nNumberOfBytesToLockLow
;
2086 f
.l_whence
= SEEK_SET
;
2090 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2092 /* shadow locks internally */
2093 if (!DOS_AddLock(file
, &f
)) {
2094 SetLastError( ERROR_LOCK_VIOLATION
);
2098 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2099 #ifdef USE_UNIX_LOCKS
2100 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2101 if (errno
== EACCES
|| errno
== EAGAIN
) {
2102 SetLastError( ERROR_LOCK_VIOLATION
);
2107 /* remove our internal copy of the lock */
2108 DOS_RemoveLock(file
, &f
);
2116 /**************************************************************************
2117 * UnlockFile (KERNEL32.703)
2119 BOOL WINAPI
UnlockFile(
2120 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2121 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2126 TRACE(file
, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2127 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2128 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2130 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2131 WARN(file
, "Unimplemented bytes > 32bits\n");
2135 f
.l_start
= dwFileOffsetLow
;
2136 f
.l_len
= nNumberOfBytesToUnlockLow
;
2137 f
.l_whence
= SEEK_SET
;
2141 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2143 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2145 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2146 #ifdef USE_UNIX_LOCKS
2147 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2156 /**************************************************************************
2157 * GetFileAttributesEx32A [KERNEL32.874]
2159 BOOL WINAPI
GetFileAttributesExA(
2160 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2161 LPVOID lpFileInformation
)
2163 DOS_FULL_NAME full_name
;
2164 BY_HANDLE_FILE_INFORMATION info
;
2166 if (lpFileName
== NULL
) return FALSE
;
2167 if (lpFileInformation
== NULL
) return FALSE
;
2169 if (fInfoLevelId
== GetFileExInfoStandard
) {
2170 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2171 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2172 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2173 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2175 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2176 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2177 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2178 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2179 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2180 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2183 FIXME (file
, "invalid info level %d!\n", fInfoLevelId
);
2191 /**************************************************************************
2192 * GetFileAttributesEx32W [KERNEL32.875]
2194 BOOL WINAPI
GetFileAttributesExW(
2195 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2196 LPVOID lpFileInformation
)
2198 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2200 GetFileAttributesExA( nameA
, fInfoLevelId
, lpFileInformation
);
2201 HeapFree( GetProcessHeap(), 0, nameA
);