2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
8 * Fix the CopyFileEx methods to implement the "extended" functionality.
9 * Right now, they simply call the CopyFile method.
21 #ifdef HAVE_SYS_ERRNO_H
22 #include <sys/errno.h>
24 #include <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
37 #include "wine/winbase16.h"
38 #include "wine/winestring.h"
47 #include "debugtools.h"
51 DEFAULT_DEBUG_CHANNEL(file
);
53 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
54 #define MAP_ANON MAP_ANONYMOUS
57 /* Size of per-process table of DOS handles */
58 #define DOS_TABLE_SIZE 256
60 static HANDLE dos_handles
[DOS_TABLE_SIZE
];
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 SetLastError( ERROR_BAD_FORMAT
);
299 perror( "FILE_SetDosError: unknown errno" );
300 SetLastError( ERROR_GEN_FAILURE
);
307 /***********************************************************************
310 * Duplicate a Unix handle into a task handle.
312 HFILE
FILE_DupUnixHandle( int fd
, DWORD access
)
314 struct alloc_file_handle_request
*req
= get_req_buffer();
315 req
->access
= access
;
316 server_call_fd( REQ_ALLOC_FILE_HANDLE
, fd
, NULL
);
321 /***********************************************************************
324 * Retrieve the Unix handle corresponding to a file handle.
326 int FILE_GetUnixHandle( HANDLE handle
, DWORD access
)
328 int unix_handle
= -1;
329 if (access
== GENERIC_READ
)
331 struct get_read_fd_request
*req
= get_req_buffer();
332 req
->handle
= handle
;
333 server_call_fd( REQ_GET_READ_FD
, -1, &unix_handle
);
335 else if (access
== GENERIC_WRITE
)
337 struct get_write_fd_request
*req
= get_req_buffer();
338 req
->handle
= handle
;
339 server_call_fd( REQ_GET_WRITE_FD
, -1, &unix_handle
);
341 else ERR( "bad access %08lx\n", access
);
346 /***********************************************************************
349 * Implementation of CreateFile. Takes a Unix path name.
351 HANDLE
FILE_CreateFile( LPCSTR filename
, DWORD access
, DWORD sharing
,
352 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
353 DWORD attributes
, HANDLE
template, BOOL fail_read_only
)
357 size_t len
= strlen(filename
);
359 if (len
> REQUEST_MAX_VAR_SIZE
)
361 FIXME("filename '%s' too long\n", filename
);
362 SetLastError( ERROR_INVALID_PARAMETER
);
369 struct create_file_request
*req
= server_alloc_req( sizeof(*req
), len
);
370 req
->access
= access
;
371 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
372 req
->sharing
= sharing
;
373 req
->create
= creation
;
374 req
->attrs
= attributes
;
375 memcpy( server_data_ptr(req
), filename
, len
);
377 err
= server_call( REQ_CREATE_FILE
);
382 /* If write access failed, retry without GENERIC_WRITE */
384 if ((ret
== -1) && !fail_read_only
&& (access
& GENERIC_WRITE
))
386 if ((err
== STATUS_MEDIA_WRITE_PROTECTED
) || (err
== STATUS_ACCESS_DENIED
))
388 TRACE("Write access failed for file '%s', trying without "
389 "write access\n", filename
);
390 access
&= ~GENERIC_WRITE
;
396 WARN("Unable to create file '%s' (GLE %ld)\n", filename
,
403 /***********************************************************************
406 * Same as FILE_CreateFile but for a device
408 HFILE
FILE_CreateDevice( int client_id
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
413 struct create_device_request
*req
= server_alloc_req( sizeof(*req
), 0 );
415 req
->access
= access
;
416 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
419 server_call( REQ_CREATE_DEVICE
);
427 /*************************************************************************
428 * CreateFileA [KERNEL32.45] Creates or opens a file or other object
430 * Creates or opens an object, and returns a handle that can be used to
431 * access that object.
435 * filename [I] pointer to filename to be accessed
436 * access [I] access mode requested
437 * sharing [I] share mode
438 * sa [I] pointer to security attributes
439 * creation [I] how to create the file
440 * attributes [I] attributes for newly created file
441 * template [I] handle to file with extended attributes to copy
444 * Success: Open handle to specified file
445 * Failure: INVALID_HANDLE_VALUE
448 * Should call SetLastError() on failure.
452 * Doesn't support character devices, pipes, template files, or a
453 * lot of the 'attributes' flags yet.
455 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
456 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
457 DWORD attributes
, HANDLE
template )
459 DOS_FULL_NAME full_name
;
463 SetLastError( ERROR_INVALID_PARAMETER
);
466 TRACE("%s %s%s%s%s%s%s%s\n",filename
,
467 ((access
& GENERIC_READ
)==GENERIC_READ
)?"GENERIC_READ ":"",
468 ((access
& GENERIC_WRITE
)==GENERIC_WRITE
)?"GENERIC_WRITE ":"",
469 (!access
)?"QUERY_ACCESS ":"",
470 ((sharing
& FILE_SHARE_READ
)==FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
471 ((sharing
& FILE_SHARE_WRITE
)==FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
472 ((sharing
& FILE_SHARE_DELETE
)==FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
473 (creation
==CREATE_NEW
)?"CREATE_NEW":
474 (creation
==CREATE_ALWAYS
)?"CREATE_ALWAYS ":
475 (creation
==OPEN_EXISTING
)?"OPEN_EXISTING ":
476 (creation
==OPEN_ALWAYS
)?"OPEN_ALWAYS ":
477 (creation
==TRUNCATE_EXISTING
)?"TRUNCATE_EXISTING ":"");
479 /* If the name starts with '\\?\', ignore the first 4 chars. */
480 if (!strncmp(filename
, "\\\\?\\", 4))
483 if (!strncmp(filename
, "UNC\\", 4))
485 FIXME("UNC name (%s) not supported.\n", filename
);
486 SetLastError( ERROR_PATH_NOT_FOUND
);
491 if (!strncmp(filename
, "\\\\.\\", 4)) {
492 if (!DOSFS_GetDevice( filename
))
493 return DEVICE_Open( filename
+4, access
, sa
);
495 filename
+=4; /* fall into DOSFS_Device case below */
498 /* If the name still starts with '\\', it's a UNC name. */
499 if (!strncmp(filename
, "\\\\", 2))
501 FIXME("UNC name (%s) not supported.\n", filename
);
502 SetLastError( ERROR_PATH_NOT_FOUND
);
506 /* If the name contains a DOS wild card (* or ?), do no create a file */
507 if(strchr(filename
,'*') || strchr(filename
,'?'))
510 /* Open a console for CONIN$ or CONOUT$ */
511 if (!strcasecmp(filename
, "CONIN$")) return CONSOLE_OpenHandle( FALSE
, access
, sa
);
512 if (!strcasecmp(filename
, "CONOUT$")) return CONSOLE_OpenHandle( TRUE
, access
, sa
);
514 if (DOSFS_GetDevice( filename
))
518 TRACE("opening device '%s'\n", filename
);
520 if (HFILE_ERROR
!=(ret
=DOSFS_OpenDevice( filename
, access
)))
523 /* Do not silence this please. It is a critical error. -MM */
524 ERR("Couldn't open device '%s'!\n",filename
);
525 SetLastError( ERROR_FILE_NOT_FOUND
);
529 /* check for filename, don't check for last entry if creating */
530 if (!DOSFS_GetFullName( filename
,
531 (creation
== OPEN_EXISTING
) ||
532 (creation
== TRUNCATE_EXISTING
),
534 WARN("Unable to get full filename from '%s' (GLE %ld)\n",
535 filename
, GetLastError());
539 return FILE_CreateFile( full_name
.long_name
, access
, sharing
,
540 sa
, creation
, attributes
, template,
541 DRIVE_GetFlags(full_name
.drive
) & DRIVE_FAIL_READ_ONLY
);
546 /*************************************************************************
547 * CreateFileW (KERNEL32.48)
549 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
550 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
551 DWORD attributes
, HANDLE
template)
553 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
554 HANDLE res
= CreateFileA( afn
, access
, sharing
, sa
, creation
, attributes
, template );
555 HeapFree( GetProcessHeap(), 0, afn
);
560 /***********************************************************************
563 * Fill a file information from a struct stat.
565 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
567 if (S_ISDIR(st
->st_mode
))
568 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
570 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
571 if (!(st
->st_mode
& S_IWUSR
))
572 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
574 RtlSecondsSince1970ToTime( st
->st_mtime
, &info
->ftCreationTime
);
575 RtlSecondsSince1970ToTime( st
->st_mtime
, &info
->ftLastWriteTime
);
576 RtlSecondsSince1970ToTime( st
->st_atime
, &info
->ftLastAccessTime
);
578 info
->dwVolumeSerialNumber
= 0; /* FIXME */
579 info
->nFileSizeHigh
= 0;
580 info
->nFileSizeLow
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
581 info
->nNumberOfLinks
= st
->st_nlink
;
582 info
->nFileIndexHigh
= 0;
583 info
->nFileIndexLow
= st
->st_ino
;
587 /***********************************************************************
590 * Stat a Unix path name. Return TRUE if OK.
592 BOOL
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
596 if (!unixName
|| !info
) return FALSE
;
598 if (stat( unixName
, &st
) == -1)
603 FILE_FillInfo( &st
, info
);
608 /***********************************************************************
609 * GetFileInformationByHandle (KERNEL32.219)
611 DWORD WINAPI
GetFileInformationByHandle( HANDLE hFile
,
612 BY_HANDLE_FILE_INFORMATION
*info
)
619 struct get_file_info_request
*req
= server_alloc_req( sizeof(*req
), 0 );
621 if ((ret
= !server_call( REQ_GET_FILE_INFO
)))
623 RtlSecondsSince1970ToTime( req
->write_time
, &info
->ftCreationTime
);
624 RtlSecondsSince1970ToTime( req
->write_time
, &info
->ftLastWriteTime
);
625 RtlSecondsSince1970ToTime( req
->access_time
, &info
->ftLastAccessTime
);
626 info
->dwFileAttributes
= req
->attr
;
627 info
->dwVolumeSerialNumber
= req
->serial
;
628 info
->nFileSizeHigh
= req
->size_high
;
629 info
->nFileSizeLow
= req
->size_low
;
630 info
->nNumberOfLinks
= req
->links
;
631 info
->nFileIndexHigh
= req
->index_high
;
632 info
->nFileIndexLow
= req
->index_low
;
640 /**************************************************************************
641 * GetFileAttributes16 (KERNEL.420)
643 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
645 return GetFileAttributesA( name
);
649 /**************************************************************************
650 * GetFileAttributesA (KERNEL32.217)
652 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
654 DOS_FULL_NAME full_name
;
655 BY_HANDLE_FILE_INFORMATION info
;
657 if (name
== NULL
|| *name
=='\0') return -1;
659 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
)) return -1;
660 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
661 return info
.dwFileAttributes
;
665 /**************************************************************************
666 * GetFileAttributesW (KERNEL32.218)
668 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
670 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
671 DWORD res
= GetFileAttributesA( nameA
);
672 HeapFree( GetProcessHeap(), 0, nameA
);
677 /***********************************************************************
678 * GetFileSize (KERNEL32.220)
680 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
682 BY_HANDLE_FILE_INFORMATION info
;
683 if (!GetFileInformationByHandle( hFile
, &info
)) return 0;
684 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
685 return info
.nFileSizeLow
;
689 /***********************************************************************
690 * GetFileTime (KERNEL32.221)
692 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
693 FILETIME
*lpLastAccessTime
,
694 FILETIME
*lpLastWriteTime
)
696 BY_HANDLE_FILE_INFORMATION info
;
697 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
698 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
699 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
700 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
704 /***********************************************************************
705 * CompareFileTime (KERNEL32.28)
707 INT WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
709 if (!x
|| !y
) return -1;
711 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
713 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
715 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
717 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
722 /***********************************************************************
723 * FILE_GetTempFileName : utility for GetTempFileName
725 static UINT
FILE_GetTempFileName( LPCSTR path
, LPCSTR prefix
, UINT unique
,
726 LPSTR buffer
, BOOL isWin16
)
728 static UINT unique_temp
;
729 DOS_FULL_NAME full_name
;
734 if ( !path
|| !prefix
|| !buffer
) return 0;
736 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
737 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
739 strcpy( buffer
, path
);
740 p
= buffer
+ strlen(buffer
);
742 /* add a \, if there isn't one and path is more than just the drive letter ... */
743 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
744 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
746 if (isWin16
) *p
++ = '~';
747 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
748 sprintf( p
, "%04x.tmp", num
);
750 /* Now try to create it */
756 HFILE handle
= CreateFileA( buffer
, GENERIC_WRITE
, 0, NULL
,
757 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, -1 );
758 if (handle
!= INVALID_HANDLE_VALUE
)
759 { /* We created it */
760 TRACE("created %s\n",
762 CloseHandle( handle
);
765 if (GetLastError() != ERROR_FILE_EXISTS
)
766 break; /* No need to go on */
768 sprintf( p
, "%04x.tmp", num
);
769 } while (num
!= (unique
& 0xffff));
772 /* Get the full path name */
774 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
776 /* Check if we have write access in the directory */
777 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
778 if (access( full_name
.long_name
, W_OK
) == -1)
779 WARN("returns '%s', which doesn't seem to be writeable.\n",
782 TRACE("returning %s\n", buffer
);
783 return unique
? unique
: num
;
787 /***********************************************************************
788 * GetTempFileNameA (KERNEL32.290)
790 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
,
793 return FILE_GetTempFileName(path
, prefix
, unique
, buffer
, FALSE
);
796 /***********************************************************************
797 * GetTempFileNameW (KERNEL32.291)
799 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
,
807 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
808 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
809 ret
= FILE_GetTempFileName( patha
, prefixa
, unique
, buffera
, FALSE
);
810 lstrcpyAtoW( buffer
, buffera
);
811 HeapFree( GetProcessHeap(), 0, patha
);
812 HeapFree( GetProcessHeap(), 0, prefixa
);
817 /***********************************************************************
818 * GetTempFileName16 (KERNEL.97)
820 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
825 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
826 drive
|= DRIVE_GetCurrentDrive() + 'A';
828 if ((drive
& TF_FORCEDRIVE
) &&
829 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
831 drive
&= ~TF_FORCEDRIVE
;
832 WARN("invalid drive %d specified\n", drive
);
835 if (drive
& TF_FORCEDRIVE
)
836 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
838 GetTempPathA( 132, temppath
);
839 return (UINT16
)FILE_GetTempFileName( temppath
, prefix
, unique
, buffer
, TRUE
);
842 /***********************************************************************
845 * Implementation of OpenFile16() and OpenFile32().
847 static HFILE
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
,
852 WORD filedatetime
[2];
853 DOS_FULL_NAME full_name
;
854 DWORD access
, sharing
;
857 if (!ofs
) return HFILE_ERROR
;
859 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
860 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
861 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
862 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
863 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
864 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
865 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
866 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
867 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
868 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
869 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
870 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
871 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
872 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
873 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
874 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
875 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
876 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
880 ofs
->cBytes
= sizeof(OFSTRUCT
);
882 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
885 ERR("called with `name' set to NULL ! Please debug.\n");
889 TRACE("%s %04x\n", name
, mode
);
891 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
892 Are there any cases where getting the path here is wrong?
893 Uwe Bonnes 1997 Apr 2 */
894 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
),
895 ofs
->szPathName
, NULL
)) goto error
;
896 FILE_ConvertOFMode( mode
, &access
, &sharing
);
898 /* OF_PARSE simply fills the structure */
902 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
904 TRACE("(%s): OF_PARSE, res = '%s'\n",
905 name
, ofs
->szPathName
);
909 /* OF_CREATE is completely different from all other options, so
912 if (mode
& OF_CREATE
)
914 if ((hFileRet
= CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
915 sharing
, NULL
, CREATE_ALWAYS
,
916 FILE_ATTRIBUTE_NORMAL
, -1 ))== INVALID_HANDLE_VALUE
)
921 /* If OF_SEARCH is set, ignore the given path */
923 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
925 /* First try the file name as is */
926 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
927 /* Now remove the path */
928 if (name
[0] && (name
[1] == ':')) name
+= 2;
929 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
930 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
931 if (!name
[0]) goto not_found
;
934 /* Now look for the file */
936 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
939 TRACE("found %s = %s\n",
940 full_name
.long_name
, full_name
.short_name
);
941 lstrcpynA( ofs
->szPathName
, full_name
.short_name
,
942 sizeof(ofs
->szPathName
) );
944 if (mode
& OF_SHARE_EXCLUSIVE
)
945 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
946 on the file <tempdir>/_ins0432._mp to determine how
947 far installation has proceeded.
948 _ins0432._mp is an executable and while running the
949 application expects the open with OF_SHARE_ to fail*/
951 As our loader closes the files after loading the executable,
952 we can't find the running executable with FILE_InUse.
953 Perhaps the loader should keep the file open.
954 Recheck against how Win handles that case */
956 char *last
= strrchr(full_name
.long_name
,'/');
958 last
= full_name
.long_name
- 1;
959 if (GetModuleHandle16(last
+1))
961 TRACE("Denying shared open for %s\n",full_name
.long_name
);
966 if (mode
& OF_DELETE
)
968 if (unlink( full_name
.long_name
) == -1) goto not_found
;
969 TRACE("(%s): OF_DELETE return = OK\n", name
);
973 hFileRet
= FILE_CreateFile( full_name
.long_name
, access
, sharing
,
974 NULL
, OPEN_EXISTING
, 0, -1,
975 DRIVE_GetFlags(full_name
.drive
) & DRIVE_FAIL_READ_ONLY
);
976 if (hFileRet
== HFILE_ERROR
) goto not_found
;
978 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
979 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
980 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
982 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
984 CloseHandle( hFileRet
);
985 WARN("(%s): OF_VERIFY failed\n", name
);
986 /* FIXME: what error here? */
987 SetLastError( ERROR_FILE_NOT_FOUND
);
991 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
993 success
: /* We get here if the open was successful */
994 TRACE("(%s): OK, return = %d\n", name
, hFileRet
);
997 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
998 CloseHandle( hFileRet
);
1002 hFileRet
= FILE_AllocDosHandle( hFileRet
);
1003 if (hFileRet
== HFILE_ERROR16
) goto error
;
1004 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
1005 _lclose16( hFileRet
);
1009 not_found
: /* We get here if the file does not exist */
1010 WARN("'%s' not found or sharing violation\n", name
);
1011 SetLastError( ERROR_FILE_NOT_FOUND
);
1014 error
: /* We get here if there was an error opening the file */
1015 ofs
->nErrCode
= GetLastError();
1016 WARN("(%s): return = HFILE_ERROR error= %d\n",
1017 name
,ofs
->nErrCode
);
1022 /***********************************************************************
1023 * OpenFile16 (KERNEL.74)
1025 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
1027 return FILE_DoOpenFile( name
, ofs
, mode
, FALSE
);
1031 /***********************************************************************
1032 * OpenFile (KERNEL32.396)
1034 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
1036 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
1040 /***********************************************************************
1041 * FILE_InitProcessDosHandles
1043 * Allocates the default DOS handles for a process. Called either by
1044 * AllocDosHandle below or by the DOSVM stuff.
1046 static void FILE_InitProcessDosHandles( void )
1048 dos_handles
[0] = GetStdHandle(STD_INPUT_HANDLE
);
1049 dos_handles
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
1050 dos_handles
[2] = GetStdHandle(STD_ERROR_HANDLE
);
1051 dos_handles
[3] = GetStdHandle(STD_ERROR_HANDLE
);
1052 dos_handles
[4] = GetStdHandle(STD_ERROR_HANDLE
);
1055 /***********************************************************************
1056 * FILE_AllocDosHandle
1058 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1059 * longer valid after this function (even on failure).
1061 HFILE16
FILE_AllocDosHandle( HANDLE handle
)
1065 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
1066 return INVALID_HANDLE_VALUE16
;
1068 for (i
= 5; i
< DOS_TABLE_SIZE
; i
++)
1069 if (!dos_handles
[i
])
1071 dos_handles
[i
] = handle
;
1072 TRACE("Got %d for h32 %d\n", i
, handle
);
1075 CloseHandle( handle
);
1076 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
1077 return INVALID_HANDLE_VALUE16
;
1081 /***********************************************************************
1084 * Return the Win32 handle for a DOS handle.
1086 HANDLE
FILE_GetHandle( HFILE16 hfile
)
1088 if (hfile
< 5 && !dos_handles
[hfile
]) FILE_InitProcessDosHandles();
1089 if ((hfile
>= DOS_TABLE_SIZE
) || !dos_handles
[hfile
])
1091 SetLastError( ERROR_INVALID_HANDLE
);
1092 return INVALID_HANDLE_VALUE
;
1094 return dos_handles
[hfile
];
1098 /***********************************************************************
1101 * dup2() function for DOS handles.
1103 HFILE16
FILE_Dup2( HFILE16 hFile1
, HFILE16 hFile2
)
1107 if (hFile1
< 5 && !dos_handles
[hFile1
]) FILE_InitProcessDosHandles();
1109 if ((hFile1
>= DOS_TABLE_SIZE
) || (hFile2
>= DOS_TABLE_SIZE
) || !dos_handles
[hFile1
])
1111 SetLastError( ERROR_INVALID_HANDLE
);
1112 return HFILE_ERROR16
;
1116 FIXME("stdio handle closed, need proper conversion\n" );
1117 SetLastError( ERROR_INVALID_HANDLE
);
1118 return HFILE_ERROR16
;
1120 if (!DuplicateHandle( GetCurrentProcess(), dos_handles
[hFile1
],
1121 GetCurrentProcess(), &new_handle
,
1122 0, FALSE
, DUPLICATE_SAME_ACCESS
))
1123 return HFILE_ERROR16
;
1124 if (dos_handles
[hFile2
]) CloseHandle( dos_handles
[hFile2
] );
1125 dos_handles
[hFile2
] = new_handle
;
1130 /***********************************************************************
1131 * _lclose16 (KERNEL.81)
1133 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1137 FIXME("stdio handle closed, need proper conversion\n" );
1138 SetLastError( ERROR_INVALID_HANDLE
);
1139 return HFILE_ERROR16
;
1141 if ((hFile
>= DOS_TABLE_SIZE
) || !dos_handles
[hFile
])
1143 SetLastError( ERROR_INVALID_HANDLE
);
1144 return HFILE_ERROR16
;
1146 TRACE("%d (handle32=%d)\n", hFile
, dos_handles
[hFile
] );
1147 CloseHandle( dos_handles
[hFile
] );
1148 dos_handles
[hFile
] = 0;
1153 /***********************************************************************
1154 * _lclose (KERNEL32.592)
1156 HFILE WINAPI
_lclose( HFILE hFile
)
1158 TRACE("handle %d\n", hFile
);
1159 return CloseHandle( hFile
) ? 0 : HFILE_ERROR
;
1162 /***********************************************************************
1163 * GetOverlappedResult (KERNEL32.360)
1165 BOOL WINAPI
GetOverlappedResult(HANDLE hFile
,LPOVERLAPPED lpOverlapped
,
1166 LPDWORD lpNumberOfBytesTransferred
,
1169 /* Since all i/o is currently synchronous,
1170 * return true, assuming ReadFile/WriteFile
1171 * have completed the operation */
1172 FIXME("NO Asynch I/O, assuming Read/Write succeeded\n" );
1176 /***********************************************************************
1177 * ReadFile (KERNEL32.428)
1179 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
1180 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
1182 int unix_handle
, result
;
1184 TRACE("%d %p %ld\n", hFile
, buffer
, bytesToRead
);
1186 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
1187 if (!bytesToRead
) return TRUE
;
1190 SetLastError ( ERROR_INVALID_PARAMETER
);
1194 unix_handle
= FILE_GetUnixHandle( hFile
, GENERIC_READ
);
1195 if (unix_handle
== -1) return FALSE
;
1196 while ((result
= read( unix_handle
, buffer
, bytesToRead
)) == -1)
1198 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1199 if ((errno
== EFAULT
) && !VIRTUAL_HandleFault( buffer
)) continue;
1203 close( unix_handle
);
1204 if (result
== -1) return FALSE
;
1205 if (bytesRead
) *bytesRead
= result
;
1210 /***********************************************************************
1211 * WriteFile (KERNEL32.578)
1213 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
1214 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
1216 int unix_handle
, result
;
1218 TRACE("%d %p %ld\n", hFile
, buffer
, bytesToWrite
);
1220 if (bytesWritten
) *bytesWritten
= 0; /* Do this before anything else */
1221 if (!bytesToWrite
) return TRUE
;
1224 SetLastError ( ERROR_INVALID_PARAMETER
);
1228 unix_handle
= FILE_GetUnixHandle( hFile
, GENERIC_WRITE
);
1229 if (unix_handle
== -1) return FALSE
;
1230 while ((result
= write( unix_handle
, buffer
, bytesToWrite
)) == -1)
1232 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1233 if ((errno
== EFAULT
) && !VIRTUAL_HandleFault( buffer
)) continue;
1234 if (errno
== ENOSPC
)
1235 SetLastError( ERROR_DISK_FULL
);
1240 close( unix_handle
);
1241 if (result
== -1) return FALSE
;
1242 if (bytesWritten
) *bytesWritten
= result
;
1247 /***********************************************************************
1250 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1254 TRACE("%d %08lx %ld\n",
1255 hFile
, (DWORD
)buffer
, count
);
1257 /* Some programs pass a count larger than the allocated buffer */
1258 maxlen
= GetSelectorLimit16( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1259 if (count
> maxlen
) count
= maxlen
;
1260 return _lread(FILE_GetHandle(hFile
), PTR_SEG_TO_LIN(buffer
), count
);
1264 /***********************************************************************
1267 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1269 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1273 /***********************************************************************
1274 * _lread (KERNEL32.596)
1276 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
1279 if (!ReadFile( handle
, buffer
, count
, &result
, NULL
)) return -1;
1284 /***********************************************************************
1285 * _lread16 (KERNEL.82)
1287 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1289 return (UINT16
)_lread(FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1293 /***********************************************************************
1294 * _lcreat16 (KERNEL.83)
1296 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1298 return FILE_AllocDosHandle( _lcreat( path
, attr
) );
1302 /***********************************************************************
1303 * _lcreat (KERNEL32.593)
1305 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
1307 /* Mask off all flags not explicitly allowed by the doc */
1308 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
1309 TRACE("%s %02x\n", path
, attr
);
1310 return CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
1311 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1312 CREATE_ALWAYS
, attr
, -1 );
1316 /***********************************************************************
1317 * SetFilePointer (KERNEL32.492)
1319 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
,
1322 DWORD ret
= 0xffffffff;
1325 ((distance
>= 0 && *highword
!= 0) || (distance
< 0 && *highword
!= -1)))
1327 FIXME("64-bit offsets not supported yet\n"
1328 "SetFilePointer(%08x,%08lx,%08lx,%08lx)\n",
1329 hFile
,distance
,*highword
,method
);
1330 SetLastError( ERROR_INVALID_PARAMETER
);
1333 TRACE("handle %d offset %ld origin %ld\n",
1334 hFile
, distance
, method
);
1338 struct set_file_pointer_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1339 req
->handle
= hFile
;
1340 req
->low
= distance
;
1341 req
->high
= highword
? *highword
: (distance
>= 0) ? 0 : -1;
1342 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1343 req
->whence
= method
;
1345 if (!server_call( REQ_SET_FILE_POINTER
))
1348 if (highword
) *highword
= req
->new_high
;
1356 /***********************************************************************
1357 * _llseek16 (KERNEL.84)
1360 * Seeking before the start of the file should be allowed for _llseek16,
1361 * but cause subsequent I/O operations to fail (cf. interrupt list)
1364 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1366 return SetFilePointer( FILE_GetHandle(hFile
), lOffset
, NULL
, nOrigin
);
1370 /***********************************************************************
1371 * _llseek (KERNEL32.594)
1373 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
1375 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1379 /***********************************************************************
1380 * _lopen16 (KERNEL.85)
1382 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1384 return FILE_AllocDosHandle( _lopen( path
, mode
) );
1388 /***********************************************************************
1389 * _lopen (KERNEL32.595)
1391 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
1393 DWORD access
, sharing
;
1395 TRACE("('%s',%04x)\n", path
, mode
);
1396 FILE_ConvertOFMode( mode
, &access
, &sharing
);
1397 return CreateFileA( path
, access
, sharing
, NULL
, OPEN_EXISTING
, 0, -1 );
1401 /***********************************************************************
1402 * _lwrite16 (KERNEL.86)
1404 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1406 return (UINT16
)_hwrite( FILE_GetHandle(hFile
), buffer
, (LONG
)count
);
1409 /***********************************************************************
1410 * _lwrite (KERNEL32.761)
1412 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
1414 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
1418 /***********************************************************************
1419 * _hread16 (KERNEL.349)
1421 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1423 return _lread( FILE_GetHandle(hFile
), buffer
, count
);
1427 /***********************************************************************
1428 * _hread (KERNEL32.590)
1430 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
1432 return _lread( hFile
, buffer
, count
);
1436 /***********************************************************************
1437 * _hwrite16 (KERNEL.350)
1439 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1441 return _hwrite( FILE_GetHandle(hFile
), buffer
, count
);
1445 /***********************************************************************
1446 * _hwrite (KERNEL32.591)
1448 * experimentation yields that _lwrite:
1449 * o truncates the file at the current position with
1451 * o returns 0 on a 0 length write
1452 * o works with console handles
1455 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
1459 TRACE("%d %p %ld\n", handle
, buffer
, count
);
1463 /* Expand or truncate at current position */
1464 if (!SetEndOfFile( handle
)) return HFILE_ERROR
;
1467 if (!WriteFile( handle
, buffer
, count
, &result
, NULL
))
1473 /***********************************************************************
1474 * SetHandleCount16 (KERNEL.199)
1476 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1478 HGLOBAL16 hPDB
= GetCurrentPDB16();
1479 PDB16
*pdb
= (PDB16
*)GlobalLock16( hPDB
);
1480 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1482 TRACE("(%d)\n", count
);
1484 if (count
< 20) count
= 20; /* No point in going below 20 */
1485 else if (count
> 254) count
= 254;
1489 if (pdb
->nbFiles
> 20)
1491 memcpy( pdb
->fileHandles
, files
, 20 );
1492 GlobalFree16( pdb
->hFileHandles
);
1493 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1494 GlobalHandleToSel16( hPDB
) );
1495 pdb
->hFileHandles
= 0;
1499 else /* More than 20, need a new file handles table */
1502 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1505 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1506 return pdb
->nbFiles
;
1508 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1510 if (count
> pdb
->nbFiles
)
1512 memcpy( newfiles
, files
, pdb
->nbFiles
);
1513 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1515 else memcpy( newfiles
, files
, count
);
1516 if (pdb
->nbFiles
> 20) GlobalFree16( pdb
->hFileHandles
);
1517 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1518 pdb
->hFileHandles
= newhandle
;
1519 pdb
->nbFiles
= count
;
1521 return pdb
->nbFiles
;
1525 /*************************************************************************
1526 * SetHandleCount (KERNEL32.494)
1528 UINT WINAPI
SetHandleCount( UINT count
)
1530 return min( 256, count
);
1534 /***********************************************************************
1535 * FlushFileBuffers (KERNEL32.133)
1537 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
1542 struct flush_file_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1543 req
->handle
= hFile
;
1544 ret
= !server_call( REQ_FLUSH_FILE
);
1551 /**************************************************************************
1552 * SetEndOfFile (KERNEL32.483)
1554 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
1559 struct truncate_file_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1560 req
->handle
= hFile
;
1561 ret
= !server_call( REQ_TRUNCATE_FILE
);
1568 /***********************************************************************
1569 * DeleteFile16 (KERNEL.146)
1571 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1573 return DeleteFileA( path
);
1577 /***********************************************************************
1578 * DeleteFileA (KERNEL32.71)
1580 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1582 DOS_FULL_NAME full_name
;
1584 TRACE("'%s'\n", path
);
1588 ERR("Empty path passed\n");
1591 if (DOSFS_GetDevice( path
))
1593 WARN("cannot remove DOS device '%s'!\n", path
);
1594 SetLastError( ERROR_FILE_NOT_FOUND
);
1598 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1599 if (unlink( full_name
.long_name
) == -1)
1608 /***********************************************************************
1609 * DeleteFileW (KERNEL32.72)
1611 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1613 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1614 BOOL ret
= DeleteFileA( xpath
);
1615 HeapFree( GetProcessHeap(), 0, xpath
);
1620 /***********************************************************************
1621 * GetFileType (KERNEL32.222)
1623 DWORD WINAPI
GetFileType( HANDLE hFile
)
1625 DWORD ret
= FILE_TYPE_UNKNOWN
;
1628 struct get_file_info_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1629 req
->handle
= hFile
;
1630 if (!server_call( REQ_GET_FILE_INFO
)) ret
= req
->type
;
1637 /**************************************************************************
1638 * MoveFileExA (KERNEL32.???)
1640 BOOL WINAPI
MoveFileExA( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1642 DOS_FULL_NAME full_name1
, full_name2
;
1644 TRACE("(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1646 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1648 if (fn2
) /* !fn2 means delete fn1 */
1650 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1652 /* target exists, check if we may overwrite */
1653 if (!(flag
& MOVEFILE_REPLACE_EXISTING
))
1655 /* FIXME: Use right error code */
1656 SetLastError( ERROR_ACCESS_DENIED
);
1660 else if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1662 /* Source name and target path are valid */
1664 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
)
1666 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1667 Perhaps we should queue these command and execute it
1668 when exiting... What about using on_exit(2)
1670 FIXME("Please move existing file '%s' to file '%s' when Wine has finished\n",
1671 full_name1
.long_name
, full_name2
.long_name
);
1675 if (full_name1
.drive
!= full_name2
.drive
)
1677 /* use copy, if allowed */
1678 if (!(flag
& MOVEFILE_COPY_ALLOWED
))
1680 /* FIXME: Use right error code */
1681 SetLastError( ERROR_FILE_EXISTS
);
1684 return CopyFileA( fn1
, fn2
, !(flag
& MOVEFILE_REPLACE_EXISTING
) );
1686 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1693 else /* fn2 == NULL means delete source */
1695 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
)
1697 if (flag
& MOVEFILE_COPY_ALLOWED
) {
1698 WARN("Illegal flag\n");
1699 SetLastError( ERROR_GEN_FAILURE
);
1702 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1703 Perhaps we should queue these command and execute it
1704 when exiting... What about using on_exit(2)
1706 FIXME("Please delete file '%s' when Wine has finished\n",
1707 full_name1
.long_name
);
1711 if (unlink( full_name1
.long_name
) == -1)
1716 return TRUE
; /* successfully deleted */
1720 /**************************************************************************
1721 * MoveFileExW (KERNEL32.???)
1723 BOOL WINAPI
MoveFileExW( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
1725 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1726 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1727 BOOL res
= MoveFileExA( afn1
, afn2
, flag
);
1728 HeapFree( GetProcessHeap(), 0, afn1
);
1729 HeapFree( GetProcessHeap(), 0, afn2
);
1734 /**************************************************************************
1735 * MoveFileA (KERNEL32.387)
1737 * Move file or directory
1739 BOOL WINAPI
MoveFileA( LPCSTR fn1
, LPCSTR fn2
)
1741 DOS_FULL_NAME full_name1
, full_name2
;
1744 TRACE("(%s,%s)\n", fn1
, fn2
);
1746 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1747 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
)) {
1748 /* The new name must not already exist */
1749 SetLastError(ERROR_ALREADY_EXISTS
);
1752 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1754 if (full_name1
.drive
== full_name2
.drive
) /* move */
1755 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1762 if (stat( full_name1
.long_name
, &fstat
))
1764 WARN("Invalid source file %s\n",
1765 full_name1
.long_name
);
1769 if (S_ISDIR(fstat
.st_mode
)) {
1770 /* No Move for directories across file systems */
1771 /* FIXME: Use right error code */
1772 SetLastError( ERROR_GEN_FAILURE
);
1776 return CopyFileA(fn1
, fn2
, TRUE
); /*fail, if exist */
1781 /**************************************************************************
1782 * MoveFileW (KERNEL32.390)
1784 BOOL WINAPI
MoveFileW( LPCWSTR fn1
, LPCWSTR fn2
)
1786 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
1787 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
1788 BOOL res
= MoveFileA( afn1
, afn2
);
1789 HeapFree( GetProcessHeap(), 0, afn1
);
1790 HeapFree( GetProcessHeap(), 0, afn2
);
1795 /**************************************************************************
1796 * CopyFileA (KERNEL32.36)
1798 BOOL WINAPI
CopyFileA( LPCSTR source
, LPCSTR dest
, BOOL fail_if_exists
)
1801 BY_HANDLE_FILE_INFORMATION info
;
1807 if ((h1
= _lopen( source
, OF_READ
)) == HFILE_ERROR
) return FALSE
;
1808 if (!GetFileInformationByHandle( h1
, &info
))
1813 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
1814 if ((h2
= CreateFileA( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1815 fail_if_exists
? CREATE_NEW
: CREATE_ALWAYS
,
1816 info
.dwFileAttributes
, h1
)) == HFILE_ERROR
)
1821 while ((count
= _lread( h1
, buffer
, sizeof(buffer
) )) > 0)
1826 INT res
= _lwrite( h2
, p
, count
);
1827 if (res
<= 0) goto done
;
1840 /**************************************************************************
1841 * CopyFileW (KERNEL32.37)
1843 BOOL WINAPI
CopyFileW( LPCWSTR source
, LPCWSTR dest
, BOOL fail_if_exists
)
1845 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
1846 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
1847 BOOL ret
= CopyFileA( sourceA
, destA
, fail_if_exists
);
1848 HeapFree( GetProcessHeap(), 0, sourceA
);
1849 HeapFree( GetProcessHeap(), 0, destA
);
1854 /**************************************************************************
1855 * CopyFileExA (KERNEL32.858)
1857 * This implementation ignores most of the extra parameters passed-in into
1858 * the "ex" version of the method and calls the CopyFile method.
1859 * It will have to be fixed eventually.
1861 BOOL WINAPI
CopyFileExA(LPCSTR sourceFilename
,
1862 LPCSTR destFilename
,
1863 LPPROGRESS_ROUTINE progressRoutine
,
1865 LPBOOL cancelFlagPointer
,
1868 BOOL failIfExists
= FALSE
;
1871 * Interpret the only flag that CopyFile can interpret.
1873 if ( (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0)
1875 failIfExists
= TRUE
;
1878 return CopyFileA(sourceFilename
, destFilename
, failIfExists
);
1881 /**************************************************************************
1882 * CopyFileExW (KERNEL32.859)
1884 BOOL WINAPI
CopyFileExW(LPCWSTR sourceFilename
,
1885 LPCWSTR destFilename
,
1886 LPPROGRESS_ROUTINE progressRoutine
,
1888 LPBOOL cancelFlagPointer
,
1891 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename
);
1892 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename
);
1894 BOOL ret
= CopyFileExA(sourceA
,
1901 HeapFree( GetProcessHeap(), 0, sourceA
);
1902 HeapFree( GetProcessHeap(), 0, destA
);
1908 /***********************************************************************
1909 * SetFileTime (KERNEL32.650)
1911 BOOL WINAPI
SetFileTime( HANDLE hFile
,
1912 const FILETIME
*lpCreationTime
,
1913 const FILETIME
*lpLastAccessTime
,
1914 const FILETIME
*lpLastWriteTime
)
1919 struct set_file_time_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1920 req
->handle
= hFile
;
1921 if (lpLastAccessTime
)
1922 req
->access_time
= DOSFS_FileTimeToUnixTime(lpLastAccessTime
, NULL
);
1924 req
->access_time
= 0; /* FIXME */
1925 if (lpLastWriteTime
)
1926 req
->write_time
= DOSFS_FileTimeToUnixTime(lpLastWriteTime
, NULL
);
1928 req
->write_time
= 0; /* FIXME */
1929 ret
= !server_call( REQ_SET_FILE_TIME
);
1936 /**************************************************************************
1937 * LockFile (KERNEL32.511)
1939 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1940 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
)
1945 struct lock_file_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1947 req
->handle
= hFile
;
1948 req
->offset_low
= dwFileOffsetLow
;
1949 req
->offset_high
= dwFileOffsetHigh
;
1950 req
->count_low
= nNumberOfBytesToLockLow
;
1951 req
->count_high
= nNumberOfBytesToLockHigh
;
1952 ret
= !server_call( REQ_LOCK_FILE
);
1958 /**************************************************************************
1959 * LockFileEx [KERNEL32.512]
1961 * Locks a byte range within an open file for shared or exclusive access.
1968 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1970 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
1971 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
,
1972 LPOVERLAPPED pOverlapped
)
1974 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1975 hFile
, flags
, reserved
, nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
,
1978 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1981 ERR("reserved == %ld: Supposed to be 0??\n", reserved
);
1982 SetLastError(ERROR_INVALID_PARAMETER
);
1989 /**************************************************************************
1990 * UnlockFile (KERNEL32.703)
1992 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
1993 DWORD nNumberOfBytesToUnlockLow
, DWORD nNumberOfBytesToUnlockHigh
)
1998 struct unlock_file_request
*req
= server_alloc_req( sizeof(*req
), 0 );
2000 req
->handle
= hFile
;
2001 req
->offset_low
= dwFileOffsetLow
;
2002 req
->offset_high
= dwFileOffsetHigh
;
2003 req
->count_low
= nNumberOfBytesToUnlockLow
;
2004 req
->count_high
= nNumberOfBytesToUnlockHigh
;
2005 ret
= !server_call( REQ_UNLOCK_FILE
);
2012 /**************************************************************************
2013 * UnlockFileEx (KERNEL32.705)
2015 BOOL WINAPI
UnlockFileEx(
2018 DWORD nNumberOfBytesToUnlockLow
,
2019 DWORD nNumberOfBytesToUnlockHigh
,
2020 LPOVERLAPPED lpOverlapped
2023 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2024 hFile
, dwReserved
, nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
,
2026 if (dwReserved
== 0)
2027 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2030 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved
);
2031 SetLastError(ERROR_INVALID_PARAMETER
);
2040 struct DOS_FILE_LOCK
{
2041 struct DOS_FILE_LOCK
* next
;
2045 FILE_OBJECT
* dos_file
;
2046 /* char * unix_name;*/
2049 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
2051 static DOS_FILE_LOCK
*locks
= NULL
;
2052 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
2055 /* Locks need to be mirrored because unix file locking is based
2056 * on the pid. Inside of wine there can be multiple WINE processes
2057 * that share the same unix pid.
2058 * Read's and writes should check these locks also - not sure
2059 * how critical that is at this point (FIXME).
2062 static BOOL
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
2064 DOS_FILE_LOCK
*curr
;
2067 processId
= GetCurrentProcessId();
2069 /* check if lock overlaps a current lock for the same file */
2071 for (curr
= locks
; curr
; curr
= curr
->next
) {
2072 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
2073 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
2074 return TRUE
;/* region is identic */
2075 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
2076 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
2077 /* region overlaps */
2084 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(DOS_FILE_LOCK
) );
2085 curr
->processId
= GetCurrentProcessId();
2086 curr
->base
= f
->l_start
;
2087 curr
->len
= f
->l_len
;
2088 /* curr->unix_name = HEAP_strdupA( GetProcessHeap(), 0, file->unix_name);*/
2090 curr
->dos_file
= file
;
2095 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
2098 DOS_FILE_LOCK
**curr
;
2101 processId
= GetCurrentProcessId();
2104 if ((*curr
)->dos_file
== file
) {
2106 *curr
= (*curr
)->next
;
2107 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2108 HeapFree( GetProcessHeap(), 0, rem
);
2111 curr
= &(*curr
)->next
;
2115 static BOOL
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2118 DOS_FILE_LOCK
**curr
;
2121 processId
= GetCurrentProcessId();
2122 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2123 if ((*curr
)->processId
== processId
&&
2124 (*curr
)->dos_file
== file
&&
2125 (*curr
)->base
== f
->l_start
&&
2126 (*curr
)->len
== f
->l_len
) {
2127 /* this is the same lock */
2129 *curr
= (*curr
)->next
;
2130 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2131 HeapFree( GetProcessHeap(), 0, rem
);
2135 /* no matching lock found */
2140 /**************************************************************************
2141 * LockFile (KERNEL32.511)
2143 BOOL WINAPI
LockFile(
2144 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2145 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2150 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2151 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2152 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2154 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2155 FIXME("Unimplemented bytes > 32bits\n");
2159 f
.l_start
= dwFileOffsetLow
;
2160 f
.l_len
= nNumberOfBytesToLockLow
;
2161 f
.l_whence
= SEEK_SET
;
2165 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2167 /* shadow locks internally */
2168 if (!DOS_AddLock(file
, &f
)) {
2169 SetLastError( ERROR_LOCK_VIOLATION
);
2173 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2174 #ifdef USE_UNIX_LOCKS
2175 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2176 if (errno
== EACCES
|| errno
== EAGAIN
) {
2177 SetLastError( ERROR_LOCK_VIOLATION
);
2182 /* remove our internal copy of the lock */
2183 DOS_RemoveLock(file
, &f
);
2191 /**************************************************************************
2192 * UnlockFile (KERNEL32.703)
2194 BOOL WINAPI
UnlockFile(
2195 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2196 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2201 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2202 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2203 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2205 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2206 WARN("Unimplemented bytes > 32bits\n");
2210 f
.l_start
= dwFileOffsetLow
;
2211 f
.l_len
= nNumberOfBytesToUnlockLow
;
2212 f
.l_whence
= SEEK_SET
;
2216 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2218 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2220 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2221 #ifdef USE_UNIX_LOCKS
2222 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2231 /**************************************************************************
2232 * GetFileAttributesExA [KERNEL32.874]
2234 BOOL WINAPI
GetFileAttributesExA(
2235 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2236 LPVOID lpFileInformation
)
2238 DOS_FULL_NAME full_name
;
2239 BY_HANDLE_FILE_INFORMATION info
;
2241 if (lpFileName
== NULL
) return FALSE
;
2242 if (lpFileInformation
== NULL
) return FALSE
;
2244 if (fInfoLevelId
== GetFileExInfoStandard
) {
2245 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2246 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2247 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2248 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2250 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2251 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2252 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2253 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2254 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2255 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2258 FIXME("invalid info level %d!\n", fInfoLevelId
);
2266 /**************************************************************************
2267 * GetFileAttributesExW [KERNEL32.875]
2269 BOOL WINAPI
GetFileAttributesExW(
2270 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2271 LPVOID lpFileInformation
)
2273 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2275 GetFileAttributesExA( nameA
, fInfoLevelId
, lpFileInformation
);
2276 HeapFree( GetProcessHeap(), 0, nameA
);