2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
8 * Fix the CopyFileEx methods to implement the "extented" functionality.
9 * Right now, they simply call the CopyFile method.
18 #include <sys/errno.h>
19 #include <sys/types.h>
30 #include "wine/winbase16.h"
31 #include "wine/winestring.h"
46 #include "server/request.h"
49 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
50 #define MAP_ANON MAP_ANONYMOUS
53 /* Size of per-process table of DOS handles */
54 #define DOS_TABLE_SIZE 256
57 /***********************************************************************
60 * Convert OF_* mode into flags for CreateFile.
62 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
66 case OF_READ: *access = GENERIC_READ; break;
67 case OF_WRITE: *access = GENERIC_WRITE; break;
68 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
69 default: *access = 0; break;
73 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
74 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
75 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
76 case OF_SHARE_DENY_NONE:
78 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
84 /***********************************************************************
88 * oldmode[I] mode how file was first opened
89 * mode[I] mode how the file should get opened
94 * Look what we have to do with the given SHARE modes
96 * Ralph Brown's interrupt list gives following explication, I guess
97 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
99 * FIXME: Validate this function
100 ========from Ralph Brown's list =========
102 Values of DOS file sharing behavior:
103 | Second and subsequent Opens
104 First |Compat Deny Deny Deny Deny
105 Open | All Write Read None
106 |R W RW R W RW R W RW R W RW R W RW
107 - - - - -| - - - - - - - - - - - - - - - - -
108 Compat R |Y Y Y N N N 1 N N N N N 1 N N
109 W |Y Y Y N N N N N N N N N N N N
110 RW|Y Y Y N N N N N N N N N N N N
112 Deny R |C C C N N N N N N N N N N N N
113 All W |C C C N N N N N N N N N N N N
114 RW|C C C N N N N N N N N N N N N
116 Deny R |2 C C N N N Y N N N N N Y N N
117 Write W |C C C N N N N N N Y N N Y N N
118 RW|C C C N N N N N N N N N Y N N
120 Deny R |C C C N N N N Y N N N N N Y N
121 Read W |C C C N N N N N N N Y N N Y N
122 RW|C C C N N N N N N N N N N Y N
124 Deny R |2 C C N N N Y Y Y N N N Y Y Y
125 None W |C C C N N N N N N Y Y Y Y Y Y
126 RW|C C C N N N N N N N N N Y Y Y
127 Legend: Y = open succeeds, N = open fails with error code 05h
128 C = open fails, INT 24 generated
129 1 = open succeeds if file read-only, else fails with error code
130 2 = open succeeds if file read-only, else fails with INT 24
131 ========end of description from Ralph Brown's List =====
132 For every "Y" in the table we return FALSE
133 For every "N" we set the DOS_ERROR and return TRUE
134 For all other cases we barf,set the DOS_ERROR and return TRUE
137 static BOOL FILE_ShareDeny( int mode, int oldmode)
139 int oldsharemode = oldmode & 0x70;
140 int sharemode = mode & 0x70;
141 int oldopenmode = oldmode & 3;
142 int openmode = mode & 3;
144 switch (oldsharemode)
146 case OF_SHARE_COMPAT:
147 if (sharemode == OF_SHARE_COMPAT) return FALSE;
148 if (openmode == OF_READ) goto test_ro_err05 ;
150 case OF_SHARE_EXCLUSIVE:
151 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
153 case OF_SHARE_DENY_WRITE:
154 if (openmode != OF_READ)
156 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
161 case OF_SHARE_COMPAT:
162 if (oldopenmode == OF_READ) goto test_ro_int24 ;
164 case OF_SHARE_DENY_NONE :
166 case OF_SHARE_DENY_WRITE :
167 if (oldopenmode == OF_READ) return FALSE;
168 case OF_SHARE_DENY_READ :
169 if (oldopenmode == OF_WRITE) return FALSE;
170 case OF_SHARE_EXCLUSIVE:
175 case OF_SHARE_DENY_READ:
176 if (openmode != OF_WRITE)
178 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
183 case OF_SHARE_COMPAT:
185 case OF_SHARE_DENY_NONE :
187 case OF_SHARE_DENY_WRITE :
188 if (oldopenmode == OF_READ) return FALSE;
189 case OF_SHARE_DENY_READ :
190 if (oldopenmode == OF_WRITE) return FALSE;
191 case OF_SHARE_EXCLUSIVE:
196 case OF_SHARE_DENY_NONE:
199 case OF_SHARE_COMPAT:
201 case OF_SHARE_DENY_NONE :
203 case OF_SHARE_DENY_WRITE :
204 if (oldopenmode == OF_READ) return FALSE;
205 case OF_SHARE_DENY_READ :
206 if (oldopenmode == OF_WRITE) return FALSE;
207 case OF_SHARE_EXCLUSIVE:
212 ERR(file,"unknown mode\n");
214 ERR(file,"shouldn't happen\n");
215 ERR(file,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
219 if (oldmode == OF_READ)
223 FIXME(file,"generate INT24 missing\n");
224 /* Is this the right error? */
225 SetLastError( ERROR_ACCESS_DENIED );
229 if (oldmode == OF_READ)
233 TRACE(file,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
234 SetLastError( ERROR_ACCESS_DENIED );
240 /***********************************************************************
243 * Set the DOS error code from errno.
245 void FILE_SetDosError(void)
247 int save_errno = errno; /* errno gets overwritten by printf */
249 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
253 SetLastError( ERROR_SHARING_VIOLATION );
256 SetLastError( ERROR_INVALID_HANDLE );
259 SetLastError( ERROR_HANDLE_DISK_FULL );
264 SetLastError( ERROR_ACCESS_DENIED );
267 SetLastError( ERROR_LOCK_VIOLATION );
270 SetLastError( ERROR_FILE_NOT_FOUND );
273 SetLastError( ERROR_CANNOT_MAKE );
277 SetLastError( ERROR_NO_MORE_FILES );
280 SetLastError( ERROR_FILE_EXISTS );
284 SetLastError( ERROR_SEEK );
287 SetLastError( ERROR_DIR_NOT_EMPTY );
290 perror( "int21: unknown errno" );
291 SetLastError( ERROR_GEN_FAILURE );
298 /***********************************************************************
301 * Duplicate a Unix handle into a task handle.
303 HFILE FILE_DupUnixHandle( int fd, DWORD access )
306 struct create_file_request req;
307 struct create_file_reply reply;
309 if ((unix_handle = dup(fd)) == -1)
312 return INVALID_HANDLE_VALUE;
316 req.sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
320 CLIENT_SendRequest( REQ_CREATE_FILE, unix_handle, 1,
322 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
327 /***********************************************************************
330 * Implementation of CreateFile. Takes a Unix path name.
332 HFILE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
333 LPSECURITY_ATTRIBUTES sa, DWORD creation,
334 DWORD attributes, HANDLE template )
336 struct create_file_request req;
337 struct create_file_reply reply;
340 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
341 req.sharing = sharing;
342 req.create = creation;
343 req.attrs = attributes;
344 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
346 filename, strlen(filename) + 1 );
348 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
350 /* If write access failed, retry without GENERIC_WRITE */
352 if ((reply.handle == -1) && !Options.failReadOnly &&
353 (access & GENERIC_WRITE))
355 DWORD lasterror = GetLastError();
356 if ((lasterror == ERROR_ACCESS_DENIED) ||
357 (lasterror == ERROR_WRITE_PROTECT))
359 req.access &= ~GENERIC_WRITE;
360 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
362 filename, strlen(filename) + 1 );
364 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
371 /***********************************************************************
374 * Same as FILE_CreateFile but for a device
376 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
378 struct create_device_request req;
379 struct create_device_reply reply;
382 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
384 CLIENT_SendRequest( REQ_CREATE_DEVICE, -1, 1, &req, sizeof(req) );
386 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
391 /*************************************************************************
392 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
394 * Creates or opens an object, and returns a handle that can be used to
395 * access that object.
399 * filename [I] pointer to filename to be accessed
400 * access [I] access mode requested
401 * sharing [I] share mode
402 * sa [I] pointer to security attributes
403 * creation [I] how to create the file
404 * attributes [I] attributes for newly created file
405 * template [I] handle to file with extended attributes to copy
408 * Success: Open handle to specified file
409 * Failure: INVALID_HANDLE_VALUE
412 * Should call SetLastError() on failure.
416 * Doesn't support character devices, pipes, template files, or a
417 * lot of the 'attributes' flags yet.
419 HFILE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
420 LPSECURITY_ATTRIBUTES sa, DWORD creation,
421 DWORD attributes, HANDLE template )
423 DOS_FULL_NAME full_name;
427 SetLastError( ERROR_INVALID_PARAMETER );
431 /* If the name starts with '\\?\', ignore the first 4 chars. */
432 if (!strncmp(filename, "\\\\?\\", 4))
435 if (!strncmp(filename, "UNC\\", 4))
437 FIXME( file, "UNC name (%s) not supported.\n", filename );
438 SetLastError( ERROR_PATH_NOT_FOUND );
443 if (!strncmp(filename, "\\\\.\\", 4))
444 return DEVICE_Open( filename+4, access, sa );
446 /* If the name still starts with '\\', it's a UNC name. */
447 if (!strncmp(filename, "\\\\", 2))
449 FIXME( file, "UNC name (%s) not supported.\n", filename );
450 SetLastError( ERROR_PATH_NOT_FOUND );
454 /* Open a console for CONIN$ or CONOUT$ */
455 if (!lstrcmpiA(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
456 if (!lstrcmpiA(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
458 if (DOSFS_GetDevice( filename ))
462 TRACE(file, "opening device '%s'\n", filename );
464 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
467 /* Do not silence this please. It is a critical error. -MM */
468 ERR(file, "Couldn't open device '%s'!\n",filename);
469 SetLastError( ERROR_FILE_NOT_FOUND );
473 /* check for filename, don't check for last entry if creating */
474 if (!DOSFS_GetFullName( filename,
475 (creation == OPEN_EXISTING) || (creation == TRUNCATE_EXISTING), &full_name ))
478 return FILE_CreateFile( full_name.long_name, access, sharing,
479 sa, creation, attributes, template );
484 /*************************************************************************
485 * CreateFile32W (KERNEL32.48)
487 HFILE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
488 LPSECURITY_ATTRIBUTES sa, DWORD creation,
489 DWORD attributes, HANDLE template)
491 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
492 HFILE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
493 HeapFree( GetProcessHeap(), 0, afn );
498 /***********************************************************************
501 * Fill a file information from a struct stat.
503 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
505 if (S_ISDIR(st->st_mode))
506 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
508 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
509 if (!(st->st_mode & S_IWUSR))
510 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
512 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
513 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
514 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
516 info->dwVolumeSerialNumber = 0; /* FIXME */
517 info->nFileSizeHigh = 0;
518 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
519 info->nNumberOfLinks = st->st_nlink;
520 info->nFileIndexHigh = 0;
521 info->nFileIndexLow = st->st_ino;
525 /***********************************************************************
528 * Stat a Unix path name. Return TRUE if OK.
530 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
534 if (!unixName || !info) return FALSE;
536 if (stat( unixName, &st ) == -1)
541 FILE_FillInfo( &st, info );
546 /***********************************************************************
547 * GetFileInformationByHandle (KERNEL32.219)
549 DWORD WINAPI GetFileInformationByHandle( HFILE hFile,
550 BY_HANDLE_FILE_INFORMATION *info )
552 struct get_file_info_request req;
553 struct get_file_info_reply reply;
557 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
558 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
560 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftCreationTime, 0 );
561 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftLastWriteTime, 0 );
562 DOSFS_UnixTimeToFileTime( reply.access_time, &info->ftLastAccessTime, 0 );
563 info->dwFileAttributes = reply.attr;
564 info->dwVolumeSerialNumber = reply.serial;
565 info->nFileSizeHigh = reply.size_high;
566 info->nFileSizeLow = reply.size_low;
567 info->nNumberOfLinks = reply.links;
568 info->nFileIndexHigh = reply.index_high;
569 info->nFileIndexLow = reply.index_low;
574 /**************************************************************************
575 * GetFileAttributes16 (KERNEL.420)
577 DWORD WINAPI GetFileAttributes16( LPCSTR name )
579 return GetFileAttributesA( name );
583 /**************************************************************************
584 * GetFileAttributes32A (KERNEL32.217)
586 DWORD WINAPI GetFileAttributesA( LPCSTR name )
588 DOS_FULL_NAME full_name;
589 BY_HANDLE_FILE_INFORMATION info;
591 if (name == NULL || *name=='\0') return -1;
593 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
594 if (!FILE_Stat( full_name.long_name, &info )) return -1;
595 return info.dwFileAttributes;
599 /**************************************************************************
600 * GetFileAttributes32W (KERNEL32.218)
602 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
604 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
605 DWORD res = GetFileAttributesA( nameA );
606 HeapFree( GetProcessHeap(), 0, nameA );
611 /***********************************************************************
612 * GetFileSize (KERNEL32.220)
614 DWORD WINAPI GetFileSize( HFILE hFile, LPDWORD filesizehigh )
616 BY_HANDLE_FILE_INFORMATION info;
617 if (!GetFileInformationByHandle( hFile, &info )) return 0;
618 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
619 return info.nFileSizeLow;
623 /***********************************************************************
624 * GetFileTime (KERNEL32.221)
626 BOOL WINAPI GetFileTime( HFILE hFile, FILETIME *lpCreationTime,
627 FILETIME *lpLastAccessTime,
628 FILETIME *lpLastWriteTime )
630 BY_HANDLE_FILE_INFORMATION info;
631 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
632 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
633 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
634 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
638 /***********************************************************************
639 * CompareFileTime (KERNEL32.28)
641 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
643 if (!x || !y) return -1;
645 if (x->dwHighDateTime > y->dwHighDateTime)
647 if (x->dwHighDateTime < y->dwHighDateTime)
649 if (x->dwLowDateTime > y->dwLowDateTime)
651 if (x->dwLowDateTime < y->dwLowDateTime)
657 /***********************************************************************
658 * GetTempFileName16 (KERNEL.97)
660 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
665 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
666 drive |= DRIVE_GetCurrentDrive() + 'A';
668 if ((drive & TF_FORCEDRIVE) &&
669 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
671 drive &= ~TF_FORCEDRIVE;
672 WARN(file, "invalid drive %d specified\n", drive );
675 if (drive & TF_FORCEDRIVE)
676 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
678 GetTempPathA( 132, temppath );
679 return (UINT16)GetTempFileNameA( temppath, prefix, unique, buffer );
683 /***********************************************************************
684 * GetTempFileName32A (KERNEL32.290)
686 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
689 static UINT unique_temp;
690 DOS_FULL_NAME full_name;
695 if ( !path || !prefix || !buffer ) return 0;
697 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
698 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
700 strcpy( buffer, path );
701 p = buffer + strlen(buffer);
703 /* add a \, if there isn't one and path is more than just the drive letter ... */
704 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
705 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
708 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
709 sprintf( p, "%04x.tmp", num );
711 /* Now try to create it */
717 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
718 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
719 if (handle != INVALID_HANDLE_VALUE)
720 { /* We created it */
721 TRACE(file, "created %s\n",
723 CloseHandle( handle );
726 if (GetLastError() != ERROR_FILE_EXISTS)
727 break; /* No need to go on */
729 sprintf( p, "%04x.tmp", num );
730 } while (num != (unique & 0xffff));
733 /* Get the full path name */
735 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
737 /* Check if we have write access in the directory */
738 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
739 if (access( full_name.long_name, W_OK ) == -1)
740 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
743 TRACE(file, "returning %s\n", buffer );
744 return unique ? unique : num;
748 /***********************************************************************
749 * GetTempFileName32W (KERNEL32.291)
751 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
759 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
760 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
761 ret = GetTempFileNameA( patha, prefixa, unique, buffera );
762 lstrcpyAtoW( buffer, buffera );
763 HeapFree( GetProcessHeap(), 0, patha );
764 HeapFree( GetProcessHeap(), 0, prefixa );
769 /***********************************************************************
772 * Implementation of OpenFile16() and OpenFile32().
774 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
779 WORD filedatetime[2];
780 DOS_FULL_NAME full_name;
781 DWORD access, sharing;
784 if (!ofs) return HFILE_ERROR;
786 ofs->cBytes = sizeof(OFSTRUCT);
788 if (mode & OF_REOPEN) name = ofs->szPathName;
791 ERR(file, "called with `name' set to NULL ! Please debug.\n");
795 TRACE(file, "%s %04x\n", name, mode );
797 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
798 Are there any cases where getting the path here is wrong?
799 Uwe Bonnes 1997 Apr 2 */
800 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
801 ofs->szPathName, NULL )) goto error;
802 FILE_ConvertOFMode( mode, &access, &sharing );
804 /* OF_PARSE simply fills the structure */
808 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
810 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
811 name, ofs->szPathName );
815 /* OF_CREATE is completely different from all other options, so
818 if (mode & OF_CREATE)
820 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
821 sharing, NULL, CREATE_ALWAYS,
822 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
827 /* If OF_SEARCH is set, ignore the given path */
829 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
831 /* First try the file name as is */
832 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
833 /* Now remove the path */
834 if (name[0] && (name[1] == ':')) name += 2;
835 if ((p = strrchr( name, '\\' ))) name = p + 1;
836 if ((p = strrchr( name, '/' ))) name = p + 1;
837 if (!name[0]) goto not_found;
840 /* Now look for the file */
842 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
845 TRACE(file, "found %s = %s\n",
846 full_name.long_name, full_name.short_name );
847 lstrcpynA( ofs->szPathName, full_name.short_name,
848 sizeof(ofs->szPathName) );
850 if (mode & OF_SHARE_EXCLUSIVE)
851 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
852 on the file <tempdir>/_ins0432._mp to determine how
853 far installation has proceeded.
854 _ins0432._mp is an executable and while running the
855 application expects the open with OF_SHARE_ to fail*/
857 As our loader closes the files after loading the executable,
858 we can't find the running executable with FILE_InUse.
859 Perhaps the loader should keep the file open.
860 Recheck against how Win handles that case */
862 char *last = strrchr(full_name.long_name,'/');
864 last = full_name.long_name - 1;
865 if (GetModuleHandle16(last+1))
867 TRACE(file,"Denying shared open for %s\n",full_name.long_name);
872 if (mode & OF_DELETE)
874 if (unlink( full_name.long_name ) == -1) goto not_found;
875 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
879 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
880 NULL, OPEN_EXISTING, 0, -1 );
881 if (hFileRet == HFILE_ERROR) goto not_found;
883 GetFileTime( hFileRet, NULL, NULL, &filetime );
884 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
885 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
887 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
889 CloseHandle( hFileRet );
890 WARN(file, "(%s): OF_VERIFY failed\n", name );
891 /* FIXME: what error here? */
892 SetLastError( ERROR_FILE_NOT_FOUND );
896 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
898 success: /* We get here if the open was successful */
899 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
902 if (mode & OF_EXIST) /* Return the handle, but close it first */
903 CloseHandle( hFileRet );
907 hFileRet = FILE_AllocDosHandle( hFileRet );
908 if (hFileRet == HFILE_ERROR16) goto error;
909 if (mode & OF_EXIST) /* Return the handle, but close it first */
910 _lclose16( hFileRet );
914 not_found: /* We get here if the file does not exist */
915 WARN(file, "'%s' not found\n", name );
916 SetLastError( ERROR_FILE_NOT_FOUND );
919 error: /* We get here if there was an error opening the file */
920 ofs->nErrCode = GetLastError();
921 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
922 name,ofs->nErrCode );
927 /***********************************************************************
928 * OpenFile16 (KERNEL.74)
930 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
932 return FILE_DoOpenFile( name, ofs, mode, FALSE );
936 /***********************************************************************
937 * OpenFile32 (KERNEL32.396)
939 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
941 return FILE_DoOpenFile( name, ofs, mode, TRUE );
945 /***********************************************************************
946 * FILE_InitProcessDosHandles
948 * Allocates the default DOS handles for a process. Called either by
949 * AllocDosHandle below or by the DOSVM stuff.
951 BOOL FILE_InitProcessDosHandles( void ) {
954 if (!(ptr = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY,
955 sizeof(*ptr) * DOS_TABLE_SIZE )))
957 PROCESS_Current()->dos_handles = ptr;
958 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
959 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
960 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
961 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
962 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
966 /***********************************************************************
967 * FILE_AllocDosHandle
969 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
970 * longer valid after this function (even on failure).
972 HFILE16 FILE_AllocDosHandle( HANDLE handle )
975 HANDLE *ptr = PROCESS_Current()->dos_handles;
977 if (!handle || (handle == INVALID_HANDLE_VALUE))
978 return INVALID_HANDLE_VALUE16;
981 if (!FILE_InitProcessDosHandles())
983 ptr = PROCESS_Current()->dos_handles;
986 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
990 TRACE( file, "Got %d for h32 %d\n", i, handle );
994 CloseHandle( handle );
995 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
996 return INVALID_HANDLE_VALUE16;
1000 /***********************************************************************
1003 * Return the Win32 handle for a DOS handle.
1005 HANDLE FILE_GetHandle( HFILE16 hfile )
1007 HANDLE *table = PROCESS_Current()->dos_handles;
1008 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1010 SetLastError( ERROR_INVALID_HANDLE );
1011 return INVALID_HANDLE_VALUE;
1013 return table[hfile];
1017 /***********************************************************************
1020 * dup2() function for DOS handles.
1022 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1024 HANDLE *table = PROCESS_Current()->dos_handles;
1027 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1028 !table || !table[hFile1])
1030 SetLastError( ERROR_INVALID_HANDLE );
1031 return HFILE_ERROR16;
1035 FIXME( file, "stdio handle closed, need proper conversion\n" );
1036 SetLastError( ERROR_INVALID_HANDLE );
1037 return HFILE_ERROR16;
1039 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1040 GetCurrentProcess(), &new_handle,
1041 0, FALSE, DUPLICATE_SAME_ACCESS ))
1042 return HFILE_ERROR16;
1043 if (table[hFile2]) CloseHandle( table[hFile2] );
1044 table[hFile2] = new_handle;
1049 /***********************************************************************
1050 * _lclose16 (KERNEL.81)
1052 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1054 HANDLE *table = PROCESS_Current()->dos_handles;
1058 FIXME( file, "stdio handle closed, need proper conversion\n" );
1059 SetLastError( ERROR_INVALID_HANDLE );
1060 return HFILE_ERROR16;
1062 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1064 SetLastError( ERROR_INVALID_HANDLE );
1065 return HFILE_ERROR16;
1067 TRACE( file, "%d (handle32=%d)\n", hFile, table[hFile] );
1068 CloseHandle( table[hFile] );
1074 /***********************************************************************
1075 * _lclose32 (KERNEL32.592)
1077 HFILE WINAPI _lclose( HFILE hFile )
1079 TRACE(file, "handle %d\n", hFile );
1080 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1084 /***********************************************************************
1085 * ReadFile (KERNEL32.428)
1087 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1088 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1090 struct get_read_fd_request req;
1091 int unix_handle, result;
1093 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToRead );
1095 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1096 if (!bytesToRead) return TRUE;
1099 CLIENT_SendRequest( REQ_GET_READ_FD, -1, 1, &req, sizeof(req) );
1100 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1101 if (unix_handle == -1) return FALSE;
1102 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1104 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1105 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1109 close( unix_handle );
1110 if (result == -1) return FALSE;
1111 if (bytesRead) *bytesRead = result;
1116 /***********************************************************************
1117 * WriteFile (KERNEL32.578)
1119 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1120 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1122 struct get_write_fd_request req;
1123 int unix_handle, result;
1125 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToWrite );
1127 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1128 if (!bytesToWrite) return TRUE;
1131 CLIENT_SendRequest( REQ_GET_WRITE_FD, -1, 1, &req, sizeof(req) );
1132 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1133 if (unix_handle == -1) return FALSE;
1134 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1136 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1137 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1141 close( unix_handle );
1142 if (result == -1) return FALSE;
1143 if (bytesWritten) *bytesWritten = result;
1148 /***********************************************************************
1151 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1155 TRACE(file, "%d %08lx %ld\n",
1156 hFile, (DWORD)buffer, count );
1158 /* Some programs pass a count larger than the allocated buffer */
1159 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1160 if (count > maxlen) count = maxlen;
1161 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1165 /***********************************************************************
1168 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1170 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1174 /***********************************************************************
1175 * _lread32 (KERNEL32.596)
1177 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1180 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1185 /***********************************************************************
1186 * _lread16 (KERNEL.82)
1188 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1190 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1194 /***********************************************************************
1195 * _lcreat16 (KERNEL.83)
1197 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1199 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1203 /***********************************************************************
1204 * _lcreat (KERNEL32.593)
1206 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1208 /* Mask off all flags not explicitly allowed by the doc */
1209 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1210 TRACE(file, "%s %02x\n", path, attr );
1211 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1212 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1213 CREATE_ALWAYS, attr, -1 );
1217 /***********************************************************************
1218 * SetFilePointer (KERNEL32.492)
1220 DWORD WINAPI SetFilePointer( HFILE hFile, LONG distance, LONG *highword,
1223 struct set_file_pointer_request req;
1224 struct set_file_pointer_reply reply;
1226 if (highword && *highword)
1228 FIXME(file, "64-bit offsets not supported yet\n");
1229 SetLastError( ERROR_INVALID_PARAMETER );
1232 TRACE(file, "handle %d offset %ld origin %ld\n",
1233 hFile, distance, method );
1237 req.high = highword ? *highword : 0;
1238 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1239 req.whence = method;
1240 CLIENT_SendRequest( REQ_SET_FILE_POINTER, -1, 1, &req, sizeof(req) );
1241 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
1243 if (highword) *highword = reply.high;
1248 /***********************************************************************
1249 * _llseek16 (KERNEL.84)
1252 * Seeking before the start of the file should be allowed for _llseek16,
1253 * but cause subsequent I/O operations to fail (cf. interrupt list)
1256 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1258 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1262 /***********************************************************************
1263 * _llseek32 (KERNEL32.594)
1265 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1267 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1271 /***********************************************************************
1272 * _lopen16 (KERNEL.85)
1274 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1276 return FILE_AllocDosHandle( _lopen( path, mode ) );
1280 /***********************************************************************
1281 * _lopen32 (KERNEL32.595)
1283 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1285 DWORD access, sharing;
1287 TRACE(file, "('%s',%04x)\n", path, mode );
1288 FILE_ConvertOFMode( mode, &access, &sharing );
1289 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1293 /***********************************************************************
1294 * _lwrite16 (KERNEL.86)
1296 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1298 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1301 /***********************************************************************
1302 * _lwrite32 (KERNEL32.761)
1304 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1306 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1310 /***********************************************************************
1311 * _hread16 (KERNEL.349)
1313 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1315 return _lread( FILE_GetHandle(hFile), buffer, count );
1319 /***********************************************************************
1320 * _hread32 (KERNEL32.590)
1322 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1324 return _lread( hFile, buffer, count );
1328 /***********************************************************************
1329 * _hwrite16 (KERNEL.350)
1331 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1333 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1337 /***********************************************************************
1338 * _hwrite32 (KERNEL32.591)
1340 * experimentation yields that _lwrite:
1341 * o truncates the file at the current position with
1343 * o returns 0 on a 0 length write
1344 * o works with console handles
1347 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1351 TRACE(file, "%d %p %ld\n", handle, buffer, count );
1355 /* Expand or truncate at current position */
1356 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1359 if (!WriteFile( handle, buffer, count, &result, NULL ))
1365 /***********************************************************************
1366 * SetHandleCount16 (KERNEL.199)
1368 UINT16 WINAPI SetHandleCount16( UINT16 count )
1370 HGLOBAL16 hPDB = GetCurrentPDB16();
1371 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1372 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1374 TRACE(file, "(%d)\n", count );
1376 if (count < 20) count = 20; /* No point in going below 20 */
1377 else if (count > 254) count = 254;
1381 if (pdb->nbFiles > 20)
1383 memcpy( pdb->fileHandles, files, 20 );
1384 GlobalFree16( pdb->hFileHandles );
1385 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1386 GlobalHandleToSel16( hPDB ) );
1387 pdb->hFileHandles = 0;
1391 else /* More than 20, need a new file handles table */
1394 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1397 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1398 return pdb->nbFiles;
1400 newfiles = (BYTE *)GlobalLock16( newhandle );
1402 if (count > pdb->nbFiles)
1404 memcpy( newfiles, files, pdb->nbFiles );
1405 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1407 else memcpy( newfiles, files, count );
1408 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1409 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1410 pdb->hFileHandles = newhandle;
1411 pdb->nbFiles = count;
1413 return pdb->nbFiles;
1417 /*************************************************************************
1418 * SetHandleCount32 (KERNEL32.494)
1420 UINT WINAPI SetHandleCount( UINT count )
1422 return MIN( 256, count );
1426 /***********************************************************************
1427 * FlushFileBuffers (KERNEL32.133)
1429 BOOL WINAPI FlushFileBuffers( HFILE hFile )
1431 struct flush_file_request req;
1434 CLIENT_SendRequest( REQ_FLUSH_FILE, -1, 1, &req, sizeof(req) );
1435 return !CLIENT_WaitReply( NULL, NULL, 0 );
1439 /**************************************************************************
1440 * SetEndOfFile (KERNEL32.483)
1442 BOOL WINAPI SetEndOfFile( HFILE hFile )
1444 struct truncate_file_request req;
1447 CLIENT_SendRequest( REQ_TRUNCATE_FILE, -1, 1, &req, sizeof(req) );
1448 return !CLIENT_WaitReply( NULL, NULL, 0 );
1452 /***********************************************************************
1453 * DeleteFile16 (KERNEL.146)
1455 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1457 return DeleteFileA( path );
1461 /***********************************************************************
1462 * DeleteFile32A (KERNEL32.71)
1464 BOOL WINAPI DeleteFileA( LPCSTR path )
1466 DOS_FULL_NAME full_name;
1468 TRACE(file, "'%s'\n", path );
1472 ERR(file, "Empty path passed\n");
1475 if (DOSFS_GetDevice( path ))
1477 WARN(file, "cannot remove DOS device '%s'!\n", path);
1478 SetLastError( ERROR_FILE_NOT_FOUND );
1482 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1483 if (unlink( full_name.long_name ) == -1)
1492 /***********************************************************************
1493 * DeleteFile32W (KERNEL32.72)
1495 BOOL WINAPI DeleteFileW( LPCWSTR path )
1497 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1498 BOOL ret = DeleteFileA( xpath );
1499 HeapFree( GetProcessHeap(), 0, xpath );
1504 /***********************************************************************
1507 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1508 DWORD size_high, DWORD size_low,
1509 DWORD offset_high, DWORD offset_low,
1510 int prot, int flags )
1516 if (size_high || offset_high)
1517 FIXME(file, "offsets larger than 4Gb not supported\n");
1519 if (unix_handle == -1)
1524 static int fdzero = -1;
1528 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1530 perror( "/dev/zero: open" );
1535 #endif /* MAP_ANON */
1536 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1538 flags &= ~MAP_SHARED;
1541 flags |= MAP_PRIVATE;
1544 else fd = unix_handle;
1546 if ((ret = mmap( start, size_low, prot,
1547 flags, fd, offset_low )) != (LPVOID)-1)
1550 /* mmap() failed; if this is because the file offset is not */
1551 /* page-aligned (EINVAL), or because the underlying filesystem */
1552 /* does not support mmap() (ENOEXEC), we do it by hand. */
1554 if (unix_handle == -1) return ret;
1555 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1556 if (prot & PROT_WRITE)
1558 /* We cannot fake shared write mappings */
1560 if (flags & MAP_SHARED) return ret;
1563 if (!(flags & MAP_PRIVATE)) return ret;
1566 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1567 /* Reserve the memory with an anonymous mmap */
1568 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1569 PROT_READ | PROT_WRITE, flags );
1570 if (ret == (LPVOID)-1) return ret;
1571 /* Now read in the file */
1572 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1574 FILE_munmap( ret, size_high, size_low );
1577 read( fd, ret, size_low );
1578 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1579 mprotect( ret, size_low, prot ); /* Set the right protection */
1584 /***********************************************************************
1587 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1590 FIXME(file, "offsets larger than 4Gb not supported\n");
1591 return munmap( start, size_low );
1595 /***********************************************************************
1596 * GetFileType (KERNEL32.222)
1598 DWORD WINAPI GetFileType( HFILE hFile )
1600 struct get_file_info_request req;
1601 struct get_file_info_reply reply;
1604 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
1605 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
1606 return FILE_TYPE_UNKNOWN;
1611 /**************************************************************************
1612 * MoveFileEx32A (KERNEL32.???)
1614 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1616 DOS_FULL_NAME full_name1, full_name2;
1617 int mode=0; /* mode == 1: use copy */
1619 TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag);
1621 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1622 if (fn2) { /* !fn2 means delete fn1 */
1623 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1624 /* Source name and target path are valid */
1625 if ( full_name1.drive != full_name2.drive)
1627 /* use copy, if allowed */
1628 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1629 /* FIXME: Use right error code */
1630 SetLastError( ERROR_FILE_EXISTS );
1635 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1636 /* target exists, check if we may overwrite */
1637 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1638 /* FIXME: Use right error code */
1639 SetLastError( ERROR_ACCESS_DENIED );
1643 else /* fn2 == NULL means delete source */
1644 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1645 if (flag & MOVEFILE_COPY_ALLOWED) {
1646 WARN(file, "Illegal flag\n");
1647 SetLastError( ERROR_GEN_FAILURE );
1650 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1651 Perhaps we should queue these command and execute it
1652 when exiting... What about using on_exit(2)
1654 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1655 full_name1.long_name);
1658 else if (unlink( full_name1.long_name ) == -1)
1663 else return TRUE; /* successfully deleted */
1665 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(file,"Please move existing file '%s' to file '%s'"
1671 "when Wine has finished\n",
1672 full_name1.long_name, full_name2.long_name);
1676 if (!mode) /* move the file */
1677 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1683 else /* copy File */
1684 return CopyFileA(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1688 /**************************************************************************
1689 * MoveFileEx32W (KERNEL32.???)
1691 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1693 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1694 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1695 BOOL res = MoveFileExA( afn1, afn2, flag );
1696 HeapFree( GetProcessHeap(), 0, afn1 );
1697 HeapFree( GetProcessHeap(), 0, afn2 );
1702 /**************************************************************************
1703 * MoveFile32A (KERNEL32.387)
1705 * Move file or directory
1707 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1709 DOS_FULL_NAME full_name1, full_name2;
1712 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1714 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1715 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1716 /* The new name must not already exist */
1718 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1720 if (full_name1.drive == full_name2.drive) /* move */
1721 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1728 if (stat( full_name1.long_name, &fstat ))
1730 WARN(file, "Invalid source file %s\n",
1731 full_name1.long_name);
1735 if (S_ISDIR(fstat.st_mode)) {
1736 /* No Move for directories across file systems */
1737 /* FIXME: Use right error code */
1738 SetLastError( ERROR_GEN_FAILURE );
1742 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1747 /**************************************************************************
1748 * MoveFile32W (KERNEL32.390)
1750 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1752 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1753 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1754 BOOL res = MoveFileA( afn1, afn2 );
1755 HeapFree( GetProcessHeap(), 0, afn1 );
1756 HeapFree( GetProcessHeap(), 0, afn2 );
1761 /**************************************************************************
1762 * CopyFile32A (KERNEL32.36)
1764 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1767 BY_HANDLE_FILE_INFORMATION info;
1773 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1774 if (!GetFileInformationByHandle( h1, &info ))
1779 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1780 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1781 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1782 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1787 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1792 INT res = _lwrite( h2, p, count );
1793 if (res <= 0) goto done;
1806 /**************************************************************************
1807 * CopyFile32W (KERNEL32.37)
1809 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1811 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1812 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1813 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1814 HeapFree( GetProcessHeap(), 0, sourceA );
1815 HeapFree( GetProcessHeap(), 0, destA );
1820 /**************************************************************************
1821 * CopyFileEx32A (KERNEL32.858)
1823 * This implementation ignores most of the extra parameters passed-in into
1824 * the "ex" version of the method and calls the CopyFile method.
1825 * It will have to be fixed eventually.
1827 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1828 LPCSTR destFilename,
1829 LPPROGRESS_ROUTINE progressRoutine,
1831 LPBOOL cancelFlagPointer,
1834 BOOL failIfExists = FALSE;
1837 * Interpret the only flag that CopyFile can interpret.
1839 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1841 failIfExists = TRUE;
1844 return CopyFileA(sourceFilename, destFilename, failIfExists);
1847 /**************************************************************************
1848 * CopyFileEx32W (KERNEL32.859)
1850 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1851 LPCWSTR destFilename,
1852 LPPROGRESS_ROUTINE progressRoutine,
1854 LPBOOL cancelFlagPointer,
1857 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1858 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1860 BOOL ret = CopyFileExA(sourceA,
1867 HeapFree( GetProcessHeap(), 0, sourceA );
1868 HeapFree( GetProcessHeap(), 0, destA );
1874 /***********************************************************************
1875 * SetFileTime (KERNEL32.650)
1877 BOOL WINAPI SetFileTime( HFILE hFile,
1878 const FILETIME *lpCreationTime,
1879 const FILETIME *lpLastAccessTime,
1880 const FILETIME *lpLastWriteTime )
1882 struct set_file_time_request req;
1885 if (lpLastAccessTime)
1886 req.access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1888 req.access_time = 0; /* FIXME */
1889 if (lpLastWriteTime)
1890 req.write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1892 req.write_time = 0; /* FIXME */
1894 CLIENT_SendRequest( REQ_SET_FILE_TIME, -1, 1, &req, sizeof(req) );
1895 return !CLIENT_WaitReply( NULL, NULL, 0 );
1899 /**************************************************************************
1900 * LockFile (KERNEL32.511)
1902 BOOL WINAPI LockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1903 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1905 struct lock_file_request req;
1908 req.offset_low = dwFileOffsetLow;
1909 req.offset_high = dwFileOffsetHigh;
1910 req.count_low = nNumberOfBytesToLockLow;
1911 req.count_high = nNumberOfBytesToLockHigh;
1912 CLIENT_SendRequest( REQ_LOCK_FILE, -1, 1, &req, sizeof(req) );
1913 return !CLIENT_WaitReply( NULL, NULL, 0 );
1917 /**************************************************************************
1918 * UnlockFile (KERNEL32.703)
1920 BOOL WINAPI UnlockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1921 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1923 struct unlock_file_request req;
1926 req.offset_low = dwFileOffsetLow;
1927 req.offset_high = dwFileOffsetHigh;
1928 req.count_low = nNumberOfBytesToUnlockLow;
1929 req.count_high = nNumberOfBytesToUnlockHigh;
1930 CLIENT_SendRequest( REQ_UNLOCK_FILE, -1, 1, &req, sizeof(req) );
1931 return !CLIENT_WaitReply( NULL, NULL, 0 );
1937 struct DOS_FILE_LOCK {
1938 struct DOS_FILE_LOCK * next;
1942 FILE_OBJECT * dos_file;
1943 /* char * unix_name;*/
1946 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
1948 static DOS_FILE_LOCK *locks = NULL;
1949 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
1952 /* Locks need to be mirrored because unix file locking is based
1953 * on the pid. Inside of wine there can be multiple WINE processes
1954 * that share the same unix pid.
1955 * Read's and writes should check these locks also - not sure
1956 * how critical that is at this point (FIXME).
1959 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
1961 DOS_FILE_LOCK *curr;
1964 processId = GetCurrentProcessId();
1966 /* check if lock overlaps a current lock for the same file */
1968 for (curr = locks; curr; curr = curr->next) {
1969 if (strcmp(curr->unix_name, file->unix_name) == 0) {
1970 if ((f->l_start == curr->base) && (f->l_len == curr->len))
1971 return TRUE;/* region is identic */
1972 if ((f->l_start < (curr->base + curr->len)) &&
1973 ((f->l_start + f->l_len) > curr->base)) {
1974 /* region overlaps */
1981 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
1982 curr->processId = GetCurrentProcessId();
1983 curr->base = f->l_start;
1984 curr->len = f->l_len;
1985 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
1987 curr->dos_file = file;
1992 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
1995 DOS_FILE_LOCK **curr;
1998 processId = GetCurrentProcessId();
2001 if ((*curr)->dos_file == file) {
2003 *curr = (*curr)->next;
2004 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2005 HeapFree( SystemHeap, 0, rem );
2008 curr = &(*curr)->next;
2012 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2015 DOS_FILE_LOCK **curr;
2018 processId = GetCurrentProcessId();
2019 for (curr = &locks; *curr; curr = &(*curr)->next) {
2020 if ((*curr)->processId == processId &&
2021 (*curr)->dos_file == file &&
2022 (*curr)->base == f->l_start &&
2023 (*curr)->len == f->l_len) {
2024 /* this is the same lock */
2026 *curr = (*curr)->next;
2027 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2028 HeapFree( SystemHeap, 0, rem );
2032 /* no matching lock found */
2037 /**************************************************************************
2038 * LockFile (KERNEL32.511)
2040 BOOL WINAPI LockFile(
2041 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2042 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2047 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2048 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2049 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2051 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2052 FIXME(file, "Unimplemented bytes > 32bits\n");
2056 f.l_start = dwFileOffsetLow;
2057 f.l_len = nNumberOfBytesToLockLow;
2058 f.l_whence = SEEK_SET;
2062 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2064 /* shadow locks internally */
2065 if (!DOS_AddLock(file, &f)) {
2066 SetLastError( ERROR_LOCK_VIOLATION );
2070 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2071 #ifdef USE_UNIX_LOCKS
2072 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2073 if (errno == EACCES || errno == EAGAIN) {
2074 SetLastError( ERROR_LOCK_VIOLATION );
2079 /* remove our internal copy of the lock */
2080 DOS_RemoveLock(file, &f);
2088 /**************************************************************************
2089 * UnlockFile (KERNEL32.703)
2091 BOOL WINAPI UnlockFile(
2092 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2093 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2098 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2099 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2100 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2102 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2103 WARN(file, "Unimplemented bytes > 32bits\n");
2107 f.l_start = dwFileOffsetLow;
2108 f.l_len = nNumberOfBytesToUnlockLow;
2109 f.l_whence = SEEK_SET;
2113 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2115 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2117 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2118 #ifdef USE_UNIX_LOCKS
2119 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2128 /**************************************************************************
2129 * GetFileAttributesEx32A [KERNEL32.874]
2131 BOOL WINAPI GetFileAttributesExA(
2132 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2133 LPVOID lpFileInformation)
2135 DOS_FULL_NAME full_name;
2136 BY_HANDLE_FILE_INFORMATION info;
2138 if (lpFileName == NULL) return FALSE;
2139 if (lpFileInformation == NULL) return FALSE;
2141 if (fInfoLevelId == GetFileExInfoStandard) {
2142 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2143 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2144 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2145 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2147 lpFad->dwFileAttributes = info.dwFileAttributes;
2148 lpFad->ftCreationTime = info.ftCreationTime;
2149 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2150 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2151 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2152 lpFad->nFileSizeLow = info.nFileSizeLow;
2155 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
2163 /**************************************************************************
2164 * GetFileAttributesEx32W [KERNEL32.875]
2166 BOOL WINAPI GetFileAttributesExW(
2167 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2168 LPVOID lpFileInformation)
2170 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2172 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2173 HeapFree( GetProcessHeap(), 0, nameA );