Added COMPOBJ.DllEntryPoint (Acrobat3 16bit needs it).
[wine.git] / files / file.c
blobd7e8558250bd3dabcd7dc4e495cec5e024b8e12c
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 <assert.h>
13 #include <ctype.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/errno.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/time.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <utime.h>
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/winbase16.h"
31 #include "wine/winestring.h"
32 #include "drive.h"
33 #include "device.h"
34 #include "file.h"
35 #include "global.h"
36 #include "heap.h"
37 #include "msdos.h"
38 #include "options.h"
39 #include "ldt.h"
40 #include "process.h"
41 #include "task.h"
42 #include "async.h"
43 #include "wincon.h"
44 #include "debug.h"
46 #include "server.h"
48 DEFAULT_DEBUG_CHANNEL(file)
50 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
51 #define MAP_ANON MAP_ANONYMOUS
52 #endif
54 /* Size of per-process table of DOS handles */
55 #define DOS_TABLE_SIZE 256
58 /***********************************************************************
59 * FILE_ConvertOFMode
61 * Convert OF_* mode into flags for CreateFile.
63 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
65 switch(mode & 0x03)
67 case OF_READ: *access = GENERIC_READ; break;
68 case OF_WRITE: *access = GENERIC_WRITE; break;
69 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
70 default: *access = 0; break;
72 switch(mode & 0x70)
74 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
75 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
76 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
77 case OF_SHARE_DENY_NONE:
78 case OF_SHARE_COMPAT:
79 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
84 #if 0
85 /***********************************************************************
86 * FILE_ShareDeny
88 * PARAMS
89 * oldmode[I] mode how file was first opened
90 * mode[I] mode how the file should get opened
91 * RETURNS
92 * TRUE: deny open
93 * FALSE: allow open
95 * Look what we have to do with the given SHARE modes
97 * Ralph Brown's interrupt list gives following explication, I guess
98 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
100 * FIXME: Validate this function
101 ========from Ralph Brown's list =========
102 (Table 0750)
103 Values of DOS file sharing behavior:
104 | Second and subsequent Opens
105 First |Compat Deny Deny Deny Deny
106 Open | All Write Read None
107 |R W RW R W RW R W RW R W RW R W RW
108 - - - - -| - - - - - - - - - - - - - - - - -
109 Compat R |Y Y Y N N N 1 N N N N N 1 N N
110 W |Y Y Y N N N N N N N N N N N N
111 RW|Y Y Y N N N N N N N N N N N N
112 - - - - -|
113 Deny R |C C C N N N N N N N N N N N N
114 All W |C C C N N N N N N N N N N N N
115 RW|C C C N N N N N N N N N N N N
116 - - - - -|
117 Deny R |2 C C N N N Y N N N N N Y N N
118 Write W |C C C N N N N N N Y N N Y N N
119 RW|C C C N N N N N N N N N Y N N
120 - - - - -|
121 Deny R |C C C N N N N Y N N N N N Y N
122 Read W |C C C N N N N N N N Y N N Y N
123 RW|C C C N N N N N N N N N N Y N
124 - - - - -|
125 Deny R |2 C C N N N Y Y Y N N N Y Y Y
126 None W |C C C N N N N N N Y Y Y Y Y Y
127 RW|C C C N N N N N N N N N Y Y Y
128 Legend: Y = open succeeds, N = open fails with error code 05h
129 C = open fails, INT 24 generated
130 1 = open succeeds if file read-only, else fails with error code
131 2 = open succeeds if file read-only, else fails with INT 24
132 ========end of description from Ralph Brown's List =====
133 For every "Y" in the table we return FALSE
134 For every "N" we set the DOS_ERROR and return TRUE
135 For all other cases we barf,set the DOS_ERROR and return TRUE
138 static BOOL FILE_ShareDeny( int mode, int oldmode)
140 int oldsharemode = oldmode & 0x70;
141 int sharemode = mode & 0x70;
142 int oldopenmode = oldmode & 3;
143 int openmode = mode & 3;
145 switch (oldsharemode)
147 case OF_SHARE_COMPAT:
148 if (sharemode == OF_SHARE_COMPAT) return FALSE;
149 if (openmode == OF_READ) goto test_ro_err05 ;
150 goto fail_error05;
151 case OF_SHARE_EXCLUSIVE:
152 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
153 goto fail_error05;
154 case OF_SHARE_DENY_WRITE:
155 if (openmode != OF_READ)
157 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
158 goto fail_error05;
160 switch (sharemode)
162 case OF_SHARE_COMPAT:
163 if (oldopenmode == OF_READ) goto test_ro_int24 ;
164 goto fail_int24;
165 case OF_SHARE_DENY_NONE :
166 return FALSE;
167 case OF_SHARE_DENY_WRITE :
168 if (oldopenmode == OF_READ) return FALSE;
169 case OF_SHARE_DENY_READ :
170 if (oldopenmode == OF_WRITE) return FALSE;
171 case OF_SHARE_EXCLUSIVE:
172 default:
173 goto fail_error05;
175 break;
176 case OF_SHARE_DENY_READ:
177 if (openmode != OF_WRITE)
179 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
180 goto fail_error05;
182 switch (sharemode)
184 case OF_SHARE_COMPAT:
185 goto fail_int24;
186 case OF_SHARE_DENY_NONE :
187 return FALSE;
188 case OF_SHARE_DENY_WRITE :
189 if (oldopenmode == OF_READ) return FALSE;
190 case OF_SHARE_DENY_READ :
191 if (oldopenmode == OF_WRITE) return FALSE;
192 case OF_SHARE_EXCLUSIVE:
193 default:
194 goto fail_error05;
196 break;
197 case OF_SHARE_DENY_NONE:
198 switch (sharemode)
200 case OF_SHARE_COMPAT:
201 goto fail_int24;
202 case OF_SHARE_DENY_NONE :
203 return FALSE;
204 case OF_SHARE_DENY_WRITE :
205 if (oldopenmode == OF_READ) return FALSE;
206 case OF_SHARE_DENY_READ :
207 if (oldopenmode == OF_WRITE) return FALSE;
208 case OF_SHARE_EXCLUSIVE:
209 default:
210 goto fail_error05;
212 default:
213 ERR(file,"unknown mode\n");
215 ERR(file,"shouldn't happen\n");
216 ERR(file,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
217 return TRUE;
219 test_ro_int24:
220 if (oldmode == OF_READ)
221 return FALSE;
222 /* Fall through */
223 fail_int24:
224 FIXME(file,"generate INT24 missing\n");
225 /* Is this the right error? */
226 SetLastError( ERROR_ACCESS_DENIED );
227 return TRUE;
229 test_ro_err05:
230 if (oldmode == OF_READ)
231 return FALSE;
232 /* fall through */
233 fail_error05:
234 TRACE(file,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
235 SetLastError( ERROR_ACCESS_DENIED );
236 return TRUE;
238 #endif
241 /***********************************************************************
242 * FILE_SetDosError
244 * Set the DOS error code from errno.
246 void FILE_SetDosError(void)
248 int save_errno = errno; /* errno gets overwritten by printf */
250 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
251 switch (save_errno)
253 case EAGAIN:
254 SetLastError( ERROR_SHARING_VIOLATION );
255 break;
256 case EBADF:
257 SetLastError( ERROR_INVALID_HANDLE );
258 break;
259 case ENOSPC:
260 SetLastError( ERROR_HANDLE_DISK_FULL );
261 break;
262 case EACCES:
263 case EPERM:
264 case EROFS:
265 SetLastError( ERROR_ACCESS_DENIED );
266 break;
267 case EBUSY:
268 SetLastError( ERROR_LOCK_VIOLATION );
269 break;
270 case ENOENT:
271 SetLastError( ERROR_FILE_NOT_FOUND );
272 break;
273 case EISDIR:
274 SetLastError( ERROR_CANNOT_MAKE );
275 break;
276 case ENFILE:
277 case EMFILE:
278 SetLastError( ERROR_NO_MORE_FILES );
279 break;
280 case EEXIST:
281 SetLastError( ERROR_FILE_EXISTS );
282 break;
283 case EINVAL:
284 case ESPIPE:
285 SetLastError( ERROR_SEEK );
286 break;
287 case ENOTEMPTY:
288 SetLastError( ERROR_DIR_NOT_EMPTY );
289 break;
290 default:
291 perror( "int21: unknown errno" );
292 SetLastError( ERROR_GEN_FAILURE );
293 break;
295 errno = save_errno;
299 /***********************************************************************
300 * FILE_DupUnixHandle
302 * Duplicate a Unix handle into a task handle.
304 HFILE FILE_DupUnixHandle( int fd, DWORD access )
306 int unix_handle;
307 struct create_file_request req;
308 struct create_file_reply reply;
310 if ((unix_handle = dup(fd)) == -1)
312 FILE_SetDosError();
313 return INVALID_HANDLE_VALUE;
315 req.access = access;
316 req.inherit = 1;
317 req.sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
318 req.create = 0;
319 req.attrs = 0;
321 CLIENT_SendRequest( REQ_CREATE_FILE, unix_handle, 1,
322 &req, sizeof(req) );
323 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
324 return reply.handle;
328 /***********************************************************************
329 * FILE_CreateFile
331 * Implementation of CreateFile. Takes a Unix path name.
333 HFILE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
334 LPSECURITY_ATTRIBUTES sa, DWORD creation,
335 DWORD attributes, HANDLE template )
337 struct create_file_request req;
338 struct create_file_reply reply;
340 req.access = access;
341 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
342 req.sharing = sharing;
343 req.create = creation;
344 req.attrs = attributes;
345 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
346 &req, sizeof(req),
347 filename, strlen(filename) + 1 );
348 SetLastError(0);
349 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
351 /* If write access failed, retry without GENERIC_WRITE */
353 if ((reply.handle == -1) && !Options.failReadOnly &&
354 (access & GENERIC_WRITE))
356 DWORD lasterror = GetLastError();
357 if ((lasterror == ERROR_ACCESS_DENIED) ||
358 (lasterror == ERROR_WRITE_PROTECT))
360 req.access &= ~GENERIC_WRITE;
361 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
362 &req, sizeof(req),
363 filename, strlen(filename) + 1 );
364 SetLastError(0);
365 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
368 return reply.handle;
372 /***********************************************************************
373 * FILE_CreateDevice
375 * Same as FILE_CreateFile but for a device
377 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
379 struct create_device_request req;
380 struct create_device_reply reply;
382 req.access = access;
383 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
384 req.id = client_id;
385 CLIENT_SendRequest( REQ_CREATE_DEVICE, -1, 1, &req, sizeof(req) );
386 SetLastError(0);
387 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
388 return reply.handle;
392 /*************************************************************************
393 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
395 * Creates or opens an object, and returns a handle that can be used to
396 * access that object.
398 * PARAMS
400 * filename [I] pointer to filename to be accessed
401 * access [I] access mode requested
402 * sharing [I] share mode
403 * sa [I] pointer to security attributes
404 * creation [I] how to create the file
405 * attributes [I] attributes for newly created file
406 * template [I] handle to file with extended attributes to copy
408 * RETURNS
409 * Success: Open handle to specified file
410 * Failure: INVALID_HANDLE_VALUE
412 * NOTES
413 * Should call SetLastError() on failure.
415 * BUGS
417 * Doesn't support character devices, pipes, template files, or a
418 * lot of the 'attributes' flags yet.
420 HFILE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
421 LPSECURITY_ATTRIBUTES sa, DWORD creation,
422 DWORD attributes, HANDLE template )
424 DOS_FULL_NAME full_name;
426 if (!filename)
428 SetLastError( ERROR_INVALID_PARAMETER );
429 return HFILE_ERROR;
431 TRACE(file,"%s %s%s%s%s%s%s%s\n",filename,
432 ((access & GENERIC_READ)==GENERIC_READ)?"GENERIC_READ ":"",
433 ((access & GENERIC_WRITE)==GENERIC_WRITE)?"GENERIC_WRITE ":"",
434 (!access)?"QUERY_ACCESS ":"",
435 ((sharing & FILE_SHARE_READ)==FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
436 ((sharing & FILE_SHARE_WRITE)==FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
437 ((sharing & FILE_SHARE_DELETE)==FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
438 (creation ==CREATE_NEW)?"CREATE_NEW":
439 (creation ==CREATE_ALWAYS)?"CREATE_ALWAYS ":
440 (creation ==OPEN_EXISTING)?"OPEN_EXISTING ":
441 (creation ==OPEN_ALWAYS)?"OPEN_ALWAYS ":
442 (creation ==TRUNCATE_EXISTING)?"TRUNCATE_EXISTING ":"");
444 /* If the name starts with '\\?\', ignore the first 4 chars. */
445 if (!strncmp(filename, "\\\\?\\", 4))
447 filename += 4;
448 if (!strncmp(filename, "UNC\\", 4))
450 FIXME( file, "UNC name (%s) not supported.\n", filename );
451 SetLastError( ERROR_PATH_NOT_FOUND );
452 return HFILE_ERROR;
456 if (!strncmp(filename, "\\\\.\\", 4))
457 return DEVICE_Open( filename+4, access, sa );
459 /* If the name still starts with '\\', it's a UNC name. */
460 if (!strncmp(filename, "\\\\", 2))
462 FIXME( file, "UNC name (%s) not supported.\n", filename );
463 SetLastError( ERROR_PATH_NOT_FOUND );
464 return HFILE_ERROR;
467 /* Open a console for CONIN$ or CONOUT$ */
468 if (!lstrcmpiA(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
469 if (!lstrcmpiA(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
471 if (DOSFS_GetDevice( filename ))
473 HFILE ret;
475 TRACE(file, "opening device '%s'\n", filename );
477 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
478 return ret;
480 /* Do not silence this please. It is a critical error. -MM */
481 ERR(file, "Couldn't open device '%s'!\n",filename);
482 SetLastError( ERROR_FILE_NOT_FOUND );
483 return HFILE_ERROR;
486 /* check for filename, don't check for last entry if creating */
487 if (!DOSFS_GetFullName( filename,
488 (creation == OPEN_EXISTING) || (creation == TRUNCATE_EXISTING), &full_name ))
489 return HFILE_ERROR;
491 return FILE_CreateFile( full_name.long_name, access, sharing,
492 sa, creation, attributes, template );
497 /*************************************************************************
498 * CreateFile32W (KERNEL32.48)
500 HFILE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
501 LPSECURITY_ATTRIBUTES sa, DWORD creation,
502 DWORD attributes, HANDLE template)
504 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
505 HFILE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
506 HeapFree( GetProcessHeap(), 0, afn );
507 return res;
511 /***********************************************************************
512 * FILE_FillInfo
514 * Fill a file information from a struct stat.
516 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
518 if (S_ISDIR(st->st_mode))
519 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
520 else
521 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
522 if (!(st->st_mode & S_IWUSR))
523 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
525 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
526 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
527 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
529 info->dwVolumeSerialNumber = 0; /* FIXME */
530 info->nFileSizeHigh = 0;
531 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
532 info->nNumberOfLinks = st->st_nlink;
533 info->nFileIndexHigh = 0;
534 info->nFileIndexLow = st->st_ino;
538 /***********************************************************************
539 * FILE_Stat
541 * Stat a Unix path name. Return TRUE if OK.
543 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
545 struct stat st;
547 if (!unixName || !info) return FALSE;
549 if (stat( unixName, &st ) == -1)
551 FILE_SetDosError();
552 return FALSE;
554 FILE_FillInfo( &st, info );
555 return TRUE;
559 /***********************************************************************
560 * GetFileInformationByHandle (KERNEL32.219)
562 DWORD WINAPI GetFileInformationByHandle( HFILE hFile,
563 BY_HANDLE_FILE_INFORMATION *info )
565 struct get_file_info_request req;
566 struct get_file_info_reply reply;
568 if (!info) return 0;
569 req.handle = hFile;
570 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
571 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
572 return 0;
573 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftCreationTime, 0 );
574 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftLastWriteTime, 0 );
575 DOSFS_UnixTimeToFileTime( reply.access_time, &info->ftLastAccessTime, 0 );
576 info->dwFileAttributes = reply.attr;
577 info->dwVolumeSerialNumber = reply.serial;
578 info->nFileSizeHigh = reply.size_high;
579 info->nFileSizeLow = reply.size_low;
580 info->nNumberOfLinks = reply.links;
581 info->nFileIndexHigh = reply.index_high;
582 info->nFileIndexLow = reply.index_low;
583 return 1;
587 /**************************************************************************
588 * GetFileAttributes16 (KERNEL.420)
590 DWORD WINAPI GetFileAttributes16( LPCSTR name )
592 return GetFileAttributesA( name );
596 /**************************************************************************
597 * GetFileAttributes32A (KERNEL32.217)
599 DWORD WINAPI GetFileAttributesA( LPCSTR name )
601 DOS_FULL_NAME full_name;
602 BY_HANDLE_FILE_INFORMATION info;
604 if (name == NULL || *name=='\0') return -1;
606 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
607 if (!FILE_Stat( full_name.long_name, &info )) return -1;
608 return info.dwFileAttributes;
612 /**************************************************************************
613 * GetFileAttributes32W (KERNEL32.218)
615 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
617 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
618 DWORD res = GetFileAttributesA( nameA );
619 HeapFree( GetProcessHeap(), 0, nameA );
620 return res;
624 /***********************************************************************
625 * GetFileSize (KERNEL32.220)
627 DWORD WINAPI GetFileSize( HFILE hFile, LPDWORD filesizehigh )
629 BY_HANDLE_FILE_INFORMATION info;
630 if (!GetFileInformationByHandle( hFile, &info )) return 0;
631 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
632 return info.nFileSizeLow;
636 /***********************************************************************
637 * GetFileTime (KERNEL32.221)
639 BOOL WINAPI GetFileTime( HFILE hFile, FILETIME *lpCreationTime,
640 FILETIME *lpLastAccessTime,
641 FILETIME *lpLastWriteTime )
643 BY_HANDLE_FILE_INFORMATION info;
644 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
645 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
646 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
647 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
648 return TRUE;
651 /***********************************************************************
652 * CompareFileTime (KERNEL32.28)
654 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
656 if (!x || !y) return -1;
658 if (x->dwHighDateTime > y->dwHighDateTime)
659 return 1;
660 if (x->dwHighDateTime < y->dwHighDateTime)
661 return -1;
662 if (x->dwLowDateTime > y->dwLowDateTime)
663 return 1;
664 if (x->dwLowDateTime < y->dwLowDateTime)
665 return -1;
666 return 0;
670 /***********************************************************************
671 * GetTempFileName16 (KERNEL.97)
673 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
674 LPSTR buffer )
676 char temppath[144];
678 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
679 drive |= DRIVE_GetCurrentDrive() + 'A';
681 if ((drive & TF_FORCEDRIVE) &&
682 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
684 drive &= ~TF_FORCEDRIVE;
685 WARN(file, "invalid drive %d specified\n", drive );
688 if (drive & TF_FORCEDRIVE)
689 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
690 else
691 GetTempPathA( 132, temppath );
692 return (UINT16)GetTempFileNameA( temppath, prefix, unique, buffer );
696 /***********************************************************************
697 * GetTempFileName32A (KERNEL32.290)
699 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
700 LPSTR buffer)
702 static UINT unique_temp;
703 DOS_FULL_NAME full_name;
704 int i;
705 LPSTR p;
706 UINT num;
708 if ( !path || !prefix || !buffer ) return 0;
710 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
711 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
713 strcpy( buffer, path );
714 p = buffer + strlen(buffer);
716 /* add a \, if there isn't one and path is more than just the drive letter ... */
717 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
718 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
720 *p++ = '~';
721 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
722 sprintf( p, "%04x.tmp", num );
724 /* Now try to create it */
726 if (!unique)
730 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
731 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
732 if (handle != INVALID_HANDLE_VALUE)
733 { /* We created it */
734 TRACE(file, "created %s\n",
735 buffer);
736 CloseHandle( handle );
737 break;
739 if (GetLastError() != ERROR_FILE_EXISTS)
740 break; /* No need to go on */
741 num++;
742 sprintf( p, "%04x.tmp", num );
743 } while (num != (unique & 0xffff));
746 /* Get the full path name */
748 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
750 /* Check if we have write access in the directory */
751 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
752 if (access( full_name.long_name, W_OK ) == -1)
753 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
754 buffer);
756 TRACE(file, "returning %s\n", buffer );
757 return unique ? unique : num;
761 /***********************************************************************
762 * GetTempFileName32W (KERNEL32.291)
764 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
765 LPWSTR buffer )
767 LPSTR patha,prefixa;
768 char buffera[144];
769 UINT ret;
771 if (!path) return 0;
772 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
773 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
774 ret = GetTempFileNameA( patha, prefixa, unique, buffera );
775 lstrcpyAtoW( buffer, buffera );
776 HeapFree( GetProcessHeap(), 0, patha );
777 HeapFree( GetProcessHeap(), 0, prefixa );
778 return ret;
782 /***********************************************************************
783 * FILE_DoOpenFile
785 * Implementation of OpenFile16() and OpenFile32().
787 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
788 BOOL win32 )
790 HFILE hFileRet;
791 FILETIME filetime;
792 WORD filedatetime[2];
793 DOS_FULL_NAME full_name;
794 DWORD access, sharing;
795 char *p;
797 if (!ofs) return HFILE_ERROR;
799 TRACE(file,"%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
800 ((mode & 0x3 )==OF_READ)?"OF_READ":
801 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
802 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
803 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
804 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
805 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
806 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
807 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
808 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
809 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
810 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
811 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
812 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
813 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
814 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
815 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
816 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
820 ofs->cBytes = sizeof(OFSTRUCT);
821 ofs->nErrCode = 0;
822 if (mode & OF_REOPEN) name = ofs->szPathName;
824 if (!name) {
825 ERR(file, "called with `name' set to NULL ! Please debug.\n");
826 return HFILE_ERROR;
829 TRACE(file, "%s %04x\n", name, mode );
831 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
832 Are there any cases where getting the path here is wrong?
833 Uwe Bonnes 1997 Apr 2 */
834 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
835 ofs->szPathName, NULL )) goto error;
836 FILE_ConvertOFMode( mode, &access, &sharing );
838 /* OF_PARSE simply fills the structure */
840 if (mode & OF_PARSE)
842 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
843 != DRIVE_REMOVABLE);
844 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
845 name, ofs->szPathName );
846 return 0;
849 /* OF_CREATE is completely different from all other options, so
850 handle it first */
852 if (mode & OF_CREATE)
854 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
855 sharing, NULL, CREATE_ALWAYS,
856 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
857 goto error;
858 goto success;
861 /* If OF_SEARCH is set, ignore the given path */
863 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
865 /* First try the file name as is */
866 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
867 /* Now remove the path */
868 if (name[0] && (name[1] == ':')) name += 2;
869 if ((p = strrchr( name, '\\' ))) name = p + 1;
870 if ((p = strrchr( name, '/' ))) name = p + 1;
871 if (!name[0]) goto not_found;
874 /* Now look for the file */
876 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
878 found:
879 TRACE(file, "found %s = %s\n",
880 full_name.long_name, full_name.short_name );
881 lstrcpynA( ofs->szPathName, full_name.short_name,
882 sizeof(ofs->szPathName) );
884 if (mode & OF_SHARE_EXCLUSIVE)
885 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
886 on the file <tempdir>/_ins0432._mp to determine how
887 far installation has proceeded.
888 _ins0432._mp is an executable and while running the
889 application expects the open with OF_SHARE_ to fail*/
890 /* Probable FIXME:
891 As our loader closes the files after loading the executable,
892 we can't find the running executable with FILE_InUse.
893 Perhaps the loader should keep the file open.
894 Recheck against how Win handles that case */
896 char *last = strrchr(full_name.long_name,'/');
897 if (!last)
898 last = full_name.long_name - 1;
899 if (GetModuleHandle16(last+1))
901 TRACE(file,"Denying shared open for %s\n",full_name.long_name);
902 return HFILE_ERROR;
906 if (mode & OF_DELETE)
908 if (unlink( full_name.long_name ) == -1) goto not_found;
909 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
910 return 1;
913 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
914 NULL, OPEN_EXISTING, 0, -1 );
915 if (hFileRet == HFILE_ERROR) goto not_found;
917 GetFileTime( hFileRet, NULL, NULL, &filetime );
918 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
919 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
921 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
923 CloseHandle( hFileRet );
924 WARN(file, "(%s): OF_VERIFY failed\n", name );
925 /* FIXME: what error here? */
926 SetLastError( ERROR_FILE_NOT_FOUND );
927 goto error;
930 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
932 success: /* We get here if the open was successful */
933 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
934 if (win32)
936 if (mode & OF_EXIST) /* Return the handle, but close it first */
937 CloseHandle( hFileRet );
939 else
941 hFileRet = FILE_AllocDosHandle( hFileRet );
942 if (hFileRet == HFILE_ERROR16) goto error;
943 if (mode & OF_EXIST) /* Return the handle, but close it first */
944 _lclose16( hFileRet );
946 return hFileRet;
948 not_found: /* We get here if the file does not exist */
949 WARN(file, "'%s' not found\n", name );
950 SetLastError( ERROR_FILE_NOT_FOUND );
951 /* fall through */
953 error: /* We get here if there was an error opening the file */
954 ofs->nErrCode = GetLastError();
955 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
956 name,ofs->nErrCode );
957 return HFILE_ERROR;
961 /***********************************************************************
962 * OpenFile16 (KERNEL.74)
964 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
966 return FILE_DoOpenFile( name, ofs, mode, FALSE );
970 /***********************************************************************
971 * OpenFile32 (KERNEL32.396)
973 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
975 return FILE_DoOpenFile( name, ofs, mode, TRUE );
979 /***********************************************************************
980 * FILE_InitProcessDosHandles
982 * Allocates the default DOS handles for a process. Called either by
983 * AllocDosHandle below or by the DOSVM stuff.
985 BOOL FILE_InitProcessDosHandles( void ) {
986 HANDLE *ptr;
988 if (!(ptr = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY,
989 sizeof(*ptr) * DOS_TABLE_SIZE )))
990 return FALSE;
991 PROCESS_Current()->dos_handles = ptr;
992 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
993 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
994 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
995 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
996 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
997 return TRUE;
1000 /***********************************************************************
1001 * FILE_AllocDosHandle
1003 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1004 * longer valid after this function (even on failure).
1006 HFILE16 FILE_AllocDosHandle( HANDLE handle )
1008 int i;
1009 HANDLE *ptr = PROCESS_Current()->dos_handles;
1011 if (!handle || (handle == INVALID_HANDLE_VALUE))
1012 return INVALID_HANDLE_VALUE16;
1014 if (!ptr) {
1015 if (!FILE_InitProcessDosHandles())
1016 goto error;
1017 ptr = PROCESS_Current()->dos_handles;
1020 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
1021 if (!*ptr)
1023 *ptr = handle;
1024 TRACE( file, "Got %d for h32 %d\n", i, handle );
1025 return i;
1027 error:
1028 CloseHandle( handle );
1029 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1030 return INVALID_HANDLE_VALUE16;
1034 /***********************************************************************
1035 * FILE_GetHandle32
1037 * Return the Win32 handle for a DOS handle.
1039 HANDLE FILE_GetHandle( HFILE16 hfile )
1041 HANDLE *table = PROCESS_Current()->dos_handles;
1042 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1044 SetLastError( ERROR_INVALID_HANDLE );
1045 return INVALID_HANDLE_VALUE;
1047 return table[hfile];
1051 /***********************************************************************
1052 * FILE_Dup2
1054 * dup2() function for DOS handles.
1056 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1058 HANDLE *table = PROCESS_Current()->dos_handles;
1059 HANDLE new_handle;
1061 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1062 !table || !table[hFile1])
1064 SetLastError( ERROR_INVALID_HANDLE );
1065 return HFILE_ERROR16;
1067 if (hFile2 < 5)
1069 FIXME( file, "stdio handle closed, need proper conversion\n" );
1070 SetLastError( ERROR_INVALID_HANDLE );
1071 return HFILE_ERROR16;
1073 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1074 GetCurrentProcess(), &new_handle,
1075 0, FALSE, DUPLICATE_SAME_ACCESS ))
1076 return HFILE_ERROR16;
1077 if (table[hFile2]) CloseHandle( table[hFile2] );
1078 table[hFile2] = new_handle;
1079 return hFile2;
1083 /***********************************************************************
1084 * _lclose16 (KERNEL.81)
1086 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1088 HANDLE *table = PROCESS_Current()->dos_handles;
1090 if (hFile < 5)
1092 FIXME( file, "stdio handle closed, need proper conversion\n" );
1093 SetLastError( ERROR_INVALID_HANDLE );
1094 return HFILE_ERROR16;
1096 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1098 SetLastError( ERROR_INVALID_HANDLE );
1099 return HFILE_ERROR16;
1101 TRACE( file, "%d (handle32=%d)\n", hFile, table[hFile] );
1102 CloseHandle( table[hFile] );
1103 table[hFile] = 0;
1104 return 0;
1108 /***********************************************************************
1109 * _lclose32 (KERNEL32.592)
1111 HFILE WINAPI _lclose( HFILE hFile )
1113 TRACE(file, "handle %d\n", hFile );
1114 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1118 /***********************************************************************
1119 * ReadFile (KERNEL32.428)
1121 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1122 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1124 struct get_read_fd_request req;
1125 int unix_handle, result;
1127 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToRead );
1129 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1130 if (!bytesToRead) return TRUE;
1132 req.handle = hFile;
1133 CLIENT_SendRequest( REQ_GET_READ_FD, -1, 1, &req, sizeof(req) );
1134 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1135 if (unix_handle == -1) return FALSE;
1136 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1138 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1139 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1140 FILE_SetDosError();
1141 break;
1143 close( unix_handle );
1144 if (result == -1) return FALSE;
1145 if (bytesRead) *bytesRead = result;
1146 return TRUE;
1150 /***********************************************************************
1151 * WriteFile (KERNEL32.578)
1153 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1154 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1156 struct get_write_fd_request req;
1157 int unix_handle, result;
1159 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToWrite );
1161 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1162 if (!bytesToWrite) return TRUE;
1164 req.handle = hFile;
1165 CLIENT_SendRequest( REQ_GET_WRITE_FD, -1, 1, &req, sizeof(req) );
1166 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1167 if (unix_handle == -1) return FALSE;
1168 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1170 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1171 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1172 FILE_SetDosError();
1173 break;
1175 close( unix_handle );
1176 if (result == -1) return FALSE;
1177 if (bytesWritten) *bytesWritten = result;
1178 return TRUE;
1182 /***********************************************************************
1183 * WIN16_hread
1185 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1187 LONG maxlen;
1189 TRACE(file, "%d %08lx %ld\n",
1190 hFile, (DWORD)buffer, count );
1192 /* Some programs pass a count larger than the allocated buffer */
1193 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1194 if (count > maxlen) count = maxlen;
1195 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1199 /***********************************************************************
1200 * WIN16_lread
1202 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1204 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1208 /***********************************************************************
1209 * _lread32 (KERNEL32.596)
1211 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1213 DWORD result;
1214 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1215 return result;
1219 /***********************************************************************
1220 * _lread16 (KERNEL.82)
1222 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1224 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1228 /***********************************************************************
1229 * _lcreat16 (KERNEL.83)
1231 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1233 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1237 /***********************************************************************
1238 * _lcreat (KERNEL32.593)
1240 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1242 /* Mask off all flags not explicitly allowed by the doc */
1243 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1244 TRACE(file, "%s %02x\n", path, attr );
1245 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1246 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1247 CREATE_ALWAYS, attr, -1 );
1251 /***********************************************************************
1252 * SetFilePointer (KERNEL32.492)
1254 DWORD WINAPI SetFilePointer( HFILE hFile, LONG distance, LONG *highword,
1255 DWORD method )
1257 struct set_file_pointer_request req;
1258 struct set_file_pointer_reply reply;
1260 if (highword && *highword)
1262 FIXME(file, "64-bit offsets not supported yet\n");
1263 SetLastError( ERROR_INVALID_PARAMETER );
1264 return 0xffffffff;
1266 TRACE(file, "handle %d offset %ld origin %ld\n",
1267 hFile, distance, method );
1269 req.handle = hFile;
1270 req.low = distance;
1271 req.high = highword ? *highword : 0;
1272 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1273 req.whence = method;
1274 CLIENT_SendRequest( REQ_SET_FILE_POINTER, -1, 1, &req, sizeof(req) );
1275 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
1276 SetLastError( 0 );
1277 if (highword) *highword = reply.high;
1278 return reply.low;
1282 /***********************************************************************
1283 * _llseek16 (KERNEL.84)
1285 * FIXME:
1286 * Seeking before the start of the file should be allowed for _llseek16,
1287 * but cause subsequent I/O operations to fail (cf. interrupt list)
1290 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1292 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1296 /***********************************************************************
1297 * _llseek32 (KERNEL32.594)
1299 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1301 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1305 /***********************************************************************
1306 * _lopen16 (KERNEL.85)
1308 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1310 return FILE_AllocDosHandle( _lopen( path, mode ) );
1314 /***********************************************************************
1315 * _lopen32 (KERNEL32.595)
1317 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1319 DWORD access, sharing;
1321 TRACE(file, "('%s',%04x)\n", path, mode );
1322 FILE_ConvertOFMode( mode, &access, &sharing );
1323 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1327 /***********************************************************************
1328 * _lwrite16 (KERNEL.86)
1330 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1332 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1335 /***********************************************************************
1336 * _lwrite32 (KERNEL32.761)
1338 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1340 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1344 /***********************************************************************
1345 * _hread16 (KERNEL.349)
1347 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1349 return _lread( FILE_GetHandle(hFile), buffer, count );
1353 /***********************************************************************
1354 * _hread32 (KERNEL32.590)
1356 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1358 return _lread( hFile, buffer, count );
1362 /***********************************************************************
1363 * _hwrite16 (KERNEL.350)
1365 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1367 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1371 /***********************************************************************
1372 * _hwrite32 (KERNEL32.591)
1374 * experimentation yields that _lwrite:
1375 * o truncates the file at the current position with
1376 * a 0 len write
1377 * o returns 0 on a 0 length write
1378 * o works with console handles
1381 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1383 DWORD result;
1385 TRACE(file, "%d %p %ld\n", handle, buffer, count );
1387 if (!count)
1389 /* Expand or truncate at current position */
1390 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1391 return 0;
1393 if (!WriteFile( handle, buffer, count, &result, NULL ))
1394 return HFILE_ERROR;
1395 return result;
1399 /***********************************************************************
1400 * SetHandleCount16 (KERNEL.199)
1402 UINT16 WINAPI SetHandleCount16( UINT16 count )
1404 HGLOBAL16 hPDB = GetCurrentPDB16();
1405 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1406 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1408 TRACE(file, "(%d)\n", count );
1410 if (count < 20) count = 20; /* No point in going below 20 */
1411 else if (count > 254) count = 254;
1413 if (count == 20)
1415 if (pdb->nbFiles > 20)
1417 memcpy( pdb->fileHandles, files, 20 );
1418 GlobalFree16( pdb->hFileHandles );
1419 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1420 GlobalHandleToSel16( hPDB ) );
1421 pdb->hFileHandles = 0;
1422 pdb->nbFiles = 20;
1425 else /* More than 20, need a new file handles table */
1427 BYTE *newfiles;
1428 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1429 if (!newhandle)
1431 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1432 return pdb->nbFiles;
1434 newfiles = (BYTE *)GlobalLock16( newhandle );
1436 if (count > pdb->nbFiles)
1438 memcpy( newfiles, files, pdb->nbFiles );
1439 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1441 else memcpy( newfiles, files, count );
1442 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1443 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1444 pdb->hFileHandles = newhandle;
1445 pdb->nbFiles = count;
1447 return pdb->nbFiles;
1451 /*************************************************************************
1452 * SetHandleCount32 (KERNEL32.494)
1454 UINT WINAPI SetHandleCount( UINT count )
1456 return MIN( 256, count );
1460 /***********************************************************************
1461 * FlushFileBuffers (KERNEL32.133)
1463 BOOL WINAPI FlushFileBuffers( HFILE hFile )
1465 struct flush_file_request req;
1467 req.handle = hFile;
1468 CLIENT_SendRequest( REQ_FLUSH_FILE, -1, 1, &req, sizeof(req) );
1469 return !CLIENT_WaitReply( NULL, NULL, 0 );
1473 /**************************************************************************
1474 * SetEndOfFile (KERNEL32.483)
1476 BOOL WINAPI SetEndOfFile( HFILE hFile )
1478 struct truncate_file_request req;
1480 req.handle = hFile;
1481 CLIENT_SendRequest( REQ_TRUNCATE_FILE, -1, 1, &req, sizeof(req) );
1482 return !CLIENT_WaitReply( NULL, NULL, 0 );
1486 /***********************************************************************
1487 * DeleteFile16 (KERNEL.146)
1489 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1491 return DeleteFileA( path );
1495 /***********************************************************************
1496 * DeleteFile32A (KERNEL32.71)
1498 BOOL WINAPI DeleteFileA( LPCSTR path )
1500 DOS_FULL_NAME full_name;
1502 TRACE(file, "'%s'\n", path );
1504 if (!*path)
1506 ERR(file, "Empty path passed\n");
1507 return FALSE;
1509 if (DOSFS_GetDevice( path ))
1511 WARN(file, "cannot remove DOS device '%s'!\n", path);
1512 SetLastError( ERROR_FILE_NOT_FOUND );
1513 return FALSE;
1516 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1517 if (unlink( full_name.long_name ) == -1)
1519 FILE_SetDosError();
1520 return FALSE;
1522 return TRUE;
1526 /***********************************************************************
1527 * DeleteFile32W (KERNEL32.72)
1529 BOOL WINAPI DeleteFileW( LPCWSTR path )
1531 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1532 BOOL ret = DeleteFileA( xpath );
1533 HeapFree( GetProcessHeap(), 0, xpath );
1534 return ret;
1538 /***********************************************************************
1539 * FILE_dommap
1541 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1542 DWORD size_high, DWORD size_low,
1543 DWORD offset_high, DWORD offset_low,
1544 int prot, int flags )
1546 int fd = -1;
1547 int pos;
1548 LPVOID ret;
1550 if (size_high || offset_high)
1551 FIXME(file, "offsets larger than 4Gb not supported\n");
1553 if (unix_handle == -1)
1555 #ifdef MAP_ANON
1556 flags |= MAP_ANON;
1557 #else
1558 static int fdzero = -1;
1560 if (fdzero == -1)
1562 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1564 perror( "/dev/zero: open" );
1565 exit(1);
1568 fd = fdzero;
1569 #endif /* MAP_ANON */
1570 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1571 #ifdef MAP_SHARED
1572 flags &= ~MAP_SHARED;
1573 #endif
1574 #ifdef MAP_PRIVATE
1575 flags |= MAP_PRIVATE;
1576 #endif
1578 else fd = unix_handle;
1580 if ((ret = mmap( start, size_low, prot,
1581 flags, fd, offset_low )) != (LPVOID)-1)
1582 return ret;
1584 /* mmap() failed; if this is because the file offset is not */
1585 /* page-aligned (EINVAL), or because the underlying filesystem */
1586 /* does not support mmap() (ENOEXEC), we do it by hand. */
1588 if (unix_handle == -1) return ret;
1589 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1590 if (prot & PROT_WRITE)
1592 /* We cannot fake shared write mappings */
1593 #ifdef MAP_SHARED
1594 if (flags & MAP_SHARED) return ret;
1595 #endif
1596 #ifdef MAP_PRIVATE
1597 if (!(flags & MAP_PRIVATE)) return ret;
1598 #endif
1600 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1601 /* Reserve the memory with an anonymous mmap */
1602 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1603 PROT_READ | PROT_WRITE, flags );
1604 if (ret == (LPVOID)-1) return ret;
1605 /* Now read in the file */
1606 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1608 FILE_munmap( ret, size_high, size_low );
1609 return (LPVOID)-1;
1611 read( fd, ret, size_low );
1612 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1613 mprotect( ret, size_low, prot ); /* Set the right protection */
1614 return ret;
1618 /***********************************************************************
1619 * FILE_munmap
1621 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1623 if (size_high)
1624 FIXME(file, "offsets larger than 4Gb not supported\n");
1625 return munmap( start, size_low );
1629 /***********************************************************************
1630 * GetFileType (KERNEL32.222)
1632 DWORD WINAPI GetFileType( HFILE hFile )
1634 struct get_file_info_request req;
1635 struct get_file_info_reply reply;
1637 req.handle = hFile;
1638 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
1639 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
1640 return FILE_TYPE_UNKNOWN;
1641 return reply.type;
1645 /**************************************************************************
1646 * MoveFileEx32A (KERNEL32.???)
1648 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1650 DOS_FULL_NAME full_name1, full_name2;
1651 int mode=0; /* mode == 1: use copy */
1653 TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag);
1655 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1656 if (fn2) { /* !fn2 means delete fn1 */
1657 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1658 /* Source name and target path are valid */
1659 if ( full_name1.drive != full_name2.drive)
1661 /* use copy, if allowed */
1662 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1663 /* FIXME: Use right error code */
1664 SetLastError( ERROR_FILE_EXISTS );
1665 return FALSE;
1667 else mode =1;
1669 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1670 /* target exists, check if we may overwrite */
1671 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1672 /* FIXME: Use right error code */
1673 SetLastError( ERROR_ACCESS_DENIED );
1674 return FALSE;
1677 else /* fn2 == NULL means delete source */
1678 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1679 if (flag & MOVEFILE_COPY_ALLOWED) {
1680 WARN(file, "Illegal flag\n");
1681 SetLastError( ERROR_GEN_FAILURE );
1682 return FALSE;
1684 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1685 Perhaps we should queue these command and execute it
1686 when exiting... What about using on_exit(2)
1688 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1689 full_name1.long_name);
1690 return TRUE;
1692 else if (unlink( full_name1.long_name ) == -1)
1694 FILE_SetDosError();
1695 return FALSE;
1697 else return TRUE; /* successfully deleted */
1699 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1700 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1701 Perhaps we should queue these command and execute it
1702 when exiting... What about using on_exit(2)
1704 FIXME(file,"Please move existing file '%s' to file '%s'"
1705 "when Wine has finished\n",
1706 full_name1.long_name, full_name2.long_name);
1707 return TRUE;
1710 if (!mode) /* move the file */
1711 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1713 FILE_SetDosError();
1714 return FALSE;
1716 else return TRUE;
1717 else /* copy File */
1718 return CopyFileA(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1722 /**************************************************************************
1723 * MoveFileEx32W (KERNEL32.???)
1725 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1727 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1728 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1729 BOOL res = MoveFileExA( afn1, afn2, flag );
1730 HeapFree( GetProcessHeap(), 0, afn1 );
1731 HeapFree( GetProcessHeap(), 0, afn2 );
1732 return res;
1736 /**************************************************************************
1737 * MoveFile32A (KERNEL32.387)
1739 * Move file or directory
1741 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1743 DOS_FULL_NAME full_name1, full_name2;
1744 struct stat fstat;
1746 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1748 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1749 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1750 /* The new name must not already exist */
1751 return FALSE;
1752 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1754 if (full_name1.drive == full_name2.drive) /* move */
1755 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1757 FILE_SetDosError();
1758 return FALSE;
1760 else return TRUE;
1761 else /*copy */ {
1762 if (stat( full_name1.long_name, &fstat ))
1764 WARN(file, "Invalid source file %s\n",
1765 full_name1.long_name);
1766 FILE_SetDosError();
1767 return FALSE;
1769 if (S_ISDIR(fstat.st_mode)) {
1770 /* No Move for directories across file systems */
1771 /* FIXME: Use right error code */
1772 SetLastError( ERROR_GEN_FAILURE );
1773 return FALSE;
1775 else
1776 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1781 /**************************************************************************
1782 * MoveFile32W (KERNEL32.390)
1784 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1786 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1787 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1788 BOOL res = MoveFileA( afn1, afn2 );
1789 HeapFree( GetProcessHeap(), 0, afn1 );
1790 HeapFree( GetProcessHeap(), 0, afn2 );
1791 return res;
1795 /**************************************************************************
1796 * CopyFile32A (KERNEL32.36)
1798 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1800 HFILE h1, h2;
1801 BY_HANDLE_FILE_INFORMATION info;
1802 UINT count;
1803 BOOL ret = FALSE;
1804 int mode;
1805 char buffer[2048];
1807 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1808 if (!GetFileInformationByHandle( h1, &info ))
1810 CloseHandle( h1 );
1811 return FALSE;
1813 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1814 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1815 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1816 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1818 CloseHandle( h1 );
1819 return FALSE;
1821 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1823 char *p = buffer;
1824 while (count > 0)
1826 INT res = _lwrite( h2, p, count );
1827 if (res <= 0) goto done;
1828 p += res;
1829 count -= res;
1832 ret = TRUE;
1833 done:
1834 CloseHandle( h1 );
1835 CloseHandle( h2 );
1836 return ret;
1840 /**************************************************************************
1841 * CopyFile32W (KERNEL32.37)
1843 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1845 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1846 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1847 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1848 HeapFree( GetProcessHeap(), 0, sourceA );
1849 HeapFree( GetProcessHeap(), 0, destA );
1850 return ret;
1854 /**************************************************************************
1855 * CopyFileEx32A (KERNEL32.858)
1857 * This implementation ignores most of the extra parameters passed-in into
1858 * the "ex" version of the method and calls the CopyFile method.
1859 * It will have to be fixed eventually.
1861 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1862 LPCSTR destFilename,
1863 LPPROGRESS_ROUTINE progressRoutine,
1864 LPVOID appData,
1865 LPBOOL cancelFlagPointer,
1866 DWORD copyFlags)
1868 BOOL failIfExists = FALSE;
1871 * Interpret the only flag that CopyFile can interpret.
1873 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1875 failIfExists = TRUE;
1878 return CopyFileA(sourceFilename, destFilename, failIfExists);
1881 /**************************************************************************
1882 * CopyFileEx32W (KERNEL32.859)
1884 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1885 LPCWSTR destFilename,
1886 LPPROGRESS_ROUTINE progressRoutine,
1887 LPVOID appData,
1888 LPBOOL cancelFlagPointer,
1889 DWORD copyFlags)
1891 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1892 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1894 BOOL ret = CopyFileExA(sourceA,
1895 destA,
1896 progressRoutine,
1897 appData,
1898 cancelFlagPointer,
1899 copyFlags);
1901 HeapFree( GetProcessHeap(), 0, sourceA );
1902 HeapFree( GetProcessHeap(), 0, destA );
1904 return ret;
1908 /***********************************************************************
1909 * SetFileTime (KERNEL32.650)
1911 BOOL WINAPI SetFileTime( HFILE hFile,
1912 const FILETIME *lpCreationTime,
1913 const FILETIME *lpLastAccessTime,
1914 const FILETIME *lpLastWriteTime )
1916 struct set_file_time_request req;
1918 req.handle = hFile;
1919 if (lpLastAccessTime)
1920 req.access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1921 else
1922 req.access_time = 0; /* FIXME */
1923 if (lpLastWriteTime)
1924 req.write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1925 else
1926 req.write_time = 0; /* FIXME */
1928 CLIENT_SendRequest( REQ_SET_FILE_TIME, -1, 1, &req, sizeof(req) );
1929 return !CLIENT_WaitReply( NULL, NULL, 0 );
1933 /**************************************************************************
1934 * LockFile (KERNEL32.511)
1936 BOOL WINAPI LockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1937 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1939 struct lock_file_request req;
1941 req.handle = hFile;
1942 req.offset_low = dwFileOffsetLow;
1943 req.offset_high = dwFileOffsetHigh;
1944 req.count_low = nNumberOfBytesToLockLow;
1945 req.count_high = nNumberOfBytesToLockHigh;
1946 CLIENT_SendRequest( REQ_LOCK_FILE, -1, 1, &req, sizeof(req) );
1947 return !CLIENT_WaitReply( NULL, NULL, 0 );
1950 /**************************************************************************
1951 * LockFileEx [KERNEL32.512]
1953 * Locks a byte range within an open file for shared or exclusive access.
1955 * RETURNS
1956 * success: TRUE
1957 * failure: FALSE
1958 * NOTES
1960 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1962 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1963 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1964 LPOVERLAPPED pOverlapped )
1966 FIXME(file, "hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1967 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
1968 pOverlapped);
1969 if (reserved == 0)
1970 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1971 else
1973 ERR(file, "reserved == %ld: Supposed to be 0??\n", reserved);
1974 SetLastError(ERROR_INVALID_PARAMETER);
1977 return FALSE;
1981 /**************************************************************************
1982 * UnlockFile (KERNEL32.703)
1984 BOOL WINAPI UnlockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1985 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1987 struct unlock_file_request req;
1989 req.handle = hFile;
1990 req.offset_low = dwFileOffsetLow;
1991 req.offset_high = dwFileOffsetHigh;
1992 req.count_low = nNumberOfBytesToUnlockLow;
1993 req.count_high = nNumberOfBytesToUnlockHigh;
1994 CLIENT_SendRequest( REQ_UNLOCK_FILE, -1, 1, &req, sizeof(req) );
1995 return !CLIENT_WaitReply( NULL, NULL, 0 );
1999 #if 0
2001 struct DOS_FILE_LOCK {
2002 struct DOS_FILE_LOCK * next;
2003 DWORD base;
2004 DWORD len;
2005 DWORD processId;
2006 FILE_OBJECT * dos_file;
2007 /* char * unix_name;*/
2010 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2012 static DOS_FILE_LOCK *locks = NULL;
2013 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2016 /* Locks need to be mirrored because unix file locking is based
2017 * on the pid. Inside of wine there can be multiple WINE processes
2018 * that share the same unix pid.
2019 * Read's and writes should check these locks also - not sure
2020 * how critical that is at this point (FIXME).
2023 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2025 DOS_FILE_LOCK *curr;
2026 DWORD processId;
2028 processId = GetCurrentProcessId();
2030 /* check if lock overlaps a current lock for the same file */
2031 #if 0
2032 for (curr = locks; curr; curr = curr->next) {
2033 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2034 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2035 return TRUE;/* region is identic */
2036 if ((f->l_start < (curr->base + curr->len)) &&
2037 ((f->l_start + f->l_len) > curr->base)) {
2038 /* region overlaps */
2039 return FALSE;
2043 #endif
2045 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2046 curr->processId = GetCurrentProcessId();
2047 curr->base = f->l_start;
2048 curr->len = f->l_len;
2049 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
2050 curr->next = locks;
2051 curr->dos_file = file;
2052 locks = curr;
2053 return TRUE;
2056 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2058 DWORD processId;
2059 DOS_FILE_LOCK **curr;
2060 DOS_FILE_LOCK *rem;
2062 processId = GetCurrentProcessId();
2063 curr = &locks;
2064 while (*curr) {
2065 if ((*curr)->dos_file == file) {
2066 rem = *curr;
2067 *curr = (*curr)->next;
2068 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2069 HeapFree( SystemHeap, 0, rem );
2071 else
2072 curr = &(*curr)->next;
2076 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2078 DWORD processId;
2079 DOS_FILE_LOCK **curr;
2080 DOS_FILE_LOCK *rem;
2082 processId = GetCurrentProcessId();
2083 for (curr = &locks; *curr; curr = &(*curr)->next) {
2084 if ((*curr)->processId == processId &&
2085 (*curr)->dos_file == file &&
2086 (*curr)->base == f->l_start &&
2087 (*curr)->len == f->l_len) {
2088 /* this is the same lock */
2089 rem = *curr;
2090 *curr = (*curr)->next;
2091 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2092 HeapFree( SystemHeap, 0, rem );
2093 return TRUE;
2096 /* no matching lock found */
2097 return FALSE;
2101 /**************************************************************************
2102 * LockFile (KERNEL32.511)
2104 BOOL WINAPI LockFile(
2105 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2106 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2108 struct flock f;
2109 FILE_OBJECT *file;
2111 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2112 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2113 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2115 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2116 FIXME(file, "Unimplemented bytes > 32bits\n");
2117 return FALSE;
2120 f.l_start = dwFileOffsetLow;
2121 f.l_len = nNumberOfBytesToLockLow;
2122 f.l_whence = SEEK_SET;
2123 f.l_pid = 0;
2124 f.l_type = F_WRLCK;
2126 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2128 /* shadow locks internally */
2129 if (!DOS_AddLock(file, &f)) {
2130 SetLastError( ERROR_LOCK_VIOLATION );
2131 return FALSE;
2134 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2135 #ifdef USE_UNIX_LOCKS
2136 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2137 if (errno == EACCES || errno == EAGAIN) {
2138 SetLastError( ERROR_LOCK_VIOLATION );
2140 else {
2141 FILE_SetDosError();
2143 /* remove our internal copy of the lock */
2144 DOS_RemoveLock(file, &f);
2145 return FALSE;
2147 #endif
2148 return TRUE;
2152 /**************************************************************************
2153 * UnlockFile (KERNEL32.703)
2155 BOOL WINAPI UnlockFile(
2156 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2157 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2159 FILE_OBJECT *file;
2160 struct flock f;
2162 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2163 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2164 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2166 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2167 WARN(file, "Unimplemented bytes > 32bits\n");
2168 return FALSE;
2171 f.l_start = dwFileOffsetLow;
2172 f.l_len = nNumberOfBytesToUnlockLow;
2173 f.l_whence = SEEK_SET;
2174 f.l_pid = 0;
2175 f.l_type = F_UNLCK;
2177 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2179 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2181 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2182 #ifdef USE_UNIX_LOCKS
2183 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2184 FILE_SetDosError();
2185 return FALSE;
2187 #endif
2188 return TRUE;
2190 #endif
2192 /**************************************************************************
2193 * GetFileAttributesEx32A [KERNEL32.874]
2195 BOOL WINAPI GetFileAttributesExA(
2196 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2197 LPVOID lpFileInformation)
2199 DOS_FULL_NAME full_name;
2200 BY_HANDLE_FILE_INFORMATION info;
2202 if (lpFileName == NULL) return FALSE;
2203 if (lpFileInformation == NULL) return FALSE;
2205 if (fInfoLevelId == GetFileExInfoStandard) {
2206 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2207 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2208 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2209 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2211 lpFad->dwFileAttributes = info.dwFileAttributes;
2212 lpFad->ftCreationTime = info.ftCreationTime;
2213 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2214 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2215 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2216 lpFad->nFileSizeLow = info.nFileSizeLow;
2218 else {
2219 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
2220 return FALSE;
2223 return TRUE;
2227 /**************************************************************************
2228 * GetFileAttributesEx32W [KERNEL32.875]
2230 BOOL WINAPI GetFileAttributesExW(
2231 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2232 LPVOID lpFileInformation)
2234 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2235 BOOL res =
2236 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2237 HeapFree( GetProcessHeap(), 0, nameA );
2238 return res;