Release 970415
[wine/multimedia.git] / files / file.c
blob47e02e8160256ef4414024cbb49a18a9fb3338c7
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 <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/errno.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/mman.h>
19 #include <time.h>
20 #include <unistd.h>
21 #include <utime.h>
23 #include "windows.h"
24 #include "winerror.h"
25 #include "drive.h"
26 #include "file.h"
27 #include "global.h"
28 #include "heap.h"
29 #include "msdos.h"
30 #include "options.h"
31 #include "ldt.h"
32 #include "process.h"
33 #include "task.h"
34 #include "stddebug.h"
35 #include "debug.h"
37 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
38 #define MAP_ANON MAP_ANONYMOUS
39 #endif
42 /***********************************************************************
43 * FILE_Alloc
45 * Allocate a file.
47 static HFILE32 FILE_Alloc( FILE_OBJECT **file )
49 HFILE32 handle;
50 *file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) );
51 if (!*file)
53 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
54 return NULL;
56 (*file)->header.type = K32OBJ_FILE;
57 (*file)->header.refcount = 0;
58 (*file)->unix_handle = -1;
59 (*file)->unix_name = NULL;
60 (*file)->type = FILE_TYPE_DISK;
62 handle = PROCESS_AllocHandle( &(*file)->header, 0 );
63 if (handle == INVALID_HANDLE_VALUE32) *file = NULL;
64 return handle;
68 /***********************************************************************
69 * FILE_Destroy
71 * Destroy a DOS file.
73 void FILE_Destroy( K32OBJ *ptr )
75 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
76 assert( ptr->type == K32OBJ_FILE );
78 if (file->unix_handle != -1) close( file->unix_handle );
79 if (file->unix_name) HeapFree( SystemHeap, 0, file->unix_name );
80 ptr->type = K32OBJ_UNKNOWN;
81 HeapFree( SystemHeap, 0, file );
85 /***********************************************************************
86 * FILE_GetFile
88 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
89 * be called to release the file.
91 static FILE_OBJECT *FILE_GetFile( HFILE32 handle )
93 return (FILE_OBJECT *)PROCESS_GetObjPtr( handle, K32OBJ_FILE );
97 /***********************************************************************
98 * FILE_ReleaseFile
100 * Release a DOS file obtained with FILE_GetFile.
102 static void FILE_ReleaseFile( FILE_OBJECT *file )
104 K32OBJ_DecCount( &file->header );
108 /***********************************************************************
109 * FILE_GetUnixHandle
111 * Return the Unix handle associated to a file handle.
113 int FILE_GetUnixHandle( HFILE32 hFile )
115 FILE_OBJECT *file;
116 int ret;
118 if (!(file = FILE_GetFile( hFile ))) return -1;
119 ret = file->unix_handle;
120 FILE_ReleaseFile( file );
121 return ret;
125 /***********************************************************************
126 * FILE_SetDosError
128 * Set the DOS error code from errno.
130 void FILE_SetDosError(void)
132 dprintf_file(stddeb, "FILE_SetDosError: errno = %d\n", errno );
133 switch (errno)
135 case EAGAIN:
136 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
137 break;
138 case EBADF:
139 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
140 break;
141 case ENOSPC:
142 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
143 break;
144 case EACCES:
145 case EPERM:
146 case EROFS:
147 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
148 break;
149 case EBUSY:
150 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
151 break;
152 case ENOENT:
153 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
154 break;
155 case EISDIR:
156 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
157 break;
158 case ENFILE:
159 case EMFILE:
160 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
161 break;
162 case EEXIST:
163 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
164 break;
165 default:
166 perror( "int21: unknown errno" );
167 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
168 break;
173 /***********************************************************************
174 * FILE_DupUnixHandle
176 * Duplicate a Unix handle into a task handle.
178 HFILE32 FILE_DupUnixHandle( int fd )
180 HFILE32 handle;
181 FILE_OBJECT *file;
183 if ((handle = FILE_Alloc( &file )) != INVALID_HANDLE_VALUE32)
185 if ((file->unix_handle = dup(fd)) == -1)
187 FILE_SetDosError();
188 CloseHandle( handle );
189 return INVALID_HANDLE_VALUE32;
192 return handle;
196 /***********************************************************************
197 * FILE_OpenUnixFile
199 static HFILE32 FILE_OpenUnixFile( const char *name, int mode )
201 HFILE32 handle;
202 FILE_OBJECT *file;
203 struct stat st;
205 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
206 return INVALID_HANDLE_VALUE32;
208 if ((file->unix_handle = open( name, mode, 0666 )) == -1)
210 if (!Options.failReadOnly && (mode == O_RDWR))
211 file->unix_handle = open( name, O_RDONLY );
213 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
215 FILE_SetDosError();
216 CloseHandle( handle );
217 return INVALID_HANDLE_VALUE32;
219 if (S_ISDIR(st.st_mode))
221 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
222 CloseHandle( handle );
223 return INVALID_HANDLE_VALUE32;
226 /* File opened OK, now fill the FILE_OBJECT */
228 file->unix_name = HEAP_strdupA( SystemHeap, 0, name );
229 return handle;
233 /***********************************************************************
234 * FILE_Open
236 HFILE32 FILE_Open( LPCSTR path, INT32 mode )
238 DOS_FULL_NAME full_name;
239 const char *unixName;
241 dprintf_file(stddeb, "FILE_Open: '%s' %04x\n", path, mode );
242 if ((unixName = DOSFS_IsDevice( path )) != NULL)
244 dprintf_file( stddeb, "FILE_Open: opening device '%s'\n", unixName );
245 if (!unixName[0]) /* Non-existing device */
247 dprintf_file(stddeb, "FILE_Open: Non-existing device\n");
248 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
249 return HFILE_ERROR32;
252 else /* check for filename, don't check for last entry if creating */
254 if (!DOSFS_GetFullName( path, !(mode & O_CREAT), &full_name ))
255 return HFILE_ERROR32;
256 unixName = full_name.long_name;
258 return FILE_OpenUnixFile( unixName, mode );
262 /***********************************************************************
263 * FILE_Create
265 static HFILE32 FILE_Create( LPCSTR path, int mode, int unique )
267 HFILE32 handle;
268 FILE_OBJECT *file;
269 const char *unixName;
270 DOS_FULL_NAME full_name;
272 dprintf_file(stddeb, "FILE_Create: '%s' %04x %d\n", path, mode, unique );
274 if ((unixName = DOSFS_IsDevice( path )) != NULL)
276 dprintf_file(stddeb, "FILE_Create: creating device '%s'!\n", unixName);
277 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
278 return INVALID_HANDLE_VALUE32;
281 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
282 return INVALID_HANDLE_VALUE32;
284 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
286 CloseHandle( handle );
287 return INVALID_HANDLE_VALUE32;
289 if ((file->unix_handle = open( full_name.long_name,
290 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
291 mode )) == -1)
293 FILE_SetDosError();
294 CloseHandle( handle );
295 return INVALID_HANDLE_VALUE32;
298 /* File created OK, now fill the FILE_OBJECT */
300 file->unix_name = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
301 return handle;
305 /***********************************************************************
306 * FILE_FillInfo
308 * Fill a file information from a struct stat.
310 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
312 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
313 if (S_ISDIR(st->st_mode))
314 info->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
315 if (!(st->st_mode & S_IWUSR))
316 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
318 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
319 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
320 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
322 info->dwVolumeSerialNumber = 0; /* FIXME */
323 info->nFileSizeHigh = 0;
324 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
325 info->nNumberOfLinks = st->st_nlink;
326 info->nFileIndexHigh = 0;
327 info->nFileIndexLow = st->st_ino;
331 /***********************************************************************
332 * FILE_Stat
334 * Stat a Unix path name. Return TRUE if OK.
336 BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
338 struct stat st;
340 if (stat( unixName, &st ) == -1)
342 FILE_SetDosError();
343 return FALSE;
345 FILE_FillInfo( &st, info );
346 return TRUE;
350 /***********************************************************************
351 * GetFileInformationByHandle (KERNEL32.219)
353 DWORD GetFileInformationByHandle( HFILE32 hFile,
354 BY_HANDLE_FILE_INFORMATION *info )
356 FILE_OBJECT *file;
357 DWORD ret = 0;
358 struct stat st;
360 if (!(file = FILE_GetFile( hFile ))) return 0;
361 if (fstat( file->unix_handle, &st ) == -1) FILE_SetDosError();
362 else
364 FILE_FillInfo( &st, info );
365 ret = 1;
367 FILE_ReleaseFile( file );
368 return ret;
372 /**************************************************************************
373 * GetFileAttributes16 (KERNEL.420)
375 DWORD GetFileAttributes16( LPCSTR name )
377 return GetFileAttributes32A( name );
381 /**************************************************************************
382 * GetFileAttributes32A (KERNEL32.217)
384 DWORD GetFileAttributes32A( LPCSTR name )
386 DOS_FULL_NAME full_name;
387 BY_HANDLE_FILE_INFORMATION info;
389 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
390 if (!FILE_Stat( full_name.long_name, &info )) return -1;
391 return info.dwFileAttributes;
395 /**************************************************************************
396 * GetFileAttributes32W (KERNEL32.218)
398 DWORD GetFileAttributes32W( LPCWSTR name )
400 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
401 DWORD res = GetFileAttributes32A( nameA );
402 HeapFree( GetProcessHeap(), 0, nameA );
403 return res;
407 /***********************************************************************
408 * GetFileSize (KERNEL32.220)
410 DWORD GetFileSize( HFILE32 hFile, LPDWORD filesizehigh )
412 BY_HANDLE_FILE_INFORMATION info;
413 if (!GetFileInformationByHandle( hFile, &info )) return 0;
414 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
415 return info.nFileSizeLow;
419 /***********************************************************************
420 * GetFileTime (KERNEL32.221)
422 BOOL32 GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
423 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
425 BY_HANDLE_FILE_INFORMATION info;
426 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
427 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
428 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
429 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
430 return TRUE;
433 /***********************************************************************
434 * CompareFileTime (KERNEL32.28)
436 INT32 CompareFileTime( LPFILETIME x, LPFILETIME y )
438 if (x->dwHighDateTime > y->dwHighDateTime)
439 return 1;
440 if (x->dwHighDateTime < y->dwHighDateTime)
441 return -1;
442 if (x->dwLowDateTime > y->dwLowDateTime)
443 return 1;
444 if (x->dwLowDateTime < y->dwLowDateTime)
445 return -1;
446 return 0;
449 /***********************************************************************
450 * FILE_Dup
452 * dup() function for DOS handles.
454 HFILE32 FILE_Dup( HFILE32 hFile )
456 FILE_OBJECT *file;
457 HFILE32 handle;
459 dprintf_file( stddeb, "FILE_Dup for handle %d\n", hFile );
460 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR32;
461 handle = PROCESS_AllocHandle( &file->header, 0 );
462 FILE_ReleaseFile( file );
463 dprintf_file( stddeb, "FILE_Dup return handle %d\n", handle );
464 return handle;
468 /***********************************************************************
469 * FILE_Dup2
471 * dup2() function for DOS handles.
473 HFILE32 FILE_Dup2( HFILE32 hFile1, HFILE32 hFile2 )
475 FILE_OBJECT *file;
477 dprintf_file( stddeb, "FILE_Dup2 for handle %d\n", hFile1 );
478 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR32;
479 if (!PROCESS_SetObjPtr( hFile2, &file->header, 0 )) hFile2 = HFILE_ERROR32;
480 FILE_ReleaseFile( file );
481 return hFile2;
485 /***********************************************************************
486 * GetTempFileName16 (KERNEL.97)
488 UINT16 GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
489 LPSTR buffer )
491 char temppath[144];
493 if ((drive & TF_FORCEDRIVE) &&
494 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
496 drive &= ~TF_FORCEDRIVE;
497 fprintf( stderr, "Warning: GetTempFileName: invalid drive %d specified\n",
498 drive );
501 if (drive & TF_FORCEDRIVE)
502 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
503 else
505 GetTempPath32A( 132, temppath );
506 strcat( temppath, "\\" );
508 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
512 /***********************************************************************
513 * GetTempFileName32A (KERNEL32.290)
515 UINT32 GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
516 LPSTR buffer)
518 DOS_FULL_NAME full_name;
519 int i;
520 LPSTR p;
521 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
523 if (!path) return 0;
524 strcpy( buffer, path );
525 p = buffer + strlen(buffer);
526 /* add a \, if there isn't one ... */
527 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
528 *p++ = '~';
529 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
530 sprintf( p, "%04x.tmp", num );
532 /* Now try to create it */
534 if (!unique)
538 HFILE32 handle = FILE_Create( buffer, 0666, TRUE );
539 if (handle != INVALID_HANDLE_VALUE32)
540 { /* We created it */
541 dprintf_file( stddeb, "GetTempFileName32A: created %s\n",
542 buffer);
543 CloseHandle( handle );
544 break;
546 if (DOS_ExtendedError != ER_FileExists)
547 break; /* No need to go on */
548 num++;
549 sprintf( p, "%04x.tmp", num );
550 } while (num != (unique & 0xffff));
553 /* Get the full path name */
555 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
557 /* Check if we have write access in the directory */
558 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
559 if (access( full_name.long_name, W_OK ) == -1)
560 fprintf( stderr,
561 "Warning: GetTempFileName returns '%s', which doesn't seem to be writeable.\n"
562 "Please check your configuration file if this generates a failure.\n",
563 buffer);
565 dprintf_file( stddeb, "GetTempFileName32A: returning %s\n", buffer );
566 return unique ? unique : num;
570 /***********************************************************************
571 * GetTempFileName32W (KERNEL32.291)
573 UINT32 GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
574 LPWSTR buffer )
576 LPSTR patha,prefixa;
577 char buffera[144];
578 UINT32 ret;
580 if (!path) return 0;
581 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
582 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
583 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
584 lstrcpyAtoW( buffer, buffera );
585 HeapFree( GetProcessHeap(), 0, patha );
586 HeapFree( GetProcessHeap(), 0, prefixa );
587 return ret;
591 /***********************************************************************
592 * FILE_DoOpenFile
594 * Implementation of OpenFile16() and OpenFile32().
596 static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
597 BOOL32 win32 )
599 HFILE32 hFileRet;
600 FILETIME filetime;
601 WORD filedatetime[2];
602 DOS_FULL_NAME full_name;
603 char *p;
604 int unixMode;
606 ofs->cBytes = sizeof(OFSTRUCT);
607 ofs->nErrCode = 0;
608 if (mode & OF_REOPEN) name = ofs->szPathName;
609 dprintf_file( stddeb, "OpenFile: %s %04x\n", name, mode );
611 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
612 Are there any cases where getting the path here is wrong?
613 Uwe Bonnes 1997 Apr 2 */
614 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
615 ofs->szPathName, NULL )) goto error;
617 /* OF_PARSE simply fills the structure */
619 if (mode & OF_PARSE)
621 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
622 != DRIVE_REMOVABLE);
623 dprintf_file( stddeb, "OpenFile(%s): OF_PARSE, res = '%s'\n",
624 name, ofs->szPathName );
625 return 0;
628 /* OF_CREATE is completely different from all other options, so
629 handle it first */
631 if (mode & OF_CREATE)
633 if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32)
634 goto error;
635 goto success;
638 /* If OF_SEARCH is set, ignore the given path */
640 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
642 /* First try the file name as is */
643 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
644 /* Now remove the path */
645 if (name[0] && (name[1] == ':')) name += 2;
646 if ((p = strrchr( name, '\\' ))) name = p + 1;
647 if ((p = strrchr( name, '/' ))) name = p + 1;
648 if (!name[0]) goto not_found;
651 /* Now look for the file */
653 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
655 found:
656 dprintf_file( stddeb, "OpenFile: found %s = %s\n",
657 full_name.long_name, full_name.short_name );
658 lstrcpyn32A( ofs->szPathName, full_name.short_name,
659 sizeof(ofs->szPathName) );
661 if (mode & OF_DELETE)
663 if (unlink( full_name.long_name ) == -1) goto not_found;
664 dprintf_file( stddeb, "OpenFile(%s): OF_DELETE return = OK\n", name);
665 return 1;
668 switch(mode & 3)
670 case OF_WRITE:
671 unixMode = O_WRONLY; break;
672 case OF_READWRITE:
673 unixMode = O_RDWR; break;
674 case OF_READ:
675 default:
676 unixMode = O_RDONLY; break;
679 hFileRet = FILE_OpenUnixFile( full_name.long_name, unixMode );
680 if (hFileRet == HFILE_ERROR32) goto not_found;
681 GetFileTime( hFileRet, NULL, NULL, &filetime );
682 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
683 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
685 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
687 CloseHandle( hFileRet );
688 dprintf_file( stddeb, "OpenFile(%s): OF_VERIFY failed\n", name );
689 /* FIXME: what error here? */
690 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
691 goto error;
694 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
696 success: /* We get here if the open was successful */
697 dprintf_file( stddeb, "OpenFile(%s): OK, return = %d\n", name, hFileRet );
698 if (mode & OF_EXIST) /* Return the handle, but close it first */
699 CloseHandle( hFileRet );
700 return hFileRet;
702 not_found: /* We get here if the file does not exist */
703 dprintf_file( stddeb, "OpenFile: '%s' not found\n", name );
704 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
705 /* fall through */
707 error: /* We get here if there was an error opening the file */
708 ofs->nErrCode = DOS_ExtendedError;
709 dprintf_file( stddeb, "OpenFile(%s): return = HFILE_ERROR error= %d\n",
710 name,ofs->nErrCode );
711 return HFILE_ERROR32;
715 /***********************************************************************
716 * OpenFile16 (KERNEL.74)
718 HFILE16 OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
720 return FILE_DoOpenFile( name, ofs, mode, FALSE );
724 /***********************************************************************
725 * OpenFile32 (KERNEL32.396)
727 HFILE32 OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
729 return FILE_DoOpenFile( name, ofs, mode, TRUE );
733 /***********************************************************************
734 * _lclose16 (KERNEL.81)
736 HFILE16 _lclose16( HFILE16 hFile )
738 dprintf_file( stddeb, "_lclose16: handle %d\n", hFile );
739 return CloseHandle( hFile ) ? 0 : HFILE_ERROR16;
743 /***********************************************************************
744 * _lclose32 (KERNEL32.592)
746 HFILE32 _lclose32( HFILE32 hFile )
748 dprintf_file( stddeb, "_lclose32: handle %d\n", hFile );
749 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
753 /***********************************************************************
754 * WIN16_hread
756 LONG WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
758 LONG maxlen;
760 dprintf_file( stddeb, "WIN16_hread: %d %08lx %ld\n",
761 hFile, (DWORD)buffer, count );
763 /* Some programs pass a count larger than the allocated buffer */
764 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
765 if (count > maxlen) count = maxlen;
766 return _lread32( hFile, PTR_SEG_TO_LIN(buffer), count );
770 /***********************************************************************
771 * WIN16_lread
773 UINT16 WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
775 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
779 /***********************************************************************
780 * _lread32 (KERNEL32.596)
782 UINT32 _lread32( HFILE32 hFile, LPVOID buffer, UINT32 count )
784 FILE_OBJECT *file;
785 UINT32 result;
787 dprintf_file( stddeb, "_lread32: %d %p %d\n", hFile, buffer, count );
788 if (!(file = FILE_GetFile( hFile ))) return -1;
789 if (!count) result = 0;
790 else if ((result = read( file->unix_handle, buffer, count )) == -1)
791 FILE_SetDosError();
792 FILE_ReleaseFile( file );
793 return result;
797 /***********************************************************************
798 * _lread16 (KERNEL.82)
800 UINT16 _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
802 return (UINT16)_lread32( hFile, buffer, (LONG)count );
806 /***********************************************************************
807 * _lcreat16 (KERNEL.83)
809 HFILE16 _lcreat16( LPCSTR path, INT16 attr )
811 int mode = (attr & 1) ? 0444 : 0666;
812 dprintf_file( stddeb, "_lcreat16: %s %02x\n", path, attr );
813 return (HFILE16)FILE_Create( path, mode, FALSE );
817 /***********************************************************************
818 * _lcreat32 (KERNEL32.593)
820 HFILE32 _lcreat32( LPCSTR path, INT32 attr )
822 int mode = (attr & 1) ? 0444 : 0666;
823 dprintf_file( stddeb, "_lcreat32: %s %02x\n", path, attr );
824 return FILE_Create( path, mode, FALSE );
828 /***********************************************************************
829 * _lcreat_uniq (Not a Windows API)
831 HFILE32 _lcreat_uniq( LPCSTR path, INT32 attr )
833 int mode = (attr & 1) ? 0444 : 0666;
834 dprintf_file( stddeb, "_lcreat_uniq: %s %02x\n", path, attr );
835 return FILE_Create( path, mode, TRUE );
839 /***********************************************************************
840 * SetFilePointer (KERNEL32.492)
842 DWORD SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
843 DWORD method )
845 FILE_OBJECT *file;
846 int origin, result;
848 if (highword && *highword)
850 fprintf( stderr, "SetFilePointer: 64-bit offsets not supported yet\n");
851 SetLastError( ERROR_INVALID_PARAMETER );
852 return 0xffffffff;
854 dprintf_file( stddeb, "SetFilePointer: handle %d offset %ld origin %ld\n",
855 hFile, distance, method );
857 if (!(file = FILE_GetFile( hFile ))) return 0xffffffff;
858 switch(method)
860 case FILE_CURRENT: origin = SEEK_CUR; break;
861 case FILE_END: origin = SEEK_END; break;
862 default: origin = SEEK_SET; break;
865 if ((result = lseek( file->unix_handle, distance, origin )) == -1)
866 FILE_SetDosError();
867 FILE_ReleaseFile( file );
868 return (DWORD)result;
872 /***********************************************************************
873 * _llseek16 (KERNEL.84)
875 LONG _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
877 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
881 /***********************************************************************
882 * _llseek32 (KERNEL32.594)
884 LONG _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
886 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
890 /***********************************************************************
891 * _lopen16 (KERNEL.85)
893 HFILE16 _lopen16( LPCSTR path, INT16 mode )
895 return _lopen32( path, mode );
899 /***********************************************************************
900 * _lopen32 (KERNEL32.595)
902 HFILE32 _lopen32( LPCSTR path, INT32 mode )
904 INT32 unixMode;
906 dprintf_file(stddeb, "_lopen32('%s',%04x)\n", path, mode );
908 switch(mode & 3)
910 case OF_WRITE:
911 unixMode = O_WRONLY | O_TRUNC;
912 break;
913 case OF_READWRITE:
914 unixMode = O_RDWR;
915 break;
916 case OF_READ:
917 default:
918 unixMode = O_RDONLY;
919 break;
921 return FILE_Open( path, unixMode );
925 /***********************************************************************
926 * _lwrite16 (KERNEL.86)
928 UINT16 _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
930 return (UINT16)_hwrite32( hFile, buffer, (LONG)count );
933 /***********************************************************************
934 * _lwrite32 (KERNEL.86)
936 UINT32 _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
938 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
942 /***********************************************************************
943 * _hread16 (KERNEL.349)
945 LONG _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
947 return _lread32( hFile, buffer, count );
951 /***********************************************************************
952 * _hread32 (KERNEL32.590)
954 LONG _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
956 return _lread32( hFile, buffer, count );
960 /***********************************************************************
961 * _hwrite16 (KERNEL.350)
963 LONG _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
965 return _hwrite32( hFile, buffer, count );
969 /***********************************************************************
970 * _hwrite32 (KERNEL32.591)
972 LONG _hwrite32( HFILE32 hFile, LPCSTR buffer, LONG count )
974 FILE_OBJECT *file;
975 LONG result;
977 dprintf_file( stddeb, "_hwrite32: %d %p %ld\n", hFile, buffer, count );
979 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR32;
980 if (count == 0) /* Expand or truncate at current position */
981 result = ftruncate( file->unix_handle,
982 lseek( file->unix_handle, 0, SEEK_CUR ) );
983 else
984 result = write( file->unix_handle, buffer, count );
986 if (result == -1) FILE_SetDosError();
987 FILE_ReleaseFile( file );
988 return result;
992 /***********************************************************************
993 * SetHandleCount16 (KERNEL.199)
995 UINT16 SetHandleCount16( UINT16 count )
997 HGLOBAL16 hPDB = GetCurrentPDB();
998 PDB *pdb = (PDB *)GlobalLock16( hPDB );
999 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1001 dprintf_file( stddeb, "SetHandleCount16(%d)\n", count );
1003 if (count < 20) count = 20; /* No point in going below 20 */
1004 else if (count > 254) count = 254;
1006 if (count == 20)
1008 if (pdb->nbFiles > 20)
1010 memcpy( pdb->fileHandles, files, 20 );
1011 GlobalFree16( pdb->hFileHandles );
1012 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1013 GlobalHandleToSel( hPDB ) );
1014 pdb->hFileHandles = 0;
1015 pdb->nbFiles = 20;
1018 else /* More than 20, need a new file handles table */
1020 BYTE *newfiles;
1021 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1022 if (!newhandle)
1024 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1025 return pdb->nbFiles;
1027 newfiles = (BYTE *)GlobalLock16( newhandle );
1029 if (count > pdb->nbFiles)
1031 memcpy( newfiles, files, pdb->nbFiles );
1032 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1034 else memcpy( newfiles, files, count );
1035 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1036 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1037 pdb->hFileHandles = newhandle;
1038 pdb->nbFiles = count;
1040 return pdb->nbFiles;
1044 /*************************************************************************
1045 * SetHandleCount32 (KERNEL32.494)
1047 UINT32 SetHandleCount32( UINT32 count )
1049 return MIN( 256, count );
1053 /***********************************************************************
1054 * FlushFileBuffers (KERNEL32.133)
1056 BOOL32 FlushFileBuffers( HFILE32 hFile )
1058 FILE_OBJECT *file;
1059 BOOL32 ret;
1061 dprintf_file( stddeb, "FlushFileBuffers(%d)\n", hFile );
1062 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1063 if (fsync( file->unix_handle ) != -1) ret = TRUE;
1064 else
1066 FILE_SetDosError();
1067 ret = FALSE;
1069 FILE_ReleaseFile( file );
1070 return ret;
1074 /**************************************************************************
1075 * SetEndOfFile (KERNEL32.483)
1077 BOOL32 SetEndOfFile( HFILE32 hFile )
1079 FILE_OBJECT *file;
1080 BOOL32 ret = TRUE;
1082 dprintf_file( stddeb, "SetEndOfFile(%d)\n", hFile );
1083 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1084 if (ftruncate( file->unix_handle,
1085 lseek( file->unix_handle, 0, SEEK_CUR ) ))
1087 FILE_SetDosError();
1088 ret = FALSE;
1090 FILE_ReleaseFile( file );
1091 return ret;
1095 /***********************************************************************
1096 * DeleteFile16 (KERNEL.146)
1098 BOOL16 DeleteFile16( LPCSTR path )
1100 return DeleteFile32A( path );
1104 /***********************************************************************
1105 * DeleteFile32A (KERNEL32.71)
1107 BOOL32 DeleteFile32A( LPCSTR path )
1109 DOS_FULL_NAME full_name;
1110 const char *unixName;
1112 dprintf_file(stddeb, "DeleteFile: '%s'\n", path );
1114 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1116 dprintf_file(stddeb, "DeleteFile: removing device '%s'!\n", unixName);
1117 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1118 return FALSE;
1121 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1122 if (unlink( full_name.long_name ) == -1)
1124 FILE_SetDosError();
1125 return FALSE;
1127 return TRUE;
1131 /***********************************************************************
1132 * DeleteFile32W (KERNEL32.72)
1134 BOOL32 DeleteFile32W( LPCWSTR path )
1136 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1137 BOOL32 ret = RemoveDirectory32A( xpath );
1138 HeapFree( GetProcessHeap(), 0, xpath );
1139 return ret;
1143 /***********************************************************************
1144 * FILE_SetFileType
1146 BOOL32 FILE_SetFileType( HFILE32 hFile, DWORD type )
1148 FILE_OBJECT *file = FILE_GetFile( hFile );
1149 if (!file) return FALSE;
1150 file->type = type;
1151 FILE_ReleaseFile( file );
1152 return TRUE;
1156 /***********************************************************************
1157 * FILE_mmap
1159 LPVOID FILE_mmap( FILE_OBJECT *file, LPVOID start,
1160 DWORD size_high, DWORD size_low,
1161 DWORD offset_high, DWORD offset_low,
1162 int prot, int flags )
1164 int fd = -1;
1166 if (size_high || offset_high)
1167 fprintf( stderr, "FILE_mmap: offsets larger than 4Gb not supported\n");
1169 if (!file)
1171 #ifdef MAP_ANON
1172 flags |= MAP_ANON;
1173 #else
1174 static int fdzero = -1;
1176 if (fdzero == -1)
1178 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1180 perror( "/dev/zero: open" );
1181 exit(1);
1184 fd = fdzero;
1185 #endif /* MAP_ANON */
1187 else fd = file->unix_handle;
1189 return mmap( start, size_low, prot, flags, fd, offset_low );
1193 /***********************************************************************
1194 * GetFileType (KERNEL32.222)
1196 DWORD GetFileType( HFILE32 hFile )
1198 FILE_OBJECT *file = FILE_GetFile(hFile);
1199 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1200 FILE_ReleaseFile( file );
1201 return file->type;
1205 /**************************************************************************
1206 * MoveFile32A (KERNEL32.387)
1208 BOOL32 MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
1210 DOS_FULL_NAME full_name1, full_name2;
1212 dprintf_file( stddeb, "MoveFile32A(%s,%s)\n", fn1, fn2 );
1214 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1215 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1216 /* FIXME: should not replace an existing file */
1217 /* FIXME: should handle renaming across devices */
1218 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1220 FILE_SetDosError();
1221 return FALSE;
1223 return TRUE;
1227 /**************************************************************************
1228 * MoveFile32W (KERNEL32.390)
1230 BOOL32 MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
1232 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1233 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1234 BOOL32 res = MoveFile32A( afn1, afn2 );
1235 HeapFree( GetProcessHeap(), 0, afn1 );
1236 HeapFree( GetProcessHeap(), 0, afn2 );
1237 return res;
1241 /**************************************************************************
1242 * CopyFile32A (KERNEL32.36)
1244 BOOL32 CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
1246 HFILE32 h1, h2;
1247 BY_HANDLE_FILE_INFORMATION info;
1248 UINT32 count;
1249 BOOL32 ret = FALSE;
1250 int mode;
1251 char buffer[2048];
1253 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1254 if (!GetFileInformationByHandle( h1, &info ))
1256 CloseHandle( h1 );
1257 return FALSE;
1259 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1260 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1262 CloseHandle( h1 );
1263 return FALSE;
1265 while ((count = _lread32( h2, buffer, sizeof(buffer) )) > 0)
1267 char *p = buffer;
1268 while (count > 0)
1270 INT32 res = _lwrite32( h2, p, count );
1271 if (res <= 0) goto done;
1272 p += res;
1273 count -= res;
1276 ret = TRUE;
1277 done:
1278 CloseHandle( h1 );
1279 CloseHandle( h2 );
1280 return ret;
1284 /**************************************************************************
1285 * CopyFile32W (KERNEL32.37)
1287 BOOL32 CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists )
1289 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1290 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1291 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1292 HeapFree( GetProcessHeap(), 0, sourceA );
1293 HeapFree( GetProcessHeap(), 0, destA );
1294 return ret;
1298 /***********************************************************************
1299 * SetFileTime (KERNEL32.493)
1301 BOOL32 SetFileTime( HFILE32 hFile,
1302 const FILETIME *lpCreationTime,
1303 const FILETIME *lpLastAccessTime,
1304 const FILETIME *lpLastWriteTime )
1306 FILE_OBJECT *file = FILE_GetFile(hFile);
1307 struct utimbuf utimbuf;
1309 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1310 dprintf_file(stddeb,"SetFileTime(%s,%p,%p,%p)\n",
1311 file->unix_name,
1312 lpCreationTime,
1313 lpLastAccessTime,
1314 lpLastWriteTime
1316 if (lpLastAccessTime)
1317 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1318 else
1319 utimbuf.actime = 0; /* FIXME */
1320 if (lpLastWriteTime)
1321 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1322 else
1323 utimbuf.modtime = 0; /* FIXME */
1324 if (-1==utime(file->unix_name,&utimbuf))
1326 FILE_ReleaseFile( file );
1327 FILE_SetDosError();
1328 return FALSE;
1330 FILE_ReleaseFile( file );
1331 return TRUE;