(CoGetClassObject) one missing CALLBACK, extremely simplified clsid
[wine/multimedia.git] / files / file.c
blobe4afe2100a95a81c803051ae1b4d4f64be294d07
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)
1815 /* use copy, if allowed */
1816 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1817 /* FIXME: Use right error code */
1818 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
1819 return FALSE;
1821 else mode =1;
1823 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1824 /* target exists, check if we may overwrite */
1825 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1826 /* FIXME: Use right error code */
1827 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1828 return FALSE;
1831 else /* fn2 == NULL means delete source */
1832 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1833 if (flag & MOVEFILE_COPY_ALLOWED) {
1834 WARN(file, "Illegal flag\n");
1835 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1836 EL_Unknown );
1837 return FALSE;
1839 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1840 Perhaps we should queue these command and execute it
1841 when exiting... What about using on_exit(2)
1843 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1844 full_name1.long_name);
1845 return TRUE;
1847 else if (unlink( full_name1.long_name ) == -1)
1849 FILE_SetDosError();
1850 return FALSE;
1852 else return TRUE; /* successfully deleted */
1854 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1855 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1856 Perhaps we should queue these command and execute it
1857 when exiting... What about using on_exit(2)
1859 FIXME(file,"Please move existing file '%s' to file '%s'"
1860 "when Wine has finished\n",
1861 full_name1.long_name, full_name2.long_name);
1862 return TRUE;
1865 if (!mode) /* move the file */
1866 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1868 FILE_SetDosError();
1869 return FALSE;
1871 else return TRUE;
1872 else /* copy File */
1873 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1877 /**************************************************************************
1878 * MoveFileEx32W (KERNEL32.???)
1880 BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1882 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1883 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1884 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1885 HeapFree( GetProcessHeap(), 0, afn1 );
1886 HeapFree( GetProcessHeap(), 0, afn2 );
1887 return res;
1891 /**************************************************************************
1892 * MoveFile32A (KERNEL32.387)
1894 * Move file or directory
1896 BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1898 DOS_FULL_NAME full_name1, full_name2;
1899 struct stat fstat;
1901 TRACE(file, "(%s,%s)\n", fn1, fn2 );
1903 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1904 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1905 /* The new name must not already exist */
1906 return FALSE;
1907 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1909 if (full_name1.drive == full_name2.drive) /* move */
1910 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1912 FILE_SetDosError();
1913 return FALSE;
1915 else return TRUE;
1916 else /*copy */ {
1917 if (stat( full_name1.long_name, &fstat ))
1919 WARN(file, "Invalid source file %s\n",
1920 full_name1.long_name);
1921 FILE_SetDosError();
1922 return FALSE;
1924 if (S_ISDIR(fstat.st_mode)) {
1925 /* No Move for directories across file systems */
1926 /* FIXME: Use right error code */
1927 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1928 EL_Unknown );
1929 return FALSE;
1931 else
1932 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1937 /**************************************************************************
1938 * MoveFile32W (KERNEL32.390)
1940 BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1942 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1943 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1944 BOOL32 res = MoveFile32A( afn1, afn2 );
1945 HeapFree( GetProcessHeap(), 0, afn1 );
1946 HeapFree( GetProcessHeap(), 0, afn2 );
1947 return res;
1951 /**************************************************************************
1952 * CopyFile32A (KERNEL32.36)
1954 BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1956 HFILE32 h1, h2;
1957 BY_HANDLE_FILE_INFORMATION info;
1958 UINT32 count;
1959 BOOL32 ret = FALSE;
1960 int mode;
1961 char buffer[2048];
1963 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1964 if (!GetFileInformationByHandle( h1, &info ))
1966 CloseHandle( h1 );
1967 return FALSE;
1969 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1970 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1972 CloseHandle( h1 );
1973 return FALSE;
1975 while ((count = _lread32( h1, buffer, sizeof(buffer) )) > 0)
1977 char *p = buffer;
1978 while (count > 0)
1980 INT32 res = _lwrite32( h2, p, count );
1981 if (res <= 0) goto done;
1982 p += res;
1983 count -= res;
1986 ret = TRUE;
1987 done:
1988 CloseHandle( h1 );
1989 CloseHandle( h2 );
1990 return ret;
1994 /**************************************************************************
1995 * CopyFile32W (KERNEL32.37)
1997 BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
1999 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
2000 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
2001 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
2002 HeapFree( GetProcessHeap(), 0, sourceA );
2003 HeapFree( GetProcessHeap(), 0, destA );
2004 return ret;
2008 /***********************************************************************
2009 * SetFileTime (KERNEL32.650)
2011 BOOL32 WINAPI SetFileTime( HFILE32 hFile,
2012 const FILETIME *lpCreationTime,
2013 const FILETIME *lpLastAccessTime,
2014 const FILETIME *lpLastWriteTime )
2016 FILE_OBJECT *file = FILE_GetFile(hFile);
2017 struct utimbuf utimbuf;
2019 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
2020 TRACE(file,"('%s',%p,%p,%p)\n",
2021 file->unix_name,
2022 lpCreationTime,
2023 lpLastAccessTime,
2024 lpLastWriteTime
2026 if (lpLastAccessTime)
2027 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
2028 else
2029 utimbuf.actime = 0; /* FIXME */
2030 if (lpLastWriteTime)
2031 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
2032 else
2033 utimbuf.modtime = 0; /* FIXME */
2034 if (-1==utime(file->unix_name,&utimbuf))
2036 MSG("Couldn't set the time for file '%s'. Insufficient permissions !?\n", file->unix_name);
2037 FILE_ReleaseFile( file );
2038 FILE_SetDosError();
2039 return FALSE;
2041 FILE_ReleaseFile( file );
2042 return TRUE;
2045 /* Locks need to be mirrored because unix file locking is based
2046 * on the pid. Inside of wine there can be multiple WINE processes
2047 * that share the same unix pid.
2048 * Read's and writes should check these locks also - not sure
2049 * how critical that is at this point (FIXME).
2052 static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2054 DOS_FILE_LOCK *curr;
2055 DWORD processId;
2057 processId = GetCurrentProcessId();
2059 /* check if lock overlaps a current lock for the same file */
2060 for (curr = locks; curr; curr = curr->next) {
2061 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2062 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2063 return TRUE;/* region is identic */
2064 if ((f->l_start < (curr->base + curr->len)) &&
2065 ((f->l_start + f->l_len) > curr->base)) {
2066 /* region overlaps */
2067 return FALSE;
2072 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2073 curr->processId = GetCurrentProcessId();
2074 curr->base = f->l_start;
2075 curr->len = f->l_len;
2076 curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);
2077 curr->next = locks;
2078 curr->dos_file = file;
2079 locks = curr;
2080 return TRUE;
2083 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2085 DWORD processId;
2086 DOS_FILE_LOCK **curr;
2087 DOS_FILE_LOCK *rem;
2089 processId = GetCurrentProcessId();
2090 curr = &locks;
2091 while (*curr) {
2092 if ((*curr)->dos_file == file) {
2093 rem = *curr;
2094 *curr = (*curr)->next;
2095 HeapFree( SystemHeap, 0, rem->unix_name );
2096 HeapFree( SystemHeap, 0, rem );
2098 else
2099 curr = &(*curr)->next;
2103 static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2105 DWORD processId;
2106 DOS_FILE_LOCK **curr;
2107 DOS_FILE_LOCK *rem;
2109 processId = GetCurrentProcessId();
2110 for (curr = &locks; *curr; curr = &(*curr)->next) {
2111 if ((*curr)->processId == processId &&
2112 (*curr)->dos_file == file &&
2113 (*curr)->base == f->l_start &&
2114 (*curr)->len == f->l_len) {
2115 /* this is the same lock */
2116 rem = *curr;
2117 *curr = (*curr)->next;
2118 HeapFree( SystemHeap, 0, rem->unix_name );
2119 HeapFree( SystemHeap, 0, rem );
2120 return TRUE;
2123 /* no matching lock found */
2124 return FALSE;
2128 /**************************************************************************
2129 * LockFile (KERNEL32.511)
2131 BOOL32 WINAPI LockFile(
2132 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2133 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2135 struct flock f;
2136 FILE_OBJECT *file;
2138 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2139 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2140 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2142 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2143 FIXME(file, "Unimplemented bytes > 32bits\n");
2144 return FALSE;
2147 f.l_start = dwFileOffsetLow;
2148 f.l_len = nNumberOfBytesToLockLow;
2149 f.l_whence = SEEK_SET;
2150 f.l_pid = 0;
2151 f.l_type = F_WRLCK;
2153 if (!(file = FILE_GetFile(hFile))) return FALSE;
2155 /* shadow locks internally */
2156 if (!DOS_AddLock(file, &f)) {
2157 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
2158 return FALSE;
2161 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2162 #ifdef USE_UNIX_LOCKS
2163 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2164 if (errno == EACCES || errno == EAGAIN) {
2165 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
2167 else {
2168 FILE_SetDosError();
2170 /* remove our internal copy of the lock */
2171 DOS_RemoveLock(file, &f);
2172 return FALSE;
2174 #endif
2175 return TRUE;
2179 /**************************************************************************
2180 * UnlockFile (KERNEL32.703)
2182 BOOL32 WINAPI UnlockFile(
2183 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2184 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2186 FILE_OBJECT *file;
2187 struct flock f;
2189 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2190 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2191 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2193 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2194 WARN(file, "Unimplemented bytes > 32bits\n");
2195 return FALSE;
2198 f.l_start = dwFileOffsetLow;
2199 f.l_len = nNumberOfBytesToUnlockLow;
2200 f.l_whence = SEEK_SET;
2201 f.l_pid = 0;
2202 f.l_type = F_UNLCK;
2204 if (!(file = FILE_GetFile(hFile))) return FALSE;
2206 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2208 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2209 #ifdef USE_UNIX_LOCKS
2210 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2211 FILE_SetDosError();
2212 return FALSE;
2214 #endif
2215 return TRUE;
2218 /**************************************************************************
2219 * GetFileAttributesEx32A [KERNEL32.874]
2221 BOOL32 WINAPI GetFileAttributesEx32A(
2222 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2223 LPVOID lpFileInformation)
2225 DOS_FULL_NAME full_name;
2226 BY_HANDLE_FILE_INFORMATION info;
2228 if (lpFileName == NULL) return FALSE;
2229 if (lpFileInformation == NULL) return FALSE;
2231 if (fInfoLevelId == GetFileExInfoStandard) {
2232 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2233 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2234 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2235 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2237 lpFad->dwFileAttributes = info.dwFileAttributes;
2238 lpFad->ftCreationTime = info.ftCreationTime;
2239 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2240 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2241 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2242 lpFad->nFileSizeLow = info.nFileSizeLow;
2244 else {
2245 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
2246 return FALSE;
2249 return TRUE;
2253 /**************************************************************************
2254 * GetFileAttributesEx32W [KERNEL32.875]
2256 BOOL32 WINAPI GetFileAttributesEx32W(
2257 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2258 LPVOID lpFileInformation)
2260 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2261 BOOL32 res =
2262 GetFileAttributesEx32A( nameA, fInfoLevelId, lpFileInformation);
2263 HeapFree( GetProcessHeap(), 0, nameA );
2264 return res;