More debug output for InternetGetCookie, added stub for InternetSetCookie.
[wine.git] / files / file.c
bloba9cedb320c2674d000a12674970f8d378507793a
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
7 * TODO:
8 * Fix the CopyFileEx methods to implement the "extended" functionality.
9 * Right now, they simply call the CopyFile method.
12 #include "config.h"
13 #include "wine/port.h"
15 #include <assert.h>
16 #include <ctype.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #ifdef HAVE_SYS_ERRNO_H
23 #include <sys/errno.h>
24 #endif
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #ifdef HAVE_SYS_MMAN_H
28 #include <sys/mman.h>
29 #endif
30 #include <sys/time.h>
31 #include <sys/poll.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <utime.h>
36 #include "winerror.h"
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wine/winbase16.h"
40 #include "drive.h"
41 #include "file.h"
42 #include "heap.h"
43 #include "msdos.h"
44 #include "wincon.h"
45 #include "debugtools.h"
47 #include "wine/server.h"
49 DEFAULT_DEBUG_CHANNEL(file);
51 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
52 #define MAP_ANON MAP_ANONYMOUS
53 #endif
55 /* Size of per-process table of DOS handles */
56 #define DOS_TABLE_SIZE 256
58 static HANDLE dos_handles[DOS_TABLE_SIZE];
61 /***********************************************************************
62 * FILE_ConvertOFMode
64 * Convert OF_* mode into flags for CreateFile.
66 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
68 switch(mode & 0x03)
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;
75 switch(mode & 0x70)
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:
81 case OF_SHARE_COMPAT:
82 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
87 /***********************************************************************
88 * FILE_strcasecmp
90 * locale-independent case conversion for file I/O
92 int FILE_strcasecmp( const char *str1, const char *str2 )
94 for (;;)
96 int ret = FILE_toupper(*str1) - FILE_toupper(*str2);
97 if (ret || !*str1) return ret;
98 str1++;
99 str2++;
104 /***********************************************************************
105 * FILE_strncasecmp
107 * locale-independent case conversion for file I/O
109 int FILE_strncasecmp( const char *str1, const char *str2, int len )
111 int ret = 0;
112 for ( ; len > 0; len--, str1++, str2++)
113 if ((ret = FILE_toupper(*str1) - FILE_toupper(*str2)) || !*str1) break;
114 return ret;
118 /***********************************************************************
119 * FILE_SetDosError
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));
128 switch (save_errno)
130 case EAGAIN:
131 SetLastError( ERROR_SHARING_VIOLATION );
132 break;
133 case EBADF:
134 SetLastError( ERROR_INVALID_HANDLE );
135 break;
136 case ENOSPC:
137 SetLastError( ERROR_HANDLE_DISK_FULL );
138 break;
139 case EACCES:
140 case EPERM:
141 case EROFS:
142 SetLastError( ERROR_ACCESS_DENIED );
143 break;
144 case EBUSY:
145 SetLastError( ERROR_LOCK_VIOLATION );
146 break;
147 case ENOENT:
148 SetLastError( ERROR_FILE_NOT_FOUND );
149 break;
150 case EISDIR:
151 SetLastError( ERROR_CANNOT_MAKE );
152 break;
153 case ENFILE:
154 case EMFILE:
155 SetLastError( ERROR_NO_MORE_FILES );
156 break;
157 case EEXIST:
158 SetLastError( ERROR_FILE_EXISTS );
159 break;
160 case EINVAL:
161 case ESPIPE:
162 SetLastError( ERROR_SEEK );
163 break;
164 case ENOTEMPTY:
165 SetLastError( ERROR_DIR_NOT_EMPTY );
166 break;
167 case ENOEXEC:
168 SetLastError( ERROR_BAD_FORMAT );
169 break;
170 default:
171 WARN("unknown file error: %s\n", strerror(save_errno) );
172 SetLastError( ERROR_GEN_FAILURE );
173 break;
175 errno = save_errno;
179 /***********************************************************************
180 * FILE_DupUnixHandle
182 * Duplicate a Unix handle into a task handle.
183 * Returns 0 on failure.
185 HANDLE FILE_DupUnixHandle( int fd, DWORD access )
187 HANDLE ret;
189 wine_server_send_fd( fd );
191 SERVER_START_REQ( alloc_file_handle )
193 req->access = access;
194 req->fd = fd;
195 SERVER_CALL();
196 ret = req->handle;
198 SERVER_END_REQ;
199 return ret;
203 /***********************************************************************
204 * FILE_GetUnixHandle
206 * Retrieve the Unix handle corresponding to a file handle.
207 * Returns -1 on failure.
209 int FILE_GetUnixHandle( HANDLE handle, DWORD access )
211 int ret, fd = -1;
215 SERVER_START_REQ( get_handle_fd )
217 req->handle = handle;
218 req->access = access;
219 if (!(ret = SERVER_CALL_ERR())) fd = req->fd;
221 SERVER_END_REQ;
222 if (ret) return -1;
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 */
229 if (fd != -1)
231 if ((fd = dup(fd)) == -1)
232 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
234 return fd;
238 /*************************************************************************
239 * FILE_OpenConsole
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 )
246 HANDLE ret;
248 SERVER_START_REQ( open_console )
250 req->output = output;
251 req->access = access;
252 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
253 SetLastError(0);
254 SERVER_CALL_ERR();
255 ret = req->handle;
257 SERVER_END_REQ;
258 return ret;
262 /***********************************************************************
263 * FILE_CreateFile
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 )
272 DWORD err;
273 HANDLE ret;
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 );
280 return 0;
283 restart:
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 );
292 SetLastError(0);
293 err = SERVER_CALL();
294 ret = req->handle;
296 SERVER_END_VAR_REQ;
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;
307 goto restart;
311 if (err) SetLastError( RtlNtStatusToDosError(err) );
313 if (!ret)
314 WARN("Unable to create file '%s' (GLE %ld)\n", filename, GetLastError());
316 return ret;
320 /***********************************************************************
321 * FILE_CreateDevice
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 )
328 HANDLE ret;
329 SERVER_START_REQ( create_device )
331 req->access = access;
332 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
333 req->id = client_id;
334 SetLastError(0);
335 SERVER_CALL_ERR();
336 ret = req->handle;
338 SERVER_END_REQ;
339 return ret;
342 static HANDLE FILE_OpenPipe(LPCSTR name, DWORD access)
344 HANDLE ret;
345 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
347 TRACE("name %s access %lx\n",name,access);
349 if (len >= MAX_PATH)
351 SetLastError( ERROR_FILENAME_EXCED_RANGE );
352 return 0;
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 );
359 SetLastError(0);
360 SERVER_CALL_ERR();
361 ret = req->handle;
363 SERVER_END_VAR_REQ;
364 TRACE("Returned %d\n",ret);
365 return 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.
374 * PARAMS
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
384 * RETURNS
385 * Success: Open handle to specified file
386 * Failure: INVALID_HANDLE_VALUE
388 * NOTES
389 * Should call SetLastError() on failure.
391 * BUGS
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;
401 HANDLE ret;
403 if (!filename)
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))
424 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 );
442 goto done;
444 else
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 );
464 goto done;
466 if (!strcasecmp(filename, "CONOUT$"))
468 ret = FILE_OpenConsole( TRUE, access, sa );
469 goto done;
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 );
482 goto done;
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),
489 &full_name )) {
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 );
498 done:
499 if (!ret) ret = INVALID_HANDLE_VALUE;
500 return ret;
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 );
515 return res;
519 /***********************************************************************
520 * FILE_FillInfo
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;
528 else
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 /***********************************************************************
551 * FILE_Stat
553 * Stat a Unix path name. Return TRUE if OK.
555 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
557 struct stat st;
559 if (lstat( unixName, &st ) == -1)
561 FILE_SetDosError();
562 return FALSE;
564 if (!S_ISLNK(st.st_mode)) FILE_FillInfo( &st, info );
565 else
567 /* do a "real" stat to find out
568 about the type of the symlink destination */
569 if (stat( unixName, &st ) == -1)
571 FILE_SetDosError();
572 return FALSE;
574 FILE_FillInfo( &st, info );
575 info->dwFileAttributes |= FILE_ATTRIBUTE_SYMLINK;
577 return TRUE;
581 /***********************************************************************
582 * GetFileInformationByHandle (KERNEL32.@)
584 DWORD WINAPI GetFileInformationByHandle( HANDLE hFile,
585 BY_HANDLE_FILE_INFORMATION *info )
587 DWORD ret;
588 if (!info) return 0;
590 SERVER_START_REQ( get_file_info )
592 req->handle = hFile;
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;
614 else
616 SetLastError(ERROR_NOT_SUPPORTED);
617 ret = 0;
621 SERVER_END_REQ;
622 return ret;
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;
643 if (name == NULL)
645 SetLastError( ERROR_INVALID_PARAMETER );
646 return -1;
648 if (!DOSFS_GetFullName( name, TRUE, &full_name) )
649 return -1;
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 );
663 return res;
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;
691 return TRUE;
694 /***********************************************************************
695 * CompareFileTime (KERNEL32.@)
697 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
699 if (!x || !y) return -1;
701 if (x->dwHighDateTime > y->dwHighDateTime)
702 return 1;
703 if (x->dwHighDateTime < y->dwHighDateTime)
704 return -1;
705 if (x->dwLowDateTime > y->dwLowDateTime)
706 return 1;
707 if (x->dwLowDateTime < y->dwLowDateTime)
708 return -1;
709 return 0;
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;
720 int i;
721 LPSTR p;
722 UINT num;
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 */
742 if (!unique)
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",
751 buffer);
752 CloseHandle( handle );
753 break;
755 if (GetLastError() != ERROR_FILE_EXISTS)
756 break; /* No need to go on */
757 num++;
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",
770 buffer);
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,
781 LPSTR buffer)
783 return FILE_GetTempFileName(path, prefix, unique, buffer, FALSE);
786 /***********************************************************************
787 * GetTempFileNameW (KERNEL32.@)
789 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
790 LPWSTR buffer )
792 LPSTR patha,prefixa;
793 char buffera[144];
794 UINT ret;
796 if (!path) return 0;
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 );
803 return ret;
807 /***********************************************************************
808 * GetTempFileName (KERNEL.97)
810 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
811 LPSTR buffer )
813 char temppath[144];
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 );
827 else
828 GetTempPathA( 132, temppath );
829 return (UINT16)FILE_GetTempFileName( temppath, prefix, unique, buffer, TRUE );
832 /***********************************************************************
833 * FILE_DoOpenFile
835 * Implementation of OpenFile16() and OpenFile32().
837 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
838 BOOL win32 )
840 HFILE hFileRet;
841 FILETIME filetime;
842 WORD filedatetime[2];
843 DOS_FULL_NAME full_name;
844 DWORD access, sharing;
845 char *p;
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);
871 ofs->nErrCode = 0;
872 if (mode & OF_REOPEN) name = ofs->szPathName;
874 if (!name) {
875 ERR("called with `name' set to NULL ! Please debug.\n");
876 return HFILE_ERROR;
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 */
890 if (mode & OF_PARSE)
892 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
893 != DRIVE_REMOVABLE);
894 TRACE("(%s): OF_PARSE, res = '%s'\n",
895 name, ofs->szPathName );
896 return 0;
899 /* OF_CREATE is completely different from all other options, so
900 handle it first */
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)
907 goto error;
908 goto success;
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;
928 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*/
940 /* Probable FIXME:
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,'/');
947 if (!last)
948 last = full_name.long_name - 1;
949 if (GetModuleHandle16(last+1))
951 TRACE("Denying shared open for %s\n",full_name.long_name);
952 return HFILE_ERROR;
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);
960 return 1;
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 );
978 goto error;
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 );
985 if (win32)
987 if (mode & OF_EXIST) /* Return the handle, but close it first */
988 CloseHandle( hFileRet );
990 else
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 );
997 return 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 );
1002 /* fall through */
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 );
1008 return HFILE_ERROR;
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 )
1058 int i;
1060 if (!handle || (handle == INVALID_HANDLE_VALUE))
1061 return HFILE_ERROR;
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 );
1068 return (HFILE)i;
1070 CloseHandle( handle );
1071 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1072 return HFILE_ERROR;
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 )
1108 int i;
1110 if (!handle || (handle == INVALID_HANDLE_VALUE)) return;
1112 for (i = 5; i < DOS_TABLE_SIZE; i++)
1113 if (dos_handles[i] == handle)
1115 dos_handles[i] = 0;
1116 CloseHandle( handle );
1117 break;
1122 /***********************************************************************
1123 * FILE_Dup2
1125 * dup2() function for DOS handles.
1127 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1129 HANDLE new_handle;
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;
1138 if (hFile2 < 5)
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;
1150 return hFile2;
1154 /***********************************************************************
1155 * _lclose (KERNEL.81)
1157 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1159 if (hFile < 5)
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;
1173 return 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.
1191 * RETURNS
1192 * TRUE on success
1193 * FALSE on failure
1195 * If successful (and relevant) lpTransfered will hold the number of
1196 * bytes transfered during the async operation.
1198 * BUGS
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 ? */
1210 DWORD r;
1212 TRACE("(%d %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait);
1214 if(lpOverlapped==NULL)
1216 ERR("lpOverlapped was null\n");
1217 return FALSE;
1219 if(!lpOverlapped->hEvent)
1221 ERR("lpOverlapped->hEvent was null\n");
1222 return FALSE;
1225 do {
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);
1231 if(lpTransferred)
1232 *lpTransferred = lpOverlapped->InternalHigh;
1234 SetLastError(lpOverlapped->Internal);
1236 return (r==WAIT_OBJECT_0);
1240 /***********************************************************************
1241 * CancelIo (KERNEL32.@)
1243 BOOL WINAPI CancelIo(HANDLE handle)
1245 FIXME("(%d) stub\n",handle);
1246 return FALSE;
1249 /***********************************************************************
1250 * FILE_AsyncReadService (INTERNAL)
1252 * This function is called while the client is waiting on the
1253 * server, so we can't make any server calls here.
1255 static void FILE_AsyncReadService(async_private *ovp, int events)
1257 LPOVERLAPPED lpOverlapped = ovp->lpOverlapped;
1258 int result, r;
1260 TRACE("%p %p %08x\n", lpOverlapped, ovp->buffer, events );
1262 /* if POLLNVAL, then our fd was closed or we have the wrong fd */
1263 if(events&POLLNVAL)
1265 ERR("fd %d invalid for %p\n",ovp->fd,ovp);
1266 r = STATUS_UNSUCCESSFUL;
1267 goto async_end;
1270 /* if there are no events, it must be a timeout */
1271 if(events==0)
1273 TRACE("read timed out\n");
1274 r = STATUS_TIMEOUT;
1275 goto async_end;
1278 /* check to see if the data is ready (non-blocking) */
1279 result = read(ovp->fd, &ovp->buffer[lpOverlapped->InternalHigh],
1280 ovp->count - lpOverlapped->InternalHigh);
1282 if ( (result<0) && ((errno == EAGAIN) || (errno == EINTR)))
1284 TRACE("Deferred read %d\n",errno);
1285 r = STATUS_PENDING;
1286 goto async_end;
1289 /* check to see if the transfer is complete */
1290 if(result<0)
1292 TRACE("read returned errno %d\n",errno);
1293 r = STATUS_UNSUCCESSFUL;
1294 goto async_end;
1297 lpOverlapped->InternalHigh += result;
1298 TRACE("read %d more bytes %ld/%d so far\n",result,lpOverlapped->InternalHigh,ovp->count);
1300 if(lpOverlapped->InternalHigh < ovp->count)
1301 r = STATUS_PENDING;
1302 else
1303 r = STATUS_SUCCESS;
1305 async_end:
1306 lpOverlapped->Internal = r;
1309 /* flogged from wineserver */
1310 /* add a timeout in milliseconds to an absolute time */
1311 static void add_timeout( struct timeval *when, int timeout )
1313 if (timeout)
1315 long sec = timeout / 1000;
1316 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
1318 when->tv_usec -= 1000000;
1319 when->tv_sec++;
1321 when->tv_sec += sec;
1325 /***********************************************************************
1326 * FILE_ReadFileEx (INTERNAL)
1328 static BOOL FILE_ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1329 LPOVERLAPPED overlapped,
1330 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1332 async_private *ovp;
1333 int fd, timeout, ret;
1335 TRACE("file %d to buf %p num %ld %p func %p\n",
1336 hFile, buffer, bytesToRead, overlapped, lpCompletionRoutine);
1338 /* check that there is an overlapped struct with an event flag */
1339 if ( (overlapped==NULL) || NtResetEvent( overlapped->hEvent, NULL ) )
1341 TRACE("Overlapped not specified or invalid event flag\n");
1342 SetLastError(ERROR_INVALID_PARAMETER);
1343 return FALSE;
1347 * Although the overlapped transfer will be done in this thread
1348 * we still need to register the operation with the server, in
1349 * case it is cancelled and to get a file handle and the timeout info.
1351 SERVER_START_REQ(create_async)
1353 req->count = bytesToRead;
1354 req->type = ASYNC_TYPE_READ;
1355 req->file_handle = hFile;
1356 ret = SERVER_CALL();
1357 timeout = req->timeout;
1359 SERVER_END_REQ;
1360 if (ret)
1362 TRACE("server call failed\n");
1363 return FALSE;
1366 fd = FILE_GetUnixHandle( hFile, GENERIC_WRITE );
1367 if(fd<0)
1369 TRACE("Couldn't get FD\n");
1370 return FALSE;
1373 ovp = (async_private *) HeapAlloc(GetProcessHeap(), 0, sizeof (async_private));
1374 if(!ovp)
1376 TRACE("HeapAlloc Failed\n");
1377 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1378 close(fd);
1379 return FALSE;
1381 ovp->lpOverlapped = overlapped;
1382 ovp->count = bytesToRead;
1383 ovp->completion_func = lpCompletionRoutine;
1384 ovp->timeout = timeout;
1385 gettimeofday(&ovp->tv,NULL);
1386 add_timeout(&ovp->tv,timeout);
1387 ovp->event = POLLIN;
1388 ovp->func = FILE_AsyncReadService;
1389 ovp->buffer = buffer;
1390 ovp->fd = fd;
1392 /* hook this overlap into the pending async operation list */
1393 ovp->next = NtCurrentTeb()->pending_list;
1394 ovp->prev = NULL;
1395 if(ovp->next)
1396 ovp->next->prev = ovp;
1397 NtCurrentTeb()->pending_list = ovp;
1399 return TRUE;
1402 /***********************************************************************
1403 * ReadFileEx (KERNEL32.@)
1405 BOOL WINAPI ReadFileEx(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1406 LPOVERLAPPED overlapped,
1407 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1409 /* FIXME: MS docs say we shouldn't set overlapped->hEvent */
1410 overlapped->Internal = STATUS_PENDING;
1411 overlapped->InternalHigh = 0;
1412 return FILE_ReadFileEx(hFile,buffer,bytesToRead,overlapped,lpCompletionRoutine);
1415 /***********************************************************************
1416 * ReadFile (KERNEL32.@)
1418 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1419 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1421 int unix_handle, result;
1423 TRACE("%d %p %ld %p %p\n", hFile, buffer, bytesToRead,
1424 bytesRead, overlapped );
1426 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1427 if (!bytesToRead) return TRUE;
1429 /* this will only have impact if the overlapped structure is specified */
1430 if ( overlapped )
1432 /* see if we can read some data already (this shouldn't block) */
1433 unix_handle = FILE_GetUnixHandle( hFile, GENERIC_READ );
1434 if (unix_handle == -1)
1435 return FALSE;
1436 result = read( unix_handle, buffer, bytesToRead );
1437 close(unix_handle);
1439 if(result<0)
1441 FILE_SetDosError();
1442 return FALSE;
1445 /* if we read enough to keep the app happy, then return now */
1446 if(result>=bytesToRead)
1448 *bytesRead = result;
1449 return TRUE;
1452 /* at last resort, do an overlapped read */
1453 overlapped->Internal = STATUS_PENDING;
1454 overlapped->InternalHigh = result;
1456 if(!FILE_ReadFileEx(hFile, buffer, bytesToRead, overlapped, NULL))
1457 return FALSE;
1459 /* fail on return, with ERROR_IO_PENDING */
1460 SetLastError(ERROR_IO_PENDING);
1461 return FALSE;
1464 unix_handle = FILE_GetUnixHandle( hFile, GENERIC_READ );
1465 if (unix_handle == -1) return FALSE;
1467 /* code for synchronous reads */
1468 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1470 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1471 if ((errno == EFAULT) && !IsBadWritePtr( buffer, bytesToRead )) continue;
1472 FILE_SetDosError();
1473 break;
1475 close( unix_handle );
1476 if (result == -1) return FALSE;
1477 if (bytesRead) *bytesRead = result;
1478 return TRUE;
1482 /***********************************************************************
1483 * FILE_AsyncWriteService (INTERNAL)
1485 * This function is called while the client is waiting on the
1486 * server, so we can't make any server calls here.
1488 static void FILE_AsyncWriteService(struct async_private *ovp, int events)
1490 LPOVERLAPPED lpOverlapped = ovp->lpOverlapped;
1491 int result, r;
1493 TRACE("(%p %p %08x)\n",lpOverlapped,ovp->buffer,events);
1495 /* if POLLNVAL, then our fd was closed or we have the wrong fd */
1496 if(events&POLLNVAL)
1498 ERR("fd %d invalid for %p\n",ovp->fd,ovp);
1499 r = STATUS_UNSUCCESSFUL;
1500 goto async_end;
1503 /* if there are no events, it must be a timeout */
1504 if(events==0)
1506 TRACE("write timed out\n");
1507 r = STATUS_TIMEOUT;
1508 goto async_end;
1511 /* write some data (non-blocking) */
1512 result = write(ovp->fd, &ovp->buffer[lpOverlapped->InternalHigh],
1513 ovp->count-lpOverlapped->InternalHigh);
1515 if ( (result<0) && ((errno == EAGAIN) || (errno == EINTR)))
1517 r = STATUS_PENDING;
1518 goto async_end;
1521 /* check to see if the transfer is complete */
1522 if(result<0)
1524 r = STATUS_UNSUCCESSFUL;
1525 goto async_end;
1528 lpOverlapped->InternalHigh += result;
1530 TRACE("wrote %d more bytes %ld/%d so far\n",result,lpOverlapped->InternalHigh,ovp->count);
1532 if(lpOverlapped->InternalHigh < ovp->count)
1533 r = STATUS_PENDING;
1534 else
1535 r = STATUS_SUCCESS;
1537 async_end:
1538 lpOverlapped->Internal = r;
1541 /***********************************************************************
1542 * WriteFileEx (KERNEL32.@)
1544 BOOL WINAPI WriteFileEx(HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1545 LPOVERLAPPED overlapped,
1546 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1548 async_private *ovp;
1549 int timeout,ret;
1551 TRACE("file %d to buf %p num %ld %p func %p stub\n",
1552 hFile, buffer, bytesToWrite, overlapped, lpCompletionRoutine);
1554 if ( (overlapped == NULL) || NtResetEvent( overlapped->hEvent, NULL ) )
1556 SetLastError(ERROR_INVALID_PARAMETER);
1557 return FALSE;
1560 overlapped->Internal = STATUS_PENDING;
1561 overlapped->InternalHigh = 0;
1563 /* need to check the server to get the timeout info */
1564 SERVER_START_REQ(create_async)
1566 req->count = bytesToWrite;
1567 req->type = ASYNC_TYPE_WRITE;
1568 req->file_handle = hFile;
1569 ret = SERVER_CALL();
1570 timeout = req->timeout;
1572 SERVER_END_REQ;
1573 if (ret)
1575 TRACE("server call failed\n");
1576 return FALSE;
1579 ovp = (async_private*) HeapAlloc(GetProcessHeap(), 0, sizeof (async_private));
1580 if(!ovp)
1582 TRACE("HeapAlloc Failed\n");
1583 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1584 return FALSE;
1586 ovp->lpOverlapped = overlapped;
1587 ovp->timeout = timeout;
1588 gettimeofday(&ovp->tv,NULL);
1589 add_timeout(&ovp->tv,timeout);
1590 ovp->event = POLLOUT;
1591 ovp->func = FILE_AsyncWriteService;
1592 ovp->buffer = (LPVOID) buffer;
1593 ovp->count = bytesToWrite;
1594 ovp->completion_func = lpCompletionRoutine;
1595 ovp->fd = FILE_GetUnixHandle( hFile, GENERIC_WRITE );
1596 if(ovp->fd <0)
1598 HeapFree(GetProcessHeap(), 0, ovp);
1599 return FALSE;
1602 /* hook this overlap into the pending async operation list */
1603 ovp->next = NtCurrentTeb()->pending_list;
1604 ovp->prev = NULL;
1605 if(ovp->next)
1606 ovp->next->prev = ovp;
1607 NtCurrentTeb()->pending_list = ovp;
1609 SetLastError(ERROR_IO_PENDING);
1611 /* always fail on return, either ERROR_IO_PENDING or other error */
1612 return FALSE;
1615 /***********************************************************************
1616 * WriteFile (KERNEL32.@)
1618 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1619 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1621 int unix_handle, result;
1623 TRACE("%d %p %ld %p %p\n", hFile, buffer, bytesToWrite,
1624 bytesWritten, overlapped );
1626 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1627 if (!bytesToWrite) return TRUE;
1629 /* this will only have impact if the overlappd structure is specified */
1630 if ( overlapped )
1631 return WriteFileEx(hFile, buffer, bytesToWrite, overlapped, NULL);
1633 unix_handle = FILE_GetUnixHandle( hFile, GENERIC_WRITE );
1634 if (unix_handle == -1) return FALSE;
1636 /* synchronous file write */
1637 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1639 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1640 if ((errno == EFAULT) && !IsBadReadPtr( buffer, bytesToWrite )) continue;
1641 if (errno == ENOSPC)
1642 SetLastError( ERROR_DISK_FULL );
1643 else
1644 FILE_SetDosError();
1645 break;
1647 close( unix_handle );
1648 if (result == -1) return FALSE;
1649 if (bytesWritten) *bytesWritten = result;
1650 return TRUE;
1654 /***********************************************************************
1655 * _hread (KERNEL.349)
1657 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1659 LONG maxlen;
1661 TRACE("%d %08lx %ld\n",
1662 hFile, (DWORD)buffer, count );
1664 /* Some programs pass a count larger than the allocated buffer */
1665 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1666 if (count > maxlen) count = maxlen;
1667 return _lread(DosFileHandleToWin32Handle(hFile), MapSL(buffer), count );
1671 /***********************************************************************
1672 * _lread (KERNEL.82)
1674 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1676 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1680 /***********************************************************************
1681 * _lread (KERNEL32.@)
1683 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1685 DWORD result;
1686 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1687 return result;
1691 /***********************************************************************
1692 * _lread16 (KERNEL.82)
1694 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1696 return (UINT16)_lread(DosFileHandleToWin32Handle(hFile), buffer, (LONG)count );
1700 /***********************************************************************
1701 * _lcreat (KERNEL.83)
1703 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1705 return Win32HandleToDosFileHandle( _lcreat( path, attr ) );
1709 /***********************************************************************
1710 * _lcreat (KERNEL32.@)
1712 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1714 /* Mask off all flags not explicitly allowed by the doc */
1715 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1716 TRACE("%s %02x\n", path, attr );
1717 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1718 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1719 CREATE_ALWAYS, attr, 0 );
1723 /***********************************************************************
1724 * SetFilePointer (KERNEL32.@)
1726 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword,
1727 DWORD method )
1729 DWORD ret = 0xffffffff;
1731 TRACE("handle %d offset %ld high %ld origin %ld\n",
1732 hFile, distance, highword?*highword:0, method );
1734 SERVER_START_REQ( set_file_pointer )
1736 req->handle = hFile;
1737 req->low = distance;
1738 req->high = highword ? *highword : (distance >= 0) ? 0 : -1;
1739 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1740 req->whence = method;
1741 SetLastError( 0 );
1742 if (!SERVER_CALL_ERR())
1744 ret = req->new_low;
1745 if (highword) *highword = req->new_high;
1748 SERVER_END_REQ;
1749 return ret;
1753 /***********************************************************************
1754 * _llseek (KERNEL.84)
1756 * FIXME:
1757 * Seeking before the start of the file should be allowed for _llseek16,
1758 * but cause subsequent I/O operations to fail (cf. interrupt list)
1761 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1763 return SetFilePointer( DosFileHandleToWin32Handle(hFile), lOffset, NULL, nOrigin );
1767 /***********************************************************************
1768 * _llseek (KERNEL32.@)
1770 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1772 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1776 /***********************************************************************
1777 * _lopen (KERNEL.85)
1779 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1781 return Win32HandleToDosFileHandle( _lopen( path, mode ) );
1785 /***********************************************************************
1786 * _lopen (KERNEL32.@)
1788 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1790 DWORD access, sharing;
1792 TRACE("('%s',%04x)\n", path, mode );
1793 FILE_ConvertOFMode( mode, &access, &sharing );
1794 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, 0 );
1798 /***********************************************************************
1799 * _lwrite (KERNEL.86)
1801 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1803 return (UINT16)_hwrite( DosFileHandleToWin32Handle(hFile), buffer, (LONG)count );
1806 /***********************************************************************
1807 * _lwrite (KERNEL32.@)
1809 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1811 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1815 /***********************************************************************
1816 * _hread16 (KERNEL.349)
1818 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1820 return _lread( DosFileHandleToWin32Handle(hFile), buffer, count );
1824 /***********************************************************************
1825 * _hread (KERNEL32.@)
1827 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1829 return _lread( hFile, buffer, count );
1833 /***********************************************************************
1834 * _hwrite (KERNEL.350)
1836 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1838 return _hwrite( DosFileHandleToWin32Handle(hFile), buffer, count );
1842 /***********************************************************************
1843 * _hwrite (KERNEL32.@)
1845 * experimentation yields that _lwrite:
1846 * o truncates the file at the current position with
1847 * a 0 len write
1848 * o returns 0 on a 0 length write
1849 * o works with console handles
1852 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1854 DWORD result;
1856 TRACE("%d %p %ld\n", handle, buffer, count );
1858 if (!count)
1860 /* Expand or truncate at current position */
1861 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1862 return 0;
1864 if (!WriteFile( handle, buffer, count, &result, NULL ))
1865 return HFILE_ERROR;
1866 return result;
1870 /***********************************************************************
1871 * SetHandleCount (KERNEL.199)
1873 UINT16 WINAPI SetHandleCount16( UINT16 count )
1875 return SetHandleCount( count );
1879 /*************************************************************************
1880 * SetHandleCount (KERNEL32.@)
1882 UINT WINAPI SetHandleCount( UINT count )
1884 return min( 256, count );
1888 /***********************************************************************
1889 * FlushFileBuffers (KERNEL32.@)
1891 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
1893 BOOL ret;
1894 SERVER_START_REQ( flush_file )
1896 req->handle = hFile;
1897 ret = !SERVER_CALL_ERR();
1899 SERVER_END_REQ;
1900 return ret;
1904 /**************************************************************************
1905 * SetEndOfFile (KERNEL32.@)
1907 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1909 BOOL ret;
1910 SERVER_START_REQ( truncate_file )
1912 req->handle = hFile;
1913 ret = !SERVER_CALL_ERR();
1915 SERVER_END_REQ;
1916 return ret;
1920 /***********************************************************************
1921 * DeleteFile (KERNEL.146)
1923 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1925 return DeleteFileA( path );
1929 /***********************************************************************
1930 * DeleteFileA (KERNEL32.@)
1932 BOOL WINAPI DeleteFileA( LPCSTR path )
1934 DOS_FULL_NAME full_name;
1936 TRACE("'%s'\n", path );
1938 if (!*path)
1940 ERR("Empty path passed\n");
1941 return FALSE;
1943 if (DOSFS_GetDevice( path ))
1945 WARN("cannot remove DOS device '%s'!\n", path);
1946 SetLastError( ERROR_FILE_NOT_FOUND );
1947 return FALSE;
1950 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1951 if (unlink( full_name.long_name ) == -1)
1953 FILE_SetDosError();
1954 return FALSE;
1956 return TRUE;
1960 /***********************************************************************
1961 * DeleteFileW (KERNEL32.@)
1963 BOOL WINAPI DeleteFileW( LPCWSTR path )
1965 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1966 BOOL ret = DeleteFileA( xpath );
1967 HeapFree( GetProcessHeap(), 0, xpath );
1968 return ret;
1972 /***********************************************************************
1973 * GetFileType (KERNEL32.@)
1975 DWORD WINAPI GetFileType( HANDLE hFile )
1977 DWORD ret = FILE_TYPE_UNKNOWN;
1978 SERVER_START_REQ( get_file_info )
1980 req->handle = hFile;
1981 if (!SERVER_CALL_ERR()) ret = req->type;
1983 SERVER_END_REQ;
1984 return ret;
1988 /* check if a file name is for an executable file (.exe or .com) */
1989 inline static BOOL is_executable( const char *name )
1991 int len = strlen(name);
1993 if (len < 4) return FALSE;
1994 return (!strcasecmp( name + len - 4, ".exe" ) ||
1995 !strcasecmp( name + len - 4, ".com" ));
1999 /**************************************************************************
2000 * MoveFileExA (KERNEL32.@)
2002 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
2004 DOS_FULL_NAME full_name1, full_name2;
2006 TRACE("(%s,%s,%04lx)\n", fn1, fn2, flag);
2008 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
2010 if (fn2) /* !fn2 means delete fn1 */
2012 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
2014 /* target exists, check if we may overwrite */
2015 if (!(flag & MOVEFILE_REPLACE_EXISTING))
2017 /* FIXME: Use right error code */
2018 SetLastError( ERROR_ACCESS_DENIED );
2019 return FALSE;
2022 else if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
2024 /* Source name and target path are valid */
2026 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
2028 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
2029 Perhaps we should queue these command and execute it
2030 when exiting... What about using on_exit(2)
2032 FIXME("Please move existing file '%s' to file '%s' when Wine has finished\n",
2033 full_name1.long_name, full_name2.long_name);
2034 return TRUE;
2037 if (full_name1.drive != full_name2.drive)
2039 /* use copy, if allowed */
2040 if (!(flag & MOVEFILE_COPY_ALLOWED))
2042 /* FIXME: Use right error code */
2043 SetLastError( ERROR_FILE_EXISTS );
2044 return FALSE;
2046 return CopyFileA( fn1, fn2, !(flag & MOVEFILE_REPLACE_EXISTING) );
2048 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
2050 FILE_SetDosError();
2051 return FALSE;
2053 if (is_executable( full_name1.long_name ) != is_executable( full_name2.long_name ))
2055 struct stat fstat;
2056 if (stat( full_name2.long_name, &fstat ) != -1)
2058 if (is_executable( full_name2.long_name ))
2059 /* set executable bit where read bit is set */
2060 fstat.st_mode |= (fstat.st_mode & 0444) >> 2;
2061 else
2062 fstat.st_mode &= ~0111;
2063 chmod( full_name2.long_name, fstat.st_mode );
2066 return TRUE;
2068 else /* fn2 == NULL means delete source */
2070 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
2072 if (flag & MOVEFILE_COPY_ALLOWED) {
2073 WARN("Illegal flag\n");
2074 SetLastError( ERROR_GEN_FAILURE );
2075 return FALSE;
2077 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
2078 Perhaps we should queue these command and execute it
2079 when exiting... What about using on_exit(2)
2081 FIXME("Please delete file '%s' when Wine has finished\n",
2082 full_name1.long_name);
2083 return TRUE;
2086 if (unlink( full_name1.long_name ) == -1)
2088 FILE_SetDosError();
2089 return FALSE;
2091 return TRUE; /* successfully deleted */
2095 /**************************************************************************
2096 * MoveFileExW (KERNEL32.@)
2098 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
2100 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
2101 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
2102 BOOL res = MoveFileExA( afn1, afn2, flag );
2103 HeapFree( GetProcessHeap(), 0, afn1 );
2104 HeapFree( GetProcessHeap(), 0, afn2 );
2105 return res;
2109 /**************************************************************************
2110 * MoveFileA (KERNEL32.@)
2112 * Move file or directory
2114 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
2116 DOS_FULL_NAME full_name1, full_name2;
2117 struct stat fstat;
2119 TRACE("(%s,%s)\n", fn1, fn2 );
2121 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
2122 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 )) {
2123 /* The new name must not already exist */
2124 SetLastError(ERROR_ALREADY_EXISTS);
2125 return FALSE;
2127 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
2129 if (full_name1.drive == full_name2.drive) /* move */
2130 return MoveFileExA( fn1, fn2, MOVEFILE_COPY_ALLOWED );
2132 /* copy */
2133 if (stat( full_name1.long_name, &fstat ))
2135 WARN("Invalid source file %s\n",
2136 full_name1.long_name);
2137 FILE_SetDosError();
2138 return FALSE;
2140 if (S_ISDIR(fstat.st_mode)) {
2141 /* No Move for directories across file systems */
2142 /* FIXME: Use right error code */
2143 SetLastError( ERROR_GEN_FAILURE );
2144 return FALSE;
2146 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
2150 /**************************************************************************
2151 * MoveFileW (KERNEL32.@)
2153 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
2155 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
2156 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
2157 BOOL res = MoveFileA( afn1, afn2 );
2158 HeapFree( GetProcessHeap(), 0, afn1 );
2159 HeapFree( GetProcessHeap(), 0, afn2 );
2160 return res;
2164 /**************************************************************************
2165 * CopyFileA (KERNEL32.@)
2167 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
2169 HFILE h1, h2;
2170 BY_HANDLE_FILE_INFORMATION info;
2171 UINT count;
2172 BOOL ret = FALSE;
2173 int mode;
2174 char buffer[2048];
2176 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
2177 if (!GetFileInformationByHandle( h1, &info ))
2179 CloseHandle( h1 );
2180 return FALSE;
2182 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
2183 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2184 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
2185 info.dwFileAttributes, h1 )) == INVALID_HANDLE_VALUE)
2187 CloseHandle( h1 );
2188 return FALSE;
2190 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
2192 char *p = buffer;
2193 while (count > 0)
2195 INT res = _lwrite( h2, p, count );
2196 if (res <= 0) goto done;
2197 p += res;
2198 count -= res;
2201 ret = TRUE;
2202 done:
2203 CloseHandle( h1 );
2204 CloseHandle( h2 );
2205 return ret;
2209 /**************************************************************************
2210 * CopyFileW (KERNEL32.@)
2212 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
2214 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
2215 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
2216 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
2217 HeapFree( GetProcessHeap(), 0, sourceA );
2218 HeapFree( GetProcessHeap(), 0, destA );
2219 return ret;
2223 /**************************************************************************
2224 * CopyFileExA (KERNEL32.@)
2226 * This implementation ignores most of the extra parameters passed-in into
2227 * the "ex" version of the method and calls the CopyFile method.
2228 * It will have to be fixed eventually.
2230 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
2231 LPCSTR destFilename,
2232 LPPROGRESS_ROUTINE progressRoutine,
2233 LPVOID appData,
2234 LPBOOL cancelFlagPointer,
2235 DWORD copyFlags)
2237 BOOL failIfExists = FALSE;
2240 * Interpret the only flag that CopyFile can interpret.
2242 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
2244 failIfExists = TRUE;
2247 return CopyFileA(sourceFilename, destFilename, failIfExists);
2250 /**************************************************************************
2251 * CopyFileExW (KERNEL32.@)
2253 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
2254 LPCWSTR destFilename,
2255 LPPROGRESS_ROUTINE progressRoutine,
2256 LPVOID appData,
2257 LPBOOL cancelFlagPointer,
2258 DWORD copyFlags)
2260 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
2261 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
2263 BOOL ret = CopyFileExA(sourceA,
2264 destA,
2265 progressRoutine,
2266 appData,
2267 cancelFlagPointer,
2268 copyFlags);
2270 HeapFree( GetProcessHeap(), 0, sourceA );
2271 HeapFree( GetProcessHeap(), 0, destA );
2273 return ret;
2277 /***********************************************************************
2278 * SetFileTime (KERNEL32.@)
2280 BOOL WINAPI SetFileTime( HANDLE hFile,
2281 const FILETIME *lpCreationTime,
2282 const FILETIME *lpLastAccessTime,
2283 const FILETIME *lpLastWriteTime )
2285 BOOL ret;
2286 SERVER_START_REQ( set_file_time )
2288 req->handle = hFile;
2289 if (lpLastAccessTime)
2290 RtlTimeToSecondsSince1970( lpLastAccessTime, (DWORD *)&req->access_time );
2291 else
2292 req->access_time = 0; /* FIXME */
2293 if (lpLastWriteTime)
2294 RtlTimeToSecondsSince1970( lpLastWriteTime, (DWORD *)&req->write_time );
2295 else
2296 req->write_time = 0; /* FIXME */
2297 ret = !SERVER_CALL_ERR();
2299 SERVER_END_REQ;
2300 return ret;
2304 /**************************************************************************
2305 * LockFile (KERNEL32.@)
2307 BOOL WINAPI LockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
2308 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
2310 BOOL ret;
2311 SERVER_START_REQ( lock_file )
2313 req->handle = hFile;
2314 req->offset_low = dwFileOffsetLow;
2315 req->offset_high = dwFileOffsetHigh;
2316 req->count_low = nNumberOfBytesToLockLow;
2317 req->count_high = nNumberOfBytesToLockHigh;
2318 ret = !SERVER_CALL_ERR();
2320 SERVER_END_REQ;
2321 return ret;
2324 /**************************************************************************
2325 * LockFileEx [KERNEL32.@]
2327 * Locks a byte range within an open file for shared or exclusive access.
2329 * RETURNS
2330 * success: TRUE
2331 * failure: FALSE
2333 * NOTES
2334 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
2336 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
2337 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
2338 LPOVERLAPPED pOverlapped )
2340 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2341 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
2342 pOverlapped);
2343 if (reserved == 0)
2344 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2345 else
2347 ERR("reserved == %ld: Supposed to be 0??\n", reserved);
2348 SetLastError(ERROR_INVALID_PARAMETER);
2351 return FALSE;
2355 /**************************************************************************
2356 * UnlockFile (KERNEL32.@)
2358 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
2359 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
2361 BOOL ret;
2362 SERVER_START_REQ( unlock_file )
2364 req->handle = hFile;
2365 req->offset_low = dwFileOffsetLow;
2366 req->offset_high = dwFileOffsetHigh;
2367 req->count_low = nNumberOfBytesToUnlockLow;
2368 req->count_high = nNumberOfBytesToUnlockHigh;
2369 ret = !SERVER_CALL_ERR();
2371 SERVER_END_REQ;
2372 return ret;
2376 /**************************************************************************
2377 * UnlockFileEx (KERNEL32.@)
2379 BOOL WINAPI UnlockFileEx(
2380 HFILE hFile,
2381 DWORD dwReserved,
2382 DWORD nNumberOfBytesToUnlockLow,
2383 DWORD nNumberOfBytesToUnlockHigh,
2384 LPOVERLAPPED lpOverlapped
2387 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2388 hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh,
2389 lpOverlapped);
2390 if (dwReserved == 0)
2391 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2392 else
2394 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved);
2395 SetLastError(ERROR_INVALID_PARAMETER);
2398 return FALSE;
2402 #if 0
2404 struct DOS_FILE_LOCK {
2405 struct DOS_FILE_LOCK * next;
2406 DWORD base;
2407 DWORD len;
2408 DWORD processId;
2409 FILE_OBJECT * dos_file;
2410 /* char * unix_name;*/
2413 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2415 static DOS_FILE_LOCK *locks = NULL;
2416 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2419 /* Locks need to be mirrored because unix file locking is based
2420 * on the pid. Inside of wine there can be multiple WINE processes
2421 * that share the same unix pid.
2422 * Read's and writes should check these locks also - not sure
2423 * how critical that is at this point (FIXME).
2426 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2428 DOS_FILE_LOCK *curr;
2429 DWORD processId;
2431 processId = GetCurrentProcessId();
2433 /* check if lock overlaps a current lock for the same file */
2434 #if 0
2435 for (curr = locks; curr; curr = curr->next) {
2436 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2437 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2438 return TRUE;/* region is identic */
2439 if ((f->l_start < (curr->base + curr->len)) &&
2440 ((f->l_start + f->l_len) > curr->base)) {
2441 /* region overlaps */
2442 return FALSE;
2446 #endif
2448 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(DOS_FILE_LOCK) );
2449 curr->processId = GetCurrentProcessId();
2450 curr->base = f->l_start;
2451 curr->len = f->l_len;
2452 /* curr->unix_name = HEAP_strdupA( GetProcessHeap(), 0, file->unix_name);*/
2453 curr->next = locks;
2454 curr->dos_file = file;
2455 locks = curr;
2456 return TRUE;
2459 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2461 DWORD processId;
2462 DOS_FILE_LOCK **curr;
2463 DOS_FILE_LOCK *rem;
2465 processId = GetCurrentProcessId();
2466 curr = &locks;
2467 while (*curr) {
2468 if ((*curr)->dos_file == file) {
2469 rem = *curr;
2470 *curr = (*curr)->next;
2471 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2472 HeapFree( GetProcessHeap(), 0, rem );
2474 else
2475 curr = &(*curr)->next;
2479 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2481 DWORD processId;
2482 DOS_FILE_LOCK **curr;
2483 DOS_FILE_LOCK *rem;
2485 processId = GetCurrentProcessId();
2486 for (curr = &locks; *curr; curr = &(*curr)->next) {
2487 if ((*curr)->processId == processId &&
2488 (*curr)->dos_file == file &&
2489 (*curr)->base == f->l_start &&
2490 (*curr)->len == f->l_len) {
2491 /* this is the same lock */
2492 rem = *curr;
2493 *curr = (*curr)->next;
2494 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2495 HeapFree( GetProcessHeap(), 0, rem );
2496 return TRUE;
2499 /* no matching lock found */
2500 return FALSE;
2504 /**************************************************************************
2505 * LockFile (KERNEL32.@)
2507 BOOL WINAPI LockFile(
2508 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2509 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2511 struct flock f;
2512 FILE_OBJECT *file;
2514 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2515 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2516 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2518 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2519 FIXME("Unimplemented bytes > 32bits\n");
2520 return FALSE;
2523 f.l_start = dwFileOffsetLow;
2524 f.l_len = nNumberOfBytesToLockLow;
2525 f.l_whence = SEEK_SET;
2526 f.l_pid = 0;
2527 f.l_type = F_WRLCK;
2529 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2531 /* shadow locks internally */
2532 if (!DOS_AddLock(file, &f)) {
2533 SetLastError( ERROR_LOCK_VIOLATION );
2534 return FALSE;
2537 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2538 #ifdef USE_UNIX_LOCKS
2539 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2540 if (errno == EACCES || errno == EAGAIN) {
2541 SetLastError( ERROR_LOCK_VIOLATION );
2543 else {
2544 FILE_SetDosError();
2546 /* remove our internal copy of the lock */
2547 DOS_RemoveLock(file, &f);
2548 return FALSE;
2550 #endif
2551 return TRUE;
2555 /**************************************************************************
2556 * UnlockFile (KERNEL32.@)
2558 BOOL WINAPI UnlockFile(
2559 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2560 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2562 FILE_OBJECT *file;
2563 struct flock f;
2565 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2566 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2567 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2569 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2570 WARN("Unimplemented bytes > 32bits\n");
2571 return FALSE;
2574 f.l_start = dwFileOffsetLow;
2575 f.l_len = nNumberOfBytesToUnlockLow;
2576 f.l_whence = SEEK_SET;
2577 f.l_pid = 0;
2578 f.l_type = F_UNLCK;
2580 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2582 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2584 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2585 #ifdef USE_UNIX_LOCKS
2586 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2587 FILE_SetDosError();
2588 return FALSE;
2590 #endif
2591 return TRUE;
2593 #endif
2595 /**************************************************************************
2596 * GetFileAttributesExA [KERNEL32.@]
2598 BOOL WINAPI GetFileAttributesExA(
2599 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2600 LPVOID lpFileInformation)
2602 DOS_FULL_NAME full_name;
2603 BY_HANDLE_FILE_INFORMATION info;
2605 if (lpFileName == NULL) return FALSE;
2606 if (lpFileInformation == NULL) return FALSE;
2608 if (fInfoLevelId == GetFileExInfoStandard) {
2609 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2610 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2611 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2612 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2614 lpFad->dwFileAttributes = info.dwFileAttributes;
2615 lpFad->ftCreationTime = info.ftCreationTime;
2616 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2617 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2618 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2619 lpFad->nFileSizeLow = info.nFileSizeLow;
2621 else {
2622 FIXME("invalid info level %d!\n", fInfoLevelId);
2623 return FALSE;
2626 return TRUE;
2630 /**************************************************************************
2631 * GetFileAttributesExW [KERNEL32.@]
2633 BOOL WINAPI GetFileAttributesExW(
2634 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2635 LPVOID lpFileInformation)
2637 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2638 BOOL res =
2639 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2640 HeapFree( GetProcessHeap(), 0, nameA );
2641 return res;