Fixed/commented out duplicate entry point names.
[wine/hacks.git] / files / file.c
blob2c373d229075cd3e07dff06cb52ea965e4c0d1e8
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 "extented" functionality.
9 * Right now, they simply call the CopyFile method.
12 #include "config.h"
14 #include <assert.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #ifdef HAVE_SYS_ERRNO_H
21 #include <sys/errno.h>
22 #endif
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #ifdef HAVE_SYS_MMAN_H
26 #include <sys/mman.h>
27 #endif
28 #include <sys/time.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <utime.h>
33 #include "winerror.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wine/winbase16.h"
37 #include "wine/winestring.h"
38 #include "drive.h"
39 #include "device.h"
40 #include "file.h"
41 #include "global.h"
42 #include "heap.h"
43 #include "msdos.h"
44 #include "options.h"
45 #include "ldt.h"
46 #include "process.h"
47 #include "task.h"
48 #include "wincon.h"
49 #include "debugtools.h"
51 #include "server.h"
53 DEFAULT_DEBUG_CHANNEL(file)
55 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
56 #define MAP_ANON MAP_ANONYMOUS
57 #endif
59 /* Size of per-process table of DOS handles */
60 #define DOS_TABLE_SIZE 256
63 /***********************************************************************
64 * FILE_ConvertOFMode
66 * Convert OF_* mode into flags for CreateFile.
68 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
70 switch(mode & 0x03)
72 case OF_READ: *access = GENERIC_READ; break;
73 case OF_WRITE: *access = GENERIC_WRITE; break;
74 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
75 default: *access = 0; break;
77 switch(mode & 0x70)
79 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
80 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
81 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
82 case OF_SHARE_DENY_NONE:
83 case OF_SHARE_COMPAT:
84 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
89 #if 0
90 /***********************************************************************
91 * FILE_ShareDeny
93 * PARAMS
94 * oldmode[I] mode how file was first opened
95 * mode[I] mode how the file should get opened
96 * RETURNS
97 * TRUE: deny open
98 * FALSE: allow open
100 * Look what we have to do with the given SHARE modes
102 * Ralph Brown's interrupt list gives following explication, I guess
103 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
105 * FIXME: Validate this function
106 ========from Ralph Brown's list =========
107 (Table 0750)
108 Values of DOS file sharing behavior:
109 | Second and subsequent Opens
110 First |Compat Deny Deny Deny Deny
111 Open | All Write Read None
112 |R W RW R W RW R W RW R W RW R W RW
113 - - - - -| - - - - - - - - - - - - - - - - -
114 Compat R |Y Y Y N N N 1 N N N N N 1 N N
115 W |Y Y Y N N N N N N N N N N N N
116 RW|Y Y Y N N N N N N N N N N N N
117 - - - - -|
118 Deny R |C C C N N N N N N N N N N N N
119 All W |C C C N N N N N N N N N N N N
120 RW|C C C N N N N N N N N N N N N
121 - - - - -|
122 Deny R |2 C C N N N Y N N N N N Y N N
123 Write W |C C C N N N N N N Y N N Y N N
124 RW|C C C N N N N N N N N N Y N N
125 - - - - -|
126 Deny R |C C C N N N N Y N N N N N Y N
127 Read W |C C C N N N N N N N Y N N Y N
128 RW|C C C N N N N N N N N N N Y N
129 - - - - -|
130 Deny R |2 C C N N N Y Y Y N N N Y Y Y
131 None W |C C C N N N N N N Y Y Y Y Y Y
132 RW|C C C N N N N N N N N N Y Y Y
133 Legend: Y = open succeeds, N = open fails with error code 05h
134 C = open fails, INT 24 generated
135 1 = open succeeds if file read-only, else fails with error code
136 2 = open succeeds if file read-only, else fails with INT 24
137 ========end of description from Ralph Brown's List =====
138 For every "Y" in the table we return FALSE
139 For every "N" we set the DOS_ERROR and return TRUE
140 For all other cases we barf,set the DOS_ERROR and return TRUE
143 static BOOL FILE_ShareDeny( int mode, int oldmode)
145 int oldsharemode = oldmode & 0x70;
146 int sharemode = mode & 0x70;
147 int oldopenmode = oldmode & 3;
148 int openmode = mode & 3;
150 switch (oldsharemode)
152 case OF_SHARE_COMPAT:
153 if (sharemode == OF_SHARE_COMPAT) return FALSE;
154 if (openmode == OF_READ) goto test_ro_err05 ;
155 goto fail_error05;
156 case OF_SHARE_EXCLUSIVE:
157 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
158 goto fail_error05;
159 case OF_SHARE_DENY_WRITE:
160 if (openmode != OF_READ)
162 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
163 goto fail_error05;
165 switch (sharemode)
167 case OF_SHARE_COMPAT:
168 if (oldopenmode == OF_READ) goto test_ro_int24 ;
169 goto fail_int24;
170 case OF_SHARE_DENY_NONE :
171 return FALSE;
172 case OF_SHARE_DENY_WRITE :
173 if (oldopenmode == OF_READ) return FALSE;
174 case OF_SHARE_DENY_READ :
175 if (oldopenmode == OF_WRITE) return FALSE;
176 case OF_SHARE_EXCLUSIVE:
177 default:
178 goto fail_error05;
180 break;
181 case OF_SHARE_DENY_READ:
182 if (openmode != OF_WRITE)
184 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
185 goto fail_error05;
187 switch (sharemode)
189 case OF_SHARE_COMPAT:
190 goto fail_int24;
191 case OF_SHARE_DENY_NONE :
192 return FALSE;
193 case OF_SHARE_DENY_WRITE :
194 if (oldopenmode == OF_READ) return FALSE;
195 case OF_SHARE_DENY_READ :
196 if (oldopenmode == OF_WRITE) return FALSE;
197 case OF_SHARE_EXCLUSIVE:
198 default:
199 goto fail_error05;
201 break;
202 case OF_SHARE_DENY_NONE:
203 switch (sharemode)
205 case OF_SHARE_COMPAT:
206 goto fail_int24;
207 case OF_SHARE_DENY_NONE :
208 return FALSE;
209 case OF_SHARE_DENY_WRITE :
210 if (oldopenmode == OF_READ) return FALSE;
211 case OF_SHARE_DENY_READ :
212 if (oldopenmode == OF_WRITE) return FALSE;
213 case OF_SHARE_EXCLUSIVE:
214 default:
215 goto fail_error05;
217 default:
218 ERR("unknown mode\n");
220 ERR("shouldn't happen\n");
221 ERR("Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
222 return TRUE;
224 test_ro_int24:
225 if (oldmode == OF_READ)
226 return FALSE;
227 /* Fall through */
228 fail_int24:
229 FIXME("generate INT24 missing\n");
230 /* Is this the right error? */
231 SetLastError( ERROR_ACCESS_DENIED );
232 return TRUE;
234 test_ro_err05:
235 if (oldmode == OF_READ)
236 return FALSE;
237 /* fall through */
238 fail_error05:
239 TRACE("Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
240 SetLastError( ERROR_ACCESS_DENIED );
241 return TRUE;
243 #endif
246 /***********************************************************************
247 * FILE_SetDosError
249 * Set the DOS error code from errno.
251 void FILE_SetDosError(void)
253 int save_errno = errno; /* errno gets overwritten by printf */
255 TRACE("errno = %d %s\n", errno, strerror(errno));
256 switch (save_errno)
258 case EAGAIN:
259 SetLastError( ERROR_SHARING_VIOLATION );
260 break;
261 case EBADF:
262 SetLastError( ERROR_INVALID_HANDLE );
263 break;
264 case ENOSPC:
265 SetLastError( ERROR_HANDLE_DISK_FULL );
266 break;
267 case EACCES:
268 case EPERM:
269 case EROFS:
270 SetLastError( ERROR_ACCESS_DENIED );
271 break;
272 case EBUSY:
273 SetLastError( ERROR_LOCK_VIOLATION );
274 break;
275 case ENOENT:
276 SetLastError( ERROR_FILE_NOT_FOUND );
277 break;
278 case EISDIR:
279 SetLastError( ERROR_CANNOT_MAKE );
280 break;
281 case ENFILE:
282 case EMFILE:
283 SetLastError( ERROR_NO_MORE_FILES );
284 break;
285 case EEXIST:
286 SetLastError( ERROR_FILE_EXISTS );
287 break;
288 case EINVAL:
289 case ESPIPE:
290 SetLastError( ERROR_SEEK );
291 break;
292 case ENOTEMPTY:
293 SetLastError( ERROR_DIR_NOT_EMPTY );
294 break;
295 default:
296 perror( "int21: unknown errno" );
297 SetLastError( ERROR_GEN_FAILURE );
298 break;
300 errno = save_errno;
304 /***********************************************************************
305 * FILE_DupUnixHandle
307 * Duplicate a Unix handle into a task handle.
309 HFILE FILE_DupUnixHandle( int fd, DWORD access )
311 int unix_handle;
312 struct alloc_file_handle_request *req = get_req_buffer();
314 if ((unix_handle = dup(fd)) == -1)
316 FILE_SetDosError();
317 return INVALID_HANDLE_VALUE;
319 req->access = access;
320 server_call_fd( REQ_ALLOC_FILE_HANDLE, unix_handle, NULL );
321 return req->handle;
325 /***********************************************************************
326 * FILE_CreateFile
328 * Implementation of CreateFile. Takes a Unix path name.
330 HANDLE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
331 LPSECURITY_ATTRIBUTES sa, DWORD creation,
332 DWORD attributes, HANDLE template )
334 struct create_file_request *req = get_req_buffer();
336 req->access = access;
337 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
338 req->sharing = sharing;
339 req->create = creation;
340 req->attrs = attributes;
341 lstrcpynA( req->name, filename, server_remaining(req->name) );
342 SetLastError(0);
343 server_call( REQ_CREATE_FILE );
345 /* If write access failed, retry without GENERIC_WRITE */
347 if ((req->handle == -1) && !Options.failReadOnly &&
348 (access & GENERIC_WRITE))
350 DWORD lasterror = GetLastError();
351 if ((lasterror == ERROR_ACCESS_DENIED) || (lasterror == ERROR_WRITE_PROTECT))
352 return FILE_CreateFile( filename, access & ~GENERIC_WRITE, sharing,
353 sa, creation, attributes, template );
355 return req->handle;
359 /***********************************************************************
360 * FILE_CreateDevice
362 * Same as FILE_CreateFile but for a device
364 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
366 struct create_device_request *req = get_req_buffer();
368 req->access = access;
369 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
370 req->id = client_id;
371 SetLastError(0);
372 server_call( REQ_CREATE_DEVICE );
373 return req->handle;
377 /*************************************************************************
378 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
380 * Creates or opens an object, and returns a handle that can be used to
381 * access that object.
383 * PARAMS
385 * filename [I] pointer to filename to be accessed
386 * access [I] access mode requested
387 * sharing [I] share mode
388 * sa [I] pointer to security attributes
389 * creation [I] how to create the file
390 * attributes [I] attributes for newly created file
391 * template [I] handle to file with extended attributes to copy
393 * RETURNS
394 * Success: Open handle to specified file
395 * Failure: INVALID_HANDLE_VALUE
397 * NOTES
398 * Should call SetLastError() on failure.
400 * BUGS
402 * Doesn't support character devices, pipes, template files, or a
403 * lot of the 'attributes' flags yet.
405 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
406 LPSECURITY_ATTRIBUTES sa, DWORD creation,
407 DWORD attributes, HANDLE template )
409 DOS_FULL_NAME full_name;
411 if (!filename)
413 SetLastError( ERROR_INVALID_PARAMETER );
414 return HFILE_ERROR;
416 TRACE("%s %s%s%s%s%s%s%s\n",filename,
417 ((access & GENERIC_READ)==GENERIC_READ)?"GENERIC_READ ":"",
418 ((access & GENERIC_WRITE)==GENERIC_WRITE)?"GENERIC_WRITE ":"",
419 (!access)?"QUERY_ACCESS ":"",
420 ((sharing & FILE_SHARE_READ)==FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
421 ((sharing & FILE_SHARE_WRITE)==FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
422 ((sharing & FILE_SHARE_DELETE)==FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
423 (creation ==CREATE_NEW)?"CREATE_NEW":
424 (creation ==CREATE_ALWAYS)?"CREATE_ALWAYS ":
425 (creation ==OPEN_EXISTING)?"OPEN_EXISTING ":
426 (creation ==OPEN_ALWAYS)?"OPEN_ALWAYS ":
427 (creation ==TRUNCATE_EXISTING)?"TRUNCATE_EXISTING ":"");
429 /* If the name starts with '\\?\', ignore the first 4 chars. */
430 if (!strncmp(filename, "\\\\?\\", 4))
432 filename += 4;
433 if (!strncmp(filename, "UNC\\", 4))
435 FIXME("UNC name (%s) not supported.\n", filename );
436 SetLastError( ERROR_PATH_NOT_FOUND );
437 return HFILE_ERROR;
441 if (!strncmp(filename, "\\\\.\\", 4))
442 return DEVICE_Open( filename+4, access, sa );
444 /* If the name still starts with '\\', it's a UNC name. */
445 if (!strncmp(filename, "\\\\", 2))
447 FIXME("UNC name (%s) not supported.\n", filename );
448 SetLastError( ERROR_PATH_NOT_FOUND );
449 return HFILE_ERROR;
452 /* If the name contains a DOS wild card (* or ?), do no create a file */
453 if(strchr(filename,'*') || strchr(filename,'?'))
454 return HFILE_ERROR;
456 /* Open a console for CONIN$ or CONOUT$ */
457 if (!lstrcmpiA(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
458 if (!lstrcmpiA(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
460 if (DOSFS_GetDevice( filename ))
462 HFILE ret;
464 TRACE("opening device '%s'\n", filename );
466 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
467 return ret;
469 /* Do not silence this please. It is a critical error. -MM */
470 ERR("Couldn't open device '%s'!\n",filename);
471 SetLastError( ERROR_FILE_NOT_FOUND );
472 return HFILE_ERROR;
475 /* check for filename, don't check for last entry if creating */
476 if (!DOSFS_GetFullName( filename,
477 (creation == OPEN_EXISTING) || (creation == TRUNCATE_EXISTING), &full_name ))
478 return HFILE_ERROR;
480 return FILE_CreateFile( full_name.long_name, access, sharing,
481 sa, creation, attributes, template );
486 /*************************************************************************
487 * CreateFile32W (KERNEL32.48)
489 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
490 LPSECURITY_ATTRIBUTES sa, DWORD creation,
491 DWORD attributes, HANDLE template)
493 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
494 HANDLE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
495 HeapFree( GetProcessHeap(), 0, afn );
496 return res;
500 /***********************************************************************
501 * FILE_FillInfo
503 * Fill a file information from a struct stat.
505 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
507 if (S_ISDIR(st->st_mode))
508 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
509 else
510 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
511 if (!(st->st_mode & S_IWUSR))
512 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
514 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
515 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
516 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
518 info->dwVolumeSerialNumber = 0; /* FIXME */
519 info->nFileSizeHigh = 0;
520 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
521 info->nNumberOfLinks = st->st_nlink;
522 info->nFileIndexHigh = 0;
523 info->nFileIndexLow = st->st_ino;
527 /***********************************************************************
528 * FILE_Stat
530 * Stat a Unix path name. Return TRUE if OK.
532 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
534 struct stat st;
536 if (!unixName || !info) return FALSE;
538 if (stat( unixName, &st ) == -1)
540 FILE_SetDosError();
541 return FALSE;
543 FILE_FillInfo( &st, info );
544 return TRUE;
548 /***********************************************************************
549 * GetFileInformationByHandle (KERNEL32.219)
551 DWORD WINAPI GetFileInformationByHandle( HANDLE hFile,
552 BY_HANDLE_FILE_INFORMATION *info )
554 struct get_file_info_request *req = get_req_buffer();
556 if (!info) return 0;
557 req->handle = hFile;
558 if (server_call( REQ_GET_FILE_INFO )) return 0;
559 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftCreationTime, 0 );
560 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftLastWriteTime, 0 );
561 DOSFS_UnixTimeToFileTime( req->access_time, &info->ftLastAccessTime, 0 );
562 info->dwFileAttributes = req->attr;
563 info->dwVolumeSerialNumber = req->serial;
564 info->nFileSizeHigh = req->size_high;
565 info->nFileSizeLow = req->size_low;
566 info->nNumberOfLinks = req->links;
567 info->nFileIndexHigh = req->index_high;
568 info->nFileIndexLow = req->index_low;
569 return 1;
573 /**************************************************************************
574 * GetFileAttributes16 (KERNEL.420)
576 DWORD WINAPI GetFileAttributes16( LPCSTR name )
578 return GetFileAttributesA( name );
582 /**************************************************************************
583 * GetFileAttributes32A (KERNEL32.217)
585 DWORD WINAPI GetFileAttributesA( LPCSTR name )
587 DOS_FULL_NAME full_name;
588 BY_HANDLE_FILE_INFORMATION info;
590 if (name == NULL || *name=='\0') return -1;
592 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
593 if (!FILE_Stat( full_name.long_name, &info )) return -1;
594 return info.dwFileAttributes;
598 /**************************************************************************
599 * GetFileAttributes32W (KERNEL32.218)
601 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
603 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
604 DWORD res = GetFileAttributesA( nameA );
605 HeapFree( GetProcessHeap(), 0, nameA );
606 return res;
610 /***********************************************************************
611 * GetFileSize (KERNEL32.220)
613 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
615 BY_HANDLE_FILE_INFORMATION info;
616 if (!GetFileInformationByHandle( hFile, &info )) return 0;
617 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
618 return info.nFileSizeLow;
622 /***********************************************************************
623 * GetFileTime (KERNEL32.221)
625 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
626 FILETIME *lpLastAccessTime,
627 FILETIME *lpLastWriteTime )
629 BY_HANDLE_FILE_INFORMATION info;
630 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
631 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
632 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
633 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
634 return TRUE;
637 /***********************************************************************
638 * CompareFileTime (KERNEL32.28)
640 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
642 if (!x || !y) return -1;
644 if (x->dwHighDateTime > y->dwHighDateTime)
645 return 1;
646 if (x->dwHighDateTime < y->dwHighDateTime)
647 return -1;
648 if (x->dwLowDateTime > y->dwLowDateTime)
649 return 1;
650 if (x->dwLowDateTime < y->dwLowDateTime)
651 return -1;
652 return 0;
655 /***********************************************************************
656 * FILE_GetTempFileName : utility for GetTempFileName
658 static UINT FILE_GetTempFileName( LPCSTR path, LPCSTR prefix, UINT unique,
659 LPSTR buffer, BOOL isWin16 )
661 static UINT unique_temp;
662 DOS_FULL_NAME full_name;
663 int i;
664 LPSTR p;
665 UINT num;
667 if ( !path || !prefix || !buffer ) return 0;
669 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
670 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
672 strcpy( buffer, path );
673 p = buffer + strlen(buffer);
675 /* add a \, if there isn't one and path is more than just the drive letter ... */
676 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
677 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
679 if (isWin16) *p++ = '~';
680 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
681 sprintf( p, "%04x.tmp", num );
683 /* Now try to create it */
685 if (!unique)
689 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
690 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
691 if (handle != INVALID_HANDLE_VALUE)
692 { /* We created it */
693 TRACE("created %s\n",
694 buffer);
695 CloseHandle( handle );
696 break;
698 if (GetLastError() != ERROR_FILE_EXISTS)
699 break; /* No need to go on */
700 num++;
701 sprintf( p, "%04x.tmp", num );
702 } while (num != (unique & 0xffff));
705 /* Get the full path name */
707 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
709 /* Check if we have write access in the directory */
710 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
711 if (access( full_name.long_name, W_OK ) == -1)
712 WARN("returns '%s', which doesn't seem to be writeable.\n",
713 buffer);
715 TRACE("returning %s\n", buffer );
716 return unique ? unique : num;
720 /***********************************************************************
721 * GetTempFileName32A (KERNEL32.290)
723 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
724 LPSTR buffer)
726 return FILE_GetTempFileName(path, prefix, unique, buffer, FALSE);
729 /***********************************************************************
730 * GetTempFileName32W (KERNEL32.291)
732 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
733 LPWSTR buffer )
735 LPSTR patha,prefixa;
736 char buffera[144];
737 UINT ret;
739 if (!path) return 0;
740 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
741 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
742 ret = FILE_GetTempFileName( patha, prefixa, unique, buffera, FALSE );
743 lstrcpyAtoW( buffer, buffera );
744 HeapFree( GetProcessHeap(), 0, patha );
745 HeapFree( GetProcessHeap(), 0, prefixa );
746 return ret;
750 /***********************************************************************
751 * GetTempFileName16 (KERNEL.97)
753 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
754 LPSTR buffer )
756 char temppath[144];
758 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
759 drive |= DRIVE_GetCurrentDrive() + 'A';
761 if ((drive & TF_FORCEDRIVE) &&
762 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
764 drive &= ~TF_FORCEDRIVE;
765 WARN("invalid drive %d specified\n", drive );
768 if (drive & TF_FORCEDRIVE)
769 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
770 else
771 GetTempPathA( 132, temppath );
772 return (UINT16)FILE_GetTempFileName( temppath, prefix, unique, buffer, TRUE );
775 /***********************************************************************
776 * FILE_DoOpenFile
778 * Implementation of OpenFile16() and OpenFile32().
780 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
781 BOOL win32 )
783 HFILE hFileRet;
784 FILETIME filetime;
785 WORD filedatetime[2];
786 DOS_FULL_NAME full_name;
787 DWORD access, sharing;
788 char *p;
790 if (!ofs) return HFILE_ERROR;
792 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
793 ((mode & 0x3 )==OF_READ)?"OF_READ":
794 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
795 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
796 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
797 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
798 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
799 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
800 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
801 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
802 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
803 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
804 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
805 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
806 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
807 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
808 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
809 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
813 ofs->cBytes = sizeof(OFSTRUCT);
814 ofs->nErrCode = 0;
815 if (mode & OF_REOPEN) name = ofs->szPathName;
817 if (!name) {
818 ERR("called with `name' set to NULL ! Please debug.\n");
819 return HFILE_ERROR;
822 TRACE("%s %04x\n", name, mode );
824 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
825 Are there any cases where getting the path here is wrong?
826 Uwe Bonnes 1997 Apr 2 */
827 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
828 ofs->szPathName, NULL )) goto error;
829 FILE_ConvertOFMode( mode, &access, &sharing );
831 /* OF_PARSE simply fills the structure */
833 if (mode & OF_PARSE)
835 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
836 != DRIVE_REMOVABLE);
837 TRACE("(%s): OF_PARSE, res = '%s'\n",
838 name, ofs->szPathName );
839 return 0;
842 /* OF_CREATE is completely different from all other options, so
843 handle it first */
845 if (mode & OF_CREATE)
847 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
848 sharing, NULL, CREATE_ALWAYS,
849 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
850 goto error;
851 goto success;
854 /* If OF_SEARCH is set, ignore the given path */
856 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
858 /* First try the file name as is */
859 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
860 /* Now remove the path */
861 if (name[0] && (name[1] == ':')) name += 2;
862 if ((p = strrchr( name, '\\' ))) name = p + 1;
863 if ((p = strrchr( name, '/' ))) name = p + 1;
864 if (!name[0]) goto not_found;
867 /* Now look for the file */
869 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
871 found:
872 TRACE("found %s = %s\n",
873 full_name.long_name, full_name.short_name );
874 lstrcpynA( ofs->szPathName, full_name.short_name,
875 sizeof(ofs->szPathName) );
877 if (mode & OF_SHARE_EXCLUSIVE)
878 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
879 on the file <tempdir>/_ins0432._mp to determine how
880 far installation has proceeded.
881 _ins0432._mp is an executable and while running the
882 application expects the open with OF_SHARE_ to fail*/
883 /* Probable FIXME:
884 As our loader closes the files after loading the executable,
885 we can't find the running executable with FILE_InUse.
886 Perhaps the loader should keep the file open.
887 Recheck against how Win handles that case */
889 char *last = strrchr(full_name.long_name,'/');
890 if (!last)
891 last = full_name.long_name - 1;
892 if (GetModuleHandle16(last+1))
894 TRACE("Denying shared open for %s\n",full_name.long_name);
895 return HFILE_ERROR;
899 if (mode & OF_DELETE)
901 if (unlink( full_name.long_name ) == -1) goto not_found;
902 TRACE("(%s): OF_DELETE return = OK\n", name);
903 return 1;
906 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
907 NULL, OPEN_EXISTING, 0, -1 );
908 if (hFileRet == HFILE_ERROR) goto not_found;
910 GetFileTime( hFileRet, NULL, NULL, &filetime );
911 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
912 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
914 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
916 CloseHandle( hFileRet );
917 WARN("(%s): OF_VERIFY failed\n", name );
918 /* FIXME: what error here? */
919 SetLastError( ERROR_FILE_NOT_FOUND );
920 goto error;
923 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
925 success: /* We get here if the open was successful */
926 TRACE("(%s): OK, return = %d\n", name, hFileRet );
927 if (win32)
929 if (mode & OF_EXIST) /* Return the handle, but close it first */
930 CloseHandle( hFileRet );
932 else
934 hFileRet = FILE_AllocDosHandle( hFileRet );
935 if (hFileRet == HFILE_ERROR16) goto error;
936 if (mode & OF_EXIST) /* Return the handle, but close it first */
937 _lclose16( hFileRet );
939 return hFileRet;
941 not_found: /* We get here if the file does not exist */
942 WARN("'%s' not found\n", name );
943 SetLastError( ERROR_FILE_NOT_FOUND );
944 /* fall through */
946 error: /* We get here if there was an error opening the file */
947 ofs->nErrCode = GetLastError();
948 WARN("(%s): return = HFILE_ERROR error= %d\n",
949 name,ofs->nErrCode );
950 return HFILE_ERROR;
954 /***********************************************************************
955 * OpenFile16 (KERNEL.74)
957 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
959 return FILE_DoOpenFile( name, ofs, mode, FALSE );
963 /***********************************************************************
964 * OpenFile32 (KERNEL32.396)
966 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
968 return FILE_DoOpenFile( name, ofs, mode, TRUE );
972 /***********************************************************************
973 * FILE_InitProcessDosHandles
975 * Allocates the default DOS handles for a process. Called either by
976 * AllocDosHandle below or by the DOSVM stuff.
978 BOOL FILE_InitProcessDosHandles( void ) {
979 HANDLE *ptr;
981 if (!(ptr = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY,
982 sizeof(*ptr) * DOS_TABLE_SIZE )))
983 return FALSE;
984 PROCESS_Current()->dos_handles = ptr;
985 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
986 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
987 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
988 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
989 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
990 return TRUE;
993 /***********************************************************************
994 * FILE_AllocDosHandle
996 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
997 * longer valid after this function (even on failure).
999 HFILE16 FILE_AllocDosHandle( HANDLE handle )
1001 int i;
1002 HANDLE *ptr = PROCESS_Current()->dos_handles;
1004 if (!handle || (handle == INVALID_HANDLE_VALUE))
1005 return INVALID_HANDLE_VALUE16;
1007 if (!ptr) {
1008 if (!FILE_InitProcessDosHandles())
1009 goto error;
1010 ptr = PROCESS_Current()->dos_handles;
1013 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
1014 if (!*ptr)
1016 *ptr = handle;
1017 TRACE("Got %d for h32 %d\n", i, handle );
1018 return i;
1020 error:
1021 CloseHandle( handle );
1022 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1023 return INVALID_HANDLE_VALUE16;
1027 /***********************************************************************
1028 * FILE_GetHandle32
1030 * Return the Win32 handle for a DOS handle.
1032 HANDLE FILE_GetHandle( HFILE16 hfile )
1034 HANDLE *table = PROCESS_Current()->dos_handles;
1035 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1037 SetLastError( ERROR_INVALID_HANDLE );
1038 return INVALID_HANDLE_VALUE;
1040 return table[hfile];
1044 /***********************************************************************
1045 * FILE_Dup2
1047 * dup2() function for DOS handles.
1049 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1051 HANDLE *table = PROCESS_Current()->dos_handles;
1052 HANDLE new_handle;
1054 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1055 !table || !table[hFile1])
1057 SetLastError( ERROR_INVALID_HANDLE );
1058 return HFILE_ERROR16;
1060 if (hFile2 < 5)
1062 FIXME("stdio handle closed, need proper conversion\n" );
1063 SetLastError( ERROR_INVALID_HANDLE );
1064 return HFILE_ERROR16;
1066 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1067 GetCurrentProcess(), &new_handle,
1068 0, FALSE, DUPLICATE_SAME_ACCESS ))
1069 return HFILE_ERROR16;
1070 if (table[hFile2]) CloseHandle( table[hFile2] );
1071 table[hFile2] = new_handle;
1072 return hFile2;
1076 /***********************************************************************
1077 * _lclose16 (KERNEL.81)
1079 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1081 HANDLE *table = PROCESS_Current()->dos_handles;
1083 if (hFile < 5)
1085 FIXME("stdio handle closed, need proper conversion\n" );
1086 SetLastError( ERROR_INVALID_HANDLE );
1087 return HFILE_ERROR16;
1089 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1091 SetLastError( ERROR_INVALID_HANDLE );
1092 return HFILE_ERROR16;
1094 TRACE("%d (handle32=%d)\n", hFile, table[hFile] );
1095 CloseHandle( table[hFile] );
1096 table[hFile] = 0;
1097 return 0;
1101 /***********************************************************************
1102 * _lclose32 (KERNEL32.592)
1104 HFILE WINAPI _lclose( HFILE hFile )
1106 TRACE("handle %d\n", hFile );
1107 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1110 /***********************************************************************
1111 * GetOverlappedResult (KERNEL32.360)
1113 BOOL WINAPI GetOverlappedResult(HANDLE hFile,LPOVERLAPPED lpOverlapped,
1114 LPDWORD lpNumberOfBytesTransferred,
1115 BOOL bWait)
1117 /* Since all i/o is currently synchronuos,
1118 * return true, assuming ReadFile/WriteFile
1119 * have completed the operation */
1120 FIXME("NO Asynch I/O, assuming Read/Write succeeded\n" );
1121 return TRUE;
1124 /***********************************************************************
1125 * ReadFile (KERNEL32.428)
1127 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1128 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1130 struct get_read_fd_request *req = get_req_buffer();
1131 int unix_handle, result;
1133 TRACE("%d %p %ld\n", hFile, buffer, bytesToRead );
1135 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1136 if (!bytesToRead) return TRUE;
1138 if ( overlapped ) {
1139 SetLastError ( ERROR_INVALID_PARAMETER );
1140 return FALSE;
1143 req->handle = hFile;
1144 server_call_fd( REQ_GET_READ_FD, -1, &unix_handle );
1145 if (unix_handle == -1) return FALSE;
1146 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1148 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1149 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1150 FILE_SetDosError();
1151 break;
1153 close( unix_handle );
1154 if (result == -1) return FALSE;
1155 if (bytesRead) *bytesRead = result;
1156 return TRUE;
1160 /***********************************************************************
1161 * WriteFile (KERNEL32.578)
1163 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1164 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1166 struct get_write_fd_request *req = get_req_buffer();
1167 int unix_handle, result;
1169 TRACE("%d %p %ld\n", hFile, buffer, bytesToWrite );
1171 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1172 if (!bytesToWrite) return TRUE;
1174 if ( overlapped ) {
1175 SetLastError ( ERROR_INVALID_PARAMETER );
1176 return FALSE;
1179 req->handle = hFile;
1180 server_call_fd( REQ_GET_WRITE_FD, -1, &unix_handle );
1181 if (unix_handle == -1) return FALSE;
1182 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1184 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1185 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1186 if (errno == ENOSPC)
1187 SetLastError( ERROR_DISK_FULL );
1188 else
1189 FILE_SetDosError();
1190 break;
1192 close( unix_handle );
1193 if (result == -1) return FALSE;
1194 if (bytesWritten) *bytesWritten = result;
1195 return TRUE;
1199 /***********************************************************************
1200 * WIN16_hread
1202 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1204 LONG maxlen;
1206 TRACE("%d %08lx %ld\n",
1207 hFile, (DWORD)buffer, count );
1209 /* Some programs pass a count larger than the allocated buffer */
1210 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1211 if (count > maxlen) count = maxlen;
1212 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1216 /***********************************************************************
1217 * WIN16_lread
1219 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1221 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1225 /***********************************************************************
1226 * _lread32 (KERNEL32.596)
1228 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1230 DWORD result;
1231 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1232 return result;
1236 /***********************************************************************
1237 * _lread16 (KERNEL.82)
1239 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1241 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1245 /***********************************************************************
1246 * _lcreat16 (KERNEL.83)
1248 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1250 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1254 /***********************************************************************
1255 * _lcreat (KERNEL32.593)
1257 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1259 /* Mask off all flags not explicitly allowed by the doc */
1260 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1261 TRACE("%s %02x\n", path, attr );
1262 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1263 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1264 CREATE_ALWAYS, attr, -1 );
1268 /***********************************************************************
1269 * SetFilePointer (KERNEL32.492)
1271 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword,
1272 DWORD method )
1274 struct set_file_pointer_request *req = get_req_buffer();
1276 if (highword && *highword)
1278 FIXME("64-bit offsets not supported yet\n");
1279 SetLastError( ERROR_INVALID_PARAMETER );
1280 return 0xffffffff;
1282 TRACE("handle %d offset %ld origin %ld\n",
1283 hFile, distance, method );
1285 req->handle = hFile;
1286 req->low = distance;
1287 req->high = highword ? *highword : 0;
1288 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1289 req->whence = method;
1290 SetLastError( 0 );
1291 if (server_call( REQ_SET_FILE_POINTER )) return 0xffffffff;
1292 if (highword) *highword = req->new_high;
1293 return req->new_low;
1297 /***********************************************************************
1298 * _llseek16 (KERNEL.84)
1300 * FIXME:
1301 * Seeking before the start of the file should be allowed for _llseek16,
1302 * but cause subsequent I/O operations to fail (cf. interrupt list)
1305 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1307 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1311 /***********************************************************************
1312 * _llseek32 (KERNEL32.594)
1314 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1316 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1320 /***********************************************************************
1321 * _lopen16 (KERNEL.85)
1323 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1325 return FILE_AllocDosHandle( _lopen( path, mode ) );
1329 /***********************************************************************
1330 * _lopen32 (KERNEL32.595)
1332 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1334 DWORD access, sharing;
1336 TRACE("('%s',%04x)\n", path, mode );
1337 FILE_ConvertOFMode( mode, &access, &sharing );
1338 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1342 /***********************************************************************
1343 * _lwrite16 (KERNEL.86)
1345 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1347 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1350 /***********************************************************************
1351 * _lwrite32 (KERNEL32.761)
1353 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1355 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1359 /***********************************************************************
1360 * _hread16 (KERNEL.349)
1362 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1364 return _lread( FILE_GetHandle(hFile), buffer, count );
1368 /***********************************************************************
1369 * _hread32 (KERNEL32.590)
1371 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1373 return _lread( hFile, buffer, count );
1377 /***********************************************************************
1378 * _hwrite16 (KERNEL.350)
1380 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1382 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1386 /***********************************************************************
1387 * _hwrite32 (KERNEL32.591)
1389 * experimentation yields that _lwrite:
1390 * o truncates the file at the current position with
1391 * a 0 len write
1392 * o returns 0 on a 0 length write
1393 * o works with console handles
1396 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1398 DWORD result;
1400 TRACE("%d %p %ld\n", handle, buffer, count );
1402 if (!count)
1404 /* Expand or truncate at current position */
1405 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1406 return 0;
1408 if (!WriteFile( handle, buffer, count, &result, NULL ))
1409 return HFILE_ERROR;
1410 return result;
1414 /***********************************************************************
1415 * SetHandleCount16 (KERNEL.199)
1417 UINT16 WINAPI SetHandleCount16( UINT16 count )
1419 HGLOBAL16 hPDB = GetCurrentPDB16();
1420 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1421 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1423 TRACE("(%d)\n", count );
1425 if (count < 20) count = 20; /* No point in going below 20 */
1426 else if (count > 254) count = 254;
1428 if (count == 20)
1430 if (pdb->nbFiles > 20)
1432 memcpy( pdb->fileHandles, files, 20 );
1433 GlobalFree16( pdb->hFileHandles );
1434 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1435 GlobalHandleToSel16( hPDB ) );
1436 pdb->hFileHandles = 0;
1437 pdb->nbFiles = 20;
1440 else /* More than 20, need a new file handles table */
1442 BYTE *newfiles;
1443 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1444 if (!newhandle)
1446 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1447 return pdb->nbFiles;
1449 newfiles = (BYTE *)GlobalLock16( newhandle );
1451 if (count > pdb->nbFiles)
1453 memcpy( newfiles, files, pdb->nbFiles );
1454 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1456 else memcpy( newfiles, files, count );
1457 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1458 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1459 pdb->hFileHandles = newhandle;
1460 pdb->nbFiles = count;
1462 return pdb->nbFiles;
1466 /*************************************************************************
1467 * SetHandleCount32 (KERNEL32.494)
1469 UINT WINAPI SetHandleCount( UINT count )
1471 return MIN( 256, count );
1475 /***********************************************************************
1476 * FlushFileBuffers (KERNEL32.133)
1478 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
1480 struct flush_file_request *req = get_req_buffer();
1481 req->handle = hFile;
1482 return !server_call( REQ_FLUSH_FILE );
1486 /**************************************************************************
1487 * SetEndOfFile (KERNEL32.483)
1489 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1491 struct truncate_file_request *req = get_req_buffer();
1492 req->handle = hFile;
1493 return !server_call( REQ_TRUNCATE_FILE );
1497 /***********************************************************************
1498 * DeleteFile16 (KERNEL.146)
1500 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1502 return DeleteFileA( path );
1506 /***********************************************************************
1507 * DeleteFile32A (KERNEL32.71)
1509 BOOL WINAPI DeleteFileA( LPCSTR path )
1511 DOS_FULL_NAME full_name;
1513 TRACE("'%s'\n", path );
1515 if (!*path)
1517 ERR("Empty path passed\n");
1518 return FALSE;
1520 if (DOSFS_GetDevice( path ))
1522 WARN("cannot remove DOS device '%s'!\n", path);
1523 SetLastError( ERROR_FILE_NOT_FOUND );
1524 return FALSE;
1527 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1528 if (unlink( full_name.long_name ) == -1)
1530 FILE_SetDosError();
1531 return FALSE;
1533 return TRUE;
1537 /***********************************************************************
1538 * DeleteFile32W (KERNEL32.72)
1540 BOOL WINAPI DeleteFileW( LPCWSTR path )
1542 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1543 BOOL ret = DeleteFileA( xpath );
1544 HeapFree( GetProcessHeap(), 0, xpath );
1545 return ret;
1549 /***********************************************************************
1550 * FILE_dommap
1552 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1553 DWORD size_high, DWORD size_low,
1554 DWORD offset_high, DWORD offset_low,
1555 int prot, int flags )
1557 int fd = -1;
1558 int pos;
1559 LPVOID ret;
1561 if (size_high || offset_high)
1562 FIXME("offsets larger than 4Gb not supported\n");
1564 if (unix_handle == -1)
1566 #ifdef MAP_ANON
1567 flags |= MAP_ANON;
1568 #else
1569 static int fdzero = -1;
1571 if (fdzero == -1)
1573 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1575 perror( "/dev/zero: open" );
1576 exit(1);
1579 fd = fdzero;
1580 #endif /* MAP_ANON */
1581 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1582 #ifdef MAP_SHARED
1583 flags &= ~MAP_SHARED;
1584 #endif
1585 #ifdef MAP_PRIVATE
1586 flags |= MAP_PRIVATE;
1587 #endif
1589 else fd = unix_handle;
1591 if ((ret = mmap( start, size_low, prot,
1592 flags, fd, offset_low )) != (LPVOID)-1)
1593 return ret;
1595 /* mmap() failed; if this is because the file offset is not */
1596 /* page-aligned (EINVAL), or because the underlying filesystem */
1597 /* does not support mmap() (ENOEXEC), we do it by hand. */
1599 if (unix_handle == -1) return ret;
1600 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1601 if (prot & PROT_WRITE)
1603 /* We cannot fake shared write mappings */
1604 #ifdef MAP_SHARED
1605 if (flags & MAP_SHARED) return ret;
1606 #endif
1607 #ifdef MAP_PRIVATE
1608 if (!(flags & MAP_PRIVATE)) return ret;
1609 #endif
1611 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1612 /* Reserve the memory with an anonymous mmap */
1613 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1614 PROT_READ | PROT_WRITE, flags );
1615 if (ret == (LPVOID)-1) return ret;
1616 /* Now read in the file */
1617 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1619 FILE_munmap( ret, size_high, size_low );
1620 return (LPVOID)-1;
1622 read( fd, ret, size_low );
1623 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1624 mprotect( ret, size_low, prot ); /* Set the right protection */
1625 return ret;
1629 /***********************************************************************
1630 * FILE_munmap
1632 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1634 if (size_high)
1635 FIXME("offsets larger than 4Gb not supported\n");
1636 return munmap( start, size_low );
1640 /***********************************************************************
1641 * GetFileType (KERNEL32.222)
1643 DWORD WINAPI GetFileType( HANDLE hFile )
1645 struct get_file_info_request *req = get_req_buffer();
1646 req->handle = hFile;
1647 if (server_call( REQ_GET_FILE_INFO )) return FILE_TYPE_UNKNOWN;
1648 return req->type;
1652 /**************************************************************************
1653 * MoveFileExA (KERNEL32.???)
1655 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1657 DOS_FULL_NAME full_name1, full_name2;
1659 TRACE("(%s,%s,%04lx)\n", fn1, fn2, flag);
1661 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1663 if (fn2) /* !fn2 means delete fn1 */
1665 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1667 /* target exists, check if we may overwrite */
1668 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1670 /* FIXME: Use right error code */
1671 SetLastError( ERROR_ACCESS_DENIED );
1672 return FALSE;
1675 else if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1677 /* Source name and target path are valid */
1679 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1681 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1682 Perhaps we should queue these command and execute it
1683 when exiting... What about using on_exit(2)
1685 FIXME("Please move existing file '%s' to file '%s' when Wine has finished\n",
1686 full_name1.long_name, full_name2.long_name);
1687 return TRUE;
1690 if (full_name1.drive != full_name2.drive)
1692 /* use copy, if allowed */
1693 if (!(flag & MOVEFILE_COPY_ALLOWED))
1695 /* FIXME: Use right error code */
1696 SetLastError( ERROR_FILE_EXISTS );
1697 return FALSE;
1699 return CopyFileA( fn1, fn2, !(flag & MOVEFILE_REPLACE_EXISTING) );
1701 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1703 FILE_SetDosError();
1704 return FALSE;
1706 return TRUE;
1708 else /* fn2 == NULL means delete source */
1710 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1712 if (flag & MOVEFILE_COPY_ALLOWED) {
1713 WARN("Illegal flag\n");
1714 SetLastError( ERROR_GEN_FAILURE );
1715 return FALSE;
1717 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1718 Perhaps we should queue these command and execute it
1719 when exiting... What about using on_exit(2)
1721 FIXME("Please delete file '%s' when Wine has finished\n",
1722 full_name1.long_name);
1723 return TRUE;
1726 if (unlink( full_name1.long_name ) == -1)
1728 FILE_SetDosError();
1729 return FALSE;
1731 return TRUE; /* successfully deleted */
1735 /**************************************************************************
1736 * MoveFileEx32W (KERNEL32.???)
1738 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1740 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1741 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1742 BOOL res = MoveFileExA( afn1, afn2, flag );
1743 HeapFree( GetProcessHeap(), 0, afn1 );
1744 HeapFree( GetProcessHeap(), 0, afn2 );
1745 return res;
1749 /**************************************************************************
1750 * MoveFile32A (KERNEL32.387)
1752 * Move file or directory
1754 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1756 DOS_FULL_NAME full_name1, full_name2;
1757 struct stat fstat;
1759 TRACE("(%s,%s)\n", fn1, fn2 );
1761 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1762 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1763 /* The new name must not already exist */
1764 return FALSE;
1765 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1767 if (full_name1.drive == full_name2.drive) /* move */
1768 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1770 FILE_SetDosError();
1771 return FALSE;
1773 else return TRUE;
1774 else /*copy */ {
1775 if (stat( full_name1.long_name, &fstat ))
1777 WARN("Invalid source file %s\n",
1778 full_name1.long_name);
1779 FILE_SetDosError();
1780 return FALSE;
1782 if (S_ISDIR(fstat.st_mode)) {
1783 /* No Move for directories across file systems */
1784 /* FIXME: Use right error code */
1785 SetLastError( ERROR_GEN_FAILURE );
1786 return FALSE;
1788 else
1789 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1794 /**************************************************************************
1795 * MoveFile32W (KERNEL32.390)
1797 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1799 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1800 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1801 BOOL res = MoveFileA( afn1, afn2 );
1802 HeapFree( GetProcessHeap(), 0, afn1 );
1803 HeapFree( GetProcessHeap(), 0, afn2 );
1804 return res;
1808 /**************************************************************************
1809 * CopyFile32A (KERNEL32.36)
1811 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1813 HFILE h1, h2;
1814 BY_HANDLE_FILE_INFORMATION info;
1815 UINT count;
1816 BOOL ret = FALSE;
1817 int mode;
1818 char buffer[2048];
1820 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1821 if (!GetFileInformationByHandle( h1, &info ))
1823 CloseHandle( h1 );
1824 return FALSE;
1826 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1827 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1828 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1829 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1831 CloseHandle( h1 );
1832 return FALSE;
1834 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1836 char *p = buffer;
1837 while (count > 0)
1839 INT res = _lwrite( h2, p, count );
1840 if (res <= 0) goto done;
1841 p += res;
1842 count -= res;
1845 ret = TRUE;
1846 done:
1847 CloseHandle( h1 );
1848 CloseHandle( h2 );
1849 return ret;
1853 /**************************************************************************
1854 * CopyFile32W (KERNEL32.37)
1856 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1858 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1859 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1860 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1861 HeapFree( GetProcessHeap(), 0, sourceA );
1862 HeapFree( GetProcessHeap(), 0, destA );
1863 return ret;
1867 /**************************************************************************
1868 * CopyFileEx32A (KERNEL32.858)
1870 * This implementation ignores most of the extra parameters passed-in into
1871 * the "ex" version of the method and calls the CopyFile method.
1872 * It will have to be fixed eventually.
1874 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1875 LPCSTR destFilename,
1876 LPPROGRESS_ROUTINE progressRoutine,
1877 LPVOID appData,
1878 LPBOOL cancelFlagPointer,
1879 DWORD copyFlags)
1881 BOOL failIfExists = FALSE;
1884 * Interpret the only flag that CopyFile can interpret.
1886 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1888 failIfExists = TRUE;
1891 return CopyFileA(sourceFilename, destFilename, failIfExists);
1894 /**************************************************************************
1895 * CopyFileEx32W (KERNEL32.859)
1897 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1898 LPCWSTR destFilename,
1899 LPPROGRESS_ROUTINE progressRoutine,
1900 LPVOID appData,
1901 LPBOOL cancelFlagPointer,
1902 DWORD copyFlags)
1904 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1905 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1907 BOOL ret = CopyFileExA(sourceA,
1908 destA,
1909 progressRoutine,
1910 appData,
1911 cancelFlagPointer,
1912 copyFlags);
1914 HeapFree( GetProcessHeap(), 0, sourceA );
1915 HeapFree( GetProcessHeap(), 0, destA );
1917 return ret;
1921 /***********************************************************************
1922 * SetFileTime (KERNEL32.650)
1924 BOOL WINAPI SetFileTime( HANDLE hFile,
1925 const FILETIME *lpCreationTime,
1926 const FILETIME *lpLastAccessTime,
1927 const FILETIME *lpLastWriteTime )
1929 struct set_file_time_request *req = get_req_buffer();
1931 req->handle = hFile;
1932 if (lpLastAccessTime)
1933 req->access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1934 else
1935 req->access_time = 0; /* FIXME */
1936 if (lpLastWriteTime)
1937 req->write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1938 else
1939 req->write_time = 0; /* FIXME */
1940 return !server_call( REQ_SET_FILE_TIME );
1944 /**************************************************************************
1945 * LockFile (KERNEL32.511)
1947 BOOL WINAPI LockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1948 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1950 struct lock_file_request *req = get_req_buffer();
1952 req->handle = hFile;
1953 req->offset_low = dwFileOffsetLow;
1954 req->offset_high = dwFileOffsetHigh;
1955 req->count_low = nNumberOfBytesToLockLow;
1956 req->count_high = nNumberOfBytesToLockHigh;
1957 return !server_call( REQ_LOCK_FILE );
1960 /**************************************************************************
1961 * LockFileEx [KERNEL32.512]
1963 * Locks a byte range within an open file for shared or exclusive access.
1965 * RETURNS
1966 * success: TRUE
1967 * failure: FALSE
1968 * NOTES
1970 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1972 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1973 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1974 LPOVERLAPPED pOverlapped )
1976 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1977 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
1978 pOverlapped);
1979 if (reserved == 0)
1980 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1981 else
1983 ERR("reserved == %ld: Supposed to be 0??\n", reserved);
1984 SetLastError(ERROR_INVALID_PARAMETER);
1987 return FALSE;
1991 /**************************************************************************
1992 * UnlockFile (KERNEL32.703)
1994 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1995 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1997 struct unlock_file_request *req = get_req_buffer();
1999 req->handle = hFile;
2000 req->offset_low = dwFileOffsetLow;
2001 req->offset_high = dwFileOffsetHigh;
2002 req->count_low = nNumberOfBytesToUnlockLow;
2003 req->count_high = nNumberOfBytesToUnlockHigh;
2004 return !server_call( REQ_UNLOCK_FILE );
2008 /**************************************************************************
2009 * UnlockFileEx (KERNEL32.705)
2011 BOOL WINAPI UnlockFileEx(
2012 HFILE hFile,
2013 DWORD dwReserved,
2014 DWORD nNumberOfBytesToUnlockLow,
2015 DWORD nNumberOfBytesToUnlockHigh,
2016 LPOVERLAPPED lpOverlapped
2019 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2020 hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh,
2021 lpOverlapped);
2022 if (dwReserved == 0)
2023 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2024 else
2026 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved);
2027 SetLastError(ERROR_INVALID_PARAMETER);
2030 return FALSE;
2034 #if 0
2036 struct DOS_FILE_LOCK {
2037 struct DOS_FILE_LOCK * next;
2038 DWORD base;
2039 DWORD len;
2040 DWORD processId;
2041 FILE_OBJECT * dos_file;
2042 /* char * unix_name;*/
2045 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2047 static DOS_FILE_LOCK *locks = NULL;
2048 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2051 /* Locks need to be mirrored because unix file locking is based
2052 * on the pid. Inside of wine there can be multiple WINE processes
2053 * that share the same unix pid.
2054 * Read's and writes should check these locks also - not sure
2055 * how critical that is at this point (FIXME).
2058 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2060 DOS_FILE_LOCK *curr;
2061 DWORD processId;
2063 processId = GetCurrentProcessId();
2065 /* check if lock overlaps a current lock for the same file */
2066 #if 0
2067 for (curr = locks; curr; curr = curr->next) {
2068 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2069 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2070 return TRUE;/* region is identic */
2071 if ((f->l_start < (curr->base + curr->len)) &&
2072 ((f->l_start + f->l_len) > curr->base)) {
2073 /* region overlaps */
2074 return FALSE;
2078 #endif
2080 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2081 curr->processId = GetCurrentProcessId();
2082 curr->base = f->l_start;
2083 curr->len = f->l_len;
2084 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
2085 curr->next = locks;
2086 curr->dos_file = file;
2087 locks = curr;
2088 return TRUE;
2091 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2093 DWORD processId;
2094 DOS_FILE_LOCK **curr;
2095 DOS_FILE_LOCK *rem;
2097 processId = GetCurrentProcessId();
2098 curr = &locks;
2099 while (*curr) {
2100 if ((*curr)->dos_file == file) {
2101 rem = *curr;
2102 *curr = (*curr)->next;
2103 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2104 HeapFree( SystemHeap, 0, rem );
2106 else
2107 curr = &(*curr)->next;
2111 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2113 DWORD processId;
2114 DOS_FILE_LOCK **curr;
2115 DOS_FILE_LOCK *rem;
2117 processId = GetCurrentProcessId();
2118 for (curr = &locks; *curr; curr = &(*curr)->next) {
2119 if ((*curr)->processId == processId &&
2120 (*curr)->dos_file == file &&
2121 (*curr)->base == f->l_start &&
2122 (*curr)->len == f->l_len) {
2123 /* this is the same lock */
2124 rem = *curr;
2125 *curr = (*curr)->next;
2126 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2127 HeapFree( SystemHeap, 0, rem );
2128 return TRUE;
2131 /* no matching lock found */
2132 return FALSE;
2136 /**************************************************************************
2137 * LockFile (KERNEL32.511)
2139 BOOL WINAPI LockFile(
2140 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2141 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2143 struct flock f;
2144 FILE_OBJECT *file;
2146 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2147 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2148 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2150 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2151 FIXME("Unimplemented bytes > 32bits\n");
2152 return FALSE;
2155 f.l_start = dwFileOffsetLow;
2156 f.l_len = nNumberOfBytesToLockLow;
2157 f.l_whence = SEEK_SET;
2158 f.l_pid = 0;
2159 f.l_type = F_WRLCK;
2161 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2163 /* shadow locks internally */
2164 if (!DOS_AddLock(file, &f)) {
2165 SetLastError( ERROR_LOCK_VIOLATION );
2166 return FALSE;
2169 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2170 #ifdef USE_UNIX_LOCKS
2171 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2172 if (errno == EACCES || errno == EAGAIN) {
2173 SetLastError( ERROR_LOCK_VIOLATION );
2175 else {
2176 FILE_SetDosError();
2178 /* remove our internal copy of the lock */
2179 DOS_RemoveLock(file, &f);
2180 return FALSE;
2182 #endif
2183 return TRUE;
2187 /**************************************************************************
2188 * UnlockFile (KERNEL32.703)
2190 BOOL WINAPI UnlockFile(
2191 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2192 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2194 FILE_OBJECT *file;
2195 struct flock f;
2197 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2198 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2199 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2201 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2202 WARN("Unimplemented bytes > 32bits\n");
2203 return FALSE;
2206 f.l_start = dwFileOffsetLow;
2207 f.l_len = nNumberOfBytesToUnlockLow;
2208 f.l_whence = SEEK_SET;
2209 f.l_pid = 0;
2210 f.l_type = F_UNLCK;
2212 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2214 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2216 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2217 #ifdef USE_UNIX_LOCKS
2218 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2219 FILE_SetDosError();
2220 return FALSE;
2222 #endif
2223 return TRUE;
2225 #endif
2227 /**************************************************************************
2228 * GetFileAttributesEx32A [KERNEL32.874]
2230 BOOL WINAPI GetFileAttributesExA(
2231 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2232 LPVOID lpFileInformation)
2234 DOS_FULL_NAME full_name;
2235 BY_HANDLE_FILE_INFORMATION info;
2237 if (lpFileName == NULL) return FALSE;
2238 if (lpFileInformation == NULL) return FALSE;
2240 if (fInfoLevelId == GetFileExInfoStandard) {
2241 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2242 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2243 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2244 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2246 lpFad->dwFileAttributes = info.dwFileAttributes;
2247 lpFad->ftCreationTime = info.ftCreationTime;
2248 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2249 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2250 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2251 lpFad->nFileSizeLow = info.nFileSizeLow;
2253 else {
2254 FIXME("invalid info level %d!\n", fInfoLevelId);
2255 return FALSE;
2258 return TRUE;
2262 /**************************************************************************
2263 * GetFileAttributesEx32W [KERNEL32.875]
2265 BOOL WINAPI GetFileAttributesExW(
2266 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2267 LPVOID lpFileInformation)
2269 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2270 BOOL res =
2271 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2272 HeapFree( GetProcessHeap(), 0, nameA );
2273 return res;