Added stub for WIN32S16.EXP1 (most likely LoadPeResource16).
[wine/dcerpc.git] / files / file.c
blob379676507ee4bab3ade9efb100f5cd56ccc8e025
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 HFILE 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 HFILE 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 /* Open a console for CONIN$ or CONOUT$ */
453 if (!lstrcmpiA(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
454 if (!lstrcmpiA(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
456 if (DOSFS_GetDevice( filename ))
458 HFILE ret;
460 TRACE("opening device '%s'\n", filename );
462 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
463 return ret;
465 /* Do not silence this please. It is a critical error. -MM */
466 ERR("Couldn't open device '%s'!\n",filename);
467 SetLastError( ERROR_FILE_NOT_FOUND );
468 return HFILE_ERROR;
471 /* check for filename, don't check for last entry if creating */
472 if (!DOSFS_GetFullName( filename,
473 (creation == OPEN_EXISTING) || (creation == TRUNCATE_EXISTING), &full_name ))
474 return HFILE_ERROR;
476 return FILE_CreateFile( full_name.long_name, access, sharing,
477 sa, creation, attributes, template );
482 /*************************************************************************
483 * CreateFile32W (KERNEL32.48)
485 HFILE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
486 LPSECURITY_ATTRIBUTES sa, DWORD creation,
487 DWORD attributes, HANDLE template)
489 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
490 HFILE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
491 HeapFree( GetProcessHeap(), 0, afn );
492 return res;
496 /***********************************************************************
497 * FILE_FillInfo
499 * Fill a file information from a struct stat.
501 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
503 if (S_ISDIR(st->st_mode))
504 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
505 else
506 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
507 if (!(st->st_mode & S_IWUSR))
508 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
510 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
511 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
512 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
514 info->dwVolumeSerialNumber = 0; /* FIXME */
515 info->nFileSizeHigh = 0;
516 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
517 info->nNumberOfLinks = st->st_nlink;
518 info->nFileIndexHigh = 0;
519 info->nFileIndexLow = st->st_ino;
523 /***********************************************************************
524 * FILE_Stat
526 * Stat a Unix path name. Return TRUE if OK.
528 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
530 struct stat st;
532 if (!unixName || !info) return FALSE;
534 if (stat( unixName, &st ) == -1)
536 FILE_SetDosError();
537 return FALSE;
539 FILE_FillInfo( &st, info );
540 return TRUE;
544 /***********************************************************************
545 * GetFileInformationByHandle (KERNEL32.219)
547 DWORD WINAPI GetFileInformationByHandle( HFILE hFile,
548 BY_HANDLE_FILE_INFORMATION *info )
550 struct get_file_info_request *req = get_req_buffer();
552 if (!info) return 0;
553 req->handle = hFile;
554 if (server_call( REQ_GET_FILE_INFO )) return 0;
555 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftCreationTime, 0 );
556 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftLastWriteTime, 0 );
557 DOSFS_UnixTimeToFileTime( req->access_time, &info->ftLastAccessTime, 0 );
558 info->dwFileAttributes = req->attr;
559 info->dwVolumeSerialNumber = req->serial;
560 info->nFileSizeHigh = req->size_high;
561 info->nFileSizeLow = req->size_low;
562 info->nNumberOfLinks = req->links;
563 info->nFileIndexHigh = req->index_high;
564 info->nFileIndexLow = req->index_low;
565 return 1;
569 /**************************************************************************
570 * GetFileAttributes16 (KERNEL.420)
572 DWORD WINAPI GetFileAttributes16( LPCSTR name )
574 return GetFileAttributesA( name );
578 /**************************************************************************
579 * GetFileAttributes32A (KERNEL32.217)
581 DWORD WINAPI GetFileAttributesA( LPCSTR name )
583 DOS_FULL_NAME full_name;
584 BY_HANDLE_FILE_INFORMATION info;
586 if (name == NULL || *name=='\0') return -1;
588 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
589 if (!FILE_Stat( full_name.long_name, &info )) return -1;
590 return info.dwFileAttributes;
594 /**************************************************************************
595 * GetFileAttributes32W (KERNEL32.218)
597 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
599 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
600 DWORD res = GetFileAttributesA( nameA );
601 HeapFree( GetProcessHeap(), 0, nameA );
602 return res;
606 /***********************************************************************
607 * GetFileSize (KERNEL32.220)
609 DWORD WINAPI GetFileSize( HFILE hFile, LPDWORD filesizehigh )
611 BY_HANDLE_FILE_INFORMATION info;
612 if (!GetFileInformationByHandle( hFile, &info )) return 0;
613 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
614 return info.nFileSizeLow;
618 /***********************************************************************
619 * GetFileTime (KERNEL32.221)
621 BOOL WINAPI GetFileTime( HFILE hFile, FILETIME *lpCreationTime,
622 FILETIME *lpLastAccessTime,
623 FILETIME *lpLastWriteTime )
625 BY_HANDLE_FILE_INFORMATION info;
626 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
627 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
628 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
629 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
630 return TRUE;
633 /***********************************************************************
634 * CompareFileTime (KERNEL32.28)
636 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
638 if (!x || !y) return -1;
640 if (x->dwHighDateTime > y->dwHighDateTime)
641 return 1;
642 if (x->dwHighDateTime < y->dwHighDateTime)
643 return -1;
644 if (x->dwLowDateTime > y->dwLowDateTime)
645 return 1;
646 if (x->dwLowDateTime < y->dwLowDateTime)
647 return -1;
648 return 0;
652 /***********************************************************************
653 * GetTempFileName16 (KERNEL.97)
655 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
656 LPSTR buffer )
658 char temppath[144];
660 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
661 drive |= DRIVE_GetCurrentDrive() + 'A';
663 if ((drive & TF_FORCEDRIVE) &&
664 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
666 drive &= ~TF_FORCEDRIVE;
667 WARN("invalid drive %d specified\n", drive );
670 if (drive & TF_FORCEDRIVE)
671 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
672 else
673 GetTempPathA( 132, temppath );
674 return (UINT16)GetTempFileNameA( temppath, prefix, unique, buffer );
678 /***********************************************************************
679 * GetTempFileName32A (KERNEL32.290)
681 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
682 LPSTR buffer)
684 static UINT unique_temp;
685 DOS_FULL_NAME full_name;
686 int i;
687 LPSTR p;
688 UINT num;
690 if ( !path || !prefix || !buffer ) return 0;
692 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
693 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
695 strcpy( buffer, path );
696 p = buffer + strlen(buffer);
698 /* add a \, if there isn't one and path is more than just the drive letter ... */
699 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
700 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
702 *p++ = '~';
703 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
704 sprintf( p, "%04x.tmp", num );
706 /* Now try to create it */
708 if (!unique)
712 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
713 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
714 if (handle != INVALID_HANDLE_VALUE)
715 { /* We created it */
716 TRACE("created %s\n",
717 buffer);
718 CloseHandle( handle );
719 break;
721 if (GetLastError() != ERROR_FILE_EXISTS)
722 break; /* No need to go on */
723 num++;
724 sprintf( p, "%04x.tmp", num );
725 } while (num != (unique & 0xffff));
728 /* Get the full path name */
730 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
732 /* Check if we have write access in the directory */
733 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
734 if (access( full_name.long_name, W_OK ) == -1)
735 WARN("returns '%s', which doesn't seem to be writeable.\n",
736 buffer);
738 TRACE("returning %s\n", buffer );
739 return unique ? unique : num;
743 /***********************************************************************
744 * GetTempFileName32W (KERNEL32.291)
746 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
747 LPWSTR buffer )
749 LPSTR patha,prefixa;
750 char buffera[144];
751 UINT ret;
753 if (!path) return 0;
754 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
755 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
756 ret = GetTempFileNameA( patha, prefixa, unique, buffera );
757 lstrcpyAtoW( buffer, buffera );
758 HeapFree( GetProcessHeap(), 0, patha );
759 HeapFree( GetProcessHeap(), 0, prefixa );
760 return ret;
764 /***********************************************************************
765 * FILE_DoOpenFile
767 * Implementation of OpenFile16() and OpenFile32().
769 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
770 BOOL win32 )
772 HFILE hFileRet;
773 FILETIME filetime;
774 WORD filedatetime[2];
775 DOS_FULL_NAME full_name;
776 DWORD access, sharing;
777 char *p;
779 if (!ofs) return HFILE_ERROR;
781 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
782 ((mode & 0x3 )==OF_READ)?"OF_READ":
783 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
784 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
785 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
786 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
787 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
788 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
789 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
790 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
791 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
792 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
793 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
794 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
795 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
796 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
797 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
798 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
802 ofs->cBytes = sizeof(OFSTRUCT);
803 ofs->nErrCode = 0;
804 if (mode & OF_REOPEN) name = ofs->szPathName;
806 if (!name) {
807 ERR("called with `name' set to NULL ! Please debug.\n");
808 return HFILE_ERROR;
811 TRACE("%s %04x\n", name, mode );
813 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
814 Are there any cases where getting the path here is wrong?
815 Uwe Bonnes 1997 Apr 2 */
816 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
817 ofs->szPathName, NULL )) goto error;
818 FILE_ConvertOFMode( mode, &access, &sharing );
820 /* OF_PARSE simply fills the structure */
822 if (mode & OF_PARSE)
824 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
825 != DRIVE_REMOVABLE);
826 TRACE("(%s): OF_PARSE, res = '%s'\n",
827 name, ofs->szPathName );
828 return 0;
831 /* OF_CREATE is completely different from all other options, so
832 handle it first */
834 if (mode & OF_CREATE)
836 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
837 sharing, NULL, CREATE_ALWAYS,
838 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
839 goto error;
840 goto success;
843 /* If OF_SEARCH is set, ignore the given path */
845 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
847 /* First try the file name as is */
848 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
849 /* Now remove the path */
850 if (name[0] && (name[1] == ':')) name += 2;
851 if ((p = strrchr( name, '\\' ))) name = p + 1;
852 if ((p = strrchr( name, '/' ))) name = p + 1;
853 if (!name[0]) goto not_found;
856 /* Now look for the file */
858 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
860 found:
861 TRACE("found %s = %s\n",
862 full_name.long_name, full_name.short_name );
863 lstrcpynA( ofs->szPathName, full_name.short_name,
864 sizeof(ofs->szPathName) );
866 if (mode & OF_SHARE_EXCLUSIVE)
867 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
868 on the file <tempdir>/_ins0432._mp to determine how
869 far installation has proceeded.
870 _ins0432._mp is an executable and while running the
871 application expects the open with OF_SHARE_ to fail*/
872 /* Probable FIXME:
873 As our loader closes the files after loading the executable,
874 we can't find the running executable with FILE_InUse.
875 Perhaps the loader should keep the file open.
876 Recheck against how Win handles that case */
878 char *last = strrchr(full_name.long_name,'/');
879 if (!last)
880 last = full_name.long_name - 1;
881 if (GetModuleHandle16(last+1))
883 TRACE("Denying shared open for %s\n",full_name.long_name);
884 return HFILE_ERROR;
888 if (mode & OF_DELETE)
890 if (unlink( full_name.long_name ) == -1) goto not_found;
891 TRACE("(%s): OF_DELETE return = OK\n", name);
892 return 1;
895 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
896 NULL, OPEN_EXISTING, 0, -1 );
897 if (hFileRet == HFILE_ERROR) goto not_found;
899 GetFileTime( hFileRet, NULL, NULL, &filetime );
900 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
901 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
903 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
905 CloseHandle( hFileRet );
906 WARN("(%s): OF_VERIFY failed\n", name );
907 /* FIXME: what error here? */
908 SetLastError( ERROR_FILE_NOT_FOUND );
909 goto error;
912 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
914 success: /* We get here if the open was successful */
915 TRACE("(%s): OK, return = %d\n", name, hFileRet );
916 if (win32)
918 if (mode & OF_EXIST) /* Return the handle, but close it first */
919 CloseHandle( hFileRet );
921 else
923 hFileRet = FILE_AllocDosHandle( hFileRet );
924 if (hFileRet == HFILE_ERROR16) goto error;
925 if (mode & OF_EXIST) /* Return the handle, but close it first */
926 _lclose16( hFileRet );
928 return hFileRet;
930 not_found: /* We get here if the file does not exist */
931 WARN("'%s' not found\n", name );
932 SetLastError( ERROR_FILE_NOT_FOUND );
933 /* fall through */
935 error: /* We get here if there was an error opening the file */
936 ofs->nErrCode = GetLastError();
937 WARN("(%s): return = HFILE_ERROR error= %d\n",
938 name,ofs->nErrCode );
939 return HFILE_ERROR;
943 /***********************************************************************
944 * OpenFile16 (KERNEL.74)
946 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
948 return FILE_DoOpenFile( name, ofs, mode, FALSE );
952 /***********************************************************************
953 * OpenFile32 (KERNEL32.396)
955 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
957 return FILE_DoOpenFile( name, ofs, mode, TRUE );
961 /***********************************************************************
962 * FILE_InitProcessDosHandles
964 * Allocates the default DOS handles for a process. Called either by
965 * AllocDosHandle below or by the DOSVM stuff.
967 BOOL FILE_InitProcessDosHandles( void ) {
968 HANDLE *ptr;
970 if (!(ptr = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY,
971 sizeof(*ptr) * DOS_TABLE_SIZE )))
972 return FALSE;
973 PROCESS_Current()->dos_handles = ptr;
974 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
975 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
976 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
977 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
978 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
979 return TRUE;
982 /***********************************************************************
983 * FILE_AllocDosHandle
985 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
986 * longer valid after this function (even on failure).
988 HFILE16 FILE_AllocDosHandle( HANDLE handle )
990 int i;
991 HANDLE *ptr = PROCESS_Current()->dos_handles;
993 if (!handle || (handle == INVALID_HANDLE_VALUE))
994 return INVALID_HANDLE_VALUE16;
996 if (!ptr) {
997 if (!FILE_InitProcessDosHandles())
998 goto error;
999 ptr = PROCESS_Current()->dos_handles;
1002 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
1003 if (!*ptr)
1005 *ptr = handle;
1006 TRACE("Got %d for h32 %d\n", i, handle );
1007 return i;
1009 error:
1010 CloseHandle( handle );
1011 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1012 return INVALID_HANDLE_VALUE16;
1016 /***********************************************************************
1017 * FILE_GetHandle32
1019 * Return the Win32 handle for a DOS handle.
1021 HANDLE FILE_GetHandle( HFILE16 hfile )
1023 HANDLE *table = PROCESS_Current()->dos_handles;
1024 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1026 SetLastError( ERROR_INVALID_HANDLE );
1027 return INVALID_HANDLE_VALUE;
1029 return table[hfile];
1033 /***********************************************************************
1034 * FILE_Dup2
1036 * dup2() function for DOS handles.
1038 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1040 HANDLE *table = PROCESS_Current()->dos_handles;
1041 HANDLE new_handle;
1043 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1044 !table || !table[hFile1])
1046 SetLastError( ERROR_INVALID_HANDLE );
1047 return HFILE_ERROR16;
1049 if (hFile2 < 5)
1051 FIXME("stdio handle closed, need proper conversion\n" );
1052 SetLastError( ERROR_INVALID_HANDLE );
1053 return HFILE_ERROR16;
1055 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1056 GetCurrentProcess(), &new_handle,
1057 0, FALSE, DUPLICATE_SAME_ACCESS ))
1058 return HFILE_ERROR16;
1059 if (table[hFile2]) CloseHandle( table[hFile2] );
1060 table[hFile2] = new_handle;
1061 return hFile2;
1065 /***********************************************************************
1066 * _lclose16 (KERNEL.81)
1068 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1070 HANDLE *table = PROCESS_Current()->dos_handles;
1072 if (hFile < 5)
1074 FIXME("stdio handle closed, need proper conversion\n" );
1075 SetLastError( ERROR_INVALID_HANDLE );
1076 return HFILE_ERROR16;
1078 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1080 SetLastError( ERROR_INVALID_HANDLE );
1081 return HFILE_ERROR16;
1083 TRACE("%d (handle32=%d)\n", hFile, table[hFile] );
1084 CloseHandle( table[hFile] );
1085 table[hFile] = 0;
1086 return 0;
1090 /***********************************************************************
1091 * _lclose32 (KERNEL32.592)
1093 HFILE WINAPI _lclose( HFILE hFile )
1095 TRACE("handle %d\n", hFile );
1096 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1100 /***********************************************************************
1101 * ReadFile (KERNEL32.428)
1103 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1104 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1106 struct get_read_fd_request *req = get_req_buffer();
1107 int unix_handle, result;
1109 TRACE("%d %p %ld\n", hFile, buffer, bytesToRead );
1111 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1112 if (!bytesToRead) return TRUE;
1114 req->handle = hFile;
1115 server_call_fd( REQ_GET_READ_FD, -1, &unix_handle );
1116 if (unix_handle == -1) return FALSE;
1117 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1119 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1120 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1121 FILE_SetDosError();
1122 break;
1124 close( unix_handle );
1125 if (result == -1) return FALSE;
1126 if (bytesRead) *bytesRead = result;
1127 return TRUE;
1131 /***********************************************************************
1132 * WriteFile (KERNEL32.578)
1134 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1135 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1137 struct get_write_fd_request *req = get_req_buffer();
1138 int unix_handle, result;
1140 TRACE("%d %p %ld\n", hFile, buffer, bytesToWrite );
1142 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1143 if (!bytesToWrite) return TRUE;
1145 req->handle = hFile;
1146 server_call_fd( REQ_GET_WRITE_FD, -1, &unix_handle );
1147 if (unix_handle == -1) return FALSE;
1148 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1150 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1151 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1152 if (errno == ENOSPC)
1153 SetLastError( ERROR_DISK_FULL );
1154 else
1155 FILE_SetDosError();
1156 break;
1158 close( unix_handle );
1159 if (result == -1) return FALSE;
1160 if (bytesWritten) *bytesWritten = result;
1161 return TRUE;
1165 /***********************************************************************
1166 * WIN16_hread
1168 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1170 LONG maxlen;
1172 TRACE("%d %08lx %ld\n",
1173 hFile, (DWORD)buffer, count );
1175 /* Some programs pass a count larger than the allocated buffer */
1176 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1177 if (count > maxlen) count = maxlen;
1178 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1182 /***********************************************************************
1183 * WIN16_lread
1185 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1187 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1191 /***********************************************************************
1192 * _lread32 (KERNEL32.596)
1194 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1196 DWORD result;
1197 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1198 return result;
1202 /***********************************************************************
1203 * _lread16 (KERNEL.82)
1205 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1207 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1211 /***********************************************************************
1212 * _lcreat16 (KERNEL.83)
1214 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1216 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1220 /***********************************************************************
1221 * _lcreat (KERNEL32.593)
1223 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1225 /* Mask off all flags not explicitly allowed by the doc */
1226 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1227 TRACE("%s %02x\n", path, attr );
1228 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1229 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1230 CREATE_ALWAYS, attr, -1 );
1234 /***********************************************************************
1235 * SetFilePointer (KERNEL32.492)
1237 DWORD WINAPI SetFilePointer( HFILE hFile, LONG distance, LONG *highword,
1238 DWORD method )
1240 struct set_file_pointer_request *req = get_req_buffer();
1242 if (highword && *highword)
1244 FIXME("64-bit offsets not supported yet\n");
1245 SetLastError( ERROR_INVALID_PARAMETER );
1246 return 0xffffffff;
1248 TRACE("handle %d offset %ld origin %ld\n",
1249 hFile, distance, method );
1251 req->handle = hFile;
1252 req->low = distance;
1253 req->high = highword ? *highword : 0;
1254 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1255 req->whence = method;
1256 SetLastError( 0 );
1257 if (server_call( REQ_SET_FILE_POINTER )) return 0xffffffff;
1258 if (highword) *highword = req->new_high;
1259 return req->new_low;
1263 /***********************************************************************
1264 * _llseek16 (KERNEL.84)
1266 * FIXME:
1267 * Seeking before the start of the file should be allowed for _llseek16,
1268 * but cause subsequent I/O operations to fail (cf. interrupt list)
1271 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1273 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1277 /***********************************************************************
1278 * _llseek32 (KERNEL32.594)
1280 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1282 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1286 /***********************************************************************
1287 * _lopen16 (KERNEL.85)
1289 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1291 return FILE_AllocDosHandle( _lopen( path, mode ) );
1295 /***********************************************************************
1296 * _lopen32 (KERNEL32.595)
1298 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1300 DWORD access, sharing;
1302 TRACE("('%s',%04x)\n", path, mode );
1303 FILE_ConvertOFMode( mode, &access, &sharing );
1304 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1308 /***********************************************************************
1309 * _lwrite16 (KERNEL.86)
1311 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1313 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1316 /***********************************************************************
1317 * _lwrite32 (KERNEL32.761)
1319 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1321 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1325 /***********************************************************************
1326 * _hread16 (KERNEL.349)
1328 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1330 return _lread( FILE_GetHandle(hFile), buffer, count );
1334 /***********************************************************************
1335 * _hread32 (KERNEL32.590)
1337 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1339 return _lread( hFile, buffer, count );
1343 /***********************************************************************
1344 * _hwrite16 (KERNEL.350)
1346 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1348 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1352 /***********************************************************************
1353 * _hwrite32 (KERNEL32.591)
1355 * experimentation yields that _lwrite:
1356 * o truncates the file at the current position with
1357 * a 0 len write
1358 * o returns 0 on a 0 length write
1359 * o works with console handles
1362 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1364 DWORD result;
1366 TRACE("%d %p %ld\n", handle, buffer, count );
1368 if (!count)
1370 /* Expand or truncate at current position */
1371 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1372 return 0;
1374 if (!WriteFile( handle, buffer, count, &result, NULL ))
1375 return HFILE_ERROR;
1376 return result;
1380 /***********************************************************************
1381 * SetHandleCount16 (KERNEL.199)
1383 UINT16 WINAPI SetHandleCount16( UINT16 count )
1385 HGLOBAL16 hPDB = GetCurrentPDB16();
1386 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1387 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1389 TRACE("(%d)\n", count );
1391 if (count < 20) count = 20; /* No point in going below 20 */
1392 else if (count > 254) count = 254;
1394 if (count == 20)
1396 if (pdb->nbFiles > 20)
1398 memcpy( pdb->fileHandles, files, 20 );
1399 GlobalFree16( pdb->hFileHandles );
1400 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1401 GlobalHandleToSel16( hPDB ) );
1402 pdb->hFileHandles = 0;
1403 pdb->nbFiles = 20;
1406 else /* More than 20, need a new file handles table */
1408 BYTE *newfiles;
1409 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1410 if (!newhandle)
1412 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1413 return pdb->nbFiles;
1415 newfiles = (BYTE *)GlobalLock16( newhandle );
1417 if (count > pdb->nbFiles)
1419 memcpy( newfiles, files, pdb->nbFiles );
1420 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1422 else memcpy( newfiles, files, count );
1423 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1424 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1425 pdb->hFileHandles = newhandle;
1426 pdb->nbFiles = count;
1428 return pdb->nbFiles;
1432 /*************************************************************************
1433 * SetHandleCount32 (KERNEL32.494)
1435 UINT WINAPI SetHandleCount( UINT count )
1437 return MIN( 256, count );
1441 /***********************************************************************
1442 * FlushFileBuffers (KERNEL32.133)
1444 BOOL WINAPI FlushFileBuffers( HFILE hFile )
1446 struct flush_file_request *req = get_req_buffer();
1447 req->handle = hFile;
1448 return !server_call( REQ_FLUSH_FILE );
1452 /**************************************************************************
1453 * SetEndOfFile (KERNEL32.483)
1455 BOOL WINAPI SetEndOfFile( HFILE hFile )
1457 struct truncate_file_request *req = get_req_buffer();
1458 req->handle = hFile;
1459 return !server_call( REQ_TRUNCATE_FILE );
1463 /***********************************************************************
1464 * DeleteFile16 (KERNEL.146)
1466 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1468 return DeleteFileA( path );
1472 /***********************************************************************
1473 * DeleteFile32A (KERNEL32.71)
1475 BOOL WINAPI DeleteFileA( LPCSTR path )
1477 DOS_FULL_NAME full_name;
1479 TRACE("'%s'\n", path );
1481 if (!*path)
1483 ERR("Empty path passed\n");
1484 return FALSE;
1486 if (DOSFS_GetDevice( path ))
1488 WARN("cannot remove DOS device '%s'!\n", path);
1489 SetLastError( ERROR_FILE_NOT_FOUND );
1490 return FALSE;
1493 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1494 if (unlink( full_name.long_name ) == -1)
1496 FILE_SetDosError();
1497 return FALSE;
1499 return TRUE;
1503 /***********************************************************************
1504 * DeleteFile32W (KERNEL32.72)
1506 BOOL WINAPI DeleteFileW( LPCWSTR path )
1508 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1509 BOOL ret = DeleteFileA( xpath );
1510 HeapFree( GetProcessHeap(), 0, xpath );
1511 return ret;
1515 /***********************************************************************
1516 * FILE_dommap
1518 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1519 DWORD size_high, DWORD size_low,
1520 DWORD offset_high, DWORD offset_low,
1521 int prot, int flags )
1523 int fd = -1;
1524 int pos;
1525 LPVOID ret;
1527 if (size_high || offset_high)
1528 FIXME("offsets larger than 4Gb not supported\n");
1530 if (unix_handle == -1)
1532 #ifdef MAP_ANON
1533 flags |= MAP_ANON;
1534 #else
1535 static int fdzero = -1;
1537 if (fdzero == -1)
1539 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1541 perror( "/dev/zero: open" );
1542 exit(1);
1545 fd = fdzero;
1546 #endif /* MAP_ANON */
1547 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1548 #ifdef MAP_SHARED
1549 flags &= ~MAP_SHARED;
1550 #endif
1551 #ifdef MAP_PRIVATE
1552 flags |= MAP_PRIVATE;
1553 #endif
1555 else fd = unix_handle;
1557 if ((ret = mmap( start, size_low, prot,
1558 flags, fd, offset_low )) != (LPVOID)-1)
1559 return ret;
1561 /* mmap() failed; if this is because the file offset is not */
1562 /* page-aligned (EINVAL), or because the underlying filesystem */
1563 /* does not support mmap() (ENOEXEC), we do it by hand. */
1565 if (unix_handle == -1) return ret;
1566 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1567 if (prot & PROT_WRITE)
1569 /* We cannot fake shared write mappings */
1570 #ifdef MAP_SHARED
1571 if (flags & MAP_SHARED) return ret;
1572 #endif
1573 #ifdef MAP_PRIVATE
1574 if (!(flags & MAP_PRIVATE)) return ret;
1575 #endif
1577 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1578 /* Reserve the memory with an anonymous mmap */
1579 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1580 PROT_READ | PROT_WRITE, flags );
1581 if (ret == (LPVOID)-1) return ret;
1582 /* Now read in the file */
1583 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1585 FILE_munmap( ret, size_high, size_low );
1586 return (LPVOID)-1;
1588 read( fd, ret, size_low );
1589 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1590 mprotect( ret, size_low, prot ); /* Set the right protection */
1591 return ret;
1595 /***********************************************************************
1596 * FILE_munmap
1598 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1600 if (size_high)
1601 FIXME("offsets larger than 4Gb not supported\n");
1602 return munmap( start, size_low );
1606 /***********************************************************************
1607 * GetFileType (KERNEL32.222)
1609 DWORD WINAPI GetFileType( HFILE hFile )
1611 struct get_file_info_request *req = get_req_buffer();
1612 req->handle = hFile;
1613 if (server_call( REQ_GET_FILE_INFO )) return FILE_TYPE_UNKNOWN;
1614 return req->type;
1618 /**************************************************************************
1619 * MoveFileEx32A (KERNEL32.???)
1621 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1623 DOS_FULL_NAME full_name1, full_name2;
1624 int mode=0; /* mode == 1: use copy */
1626 TRACE("(%s,%s,%04lx)\n", fn1, fn2, flag);
1628 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1629 if (fn2) { /* !fn2 means delete fn1 */
1630 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1631 /* Source name and target path are valid */
1632 if ( full_name1.drive != full_name2.drive)
1634 /* use copy, if allowed */
1635 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1636 /* FIXME: Use right error code */
1637 SetLastError( ERROR_FILE_EXISTS );
1638 return FALSE;
1640 else mode =1;
1642 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1643 /* target exists, check if we may overwrite */
1644 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1645 /* FIXME: Use right error code */
1646 SetLastError( ERROR_ACCESS_DENIED );
1647 return FALSE;
1650 else /* fn2 == NULL means delete source */
1651 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1652 if (flag & MOVEFILE_COPY_ALLOWED) {
1653 WARN("Illegal flag\n");
1654 SetLastError( ERROR_GEN_FAILURE );
1655 return FALSE;
1657 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1658 Perhaps we should queue these command and execute it
1659 when exiting... What about using on_exit(2)
1661 FIXME("Please delete file '%s' when Wine has finished\n",
1662 full_name1.long_name);
1663 return TRUE;
1665 else if (unlink( full_name1.long_name ) == -1)
1667 FILE_SetDosError();
1668 return FALSE;
1670 else return TRUE; /* successfully deleted */
1672 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1673 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1674 Perhaps we should queue these command and execute it
1675 when exiting... What about using on_exit(2)
1677 FIXME("Please move existing file '%s' to file '%s'"
1678 "when Wine has finished\n",
1679 full_name1.long_name, full_name2.long_name);
1680 return TRUE;
1683 if (!mode) /* move the file */
1684 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1686 FILE_SetDosError();
1687 return FALSE;
1689 else return TRUE;
1690 else /* copy File */
1691 return CopyFileA(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1695 /**************************************************************************
1696 * MoveFileEx32W (KERNEL32.???)
1698 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1700 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1701 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1702 BOOL res = MoveFileExA( afn1, afn2, flag );
1703 HeapFree( GetProcessHeap(), 0, afn1 );
1704 HeapFree( GetProcessHeap(), 0, afn2 );
1705 return res;
1709 /**************************************************************************
1710 * MoveFile32A (KERNEL32.387)
1712 * Move file or directory
1714 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1716 DOS_FULL_NAME full_name1, full_name2;
1717 struct stat fstat;
1719 TRACE("(%s,%s)\n", fn1, fn2 );
1721 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1722 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1723 /* The new name must not already exist */
1724 return FALSE;
1725 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1727 if (full_name1.drive == full_name2.drive) /* move */
1728 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1730 FILE_SetDosError();
1731 return FALSE;
1733 else return TRUE;
1734 else /*copy */ {
1735 if (stat( full_name1.long_name, &fstat ))
1737 WARN("Invalid source file %s\n",
1738 full_name1.long_name);
1739 FILE_SetDosError();
1740 return FALSE;
1742 if (S_ISDIR(fstat.st_mode)) {
1743 /* No Move for directories across file systems */
1744 /* FIXME: Use right error code */
1745 SetLastError( ERROR_GEN_FAILURE );
1746 return FALSE;
1748 else
1749 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1754 /**************************************************************************
1755 * MoveFile32W (KERNEL32.390)
1757 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1759 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1760 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1761 BOOL res = MoveFileA( afn1, afn2 );
1762 HeapFree( GetProcessHeap(), 0, afn1 );
1763 HeapFree( GetProcessHeap(), 0, afn2 );
1764 return res;
1768 /**************************************************************************
1769 * CopyFile32A (KERNEL32.36)
1771 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1773 HFILE h1, h2;
1774 BY_HANDLE_FILE_INFORMATION info;
1775 UINT count;
1776 BOOL ret = FALSE;
1777 int mode;
1778 char buffer[2048];
1780 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1781 if (!GetFileInformationByHandle( h1, &info ))
1783 CloseHandle( h1 );
1784 return FALSE;
1786 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1787 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1788 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1789 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1791 CloseHandle( h1 );
1792 return FALSE;
1794 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1796 char *p = buffer;
1797 while (count > 0)
1799 INT res = _lwrite( h2, p, count );
1800 if (res <= 0) goto done;
1801 p += res;
1802 count -= res;
1805 ret = TRUE;
1806 done:
1807 CloseHandle( h1 );
1808 CloseHandle( h2 );
1809 return ret;
1813 /**************************************************************************
1814 * CopyFile32W (KERNEL32.37)
1816 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1818 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1819 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1820 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1821 HeapFree( GetProcessHeap(), 0, sourceA );
1822 HeapFree( GetProcessHeap(), 0, destA );
1823 return ret;
1827 /**************************************************************************
1828 * CopyFileEx32A (KERNEL32.858)
1830 * This implementation ignores most of the extra parameters passed-in into
1831 * the "ex" version of the method and calls the CopyFile method.
1832 * It will have to be fixed eventually.
1834 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1835 LPCSTR destFilename,
1836 LPPROGRESS_ROUTINE progressRoutine,
1837 LPVOID appData,
1838 LPBOOL cancelFlagPointer,
1839 DWORD copyFlags)
1841 BOOL failIfExists = FALSE;
1844 * Interpret the only flag that CopyFile can interpret.
1846 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1848 failIfExists = TRUE;
1851 return CopyFileA(sourceFilename, destFilename, failIfExists);
1854 /**************************************************************************
1855 * CopyFileEx32W (KERNEL32.859)
1857 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1858 LPCWSTR destFilename,
1859 LPPROGRESS_ROUTINE progressRoutine,
1860 LPVOID appData,
1861 LPBOOL cancelFlagPointer,
1862 DWORD copyFlags)
1864 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1865 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1867 BOOL ret = CopyFileExA(sourceA,
1868 destA,
1869 progressRoutine,
1870 appData,
1871 cancelFlagPointer,
1872 copyFlags);
1874 HeapFree( GetProcessHeap(), 0, sourceA );
1875 HeapFree( GetProcessHeap(), 0, destA );
1877 return ret;
1881 /***********************************************************************
1882 * SetFileTime (KERNEL32.650)
1884 BOOL WINAPI SetFileTime( HFILE hFile,
1885 const FILETIME *lpCreationTime,
1886 const FILETIME *lpLastAccessTime,
1887 const FILETIME *lpLastWriteTime )
1889 struct set_file_time_request *req = get_req_buffer();
1891 req->handle = hFile;
1892 if (lpLastAccessTime)
1893 req->access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1894 else
1895 req->access_time = 0; /* FIXME */
1896 if (lpLastWriteTime)
1897 req->write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1898 else
1899 req->write_time = 0; /* FIXME */
1900 return !server_call( REQ_SET_FILE_TIME );
1904 /**************************************************************************
1905 * LockFile (KERNEL32.511)
1907 BOOL WINAPI LockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1908 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1910 struct lock_file_request *req = get_req_buffer();
1912 req->handle = hFile;
1913 req->offset_low = dwFileOffsetLow;
1914 req->offset_high = dwFileOffsetHigh;
1915 req->count_low = nNumberOfBytesToLockLow;
1916 req->count_high = nNumberOfBytesToLockHigh;
1917 return !server_call( REQ_LOCK_FILE );
1920 /**************************************************************************
1921 * LockFileEx [KERNEL32.512]
1923 * Locks a byte range within an open file for shared or exclusive access.
1925 * RETURNS
1926 * success: TRUE
1927 * failure: FALSE
1928 * NOTES
1930 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1932 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1933 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1934 LPOVERLAPPED pOverlapped )
1936 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1937 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
1938 pOverlapped);
1939 if (reserved == 0)
1940 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1941 else
1943 ERR("reserved == %ld: Supposed to be 0??\n", reserved);
1944 SetLastError(ERROR_INVALID_PARAMETER);
1947 return FALSE;
1951 /**************************************************************************
1952 * UnlockFile (KERNEL32.703)
1954 BOOL WINAPI UnlockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1955 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1957 struct unlock_file_request *req = get_req_buffer();
1959 req->handle = hFile;
1960 req->offset_low = dwFileOffsetLow;
1961 req->offset_high = dwFileOffsetHigh;
1962 req->count_low = nNumberOfBytesToUnlockLow;
1963 req->count_high = nNumberOfBytesToUnlockHigh;
1964 return !server_call( REQ_UNLOCK_FILE );
1968 /**************************************************************************
1969 * UnlockFileEx (KERNEL32.705)
1971 BOOL WINAPI UnlockFileEx(
1972 HFILE hFile,
1973 DWORD dwReserved,
1974 DWORD nNumberOfBytesToUnlockLow,
1975 DWORD nNumberOfBytesToUnlockHigh,
1976 LPOVERLAPPED lpOverlapped
1979 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1980 hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh,
1981 lpOverlapped);
1982 if (dwReserved == 0)
1983 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1984 else
1986 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved);
1987 SetLastError(ERROR_INVALID_PARAMETER);
1990 return FALSE;
1994 #if 0
1996 struct DOS_FILE_LOCK {
1997 struct DOS_FILE_LOCK * next;
1998 DWORD base;
1999 DWORD len;
2000 DWORD processId;
2001 FILE_OBJECT * dos_file;
2002 /* char * unix_name;*/
2005 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2007 static DOS_FILE_LOCK *locks = NULL;
2008 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2011 /* Locks need to be mirrored because unix file locking is based
2012 * on the pid. Inside of wine there can be multiple WINE processes
2013 * that share the same unix pid.
2014 * Read's and writes should check these locks also - not sure
2015 * how critical that is at this point (FIXME).
2018 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2020 DOS_FILE_LOCK *curr;
2021 DWORD processId;
2023 processId = GetCurrentProcessId();
2025 /* check if lock overlaps a current lock for the same file */
2026 #if 0
2027 for (curr = locks; curr; curr = curr->next) {
2028 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2029 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2030 return TRUE;/* region is identic */
2031 if ((f->l_start < (curr->base + curr->len)) &&
2032 ((f->l_start + f->l_len) > curr->base)) {
2033 /* region overlaps */
2034 return FALSE;
2038 #endif
2040 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2041 curr->processId = GetCurrentProcessId();
2042 curr->base = f->l_start;
2043 curr->len = f->l_len;
2044 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
2045 curr->next = locks;
2046 curr->dos_file = file;
2047 locks = curr;
2048 return TRUE;
2051 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2053 DWORD processId;
2054 DOS_FILE_LOCK **curr;
2055 DOS_FILE_LOCK *rem;
2057 processId = GetCurrentProcessId();
2058 curr = &locks;
2059 while (*curr) {
2060 if ((*curr)->dos_file == file) {
2061 rem = *curr;
2062 *curr = (*curr)->next;
2063 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2064 HeapFree( SystemHeap, 0, rem );
2066 else
2067 curr = &(*curr)->next;
2071 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2073 DWORD processId;
2074 DOS_FILE_LOCK **curr;
2075 DOS_FILE_LOCK *rem;
2077 processId = GetCurrentProcessId();
2078 for (curr = &locks; *curr; curr = &(*curr)->next) {
2079 if ((*curr)->processId == processId &&
2080 (*curr)->dos_file == file &&
2081 (*curr)->base == f->l_start &&
2082 (*curr)->len == f->l_len) {
2083 /* this is the same lock */
2084 rem = *curr;
2085 *curr = (*curr)->next;
2086 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2087 HeapFree( SystemHeap, 0, rem );
2088 return TRUE;
2091 /* no matching lock found */
2092 return FALSE;
2096 /**************************************************************************
2097 * LockFile (KERNEL32.511)
2099 BOOL WINAPI LockFile(
2100 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2101 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2103 struct flock f;
2104 FILE_OBJECT *file;
2106 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2107 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2108 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2110 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2111 FIXME("Unimplemented bytes > 32bits\n");
2112 return FALSE;
2115 f.l_start = dwFileOffsetLow;
2116 f.l_len = nNumberOfBytesToLockLow;
2117 f.l_whence = SEEK_SET;
2118 f.l_pid = 0;
2119 f.l_type = F_WRLCK;
2121 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2123 /* shadow locks internally */
2124 if (!DOS_AddLock(file, &f)) {
2125 SetLastError( ERROR_LOCK_VIOLATION );
2126 return FALSE;
2129 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2130 #ifdef USE_UNIX_LOCKS
2131 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2132 if (errno == EACCES || errno == EAGAIN) {
2133 SetLastError( ERROR_LOCK_VIOLATION );
2135 else {
2136 FILE_SetDosError();
2138 /* remove our internal copy of the lock */
2139 DOS_RemoveLock(file, &f);
2140 return FALSE;
2142 #endif
2143 return TRUE;
2147 /**************************************************************************
2148 * UnlockFile (KERNEL32.703)
2150 BOOL WINAPI UnlockFile(
2151 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2152 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2154 FILE_OBJECT *file;
2155 struct flock f;
2157 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2158 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2159 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2161 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2162 WARN("Unimplemented bytes > 32bits\n");
2163 return FALSE;
2166 f.l_start = dwFileOffsetLow;
2167 f.l_len = nNumberOfBytesToUnlockLow;
2168 f.l_whence = SEEK_SET;
2169 f.l_pid = 0;
2170 f.l_type = F_UNLCK;
2172 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2174 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2176 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2177 #ifdef USE_UNIX_LOCKS
2178 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2179 FILE_SetDosError();
2180 return FALSE;
2182 #endif
2183 return TRUE;
2185 #endif
2187 /**************************************************************************
2188 * GetFileAttributesEx32A [KERNEL32.874]
2190 BOOL WINAPI GetFileAttributesExA(
2191 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2192 LPVOID lpFileInformation)
2194 DOS_FULL_NAME full_name;
2195 BY_HANDLE_FILE_INFORMATION info;
2197 if (lpFileName == NULL) return FALSE;
2198 if (lpFileInformation == NULL) return FALSE;
2200 if (fInfoLevelId == GetFileExInfoStandard) {
2201 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2202 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2203 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2204 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2206 lpFad->dwFileAttributes = info.dwFileAttributes;
2207 lpFad->ftCreationTime = info.ftCreationTime;
2208 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2209 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2210 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2211 lpFad->nFileSizeLow = info.nFileSizeLow;
2213 else {
2214 FIXME("invalid info level %d!\n", fInfoLevelId);
2215 return FALSE;
2218 return TRUE;
2222 /**************************************************************************
2223 * GetFileAttributesEx32W [KERNEL32.875]
2225 BOOL WINAPI GetFileAttributesExW(
2226 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2227 LPVOID lpFileInformation)
2229 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2230 BOOL res =
2231 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2232 HeapFree( GetProcessHeap(), 0, nameA );
2233 return res;