Fix if EndDialog called in WM_INITDIALOG (DIALOG_DoDialogBox).
[wine/multimedia.git] / files / file.c
blob9768cac1d1fb7164f2fd4b9f73b2cb86ebb2b9e4
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 "windows.h"
28 #include "winerror.h"
29 #include "drive.h"
30 #include "device.h"
31 #include "file.h"
32 #include "global.h"
33 #include "heap.h"
34 #include "msdos.h"
35 #include "options.h"
36 #include "ldt.h"
37 #include "process.h"
38 #include "task.h"
39 #include "async.h"
40 #include "wincon.h"
41 #include "debug.h"
43 #include "server/request.h"
44 #include "server.h"
46 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
47 #define MAP_ANON MAP_ANONYMOUS
48 #endif
50 /* The file object */
51 typedef struct
53 K32OBJ header;
54 } FILE_OBJECT;
57 /* Size of per-process table of DOS handles */
58 #define DOS_TABLE_SIZE 256
61 /***********************************************************************
62 * FILE_ConvertOFMode
64 * Convert OF_* mode into flags for CreateFile.
66 static void FILE_ConvertOFMode( INT32 mode, DWORD *access, DWORD *sharing )
68 switch(mode & 0x03)
70 case OF_READ: *access = GENERIC_READ; break;
71 case OF_WRITE: *access = GENERIC_WRITE; break;
72 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
73 default: *access = 0; break;
75 switch(mode & 0x70)
77 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
78 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
79 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
80 case OF_SHARE_DENY_NONE:
81 case OF_SHARE_COMPAT:
82 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
87 #if 0
88 /***********************************************************************
89 * FILE_ShareDeny
91 * PARAMS
92 * oldmode[I] mode how file was first opened
93 * mode[I] mode how the file should get opened
94 * RETURNS
95 * TRUE: deny open
96 * FALSE: allow open
98 * Look what we have to do with the given SHARE modes
100 * Ralph Brown's interrupt list gives following explication, I guess
101 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
103 * FIXME: Validate this function
104 ========from Ralph Brown's list =========
105 (Table 0750)
106 Values of DOS file sharing behavior:
107 | Second and subsequent Opens
108 First |Compat Deny Deny Deny Deny
109 Open | All Write Read None
110 |R W RW R W RW R W RW R W RW R W RW
111 - - - - -| - - - - - - - - - - - - - - - - -
112 Compat R |Y Y Y N N N 1 N N N N N 1 N N
113 W |Y Y Y N N N N N N N N N N N N
114 RW|Y Y Y N N N N N N N N N N N N
115 - - - - -|
116 Deny R |C C C N N N N N N N N N N N N
117 All W |C C C N N N N N N N N N N N N
118 RW|C C C N N N N N N N N N N N N
119 - - - - -|
120 Deny R |2 C C N N N Y N N N N N Y N N
121 Write W |C C C N N N N N N Y N N Y N N
122 RW|C C C N N N N N N N N N Y N N
123 - - - - -|
124 Deny R |C C C N N N N Y N N N N N Y N
125 Read W |C C C N N N N N N N Y N N Y N
126 RW|C C C N N N N N N N N N N Y N
127 - - - - -|
128 Deny R |2 C C N N N Y Y Y N N N Y Y Y
129 None W |C C C N N N N N N Y Y Y Y Y Y
130 RW|C C C N N N N N N N N N Y Y Y
131 Legend: Y = open succeeds, N = open fails with error code 05h
132 C = open fails, INT 24 generated
133 1 = open succeeds if file read-only, else fails with error code
134 2 = open succeeds if file read-only, else fails with INT 24
135 ========end of description from Ralph Brown's List =====
136 For every "Y" in the table we return FALSE
137 For every "N" we set the DOS_ERROR and return TRUE
138 For all other cases we barf,set the DOS_ERROR and return TRUE
141 static BOOL32 FILE_ShareDeny( int mode, int oldmode)
143 int oldsharemode = oldmode & 0x70;
144 int sharemode = mode & 0x70;
145 int oldopenmode = oldmode & 3;
146 int openmode = mode & 3;
148 switch (oldsharemode)
150 case OF_SHARE_COMPAT:
151 if (sharemode == OF_SHARE_COMPAT) return FALSE;
152 if (openmode == OF_READ) goto test_ro_err05 ;
153 goto fail_error05;
154 case OF_SHARE_EXCLUSIVE:
155 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
156 goto fail_error05;
157 case OF_SHARE_DENY_WRITE:
158 if (openmode != OF_READ)
160 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
161 goto fail_error05;
163 switch (sharemode)
165 case OF_SHARE_COMPAT:
166 if (oldopenmode == OF_READ) goto test_ro_int24 ;
167 goto fail_int24;
168 case OF_SHARE_DENY_NONE :
169 return FALSE;
170 case OF_SHARE_DENY_WRITE :
171 if (oldopenmode == OF_READ) return FALSE;
172 case OF_SHARE_DENY_READ :
173 if (oldopenmode == OF_WRITE) return FALSE;
174 case OF_SHARE_EXCLUSIVE:
175 default:
176 goto fail_error05;
178 break;
179 case OF_SHARE_DENY_READ:
180 if (openmode != OF_WRITE)
182 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
183 goto fail_error05;
185 switch (sharemode)
187 case OF_SHARE_COMPAT:
188 goto fail_int24;
189 case OF_SHARE_DENY_NONE :
190 return FALSE;
191 case OF_SHARE_DENY_WRITE :
192 if (oldopenmode == OF_READ) return FALSE;
193 case OF_SHARE_DENY_READ :
194 if (oldopenmode == OF_WRITE) return FALSE;
195 case OF_SHARE_EXCLUSIVE:
196 default:
197 goto fail_error05;
199 break;
200 case OF_SHARE_DENY_NONE:
201 switch (sharemode)
203 case OF_SHARE_COMPAT:
204 goto fail_int24;
205 case OF_SHARE_DENY_NONE :
206 return FALSE;
207 case OF_SHARE_DENY_WRITE :
208 if (oldopenmode == OF_READ) return FALSE;
209 case OF_SHARE_DENY_READ :
210 if (oldopenmode == OF_WRITE) return FALSE;
211 case OF_SHARE_EXCLUSIVE:
212 default:
213 goto fail_error05;
215 default:
216 ERR(file,"unknown mode\n");
218 ERR(file,"shouldn't happen\n");
219 ERR(file,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
220 return TRUE;
222 test_ro_int24:
223 if (oldmode == OF_READ)
224 return FALSE;
225 /* Fall through */
226 fail_int24:
227 FIXME(file,"generate INT24 missing\n");
228 /* Is this the right error? */
229 SetLastError( ERROR_ACCESS_DENIED );
230 return TRUE;
232 test_ro_err05:
233 if (oldmode == OF_READ)
234 return FALSE;
235 /* fall through */
236 fail_error05:
237 TRACE(file,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
238 SetLastError( ERROR_ACCESS_DENIED );
239 return TRUE;
241 #endif
244 /***********************************************************************
245 * FILE_SetDosError
247 * Set the DOS error code from errno.
249 void FILE_SetDosError(void)
251 int save_errno = errno; /* errno gets overwritten by printf */
253 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
254 switch (save_errno)
256 case EAGAIN:
257 SetLastError( ERROR_SHARING_VIOLATION );
258 break;
259 case EBADF:
260 SetLastError( ERROR_INVALID_HANDLE );
261 break;
262 case ENOSPC:
263 SetLastError( ERROR_HANDLE_DISK_FULL );
264 break;
265 case EACCES:
266 case EPERM:
267 case EROFS:
268 SetLastError( ERROR_ACCESS_DENIED );
269 break;
270 case EBUSY:
271 SetLastError( ERROR_LOCK_VIOLATION );
272 break;
273 case ENOENT:
274 SetLastError( ERROR_FILE_NOT_FOUND );
275 break;
276 case EISDIR:
277 SetLastError( ERROR_CANNOT_MAKE );
278 break;
279 case ENFILE:
280 case EMFILE:
281 SetLastError( ERROR_NO_MORE_FILES );
282 break;
283 case EEXIST:
284 SetLastError( ERROR_FILE_EXISTS );
285 break;
286 case EINVAL:
287 case ESPIPE:
288 SetLastError( ERROR_SEEK );
289 break;
290 case ENOTEMPTY:
291 SetLastError( ERROR_DIR_NOT_EMPTY );
292 break;
293 default:
294 perror( "int21: unknown errno" );
295 SetLastError( ERROR_GEN_FAILURE );
296 break;
298 errno = save_errno;
302 /***********************************************************************
303 * FILE_DupUnixHandle
305 * Duplicate a Unix handle into a task handle.
307 HFILE32 FILE_DupUnixHandle( int fd, DWORD access )
309 FILE_OBJECT *file;
310 int unix_handle;
311 struct create_file_request req;
312 struct create_file_reply reply;
314 if ((unix_handle = dup(fd)) == -1)
316 FILE_SetDosError();
317 return INVALID_HANDLE_VALUE32;
319 req.access = access;
320 req.inherit = 1;
321 req.sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
322 req.create = 0;
323 req.attrs = 0;
325 CLIENT_SendRequest( REQ_CREATE_FILE, unix_handle, 1,
326 &req, sizeof(req) );
327 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
328 if (reply.handle == -1) return INVALID_HANDLE_VALUE32;
330 if (!(file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) )))
332 CLIENT_CloseHandle( reply.handle );
333 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
334 return (HFILE32)NULL;
336 file->header.type = K32OBJ_FILE;
337 file->header.refcount = 0;
338 return HANDLE_Alloc( PROCESS_Current(), &file->header, req.access,
339 req.inherit, reply.handle );
343 /***********************************************************************
344 * FILE_CreateFile
346 * Implementation of CreateFile. Takes a Unix path name.
348 HFILE32 FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
349 LPSECURITY_ATTRIBUTES sa, DWORD creation,
350 DWORD attributes, HANDLE32 template )
352 FILE_OBJECT *file;
353 struct create_file_request req;
354 struct create_file_reply reply;
356 req.access = access;
357 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
358 req.sharing = sharing;
359 req.create = creation;
360 req.attrs = attributes;
361 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
362 &req, sizeof(req),
363 filename, strlen(filename) + 1 );
364 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
366 /* If write access failed, retry without GENERIC_WRITE */
368 if ((reply.handle == -1) && !Options.failReadOnly &&
369 (access & GENERIC_WRITE))
371 DWORD lasterror = GetLastError();
372 if ((lasterror == ERROR_ACCESS_DENIED) ||
373 (lasterror == ERROR_WRITE_PROTECT))
375 req.access &= ~GENERIC_WRITE;
376 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
377 &req, sizeof(req),
378 filename, strlen(filename) + 1 );
379 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
382 if (reply.handle == -1) return INVALID_HANDLE_VALUE32;
384 /* Now build the FILE_OBJECT */
386 if (!(file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) )))
388 SetLastError( ERROR_OUTOFMEMORY );
389 CLIENT_CloseHandle( reply.handle );
390 return (HFILE32)INVALID_HANDLE_VALUE32;
392 file->header.type = K32OBJ_FILE;
393 file->header.refcount = 0;
394 return HANDLE_Alloc( PROCESS_Current(), &file->header, req.access,
395 req.inherit, reply.handle );
399 /***********************************************************************
400 * FILE_CreateDevice
402 * Same as FILE_CreateFile but for a device
404 HFILE32 FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
406 FILE_OBJECT *file;
407 struct create_device_request req;
408 struct create_device_reply reply;
410 req.access = access;
411 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
412 req.id = client_id;
413 CLIENT_SendRequest( REQ_CREATE_DEVICE, -1, 1, &req, sizeof(req) );
414 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
415 if (reply.handle == -1) return INVALID_HANDLE_VALUE32;
417 /* Now build the FILE_OBJECT */
419 if (!(file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) )))
421 SetLastError( ERROR_OUTOFMEMORY );
422 CLIENT_CloseHandle( reply.handle );
423 return (HFILE32)INVALID_HANDLE_VALUE32;
425 file->header.type = K32OBJ_FILE;
426 file->header.refcount = 0;
427 return HANDLE_Alloc( PROCESS_Current(), &file->header, req.access,
428 req.inherit, reply.handle );
432 /*************************************************************************
433 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
435 * Creates or opens an object, and returns a handle that can be used to
436 * access that object.
438 * PARAMS
440 * filename [I] pointer to filename to be accessed
441 * access [I] access mode requested
442 * sharing [I] share mode
443 * sa [I] pointer to security attributes
444 * creation [I] how to create the file
445 * attributes [I] attributes for newly created file
446 * template [I] handle to file with extended attributes to copy
448 * RETURNS
449 * Success: Open handle to specified file
450 * Failure: INVALID_HANDLE_VALUE
452 * NOTES
453 * Should call SetLastError() on failure.
455 * BUGS
457 * Doesn't support character devices, pipes, template files, or a
458 * lot of the 'attributes' flags yet.
460 HFILE32 WINAPI CreateFile32A( LPCSTR filename, DWORD access, DWORD sharing,
461 LPSECURITY_ATTRIBUTES sa, DWORD creation,
462 DWORD attributes, HANDLE32 template )
464 DOS_FULL_NAME full_name;
466 if (!filename)
468 SetLastError( ERROR_INVALID_PARAMETER );
469 return HFILE_ERROR32;
472 /* If the name starts with '\\?\', ignore the first 4 chars. */
473 if (!strncmp(filename, "\\\\?\\", 4))
475 filename += 4;
476 if (!strncmp(filename, "UNC\\", 4))
478 FIXME( file, "UNC name (%s) not supported.\n", filename );
479 SetLastError( ERROR_PATH_NOT_FOUND );
480 return HFILE_ERROR32;
484 if (!strncmp(filename, "\\\\.\\", 4))
485 return DEVICE_Open( filename+4, access, sa );
487 /* If the name still starts with '\\', it's a UNC name. */
488 if (!strncmp(filename, "\\\\", 2))
490 FIXME( file, "UNC name (%s) not supported.\n", filename );
491 SetLastError( ERROR_PATH_NOT_FOUND );
492 return HFILE_ERROR32;
495 /* Open a console for CONIN$ or CONOUT$ */
496 if (!lstrcmpi32A(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
497 if (!lstrcmpi32A(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
499 if (DOSFS_GetDevice( filename ))
501 HFILE32 ret;
503 TRACE(file, "opening device '%s'\n", filename );
505 if (HFILE_ERROR32!=(ret=DOSFS_OpenDevice( filename, access )))
506 return ret;
508 /* Do not silence this please. It is a critical error. -MM */
509 ERR(file, "Couldn't open device '%s'!\n",filename);
510 SetLastError( ERROR_FILE_NOT_FOUND );
511 return HFILE_ERROR32;
514 /* check for filename, don't check for last entry if creating */
515 if (!DOSFS_GetFullName( filename,
516 (creation == OPEN_EXISTING) || (creation == TRUNCATE_EXISTING), &full_name ))
517 return HFILE_ERROR32;
519 return FILE_CreateFile( full_name.long_name, access, sharing,
520 sa, creation, attributes, template );
525 /*************************************************************************
526 * CreateFile32W (KERNEL32.48)
528 HFILE32 WINAPI CreateFile32W( LPCWSTR filename, DWORD access, DWORD sharing,
529 LPSECURITY_ATTRIBUTES sa, DWORD creation,
530 DWORD attributes, HANDLE32 template)
532 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
533 HFILE32 res = CreateFile32A( afn, access, sharing, sa, creation, attributes, template );
534 HeapFree( GetProcessHeap(), 0, afn );
535 return res;
539 /***********************************************************************
540 * FILE_FillInfo
542 * Fill a file information from a struct stat.
544 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
546 if (S_ISDIR(st->st_mode))
547 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
548 else
549 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
550 if (!(st->st_mode & S_IWUSR))
551 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
553 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
554 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
555 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
557 info->dwVolumeSerialNumber = 0; /* FIXME */
558 info->nFileSizeHigh = 0;
559 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
560 info->nNumberOfLinks = st->st_nlink;
561 info->nFileIndexHigh = 0;
562 info->nFileIndexLow = st->st_ino;
566 /***********************************************************************
567 * FILE_Stat
569 * Stat a Unix path name. Return TRUE if OK.
571 BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
573 struct stat st;
575 if (!unixName || !info) return FALSE;
577 if (stat( unixName, &st ) == -1)
579 FILE_SetDosError();
580 return FALSE;
582 FILE_FillInfo( &st, info );
583 return TRUE;
587 /***********************************************************************
588 * GetFileInformationByHandle (KERNEL32.219)
590 DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile,
591 BY_HANDLE_FILE_INFORMATION *info )
593 struct get_file_info_request req;
594 struct get_file_info_reply reply;
596 if (!info) return 0;
597 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
598 K32OBJ_FILE, 0 )) == -1)
599 return 0;
600 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
601 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
602 return 0;
603 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftCreationTime, 0 );
604 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftLastWriteTime, 0 );
605 DOSFS_UnixTimeToFileTime( reply.access_time, &info->ftLastAccessTime, 0 );
606 info->dwFileAttributes = reply.attr;
607 info->dwVolumeSerialNumber = reply.serial;
608 info->nFileSizeHigh = reply.size_high;
609 info->nFileSizeLow = reply.size_low;
610 info->nNumberOfLinks = reply.links;
611 info->nFileIndexHigh = reply.index_high;
612 info->nFileIndexLow = reply.index_low;
613 return 1;
617 /**************************************************************************
618 * GetFileAttributes16 (KERNEL.420)
620 DWORD WINAPI GetFileAttributes16( LPCSTR name )
622 return GetFileAttributes32A( name );
626 /**************************************************************************
627 * GetFileAttributes32A (KERNEL32.217)
629 DWORD WINAPI GetFileAttributes32A( LPCSTR name )
631 DOS_FULL_NAME full_name;
632 BY_HANDLE_FILE_INFORMATION info;
634 if (name == NULL || *name=='\0') return -1;
636 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
637 if (!FILE_Stat( full_name.long_name, &info )) return -1;
638 return info.dwFileAttributes;
642 /**************************************************************************
643 * GetFileAttributes32W (KERNEL32.218)
645 DWORD WINAPI GetFileAttributes32W( LPCWSTR name )
647 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
648 DWORD res = GetFileAttributes32A( nameA );
649 HeapFree( GetProcessHeap(), 0, nameA );
650 return res;
654 /***********************************************************************
655 * GetFileSize (KERNEL32.220)
657 DWORD WINAPI GetFileSize( HFILE32 hFile, LPDWORD filesizehigh )
659 BY_HANDLE_FILE_INFORMATION info;
660 if (!GetFileInformationByHandle( hFile, &info )) return 0;
661 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
662 return info.nFileSizeLow;
666 /***********************************************************************
667 * GetFileTime (KERNEL32.221)
669 BOOL32 WINAPI GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
670 FILETIME *lpLastAccessTime,
671 FILETIME *lpLastWriteTime )
673 BY_HANDLE_FILE_INFORMATION info;
674 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
675 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
676 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
677 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
678 return TRUE;
681 /***********************************************************************
682 * CompareFileTime (KERNEL32.28)
684 INT32 WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
686 if (!x || !y) return -1;
688 if (x->dwHighDateTime > y->dwHighDateTime)
689 return 1;
690 if (x->dwHighDateTime < y->dwHighDateTime)
691 return -1;
692 if (x->dwLowDateTime > y->dwLowDateTime)
693 return 1;
694 if (x->dwLowDateTime < y->dwLowDateTime)
695 return -1;
696 return 0;
700 /***********************************************************************
701 * GetTempFileName16 (KERNEL.97)
703 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
704 LPSTR buffer )
706 char temppath[144];
708 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
709 drive |= DRIVE_GetCurrentDrive() + 'A';
711 if ((drive & TF_FORCEDRIVE) &&
712 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
714 drive &= ~TF_FORCEDRIVE;
715 WARN(file, "invalid drive %d specified\n", drive );
718 if (drive & TF_FORCEDRIVE)
719 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
720 else
721 GetTempPath32A( 132, temppath );
722 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
726 /***********************************************************************
727 * GetTempFileName32A (KERNEL32.290)
729 UINT32 WINAPI GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
730 LPSTR buffer)
732 static UINT32 unique_temp;
733 DOS_FULL_NAME full_name;
734 int i;
735 LPSTR p;
736 UINT32 num;
738 if ( !path || !prefix || !buffer ) return 0;
740 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
741 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
743 strcpy( buffer, path );
744 p = buffer + strlen(buffer);
746 /* add a \, if there isn't one and path is more than just the drive letter ... */
747 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
748 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
750 *p++ = '~';
751 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
752 sprintf( p, "%04x.tmp", num );
754 /* Now try to create it */
756 if (!unique)
760 HFILE32 handle = CreateFile32A( buffer, GENERIC_WRITE, 0, NULL,
761 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
762 if (handle != INVALID_HANDLE_VALUE32)
763 { /* We created it */
764 TRACE(file, "created %s\n",
765 buffer);
766 CloseHandle( handle );
767 break;
769 if (GetLastError() != ERROR_FILE_EXISTS)
770 break; /* No need to go on */
771 num++;
772 sprintf( p, "%04x.tmp", num );
773 } while (num != (unique & 0xffff));
776 /* Get the full path name */
778 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
780 /* Check if we have write access in the directory */
781 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
782 if (access( full_name.long_name, W_OK ) == -1)
783 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
784 buffer);
786 TRACE(file, "returning %s\n", buffer );
787 return unique ? unique : num;
791 /***********************************************************************
792 * GetTempFileName32W (KERNEL32.291)
794 UINT32 WINAPI GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
795 LPWSTR buffer )
797 LPSTR patha,prefixa;
798 char buffera[144];
799 UINT32 ret;
801 if (!path) return 0;
802 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
803 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
804 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
805 lstrcpyAtoW( buffer, buffera );
806 HeapFree( GetProcessHeap(), 0, patha );
807 HeapFree( GetProcessHeap(), 0, prefixa );
808 return ret;
812 /***********************************************************************
813 * FILE_DoOpenFile
815 * Implementation of OpenFile16() and OpenFile32().
817 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
818 BOOL32 win32 )
820 HFILE32 hFileRet;
821 FILETIME filetime;
822 WORD filedatetime[2];
823 DOS_FULL_NAME full_name;
824 DWORD access, sharing;
825 char *p;
827 if (!ofs) return HFILE_ERROR32;
829 ofs->cBytes = sizeof(OFSTRUCT);
830 ofs->nErrCode = 0;
831 if (mode & OF_REOPEN) name = ofs->szPathName;
833 if (!name) {
834 ERR(file, "called with `name' set to NULL ! Please debug.\n");
835 return HFILE_ERROR32;
838 TRACE(file, "%s %04x\n", name, mode );
840 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
841 Are there any cases where getting the path here is wrong?
842 Uwe Bonnes 1997 Apr 2 */
843 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
844 ofs->szPathName, NULL )) goto error;
845 FILE_ConvertOFMode( mode, &access, &sharing );
847 /* OF_PARSE simply fills the structure */
849 if (mode & OF_PARSE)
851 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
852 != DRIVE_REMOVABLE);
853 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
854 name, ofs->szPathName );
855 return 0;
858 /* OF_CREATE is completely different from all other options, so
859 handle it first */
861 if (mode & OF_CREATE)
863 if ((hFileRet = CreateFile32A( name, GENERIC_READ | GENERIC_WRITE,
864 sharing, NULL, CREATE_ALWAYS,
865 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE32)
866 goto error;
867 goto success;
870 /* If OF_SEARCH is set, ignore the given path */
872 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
874 /* First try the file name as is */
875 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
876 /* Now remove the path */
877 if (name[0] && (name[1] == ':')) name += 2;
878 if ((p = strrchr( name, '\\' ))) name = p + 1;
879 if ((p = strrchr( name, '/' ))) name = p + 1;
880 if (!name[0]) goto not_found;
883 /* Now look for the file */
885 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
887 found:
888 TRACE(file, "found %s = %s\n",
889 full_name.long_name, full_name.short_name );
890 lstrcpyn32A( ofs->szPathName, full_name.short_name,
891 sizeof(ofs->szPathName) );
893 if (mode & OF_SHARE_EXCLUSIVE)
894 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
895 on the file <tempdir>/_ins0432._mp to determine how
896 far installation has proceeded.
897 _ins0432._mp is an executable and while running the
898 application expects the open with OF_SHARE_ to fail*/
899 /* Probable FIXME:
900 As our loader closes the files after loading the executable,
901 we can't find the running executable with FILE_InUse.
902 Perhaps the loader should keep the file open.
903 Recheck against how Win handles that case */
905 char *last = strrchr(full_name.long_name,'/');
906 if (!last)
907 last = full_name.long_name - 1;
908 if (GetModuleHandle16(last+1))
910 TRACE(file,"Denying shared open for %s\n",full_name.long_name);
911 return HFILE_ERROR32;
915 if (mode & OF_DELETE)
917 if (unlink( full_name.long_name ) == -1) goto not_found;
918 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
919 return 1;
922 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
923 NULL, OPEN_EXISTING, 0, -1 );
924 if (hFileRet == HFILE_ERROR32) goto not_found;
926 GetFileTime( hFileRet, NULL, NULL, &filetime );
927 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
928 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
930 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
932 CloseHandle( hFileRet );
933 WARN(file, "(%s): OF_VERIFY failed\n", name );
934 /* FIXME: what error here? */
935 SetLastError( ERROR_FILE_NOT_FOUND );
936 goto error;
939 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
941 success: /* We get here if the open was successful */
942 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
943 if (win32)
945 if (mode & OF_EXIST) /* Return the handle, but close it first */
946 CloseHandle( hFileRet );
948 else
950 hFileRet = FILE_AllocDosHandle( hFileRet );
951 if (hFileRet == HFILE_ERROR16) goto error;
952 if (mode & OF_EXIST) /* Return the handle, but close it first */
953 _lclose16( hFileRet );
955 return hFileRet;
957 not_found: /* We get here if the file does not exist */
958 WARN(file, "'%s' not found\n", name );
959 SetLastError( ERROR_FILE_NOT_FOUND );
960 /* fall through */
962 error: /* We get here if there was an error opening the file */
963 ofs->nErrCode = GetLastError();
964 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
965 name,ofs->nErrCode );
966 return HFILE_ERROR32;
970 /***********************************************************************
971 * OpenFile16 (KERNEL.74)
973 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
975 return FILE_DoOpenFile( name, ofs, mode, FALSE );
979 /***********************************************************************
980 * OpenFile32 (KERNEL32.396)
982 HFILE32 WINAPI OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
984 return FILE_DoOpenFile( name, ofs, mode, TRUE );
988 /***********************************************************************
989 * FILE_InitProcessDosHandles
991 * Allocates the default DOS handles for a process. Called either by
992 * AllocDosHandle below or by the DOSVM stuff.
994 BOOL32 FILE_InitProcessDosHandles( void ) {
995 HANDLE32 *ptr;
997 if (!(ptr = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY,
998 sizeof(*ptr) * DOS_TABLE_SIZE )))
999 return FALSE;
1000 PROCESS_Current()->dos_handles = ptr;
1001 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
1002 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
1003 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
1004 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
1005 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
1006 return TRUE;
1009 /***********************************************************************
1010 * FILE_AllocDosHandle
1012 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1013 * longer valid after this function (even on failure).
1015 HFILE16 FILE_AllocDosHandle( HANDLE32 handle )
1017 int i;
1018 HANDLE32 *ptr = PROCESS_Current()->dos_handles;
1020 if (!handle || (handle == INVALID_HANDLE_VALUE32))
1021 return INVALID_HANDLE_VALUE16;
1023 if (!ptr) {
1024 if (!FILE_InitProcessDosHandles())
1025 goto error;
1026 ptr = PROCESS_Current()->dos_handles;
1029 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
1030 if (!*ptr)
1032 *ptr = handle;
1033 TRACE( file, "Got %d for h32 %d\n", i, handle );
1034 return i;
1036 error:
1037 CloseHandle( handle );
1038 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1039 return INVALID_HANDLE_VALUE16;
1043 /***********************************************************************
1044 * FILE_GetHandle32
1046 * Return the Win32 handle for a DOS handle.
1048 HANDLE32 FILE_GetHandle32( HFILE16 hfile )
1050 HANDLE32 *table = PROCESS_Current()->dos_handles;
1051 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1053 SetLastError( ERROR_INVALID_HANDLE );
1054 return INVALID_HANDLE_VALUE32;
1056 return table[hfile];
1060 /***********************************************************************
1061 * FILE_Dup2
1063 * dup2() function for DOS handles.
1065 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1067 HANDLE32 *table = PROCESS_Current()->dos_handles;
1068 HANDLE32 new_handle;
1070 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1071 !table || !table[hFile1])
1073 SetLastError( ERROR_INVALID_HANDLE );
1074 return HFILE_ERROR16;
1076 if (hFile2 < 5)
1078 FIXME( file, "stdio handle closed, need proper conversion\n" );
1079 SetLastError( ERROR_INVALID_HANDLE );
1080 return HFILE_ERROR16;
1082 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1083 GetCurrentProcess(), &new_handle,
1084 0, FALSE, DUPLICATE_SAME_ACCESS ))
1085 return HFILE_ERROR16;
1086 if (table[hFile2]) CloseHandle( table[hFile2] );
1087 table[hFile2] = new_handle;
1088 return hFile2;
1092 /***********************************************************************
1093 * _lclose16 (KERNEL.81)
1095 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1097 HANDLE32 *table = PROCESS_Current()->dos_handles;
1099 if (hFile < 5)
1101 FIXME( file, "stdio handle closed, need proper conversion\n" );
1102 SetLastError( ERROR_INVALID_HANDLE );
1103 return HFILE_ERROR16;
1105 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1107 SetLastError( ERROR_INVALID_HANDLE );
1108 return HFILE_ERROR16;
1110 TRACE( file, "%d (handle32=%d)\n", hFile, table[hFile] );
1111 CloseHandle( table[hFile] );
1112 table[hFile] = 0;
1113 return 0;
1117 /***********************************************************************
1118 * _lclose32 (KERNEL32.592)
1120 HFILE32 WINAPI _lclose32( HFILE32 hFile )
1122 TRACE(file, "handle %d\n", hFile );
1123 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
1127 /***********************************************************************
1128 * ReadFile (KERNEL32.428)
1130 BOOL32 WINAPI ReadFile( HANDLE32 hFile, LPVOID buffer, DWORD bytesToRead,
1131 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1133 struct get_read_fd_request req;
1134 int unix_handle, result;
1136 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToRead );
1138 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1139 if (!bytesToRead) return TRUE;
1141 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1142 K32OBJ_UNKNOWN, GENERIC_READ )) == -1)
1143 return FALSE;
1144 CLIENT_SendRequest( REQ_GET_READ_FD, -1, 1, &req, sizeof(req) );
1145 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1146 if (unix_handle == -1) return FALSE;
1147 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1149 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1150 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1151 FILE_SetDosError();
1152 break;
1154 close( unix_handle );
1155 if (result == -1) return FALSE;
1156 if (bytesRead) *bytesRead = result;
1157 return TRUE;
1161 /***********************************************************************
1162 * WriteFile (KERNEL32.578)
1164 BOOL32 WINAPI WriteFile( HANDLE32 hFile, LPCVOID buffer, DWORD bytesToWrite,
1165 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1167 struct get_write_fd_request req;
1168 int unix_handle, result;
1170 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToWrite );
1172 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1173 if (!bytesToWrite) return TRUE;
1175 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1176 K32OBJ_UNKNOWN, GENERIC_WRITE )) == -1)
1177 return FALSE;
1178 CLIENT_SendRequest( REQ_GET_WRITE_FD, -1, 1, &req, sizeof(req) );
1179 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1180 if (unix_handle == -1) return FALSE;
1181 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1183 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1184 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1185 FILE_SetDosError();
1186 break;
1188 close( unix_handle );
1189 if (result == -1) return FALSE;
1190 if (bytesWritten) *bytesWritten = result;
1191 return TRUE;
1195 /***********************************************************************
1196 * WIN16_hread
1198 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1200 LONG maxlen;
1202 TRACE(file, "%d %08lx %ld\n",
1203 hFile, (DWORD)buffer, count );
1205 /* Some programs pass a count larger than the allocated buffer */
1206 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1207 if (count > maxlen) count = maxlen;
1208 return _lread32(FILE_GetHandle32(hFile), PTR_SEG_TO_LIN(buffer), count );
1212 /***********************************************************************
1213 * WIN16_lread
1215 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1217 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1221 /***********************************************************************
1222 * _lread32 (KERNEL32.596)
1224 UINT32 WINAPI _lread32( HFILE32 handle, LPVOID buffer, UINT32 count )
1226 DWORD result;
1227 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1228 return result;
1232 /***********************************************************************
1233 * _lread16 (KERNEL.82)
1235 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1237 return (UINT16)_lread32(FILE_GetHandle32(hFile), buffer, (LONG)count );
1241 /***********************************************************************
1242 * _lcreat16 (KERNEL.83)
1244 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1246 TRACE(file, "%s %02x\n", path, attr );
1247 return FILE_AllocDosHandle( _lcreat32( path, attr ) );
1251 /***********************************************************************
1252 * _lcreat32 (KERNEL32.593)
1254 HFILE32 WINAPI _lcreat32( LPCSTR path, INT32 attr )
1256 TRACE(file, "%s %02x\n", path, attr );
1257 return CreateFile32A( path, GENERIC_READ | GENERIC_WRITE,
1258 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1259 CREATE_ALWAYS, attr, -1 );
1263 /***********************************************************************
1264 * _lcreat16_uniq (Not a Windows API)
1266 HFILE16 _lcreat16_uniq( LPCSTR path, INT32 attr )
1268 TRACE(file, "%s %02x\n", path, attr );
1269 return FILE_AllocDosHandle( CreateFile32A( path, GENERIC_READ | GENERIC_WRITE,
1270 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1271 CREATE_NEW, attr, -1 ));
1275 /***********************************************************************
1276 * SetFilePointer (KERNEL32.492)
1278 DWORD WINAPI SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
1279 DWORD method )
1281 struct set_file_pointer_request req;
1282 struct set_file_pointer_reply reply;
1284 if (highword && *highword)
1286 FIXME(file, "64-bit offsets not supported yet\n");
1287 SetLastError( ERROR_INVALID_PARAMETER );
1288 return 0xffffffff;
1290 TRACE(file, "handle %d offset %ld origin %ld\n",
1291 hFile, distance, method );
1293 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1294 K32OBJ_FILE, 0 )) == -1)
1295 return 0xffffffff;
1296 req.low = distance;
1297 req.high = highword ? *highword : 0;
1298 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1299 req.whence = method;
1300 CLIENT_SendRequest( REQ_SET_FILE_POINTER, -1, 1, &req, sizeof(req) );
1301 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
1302 SetLastError( 0 );
1303 if (highword) *highword = reply.high;
1304 return reply.low;
1308 /***********************************************************************
1309 * _llseek16 (KERNEL.84)
1311 * FIXME:
1312 * Seeking before the start of the file should be allowed for _llseek16,
1313 * but cause subsequent I/O operations to fail (cf. interrupt list)
1316 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1318 return SetFilePointer( FILE_GetHandle32(hFile), lOffset, NULL, nOrigin );
1322 /***********************************************************************
1323 * _llseek32 (KERNEL32.594)
1325 LONG WINAPI _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
1327 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1331 /***********************************************************************
1332 * _lopen16 (KERNEL.85)
1334 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1336 return FILE_AllocDosHandle( _lopen32( path, mode ) );
1340 /***********************************************************************
1341 * _lopen32 (KERNEL32.595)
1343 HFILE32 WINAPI _lopen32( LPCSTR path, INT32 mode )
1345 DWORD access, sharing;
1347 TRACE(file, "('%s',%04x)\n", path, mode );
1348 FILE_ConvertOFMode( mode, &access, &sharing );
1349 return CreateFile32A( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1353 /***********************************************************************
1354 * _lwrite16 (KERNEL.86)
1356 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1358 return (UINT16)_hwrite32( FILE_GetHandle32(hFile), buffer, (LONG)count );
1361 /***********************************************************************
1362 * _lwrite32 (KERNEL32.761)
1364 UINT32 WINAPI _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
1366 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
1370 /***********************************************************************
1371 * _hread16 (KERNEL.349)
1373 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1375 return _lread32( FILE_GetHandle32(hFile), buffer, count );
1379 /***********************************************************************
1380 * _hread32 (KERNEL32.590)
1382 LONG WINAPI _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
1384 return _lread32( hFile, buffer, count );
1388 /***********************************************************************
1389 * _hwrite16 (KERNEL.350)
1391 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1393 return _hwrite32( FILE_GetHandle32(hFile), buffer, count );
1397 /***********************************************************************
1398 * _hwrite32 (KERNEL32.591)
1400 * experimentation yields that _lwrite:
1401 * o truncates the file at the current position with
1402 * a 0 len write
1403 * o returns 0 on a 0 length write
1404 * o works with console handles
1407 LONG WINAPI _hwrite32( HFILE32 handle, LPCSTR buffer, LONG count )
1409 DWORD result;
1411 TRACE(file, "%d %p %ld\n", handle, buffer, count );
1413 if (!count)
1415 /* Expand or truncate at current position */
1416 if (!SetEndOfFile( handle )) return HFILE_ERROR32;
1417 return 0;
1419 if (!WriteFile( handle, buffer, count, &result, NULL ))
1420 return HFILE_ERROR32;
1421 return result;
1425 /***********************************************************************
1426 * SetHandleCount16 (KERNEL.199)
1428 UINT16 WINAPI SetHandleCount16( UINT16 count )
1430 HGLOBAL16 hPDB = GetCurrentPDB();
1431 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1432 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1434 TRACE(file, "(%d)\n", count );
1436 if (count < 20) count = 20; /* No point in going below 20 */
1437 else if (count > 254) count = 254;
1439 if (count == 20)
1441 if (pdb->nbFiles > 20)
1443 memcpy( pdb->fileHandles, files, 20 );
1444 GlobalFree16( pdb->hFileHandles );
1445 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1446 GlobalHandleToSel( hPDB ) );
1447 pdb->hFileHandles = 0;
1448 pdb->nbFiles = 20;
1451 else /* More than 20, need a new file handles table */
1453 BYTE *newfiles;
1454 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1455 if (!newhandle)
1457 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1458 return pdb->nbFiles;
1460 newfiles = (BYTE *)GlobalLock16( newhandle );
1462 if (count > pdb->nbFiles)
1464 memcpy( newfiles, files, pdb->nbFiles );
1465 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1467 else memcpy( newfiles, files, count );
1468 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1469 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1470 pdb->hFileHandles = newhandle;
1471 pdb->nbFiles = count;
1473 return pdb->nbFiles;
1477 /*************************************************************************
1478 * SetHandleCount32 (KERNEL32.494)
1480 UINT32 WINAPI SetHandleCount32( UINT32 count )
1482 return MIN( 256, count );
1486 /***********************************************************************
1487 * FlushFileBuffers (KERNEL32.133)
1489 BOOL32 WINAPI FlushFileBuffers( HFILE32 hFile )
1491 struct flush_file_request req;
1493 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1494 K32OBJ_FILE, 0 )) == -1)
1495 return FALSE;
1496 CLIENT_SendRequest( REQ_FLUSH_FILE, -1, 1, &req, sizeof(req) );
1497 return !CLIENT_WaitReply( NULL, NULL, 0 );
1501 /**************************************************************************
1502 * SetEndOfFile (KERNEL32.483)
1504 BOOL32 WINAPI SetEndOfFile( HFILE32 hFile )
1506 struct truncate_file_request req;
1508 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1509 K32OBJ_FILE, 0 )) == -1)
1510 return FALSE;
1511 CLIENT_SendRequest( REQ_TRUNCATE_FILE, -1, 1, &req, sizeof(req) );
1512 return !CLIENT_WaitReply( NULL, NULL, 0 );
1516 /***********************************************************************
1517 * DeleteFile16 (KERNEL.146)
1519 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1521 return DeleteFile32A( path );
1525 /***********************************************************************
1526 * DeleteFile32A (KERNEL32.71)
1528 BOOL32 WINAPI DeleteFile32A( LPCSTR path )
1530 DOS_FULL_NAME full_name;
1532 TRACE(file, "'%s'\n", path );
1534 if (!*path)
1536 ERR(file, "Empty path passed\n");
1537 return FALSE;
1539 if (DOSFS_GetDevice( path ))
1541 WARN(file, "cannot remove DOS device '%s'!\n", path);
1542 SetLastError( ERROR_FILE_NOT_FOUND );
1543 return FALSE;
1546 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1547 if (unlink( full_name.long_name ) == -1)
1549 FILE_SetDosError();
1550 return FALSE;
1552 return TRUE;
1556 /***********************************************************************
1557 * DeleteFile32W (KERNEL32.72)
1559 BOOL32 WINAPI DeleteFile32W( LPCWSTR path )
1561 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1562 BOOL32 ret = DeleteFile32A( xpath );
1563 HeapFree( GetProcessHeap(), 0, xpath );
1564 return ret;
1568 /***********************************************************************
1569 * FILE_dommap
1571 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1572 DWORD size_high, DWORD size_low,
1573 DWORD offset_high, DWORD offset_low,
1574 int prot, int flags )
1576 int fd = -1;
1577 int pos;
1578 LPVOID ret;
1580 if (size_high || offset_high)
1581 FIXME(file, "offsets larger than 4Gb not supported\n");
1583 if (unix_handle == -1)
1585 #ifdef MAP_ANON
1586 flags |= MAP_ANON;
1587 #else
1588 static int fdzero = -1;
1590 if (fdzero == -1)
1592 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1594 perror( "/dev/zero: open" );
1595 exit(1);
1598 fd = fdzero;
1599 #endif /* MAP_ANON */
1600 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1601 #ifdef MAP_SHARED
1602 flags &= ~MAP_SHARED;
1603 #endif
1604 #ifdef MAP_PRIVATE
1605 flags |= MAP_PRIVATE;
1606 #endif
1608 else fd = unix_handle;
1610 if ((ret = mmap( start, size_low, prot,
1611 flags, fd, offset_low )) != (LPVOID)-1)
1612 return ret;
1614 /* mmap() failed; if this is because the file offset is not */
1615 /* page-aligned (EINVAL), or because the underlying filesystem */
1616 /* does not support mmap() (ENOEXEC), we do it by hand. */
1618 if (unix_handle == -1) return ret;
1619 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1620 if (prot & PROT_WRITE)
1622 /* We cannot fake shared write mappings */
1623 #ifdef MAP_SHARED
1624 if (flags & MAP_SHARED) return ret;
1625 #endif
1626 #ifdef MAP_PRIVATE
1627 if (!(flags & MAP_PRIVATE)) return ret;
1628 #endif
1630 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1631 /* Reserve the memory with an anonymous mmap */
1632 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1633 PROT_READ | PROT_WRITE, flags );
1634 if (ret == (LPVOID)-1) return ret;
1635 /* Now read in the file */
1636 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1638 FILE_munmap( ret, size_high, size_low );
1639 return (LPVOID)-1;
1641 read( fd, ret, size_low );
1642 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1643 mprotect( ret, size_low, prot ); /* Set the right protection */
1644 return ret;
1648 /***********************************************************************
1649 * FILE_munmap
1651 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1653 if (size_high)
1654 FIXME(file, "offsets larger than 4Gb not supported\n");
1655 return munmap( start, size_low );
1659 /***********************************************************************
1660 * GetFileType (KERNEL32.222)
1662 DWORD WINAPI GetFileType( HFILE32 hFile )
1664 struct get_file_info_request req;
1665 struct get_file_info_reply reply;
1667 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1668 K32OBJ_UNKNOWN, 0 )) == -1)
1669 return FILE_TYPE_UNKNOWN;
1670 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
1671 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
1672 return FILE_TYPE_UNKNOWN;
1673 return reply.type;
1677 /**************************************************************************
1678 * MoveFileEx32A (KERNEL32.???)
1680 BOOL32 WINAPI MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1682 DOS_FULL_NAME full_name1, full_name2;
1683 int mode=0; /* mode == 1: use copy */
1685 TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag);
1687 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1688 if (fn2) { /* !fn2 means delete fn1 */
1689 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1690 /* Source name and target path are valid */
1691 if ( full_name1.drive != full_name2.drive)
1693 /* use copy, if allowed */
1694 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1695 /* FIXME: Use right error code */
1696 SetLastError( ERROR_FILE_EXISTS );
1697 return FALSE;
1699 else mode =1;
1701 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1702 /* target exists, check if we may overwrite */
1703 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1704 /* FIXME: Use right error code */
1705 SetLastError( ERROR_ACCESS_DENIED );
1706 return FALSE;
1709 else /* fn2 == NULL means delete source */
1710 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1711 if (flag & MOVEFILE_COPY_ALLOWED) {
1712 WARN(file, "Illegal flag\n");
1713 SetLastError( ERROR_GEN_FAILURE );
1714 return FALSE;
1716 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1717 Perhaps we should queue these command and execute it
1718 when exiting... What about using on_exit(2)
1720 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1721 full_name1.long_name);
1722 return TRUE;
1724 else if (unlink( full_name1.long_name ) == -1)
1726 FILE_SetDosError();
1727 return FALSE;
1729 else return TRUE; /* successfully deleted */
1731 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1732 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1733 Perhaps we should queue these command and execute it
1734 when exiting... What about using on_exit(2)
1736 FIXME(file,"Please move existing file '%s' to file '%s'"
1737 "when Wine has finished\n",
1738 full_name1.long_name, full_name2.long_name);
1739 return TRUE;
1742 if (!mode) /* move the file */
1743 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1745 FILE_SetDosError();
1746 return FALSE;
1748 else return TRUE;
1749 else /* copy File */
1750 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1754 /**************************************************************************
1755 * MoveFileEx32W (KERNEL32.???)
1757 BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1759 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1760 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1761 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1762 HeapFree( GetProcessHeap(), 0, afn1 );
1763 HeapFree( GetProcessHeap(), 0, afn2 );
1764 return res;
1768 /**************************************************************************
1769 * MoveFile32A (KERNEL32.387)
1771 * Move file or directory
1773 BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1775 DOS_FULL_NAME full_name1, full_name2;
1776 struct stat fstat;
1778 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1780 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1781 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1782 /* The new name must not already exist */
1783 return FALSE;
1784 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1786 if (full_name1.drive == full_name2.drive) /* move */
1787 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1789 FILE_SetDosError();
1790 return FALSE;
1792 else return TRUE;
1793 else /*copy */ {
1794 if (stat( full_name1.long_name, &fstat ))
1796 WARN(file, "Invalid source file %s\n",
1797 full_name1.long_name);
1798 FILE_SetDosError();
1799 return FALSE;
1801 if (S_ISDIR(fstat.st_mode)) {
1802 /* No Move for directories across file systems */
1803 /* FIXME: Use right error code */
1804 SetLastError( ERROR_GEN_FAILURE );
1805 return FALSE;
1807 else
1808 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1813 /**************************************************************************
1814 * MoveFile32W (KERNEL32.390)
1816 BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1818 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1819 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1820 BOOL32 res = MoveFile32A( afn1, afn2 );
1821 HeapFree( GetProcessHeap(), 0, afn1 );
1822 HeapFree( GetProcessHeap(), 0, afn2 );
1823 return res;
1827 /**************************************************************************
1828 * CopyFile32A (KERNEL32.36)
1830 BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1832 HFILE32 h1, h2;
1833 BY_HANDLE_FILE_INFORMATION info;
1834 UINT32 count;
1835 BOOL32 ret = FALSE;
1836 int mode;
1837 char buffer[2048];
1839 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1840 if (!GetFileInformationByHandle( h1, &info ))
1842 CloseHandle( h1 );
1843 return FALSE;
1845 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1846 if ((h2 = CreateFile32A( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1847 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1848 info.dwFileAttributes, h1 )) == HFILE_ERROR32)
1850 CloseHandle( h1 );
1851 return FALSE;
1853 while ((count = _lread32( h1, buffer, sizeof(buffer) )) > 0)
1855 char *p = buffer;
1856 while (count > 0)
1858 INT32 res = _lwrite32( h2, p, count );
1859 if (res <= 0) goto done;
1860 p += res;
1861 count -= res;
1864 ret = TRUE;
1865 done:
1866 CloseHandle( h1 );
1867 CloseHandle( h2 );
1868 return ret;
1872 /**************************************************************************
1873 * CopyFile32W (KERNEL32.37)
1875 BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
1877 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1878 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1879 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1880 HeapFree( GetProcessHeap(), 0, sourceA );
1881 HeapFree( GetProcessHeap(), 0, destA );
1882 return ret;
1886 /**************************************************************************
1887 * CopyFileEx32A (KERNEL32.858)
1889 * This implementation ignores most of the extra parameters passed-in into
1890 * the "ex" version of the method and calls the CopyFile method.
1891 * It will have to be fixed eventually.
1893 BOOL32 WINAPI CopyFileEx32A(LPCSTR sourceFilename,
1894 LPCSTR destFilename,
1895 LPPROGRESS_ROUTINE progressRoutine,
1896 LPVOID appData,
1897 LPBOOL32 cancelFlagPointer,
1898 DWORD copyFlags)
1900 BOOL32 failIfExists = FALSE;
1903 * Interpret the only flag that CopyFile can interpret.
1905 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1907 failIfExists = TRUE;
1910 return CopyFile32A(sourceFilename, destFilename, failIfExists);
1913 /**************************************************************************
1914 * CopyFileEx32W (KERNEL32.859)
1916 BOOL32 WINAPI CopyFileEx32W(LPCWSTR sourceFilename,
1917 LPCWSTR destFilename,
1918 LPPROGRESS_ROUTINE progressRoutine,
1919 LPVOID appData,
1920 LPBOOL32 cancelFlagPointer,
1921 DWORD copyFlags)
1923 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1924 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1926 BOOL32 ret = CopyFileEx32A(sourceA,
1927 destA,
1928 progressRoutine,
1929 appData,
1930 cancelFlagPointer,
1931 copyFlags);
1933 HeapFree( GetProcessHeap(), 0, sourceA );
1934 HeapFree( GetProcessHeap(), 0, destA );
1936 return ret;
1940 /***********************************************************************
1941 * SetFileTime (KERNEL32.650)
1943 BOOL32 WINAPI SetFileTime( HFILE32 hFile,
1944 const FILETIME *lpCreationTime,
1945 const FILETIME *lpLastAccessTime,
1946 const FILETIME *lpLastWriteTime )
1948 struct set_file_time_request req;
1950 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1951 K32OBJ_FILE, GENERIC_WRITE )) == -1)
1952 return FALSE;
1953 if (lpLastAccessTime)
1954 req.access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1955 else
1956 req.access_time = 0; /* FIXME */
1957 if (lpLastWriteTime)
1958 req.write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1959 else
1960 req.write_time = 0; /* FIXME */
1962 CLIENT_SendRequest( REQ_SET_FILE_TIME, -1, 1, &req, sizeof(req) );
1963 return !CLIENT_WaitReply( NULL, NULL, 0 );
1967 /**************************************************************************
1968 * LockFile (KERNEL32.511)
1970 BOOL32 WINAPI LockFile( HFILE32 hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1971 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1973 struct lock_file_request req;
1975 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1976 K32OBJ_FILE, 0 )) == -1)
1977 return FALSE;
1978 req.offset_low = dwFileOffsetLow;
1979 req.offset_high = dwFileOffsetHigh;
1980 req.count_low = nNumberOfBytesToLockLow;
1981 req.count_high = nNumberOfBytesToLockHigh;
1982 CLIENT_SendRequest( REQ_LOCK_FILE, -1, 1, &req, sizeof(req) );
1983 return !CLIENT_WaitReply( NULL, NULL, 0 );
1987 /**************************************************************************
1988 * UnlockFile (KERNEL32.703)
1990 BOOL32 WINAPI UnlockFile( HFILE32 hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1991 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1993 struct unlock_file_request req;
1995 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1996 K32OBJ_FILE, 0 )) == -1)
1997 return FALSE;
1998 req.offset_low = dwFileOffsetLow;
1999 req.offset_high = dwFileOffsetHigh;
2000 req.count_low = nNumberOfBytesToUnlockLow;
2001 req.count_high = nNumberOfBytesToUnlockHigh;
2002 CLIENT_SendRequest( REQ_UNLOCK_FILE, -1, 1, &req, sizeof(req) );
2003 return !CLIENT_WaitReply( NULL, NULL, 0 );
2007 #if 0
2009 struct DOS_FILE_LOCK {
2010 struct DOS_FILE_LOCK * next;
2011 DWORD base;
2012 DWORD len;
2013 DWORD processId;
2014 FILE_OBJECT * dos_file;
2015 /* char * unix_name;*/
2018 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2020 static DOS_FILE_LOCK *locks = NULL;
2021 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2024 /* Locks need to be mirrored because unix file locking is based
2025 * on the pid. Inside of wine there can be multiple WINE processes
2026 * that share the same unix pid.
2027 * Read's and writes should check these locks also - not sure
2028 * how critical that is at this point (FIXME).
2031 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2033 DOS_FILE_LOCK *curr;
2034 DWORD processId;
2036 processId = GetCurrentProcessId();
2038 /* check if lock overlaps a current lock for the same file */
2039 #if 0
2040 for (curr = locks; curr; curr = curr->next) {
2041 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2042 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2043 return TRUE;/* region is identic */
2044 if ((f->l_start < (curr->base + curr->len)) &&
2045 ((f->l_start + f->l_len) > curr->base)) {
2046 /* region overlaps */
2047 return FALSE;
2051 #endif
2053 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2054 curr->processId = GetCurrentProcessId();
2055 curr->base = f->l_start;
2056 curr->len = f->l_len;
2057 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
2058 curr->next = locks;
2059 curr->dos_file = file;
2060 locks = curr;
2061 return TRUE;
2064 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2066 DWORD processId;
2067 DOS_FILE_LOCK **curr;
2068 DOS_FILE_LOCK *rem;
2070 processId = GetCurrentProcessId();
2071 curr = &locks;
2072 while (*curr) {
2073 if ((*curr)->dos_file == file) {
2074 rem = *curr;
2075 *curr = (*curr)->next;
2076 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2077 HeapFree( SystemHeap, 0, rem );
2079 else
2080 curr = &(*curr)->next;
2084 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2086 DWORD processId;
2087 DOS_FILE_LOCK **curr;
2088 DOS_FILE_LOCK *rem;
2090 processId = GetCurrentProcessId();
2091 for (curr = &locks; *curr; curr = &(*curr)->next) {
2092 if ((*curr)->processId == processId &&
2093 (*curr)->dos_file == file &&
2094 (*curr)->base == f->l_start &&
2095 (*curr)->len == f->l_len) {
2096 /* this is the same lock */
2097 rem = *curr;
2098 *curr = (*curr)->next;
2099 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2100 HeapFree( SystemHeap, 0, rem );
2101 return TRUE;
2104 /* no matching lock found */
2105 return FALSE;
2109 /**************************************************************************
2110 * LockFile (KERNEL32.511)
2112 BOOL32 WINAPI LockFile(
2113 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2114 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2116 struct flock f;
2117 FILE_OBJECT *file;
2119 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2120 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2121 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2123 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2124 FIXME(file, "Unimplemented bytes > 32bits\n");
2125 return FALSE;
2128 f.l_start = dwFileOffsetLow;
2129 f.l_len = nNumberOfBytesToLockLow;
2130 f.l_whence = SEEK_SET;
2131 f.l_pid = 0;
2132 f.l_type = F_WRLCK;
2134 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2136 /* shadow locks internally */
2137 if (!DOS_AddLock(file, &f)) {
2138 SetLastError( ERROR_LOCK_VIOLATION );
2139 return FALSE;
2142 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2143 #ifdef USE_UNIX_LOCKS
2144 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2145 if (errno == EACCES || errno == EAGAIN) {
2146 SetLastError( ERROR_LOCK_VIOLATION );
2148 else {
2149 FILE_SetDosError();
2151 /* remove our internal copy of the lock */
2152 DOS_RemoveLock(file, &f);
2153 return FALSE;
2155 #endif
2156 return TRUE;
2160 /**************************************************************************
2161 * UnlockFile (KERNEL32.703)
2163 BOOL32 WINAPI UnlockFile(
2164 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2165 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2167 FILE_OBJECT *file;
2168 struct flock f;
2170 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2171 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2172 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2174 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2175 WARN(file, "Unimplemented bytes > 32bits\n");
2176 return FALSE;
2179 f.l_start = dwFileOffsetLow;
2180 f.l_len = nNumberOfBytesToUnlockLow;
2181 f.l_whence = SEEK_SET;
2182 f.l_pid = 0;
2183 f.l_type = F_UNLCK;
2185 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2187 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2189 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2190 #ifdef USE_UNIX_LOCKS
2191 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2192 FILE_SetDosError();
2193 return FALSE;
2195 #endif
2196 return TRUE;
2198 #endif
2200 /**************************************************************************
2201 * GetFileAttributesEx32A [KERNEL32.874]
2203 BOOL32 WINAPI GetFileAttributesEx32A(
2204 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2205 LPVOID lpFileInformation)
2207 DOS_FULL_NAME full_name;
2208 BY_HANDLE_FILE_INFORMATION info;
2210 if (lpFileName == NULL) return FALSE;
2211 if (lpFileInformation == NULL) return FALSE;
2213 if (fInfoLevelId == GetFileExInfoStandard) {
2214 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2215 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2216 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2217 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2219 lpFad->dwFileAttributes = info.dwFileAttributes;
2220 lpFad->ftCreationTime = info.ftCreationTime;
2221 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2222 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2223 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2224 lpFad->nFileSizeLow = info.nFileSizeLow;
2226 else {
2227 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
2228 return FALSE;
2231 return TRUE;
2235 /**************************************************************************
2236 * GetFileAttributesEx32W [KERNEL32.875]
2238 BOOL32 WINAPI GetFileAttributesEx32W(
2239 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2240 LPVOID lpFileInformation)
2242 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2243 BOOL32 res =
2244 GetFileAttributesEx32A( nameA, fInfoLevelId, lpFileInformation);
2245 HeapFree( GetProcessHeap(), 0, nameA );
2246 return res;