Fixed typo in the IDispatch_GetTypeInfo macro.
[wine/testsucceed.git] / files / file.c
blobad9e86bcade7a892a4b1f134287dd7a2dd744d81
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
7 * TODO:
8 * Fix the CopyFileEx methods to implement the "extented" functionality.
9 * Right now, they simply call the CopyFile method.
12 #include "config.h"
14 #include <assert.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #ifdef HAVE_SYS_ERRNO_H
22 #include <sys/errno.h>
23 #endif
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29 #include <sys/time.h>
30 #include <time.h>
31 #include <unistd.h>
32 #include <utime.h>
34 #include "winerror.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wine/winbase16.h"
38 #include "wine/winestring.h"
39 #include "drive.h"
40 #include "file.h"
41 #include "global.h"
42 #include "heap.h"
43 #include "msdos.h"
44 #include "ldt.h"
45 #include "process.h"
46 #include "task.h"
47 #include "wincon.h"
48 #include "debugtools.h"
50 #include "server.h"
52 DEFAULT_DEBUG_CHANNEL(file);
54 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
55 #define MAP_ANON MAP_ANONYMOUS
56 #endif
58 /* Size of per-process table of DOS handles */
59 #define DOS_TABLE_SIZE 256
62 /***********************************************************************
63 * FILE_ConvertOFMode
65 * Convert OF_* mode into flags for CreateFile.
67 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
69 switch(mode & 0x03)
71 case OF_READ: *access = GENERIC_READ; break;
72 case OF_WRITE: *access = GENERIC_WRITE; break;
73 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
74 default: *access = 0; break;
76 switch(mode & 0x70)
78 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
79 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
80 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
81 case OF_SHARE_DENY_NONE:
82 case OF_SHARE_COMPAT:
83 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
88 #if 0
89 /***********************************************************************
90 * FILE_ShareDeny
92 * PARAMS
93 * oldmode[I] mode how file was first opened
94 * mode[I] mode how the file should get opened
95 * RETURNS
96 * TRUE: deny open
97 * FALSE: allow open
99 * Look what we have to do with the given SHARE modes
101 * Ralph Brown's interrupt list gives following explication, I guess
102 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
104 * FIXME: Validate this function
105 ========from Ralph Brown's list =========
106 (Table 0750)
107 Values of DOS file sharing behavior:
108 | Second and subsequent Opens
109 First |Compat Deny Deny Deny Deny
110 Open | All Write Read None
111 |R W RW R W RW R W RW R W RW R W RW
112 - - - - -| - - - - - - - - - - - - - - - - -
113 Compat R |Y Y Y N N N 1 N N N N N 1 N N
114 W |Y Y Y N N N N N N N N N N N N
115 RW|Y Y Y N N N N N N N N N N N N
116 - - - - -|
117 Deny R |C C C N N N N N N N N N N N N
118 All W |C C C N N N N N N N N N N N N
119 RW|C C C N N N N N N N N N N N N
120 - - - - -|
121 Deny R |2 C C N N N Y N N N N N Y N N
122 Write W |C C C N N N N N N Y N N Y N N
123 RW|C C C N N N N N N N N N Y N N
124 - - - - -|
125 Deny R |C C C N N N N Y N N N N N Y N
126 Read W |C C C N N N N N N N Y N N Y N
127 RW|C C C N N N N N N N N N N Y N
128 - - - - -|
129 Deny R |2 C C N N N Y Y Y N N N Y Y Y
130 None W |C C C N N N N N N Y Y Y Y Y Y
131 RW|C C C N N N N N N N N N Y Y Y
132 Legend: Y = open succeeds, N = open fails with error code 05h
133 C = open fails, INT 24 generated
134 1 = open succeeds if file read-only, else fails with error code
135 2 = open succeeds if file read-only, else fails with INT 24
136 ========end of description from Ralph Brown's List =====
137 For every "Y" in the table we return FALSE
138 For every "N" we set the DOS_ERROR and return TRUE
139 For all other cases we barf,set the DOS_ERROR and return TRUE
142 static BOOL FILE_ShareDeny( int mode, int oldmode)
144 int oldsharemode = oldmode & 0x70;
145 int sharemode = mode & 0x70;
146 int oldopenmode = oldmode & 3;
147 int openmode = mode & 3;
149 switch (oldsharemode)
151 case OF_SHARE_COMPAT:
152 if (sharemode == OF_SHARE_COMPAT) return FALSE;
153 if (openmode == OF_READ) goto test_ro_err05 ;
154 goto fail_error05;
155 case OF_SHARE_EXCLUSIVE:
156 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
157 goto fail_error05;
158 case OF_SHARE_DENY_WRITE:
159 if (openmode != OF_READ)
161 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
162 goto fail_error05;
164 switch (sharemode)
166 case OF_SHARE_COMPAT:
167 if (oldopenmode == OF_READ) goto test_ro_int24 ;
168 goto fail_int24;
169 case OF_SHARE_DENY_NONE :
170 return FALSE;
171 case OF_SHARE_DENY_WRITE :
172 if (oldopenmode == OF_READ) return FALSE;
173 case OF_SHARE_DENY_READ :
174 if (oldopenmode == OF_WRITE) return FALSE;
175 case OF_SHARE_EXCLUSIVE:
176 default:
177 goto fail_error05;
179 break;
180 case OF_SHARE_DENY_READ:
181 if (openmode != OF_WRITE)
183 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
184 goto fail_error05;
186 switch (sharemode)
188 case OF_SHARE_COMPAT:
189 goto fail_int24;
190 case OF_SHARE_DENY_NONE :
191 return FALSE;
192 case OF_SHARE_DENY_WRITE :
193 if (oldopenmode == OF_READ) return FALSE;
194 case OF_SHARE_DENY_READ :
195 if (oldopenmode == OF_WRITE) return FALSE;
196 case OF_SHARE_EXCLUSIVE:
197 default:
198 goto fail_error05;
200 break;
201 case OF_SHARE_DENY_NONE:
202 switch (sharemode)
204 case OF_SHARE_COMPAT:
205 goto fail_int24;
206 case OF_SHARE_DENY_NONE :
207 return FALSE;
208 case OF_SHARE_DENY_WRITE :
209 if (oldopenmode == OF_READ) return FALSE;
210 case OF_SHARE_DENY_READ :
211 if (oldopenmode == OF_WRITE) return FALSE;
212 case OF_SHARE_EXCLUSIVE:
213 default:
214 goto fail_error05;
216 default:
217 ERR("unknown mode\n");
219 ERR("shouldn't happen\n");
220 ERR("Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
221 return TRUE;
223 test_ro_int24:
224 if (oldmode == OF_READ)
225 return FALSE;
226 /* Fall through */
227 fail_int24:
228 FIXME("generate INT24 missing\n");
229 /* Is this the right error? */
230 SetLastError( ERROR_ACCESS_DENIED );
231 return TRUE;
233 test_ro_err05:
234 if (oldmode == OF_READ)
235 return FALSE;
236 /* fall through */
237 fail_error05:
238 TRACE("Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
239 SetLastError( ERROR_ACCESS_DENIED );
240 return TRUE;
242 #endif
245 /***********************************************************************
246 * FILE_SetDosError
248 * Set the DOS error code from errno.
250 void FILE_SetDosError(void)
252 int save_errno = errno; /* errno gets overwritten by printf */
254 TRACE("errno = %d %s\n", errno, strerror(errno));
255 switch (save_errno)
257 case EAGAIN:
258 SetLastError( ERROR_SHARING_VIOLATION );
259 break;
260 case EBADF:
261 SetLastError( ERROR_INVALID_HANDLE );
262 break;
263 case ENOSPC:
264 SetLastError( ERROR_HANDLE_DISK_FULL );
265 break;
266 case EACCES:
267 case EPERM:
268 case EROFS:
269 SetLastError( ERROR_ACCESS_DENIED );
270 break;
271 case EBUSY:
272 SetLastError( ERROR_LOCK_VIOLATION );
273 break;
274 case ENOENT:
275 SetLastError( ERROR_FILE_NOT_FOUND );
276 break;
277 case EISDIR:
278 SetLastError( ERROR_CANNOT_MAKE );
279 break;
280 case ENFILE:
281 case EMFILE:
282 SetLastError( ERROR_NO_MORE_FILES );
283 break;
284 case EEXIST:
285 SetLastError( ERROR_FILE_EXISTS );
286 break;
287 case EINVAL:
288 case ESPIPE:
289 SetLastError( ERROR_SEEK );
290 break;
291 case ENOTEMPTY:
292 SetLastError( ERROR_DIR_NOT_EMPTY );
293 break;
294 case ENOEXEC:
295 SetLastError( ERROR_BAD_FORMAT );
296 break;
297 default:
298 perror( "FILE_SetDosError: unknown errno" );
299 SetLastError( ERROR_GEN_FAILURE );
300 break;
302 errno = save_errno;
306 /***********************************************************************
307 * FILE_DupUnixHandle
309 * Duplicate a Unix handle into a task handle.
311 HFILE FILE_DupUnixHandle( int fd, DWORD access )
313 struct alloc_file_handle_request *req = get_req_buffer();
314 req->access = access;
315 server_call_fd( REQ_ALLOC_FILE_HANDLE, fd, NULL );
316 return req->handle;
320 /***********************************************************************
321 * FILE_CreateFile
323 * Implementation of CreateFile. Takes a Unix path name.
325 HANDLE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
326 LPSECURITY_ATTRIBUTES sa, DWORD creation,
327 DWORD attributes, HANDLE template, BOOL fail_read_only )
329 DWORD err;
330 struct create_file_request *req = get_req_buffer();
332 restart:
333 req->access = access;
334 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
335 req->sharing = sharing;
336 req->create = creation;
337 req->attrs = attributes;
338 lstrcpynA( req->name, filename, server_remaining(req->name) );
339 SetLastError(0);
340 err = server_call( REQ_CREATE_FILE );
342 /* If write access failed, retry without GENERIC_WRITE */
344 if ((req->handle == -1) && !fail_read_only && (access & GENERIC_WRITE))
346 if ((err == STATUS_MEDIA_WRITE_PROTECTED) || (err == STATUS_ACCESS_DENIED))
348 TRACE("Write access failed for file '%s', trying without "
349 "write access\n", filename);
350 access &= ~GENERIC_WRITE;
351 goto restart;
355 if (req->handle == -1)
356 WARN("Unable to create file '%s' (GLE %ld)\n", filename,
357 GetLastError());
359 return req->handle;
363 /***********************************************************************
364 * FILE_CreateDevice
366 * Same as FILE_CreateFile but for a device
368 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
370 struct create_device_request *req = get_req_buffer();
372 req->access = access;
373 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
374 req->id = client_id;
375 SetLastError(0);
376 server_call( REQ_CREATE_DEVICE );
377 return req->handle;
381 /*************************************************************************
382 * CreateFileA [KERNEL32.45] Creates or opens a file or other object
384 * Creates or opens an object, and returns a handle that can be used to
385 * access that object.
387 * PARAMS
389 * filename [I] pointer to filename to be accessed
390 * access [I] access mode requested
391 * sharing [I] share mode
392 * sa [I] pointer to security attributes
393 * creation [I] how to create the file
394 * attributes [I] attributes for newly created file
395 * template [I] handle to file with extended attributes to copy
397 * RETURNS
398 * Success: Open handle to specified file
399 * Failure: INVALID_HANDLE_VALUE
401 * NOTES
402 * Should call SetLastError() on failure.
404 * BUGS
406 * Doesn't support character devices, pipes, template files, or a
407 * lot of the 'attributes' flags yet.
409 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
410 LPSECURITY_ATTRIBUTES sa, DWORD creation,
411 DWORD attributes, HANDLE template )
413 DOS_FULL_NAME full_name;
415 if (!filename)
417 SetLastError( ERROR_INVALID_PARAMETER );
418 return HFILE_ERROR;
420 TRACE("%s %s%s%s%s%s%s%s\n",filename,
421 ((access & GENERIC_READ)==GENERIC_READ)?"GENERIC_READ ":"",
422 ((access & GENERIC_WRITE)==GENERIC_WRITE)?"GENERIC_WRITE ":"",
423 (!access)?"QUERY_ACCESS ":"",
424 ((sharing & FILE_SHARE_READ)==FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
425 ((sharing & FILE_SHARE_WRITE)==FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
426 ((sharing & FILE_SHARE_DELETE)==FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
427 (creation ==CREATE_NEW)?"CREATE_NEW":
428 (creation ==CREATE_ALWAYS)?"CREATE_ALWAYS ":
429 (creation ==OPEN_EXISTING)?"OPEN_EXISTING ":
430 (creation ==OPEN_ALWAYS)?"OPEN_ALWAYS ":
431 (creation ==TRUNCATE_EXISTING)?"TRUNCATE_EXISTING ":"");
433 /* If the name starts with '\\?\', ignore the first 4 chars. */
434 if (!strncmp(filename, "\\\\?\\", 4))
436 filename += 4;
437 if (!strncmp(filename, "UNC\\", 4))
439 FIXME("UNC name (%s) not supported.\n", filename );
440 SetLastError( ERROR_PATH_NOT_FOUND );
441 return HFILE_ERROR;
445 if (!strncmp(filename, "\\\\.\\", 4)) {
446 if (!DOSFS_GetDevice( filename ))
447 return DEVICE_Open( filename+4, access, sa );
448 else
449 filename+=4; /* fall into DOSFS_Device case below */
452 /* If the name still starts with '\\', it's a UNC name. */
453 if (!strncmp(filename, "\\\\", 2))
455 FIXME("UNC name (%s) not supported.\n", filename );
456 SetLastError( ERROR_PATH_NOT_FOUND );
457 return HFILE_ERROR;
460 /* If the name contains a DOS wild card (* or ?), do no create a file */
461 if(strchr(filename,'*') || strchr(filename,'?'))
462 return HFILE_ERROR;
464 /* Open a console for CONIN$ or CONOUT$ */
465 if (!strcasecmp(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
466 if (!strcasecmp(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
468 if (DOSFS_GetDevice( filename ))
470 HFILE ret;
472 TRACE("opening device '%s'\n", filename );
474 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
475 return ret;
477 /* Do not silence this please. It is a critical error. -MM */
478 ERR("Couldn't open device '%s'!\n",filename);
479 SetLastError( ERROR_FILE_NOT_FOUND );
480 return HFILE_ERROR;
483 /* check for filename, don't check for last entry if creating */
484 if (!DOSFS_GetFullName( filename,
485 (creation == OPEN_EXISTING) ||
486 (creation == TRUNCATE_EXISTING),
487 &full_name )) {
488 WARN("Unable to get full filename from '%s' (GLE %ld)\n",
489 filename, GetLastError());
490 return HFILE_ERROR;
493 return FILE_CreateFile( full_name.long_name, access, sharing,
494 sa, creation, attributes, template,
495 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
500 /*************************************************************************
501 * CreateFileW (KERNEL32.48)
503 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
504 LPSECURITY_ATTRIBUTES sa, DWORD creation,
505 DWORD attributes, HANDLE template)
507 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
508 HANDLE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
509 HeapFree( GetProcessHeap(), 0, afn );
510 return res;
514 /***********************************************************************
515 * FILE_FillInfo
517 * Fill a file information from a struct stat.
519 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
521 if (S_ISDIR(st->st_mode))
522 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
523 else
524 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
525 if (!(st->st_mode & S_IWUSR))
526 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
528 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
529 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
530 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
532 info->dwVolumeSerialNumber = 0; /* FIXME */
533 info->nFileSizeHigh = 0;
534 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
535 info->nNumberOfLinks = st->st_nlink;
536 info->nFileIndexHigh = 0;
537 info->nFileIndexLow = st->st_ino;
541 /***********************************************************************
542 * FILE_Stat
544 * Stat a Unix path name. Return TRUE if OK.
546 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
548 struct stat st;
550 if (!unixName || !info) return FALSE;
552 if (stat( unixName, &st ) == -1)
554 FILE_SetDosError();
555 return FALSE;
557 FILE_FillInfo( &st, info );
558 return TRUE;
562 /***********************************************************************
563 * GetFileInformationByHandle (KERNEL32.219)
565 DWORD WINAPI GetFileInformationByHandle( HANDLE hFile,
566 BY_HANDLE_FILE_INFORMATION *info )
568 struct get_file_info_request *req = get_req_buffer();
570 if (!info) return 0;
571 req->handle = hFile;
572 if (server_call( REQ_GET_FILE_INFO )) return 0;
573 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftCreationTime, 0 );
574 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftLastWriteTime, 0 );
575 DOSFS_UnixTimeToFileTime( req->access_time, &info->ftLastAccessTime, 0 );
576 info->dwFileAttributes = req->attr;
577 info->dwVolumeSerialNumber = req->serial;
578 info->nFileSizeHigh = req->size_high;
579 info->nFileSizeLow = req->size_low;
580 info->nNumberOfLinks = req->links;
581 info->nFileIndexHigh = req->index_high;
582 info->nFileIndexLow = req->index_low;
583 return 1;
587 /**************************************************************************
588 * GetFileAttributes16 (KERNEL.420)
590 DWORD WINAPI GetFileAttributes16( LPCSTR name )
592 return GetFileAttributesA( name );
596 /**************************************************************************
597 * GetFileAttributesA (KERNEL32.217)
599 DWORD WINAPI GetFileAttributesA( LPCSTR name )
601 DOS_FULL_NAME full_name;
602 BY_HANDLE_FILE_INFORMATION info;
604 if (name == NULL || *name=='\0') return -1;
606 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
607 if (!FILE_Stat( full_name.long_name, &info )) return -1;
608 return info.dwFileAttributes;
612 /**************************************************************************
613 * GetFileAttributesW (KERNEL32.218)
615 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
617 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
618 DWORD res = GetFileAttributesA( nameA );
619 HeapFree( GetProcessHeap(), 0, nameA );
620 return res;
624 /***********************************************************************
625 * GetFileSize (KERNEL32.220)
627 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
629 BY_HANDLE_FILE_INFORMATION info;
630 if (!GetFileInformationByHandle( hFile, &info )) return 0;
631 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
632 return info.nFileSizeLow;
636 /***********************************************************************
637 * GetFileTime (KERNEL32.221)
639 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
640 FILETIME *lpLastAccessTime,
641 FILETIME *lpLastWriteTime )
643 BY_HANDLE_FILE_INFORMATION info;
644 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
645 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
646 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
647 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
648 return TRUE;
651 /***********************************************************************
652 * CompareFileTime (KERNEL32.28)
654 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
656 if (!x || !y) return -1;
658 if (x->dwHighDateTime > y->dwHighDateTime)
659 return 1;
660 if (x->dwHighDateTime < y->dwHighDateTime)
661 return -1;
662 if (x->dwLowDateTime > y->dwLowDateTime)
663 return 1;
664 if (x->dwLowDateTime < y->dwLowDateTime)
665 return -1;
666 return 0;
669 /***********************************************************************
670 * FILE_GetTempFileName : utility for GetTempFileName
672 static UINT FILE_GetTempFileName( LPCSTR path, LPCSTR prefix, UINT unique,
673 LPSTR buffer, BOOL isWin16 )
675 static UINT unique_temp;
676 DOS_FULL_NAME full_name;
677 int i;
678 LPSTR p;
679 UINT num;
681 if ( !path || !prefix || !buffer ) return 0;
683 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
684 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
686 strcpy( buffer, path );
687 p = buffer + strlen(buffer);
689 /* add a \, if there isn't one and path is more than just the drive letter ... */
690 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
691 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
693 if (isWin16) *p++ = '~';
694 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
695 sprintf( p, "%04x.tmp", num );
697 /* Now try to create it */
699 if (!unique)
703 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
704 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
705 if (handle != INVALID_HANDLE_VALUE)
706 { /* We created it */
707 TRACE("created %s\n",
708 buffer);
709 CloseHandle( handle );
710 break;
712 if (GetLastError() != ERROR_FILE_EXISTS)
713 break; /* No need to go on */
714 num++;
715 sprintf( p, "%04x.tmp", num );
716 } while (num != (unique & 0xffff));
719 /* Get the full path name */
721 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
723 /* Check if we have write access in the directory */
724 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
725 if (access( full_name.long_name, W_OK ) == -1)
726 WARN("returns '%s', which doesn't seem to be writeable.\n",
727 buffer);
729 TRACE("returning %s\n", buffer );
730 return unique ? unique : num;
734 /***********************************************************************
735 * GetTempFileNameA (KERNEL32.290)
737 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
738 LPSTR buffer)
740 return FILE_GetTempFileName(path, prefix, unique, buffer, FALSE);
743 /***********************************************************************
744 * GetTempFileNameW (KERNEL32.291)
746 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
747 LPWSTR buffer )
749 LPSTR patha,prefixa;
750 char buffera[144];
751 UINT ret;
753 if (!path) return 0;
754 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
755 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
756 ret = FILE_GetTempFileName( patha, prefixa, unique, buffera, FALSE );
757 lstrcpyAtoW( buffer, buffera );
758 HeapFree( GetProcessHeap(), 0, patha );
759 HeapFree( GetProcessHeap(), 0, prefixa );
760 return ret;
764 /***********************************************************************
765 * GetTempFileName16 (KERNEL.97)
767 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
768 LPSTR buffer )
770 char temppath[144];
772 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
773 drive |= DRIVE_GetCurrentDrive() + 'A';
775 if ((drive & TF_FORCEDRIVE) &&
776 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
778 drive &= ~TF_FORCEDRIVE;
779 WARN("invalid drive %d specified\n", drive );
782 if (drive & TF_FORCEDRIVE)
783 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
784 else
785 GetTempPathA( 132, temppath );
786 return (UINT16)FILE_GetTempFileName( temppath, prefix, unique, buffer, TRUE );
789 /***********************************************************************
790 * FILE_DoOpenFile
792 * Implementation of OpenFile16() and OpenFile32().
794 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
795 BOOL win32 )
797 HFILE hFileRet;
798 FILETIME filetime;
799 WORD filedatetime[2];
800 DOS_FULL_NAME full_name;
801 DWORD access, sharing;
802 char *p;
804 if (!ofs) return HFILE_ERROR;
806 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
807 ((mode & 0x3 )==OF_READ)?"OF_READ":
808 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
809 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
810 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
811 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
812 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
813 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
814 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
815 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
816 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
817 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
818 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
819 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
820 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
821 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
822 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
823 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
827 ofs->cBytes = sizeof(OFSTRUCT);
828 ofs->nErrCode = 0;
829 if (mode & OF_REOPEN) name = ofs->szPathName;
831 if (!name) {
832 ERR("called with `name' set to NULL ! Please debug.\n");
833 return HFILE_ERROR;
836 TRACE("%s %04x\n", name, mode );
838 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
839 Are there any cases where getting the path here is wrong?
840 Uwe Bonnes 1997 Apr 2 */
841 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
842 ofs->szPathName, NULL )) goto error;
843 FILE_ConvertOFMode( mode, &access, &sharing );
845 /* OF_PARSE simply fills the structure */
847 if (mode & OF_PARSE)
849 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
850 != DRIVE_REMOVABLE);
851 TRACE("(%s): OF_PARSE, res = '%s'\n",
852 name, ofs->szPathName );
853 return 0;
856 /* OF_CREATE is completely different from all other options, so
857 handle it first */
859 if (mode & OF_CREATE)
861 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
862 sharing, NULL, CREATE_ALWAYS,
863 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
864 goto error;
865 goto success;
868 /* If OF_SEARCH is set, ignore the given path */
870 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
872 /* First try the file name as is */
873 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
874 /* Now remove the path */
875 if (name[0] && (name[1] == ':')) name += 2;
876 if ((p = strrchr( name, '\\' ))) name = p + 1;
877 if ((p = strrchr( name, '/' ))) name = p + 1;
878 if (!name[0]) goto not_found;
881 /* Now look for the file */
883 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
885 found:
886 TRACE("found %s = %s\n",
887 full_name.long_name, full_name.short_name );
888 lstrcpynA( ofs->szPathName, full_name.short_name,
889 sizeof(ofs->szPathName) );
891 if (mode & OF_SHARE_EXCLUSIVE)
892 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
893 on the file <tempdir>/_ins0432._mp to determine how
894 far installation has proceeded.
895 _ins0432._mp is an executable and while running the
896 application expects the open with OF_SHARE_ to fail*/
897 /* Probable FIXME:
898 As our loader closes the files after loading the executable,
899 we can't find the running executable with FILE_InUse.
900 Perhaps the loader should keep the file open.
901 Recheck against how Win handles that case */
903 char *last = strrchr(full_name.long_name,'/');
904 if (!last)
905 last = full_name.long_name - 1;
906 if (GetModuleHandle16(last+1))
908 TRACE("Denying shared open for %s\n",full_name.long_name);
909 return HFILE_ERROR;
913 if (mode & OF_DELETE)
915 if (unlink( full_name.long_name ) == -1) goto not_found;
916 TRACE("(%s): OF_DELETE return = OK\n", name);
917 return 1;
920 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
921 NULL, OPEN_EXISTING, 0, -1,
922 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
923 if (hFileRet == HFILE_ERROR) goto not_found;
925 GetFileTime( hFileRet, NULL, NULL, &filetime );
926 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
927 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
929 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
931 CloseHandle( hFileRet );
932 WARN("(%s): OF_VERIFY failed\n", name );
933 /* FIXME: what error here? */
934 SetLastError( ERROR_FILE_NOT_FOUND );
935 goto error;
938 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
940 success: /* We get here if the open was successful */
941 TRACE("(%s): OK, return = %d\n", name, hFileRet );
942 if (win32)
944 if (mode & OF_EXIST) /* Return the handle, but close it first */
945 CloseHandle( hFileRet );
947 else
949 hFileRet = FILE_AllocDosHandle( hFileRet );
950 if (hFileRet == HFILE_ERROR16) goto error;
951 if (mode & OF_EXIST) /* Return the handle, but close it first */
952 _lclose16( hFileRet );
954 return hFileRet;
956 not_found: /* We get here if the file does not exist */
957 WARN("'%s' not found\n", name );
958 SetLastError( ERROR_FILE_NOT_FOUND );
959 /* fall through */
961 error: /* We get here if there was an error opening the file */
962 ofs->nErrCode = GetLastError();
963 WARN("(%s): return = HFILE_ERROR error= %d\n",
964 name,ofs->nErrCode );
965 return HFILE_ERROR;
969 /***********************************************************************
970 * OpenFile16 (KERNEL.74)
972 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
974 return FILE_DoOpenFile( name, ofs, mode, FALSE );
978 /***********************************************************************
979 * OpenFile (KERNEL32.396)
981 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
983 return FILE_DoOpenFile( name, ofs, mode, TRUE );
987 /***********************************************************************
988 * FILE_InitProcessDosHandles
990 * Allocates the default DOS handles for a process. Called either by
991 * AllocDosHandle below or by the DOSVM stuff.
993 BOOL FILE_InitProcessDosHandles( void ) {
994 HANDLE *ptr;
996 if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
997 sizeof(*ptr) * DOS_TABLE_SIZE )))
998 return FALSE;
999 PROCESS_Current()->dos_handles = ptr;
1000 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
1001 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
1002 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
1003 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
1004 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
1005 return TRUE;
1008 /***********************************************************************
1009 * FILE_AllocDosHandle
1011 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1012 * longer valid after this function (even on failure).
1014 HFILE16 FILE_AllocDosHandle( HANDLE handle )
1016 int i;
1017 HANDLE *ptr = PROCESS_Current()->dos_handles;
1019 if (!handle || (handle == INVALID_HANDLE_VALUE))
1020 return INVALID_HANDLE_VALUE16;
1022 if (!ptr) {
1023 if (!FILE_InitProcessDosHandles())
1024 goto error;
1025 ptr = PROCESS_Current()->dos_handles;
1028 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
1029 if (!*ptr)
1031 *ptr = handle;
1032 TRACE("Got %d for h32 %d\n", i, handle );
1033 return i;
1035 error:
1036 CloseHandle( handle );
1037 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1038 return INVALID_HANDLE_VALUE16;
1042 /***********************************************************************
1043 * FILE_GetHandle
1045 * Return the Win32 handle for a DOS handle.
1047 HANDLE FILE_GetHandle( HFILE16 hfile )
1049 HANDLE *table = PROCESS_Current()->dos_handles;
1050 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1052 SetLastError( ERROR_INVALID_HANDLE );
1053 return INVALID_HANDLE_VALUE;
1055 return table[hfile];
1059 /***********************************************************************
1060 * FILE_Dup2
1062 * dup2() function for DOS handles.
1064 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1066 HANDLE *table = PROCESS_Current()->dos_handles;
1067 HANDLE new_handle;
1069 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1070 !table || !table[hFile1])
1072 SetLastError( ERROR_INVALID_HANDLE );
1073 return HFILE_ERROR16;
1075 if (hFile2 < 5)
1077 FIXME("stdio handle closed, need proper conversion\n" );
1078 SetLastError( ERROR_INVALID_HANDLE );
1079 return HFILE_ERROR16;
1081 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1082 GetCurrentProcess(), &new_handle,
1083 0, FALSE, DUPLICATE_SAME_ACCESS ))
1084 return HFILE_ERROR16;
1085 if (table[hFile2]) CloseHandle( table[hFile2] );
1086 table[hFile2] = new_handle;
1087 return hFile2;
1091 /***********************************************************************
1092 * _lclose16 (KERNEL.81)
1094 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1096 HANDLE *table = PROCESS_Current()->dos_handles;
1098 if (hFile < 5)
1100 FIXME("stdio handle closed, need proper conversion\n" );
1101 SetLastError( ERROR_INVALID_HANDLE );
1102 return HFILE_ERROR16;
1104 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1106 SetLastError( ERROR_INVALID_HANDLE );
1107 return HFILE_ERROR16;
1109 TRACE("%d (handle32=%d)\n", hFile, table[hFile] );
1110 CloseHandle( table[hFile] );
1111 table[hFile] = 0;
1112 return 0;
1116 /***********************************************************************
1117 * _lclose (KERNEL32.592)
1119 HFILE WINAPI _lclose( HFILE hFile )
1121 TRACE("handle %d\n", hFile );
1122 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1125 /***********************************************************************
1126 * GetOverlappedResult (KERNEL32.360)
1128 BOOL WINAPI GetOverlappedResult(HANDLE hFile,LPOVERLAPPED lpOverlapped,
1129 LPDWORD lpNumberOfBytesTransferred,
1130 BOOL bWait)
1132 /* Since all i/o is currently synchronous,
1133 * return true, assuming ReadFile/WriteFile
1134 * have completed the operation */
1135 FIXME("NO Asynch I/O, assuming Read/Write succeeded\n" );
1136 return TRUE;
1139 /***********************************************************************
1140 * ReadFile (KERNEL32.428)
1142 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1143 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1145 struct get_read_fd_request *req = get_req_buffer();
1146 int unix_handle, result;
1148 TRACE("%d %p %ld\n", hFile, buffer, bytesToRead );
1150 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1151 if (!bytesToRead) return TRUE;
1153 if ( overlapped ) {
1154 SetLastError ( ERROR_INVALID_PARAMETER );
1155 return FALSE;
1158 req->handle = hFile;
1159 server_call_fd( REQ_GET_READ_FD, -1, &unix_handle );
1160 if (unix_handle == -1) return FALSE;
1161 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1163 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1164 if ((errno == EFAULT) && !VIRTUAL_HandleFault( buffer )) continue;
1165 FILE_SetDosError();
1166 break;
1168 close( unix_handle );
1169 if (result == -1) return FALSE;
1170 if (bytesRead) *bytesRead = result;
1171 return TRUE;
1175 /***********************************************************************
1176 * WriteFile (KERNEL32.578)
1178 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1179 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1181 struct get_write_fd_request *req = get_req_buffer();
1182 int unix_handle, result;
1184 TRACE("%d %p %ld\n", hFile, buffer, bytesToWrite );
1186 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1187 if (!bytesToWrite) return TRUE;
1189 if ( overlapped ) {
1190 SetLastError ( ERROR_INVALID_PARAMETER );
1191 return FALSE;
1194 req->handle = hFile;
1195 server_call_fd( REQ_GET_WRITE_FD, -1, &unix_handle );
1196 if (unix_handle == -1) return FALSE;
1197 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1199 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1200 if ((errno == EFAULT) && !VIRTUAL_HandleFault( buffer )) continue;
1201 if (errno == ENOSPC)
1202 SetLastError( ERROR_DISK_FULL );
1203 else
1204 FILE_SetDosError();
1205 break;
1207 close( unix_handle );
1208 if (result == -1) return FALSE;
1209 if (bytesWritten) *bytesWritten = result;
1210 return TRUE;
1214 /***********************************************************************
1215 * WIN16_hread
1217 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1219 LONG maxlen;
1221 TRACE("%d %08lx %ld\n",
1222 hFile, (DWORD)buffer, count );
1224 /* Some programs pass a count larger than the allocated buffer */
1225 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1226 if (count > maxlen) count = maxlen;
1227 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1231 /***********************************************************************
1232 * WIN16_lread
1234 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1236 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1240 /***********************************************************************
1241 * _lread (KERNEL32.596)
1243 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1245 DWORD result;
1246 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1247 return result;
1251 /***********************************************************************
1252 * _lread16 (KERNEL.82)
1254 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1256 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1260 /***********************************************************************
1261 * _lcreat16 (KERNEL.83)
1263 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1265 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1269 /***********************************************************************
1270 * _lcreat (KERNEL32.593)
1272 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1274 /* Mask off all flags not explicitly allowed by the doc */
1275 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1276 TRACE("%s %02x\n", path, attr );
1277 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1278 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1279 CREATE_ALWAYS, attr, -1 );
1283 /***********************************************************************
1284 * SetFilePointer (KERNEL32.492)
1286 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword,
1287 DWORD method )
1289 struct set_file_pointer_request *req = get_req_buffer();
1291 if (highword &&
1292 ((distance >= 0 && *highword != 0) || (distance < 0 && *highword != -1)))
1294 FIXME("64-bit offsets not supported yet\n"
1295 "SetFilePointer(%08x,%08lx,%08lx,%08lx)\n",
1296 hFile,distance,*highword,method);
1297 SetLastError( ERROR_INVALID_PARAMETER );
1298 return 0xffffffff;
1300 TRACE("handle %d offset %ld origin %ld\n",
1301 hFile, distance, method );
1303 req->handle = hFile;
1304 req->low = distance;
1305 req->high = highword ? *highword : (distance >= 0) ? 0 : -1;
1306 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1307 req->whence = method;
1308 SetLastError( 0 );
1309 if (server_call( REQ_SET_FILE_POINTER )) return 0xffffffff;
1310 if (highword) *highword = req->new_high;
1311 return req->new_low;
1315 /***********************************************************************
1316 * _llseek16 (KERNEL.84)
1318 * FIXME:
1319 * Seeking before the start of the file should be allowed for _llseek16,
1320 * but cause subsequent I/O operations to fail (cf. interrupt list)
1323 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1325 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1329 /***********************************************************************
1330 * _llseek (KERNEL32.594)
1332 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1334 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1338 /***********************************************************************
1339 * _lopen16 (KERNEL.85)
1341 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1343 return FILE_AllocDosHandle( _lopen( path, mode ) );
1347 /***********************************************************************
1348 * _lopen (KERNEL32.595)
1350 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1352 DWORD access, sharing;
1354 TRACE("('%s',%04x)\n", path, mode );
1355 FILE_ConvertOFMode( mode, &access, &sharing );
1356 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1360 /***********************************************************************
1361 * _lwrite16 (KERNEL.86)
1363 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1365 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1368 /***********************************************************************
1369 * _lwrite (KERNEL32.761)
1371 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1373 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1377 /***********************************************************************
1378 * _hread16 (KERNEL.349)
1380 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1382 return _lread( FILE_GetHandle(hFile), buffer, count );
1386 /***********************************************************************
1387 * _hread (KERNEL32.590)
1389 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1391 return _lread( hFile, buffer, count );
1395 /***********************************************************************
1396 * _hwrite16 (KERNEL.350)
1398 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1400 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1404 /***********************************************************************
1405 * _hwrite (KERNEL32.591)
1407 * experimentation yields that _lwrite:
1408 * o truncates the file at the current position with
1409 * a 0 len write
1410 * o returns 0 on a 0 length write
1411 * o works with console handles
1414 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1416 DWORD result;
1418 TRACE("%d %p %ld\n", handle, buffer, count );
1420 if (!count)
1422 /* Expand or truncate at current position */
1423 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1424 return 0;
1426 if (!WriteFile( handle, buffer, count, &result, NULL ))
1427 return HFILE_ERROR;
1428 return result;
1432 /***********************************************************************
1433 * SetHandleCount16 (KERNEL.199)
1435 UINT16 WINAPI SetHandleCount16( UINT16 count )
1437 HGLOBAL16 hPDB = GetCurrentPDB16();
1438 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1439 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1441 TRACE("(%d)\n", count );
1443 if (count < 20) count = 20; /* No point in going below 20 */
1444 else if (count > 254) count = 254;
1446 if (count == 20)
1448 if (pdb->nbFiles > 20)
1450 memcpy( pdb->fileHandles, files, 20 );
1451 GlobalFree16( pdb->hFileHandles );
1452 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1453 GlobalHandleToSel16( hPDB ) );
1454 pdb->hFileHandles = 0;
1455 pdb->nbFiles = 20;
1458 else /* More than 20, need a new file handles table */
1460 BYTE *newfiles;
1461 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1462 if (!newhandle)
1464 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1465 return pdb->nbFiles;
1467 newfiles = (BYTE *)GlobalLock16( newhandle );
1469 if (count > pdb->nbFiles)
1471 memcpy( newfiles, files, pdb->nbFiles );
1472 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1474 else memcpy( newfiles, files, count );
1475 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1476 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1477 pdb->hFileHandles = newhandle;
1478 pdb->nbFiles = count;
1480 return pdb->nbFiles;
1484 /*************************************************************************
1485 * SetHandleCount (KERNEL32.494)
1487 UINT WINAPI SetHandleCount( UINT count )
1489 return min( 256, count );
1493 /***********************************************************************
1494 * FlushFileBuffers (KERNEL32.133)
1496 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
1498 struct flush_file_request *req = get_req_buffer();
1499 req->handle = hFile;
1500 return !server_call( REQ_FLUSH_FILE );
1504 /**************************************************************************
1505 * SetEndOfFile (KERNEL32.483)
1507 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1509 struct truncate_file_request *req = get_req_buffer();
1510 req->handle = hFile;
1511 return !server_call( REQ_TRUNCATE_FILE );
1515 /***********************************************************************
1516 * DeleteFile16 (KERNEL.146)
1518 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1520 return DeleteFileA( path );
1524 /***********************************************************************
1525 * DeleteFileA (KERNEL32.71)
1527 BOOL WINAPI DeleteFileA( LPCSTR path )
1529 DOS_FULL_NAME full_name;
1531 TRACE("'%s'\n", path );
1533 if (!*path)
1535 ERR("Empty path passed\n");
1536 return FALSE;
1538 if (DOSFS_GetDevice( path ))
1540 WARN("cannot remove DOS device '%s'!\n", path);
1541 SetLastError( ERROR_FILE_NOT_FOUND );
1542 return FALSE;
1545 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1546 if (unlink( full_name.long_name ) == -1)
1548 FILE_SetDosError();
1549 return FALSE;
1551 return TRUE;
1555 /***********************************************************************
1556 * DeleteFileW (KERNEL32.72)
1558 BOOL WINAPI DeleteFileW( LPCWSTR path )
1560 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1561 BOOL ret = DeleteFileA( xpath );
1562 HeapFree( GetProcessHeap(), 0, xpath );
1563 return ret;
1567 /***********************************************************************
1568 * FILE_dommap
1570 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1571 DWORD size_high, DWORD size_low,
1572 DWORD offset_high, DWORD offset_low,
1573 int prot, int flags )
1575 int fd = -1;
1576 int pos;
1577 LPVOID ret;
1579 if (size_high || offset_high)
1580 FIXME("offsets larger than 4Gb not supported\n");
1582 if (unix_handle == -1)
1584 #ifdef MAP_ANON
1585 flags |= MAP_ANON;
1586 #else
1587 static int fdzero = -1;
1589 if (fdzero == -1)
1591 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1593 perror( "/dev/zero: open" );
1594 exit(1);
1597 fd = fdzero;
1598 #endif /* MAP_ANON */
1599 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1600 #ifdef MAP_SHARED
1601 flags &= ~MAP_SHARED;
1602 #endif
1603 #ifdef MAP_PRIVATE
1604 flags |= MAP_PRIVATE;
1605 #endif
1607 else fd = unix_handle;
1609 if ((ret = mmap( start, size_low, prot,
1610 flags, fd, offset_low )) != (LPVOID)-1)
1611 return ret;
1613 /* mmap() failed; if this is because the file offset is not */
1614 /* page-aligned (EINVAL), or because the underlying filesystem */
1615 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
1617 if (unix_handle == -1) return ret;
1618 if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return ret;
1619 if (prot & PROT_WRITE)
1621 /* We cannot fake shared write mappings */
1622 #ifdef MAP_SHARED
1623 if (flags & MAP_SHARED) return ret;
1624 #endif
1625 #ifdef MAP_PRIVATE
1626 if (!(flags & MAP_PRIVATE)) return ret;
1627 #endif
1629 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1630 /* Reserve the memory with an anonymous mmap */
1631 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1632 PROT_READ | PROT_WRITE, flags );
1633 if (ret == (LPVOID)-1) return ret;
1634 /* Now read in the file */
1635 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1637 FILE_munmap( ret, size_high, size_low );
1638 return (LPVOID)-1;
1640 read( fd, ret, size_low );
1641 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1642 mprotect( ret, size_low, prot ); /* Set the right protection */
1643 return ret;
1647 /***********************************************************************
1648 * FILE_munmap
1650 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1652 if (size_high)
1653 FIXME("offsets larger than 4Gb not supported\n");
1654 return munmap( start, size_low );
1658 /***********************************************************************
1659 * GetFileType (KERNEL32.222)
1661 DWORD WINAPI GetFileType( HANDLE hFile )
1663 struct get_file_info_request *req = get_req_buffer();
1664 req->handle = hFile;
1665 if (server_call( REQ_GET_FILE_INFO )) return FILE_TYPE_UNKNOWN;
1666 return req->type;
1670 /**************************************************************************
1671 * MoveFileExA (KERNEL32.???)
1673 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1675 DOS_FULL_NAME full_name1, full_name2;
1677 TRACE("(%s,%s,%04lx)\n", fn1, fn2, flag);
1679 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1681 if (fn2) /* !fn2 means delete fn1 */
1683 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1685 /* target exists, check if we may overwrite */
1686 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1688 /* FIXME: Use right error code */
1689 SetLastError( ERROR_ACCESS_DENIED );
1690 return FALSE;
1693 else if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1695 /* Source name and target path are valid */
1697 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1699 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1700 Perhaps we should queue these command and execute it
1701 when exiting... What about using on_exit(2)
1703 FIXME("Please move existing file '%s' to file '%s' when Wine has finished\n",
1704 full_name1.long_name, full_name2.long_name);
1705 return TRUE;
1708 if (full_name1.drive != full_name2.drive)
1710 /* use copy, if allowed */
1711 if (!(flag & MOVEFILE_COPY_ALLOWED))
1713 /* FIXME: Use right error code */
1714 SetLastError( ERROR_FILE_EXISTS );
1715 return FALSE;
1717 return CopyFileA( fn1, fn2, !(flag & MOVEFILE_REPLACE_EXISTING) );
1719 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1721 FILE_SetDosError();
1722 return FALSE;
1724 return TRUE;
1726 else /* fn2 == NULL means delete source */
1728 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1730 if (flag & MOVEFILE_COPY_ALLOWED) {
1731 WARN("Illegal flag\n");
1732 SetLastError( ERROR_GEN_FAILURE );
1733 return FALSE;
1735 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1736 Perhaps we should queue these command and execute it
1737 when exiting... What about using on_exit(2)
1739 FIXME("Please delete file '%s' when Wine has finished\n",
1740 full_name1.long_name);
1741 return TRUE;
1744 if (unlink( full_name1.long_name ) == -1)
1746 FILE_SetDosError();
1747 return FALSE;
1749 return TRUE; /* successfully deleted */
1753 /**************************************************************************
1754 * MoveFileExW (KERNEL32.???)
1756 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1758 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1759 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1760 BOOL res = MoveFileExA( afn1, afn2, flag );
1761 HeapFree( GetProcessHeap(), 0, afn1 );
1762 HeapFree( GetProcessHeap(), 0, afn2 );
1763 return res;
1767 /**************************************************************************
1768 * MoveFileA (KERNEL32.387)
1770 * Move file or directory
1772 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1774 DOS_FULL_NAME full_name1, full_name2;
1775 struct stat fstat;
1777 TRACE("(%s,%s)\n", fn1, fn2 );
1779 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1780 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1781 /* The new name must not already exist */
1782 return FALSE;
1783 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1785 if (full_name1.drive == full_name2.drive) /* move */
1786 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1788 FILE_SetDosError();
1789 return FALSE;
1791 else return TRUE;
1792 else /*copy */ {
1793 if (stat( full_name1.long_name, &fstat ))
1795 WARN("Invalid source file %s\n",
1796 full_name1.long_name);
1797 FILE_SetDosError();
1798 return FALSE;
1800 if (S_ISDIR(fstat.st_mode)) {
1801 /* No Move for directories across file systems */
1802 /* FIXME: Use right error code */
1803 SetLastError( ERROR_GEN_FAILURE );
1804 return FALSE;
1806 else
1807 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1812 /**************************************************************************
1813 * MoveFileW (KERNEL32.390)
1815 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1817 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1818 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1819 BOOL res = MoveFileA( afn1, afn2 );
1820 HeapFree( GetProcessHeap(), 0, afn1 );
1821 HeapFree( GetProcessHeap(), 0, afn2 );
1822 return res;
1826 /**************************************************************************
1827 * CopyFileA (KERNEL32.36)
1829 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1831 HFILE h1, h2;
1832 BY_HANDLE_FILE_INFORMATION info;
1833 UINT count;
1834 BOOL ret = FALSE;
1835 int mode;
1836 char buffer[2048];
1838 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1839 if (!GetFileInformationByHandle( h1, &info ))
1841 CloseHandle( h1 );
1842 return FALSE;
1844 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1845 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1846 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1847 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1849 CloseHandle( h1 );
1850 return FALSE;
1852 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1854 char *p = buffer;
1855 while (count > 0)
1857 INT res = _lwrite( h2, p, count );
1858 if (res <= 0) goto done;
1859 p += res;
1860 count -= res;
1863 ret = TRUE;
1864 done:
1865 CloseHandle( h1 );
1866 CloseHandle( h2 );
1867 return ret;
1871 /**************************************************************************
1872 * CopyFileW (KERNEL32.37)
1874 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1876 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1877 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1878 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1879 HeapFree( GetProcessHeap(), 0, sourceA );
1880 HeapFree( GetProcessHeap(), 0, destA );
1881 return ret;
1885 /**************************************************************************
1886 * CopyFileExA (KERNEL32.858)
1888 * This implementation ignores most of the extra parameters passed-in into
1889 * the "ex" version of the method and calls the CopyFile method.
1890 * It will have to be fixed eventually.
1892 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1893 LPCSTR destFilename,
1894 LPPROGRESS_ROUTINE progressRoutine,
1895 LPVOID appData,
1896 LPBOOL cancelFlagPointer,
1897 DWORD copyFlags)
1899 BOOL failIfExists = FALSE;
1902 * Interpret the only flag that CopyFile can interpret.
1904 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1906 failIfExists = TRUE;
1909 return CopyFileA(sourceFilename, destFilename, failIfExists);
1912 /**************************************************************************
1913 * CopyFileExW (KERNEL32.859)
1915 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1916 LPCWSTR destFilename,
1917 LPPROGRESS_ROUTINE progressRoutine,
1918 LPVOID appData,
1919 LPBOOL cancelFlagPointer,
1920 DWORD copyFlags)
1922 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1923 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1925 BOOL ret = CopyFileExA(sourceA,
1926 destA,
1927 progressRoutine,
1928 appData,
1929 cancelFlagPointer,
1930 copyFlags);
1932 HeapFree( GetProcessHeap(), 0, sourceA );
1933 HeapFree( GetProcessHeap(), 0, destA );
1935 return ret;
1939 /***********************************************************************
1940 * SetFileTime (KERNEL32.650)
1942 BOOL WINAPI SetFileTime( HANDLE hFile,
1943 const FILETIME *lpCreationTime,
1944 const FILETIME *lpLastAccessTime,
1945 const FILETIME *lpLastWriteTime )
1947 struct set_file_time_request *req = get_req_buffer();
1949 req->handle = hFile;
1950 if (lpLastAccessTime)
1951 req->access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1952 else
1953 req->access_time = 0; /* FIXME */
1954 if (lpLastWriteTime)
1955 req->write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1956 else
1957 req->write_time = 0; /* FIXME */
1958 return !server_call( REQ_SET_FILE_TIME );
1962 /**************************************************************************
1963 * LockFile (KERNEL32.511)
1965 BOOL WINAPI LockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1966 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1968 struct lock_file_request *req = get_req_buffer();
1970 req->handle = hFile;
1971 req->offset_low = dwFileOffsetLow;
1972 req->offset_high = dwFileOffsetHigh;
1973 req->count_low = nNumberOfBytesToLockLow;
1974 req->count_high = nNumberOfBytesToLockHigh;
1975 return !server_call( REQ_LOCK_FILE );
1978 /**************************************************************************
1979 * LockFileEx [KERNEL32.512]
1981 * Locks a byte range within an open file for shared or exclusive access.
1983 * RETURNS
1984 * success: TRUE
1985 * failure: FALSE
1986 * NOTES
1988 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1990 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1991 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1992 LPOVERLAPPED pOverlapped )
1994 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1995 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
1996 pOverlapped);
1997 if (reserved == 0)
1998 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1999 else
2001 ERR("reserved == %ld: Supposed to be 0??\n", reserved);
2002 SetLastError(ERROR_INVALID_PARAMETER);
2005 return FALSE;
2009 /**************************************************************************
2010 * UnlockFile (KERNEL32.703)
2012 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
2013 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
2015 struct unlock_file_request *req = get_req_buffer();
2017 req->handle = hFile;
2018 req->offset_low = dwFileOffsetLow;
2019 req->offset_high = dwFileOffsetHigh;
2020 req->count_low = nNumberOfBytesToUnlockLow;
2021 req->count_high = nNumberOfBytesToUnlockHigh;
2022 return !server_call( REQ_UNLOCK_FILE );
2026 /**************************************************************************
2027 * UnlockFileEx (KERNEL32.705)
2029 BOOL WINAPI UnlockFileEx(
2030 HFILE hFile,
2031 DWORD dwReserved,
2032 DWORD nNumberOfBytesToUnlockLow,
2033 DWORD nNumberOfBytesToUnlockHigh,
2034 LPOVERLAPPED lpOverlapped
2037 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2038 hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh,
2039 lpOverlapped);
2040 if (dwReserved == 0)
2041 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2042 else
2044 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved);
2045 SetLastError(ERROR_INVALID_PARAMETER);
2048 return FALSE;
2052 #if 0
2054 struct DOS_FILE_LOCK {
2055 struct DOS_FILE_LOCK * next;
2056 DWORD base;
2057 DWORD len;
2058 DWORD processId;
2059 FILE_OBJECT * dos_file;
2060 /* char * unix_name;*/
2063 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2065 static DOS_FILE_LOCK *locks = NULL;
2066 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2069 /* Locks need to be mirrored because unix file locking is based
2070 * on the pid. Inside of wine there can be multiple WINE processes
2071 * that share the same unix pid.
2072 * Read's and writes should check these locks also - not sure
2073 * how critical that is at this point (FIXME).
2076 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2078 DOS_FILE_LOCK *curr;
2079 DWORD processId;
2081 processId = GetCurrentProcessId();
2083 /* check if lock overlaps a current lock for the same file */
2084 #if 0
2085 for (curr = locks; curr; curr = curr->next) {
2086 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2087 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2088 return TRUE;/* region is identic */
2089 if ((f->l_start < (curr->base + curr->len)) &&
2090 ((f->l_start + f->l_len) > curr->base)) {
2091 /* region overlaps */
2092 return FALSE;
2096 #endif
2098 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(DOS_FILE_LOCK) );
2099 curr->processId = GetCurrentProcessId();
2100 curr->base = f->l_start;
2101 curr->len = f->l_len;
2102 /* curr->unix_name = HEAP_strdupA( GetProcessHeap(), 0, file->unix_name);*/
2103 curr->next = locks;
2104 curr->dos_file = file;
2105 locks = curr;
2106 return TRUE;
2109 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2111 DWORD processId;
2112 DOS_FILE_LOCK **curr;
2113 DOS_FILE_LOCK *rem;
2115 processId = GetCurrentProcessId();
2116 curr = &locks;
2117 while (*curr) {
2118 if ((*curr)->dos_file == file) {
2119 rem = *curr;
2120 *curr = (*curr)->next;
2121 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2122 HeapFree( GetProcessHeap(), 0, rem );
2124 else
2125 curr = &(*curr)->next;
2129 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2131 DWORD processId;
2132 DOS_FILE_LOCK **curr;
2133 DOS_FILE_LOCK *rem;
2135 processId = GetCurrentProcessId();
2136 for (curr = &locks; *curr; curr = &(*curr)->next) {
2137 if ((*curr)->processId == processId &&
2138 (*curr)->dos_file == file &&
2139 (*curr)->base == f->l_start &&
2140 (*curr)->len == f->l_len) {
2141 /* this is the same lock */
2142 rem = *curr;
2143 *curr = (*curr)->next;
2144 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2145 HeapFree( GetProcessHeap(), 0, rem );
2146 return TRUE;
2149 /* no matching lock found */
2150 return FALSE;
2154 /**************************************************************************
2155 * LockFile (KERNEL32.511)
2157 BOOL WINAPI LockFile(
2158 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2159 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2161 struct flock f;
2162 FILE_OBJECT *file;
2164 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2165 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2166 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2168 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2169 FIXME("Unimplemented bytes > 32bits\n");
2170 return FALSE;
2173 f.l_start = dwFileOffsetLow;
2174 f.l_len = nNumberOfBytesToLockLow;
2175 f.l_whence = SEEK_SET;
2176 f.l_pid = 0;
2177 f.l_type = F_WRLCK;
2179 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2181 /* shadow locks internally */
2182 if (!DOS_AddLock(file, &f)) {
2183 SetLastError( ERROR_LOCK_VIOLATION );
2184 return FALSE;
2187 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2188 #ifdef USE_UNIX_LOCKS
2189 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2190 if (errno == EACCES || errno == EAGAIN) {
2191 SetLastError( ERROR_LOCK_VIOLATION );
2193 else {
2194 FILE_SetDosError();
2196 /* remove our internal copy of the lock */
2197 DOS_RemoveLock(file, &f);
2198 return FALSE;
2200 #endif
2201 return TRUE;
2205 /**************************************************************************
2206 * UnlockFile (KERNEL32.703)
2208 BOOL WINAPI UnlockFile(
2209 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2210 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2212 FILE_OBJECT *file;
2213 struct flock f;
2215 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2216 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2217 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2219 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2220 WARN("Unimplemented bytes > 32bits\n");
2221 return FALSE;
2224 f.l_start = dwFileOffsetLow;
2225 f.l_len = nNumberOfBytesToUnlockLow;
2226 f.l_whence = SEEK_SET;
2227 f.l_pid = 0;
2228 f.l_type = F_UNLCK;
2230 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2232 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2234 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2235 #ifdef USE_UNIX_LOCKS
2236 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2237 FILE_SetDosError();
2238 return FALSE;
2240 #endif
2241 return TRUE;
2243 #endif
2245 /**************************************************************************
2246 * GetFileAttributesExA [KERNEL32.874]
2248 BOOL WINAPI GetFileAttributesExA(
2249 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2250 LPVOID lpFileInformation)
2252 DOS_FULL_NAME full_name;
2253 BY_HANDLE_FILE_INFORMATION info;
2255 if (lpFileName == NULL) return FALSE;
2256 if (lpFileInformation == NULL) return FALSE;
2258 if (fInfoLevelId == GetFileExInfoStandard) {
2259 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2260 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2261 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2262 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2264 lpFad->dwFileAttributes = info.dwFileAttributes;
2265 lpFad->ftCreationTime = info.ftCreationTime;
2266 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2267 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2268 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2269 lpFad->nFileSizeLow = info.nFileSizeLow;
2271 else {
2272 FIXME("invalid info level %d!\n", fInfoLevelId);
2273 return FALSE;
2276 return TRUE;
2280 /**************************************************************************
2281 * GetFileAttributesExW [KERNEL32.875]
2283 BOOL WINAPI GetFileAttributesExW(
2284 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2285 LPVOID lpFileInformation)
2287 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2288 BOOL res =
2289 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2290 HeapFree( GetProcessHeap(), 0, nameA );
2291 return res;