DOS programs use handles 0-4 without opening/closing any of those
[wine/testsucceed.git] / files / file.c
blob3d5b46969ce7a1321d4d50c710229d1e6b9fb43e
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 "debug.h"
42 #include "server/request.h"
43 #include "server.h"
45 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
46 #define MAP_ANON MAP_ANONYMOUS
47 #endif
49 /* The file object */
50 typedef struct
52 K32OBJ header;
53 } FILE_OBJECT;
56 /* Size of per-process table of DOS handles */
57 #define DOS_TABLE_SIZE 256
60 /***********************************************************************
61 * FILE_ConvertOFMode
63 * Convert OF_* mode into flags for CreateFile.
65 static void FILE_ConvertOFMode( INT32 mode, DWORD *access, DWORD *sharing )
67 switch(mode & 0x03)
69 case OF_READ: *access = GENERIC_READ; break;
70 case OF_WRITE: *access = GENERIC_WRITE; break;
71 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
72 default: *access = 0; break;
74 switch(mode & 0x70)
76 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
77 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
78 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
79 case OF_SHARE_DENY_NONE:
80 case OF_SHARE_COMPAT:
81 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
86 #if 0
87 /***********************************************************************
88 * FILE_ShareDeny
90 * PARAMS
91 * oldmode[I] mode how file was first opened
92 * mode[I] mode how the file should get opened
93 * RETURNS
94 * TRUE: deny open
95 * FALSE: allow open
97 * Look what we have to do with the given SHARE modes
99 * Ralph Brown's interrupt list gives following explication, I guess
100 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
102 * FIXME: Validate this function
103 ========from Ralph Brown's list =========
104 (Table 0750)
105 Values of DOS file sharing behavior:
106 | Second and subsequent Opens
107 First |Compat Deny Deny Deny Deny
108 Open | All Write Read None
109 |R W RW R W RW R W RW R W RW R W RW
110 - - - - -| - - - - - - - - - - - - - - - - -
111 Compat R |Y Y Y N N N 1 N N N N N 1 N N
112 W |Y Y Y N N N N N N N N N N N N
113 RW|Y Y Y N N N N N N N N N N N N
114 - - - - -|
115 Deny R |C C C N N N N N N N N N N N N
116 All W |C C C N N N N N N N N N N N N
117 RW|C C C N N N N N N N N N N N N
118 - - - - -|
119 Deny R |2 C C N N N Y N N N N N Y N N
120 Write W |C C C N N N N N N Y N N Y N N
121 RW|C C C N N N N N N N N N Y N N
122 - - - - -|
123 Deny R |C C C N N N N Y N N N N N Y N
124 Read W |C C C N N N N N N N Y N N Y N
125 RW|C C C N N N N N N N N N N Y N
126 - - - - -|
127 Deny R |2 C C N N N Y Y Y N N N Y Y Y
128 None W |C C C N N N N N N Y Y Y Y Y Y
129 RW|C C C N N N N N N N N N Y Y Y
130 Legend: Y = open succeeds, N = open fails with error code 05h
131 C = open fails, INT 24 generated
132 1 = open succeeds if file read-only, else fails with error code
133 2 = open succeeds if file read-only, else fails with INT 24
134 ========end of description from Ralph Brown's List =====
135 For every "Y" in the table we return FALSE
136 For every "N" we set the DOS_ERROR and return TRUE
137 For all other cases we barf,set the DOS_ERROR and return TRUE
140 static BOOL32 FILE_ShareDeny( int mode, int oldmode)
142 int oldsharemode = oldmode & 0x70;
143 int sharemode = mode & 0x70;
144 int oldopenmode = oldmode & 3;
145 int openmode = mode & 3;
147 switch (oldsharemode)
149 case OF_SHARE_COMPAT:
150 if (sharemode == OF_SHARE_COMPAT) return FALSE;
151 if (openmode == OF_READ) goto test_ro_err05 ;
152 goto fail_error05;
153 case OF_SHARE_EXCLUSIVE:
154 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
155 goto fail_error05;
156 case OF_SHARE_DENY_WRITE:
157 if (openmode != OF_READ)
159 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
160 goto fail_error05;
162 switch (sharemode)
164 case OF_SHARE_COMPAT:
165 if (oldopenmode == OF_READ) goto test_ro_int24 ;
166 goto fail_int24;
167 case OF_SHARE_DENY_NONE :
168 return FALSE;
169 case OF_SHARE_DENY_WRITE :
170 if (oldopenmode == OF_READ) return FALSE;
171 case OF_SHARE_DENY_READ :
172 if (oldopenmode == OF_WRITE) return FALSE;
173 case OF_SHARE_EXCLUSIVE:
174 default:
175 goto fail_error05;
177 break;
178 case OF_SHARE_DENY_READ:
179 if (openmode != OF_WRITE)
181 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
182 goto fail_error05;
184 switch (sharemode)
186 case OF_SHARE_COMPAT:
187 goto fail_int24;
188 case OF_SHARE_DENY_NONE :
189 return FALSE;
190 case OF_SHARE_DENY_WRITE :
191 if (oldopenmode == OF_READ) return FALSE;
192 case OF_SHARE_DENY_READ :
193 if (oldopenmode == OF_WRITE) return FALSE;
194 case OF_SHARE_EXCLUSIVE:
195 default:
196 goto fail_error05;
198 break;
199 case OF_SHARE_DENY_NONE:
200 switch (sharemode)
202 case OF_SHARE_COMPAT:
203 goto fail_int24;
204 case OF_SHARE_DENY_NONE :
205 return FALSE;
206 case OF_SHARE_DENY_WRITE :
207 if (oldopenmode == OF_READ) return FALSE;
208 case OF_SHARE_DENY_READ :
209 if (oldopenmode == OF_WRITE) return FALSE;
210 case OF_SHARE_EXCLUSIVE:
211 default:
212 goto fail_error05;
214 default:
215 ERR(file,"unknown mode\n");
217 ERR(file,"shouldn't happen\n");
218 ERR(file,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
219 return TRUE;
221 test_ro_int24:
222 if (oldmode == OF_READ)
223 return FALSE;
224 /* Fall through */
225 fail_int24:
226 FIXME(file,"generate INT24 missing\n");
227 /* Is this the right error? */
228 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
229 return TRUE;
231 test_ro_err05:
232 if (oldmode == OF_READ)
233 return FALSE;
234 /* fall through */
235 fail_error05:
236 TRACE(file,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
237 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
238 return TRUE;
240 #endif
243 /***********************************************************************
244 * FILE_SetDosError
246 * Set the DOS error code from errno.
248 void FILE_SetDosError(void)
250 int save_errno = errno; /* errno gets overwritten by printf */
252 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
253 switch (save_errno)
255 case EAGAIN:
256 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
257 break;
258 case EBADF:
259 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
260 break;
261 case ENOSPC:
262 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
263 break;
264 case EACCES:
265 case EPERM:
266 case EROFS:
267 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
268 break;
269 case EBUSY:
270 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
271 break;
272 case ENOENT:
273 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
274 break;
275 case EISDIR:
276 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
277 break;
278 case ENFILE:
279 case EMFILE:
280 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
281 break;
282 case EEXIST:
283 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
284 break;
285 case EINVAL:
286 case ESPIPE:
287 DOS_ERROR( ER_SeekError, EC_NotFound, SA_Ignore, EL_Disk );
288 break;
289 case ENOTEMPTY:
290 DOS_ERROR( ERROR_DIR_NOT_EMPTY, EC_Exists, SA_Ignore, EL_Disk );
291 break;
292 default:
293 perror( "int21: unknown errno" );
294 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
295 break;
297 errno = save_errno;
301 /***********************************************************************
302 * FILE_DupUnixHandle
304 * Duplicate a Unix handle into a task handle.
306 HFILE32 FILE_DupUnixHandle( int fd, DWORD access )
308 FILE_OBJECT *file;
309 int unix_handle;
310 struct create_file_request req;
311 struct create_file_reply reply;
313 if ((unix_handle = dup(fd)) == -1)
315 FILE_SetDosError();
316 return INVALID_HANDLE_VALUE32;
318 req.access = access;
319 req.inherit = 1;
320 req.sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
321 req.create = 0;
322 req.attrs = 0;
324 CLIENT_SendRequest( REQ_CREATE_FILE, unix_handle, 1,
325 &req, sizeof(req) );
326 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
327 if (reply.handle == -1) return INVALID_HANDLE_VALUE32;
329 if (!(file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) )))
331 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
332 CLIENT_CloseHandle( reply.handle );
333 return (HFILE32)NULL;
335 file->header.type = K32OBJ_FILE;
336 file->header.refcount = 0;
337 return HANDLE_Alloc( PROCESS_Current(), &file->header, req.access,
338 req.inherit, reply.handle );
342 /***********************************************************************
343 * FILE_CreateFile
345 * Implementation of CreateFile. Takes a Unix path name.
347 HFILE32 FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
348 LPSECURITY_ATTRIBUTES sa, DWORD creation,
349 DWORD attributes, HANDLE32 template )
351 FILE_OBJECT *file;
352 struct create_file_request req;
353 struct create_file_reply reply;
355 req.access = access;
356 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
357 req.sharing = sharing;
358 req.create = creation;
359 req.attrs = attributes;
360 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
361 &req, sizeof(req),
362 filename, strlen(filename) + 1 );
363 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
365 /* If write access failed, retry without GENERIC_WRITE */
367 if ((reply.handle == -1) && !Options.failReadOnly &&
368 (access & GENERIC_WRITE))
370 DWORD lasterror = GetLastError();
371 if ((lasterror == ERROR_ACCESS_DENIED) ||
372 (lasterror == ERROR_WRITE_PROTECT))
374 req.access &= ~GENERIC_WRITE;
375 CLIENT_SendRequest( REQ_CREATE_FILE, -1, 2,
376 &req, sizeof(req),
377 filename, strlen(filename) + 1 );
378 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
381 if (reply.handle == -1) return INVALID_HANDLE_VALUE32;
383 /* Now build the FILE_OBJECT */
385 if (!(file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) )))
387 SetLastError( ERROR_OUTOFMEMORY );
388 CLIENT_CloseHandle( reply.handle );
389 return (HFILE32)INVALID_HANDLE_VALUE32;
391 file->header.type = K32OBJ_FILE;
392 file->header.refcount = 0;
393 return HANDLE_Alloc( PROCESS_Current(), &file->header, req.access,
394 req.inherit, reply.handle );
398 /***********************************************************************
399 * FILE_CreateDevice
401 * Same as FILE_CreateFile but for a device
403 HFILE32 FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
405 FILE_OBJECT *file;
406 struct create_device_request req;
407 struct create_device_reply reply;
409 req.access = access;
410 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
411 req.id = client_id;
412 CLIENT_SendRequest( REQ_CREATE_DEVICE, -1, 1, &req, sizeof(req) );
413 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
414 if (reply.handle == -1) return INVALID_HANDLE_VALUE32;
416 /* Now build the FILE_OBJECT */
418 if (!(file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) )))
420 SetLastError( ERROR_OUTOFMEMORY );
421 CLIENT_CloseHandle( reply.handle );
422 return (HFILE32)INVALID_HANDLE_VALUE32;
424 file->header.type = K32OBJ_FILE;
425 file->header.refcount = 0;
426 return HANDLE_Alloc( PROCESS_Current(), &file->header, req.access,
427 req.inherit, reply.handle );
431 /*************************************************************************
432 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
434 * Creates or opens an object, and returns a handle that can be used to
435 * access that object.
437 * PARAMS
439 * filename [I] pointer to filename to be accessed
440 * access [I] access mode requested
441 * sharing [I] share mode
442 * sa [I] pointer to security attributes
443 * creation [I] how to create the file
444 * attributes [I] attributes for newly created file
445 * template [I] handle to file with extended attributes to copy
447 * RETURNS
448 * Success: Open handle to specified file
449 * Failure: INVALID_HANDLE_VALUE
451 * NOTES
452 * Should call SetLastError() on failure.
454 * BUGS
456 * Doesn't support character devices, pipes, template files, or a
457 * lot of the 'attributes' flags yet.
459 HFILE32 WINAPI CreateFile32A( LPCSTR filename, DWORD access, DWORD sharing,
460 LPSECURITY_ATTRIBUTES sa, DWORD creation,
461 DWORD attributes, HANDLE32 template )
463 DOS_FULL_NAME full_name;
465 if (!filename)
467 SetLastError( ERROR_INVALID_PARAMETER );
468 return HFILE_ERROR32;
471 /* If the name starts with '\\?\', ignore the first 4 chars. */
472 if (!strncmp(filename, "\\\\?\\", 4))
474 filename += 4;
475 if (!strncmp(filename, "UNC\\", 4))
477 FIXME( file, "UNC name (%s) not supported.\n", filename );
478 SetLastError( ERROR_PATH_NOT_FOUND );
479 return HFILE_ERROR32;
483 if (!strncmp(filename, "\\\\.\\", 4))
484 return DEVICE_Open( filename+4, access, sa );
486 /* If the name still starts with '\\', it's a UNC name. */
487 if (!strncmp(filename, "\\\\", 2))
489 FIXME( file, "UNC name (%s) not supported.\n", filename );
490 SetLastError( ERROR_PATH_NOT_FOUND );
491 return HFILE_ERROR32;
494 /* Open a console for CONIN$ or CONOUT$ */
495 if (!lstrcmpi32A(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
496 if (!lstrcmpi32A(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
498 if (DOSFS_GetDevice( filename ))
500 HFILE32 ret;
502 TRACE(file, "opening device '%s'\n", filename );
504 if (HFILE_ERROR32!=(ret=DOSFS_OpenDevice( filename, access )))
505 return ret;
507 /* Do not silence this please. It is a critical error. -MM */
508 ERR(file, "Couldn't open device '%s'!\n",filename);
509 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
510 return HFILE_ERROR32;
513 /* check for filename, don't check for last entry if creating */
514 if (!DOSFS_GetFullName( filename,
515 (creation == OPEN_EXISTING) || (creation == TRUNCATE_EXISTING), &full_name ))
516 return HFILE_ERROR32;
518 return FILE_CreateFile( full_name.long_name, access, sharing,
519 sa, creation, attributes, template );
524 /*************************************************************************
525 * CreateFile32W (KERNEL32.48)
527 HFILE32 WINAPI CreateFile32W( LPCWSTR filename, DWORD access, DWORD sharing,
528 LPSECURITY_ATTRIBUTES sa, DWORD creation,
529 DWORD attributes, HANDLE32 template)
531 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
532 HFILE32 res = CreateFile32A( afn, access, sharing, sa, creation, attributes, template );
533 HeapFree( GetProcessHeap(), 0, afn );
534 return res;
538 /***********************************************************************
539 * FILE_FillInfo
541 * Fill a file information from a struct stat.
543 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
545 if (S_ISDIR(st->st_mode))
546 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
547 else
548 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
549 if (!(st->st_mode & S_IWUSR))
550 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
552 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
553 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
554 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
556 info->dwVolumeSerialNumber = 0; /* FIXME */
557 info->nFileSizeHigh = 0;
558 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
559 info->nNumberOfLinks = st->st_nlink;
560 info->nFileIndexHigh = 0;
561 info->nFileIndexLow = st->st_ino;
565 /***********************************************************************
566 * FILE_Stat
568 * Stat a Unix path name. Return TRUE if OK.
570 BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
572 struct stat st;
574 if (!unixName || !info) return FALSE;
576 if (stat( unixName, &st ) == -1)
578 FILE_SetDosError();
579 return FALSE;
581 FILE_FillInfo( &st, info );
582 return TRUE;
586 /***********************************************************************
587 * GetFileInformationByHandle (KERNEL32.219)
589 DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile,
590 BY_HANDLE_FILE_INFORMATION *info )
592 struct get_file_info_request req;
593 struct get_file_info_reply reply;
595 if (!info) return 0;
596 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
597 K32OBJ_FILE, 0 )) == -1)
598 return 0;
599 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
600 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
601 return 0;
602 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftCreationTime, 0 );
603 DOSFS_UnixTimeToFileTime( reply.write_time, &info->ftLastWriteTime, 0 );
604 DOSFS_UnixTimeToFileTime( reply.access_time, &info->ftLastAccessTime, 0 );
605 info->dwFileAttributes = reply.attr;
606 info->dwVolumeSerialNumber = reply.serial;
607 info->nFileSizeHigh = reply.size_high;
608 info->nFileSizeLow = reply.size_low;
609 info->nNumberOfLinks = reply.links;
610 info->nFileIndexHigh = reply.index_high;
611 info->nFileIndexLow = reply.index_low;
612 return 1;
616 /**************************************************************************
617 * GetFileAttributes16 (KERNEL.420)
619 DWORD WINAPI GetFileAttributes16( LPCSTR name )
621 return GetFileAttributes32A( name );
625 /**************************************************************************
626 * GetFileAttributes32A (KERNEL32.217)
628 DWORD WINAPI GetFileAttributes32A( LPCSTR name )
630 DOS_FULL_NAME full_name;
631 BY_HANDLE_FILE_INFORMATION info;
633 if (name == NULL || *name=='\0') return -1;
635 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
636 if (!FILE_Stat( full_name.long_name, &info )) return -1;
637 return info.dwFileAttributes;
641 /**************************************************************************
642 * GetFileAttributes32W (KERNEL32.218)
644 DWORD WINAPI GetFileAttributes32W( LPCWSTR name )
646 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
647 DWORD res = GetFileAttributes32A( nameA );
648 HeapFree( GetProcessHeap(), 0, nameA );
649 return res;
653 /***********************************************************************
654 * GetFileSize (KERNEL32.220)
656 DWORD WINAPI GetFileSize( HFILE32 hFile, LPDWORD filesizehigh )
658 BY_HANDLE_FILE_INFORMATION info;
659 if (!GetFileInformationByHandle( hFile, &info )) return 0;
660 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
661 return info.nFileSizeLow;
665 /***********************************************************************
666 * GetFileTime (KERNEL32.221)
668 BOOL32 WINAPI GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
669 FILETIME *lpLastAccessTime,
670 FILETIME *lpLastWriteTime )
672 BY_HANDLE_FILE_INFORMATION info;
673 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
674 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
675 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
676 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
677 return TRUE;
680 /***********************************************************************
681 * CompareFileTime (KERNEL32.28)
683 INT32 WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
685 if (!x || !y) return -1;
687 if (x->dwHighDateTime > y->dwHighDateTime)
688 return 1;
689 if (x->dwHighDateTime < y->dwHighDateTime)
690 return -1;
691 if (x->dwLowDateTime > y->dwLowDateTime)
692 return 1;
693 if (x->dwLowDateTime < y->dwLowDateTime)
694 return -1;
695 return 0;
699 /***********************************************************************
700 * GetTempFileName16 (KERNEL.97)
702 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
703 LPSTR buffer )
705 char temppath[144];
707 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
708 drive |= DRIVE_GetCurrentDrive() + 'A';
710 if ((drive & TF_FORCEDRIVE) &&
711 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
713 drive &= ~TF_FORCEDRIVE;
714 WARN(file, "invalid drive %d specified\n", drive );
717 if (drive & TF_FORCEDRIVE)
718 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
719 else
720 GetTempPath32A( 132, temppath );
721 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
725 /***********************************************************************
726 * GetTempFileName32A (KERNEL32.290)
728 UINT32 WINAPI GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
729 LPSTR buffer)
731 static UINT32 unique_temp;
732 DOS_FULL_NAME full_name;
733 int i;
734 LPSTR p;
735 UINT32 num;
737 if ( !path || !prefix || !buffer ) return 0;
739 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
740 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
742 strcpy( buffer, path );
743 p = buffer + strlen(buffer);
745 /* add a \, if there isn't one and path is more than just the drive letter ... */
746 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
747 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
749 *p++ = '~';
750 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
751 sprintf( p, "%04x.tmp", num );
753 /* Now try to create it */
755 if (!unique)
759 HFILE32 handle = CreateFile32A( buffer, GENERIC_WRITE, 0, NULL,
760 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
761 if (handle != INVALID_HANDLE_VALUE32)
762 { /* We created it */
763 TRACE(file, "created %s\n",
764 buffer);
765 CloseHandle( handle );
766 break;
768 if (DOS_ExtendedError != ER_FileExists)
769 break; /* No need to go on */
770 num++;
771 sprintf( p, "%04x.tmp", num );
772 } while (num != (unique & 0xffff));
775 /* Get the full path name */
777 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
779 /* Check if we have write access in the directory */
780 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
781 if (access( full_name.long_name, W_OK ) == -1)
782 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
783 buffer);
785 TRACE(file, "returning %s\n", buffer );
786 return unique ? unique : num;
790 /***********************************************************************
791 * GetTempFileName32W (KERNEL32.291)
793 UINT32 WINAPI GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
794 LPWSTR buffer )
796 LPSTR patha,prefixa;
797 char buffera[144];
798 UINT32 ret;
800 if (!path) return 0;
801 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
802 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
803 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
804 lstrcpyAtoW( buffer, buffera );
805 HeapFree( GetProcessHeap(), 0, patha );
806 HeapFree( GetProcessHeap(), 0, prefixa );
807 return ret;
811 /***********************************************************************
812 * FILE_DoOpenFile
814 * Implementation of OpenFile16() and OpenFile32().
816 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
817 BOOL32 win32 )
819 HFILE32 hFileRet;
820 FILETIME filetime;
821 WORD filedatetime[2];
822 DOS_FULL_NAME full_name;
823 DWORD access, sharing;
824 char *p;
826 if (!ofs) return HFILE_ERROR32;
828 ofs->cBytes = sizeof(OFSTRUCT);
829 ofs->nErrCode = 0;
830 if (mode & OF_REOPEN) name = ofs->szPathName;
832 if (!name) {
833 ERR(file, "called with `name' set to NULL ! Please debug.\n");
834 return HFILE_ERROR32;
837 TRACE(file, "%s %04x\n", name, mode );
839 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
840 Are there any cases where getting the path here is wrong?
841 Uwe Bonnes 1997 Apr 2 */
842 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
843 ofs->szPathName, NULL )) goto error;
844 FILE_ConvertOFMode( mode, &access, &sharing );
846 /* OF_PARSE simply fills the structure */
848 if (mode & OF_PARSE)
850 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
851 != DRIVE_REMOVABLE);
852 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
853 name, ofs->szPathName );
854 return 0;
857 /* OF_CREATE is completely different from all other options, so
858 handle it first */
860 if (mode & OF_CREATE)
862 if ((hFileRet = CreateFile32A( name, GENERIC_READ | GENERIC_WRITE,
863 sharing, NULL, CREATE_ALWAYS,
864 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE32)
865 goto error;
866 goto success;
869 /* If OF_SEARCH is set, ignore the given path */
871 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
873 /* First try the file name as is */
874 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
875 /* Now remove the path */
876 if (name[0] && (name[1] == ':')) name += 2;
877 if ((p = strrchr( name, '\\' ))) name = p + 1;
878 if ((p = strrchr( name, '/' ))) name = p + 1;
879 if (!name[0]) goto not_found;
882 /* Now look for the file */
884 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
886 found:
887 TRACE(file, "found %s = %s\n",
888 full_name.long_name, full_name.short_name );
889 lstrcpyn32A( ofs->szPathName, full_name.short_name,
890 sizeof(ofs->szPathName) );
892 if (mode & OF_SHARE_EXCLUSIVE)
893 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
894 on the file <tempdir>/_ins0432._mp to determine how
895 far installation has proceeded.
896 _ins0432._mp is an executable and while running the
897 application expects the open with OF_SHARE_ to fail*/
898 /* Probable FIXME:
899 As our loader closes the files after loading the executable,
900 we can't find the running executable with FILE_InUse.
901 Perhaps the loader should keep the file open.
902 Recheck against how Win handles that case */
904 char *last = strrchr(full_name.long_name,'/');
905 if (!last)
906 last = full_name.long_name - 1;
907 if (GetModuleHandle16(last+1))
909 TRACE(file,"Denying shared open for %s\n",full_name.long_name);
910 return HFILE_ERROR32;
914 if (mode & OF_DELETE)
916 if (unlink( full_name.long_name ) == -1) goto not_found;
917 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
918 return 1;
921 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
922 NULL, OPEN_EXISTING, 0, -1 );
923 if (hFileRet == HFILE_ERROR32) goto not_found;
925 GetFileTime( hFileRet, NULL, NULL, &filetime );
926 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
927 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
929 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
931 CloseHandle( hFileRet );
932 WARN(file, "(%s): OF_VERIFY failed\n", name );
933 /* FIXME: what error here? */
934 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
935 goto error;
938 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
940 success: /* We get here if the open was successful */
941 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
942 if (mode & OF_EXIST) /* Return the handle, but close it first */
943 CloseHandle( hFileRet );
944 return hFileRet;
946 not_found: /* We get here if the file does not exist */
947 WARN(file, "'%s' not found\n", name );
948 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
949 /* fall through */
951 error: /* We get here if there was an error opening the file */
952 ofs->nErrCode = DOS_ExtendedError;
953 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
954 name,ofs->nErrCode );
955 return HFILE_ERROR32;
959 /***********************************************************************
960 * OpenFile16 (KERNEL.74)
962 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
964 TRACE(file,"OpenFile16(%s,%i)\n", name, mode);
965 return FILE_AllocDosHandle( FILE_DoOpenFile( name, ofs, mode, FALSE ) );
969 /***********************************************************************
970 * OpenFile32 (KERNEL32.396)
972 HFILE32 WINAPI OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
974 return FILE_DoOpenFile( name, ofs, mode, TRUE );
978 /***********************************************************************
979 * FILE_InitProcessDosHandles
981 * Allocates the default DOS handles for a process. Called either by
982 * AllocDosHandle below or by the DOSVM stuff.
984 BOOL32 FILE_InitProcessDosHandles( void ) {
985 HANDLE32 *ptr;
987 if (!(ptr = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY,
988 sizeof(*ptr) * DOS_TABLE_SIZE )))
989 return FALSE;
990 PROCESS_Current()->dos_handles = ptr;
991 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
992 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
993 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
994 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
995 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
996 return TRUE;
999 /***********************************************************************
1000 * FILE_AllocDosHandle
1002 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1003 * longer valid after this function (even on failure).
1005 HFILE16 FILE_AllocDosHandle( HANDLE32 handle )
1007 int i;
1008 HANDLE32 *ptr = PROCESS_Current()->dos_handles;
1010 if (!handle || (handle == INVALID_HANDLE_VALUE32))
1011 return INVALID_HANDLE_VALUE16;
1013 if (!ptr) {
1014 if (!FILE_InitProcessDosHandles())
1015 goto error;
1016 ptr = PROCESS_Current()->dos_handles;
1019 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
1020 if (!*ptr)
1022 *ptr = handle;
1023 TRACE( file, "Got %d for h32 %d\n", i, handle );
1024 return i;
1026 error:
1027 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
1028 CloseHandle( handle );
1029 return INVALID_HANDLE_VALUE16;
1033 /***********************************************************************
1034 * FILE_GetHandle32
1036 * Return the Win32 handle for a DOS handle.
1038 HANDLE32 FILE_GetHandle32( HFILE16 hfile )
1040 HANDLE32 *table = PROCESS_Current()->dos_handles;
1041 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1043 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
1044 return INVALID_HANDLE_VALUE32;
1046 return table[hfile];
1050 /***********************************************************************
1051 * FILE_Dup2
1053 * dup2() function for DOS handles.
1055 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1057 HANDLE32 *table = PROCESS_Current()->dos_handles;
1058 HANDLE32 new_handle;
1060 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1061 !table || !table[hFile1])
1063 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
1064 return HFILE_ERROR16;
1066 if (hFile2 < 5)
1068 FIXME( file, "stdio handle closed, need proper conversion\n" );
1069 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
1070 return HFILE_ERROR16;
1072 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1073 GetCurrentProcess(), &new_handle,
1074 0, FALSE, DUPLICATE_SAME_ACCESS ))
1075 return HFILE_ERROR16;
1076 if (table[hFile2]) CloseHandle( table[hFile2] );
1077 table[hFile2] = new_handle;
1078 return hFile2;
1082 /***********************************************************************
1083 * _lclose16 (KERNEL.81)
1085 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1087 HANDLE32 *table = PROCESS_Current()->dos_handles;
1089 if (hFile < 5)
1091 FIXME( file, "stdio handle closed, need proper conversion\n" );
1092 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
1093 return HFILE_ERROR16;
1095 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1097 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
1098 return HFILE_ERROR16;
1100 TRACE( file, "%d (handle32=%d)\n", hFile, table[hFile] );
1101 CloseHandle( table[hFile] );
1102 table[hFile] = 0;
1103 return 0;
1107 /***********************************************************************
1108 * _lclose32 (KERNEL32.592)
1110 HFILE32 WINAPI _lclose32( HFILE32 hFile )
1112 TRACE(file, "handle %d\n", hFile );
1113 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
1117 /***********************************************************************
1118 * ReadFile (KERNEL32.428)
1120 BOOL32 WINAPI ReadFile( HANDLE32 hFile, LPVOID buffer, DWORD bytesToRead,
1121 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1123 struct get_read_fd_request req;
1124 int unix_handle, result;
1126 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToRead );
1128 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1129 if (!bytesToRead) return TRUE;
1131 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1132 K32OBJ_UNKNOWN, GENERIC_READ )) == -1)
1133 return FALSE;
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 FILE_SetDosError();
1141 break;
1143 close( unix_handle );
1144 if (result == -1) return FALSE;
1145 if (bytesRead) *bytesRead = result;
1146 return TRUE;
1150 /***********************************************************************
1151 * WriteFile (KERNEL32.578)
1153 BOOL32 WINAPI WriteFile( HANDLE32 hFile, LPCVOID buffer, DWORD bytesToWrite,
1154 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1156 struct get_write_fd_request req;
1157 int unix_handle, result;
1159 TRACE(file, "%d %p %ld\n", hFile, buffer, bytesToWrite );
1161 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1162 if (!bytesToWrite) return TRUE;
1164 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1165 K32OBJ_UNKNOWN, GENERIC_READ )) == -1)
1166 return FALSE;
1167 CLIENT_SendRequest( REQ_GET_WRITE_FD, -1, 1, &req, sizeof(req) );
1168 CLIENT_WaitReply( NULL, &unix_handle, 0 );
1169 if (unix_handle == -1) return FALSE;
1170 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1172 if ((errno == EAGAIN) || (errno == EINTR)) 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 = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1195 if (count > maxlen) count = maxlen;
1196 return _lread32(FILE_GetHandle32(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 UINT32 WINAPI _lread32( HFILE32 handle, LPVOID buffer, UINT32 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)_lread32(FILE_GetHandle32(hFile), buffer, (LONG)count );
1229 /***********************************************************************
1230 * _lcreat16 (KERNEL.83)
1232 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1234 TRACE(file, "%s %02x\n", path, attr );
1235 return FILE_AllocDosHandle( _lcreat32( path, attr ) );
1239 /***********************************************************************
1240 * _lcreat32 (KERNEL32.593)
1242 HFILE32 WINAPI _lcreat32( LPCSTR path, INT32 attr )
1244 TRACE(file, "%s %02x\n", path, attr );
1245 return CreateFile32A( path, GENERIC_READ | GENERIC_WRITE,
1246 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1247 CREATE_ALWAYS, attr, -1 );
1251 /***********************************************************************
1252 * _lcreat16_uniq (Not a Windows API)
1254 HFILE16 _lcreat16_uniq( LPCSTR path, INT32 attr )
1256 TRACE(file, "%s %02x\n", path, attr );
1257 return FILE_AllocDosHandle( CreateFile32A( path, GENERIC_READ | GENERIC_WRITE,
1258 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1259 CREATE_NEW, attr, -1 ));
1263 /***********************************************************************
1264 * SetFilePointer (KERNEL32.492)
1266 DWORD WINAPI SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
1267 DWORD method )
1269 struct set_file_pointer_request req;
1270 struct set_file_pointer_reply reply;
1272 if (highword && *highword)
1274 FIXME(file, "64-bit offsets not supported yet\n");
1275 SetLastError( ERROR_INVALID_PARAMETER );
1276 return 0xffffffff;
1278 TRACE(file, "handle %d offset %ld origin %ld\n",
1279 hFile, distance, method );
1281 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1282 K32OBJ_FILE, 0 )) == -1)
1283 return 0xffffffff;
1284 req.low = distance;
1285 req.high = highword ? *highword : 0;
1286 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1287 req.whence = method;
1288 CLIENT_SendRequest( REQ_SET_FILE_POINTER, -1, 1, &req, sizeof(req) );
1289 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
1290 SetLastError( 0 );
1291 if (highword) *highword = reply.high;
1292 return reply.low;
1296 /***********************************************************************
1297 * _llseek16 (KERNEL.84)
1299 * FIXME:
1300 * Seeking before the start of the file should be allowed for _llseek16,
1301 * but cause subsequent I/O operations to fail (cf. interrupt list)
1304 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1306 return SetFilePointer( FILE_GetHandle32(hFile), lOffset, NULL, nOrigin );
1310 /***********************************************************************
1311 * _llseek32 (KERNEL32.594)
1313 LONG WINAPI _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
1315 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1319 /***********************************************************************
1320 * _lopen16 (KERNEL.85)
1322 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1324 return FILE_AllocDosHandle( _lopen32( path, mode ) );
1328 /***********************************************************************
1329 * _lopen32 (KERNEL32.595)
1331 HFILE32 WINAPI _lopen32( LPCSTR path, INT32 mode )
1333 DWORD access, sharing;
1335 TRACE(file, "('%s',%04x)\n", path, mode );
1336 FILE_ConvertOFMode( mode, &access, &sharing );
1337 return CreateFile32A( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1341 /***********************************************************************
1342 * _lwrite16 (KERNEL.86)
1344 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1346 return (UINT16)_hwrite32( FILE_GetHandle32(hFile), buffer, (LONG)count );
1349 /***********************************************************************
1350 * _lwrite32 (KERNEL32.761)
1352 UINT32 WINAPI _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
1354 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
1358 /***********************************************************************
1359 * _hread16 (KERNEL.349)
1361 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1363 return _lread32( FILE_GetHandle32(hFile), buffer, count );
1367 /***********************************************************************
1368 * _hread32 (KERNEL32.590)
1370 LONG WINAPI _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
1372 return _lread32( hFile, buffer, count );
1376 /***********************************************************************
1377 * _hwrite16 (KERNEL.350)
1379 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1381 return _hwrite32( FILE_GetHandle32(hFile), buffer, count );
1385 /***********************************************************************
1386 * _hwrite32 (KERNEL32.591)
1388 * experimentation yields that _lwrite:
1389 * o truncates the file at the current position with
1390 * a 0 len write
1391 * o returns 0 on a 0 length write
1392 * o works with console handles
1395 LONG WINAPI _hwrite32( HFILE32 handle, LPCSTR buffer, LONG count )
1397 DWORD result;
1399 TRACE(file, "%d %p %ld\n", handle, buffer, count );
1401 if (!count)
1403 /* Expand or truncate at current position */
1404 if (!SetEndOfFile( handle )) return HFILE_ERROR32;
1405 return 0;
1407 if (!WriteFile( handle, buffer, count, &result, NULL ))
1408 return HFILE_ERROR32;
1409 return result;
1413 /***********************************************************************
1414 * SetHandleCount16 (KERNEL.199)
1416 UINT16 WINAPI SetHandleCount16( UINT16 count )
1418 HGLOBAL16 hPDB = GetCurrentPDB();
1419 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1420 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1422 TRACE(file, "(%d)\n", count );
1424 if (count < 20) count = 20; /* No point in going below 20 */
1425 else if (count > 254) count = 254;
1427 if (count == 20)
1429 if (pdb->nbFiles > 20)
1431 memcpy( pdb->fileHandles, files, 20 );
1432 GlobalFree16( pdb->hFileHandles );
1433 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1434 GlobalHandleToSel( hPDB ) );
1435 pdb->hFileHandles = 0;
1436 pdb->nbFiles = 20;
1439 else /* More than 20, need a new file handles table */
1441 BYTE *newfiles;
1442 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1443 if (!newhandle)
1445 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1446 return pdb->nbFiles;
1448 newfiles = (BYTE *)GlobalLock16( newhandle );
1450 if (count > pdb->nbFiles)
1452 memcpy( newfiles, files, pdb->nbFiles );
1453 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1455 else memcpy( newfiles, files, count );
1456 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1457 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1458 pdb->hFileHandles = newhandle;
1459 pdb->nbFiles = count;
1461 return pdb->nbFiles;
1465 /*************************************************************************
1466 * SetHandleCount32 (KERNEL32.494)
1468 UINT32 WINAPI SetHandleCount32( UINT32 count )
1470 return MIN( 256, count );
1474 /***********************************************************************
1475 * FlushFileBuffers (KERNEL32.133)
1477 BOOL32 WINAPI FlushFileBuffers( HFILE32 hFile )
1479 struct flush_file_request req;
1481 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1482 K32OBJ_FILE, 0 )) == -1)
1483 return FALSE;
1484 CLIENT_SendRequest( REQ_FLUSH_FILE, -1, 1, &req, sizeof(req) );
1485 return !CLIENT_WaitReply( NULL, NULL, 0 );
1489 /**************************************************************************
1490 * SetEndOfFile (KERNEL32.483)
1492 BOOL32 WINAPI SetEndOfFile( HFILE32 hFile )
1494 struct truncate_file_request req;
1496 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1497 K32OBJ_FILE, 0 )) == -1)
1498 return FALSE;
1499 CLIENT_SendRequest( REQ_TRUNCATE_FILE, -1, 1, &req, sizeof(req) );
1500 return !CLIENT_WaitReply( NULL, NULL, 0 );
1504 /***********************************************************************
1505 * DeleteFile16 (KERNEL.146)
1507 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1509 return DeleteFile32A( path );
1513 /***********************************************************************
1514 * DeleteFile32A (KERNEL32.71)
1516 BOOL32 WINAPI DeleteFile32A( LPCSTR path )
1518 DOS_FULL_NAME full_name;
1520 TRACE(file, "'%s'\n", path );
1522 if (!*path)
1524 ERR(file, "Empty path passed\n");
1525 return FALSE;
1527 if (DOSFS_GetDevice( path ))
1529 WARN(file, "cannot remove DOS device '%s'!\n", path);
1530 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1531 return FALSE;
1534 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1535 if (unlink( full_name.long_name ) == -1)
1537 FILE_SetDosError();
1538 return FALSE;
1540 return TRUE;
1544 /***********************************************************************
1545 * DeleteFile32W (KERNEL32.72)
1547 BOOL32 WINAPI DeleteFile32W( LPCWSTR path )
1549 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1550 BOOL32 ret = DeleteFile32A( xpath );
1551 HeapFree( GetProcessHeap(), 0, xpath );
1552 return ret;
1556 /***********************************************************************
1557 * FILE_dommap
1559 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1560 DWORD size_high, DWORD size_low,
1561 DWORD offset_high, DWORD offset_low,
1562 int prot, int flags )
1564 int fd = -1;
1565 int pos;
1566 LPVOID ret;
1568 if (size_high || offset_high)
1569 FIXME(file, "offsets larger than 4Gb not supported\n");
1571 if (unix_handle == -1)
1573 #ifdef MAP_ANON
1574 flags |= MAP_ANON;
1575 #else
1576 static int fdzero = -1;
1578 if (fdzero == -1)
1580 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1582 perror( "/dev/zero: open" );
1583 exit(1);
1586 fd = fdzero;
1587 #endif /* MAP_ANON */
1588 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1589 #ifdef MAP_SHARED
1590 flags &= ~MAP_SHARED;
1591 #endif
1592 #ifdef MAP_PRIVATE
1593 flags |= MAP_PRIVATE;
1594 #endif
1596 else fd = unix_handle;
1598 if ((ret = mmap( start, size_low, prot,
1599 flags, fd, offset_low )) != (LPVOID)-1)
1600 return ret;
1602 /* mmap() failed; if this is because the file offset is not */
1603 /* page-aligned (EINVAL), or because the underlying filesystem */
1604 /* does not support mmap() (ENOEXEC), we do it by hand. */
1606 if (unix_handle == -1) return ret;
1607 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1608 if (prot & PROT_WRITE)
1610 /* We cannot fake shared write mappings */
1611 #ifdef MAP_SHARED
1612 if (flags & MAP_SHARED) return ret;
1613 #endif
1614 #ifdef MAP_PRIVATE
1615 if (!(flags & MAP_PRIVATE)) return ret;
1616 #endif
1618 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1619 /* Reserve the memory with an anonymous mmap */
1620 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1621 PROT_READ | PROT_WRITE, flags );
1622 if (ret == (LPVOID)-1) return ret;
1623 /* Now read in the file */
1624 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1626 FILE_munmap( ret, size_high, size_low );
1627 return (LPVOID)-1;
1629 read( fd, ret, size_low );
1630 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1631 mprotect( ret, size_low, prot ); /* Set the right protection */
1632 return ret;
1636 /***********************************************************************
1637 * FILE_munmap
1639 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1641 if (size_high)
1642 FIXME(file, "offsets larger than 4Gb not supported\n");
1643 return munmap( start, size_low );
1647 /***********************************************************************
1648 * GetFileType (KERNEL32.222)
1650 DWORD WINAPI GetFileType( HFILE32 hFile )
1652 struct get_file_info_request req;
1653 struct get_file_info_reply reply;
1655 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1656 K32OBJ_UNKNOWN, 0 )) == -1)
1657 return FILE_TYPE_UNKNOWN;
1658 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
1659 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
1660 return FILE_TYPE_UNKNOWN;
1661 return reply.type;
1665 /**************************************************************************
1666 * MoveFileEx32A (KERNEL32.???)
1668 BOOL32 WINAPI MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1670 DOS_FULL_NAME full_name1, full_name2;
1671 int mode=0; /* mode == 1: use copy */
1673 TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag);
1675 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1676 if (fn2) { /* !fn2 means delete fn1 */
1677 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1678 /* Source name and target path are valid */
1679 if ( full_name1.drive != full_name2.drive)
1681 /* use copy, if allowed */
1682 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1683 /* FIXME: Use right error code */
1684 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
1685 return FALSE;
1687 else mode =1;
1689 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1690 /* target exists, check if we may overwrite */
1691 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1692 /* FIXME: Use right error code */
1693 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1694 return FALSE;
1697 else /* fn2 == NULL means delete source */
1698 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1699 if (flag & MOVEFILE_COPY_ALLOWED) {
1700 WARN(file, "Illegal flag\n");
1701 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1702 EL_Unknown );
1703 return FALSE;
1705 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1706 Perhaps we should queue these command and execute it
1707 when exiting... What about using on_exit(2)
1709 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1710 full_name1.long_name);
1711 return TRUE;
1713 else if (unlink( full_name1.long_name ) == -1)
1715 FILE_SetDosError();
1716 return FALSE;
1718 else return TRUE; /* successfully deleted */
1720 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1721 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1722 Perhaps we should queue these command and execute it
1723 when exiting... What about using on_exit(2)
1725 FIXME(file,"Please move existing file '%s' to file '%s'"
1726 "when Wine has finished\n",
1727 full_name1.long_name, full_name2.long_name);
1728 return TRUE;
1731 if (!mode) /* move the file */
1732 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1734 FILE_SetDosError();
1735 return FALSE;
1737 else return TRUE;
1738 else /* copy File */
1739 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1743 /**************************************************************************
1744 * MoveFileEx32W (KERNEL32.???)
1746 BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1748 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1749 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1750 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1751 HeapFree( GetProcessHeap(), 0, afn1 );
1752 HeapFree( GetProcessHeap(), 0, afn2 );
1753 return res;
1757 /**************************************************************************
1758 * MoveFile32A (KERNEL32.387)
1760 * Move file or directory
1762 BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1764 DOS_FULL_NAME full_name1, full_name2;
1765 struct stat fstat;
1767 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1769 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1770 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1771 /* The new name must not already exist */
1772 return FALSE;
1773 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1775 if (full_name1.drive == full_name2.drive) /* move */
1776 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1778 FILE_SetDosError();
1779 return FALSE;
1781 else return TRUE;
1782 else /*copy */ {
1783 if (stat( full_name1.long_name, &fstat ))
1785 WARN(file, "Invalid source file %s\n",
1786 full_name1.long_name);
1787 FILE_SetDosError();
1788 return FALSE;
1790 if (S_ISDIR(fstat.st_mode)) {
1791 /* No Move for directories across file systems */
1792 /* FIXME: Use right error code */
1793 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1794 EL_Unknown );
1795 return FALSE;
1797 else
1798 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1803 /**************************************************************************
1804 * MoveFile32W (KERNEL32.390)
1806 BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1808 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1809 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1810 BOOL32 res = MoveFile32A( afn1, afn2 );
1811 HeapFree( GetProcessHeap(), 0, afn1 );
1812 HeapFree( GetProcessHeap(), 0, afn2 );
1813 return res;
1817 /**************************************************************************
1818 * CopyFile32A (KERNEL32.36)
1820 BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1822 HFILE32 h1, h2;
1823 BY_HANDLE_FILE_INFORMATION info;
1824 UINT32 count;
1825 BOOL32 ret = FALSE;
1826 int mode;
1827 char buffer[2048];
1829 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1830 if (!GetFileInformationByHandle( h1, &info ))
1832 CloseHandle( h1 );
1833 return FALSE;
1835 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1836 if ((h2 = CreateFile32A( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1837 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1838 info.dwFileAttributes, h1 )) == HFILE_ERROR32)
1840 CloseHandle( h1 );
1841 return FALSE;
1843 while ((count = _lread32( h1, buffer, sizeof(buffer) )) > 0)
1845 char *p = buffer;
1846 while (count > 0)
1848 INT32 res = _lwrite32( h2, p, count );
1849 if (res <= 0) goto done;
1850 p += res;
1851 count -= res;
1854 ret = TRUE;
1855 done:
1856 CloseHandle( h1 );
1857 CloseHandle( h2 );
1858 return ret;
1862 /**************************************************************************
1863 * CopyFile32W (KERNEL32.37)
1865 BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
1867 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1868 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1869 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1870 HeapFree( GetProcessHeap(), 0, sourceA );
1871 HeapFree( GetProcessHeap(), 0, destA );
1872 return ret;
1876 /**************************************************************************
1877 * CopyFileEx32A (KERNEL32.858)
1879 * This implementation ignores most of the extra parameters passed-in into
1880 * the "ex" version of the method and calls the CopyFile method.
1881 * It will have to be fixed eventually.
1883 BOOL32 WINAPI CopyFileEx32A(LPCSTR sourceFilename,
1884 LPCSTR destFilename,
1885 LPPROGRESS_ROUTINE progressRoutine,
1886 LPVOID appData,
1887 LPBOOL32 cancelFlagPointer,
1888 DWORD copyFlags)
1890 BOOL32 failIfExists = FALSE;
1893 * Interpret the only flag that CopyFile can interpret.
1895 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1897 failIfExists = TRUE;
1900 return CopyFile32A(sourceFilename, destFilename, failIfExists);
1903 /**************************************************************************
1904 * CopyFileEx32W (KERNEL32.859)
1906 BOOL32 WINAPI CopyFileEx32W(LPCWSTR sourceFilename,
1907 LPCWSTR destFilename,
1908 LPPROGRESS_ROUTINE progressRoutine,
1909 LPVOID appData,
1910 LPBOOL32 cancelFlagPointer,
1911 DWORD copyFlags)
1913 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1914 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1916 BOOL32 ret = CopyFileEx32A(sourceA,
1917 destA,
1918 progressRoutine,
1919 appData,
1920 cancelFlagPointer,
1921 copyFlags);
1923 HeapFree( GetProcessHeap(), 0, sourceA );
1924 HeapFree( GetProcessHeap(), 0, destA );
1926 return ret;
1930 /***********************************************************************
1931 * SetFileTime (KERNEL32.650)
1933 BOOL32 WINAPI SetFileTime( HFILE32 hFile,
1934 const FILETIME *lpCreationTime,
1935 const FILETIME *lpLastAccessTime,
1936 const FILETIME *lpLastWriteTime )
1938 struct set_file_time_request req;
1940 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1941 K32OBJ_FILE, GENERIC_WRITE )) == -1)
1942 return FALSE;
1943 if (lpLastAccessTime)
1944 req.access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1945 else
1946 req.access_time = 0; /* FIXME */
1947 if (lpLastWriteTime)
1948 req.write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1949 else
1950 req.write_time = 0; /* FIXME */
1952 CLIENT_SendRequest( REQ_SET_FILE_TIME, -1, 1, &req, sizeof(req) );
1953 return !CLIENT_WaitReply( NULL, NULL, 0 );
1957 /**************************************************************************
1958 * LockFile (KERNEL32.511)
1960 BOOL32 WINAPI LockFile( HFILE32 hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1961 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1963 struct lock_file_request req;
1965 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1966 K32OBJ_FILE, 0 )) == -1)
1967 return FALSE;
1968 req.offset_low = dwFileOffsetLow;
1969 req.offset_high = dwFileOffsetHigh;
1970 req.count_low = nNumberOfBytesToLockLow;
1971 req.count_high = nNumberOfBytesToLockHigh;
1972 CLIENT_SendRequest( REQ_LOCK_FILE, -1, 1, &req, sizeof(req) );
1973 return !CLIENT_WaitReply( NULL, NULL, 0 );
1977 /**************************************************************************
1978 * UnlockFile (KERNEL32.703)
1980 BOOL32 WINAPI UnlockFile( HFILE32 hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1981 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1983 struct unlock_file_request req;
1985 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1986 K32OBJ_FILE, 0 )) == -1)
1987 return FALSE;
1988 req.offset_low = dwFileOffsetLow;
1989 req.offset_high = dwFileOffsetHigh;
1990 req.count_low = nNumberOfBytesToUnlockLow;
1991 req.count_high = nNumberOfBytesToUnlockHigh;
1992 CLIENT_SendRequest( REQ_UNLOCK_FILE, -1, 1, &req, sizeof(req) );
1993 return !CLIENT_WaitReply( NULL, NULL, 0 );
1997 #if 0
1999 struct DOS_FILE_LOCK {
2000 struct DOS_FILE_LOCK * next;
2001 DWORD base;
2002 DWORD len;
2003 DWORD processId;
2004 FILE_OBJECT * dos_file;
2005 /* char * unix_name;*/
2008 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2010 static DOS_FILE_LOCK *locks = NULL;
2011 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2014 /* Locks need to be mirrored because unix file locking is based
2015 * on the pid. Inside of wine there can be multiple WINE processes
2016 * that share the same unix pid.
2017 * Read's and writes should check these locks also - not sure
2018 * how critical that is at this point (FIXME).
2021 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2023 DOS_FILE_LOCK *curr;
2024 DWORD processId;
2026 processId = GetCurrentProcessId();
2028 /* check if lock overlaps a current lock for the same file */
2029 #if 0
2030 for (curr = locks; curr; curr = curr->next) {
2031 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2032 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2033 return TRUE;/* region is identic */
2034 if ((f->l_start < (curr->base + curr->len)) &&
2035 ((f->l_start + f->l_len) > curr->base)) {
2036 /* region overlaps */
2037 return FALSE;
2041 #endif
2043 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2044 curr->processId = GetCurrentProcessId();
2045 curr->base = f->l_start;
2046 curr->len = f->l_len;
2047 /* curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);*/
2048 curr->next = locks;
2049 curr->dos_file = file;
2050 locks = curr;
2051 return TRUE;
2054 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2056 DWORD processId;
2057 DOS_FILE_LOCK **curr;
2058 DOS_FILE_LOCK *rem;
2060 processId = GetCurrentProcessId();
2061 curr = &locks;
2062 while (*curr) {
2063 if ((*curr)->dos_file == file) {
2064 rem = *curr;
2065 *curr = (*curr)->next;
2066 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2067 HeapFree( SystemHeap, 0, rem );
2069 else
2070 curr = &(*curr)->next;
2074 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2076 DWORD processId;
2077 DOS_FILE_LOCK **curr;
2078 DOS_FILE_LOCK *rem;
2080 processId = GetCurrentProcessId();
2081 for (curr = &locks; *curr; curr = &(*curr)->next) {
2082 if ((*curr)->processId == processId &&
2083 (*curr)->dos_file == file &&
2084 (*curr)->base == f->l_start &&
2085 (*curr)->len == f->l_len) {
2086 /* this is the same lock */
2087 rem = *curr;
2088 *curr = (*curr)->next;
2089 /* HeapFree( SystemHeap, 0, rem->unix_name );*/
2090 HeapFree( SystemHeap, 0, rem );
2091 return TRUE;
2094 /* no matching lock found */
2095 return FALSE;
2099 /**************************************************************************
2100 * LockFile (KERNEL32.511)
2102 BOOL32 WINAPI LockFile(
2103 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2104 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2106 struct flock f;
2107 FILE_OBJECT *file;
2109 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2110 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2111 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2113 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2114 FIXME(file, "Unimplemented bytes > 32bits\n");
2115 return FALSE;
2118 f.l_start = dwFileOffsetLow;
2119 f.l_len = nNumberOfBytesToLockLow;
2120 f.l_whence = SEEK_SET;
2121 f.l_pid = 0;
2122 f.l_type = F_WRLCK;
2124 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2126 /* shadow locks internally */
2127 if (!DOS_AddLock(file, &f)) {
2128 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
2129 return FALSE;
2132 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2133 #ifdef USE_UNIX_LOCKS
2134 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2135 if (errno == EACCES || errno == EAGAIN) {
2136 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
2138 else {
2139 FILE_SetDosError();
2141 /* remove our internal copy of the lock */
2142 DOS_RemoveLock(file, &f);
2143 return FALSE;
2145 #endif
2146 return TRUE;
2150 /**************************************************************************
2151 * UnlockFile (KERNEL32.703)
2153 BOOL32 WINAPI UnlockFile(
2154 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2155 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2157 FILE_OBJECT *file;
2158 struct flock f;
2160 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2161 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2162 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2164 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2165 WARN(file, "Unimplemented bytes > 32bits\n");
2166 return FALSE;
2169 f.l_start = dwFileOffsetLow;
2170 f.l_len = nNumberOfBytesToUnlockLow;
2171 f.l_whence = SEEK_SET;
2172 f.l_pid = 0;
2173 f.l_type = F_UNLCK;
2175 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2177 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2179 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2180 #ifdef USE_UNIX_LOCKS
2181 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2182 FILE_SetDosError();
2183 return FALSE;
2185 #endif
2186 return TRUE;
2188 #endif
2190 /**************************************************************************
2191 * GetFileAttributesEx32A [KERNEL32.874]
2193 BOOL32 WINAPI GetFileAttributesEx32A(
2194 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2195 LPVOID lpFileInformation)
2197 DOS_FULL_NAME full_name;
2198 BY_HANDLE_FILE_INFORMATION info;
2200 if (lpFileName == NULL) return FALSE;
2201 if (lpFileInformation == NULL) return FALSE;
2203 if (fInfoLevelId == GetFileExInfoStandard) {
2204 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2205 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2206 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2207 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2209 lpFad->dwFileAttributes = info.dwFileAttributes;
2210 lpFad->ftCreationTime = info.ftCreationTime;
2211 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2212 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2213 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2214 lpFad->nFileSizeLow = info.nFileSizeLow;
2216 else {
2217 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
2218 return FALSE;
2221 return TRUE;
2225 /**************************************************************************
2226 * GetFileAttributesEx32W [KERNEL32.875]
2228 BOOL32 WINAPI GetFileAttributesEx32W(
2229 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2230 LPVOID lpFileInformation)
2232 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2233 BOOL32 res =
2234 GetFileAttributesEx32A( nameA, fInfoLevelId, lpFileInformation);
2235 HeapFree( GetProcessHeap(), 0, nameA );
2236 return res;