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.
20 #ifdef HAVE_SYS_ERRNO_H
21 #include <sys/errno.h>
23 #include <sys/types.h>
25 #ifdef HAVE_SYS_MMAN_H
36 #include "wine/winbase16.h"
37 #include "wine/winestring.h"
49 #include "debugtools.h"
53 DEFAULT_DEBUG_CHANNEL(file
)
55 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
56 #define MAP_ANON MAP_ANONYMOUS
59 /* Size of per-process table of DOS handles */
60 #define DOS_TABLE_SIZE 256
63 /***********************************************************************
66 * Convert OF_* mode into flags for CreateFile.
68 static void FILE_ConvertOFMode( INT mode
, DWORD
*access
, DWORD
*sharing
)
72 case OF_READ
: *access
= GENERIC_READ
; break;
73 case OF_WRITE
: *access
= GENERIC_WRITE
; break;
74 case OF_READWRITE
: *access
= GENERIC_READ
| GENERIC_WRITE
; break;
75 default: *access
= 0; break;
79 case OF_SHARE_EXCLUSIVE
: *sharing
= 0; break;
80 case OF_SHARE_DENY_WRITE
: *sharing
= FILE_SHARE_READ
; break;
81 case OF_SHARE_DENY_READ
: *sharing
= FILE_SHARE_WRITE
; break;
82 case OF_SHARE_DENY_NONE
:
84 default: *sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
90 /***********************************************************************
94 * oldmode[I] mode how file was first opened
95 * mode[I] mode how the file should get opened
100 * Look what we have to do with the given SHARE modes
102 * Ralph Brown's interrupt list gives following explication, I guess
103 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
105 * FIXME: Validate this function
106 ========from Ralph Brown's list =========
108 Values of DOS file sharing behavior:
109 | Second and subsequent Opens
110 First |Compat Deny Deny Deny Deny
111 Open | All Write Read None
112 |R W RW R W RW R W RW R W RW R W RW
113 - - - - -| - - - - - - - - - - - - - - - - -
114 Compat R |Y Y Y N N N 1 N N N N N 1 N N
115 W |Y Y Y N N N N N N N N N N N N
116 RW|Y Y Y N N N N N N N N N N N N
118 Deny R |C C C N N N N N N N N N N N N
119 All W |C C C N N N N N N N N N N N N
120 RW|C C C N N N N N N N N N N N N
122 Deny R |2 C C N N N Y N N N N N Y N N
123 Write W |C C C N N N N N N Y N N Y N N
124 RW|C C C N N N N N N N N N Y N N
126 Deny R |C C C N N N N Y N N N N N Y N
127 Read W |C C C N N N N N N N Y N N Y N
128 RW|C C C N N N N N N N N N N Y N
130 Deny R |2 C C N N N Y Y Y N N N Y Y Y
131 None W |C C C N N N N N N Y Y Y Y Y Y
132 RW|C C C N N N N N N N N N Y Y Y
133 Legend: Y = open succeeds, N = open fails with error code 05h
134 C = open fails, INT 24 generated
135 1 = open succeeds if file read-only, else fails with error code
136 2 = open succeeds if file read-only, else fails with INT 24
137 ========end of description from Ralph Brown's List =====
138 For every "Y" in the table we return FALSE
139 For every "N" we set the DOS_ERROR and return TRUE
140 For all other cases we barf,set the DOS_ERROR and return TRUE
143 static BOOL
FILE_ShareDeny( int mode
, int oldmode
)
145 int oldsharemode
= oldmode
& 0x70;
146 int sharemode
= mode
& 0x70;
147 int oldopenmode
= oldmode
& 3;
148 int openmode
= mode
& 3;
150 switch (oldsharemode
)
152 case OF_SHARE_COMPAT
:
153 if (sharemode
== OF_SHARE_COMPAT
) return FALSE
;
154 if (openmode
== OF_READ
) goto test_ro_err05
;
156 case OF_SHARE_EXCLUSIVE
:
157 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
159 case OF_SHARE_DENY_WRITE
:
160 if (openmode
!= OF_READ
)
162 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
167 case OF_SHARE_COMPAT
:
168 if (oldopenmode
== OF_READ
) goto test_ro_int24
;
170 case OF_SHARE_DENY_NONE
:
172 case OF_SHARE_DENY_WRITE
:
173 if (oldopenmode
== OF_READ
) return FALSE
;
174 case OF_SHARE_DENY_READ
:
175 if (oldopenmode
== OF_WRITE
) return FALSE
;
176 case OF_SHARE_EXCLUSIVE
:
181 case OF_SHARE_DENY_READ
:
182 if (openmode
!= OF_WRITE
)
184 if (sharemode
== OF_SHARE_COMPAT
) goto fail_int24
;
189 case OF_SHARE_COMPAT
:
191 case OF_SHARE_DENY_NONE
:
193 case OF_SHARE_DENY_WRITE
:
194 if (oldopenmode
== OF_READ
) return FALSE
;
195 case OF_SHARE_DENY_READ
:
196 if (oldopenmode
== OF_WRITE
) return FALSE
;
197 case OF_SHARE_EXCLUSIVE
:
202 case OF_SHARE_DENY_NONE
:
205 case OF_SHARE_COMPAT
:
207 case OF_SHARE_DENY_NONE
:
209 case OF_SHARE_DENY_WRITE
:
210 if (oldopenmode
== OF_READ
) return FALSE
;
211 case OF_SHARE_DENY_READ
:
212 if (oldopenmode
== OF_WRITE
) return FALSE
;
213 case OF_SHARE_EXCLUSIVE
:
218 ERR("unknown mode\n");
220 ERR("shouldn't happen\n");
221 ERR("Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
225 if (oldmode
== OF_READ
)
229 FIXME("generate INT24 missing\n");
230 /* Is this the right error? */
231 SetLastError( ERROR_ACCESS_DENIED
);
235 if (oldmode
== OF_READ
)
239 TRACE("Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode
,mode
);
240 SetLastError( ERROR_ACCESS_DENIED
);
246 /***********************************************************************
249 * Set the DOS error code from errno.
251 void FILE_SetDosError(void)
253 int save_errno
= errno
; /* errno gets overwritten by printf */
255 TRACE("errno = %d %s\n", errno
, strerror(errno
));
259 SetLastError( ERROR_SHARING_VIOLATION
);
262 SetLastError( ERROR_INVALID_HANDLE
);
265 SetLastError( ERROR_HANDLE_DISK_FULL
);
270 SetLastError( ERROR_ACCESS_DENIED
);
273 SetLastError( ERROR_LOCK_VIOLATION
);
276 SetLastError( ERROR_FILE_NOT_FOUND
);
279 SetLastError( ERROR_CANNOT_MAKE
);
283 SetLastError( ERROR_NO_MORE_FILES
);
286 SetLastError( ERROR_FILE_EXISTS
);
290 SetLastError( ERROR_SEEK
);
293 SetLastError( ERROR_DIR_NOT_EMPTY
);
296 perror( "int21: unknown errno" );
297 SetLastError( ERROR_GEN_FAILURE
);
304 /***********************************************************************
307 * Duplicate a Unix handle into a task handle.
309 HFILE
FILE_DupUnixHandle( int fd
, DWORD access
)
312 struct alloc_file_handle_request
*req
= get_req_buffer();
314 if ((unix_handle
= dup(fd
)) == -1)
317 return INVALID_HANDLE_VALUE
;
319 req
->access
= access
;
320 server_call_fd( REQ_ALLOC_FILE_HANDLE
, unix_handle
, NULL
);
325 /***********************************************************************
328 * Implementation of CreateFile. Takes a Unix path name.
330 HANDLE
FILE_CreateFile( LPCSTR filename
, DWORD access
, DWORD sharing
,
331 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
332 DWORD attributes
, HANDLE
template )
334 struct create_file_request
*req
= get_req_buffer();
336 req
->access
= access
;
337 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
338 req
->sharing
= sharing
;
339 req
->create
= creation
;
340 req
->attrs
= attributes
;
341 lstrcpynA( req
->name
, filename
, server_remaining(req
->name
) );
343 server_call( REQ_CREATE_FILE
);
345 /* If write access failed, retry without GENERIC_WRITE */
347 if ((req
->handle
== -1) && !Options
.failReadOnly
&&
348 (access
& GENERIC_WRITE
))
350 DWORD lasterror
= GetLastError();
351 if ((lasterror
== ERROR_ACCESS_DENIED
) || (lasterror
== ERROR_WRITE_PROTECT
))
352 return FILE_CreateFile( filename
, access
& ~GENERIC_WRITE
, sharing
,
353 sa
, creation
, attributes
, template );
359 /***********************************************************************
362 * Same as FILE_CreateFile but for a device
364 HFILE
FILE_CreateDevice( int client_id
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
366 struct create_device_request
*req
= get_req_buffer();
368 req
->access
= access
;
369 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
372 server_call( REQ_CREATE_DEVICE
);
377 /*************************************************************************
378 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
380 * Creates or opens an object, and returns a handle that can be used to
381 * access that object.
385 * filename [I] pointer to filename to be accessed
386 * access [I] access mode requested
387 * sharing [I] share mode
388 * sa [I] pointer to security attributes
389 * creation [I] how to create the file
390 * attributes [I] attributes for newly created file
391 * template [I] handle to file with extended attributes to copy
394 * Success: Open handle to specified file
395 * Failure: INVALID_HANDLE_VALUE
398 * Should call SetLastError() on failure.
402 * Doesn't support character devices, pipes, template files, or a
403 * lot of the 'attributes' flags yet.
405 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
406 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
407 DWORD attributes
, HANDLE
template )
409 DOS_FULL_NAME full_name
;
413 SetLastError( ERROR_INVALID_PARAMETER
);
416 TRACE("%s %s%s%s%s%s%s%s\n",filename
,
417 ((access
& GENERIC_READ
)==GENERIC_READ
)?"GENERIC_READ ":"",
418 ((access
& GENERIC_WRITE
)==GENERIC_WRITE
)?"GENERIC_WRITE ":"",
419 (!access
)?"QUERY_ACCESS ":"",
420 ((sharing
& FILE_SHARE_READ
)==FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
421 ((sharing
& FILE_SHARE_WRITE
)==FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
422 ((sharing
& FILE_SHARE_DELETE
)==FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
423 (creation
==CREATE_NEW
)?"CREATE_NEW":
424 (creation
==CREATE_ALWAYS
)?"CREATE_ALWAYS ":
425 (creation
==OPEN_EXISTING
)?"OPEN_EXISTING ":
426 (creation
==OPEN_ALWAYS
)?"OPEN_ALWAYS ":
427 (creation
==TRUNCATE_EXISTING
)?"TRUNCATE_EXISTING ":"");
429 /* If the name starts with '\\?\', ignore the first 4 chars. */
430 if (!strncmp(filename
, "\\\\?\\", 4))
433 if (!strncmp(filename
, "UNC\\", 4))
435 FIXME("UNC name (%s) not supported.\n", filename
);
436 SetLastError( ERROR_PATH_NOT_FOUND
);
441 if (!strncmp(filename
, "\\\\.\\", 4))
442 return DEVICE_Open( filename
+4, access
, sa
);
444 /* If the name still starts with '\\', it's a UNC name. */
445 if (!strncmp(filename
, "\\\\", 2))
447 FIXME("UNC name (%s) not supported.\n", filename
);
448 SetLastError( ERROR_PATH_NOT_FOUND
);
452 /* If the name contains a DOS wild card (* or ?), do no create a file */
453 if(strchr(filename
,'*') || strchr(filename
,'?'))
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("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("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 HANDLE 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 HANDLE 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( HANDLE hFile
,
552 BY_HANDLE_FILE_INFORMATION
*info
)
554 struct get_file_info_request
*req
= get_req_buffer();
558 if (server_call( REQ_GET_FILE_INFO
)) return 0;
559 DOSFS_UnixTimeToFileTime( req
->write_time
, &info
->ftCreationTime
, 0 );
560 DOSFS_UnixTimeToFileTime( req
->write_time
, &info
->ftLastWriteTime
, 0 );
561 DOSFS_UnixTimeToFileTime( req
->access_time
, &info
->ftLastAccessTime
, 0 );
562 info
->dwFileAttributes
= req
->attr
;
563 info
->dwVolumeSerialNumber
= req
->serial
;
564 info
->nFileSizeHigh
= req
->size_high
;
565 info
->nFileSizeLow
= req
->size_low
;
566 info
->nNumberOfLinks
= req
->links
;
567 info
->nFileIndexHigh
= req
->index_high
;
568 info
->nFileIndexLow
= req
->index_low
;
573 /**************************************************************************
574 * GetFileAttributes16 (KERNEL.420)
576 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
578 return GetFileAttributesA( name
);
582 /**************************************************************************
583 * GetFileAttributes32A (KERNEL32.217)
585 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
587 DOS_FULL_NAME full_name
;
588 BY_HANDLE_FILE_INFORMATION info
;
590 if (name
== NULL
|| *name
=='\0') return -1;
592 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
593 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
594 return info
.dwFileAttributes
;
598 /**************************************************************************
599 * GetFileAttributes32W (KERNEL32.218)
601 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
603 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
604 DWORD res
= GetFileAttributesA( nameA
);
605 HeapFree( GetProcessHeap(), 0, nameA
);
610 /***********************************************************************
611 * GetFileSize (KERNEL32.220)
613 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
615 BY_HANDLE_FILE_INFORMATION info
;
616 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
617 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
618 return info
.nFileSizeLow
;
622 /***********************************************************************
623 * GetFileTime (KERNEL32.221)
625 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
626 FILETIME
*lpLastAccessTime
,
627 FILETIME
*lpLastWriteTime
)
629 BY_HANDLE_FILE_INFORMATION info
;
630 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
631 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
632 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
633 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
637 /***********************************************************************
638 * CompareFileTime (KERNEL32.28)
640 INT WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
642 if (!x
|| !y
) return -1;
644 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
646 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
648 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
650 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
656 /***********************************************************************
657 * GetTempFileName16 (KERNEL.97)
659 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
664 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
665 drive
|= DRIVE_GetCurrentDrive() + 'A';
667 if ((drive
& TF_FORCEDRIVE
) &&
668 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
670 drive
&= ~TF_FORCEDRIVE
;
671 WARN("invalid drive %d specified\n", drive
);
674 if (drive
& TF_FORCEDRIVE
)
675 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
677 GetTempPathA( 132, temppath
);
678 return (UINT16
)GetTempFileNameA( temppath
, prefix
, unique
, buffer
);
682 /***********************************************************************
683 * GetTempFileName32A (KERNEL32.290)
685 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
,
688 static UINT unique_temp
;
689 DOS_FULL_NAME full_name
;
694 if ( !path
|| !prefix
|| !buffer
) return 0;
696 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
697 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
699 strcpy( buffer
, path
);
700 p
= buffer
+ strlen(buffer
);
702 /* add a \, if there isn't one and path is more than just the drive letter ... */
703 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
704 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
707 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
708 sprintf( p
, "%04x.tmp", num
);
710 /* Now try to create it */
716 HFILE handle
= CreateFileA( buffer
, GENERIC_WRITE
, 0, NULL
,
717 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, -1 );
718 if (handle
!= INVALID_HANDLE_VALUE
)
719 { /* We created it */
720 TRACE("created %s\n",
722 CloseHandle( handle
);
725 if (GetLastError() != ERROR_FILE_EXISTS
)
726 break; /* No need to go on */
728 sprintf( p
, "%04x.tmp", num
);
729 } while (num
!= (unique
& 0xffff));
732 /* Get the full path name */
734 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
736 /* Check if we have write access in the directory */
737 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
738 if (access( full_name
.long_name
, W_OK
) == -1)
739 WARN("returns '%s', which doesn't seem to be writeable.\n",
742 TRACE("returning %s\n", buffer
);
743 return unique
? unique
: num
;
747 /***********************************************************************
748 * GetTempFileName32W (KERNEL32.291)
750 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
,
758 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
759 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
760 ret
= GetTempFileNameA( patha
, prefixa
, unique
, buffera
);
761 lstrcpyAtoW( buffer
, buffera
);
762 HeapFree( GetProcessHeap(), 0, patha
);
763 HeapFree( GetProcessHeap(), 0, prefixa
);
768 /***********************************************************************
771 * Implementation of OpenFile16() and OpenFile32().
773 static HFILE
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
,
778 WORD filedatetime
[2];
779 DOS_FULL_NAME full_name
;
780 DWORD access
, sharing
;
783 if (!ofs
) return HFILE_ERROR
;
785 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
786 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
787 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
788 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
789 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
790 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
791 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
792 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
793 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
794 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
795 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
796 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
797 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
798 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
799 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
800 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
801 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
802 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
806 ofs
->cBytes
= sizeof(OFSTRUCT
);
808 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
811 ERR("called with `name' set to NULL ! Please debug.\n");
815 TRACE("%s %04x\n", name
, mode
);
817 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
818 Are there any cases where getting the path here is wrong?
819 Uwe Bonnes 1997 Apr 2 */
820 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
),
821 ofs
->szPathName
, NULL
)) goto error
;
822 FILE_ConvertOFMode( mode
, &access
, &sharing
);
824 /* OF_PARSE simply fills the structure */
828 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
830 TRACE("(%s): OF_PARSE, res = '%s'\n",
831 name
, ofs
->szPathName
);
835 /* OF_CREATE is completely different from all other options, so
838 if (mode
& OF_CREATE
)
840 if ((hFileRet
= CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
841 sharing
, NULL
, CREATE_ALWAYS
,
842 FILE_ATTRIBUTE_NORMAL
, -1 ))== INVALID_HANDLE_VALUE
)
847 /* If OF_SEARCH is set, ignore the given path */
849 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
851 /* First try the file name as is */
852 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
853 /* Now remove the path */
854 if (name
[0] && (name
[1] == ':')) name
+= 2;
855 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
856 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
857 if (!name
[0]) goto not_found
;
860 /* Now look for the file */
862 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
865 TRACE("found %s = %s\n",
866 full_name
.long_name
, full_name
.short_name
);
867 lstrcpynA( ofs
->szPathName
, full_name
.short_name
,
868 sizeof(ofs
->szPathName
) );
870 if (mode
& OF_SHARE_EXCLUSIVE
)
871 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
872 on the file <tempdir>/_ins0432._mp to determine how
873 far installation has proceeded.
874 _ins0432._mp is an executable and while running the
875 application expects the open with OF_SHARE_ to fail*/
877 As our loader closes the files after loading the executable,
878 we can't find the running executable with FILE_InUse.
879 Perhaps the loader should keep the file open.
880 Recheck against how Win handles that case */
882 char *last
= strrchr(full_name
.long_name
,'/');
884 last
= full_name
.long_name
- 1;
885 if (GetModuleHandle16(last
+1))
887 TRACE("Denying shared open for %s\n",full_name
.long_name
);
892 if (mode
& OF_DELETE
)
894 if (unlink( full_name
.long_name
) == -1) goto not_found
;
895 TRACE("(%s): OF_DELETE return = OK\n", name
);
899 hFileRet
= FILE_CreateFile( full_name
.long_name
, access
, sharing
,
900 NULL
, OPEN_EXISTING
, 0, -1 );
901 if (hFileRet
== HFILE_ERROR
) goto not_found
;
903 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
904 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
905 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
907 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
909 CloseHandle( hFileRet
);
910 WARN("(%s): OF_VERIFY failed\n", name
);
911 /* FIXME: what error here? */
912 SetLastError( ERROR_FILE_NOT_FOUND
);
916 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
918 success
: /* We get here if the open was successful */
919 TRACE("(%s): OK, return = %d\n", name
, hFileRet
);
922 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
923 CloseHandle( hFileRet
);
927 hFileRet
= FILE_AllocDosHandle( hFileRet
);
928 if (hFileRet
== HFILE_ERROR16
) goto error
;
929 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
930 _lclose16( hFileRet
);
934 not_found
: /* We get here if the file does not exist */
935 WARN("'%s' not found\n", name
);
936 SetLastError( ERROR_FILE_NOT_FOUND
);
939 error
: /* We get here if there was an error opening the file */
940 ofs
->nErrCode
= GetLastError();
941 WARN("(%s): return = HFILE_ERROR error= %d\n",
942 name
,ofs
->nErrCode
);
947 /***********************************************************************
948 * OpenFile16 (KERNEL.74)
950 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
952 return FILE_DoOpenFile( name
, ofs
, mode
, FALSE
);
956 /***********************************************************************
957 * OpenFile32 (KERNEL32.396)
959 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
961 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
965 /***********************************************************************
966 * FILE_InitProcessDosHandles
968 * Allocates the default DOS handles for a process. Called either by
969 * AllocDosHandle below or by the DOSVM stuff.
971 BOOL
FILE_InitProcessDosHandles( void ) {
974 if (!(ptr
= HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
,
975 sizeof(*ptr
) * DOS_TABLE_SIZE
)))
977 PROCESS_Current()->dos_handles
= ptr
;
978 ptr
[0] = GetStdHandle(STD_INPUT_HANDLE
);
979 ptr
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
980 ptr
[2] = GetStdHandle(STD_ERROR_HANDLE
);
981 ptr
[3] = GetStdHandle(STD_ERROR_HANDLE
);
982 ptr
[4] = GetStdHandle(STD_ERROR_HANDLE
);
986 /***********************************************************************
987 * FILE_AllocDosHandle
989 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
990 * longer valid after this function (even on failure).
992 HFILE16
FILE_AllocDosHandle( HANDLE handle
)
995 HANDLE
*ptr
= PROCESS_Current()->dos_handles
;
997 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
998 return INVALID_HANDLE_VALUE16
;
1001 if (!FILE_InitProcessDosHandles())
1003 ptr
= PROCESS_Current()->dos_handles
;
1006 for (i
= 0; i
< DOS_TABLE_SIZE
; i
++, ptr
++)
1010 TRACE("Got %d for h32 %d\n", i
, handle
);
1014 CloseHandle( handle
);
1015 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
1016 return INVALID_HANDLE_VALUE16
;
1020 /***********************************************************************
1023 * Return the Win32 handle for a DOS handle.
1025 HANDLE
FILE_GetHandle( HFILE16 hfile
)
1027 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1028 if ((hfile
>= DOS_TABLE_SIZE
) || !table
|| !table
[hfile
])
1030 SetLastError( ERROR_INVALID_HANDLE
);
1031 return INVALID_HANDLE_VALUE
;
1033 return table
[hfile
];
1037 /***********************************************************************
1040 * dup2() function for DOS handles.
1042 HFILE16
FILE_Dup2( HFILE16 hFile1
, HFILE16 hFile2
)
1044 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1047 if ((hFile1
>= DOS_TABLE_SIZE
) || (hFile2
>= DOS_TABLE_SIZE
) ||
1048 !table
|| !table
[hFile1
])
1050 SetLastError( ERROR_INVALID_HANDLE
);
1051 return HFILE_ERROR16
;
1055 FIXME("stdio handle closed, need proper conversion\n" );
1056 SetLastError( ERROR_INVALID_HANDLE
);
1057 return HFILE_ERROR16
;
1059 if (!DuplicateHandle( GetCurrentProcess(), table
[hFile1
],
1060 GetCurrentProcess(), &new_handle
,
1061 0, FALSE
, DUPLICATE_SAME_ACCESS
))
1062 return HFILE_ERROR16
;
1063 if (table
[hFile2
]) CloseHandle( table
[hFile2
] );
1064 table
[hFile2
] = new_handle
;
1069 /***********************************************************************
1070 * _lclose16 (KERNEL.81)
1072 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1074 HANDLE
*table
= PROCESS_Current()->dos_handles
;
1078 FIXME("stdio handle closed, need proper conversion\n" );
1079 SetLastError( ERROR_INVALID_HANDLE
);
1080 return HFILE_ERROR16
;
1082 if ((hFile
>= DOS_TABLE_SIZE
) || !table
|| !table
[hFile
])
1084 SetLastError( ERROR_INVALID_HANDLE
);
1085 return HFILE_ERROR16
;
1087 TRACE("%d (handle32=%d)\n", hFile
, table
[hFile
] );
1088 CloseHandle( table
[hFile
] );
1094 /***********************************************************************
1095 * _lclose32 (KERNEL32.592)
1097 HFILE WINAPI
_lclose( HFILE hFile
)
1099 TRACE("handle %d\n", hFile
);
1100 return CloseHandle( hFile
) ? 0 : HFILE_ERROR
;
1104 /***********************************************************************
1105 * ReadFile (KERNEL32.428)
1107 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
1108 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
1110 struct get_read_fd_request
*req
= get_req_buffer();
1111 int unix_handle
, result
;
1113 TRACE("%d %p %ld\n", hFile
, buffer
, bytesToRead
);
1115 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
1116 if (!bytesToRead
) return TRUE
;
1118 req
->handle
= hFile
;
1119 server_call_fd( REQ_GET_READ_FD
, -1, &unix_handle
);
1120 if (unix_handle
== -1) return FALSE
;
1121 while ((result
= read( unix_handle
, buffer
, bytesToRead
)) == -1)
1123 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1124 if ((errno
== EFAULT
) && VIRTUAL_HandleFault( buffer
)) continue;
1128 close( unix_handle
);
1129 if (result
== -1) return FALSE
;
1130 if (bytesRead
) *bytesRead
= result
;
1135 /***********************************************************************
1136 * WriteFile (KERNEL32.578)
1138 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
1139 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
1141 struct get_write_fd_request
*req
= get_req_buffer();
1142 int unix_handle
, result
;
1144 TRACE("%d %p %ld\n", hFile
, buffer
, bytesToWrite
);
1146 if (bytesWritten
) *bytesWritten
= 0; /* Do this before anything else */
1147 if (!bytesToWrite
) return TRUE
;
1149 req
->handle
= hFile
;
1150 server_call_fd( REQ_GET_WRITE_FD
, -1, &unix_handle
);
1151 if (unix_handle
== -1) return FALSE
;
1152 while ((result
= write( unix_handle
, buffer
, bytesToWrite
)) == -1)
1154 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1155 if ((errno
== EFAULT
) && VIRTUAL_HandleFault( buffer
)) continue;
1156 if (errno
== ENOSPC
)
1157 SetLastError( ERROR_DISK_FULL
);
1162 close( unix_handle
);
1163 if (result
== -1) return FALSE
;
1164 if (bytesWritten
) *bytesWritten
= result
;
1169 /***********************************************************************
1172 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1176 TRACE("%d %08lx %ld\n",
1177 hFile
, (DWORD
)buffer
, count
);
1179 /* Some programs pass a count larger than the allocated buffer */
1180 maxlen
= GetSelectorLimit16( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1181 if (count
> maxlen
) count
= maxlen
;
1182 return _lread(FILE_GetHandle(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1186 /***********************************************************************
1189 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1191 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1195 /***********************************************************************
1196 * _lread32 (KERNEL32.596)
1198 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
1201 if (!ReadFile( handle
, buffer
, count
, &result
, NULL
)) return -1;
1206 /***********************************************************************
1207 * _lread16 (KERNEL.82)
1209 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1211 return (UINT16
)_lread(FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1215 /***********************************************************************
1216 * _lcreat16 (KERNEL.83)
1218 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1220 return FILE_AllocDosHandle( _lcreat( path
, attr
) );
1224 /***********************************************************************
1225 * _lcreat (KERNEL32.593)
1227 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
1229 /* Mask off all flags not explicitly allowed by the doc */
1230 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
1231 TRACE("%s %02x\n", path
, attr
);
1232 return CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
1233 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1234 CREATE_ALWAYS
, attr
, -1 );
1238 /***********************************************************************
1239 * SetFilePointer (KERNEL32.492)
1241 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
,
1244 struct set_file_pointer_request
*req
= get_req_buffer();
1246 if (highword
&& *highword
)
1248 FIXME("64-bit offsets not supported yet\n");
1249 SetLastError( ERROR_INVALID_PARAMETER
);
1252 TRACE("handle %d offset %ld origin %ld\n",
1253 hFile
, distance
, method
);
1255 req
->handle
= hFile
;
1256 req
->low
= distance
;
1257 req
->high
= highword
? *highword
: 0;
1258 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1259 req
->whence
= method
;
1261 if (server_call( REQ_SET_FILE_POINTER
)) return 0xffffffff;
1262 if (highword
) *highword
= req
->new_high
;
1263 return req
->new_low
;
1267 /***********************************************************************
1268 * _llseek16 (KERNEL.84)
1271 * Seeking before the start of the file should be allowed for _llseek16,
1272 * but cause subsequent I/O operations to fail (cf. interrupt list)
1275 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1277 return SetFilePointer( FILE_GetHandle(hFile
), lOffset
, NULL
, nOrigin
);
1281 /***********************************************************************
1282 * _llseek32 (KERNEL32.594)
1284 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
1286 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1290 /***********************************************************************
1291 * _lopen16 (KERNEL.85)
1293 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1295 return FILE_AllocDosHandle( _lopen( path
, mode
) );
1299 /***********************************************************************
1300 * _lopen32 (KERNEL32.595)
1302 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
1304 DWORD access
, sharing
;
1306 TRACE("('%s',%04x)\n", path
, mode
);
1307 FILE_ConvertOFMode( mode
, &access
, &sharing
);
1308 return CreateFileA( path
, access
, sharing
, NULL
, OPEN_EXISTING
, 0, -1 );
1312 /***********************************************************************
1313 * _lwrite16 (KERNEL.86)
1315 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1317 return (UINT16
)_hwrite( FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1320 /***********************************************************************
1321 * _lwrite32 (KERNEL32.761)
1323 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
1325 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
1329 /***********************************************************************
1330 * _hread16 (KERNEL.349)
1332 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1334 return _lread( FILE_GetHandle(hFile
), buffer
, count
);
1338 /***********************************************************************
1339 * _hread32 (KERNEL32.590)
1341 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
1343 return _lread( hFile
, buffer
, count
);
1347 /***********************************************************************
1348 * _hwrite16 (KERNEL.350)
1350 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1352 return _hwrite( FILE_GetHandle(hFile
), buffer
, count
);
1356 /***********************************************************************
1357 * _hwrite32 (KERNEL32.591)
1359 * experimentation yields that _lwrite:
1360 * o truncates the file at the current position with
1362 * o returns 0 on a 0 length write
1363 * o works with console handles
1366 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
1370 TRACE("%d %p %ld\n", handle
, buffer
, count
);
1374 /* Expand or truncate at current position */
1375 if (!SetEndOfFile( handle
)) return HFILE_ERROR
;
1378 if (!WriteFile( handle
, buffer
, count
, &result
, NULL
))
1384 /***********************************************************************
1385 * SetHandleCount16 (KERNEL.199)
1387 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1389 HGLOBAL16 hPDB
= GetCurrentPDB16();
1390 PDB16
*pdb
= (PDB16
*)GlobalLock16( hPDB
);
1391 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1393 TRACE("(%d)\n", count
);
1395 if (count
< 20) count
= 20; /* No point in going below 20 */
1396 else if (count
> 254) count
= 254;
1400 if (pdb
->nbFiles
> 20)
1402 memcpy( pdb
->fileHandles
, files
, 20 );
1403 GlobalFree16( pdb
->hFileHandles
);
1404 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1405 GlobalHandleToSel16( hPDB
) );
1406 pdb
->hFileHandles
= 0;
1410 else /* More than 20, need a new file handles table */
1413 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1416 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1417 return pdb
->nbFiles
;
1419 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1421 if (count
> pdb
->nbFiles
)
1423 memcpy( newfiles
, files
, pdb
->nbFiles
);
1424 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1426 else memcpy( newfiles
, files
, count
);
1427 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1428 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1429 pdb
->hFileHandles
= newhandle
;
1430 pdb
->nbFiles
= count
;
1432 return pdb
->nbFiles
;
1436 /*************************************************************************
1437 * SetHandleCount32 (KERNEL32.494)
1439 UINT WINAPI
SetHandleCount( UINT count
)
1441 return MIN( 256, count
);
1445 /***********************************************************************
1446 * FlushFileBuffers (KERNEL32.133)
1448 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
1450 struct flush_file_request
*req
= get_req_buffer();
1451 req
->handle
= hFile
;
1452 return !server_call( REQ_FLUSH_FILE
);
1456 /**************************************************************************
1457 * SetEndOfFile (KERNEL32.483)
1459 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
1461 struct truncate_file_request
*req
= get_req_buffer();
1462 req
->handle
= hFile
;
1463 return !server_call( REQ_TRUNCATE_FILE
);
1467 /***********************************************************************
1468 * DeleteFile16 (KERNEL.146)
1470 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1472 return DeleteFileA( path
);
1476 /***********************************************************************
1477 * DeleteFile32A (KERNEL32.71)
1479 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1481 DOS_FULL_NAME full_name
;
1483 TRACE("'%s'\n", path
);
1487 ERR("Empty path passed\n");
1490 if (DOSFS_GetDevice( path
))
1492 WARN("cannot remove DOS device '%s'!\n", path
);
1493 SetLastError( ERROR_FILE_NOT_FOUND
);
1497 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1498 if (unlink( full_name
.long_name
) == -1)
1507 /***********************************************************************
1508 * DeleteFile32W (KERNEL32.72)
1510 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1512 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1513 BOOL ret
= DeleteFileA( xpath
);
1514 HeapFree( GetProcessHeap(), 0, xpath
);
1519 /***********************************************************************
1522 LPVOID
FILE_dommap( int unix_handle
, LPVOID start
,
1523 DWORD size_high
, DWORD size_low
,
1524 DWORD offset_high
, DWORD offset_low
,
1525 int prot
, int flags
)
1531 if (size_high
|| offset_high
)
1532 FIXME("offsets larger than 4Gb not supported\n");
1534 if (unix_handle
== -1)
1539 static int fdzero
= -1;
1543 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
1545 perror( "/dev/zero: open" );
1550 #endif /* MAP_ANON */
1551 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1553 flags
&= ~MAP_SHARED
;
1556 flags
|= MAP_PRIVATE
;
1559 else fd
= unix_handle
;
1561 if ((ret
= mmap( start
, size_low
, prot
,
1562 flags
, fd
, offset_low
)) != (LPVOID
)-1)
1565 /* mmap() failed; if this is because the file offset is not */
1566 /* page-aligned (EINVAL), or because the underlying filesystem */
1567 /* does not support mmap() (ENOEXEC), we do it by hand. */
1569 if (unix_handle
== -1) return ret
;
1570 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
)) return ret
;
1571 if (prot
& PROT_WRITE
)
1573 /* We cannot fake shared write mappings */
1575 if (flags
& MAP_SHARED
) return ret
;
1578 if (!(flags
& MAP_PRIVATE
)) return ret
;
1581 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1582 /* Reserve the memory with an anonymous mmap */
1583 ret
= FILE_dommap( -1, start
, size_high
, size_low
, 0, 0,
1584 PROT_READ
| PROT_WRITE
, flags
);
1585 if (ret
== (LPVOID
)-1) return ret
;
1586 /* Now read in the file */
1587 if ((pos
= lseek( fd
, offset_low
, SEEK_SET
)) == -1)
1589 FILE_munmap( ret
, size_high
, size_low
);
1592 read( fd
, ret
, size_low
);
1593 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
1594 mprotect( ret
, size_low
, prot
); /* Set the right protection */
1599 /***********************************************************************
1602 int FILE_munmap( LPVOID start
, DWORD size_high
, DWORD size_low
)
1605 FIXME("offsets larger than 4Gb not supported\n");
1606 return munmap( start
, size_low
);
1610 /***********************************************************************
1611 * GetFileType (KERNEL32.222)
1613 DWORD WINAPI
GetFileType( HANDLE hFile
)
1615 struct get_file_info_request
*req
= get_req_buffer();
1616 req
->handle
= hFile
;
1617 if (server_call( REQ_GET_FILE_INFO
)) return FILE_TYPE_UNKNOWN
;
1622 /**************************************************************************
1623 * MoveFileEx32A (KERNEL32.???)
1625 BOOL WINAPI
MoveFileExA( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1627 DOS_FULL_NAME full_name1
, full_name2
;
1628 int mode
=0; /* mode == 1: use copy */
1630 TRACE("(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1632 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1633 if (fn2
) { /* !fn2 means delete fn1 */
1634 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1635 /* Source name and target path are valid */
1636 if ( full_name1
.drive
!= full_name2
.drive
)
1638 /* use copy, if allowed */
1639 if (!(flag
& MOVEFILE_COPY_ALLOWED
)) {
1640 /* FIXME: Use right error code */
1641 SetLastError( ERROR_FILE_EXISTS
);
1646 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1647 /* target exists, check if we may overwrite */
1648 if (!(flag
& MOVEFILE_REPLACE_EXISTING
)) {
1649 /* FIXME: Use right error code */
1650 SetLastError( ERROR_ACCESS_DENIED
);
1654 else /* fn2 == NULL means delete source */
1655 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1656 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1657 WARN("Illegal flag\n");
1658 SetLastError( ERROR_GEN_FAILURE
);
1661 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1662 Perhaps we should queue these command and execute it
1663 when exiting... What about using on_exit(2)
1665 FIXME("Please delete file '%s' when Wine has finished\n",
1666 full_name1
.long_name
);
1669 else if (unlink( full_name1
.long_name
) == -1)
1674 else return TRUE
; /* successfully deleted */
1676 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) {
1677 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1678 Perhaps we should queue these command and execute it
1679 when exiting... What about using on_exit(2)
1681 FIXME("Please move existing file '%s' to file '%s'"
1682 "when Wine has finished\n",
1683 full_name1
.long_name
, full_name2
.long_name
);
1687 if (!mode
) /* move the file */
1688 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1694 else /* copy File */
1695 return CopyFileA(fn1
, fn2
, (!(flag
& MOVEFILE_REPLACE_EXISTING
)));
1699 /**************************************************************************
1700 * MoveFileEx32W (KERNEL32.???)
1702 BOOL WINAPI
MoveFileExW( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1704 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1705 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1706 BOOL res
= MoveFileExA( afn1
, afn2
, flag
);
1707 HeapFree( GetProcessHeap(), 0, afn1
);
1708 HeapFree( GetProcessHeap(), 0, afn2
);
1713 /**************************************************************************
1714 * MoveFile32A (KERNEL32.387)
1716 * Move file or directory
1718 BOOL WINAPI
MoveFileA( LPCSTR fn1
, LPCSTR fn2
)
1720 DOS_FULL_NAME full_name1
, full_name2
;
1723 TRACE("(%s,%s)\n", fn1
, fn2
);
1725 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1726 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1727 /* The new name must not already exist */
1729 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1731 if (full_name1
.drive
== full_name2
.drive
) /* move */
1732 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1739 if (stat( full_name1
.long_name
, &fstat
))
1741 WARN("Invalid source file %s\n",
1742 full_name1
.long_name
);
1746 if (S_ISDIR(fstat
.st_mode
)) {
1747 /* No Move for directories across file systems */
1748 /* FIXME: Use right error code */
1749 SetLastError( ERROR_GEN_FAILURE
);
1753 return CopyFileA(fn1
, fn2
, TRUE
); /*fail, if exist */
1758 /**************************************************************************
1759 * MoveFile32W (KERNEL32.390)
1761 BOOL WINAPI
MoveFileW( LPCWSTR fn1
, LPCWSTR fn2
)
1763 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1764 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1765 BOOL res
= MoveFileA( afn1
, afn2
);
1766 HeapFree( GetProcessHeap(), 0, afn1
);
1767 HeapFree( GetProcessHeap(), 0, afn2
);
1772 /**************************************************************************
1773 * CopyFile32A (KERNEL32.36)
1775 BOOL WINAPI
CopyFileA( LPCSTR source
, LPCSTR dest
, BOOL fail_if_exists
)
1778 BY_HANDLE_FILE_INFORMATION info
;
1784 if ((h1
= _lopen( source
, OF_READ
)) == HFILE_ERROR
) return FALSE
;
1785 if (!GetFileInformationByHandle( h1
, &info
))
1790 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1791 if ((h2
= CreateFileA( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1792 fail_if_exists
? CREATE_NEW
: CREATE_ALWAYS
,
1793 info
.dwFileAttributes
, h1
)) == HFILE_ERROR
)
1798 while ((count
= _lread( h1
, buffer
, sizeof(buffer
) )) > 0)
1803 INT res
= _lwrite( h2
, p
, count
);
1804 if (res
<= 0) goto done
;
1817 /**************************************************************************
1818 * CopyFile32W (KERNEL32.37)
1820 BOOL WINAPI
CopyFileW( LPCWSTR source
, LPCWSTR dest
, BOOL fail_if_exists
)
1822 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1823 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1824 BOOL ret
= CopyFileA( sourceA
, destA
, fail_if_exists
);
1825 HeapFree( GetProcessHeap(), 0, sourceA
);
1826 HeapFree( GetProcessHeap(), 0, destA
);
1831 /**************************************************************************
1832 * CopyFileEx32A (KERNEL32.858)
1834 * This implementation ignores most of the extra parameters passed-in into
1835 * the "ex" version of the method and calls the CopyFile method.
1836 * It will have to be fixed eventually.
1838 BOOL WINAPI
CopyFileExA(LPCSTR sourceFilename
,
1839 LPCSTR destFilename
,
1840 LPPROGRESS_ROUTINE progressRoutine
,
1842 LPBOOL cancelFlagPointer
,
1845 BOOL failIfExists
= FALSE
;
1848 * Interpret the only flag that CopyFile can interpret.
1850 if ( (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0)
1852 failIfExists
= TRUE
;
1855 return CopyFileA(sourceFilename
, destFilename
, failIfExists
);
1858 /**************************************************************************
1859 * CopyFileEx32W (KERNEL32.859)
1861 BOOL WINAPI
CopyFileExW(LPCWSTR sourceFilename
,
1862 LPCWSTR destFilename
,
1863 LPPROGRESS_ROUTINE progressRoutine
,
1865 LPBOOL cancelFlagPointer
,
1868 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename
);
1869 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename
);
1871 BOOL ret
= CopyFileExA(sourceA
,
1878 HeapFree( GetProcessHeap(), 0, sourceA
);
1879 HeapFree( GetProcessHeap(), 0, destA
);
1885 /***********************************************************************
1886 * SetFileTime (KERNEL32.650)
1888 BOOL WINAPI
SetFileTime( HANDLE hFile
,
1889 const FILETIME
*lpCreationTime
,
1890 const FILETIME
*lpLastAccessTime
,
1891 const FILETIME
*lpLastWriteTime
)
1893 struct set_file_time_request
*req
= get_req_buffer();
1895 req
->handle
= hFile
;
1896 if (lpLastAccessTime
)
1897 req
->access_time
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1899 req
->access_time
= 0; /* FIXME */
1900 if (lpLastWriteTime
)
1901 req
->write_time
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1903 req
->write_time
= 0; /* FIXME */
1904 return !server_call( REQ_SET_FILE_TIME
);
1908 /**************************************************************************
1909 * LockFile (KERNEL32.511)
1911 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1912 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
)
1914 struct lock_file_request
*req
= get_req_buffer();
1916 req
->handle
= hFile
;
1917 req
->offset_low
= dwFileOffsetLow
;
1918 req
->offset_high
= dwFileOffsetHigh
;
1919 req
->count_low
= nNumberOfBytesToLockLow
;
1920 req
->count_high
= nNumberOfBytesToLockHigh
;
1921 return !server_call( REQ_LOCK_FILE
);
1924 /**************************************************************************
1925 * LockFileEx [KERNEL32.512]
1927 * Locks a byte range within an open file for shared or exclusive access.
1934 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1936 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1937 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
,
1938 LPOVERLAPPED pOverlapped
)
1940 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1941 hFile
, flags
, reserved
, nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
,
1944 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1947 ERR("reserved == %ld: Supposed to be 0??\n", reserved
);
1948 SetLastError(ERROR_INVALID_PARAMETER
);
1955 /**************************************************************************
1956 * UnlockFile (KERNEL32.703)
1958 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1959 DWORD nNumberOfBytesToUnlockLow
, DWORD nNumberOfBytesToUnlockHigh
)
1961 struct unlock_file_request
*req
= get_req_buffer();
1963 req
->handle
= hFile
;
1964 req
->offset_low
= dwFileOffsetLow
;
1965 req
->offset_high
= dwFileOffsetHigh
;
1966 req
->count_low
= nNumberOfBytesToUnlockLow
;
1967 req
->count_high
= nNumberOfBytesToUnlockHigh
;
1968 return !server_call( REQ_UNLOCK_FILE
);
1972 /**************************************************************************
1973 * UnlockFileEx (KERNEL32.705)
1975 BOOL WINAPI
UnlockFileEx(
1978 DWORD nNumberOfBytesToUnlockLow
,
1979 DWORD nNumberOfBytesToUnlockHigh
,
1980 LPOVERLAPPED lpOverlapped
1983 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1984 hFile
, dwReserved
, nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
,
1986 if (dwReserved
== 0)
1987 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1990 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved
);
1991 SetLastError(ERROR_INVALID_PARAMETER
);
2000 struct DOS_FILE_LOCK
{
2001 struct DOS_FILE_LOCK
* next
;
2005 FILE_OBJECT
* dos_file
;
2006 /* char * unix_name;*/
2009 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
2011 static DOS_FILE_LOCK
*locks
= NULL
;
2012 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
2015 /* Locks need to be mirrored because unix file locking is based
2016 * on the pid. Inside of wine there can be multiple WINE processes
2017 * that share the same unix pid.
2018 * Read's and writes should check these locks also - not sure
2019 * how critical that is at this point (FIXME).
2022 static BOOL
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
2024 DOS_FILE_LOCK
*curr
;
2027 processId
= GetCurrentProcessId();
2029 /* check if lock overlaps a current lock for the same file */
2031 for (curr
= locks
; curr
; curr
= curr
->next
) {
2032 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
2033 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
2034 return TRUE
;/* region is identic */
2035 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
2036 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
2037 /* region overlaps */
2044 curr
= HeapAlloc( SystemHeap
, 0, sizeof(DOS_FILE_LOCK
) );
2045 curr
->processId
= GetCurrentProcessId();
2046 curr
->base
= f
->l_start
;
2047 curr
->len
= f
->l_len
;
2048 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
2050 curr
->dos_file
= file
;
2055 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
2058 DOS_FILE_LOCK
**curr
;
2061 processId
= GetCurrentProcessId();
2064 if ((*curr
)->dos_file
== file
) {
2066 *curr
= (*curr
)->next
;
2067 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2068 HeapFree( SystemHeap
, 0, rem
);
2071 curr
= &(*curr
)->next
;
2075 static BOOL
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2078 DOS_FILE_LOCK
**curr
;
2081 processId
= GetCurrentProcessId();
2082 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2083 if ((*curr
)->processId
== processId
&&
2084 (*curr
)->dos_file
== file
&&
2085 (*curr
)->base
== f
->l_start
&&
2086 (*curr
)->len
== f
->l_len
) {
2087 /* this is the same lock */
2089 *curr
= (*curr
)->next
;
2090 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2091 HeapFree( SystemHeap
, 0, rem
);
2095 /* no matching lock found */
2100 /**************************************************************************
2101 * LockFile (KERNEL32.511)
2103 BOOL WINAPI
LockFile(
2104 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2105 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2110 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2111 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2112 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2114 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2115 FIXME("Unimplemented bytes > 32bits\n");
2119 f
.l_start
= dwFileOffsetLow
;
2120 f
.l_len
= nNumberOfBytesToLockLow
;
2121 f
.l_whence
= SEEK_SET
;
2125 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2127 /* shadow locks internally */
2128 if (!DOS_AddLock(file
, &f
)) {
2129 SetLastError( ERROR_LOCK_VIOLATION
);
2133 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2134 #ifdef USE_UNIX_LOCKS
2135 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2136 if (errno
== EACCES
|| errno
== EAGAIN
) {
2137 SetLastError( ERROR_LOCK_VIOLATION
);
2142 /* remove our internal copy of the lock */
2143 DOS_RemoveLock(file
, &f
);
2151 /**************************************************************************
2152 * UnlockFile (KERNEL32.703)
2154 BOOL WINAPI
UnlockFile(
2155 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2156 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2161 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2162 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2163 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2165 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2166 WARN("Unimplemented bytes > 32bits\n");
2170 f
.l_start
= dwFileOffsetLow
;
2171 f
.l_len
= nNumberOfBytesToUnlockLow
;
2172 f
.l_whence
= SEEK_SET
;
2176 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2178 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2180 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2181 #ifdef USE_UNIX_LOCKS
2182 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2191 /**************************************************************************
2192 * GetFileAttributesEx32A [KERNEL32.874]
2194 BOOL WINAPI
GetFileAttributesExA(
2195 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2196 LPVOID lpFileInformation
)
2198 DOS_FULL_NAME full_name
;
2199 BY_HANDLE_FILE_INFORMATION info
;
2201 if (lpFileName
== NULL
) return FALSE
;
2202 if (lpFileInformation
== NULL
) return FALSE
;
2204 if (fInfoLevelId
== GetFileExInfoStandard
) {
2205 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2206 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2207 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2208 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2210 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2211 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2212 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2213 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2214 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2215 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2218 FIXME("invalid info level %d!\n", fInfoLevelId
);
2226 /**************************************************************************
2227 * GetFileAttributesEx32W [KERNEL32.875]
2229 BOOL WINAPI
GetFileAttributesExW(
2230 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2231 LPVOID lpFileInformation
)
2233 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2235 GetFileAttributesExA( nameA
, fInfoLevelId
, lpFileInformation
);
2236 HeapFree( GetProcessHeap(), 0, nameA
);