Get rid of the redefinition of MAX_PATH and move PROCESSENTRY32
[wine/wine64.git] / files / file.c
blob451c3573362fde04d47f4d92f106cfad4e90b41d
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <assert.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/mman.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include <utime.h>
22 #include "windows.h"
23 #include "winerror.h"
24 #include "drive.h"
25 #include "file.h"
26 #include "global.h"
27 #include "heap.h"
28 #include "msdos.h"
29 #include "options.h"
30 #include "ldt.h"
31 #include "process.h"
32 #include "task.h"
33 #include "debug.h"
35 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
36 #define MAP_ANON MAP_ANONYMOUS
37 #endif
39 static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
40 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped);
41 static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars,
42 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped);
43 static void FILE_Destroy( K32OBJ *obj );
45 const K32OBJ_OPS FILE_Ops =
47 /* Object cannot be waited upon (FIXME: for now) */
48 NULL, /* signaled */
49 NULL, /* satisfied */
50 NULL, /* add_wait */
51 NULL, /* remove_wait */
52 FILE_Read, /* read */
53 FILE_Write, /* write */
54 FILE_Destroy /* destroy */
57 struct DOS_FILE_LOCK {
58 struct DOS_FILE_LOCK * next;
59 DWORD base;
60 DWORD len;
61 DWORD processId;
62 FILE_OBJECT * dos_file;
63 char * unix_name;
66 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
68 static DOS_FILE_LOCK *locks = NULL;
69 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
71 /***********************************************************************
72 * FILE_Alloc
74 * Allocate a file.
76 HFILE32 FILE_Alloc( FILE_OBJECT **file )
78 HFILE32 handle;
79 *file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) );
80 if (!*file)
82 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
83 return (HFILE32)NULL;
85 (*file)->header.type = K32OBJ_FILE;
86 (*file)->header.refcount = 0;
87 (*file)->unix_handle = -1;
88 (*file)->unix_name = NULL;
89 (*file)->type = FILE_TYPE_DISK;
90 (*file)->pos = 0;
91 (*file)->mode = 0;
93 handle = HANDLE_Alloc( PROCESS_Current(), &(*file)->header,
94 FILE_ALL_ACCESS | GENERIC_READ |
95 GENERIC_WRITE | GENERIC_EXECUTE /*FIXME*/, TRUE, -1 );
96 /* If the allocation failed, the object is already destroyed */
97 if (handle == INVALID_HANDLE_VALUE32) *file = NULL;
98 return handle;
102 /* FIXME: lpOverlapped is ignored */
103 static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
104 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
106 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
107 int result;
109 TRACE(file, "%p %p %ld\n", ptr, lpBuffer,
110 nNumberOfChars);
112 if (nNumberOfChars == 0) {
113 *lpNumberOfChars = 0; /* FIXME: does this change */
114 return TRUE;
117 if ( (file->pos < 0) || /* workaround, see SetFilePointer */
118 ((result = read(file->unix_handle, lpBuffer, nNumberOfChars)) == -1) )
120 FILE_SetDosError();
121 return FALSE;
123 file->pos += result;
124 *lpNumberOfChars = result;
125 return TRUE;
129 * experimentation yields that WriteFile:
130 * o does not truncate on write of 0
131 * o always changes the *lpNumberOfChars to actual number of
132 * characters written
133 * o write of 0 nNumberOfChars returns TRUE
135 static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars,
136 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
138 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
139 int result;
141 TRACE(file, "%p %p %ld\n", ptr, lpBuffer,
142 nNumberOfChars);
144 *lpNumberOfChars = 0;
147 * I assume this loop around EAGAIN is here because
148 * win32 doesn't have interrupted system calls
151 if (file->pos < 0) { /* workaround, see SetFilePointer */
152 FILE_SetDosError();
153 return FALSE;
156 for (;;)
158 result = write(file->unix_handle, lpBuffer, nNumberOfChars);
159 if (result != -1) {
160 *lpNumberOfChars = result;
161 file->pos += result;
162 return TRUE;
164 if (errno != EINTR) {
165 FILE_SetDosError();
166 return FALSE;
173 /***********************************************************************
174 * FILE_Destroy
176 * Destroy a DOS file.
178 static void FILE_Destroy( K32OBJ *ptr )
180 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
181 assert( ptr->type == K32OBJ_FILE );
183 DOS_RemoveFileLocks(file);
185 if (file->unix_handle != -1) close( file->unix_handle );
186 if (file->unix_name) HeapFree( SystemHeap, 0, file->unix_name );
187 ptr->type = K32OBJ_UNKNOWN;
188 HeapFree( SystemHeap, 0, file );
192 /***********************************************************************
193 * FILE_GetFile
195 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
196 * be called to release the file.
198 FILE_OBJECT *FILE_GetFile( HFILE32 handle )
200 return (FILE_OBJECT *)HANDLE_GetObjPtr( PROCESS_Current(), handle,
201 K32OBJ_FILE, 0 /*FIXME*/, NULL );
205 /***********************************************************************
206 * FILE_ReleaseFile
208 * Release a DOS file obtained with FILE_GetFile.
210 void FILE_ReleaseFile( FILE_OBJECT *file )
212 K32OBJ_DecCount( &file->header );
216 /***********************************************************************
217 * FILE_GetUnixHandle
219 * Return the Unix handle associated to a file handle.
221 int FILE_GetUnixHandle( HFILE32 hFile )
223 FILE_OBJECT *file;
224 int ret;
226 if (!(file = FILE_GetFile( hFile ))) return -1;
227 ret = file->unix_handle;
228 FILE_ReleaseFile( file );
229 return ret;
232 /***********************************************************************
233 * FILE_UnixToDosMode
235 * PARAMS
236 * unixmode[I]
237 * RETURNS
238 * dosmode
240 static int FILE_UnixToDosMode(int unixMode)
242 int dosMode;
243 switch(unixMode & 3)
245 case O_WRONLY:
246 dosMode = OF_WRITE;
247 break;
248 case O_RDWR:
249 dosMode =OF_READWRITE;
250 break;
251 case O_RDONLY:
252 default:
253 dosMode = OF_READ;
254 break;
256 return dosMode;
259 /***********************************************************************
260 * FILE_DOSToUnixMode
262 * PARAMS
263 * dosMode[I]
264 * RETURNS
265 * unixmode
267 static int FILE_DOSToUnixMode(int dosMode)
269 int unixMode;
270 switch(dosMode & 3)
272 case OF_WRITE:
273 unixMode = O_WRONLY; break;
274 case OF_READWRITE:
275 unixMode = O_RDWR; break;
276 case OF_READ:
277 default:
278 unixMode = O_RDONLY; break;
280 return unixMode;
283 /***********************************************************************
284 * FILE_ShareDeny
286 * PARAMS
287 * oldmode[I] mode how file was first opened
288 * mode[I] mode how the file should get opened
289 * RETURNS
290 * TRUE: deny open
291 * FALSE: allow open
293 * Look what we have to do with the given SHARE modes
295 * Ralph Brown's interrupt list gives following explication, I guess
296 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
298 * FIXME: Validate this function
299 ========from Ralph Brown's list =========
300 (Table 0750)
301 Values of DOS file sharing behavior:
302 | Second and subsequent Opens
303 First |Compat Deny Deny Deny Deny
304 Open | All Write Read None
305 |R W RW R W RW R W RW R W RW R W RW
306 - - - - -| - - - - - - - - - - - - - - - - -
307 Compat R |Y Y Y N N N 1 N N N N N 1 N N
308 W |Y Y Y N N N N N N N N N N N N
309 RW|Y Y Y N N N N N N N N N N N N
310 - - - - -|
311 Deny R |C C C N N N N N N N N N N N N
312 All W |C C C N N N N N N N N N N N N
313 RW|C C C N N N N N N N N N N N N
314 - - - - -|
315 Deny R |2 C C N N N Y N N N N N Y N N
316 Write W |C C C N N N N N N Y N N Y N N
317 RW|C C C N N N N N N N N N Y N N
318 - - - - -|
319 Deny R |C C C N N N N Y N N N N N Y N
320 Read W |C C C N N N N N N N Y N N Y N
321 RW|C C C N N N N N N N N N N Y N
322 - - - - -|
323 Deny R |2 C C N N N Y Y Y N N N Y Y Y
324 None W |C C C N N N N N N Y Y Y Y Y Y
325 RW|C C C N N N N N N N N N Y Y Y
326 Legend: Y = open succeeds, N = open fails with error code 05h
327 C = open fails, INT 24 generated
328 1 = open succeeds if file read-only, else fails with error code
329 2 = open succeeds if file read-only, else fails with INT 24
330 ========end of description from Ralph Brown's List =====
331 For every "Y" in the table we return FALSE
332 For every "N" we set the DOS_ERROR and return TRUE
333 For all other cases we barf,set the DOS_ERROR and return TRUE
336 static BOOL32 FILE_ShareDeny( int mode, int oldmode)
338 int oldsharemode = oldmode & 0x70;
339 int sharemode = mode & 0x70;
340 int oldopenmode = oldmode & 3;
341 int openmode = mode & 3;
343 switch (oldsharemode)
345 case OF_SHARE_COMPAT:
346 if (sharemode == OF_SHARE_COMPAT) return FALSE;
347 if (openmode == OF_READ) goto test_ro_err05 ;
348 goto fail_error05;
349 case OF_SHARE_EXCLUSIVE:
350 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
351 goto fail_error05;
352 case OF_SHARE_DENY_WRITE:
353 if (openmode != OF_READ)
355 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
356 goto fail_error05;
358 switch (sharemode)
360 case OF_SHARE_COMPAT:
361 if (oldopenmode == OF_READ) goto test_ro_int24 ;
362 goto fail_int24;
363 case OF_SHARE_DENY_NONE :
364 return FALSE;
365 case OF_SHARE_DENY_WRITE :
366 if (oldopenmode == OF_READ) return FALSE;
367 case OF_SHARE_DENY_READ :
368 if (oldopenmode == OF_WRITE) return FALSE;
369 case OF_SHARE_EXCLUSIVE:
370 default:
371 goto fail_error05;
373 break;
374 case OF_SHARE_DENY_READ:
375 if (openmode != OF_WRITE)
377 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
378 goto fail_error05;
380 switch (sharemode)
382 case OF_SHARE_COMPAT:
383 goto fail_int24;
384 case OF_SHARE_DENY_NONE :
385 return FALSE;
386 case OF_SHARE_DENY_WRITE :
387 if (oldopenmode == OF_READ) return FALSE;
388 case OF_SHARE_DENY_READ :
389 if (oldopenmode == OF_WRITE) return FALSE;
390 case OF_SHARE_EXCLUSIVE:
391 default:
392 goto fail_error05;
394 break;
395 case OF_SHARE_DENY_NONE:
396 switch (sharemode)
398 case OF_SHARE_COMPAT:
399 goto fail_int24;
400 case OF_SHARE_DENY_NONE :
401 return FALSE;
402 case OF_SHARE_DENY_WRITE :
403 if (oldopenmode == OF_READ) return FALSE;
404 case OF_SHARE_DENY_READ :
405 if (oldopenmode == OF_WRITE) return FALSE;
406 case OF_SHARE_EXCLUSIVE:
407 default:
408 goto fail_error05;
410 default:
411 ERR(file,"unknown mode\n");
413 ERR(file,"shouldn't happen\n");
414 ERR(file,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
415 return TRUE;
417 test_ro_int24:
418 if (oldmode & OF_READ)
419 return FALSE;
420 /* Fall through */
421 fail_int24:
422 FIXME(file,"generate INT24 missing\n");
423 /* Is this the right error? */
424 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
425 return TRUE;
427 test_ro_err05:
428 if (oldmode & OF_READ)
429 return FALSE;
430 /* fall through */
431 fail_error05:
432 TRACE(file,"Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
433 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
434 return TRUE;
439 /***********************************************************************
442 * Look if the File is in Use For the OF_SHARE_XXX options
444 * PARAMS
445 * name [I]: full unix name of the file that should be opened
446 * mode [O]: mode how the file was first opened
447 * RETURNS
448 * TRUE if the file was opened before
449 * FALSE if we open the file exclusive for this process
451 * Scope of the files we look for is only the current pdb
452 * Could we use /proc/self/? on Linux for this?
453 * Should we use flock? Should we create another structure?
454 * Searching through all files seem quite expensive for me, but
455 * I don't see any other way.
457 * FIXME: Extend scope to the whole Wine process
460 static BOOL32 FILE_InUse(char * name, int * mode)
462 FILE_OBJECT *file;
463 int i;
464 HGLOBAL16 hPDB = GetCurrentPDB();
465 PDB *pdb = (PDB *)GlobalLock16( hPDB );
467 if (!pdb) return 0;
468 for (i=0;i<pdb->nbFiles;i++)
470 file =FILE_GetFile( (HFILE32) i);
471 if(file)
473 if(file->unix_name)
475 TRACE(file,"got %s at %d\n",file->unix_name,i);
476 if(!lstrcmp32A(file->unix_name,name))
478 *mode = file->mode;
479 FILE_ReleaseFile(file);
480 return TRUE;
483 FILE_ReleaseFile(file);
486 return FALSE;
489 /***********************************************************************
490 * FILE_SetDosError
492 * Set the DOS error code from errno.
494 void FILE_SetDosError(void)
496 int save_errno = errno; /* errno gets overwritten by printf */
498 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
499 switch (save_errno)
501 case EAGAIN:
502 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
503 break;
504 case EBADF:
505 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
506 break;
507 case ENOSPC:
508 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
509 break;
510 case EACCES:
511 case EPERM:
512 case EROFS:
513 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
514 break;
515 case EBUSY:
516 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
517 break;
518 case ENOENT:
519 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
520 break;
521 case EISDIR:
522 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
523 break;
524 case ENFILE:
525 case EMFILE:
526 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
527 break;
528 case EEXIST:
529 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
530 break;
531 case EINVAL:
532 case ESPIPE:
533 DOS_ERROR( ER_SeekError, EC_NotFound, SA_Ignore, EL_Disk );
534 break;
535 case ENOTEMPTY:
536 DOS_ERROR( ERROR_DIR_NOT_EMPTY, EC_Exists, SA_Ignore, EL_Disk );
537 break;
538 default:
539 perror( "int21: unknown errno" );
540 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
541 break;
543 errno = save_errno;
547 /***********************************************************************
548 * FILE_DupUnixHandle
550 * Duplicate a Unix handle into a task handle.
552 HFILE32 FILE_DupUnixHandle( int fd )
554 HFILE32 handle;
555 FILE_OBJECT *file;
557 if ((handle = FILE_Alloc( &file )) != INVALID_HANDLE_VALUE32)
559 if ((file->unix_handle = dup(fd)) == -1)
561 FILE_SetDosError();
562 CloseHandle( handle );
563 return INVALID_HANDLE_VALUE32;
566 return handle;
570 /***********************************************************************
571 * FILE_OpenUnixFile
573 HFILE32 FILE_OpenUnixFile( const char *name, int mode )
575 HFILE32 handle;
576 FILE_OBJECT *file;
577 struct stat st;
579 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
580 return INVALID_HANDLE_VALUE32;
582 if ((file->unix_handle = open( name, mode, 0666 )) == -1)
584 if (!Options.failReadOnly && (mode == O_RDWR))
585 file->unix_handle = open( name, O_RDONLY );
587 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
589 FILE_SetDosError();
590 CloseHandle( handle );
591 return INVALID_HANDLE_VALUE32;
593 if (S_ISDIR(st.st_mode))
595 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
596 CloseHandle( handle );
597 return INVALID_HANDLE_VALUE32;
600 /* File opened OK, now fill the FILE_OBJECT */
602 file->unix_name = HEAP_strdupA( SystemHeap, 0, name );
603 return handle;
607 /***********************************************************************
608 * FILE_Open
610 * path[I] name of file to open
611 * mode[I] mode how to open, in unix notation
612 * shareMode[I] the sharing mode in the win OpenFile notation
615 HFILE32 FILE_Open( LPCSTR path, INT32 mode, INT32 shareMode )
617 DOS_FULL_NAME full_name;
618 const char *unixName;
619 int oldMode, dosMode; /* FIXME: Do we really need unixmode as argument for
620 FILE_Open */
621 FILE_OBJECT *file;
622 HFILE32 hFileRet;
623 BOOL32 fileInUse = FALSE;
625 TRACE(file, "'%s' %04x\n", path, mode );
627 if (!path) return HFILE_ERROR32;
629 if (DOSFS_GetDevice( path ))
631 HFILE32 ret;
633 TRACE(file, "opening device '%s'\n", path );
635 if (HFILE_ERROR32!=(ret=DOSFS_OpenDevice( path, mode )))
636 return ret;
638 /* Do not silence this please. It is a critical error. -MM */
639 ERR(file, "Couldn't open device '%s'!\n",path);
640 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
641 return HFILE_ERROR32;
644 else /* check for filename, don't check for last entry if creating */
646 if (!DOSFS_GetFullName( path, !(mode & O_CREAT), &full_name ))
647 return HFILE_ERROR32;
648 unixName = full_name.long_name;
651 dosMode = FILE_UnixToDosMode(mode)| shareMode;
652 fileInUse = FILE_InUse(full_name.long_name,&oldMode);
653 if(fileInUse)
655 TRACE(file, "found another instance with mode 0x%02x\n",oldMode&0x70);
656 if (FILE_ShareDeny(dosMode,oldMode)) return HFILE_ERROR32;
658 hFileRet = FILE_OpenUnixFile( unixName, mode );
659 /* we need to save the mode, but only if it is not in use yet*/
660 if ((hFileRet) && (!fileInUse) && ((file =FILE_GetFile(hFileRet))))
662 file->mode=dosMode;
663 FILE_ReleaseFile(file);
665 return hFileRet;
670 /***********************************************************************
671 * FILE_Create
673 static HFILE32 FILE_Create( LPCSTR path, int mode, int unique )
675 HFILE32 handle;
676 FILE_OBJECT *file;
677 DOS_FULL_NAME full_name;
678 BOOL32 fileInUse = FALSE;
679 int oldMode,dosMode; /* FIXME: Do we really need unixmode as argument for
680 FILE_Create */;
682 TRACE(file, "'%s' %04x %d\n", path, mode, unique );
684 if (!path) return INVALID_HANDLE_VALUE32;
686 if (DOSFS_GetDevice( path ))
688 WARN(file, "cannot create DOS device '%s'!\n", path);
689 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
690 return INVALID_HANDLE_VALUE32;
693 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
694 return INVALID_HANDLE_VALUE32;
696 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
698 CloseHandle( handle );
699 return INVALID_HANDLE_VALUE32;
702 dosMode = FILE_UnixToDosMode(mode);
703 fileInUse = FILE_InUse(full_name.long_name,&oldMode);
704 if(fileInUse)
706 TRACE(file, "found another instance with mode 0x%02x\n",oldMode&0x70);
707 if (FILE_ShareDeny(dosMode,oldMode)) return INVALID_HANDLE_VALUE32;
710 if ((file->unix_handle = open( full_name.long_name,
711 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
712 mode )) == -1)
714 FILE_SetDosError();
715 CloseHandle( handle );
716 return INVALID_HANDLE_VALUE32;
719 /* File created OK, now fill the FILE_OBJECT */
721 file->unix_name = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
722 file->mode = dosMode;
723 return handle;
727 /***********************************************************************
728 * FILE_FillInfo
730 * Fill a file information from a struct stat.
732 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
734 if (S_ISDIR(st->st_mode))
735 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
736 else
737 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
738 if (!(st->st_mode & S_IWUSR))
739 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
741 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
742 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
743 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
745 info->dwVolumeSerialNumber = 0; /* FIXME */
746 info->nFileSizeHigh = 0;
747 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
748 info->nNumberOfLinks = st->st_nlink;
749 info->nFileIndexHigh = 0;
750 info->nFileIndexLow = st->st_ino;
754 /***********************************************************************
755 * FILE_Stat
757 * Stat a Unix path name. Return TRUE if OK.
759 BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
761 struct stat st;
763 if (!unixName || !info) return FALSE;
765 if (stat( unixName, &st ) == -1)
767 FILE_SetDosError();
768 return FALSE;
770 FILE_FillInfo( &st, info );
771 return TRUE;
775 /***********************************************************************
776 * GetFileInformationByHandle (KERNEL32.219)
778 DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile,
779 BY_HANDLE_FILE_INFORMATION *info )
781 FILE_OBJECT *file;
782 DWORD ret = 0;
783 struct stat st;
785 if (!info) return 0;
787 if (!(file = FILE_GetFile( hFile ))) return 0;
788 if (fstat( file->unix_handle, &st ) == -1) FILE_SetDosError();
789 else
791 FILE_FillInfo( &st, info );
792 ret = 1;
794 FILE_ReleaseFile( file );
795 return ret;
799 /**************************************************************************
800 * GetFileAttributes16 (KERNEL.420)
802 DWORD WINAPI GetFileAttributes16( LPCSTR name )
804 return GetFileAttributes32A( name );
808 /**************************************************************************
809 * GetFileAttributes32A (KERNEL32.217)
811 DWORD WINAPI GetFileAttributes32A( LPCSTR name )
813 DOS_FULL_NAME full_name;
814 BY_HANDLE_FILE_INFORMATION info;
816 if (name == NULL || *name=='\0') return -1;
818 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
819 if (!FILE_Stat( full_name.long_name, &info )) return -1;
820 return info.dwFileAttributes;
824 /**************************************************************************
825 * GetFileAttributes32W (KERNEL32.218)
827 DWORD WINAPI GetFileAttributes32W( LPCWSTR name )
829 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
830 DWORD res = GetFileAttributes32A( nameA );
831 HeapFree( GetProcessHeap(), 0, nameA );
832 return res;
836 /***********************************************************************
837 * GetFileSize (KERNEL32.220)
839 DWORD WINAPI GetFileSize( HFILE32 hFile, LPDWORD filesizehigh )
841 BY_HANDLE_FILE_INFORMATION info;
842 if (!GetFileInformationByHandle( hFile, &info )) return 0;
843 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
844 return info.nFileSizeLow;
848 /***********************************************************************
849 * GetFileTime (KERNEL32.221)
851 BOOL32 WINAPI GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
852 FILETIME *lpLastAccessTime,
853 FILETIME *lpLastWriteTime )
855 BY_HANDLE_FILE_INFORMATION info;
856 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
857 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
858 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
859 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
860 return TRUE;
863 /***********************************************************************
864 * CompareFileTime (KERNEL32.28)
866 INT32 WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
868 if (!x || !y) return -1;
870 if (x->dwHighDateTime > y->dwHighDateTime)
871 return 1;
872 if (x->dwHighDateTime < y->dwHighDateTime)
873 return -1;
874 if (x->dwLowDateTime > y->dwLowDateTime)
875 return 1;
876 if (x->dwLowDateTime < y->dwLowDateTime)
877 return -1;
878 return 0;
881 /***********************************************************************
882 * FILE_Dup
884 * dup() function for DOS handles.
886 HFILE32 FILE_Dup( HFILE32 hFile )
888 HFILE32 handle;
890 TRACE(file, "FILE_Dup for handle %d\n", hFile );
891 if (!DuplicateHandle( GetCurrentProcess(), hFile, GetCurrentProcess(),
892 &handle, FILE_ALL_ACCESS /* FIXME */, FALSE, 0 ))
893 handle = HFILE_ERROR32;
894 TRACE(file, "FILE_Dup return handle %d\n", handle );
895 return handle;
899 /***********************************************************************
900 * FILE_Dup2
902 * dup2() function for DOS handles.
904 HFILE32 FILE_Dup2( HFILE32 hFile1, HFILE32 hFile2 )
906 FILE_OBJECT *file;
908 TRACE(file, "FILE_Dup2 for handle %d\n", hFile1 );
909 /* FIXME: should use DuplicateHandle */
910 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR32;
911 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2, &file->header, 0 ))
912 hFile2 = HFILE_ERROR32;
913 FILE_ReleaseFile( file );
914 return hFile2;
918 /***********************************************************************
919 * GetTempFileName16 (KERNEL.97)
921 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
922 LPSTR buffer )
924 char temppath[144];
926 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
927 drive |= DRIVE_GetCurrentDrive() + 'A';
929 if ((drive & TF_FORCEDRIVE) &&
930 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
932 drive &= ~TF_FORCEDRIVE;
933 WARN(file, "invalid drive %d specified\n", drive );
936 if (drive & TF_FORCEDRIVE)
937 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
938 else
939 GetTempPath32A( 132, temppath );
940 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
944 /***********************************************************************
945 * GetTempFileName32A (KERNEL32.290)
947 UINT32 WINAPI GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
948 LPSTR buffer)
950 static UINT32 unique_temp;
951 DOS_FULL_NAME full_name;
952 int i;
953 LPSTR p;
954 UINT32 num;
956 if ( !path || !prefix || !buffer ) return 0;
958 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
959 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
961 strcpy( buffer, path );
962 p = buffer + strlen(buffer);
964 /* add a \, if there isn't one and path is more than just the drive letter ... */
965 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
966 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
968 *p++ = '~';
969 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
970 sprintf( p, "%04x.tmp", num );
972 /* Now try to create it */
974 if (!unique)
978 HFILE32 handle = FILE_Create( buffer, 0666, TRUE );
979 if (handle != INVALID_HANDLE_VALUE32)
980 { /* We created it */
981 TRACE(file, "created %s\n",
982 buffer);
983 CloseHandle( handle );
984 break;
986 if (DOS_ExtendedError != ER_FileExists)
987 break; /* No need to go on */
988 num++;
989 sprintf( p, "%04x.tmp", num );
990 } while (num != (unique & 0xffff));
993 /* Get the full path name */
995 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
997 /* Check if we have write access in the directory */
998 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
999 if (access( full_name.long_name, W_OK ) == -1)
1000 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
1001 buffer);
1003 TRACE(file, "returning %s\n", buffer );
1004 return unique ? unique : num;
1008 /***********************************************************************
1009 * GetTempFileName32W (KERNEL32.291)
1011 UINT32 WINAPI GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
1012 LPWSTR buffer )
1014 LPSTR patha,prefixa;
1015 char buffera[144];
1016 UINT32 ret;
1018 if (!path) return 0;
1019 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1020 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
1021 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
1022 lstrcpyAtoW( buffer, buffera );
1023 HeapFree( GetProcessHeap(), 0, patha );
1024 HeapFree( GetProcessHeap(), 0, prefixa );
1025 return ret;
1029 /***********************************************************************
1030 * FILE_DoOpenFile
1032 * Implementation of OpenFile16() and OpenFile32().
1034 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
1035 BOOL32 win32 )
1037 HFILE32 hFileRet;
1038 FILETIME filetime;
1039 WORD filedatetime[2];
1040 DOS_FULL_NAME full_name;
1041 char *p;
1042 int unixMode, oldMode;
1043 FILE_OBJECT *file;
1044 BOOL32 fileInUse = FALSE;
1046 if (!ofs) return HFILE_ERROR32;
1049 ofs->cBytes = sizeof(OFSTRUCT);
1050 ofs->nErrCode = 0;
1051 if (mode & OF_REOPEN) name = ofs->szPathName;
1053 if (!name) {
1054 ERR(file, "called with `name' set to NULL ! Please debug.\n");
1055 return HFILE_ERROR32;
1058 TRACE(file, "%s %04x\n", name, mode );
1060 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
1061 Are there any cases where getting the path here is wrong?
1062 Uwe Bonnes 1997 Apr 2 */
1063 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
1064 ofs->szPathName, NULL )) goto error;
1066 /* OF_PARSE simply fills the structure */
1068 if (mode & OF_PARSE)
1070 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
1071 != DRIVE_REMOVABLE);
1072 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
1073 name, ofs->szPathName );
1074 return 0;
1077 /* OF_CREATE is completely different from all other options, so
1078 handle it first */
1080 if (mode & OF_CREATE)
1082 if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32)
1083 goto error;
1084 goto success;
1087 /* If OF_SEARCH is set, ignore the given path */
1089 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
1091 /* First try the file name as is */
1092 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
1093 /* Now remove the path */
1094 if (name[0] && (name[1] == ':')) name += 2;
1095 if ((p = strrchr( name, '\\' ))) name = p + 1;
1096 if ((p = strrchr( name, '/' ))) name = p + 1;
1097 if (!name[0]) goto not_found;
1100 /* Now look for the file */
1102 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
1104 found:
1105 TRACE(file, "found %s = %s\n",
1106 full_name.long_name, full_name.short_name );
1107 lstrcpyn32A( ofs->szPathName, full_name.short_name,
1108 sizeof(ofs->szPathName) );
1110 fileInUse = FILE_InUse(full_name.long_name,&oldMode);
1111 if(fileInUse)
1113 TRACE(file, "found another instance with mode 0x%02x\n",oldMode&0x70);
1114 if (FILE_ShareDeny(mode,oldMode)) return HFILE_ERROR32;
1117 if (mode & OF_SHARE_EXCLUSIVE)
1118 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
1119 on the file <tempdir>/_ins0432._mp to determine how
1120 far installation has proceeded.
1121 _ins0432._mp is an executable and while running the
1122 application expects the open with OF_SHARE_ to fail*/
1123 /* Probable FIXME:
1124 As our loader closes the files after loading the executable,
1125 we can't find the running executable with FILE_InUse.
1126 Perhaps the loader should keep the file open.
1127 Recheck against how Win handles that case */
1129 char *last = strrchr(full_name.long_name,'/');
1130 if (!last)
1131 last = full_name.long_name - 1;
1132 if (GetModuleHandle16(last+1))
1134 TRACE(file,"Denying shared open for %s\n",full_name.long_name);
1135 return HFILE_ERROR32;
1139 if (mode & OF_DELETE)
1141 if (unlink( full_name.long_name ) == -1) goto not_found;
1142 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
1143 return 1;
1146 unixMode=FILE_DOSToUnixMode(mode);
1148 hFileRet = FILE_OpenUnixFile( full_name.long_name, unixMode );
1149 if (hFileRet == HFILE_ERROR32) goto not_found;
1150 /* we need to save the mode, but only if it is not in use yet*/
1151 if( (!fileInUse) &&(file =FILE_GetFile(hFileRet)))
1153 file->mode=mode;
1154 FILE_ReleaseFile(file);
1157 GetFileTime( hFileRet, NULL, NULL, &filetime );
1158 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
1159 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
1161 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
1163 CloseHandle( hFileRet );
1164 WARN(file, "(%s): OF_VERIFY failed\n", name );
1165 /* FIXME: what error here? */
1166 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1167 goto error;
1170 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
1172 success: /* We get here if the open was successful */
1173 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
1174 if (mode & OF_EXIST) /* Return the handle, but close it first */
1175 CloseHandle( hFileRet );
1176 return hFileRet;
1178 not_found: /* We get here if the file does not exist */
1179 WARN(file, "'%s' not found\n", name );
1180 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1181 /* fall through */
1183 error: /* We get here if there was an error opening the file */
1184 ofs->nErrCode = DOS_ExtendedError;
1185 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
1186 name,ofs->nErrCode );
1187 return HFILE_ERROR32;
1191 /***********************************************************************
1192 * OpenFile16 (KERNEL.74)
1194 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
1196 TRACE(file,"OpenFile16(%s,%i)\n", name, mode);
1197 return HFILE32_TO_HFILE16(FILE_DoOpenFile( name, ofs, mode, FALSE ));
1201 /***********************************************************************
1202 * OpenFile32 (KERNEL32.396)
1204 HFILE32 WINAPI OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
1206 return FILE_DoOpenFile( name, ofs, mode, TRUE );
1210 /***********************************************************************
1211 * _lclose16 (KERNEL.81)
1213 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1215 TRACE(file, "handle %d\n", hFile );
1216 return CloseHandle( HFILE16_TO_HFILE32( hFile ) ) ? 0 : HFILE_ERROR16;
1220 /***********************************************************************
1221 * _lclose32 (KERNEL32.592)
1223 HFILE32 WINAPI _lclose32( HFILE32 hFile )
1225 TRACE(file, "handle %d\n", hFile );
1226 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
1230 /***********************************************************************
1231 * WIN16_hread
1233 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1235 LONG maxlen;
1237 TRACE(file, "%d %08lx %ld\n",
1238 hFile, (DWORD)buffer, count );
1240 /* Some programs pass a count larger than the allocated buffer */
1241 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1242 if (count > maxlen) count = maxlen;
1243 return _lread32(HFILE16_TO_HFILE32(hFile), PTR_SEG_TO_LIN(buffer), count );
1247 /***********************************************************************
1248 * WIN16_lread
1250 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1252 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1256 /***********************************************************************
1257 * _lread32 (KERNEL32.596)
1259 UINT32 WINAPI _lread32( HFILE32 handle, LPVOID buffer, UINT32 count )
1261 K32OBJ *ptr;
1262 DWORD numWritten;
1263 BOOL32 result = FALSE;
1265 TRACE( file, "%d %p %d\n", handle, buffer, count);
1266 if (!(ptr = HANDLE_GetObjPtr( PROCESS_Current(), handle,
1267 K32OBJ_UNKNOWN, 0, NULL))) return -1;
1268 if (K32OBJ_OPS(ptr)->read)
1269 result = K32OBJ_OPS(ptr)->read(ptr, buffer, count, &numWritten, NULL);
1270 K32OBJ_DecCount( ptr );
1271 if (!result) return -1;
1272 return (UINT32)numWritten;
1276 /***********************************************************************
1277 * _lread16 (KERNEL.82)
1279 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1281 return (UINT16)_lread32(HFILE16_TO_HFILE32(hFile), buffer, (LONG)count );
1285 /***********************************************************************
1286 * _lcreat16 (KERNEL.83)
1288 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1290 int mode = (attr & 1) ? 0444 : 0666;
1291 TRACE(file, "%s %02x\n", path, attr );
1292 return (HFILE16) HFILE32_TO_HFILE16(FILE_Create( path, mode, FALSE ));
1296 /***********************************************************************
1297 * _lcreat32 (KERNEL32.593)
1299 HFILE32 WINAPI _lcreat32( LPCSTR path, INT32 attr )
1301 int mode = (attr & 1) ? 0444 : 0666;
1302 TRACE(file, "%s %02x\n", path, attr );
1303 return FILE_Create( path, mode, FALSE );
1307 /***********************************************************************
1308 * _lcreat_uniq (Not a Windows API)
1310 HFILE32 _lcreat_uniq( LPCSTR path, INT32 attr )
1312 int mode = (attr & 1) ? 0444 : 0666;
1313 TRACE(file, "%s %02x\n", path, attr );
1314 return FILE_Create( path, mode, TRUE );
1318 /***********************************************************************
1319 * SetFilePointer (KERNEL32.492)
1321 DWORD WINAPI SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
1322 DWORD method )
1324 FILE_OBJECT *file;
1325 DWORD result = 0xffffffff;
1327 if (highword && *highword)
1329 FIXME(file, "64-bit offsets not supported yet\n");
1330 SetLastError( ERROR_INVALID_PARAMETER );
1331 return 0xffffffff;
1333 TRACE(file, "handle %d offset %ld origin %ld\n",
1334 hFile, distance, method );
1336 if (!(file = FILE_GetFile( hFile ))) return 0xffffffff;
1339 /* the pointer may be positioned before the start of the file;
1340 no error is returned in that case,
1341 but subsequent attempts at I/O will produce errors.
1342 This is not allowed with Unix lseek(),
1343 so we'll need some emulation here */
1344 switch(method)
1346 case FILE_CURRENT:
1347 distance += file->pos; /* fall through */
1348 case FILE_BEGIN:
1349 if ((result = lseek(file->unix_handle, distance, SEEK_SET)) == -1)
1351 if ((INT32)distance < 0)
1352 file->pos = result = distance;
1354 else
1355 file->pos = result;
1356 break;
1357 case FILE_END:
1358 if ((result = lseek(file->unix_handle, distance, SEEK_END)) == -1)
1360 if ((INT32)distance < 0)
1362 /* get EOF */
1363 result = lseek(file->unix_handle, 0, SEEK_END);
1365 /* return to the old pos, as the first lseek failed */
1366 lseek(file->unix_handle, file->pos, SEEK_END);
1368 file->pos = (result += distance);
1370 else
1371 ERR(file, "lseek: unknown error. Please report.\n");
1373 else file->pos = result;
1374 break;
1375 default:
1376 ERR(file, "Unknown origin %ld !\n", method);
1379 if (result == -1)
1380 FILE_SetDosError();
1382 FILE_ReleaseFile( file );
1383 return result;
1387 /***********************************************************************
1388 * _llseek16 (KERNEL.84)
1390 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1392 return SetFilePointer( HFILE16_TO_HFILE32(hFile), lOffset, NULL, nOrigin );
1396 /***********************************************************************
1397 * _llseek32 (KERNEL32.594)
1399 LONG WINAPI _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
1401 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1405 /***********************************************************************
1406 * _lopen16 (KERNEL.85)
1408 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1410 return HFILE32_TO_HFILE16(_lopen32( path, mode ));
1414 /***********************************************************************
1415 * _lopen32 (KERNEL32.595)
1417 HFILE32 WINAPI _lopen32( LPCSTR path, INT32 mode )
1419 INT32 unixMode;
1421 TRACE(file, "('%s',%04x)\n", path, mode );
1423 unixMode= FILE_DOSToUnixMode(mode);
1424 return FILE_Open( path, unixMode , (mode & 0x70));
1428 /***********************************************************************
1429 * _lwrite16 (KERNEL.86)
1431 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1433 return (UINT16)_hwrite32( HFILE16_TO_HFILE32(hFile), buffer, (LONG)count );
1436 /***********************************************************************
1437 * _lwrite32 (KERNEL.86)
1439 UINT32 WINAPI _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
1441 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
1445 /***********************************************************************
1446 * _hread16 (KERNEL.349)
1448 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1450 return _lread32( HFILE16_TO_HFILE32(hFile), buffer, count );
1454 /***********************************************************************
1455 * _hread32 (KERNEL32.590)
1457 LONG WINAPI _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
1459 return _lread32( hFile, buffer, count );
1463 /***********************************************************************
1464 * _hwrite16 (KERNEL.350)
1466 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1468 return _hwrite32( HFILE16_TO_HFILE32(hFile), buffer, count );
1472 /***********************************************************************
1473 * _hwrite32 (KERNEL32.591)
1475 * experimenation yields that _lwrite:
1476 * o truncates the file at the current position with
1477 * a 0 len write
1478 * o returns 0 on a 0 length write
1479 * o works with console handles
1482 LONG WINAPI _hwrite32( HFILE32 handle, LPCSTR buffer, LONG count )
1484 K32OBJ *ioptr;
1485 DWORD result;
1486 BOOL32 status = FALSE;
1488 TRACE(file, "%d %p %ld\n", handle, buffer, count );
1490 if (count == 0) { /* Expand or truncate at current position */
1491 FILE_OBJECT *file = FILE_GetFile(handle);
1493 if ( ftruncate(file->unix_handle,
1494 lseek( file->unix_handle, 0, SEEK_CUR)) == 0 ) {
1495 FILE_ReleaseFile(file);
1496 return 0;
1497 } else {
1498 FILE_SetDosError();
1499 FILE_ReleaseFile(file);
1500 return HFILE_ERROR32;
1504 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), handle,
1505 K32OBJ_UNKNOWN, 0, NULL )))
1506 return HFILE_ERROR32;
1507 if (K32OBJ_OPS(ioptr)->write)
1508 status = K32OBJ_OPS(ioptr)->write(ioptr, buffer, count, &result, NULL);
1509 K32OBJ_DecCount( ioptr );
1510 if (!status) result = HFILE_ERROR32;
1511 return result;
1515 /***********************************************************************
1516 * SetHandleCount16 (KERNEL.199)
1518 UINT16 WINAPI SetHandleCount16( UINT16 count )
1520 HGLOBAL16 hPDB = GetCurrentPDB();
1521 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1522 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1524 TRACE(file, "(%d)\n", count );
1526 if (count < 20) count = 20; /* No point in going below 20 */
1527 else if (count > 254) count = 254;
1529 if (count == 20)
1531 if (pdb->nbFiles > 20)
1533 memcpy( pdb->fileHandles, files, 20 );
1534 GlobalFree16( pdb->hFileHandles );
1535 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1536 GlobalHandleToSel( hPDB ) );
1537 pdb->hFileHandles = 0;
1538 pdb->nbFiles = 20;
1541 else /* More than 20, need a new file handles table */
1543 BYTE *newfiles;
1544 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1545 if (!newhandle)
1547 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1548 return pdb->nbFiles;
1550 newfiles = (BYTE *)GlobalLock16( newhandle );
1552 if (count > pdb->nbFiles)
1554 memcpy( newfiles, files, pdb->nbFiles );
1555 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1557 else memcpy( newfiles, files, count );
1558 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1559 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1560 pdb->hFileHandles = newhandle;
1561 pdb->nbFiles = count;
1563 return pdb->nbFiles;
1567 /*************************************************************************
1568 * SetHandleCount32 (KERNEL32.494)
1570 UINT32 WINAPI SetHandleCount32( UINT32 count )
1572 return MIN( 256, count );
1576 /***********************************************************************
1577 * FlushFileBuffers (KERNEL32.133)
1579 BOOL32 WINAPI FlushFileBuffers( HFILE32 hFile )
1581 FILE_OBJECT *file;
1582 BOOL32 ret;
1584 TRACE(file, "(%d)\n", hFile );
1585 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1586 if (fsync( file->unix_handle ) != -1) ret = TRUE;
1587 else
1589 FILE_SetDosError();
1590 ret = FALSE;
1592 FILE_ReleaseFile( file );
1593 return ret;
1597 /**************************************************************************
1598 * SetEndOfFile (KERNEL32.483)
1600 BOOL32 WINAPI SetEndOfFile( HFILE32 hFile )
1602 FILE_OBJECT *file;
1603 BOOL32 ret = TRUE;
1605 TRACE(file, "(%d)\n", hFile );
1606 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1607 if (ftruncate( file->unix_handle,
1608 lseek( file->unix_handle, 0, SEEK_CUR ) ))
1610 FILE_SetDosError();
1611 ret = FALSE;
1613 FILE_ReleaseFile( file );
1614 return ret;
1618 /***********************************************************************
1619 * DeleteFile16 (KERNEL.146)
1621 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1623 return DeleteFile32A( path );
1627 /***********************************************************************
1628 * DeleteFile32A (KERNEL32.71)
1630 BOOL32 WINAPI DeleteFile32A( LPCSTR path )
1632 DOS_FULL_NAME full_name;
1634 TRACE(file, "'%s'\n", path );
1636 if (DOSFS_GetDevice( path ))
1638 WARN(file, "cannot remove DOS device '%s'!\n", path);
1639 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1640 return FALSE;
1643 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1644 if (unlink( full_name.long_name ) == -1)
1646 FILE_SetDosError();
1647 return FALSE;
1649 return TRUE;
1653 /***********************************************************************
1654 * DeleteFile32W (KERNEL32.72)
1656 BOOL32 WINAPI DeleteFile32W( LPCWSTR path )
1658 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1659 BOOL32 ret = DeleteFile32A( xpath );
1660 HeapFree( GetProcessHeap(), 0, xpath );
1661 return ret;
1665 /***********************************************************************
1666 * FILE_SetFileType
1668 BOOL32 FILE_SetFileType( HFILE32 hFile, DWORD type )
1670 FILE_OBJECT *file = FILE_GetFile( hFile );
1671 if (!file) return FALSE;
1672 file->type = type;
1673 FILE_ReleaseFile( file );
1674 return TRUE;
1678 /***********************************************************************
1679 * FILE_mmap
1681 LPVOID FILE_mmap( HFILE32 hFile, LPVOID start,
1682 DWORD size_high, DWORD size_low,
1683 DWORD offset_high, DWORD offset_low,
1684 int prot, int flags )
1686 LPVOID ret;
1687 FILE_OBJECT *file = FILE_GetFile( hFile );
1688 if (!file) return (LPVOID)-1;
1689 ret = FILE_dommap( file, start, size_high, size_low,
1690 offset_high, offset_low, prot, flags );
1691 FILE_ReleaseFile( file );
1692 return ret;
1696 /***********************************************************************
1697 * FILE_dommap
1699 LPVOID FILE_dommap( FILE_OBJECT *file, LPVOID start,
1700 DWORD size_high, DWORD size_low,
1701 DWORD offset_high, DWORD offset_low,
1702 int prot, int flags )
1704 int fd = -1;
1705 int pos;
1706 LPVOID ret;
1708 if (size_high || offset_high)
1709 FIXME(file, "offsets larger than 4Gb not supported\n");
1711 if (!file)
1713 #ifdef MAP_ANON
1714 flags |= MAP_ANON;
1715 #else
1716 static int fdzero = -1;
1718 if (fdzero == -1)
1720 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1722 perror( "/dev/zero: open" );
1723 exit(1);
1726 fd = fdzero;
1727 #endif /* MAP_ANON */
1728 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1729 #ifdef MAP_SHARED
1730 flags &= ~MAP_SHARED;
1731 #endif
1732 #ifdef MAP_PRIVATE
1733 flags |= MAP_PRIVATE;
1734 #endif
1736 else fd = file->unix_handle;
1738 if ((ret = mmap( start, size_low, prot,
1739 flags, fd, offset_low )) != (LPVOID)-1)
1740 return ret;
1742 /* mmap() failed; if this is because the file offset is not */
1743 /* page-aligned (EINVAL), or because the underlying filesystem */
1744 /* does not support mmap() (ENOEXEC), we do it by hand. */
1746 if (!file) return ret;
1747 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1748 if (prot & PROT_WRITE)
1750 /* We cannot fake shared write mappings */
1751 #ifdef MAP_SHARED
1752 if (flags & MAP_SHARED) return ret;
1753 #endif
1754 #ifdef MAP_PRIVATE
1755 if (!(flags & MAP_PRIVATE)) return ret;
1756 #endif
1758 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1759 /* Reserve the memory with an anonymous mmap */
1760 ret = FILE_dommap( NULL, start, size_high, size_low, 0, 0,
1761 PROT_READ | PROT_WRITE, flags );
1762 if (ret == (LPVOID)-1) return ret;
1763 /* Now read in the file */
1764 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1766 FILE_munmap( ret, size_high, size_low );
1767 return (LPVOID)-1;
1769 read( fd, ret, size_low );
1770 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1771 mprotect( ret, size_low, prot ); /* Set the right protection */
1772 return ret;
1776 /***********************************************************************
1777 * FILE_munmap
1779 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1781 if (size_high)
1782 FIXME(file, "offsets larger than 4Gb not supported\n");
1783 return munmap( start, size_low );
1787 /***********************************************************************
1788 * GetFileType (KERNEL32.222)
1790 DWORD WINAPI GetFileType( HFILE32 hFile )
1792 FILE_OBJECT *file = FILE_GetFile(hFile);
1793 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1794 FILE_ReleaseFile( file );
1795 return file->type;
1799 /**************************************************************************
1800 * MoveFileEx32A (KERNEL32.???)
1802 BOOL32 WINAPI MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1804 DOS_FULL_NAME full_name1, full_name2;
1805 int mode=0; /* mode == 1: use copy */
1807 TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag);
1809 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1810 if (fn2) { /* !fn2 means delete fn1 */
1811 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1812 /* Source name and target path are valid */
1813 if ( full_name1.drive != full_name2.drive)
1814 /* use copy, if allowed */
1815 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1816 /* FIXME: Use right error code */
1817 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
1818 return FALSE;
1820 else mode =1;
1821 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1822 /* target exists, check if we may overwrite */
1823 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1824 /* FIXME: Use right error code */
1825 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1826 return FALSE;
1829 else /* fn2 == NULL means delete source */
1830 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1831 if (flag & MOVEFILE_COPY_ALLOWED) {
1832 WARN(file, "Illegal flag\n");
1833 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1834 EL_Unknown );
1835 return FALSE;
1837 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1838 Perhaps we should queue these command and execute it
1839 when exiting... What about using on_exit(2)
1841 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1842 full_name1.long_name);
1843 return TRUE;
1845 else if (unlink( full_name1.long_name ) == -1)
1847 FILE_SetDosError();
1848 return FALSE;
1850 else return TRUE; /* successfully deleted */
1852 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1853 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1854 Perhaps we should queue these command and execute it
1855 when exiting... What about using on_exit(2)
1857 FIXME(file,"Please move existing file '%s' to file '%s'"
1858 "when Wine has finished\n",
1859 full_name1.long_name, full_name2.long_name);
1860 return TRUE;
1863 if (!mode) /* move the file */
1864 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1866 FILE_SetDosError();
1867 return FALSE;
1869 else return TRUE;
1870 else /* copy File */
1871 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1875 /**************************************************************************
1876 * MoveFileEx32W (KERNEL32.???)
1878 BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1880 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1881 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1882 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1883 HeapFree( GetProcessHeap(), 0, afn1 );
1884 HeapFree( GetProcessHeap(), 0, afn2 );
1885 return res;
1889 /**************************************************************************
1890 * MoveFile32A (KERNEL32.387)
1892 * Move file or directory
1894 BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1896 DOS_FULL_NAME full_name1, full_name2;
1897 struct stat fstat;
1899 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1901 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1902 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1903 /* The new name must not already exist */
1904 return FALSE;
1905 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1907 if (full_name1.drive == full_name2.drive) /* move */
1908 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1910 FILE_SetDosError();
1911 return FALSE;
1913 else return TRUE;
1914 else /*copy */ {
1915 if (stat( full_name1.long_name, &fstat ))
1917 WARN(file, "Invalid source file %s\n",
1918 full_name1.long_name);
1919 FILE_SetDosError();
1920 return FALSE;
1922 if (S_ISDIR(fstat.st_mode)) {
1923 /* No Move for directories across file systems */
1924 /* FIXME: Use right error code */
1925 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1926 EL_Unknown );
1927 return FALSE;
1929 else
1930 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1935 /**************************************************************************
1936 * MoveFile32W (KERNEL32.390)
1938 BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1940 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1941 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1942 BOOL32 res = MoveFile32A( afn1, afn2 );
1943 HeapFree( GetProcessHeap(), 0, afn1 );
1944 HeapFree( GetProcessHeap(), 0, afn2 );
1945 return res;
1949 /**************************************************************************
1950 * CopyFile32A (KERNEL32.36)
1952 BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1954 HFILE32 h1, h2;
1955 BY_HANDLE_FILE_INFORMATION info;
1956 UINT32 count;
1957 BOOL32 ret = FALSE;
1958 int mode;
1959 char buffer[2048];
1961 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1962 if (!GetFileInformationByHandle( h1, &info ))
1964 CloseHandle( h1 );
1965 return FALSE;
1967 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1968 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1970 CloseHandle( h1 );
1971 return FALSE;
1973 while ((count = _lread32( h1, buffer, sizeof(buffer) )) > 0)
1975 char *p = buffer;
1976 while (count > 0)
1978 INT32 res = _lwrite32( h2, p, count );
1979 if (res <= 0) goto done;
1980 p += res;
1981 count -= res;
1984 ret = TRUE;
1985 done:
1986 CloseHandle( h1 );
1987 CloseHandle( h2 );
1988 return ret;
1992 /**************************************************************************
1993 * CopyFile32W (KERNEL32.37)
1995 BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
1997 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1998 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1999 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
2000 HeapFree( GetProcessHeap(), 0, sourceA );
2001 HeapFree( GetProcessHeap(), 0, destA );
2002 return ret;
2006 /***********************************************************************
2007 * SetFileTime (KERNEL32.650)
2009 BOOL32 WINAPI SetFileTime( HFILE32 hFile,
2010 const FILETIME *lpCreationTime,
2011 const FILETIME *lpLastAccessTime,
2012 const FILETIME *lpLastWriteTime )
2014 FILE_OBJECT *file = FILE_GetFile(hFile);
2015 struct utimbuf utimbuf;
2017 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
2018 TRACE(file,"('%s',%p,%p,%p)\n",
2019 file->unix_name,
2020 lpCreationTime,
2021 lpLastAccessTime,
2022 lpLastWriteTime
2024 if (lpLastAccessTime)
2025 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
2026 else
2027 utimbuf.actime = 0; /* FIXME */
2028 if (lpLastWriteTime)
2029 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
2030 else
2031 utimbuf.modtime = 0; /* FIXME */
2032 if (-1==utime(file->unix_name,&utimbuf))
2034 MSG("Couldn't set the time for file '%s'. Insufficient permissions !?\n", file->unix_name);
2035 FILE_ReleaseFile( file );
2036 FILE_SetDosError();
2037 return FALSE;
2039 FILE_ReleaseFile( file );
2040 return TRUE;
2043 /* Locks need to be mirrored because unix file locking is based
2044 * on the pid. Inside of wine there can be multiple WINE processes
2045 * that share the same unix pid.
2046 * Read's and writes should check these locks also - not sure
2047 * how critical that is at this point (FIXME).
2050 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2052 DOS_FILE_LOCK *curr;
2053 DWORD processId;
2055 processId = GetCurrentProcessId();
2057 /* check if lock overlaps a current lock for the same file */
2058 for (curr = locks; curr; curr = curr->next) {
2059 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2060 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2061 return TRUE;/* region is identic */
2062 if ((f->l_start < (curr->base + curr->len)) &&
2063 ((f->l_start + f->l_len) > curr->base)) {
2064 /* region overlaps */
2065 return FALSE;
2070 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2071 curr->processId = GetCurrentProcessId();
2072 curr->base = f->l_start;
2073 curr->len = f->l_len;
2074 curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);
2075 curr->next = locks;
2076 curr->dos_file = file;
2077 locks = curr;
2078 return TRUE;
2081 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2083 DWORD processId;
2084 DOS_FILE_LOCK **curr;
2085 DOS_FILE_LOCK *rem;
2087 processId = GetCurrentProcessId();
2088 curr = &locks;
2089 while (*curr) {
2090 if ((*curr)->dos_file == file) {
2091 rem = *curr;
2092 *curr = (*curr)->next;
2093 HeapFree( SystemHeap, 0, rem->unix_name );
2094 HeapFree( SystemHeap, 0, rem );
2096 else
2097 curr = &(*curr)->next;
2101 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2103 DWORD processId;
2104 DOS_FILE_LOCK **curr;
2105 DOS_FILE_LOCK *rem;
2107 processId = GetCurrentProcessId();
2108 for (curr = &locks; *curr; curr = &(*curr)->next) {
2109 if ((*curr)->processId == processId &&
2110 (*curr)->dos_file == file &&
2111 (*curr)->base == f->l_start &&
2112 (*curr)->len == f->l_len) {
2113 /* this is the same lock */
2114 rem = *curr;
2115 *curr = (*curr)->next;
2116 HeapFree( SystemHeap, 0, rem->unix_name );
2117 HeapFree( SystemHeap, 0, rem );
2118 return TRUE;
2121 /* no matching lock found */
2122 return FALSE;
2126 /**************************************************************************
2127 * LockFile (KERNEL32.511)
2129 BOOL32 WINAPI LockFile(
2130 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2131 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2133 struct flock f;
2134 FILE_OBJECT *file;
2136 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2137 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2138 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2140 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2141 FIXME(file, "Unimplemented bytes > 32bits\n");
2142 return FALSE;
2145 f.l_start = dwFileOffsetLow;
2146 f.l_len = nNumberOfBytesToLockLow;
2147 f.l_whence = SEEK_SET;
2148 f.l_pid = 0;
2149 f.l_type = F_WRLCK;
2151 if (!(file = FILE_GetFile(hFile))) return FALSE;
2153 /* shadow locks internally */
2154 if (!DOS_AddLock(file, &f)) {
2155 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
2156 return FALSE;
2159 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2160 #ifdef USE_UNIX_LOCKS
2161 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2162 if (errno == EACCES || errno == EAGAIN) {
2163 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
2165 else {
2166 FILE_SetDosError();
2168 /* remove our internal copy of the lock */
2169 DOS_RemoveLock(file, &f);
2170 return FALSE;
2172 #endif
2173 return TRUE;
2177 /**************************************************************************
2178 * UnlockFile (KERNEL32.703)
2180 BOOL32 WINAPI UnlockFile(
2181 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2182 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2184 FILE_OBJECT *file;
2185 struct flock f;
2187 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2188 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2189 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2191 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2192 WARN(file, "Unimplemented bytes > 32bits\n");
2193 return FALSE;
2196 f.l_start = dwFileOffsetLow;
2197 f.l_len = nNumberOfBytesToUnlockLow;
2198 f.l_whence = SEEK_SET;
2199 f.l_pid = 0;
2200 f.l_type = F_UNLCK;
2202 if (!(file = FILE_GetFile(hFile))) return FALSE;
2204 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2206 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2207 #ifdef USE_UNIX_LOCKS
2208 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2209 FILE_SetDosError();
2210 return FALSE;
2212 #endif
2213 return TRUE;
2216 /**************************************************************************
2217 * GetFileAttributesEx32A [KERNEL32.874]
2219 BOOL32 WINAPI GetFileAttributesEx32A(
2220 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2221 LPVOID lpFileInformation)
2223 DOS_FULL_NAME full_name;
2224 BY_HANDLE_FILE_INFORMATION info;
2226 if (lpFileName == NULL) return FALSE;
2227 if (lpFileInformation == NULL) return FALSE;
2229 if (fInfoLevelId == GetFileExInfoStandard) {
2230 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2231 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2232 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2233 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2235 lpFad->dwFileAttributes = info.dwFileAttributes;
2236 lpFad->ftCreationTime = info.ftCreationTime;
2237 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2238 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2239 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2240 lpFad->nFileSizeLow = info.nFileSizeLow;
2242 else {
2243 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
2244 return FALSE;
2247 return TRUE;
2251 /**************************************************************************
2252 * GetFileAttributesEx32W [KERNEL32.875]
2254 BOOL32 WINAPI GetFileAttributesEx32W(
2255 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2256 LPVOID lpFileInformation)
2258 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2259 BOOL32 res =
2260 GetFileAttributesEx32A( nameA, fInfoLevelId, lpFileInformation);
2261 HeapFree( GetProcessHeap(), 0, nameA );
2262 return res;