Fixed an error and an off-by-one bug in DSA_SetItem(). This
[wine/dcerpc.git] / files / file.c
blobe915bfe4c3fcf29c269b41352db23078754bc759
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/request.h"
47 #include "server.h"
49 DEFAULT_DEBUG_CHANNEL(file)
51 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
52 #define MAP_ANON MAP_ANONYMOUS
53 #endif
55 /* Size of per-process table of DOS handles */
56 #define DOS_TABLE_SIZE 256
59 /***********************************************************************
60 * FILE_ConvertOFMode
62 * Convert OF_* mode into flags for CreateFile.
64 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
66 switch(mode & 0x03)
68 case OF_READ: *access = GENERIC_READ; break;
69 case OF_WRITE: *access = GENERIC_WRITE; break;
70 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
71 default: *access = 0; break;
73 switch(mode & 0x70)
75 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
76 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
77 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
78 case OF_SHARE_DENY_NONE:
79 case OF_SHARE_COMPAT:
80 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
85 #if 0
86 /***********************************************************************
87 * FILE_ShareDeny
89 * PARAMS
90 * oldmode[I] mode how file was first opened
91 * mode[I] mode how the file should get opened
92 * RETURNS
93 * TRUE: deny open
94 * FALSE: allow open
96 * Look what we have to do with the given SHARE modes
98 * Ralph Brown's interrupt list gives following explication, I guess
99 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
101 * FIXME: Validate this function
102 ========from Ralph Brown's list =========
103 (Table 0750)
104 Values of DOS file sharing behavior:
105 | Second and subsequent Opens
106 First |Compat Deny Deny Deny Deny
107 Open | All Write Read None
108 |R W RW R W RW R W RW R W RW R W RW
109 - - - - -| - - - - - - - - - - - - - - - - -
110 Compat R |Y Y Y N N N 1 N N N N N 1 N N
111 W |Y Y Y N N N N N N N N N N N N
112 RW|Y Y Y N N N N N N N N N N N N
113 - - - - -|
114 Deny R |C C C N N N N N N N N N N N N
115 All W |C C C N N N N N N N N N N N N
116 RW|C C C N N N N N N N N N N N N
117 - - - - -|
118 Deny R |2 C C N N N Y N N N N N Y N N
119 Write W |C C C N N N N N N Y N N Y N N
120 RW|C C C N N N N N N N N N Y N N
121 - - - - -|
122 Deny R |C C C N N N N Y N N N N N Y N
123 Read W |C C C N N N N N N N Y N N Y N
124 RW|C C C N N N N N N N N N N Y N
125 - - - - -|
126 Deny R |2 C C N N N Y Y Y N N N Y Y Y
127 None W |C C C N N N N N N Y Y Y Y Y Y
128 RW|C C C N N N N N N N N N Y Y Y
129 Legend: Y = open succeeds, N = open fails with error code 05h
130 C = open fails, INT 24 generated
131 1 = open succeeds if file read-only, else fails with error code
132 2 = open succeeds if file read-only, else fails with INT 24
133 ========end of description from Ralph Brown's List =====
134 For every "Y" in the table we return FALSE
135 For every "N" we set the DOS_ERROR and return TRUE
136 For all other cases we barf,set the DOS_ERROR and return TRUE
139 static BOOL FILE_ShareDeny( int mode, int oldmode)
141 int oldsharemode = oldmode & 0x70;
142 int sharemode = mode & 0x70;
143 int oldopenmode = oldmode & 3;
144 int openmode = mode & 3;
146 switch (oldsharemode)
148 case OF_SHARE_COMPAT:
149 if (sharemode == OF_SHARE_COMPAT) return FALSE;
150 if (openmode == OF_READ) goto test_ro_err05 ;
151 goto fail_error05;
152 case OF_SHARE_EXCLUSIVE:
153 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
154 goto fail_error05;
155 case OF_SHARE_DENY_WRITE:
156 if (openmode != OF_READ)
158 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
159 goto fail_error05;
161 switch (sharemode)
163 case OF_SHARE_COMPAT:
164 if (oldopenmode == OF_READ) goto test_ro_int24 ;
165 goto fail_int24;
166 case OF_SHARE_DENY_NONE :
167 return FALSE;
168 case OF_SHARE_DENY_WRITE :
169 if (oldopenmode == OF_READ) return FALSE;
170 case OF_SHARE_DENY_READ :
171 if (oldopenmode == OF_WRITE) return FALSE;
172 case OF_SHARE_EXCLUSIVE:
173 default:
174 goto fail_error05;
176 break;
177 case OF_SHARE_DENY_READ:
178 if (openmode != OF_WRITE)
180 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
181 goto fail_error05;
183 switch (sharemode)
185 case OF_SHARE_COMPAT:
186 goto fail_int24;
187 case OF_SHARE_DENY_NONE :
188 return FALSE;
189 case OF_SHARE_DENY_WRITE :
190 if (oldopenmode == OF_READ) return FALSE;
191 case OF_SHARE_DENY_READ :
192 if (oldopenmode == OF_WRITE) return FALSE;
193 case OF_SHARE_EXCLUSIVE:
194 default:
195 goto fail_error05;
197 break;
198 case OF_SHARE_DENY_NONE:
199 switch (sharemode)
201 case OF_SHARE_COMPAT:
202 goto fail_int24;
203 case OF_SHARE_DENY_NONE :
204 return FALSE;
205 case OF_SHARE_DENY_WRITE :
206 if (oldopenmode == OF_READ) return FALSE;
207 case OF_SHARE_DENY_READ :
208 if (oldopenmode == OF_WRITE) return FALSE;
209 case OF_SHARE_EXCLUSIVE:
210 default:
211 goto fail_error05;
213 default:
214 ERR(file,"unknown mode\n");
216 ERR(file,"shouldn't happen\n");
217 ERR(file,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
218 return TRUE;
220 test_ro_int24:
221 if (oldmode == OF_READ)
222 return FALSE;
223 /* Fall through */
224 fail_int24:
225 FIXME(file,"generate INT24 missing\n");
226 /* Is this the right error? */
227 SetLastError( ERROR_ACCESS_DENIED );
228 return TRUE;
230 test_ro_err05:
231 if (oldmode == OF_READ)
232 return FALSE;
233 /* fall through */
234 fail_error05:
235 TRACE(file,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
236 SetLastError( ERROR_ACCESS_DENIED );
237 return TRUE;
239 #endif
242 /***********************************************************************
243 * FILE_SetDosError
245 * Set the DOS error code from errno.
247 void FILE_SetDosError(void)
249 int save_errno = errno; /* errno gets overwritten by printf */
251 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
252 switch (save_errno)
254 case EAGAIN:
255 SetLastError( ERROR_SHARING_VIOLATION );
256 break;
257 case EBADF:
258 SetLastError( ERROR_INVALID_HANDLE );
259 break;
260 case ENOSPC:
261 SetLastError( ERROR_HANDLE_DISK_FULL );
262 break;
263 case EACCES:
264 case EPERM:
265 case EROFS:
266 SetLastError( ERROR_ACCESS_DENIED );
267 break;
268 case EBUSY:
269 SetLastError( ERROR_LOCK_VIOLATION );
270 break;
271 case ENOENT:
272 SetLastError( ERROR_FILE_NOT_FOUND );
273 break;
274 case EISDIR:
275 SetLastError( ERROR_CANNOT_MAKE );
276 break;
277 case ENFILE:
278 case EMFILE:
279 SetLastError( ERROR_NO_MORE_FILES );
280 break;
281 case EEXIST:
282 SetLastError( ERROR_FILE_EXISTS );
283 break;
284 case EINVAL:
285 case ESPIPE:
286 SetLastError( ERROR_SEEK );
287 break;
288 case ENOTEMPTY:
289 SetLastError( ERROR_DIR_NOT_EMPTY );
290 break;
291 default:
292 perror( "int21: unknown errno" );
293 SetLastError( ERROR_GEN_FAILURE );
294 break;
296 errno = save_errno;
300 /***********************************************************************
301 * FILE_DupUnixHandle
303 * Duplicate a Unix handle into a task handle.
305 HFILE FILE_DupUnixHandle( int fd, DWORD access )
307 int unix_handle;
308 struct create_file_request req;
309 struct create_file_reply reply;
311 if ((unix_handle = dup(fd)) == -1)
313 FILE_SetDosError();
314 return INVALID_HANDLE_VALUE;
316 req.access = access;
317 req.inherit = 1;
318 req.sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
319 req.create = 0;
320 req.attrs = 0;
322 CLIENT_SendRequest( REQ_CREATE_FILE, unix_handle, 1,
323 &req, sizeof(req) );
324 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
325 return reply.handle;
329 /***********************************************************************
330 * FILE_CreateFile
332 * Implementation of CreateFile. Takes a Unix path name.
334 HFILE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
335 LPSECURITY_ATTRIBUTES sa, DWORD creation,
336 DWORD attributes, HANDLE template )
338 struct create_file_request req;
339 struct create_file_reply reply;
341 req.access = access;
342 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
343 req.sharing = sharing;
344 req.create = creation;
345 req.attrs = attributes;
346 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
347 &req, sizeof(req),
348 filename, strlen(filename) + 1 );
349 SetLastError(0);
350 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
352 /* If write access failed, retry without GENERIC_WRITE */
354 if ((reply.handle == -1) && !Options.failReadOnly &&
355 (access & GENERIC_WRITE))
357 DWORD lasterror = GetLastError();
358 if ((lasterror == ERROR_ACCESS_DENIED) ||
359 (lasterror == ERROR_WRITE_PROTECT))
361 req.access &= ~GENERIC_WRITE;
362 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
363 &req, sizeof(req),
364 filename, strlen(filename) + 1 );
365 SetLastError(0);
366 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
369 return reply.handle;
373 /***********************************************************************
374 * FILE_CreateDevice
376 * Same as FILE_CreateFile but for a device
378 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
380 struct create_device_request req;
381 struct create_device_reply reply;
383 req.access = access;
384 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
385 req.id = client_id;
386 CLIENT_SendRequest( REQ_CREATE_DEVICE, -1, 1, &req, sizeof(req) );
387 SetLastError(0);
388 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
389 return reply.handle;
393 /*************************************************************************
394 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
396 * Creates or opens an object, and returns a handle that can be used to
397 * access that object.
399 * PARAMS
401 * filename [I] pointer to filename to be accessed
402 * access [I] access mode requested
403 * sharing [I] share mode
404 * sa [I] pointer to security attributes
405 * creation [I] how to create the file
406 * attributes [I] attributes for newly created file
407 * template [I] handle to file with extended attributes to copy
409 * RETURNS
410 * Success: Open handle to specified file
411 * Failure: INVALID_HANDLE_VALUE
413 * NOTES
414 * Should call SetLastError() on failure.
416 * BUGS
418 * Doesn't support character devices, pipes, template files, or a
419 * lot of the 'attributes' flags yet.
421 HFILE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
422 LPSECURITY_ATTRIBUTES sa, DWORD creation,
423 DWORD attributes, HANDLE template )
425 DOS_FULL_NAME full_name;
427 if (!filename)
429 SetLastError( ERROR_INVALID_PARAMETER );
430 return HFILE_ERROR;
432 TRACE(file,"%s %s%s%s%s%s%s%s\n",filename,
433 ((access & GENERIC_READ)==GENERIC_READ)?"GENERIC_READ ":"",
434 ((access & GENERIC_WRITE)==GENERIC_WRITE)?"GENERIC_WRITE ":"",
435 (!access)?"QUERY_ACCESS ":"",
436 ((sharing & FILE_SHARE_READ)==FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
437 ((sharing & FILE_SHARE_WRITE)==FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
438 ((sharing & FILE_SHARE_DELETE)==FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
439 (creation ==CREATE_NEW)?"CREATE_NEW":
440 (creation ==CREATE_ALWAYS)?"CREATE_ALWAYS ":
441 (creation ==OPEN_EXISTING)?"OPEN_EXISTING ":
442 (creation ==OPEN_ALWAYS)?"OPEN_ALWAYS ":
443 (creation ==TRUNCATE_EXISTING)?"TRUNCATE_EXISTING ":"");
445 /* If the name starts with '\\?\', ignore the first 4 chars. */
446 if (!strncmp(filename, "\\\\?\\", 4))
448 filename += 4;
449 if (!strncmp(filename, "UNC\\", 4))
451 FIXME( file, "UNC name (%s) not supported.\n", filename );
452 SetLastError( ERROR_PATH_NOT_FOUND );
453 return HFILE_ERROR;
457 if (!strncmp(filename, "\\\\.\\", 4))
458 return DEVICE_Open( filename+4, access, sa );
460 /* If the name still starts with '\\', it's a UNC name. */
461 if (!strncmp(filename, "\\\\", 2))
463 FIXME( file, "UNC name (%s) not supported.\n", filename );
464 SetLastError( ERROR_PATH_NOT_FOUND );
465 return HFILE_ERROR;
468 /* Open a console for CONIN$ or CONOUT$ */
469 if (!lstrcmpiA(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
470 if (!lstrcmpiA(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
472 if (DOSFS_GetDevice( filename ))
474 HFILE ret;
476 TRACE(file, "opening device '%s'\n", filename );
478 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
479 return ret;
481 /* Do not silence this please. It is a critical error. -MM */
482 ERR(file, "Couldn't open device '%s'!\n",filename);
483 SetLastError( ERROR_FILE_NOT_FOUND );
484 return HFILE_ERROR;
487 /* check for filename, don't check for last entry if creating */
488 if (!DOSFS_GetFullName( filename,
489 (creation == OPEN_EXISTING) || (creation == TRUNCATE_EXISTING), &full_name ))
490 return HFILE_ERROR;
492 return FILE_CreateFile( full_name.long_name, access, sharing,
493 sa, creation, attributes, template );
498 /*************************************************************************
499 * CreateFile32W (KERNEL32.48)
501 HFILE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
502 LPSECURITY_ATTRIBUTES sa, DWORD creation,
503 DWORD attributes, HANDLE template)
505 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
506 HFILE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
507 HeapFree( GetProcessHeap(), 0, afn );
508 return res;
512 /***********************************************************************
513 * FILE_FillInfo
515 * Fill a file information from a struct stat.
517 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
519 if (S_ISDIR(st->st_mode))
520 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
521 else
522 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
523 if (!(st->st_mode & S_IWUSR))
524 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
526 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
527 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
528 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
530 info->dwVolumeSerialNumber = 0; /* FIXME */
531 info->nFileSizeHigh = 0;
532 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
533 info->nNumberOfLinks = st->st_nlink;
534 info->nFileIndexHigh = 0;
535 info->nFileIndexLow = st->st_ino;
539 /***********************************************************************
540 * FILE_Stat
542 * Stat a Unix path name. Return TRUE if OK.
544 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
546 struct stat st;
548 if (!unixName || !info) return FALSE;
550 if (stat( unixName, &st ) == -1)
552 FILE_SetDosError();
553 return FALSE;
555 FILE_FillInfo( &st, info );
556 return TRUE;
560 /***********************************************************************
561 * GetFileInformationByHandle (KERNEL32.219)
563 DWORD WINAPI GetFileInformationByHandle( HFILE hFile,
564 BY_HANDLE_FILE_INFORMATION *info )
566 struct get_file_info_request req;
567 struct get_file_info_reply reply;
569 if (!info) return 0;
570 req.handle = hFile;
571 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
572 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
573 return 0;
574 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftCreationTime, 0 );
575 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftLastWriteTime, 0 );
576 DOSFS_UnixTimeToFileTime( reply.access_time, &info->ftLastAccessTime, 0 );
577 info->dwFileAttributes = reply.attr;
578 info->dwVolumeSerialNumber = reply.serial;
579 info->nFileSizeHigh = reply.size_high;
580 info->nFileSizeLow = reply.size_low;
581 info->nNumberOfLinks = reply.links;
582 info->nFileIndexHigh = reply.index_high;
583 info->nFileIndexLow = reply.index_low;
584 return 1;
588 /**************************************************************************
589 * GetFileAttributes16 (KERNEL.420)
591 DWORD WINAPI GetFileAttributes16( LPCSTR name )
593 return GetFileAttributesA( name );
597 /**************************************************************************
598 * GetFileAttributes32A (KERNEL32.217)
600 DWORD WINAPI GetFileAttributesA( LPCSTR name )
602 DOS_FULL_NAME full_name;
603 BY_HANDLE_FILE_INFORMATION info;
605 if (name == NULL || *name=='\0') return -1;
607 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
608 if (!FILE_Stat( full_name.long_name, &info )) return -1;
609 return info.dwFileAttributes;
613 /**************************************************************************
614 * GetFileAttributes32W (KERNEL32.218)
616 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
618 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
619 DWORD res = GetFileAttributesA( nameA );
620 HeapFree( GetProcessHeap(), 0, nameA );
621 return res;
625 /***********************************************************************
626 * GetFileSize (KERNEL32.220)
628 DWORD WINAPI GetFileSize( HFILE hFile, LPDWORD filesizehigh )
630 BY_HANDLE_FILE_INFORMATION info;
631 if (!GetFileInformationByHandle( hFile, &info )) return 0;
632 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
633 return info.nFileSizeLow;
637 /***********************************************************************
638 * GetFileTime (KERNEL32.221)
640 BOOL WINAPI GetFileTime( HFILE hFile, FILETIME *lpCreationTime,
641 FILETIME *lpLastAccessTime,
642 FILETIME *lpLastWriteTime )
644 BY_HANDLE_FILE_INFORMATION info;
645 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
646 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
647 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
648 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
649 return TRUE;
652 /***********************************************************************
653 * CompareFileTime (KERNEL32.28)
655 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
657 if (!x || !y) return -1;
659 if (x->dwHighDateTime > y->dwHighDateTime)
660 return 1;
661 if (x->dwHighDateTime < y->dwHighDateTime)
662 return -1;
663 if (x->dwLowDateTime > y->dwLowDateTime)
664 return 1;
665 if (x->dwLowDateTime < y->dwLowDateTime)
666 return -1;
667 return 0;
671 /***********************************************************************
672 * GetTempFileName16 (KERNEL.97)
674 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
675 LPSTR buffer )
677 char temppath[144];
679 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
680 drive |= DRIVE_GetCurrentDrive() + 'A';
682 if ((drive & TF_FORCEDRIVE) &&
683 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
685 drive &= ~TF_FORCEDRIVE;
686 WARN(file, "invalid drive %d specified\n", drive );
689 if (drive & TF_FORCEDRIVE)
690 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
691 else
692 GetTempPathA( 132, temppath );
693 return (UINT16)GetTempFileNameA( temppath, prefix, unique, buffer );
697 /***********************************************************************
698 * GetTempFileName32A (KERNEL32.290)
700 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
701 LPSTR buffer)
703 static UINT unique_temp;
704 DOS_FULL_NAME full_name;
705 int i;
706 LPSTR p;
707 UINT num;
709 if ( !path || !prefix || !buffer ) return 0;
711 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
712 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
714 strcpy( buffer, path );
715 p = buffer + strlen(buffer);
717 /* add a \, if there isn't one and path is more than just the drive letter ... */
718 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
719 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
721 *p++ = '~';
722 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
723 sprintf( p, "%04x.tmp", num );
725 /* Now try to create it */
727 if (!unique)
731 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
732 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
733 if (handle != INVALID_HANDLE_VALUE)
734 { /* We created it */
735 TRACE(file, "created %s\n",
736 buffer);
737 CloseHandle( handle );
738 break;
740 if (GetLastError() != ERROR_FILE_EXISTS)
741 break; /* No need to go on */
742 num++;
743 sprintf( p, "%04x.tmp", num );
744 } while (num != (unique & 0xffff));
747 /* Get the full path name */
749 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
751 /* Check if we have write access in the directory */
752 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
753 if (access( full_name.long_name, W_OK ) == -1)
754 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
755 buffer);
757 TRACE(file, "returning %s\n", buffer );
758 return unique ? unique : num;
762 /***********************************************************************
763 * GetTempFileName32W (KERNEL32.291)
765 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
766 LPWSTR buffer )
768 LPSTR patha,prefixa;
769 char buffera[144];
770 UINT ret;
772 if (!path) return 0;
773 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
774 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
775 ret = GetTempFileNameA( patha, prefixa, unique, buffera );
776 lstrcpyAtoW( buffer, buffera );
777 HeapFree( GetProcessHeap(), 0, patha );
778 HeapFree( GetProcessHeap(), 0, prefixa );
779 return ret;
783 /***********************************************************************
784 * FILE_DoOpenFile
786 * Implementation of OpenFile16() and OpenFile32().
788 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
789 BOOL win32 )
791 HFILE hFileRet;
792 FILETIME filetime;
793 WORD filedatetime[2];
794 DOS_FULL_NAME full_name;
795 DWORD access, sharing;
796 char *p;
798 if (!ofs) return HFILE_ERROR;
800 TRACE(file,"%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
801 ((mode & 0x3 )==OF_READ)?"OF_READ":
802 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
803 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
804 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
805 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
806 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
807 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
808 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
809 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
810 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
811 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
812 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
813 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
814 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
815 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
816 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
817 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
821 ofs->cBytes = sizeof(OFSTRUCT);
822 ofs->nErrCode = 0;
823 if (mode & OF_REOPEN) name = ofs->szPathName;
825 if (!name) {
826 ERR(file, "called with `name' set to NULL ! Please debug.\n");
827 return HFILE_ERROR;
830 TRACE(file, "%s %04x\n", name, mode );
832 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
833 Are there any cases where getting the path here is wrong?
834 Uwe Bonnes 1997 Apr 2 */
835 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
836 ofs->szPathName, NULL )) goto error;
837 FILE_ConvertOFMode( mode, &access, &sharing );
839 /* OF_PARSE simply fills the structure */
841 if (mode & OF_PARSE)
843 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
844 != DRIVE_REMOVABLE);
845 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
846 name, ofs->szPathName );
847 return 0;
850 /* OF_CREATE is completely different from all other options, so
851 handle it first */
853 if (mode & OF_CREATE)
855 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
856 sharing, NULL, CREATE_ALWAYS,
857 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
858 goto error;
859 goto success;
862 /* If OF_SEARCH is set, ignore the given path */
864 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
866 /* First try the file name as is */
867 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
868 /* Now remove the path */
869 if (name[0] && (name[1] == ':')) name += 2;
870 if ((p = strrchr( name, '\\' ))) name = p + 1;
871 if ((p = strrchr( name, '/' ))) name = p + 1;
872 if (!name[0]) goto not_found;
875 /* Now look for the file */
877 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
879 found:
880 TRACE(file, "found %s = %s\n",
881 full_name.long_name, full_name.short_name );
882 lstrcpynA( ofs->szPathName, full_name.short_name,
883 sizeof(ofs->szPathName) );
885 if (mode & OF_SHARE_EXCLUSIVE)
886 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
887 on the file <tempdir>/_ins0432._mp to determine how
888 far installation has proceeded.
889 _ins0432._mp is an executable and while running the
890 application expects the open with OF_SHARE_ to fail*/
891 /* Probable FIXME:
892 As our loader closes the files after loading the executable,
893 we can't find the running executable with FILE_InUse.
894 Perhaps the loader should keep the file open.
895 Recheck against how Win handles that case */
897 char *last = strrchr(full_name.long_name,'/');
898 if (!last)
899 last = full_name.long_name - 1;
900 if (GetModuleHandle16(last+1))
902 TRACE(file,"Denying shared open for %s\n",full_name.long_name);
903 return HFILE_ERROR;
907 if (mode & OF_DELETE)
909 if (unlink( full_name.long_name ) == -1) goto not_found;
910 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
911 return 1;
914 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
915 NULL, OPEN_EXISTING, 0, -1 );
916 if (hFileRet == HFILE_ERROR) goto not_found;
918 GetFileTime( hFileRet, NULL, NULL, &filetime );
919 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
920 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
922 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
924 CloseHandle( hFileRet );
925 WARN(file, "(%s): OF_VERIFY failed\n", name );
926 /* FIXME: what error here? */
927 SetLastError( ERROR_FILE_NOT_FOUND );
928 goto error;
931 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
933 success: /* We get here if the open was successful */
934 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
935 if (win32)
937 if (mode & OF_EXIST) /* Return the handle, but close it first */
938 CloseHandle( hFileRet );
940 else
942 hFileRet = FILE_AllocDosHandle( hFileRet );
943 if (hFileRet == HFILE_ERROR16) goto error;
944 if (mode & OF_EXIST) /* Return the handle, but close it first */
945 _lclose16( hFileRet );
947 return hFileRet;
949 not_found: /* We get here if the file does not exist */
950 WARN(file, "'%s' not found\n", name );
951 SetLastError( ERROR_FILE_NOT_FOUND );
952 /* fall through */
954 error: /* We get here if there was an error opening the file */
955 ofs->nErrCode = GetLastError();
956 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
957 name,ofs->nErrCode );
958 return HFILE_ERROR;
962 /***********************************************************************
963 * OpenFile16 (KERNEL.74)
965 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
967 return FILE_DoOpenFile( name, ofs, mode, FALSE );
971 /***********************************************************************
972 * OpenFile32 (KERNEL32.396)
974 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
976 return FILE_DoOpenFile( name, ofs, mode, TRUE );
980 /***********************************************************************
981 * FILE_InitProcessDosHandles
983 * Allocates the default DOS handles for a process. Called either by
984 * AllocDosHandle below or by the DOSVM stuff.
986 BOOL FILE_InitProcessDosHandles( void ) {
987 HANDLE *ptr;
989 if (!(ptr = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY,
990 sizeof(*ptr) * DOS_TABLE_SIZE )))
991 return FALSE;
992 PROCESS_Current()->dos_handles = ptr;
993 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
994 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
995 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
996 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
997 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
998 return TRUE;
1001 /***********************************************************************
1002 * FILE_AllocDosHandle
1004 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1005 * longer valid after this function (even on failure).
1007 HFILE16 FILE_AllocDosHandle( HANDLE handle )
1009 int i;
1010 HANDLE *ptr = PROCESS_Current()->dos_handles;
1012 if (!handle || (handle == INVALID_HANDLE_VALUE))
1013 return INVALID_HANDLE_VALUE16;
1015 if (!ptr) {
1016 if (!FILE_InitProcessDosHandles())
1017 goto error;
1018 ptr = PROCESS_Current()->dos_handles;
1021 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
1022 if (!*ptr)
1024 *ptr = handle;
1025 TRACE( file, "Got %d for h32 %d\n", i, handle );
1026 return i;
1028 error:
1029 CloseHandle( handle );
1030 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1031 return INVALID_HANDLE_VALUE16;
1035 /***********************************************************************
1036 * FILE_GetHandle32
1038 * Return the Win32 handle for a DOS handle.
1040 HANDLE FILE_GetHandle( HFILE16 hfile )
1042 HANDLE *table = PROCESS_Current()->dos_handles;
1043 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1045 SetLastError( ERROR_INVALID_HANDLE );
1046 return INVALID_HANDLE_VALUE;
1048 return table[hfile];
1052 /***********************************************************************
1053 * FILE_Dup2
1055 * dup2() function for DOS handles.
1057 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1059 HANDLE *table = PROCESS_Current()->dos_handles;
1060 HANDLE new_handle;
1062 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1063 !table || !table[hFile1])
1065 SetLastError( ERROR_INVALID_HANDLE );
1066 return HFILE_ERROR16;
1068 if (hFile2 < 5)
1070 FIXME( file, "stdio handle closed, need proper conversion\n" );
1071 SetLastError( ERROR_INVALID_HANDLE );
1072 return HFILE_ERROR16;
1074 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1075 GetCurrentProcess(), &new_handle,
1076 0, FALSE, DUPLICATE_SAME_ACCESS ))
1077 return HFILE_ERROR16;
1078 if (table[hFile2]) CloseHandle( table[hFile2] );
1079 table[hFile2] = new_handle;
1080 return hFile2;
1084 /***********************************************************************
1085 * _lclose16 (KERNEL.81)
1087 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1089 HANDLE *table = PROCESS_Current()->dos_handles;
1091 if (hFile < 5)
1093 FIXME( file, "stdio handle closed, need proper conversion\n" );
1094 SetLastError( ERROR_INVALID_HANDLE );
1095 return HFILE_ERROR16;
1097 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1099 SetLastError( ERROR_INVALID_HANDLE );
1100 return HFILE_ERROR16;
1102 TRACE( file, "%d (handle32=%d)\n", hFile, table[hFile] );
1103 CloseHandle( table[hFile] );
1104 table[hFile] = 0;
1105 return 0;
1109 /***********************************************************************
1110 * _lclose32 (KERNEL32.592)
1112 HFILE WINAPI _lclose( HFILE hFile )
1114 TRACE(file, "handle %d\n", hFile );
1115 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1119 /***********************************************************************
1120 * ReadFile (KERNEL32.428)
1122 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1123 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1125 struct get_read_fd_request req;
1126 int unix_handle, result;
1128 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToRead );
1130 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1131 if (!bytesToRead) return TRUE;
1133 req.handle = hFile;
1134 CLIENT_SendRequest( REQ_GET_READ_FD, -1, 1, &req, sizeof(req) );
1135 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1136 if (unix_handle == -1) return FALSE;
1137 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1139 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1140 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1141 FILE_SetDosError();
1142 break;
1144 close( unix_handle );
1145 if (result == -1) return FALSE;
1146 if (bytesRead) *bytesRead = result;
1147 return TRUE;
1151 /***********************************************************************
1152 * WriteFile (KERNEL32.578)
1154 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1155 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1157 struct get_write_fd_request req;
1158 int unix_handle, result;
1160 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToWrite );
1162 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1163 if (!bytesToWrite) return TRUE;
1165 req.handle = hFile;
1166 CLIENT_SendRequest( REQ_GET_WRITE_FD, -1, 1, &req, sizeof(req) );
1167 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1168 if (unix_handle == -1) return FALSE;
1169 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1171 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1172 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1173 FILE_SetDosError();
1174 break;
1176 close( unix_handle );
1177 if (result == -1) return FALSE;
1178 if (bytesWritten) *bytesWritten = result;
1179 return TRUE;
1183 /***********************************************************************
1184 * WIN16_hread
1186 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1188 LONG maxlen;
1190 TRACE(file, "%d %08lx %ld\n",
1191 hFile, (DWORD)buffer, count );
1193 /* Some programs pass a count larger than the allocated buffer */
1194 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1195 if (count > maxlen) count = maxlen;
1196 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1200 /***********************************************************************
1201 * WIN16_lread
1203 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1205 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1209 /***********************************************************************
1210 * _lread32 (KERNEL32.596)
1212 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1214 DWORD result;
1215 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1216 return result;
1220 /***********************************************************************
1221 * _lread16 (KERNEL.82)
1223 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1225 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1229 /***********************************************************************
1230 * _lcreat16 (KERNEL.83)
1232 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1234 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1238 /***********************************************************************
1239 * _lcreat (KERNEL32.593)
1241 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1243 /* Mask off all flags not explicitly allowed by the doc */
1244 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1245 TRACE(file, "%s %02x\n", path, attr );
1246 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1247 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1248 CREATE_ALWAYS, attr, -1 );
1252 /***********************************************************************
1253 * SetFilePointer (KERNEL32.492)
1255 DWORD WINAPI SetFilePointer( HFILE hFile, LONG distance, LONG *highword,
1256 DWORD method )
1258 struct set_file_pointer_request req;
1259 struct set_file_pointer_reply reply;
1261 if (highword && *highword)
1263 FIXME(file, "64-bit offsets not supported yet\n");
1264 SetLastError( ERROR_INVALID_PARAMETER );
1265 return 0xffffffff;
1267 TRACE(file, "handle %d offset %ld origin %ld\n",
1268 hFile, distance, method );
1270 req.handle = hFile;
1271 req.low = distance;
1272 req.high = highword ? *highword : 0;
1273 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1274 req.whence = method;
1275 CLIENT_SendRequest( REQ_SET_FILE_POINTER, -1, 1, &req, sizeof(req) );
1276 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
1277 SetLastError( 0 );
1278 if (highword) *highword = reply.high;
1279 return reply.low;
1283 /***********************************************************************
1284 * _llseek16 (KERNEL.84)
1286 * FIXME:
1287 * Seeking before the start of the file should be allowed for _llseek16,
1288 * but cause subsequent I/O operations to fail (cf. interrupt list)
1291 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1293 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1297 /***********************************************************************
1298 * _llseek32 (KERNEL32.594)
1300 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1302 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1306 /***********************************************************************
1307 * _lopen16 (KERNEL.85)
1309 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1311 return FILE_AllocDosHandle( _lopen( path, mode ) );
1315 /***********************************************************************
1316 * _lopen32 (KERNEL32.595)
1318 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1320 DWORD access, sharing;
1322 TRACE(file, "('%s',%04x)\n", path, mode );
1323 FILE_ConvertOFMode( mode, &access, &sharing );
1324 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1328 /***********************************************************************
1329 * _lwrite16 (KERNEL.86)
1331 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1333 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1336 /***********************************************************************
1337 * _lwrite32 (KERNEL32.761)
1339 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1341 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1345 /***********************************************************************
1346 * _hread16 (KERNEL.349)
1348 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1350 return _lread( FILE_GetHandle(hFile), buffer, count );
1354 /***********************************************************************
1355 * _hread32 (KERNEL32.590)
1357 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1359 return _lread( hFile, buffer, count );
1363 /***********************************************************************
1364 * _hwrite16 (KERNEL.350)
1366 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1368 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1372 /***********************************************************************
1373 * _hwrite32 (KERNEL32.591)
1375 * experimentation yields that _lwrite:
1376 * o truncates the file at the current position with
1377 * a 0 len write
1378 * o returns 0 on a 0 length write
1379 * o works with console handles
1382 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1384 DWORD result;
1386 TRACE(file, "%d %p %ld\n", handle, buffer, count );
1388 if (!count)
1390 /* Expand or truncate at current position */
1391 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1392 return 0;
1394 if (!WriteFile( handle, buffer, count, &result, NULL ))
1395 return HFILE_ERROR;
1396 return result;
1400 /***********************************************************************
1401 * SetHandleCount16 (KERNEL.199)
1403 UINT16 WINAPI SetHandleCount16( UINT16 count )
1405 HGLOBAL16 hPDB = GetCurrentPDB16();
1406 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1407 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1409 TRACE(file, "(%d)\n", count );
1411 if (count < 20) count = 20; /* No point in going below 20 */
1412 else if (count > 254) count = 254;
1414 if (count == 20)
1416 if (pdb->nbFiles > 20)
1418 memcpy( pdb->fileHandles, files, 20 );
1419 GlobalFree16( pdb->hFileHandles );
1420 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1421 GlobalHandleToSel16( hPDB ) );
1422 pdb->hFileHandles = 0;
1423 pdb->nbFiles = 20;
1426 else /* More than 20, need a new file handles table */
1428 BYTE *newfiles;
1429 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1430 if (!newhandle)
1432 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1433 return pdb->nbFiles;
1435 newfiles = (BYTE *)GlobalLock16( newhandle );
1437 if (count > pdb->nbFiles)
1439 memcpy( newfiles, files, pdb->nbFiles );
1440 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1442 else memcpy( newfiles, files, count );
1443 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1444 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1445 pdb->hFileHandles = newhandle;
1446 pdb->nbFiles = count;
1448 return pdb->nbFiles;
1452 /*************************************************************************
1453 * SetHandleCount32 (KERNEL32.494)
1455 UINT WINAPI SetHandleCount( UINT count )
1457 return MIN( 256, count );
1461 /***********************************************************************
1462 * FlushFileBuffers (KERNEL32.133)
1464 BOOL WINAPI FlushFileBuffers( HFILE hFile )
1466 struct flush_file_request req;
1468 req.handle = hFile;
1469 CLIENT_SendRequest( REQ_FLUSH_FILE, -1, 1, &req, sizeof(req) );
1470 return !CLIENT_WaitReply( NULL, NULL, 0 );
1474 /**************************************************************************
1475 * SetEndOfFile (KERNEL32.483)
1477 BOOL WINAPI SetEndOfFile( HFILE hFile )
1479 struct truncate_file_request req;
1481 req.handle = hFile;
1482 CLIENT_SendRequest( REQ_TRUNCATE_FILE, -1, 1, &req, sizeof(req) );
1483 return !CLIENT_WaitReply( NULL, NULL, 0 );
1487 /***********************************************************************
1488 * DeleteFile16 (KERNEL.146)
1490 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1492 return DeleteFileA( path );
1496 /***********************************************************************
1497 * DeleteFile32A (KERNEL32.71)
1499 BOOL WINAPI DeleteFileA( LPCSTR path )
1501 DOS_FULL_NAME full_name;
1503 TRACE(file, "'%s'\n", path );
1505 if (!*path)
1507 ERR(file, "Empty path passed\n");
1508 return FALSE;
1510 if (DOSFS_GetDevice( path ))
1512 WARN(file, "cannot remove DOS device '%s'!\n", path);
1513 SetLastError( ERROR_FILE_NOT_FOUND );
1514 return FALSE;
1517 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1518 if (unlink( full_name.long_name ) == -1)
1520 FILE_SetDosError();
1521 return FALSE;
1523 return TRUE;
1527 /***********************************************************************
1528 * DeleteFile32W (KERNEL32.72)
1530 BOOL WINAPI DeleteFileW( LPCWSTR path )
1532 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1533 BOOL ret = DeleteFileA( xpath );
1534 HeapFree( GetProcessHeap(), 0, xpath );
1535 return ret;
1539 /***********************************************************************
1540 * FILE_dommap
1542 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1543 DWORD size_high, DWORD size_low,
1544 DWORD offset_high, DWORD offset_low,
1545 int prot, int flags )
1547 int fd = -1;
1548 int pos;
1549 LPVOID ret;
1551 if (size_high || offset_high)
1552 FIXME(file, "offsets larger than 4Gb not supported\n");
1554 if (unix_handle == -1)
1556 #ifdef MAP_ANON
1557 flags |= MAP_ANON;
1558 #else
1559 static int fdzero = -1;
1561 if (fdzero == -1)
1563 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1565 perror( "/dev/zero: open" );
1566 exit(1);
1569 fd = fdzero;
1570 #endif /* MAP_ANON */
1571 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1572 #ifdef MAP_SHARED
1573 flags &= ~MAP_SHARED;
1574 #endif
1575 #ifdef MAP_PRIVATE
1576 flags |= MAP_PRIVATE;
1577 #endif
1579 else fd = unix_handle;
1581 if ((ret = mmap( start, size_low, prot,
1582 flags, fd, offset_low )) != (LPVOID)-1)
1583 return ret;
1585 /* mmap() failed; if this is because the file offset is not */
1586 /* page-aligned (EINVAL), or because the underlying filesystem */
1587 /* does not support mmap() (ENOEXEC), we do it by hand. */
1589 if (unix_handle == -1) return ret;
1590 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1591 if (prot & PROT_WRITE)
1593 /* We cannot fake shared write mappings */
1594 #ifdef MAP_SHARED
1595 if (flags & MAP_SHARED) return ret;
1596 #endif
1597 #ifdef MAP_PRIVATE
1598 if (!(flags & MAP_PRIVATE)) return ret;
1599 #endif
1601 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1602 /* Reserve the memory with an anonymous mmap */
1603 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1604 PROT_READ | PROT_WRITE, flags );
1605 if (ret == (LPVOID)-1) return ret;
1606 /* Now read in the file */
1607 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1609 FILE_munmap( ret, size_high, size_low );
1610 return (LPVOID)-1;
1612 read( fd, ret, size_low );
1613 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1614 mprotect( ret, size_low, prot ); /* Set the right protection */
1615 return ret;
1619 /***********************************************************************
1620 * FILE_munmap
1622 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1624 if (size_high)
1625 FIXME(file, "offsets larger than 4Gb not supported\n");
1626 return munmap( start, size_low );
1630 /***********************************************************************
1631 * GetFileType (KERNEL32.222)
1633 DWORD WINAPI GetFileType( HFILE hFile )
1635 struct get_file_info_request req;
1636 struct get_file_info_reply reply;
1638 req.handle = hFile;
1639 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
1640 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
1641 return FILE_TYPE_UNKNOWN;
1642 return reply.type;
1646 /**************************************************************************
1647 * MoveFileEx32A (KERNEL32.???)
1649 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1651 DOS_FULL_NAME full_name1, full_name2;
1652 int mode=0; /* mode == 1: use copy */
1654 TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag);
1656 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1657 if (fn2) { /* !fn2 means delete fn1 */
1658 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1659 /* Source name and target path are valid */
1660 if ( full_name1.drive != full_name2.drive)
1662 /* use copy, if allowed */
1663 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1664 /* FIXME: Use right error code */
1665 SetLastError( ERROR_FILE_EXISTS );
1666 return FALSE;
1668 else mode =1;
1670 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1671 /* target exists, check if we may overwrite */
1672 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1673 /* FIXME: Use right error code */
1674 SetLastError( ERROR_ACCESS_DENIED );
1675 return FALSE;
1678 else /* fn2 == NULL means delete source */
1679 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1680 if (flag & MOVEFILE_COPY_ALLOWED) {
1681 WARN(file, "Illegal flag\n");
1682 SetLastError( ERROR_GEN_FAILURE );
1683 return FALSE;
1685 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1686 Perhaps we should queue these command and execute it
1687 when exiting... What about using on_exit(2)
1689 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1690 full_name1.long_name);
1691 return TRUE;
1693 else if (unlink( full_name1.long_name ) == -1)
1695 FILE_SetDosError();
1696 return FALSE;
1698 else return TRUE; /* successfully deleted */
1700 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1701 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1702 Perhaps we should queue these command and execute it
1703 when exiting... What about using on_exit(2)
1705 FIXME(file,"Please move existing file '%s' to file '%s'"
1706 "when Wine has finished\n",
1707 full_name1.long_name, full_name2.long_name);
1708 return TRUE;
1711 if (!mode) /* move the file */
1712 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1714 FILE_SetDosError();
1715 return FALSE;
1717 else return TRUE;
1718 else /* copy File */
1719 return CopyFileA(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1723 /**************************************************************************
1724 * MoveFileEx32W (KERNEL32.???)
1726 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1728 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1729 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1730 BOOL res = MoveFileExA( afn1, afn2, flag );
1731 HeapFree( GetProcessHeap(), 0, afn1 );
1732 HeapFree( GetProcessHeap(), 0, afn2 );
1733 return res;
1737 /**************************************************************************
1738 * MoveFile32A (KERNEL32.387)
1740 * Move file or directory
1742 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1744 DOS_FULL_NAME full_name1, full_name2;
1745 struct stat fstat;
1747 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1749 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1750 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1751 /* The new name must not already exist */
1752 return FALSE;
1753 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1755 if (full_name1.drive == full_name2.drive) /* move */
1756 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1758 FILE_SetDosError();
1759 return FALSE;
1761 else return TRUE;
1762 else /*copy */ {
1763 if (stat( full_name1.long_name, &fstat ))
1765 WARN(file, "Invalid source file %s\n",
1766 full_name1.long_name);
1767 FILE_SetDosError();
1768 return FALSE;
1770 if (S_ISDIR(fstat.st_mode)) {
1771 /* No Move for directories across file systems */
1772 /* FIXME: Use right error code */
1773 SetLastError( ERROR_GEN_FAILURE );
1774 return FALSE;
1776 else
1777 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1782 /**************************************************************************
1783 * MoveFile32W (KERNEL32.390)
1785 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1787 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1788 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1789 BOOL res = MoveFileA( afn1, afn2 );
1790 HeapFree( GetProcessHeap(), 0, afn1 );
1791 HeapFree( GetProcessHeap(), 0, afn2 );
1792 return res;
1796 /**************************************************************************
1797 * CopyFile32A (KERNEL32.36)
1799 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1801 HFILE h1, h2;
1802 BY_HANDLE_FILE_INFORMATION info;
1803 UINT count;
1804 BOOL ret = FALSE;
1805 int mode;
1806 char buffer[2048];
1808 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1809 if (!GetFileInformationByHandle( h1, &info ))
1811 CloseHandle( h1 );
1812 return FALSE;
1814 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1815 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1816 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1817 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1819 CloseHandle( h1 );
1820 return FALSE;
1822 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1824 char *p = buffer;
1825 while (count > 0)
1827 INT res = _lwrite( h2, p, count );
1828 if (res <= 0) goto done;
1829 p += res;
1830 count -= res;
1833 ret = TRUE;
1834 done:
1835 CloseHandle( h1 );
1836 CloseHandle( h2 );
1837 return ret;
1841 /**************************************************************************
1842 * CopyFile32W (KERNEL32.37)
1844 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1846 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1847 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1848 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1849 HeapFree( GetProcessHeap(), 0, sourceA );
1850 HeapFree( GetProcessHeap(), 0, destA );
1851 return ret;
1855 /**************************************************************************
1856 * CopyFileEx32A (KERNEL32.858)
1858 * This implementation ignores most of the extra parameters passed-in into
1859 * the "ex" version of the method and calls the CopyFile method.
1860 * It will have to be fixed eventually.
1862 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1863 LPCSTR destFilename,
1864 LPPROGRESS_ROUTINE progressRoutine,
1865 LPVOID appData,
1866 LPBOOL cancelFlagPointer,
1867 DWORD copyFlags)
1869 BOOL failIfExists = FALSE;
1872 * Interpret the only flag that CopyFile can interpret.
1874 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1876 failIfExists = TRUE;
1879 return CopyFileA(sourceFilename, destFilename, failIfExists);
1882 /**************************************************************************
1883 * CopyFileEx32W (KERNEL32.859)
1885 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1886 LPCWSTR destFilename,
1887 LPPROGRESS_ROUTINE progressRoutine,
1888 LPVOID appData,
1889 LPBOOL cancelFlagPointer,
1890 DWORD copyFlags)
1892 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1893 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1895 BOOL ret = CopyFileExA(sourceA,
1896 destA,
1897 progressRoutine,
1898 appData,
1899 cancelFlagPointer,
1900 copyFlags);
1902 HeapFree( GetProcessHeap(), 0, sourceA );
1903 HeapFree( GetProcessHeap(), 0, destA );
1905 return ret;
1909 /***********************************************************************
1910 * SetFileTime (KERNEL32.650)
1912 BOOL WINAPI SetFileTime( HFILE hFile,
1913 const FILETIME *lpCreationTime,
1914 const FILETIME *lpLastAccessTime,
1915 const FILETIME *lpLastWriteTime )
1917 struct set_file_time_request req;
1919 req.handle = hFile;
1920 if (lpLastAccessTime)
1921 req.access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1922 else
1923 req.access_time = 0; /* FIXME */
1924 if (lpLastWriteTime)
1925 req.write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1926 else
1927 req.write_time = 0; /* FIXME */
1929 CLIENT_SendRequest( REQ_SET_FILE_TIME, -1, 1, &req, sizeof(req) );
1930 return !CLIENT_WaitReply( NULL, NULL, 0 );
1934 /**************************************************************************
1935 * LockFile (KERNEL32.511)
1937 BOOL WINAPI LockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1938 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1940 struct lock_file_request req;
1942 req.handle = hFile;
1943 req.offset_low = dwFileOffsetLow;
1944 req.offset_high = dwFileOffsetHigh;
1945 req.count_low = nNumberOfBytesToLockLow;
1946 req.count_high = nNumberOfBytesToLockHigh;
1947 CLIENT_SendRequest( REQ_LOCK_FILE, -1, 1, &req, sizeof(req) );
1948 return !CLIENT_WaitReply( NULL, NULL, 0 );
1951 /**************************************************************************
1952 * LockFileEx [KERNEL32.512]
1954 * Locks a byte range within an open file for shared or exclusive access.
1956 * RETURNS
1957 * success: TRUE
1958 * failure: FALSE
1959 * NOTES
1961 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1963 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1964 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1965 LPOVERLAPPED pOverlapped )
1967 FIXME(file, "hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1968 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
1969 pOverlapped);
1970 if (reserved == 0)
1971 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1972 else
1974 ERR(file, "reserved == %ld: Supposed to be 0??\n", reserved);
1975 SetLastError(ERROR_INVALID_PARAMETER);
1978 return FALSE;
1982 /**************************************************************************
1983 * UnlockFile (KERNEL32.703)
1985 BOOL WINAPI UnlockFile( HFILE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1986 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1988 struct unlock_file_request req;
1990 req.handle = hFile;
1991 req.offset_low = dwFileOffsetLow;
1992 req.offset_high = dwFileOffsetHigh;
1993 req.count_low = nNumberOfBytesToUnlockLow;
1994 req.count_high = nNumberOfBytesToUnlockHigh;
1995 CLIENT_SendRequest( REQ_UNLOCK_FILE, -1, 1, &req, sizeof(req) );
1996 return !CLIENT_WaitReply( NULL, NULL, 0 );
2000 #if 0
2002 struct DOS_FILE_LOCK {
2003 struct DOS_FILE_LOCK * next;
2004 DWORD base;
2005 DWORD len;
2006 DWORD processId;
2007 FILE_OBJECT * dos_file;
2008 /* char * unix_name;*/
2011 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2013 static DOS_FILE_LOCK *locks = NULL;
2014 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2017 /* Locks need to be mirrored because unix file locking is based
2018 * on the pid. Inside of wine there can be multiple WINE processes
2019 * that share the same unix pid.
2020 * Read's and writes should check these locks also - not sure
2021 * how critical that is at this point (FIXME).
2024 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2026 DOS_FILE_LOCK *curr;
2027 DWORD processId;
2029 processId = GetCurrentProcessId();
2031 /* check if lock overlaps a current lock for the same file */
2032 #if 0
2033 for (curr = locks; curr; curr = curr->next) {
2034 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2035 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2036 return TRUE;/* region is identic */
2037 if ((f->l_start < (curr->base + curr->len)) &&
2038 ((f->l_start + f->l_len) > curr->base)) {
2039 /* region overlaps */
2040 return FALSE;
2044 #endif
2046 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2047 curr->processId = GetCurrentProcessId();
2048 curr->base = f->l_start;
2049 curr->len = f->l_len;
2050 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
2051 curr->next = locks;
2052 curr->dos_file = file;
2053 locks = curr;
2054 return TRUE;
2057 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2059 DWORD processId;
2060 DOS_FILE_LOCK **curr;
2061 DOS_FILE_LOCK *rem;
2063 processId = GetCurrentProcessId();
2064 curr = &locks;
2065 while (*curr) {
2066 if ((*curr)->dos_file == file) {
2067 rem = *curr;
2068 *curr = (*curr)->next;
2069 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2070 HeapFree( SystemHeap, 0, rem );
2072 else
2073 curr = &(*curr)->next;
2077 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2079 DWORD processId;
2080 DOS_FILE_LOCK **curr;
2081 DOS_FILE_LOCK *rem;
2083 processId = GetCurrentProcessId();
2084 for (curr = &locks; *curr; curr = &(*curr)->next) {
2085 if ((*curr)->processId == processId &&
2086 (*curr)->dos_file == file &&
2087 (*curr)->base == f->l_start &&
2088 (*curr)->len == f->l_len) {
2089 /* this is the same lock */
2090 rem = *curr;
2091 *curr = (*curr)->next;
2092 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2093 HeapFree( SystemHeap, 0, rem );
2094 return TRUE;
2097 /* no matching lock found */
2098 return FALSE;
2102 /**************************************************************************
2103 * LockFile (KERNEL32.511)
2105 BOOL WINAPI LockFile(
2106 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2107 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2109 struct flock f;
2110 FILE_OBJECT *file;
2112 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2113 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2114 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2116 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2117 FIXME(file, "Unimplemented bytes > 32bits\n");
2118 return FALSE;
2121 f.l_start = dwFileOffsetLow;
2122 f.l_len = nNumberOfBytesToLockLow;
2123 f.l_whence = SEEK_SET;
2124 f.l_pid = 0;
2125 f.l_type = F_WRLCK;
2127 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2129 /* shadow locks internally */
2130 if (!DOS_AddLock(file, &f)) {
2131 SetLastError( ERROR_LOCK_VIOLATION );
2132 return FALSE;
2135 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2136 #ifdef USE_UNIX_LOCKS
2137 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2138 if (errno == EACCES || errno == EAGAIN) {
2139 SetLastError( ERROR_LOCK_VIOLATION );
2141 else {
2142 FILE_SetDosError();
2144 /* remove our internal copy of the lock */
2145 DOS_RemoveLock(file, &f);
2146 return FALSE;
2148 #endif
2149 return TRUE;
2153 /**************************************************************************
2154 * UnlockFile (KERNEL32.703)
2156 BOOL WINAPI UnlockFile(
2157 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2158 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2160 FILE_OBJECT *file;
2161 struct flock f;
2163 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2164 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2165 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2167 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2168 WARN(file, "Unimplemented bytes > 32bits\n");
2169 return FALSE;
2172 f.l_start = dwFileOffsetLow;
2173 f.l_len = nNumberOfBytesToUnlockLow;
2174 f.l_whence = SEEK_SET;
2175 f.l_pid = 0;
2176 f.l_type = F_UNLCK;
2178 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2180 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2182 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2183 #ifdef USE_UNIX_LOCKS
2184 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2185 FILE_SetDosError();
2186 return FALSE;
2188 #endif
2189 return TRUE;
2191 #endif
2193 /**************************************************************************
2194 * GetFileAttributesEx32A [KERNEL32.874]
2196 BOOL WINAPI GetFileAttributesExA(
2197 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2198 LPVOID lpFileInformation)
2200 DOS_FULL_NAME full_name;
2201 BY_HANDLE_FILE_INFORMATION info;
2203 if (lpFileName == NULL) return FALSE;
2204 if (lpFileInformation == NULL) return FALSE;
2206 if (fInfoLevelId == GetFileExInfoStandard) {
2207 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2208 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2209 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2210 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2212 lpFad->dwFileAttributes = info.dwFileAttributes;
2213 lpFad->ftCreationTime = info.ftCreationTime;
2214 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2215 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2216 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2217 lpFad->nFileSizeLow = info.nFileSizeLow;
2219 else {
2220 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
2221 return FALSE;
2224 return TRUE;
2228 /**************************************************************************
2229 * GetFileAttributesEx32W [KERNEL32.875]
2231 BOOL WINAPI GetFileAttributesExW(
2232 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2233 LPVOID lpFileInformation)
2235 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2236 BOOL res =
2237 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2238 HeapFree( GetProcessHeap(), 0, nameA );
2239 return res;