blob: d0cc8805cedc5d46524036334a92aeb80ccb47f9 [file] [log] [blame]
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001/*
2 * File handling functions
3 *
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
6 */
7
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00008#include <assert.h>
Alexandre Julliardd90840e1996-06-11 16:02:08 +00009#include <ctype.h>
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000010#include <errno.h>
11#include <fcntl.h>
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +000012#include <stdlib.h>
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000013#include <string.h>
14#include <sys/errno.h>
Alexandre Julliard01d63461997-01-20 19:43:45 +000015#include <sys/types.h>
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000016#include <sys/stat.h>
Alexandre Julliard21979011997-03-05 08:22:35 +000017#include <sys/mman.h>
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000018#include <time.h>
19#include <unistd.h>
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +000020#include <utime.h>
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000021
22#include "windows.h"
Alexandre Julliard8bbf8181996-09-13 16:50:47 +000023#include "winerror.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000024#include "drive.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000025#include "file.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000026#include "global.h"
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000027#include "heap.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000028#include "msdos.h"
29#include "options.h"
30#include "ldt.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000031#include "process.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000032#include "task.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000033#include "debug.h"
34
Alexandre Julliard21979011997-03-05 08:22:35 +000035#if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
36#define MAP_ANON MAP_ANONYMOUS
37#endif
Alexandre Julliard0c126c71996-02-18 18:44:41 +000038
Alexandre Julliarda11d7b11998-03-01 20:05:02 +000039static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
40 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped);
41static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars,
42 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped);
Alexandre Julliard02e90081998-01-04 17:49:09 +000043static void FILE_Destroy( K32OBJ *obj );
44
45const K32OBJ_OPS FILE_Ops =
46{
47 /* Object cannot be waited upon (FIXME: for now) */
48 NULL, /* signaled */
49 NULL, /* satisfied */
50 NULL, /* add_wait */
51 NULL, /* remove_wait */
Alexandre Julliarda11d7b11998-03-01 20:05:02 +000052 FILE_Read, /* read */
53 FILE_Write, /* write */
Alexandre Julliard02e90081998-01-04 17:49:09 +000054 FILE_Destroy /* destroy */
55};
56
Alexandre Julliardd37eb361997-07-20 16:23:21 +000057struct 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;
64};
65
66typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
67
68static DOS_FILE_LOCK *locks = NULL;
69static void DOS_RemoveFileLocks(FILE_OBJECT *file);
Alexandre Julliard0c126c71996-02-18 18:44:41 +000070
71/***********************************************************************
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +000072 * FILE_Alloc
Alexandre Julliard0c126c71996-02-18 18:44:41 +000073 *
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000074 * Allocate a file.
Alexandre Julliard0c126c71996-02-18 18:44:41 +000075 */
Alexandre Julliardf90efa91998-06-14 15:24:15 +000076HFILE32 FILE_Alloc( FILE_OBJECT **file )
Alexandre Julliard0c126c71996-02-18 18:44:41 +000077{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000078 HFILE32 handle;
Alexandre Julliard21979011997-03-05 08:22:35 +000079 *file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000080 if (!*file)
Alexandre Julliard0c126c71996-02-18 18:44:41 +000081 {
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +000082 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
Alexandre Julliardf90efa91998-06-14 15:24:15 +000083 return (HFILE32)NULL;
Alexandre Julliard0c126c71996-02-18 18:44:41 +000084 }
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000085 (*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;
Alexandre Julliarda0d77311998-09-13 16:32:00 +000090 (*file)->pos = 0;
Uwe Bonnese6b5e381998-10-18 14:48:31 +000091 (*file)->mode = 0;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000092
Alexandre Julliardc7c217b1998-04-13 12:21:30 +000093 handle = HANDLE_Alloc( PROCESS_Current(), &(*file)->header,
94 FILE_ALL_ACCESS | GENERIC_READ |
Alexandre Julliard767e6f61998-08-09 12:47:43 +000095 GENERIC_WRITE | GENERIC_EXECUTE /*FIXME*/, TRUE, -1 );
Alexandre Julliard02e90081998-01-04 17:49:09 +000096 /* If the allocation failed, the object is already destroyed */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000097 if (handle == INVALID_HANDLE_VALUE32) *file = NULL;
98 return handle;
Alexandre Julliard0c126c71996-02-18 18:44:41 +000099}
100
101
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000102/* FIXME: lpOverlapped is ignored */
103static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
104 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
105{
106 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
107 int result;
108
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000109 TRACE(file, "%p %p %ld\n", ptr, lpBuffer,
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000110 nNumberOfChars);
111
112 if (nNumberOfChars == 0) {
113 *lpNumberOfChars = 0; /* FIXME: does this change */
114 return TRUE;
115 }
116
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000117 if ( (file->pos < 0) || /* workaround, see SetFilePointer */
118 ((result = read(file->unix_handle, lpBuffer, nNumberOfChars)) == -1) )
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000119 {
120 FILE_SetDosError();
121 return FALSE;
122 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000123 file->pos += result;
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000124 *lpNumberOfChars = result;
125 return TRUE;
126}
127
128/**
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
134 */
135static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars,
136 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
137{
138 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
139 int result;
140
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000141 TRACE(file, "%p %p %ld\n", ptr, lpBuffer,
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000142 nNumberOfChars);
143
144 *lpNumberOfChars = 0;
145
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000146 /*
147 * I assume this loop around EAGAIN is here because
148 * win32 doesn't have interrupted system calls
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000149 */
150
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000151 if (file->pos < 0) { /* workaround, see SetFilePointer */
152 FILE_SetDosError();
153 return FALSE;
154 }
155
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000156 for (;;)
157 {
158 result = write(file->unix_handle, lpBuffer, nNumberOfChars);
159 if (result != -1) {
160 *lpNumberOfChars = result;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000161 file->pos += result;
162 return TRUE;
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000163 }
164 if (errno != EINTR) {
165 FILE_SetDosError();
166 return FALSE;
167 }
168 }
169}
170
171
172
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000173/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000174 * FILE_Destroy
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000175 *
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000176 * Destroy a DOS file.
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000177 */
Alexandre Julliard02e90081998-01-04 17:49:09 +0000178static void FILE_Destroy( K32OBJ *ptr )
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000179{
Alexandre Julliard21979011997-03-05 08:22:35 +0000180 FILE_OBJECT *file = (FILE_OBJECT *)ptr;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000181 assert( ptr->type == K32OBJ_FILE );
182
Alexandre Julliardd37eb361997-07-20 16:23:21 +0000183 DOS_RemoveFileLocks(file);
184
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000185 if (file->unix_handle != -1) close( file->unix_handle );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000186 if (file->unix_name) HeapFree( SystemHeap, 0, file->unix_name );
187 ptr->type = K32OBJ_UNKNOWN;
188 HeapFree( SystemHeap, 0, file );
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000189}
190
191
192/***********************************************************************
193 * FILE_GetFile
194 *
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000195 * Return the DOS file associated to a task file handle. FILE_ReleaseFile must
196 * be called to release the file.
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000197 */
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000198FILE_OBJECT *FILE_GetFile( HFILE32 handle )
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000199{
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000200 return (FILE_OBJECT *)HANDLE_GetObjPtr( PROCESS_Current(), handle,
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000201 K32OBJ_FILE, 0 /*FIXME*/, NULL );
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000202}
203
204
205/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000206 * FILE_ReleaseFile
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000207 *
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000208 * Release a DOS file obtained with FILE_GetFile.
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000209 */
Alexandre Julliard829fe321998-07-26 14:27:39 +0000210void FILE_ReleaseFile( FILE_OBJECT *file )
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000211{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000212 K32OBJ_DecCount( &file->header );
213}
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000214
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000215
216/***********************************************************************
217 * FILE_GetUnixHandle
218 *
219 * Return the Unix handle associated to a file handle.
220 */
221int FILE_GetUnixHandle( HFILE32 hFile )
222{
Alexandre Julliard21979011997-03-05 08:22:35 +0000223 FILE_OBJECT *file;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000224 int ret;
225
226 if (!(file = FILE_GetFile( hFile ))) return -1;
227 ret = file->unix_handle;
228 FILE_ReleaseFile( file );
229 return ret;
Alexandre Julliard0c126c71996-02-18 18:44:41 +0000230}
231
Uwe Bonnese6b5e381998-10-18 14:48:31 +0000232/***********************************************************************
233 * FILE_UnixToDosMode
234 *
235 * PARAMS
236 * unixmode[I]
237 * RETURNS
238 * dosmode
239 */
240static int FILE_UnixToDosMode(int unixMode)
241{
242 int dosMode;
243 switch(unixMode & 3)
244 {
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;
255 }
256 return dosMode;
257}
258
259/***********************************************************************
260 * FILE_DOSToUnixMode
261 *
262 * PARAMS
263 * dosMode[I]
264 * RETURNS
265 * unixmode
266 */
267static int FILE_DOSToUnixMode(int dosMode)
268{
269 int unixMode;
270 switch(dosMode & 3)
271 {
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;
279 }
280 return unixMode;
281}
282
283/***********************************************************************
284 * FILE_ShareDeny
285 *
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
292 *
293 * Look what we have to do with the given SHARE modes
294 *
295 * Ralph Brown's interrupt list gives following explication, I guess
296 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
297 *
298 * FIXME: Validate this function
299========from Ralph Brown's list =========
300(Table 0750)
301Values 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
326Legend: 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
334
335 */
336static BOOL32 FILE_ShareDeny( int mode, int oldmode)
337{
338 int oldsharemode = oldmode & 0x70;
339 int sharemode = mode & 0x70;
340 int oldopenmode = oldmode & 3;
341 int openmode = mode & 3;
342
343 switch (oldsharemode)
344 {
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)
354 {
355 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
356 goto fail_error05;
357 }
358 switch (sharemode)
359 {
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;
372 }
373 break;
374 case OF_SHARE_DENY_READ:
375 if (openmode != OF_WRITE)
376 {
377 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
378 goto fail_error05;
379 }
380 switch (sharemode)
381 {
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;
393 }
394 break;
395 case OF_SHARE_DENY_NONE:
396 switch (sharemode)
397 {
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;
409 }
410 default:
411 ERR(file,"unknown mode\n");
412 }
413 ERR(file,"shouldn't happen\n");
414 ERR(file,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
415 return TRUE;
416
417test_ro_int24:
418 FIXME(file,"test if file is RO missing\n");
419 /* Fall through */
420fail_int24:
421 FIXME(file,"generate INT24 missing\n");
422 /* Is this the right error? */
423 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
424 return TRUE;
425
426test_ro_err05:
427 FIXME(file,"test if file is RO missing\n");
428 /* fall through */
429fail_error05:
430 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
431 return TRUE;
432}
433
434
435
436/***********************************************************************
437 *
438 *
439 * Look if the File is in Use For the OF_SHARE_XXX options
440 *
441 * PARAMS
442 * name [I]: full unix name of the file that should be opened
443 * mode [O]: mode how the file was first opened
444 * RETURNS
445 * TRUE if the file was opened before
446 * FALSE if we open the file exclusive for this process
447 *
448 * Scope of the files we look for is only the current pdb
449 * Could we use /proc/self/? on Linux for this?
450 * Should we use flock? Should we create another structure?
451 * Searching through all files seem quite expensive for me, but
452 * I don't see any other way.
453 *
454 * FIXME: Extend scope to the whole Wine process
455 *
456 */
457static BOOL32 FILE_InUse(char * name, int * mode)
458{
459 FILE_OBJECT *file;
460 int i;
461 HGLOBAL16 hPDB = GetCurrentPDB();
462 PDB *pdb = (PDB *)GlobalLock16( hPDB );
463
464 if (!pdb) return 0;
465 for (i=0;i<pdb->nbFiles;i++)
466 {
467 file =FILE_GetFile( (HFILE32) i);
468 if(file)
469 {
470 if(file->unix_name)
471 {
472 TRACE(file,"got %s at %d\n",file->unix_name,i);
473 if(!lstrcmp32A(file->unix_name,name))
474 {
475 *mode = file->mode;
476 FILE_ReleaseFile(file);
477 return TRUE;
478 }
Uwe Bonnese6b5e381998-10-18 14:48:31 +0000479 }
Rein Klazes1518a941998-10-31 12:08:55 +0000480 FILE_ReleaseFile(file);
Uwe Bonnese6b5e381998-10-18 14:48:31 +0000481 }
482 }
483 return FALSE;
484}
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000485
486/***********************************************************************
487 * FILE_SetDosError
488 *
489 * Set the DOS error code from errno.
490 */
491void FILE_SetDosError(void)
492{
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000493 int save_errno = errno; /* errno gets overwritten by printf */
494
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000495 TRACE(file, "errno = %d %s\n", errno, strerror(errno));
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000496 switch (save_errno)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000497 {
498 case EAGAIN:
499 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
500 break;
501 case EBADF:
502 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
503 break;
504 case ENOSPC:
505 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000506 break;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000507 case EACCES:
508 case EPERM:
509 case EROFS:
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000510 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000511 break;
512 case EBUSY:
513 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000514 break;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000515 case ENOENT:
516 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000517 break;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000518 case EISDIR:
519 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
520 break;
521 case ENFILE:
522 case EMFILE:
523 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
524 break;
525 case EEXIST:
526 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000527 break;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000528 case EINVAL:
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000529 case ESPIPE:
Alexandre Julliarde658d821997-11-30 17:45:40 +0000530 DOS_ERROR( ER_SeekError, EC_NotFound, SA_Ignore, EL_Disk );
531 break;
532 case ENOTEMPTY:
533 DOS_ERROR( ERROR_DIR_NOT_EMPTY, EC_Exists, SA_Ignore, EL_Disk );
534 break;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000535 default:
536 perror( "int21: unknown errno" );
537 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
538 break;
539 }
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000540 errno = save_errno;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000541}
542
543
544/***********************************************************************
Alexandre Julliardf1aa3031996-08-05 17:42:43 +0000545 * FILE_DupUnixHandle
546 *
547 * Duplicate a Unix handle into a task handle.
548 */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000549HFILE32 FILE_DupUnixHandle( int fd )
Alexandre Julliardf1aa3031996-08-05 17:42:43 +0000550{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000551 HFILE32 handle;
Alexandre Julliard21979011997-03-05 08:22:35 +0000552 FILE_OBJECT *file;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +0000553
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000554 if ((handle = FILE_Alloc( &file )) != INVALID_HANDLE_VALUE32)
Alexandre Julliardf1aa3031996-08-05 17:42:43 +0000555 {
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000556 if ((file->unix_handle = dup(fd)) == -1)
557 {
558 FILE_SetDosError();
559 CloseHandle( handle );
560 return INVALID_HANDLE_VALUE32;
561 }
Alexandre Julliardf1aa3031996-08-05 17:42:43 +0000562 }
Alexandre Julliardf1aa3031996-08-05 17:42:43 +0000563 return handle;
564}
565
566
567/***********************************************************************
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000568 * FILE_OpenUnixFile
569 */
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000570HFILE32 FILE_OpenUnixFile( const char *name, int mode )
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000571{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000572 HFILE32 handle;
Alexandre Julliard21979011997-03-05 08:22:35 +0000573 FILE_OBJECT *file;
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000574 struct stat st;
575
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000576 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
577 return INVALID_HANDLE_VALUE32;
578
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000579 if ((file->unix_handle = open( name, mode, 0666 )) == -1)
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000580 {
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000581 if (!Options.failReadOnly && (mode == O_RDWR))
582 file->unix_handle = open( name, O_RDONLY );
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000583 }
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000584 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000585 {
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000586 FILE_SetDosError();
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000587 CloseHandle( handle );
588 return INVALID_HANDLE_VALUE32;
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000589 }
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000590 if (S_ISDIR(st.st_mode))
591 {
592 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000593 CloseHandle( handle );
594 return INVALID_HANDLE_VALUE32;
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000595 }
596
Alexandre Julliard21979011997-03-05 08:22:35 +0000597 /* File opened OK, now fill the FILE_OBJECT */
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000598
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000599 file->unix_name = HEAP_strdupA( SystemHeap, 0, name );
600 return handle;
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000601}
602
603
604/***********************************************************************
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000605 * FILE_Open
Uwe Bonnesa370ab41998-10-24 09:16:44 +0000606 *
607 * path[I] name of file to open
608 * mode[I] mode how to open, in unix notation
609 * shareMode[I] the sharing mode in the win OpenFile notation
610 *
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000611 */
Uwe Bonnesa370ab41998-10-24 09:16:44 +0000612HFILE32 FILE_Open( LPCSTR path, INT32 mode, INT32 shareMode )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000613{
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000614 DOS_FULL_NAME full_name;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000615 const char *unixName;
Uwe Bonnese6b5e381998-10-18 14:48:31 +0000616 int oldMode, dosMode; /* FIXME: Do we really need unixmode as argument for
617 FILE_Open */
618 FILE_OBJECT *file;
619 HFILE32 hFileRet;
620 BOOL32 fileInUse = FALSE;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000621
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000622 TRACE(file, "'%s' %04x\n", path, mode );
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000623
624 if (!path) return HFILE_ERROR32;
625
Alexandre Julliard829fe321998-07-26 14:27:39 +0000626 if (DOSFS_GetDevice( path ))
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000627 {
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000628 HFILE32 ret;
629
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000630 TRACE(file, "opening device '%s'\n", path );
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000631
632 if (HFILE_ERROR32!=(ret=DOSFS_OpenDevice( path, mode )))
633 return ret;
634
635 /* Do not silence this please. It is a critical error. -MM */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000636 ERR(file, "Couldn't open device '%s'!\n",path);
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000637 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
638 return HFILE_ERROR32;
639
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000640 }
Alexandre Julliard75d86e11996-11-17 18:59:11 +0000641 else /* check for filename, don't check for last entry if creating */
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000642 {
643 if (!DOSFS_GetFullName( path, !(mode & O_CREAT), &full_name ))
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000644 return HFILE_ERROR32;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000645 unixName = full_name.long_name;
646 }
Uwe Bonnese6b5e381998-10-18 14:48:31 +0000647
Uwe Bonnesa370ab41998-10-24 09:16:44 +0000648 dosMode = FILE_UnixToDosMode(mode)| shareMode;
Uwe Bonnese6b5e381998-10-18 14:48:31 +0000649 fileInUse = FILE_InUse(full_name.long_name,&oldMode);
650 if(fileInUse)
651 {
652 TRACE(file, "found another instance with mode 0x%02x\n",oldMode&0x70);
653 if (FILE_ShareDeny(dosMode,oldMode)) return HFILE_ERROR32;
654 }
655 hFileRet = FILE_OpenUnixFile( unixName, mode );
656 /* we need to save the mode, but only if it is not in use yet*/
657 if ((hFileRet) && (!fileInUse) && ((file =FILE_GetFile(hFileRet))))
658 {
659 file->mode=dosMode;
660 FILE_ReleaseFile(file);
661 }
662 return hFileRet;
663
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000664}
665
666
667/***********************************************************************
668 * FILE_Create
669 */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000670static HFILE32 FILE_Create( LPCSTR path, int mode, int unique )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000671{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000672 HFILE32 handle;
Alexandre Julliard21979011997-03-05 08:22:35 +0000673 FILE_OBJECT *file;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000674 DOS_FULL_NAME full_name;
Uwe Bonnese6b5e381998-10-18 14:48:31 +0000675 BOOL32 fileInUse = FALSE;
676 int oldMode,dosMode; /* FIXME: Do we really need unixmode as argument for
677 FILE_Create */;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000678
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000679 TRACE(file, "'%s' %04x %d\n", path, mode, unique );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000680
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000681 if (!path) return INVALID_HANDLE_VALUE32;
682
Alexandre Julliard829fe321998-07-26 14:27:39 +0000683 if (DOSFS_GetDevice( path ))
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000684 {
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000685 WARN(file, "cannot create DOS device '%s'!\n", path);
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000686 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000687 return INVALID_HANDLE_VALUE32;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000688 }
689
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000690 if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32)
691 return INVALID_HANDLE_VALUE32;
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000692
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000693 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000694 {
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000695 CloseHandle( handle );
696 return INVALID_HANDLE_VALUE32;
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000697 }
Uwe Bonnese6b5e381998-10-18 14:48:31 +0000698
699 dosMode = FILE_UnixToDosMode(mode);
700 fileInUse = FILE_InUse(full_name.long_name,&oldMode);
701 if(fileInUse)
702 {
703 TRACE(file, "found another instance with mode 0x%02x\n",oldMode&0x70);
704 if (FILE_ShareDeny(dosMode,oldMode)) return INVALID_HANDLE_VALUE32;
705 }
706
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000707 if ((file->unix_handle = open( full_name.long_name,
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000708 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
709 mode )) == -1)
710 {
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000711 FILE_SetDosError();
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000712 CloseHandle( handle );
713 return INVALID_HANDLE_VALUE32;
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000714 }
715
Alexandre Julliard21979011997-03-05 08:22:35 +0000716 /* File created OK, now fill the FILE_OBJECT */
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000717
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000718 file->unix_name = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
Uwe Bonnese6b5e381998-10-18 14:48:31 +0000719 file->mode = dosMode;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000720 return handle;
721}
722
723
724/***********************************************************************
725 * FILE_FillInfo
726 *
727 * Fill a file information from a struct stat.
728 */
729static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
730{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000731 if (S_ISDIR(st->st_mode))
Alexandre Julliard84c70f51997-05-09 08:40:27 +0000732 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
733 else
734 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000735 if (!(st->st_mode & S_IWUSR))
736 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000737
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000738 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
739 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
740 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000741
742 info->dwVolumeSerialNumber = 0; /* FIXME */
743 info->nFileSizeHigh = 0;
744 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
745 info->nNumberOfLinks = st->st_nlink;
746 info->nFileIndexHigh = 0;
747 info->nFileIndexLow = st->st_ino;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000748}
749
750
751/***********************************************************************
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000752 * FILE_Stat
753 *
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000754 * Stat a Unix path name. Return TRUE if OK.
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000755 */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000756BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000757{
758 struct stat st;
759
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000760 if (!unixName || !info) return FALSE;
761
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000762 if (stat( unixName, &st ) == -1)
763 {
764 FILE_SetDosError();
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000765 return FALSE;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000766 }
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000767 FILE_FillInfo( &st, info );
768 return TRUE;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000769}
770
771
772/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000773 * GetFileInformationByHandle (KERNEL32.219)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000774 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000775DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile,
776 BY_HANDLE_FILE_INFORMATION *info )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000777{
Alexandre Julliard21979011997-03-05 08:22:35 +0000778 FILE_OBJECT *file;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000779 DWORD ret = 0;
780 struct stat st;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000781
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000782 if (!info) return 0;
783
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000784 if (!(file = FILE_GetFile( hFile ))) return 0;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000785 if (fstat( file->unix_handle, &st ) == -1) FILE_SetDosError();
786 else
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000787 {
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000788 FILE_FillInfo( &st, info );
789 ret = 1;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000790 }
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000791 FILE_ReleaseFile( file );
792 return ret;
793}
794
795
796/**************************************************************************
797 * GetFileAttributes16 (KERNEL.420)
798 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000799DWORD WINAPI GetFileAttributes16( LPCSTR name )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000800{
801 return GetFileAttributes32A( name );
802}
803
804
805/**************************************************************************
806 * GetFileAttributes32A (KERNEL32.217)
807 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000808DWORD WINAPI GetFileAttributes32A( LPCSTR name )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000809{
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000810 DOS_FULL_NAME full_name;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000811 BY_HANDLE_FILE_INFORMATION info;
812
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000813 if (name == NULL || *name=='\0') return -1;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000814
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000815 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
816 if (!FILE_Stat( full_name.long_name, &info )) return -1;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000817 return info.dwFileAttributes;
818}
819
820
821/**************************************************************************
822 * GetFileAttributes32W (KERNEL32.218)
823 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000824DWORD WINAPI GetFileAttributes32W( LPCWSTR name )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000825{
826 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
827 DWORD res = GetFileAttributes32A( nameA );
828 HeapFree( GetProcessHeap(), 0, nameA );
829 return res;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000830}
831
832
833/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000834 * GetFileSize (KERNEL32.220)
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000835 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000836DWORD WINAPI GetFileSize( HFILE32 hFile, LPDWORD filesizehigh )
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000837{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000838 BY_HANDLE_FILE_INFORMATION info;
839 if (!GetFileInformationByHandle( hFile, &info )) return 0;
840 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
841 return info.nFileSizeLow;
842}
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000843
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000844
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000845/***********************************************************************
846 * GetFileTime (KERNEL32.221)
847 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000848BOOL32 WINAPI GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime,
849 FILETIME *lpLastAccessTime,
850 FILETIME *lpLastWriteTime )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000851{
852 BY_HANDLE_FILE_INFORMATION info;
853 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
854 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
855 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
856 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
857 return TRUE;
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000858}
859
Alexandre Julliard349a9531997-02-02 19:01:52 +0000860/***********************************************************************
861 * CompareFileTime (KERNEL32.28)
862 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000863INT32 WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
Alexandre Julliard349a9531997-02-02 19:01:52 +0000864{
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000865 if (!x || !y) return -1;
866
Alexandre Julliard349a9531997-02-02 19:01:52 +0000867 if (x->dwHighDateTime > y->dwHighDateTime)
868 return 1;
869 if (x->dwHighDateTime < y->dwHighDateTime)
870 return -1;
871 if (x->dwLowDateTime > y->dwLowDateTime)
872 return 1;
873 if (x->dwLowDateTime < y->dwLowDateTime)
874 return -1;
875 return 0;
876}
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000877
878/***********************************************************************
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000879 * FILE_Dup
880 *
881 * dup() function for DOS handles.
882 */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000883HFILE32 FILE_Dup( HFILE32 hFile )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000884{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000885 HFILE32 handle;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000886
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000887 TRACE(file, "FILE_Dup for handle %d\n", hFile );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000888 if (!DuplicateHandle( GetCurrentProcess(), hFile, GetCurrentProcess(),
889 &handle, FILE_ALL_ACCESS /* FIXME */, FALSE, 0 ))
890 handle = HFILE_ERROR32;
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000891 TRACE(file, "FILE_Dup return handle %d\n", handle );
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000892 return handle;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000893}
894
895
896/***********************************************************************
897 * FILE_Dup2
898 *
899 * dup2() function for DOS handles.
900 */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000901HFILE32 FILE_Dup2( HFILE32 hFile1, HFILE32 hFile2 )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000902{
Alexandre Julliard21979011997-03-05 08:22:35 +0000903 FILE_OBJECT *file;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000904
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000905 TRACE(file, "FILE_Dup2 for handle %d\n", hFile1 );
Alexandre Julliard03468f71998-02-15 19:40:49 +0000906 /* FIXME: should use DuplicateHandle */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000907 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR32;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000908 if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2, &file->header, 0 ))
909 hFile2 = HFILE_ERROR32;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000910 FILE_ReleaseFile( file );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000911 return hFile2;
912}
913
914
915/***********************************************************************
Alexandre Julliardca22b331996-07-12 19:02:39 +0000916 * GetTempFileName16 (KERNEL.97)
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000917 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000918UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
919 LPSTR buffer )
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000920{
Alexandre Julliardca22b331996-07-12 19:02:39 +0000921 char temppath[144];
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000922
Alexandre Julliarde658d821997-11-30 17:45:40 +0000923 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000924 drive |= DRIVE_GetCurrentDrive() + 'A';
Alexandre Julliarde658d821997-11-30 17:45:40 +0000925
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000926 if ((drive & TF_FORCEDRIVE) &&
927 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
928 {
929 drive &= ~TF_FORCEDRIVE;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000930 WARN(file, "invalid drive %d specified\n", drive );
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000931 }
932
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000933 if (drive & TF_FORCEDRIVE)
Alexandre Julliard02e90081998-01-04 17:49:09 +0000934 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000935 else
Alexandre Julliardca22b331996-07-12 19:02:39 +0000936 GetTempPath32A( 132, temppath );
Alexandre Julliardca22b331996-07-12 19:02:39 +0000937 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
938}
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000939
Alexandre Julliardca22b331996-07-12 19:02:39 +0000940
941/***********************************************************************
942 * GetTempFileName32A (KERNEL32.290)
943 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000944UINT32 WINAPI GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
945 LPSTR buffer)
Alexandre Julliardca22b331996-07-12 19:02:39 +0000946{
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000947 DOS_FULL_NAME full_name;
Alexandre Julliardca22b331996-07-12 19:02:39 +0000948 int i;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000949 LPSTR p;
Alexandre Julliardca22b331996-07-12 19:02:39 +0000950 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
951
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000952 if ( !path || !prefix || !buffer ) return 0;
953
Alexandre Julliardca22b331996-07-12 19:02:39 +0000954 strcpy( buffer, path );
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000955 p = buffer + strlen(buffer);
Alexandre Julliard02e90081998-01-04 17:49:09 +0000956
957 /* add a \, if there isn't one and path is more than just the drive letter ... */
958 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
959 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
960
Alexandre Julliard3051b641996-07-05 17:14:13 +0000961 *p++ = '~';
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000962 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
963 sprintf( p, "%04x.tmp", num );
964
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000965 /* Now try to create it */
966
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000967 if (!unique)
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000968 {
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000969 do
970 {
971 HFILE32 handle = FILE_Create( buffer, 0666, TRUE );
972 if (handle != INVALID_HANDLE_VALUE32)
973 { /* We created it */
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000974 TRACE(file, "created %s\n",
Alexandre Julliard21979011997-03-05 08:22:35 +0000975 buffer);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000976 CloseHandle( handle );
977 break;
978 }
979 if (DOS_ExtendedError != ER_FileExists)
980 break; /* No need to go on */
981 num++;
982 sprintf( p, "%04x.tmp", num );
983 } while (num != (unique & 0xffff));
984 }
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000985
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000986 /* Get the full path name */
987
988 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
989 {
Alexandre Julliard01d63461997-01-20 19:43:45 +0000990 /* Check if we have write access in the directory */
991 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000992 if (access( full_name.long_name, W_OK ) == -1)
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000993 WARN(file, "returns '%s', which doesn't seem to be writeable.\n",
994 buffer);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000995 }
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000996 TRACE(file, "returning %s\n", buffer );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000997 return unique ? unique : num;
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +0000998}
999
1000
1001/***********************************************************************
Alexandre Julliardca22b331996-07-12 19:02:39 +00001002 * GetTempFileName32W (KERNEL32.291)
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001003 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001004UINT32 WINAPI GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
1005 LPWSTR buffer )
Alexandre Julliardca22b331996-07-12 19:02:39 +00001006{
1007 LPSTR patha,prefixa;
1008 char buffera[144];
1009 UINT32 ret;
1010
1011 if (!path) return 0;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001012 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1013 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
1014 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
1015 lstrcpyAtoW( buffer, buffera );
1016 HeapFree( GetProcessHeap(), 0, patha );
1017 HeapFree( GetProcessHeap(), 0, prefixa );
Alexandre Julliardca22b331996-07-12 19:02:39 +00001018 return ret;
1019}
1020
1021
1022/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001023 * FILE_DoOpenFile
1024 *
1025 * Implementation of OpenFile16() and OpenFile32().
Alexandre Julliardca22b331996-07-12 19:02:39 +00001026 */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001027static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode,
1028 BOOL32 win32 )
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001029{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001030 HFILE32 hFileRet;
1031 FILETIME filetime;
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001032 WORD filedatetime[2];
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001033 DOS_FULL_NAME full_name;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001034 char *p;
Uwe Bonnese6b5e381998-10-18 14:48:31 +00001035 int unixMode, oldMode;
1036 FILE_OBJECT *file;
1037 BOOL32 fileInUse = FALSE;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001038
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001039 if (!ofs) return HFILE_ERROR32;
1040
1041
1042 ofs->cBytes = sizeof(OFSTRUCT);
1043 ofs->nErrCode = 0;
1044 if (mode & OF_REOPEN) name = ofs->szPathName;
Alexandre Julliard23946ad1997-06-16 17:43:53 +00001045
1046 if (!name) {
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001047 ERR(file, "called with `name' set to NULL ! Please debug.\n");
Alexandre Julliard23946ad1997-06-16 17:43:53 +00001048 return HFILE_ERROR32;
1049 }
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001050
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001051 TRACE(file, "%s %04x\n", name, mode );
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001052
Alexandre Julliard889f7421997-04-15 17:19:52 +00001053 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
1054 Are there any cases where getting the path here is wrong?
1055 Uwe Bonnes 1997 Apr 2 */
1056 if (!GetFullPathName32A( name, sizeof(ofs->szPathName),
1057 ofs->szPathName, NULL )) goto error;
1058
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001059 /* OF_PARSE simply fills the structure */
1060
1061 if (mode & OF_PARSE)
1062 {
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001063 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
1064 != DRIVE_REMOVABLE);
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001065 TRACE(file, "(%s): OF_PARSE, res = '%s'\n",
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001066 name, ofs->szPathName );
1067 return 0;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001068 }
1069
1070 /* OF_CREATE is completely different from all other options, so
1071 handle it first */
1072
1073 if (mode & OF_CREATE)
1074 {
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001075 if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32)
1076 goto error;
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001077 goto success;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001078 }
1079
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001080 /* If OF_SEARCH is set, ignore the given path */
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001081
1082 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
1083 {
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001084 /* First try the file name as is */
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001085 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001086 /* Now remove the path */
1087 if (name[0] && (name[1] == ':')) name += 2;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001088 if ((p = strrchr( name, '\\' ))) name = p + 1;
1089 if ((p = strrchr( name, '/' ))) name = p + 1;
1090 if (!name[0]) goto not_found;
1091 }
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001092
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001093 /* Now look for the file */
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001094
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001095 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001096
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001097found:
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001098 TRACE(file, "found %s = %s\n",
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001099 full_name.long_name, full_name.short_name );
1100 lstrcpyn32A( ofs->szPathName, full_name.short_name,
1101 sizeof(ofs->szPathName) );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001102
Uwe Bonnese6b5e381998-10-18 14:48:31 +00001103 fileInUse = FILE_InUse(full_name.long_name,&oldMode);
1104 if(fileInUse)
1105 {
1106 TRACE(file, "found another instance with mode 0x%02x\n",oldMode&0x70);
1107 if (FILE_ShareDeny(mode,oldMode)) return HFILE_ERROR32;
1108 }
1109
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001110 if (mode & OF_SHARE_EXCLUSIVE)
Uwe Bonnese6b5e381998-10-18 14:48:31 +00001111 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
1112 on the file <tempdir>/_ins0432._mp to determine how
1113 far installation has proceeded.
1114 _ins0432._mp is an executable and while running the
1115 application expects the open with OF_SHARE_ to fail*/
1116 /* Probable FIXME:
1117 As our loader closes the files after loading the executable,
1118 we can't find the running executable with FILE_InUse.
1119 Perhaps the loader should keep the file open.
1120 Recheck against how Win handles that case */
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001121 {
1122 char *last = strrchr(full_name.long_name,'/');
1123 if (!last)
1124 last = full_name.long_name - 1;
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001125 if (GetModuleHandle16(last+1))
1126 {
1127 TRACE(file,"Denying shared open for %s\n",full_name.long_name);
1128 return HFILE_ERROR32;
1129 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001130 }
1131
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001132 if (mode & OF_DELETE)
1133 {
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001134 if (unlink( full_name.long_name ) == -1) goto not_found;
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001135 TRACE(file, "(%s): OF_DELETE return = OK\n", name);
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001136 return 1;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001137 }
1138
Uwe Bonnese6b5e381998-10-18 14:48:31 +00001139 unixMode=FILE_DOSToUnixMode(mode);
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001140
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001141 hFileRet = FILE_OpenUnixFile( full_name.long_name, unixMode );
1142 if (hFileRet == HFILE_ERROR32) goto not_found;
Uwe Bonnese6b5e381998-10-18 14:48:31 +00001143 /* we need to save the mode, but only if it is not in use yet*/
1144 if( (!fileInUse) &&(file =FILE_GetFile(hFileRet)))
1145 {
1146 file->mode=mode;
1147 FILE_ReleaseFile(file);
1148 }
1149
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001150 GetFileTime( hFileRet, NULL, NULL, &filetime );
1151 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001152 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001153 {
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001154 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001155 {
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001156 CloseHandle( hFileRet );
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001157 WARN(file, "(%s): OF_VERIFY failed\n", name );
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001158 /* FIXME: what error here? */
1159 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1160 goto error;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001161 }
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001162 }
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001163 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001164
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001165success: /* We get here if the open was successful */
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001166 TRACE(file, "(%s): OK, return = %d\n", name, hFileRet );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001167 if (mode & OF_EXIST) /* Return the handle, but close it first */
1168 CloseHandle( hFileRet );
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001169 return hFileRet;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001170
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001171not_found: /* We get here if the file does not exist */
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001172 WARN(file, "'%s' not found\n", name );
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001173 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1174 /* fall through */
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001175
Alexandre Julliardd2e1c1a1996-03-09 16:12:43 +00001176error: /* We get here if there was an error opening the file */
1177 ofs->nErrCode = DOS_ExtendedError;
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001178 WARN(file, "(%s): return = HFILE_ERROR error= %d\n",
Alexandre Julliard889f7421997-04-15 17:19:52 +00001179 name,ofs->nErrCode );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001180 return HFILE_ERROR32;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001181}
1182
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001183
1184/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001185 * OpenFile16 (KERNEL.74)
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001186 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001187HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001188{
Alexandre Julliarda0d77311998-09-13 16:32:00 +00001189 TRACE(file,"OpenFile16(%s,%i)\n", name, mode);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001190 return HFILE32_TO_HFILE16(FILE_DoOpenFile( name, ofs, mode, FALSE ));
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001191}
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001192
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001193
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001194/***********************************************************************
1195 * OpenFile32 (KERNEL32.396)
1196 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001197HFILE32 WINAPI OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001198{
1199 return FILE_DoOpenFile( name, ofs, mode, TRUE );
1200}
1201
1202
1203/***********************************************************************
1204 * _lclose16 (KERNEL.81)
1205 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001206HFILE16 WINAPI _lclose16( HFILE16 hFile )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001207{
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001208 TRACE(file, "handle %d\n", hFile );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001209 return CloseHandle( HFILE16_TO_HFILE32( hFile ) ) ? 0 : HFILE_ERROR16;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001210}
1211
1212
1213/***********************************************************************
1214 * _lclose32 (KERNEL32.592)
1215 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001216HFILE32 WINAPI _lclose32( HFILE32 hFile )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001217{
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001218 TRACE(file, "handle %d\n", hFile );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001219 return CloseHandle( hFile ) ? 0 : HFILE_ERROR32;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001220}
1221
1222
1223/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001224 * WIN16_hread
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001225 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001226LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001227{
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001228 LONG maxlen;
1229
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001230 TRACE(file, "%d %08lx %ld\n",
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001231 hFile, (DWORD)buffer, count );
1232
1233 /* Some programs pass a count larger than the allocated buffer */
1234 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1235 if (count > maxlen) count = maxlen;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001236 return _lread32(HFILE16_TO_HFILE32(hFile), PTR_SEG_TO_LIN(buffer), count );
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001237}
1238
1239
1240/***********************************************************************
1241 * WIN16_lread
1242 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001243UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001244{
1245 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1246}
1247
1248
1249/***********************************************************************
1250 * _lread32 (KERNEL32.596)
1251 */
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00001252UINT32 WINAPI _lread32( HFILE32 handle, LPVOID buffer, UINT32 count )
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001253{
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00001254 K32OBJ *ptr;
1255 DWORD numWritten;
1256 BOOL32 result = FALSE;
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +00001257
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001258 TRACE( file, "%d %p %d\n", handle, buffer, count);
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001259 if (!(ptr = HANDLE_GetObjPtr( PROCESS_Current(), handle,
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001260 K32OBJ_UNKNOWN, 0, NULL))) return -1;
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00001261 if (K32OBJ_OPS(ptr)->read)
1262 result = K32OBJ_OPS(ptr)->read(ptr, buffer, count, &numWritten, NULL);
1263 K32OBJ_DecCount( ptr );
1264 if (!result) return -1;
1265 return (UINT32)numWritten;
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001266}
1267
1268
1269/***********************************************************************
1270 * _lread16 (KERNEL.82)
1271 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001272UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001273{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001274 return (UINT16)_lread32(HFILE16_TO_HFILE32(hFile), buffer, (LONG)count );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001275}
1276
1277
1278/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001279 * _lcreat16 (KERNEL.83)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001280 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001281HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001282{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001283 int mode = (attr & 1) ? 0444 : 0666;
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001284 TRACE(file, "%s %02x\n", path, attr );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001285 return (HFILE16) HFILE32_TO_HFILE16(FILE_Create( path, mode, FALSE ));
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001286}
1287
1288
1289/***********************************************************************
1290 * _lcreat32 (KERNEL32.593)
1291 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001292HFILE32 WINAPI _lcreat32( LPCSTR path, INT32 attr )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001293{
1294 int mode = (attr & 1) ? 0444 : 0666;
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001295 TRACE(file, "%s %02x\n", path, attr );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001296 return FILE_Create( path, mode, FALSE );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001297}
1298
1299
1300/***********************************************************************
1301 * _lcreat_uniq (Not a Windows API)
1302 */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001303HFILE32 _lcreat_uniq( LPCSTR path, INT32 attr )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001304{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001305 int mode = (attr & 1) ? 0444 : 0666;
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001306 TRACE(file, "%s %02x\n", path, attr );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001307 return FILE_Create( path, mode, TRUE );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001308}
1309
1310
1311/***********************************************************************
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001312 * SetFilePointer (KERNEL32.492)
1313 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001314DWORD WINAPI SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword,
1315 DWORD method )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001316{
Alexandre Julliard21979011997-03-05 08:22:35 +00001317 FILE_OBJECT *file;
Alexandre Julliarda0d77311998-09-13 16:32:00 +00001318 DWORD result = 0xffffffff;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001319
1320 if (highword && *highword)
1321 {
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001322 FIXME(file, "64-bit offsets not supported yet\n");
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001323 SetLastError( ERROR_INVALID_PARAMETER );
1324 return 0xffffffff;
1325 }
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001326 TRACE(file, "handle %d offset %ld origin %ld\n",
Alexandre Julliarda0d77311998-09-13 16:32:00 +00001327 hFile, distance, method );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001328
1329 if (!(file = FILE_GetFile( hFile ))) return 0xffffffff;
Alexandre Julliarda0d77311998-09-13 16:32:00 +00001330
1331
1332 /* the pointer may be positioned before the start of the file;
1333 no error is returned in that case,
1334 but subsequent attempts at I/O will produce errors.
1335 This is not allowed with Unix lseek(),
1336 so we'll need some emulation here */
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001337 switch(method)
1338 {
Alexandre Julliarda0d77311998-09-13 16:32:00 +00001339 case FILE_CURRENT:
1340 distance += file->pos; /* fall through */
1341 case FILE_BEGIN:
1342 if ((result = lseek(file->unix_handle, distance, SEEK_SET)) == -1)
1343 {
1344 if ((INT32)distance < 0)
1345 file->pos = result = distance;
1346 }
1347 else
1348 file->pos = result;
1349 break;
1350 case FILE_END:
1351 if ((result = lseek(file->unix_handle, distance, SEEK_END)) == -1)
1352 {
1353 if ((INT32)distance < 0)
1354 {
1355 /* get EOF */
1356 result = lseek(file->unix_handle, 0, SEEK_END);
1357
1358 /* return to the old pos, as the first lseek failed */
1359 lseek(file->unix_handle, file->pos, SEEK_END);
1360
1361 file->pos = (result += distance);
1362 }
1363 else
1364 ERR(file, "lseek: unknown error. Please report.\n");
1365 }
1366 else file->pos = result;
1367 break;
1368 default:
1369 ERR(file, "Unknown origin %ld !\n", method);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001370 }
1371
Alexandre Julliarda0d77311998-09-13 16:32:00 +00001372 if (result == -1)
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001373 FILE_SetDosError();
Alexandre Julliarda0d77311998-09-13 16:32:00 +00001374
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001375 FILE_ReleaseFile( file );
Alexandre Julliarda0d77311998-09-13 16:32:00 +00001376 return result;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001377}
1378
1379
1380/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001381 * _llseek16 (KERNEL.84)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001382 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001383LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001384{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001385 return SetFilePointer( HFILE16_TO_HFILE32(hFile), lOffset, NULL, nOrigin );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001386}
1387
1388
1389/***********************************************************************
1390 * _llseek32 (KERNEL32.594)
1391 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001392LONG WINAPI _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001393{
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001394 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001395}
1396
1397
1398/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001399 * _lopen16 (KERNEL.85)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001400 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001401HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001402{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001403 return HFILE32_TO_HFILE16(_lopen32( path, mode ));
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001404}
1405
1406
1407/***********************************************************************
1408 * _lopen32 (KERNEL32.595)
1409 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001410HFILE32 WINAPI _lopen32( LPCSTR path, INT32 mode )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001411{
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001412 INT32 unixMode;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001413
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001414 TRACE(file, "('%s',%04x)\n", path, mode );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001415
Uwe Bonnese6b5e381998-10-18 14:48:31 +00001416 unixMode= FILE_DOSToUnixMode(mode);
Uwe Bonnesa370ab41998-10-24 09:16:44 +00001417 return FILE_Open( path, unixMode , (mode & 0x70));
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001418}
1419
1420
1421/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001422 * _lwrite16 (KERNEL.86)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001423 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001424UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001425{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001426 return (UINT16)_hwrite32( HFILE16_TO_HFILE32(hFile), buffer, (LONG)count );
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001427}
1428
1429/***********************************************************************
1430 * _lwrite32 (KERNEL.86)
1431 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001432UINT32 WINAPI _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count )
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001433{
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001434 return (UINT32)_hwrite32( hFile, buffer, (LONG)count );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001435}
1436
1437
1438/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001439 * _hread16 (KERNEL.349)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001440 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001441LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001442{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001443 return _lread32( HFILE16_TO_HFILE32(hFile), buffer, count );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001444}
1445
1446
1447/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001448 * _hread32 (KERNEL32.590)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001449 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001450LONG WINAPI _hread32( HFILE32 hFile, LPVOID buffer, LONG count)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001451{
1452 return _lread32( hFile, buffer, count );
1453}
1454
1455
1456/***********************************************************************
1457 * _hwrite16 (KERNEL.350)
1458 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001459LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001460{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001461 return _hwrite32( HFILE16_TO_HFILE32(hFile), buffer, count );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001462}
1463
1464
1465/***********************************************************************
1466 * _hwrite32 (KERNEL32.591)
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00001467 *
1468 * experimenation yields that _lwrite:
1469 * o truncates the file at the current position with
1470 * a 0 len write
1471 * o returns 0 on a 0 length write
1472 * o works with console handles
1473 *
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001474 */
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00001475LONG WINAPI _hwrite32( HFILE32 handle, LPCSTR buffer, LONG count )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001476{
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00001477 K32OBJ *ioptr;
1478 DWORD result;
1479 BOOL32 status = FALSE;
1480
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001481 TRACE(file, "%d %p %ld\n", handle, buffer, count );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001482
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00001483 if (count == 0) { /* Expand or truncate at current position */
1484 FILE_OBJECT *file = FILE_GetFile(handle);
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001485
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00001486 if ( ftruncate(file->unix_handle,
1487 lseek( file->unix_handle, 0, SEEK_CUR)) == 0 ) {
1488 FILE_ReleaseFile(file);
1489 return 0;
1490 } else {
1491 FILE_SetDosError();
1492 FILE_ReleaseFile(file);
1493 return HFILE_ERROR32;
1494 }
1495 }
1496
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001497 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), handle,
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001498 K32OBJ_UNKNOWN, 0, NULL )))
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00001499 return HFILE_ERROR32;
1500 if (K32OBJ_OPS(ioptr)->write)
1501 status = K32OBJ_OPS(ioptr)->write(ioptr, buffer, count, &result, NULL);
1502 K32OBJ_DecCount( ioptr );
1503 if (!status) result = HFILE_ERROR32;
1504 return result;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001505}
1506
1507
1508/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001509 * SetHandleCount16 (KERNEL.199)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001510 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001511UINT16 WINAPI SetHandleCount16( UINT16 count )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001512{
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001513 HGLOBAL16 hPDB = GetCurrentPDB();
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001514 PDB *pdb = (PDB *)GlobalLock16( hPDB );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001515 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001516
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001517 TRACE(file, "(%d)\n", count );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001518
1519 if (count < 20) count = 20; /* No point in going below 20 */
1520 else if (count > 254) count = 254;
1521
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001522 if (count == 20)
1523 {
1524 if (pdb->nbFiles > 20)
1525 {
1526 memcpy( pdb->fileHandles, files, 20 );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001527 GlobalFree16( pdb->hFileHandles );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001528 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1529 GlobalHandleToSel( hPDB ) );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001530 pdb->hFileHandles = 0;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001531 pdb->nbFiles = 20;
1532 }
1533 }
1534 else /* More than 20, need a new file handles table */
1535 {
1536 BYTE *newfiles;
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001537 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001538 if (!newhandle)
1539 {
1540 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1541 return pdb->nbFiles;
1542 }
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001543 newfiles = (BYTE *)GlobalLock16( newhandle );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001544
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001545 if (count > pdb->nbFiles)
1546 {
1547 memcpy( newfiles, files, pdb->nbFiles );
1548 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1549 }
1550 else memcpy( newfiles, files, count );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001551 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001552 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001553 pdb->hFileHandles = newhandle;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001554 pdb->nbFiles = count;
1555 }
1556 return pdb->nbFiles;
1557}
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001558
1559
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001560/*************************************************************************
1561 * SetHandleCount32 (KERNEL32.494)
1562 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001563UINT32 WINAPI SetHandleCount32( UINT32 count )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001564{
1565 return MIN( 256, count );
1566}
1567
1568
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001569/***********************************************************************
1570 * FlushFileBuffers (KERNEL32.133)
1571 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001572BOOL32 WINAPI FlushFileBuffers( HFILE32 hFile )
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001573{
Alexandre Julliard21979011997-03-05 08:22:35 +00001574 FILE_OBJECT *file;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001575 BOOL32 ret;
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001576
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001577 TRACE(file, "(%d)\n", hFile );
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001578 if (!(file = FILE_GetFile( hFile ))) return FALSE;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001579 if (fsync( file->unix_handle ) != -1) ret = TRUE;
1580 else
1581 {
1582 FILE_SetDosError();
1583 ret = FALSE;
1584 }
1585 FILE_ReleaseFile( file );
1586 return ret;
1587}
1588
1589
1590/**************************************************************************
1591 * SetEndOfFile (KERNEL32.483)
1592 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001593BOOL32 WINAPI SetEndOfFile( HFILE32 hFile )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001594{
Alexandre Julliard21979011997-03-05 08:22:35 +00001595 FILE_OBJECT *file;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001596 BOOL32 ret = TRUE;
1597
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001598 TRACE(file, "(%d)\n", hFile );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001599 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1600 if (ftruncate( file->unix_handle,
1601 lseek( file->unix_handle, 0, SEEK_CUR ) ))
1602 {
1603 FILE_SetDosError();
1604 ret = FALSE;
1605 }
1606 FILE_ReleaseFile( file );
1607 return ret;
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001608}
1609
1610
1611/***********************************************************************
Alexandre Julliard3051b641996-07-05 17:14:13 +00001612 * DeleteFile16 (KERNEL.146)
1613 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001614BOOL16 WINAPI DeleteFile16( LPCSTR path )
Alexandre Julliard3051b641996-07-05 17:14:13 +00001615{
1616 return DeleteFile32A( path );
1617}
1618
1619
1620/***********************************************************************
1621 * DeleteFile32A (KERNEL32.71)
1622 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001623BOOL32 WINAPI DeleteFile32A( LPCSTR path )
Alexandre Julliard3051b641996-07-05 17:14:13 +00001624{
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001625 DOS_FULL_NAME full_name;
Alexandre Julliard3051b641996-07-05 17:14:13 +00001626
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001627 TRACE(file, "'%s'\n", path );
Alexandre Julliard3051b641996-07-05 17:14:13 +00001628
Alexandre Julliard829fe321998-07-26 14:27:39 +00001629 if (DOSFS_GetDevice( path ))
Alexandre Julliard3051b641996-07-05 17:14:13 +00001630 {
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001631 WARN(file, "cannot remove DOS device '%s'!\n", path);
Alexandre Julliard3051b641996-07-05 17:14:13 +00001632 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1633 return FALSE;
1634 }
1635
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001636 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1637 if (unlink( full_name.long_name ) == -1)
Alexandre Julliard3051b641996-07-05 17:14:13 +00001638 {
1639 FILE_SetDosError();
1640 return FALSE;
1641 }
1642 return TRUE;
1643}
1644
1645
1646/***********************************************************************
1647 * DeleteFile32W (KERNEL32.72)
1648 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001649BOOL32 WINAPI DeleteFile32W( LPCWSTR path )
Alexandre Julliard3051b641996-07-05 17:14:13 +00001650{
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001651 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
Alexandre Julliarde658d821997-11-30 17:45:40 +00001652 BOOL32 ret = DeleteFile32A( xpath );
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001653 HeapFree( GetProcessHeap(), 0, xpath );
Alexandre Julliard3051b641996-07-05 17:14:13 +00001654 return ret;
1655}
1656
1657
1658/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001659 * FILE_SetFileType
1660 */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001661BOOL32 FILE_SetFileType( HFILE32 hFile, DWORD type )
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001662{
Alexandre Julliard21979011997-03-05 08:22:35 +00001663 FILE_OBJECT *file = FILE_GetFile( hFile );
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001664 if (!file) return FALSE;
1665 file->type = type;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001666 FILE_ReleaseFile( file );
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001667 return TRUE;
1668}
1669
1670
1671/***********************************************************************
Alexandre Julliard21979011997-03-05 08:22:35 +00001672 * FILE_mmap
1673 */
Alexandre Julliard491502b1997-11-01 19:08:16 +00001674LPVOID FILE_mmap( HFILE32 hFile, LPVOID start,
Alexandre Julliard21979011997-03-05 08:22:35 +00001675 DWORD size_high, DWORD size_low,
1676 DWORD offset_high, DWORD offset_low,
1677 int prot, int flags )
1678{
Alexandre Julliard491502b1997-11-01 19:08:16 +00001679 LPVOID ret;
1680 FILE_OBJECT *file = FILE_GetFile( hFile );
1681 if (!file) return (LPVOID)-1;
1682 ret = FILE_dommap( file, start, size_high, size_low,
1683 offset_high, offset_low, prot, flags );
1684 FILE_ReleaseFile( file );
1685 return ret;
1686}
1687
1688
1689/***********************************************************************
1690 * FILE_dommap
1691 */
1692LPVOID FILE_dommap( FILE_OBJECT *file, LPVOID start,
1693 DWORD size_high, DWORD size_low,
1694 DWORD offset_high, DWORD offset_low,
1695 int prot, int flags )
1696{
Alexandre Julliard21979011997-03-05 08:22:35 +00001697 int fd = -1;
Alexandre Julliard491502b1997-11-01 19:08:16 +00001698 int pos;
1699 LPVOID ret;
Alexandre Julliard21979011997-03-05 08:22:35 +00001700
1701 if (size_high || offset_high)
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001702 FIXME(file, "offsets larger than 4Gb not supported\n");
Alexandre Julliard21979011997-03-05 08:22:35 +00001703
1704 if (!file)
1705 {
1706#ifdef MAP_ANON
1707 flags |= MAP_ANON;
1708#else
1709 static int fdzero = -1;
1710
1711 if (fdzero == -1)
1712 {
1713 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1714 {
1715 perror( "/dev/zero: open" );
1716 exit(1);
1717 }
1718 }
1719 fd = fdzero;
1720#endif /* MAP_ANON */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001721 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1722#ifdef MAP_SHARED
1723 flags &= ~MAP_SHARED;
1724#endif
1725#ifdef MAP_PRIVATE
1726 flags |= MAP_PRIVATE;
1727#endif
Alexandre Julliard21979011997-03-05 08:22:35 +00001728 }
1729 else fd = file->unix_handle;
1730
Alexandre Julliard491502b1997-11-01 19:08:16 +00001731 if ((ret = mmap( start, size_low, prot,
1732 flags, fd, offset_low )) != (LPVOID)-1)
1733 return ret;
1734
1735 /* mmap() failed; if this is because the file offset is not */
1736 /* page-aligned (EINVAL), or because the underlying filesystem */
1737 /* does not support mmap() (ENOEXEC), we do it by hand. */
1738
1739 if (!file) return ret;
1740 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1741 if (prot & PROT_WRITE)
1742 {
1743 /* We cannot fake shared write mappings */
1744#ifdef MAP_SHARED
1745 if (flags & MAP_SHARED) return ret;
1746#endif
1747#ifdef MAP_PRIVATE
1748 if (!(flags & MAP_PRIVATE)) return ret;
1749#endif
1750 }
1751/* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1752 /* Reserve the memory with an anonymous mmap */
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +00001753 ret = FILE_dommap( NULL, start, size_high, size_low, 0, 0,
1754 PROT_READ | PROT_WRITE, flags );
Alexandre Julliard491502b1997-11-01 19:08:16 +00001755 if (ret == (LPVOID)-1) return ret;
1756 /* Now read in the file */
1757 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1758 {
1759 FILE_munmap( ret, size_high, size_low );
1760 return (LPVOID)-1;
1761 }
1762 read( fd, ret, size_low );
1763 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1764 mprotect( ret, size_low, prot ); /* Set the right protection */
1765 return ret;
1766}
1767
1768
1769/***********************************************************************
1770 * FILE_munmap
1771 */
1772int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1773{
1774 if (size_high)
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001775 FIXME(file, "offsets larger than 4Gb not supported\n");
Alexandre Julliard491502b1997-11-01 19:08:16 +00001776 return munmap( start, size_low );
Alexandre Julliard21979011997-03-05 08:22:35 +00001777}
1778
1779
1780/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001781 * GetFileType (KERNEL32.222)
1782 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001783DWORD WINAPI GetFileType( HFILE32 hFile )
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001784{
Alexandre Julliard21979011997-03-05 08:22:35 +00001785 FILE_OBJECT *file = FILE_GetFile(hFile);
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001786 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1787 FILE_ReleaseFile( file );
Alexandre Julliard8bbf8181996-09-13 16:50:47 +00001788 return file->type;
1789}
Alexandre Julliard75d86e11996-11-17 18:59:11 +00001790
1791
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001792/**************************************************************************
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001793 * MoveFileEx32A (KERNEL32.???)
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001794 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001795BOOL32 WINAPI MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag )
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001796{
1797 DOS_FULL_NAME full_name1, full_name2;
1798 int mode=0; /* mode == 1: use copy */
1799
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001800 TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag);
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001801
1802 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1803 if (fn2) { /* !fn2 means delete fn1 */
1804 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1805 /* Source name and target path are valid */
1806 if ( full_name1.drive != full_name2.drive)
1807 /* use copy, if allowed */
1808 if (!(flag & MOVEFILE_COPY_ALLOWED)) {
1809 /* FIXME: Use right error code */
1810 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
1811 return FALSE;
1812 }
1813 else mode =1;
1814 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1815 /* target exists, check if we may overwrite */
1816 if (!(flag & MOVEFILE_REPLACE_EXISTING)) {
1817 /* FIXME: Use right error code */
1818 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1819 return FALSE;
1820 }
1821 }
1822 else /* fn2 == NULL means delete source */
1823 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1824 if (flag & MOVEFILE_COPY_ALLOWED) {
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001825 WARN(file, "Illegal flag\n");
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001826 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1827 EL_Unknown );
1828 return FALSE;
1829 }
1830 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1831 Perhaps we should queue these command and execute it
1832 when exiting... What about using on_exit(2)
1833 */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001834 FIXME(file, "Please delete file '%s' when Wine has finished\n",
1835 full_name1.long_name);
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001836 return TRUE;
1837 }
1838 else if (unlink( full_name1.long_name ) == -1)
1839 {
1840 FILE_SetDosError();
1841 return FALSE;
1842 }
1843 else return TRUE; /* successfully deleted */
1844
1845 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) {
1846 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1847 Perhaps we should queue these command and execute it
1848 when exiting... What about using on_exit(2)
1849 */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001850 FIXME(file,"Please move existing file '%s' to file '%s'"
1851 "when Wine has finished\n",
1852 full_name1.long_name, full_name2.long_name);
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001853 return TRUE;
1854 }
1855
1856 if (!mode) /* move the file */
1857 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1858 {
1859 FILE_SetDosError();
1860 return FALSE;
1861 }
1862 else return TRUE;
1863 else /* copy File */
1864 return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING)));
1865
1866}
1867
1868/**************************************************************************
1869 * MoveFileEx32W (KERNEL32.???)
1870 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001871BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001872{
1873 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1874 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1875 BOOL32 res = MoveFileEx32A( afn1, afn2, flag );
1876 HeapFree( GetProcessHeap(), 0, afn1 );
1877 HeapFree( GetProcessHeap(), 0, afn2 );
1878 return res;
1879}
1880
1881
1882/**************************************************************************
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001883 * MoveFile32A (KERNEL32.387)
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001884 *
1885 * Move file or directory
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001886 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001887BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001888{
1889 DOS_FULL_NAME full_name1, full_name2;
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001890 struct stat fstat;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001891
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001892 TRACE(file, "(%s,%s)\n", fn1, fn2 );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001893
1894 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001895 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1896 /* The new name must not already exist */
1897 return FALSE;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001898 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001899
1900 if (full_name1.drive == full_name2.drive) /* move */
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001901 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1902 {
1903 FILE_SetDosError();
1904 return FALSE;
1905 }
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001906 else return TRUE;
1907 else /*copy */ {
1908 if (stat( full_name1.long_name, &fstat ))
1909 {
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001910 WARN(file, "Invalid source file %s\n",
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001911 full_name1.long_name);
1912 FILE_SetDosError();
1913 return FALSE;
1914 }
1915 if (S_ISDIR(fstat.st_mode)) {
1916 /* No Move for directories across file systems */
1917 /* FIXME: Use right error code */
1918 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort,
1919 EL_Unknown );
1920 return FALSE;
1921 }
1922 else
1923 return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */
1924 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001925}
1926
1927
1928/**************************************************************************
1929 * MoveFile32W (KERNEL32.390)
1930 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001931BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001932{
1933 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1934 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1935 BOOL32 res = MoveFile32A( afn1, afn2 );
1936 HeapFree( GetProcessHeap(), 0, afn1 );
1937 HeapFree( GetProcessHeap(), 0, afn2 );
1938 return res;
1939}
1940
1941
1942/**************************************************************************
1943 * CopyFile32A (KERNEL32.36)
1944 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001945BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001946{
1947 HFILE32 h1, h2;
1948 BY_HANDLE_FILE_INFORMATION info;
1949 UINT32 count;
1950 BOOL32 ret = FALSE;
1951 int mode;
1952 char buffer[2048];
1953
1954 if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE;
1955 if (!GetFileInformationByHandle( h1, &info ))
1956 {
1957 CloseHandle( h1 );
1958 return FALSE;
1959 }
1960 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1961 if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32)
1962 {
1963 CloseHandle( h1 );
1964 return FALSE;
1965 }
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001966 while ((count = _lread32( h1, buffer, sizeof(buffer) )) > 0)
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001967 {
1968 char *p = buffer;
1969 while (count > 0)
1970 {
1971 INT32 res = _lwrite32( h2, p, count );
1972 if (res <= 0) goto done;
1973 p += res;
1974 count -= res;
1975 }
1976 }
1977 ret = TRUE;
1978done:
1979 CloseHandle( h1 );
1980 CloseHandle( h2 );
1981 return ret;
1982}
1983
1984
1985/**************************************************************************
1986 * CopyFile32W (KERNEL32.37)
1987 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001988BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists)
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001989{
1990 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1991 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1992 BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists );
1993 HeapFree( GetProcessHeap(), 0, sourceA );
1994 HeapFree( GetProcessHeap(), 0, destA );
1995 return ret;
1996}
1997
1998
Alexandre Julliard75d86e11996-11-17 18:59:11 +00001999/***********************************************************************
Alexandre Julliarda0d77311998-09-13 16:32:00 +00002000 * SetFileTime (KERNEL32.650)
Alexandre Julliard75d86e11996-11-17 18:59:11 +00002001 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00002002BOOL32 WINAPI SetFileTime( HFILE32 hFile,
2003 const FILETIME *lpCreationTime,
2004 const FILETIME *lpLastAccessTime,
2005 const FILETIME *lpLastWriteTime )
Alexandre Julliard75d86e11996-11-17 18:59:11 +00002006{
Alexandre Julliard21979011997-03-05 08:22:35 +00002007 FILE_OBJECT *file = FILE_GetFile(hFile);
Alexandre Julliard75d86e11996-11-17 18:59:11 +00002008 struct utimbuf utimbuf;
2009
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00002010 if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
Alexandre Julliarda0d77311998-09-13 16:32:00 +00002011 TRACE(file,"('%s',%p,%p,%p)\n",
Alexandre Julliard75d86e11996-11-17 18:59:11 +00002012 file->unix_name,
2013 lpCreationTime,
2014 lpLastAccessTime,
2015 lpLastWriteTime
2016 );
2017 if (lpLastAccessTime)
Alexandre Julliardc6c09441997-01-12 18:32:19 +00002018 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
Alexandre Julliard75d86e11996-11-17 18:59:11 +00002019 else
2020 utimbuf.actime = 0; /* FIXME */
2021 if (lpLastWriteTime)
Alexandre Julliardc6c09441997-01-12 18:32:19 +00002022 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
Alexandre Julliard75d86e11996-11-17 18:59:11 +00002023 else
2024 utimbuf.modtime = 0; /* FIXME */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00002025 if (-1==utime(file->unix_name,&utimbuf))
2026 {
Alexandre Julliarda0d77311998-09-13 16:32:00 +00002027 MSG("Couldn't set the time for file '%s'. Insufficient permissions !?\n", file->unix_name);
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00002028 FILE_ReleaseFile( file );
Alexandre Julliard75d86e11996-11-17 18:59:11 +00002029 FILE_SetDosError();
2030 return FALSE;
2031 }
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00002032 FILE_ReleaseFile( file );
Alexandre Julliard75d86e11996-11-17 18:59:11 +00002033 return TRUE;
2034}
Alexandre Julliardd37eb361997-07-20 16:23:21 +00002035
2036/* Locks need to be mirrored because unix file locking is based
2037 * on the pid. Inside of wine there can be multiple WINE processes
2038 * that share the same unix pid.
2039 * Read's and writes should check these locks also - not sure
2040 * how critical that is at this point (FIXME).
2041 */
2042
2043static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2044{
2045 DOS_FILE_LOCK *curr;
2046 DWORD processId;
2047
2048 processId = GetCurrentProcessId();
2049
2050 /* check if lock overlaps a current lock for the same file */
2051 for (curr = locks; curr; curr = curr->next) {
2052 if (strcmp(curr->unix_name, file->unix_name) == 0) {
Alexandre Julliard44ed71f1997-12-21 19:17:50 +00002053 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2054 return TRUE;/* region is identic */
Alexandre Julliardd37eb361997-07-20 16:23:21 +00002055 if ((f->l_start < (curr->base + curr->len)) &&
2056 ((f->l_start + f->l_len) > curr->base)) {
2057 /* region overlaps */
2058 return FALSE;
2059 }
2060 }
2061 }
2062
2063 curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) );
2064 curr->processId = GetCurrentProcessId();
2065 curr->base = f->l_start;
2066 curr->len = f->l_len;
2067 curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name);
2068 curr->next = locks;
2069 curr->dos_file = file;
2070 locks = curr;
2071 return TRUE;
2072}
2073
2074static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2075{
2076 DWORD processId;
2077 DOS_FILE_LOCK **curr;
2078 DOS_FILE_LOCK *rem;
2079
2080 processId = GetCurrentProcessId();
2081 curr = &locks;
2082 while (*curr) {
2083 if ((*curr)->dos_file == file) {
2084 rem = *curr;
2085 *curr = (*curr)->next;
2086 HeapFree( SystemHeap, 0, rem->unix_name );
2087 HeapFree( SystemHeap, 0, rem );
2088 }
2089 else
2090 curr = &(*curr)->next;
2091 }
2092}
2093
2094static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2095{
2096 DWORD processId;
2097 DOS_FILE_LOCK **curr;
2098 DOS_FILE_LOCK *rem;
2099
2100 processId = GetCurrentProcessId();
2101 for (curr = &locks; *curr; curr = &(*curr)->next) {
2102 if ((*curr)->processId == processId &&
2103 (*curr)->dos_file == file &&
2104 (*curr)->base == f->l_start &&
2105 (*curr)->len == f->l_len) {
2106 /* this is the same lock */
2107 rem = *curr;
2108 *curr = (*curr)->next;
2109 HeapFree( SystemHeap, 0, rem->unix_name );
2110 HeapFree( SystemHeap, 0, rem );
2111 return TRUE;
2112 }
2113 }
2114 /* no matching lock found */
2115 return FALSE;
2116}
2117
2118
2119/**************************************************************************
2120 * LockFile (KERNEL32.511)
2121 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00002122BOOL32 WINAPI LockFile(
Alexandre Julliardd37eb361997-07-20 16:23:21 +00002123 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2124 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2125{
2126 struct flock f;
2127 FILE_OBJECT *file;
2128
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00002129 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
Alexandre Julliardd37eb361997-07-20 16:23:21 +00002130 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2131 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2132
2133 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00002134 FIXME(file, "Unimplemented bytes > 32bits\n");
Alexandre Julliardd37eb361997-07-20 16:23:21 +00002135 return FALSE;
2136 }
2137
2138 f.l_start = dwFileOffsetLow;
2139 f.l_len = nNumberOfBytesToLockLow;
2140 f.l_whence = SEEK_SET;
2141 f.l_pid = 0;
2142 f.l_type = F_WRLCK;
2143
2144 if (!(file = FILE_GetFile(hFile))) return FALSE;
2145
2146 /* shadow locks internally */
2147 if (!DOS_AddLock(file, &f)) {
2148 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
2149 return FALSE;
2150 }
2151
2152 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2153#ifdef USE_UNIX_LOCKS
2154 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2155 if (errno == EACCES || errno == EAGAIN) {
2156 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk );
2157 }
2158 else {
2159 FILE_SetDosError();
2160 }
2161 /* remove our internal copy of the lock */
2162 DOS_RemoveLock(file, &f);
2163 return FALSE;
2164 }
2165#endif
2166 return TRUE;
2167}
2168
2169
2170/**************************************************************************
2171 * UnlockFile (KERNEL32.703)
2172 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00002173BOOL32 WINAPI UnlockFile(
Alexandre Julliardd37eb361997-07-20 16:23:21 +00002174 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2175 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2176{
2177 FILE_OBJECT *file;
2178 struct flock f;
2179
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00002180 TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
Alexandre Julliardd37eb361997-07-20 16:23:21 +00002181 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2182 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2183
2184 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00002185 WARN(file, "Unimplemented bytes > 32bits\n");
Alexandre Julliardd37eb361997-07-20 16:23:21 +00002186 return FALSE;
2187 }
2188
2189 f.l_start = dwFileOffsetLow;
2190 f.l_len = nNumberOfBytesToUnlockLow;
2191 f.l_whence = SEEK_SET;
2192 f.l_pid = 0;
2193 f.l_type = F_UNLCK;
2194
2195 if (!(file = FILE_GetFile(hFile))) return FALSE;
2196
2197 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2198
2199 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2200#ifdef USE_UNIX_LOCKS
2201 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2202 FILE_SetDosError();
2203 return FALSE;
2204 }
2205#endif
2206 return TRUE;
2207}
2208
Alexandre Julliarda845b881998-06-01 10:44:35 +00002209/**************************************************************************
2210 * GetFileAttributesEx32A [KERNEL32.874]
2211 */
2212BOOL32 WINAPI GetFileAttributesEx32A(
2213 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2214 LPVOID lpFileInformation)
2215{
2216 DOS_FULL_NAME full_name;
2217 BY_HANDLE_FILE_INFORMATION info;
2218
2219 if (lpFileName == NULL) return FALSE;
2220 if (lpFileInformation == NULL) return FALSE;
2221
2222 if (fInfoLevelId == GetFileExInfoStandard) {
2223 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2224 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2225 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2226 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2227
2228 lpFad->dwFileAttributes = info.dwFileAttributes;
2229 lpFad->ftCreationTime = info.ftCreationTime;
2230 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2231 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2232 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2233 lpFad->nFileSizeLow = info.nFileSizeLow;
2234 }
2235 else {
2236 FIXME (file, "invalid info level %d!\n", fInfoLevelId);
2237 return FALSE;
2238 }
2239
2240 return TRUE;
2241}
2242
2243
2244/**************************************************************************
2245 * GetFileAttributesEx32W [KERNEL32.875]
2246 */
2247BOOL32 WINAPI GetFileAttributesEx32W(
2248 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2249 LPVOID lpFileInformation)
2250{
2251 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2252 BOOL32 res =
2253 GetFileAttributesEx32A( nameA, fInfoLevelId, lpFileInformation);
2254 HeapFree( GetProcessHeap(), 0, nameA );
2255 return res;
2256}
2257
2258