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.
13 #include "wine/port.h"
22 #ifdef HAVE_SYS_ERRNO_H
23 #include <sys/errno.h>
25 #include <sys/types.h>
27 #ifdef HAVE_SYS_MMAN_H
39 #include "wine/winbase16.h"
45 #include "debugtools.h"
49 DEFAULT_DEBUG_CHANNEL(file
);
51 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
52 #define MAP_ANON MAP_ANONYMOUS
55 /* Size of per-process table of DOS handles */
56 #define DOS_TABLE_SIZE 256
58 static HANDLE dos_handles
[DOS_TABLE_SIZE
];
61 /***********************************************************************
64 * Convert OF_* mode into flags for CreateFile.
66 static void FILE_ConvertOFMode( INT mode
, DWORD
*access
, DWORD
*sharing
)
70 case OF_READ
: *access
= GENERIC_READ
; break;
71 case OF_WRITE
: *access
= GENERIC_WRITE
; break;
72 case OF_READWRITE
: *access
= GENERIC_READ
| GENERIC_WRITE
; break;
73 default: *access
= 0; break;
77 case OF_SHARE_EXCLUSIVE
: *sharing
= 0; break;
78 case OF_SHARE_DENY_WRITE
: *sharing
= FILE_SHARE_READ
; break;
79 case OF_SHARE_DENY_READ
: *sharing
= FILE_SHARE_WRITE
; break;
80 case OF_SHARE_DENY_NONE
:
82 default: *sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
; break;
87 /***********************************************************************
90 * locale-independent case conversion for file I/O
92 int FILE_strcasecmp( const char *str1
, const char *str2
)
96 int ret
= FILE_toupper(*str1
) - FILE_toupper(*str2
);
97 if (ret
|| !*str1
) return ret
;
104 /***********************************************************************
107 * locale-independent case conversion for file I/O
109 int FILE_strncasecmp( const char *str1
, const char *str2
, int len
)
112 for ( ; len
> 0; len
--, str1
++, str2
++)
113 if ((ret
= FILE_toupper(*str1
) - FILE_toupper(*str2
)) || !*str1
) break;
118 /***********************************************************************
121 * Set the DOS error code from errno.
123 void FILE_SetDosError(void)
125 int save_errno
= errno
; /* errno gets overwritten by printf */
127 TRACE("errno = %d %s\n", errno
, strerror(errno
));
131 SetLastError( ERROR_SHARING_VIOLATION
);
134 SetLastError( ERROR_INVALID_HANDLE
);
137 SetLastError( ERROR_HANDLE_DISK_FULL
);
142 SetLastError( ERROR_ACCESS_DENIED
);
145 SetLastError( ERROR_LOCK_VIOLATION
);
148 SetLastError( ERROR_FILE_NOT_FOUND
);
151 SetLastError( ERROR_CANNOT_MAKE
);
155 SetLastError( ERROR_NO_MORE_FILES
);
158 SetLastError( ERROR_FILE_EXISTS
);
162 SetLastError( ERROR_SEEK
);
165 SetLastError( ERROR_DIR_NOT_EMPTY
);
168 SetLastError( ERROR_BAD_FORMAT
);
171 WARN("unknown file error: %s\n", strerror(save_errno
) );
172 SetLastError( ERROR_GEN_FAILURE
);
179 /***********************************************************************
182 * Duplicate a Unix handle into a task handle.
183 * Returns 0 on failure.
185 HANDLE
FILE_DupUnixHandle( int fd
, DWORD access
)
189 wine_server_send_fd( fd
);
191 SERVER_START_REQ( alloc_file_handle
)
193 req
->access
= access
;
203 /***********************************************************************
206 * Retrieve the Unix handle corresponding to a file handle.
207 * Returns -1 on failure.
209 int FILE_GetUnixHandle( HANDLE handle
, DWORD access
)
215 SERVER_START_REQ( get_handle_fd
)
217 req
->handle
= handle
;
218 req
->access
= access
;
219 if (!(ret
= SERVER_CALL_ERR())) fd
= req
->fd
;
224 if (fd
== -1) /* it wasn't in the cache, get it from the server */
225 fd
= wine_server_recv_fd( handle
);
227 } while (fd
== -2); /* -2 means race condition, so restart from scratch */
231 if ((fd
= dup(fd
)) == -1)
232 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
238 /*************************************************************************
241 * Open a handle to the current process console.
242 * Returns 0 on failure.
244 static HANDLE
FILE_OpenConsole( BOOL output
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
248 SERVER_START_REQ( open_console
)
250 req
->output
= output
;
251 req
->access
= access
;
252 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
262 /***********************************************************************
265 * Implementation of CreateFile. Takes a Unix path name.
266 * Returns 0 on failure.
268 HANDLE
FILE_CreateFile( LPCSTR filename
, DWORD access
, DWORD sharing
,
269 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
270 DWORD attributes
, HANDLE
template, BOOL fail_read_only
)
274 size_t len
= strlen(filename
);
276 if (len
> REQUEST_MAX_VAR_SIZE
)
278 FIXME("filename '%s' too long\n", filename
);
279 SetLastError( ERROR_INVALID_PARAMETER
);
284 SERVER_START_VAR_REQ( create_file
, len
)
286 req
->access
= access
;
287 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
288 req
->sharing
= sharing
;
289 req
->create
= creation
;
290 req
->attrs
= attributes
;
291 memcpy( server_data_ptr(req
), filename
, len
);
298 /* If write access failed, retry without GENERIC_WRITE */
300 if (!ret
&& !fail_read_only
&& (access
& GENERIC_WRITE
))
302 if ((err
== STATUS_MEDIA_WRITE_PROTECTED
) || (err
== STATUS_ACCESS_DENIED
))
304 TRACE("Write access failed for file '%s', trying without "
305 "write access\n", filename
);
306 access
&= ~GENERIC_WRITE
;
311 if (err
) SetLastError( RtlNtStatusToDosError(err
) );
314 WARN("Unable to create file '%s' (GLE %ld)\n", filename
, GetLastError());
320 /***********************************************************************
323 * Same as FILE_CreateFile but for a device
324 * Returns 0 on failure.
326 HANDLE
FILE_CreateDevice( int client_id
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
329 SERVER_START_REQ( create_device
)
331 req
->access
= access
;
332 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
342 static HANDLE
FILE_OpenPipe(LPCSTR name
, DWORD access
)
345 DWORD len
= name
? MultiByteToWideChar( CP_ACP
, 0, name
, strlen(name
), NULL
, 0 ) : 0;
347 TRACE("name %s access %lx\n",name
,access
);
351 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
354 SERVER_START_VAR_REQ( open_named_pipe
, len
* sizeof(WCHAR
) )
356 req
->access
= access
;
358 if (len
) MultiByteToWideChar( CP_ACP
, 0, name
, strlen(name
), server_data_ptr(req
), len
);
364 TRACE("Returned %d\n",ret
);
368 /*************************************************************************
369 * CreateFileA [KERNEL32.@] Creates or opens a file or other object
371 * Creates or opens an object, and returns a handle that can be used to
372 * access that object.
376 * filename [in] pointer to filename to be accessed
377 * access [in] access mode requested
378 * sharing [in] share mode
379 * sa [in] pointer to security attributes
380 * creation [in] how to create the file
381 * attributes [in] attributes for newly created file
382 * template [in] handle to file with extended attributes to copy
385 * Success: Open handle to specified file
386 * Failure: INVALID_HANDLE_VALUE
389 * Should call SetLastError() on failure.
393 * Doesn't support character devices, template files, or a
394 * lot of the 'attributes' flags yet.
396 HANDLE WINAPI
CreateFileA( LPCSTR filename
, DWORD access
, DWORD sharing
,
397 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
398 DWORD attributes
, HANDLE
template )
400 DOS_FULL_NAME full_name
;
405 SetLastError( ERROR_INVALID_PARAMETER
);
406 return INVALID_HANDLE_VALUE
;
408 TRACE("%s %s%s%s%s%s%s%s\n",filename
,
409 ((access
& GENERIC_READ
)==GENERIC_READ
)?"GENERIC_READ ":"",
410 ((access
& GENERIC_WRITE
)==GENERIC_WRITE
)?"GENERIC_WRITE ":"",
411 (!access
)?"QUERY_ACCESS ":"",
412 ((sharing
& FILE_SHARE_READ
)==FILE_SHARE_READ
)?"FILE_SHARE_READ ":"",
413 ((sharing
& FILE_SHARE_WRITE
)==FILE_SHARE_WRITE
)?"FILE_SHARE_WRITE ":"",
414 ((sharing
& FILE_SHARE_DELETE
)==FILE_SHARE_DELETE
)?"FILE_SHARE_DELETE ":"",
415 (creation
==CREATE_NEW
)?"CREATE_NEW":
416 (creation
==CREATE_ALWAYS
)?"CREATE_ALWAYS ":
417 (creation
==OPEN_EXISTING
)?"OPEN_EXISTING ":
418 (creation
==OPEN_ALWAYS
)?"OPEN_ALWAYS ":
419 (creation
==TRUNCATE_EXISTING
)?"TRUNCATE_EXISTING ":"");
421 /* If the name starts with '\\?\', ignore the first 4 chars. */
422 if (!strncmp(filename
, "\\\\?\\", 4))
425 if (!strncmp(filename
, "UNC\\", 4))
427 FIXME("UNC name (%s) not supported.\n", filename
);
428 SetLastError( ERROR_PATH_NOT_FOUND
);
429 return INVALID_HANDLE_VALUE
;
433 if (!strncmp(filename
, "\\\\.\\", 4)) {
434 if(!strncasecmp(&filename
[4],"pipe\\",5))
436 TRACE("Opening a pipe: %s\n",filename
);
437 return FILE_OpenPipe(filename
,access
);
439 else if (!DOSFS_GetDevice( filename
))
441 ret
= DEVICE_Open( filename
+4, access
, sa
);
445 filename
+=4; /* fall into DOSFS_Device case below */
448 /* If the name still starts with '\\', it's a UNC name. */
449 if (!strncmp(filename
, "\\\\", 2))
451 FIXME("UNC name (%s) not supported.\n", filename
);
452 SetLastError( ERROR_PATH_NOT_FOUND
);
453 return INVALID_HANDLE_VALUE
;
456 /* If the name contains a DOS wild card (* or ?), do no create a file */
457 if(strchr(filename
,'*') || strchr(filename
,'?'))
458 return INVALID_HANDLE_VALUE
;
460 /* Open a console for CONIN$ or CONOUT$ */
461 if (!strcasecmp(filename
, "CONIN$"))
463 ret
= FILE_OpenConsole( FALSE
, access
, sa
);
466 if (!strcasecmp(filename
, "CONOUT$"))
468 ret
= FILE_OpenConsole( TRUE
, access
, sa
);
472 if (DOSFS_GetDevice( filename
))
474 TRACE("opening device '%s'\n", filename
);
476 if (!(ret
= DOSFS_OpenDevice( filename
, access
)))
478 /* Do not silence this please. It is a critical error. -MM */
479 ERR("Couldn't open device '%s'!\n",filename
);
480 SetLastError( ERROR_FILE_NOT_FOUND
);
485 /* check for filename, don't check for last entry if creating */
486 if (!DOSFS_GetFullName( filename
,
487 (creation
== OPEN_EXISTING
) ||
488 (creation
== TRUNCATE_EXISTING
),
490 WARN("Unable to get full filename from '%s' (GLE %ld)\n",
491 filename
, GetLastError());
492 return INVALID_HANDLE_VALUE
;
495 ret
= FILE_CreateFile( full_name
.long_name
, access
, sharing
,
496 sa
, creation
, attributes
, template,
497 DRIVE_GetFlags(full_name
.drive
) & DRIVE_FAIL_READ_ONLY
);
499 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
505 /*************************************************************************
506 * CreateFileW (KERNEL32.@)
508 HANDLE WINAPI
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
509 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
510 DWORD attributes
, HANDLE
template)
512 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
513 HANDLE res
= CreateFileA( afn
, access
, sharing
, sa
, creation
, attributes
, template );
514 HeapFree( GetProcessHeap(), 0, afn
);
519 /***********************************************************************
522 * Fill a file information from a struct stat.
524 static void FILE_FillInfo( struct stat
*st
, BY_HANDLE_FILE_INFORMATION
*info
)
526 if (S_ISDIR(st
->st_mode
))
527 info
->dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
529 info
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
530 if (!(st
->st_mode
& S_IWUSR
))
531 info
->dwFileAttributes
|= FILE_ATTRIBUTE_READONLY
;
533 RtlSecondsSince1970ToTime( st
->st_mtime
, &info
->ftCreationTime
);
534 RtlSecondsSince1970ToTime( st
->st_mtime
, &info
->ftLastWriteTime
);
535 RtlSecondsSince1970ToTime( st
->st_atime
, &info
->ftLastAccessTime
);
537 info
->dwVolumeSerialNumber
= 0; /* FIXME */
538 info
->nFileSizeHigh
= 0;
539 info
->nFileSizeLow
= 0;
540 if (!S_ISDIR(st
->st_mode
)) {
541 info
->nFileSizeHigh
= st
->st_size
>> 32;
542 info
->nFileSizeLow
= st
->st_size
& 0xffffffff;
544 info
->nNumberOfLinks
= st
->st_nlink
;
545 info
->nFileIndexHigh
= 0;
546 info
->nFileIndexLow
= st
->st_ino
;
550 /***********************************************************************
553 * Stat a Unix path name. Return TRUE if OK.
555 BOOL
FILE_Stat( LPCSTR unixName
, BY_HANDLE_FILE_INFORMATION
*info
)
559 if (lstat( unixName
, &st
) == -1)
564 if (!S_ISLNK(st
.st_mode
)) FILE_FillInfo( &st
, info
);
567 /* do a "real" stat to find out
568 about the type of the symlink destination */
569 if (stat( unixName
, &st
) == -1)
574 FILE_FillInfo( &st
, info
);
575 info
->dwFileAttributes
|= FILE_ATTRIBUTE_SYMLINK
;
581 /***********************************************************************
582 * GetFileInformationByHandle (KERNEL32.@)
584 DWORD WINAPI
GetFileInformationByHandle( HANDLE hFile
,
585 BY_HANDLE_FILE_INFORMATION
*info
)
590 SERVER_START_REQ( get_file_info
)
593 if ((ret
= !SERVER_CALL_ERR()))
595 /* FIXME: which file types are supported ?
596 * Serial ports (FILE_TYPE_CHAR) are not,
597 * and MSDN also says that pipes are not supported.
598 * FILE_TYPE_REMOTE seems to be supported according to
599 * MSDN q234741.txt */
600 if ((req
->type
== FILE_TYPE_DISK
)
601 || (req
->type
== FILE_TYPE_REMOTE
))
603 RtlSecondsSince1970ToTime( req
->write_time
, &info
->ftCreationTime
);
604 RtlSecondsSince1970ToTime( req
->write_time
, &info
->ftLastWriteTime
);
605 RtlSecondsSince1970ToTime( req
->access_time
, &info
->ftLastAccessTime
);
606 info
->dwFileAttributes
= req
->attr
;
607 info
->dwVolumeSerialNumber
= req
->serial
;
608 info
->nFileSizeHigh
= req
->size_high
;
609 info
->nFileSizeLow
= req
->size_low
;
610 info
->nNumberOfLinks
= req
->links
;
611 info
->nFileIndexHigh
= req
->index_high
;
612 info
->nFileIndexLow
= req
->index_low
;
616 SetLastError(ERROR_NOT_SUPPORTED
);
626 /**************************************************************************
627 * GetFileAttributes (KERNEL.420)
629 DWORD WINAPI
GetFileAttributes16( LPCSTR name
)
631 return GetFileAttributesA( name
);
635 /**************************************************************************
636 * GetFileAttributesA (KERNEL32.@)
638 DWORD WINAPI
GetFileAttributesA( LPCSTR name
)
640 DOS_FULL_NAME full_name
;
641 BY_HANDLE_FILE_INFORMATION info
;
645 SetLastError( ERROR_INVALID_PARAMETER
);
648 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
) )
650 if (!FILE_Stat( full_name
.long_name
, &info
)) return -1;
651 return info
.dwFileAttributes
;
655 /**************************************************************************
656 * GetFileAttributesW (KERNEL32.@)
658 DWORD WINAPI
GetFileAttributesW( LPCWSTR name
)
660 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
661 DWORD res
= GetFileAttributesA( nameA
);
662 HeapFree( GetProcessHeap(), 0, nameA
);
667 /***********************************************************************
668 * GetFileSize (KERNEL32.@)
670 DWORD WINAPI
GetFileSize( HANDLE hFile
, LPDWORD filesizehigh
)
672 BY_HANDLE_FILE_INFORMATION info
;
673 if (!GetFileInformationByHandle( hFile
, &info
)) return -1;
674 if (filesizehigh
) *filesizehigh
= info
.nFileSizeHigh
;
675 return info
.nFileSizeLow
;
679 /***********************************************************************
680 * GetFileTime (KERNEL32.@)
682 BOOL WINAPI
GetFileTime( HANDLE hFile
, FILETIME
*lpCreationTime
,
683 FILETIME
*lpLastAccessTime
,
684 FILETIME
*lpLastWriteTime
)
686 BY_HANDLE_FILE_INFORMATION info
;
687 if (!GetFileInformationByHandle( hFile
, &info
)) return FALSE
;
688 if (lpCreationTime
) *lpCreationTime
= info
.ftCreationTime
;
689 if (lpLastAccessTime
) *lpLastAccessTime
= info
.ftLastAccessTime
;
690 if (lpLastWriteTime
) *lpLastWriteTime
= info
.ftLastWriteTime
;
694 /***********************************************************************
695 * CompareFileTime (KERNEL32.@)
697 INT WINAPI
CompareFileTime( LPFILETIME x
, LPFILETIME y
)
699 if (!x
|| !y
) return -1;
701 if (x
->dwHighDateTime
> y
->dwHighDateTime
)
703 if (x
->dwHighDateTime
< y
->dwHighDateTime
)
705 if (x
->dwLowDateTime
> y
->dwLowDateTime
)
707 if (x
->dwLowDateTime
< y
->dwLowDateTime
)
712 /***********************************************************************
713 * FILE_GetTempFileName : utility for GetTempFileName
715 static UINT
FILE_GetTempFileName( LPCSTR path
, LPCSTR prefix
, UINT unique
,
716 LPSTR buffer
, BOOL isWin16
)
718 static UINT unique_temp
;
719 DOS_FULL_NAME full_name
;
724 if ( !path
|| !prefix
|| !buffer
) return 0;
726 if (!unique_temp
) unique_temp
= time(NULL
) & 0xffff;
727 num
= unique
? (unique
& 0xffff) : (unique_temp
++ & 0xffff);
729 strcpy( buffer
, path
);
730 p
= buffer
+ strlen(buffer
);
732 /* add a \, if there isn't one and path is more than just the drive letter ... */
733 if ( !((strlen(buffer
) == 2) && (buffer
[1] == ':'))
734 && ((p
== buffer
) || (p
[-1] != '\\'))) *p
++ = '\\';
736 if (isWin16
) *p
++ = '~';
737 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
738 sprintf( p
, "%04x.tmp", num
);
740 /* Now try to create it */
746 HFILE handle
= CreateFileA( buffer
, GENERIC_WRITE
, 0, NULL
,
747 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, 0 );
748 if (handle
!= INVALID_HANDLE_VALUE
)
749 { /* We created it */
750 TRACE("created %s\n",
752 CloseHandle( handle
);
755 if (GetLastError() != ERROR_FILE_EXISTS
)
756 break; /* No need to go on */
758 sprintf( p
, "%04x.tmp", num
);
759 } while (num
!= (unique
& 0xffff));
762 /* Get the full path name */
764 if (DOSFS_GetFullName( buffer
, FALSE
, &full_name
))
766 /* Check if we have write access in the directory */
767 if ((p
= strrchr( full_name
.long_name
, '/' ))) *p
= '\0';
768 if (access( full_name
.long_name
, W_OK
) == -1)
769 WARN("returns '%s', which doesn't seem to be writeable.\n",
772 TRACE("returning %s\n", buffer
);
773 return unique
? unique
: num
;
777 /***********************************************************************
778 * GetTempFileNameA (KERNEL32.@)
780 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
,
783 return FILE_GetTempFileName(path
, prefix
, unique
, buffer
, FALSE
);
786 /***********************************************************************
787 * GetTempFileNameW (KERNEL32.@)
789 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
,
797 patha
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
798 prefixa
= HEAP_strdupWtoA( GetProcessHeap(), 0, prefix
);
799 ret
= FILE_GetTempFileName( patha
, prefixa
, unique
, buffera
, FALSE
);
800 MultiByteToWideChar( CP_ACP
, 0, buffera
, -1, buffer
, MAX_PATH
);
801 HeapFree( GetProcessHeap(), 0, patha
);
802 HeapFree( GetProcessHeap(), 0, prefixa
);
807 /***********************************************************************
808 * GetTempFileName (KERNEL.97)
810 UINT16 WINAPI
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
815 if (!(drive
& ~TF_FORCEDRIVE
)) /* drive 0 means current default drive */
816 drive
|= DRIVE_GetCurrentDrive() + 'A';
818 if ((drive
& TF_FORCEDRIVE
) &&
819 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
821 drive
&= ~TF_FORCEDRIVE
;
822 WARN("invalid drive %d specified\n", drive
);
825 if (drive
& TF_FORCEDRIVE
)
826 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
828 GetTempPathA( 132, temppath
);
829 return (UINT16
)FILE_GetTempFileName( temppath
, prefix
, unique
, buffer
, TRUE
);
832 /***********************************************************************
835 * Implementation of OpenFile16() and OpenFile32().
837 static HFILE
FILE_DoOpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
,
842 WORD filedatetime
[2];
843 DOS_FULL_NAME full_name
;
844 DWORD access
, sharing
;
847 if (!ofs
) return HFILE_ERROR
;
849 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name
,
850 ((mode
& 0x3 )==OF_READ
)?"OF_READ":
851 ((mode
& 0x3 )==OF_WRITE
)?"OF_WRITE":
852 ((mode
& 0x3 )==OF_READWRITE
)?"OF_READWRITE":"unknown",
853 ((mode
& 0x70 )==OF_SHARE_COMPAT
)?"OF_SHARE_COMPAT":
854 ((mode
& 0x70 )==OF_SHARE_DENY_NONE
)?"OF_SHARE_DENY_NONE":
855 ((mode
& 0x70 )==OF_SHARE_DENY_READ
)?"OF_SHARE_DENY_READ":
856 ((mode
& 0x70 )==OF_SHARE_DENY_WRITE
)?"OF_SHARE_DENY_WRITE":
857 ((mode
& 0x70 )==OF_SHARE_EXCLUSIVE
)?"OF_SHARE_EXCLUSIVE":"unknown",
858 ((mode
& OF_PARSE
)==OF_PARSE
)?"OF_PARSE ":"",
859 ((mode
& OF_DELETE
)==OF_DELETE
)?"OF_DELETE ":"",
860 ((mode
& OF_VERIFY
)==OF_VERIFY
)?"OF_VERIFY ":"",
861 ((mode
& OF_SEARCH
)==OF_SEARCH
)?"OF_SEARCH ":"",
862 ((mode
& OF_CANCEL
)==OF_CANCEL
)?"OF_CANCEL ":"",
863 ((mode
& OF_CREATE
)==OF_CREATE
)?"OF_CREATE ":"",
864 ((mode
& OF_PROMPT
)==OF_PROMPT
)?"OF_PROMPT ":"",
865 ((mode
& OF_EXIST
)==OF_EXIST
)?"OF_EXIST ":"",
866 ((mode
& OF_REOPEN
)==OF_REOPEN
)?"OF_REOPEN ":""
870 ofs
->cBytes
= sizeof(OFSTRUCT
);
872 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
875 ERR("called with `name' set to NULL ! Please debug.\n");
879 TRACE("%s %04x\n", name
, mode
);
881 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
882 Are there any cases where getting the path here is wrong?
883 Uwe Bonnes 1997 Apr 2 */
884 if (!GetFullPathNameA( name
, sizeof(ofs
->szPathName
),
885 ofs
->szPathName
, NULL
)) goto error
;
886 FILE_ConvertOFMode( mode
, &access
, &sharing
);
888 /* OF_PARSE simply fills the structure */
892 ofs
->fFixedDisk
= (GetDriveType16( ofs
->szPathName
[0]-'A' )
894 TRACE("(%s): OF_PARSE, res = '%s'\n",
895 name
, ofs
->szPathName
);
899 /* OF_CREATE is completely different from all other options, so
902 if (mode
& OF_CREATE
)
904 if ((hFileRet
= CreateFileA( name
, GENERIC_READ
| GENERIC_WRITE
,
905 sharing
, NULL
, CREATE_ALWAYS
,
906 FILE_ATTRIBUTE_NORMAL
, 0 ))== INVALID_HANDLE_VALUE
)
911 /* If OF_SEARCH is set, ignore the given path */
913 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
915 /* First try the file name as is */
916 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) goto found
;
917 /* Now remove the path */
918 if (name
[0] && (name
[1] == ':')) name
+= 2;
919 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
920 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
921 if (!name
[0]) goto not_found
;
924 /* Now look for the file */
926 if (!DIR_SearchPath( NULL
, name
, NULL
, &full_name
, win32
)) goto not_found
;
929 TRACE("found %s = %s\n",
930 full_name
.long_name
, full_name
.short_name
);
931 lstrcpynA( ofs
->szPathName
, full_name
.short_name
,
932 sizeof(ofs
->szPathName
) );
934 if (mode
& OF_SHARE_EXCLUSIVE
)
935 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
936 on the file <tempdir>/_ins0432._mp to determine how
937 far installation has proceeded.
938 _ins0432._mp is an executable and while running the
939 application expects the open with OF_SHARE_ to fail*/
941 As our loader closes the files after loading the executable,
942 we can't find the running executable with FILE_InUse.
943 The loader should keep the file open, as Windows does that, too.
946 char *last
= strrchr(full_name
.long_name
,'/');
948 last
= full_name
.long_name
- 1;
949 if (GetModuleHandle16(last
+1))
951 TRACE("Denying shared open for %s\n",full_name
.long_name
);
956 if (mode
& OF_DELETE
)
958 if (unlink( full_name
.long_name
) == -1) goto not_found
;
959 TRACE("(%s): OF_DELETE return = OK\n", name
);
963 hFileRet
= FILE_CreateFile( full_name
.long_name
, access
, sharing
,
964 NULL
, OPEN_EXISTING
, 0, 0,
965 DRIVE_GetFlags(full_name
.drive
) & DRIVE_FAIL_READ_ONLY
);
966 if (!hFileRet
) goto not_found
;
968 GetFileTime( hFileRet
, NULL
, NULL
, &filetime
);
969 FileTimeToDosDateTime( &filetime
, &filedatetime
[0], &filedatetime
[1] );
970 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
972 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
974 CloseHandle( hFileRet
);
975 WARN("(%s): OF_VERIFY failed\n", name
);
976 /* FIXME: what error here? */
977 SetLastError( ERROR_FILE_NOT_FOUND
);
981 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
983 success
: /* We get here if the open was successful */
984 TRACE("(%s): OK, return = %d\n", name
, hFileRet
);
987 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
988 CloseHandle( hFileRet
);
992 hFileRet
= Win32HandleToDosFileHandle( hFileRet
);
993 if (hFileRet
== HFILE_ERROR16
) goto error
;
994 if (mode
& OF_EXIST
) /* Return the handle, but close it first */
995 _lclose16( hFileRet
);
999 not_found
: /* We get here if the file does not exist */
1000 WARN("'%s' not found or sharing violation\n", name
);
1001 SetLastError( ERROR_FILE_NOT_FOUND
);
1004 error
: /* We get here if there was an error opening the file */
1005 ofs
->nErrCode
= GetLastError();
1006 WARN("(%s): return = HFILE_ERROR error= %d\n",
1007 name
,ofs
->nErrCode
);
1012 /***********************************************************************
1013 * OpenFile (KERNEL.74)
1014 * OpenFileEx (KERNEL.360)
1016 HFILE16 WINAPI
OpenFile16( LPCSTR name
, OFSTRUCT
*ofs
, UINT16 mode
)
1018 return FILE_DoOpenFile( name
, ofs
, mode
, FALSE
);
1022 /***********************************************************************
1023 * OpenFile (KERNEL32.@)
1025 HFILE WINAPI
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
1027 return FILE_DoOpenFile( name
, ofs
, mode
, TRUE
);
1031 /***********************************************************************
1032 * FILE_InitProcessDosHandles
1034 * Allocates the default DOS handles for a process. Called either by
1035 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
1037 static void FILE_InitProcessDosHandles( void )
1039 dos_handles
[0] = GetStdHandle(STD_INPUT_HANDLE
);
1040 dos_handles
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
1041 dos_handles
[2] = GetStdHandle(STD_ERROR_HANDLE
);
1042 dos_handles
[3] = GetStdHandle(STD_ERROR_HANDLE
);
1043 dos_handles
[4] = GetStdHandle(STD_ERROR_HANDLE
);
1046 /***********************************************************************
1047 * Win32HandleToDosFileHandle (KERNEL32.21)
1049 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1050 * longer valid after this function (even on failure).
1052 * Note: this is not exactly right, since on Win95 the Win32 handles
1053 * are on top of DOS handles and we do it the other way
1054 * around. Should be good enough though.
1056 HFILE WINAPI
Win32HandleToDosFileHandle( HANDLE handle
)
1060 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
))
1063 for (i
= 5; i
< DOS_TABLE_SIZE
; i
++)
1064 if (!dos_handles
[i
])
1066 dos_handles
[i
] = handle
;
1067 TRACE("Got %d for h32 %d\n", i
, handle
);
1070 CloseHandle( handle
);
1071 SetLastError( ERROR_TOO_MANY_OPEN_FILES
);
1076 /***********************************************************************
1077 * DosFileHandleToWin32Handle (KERNEL32.20)
1079 * Return the Win32 handle for a DOS handle.
1081 * Note: this is not exactly right, since on Win95 the Win32 handles
1082 * are on top of DOS handles and we do it the other way
1083 * around. Should be good enough though.
1085 HANDLE WINAPI
DosFileHandleToWin32Handle( HFILE handle
)
1087 HFILE16 hfile
= (HFILE16
)handle
;
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 /***********************************************************************
1099 * DisposeLZ32Handle (KERNEL32.22)
1101 * Note: this is not entirely correct, we should only close the
1102 * 32-bit handle and not the 16-bit one, but we cannot do
1103 * this because of the way our DOS handles are implemented.
1104 * It shouldn't break anything though.
1106 void WINAPI
DisposeLZ32Handle( HANDLE handle
)
1110 if (!handle
|| (handle
== INVALID_HANDLE_VALUE
)) return;
1112 for (i
= 5; i
< DOS_TABLE_SIZE
; i
++)
1113 if (dos_handles
[i
] == handle
)
1116 CloseHandle( handle
);
1122 /***********************************************************************
1125 * dup2() function for DOS handles.
1127 HFILE16
FILE_Dup2( HFILE16 hFile1
, HFILE16 hFile2
)
1131 if (hFile1
< 5 && !dos_handles
[hFile1
]) FILE_InitProcessDosHandles();
1133 if ((hFile1
>= DOS_TABLE_SIZE
) || (hFile2
>= DOS_TABLE_SIZE
) || !dos_handles
[hFile1
])
1135 SetLastError( ERROR_INVALID_HANDLE
);
1136 return HFILE_ERROR16
;
1140 FIXME("stdio handle closed, need proper conversion\n" );
1141 SetLastError( ERROR_INVALID_HANDLE
);
1142 return HFILE_ERROR16
;
1144 if (!DuplicateHandle( GetCurrentProcess(), dos_handles
[hFile1
],
1145 GetCurrentProcess(), &new_handle
,
1146 0, FALSE
, DUPLICATE_SAME_ACCESS
))
1147 return HFILE_ERROR16
;
1148 if (dos_handles
[hFile2
]) CloseHandle( dos_handles
[hFile2
] );
1149 dos_handles
[hFile2
] = new_handle
;
1154 /***********************************************************************
1155 * _lclose (KERNEL.81)
1157 HFILE16 WINAPI
_lclose16( HFILE16 hFile
)
1161 FIXME("stdio handle closed, need proper conversion\n" );
1162 SetLastError( ERROR_INVALID_HANDLE
);
1163 return HFILE_ERROR16
;
1165 if ((hFile
>= DOS_TABLE_SIZE
) || !dos_handles
[hFile
])
1167 SetLastError( ERROR_INVALID_HANDLE
);
1168 return HFILE_ERROR16
;
1170 TRACE("%d (handle32=%d)\n", hFile
, dos_handles
[hFile
] );
1171 CloseHandle( dos_handles
[hFile
] );
1172 dos_handles
[hFile
] = 0;
1177 /***********************************************************************
1178 * _lclose (KERNEL32.@)
1180 HFILE WINAPI
_lclose( HFILE hFile
)
1182 TRACE("handle %d\n", hFile
);
1183 return CloseHandle( hFile
) ? 0 : HFILE_ERROR
;
1186 /***********************************************************************
1187 * GetOverlappedResult (KERNEL32.@)
1189 * Check the result of an Asynchronous data transfer from a file.
1195 * If successful (and relevant) lpTransfered will hold the number of
1196 * bytes transfered during the async operation.
1200 * Currently only works for WaitCommEvent, ReadFile, WriteFile
1201 * with communications ports.
1204 BOOL WINAPI
GetOverlappedResult(
1205 HANDLE hFile
, /* [in] handle of file to check on */
1206 LPOVERLAPPED lpOverlapped
, /* [in/out] pointer to overlapped */
1207 LPDWORD lpTransferred
, /* [in/out] number of bytes transfered */
1208 BOOL bWait
/* [in] wait for the transfer to complete ? */
1212 TRACE("(%d %p %p %x)\n", hFile
, lpOverlapped
, lpTransferred
, bWait
);
1214 if(lpOverlapped
==NULL
)
1216 ERR("lpOverlapped was null\n");
1219 if(!lpOverlapped
->hEvent
)
1221 ERR("lpOverlapped->hEvent was null\n");
1226 TRACE("waiting on %p\n",lpOverlapped
);
1227 r
= WaitForSingleObjectEx(lpOverlapped
->hEvent
, bWait
?INFINITE
:0, TRUE
);
1228 TRACE("wait on %p returned %ld\n",lpOverlapped
,r
);
1229 } while (r
==STATUS_USER_APC
);
1232 *lpTransferred
= lpOverlapped
->InternalHigh
;
1234 SetLastError(lpOverlapped
->Internal
);
1236 return (r
==WAIT_OBJECT_0
);
1240 /***********************************************************************
1241 * FILE_AsyncReadService (INTERNAL)
1243 * This function is called while the client is waiting on the
1244 * server, so we can't make any server calls here.
1246 static void FILE_AsyncReadService(async_private
*ovp
, int events
)
1248 LPOVERLAPPED lpOverlapped
= ovp
->lpOverlapped
;
1251 TRACE("%p %p %08x\n", lpOverlapped
, ovp
->buffer
, events
);
1253 /* if POLLNVAL, then our fd was closed or we have the wrong fd */
1256 ERR("fd %d invalid for %p\n",ovp
->fd
,ovp
);
1257 r
= STATUS_UNSUCCESSFUL
;
1261 /* if there are no events, it must be a timeout */
1264 TRACE("read timed out\n");
1269 /* check to see if the data is ready (non-blocking) */
1270 result
= read(ovp
->fd
, &ovp
->buffer
[lpOverlapped
->InternalHigh
],
1271 ovp
->count
- lpOverlapped
->InternalHigh
);
1273 if ( (result
<0) && ((errno
== EAGAIN
) || (errno
== EINTR
)))
1275 TRACE("Deferred read %d\n",errno
);
1280 /* check to see if the transfer is complete */
1283 TRACE("read returned errno %d\n",errno
);
1284 r
= STATUS_UNSUCCESSFUL
;
1288 lpOverlapped
->InternalHigh
+= result
;
1289 TRACE("read %d more bytes %ld/%d so far\n",result
,lpOverlapped
->InternalHigh
,ovp
->count
);
1291 if(lpOverlapped
->InternalHigh
< ovp
->count
)
1297 lpOverlapped
->Internal
= r
;
1300 /* flogged from wineserver */
1301 /* add a timeout in milliseconds to an absolute time */
1302 static void add_timeout( struct timeval
*when
, int timeout
)
1306 long sec
= timeout
/ 1000;
1307 if ((when
->tv_usec
+= (timeout
- 1000*sec
) * 1000) >= 1000000)
1309 when
->tv_usec
-= 1000000;
1312 when
->tv_sec
+= sec
;
1316 /***********************************************************************
1317 * ReadFileEx (KERNEL32.@)
1319 BOOL WINAPI
ReadFileEx(HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
1320 LPOVERLAPPED overlapped
,
1321 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
1324 int fd
, timeout
, ret
;
1326 TRACE("file %d to buf %p num %ld %p func %p\n",
1327 hFile
, buffer
, bytesToRead
, overlapped
, lpCompletionRoutine
);
1329 /* check that there is an overlapped struct with an event flag */
1330 if ( (overlapped
==NULL
) || NtResetEvent( overlapped
->hEvent
, NULL
) )
1332 TRACE("Overlapped not specified or invalid event flag\n");
1333 SetLastError(ERROR_INVALID_PARAMETER
);
1337 overlapped
->Internal
= STATUS_PENDING
;
1338 overlapped
->InternalHigh
= 0;
1341 * Although the overlapped transfer will be done in this thread
1342 * we still need to register the operation with the server, in
1343 * case it is cancelled and to get a file handle and the timeout info.
1345 SERVER_START_REQ(create_async
)
1347 req
->count
= bytesToRead
;
1348 req
->type
= ASYNC_TYPE_READ
;
1349 req
->file_handle
= hFile
;
1350 ret
= SERVER_CALL();
1351 timeout
= req
->timeout
;
1356 TRACE("server call failed\n");
1360 fd
= FILE_GetUnixHandle( hFile
, GENERIC_WRITE
);
1363 TRACE("Couldn't get FD\n");
1367 ovp
= (async_private
*) HeapAlloc(GetProcessHeap(), 0, sizeof (async_private
));
1370 TRACE("HeapAlloc Failed\n");
1371 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1375 ovp
->lpOverlapped
= overlapped
;
1376 ovp
->count
= bytesToRead
;
1377 ovp
->completion_func
= lpCompletionRoutine
;
1378 ovp
->timeout
= timeout
;
1379 gettimeofday(&ovp
->tv
,NULL
);
1380 add_timeout(&ovp
->tv
,timeout
);
1381 ovp
->event
= POLLIN
;
1382 ovp
->func
= FILE_AsyncReadService
;
1383 ovp
->buffer
= buffer
;
1386 /* hook this overlap into the pending async operation list */
1387 ovp
->next
= NtCurrentTeb()->pending_list
;
1390 ovp
->next
->prev
= ovp
;
1391 NtCurrentTeb()->pending_list
= ovp
;
1393 SetLastError(ERROR_IO_PENDING
);
1395 /* always fail on return, either ERROR_IO_PENDING or other error */
1399 /***********************************************************************
1400 * ReadFile (KERNEL32.@)
1402 BOOL WINAPI
ReadFile( HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
,
1403 LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
1405 int unix_handle
, result
;
1407 TRACE("%d %p %ld %p %p\n", hFile
, buffer
, bytesToRead
,
1408 bytesRead
, overlapped
);
1410 if (bytesRead
) *bytesRead
= 0; /* Do this before anything else */
1411 if (!bytesToRead
) return TRUE
;
1413 /* this will only have impact if the overlapped structure is specified */
1415 return ReadFileEx(hFile
, buffer
, bytesToRead
, overlapped
, NULL
);
1417 unix_handle
= FILE_GetUnixHandle( hFile
, GENERIC_READ
);
1418 if (unix_handle
== -1) return FALSE
;
1420 /* code for synchronous reads */
1421 while ((result
= read( unix_handle
, buffer
, bytesToRead
)) == -1)
1423 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1424 if ((errno
== EFAULT
) && !IsBadWritePtr( buffer
, bytesToRead
)) continue;
1428 close( unix_handle
);
1429 if (result
== -1) return FALSE
;
1430 if (bytesRead
) *bytesRead
= result
;
1435 /***********************************************************************
1436 * FILE_AsyncWriteService (INTERNAL)
1438 * This function is called while the client is waiting on the
1439 * server, so we can't make any server calls here.
1441 static void FILE_AsyncWriteService(struct async_private
*ovp
, int events
)
1443 LPOVERLAPPED lpOverlapped
= ovp
->lpOverlapped
;
1446 TRACE("(%p %p %08x)\n",lpOverlapped
,ovp
->buffer
,events
);
1448 /* if POLLNVAL, then our fd was closed or we have the wrong fd */
1451 ERR("fd %d invalid for %p\n",ovp
->fd
,ovp
);
1452 r
= STATUS_UNSUCCESSFUL
;
1456 /* if there are no events, it must be a timeout */
1459 TRACE("write timed out\n");
1464 /* write some data (non-blocking) */
1465 result
= write(ovp
->fd
, &ovp
->buffer
[lpOverlapped
->InternalHigh
],
1466 ovp
->count
-lpOverlapped
->InternalHigh
);
1468 if ( (result
<0) && ((errno
== EAGAIN
) || (errno
== EINTR
)))
1474 /* check to see if the transfer is complete */
1477 r
= STATUS_UNSUCCESSFUL
;
1481 lpOverlapped
->InternalHigh
+= result
;
1483 TRACE("wrote %d more bytes %ld/%d so far\n",result
,lpOverlapped
->InternalHigh
,ovp
->count
);
1485 if(lpOverlapped
->InternalHigh
< ovp
->count
)
1491 lpOverlapped
->Internal
= r
;
1494 /***********************************************************************
1495 * WriteFileEx (KERNEL32.@)
1497 BOOL WINAPI
WriteFileEx(HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
1498 LPOVERLAPPED overlapped
,
1499 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
1504 TRACE("file %d to buf %p num %ld %p func %p stub\n",
1505 hFile
, buffer
, bytesToWrite
, overlapped
, lpCompletionRoutine
);
1507 if ( (overlapped
== NULL
) || NtResetEvent( overlapped
->hEvent
, NULL
) )
1509 SetLastError(ERROR_INVALID_PARAMETER
);
1513 overlapped
->Internal
= STATUS_PENDING
;
1514 overlapped
->InternalHigh
= 0;
1516 /* need to check the server to get the timeout info */
1517 SERVER_START_REQ(create_async
)
1519 req
->count
= bytesToWrite
;
1520 req
->type
= ASYNC_TYPE_WRITE
;
1521 req
->file_handle
= hFile
;
1522 ret
= SERVER_CALL();
1523 timeout
= req
->timeout
;
1528 TRACE("server call failed\n");
1532 ovp
= (async_private
*) HeapAlloc(GetProcessHeap(), 0, sizeof (async_private
));
1535 TRACE("HeapAlloc Failed\n");
1536 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1539 ovp
->lpOverlapped
= overlapped
;
1540 ovp
->timeout
= timeout
;
1541 gettimeofday(&ovp
->tv
,NULL
);
1542 add_timeout(&ovp
->tv
,timeout
);
1543 ovp
->event
= POLLOUT
;
1544 ovp
->func
= FILE_AsyncWriteService
;
1545 ovp
->buffer
= (LPVOID
) buffer
;
1546 ovp
->count
= bytesToWrite
;
1547 ovp
->completion_func
= lpCompletionRoutine
;
1548 ovp
->fd
= FILE_GetUnixHandle( hFile
, GENERIC_WRITE
);
1551 HeapFree(GetProcessHeap(), 0, ovp
);
1555 /* hook this overlap into the pending async operation list */
1556 ovp
->next
= NtCurrentTeb()->pending_list
;
1559 ovp
->next
->prev
= ovp
;
1560 NtCurrentTeb()->pending_list
= ovp
;
1562 SetLastError(ERROR_IO_PENDING
);
1564 /* always fail on return, either ERROR_IO_PENDING or other error */
1568 /***********************************************************************
1569 * WriteFile (KERNEL32.@)
1571 BOOL WINAPI
WriteFile( HANDLE hFile
, LPCVOID buffer
, DWORD bytesToWrite
,
1572 LPDWORD bytesWritten
, LPOVERLAPPED overlapped
)
1574 int unix_handle
, result
;
1576 TRACE("%d %p %ld %p %p\n", hFile
, buffer
, bytesToWrite
,
1577 bytesWritten
, overlapped
);
1579 if (bytesWritten
) *bytesWritten
= 0; /* Do this before anything else */
1580 if (!bytesToWrite
) return TRUE
;
1582 /* this will only have impact if the overlappd structure is specified */
1584 return WriteFileEx(hFile
, buffer
, bytesToWrite
, overlapped
, NULL
);
1586 unix_handle
= FILE_GetUnixHandle( hFile
, GENERIC_WRITE
);
1587 if (unix_handle
== -1) return FALSE
;
1589 /* synchronous file write */
1590 while ((result
= write( unix_handle
, buffer
, bytesToWrite
)) == -1)
1592 if ((errno
== EAGAIN
) || (errno
== EINTR
)) continue;
1593 if ((errno
== EFAULT
) && !IsBadReadPtr( buffer
, bytesToWrite
)) continue;
1594 if (errno
== ENOSPC
)
1595 SetLastError( ERROR_DISK_FULL
);
1600 close( unix_handle
);
1601 if (result
== -1) return FALSE
;
1602 if (bytesWritten
) *bytesWritten
= result
;
1607 /***********************************************************************
1608 * _hread (KERNEL.349)
1610 LONG WINAPI
WIN16_hread( HFILE16 hFile
, SEGPTR buffer
, LONG count
)
1614 TRACE("%d %08lx %ld\n",
1615 hFile
, (DWORD
)buffer
, count
);
1617 /* Some programs pass a count larger than the allocated buffer */
1618 maxlen
= GetSelectorLimit16( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
1619 if (count
> maxlen
) count
= maxlen
;
1620 return _lread(DosFileHandleToWin32Handle(hFile
), MapSL(buffer
), count
);
1624 /***********************************************************************
1625 * _lread (KERNEL.82)
1627 UINT16 WINAPI
WIN16_lread( HFILE16 hFile
, SEGPTR buffer
, UINT16 count
)
1629 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
1633 /***********************************************************************
1634 * _lread (KERNEL32.@)
1636 UINT WINAPI
_lread( HFILE handle
, LPVOID buffer
, UINT count
)
1639 if (!ReadFile( handle
, buffer
, count
, &result
, NULL
)) return -1;
1644 /***********************************************************************
1645 * _lread16 (KERNEL.82)
1647 UINT16 WINAPI
_lread16( HFILE16 hFile
, LPVOID buffer
, UINT16 count
)
1649 return (UINT16
)_lread(DosFileHandleToWin32Handle(hFile
), buffer
, (LONG
)count
);
1653 /***********************************************************************
1654 * _lcreat (KERNEL.83)
1656 HFILE16 WINAPI
_lcreat16( LPCSTR path
, INT16 attr
)
1658 return Win32HandleToDosFileHandle( _lcreat( path
, attr
) );
1662 /***********************************************************************
1663 * _lcreat (KERNEL32.@)
1665 HFILE WINAPI
_lcreat( LPCSTR path
, INT attr
)
1667 /* Mask off all flags not explicitly allowed by the doc */
1668 attr
&= FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
1669 TRACE("%s %02x\n", path
, attr
);
1670 return CreateFileA( path
, GENERIC_READ
| GENERIC_WRITE
,
1671 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1672 CREATE_ALWAYS
, attr
, 0 );
1676 /***********************************************************************
1677 * SetFilePointer (KERNEL32.@)
1679 DWORD WINAPI
SetFilePointer( HANDLE hFile
, LONG distance
, LONG
*highword
,
1682 DWORD ret
= 0xffffffff;
1684 TRACE("handle %d offset %ld high %ld origin %ld\n",
1685 hFile
, distance
, highword
?*highword
:0, method
);
1687 SERVER_START_REQ( set_file_pointer
)
1689 req
->handle
= hFile
;
1690 req
->low
= distance
;
1691 req
->high
= highword
? *highword
: (distance
>= 0) ? 0 : -1;
1692 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1693 req
->whence
= method
;
1695 if (!SERVER_CALL_ERR())
1698 if (highword
) *highword
= req
->new_high
;
1706 /***********************************************************************
1707 * _llseek (KERNEL.84)
1710 * Seeking before the start of the file should be allowed for _llseek16,
1711 * but cause subsequent I/O operations to fail (cf. interrupt list)
1714 LONG WINAPI
_llseek16( HFILE16 hFile
, LONG lOffset
, INT16 nOrigin
)
1716 return SetFilePointer( DosFileHandleToWin32Handle(hFile
), lOffset
, NULL
, nOrigin
);
1720 /***********************************************************************
1721 * _llseek (KERNEL32.@)
1723 LONG WINAPI
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
1725 return SetFilePointer( hFile
, lOffset
, NULL
, nOrigin
);
1729 /***********************************************************************
1730 * _lopen (KERNEL.85)
1732 HFILE16 WINAPI
_lopen16( LPCSTR path
, INT16 mode
)
1734 return Win32HandleToDosFileHandle( _lopen( path
, mode
) );
1738 /***********************************************************************
1739 * _lopen (KERNEL32.@)
1741 HFILE WINAPI
_lopen( LPCSTR path
, INT mode
)
1743 DWORD access
, sharing
;
1745 TRACE("('%s',%04x)\n", path
, mode
);
1746 FILE_ConvertOFMode( mode
, &access
, &sharing
);
1747 return CreateFileA( path
, access
, sharing
, NULL
, OPEN_EXISTING
, 0, 0 );
1751 /***********************************************************************
1752 * _lwrite (KERNEL.86)
1754 UINT16 WINAPI
_lwrite16( HFILE16 hFile
, LPCSTR buffer
, UINT16 count
)
1756 return (UINT16
)_hwrite( DosFileHandleToWin32Handle(hFile
), buffer
, (LONG
)count
);
1759 /***********************************************************************
1760 * _lwrite (KERNEL32.@)
1762 UINT WINAPI
_lwrite( HFILE hFile
, LPCSTR buffer
, UINT count
)
1764 return (UINT
)_hwrite( hFile
, buffer
, (LONG
)count
);
1768 /***********************************************************************
1769 * _hread16 (KERNEL.349)
1771 LONG WINAPI
_hread16( HFILE16 hFile
, LPVOID buffer
, LONG count
)
1773 return _lread( DosFileHandleToWin32Handle(hFile
), buffer
, count
);
1777 /***********************************************************************
1778 * _hread (KERNEL32.@)
1780 LONG WINAPI
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
1782 return _lread( hFile
, buffer
, count
);
1786 /***********************************************************************
1787 * _hwrite (KERNEL.350)
1789 LONG WINAPI
_hwrite16( HFILE16 hFile
, LPCSTR buffer
, LONG count
)
1791 return _hwrite( DosFileHandleToWin32Handle(hFile
), buffer
, count
);
1795 /***********************************************************************
1796 * _hwrite (KERNEL32.@)
1798 * experimentation yields that _lwrite:
1799 * o truncates the file at the current position with
1801 * o returns 0 on a 0 length write
1802 * o works with console handles
1805 LONG WINAPI
_hwrite( HFILE handle
, LPCSTR buffer
, LONG count
)
1809 TRACE("%d %p %ld\n", handle
, buffer
, count
);
1813 /* Expand or truncate at current position */
1814 if (!SetEndOfFile( handle
)) return HFILE_ERROR
;
1817 if (!WriteFile( handle
, buffer
, count
, &result
, NULL
))
1823 /***********************************************************************
1824 * SetHandleCount (KERNEL.199)
1826 UINT16 WINAPI
SetHandleCount16( UINT16 count
)
1828 return SetHandleCount( count
);
1832 /*************************************************************************
1833 * SetHandleCount (KERNEL32.@)
1835 UINT WINAPI
SetHandleCount( UINT count
)
1837 return min( 256, count
);
1841 /***********************************************************************
1842 * FlushFileBuffers (KERNEL32.@)
1844 BOOL WINAPI
FlushFileBuffers( HANDLE hFile
)
1847 SERVER_START_REQ( flush_file
)
1849 req
->handle
= hFile
;
1850 ret
= !SERVER_CALL_ERR();
1857 /**************************************************************************
1858 * SetEndOfFile (KERNEL32.@)
1860 BOOL WINAPI
SetEndOfFile( HANDLE hFile
)
1863 SERVER_START_REQ( truncate_file
)
1865 req
->handle
= hFile
;
1866 ret
= !SERVER_CALL_ERR();
1873 /***********************************************************************
1874 * DeleteFile (KERNEL.146)
1876 BOOL16 WINAPI
DeleteFile16( LPCSTR path
)
1878 return DeleteFileA( path
);
1882 /***********************************************************************
1883 * DeleteFileA (KERNEL32.@)
1885 BOOL WINAPI
DeleteFileA( LPCSTR path
)
1887 DOS_FULL_NAME full_name
;
1889 TRACE("'%s'\n", path
);
1893 ERR("Empty path passed\n");
1896 if (DOSFS_GetDevice( path
))
1898 WARN("cannot remove DOS device '%s'!\n", path
);
1899 SetLastError( ERROR_FILE_NOT_FOUND
);
1903 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
1904 if (unlink( full_name
.long_name
) == -1)
1913 /***********************************************************************
1914 * DeleteFileW (KERNEL32.@)
1916 BOOL WINAPI
DeleteFileW( LPCWSTR path
)
1918 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
1919 BOOL ret
= DeleteFileA( xpath
);
1920 HeapFree( GetProcessHeap(), 0, xpath
);
1925 /***********************************************************************
1926 * GetFileType (KERNEL32.@)
1928 DWORD WINAPI
GetFileType( HANDLE hFile
)
1930 DWORD ret
= FILE_TYPE_UNKNOWN
;
1931 SERVER_START_REQ( get_file_info
)
1933 req
->handle
= hFile
;
1934 if (!SERVER_CALL_ERR()) ret
= req
->type
;
1941 /**************************************************************************
1942 * MoveFileExA (KERNEL32.@)
1944 BOOL WINAPI
MoveFileExA( LPCSTR fn1
, LPCSTR fn2
, DWORD flag
)
1946 DOS_FULL_NAME full_name1
, full_name2
;
1948 TRACE("(%s,%s,%04lx)\n", fn1
, fn2
, flag
);
1950 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
1952 if (fn2
) /* !fn2 means delete fn1 */
1954 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
))
1956 /* target exists, check if we may overwrite */
1957 if (!(flag
& MOVEFILE_REPLACE_EXISTING
))
1959 /* FIXME: Use right error code */
1960 SetLastError( ERROR_ACCESS_DENIED
);
1964 else if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
1966 /* Source name and target path are valid */
1968 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
)
1970 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1971 Perhaps we should queue these command and execute it
1972 when exiting... What about using on_exit(2)
1974 FIXME("Please move existing file '%s' to file '%s' when Wine has finished\n",
1975 full_name1
.long_name
, full_name2
.long_name
);
1979 if (full_name1
.drive
!= full_name2
.drive
)
1981 /* use copy, if allowed */
1982 if (!(flag
& MOVEFILE_COPY_ALLOWED
))
1984 /* FIXME: Use right error code */
1985 SetLastError( ERROR_FILE_EXISTS
);
1988 return CopyFileA( fn1
, fn2
, !(flag
& MOVEFILE_REPLACE_EXISTING
) );
1990 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
1997 else /* fn2 == NULL means delete source */
1999 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
)
2001 if (flag
& MOVEFILE_COPY_ALLOWED
) {
2002 WARN("Illegal flag\n");
2003 SetLastError( ERROR_GEN_FAILURE
);
2006 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
2007 Perhaps we should queue these command and execute it
2008 when exiting... What about using on_exit(2)
2010 FIXME("Please delete file '%s' when Wine has finished\n",
2011 full_name1
.long_name
);
2015 if (unlink( full_name1
.long_name
) == -1)
2020 return TRUE
; /* successfully deleted */
2024 /**************************************************************************
2025 * MoveFileExW (KERNEL32.@)
2027 BOOL WINAPI
MoveFileExW( LPCWSTR fn1
, LPCWSTR fn2
, DWORD flag
)
2029 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
2030 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
2031 BOOL res
= MoveFileExA( afn1
, afn2
, flag
);
2032 HeapFree( GetProcessHeap(), 0, afn1
);
2033 HeapFree( GetProcessHeap(), 0, afn2
);
2038 /**************************************************************************
2039 * MoveFileA (KERNEL32.@)
2041 * Move file or directory
2043 BOOL WINAPI
MoveFileA( LPCSTR fn1
, LPCSTR fn2
)
2045 DOS_FULL_NAME full_name1
, full_name2
;
2049 TRACE("(%s,%s)\n", fn1
, fn2
);
2051 if (!DOSFS_GetFullName( fn1
, TRUE
, &full_name1
)) return FALSE
;
2052 if (DOSFS_GetFullName( fn2
, TRUE
, &full_name2
)) {
2053 /* The new name must not already exist */
2054 SetLastError(ERROR_ALREADY_EXISTS
);
2057 if (!DOSFS_GetFullName( fn2
, FALSE
, &full_name2
)) return FALSE
;
2059 if (full_name1
.drive
== full_name2
.drive
) /* move */
2060 if (rename( full_name1
.long_name
, full_name2
.long_name
) == -1)
2067 if (stat( full_name1
.long_name
, &fstat
))
2069 WARN("Invalid source file %s\n",
2070 full_name1
.long_name
);
2074 if (S_ISDIR(fstat
.st_mode
)) {
2075 /* No Move for directories across file systems */
2076 /* FIXME: Use right error code */
2077 SetLastError( ERROR_GEN_FAILURE
);
2081 return CopyFileA(fn1
, fn2
, TRUE
); /*fail, if exist */
2086 /**************************************************************************
2087 * MoveFileW (KERNEL32.@)
2089 BOOL WINAPI
MoveFileW( LPCWSTR fn1
, LPCWSTR fn2
)
2091 LPSTR afn1
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn1
);
2092 LPSTR afn2
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn2
);
2093 BOOL res
= MoveFileA( afn1
, afn2
);
2094 HeapFree( GetProcessHeap(), 0, afn1
);
2095 HeapFree( GetProcessHeap(), 0, afn2
);
2100 /**************************************************************************
2101 * CopyFileA (KERNEL32.@)
2103 BOOL WINAPI
CopyFileA( LPCSTR source
, LPCSTR dest
, BOOL fail_if_exists
)
2106 BY_HANDLE_FILE_INFORMATION info
;
2112 if ((h1
= _lopen( source
, OF_READ
)) == HFILE_ERROR
) return FALSE
;
2113 if (!GetFileInformationByHandle( h1
, &info
))
2118 mode
= (info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
2119 if ((h2
= CreateFileA( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
2120 fail_if_exists
? CREATE_NEW
: CREATE_ALWAYS
,
2121 info
.dwFileAttributes
, h1
)) == INVALID_HANDLE_VALUE
)
2126 while ((count
= _lread( h1
, buffer
, sizeof(buffer
) )) > 0)
2131 INT res
= _lwrite( h2
, p
, count
);
2132 if (res
<= 0) goto done
;
2145 /**************************************************************************
2146 * CopyFileW (KERNEL32.@)
2148 BOOL WINAPI
CopyFileW( LPCWSTR source
, LPCWSTR dest
, BOOL fail_if_exists
)
2150 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, source
);
2151 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, dest
);
2152 BOOL ret
= CopyFileA( sourceA
, destA
, fail_if_exists
);
2153 HeapFree( GetProcessHeap(), 0, sourceA
);
2154 HeapFree( GetProcessHeap(), 0, destA
);
2159 /**************************************************************************
2160 * CopyFileExA (KERNEL32.@)
2162 * This implementation ignores most of the extra parameters passed-in into
2163 * the "ex" version of the method and calls the CopyFile method.
2164 * It will have to be fixed eventually.
2166 BOOL WINAPI
CopyFileExA(LPCSTR sourceFilename
,
2167 LPCSTR destFilename
,
2168 LPPROGRESS_ROUTINE progressRoutine
,
2170 LPBOOL cancelFlagPointer
,
2173 BOOL failIfExists
= FALSE
;
2176 * Interpret the only flag that CopyFile can interpret.
2178 if ( (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0)
2180 failIfExists
= TRUE
;
2183 return CopyFileA(sourceFilename
, destFilename
, failIfExists
);
2186 /**************************************************************************
2187 * CopyFileExW (KERNEL32.@)
2189 BOOL WINAPI
CopyFileExW(LPCWSTR sourceFilename
,
2190 LPCWSTR destFilename
,
2191 LPPROGRESS_ROUTINE progressRoutine
,
2193 LPBOOL cancelFlagPointer
,
2196 LPSTR sourceA
= HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename
);
2197 LPSTR destA
= HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename
);
2199 BOOL ret
= CopyFileExA(sourceA
,
2206 HeapFree( GetProcessHeap(), 0, sourceA
);
2207 HeapFree( GetProcessHeap(), 0, destA
);
2213 /***********************************************************************
2214 * SetFileTime (KERNEL32.@)
2216 BOOL WINAPI
SetFileTime( HANDLE hFile
,
2217 const FILETIME
*lpCreationTime
,
2218 const FILETIME
*lpLastAccessTime
,
2219 const FILETIME
*lpLastWriteTime
)
2222 SERVER_START_REQ( set_file_time
)
2224 req
->handle
= hFile
;
2225 if (lpLastAccessTime
)
2226 RtlTimeToSecondsSince1970( lpLastAccessTime
, (DWORD
*)&req
->access_time
);
2228 req
->access_time
= 0; /* FIXME */
2229 if (lpLastWriteTime
)
2230 RtlTimeToSecondsSince1970( lpLastWriteTime
, (DWORD
*)&req
->write_time
);
2232 req
->write_time
= 0; /* FIXME */
2233 ret
= !SERVER_CALL_ERR();
2240 /**************************************************************************
2241 * LockFile (KERNEL32.@)
2243 BOOL WINAPI
LockFile( HANDLE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
2244 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
)
2247 SERVER_START_REQ( lock_file
)
2249 req
->handle
= hFile
;
2250 req
->offset_low
= dwFileOffsetLow
;
2251 req
->offset_high
= dwFileOffsetHigh
;
2252 req
->count_low
= nNumberOfBytesToLockLow
;
2253 req
->count_high
= nNumberOfBytesToLockHigh
;
2254 ret
= !SERVER_CALL_ERR();
2260 /**************************************************************************
2261 * LockFileEx [KERNEL32.@]
2263 * Locks a byte range within an open file for shared or exclusive access.
2270 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
2272 BOOL WINAPI
LockFileEx( HANDLE hFile
, DWORD flags
, DWORD reserved
,
2273 DWORD nNumberOfBytesToLockLow
, DWORD nNumberOfBytesToLockHigh
,
2274 LPOVERLAPPED pOverlapped
)
2276 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2277 hFile
, flags
, reserved
, nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
,
2280 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2283 ERR("reserved == %ld: Supposed to be 0??\n", reserved
);
2284 SetLastError(ERROR_INVALID_PARAMETER
);
2291 /**************************************************************************
2292 * UnlockFile (KERNEL32.@)
2294 BOOL WINAPI
UnlockFile( HANDLE hFile
, DWORD dwFileOffsetLow
, DWORD dwFileOffsetHigh
,
2295 DWORD nNumberOfBytesToUnlockLow
, DWORD nNumberOfBytesToUnlockHigh
)
2298 SERVER_START_REQ( unlock_file
)
2300 req
->handle
= hFile
;
2301 req
->offset_low
= dwFileOffsetLow
;
2302 req
->offset_high
= dwFileOffsetHigh
;
2303 req
->count_low
= nNumberOfBytesToUnlockLow
;
2304 req
->count_high
= nNumberOfBytesToUnlockHigh
;
2305 ret
= !SERVER_CALL_ERR();
2312 /**************************************************************************
2313 * UnlockFileEx (KERNEL32.@)
2315 BOOL WINAPI
UnlockFileEx(
2318 DWORD nNumberOfBytesToUnlockLow
,
2319 DWORD nNumberOfBytesToUnlockHigh
,
2320 LPOVERLAPPED lpOverlapped
2323 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2324 hFile
, dwReserved
, nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
,
2326 if (dwReserved
== 0)
2327 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2330 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved
);
2331 SetLastError(ERROR_INVALID_PARAMETER
);
2340 struct DOS_FILE_LOCK
{
2341 struct DOS_FILE_LOCK
* next
;
2345 FILE_OBJECT
* dos_file
;
2346 /* char * unix_name;*/
2349 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK
;
2351 static DOS_FILE_LOCK
*locks
= NULL
;
2352 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
);
2355 /* Locks need to be mirrored because unix file locking is based
2356 * on the pid. Inside of wine there can be multiple WINE processes
2357 * that share the same unix pid.
2358 * Read's and writes should check these locks also - not sure
2359 * how critical that is at this point (FIXME).
2362 static BOOL
DOS_AddLock(FILE_OBJECT
*file
, struct flock
*f
)
2364 DOS_FILE_LOCK
*curr
;
2367 processId
= GetCurrentProcessId();
2369 /* check if lock overlaps a current lock for the same file */
2371 for (curr
= locks
; curr
; curr
= curr
->next
) {
2372 if (strcmp(curr
->unix_name
, file
->unix_name
) == 0) {
2373 if ((f
->l_start
== curr
->base
) && (f
->l_len
== curr
->len
))
2374 return TRUE
;/* region is identic */
2375 if ((f
->l_start
< (curr
->base
+ curr
->len
)) &&
2376 ((f
->l_start
+ f
->l_len
) > curr
->base
)) {
2377 /* region overlaps */
2384 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(DOS_FILE_LOCK
) );
2385 curr
->processId
= GetCurrentProcessId();
2386 curr
->base
= f
->l_start
;
2387 curr
->len
= f
->l_len
;
2388 /* curr->unix_name = HEAP_strdupA( GetProcessHeap(), 0, file->unix_name);*/
2390 curr
->dos_file
= file
;
2395 static void DOS_RemoveFileLocks(FILE_OBJECT
*file
)
2398 DOS_FILE_LOCK
**curr
;
2401 processId
= GetCurrentProcessId();
2404 if ((*curr
)->dos_file
== file
) {
2406 *curr
= (*curr
)->next
;
2407 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2408 HeapFree( GetProcessHeap(), 0, rem
);
2411 curr
= &(*curr
)->next
;
2415 static BOOL
DOS_RemoveLock(FILE_OBJECT
*file
, struct flock
*f
)
2418 DOS_FILE_LOCK
**curr
;
2421 processId
= GetCurrentProcessId();
2422 for (curr
= &locks
; *curr
; curr
= &(*curr
)->next
) {
2423 if ((*curr
)->processId
== processId
&&
2424 (*curr
)->dos_file
== file
&&
2425 (*curr
)->base
== f
->l_start
&&
2426 (*curr
)->len
== f
->l_len
) {
2427 /* this is the same lock */
2429 *curr
= (*curr
)->next
;
2430 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2431 HeapFree( GetProcessHeap(), 0, rem
);
2435 /* no matching lock found */
2440 /**************************************************************************
2441 * LockFile (KERNEL32.@)
2443 BOOL WINAPI
LockFile(
2444 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2445 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
2450 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2451 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2452 nNumberOfBytesToLockLow
, nNumberOfBytesToLockHigh
);
2454 if (dwFileOffsetHigh
|| nNumberOfBytesToLockHigh
) {
2455 FIXME("Unimplemented bytes > 32bits\n");
2459 f
.l_start
= dwFileOffsetLow
;
2460 f
.l_len
= nNumberOfBytesToLockLow
;
2461 f
.l_whence
= SEEK_SET
;
2465 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2467 /* shadow locks internally */
2468 if (!DOS_AddLock(file
, &f
)) {
2469 SetLastError( ERROR_LOCK_VIOLATION
);
2473 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2474 #ifdef USE_UNIX_LOCKS
2475 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2476 if (errno
== EACCES
|| errno
== EAGAIN
) {
2477 SetLastError( ERROR_LOCK_VIOLATION
);
2482 /* remove our internal copy of the lock */
2483 DOS_RemoveLock(file
, &f
);
2491 /**************************************************************************
2492 * UnlockFile (KERNEL32.@)
2494 BOOL WINAPI
UnlockFile(
2495 HFILE hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
2496 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
2501 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2502 hFile
, dwFileOffsetLow
, dwFileOffsetHigh
,
2503 nNumberOfBytesToUnlockLow
, nNumberOfBytesToUnlockHigh
);
2505 if (dwFileOffsetHigh
|| nNumberOfBytesToUnlockHigh
) {
2506 WARN("Unimplemented bytes > 32bits\n");
2510 f
.l_start
= dwFileOffsetLow
;
2511 f
.l_len
= nNumberOfBytesToUnlockLow
;
2512 f
.l_whence
= SEEK_SET
;
2516 if (!(file
= FILE_GetFile(hFile
,0,NULL
))) return FALSE
;
2518 DOS_RemoveLock(file
, &f
); /* ok if fails - may be another wine */
2520 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2521 #ifdef USE_UNIX_LOCKS
2522 if (fcntl(file
->unix_handle
, F_SETLK
, &f
) == -1) {
2531 /**************************************************************************
2532 * GetFileAttributesExA [KERNEL32.@]
2534 BOOL WINAPI
GetFileAttributesExA(
2535 LPCSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2536 LPVOID lpFileInformation
)
2538 DOS_FULL_NAME full_name
;
2539 BY_HANDLE_FILE_INFORMATION info
;
2541 if (lpFileName
== NULL
) return FALSE
;
2542 if (lpFileInformation
== NULL
) return FALSE
;
2544 if (fInfoLevelId
== GetFileExInfoStandard
) {
2545 LPWIN32_FILE_ATTRIBUTE_DATA lpFad
=
2546 (LPWIN32_FILE_ATTRIBUTE_DATA
) lpFileInformation
;
2547 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
)) return FALSE
;
2548 if (!FILE_Stat( full_name
.long_name
, &info
)) return FALSE
;
2550 lpFad
->dwFileAttributes
= info
.dwFileAttributes
;
2551 lpFad
->ftCreationTime
= info
.ftCreationTime
;
2552 lpFad
->ftLastAccessTime
= info
.ftLastAccessTime
;
2553 lpFad
->ftLastWriteTime
= info
.ftLastWriteTime
;
2554 lpFad
->nFileSizeHigh
= info
.nFileSizeHigh
;
2555 lpFad
->nFileSizeLow
= info
.nFileSizeLow
;
2558 FIXME("invalid info level %d!\n", fInfoLevelId
);
2566 /**************************************************************************
2567 * GetFileAttributesExW [KERNEL32.@]
2569 BOOL WINAPI
GetFileAttributesExW(
2570 LPCWSTR lpFileName
, GET_FILEEX_INFO_LEVELS fInfoLevelId
,
2571 LPVOID lpFileInformation
)
2573 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
2575 GetFileAttributesExA( nameA
, fInfoLevelId
, lpFileInformation
);
2576 HeapFree( GetProcessHeap(), 0, nameA
);