New file. Seperate implementation of the shellview background
[wine/dcerpc.git] / files / file.c
blob4fee9910399f9a20f3c7b13423ca344496fd3cfe
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 "async.h"
49 #include "wincon.h"
50 #include "debugtools.h"
52 #include "server.h"
54 DEFAULT_DEBUG_CHANNEL(file)
56 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
57 #define MAP_ANON MAP_ANONYMOUS
58 #endif
60 /* Size of per-process table of DOS handles */
61 #define DOS_TABLE_SIZE 256
64 /***********************************************************************
65 * FILE_ConvertOFMode
67 * Convert OF_* mode into flags for CreateFile.
69 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
71 switch(mode & 0x03)
73 case OF_READ: *access = GENERIC_READ; break;
74 case OF_WRITE: *access = GENERIC_WRITE; break;
75 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
76 default: *access = 0; break;
78 switch(mode & 0x70)
80 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
81 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
82 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
83 case OF_SHARE_DENY_NONE:
84 case OF_SHARE_COMPAT:
85 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
90 #if 0
91 /***********************************************************************
92 * FILE_ShareDeny
94 * PARAMS
95 * oldmode[I] mode how file was first opened
96 * mode[I] mode how the file should get opened
97 * RETURNS
98 * TRUE: deny open
99 * FALSE: allow open
101 * Look what we have to do with the given SHARE modes
103 * Ralph Brown's interrupt list gives following explication, I guess
104 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
106 * FIXME: Validate this function
107 ========from Ralph Brown's list =========
108 (Table 0750)
109 Values of DOS file sharing behavior:
110 | Second and subsequent Opens
111 First |Compat Deny Deny Deny Deny
112 Open | All Write Read None
113 |R W RW R W RW R W RW R W RW R W RW
114 - - - - -| - - - - - - - - - - - - - - - - -
115 Compat R |Y Y Y N N N 1 N N N N N 1 N N
116 W |Y Y Y N N N N N N N N N N N N
117 RW|Y Y Y N N N N N N N N N N N N
118 - - - - -|
119 Deny R |C C C N N N N N N N N N N N N
120 All W |C C C N N N N N N N N N N N N
121 RW|C C C N N N N N N N N N N N N
122 - - - - -|
123 Deny R |2 C C N N N Y N N N N N Y N N
124 Write W |C C C N N N N N N Y N N Y N N
125 RW|C C C N N N N N N N N N Y N N
126 - - - - -|
127 Deny R |C C C N N N N Y N N N N N Y N
128 Read W |C C C N N N N N N N Y N N Y N
129 RW|C C C N N N N N N N N N N Y N
130 - - - - -|
131 Deny R |2 C C N N N Y Y Y N N N Y Y Y
132 None W |C C C N N N N N N Y Y Y Y Y Y
133 RW|C C C N N N N N N N N N Y Y Y
134 Legend: Y = open succeeds, N = open fails with error code 05h
135 C = open fails, INT 24 generated
136 1 = open succeeds if file read-only, else fails with error code
137 2 = open succeeds if file read-only, else fails with INT 24
138 ========end of description from Ralph Brown's List =====
139 For every "Y" in the table we return FALSE
140 For every "N" we set the DOS_ERROR and return TRUE
141 For all other cases we barf,set the DOS_ERROR and return TRUE
144 static BOOL FILE_ShareDeny( int mode, int oldmode)
146 int oldsharemode = oldmode & 0x70;
147 int sharemode = mode & 0x70;
148 int oldopenmode = oldmode & 3;
149 int openmode = mode & 3;
151 switch (oldsharemode)
153 case OF_SHARE_COMPAT:
154 if (sharemode == OF_SHARE_COMPAT) return FALSE;
155 if (openmode == OF_READ) goto test_ro_err05 ;
156 goto fail_error05;
157 case OF_SHARE_EXCLUSIVE:
158 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
159 goto fail_error05;
160 case OF_SHARE_DENY_WRITE:
161 if (openmode != OF_READ)
163 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
164 goto fail_error05;
166 switch (sharemode)
168 case OF_SHARE_COMPAT:
169 if (oldopenmode == OF_READ) goto test_ro_int24 ;
170 goto fail_int24;
171 case OF_SHARE_DENY_NONE :
172 return FALSE;
173 case OF_SHARE_DENY_WRITE :
174 if (oldopenmode == OF_READ) return FALSE;
175 case OF_SHARE_DENY_READ :
176 if (oldopenmode == OF_WRITE) return FALSE;
177 case OF_SHARE_EXCLUSIVE:
178 default:
179 goto fail_error05;
181 break;
182 case OF_SHARE_DENY_READ:
183 if (openmode != OF_WRITE)
185 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
186 goto fail_error05;
188 switch (sharemode)
190 case OF_SHARE_COMPAT:
191 goto fail_int24;
192 case OF_SHARE_DENY_NONE :
193 return FALSE;
194 case OF_SHARE_DENY_WRITE :
195 if (oldopenmode == OF_READ) return FALSE;
196 case OF_SHARE_DENY_READ :
197 if (oldopenmode == OF_WRITE) return FALSE;
198 case OF_SHARE_EXCLUSIVE:
199 default:
200 goto fail_error05;
202 break;
203 case OF_SHARE_DENY_NONE:
204 switch (sharemode)
206 case OF_SHARE_COMPAT:
207 goto fail_int24;
208 case OF_SHARE_DENY_NONE :
209 return FALSE;
210 case OF_SHARE_DENY_WRITE :
211 if (oldopenmode == OF_READ) return FALSE;
212 case OF_SHARE_DENY_READ :
213 if (oldopenmode == OF_WRITE) return FALSE;
214 case OF_SHARE_EXCLUSIVE:
215 default:
216 goto fail_error05;
218 default:
219 ERR("unknown mode\n");
221 ERR("shouldn't happen\n");
222 ERR("Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
223 return TRUE;
225 test_ro_int24:
226 if (oldmode == OF_READ)
227 return FALSE;
228 /* Fall through */
229 fail_int24:
230 FIXME("generate INT24 missing\n");
231 /* Is this the right error? */
232 SetLastError( ERROR_ACCESS_DENIED );
233 return TRUE;
235 test_ro_err05:
236 if (oldmode == OF_READ)
237 return FALSE;
238 /* fall through */
239 fail_error05:
240 TRACE("Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
241 SetLastError( ERROR_ACCESS_DENIED );
242 return TRUE;
244 #endif
247 /***********************************************************************
248 * FILE_SetDosError
250 * Set the DOS error code from errno.
252 void FILE_SetDosError(void)
254 int save_errno = errno; /* errno gets overwritten by printf */
256 TRACE("errno = %d %s\n", errno, strerror(errno));
257 switch (save_errno)
259 case EAGAIN:
260 SetLastError( ERROR_SHARING_VIOLATION );
261 break;
262 case EBADF:
263 SetLastError( ERROR_INVALID_HANDLE );
264 break;
265 case ENOSPC:
266 SetLastError( ERROR_HANDLE_DISK_FULL );
267 break;
268 case EACCES:
269 case EPERM:
270 case EROFS:
271 SetLastError( ERROR_ACCESS_DENIED );
272 break;
273 case EBUSY:
274 SetLastError( ERROR_LOCK_VIOLATION );
275 break;
276 case ENOENT:
277 SetLastError( ERROR_FILE_NOT_FOUND );
278 break;
279 case EISDIR:
280 SetLastError( ERROR_CANNOT_MAKE );
281 break;
282 case ENFILE:
283 case EMFILE:
284 SetLastError( ERROR_NO_MORE_FILES );
285 break;
286 case EEXIST:
287 SetLastError( ERROR_FILE_EXISTS );
288 break;
289 case EINVAL:
290 case ESPIPE:
291 SetLastError( ERROR_SEEK );
292 break;
293 case ENOTEMPTY:
294 SetLastError( ERROR_DIR_NOT_EMPTY );
295 break;
296 default:
297 perror( "int21: unknown errno" );
298 SetLastError( ERROR_GEN_FAILURE );
299 break;
301 errno = save_errno;
305 /***********************************************************************
306 * FILE_DupUnixHandle
308 * Duplicate a Unix handle into a task handle.
310 HFILE FILE_DupUnixHandle( int fd, DWORD access )
312 int unix_handle;
313 struct alloc_file_handle_request *req = get_req_buffer();
315 if ((unix_handle = dup(fd)) == -1)
317 FILE_SetDosError();
318 return INVALID_HANDLE_VALUE;
320 req->access = access;
321 server_call_fd( REQ_ALLOC_FILE_HANDLE, unix_handle, NULL );
322 return req->handle;
326 /***********************************************************************
327 * FILE_CreateFile
329 * Implementation of CreateFile. Takes a Unix path name.
331 HFILE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
332 LPSECURITY_ATTRIBUTES sa, DWORD creation,
333 DWORD attributes, HANDLE template )
335 struct create_file_request *req = get_req_buffer();
337 req->access = access;
338 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
339 req->sharing = sharing;
340 req->create = creation;
341 req->attrs = attributes;
342 lstrcpynA( req->name, filename, server_remaining(req->name) );
343 SetLastError(0);
344 server_call( REQ_CREATE_FILE );
346 /* If write access failed, retry without GENERIC_WRITE */
348 if ((req->handle == -1) && !Options.failReadOnly &&
349 (access & GENERIC_WRITE))
351 DWORD lasterror = GetLastError();
352 if ((lasterror == ERROR_ACCESS_DENIED) || (lasterror == ERROR_WRITE_PROTECT))
353 return FILE_CreateFile( filename, access & ~GENERIC_WRITE, sharing,
354 sa, creation, attributes, template );
356 return req->handle;
360 /***********************************************************************
361 * FILE_CreateDevice
363 * Same as FILE_CreateFile but for a device
365 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
367 struct create_device_request *req = get_req_buffer();
369 req->access = access;
370 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
371 req->id = client_id;
372 SetLastError(0);
373 server_call( REQ_CREATE_DEVICE );
374 return req->handle;
378 /*************************************************************************
379 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
381 * Creates or opens an object, and returns a handle that can be used to
382 * access that object.
384 * PARAMS
386 * filename [I] pointer to filename to be accessed
387 * access [I] access mode requested
388 * sharing [I] share mode
389 * sa [I] pointer to security attributes
390 * creation [I] how to create the file
391 * attributes [I] attributes for newly created file
392 * template [I] handle to file with extended attributes to copy
394 * RETURNS
395 * Success: Open handle to specified file
396 * Failure: INVALID_HANDLE_VALUE
398 * NOTES
399 * Should call SetLastError() on failure.
401 * BUGS
403 * Doesn't support character devices, pipes, template files, or a
404 * lot of the 'attributes' flags yet.
406 HFILE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
407 LPSECURITY_ATTRIBUTES sa, DWORD creation,
408 DWORD attributes, HANDLE template )
410 DOS_FULL_NAME full_name;
412 if (!filename)
414 SetLastError( ERROR_INVALID_PARAMETER );
415 return HFILE_ERROR;
417 TRACE("%s %s%s%s%s%s%s%s\n",filename,
418 ((access & GENERIC_READ)==GENERIC_READ)?"GENERIC_READ ":"",
419 ((access & GENERIC_WRITE)==GENERIC_WRITE)?"GENERIC_WRITE ":"",
420 (!access)?"QUERY_ACCESS ":"",
421 ((sharing & FILE_SHARE_READ)==FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
422 ((sharing & FILE_SHARE_WRITE)==FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
423 ((sharing & FILE_SHARE_DELETE)==FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
424 (creation ==CREATE_NEW)?"CREATE_NEW":
425 (creation ==CREATE_ALWAYS)?"CREATE_ALWAYS ":
426 (creation ==OPEN_EXISTING)?"OPEN_EXISTING ":
427 (creation ==OPEN_ALWAYS)?"OPEN_ALWAYS ":
428 (creation ==TRUNCATE_EXISTING)?"TRUNCATE_EXISTING ":"");
430 /* If the name starts with '\\?\', ignore the first 4 chars. */
431 if (!strncmp(filename, "\\\\?\\", 4))
433 filename += 4;
434 if (!strncmp(filename, "UNC\\", 4))
436 FIXME("UNC name (%s) not supported.\n", filename );
437 SetLastError( ERROR_PATH_NOT_FOUND );
438 return HFILE_ERROR;
442 if (!strncmp(filename, "\\\\.\\", 4))
443 return DEVICE_Open( filename+4, access, sa );
445 /* If the name still starts with '\\', it's a UNC name. */
446 if (!strncmp(filename, "\\\\", 2))
448 FIXME("UNC name (%s) not supported.\n", filename );
449 SetLastError( ERROR_PATH_NOT_FOUND );
450 return HFILE_ERROR;
453 /* Open a console for CONIN$ or CONOUT$ */
454 if (!lstrcmpiA(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
455 if (!lstrcmpiA(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
457 if (DOSFS_GetDevice( filename ))
459 HFILE ret;
461 TRACE("opening device '%s'\n", filename );
463 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
464 return ret;
466 /* Do not silence this please. It is a critical error. -MM */
467 ERR("Couldn't open device '%s'!\n",filename);
468 SetLastError( ERROR_FILE_NOT_FOUND );
469 return HFILE_ERROR;
472 /* check for filename, don't check for last entry if creating */
473 if (!DOSFS_GetFullName( filename,
474 (creation == OPEN_EXISTING) || (creation == TRUNCATE_EXISTING), &full_name ))
475 return HFILE_ERROR;
477 return FILE_CreateFile( full_name.long_name, access, sharing,
478 sa, creation, attributes, template );
483 /*************************************************************************
484 * CreateFile32W (KERNEL32.48)
486 HFILE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
487 LPSECURITY_ATTRIBUTES sa, DWORD creation,
488 DWORD attributes, HANDLE template)
490 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
491 HFILE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
492 HeapFree( GetProcessHeap(), 0, afn );
493 return res;
497 /***********************************************************************
498 * FILE_FillInfo
500 * Fill a file information from a struct stat.
502 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
504 if (S_ISDIR(st->st_mode))
505 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
506 else
507 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
508 if (!(st->st_mode & S_IWUSR))
509 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
511 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
512 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
513 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
515 info->dwVolumeSerialNumber = 0; /* FIXME */
516 info->nFileSizeHigh = 0;
517 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
518 info->nNumberOfLinks = st->st_nlink;
519 info->nFileIndexHigh = 0;
520 info->nFileIndexLow = st->st_ino;
524 /***********************************************************************
525 * FILE_Stat
527 * Stat a Unix path name. Return TRUE if OK.
529 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
531 struct stat st;
533 if (!unixName || !info) return FALSE;
535 if (stat( unixName, &st ) == -1)
537 FILE_SetDosError();
538 return FALSE;
540 FILE_FillInfo( &st, info );
541 return TRUE;
545 /***********************************************************************
546 * GetFileInformationByHandle (KERNEL32.219)
548 DWORD WINAPI GetFileInformationByHandle( HFILE hFile,
549 BY_HANDLE_FILE_INFORMATION *info )
551 struct get_file_info_request *req = get_req_buffer();
553 if (!info) return 0;
554 req->handle = hFile;
555 if (server_call( REQ_GET_FILE_INFO )) return 0;
556 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftCreationTime, 0 );
557 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftLastWriteTime, 0 );
558 DOSFS_UnixTimeToFileTime( req->access_time, &info->ftLastAccessTime, 0 );
559 info->dwFileAttributes = req->attr;
560 info->dwVolumeSerialNumber = req->serial;
561 info->nFileSizeHigh = req->size_high;
562 info->nFileSizeLow = req->size_low;
563 info->nNumberOfLinks = req->links;
564 info->nFileIndexHigh = req->index_high;
565 info->nFileIndexLow = req->index_low;
566 return 1;
570 /**************************************************************************
571 * GetFileAttributes16 (KERNEL.420)
573 DWORD WINAPI GetFileAttributes16( LPCSTR name )
575 return GetFileAttributesA( name );
579 /**************************************************************************
580 * GetFileAttributes32A (KERNEL32.217)
582 DWORD WINAPI GetFileAttributesA( LPCSTR name )
584 DOS_FULL_NAME full_name;
585 BY_HANDLE_FILE_INFORMATION info;
587 if (name == NULL || *name=='\0') return -1;
589 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
590 if (!FILE_Stat( full_name.long_name, &info )) return -1;
591 return info.dwFileAttributes;
595 /**************************************************************************
596 * GetFileAttributes32W (KERNEL32.218)
598 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
600 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
601 DWORD res = GetFileAttributesA( nameA );
602 HeapFree( GetProcessHeap(), 0, nameA );
603 return res;
607 /***********************************************************************
608 * GetFileSize (KERNEL32.220)
610 DWORD WINAPI GetFileSize( HFILE hFile, LPDWORD filesizehigh )
612 BY_HANDLE_FILE_INFORMATION info;
613 if (!GetFileInformationByHandle( hFile, &info )) return 0;
614 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
615 return info.nFileSizeLow;
619 /***********************************************************************
620 * GetFileTime (KERNEL32.221)
622 BOOL WINAPI GetFileTime( HFILE hFile, FILETIME *lpCreationTime,
623 FILETIME *lpLastAccessTime,
624 FILETIME *lpLastWriteTime )
626 BY_HANDLE_FILE_INFORMATION info;
627 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
628 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
629 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
630 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
631 return TRUE;
634 /***********************************************************************
635 * CompareFileTime (KERNEL32.28)
637 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
639 if (!x || !y) return -1;
641 if (x->dwHighDateTime > y->dwHighDateTime)
642 return 1;
643 if (x->dwHighDateTime < y->dwHighDateTime)
644 return -1;
645 if (x->dwLowDateTime > y->dwLowDateTime)
646 return 1;
647 if (x->dwLowDateTime < y->dwLowDateTime)
648 return -1;
649 return 0;
653 /***********************************************************************
654 * GetTempFileName16 (KERNEL.97)
656 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
657 LPSTR buffer )
659 char temppath[144];
661 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
662 drive |= DRIVE_GetCurrentDrive() + 'A';
664 if ((drive & TF_FORCEDRIVE) &&
665 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
667 drive &= ~TF_FORCEDRIVE;
668 WARN("invalid drive %d specified\n", drive );
671 if (drive & TF_FORCEDRIVE)
672 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
673 else
674 GetTempPathA( 132, temppath );
675 return (UINT16)GetTempFileNameA( temppath, prefix, unique, buffer );
679 /***********************************************************************
680 * GetTempFileName32A (KERNEL32.290)
682 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
683 LPSTR buffer)
685 static UINT unique_temp;
686 DOS_FULL_NAME full_name;
687 int i;
688 LPSTR p;
689 UINT num;
691 if ( !path || !prefix || !buffer ) return 0;
693 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
694 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
696 strcpy( buffer, path );
697 p = buffer + strlen(buffer);
699 /* add a \, if there isn't one and path is more than just the drive letter ... */
700 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
701 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
703 *p++ = '~';
704 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
705 sprintf( p, "%04x.tmp", num );
707 /* Now try to create it */
709 if (!unique)
713 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
714 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
715 if (handle != INVALID_HANDLE_VALUE)
716 { /* We created it */
717 TRACE("created %s\n",
718 buffer);
719 CloseHandle( handle );
720 break;
722 if (GetLastError() != ERROR_FILE_EXISTS)
723 break; /* No need to go on */
724 num++;
725 sprintf( p, "%04x.tmp", num );
726 } while (num != (unique & 0xffff));
729 /* Get the full path name */
731 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
733 /* Check if we have write access in the directory */
734 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
735 if (access( full_name.long_name, W_OK ) == -1)
736 WARN("returns '%s', which doesn't seem to be writeable.\n",
737 buffer);
739 TRACE("returning %s\n", buffer );
740 return unique ? unique : num;
744 /***********************************************************************
745 * GetTempFileName32W (KERNEL32.291)
747 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
748 LPWSTR buffer )
750 LPSTR patha,prefixa;
751 char buffera[144];
752 UINT ret;
754 if (!path) return 0;
755 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
756 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
757 ret = GetTempFileNameA( patha, prefixa, unique, buffera );
758 lstrcpyAtoW( buffer, buffera );
759 HeapFree( GetProcessHeap(), 0, patha );
760 HeapFree( GetProcessHeap(), 0, prefixa );
761 return ret;
765 /***********************************************************************
766 * FILE_DoOpenFile
768 * Implementation of OpenFile16() and OpenFile32().
770 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
771 BOOL win32 )
773 HFILE hFileRet;
774 FILETIME filetime;
775 WORD filedatetime[2];
776 DOS_FULL_NAME full_name;
777 DWORD access, sharing;
778 char *p;
780 if (!ofs) return HFILE_ERROR;
782 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
783 ((mode & 0x3 )==OF_READ)?"OF_READ":
784 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
785 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
786 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
787 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
788 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
789 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
790 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
791 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
792 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
793 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
794 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
795 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
796 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
797 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
798 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
799 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
803 ofs->cBytes = sizeof(OFSTRUCT);
804 ofs->nErrCode = 0;
805 if (mode & OF_REOPEN) name = ofs->szPathName;
807 if (!name) {
808 ERR("called with `name' set to NULL ! Please debug.\n");
809 return HFILE_ERROR;
812 TRACE("%s %04x\n", name, mode );
814 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
815 Are there any cases where getting the path here is wrong?
816 Uwe Bonnes 1997 Apr 2 */
817 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
818 ofs->szPathName, NULL )) goto error;
819 FILE_ConvertOFMode( mode, &access, &sharing );
821 /* OF_PARSE simply fills the structure */
823 if (mode & OF_PARSE)
825 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
826 != DRIVE_REMOVABLE);
827 TRACE("(%s): OF_PARSE, res = '%s'\n",
828 name, ofs->szPathName );
829 return 0;
832 /* OF_CREATE is completely different from all other options, so
833 handle it first */
835 if (mode & OF_CREATE)
837 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
838 sharing, NULL, CREATE_ALWAYS,
839 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
840 goto error;
841 goto success;
844 /* If OF_SEARCH is set, ignore the given path */
846 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
848 /* First try the file name as is */
849 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
850 /* Now remove the path */
851 if (name[0] && (name[1] == ':')) name += 2;
852 if ((p = strrchr( name, '\\' ))) name = p + 1;
853 if ((p = strrchr( name, '/' ))) name = p + 1;
854 if (!name[0]) goto not_found;
857 /* Now look for the file */
859 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
861 found:
862 TRACE("found %s = %s\n",
863 full_name.long_name, full_name.short_name );
864 lstrcpynA( ofs->szPathName, full_name.short_name,
865 sizeof(ofs->szPathName) );
867 if (mode & OF_SHARE_EXCLUSIVE)
868 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
869 on the file <tempdir>/_ins0432._mp to determine how
870 far installation has proceeded.
871 _ins0432._mp is an executable and while running the
872 application expects the open with OF_SHARE_ to fail*/
873 /* Probable FIXME:
874 As our loader closes the files after loading the executable,
875 we can't find the running executable with FILE_InUse.
876 Perhaps the loader should keep the file open.
877 Recheck against how Win handles that case */
879 char *last = strrchr(full_name.long_name,'/');
880 if (!last)
881 last = full_name.long_name - 1;
882 if (GetModuleHandle16(last+1))
884 TRACE("Denying shared open for %s\n",full_name.long_name);
885 return HFILE_ERROR;
889 if (mode & OF_DELETE)
891 if (unlink( full_name.long_name ) == -1) goto not_found;
892 TRACE("(%s): OF_DELETE return = OK\n", name);
893 return 1;
896 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
897 NULL, OPEN_EXISTING, 0, -1 );
898 if (hFileRet == HFILE_ERROR) goto not_found;
900 GetFileTime( hFileRet, NULL, NULL, &filetime );
901 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
902 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
904 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
906 CloseHandle( hFileRet );
907 WARN("(%s): OF_VERIFY failed\n", name );
908 /* FIXME: what error here? */
909 SetLastError( ERROR_FILE_NOT_FOUND );
910 goto error;
913 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
915 success: /* We get here if the open was successful */
916 TRACE("(%s): OK, return = %d\n", name, hFileRet );
917 if (win32)
919 if (mode & OF_EXIST) /* Return the handle, but close it first */
920 CloseHandle( hFileRet );
922 else
924 hFileRet = FILE_AllocDosHandle( hFileRet );
925 if (hFileRet == HFILE_ERROR16) goto error;
926 if (mode & OF_EXIST) /* Return the handle, but close it first */
927 _lclose16( hFileRet );
929 return hFileRet;
931 not_found: /* We get here if the file does not exist */
932 WARN("'%s' not found\n", name );
933 SetLastError( ERROR_FILE_NOT_FOUND );
934 /* fall through */
936 error: /* We get here if there was an error opening the file */
937 ofs->nErrCode = GetLastError();
938 WARN("(%s): return = HFILE_ERROR error= %d\n",
939 name,ofs->nErrCode );
940 return HFILE_ERROR;
944 /***********************************************************************
945 * OpenFile16 (KERNEL.74)
947 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
949 return FILE_DoOpenFile( name, ofs, mode, FALSE );
953 /***********************************************************************
954 * OpenFile32 (KERNEL32.396)
956 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
958 return FILE_DoOpenFile( name, ofs, mode, TRUE );
962 /***********************************************************************
963 * FILE_InitProcessDosHandles
965 * Allocates the default DOS handles for a process. Called either by
966 * AllocDosHandle below or by the DOSVM stuff.
968 BOOL FILE_InitProcessDosHandles( void ) {
969 HANDLE *ptr;
971 if (!(ptr = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY,
972 sizeof(*ptr) * DOS_TABLE_SIZE )))
973 return FALSE;
974 PROCESS_Current()->dos_handles = ptr;
975 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
976 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
977 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
978 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
979 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
980 return TRUE;
983 /***********************************************************************
984 * FILE_AllocDosHandle
986 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
987 * longer valid after this function (even on failure).
989 HFILE16 FILE_AllocDosHandle( HANDLE handle )
991 int i;
992 HANDLE *ptr = PROCESS_Current()->dos_handles;
994 if (!handle || (handle == INVALID_HANDLE_VALUE))
995 return INVALID_HANDLE_VALUE16;
997 if (!ptr) {
998 if (!FILE_InitProcessDosHandles())
999 goto error;
1000 ptr = PROCESS_Current()->dos_handles;
1003 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
1004 if (!*ptr)
1006 *ptr = handle;
1007 TRACE("Got %d for h32 %d\n", i, handle );
1008 return i;
1010 error:
1011 CloseHandle( handle );
1012 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1013 return INVALID_HANDLE_VALUE16;
1017 /***********************************************************************
1018 * FILE_GetHandle32
1020 * Return the Win32 handle for a DOS handle.
1022 HANDLE FILE_GetHandle( HFILE16 hfile )
1024 HANDLE *table = PROCESS_Current()->dos_handles;
1025 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1027 SetLastError( ERROR_INVALID_HANDLE );
1028 return INVALID_HANDLE_VALUE;
1030 return table[hfile];
1034 /***********************************************************************
1035 * FILE_Dup2
1037 * dup2() function for DOS handles.
1039 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1041 HANDLE *table = PROCESS_Current()->dos_handles;
1042 HANDLE new_handle;
1044 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1045 !table || !table[hFile1])
1047 SetLastError( ERROR_INVALID_HANDLE );
1048 return HFILE_ERROR16;
1050 if (hFile2 < 5)
1052 FIXME("stdio handle closed, need proper conversion\n" );
1053 SetLastError( ERROR_INVALID_HANDLE );
1054 return HFILE_ERROR16;
1056 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1057 GetCurrentProcess(), &new_handle,
1058 0, FALSE, DUPLICATE_SAME_ACCESS ))
1059 return HFILE_ERROR16;
1060 if (table[hFile2]) CloseHandle( table[hFile2] );
1061 table[hFile2] = new_handle;
1062 return hFile2;
1066 /***********************************************************************
1067 * _lclose16 (KERNEL.81)
1069 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1071 HANDLE *table = PROCESS_Current()->dos_handles;
1073 if (hFile < 5)
1075 FIXME("stdio handle closed, need proper conversion\n" );
1076 SetLastError( ERROR_INVALID_HANDLE );
1077 return HFILE_ERROR16;
1079 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1081 SetLastError( ERROR_INVALID_HANDLE );
1082 return HFILE_ERROR16;
1084 TRACE("%d (handle32=%d)\n", hFile, table[hFile] );
1085 CloseHandle( table[hFile] );
1086 table[hFile] = 0;
1087 return 0;
1091 /***********************************************************************
1092 * _lclose32 (KERNEL32.592)
1094 HFILE WINAPI _lclose( HFILE hFile )
1096 TRACE("handle %d\n", hFile );
1097 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1101 /***********************************************************************
1102 * ReadFile (KERNEL32.428)
1104 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1105 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1107 struct get_read_fd_request *req = get_req_buffer();
1108 int unix_handle, result;
1110 TRACE("%d %p %ld\n", hFile, buffer, bytesToRead );
1112 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1113 if (!bytesToRead) return TRUE;
1115 req->handle = hFile;
1116 server_call_fd( REQ_GET_READ_FD, -1, &unix_handle );
1117 if (unix_handle == -1) return FALSE;
1118 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1120 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1121 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1122 FILE_SetDosError();
1123 break;
1125 close( unix_handle );
1126 if (result == -1) return FALSE;
1127 if (bytesRead) *bytesRead = result;
1128 return TRUE;
1132 /***********************************************************************
1133 * WriteFile (KERNEL32.578)
1135 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1136 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1138 struct get_write_fd_request *req = get_req_buffer();
1139 int unix_handle, result;
1141 TRACE("%d %p %ld\n", hFile, buffer, bytesToWrite );
1143 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1144 if (!bytesToWrite) return TRUE;
1146 req->handle = hFile;
1147 server_call_fd( REQ_GET_WRITE_FD, -1, &unix_handle );
1148 if (unix_handle == -1) return FALSE;
1149 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1151 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1152 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1153 if (errno == ENOSPC)
1154 SetLastError( ERROR_DISK_FULL );
1155 else
1156 FILE_SetDosError();
1157 break;
1159 close( unix_handle );
1160 if (result == -1) return FALSE;
1161 if (bytesWritten) *bytesWritten = result;
1162 return TRUE;
1166 /***********************************************************************
1167 * WIN16_hread
1169 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1171 LONG maxlen;
1173 TRACE("%d %08lx %ld\n",
1174 hFile, (DWORD)buffer, count );
1176 /* Some programs pass a count larger than the allocated buffer */
1177 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1178 if (count > maxlen) count = maxlen;
1179 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1183 /***********************************************************************
1184 * WIN16_lread
1186 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1188 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1192 /***********************************************************************
1193 * _lread32 (KERNEL32.596)
1195 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1197 DWORD result;
1198 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1199 return result;
1203 /***********************************************************************
1204 * _lread16 (KERNEL.82)
1206 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1208 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1212 /***********************************************************************
1213 * _lcreat16 (KERNEL.83)
1215 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1217 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1221 /***********************************************************************
1222 * _lcreat (KERNEL32.593)
1224 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1226 /* Mask off all flags not explicitly allowed by the doc */
1227 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1228 TRACE("%s %02x\n", path, attr );
1229 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1230 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1231 CREATE_ALWAYS, attr, -1 );
1235 /***********************************************************************
1236 * SetFilePointer (KERNEL32.492)
1238 DWORD WINAPI SetFilePointer( HFILE hFile, LONG distance, LONG *highword,
1239 DWORD method )
1241 struct set_file_pointer_request *req = get_req_buffer();
1243 if (highword && *highword)
1245 FIXME("64-bit offsets not supported yet\n");
1246 SetLastError( ERROR_INVALID_PARAMETER );
1247 return 0xffffffff;
1249 TRACE("handle %d offset %ld origin %ld\n",
1250 hFile, distance, method );
1252 req->handle = hFile;
1253 req->low = distance;
1254 req->high = highword ? *highword : 0;
1255 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1256 req->whence = method;
1257 SetLastError( 0 );
1258 if (server_call( REQ_SET_FILE_POINTER )) return 0xffffffff;
1259 if (highword) *highword = req->new_high;
1260 return req->new_low;
1264 /***********************************************************************
1265 * _llseek16 (KERNEL.84)
1267 * FIXME:
1268 * Seeking before the start of the file should be allowed for _llseek16,
1269 * but cause subsequent I/O operations to fail (cf. interrupt list)
1272 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1274 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1278 /***********************************************************************
1279 * _llseek32 (KERNEL32.594)
1281 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1283 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1287 /***********************************************************************
1288 * _lopen16 (KERNEL.85)
1290 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1292 return FILE_AllocDosHandle( _lopen( path, mode ) );
1296 /***********************************************************************
1297 * _lopen32 (KERNEL32.595)
1299 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1301 DWORD access, sharing;
1303 TRACE("('%s',%04x)\n", path, mode );
1304 FILE_ConvertOFMode( mode, &access, &sharing );
1305 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1309 /***********************************************************************
1310 * _lwrite16 (KERNEL.86)
1312 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1314 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1317 /***********************************************************************
1318 * _lwrite32 (KERNEL32.761)
1320 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1322 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1326 /***********************************************************************
1327 * _hread16 (KERNEL.349)
1329 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1331 return _lread( FILE_GetHandle(hFile), buffer, count );
1335 /***********************************************************************
1336 * _hread32 (KERNEL32.590)
1338 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1340 return _lread( hFile, buffer, count );
1344 /***********************************************************************
1345 * _hwrite16 (KERNEL.350)
1347 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1349 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1353 /***********************************************************************
1354 * _hwrite32 (KERNEL32.591)
1356 * experimentation yields that _lwrite:
1357 * o truncates the file at the current position with
1358 * a 0 len write
1359 * o returns 0 on a 0 length write
1360 * o works with console handles
1363 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1365 DWORD result;
1367 TRACE("%d %p %ld\n", handle, buffer, count );
1369 if (!count)
1371 /* Expand or truncate at current position */
1372 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1373 return 0;
1375 if (!WriteFile( handle, buffer, count, &result, NULL ))
1376 return HFILE_ERROR;
1377 return result;
1381 /***********************************************************************
1382 * SetHandleCount16 (KERNEL.199)
1384 UINT16 WINAPI SetHandleCount16( UINT16 count )
1386 HGLOBAL16 hPDB = GetCurrentPDB16();
1387 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1388 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1390 TRACE("(%d)\n", count );
1392 if (count < 20) count = 20; /* No point in going below 20 */
1393 else if (count > 254) count = 254;
1395 if (count == 20)
1397 if (pdb->nbFiles > 20)
1399 memcpy( pdb->fileHandles, files, 20 );
1400 GlobalFree16( pdb->hFileHandles );
1401 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1402 GlobalHandleToSel16( hPDB ) );
1403 pdb->hFileHandles = 0;
1404 pdb->nbFiles = 20;
1407 else /* More than 20, need a new file handles table */
1409 BYTE *newfiles;
1410 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1411 if (!newhandle)
1413 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1414 return pdb->nbFiles;
1416 newfiles = (BYTE *)GlobalLock16( newhandle );
1418 if (count > pdb->nbFiles)
1420 memcpy( newfiles, files, pdb->nbFiles );
1421 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1423 else memcpy( newfiles, files, count );
1424 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1425 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1426 pdb->hFileHandles = newhandle;
1427 pdb->nbFiles = count;
1429 return pdb->nbFiles;
1433 /*************************************************************************
1434 * SetHandleCount32 (KERNEL32.494)
1436 UINT WINAPI SetHandleCount( UINT count )
1438 return MIN( 256, count );
1442 /***********************************************************************
1443 * FlushFileBuffers (KERNEL32.133)
1445 BOOL WINAPI FlushFileBuffers( HFILE hFile )
1447 struct flush_file_request *req = get_req_buffer();
1448 req->handle = hFile;
1449 return !server_call( REQ_FLUSH_FILE );
1453 /**************************************************************************
1454 * SetEndOfFile (KERNEL32.483)
1456 BOOL WINAPI SetEndOfFile( HFILE hFile )
1458 struct truncate_file_request *req = get_req_buffer();
1459 req->handle = hFile;
1460 return !server_call( REQ_TRUNCATE_FILE );
1464 /***********************************************************************
1465 * DeleteFile16 (KERNEL.146)
1467 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1469 return DeleteFileA( path );
1473 /***********************************************************************
1474 * DeleteFile32A (KERNEL32.71)
1476 BOOL WINAPI DeleteFileA( LPCSTR path )
1478 DOS_FULL_NAME full_name;
1480 TRACE("'%s'\n", path );
1482 if (!*path)
1484 ERR("Empty path passed\n");
1485 return FALSE;
1487 if (DOSFS_GetDevice( path ))
1489 WARN("cannot remove DOS device '%s'!\n", path);
1490 SetLastError( ERROR_FILE_NOT_FOUND );
1491 return FALSE;
1494 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1495 if (unlink( full_name.long_name ) == -1)
1497 FILE_SetDosError();
1498 return FALSE;
1500 return TRUE;
1504 /***********************************************************************
1505 * DeleteFile32W (KERNEL32.72)
1507 BOOL WINAPI DeleteFileW( LPCWSTR path )
1509 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1510 BOOL ret = DeleteFileA( xpath );
1511 HeapFree( GetProcessHeap(), 0, xpath );
1512 return ret;
1516 /***********************************************************************
1517 * FILE_dommap
1519 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1520 DWORD size_high, DWORD size_low,
1521 DWORD offset_high, DWORD offset_low,
1522 int prot, int flags )
1524 int fd = -1;
1525 int pos;
1526 LPVOID ret;
1528 if (size_high || offset_high)
1529 FIXME("offsets larger than 4Gb not supported\n");
1531 if (unix_handle == -1)
1533 #ifdef MAP_ANON
1534 flags |= MAP_ANON;
1535 #else
1536 static int fdzero = -1;
1538 if (fdzero == -1)
1540 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1542 perror( "/dev/zero: open" );
1543 exit(1);
1546 fd = fdzero;
1547 #endif /* MAP_ANON */
1548 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1549 #ifdef MAP_SHARED
1550 flags &= ~MAP_SHARED;
1551 #endif
1552 #ifdef MAP_PRIVATE
1553 flags |= MAP_PRIVATE;
1554 #endif
1556 else fd = unix_handle;
1558 if ((ret = mmap( start, size_low, prot,
1559 flags, fd, offset_low )) != (LPVOID)-1)
1560 return ret;
1562 /* mmap() failed; if this is because the file offset is not */
1563 /* page-aligned (EINVAL), or because the underlying filesystem */
1564 /* does not support mmap() (ENOEXEC), we do it by hand. */
1566 if (unix_handle == -1) return ret;
1567 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1568 if (prot & PROT_WRITE)
1570 /* We cannot fake shared write mappings */
1571 #ifdef MAP_SHARED
1572 if (flags & MAP_SHARED) return ret;
1573 #endif
1574 #ifdef MAP_PRIVATE
1575 if (!(flags & MAP_PRIVATE)) return ret;
1576 #endif
1578 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1579 /* Reserve the memory with an anonymous mmap */
1580 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1581 PROT_READ | PROT_WRITE, flags );
1582 if (ret == (LPVOID)-1) return ret;
1583 /* Now read in the file */
1584 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1586 FILE_munmap( ret, size_high, size_low );
1587 return (LPVOID)-1;
1589 read( fd, ret, size_low );
1590 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1591 mprotect( ret, size_low, prot ); /* Set the right protection */
1592 return ret;
1596 /***********************************************************************
1597 * FILE_munmap
1599 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1601 if (size_high)
1602 FIXME("offsets larger than 4Gb not supported\n");
1603 return munmap( start, size_low );
1607 /***********************************************************************
1608 * GetFileType (KERNEL32.222)
1610 DWORD WINAPI GetFileType( HFILE hFile )
1612 struct get_file_info_request *req = get_req_buffer();
1613 req->handle = hFile;
1614 if (server_call( REQ_GET_FILE_INFO )) return FILE_TYPE_UNKNOWN;
1615 return req->type;
1619 /**************************************************************************
1620 * MoveFileEx32A (KERNEL32.???)
1622 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1624 DOS_FULL_NAME full_name1, full_name2;
1625 int mode=0; /* mode == 1: use copy */
1627 TRACE("(%s,%s,%04lx)\n", fn1, fn2, flag);
1629 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1630 if (fn2) { /* !fn2 means delete fn1 */
1631 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1632 /* Source name and target path are valid */
1633 if ( full_name1.drive != full_name2.drive)
1635 /* use copy, if allowed */
1636 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1637 /* FIXME: Use right error code */
1638 SetLastError( ERROR_FILE_EXISTS );
1639 return FALSE;
1641 else mode =1;
1643 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1644 /* target exists, check if we may overwrite */
1645 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1646 /* FIXME: Use right error code */
1647 SetLastError( ERROR_ACCESS_DENIED );
1648 return FALSE;
1651 else /* fn2 == NULL means delete source */
1652 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1653 if (flag & MOVEFILE_COPY_ALLOWED) {
1654 WARN("Illegal flag\n");
1655 SetLastError( ERROR_GEN_FAILURE );
1656 return FALSE;
1658 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1659 Perhaps we should queue these command and execute it
1660 when exiting... What about using on_exit(2)
1662 FIXME("Please delete file '%s' when Wine has finished\n",
1663 full_name1.long_name);
1664 return TRUE;
1666 else if (unlink( full_name1.long_name ) == -1)
1668 FILE_SetDosError();
1669 return FALSE;
1671 else return TRUE; /* successfully deleted */
1673 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1674 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1675 Perhaps we should queue these command and execute it
1676 when exiting... What about using on_exit(2)
1678 FIXME("Please move existing file '%s' to file '%s'"
1679 "when Wine has finished\n",
1680 full_name1.long_name, full_name2.long_name);
1681 return TRUE;
1684 if (!mode) /* move the file */
1685 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1687 FILE_SetDosError();
1688 return FALSE;
1690 else return TRUE;
1691 else /* copy File */
1692 return CopyFileA(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1696 /**************************************************************************
1697 * MoveFileEx32W (KERNEL32.???)
1699 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1701 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1702 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1703 BOOL res = MoveFileExA( afn1, afn2, flag );
1704 HeapFree( GetProcessHeap(), 0, afn1 );
1705 HeapFree( GetProcessHeap(), 0, afn2 );
1706 return res;
1710 /**************************************************************************
1711 * MoveFile32A (KERNEL32.387)
1713 * Move file or directory
1715 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1717 DOS_FULL_NAME full_name1, full_name2;
1718 struct stat fstat;
1720 TRACE("(%s,%s)\n", fn1, fn2 );
1722 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1723 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1724 /* The new name must not already exist */
1725 return FALSE;
1726 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1728 if (full_name1.drive == full_name2.drive) /* move */
1729 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1731 FILE_SetDosError();
1732 return FALSE;
1734 else return TRUE;
1735 else /*copy */ {
1736 if (stat( full_name1.long_name, &fstat ))
1738 WARN("Invalid source file %s\n",
1739 full_name1.long_name);
1740 FILE_SetDosError();
1741 return FALSE;
1743 if (S_ISDIR(fstat.st_mode)) {
1744 /* No Move for directories across file systems */
1745 /* FIXME: Use right error code */
1746 SetLastError( ERROR_GEN_FAILURE );
1747 return FALSE;
1749 else
1750 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1755 /**************************************************************************
1756 * MoveFile32W (KERNEL32.390)
1758 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1760 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1761 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1762 BOOL res = MoveFileA( afn1, afn2 );
1763 HeapFree( GetProcessHeap(), 0, afn1 );
1764 HeapFree( GetProcessHeap(), 0, afn2 );
1765 return res;
1769 /**************************************************************************
1770 * CopyFile32A (KERNEL32.36)
1772 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1774 HFILE h1, h2;
1775 BY_HANDLE_FILE_INFORMATION info;
1776 UINT count;
1777 BOOL ret = FALSE;
1778 int mode;
1779 char buffer[2048];
1781 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1782 if (!GetFileInformationByHandle( h1, &info ))
1784 CloseHandle( h1 );
1785 return FALSE;
1787 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1788 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1789 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1790 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1792 CloseHandle( h1 );
1793 return FALSE;
1795 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1797 char *p = buffer;
1798 while (count > 0)
1800 INT res = _lwrite( h2, p, count );
1801 if (res <= 0) goto done;
1802 p += res;
1803 count -= res;
1806 ret = TRUE;
1807 done:
1808 CloseHandle( h1 );
1809 CloseHandle( h2 );
1810 return ret;
1814 /**************************************************************************
1815 * CopyFile32W (KERNEL32.37)
1817 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1819 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1820 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1821 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1822 HeapFree( GetProcessHeap(), 0, sourceA );
1823 HeapFree( GetProcessHeap(), 0, destA );
1824 return ret;
1828 /**************************************************************************
1829 * CopyFileEx32A (KERNEL32.858)
1831 * This implementation ignores most of the extra parameters passed-in into
1832 * the "ex" version of the method and calls the CopyFile method.
1833 * It will have to be fixed eventually.
1835 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1836 LPCSTR destFilename,
1837 LPPROGRESS_ROUTINE progressRoutine,
1838 LPVOID appData,
1839 LPBOOL cancelFlagPointer,
1840 DWORD copyFlags)
1842 BOOL failIfExists = FALSE;
1845 * Interpret the only flag that CopyFile can interpret.
1847 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1849 failIfExists = TRUE;
1852 return CopyFileA(sourceFilename, destFilename, failIfExists);
1855 /**************************************************************************
1856 * CopyFileEx32W (KERNEL32.859)
1858 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1859 LPCWSTR destFilename,
1860 LPPROGRESS_ROUTINE progressRoutine,
1861 LPVOID appData,
1862 LPBOOL cancelFlagPointer,
1863 DWORD copyFlags)
1865 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1866 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1868 BOOL ret = CopyFileExA(sourceA,
1869 destA,
1870 progressRoutine,
1871 appData,
1872 cancelFlagPointer,
1873 copyFlags);
1875 HeapFree( GetProcessHeap(), 0, sourceA );
1876 HeapFree( GetProcessHeap(), 0, destA );
1878 return ret;
1882 /***********************************************************************
1883 * SetFileTime (KERNEL32.650)
1885 BOOL WINAPI SetFileTime( HFILE hFile,
1886 const FILETIME *lpCreationTime,
1887 const FILETIME *lpLastAccessTime,
1888 const FILETIME *lpLastWriteTime )
1890 struct set_file_time_request *req = get_req_buffer();
1892 req->handle = hFile;
1893 if (lpLastAccessTime)
1894 req->access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1895 else
1896 req->access_time = 0; /* FIXME */
1897 if (lpLastWriteTime)
1898 req->write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1899 else
1900 req->write_time = 0; /* FIXME */
1901 return !server_call( REQ_SET_FILE_TIME );
1905 /**************************************************************************
1906 * LockFile (KERNEL32.511)
1908 BOOL WINAPI LockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1909 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1911 struct lock_file_request *req = get_req_buffer();
1913 req->handle = hFile;
1914 req->offset_low = dwFileOffsetLow;
1915 req->offset_high = dwFileOffsetHigh;
1916 req->count_low = nNumberOfBytesToLockLow;
1917 req->count_high = nNumberOfBytesToLockHigh;
1918 return !server_call( REQ_LOCK_FILE );
1921 /**************************************************************************
1922 * LockFileEx [KERNEL32.512]
1924 * Locks a byte range within an open file for shared or exclusive access.
1926 * RETURNS
1927 * success: TRUE
1928 * failure: FALSE
1929 * NOTES
1931 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1933 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1934 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1935 LPOVERLAPPED pOverlapped )
1937 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1938 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
1939 pOverlapped);
1940 if (reserved == 0)
1941 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1942 else
1944 ERR("reserved == %ld: Supposed to be 0??\n", reserved);
1945 SetLastError(ERROR_INVALID_PARAMETER);
1948 return FALSE;
1952 /**************************************************************************
1953 * UnlockFile (KERNEL32.703)
1955 BOOL WINAPI UnlockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1956 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1958 struct unlock_file_request *req = get_req_buffer();
1960 req->handle = hFile;
1961 req->offset_low = dwFileOffsetLow;
1962 req->offset_high = dwFileOffsetHigh;
1963 req->count_low = nNumberOfBytesToUnlockLow;
1964 req->count_high = nNumberOfBytesToUnlockHigh;
1965 return !server_call( REQ_UNLOCK_FILE );
1969 /**************************************************************************
1970 * UnlockFileEx (KERNEL32.705)
1972 BOOL WINAPI UnlockFileEx(
1973 HFILE hFile,
1974 DWORD dwReserved,
1975 DWORD nNumberOfBytesToUnlockLow,
1976 DWORD nNumberOfBytesToUnlockHigh,
1977 LPOVERLAPPED lpOverlapped
1980 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1981 hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh,
1982 lpOverlapped);
1983 if (dwReserved == 0)
1984 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1985 else
1987 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved);
1988 SetLastError(ERROR_INVALID_PARAMETER);
1991 return FALSE;
1995 #if 0
1997 struct DOS_FILE_LOCK {
1998 struct DOS_FILE_LOCK * next;
1999 DWORD base;
2000 DWORD len;
2001 DWORD processId;
2002 FILE_OBJECT * dos_file;
2003 /* char * unix_name;*/
2006 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2008 static DOS_FILE_LOCK *locks = NULL;
2009 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2012 /* Locks need to be mirrored because unix file locking is based
2013 * on the pid. Inside of wine there can be multiple WINE processes
2014 * that share the same unix pid.
2015 * Read's and writes should check these locks also - not sure
2016 * how critical that is at this point (FIXME).
2019 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2021 DOS_FILE_LOCK *curr;
2022 DWORD processId;
2024 processId = GetCurrentProcessId();
2026 /* check if lock overlaps a current lock for the same file */
2027 #if 0
2028 for (curr = locks; curr; curr = curr->next) {
2029 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2030 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2031 return TRUE;/* region is identic */
2032 if ((f->l_start < (curr->base + curr->len)) &&
2033 ((f->l_start + f->l_len) > curr->base)) {
2034 /* region overlaps */
2035 return FALSE;
2039 #endif
2041 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2042 curr->processId = GetCurrentProcessId();
2043 curr->base = f->l_start;
2044 curr->len = f->l_len;
2045 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
2046 curr->next = locks;
2047 curr->dos_file = file;
2048 locks = curr;
2049 return TRUE;
2052 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2054 DWORD processId;
2055 DOS_FILE_LOCK **curr;
2056 DOS_FILE_LOCK *rem;
2058 processId = GetCurrentProcessId();
2059 curr = &locks;
2060 while (*curr) {
2061 if ((*curr)->dos_file == file) {
2062 rem = *curr;
2063 *curr = (*curr)->next;
2064 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2065 HeapFree( SystemHeap, 0, rem );
2067 else
2068 curr = &(*curr)->next;
2072 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2074 DWORD processId;
2075 DOS_FILE_LOCK **curr;
2076 DOS_FILE_LOCK *rem;
2078 processId = GetCurrentProcessId();
2079 for (curr = &locks; *curr; curr = &(*curr)->next) {
2080 if ((*curr)->processId == processId &&
2081 (*curr)->dos_file == file &&
2082 (*curr)->base == f->l_start &&
2083 (*curr)->len == f->l_len) {
2084 /* this is the same lock */
2085 rem = *curr;
2086 *curr = (*curr)->next;
2087 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2088 HeapFree( SystemHeap, 0, rem );
2089 return TRUE;
2092 /* no matching lock found */
2093 return FALSE;
2097 /**************************************************************************
2098 * LockFile (KERNEL32.511)
2100 BOOL WINAPI LockFile(
2101 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2102 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2104 struct flock f;
2105 FILE_OBJECT *file;
2107 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2108 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2109 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2111 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2112 FIXME("Unimplemented bytes > 32bits\n");
2113 return FALSE;
2116 f.l_start = dwFileOffsetLow;
2117 f.l_len = nNumberOfBytesToLockLow;
2118 f.l_whence = SEEK_SET;
2119 f.l_pid = 0;
2120 f.l_type = F_WRLCK;
2122 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2124 /* shadow locks internally */
2125 if (!DOS_AddLock(file, &f)) {
2126 SetLastError( ERROR_LOCK_VIOLATION );
2127 return FALSE;
2130 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2131 #ifdef USE_UNIX_LOCKS
2132 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2133 if (errno == EACCES || errno == EAGAIN) {
2134 SetLastError( ERROR_LOCK_VIOLATION );
2136 else {
2137 FILE_SetDosError();
2139 /* remove our internal copy of the lock */
2140 DOS_RemoveLock(file, &f);
2141 return FALSE;
2143 #endif
2144 return TRUE;
2148 /**************************************************************************
2149 * UnlockFile (KERNEL32.703)
2151 BOOL WINAPI UnlockFile(
2152 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2153 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2155 FILE_OBJECT *file;
2156 struct flock f;
2158 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2159 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2160 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2162 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2163 WARN("Unimplemented bytes > 32bits\n");
2164 return FALSE;
2167 f.l_start = dwFileOffsetLow;
2168 f.l_len = nNumberOfBytesToUnlockLow;
2169 f.l_whence = SEEK_SET;
2170 f.l_pid = 0;
2171 f.l_type = F_UNLCK;
2173 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2175 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2177 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2178 #ifdef USE_UNIX_LOCKS
2179 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2180 FILE_SetDosError();
2181 return FALSE;
2183 #endif
2184 return TRUE;
2186 #endif
2188 /**************************************************************************
2189 * GetFileAttributesEx32A [KERNEL32.874]
2191 BOOL WINAPI GetFileAttributesExA(
2192 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2193 LPVOID lpFileInformation)
2195 DOS_FULL_NAME full_name;
2196 BY_HANDLE_FILE_INFORMATION info;
2198 if (lpFileName == NULL) return FALSE;
2199 if (lpFileInformation == NULL) return FALSE;
2201 if (fInfoLevelId == GetFileExInfoStandard) {
2202 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2203 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2204 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2205 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2207 lpFad->dwFileAttributes = info.dwFileAttributes;
2208 lpFad->ftCreationTime = info.ftCreationTime;
2209 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2210 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2211 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2212 lpFad->nFileSizeLow = info.nFileSizeLow;
2214 else {
2215 FIXME("invalid info level %d!\n", fInfoLevelId);
2216 return FALSE;
2219 return TRUE;
2223 /**************************************************************************
2224 * GetFileAttributesEx32W [KERNEL32.875]
2226 BOOL WINAPI GetFileAttributesExW(
2227 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2228 LPVOID lpFileInformation)
2230 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2231 BOOL res =
2232 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2233 HeapFree( GetProcessHeap(), 0, nameA );
2234 return res;