Removed winmm from import list.
[wine/multimedia.git] / files / file.c
blob48bd30242296836f2ae19e3556d63c34b9f13096
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 "device.h"
41 #include "file.h"
42 #include "global.h"
43 #include "heap.h"
44 #include "msdos.h"
45 #include "ldt.h"
46 #include "process.h"
47 #include "task.h"
48 #include "wincon.h"
49 #include "debugtools.h"
51 #include "server.h"
53 DEFAULT_DEBUG_CHANNEL(file);
55 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
56 #define MAP_ANON MAP_ANONYMOUS
57 #endif
59 /* Size of per-process table of DOS handles */
60 #define DOS_TABLE_SIZE 256
63 /***********************************************************************
64 * FILE_ConvertOFMode
66 * Convert OF_* mode into flags for CreateFile.
68 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
70 switch(mode & 0x03)
72 case OF_READ: *access = GENERIC_READ; break;
73 case OF_WRITE: *access = GENERIC_WRITE; break;
74 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
75 default: *access = 0; break;
77 switch(mode & 0x70)
79 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
80 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
81 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
82 case OF_SHARE_DENY_NONE:
83 case OF_SHARE_COMPAT:
84 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
89 #if 0
90 /***********************************************************************
91 * FILE_ShareDeny
93 * PARAMS
94 * oldmode[I] mode how file was first opened
95 * mode[I] mode how the file should get opened
96 * RETURNS
97 * TRUE: deny open
98 * FALSE: allow open
100 * Look what we have to do with the given SHARE modes
102 * Ralph Brown's interrupt list gives following explication, I guess
103 * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT
105 * FIXME: Validate this function
106 ========from Ralph Brown's list =========
107 (Table 0750)
108 Values of DOS file sharing behavior:
109 | Second and subsequent Opens
110 First |Compat Deny Deny Deny Deny
111 Open | All Write Read None
112 |R W RW R W RW R W RW R W RW R W RW
113 - - - - -| - - - - - - - - - - - - - - - - -
114 Compat R |Y Y Y N N N 1 N N N N N 1 N N
115 W |Y Y Y N N N N N N N N N N N N
116 RW|Y Y Y N N N N N N N N N N N N
117 - - - - -|
118 Deny R |C C C N N N N N N N N N N N N
119 All W |C C C N N N N N N N N N N N N
120 RW|C C C N N N N N N N N N N N N
121 - - - - -|
122 Deny R |2 C C N N N Y N N N N N Y N N
123 Write W |C C C N N N N N N Y N N Y N N
124 RW|C C C N N N N N N N N N Y N N
125 - - - - -|
126 Deny R |C C C N N N N Y N N N N N Y N
127 Read W |C C C N N N N N N N Y N N Y N
128 RW|C C C N N N N N N N N N N Y N
129 - - - - -|
130 Deny R |2 C C N N N Y Y Y N N N Y Y Y
131 None W |C C C N N N N N N Y Y Y Y Y Y
132 RW|C C C N N N N N N N N N Y Y Y
133 Legend: Y = open succeeds, N = open fails with error code 05h
134 C = open fails, INT 24 generated
135 1 = open succeeds if file read-only, else fails with error code
136 2 = open succeeds if file read-only, else fails with INT 24
137 ========end of description from Ralph Brown's List =====
138 For every "Y" in the table we return FALSE
139 For every "N" we set the DOS_ERROR and return TRUE
140 For all other cases we barf,set the DOS_ERROR and return TRUE
143 static BOOL FILE_ShareDeny( int mode, int oldmode)
145 int oldsharemode = oldmode & 0x70;
146 int sharemode = mode & 0x70;
147 int oldopenmode = oldmode & 3;
148 int openmode = mode & 3;
150 switch (oldsharemode)
152 case OF_SHARE_COMPAT:
153 if (sharemode == OF_SHARE_COMPAT) return FALSE;
154 if (openmode == OF_READ) goto test_ro_err05 ;
155 goto fail_error05;
156 case OF_SHARE_EXCLUSIVE:
157 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
158 goto fail_error05;
159 case OF_SHARE_DENY_WRITE:
160 if (openmode != OF_READ)
162 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
163 goto fail_error05;
165 switch (sharemode)
167 case OF_SHARE_COMPAT:
168 if (oldopenmode == OF_READ) goto test_ro_int24 ;
169 goto fail_int24;
170 case OF_SHARE_DENY_NONE :
171 return FALSE;
172 case OF_SHARE_DENY_WRITE :
173 if (oldopenmode == OF_READ) return FALSE;
174 case OF_SHARE_DENY_READ :
175 if (oldopenmode == OF_WRITE) return FALSE;
176 case OF_SHARE_EXCLUSIVE:
177 default:
178 goto fail_error05;
180 break;
181 case OF_SHARE_DENY_READ:
182 if (openmode != OF_WRITE)
184 if (sharemode == OF_SHARE_COMPAT) goto fail_int24;
185 goto fail_error05;
187 switch (sharemode)
189 case OF_SHARE_COMPAT:
190 goto fail_int24;
191 case OF_SHARE_DENY_NONE :
192 return FALSE;
193 case OF_SHARE_DENY_WRITE :
194 if (oldopenmode == OF_READ) return FALSE;
195 case OF_SHARE_DENY_READ :
196 if (oldopenmode == OF_WRITE) return FALSE;
197 case OF_SHARE_EXCLUSIVE:
198 default:
199 goto fail_error05;
201 break;
202 case OF_SHARE_DENY_NONE:
203 switch (sharemode)
205 case OF_SHARE_COMPAT:
206 goto fail_int24;
207 case OF_SHARE_DENY_NONE :
208 return FALSE;
209 case OF_SHARE_DENY_WRITE :
210 if (oldopenmode == OF_READ) return FALSE;
211 case OF_SHARE_DENY_READ :
212 if (oldopenmode == OF_WRITE) return FALSE;
213 case OF_SHARE_EXCLUSIVE:
214 default:
215 goto fail_error05;
217 default:
218 ERR("unknown mode\n");
220 ERR("shouldn't happen\n");
221 ERR("Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n");
222 return TRUE;
224 test_ro_int24:
225 if (oldmode == OF_READ)
226 return FALSE;
227 /* Fall through */
228 fail_int24:
229 FIXME("generate INT24 missing\n");
230 /* Is this the right error? */
231 SetLastError( ERROR_ACCESS_DENIED );
232 return TRUE;
234 test_ro_err05:
235 if (oldmode == OF_READ)
236 return FALSE;
237 /* fall through */
238 fail_error05:
239 TRACE("Access Denied, oldmode 0x%02x mode 0x%02x\n",oldmode,mode);
240 SetLastError( ERROR_ACCESS_DENIED );
241 return TRUE;
243 #endif
246 /***********************************************************************
247 * FILE_SetDosError
249 * Set the DOS error code from errno.
251 void FILE_SetDosError(void)
253 int save_errno = errno; /* errno gets overwritten by printf */
255 TRACE("errno = %d %s\n", errno, strerror(errno));
256 switch (save_errno)
258 case EAGAIN:
259 SetLastError( ERROR_SHARING_VIOLATION );
260 break;
261 case EBADF:
262 SetLastError( ERROR_INVALID_HANDLE );
263 break;
264 case ENOSPC:
265 SetLastError( ERROR_HANDLE_DISK_FULL );
266 break;
267 case EACCES:
268 case EPERM:
269 case EROFS:
270 SetLastError( ERROR_ACCESS_DENIED );
271 break;
272 case EBUSY:
273 SetLastError( ERROR_LOCK_VIOLATION );
274 break;
275 case ENOENT:
276 SetLastError( ERROR_FILE_NOT_FOUND );
277 break;
278 case EISDIR:
279 SetLastError( ERROR_CANNOT_MAKE );
280 break;
281 case ENFILE:
282 case EMFILE:
283 SetLastError( ERROR_NO_MORE_FILES );
284 break;
285 case EEXIST:
286 SetLastError( ERROR_FILE_EXISTS );
287 break;
288 case EINVAL:
289 case ESPIPE:
290 SetLastError( ERROR_SEEK );
291 break;
292 case ENOTEMPTY:
293 SetLastError( ERROR_DIR_NOT_EMPTY );
294 break;
295 default:
296 perror( "int21: unknown errno" );
297 SetLastError( ERROR_GEN_FAILURE );
298 break;
300 errno = save_errno;
304 /***********************************************************************
305 * FILE_DupUnixHandle
307 * Duplicate a Unix handle into a task handle.
309 HFILE FILE_DupUnixHandle( int fd, DWORD access )
311 struct alloc_file_handle_request *req = get_req_buffer();
312 req->access = access;
313 server_call_fd( REQ_ALLOC_FILE_HANDLE, fd, NULL );
314 return req->handle;
318 /***********************************************************************
319 * FILE_CreateFile
321 * Implementation of CreateFile. Takes a Unix path name.
323 HANDLE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
324 LPSECURITY_ATTRIBUTES sa, DWORD creation,
325 DWORD attributes, HANDLE template, BOOL fail_read_only )
327 DWORD err;
328 struct create_file_request *req = get_req_buffer();
330 restart:
331 req->access = access;
332 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
333 req->sharing = sharing;
334 req->create = creation;
335 req->attrs = attributes;
336 lstrcpynA( req->name, filename, server_remaining(req->name) );
337 SetLastError(0);
338 err = server_call( REQ_CREATE_FILE );
340 /* If write access failed, retry without GENERIC_WRITE */
342 if ((req->handle == -1) && !fail_read_only && (access & GENERIC_WRITE))
344 if ((err == STATUS_MEDIA_WRITE_PROTECTED) || (err == STATUS_ACCESS_DENIED))
346 TRACE("Write access failed for file '%s', trying without "
347 "write access", filename);
348 access &= ~GENERIC_WRITE;
349 goto restart;
353 if (req->handle == -1)
354 WARN("Unable to create file '%s' (GLE %ld)", filename,
355 GetLastError());
357 return req->handle;
361 /***********************************************************************
362 * FILE_CreateDevice
364 * Same as FILE_CreateFile but for a device
366 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
368 struct create_device_request *req = get_req_buffer();
370 req->access = access;
371 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
372 req->id = client_id;
373 SetLastError(0);
374 server_call( REQ_CREATE_DEVICE );
375 return req->handle;
379 /*************************************************************************
380 * CreateFileA [KERNEL32.45] Creates or opens a file or other object
382 * Creates or opens an object, and returns a handle that can be used to
383 * access that object.
385 * PARAMS
387 * filename [I] pointer to filename to be accessed
388 * access [I] access mode requested
389 * sharing [I] share mode
390 * sa [I] pointer to security attributes
391 * creation [I] how to create the file
392 * attributes [I] attributes for newly created file
393 * template [I] handle to file with extended attributes to copy
395 * RETURNS
396 * Success: Open handle to specified file
397 * Failure: INVALID_HANDLE_VALUE
399 * NOTES
400 * Should call SetLastError() on failure.
402 * BUGS
404 * Doesn't support character devices, pipes, template files, or a
405 * lot of the 'attributes' flags yet.
407 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
408 LPSECURITY_ATTRIBUTES sa, DWORD creation,
409 DWORD attributes, HANDLE template )
411 DOS_FULL_NAME full_name;
413 if (!filename)
415 SetLastError( ERROR_INVALID_PARAMETER );
416 return HFILE_ERROR;
418 TRACE("%s %s%s%s%s%s%s%s\n",filename,
419 ((access & GENERIC_READ)==GENERIC_READ)?"GENERIC_READ ":"",
420 ((access & GENERIC_WRITE)==GENERIC_WRITE)?"GENERIC_WRITE ":"",
421 (!access)?"QUERY_ACCESS ":"",
422 ((sharing & FILE_SHARE_READ)==FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
423 ((sharing & FILE_SHARE_WRITE)==FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
424 ((sharing & FILE_SHARE_DELETE)==FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
425 (creation ==CREATE_NEW)?"CREATE_NEW":
426 (creation ==CREATE_ALWAYS)?"CREATE_ALWAYS ":
427 (creation ==OPEN_EXISTING)?"OPEN_EXISTING ":
428 (creation ==OPEN_ALWAYS)?"OPEN_ALWAYS ":
429 (creation ==TRUNCATE_EXISTING)?"TRUNCATE_EXISTING ":"");
431 /* If the name starts with '\\?\', ignore the first 4 chars. */
432 if (!strncmp(filename, "\\\\?\\", 4))
434 filename += 4;
435 if (!strncmp(filename, "UNC\\", 4))
437 FIXME("UNC name (%s) not supported.\n", filename );
438 SetLastError( ERROR_PATH_NOT_FOUND );
439 return HFILE_ERROR;
443 if (!strncmp(filename, "\\\\.\\", 4)) {
444 if (!DOSFS_GetDevice( filename ))
445 return DEVICE_Open( filename+4, access, sa );
446 else
447 filename+=4; /* fall into DOSFS_Device case below */
450 /* If the name still starts with '\\', it's a UNC name. */
451 if (!strncmp(filename, "\\\\", 2))
453 FIXME("UNC name (%s) not supported.\n", filename );
454 SetLastError( ERROR_PATH_NOT_FOUND );
455 return HFILE_ERROR;
458 /* If the name contains a DOS wild card (* or ?), do no create a file */
459 if(strchr(filename,'*') || strchr(filename,'?'))
460 return HFILE_ERROR;
462 /* Open a console for CONIN$ or CONOUT$ */
463 if (!lstrcmpiA(filename, "CONIN$")) return CONSOLE_OpenHandle( FALSE, access, sa );
464 if (!lstrcmpiA(filename, "CONOUT$")) return CONSOLE_OpenHandle( TRUE, access, sa );
466 if (DOSFS_GetDevice( filename ))
468 HFILE ret;
470 TRACE("opening device '%s'\n", filename );
472 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
473 return ret;
475 /* Do not silence this please. It is a critical error. -MM */
476 ERR("Couldn't open device '%s'!\n",filename);
477 SetLastError( ERROR_FILE_NOT_FOUND );
478 return HFILE_ERROR;
481 /* check for filename, don't check for last entry if creating */
482 if (!DOSFS_GetFullName( filename,
483 (creation == OPEN_EXISTING) ||
484 (creation == TRUNCATE_EXISTING),
485 &full_name )) {
486 WARN("Unable to get full filename from '%s' (GLE %ld)\n",
487 filename, GetLastError());
488 return HFILE_ERROR;
491 return FILE_CreateFile( full_name.long_name, access, sharing,
492 sa, creation, attributes, template,
493 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
498 /*************************************************************************
499 * CreateFileW (KERNEL32.48)
501 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
502 LPSECURITY_ATTRIBUTES sa, DWORD creation,
503 DWORD attributes, HANDLE template)
505 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
506 HANDLE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
507 HeapFree( GetProcessHeap(), 0, afn );
508 return res;
512 /***********************************************************************
513 * FILE_FillInfo
515 * Fill a file information from a struct stat.
517 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
519 if (S_ISDIR(st->st_mode))
520 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
521 else
522 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
523 if (!(st->st_mode & S_IWUSR))
524 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
526 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 );
527 DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 );
528 DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 );
530 info->dwVolumeSerialNumber = 0; /* FIXME */
531 info->nFileSizeHigh = 0;
532 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
533 info->nNumberOfLinks = st->st_nlink;
534 info->nFileIndexHigh = 0;
535 info->nFileIndexLow = st->st_ino;
539 /***********************************************************************
540 * FILE_Stat
542 * Stat a Unix path name. Return TRUE if OK.
544 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
546 struct stat st;
548 if (!unixName || !info) return FALSE;
550 if (stat( unixName, &st ) == -1)
552 FILE_SetDosError();
553 return FALSE;
555 FILE_FillInfo( &st, info );
556 return TRUE;
560 /***********************************************************************
561 * GetFileInformationByHandle (KERNEL32.219)
563 DWORD WINAPI GetFileInformationByHandle( HANDLE hFile,
564 BY_HANDLE_FILE_INFORMATION *info )
566 struct get_file_info_request *req = get_req_buffer();
568 if (!info) return 0;
569 req->handle = hFile;
570 if (server_call( REQ_GET_FILE_INFO )) return 0;
571 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftCreationTime, 0 );
572 DOSFS_UnixTimeToFileTime( req->write_time, &info->ftLastWriteTime, 0 );
573 DOSFS_UnixTimeToFileTime( req->access_time, &info->ftLastAccessTime, 0 );
574 info->dwFileAttributes = req->attr;
575 info->dwVolumeSerialNumber = req->serial;
576 info->nFileSizeHigh = req->size_high;
577 info->nFileSizeLow = req->size_low;
578 info->nNumberOfLinks = req->links;
579 info->nFileIndexHigh = req->index_high;
580 info->nFileIndexLow = req->index_low;
581 return 1;
585 /**************************************************************************
586 * GetFileAttributes16 (KERNEL.420)
588 DWORD WINAPI GetFileAttributes16( LPCSTR name )
590 return GetFileAttributesA( name );
594 /**************************************************************************
595 * GetFileAttributesA (KERNEL32.217)
597 DWORD WINAPI GetFileAttributesA( LPCSTR name )
599 DOS_FULL_NAME full_name;
600 BY_HANDLE_FILE_INFORMATION info;
602 if (name == NULL || *name=='\0') return -1;
604 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
605 if (!FILE_Stat( full_name.long_name, &info )) return -1;
606 return info.dwFileAttributes;
610 /**************************************************************************
611 * GetFileAttributesW (KERNEL32.218)
613 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
615 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
616 DWORD res = GetFileAttributesA( nameA );
617 HeapFree( GetProcessHeap(), 0, nameA );
618 return res;
622 /***********************************************************************
623 * GetFileSize (KERNEL32.220)
625 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
627 BY_HANDLE_FILE_INFORMATION info;
628 if (!GetFileInformationByHandle( hFile, &info )) return 0;
629 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
630 return info.nFileSizeLow;
634 /***********************************************************************
635 * GetFileTime (KERNEL32.221)
637 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
638 FILETIME *lpLastAccessTime,
639 FILETIME *lpLastWriteTime )
641 BY_HANDLE_FILE_INFORMATION info;
642 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
643 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
644 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
645 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
646 return TRUE;
649 /***********************************************************************
650 * CompareFileTime (KERNEL32.28)
652 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
654 if (!x || !y) return -1;
656 if (x->dwHighDateTime > y->dwHighDateTime)
657 return 1;
658 if (x->dwHighDateTime < y->dwHighDateTime)
659 return -1;
660 if (x->dwLowDateTime > y->dwLowDateTime)
661 return 1;
662 if (x->dwLowDateTime < y->dwLowDateTime)
663 return -1;
664 return 0;
667 /***********************************************************************
668 * FILE_GetTempFileName : utility for GetTempFileName
670 static UINT FILE_GetTempFileName( LPCSTR path, LPCSTR prefix, UINT unique,
671 LPSTR buffer, BOOL isWin16 )
673 static UINT unique_temp;
674 DOS_FULL_NAME full_name;
675 int i;
676 LPSTR p;
677 UINT num;
679 if ( !path || !prefix || !buffer ) return 0;
681 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
682 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
684 strcpy( buffer, path );
685 p = buffer + strlen(buffer);
687 /* add a \, if there isn't one and path is more than just the drive letter ... */
688 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
689 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
691 if (isWin16) *p++ = '~';
692 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
693 sprintf( p, "%04x.tmp", num );
695 /* Now try to create it */
697 if (!unique)
701 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
702 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
703 if (handle != INVALID_HANDLE_VALUE)
704 { /* We created it */
705 TRACE("created %s\n",
706 buffer);
707 CloseHandle( handle );
708 break;
710 if (GetLastError() != ERROR_FILE_EXISTS)
711 break; /* No need to go on */
712 num++;
713 sprintf( p, "%04x.tmp", num );
714 } while (num != (unique & 0xffff));
717 /* Get the full path name */
719 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
721 /* Check if we have write access in the directory */
722 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
723 if (access( full_name.long_name, W_OK ) == -1)
724 WARN("returns '%s', which doesn't seem to be writeable.\n",
725 buffer);
727 TRACE("returning %s\n", buffer );
728 return unique ? unique : num;
732 /***********************************************************************
733 * GetTempFileNameA (KERNEL32.290)
735 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
736 LPSTR buffer)
738 return FILE_GetTempFileName(path, prefix, unique, buffer, FALSE);
741 /***********************************************************************
742 * GetTempFileNameW (KERNEL32.291)
744 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
745 LPWSTR buffer )
747 LPSTR patha,prefixa;
748 char buffera[144];
749 UINT ret;
751 if (!path) return 0;
752 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
753 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
754 ret = FILE_GetTempFileName( patha, prefixa, unique, buffera, FALSE );
755 lstrcpyAtoW( buffer, buffera );
756 HeapFree( GetProcessHeap(), 0, patha );
757 HeapFree( GetProcessHeap(), 0, prefixa );
758 return ret;
762 /***********************************************************************
763 * GetTempFileName16 (KERNEL.97)
765 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
766 LPSTR buffer )
768 char temppath[144];
770 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
771 drive |= DRIVE_GetCurrentDrive() + 'A';
773 if ((drive & TF_FORCEDRIVE) &&
774 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
776 drive &= ~TF_FORCEDRIVE;
777 WARN("invalid drive %d specified\n", drive );
780 if (drive & TF_FORCEDRIVE)
781 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
782 else
783 GetTempPathA( 132, temppath );
784 return (UINT16)FILE_GetTempFileName( temppath, prefix, unique, buffer, TRUE );
787 /***********************************************************************
788 * FILE_DoOpenFile
790 * Implementation of OpenFile16() and OpenFile32().
792 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
793 BOOL win32 )
795 HFILE hFileRet;
796 FILETIME filetime;
797 WORD filedatetime[2];
798 DOS_FULL_NAME full_name;
799 DWORD access, sharing;
800 char *p;
802 if (!ofs) return HFILE_ERROR;
804 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
805 ((mode & 0x3 )==OF_READ)?"OF_READ":
806 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
807 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
808 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
809 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
810 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
811 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
812 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
813 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
814 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
815 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
816 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
817 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
818 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
819 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
820 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
821 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
825 ofs->cBytes = sizeof(OFSTRUCT);
826 ofs->nErrCode = 0;
827 if (mode & OF_REOPEN) name = ofs->szPathName;
829 if (!name) {
830 ERR("called with `name' set to NULL ! Please debug.\n");
831 return HFILE_ERROR;
834 TRACE("%s %04x\n", name, mode );
836 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
837 Are there any cases where getting the path here is wrong?
838 Uwe Bonnes 1997 Apr 2 */
839 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
840 ofs->szPathName, NULL )) goto error;
841 FILE_ConvertOFMode( mode, &access, &sharing );
843 /* OF_PARSE simply fills the structure */
845 if (mode & OF_PARSE)
847 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
848 != DRIVE_REMOVABLE);
849 TRACE("(%s): OF_PARSE, res = '%s'\n",
850 name, ofs->szPathName );
851 return 0;
854 /* OF_CREATE is completely different from all other options, so
855 handle it first */
857 if (mode & OF_CREATE)
859 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
860 sharing, NULL, CREATE_ALWAYS,
861 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
862 goto error;
863 goto success;
866 /* If OF_SEARCH is set, ignore the given path */
868 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
870 /* First try the file name as is */
871 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
872 /* Now remove the path */
873 if (name[0] && (name[1] == ':')) name += 2;
874 if ((p = strrchr( name, '\\' ))) name = p + 1;
875 if ((p = strrchr( name, '/' ))) name = p + 1;
876 if (!name[0]) goto not_found;
879 /* Now look for the file */
881 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
883 found:
884 TRACE("found %s = %s\n",
885 full_name.long_name, full_name.short_name );
886 lstrcpynA( ofs->szPathName, full_name.short_name,
887 sizeof(ofs->szPathName) );
889 if (mode & OF_SHARE_EXCLUSIVE)
890 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
891 on the file <tempdir>/_ins0432._mp to determine how
892 far installation has proceeded.
893 _ins0432._mp is an executable and while running the
894 application expects the open with OF_SHARE_ to fail*/
895 /* Probable FIXME:
896 As our loader closes the files after loading the executable,
897 we can't find the running executable with FILE_InUse.
898 Perhaps the loader should keep the file open.
899 Recheck against how Win handles that case */
901 char *last = strrchr(full_name.long_name,'/');
902 if (!last)
903 last = full_name.long_name - 1;
904 if (GetModuleHandle16(last+1))
906 TRACE("Denying shared open for %s\n",full_name.long_name);
907 return HFILE_ERROR;
911 if (mode & OF_DELETE)
913 if (unlink( full_name.long_name ) == -1) goto not_found;
914 TRACE("(%s): OF_DELETE return = OK\n", name);
915 return 1;
918 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
919 NULL, OPEN_EXISTING, 0, -1,
920 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
921 if (hFileRet == HFILE_ERROR) goto not_found;
923 GetFileTime( hFileRet, NULL, NULL, &filetime );
924 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
925 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
927 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
929 CloseHandle( hFileRet );
930 WARN("(%s): OF_VERIFY failed\n", name );
931 /* FIXME: what error here? */
932 SetLastError( ERROR_FILE_NOT_FOUND );
933 goto error;
936 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
938 success: /* We get here if the open was successful */
939 TRACE("(%s): OK, return = %d\n", name, hFileRet );
940 if (win32)
942 if (mode & OF_EXIST) /* Return the handle, but close it first */
943 CloseHandle( hFileRet );
945 else
947 hFileRet = FILE_AllocDosHandle( hFileRet );
948 if (hFileRet == HFILE_ERROR16) goto error;
949 if (mode & OF_EXIST) /* Return the handle, but close it first */
950 _lclose16( hFileRet );
952 return hFileRet;
954 not_found: /* We get here if the file does not exist */
955 WARN("'%s' not found\n", name );
956 SetLastError( ERROR_FILE_NOT_FOUND );
957 /* fall through */
959 error: /* We get here if there was an error opening the file */
960 ofs->nErrCode = GetLastError();
961 WARN("(%s): return = HFILE_ERROR error= %d\n",
962 name,ofs->nErrCode );
963 return HFILE_ERROR;
967 /***********************************************************************
968 * OpenFile16 (KERNEL.74)
970 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
972 return FILE_DoOpenFile( name, ofs, mode, FALSE );
976 /***********************************************************************
977 * OpenFile (KERNEL32.396)
979 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
981 return FILE_DoOpenFile( name, ofs, mode, TRUE );
985 /***********************************************************************
986 * FILE_InitProcessDosHandles
988 * Allocates the default DOS handles for a process. Called either by
989 * AllocDosHandle below or by the DOSVM stuff.
991 BOOL FILE_InitProcessDosHandles( void ) {
992 HANDLE *ptr;
994 if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
995 sizeof(*ptr) * DOS_TABLE_SIZE )))
996 return FALSE;
997 PROCESS_Current()->dos_handles = ptr;
998 ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
999 ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
1000 ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
1001 ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
1002 ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
1003 return TRUE;
1006 /***********************************************************************
1007 * FILE_AllocDosHandle
1009 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1010 * longer valid after this function (even on failure).
1012 HFILE16 FILE_AllocDosHandle( HANDLE handle )
1014 int i;
1015 HANDLE *ptr = PROCESS_Current()->dos_handles;
1017 if (!handle || (handle == INVALID_HANDLE_VALUE))
1018 return INVALID_HANDLE_VALUE16;
1020 if (!ptr) {
1021 if (!FILE_InitProcessDosHandles())
1022 goto error;
1023 ptr = PROCESS_Current()->dos_handles;
1026 for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
1027 if (!*ptr)
1029 *ptr = handle;
1030 TRACE("Got %d for h32 %d\n", i, handle );
1031 return i;
1033 error:
1034 CloseHandle( handle );
1035 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1036 return INVALID_HANDLE_VALUE16;
1040 /***********************************************************************
1041 * FILE_GetHandle
1043 * Return the Win32 handle for a DOS handle.
1045 HANDLE FILE_GetHandle( HFILE16 hfile )
1047 HANDLE *table = PROCESS_Current()->dos_handles;
1048 if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
1050 SetLastError( ERROR_INVALID_HANDLE );
1051 return INVALID_HANDLE_VALUE;
1053 return table[hfile];
1057 /***********************************************************************
1058 * FILE_Dup2
1060 * dup2() function for DOS handles.
1062 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1064 HANDLE *table = PROCESS_Current()->dos_handles;
1065 HANDLE new_handle;
1067 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
1068 !table || !table[hFile1])
1070 SetLastError( ERROR_INVALID_HANDLE );
1071 return HFILE_ERROR16;
1073 if (hFile2 < 5)
1075 FIXME("stdio handle closed, need proper conversion\n" );
1076 SetLastError( ERROR_INVALID_HANDLE );
1077 return HFILE_ERROR16;
1079 if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
1080 GetCurrentProcess(), &new_handle,
1081 0, FALSE, DUPLICATE_SAME_ACCESS ))
1082 return HFILE_ERROR16;
1083 if (table[hFile2]) CloseHandle( table[hFile2] );
1084 table[hFile2] = new_handle;
1085 return hFile2;
1089 /***********************************************************************
1090 * _lclose16 (KERNEL.81)
1092 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1094 HANDLE *table = PROCESS_Current()->dos_handles;
1096 if (hFile < 5)
1098 FIXME("stdio handle closed, need proper conversion\n" );
1099 SetLastError( ERROR_INVALID_HANDLE );
1100 return HFILE_ERROR16;
1102 if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
1104 SetLastError( ERROR_INVALID_HANDLE );
1105 return HFILE_ERROR16;
1107 TRACE("%d (handle32=%d)\n", hFile, table[hFile] );
1108 CloseHandle( table[hFile] );
1109 table[hFile] = 0;
1110 return 0;
1114 /***********************************************************************
1115 * _lclose (KERNEL32.592)
1117 HFILE WINAPI _lclose( HFILE hFile )
1119 TRACE("handle %d\n", hFile );
1120 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1123 /***********************************************************************
1124 * GetOverlappedResult (KERNEL32.360)
1126 BOOL WINAPI GetOverlappedResult(HANDLE hFile,LPOVERLAPPED lpOverlapped,
1127 LPDWORD lpNumberOfBytesTransferred,
1128 BOOL bWait)
1130 /* Since all i/o is currently synchronuos,
1131 * return true, assuming ReadFile/WriteFile
1132 * have completed the operation */
1133 FIXME("NO Asynch I/O, assuming Read/Write succeeded\n" );
1134 return TRUE;
1137 /***********************************************************************
1138 * ReadFile (KERNEL32.428)
1140 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1141 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1143 struct get_read_fd_request *req = get_req_buffer();
1144 int unix_handle, result;
1146 TRACE("%d %p %ld\n", hFile, buffer, bytesToRead );
1148 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1149 if (!bytesToRead) return TRUE;
1151 if ( overlapped ) {
1152 SetLastError ( ERROR_INVALID_PARAMETER );
1153 return FALSE;
1156 req->handle = hFile;
1157 server_call_fd( REQ_GET_READ_FD, -1, &unix_handle );
1158 if (unix_handle == -1) return FALSE;
1159 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1161 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1162 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1163 FILE_SetDosError();
1164 break;
1166 close( unix_handle );
1167 if (result == -1) return FALSE;
1168 if (bytesRead) *bytesRead = result;
1169 return TRUE;
1173 /***********************************************************************
1174 * WriteFile (KERNEL32.578)
1176 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1177 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1179 struct get_write_fd_request *req = get_req_buffer();
1180 int unix_handle, result;
1182 TRACE("%d %p %ld\n", hFile, buffer, bytesToWrite );
1184 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1185 if (!bytesToWrite) return TRUE;
1187 if ( overlapped ) {
1188 SetLastError ( ERROR_INVALID_PARAMETER );
1189 return FALSE;
1192 req->handle = hFile;
1193 server_call_fd( REQ_GET_WRITE_FD, -1, &unix_handle );
1194 if (unix_handle == -1) return FALSE;
1195 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1197 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1198 if ((errno == EFAULT) && VIRTUAL_HandleFault( buffer )) continue;
1199 if (errno == ENOSPC)
1200 SetLastError( ERROR_DISK_FULL );
1201 else
1202 FILE_SetDosError();
1203 break;
1205 close( unix_handle );
1206 if (result == -1) return FALSE;
1207 if (bytesWritten) *bytesWritten = result;
1208 return TRUE;
1212 /***********************************************************************
1213 * WIN16_hread
1215 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1217 LONG maxlen;
1219 TRACE("%d %08lx %ld\n",
1220 hFile, (DWORD)buffer, count );
1222 /* Some programs pass a count larger than the allocated buffer */
1223 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1224 if (count > maxlen) count = maxlen;
1225 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1229 /***********************************************************************
1230 * WIN16_lread
1232 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1234 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1238 /***********************************************************************
1239 * _lread (KERNEL32.596)
1241 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1243 DWORD result;
1244 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1245 return result;
1249 /***********************************************************************
1250 * _lread16 (KERNEL.82)
1252 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1254 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1258 /***********************************************************************
1259 * _lcreat16 (KERNEL.83)
1261 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1263 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1267 /***********************************************************************
1268 * _lcreat (KERNEL32.593)
1270 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1272 /* Mask off all flags not explicitly allowed by the doc */
1273 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1274 TRACE("%s %02x\n", path, attr );
1275 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1276 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1277 CREATE_ALWAYS, attr, -1 );
1281 /***********************************************************************
1282 * SetFilePointer (KERNEL32.492)
1284 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword,
1285 DWORD method )
1287 struct set_file_pointer_request *req = get_req_buffer();
1289 if (highword && *highword)
1291 FIXME("64-bit offsets not supported yet\n");
1292 SetLastError( ERROR_INVALID_PARAMETER );
1293 return 0xffffffff;
1295 TRACE("handle %d offset %ld origin %ld\n",
1296 hFile, distance, method );
1298 req->handle = hFile;
1299 req->low = distance;
1300 req->high = highword ? *highword : 0;
1301 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1302 req->whence = method;
1303 SetLastError( 0 );
1304 if (server_call( REQ_SET_FILE_POINTER )) return 0xffffffff;
1305 if (highword) *highword = req->new_high;
1306 return req->new_low;
1310 /***********************************************************************
1311 * _llseek16 (KERNEL.84)
1313 * FIXME:
1314 * Seeking before the start of the file should be allowed for _llseek16,
1315 * but cause subsequent I/O operations to fail (cf. interrupt list)
1318 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1320 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1324 /***********************************************************************
1325 * _llseek (KERNEL32.594)
1327 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1329 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1333 /***********************************************************************
1334 * _lopen16 (KERNEL.85)
1336 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1338 return FILE_AllocDosHandle( _lopen( path, mode ) );
1342 /***********************************************************************
1343 * _lopen (KERNEL32.595)
1345 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1347 DWORD access, sharing;
1349 TRACE("('%s',%04x)\n", path, mode );
1350 FILE_ConvertOFMode( mode, &access, &sharing );
1351 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1355 /***********************************************************************
1356 * _lwrite16 (KERNEL.86)
1358 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1360 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1363 /***********************************************************************
1364 * _lwrite (KERNEL32.761)
1366 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1368 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1372 /***********************************************************************
1373 * _hread16 (KERNEL.349)
1375 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1377 return _lread( FILE_GetHandle(hFile), buffer, count );
1381 /***********************************************************************
1382 * _hread (KERNEL32.590)
1384 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1386 return _lread( hFile, buffer, count );
1390 /***********************************************************************
1391 * _hwrite16 (KERNEL.350)
1393 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1395 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1399 /***********************************************************************
1400 * _hwrite (KERNEL32.591)
1402 * experimentation yields that _lwrite:
1403 * o truncates the file at the current position with
1404 * a 0 len write
1405 * o returns 0 on a 0 length write
1406 * o works with console handles
1409 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1411 DWORD result;
1413 TRACE("%d %p %ld\n", handle, buffer, count );
1415 if (!count)
1417 /* Expand or truncate at current position */
1418 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1419 return 0;
1421 if (!WriteFile( handle, buffer, count, &result, NULL ))
1422 return HFILE_ERROR;
1423 return result;
1427 /***********************************************************************
1428 * SetHandleCount16 (KERNEL.199)
1430 UINT16 WINAPI SetHandleCount16( UINT16 count )
1432 HGLOBAL16 hPDB = GetCurrentPDB16();
1433 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1434 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1436 TRACE("(%d)\n", count );
1438 if (count < 20) count = 20; /* No point in going below 20 */
1439 else if (count > 254) count = 254;
1441 if (count == 20)
1443 if (pdb->nbFiles > 20)
1445 memcpy( pdb->fileHandles, files, 20 );
1446 GlobalFree16( pdb->hFileHandles );
1447 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1448 GlobalHandleToSel16( hPDB ) );
1449 pdb->hFileHandles = 0;
1450 pdb->nbFiles = 20;
1453 else /* More than 20, need a new file handles table */
1455 BYTE *newfiles;
1456 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1457 if (!newhandle)
1459 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1460 return pdb->nbFiles;
1462 newfiles = (BYTE *)GlobalLock16( newhandle );
1464 if (count > pdb->nbFiles)
1466 memcpy( newfiles, files, pdb->nbFiles );
1467 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1469 else memcpy( newfiles, files, count );
1470 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1471 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1472 pdb->hFileHandles = newhandle;
1473 pdb->nbFiles = count;
1475 return pdb->nbFiles;
1479 /*************************************************************************
1480 * SetHandleCount (KERNEL32.494)
1482 UINT WINAPI SetHandleCount( UINT count )
1484 return min( 256, count );
1488 /***********************************************************************
1489 * FlushFileBuffers (KERNEL32.133)
1491 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
1493 struct flush_file_request *req = get_req_buffer();
1494 req->handle = hFile;
1495 return !server_call( REQ_FLUSH_FILE );
1499 /**************************************************************************
1500 * SetEndOfFile (KERNEL32.483)
1502 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1504 struct truncate_file_request *req = get_req_buffer();
1505 req->handle = hFile;
1506 return !server_call( REQ_TRUNCATE_FILE );
1510 /***********************************************************************
1511 * DeleteFile16 (KERNEL.146)
1513 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1515 return DeleteFileA( path );
1519 /***********************************************************************
1520 * DeleteFileA (KERNEL32.71)
1522 BOOL WINAPI DeleteFileA( LPCSTR path )
1524 DOS_FULL_NAME full_name;
1526 TRACE("'%s'\n", path );
1528 if (!*path)
1530 ERR("Empty path passed\n");
1531 return FALSE;
1533 if (DOSFS_GetDevice( path ))
1535 WARN("cannot remove DOS device '%s'!\n", path);
1536 SetLastError( ERROR_FILE_NOT_FOUND );
1537 return FALSE;
1540 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1541 if (unlink( full_name.long_name ) == -1)
1543 FILE_SetDosError();
1544 return FALSE;
1546 return TRUE;
1550 /***********************************************************************
1551 * DeleteFileW (KERNEL32.72)
1553 BOOL WINAPI DeleteFileW( LPCWSTR path )
1555 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1556 BOOL ret = DeleteFileA( xpath );
1557 HeapFree( GetProcessHeap(), 0, xpath );
1558 return ret;
1562 /***********************************************************************
1563 * FILE_dommap
1565 LPVOID FILE_dommap( int unix_handle, LPVOID start,
1566 DWORD size_high, DWORD size_low,
1567 DWORD offset_high, DWORD offset_low,
1568 int prot, int flags )
1570 int fd = -1;
1571 int pos;
1572 LPVOID ret;
1574 if (size_high || offset_high)
1575 FIXME("offsets larger than 4Gb not supported\n");
1577 if (unix_handle == -1)
1579 #ifdef MAP_ANON
1580 flags |= MAP_ANON;
1581 #else
1582 static int fdzero = -1;
1584 if (fdzero == -1)
1586 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
1588 perror( "/dev/zero: open" );
1589 exit(1);
1592 fd = fdzero;
1593 #endif /* MAP_ANON */
1594 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
1595 #ifdef MAP_SHARED
1596 flags &= ~MAP_SHARED;
1597 #endif
1598 #ifdef MAP_PRIVATE
1599 flags |= MAP_PRIVATE;
1600 #endif
1602 else fd = unix_handle;
1604 if ((ret = mmap( start, size_low, prot,
1605 flags, fd, offset_low )) != (LPVOID)-1)
1606 return ret;
1608 /* mmap() failed; if this is because the file offset is not */
1609 /* page-aligned (EINVAL), or because the underlying filesystem */
1610 /* does not support mmap() (ENOEXEC), we do it by hand. */
1612 if (unix_handle == -1) return ret;
1613 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
1614 if (prot & PROT_WRITE)
1616 /* We cannot fake shared write mappings */
1617 #ifdef MAP_SHARED
1618 if (flags & MAP_SHARED) return ret;
1619 #endif
1620 #ifdef MAP_PRIVATE
1621 if (!(flags & MAP_PRIVATE)) return ret;
1622 #endif
1624 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
1625 /* Reserve the memory with an anonymous mmap */
1626 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
1627 PROT_READ | PROT_WRITE, flags );
1628 if (ret == (LPVOID)-1) return ret;
1629 /* Now read in the file */
1630 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
1632 FILE_munmap( ret, size_high, size_low );
1633 return (LPVOID)-1;
1635 read( fd, ret, size_low );
1636 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
1637 mprotect( ret, size_low, prot ); /* Set the right protection */
1638 return ret;
1642 /***********************************************************************
1643 * FILE_munmap
1645 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
1647 if (size_high)
1648 FIXME("offsets larger than 4Gb not supported\n");
1649 return munmap( start, size_low );
1653 /***********************************************************************
1654 * GetFileType (KERNEL32.222)
1656 DWORD WINAPI GetFileType( HANDLE hFile )
1658 struct get_file_info_request *req = get_req_buffer();
1659 req->handle = hFile;
1660 if (server_call( REQ_GET_FILE_INFO )) return FILE_TYPE_UNKNOWN;
1661 return req->type;
1665 /**************************************************************************
1666 * MoveFileExA (KERNEL32.???)
1668 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1670 DOS_FULL_NAME full_name1, full_name2;
1672 TRACE("(%s,%s,%04lx)\n", fn1, fn2, flag);
1674 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1676 if (fn2) /* !fn2 means delete fn1 */
1678 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1680 /* target exists, check if we may overwrite */
1681 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1683 /* FIXME: Use right error code */
1684 SetLastError( ERROR_ACCESS_DENIED );
1685 return FALSE;
1688 else if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1690 /* Source name and target path are valid */
1692 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1694 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1695 Perhaps we should queue these command and execute it
1696 when exiting... What about using on_exit(2)
1698 FIXME("Please move existing file '%s' to file '%s' when Wine has finished\n",
1699 full_name1.long_name, full_name2.long_name);
1700 return TRUE;
1703 if (full_name1.drive != full_name2.drive)
1705 /* use copy, if allowed */
1706 if (!(flag & MOVEFILE_COPY_ALLOWED))
1708 /* FIXME: Use right error code */
1709 SetLastError( ERROR_FILE_EXISTS );
1710 return FALSE;
1712 return CopyFileA( fn1, fn2, !(flag & MOVEFILE_REPLACE_EXISTING) );
1714 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1716 FILE_SetDosError();
1717 return FALSE;
1719 return TRUE;
1721 else /* fn2 == NULL means delete source */
1723 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1725 if (flag & MOVEFILE_COPY_ALLOWED) {
1726 WARN("Illegal flag\n");
1727 SetLastError( ERROR_GEN_FAILURE );
1728 return FALSE;
1730 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1731 Perhaps we should queue these command and execute it
1732 when exiting... What about using on_exit(2)
1734 FIXME("Please delete file '%s' when Wine has finished\n",
1735 full_name1.long_name);
1736 return TRUE;
1739 if (unlink( full_name1.long_name ) == -1)
1741 FILE_SetDosError();
1742 return FALSE;
1744 return TRUE; /* successfully deleted */
1748 /**************************************************************************
1749 * MoveFileExW (KERNEL32.???)
1751 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1753 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1754 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1755 BOOL res = MoveFileExA( afn1, afn2, flag );
1756 HeapFree( GetProcessHeap(), 0, afn1 );
1757 HeapFree( GetProcessHeap(), 0, afn2 );
1758 return res;
1762 /**************************************************************************
1763 * MoveFileA (KERNEL32.387)
1765 * Move file or directory
1767 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1769 DOS_FULL_NAME full_name1, full_name2;
1770 struct stat fstat;
1772 TRACE("(%s,%s)\n", fn1, fn2 );
1774 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1775 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1776 /* The new name must not already exist */
1777 return FALSE;
1778 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1780 if (full_name1.drive == full_name2.drive) /* move */
1781 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1783 FILE_SetDosError();
1784 return FALSE;
1786 else return TRUE;
1787 else /*copy */ {
1788 if (stat( full_name1.long_name, &fstat ))
1790 WARN("Invalid source file %s\n",
1791 full_name1.long_name);
1792 FILE_SetDosError();
1793 return FALSE;
1795 if (S_ISDIR(fstat.st_mode)) {
1796 /* No Move for directories across file systems */
1797 /* FIXME: Use right error code */
1798 SetLastError( ERROR_GEN_FAILURE );
1799 return FALSE;
1801 else
1802 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1807 /**************************************************************************
1808 * MoveFileW (KERNEL32.390)
1810 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1812 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1813 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1814 BOOL res = MoveFileA( afn1, afn2 );
1815 HeapFree( GetProcessHeap(), 0, afn1 );
1816 HeapFree( GetProcessHeap(), 0, afn2 );
1817 return res;
1821 /**************************************************************************
1822 * CopyFileA (KERNEL32.36)
1824 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1826 HFILE h1, h2;
1827 BY_HANDLE_FILE_INFORMATION info;
1828 UINT count;
1829 BOOL ret = FALSE;
1830 int mode;
1831 char buffer[2048];
1833 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1834 if (!GetFileInformationByHandle( h1, &info ))
1836 CloseHandle( h1 );
1837 return FALSE;
1839 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1840 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1841 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1842 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1844 CloseHandle( h1 );
1845 return FALSE;
1847 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1849 char *p = buffer;
1850 while (count > 0)
1852 INT res = _lwrite( h2, p, count );
1853 if (res <= 0) goto done;
1854 p += res;
1855 count -= res;
1858 ret = TRUE;
1859 done:
1860 CloseHandle( h1 );
1861 CloseHandle( h2 );
1862 return ret;
1866 /**************************************************************************
1867 * CopyFileW (KERNEL32.37)
1869 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1871 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1872 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1873 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1874 HeapFree( GetProcessHeap(), 0, sourceA );
1875 HeapFree( GetProcessHeap(), 0, destA );
1876 return ret;
1880 /**************************************************************************
1881 * CopyFileExA (KERNEL32.858)
1883 * This implementation ignores most of the extra parameters passed-in into
1884 * the "ex" version of the method and calls the CopyFile method.
1885 * It will have to be fixed eventually.
1887 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1888 LPCSTR destFilename,
1889 LPPROGRESS_ROUTINE progressRoutine,
1890 LPVOID appData,
1891 LPBOOL cancelFlagPointer,
1892 DWORD copyFlags)
1894 BOOL failIfExists = FALSE;
1897 * Interpret the only flag that CopyFile can interpret.
1899 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1901 failIfExists = TRUE;
1904 return CopyFileA(sourceFilename, destFilename, failIfExists);
1907 /**************************************************************************
1908 * CopyFileExW (KERNEL32.859)
1910 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1911 LPCWSTR destFilename,
1912 LPPROGRESS_ROUTINE progressRoutine,
1913 LPVOID appData,
1914 LPBOOL cancelFlagPointer,
1915 DWORD copyFlags)
1917 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1918 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1920 BOOL ret = CopyFileExA(sourceA,
1921 destA,
1922 progressRoutine,
1923 appData,
1924 cancelFlagPointer,
1925 copyFlags);
1927 HeapFree( GetProcessHeap(), 0, sourceA );
1928 HeapFree( GetProcessHeap(), 0, destA );
1930 return ret;
1934 /***********************************************************************
1935 * SetFileTime (KERNEL32.650)
1937 BOOL WINAPI SetFileTime( HANDLE hFile,
1938 const FILETIME *lpCreationTime,
1939 const FILETIME *lpLastAccessTime,
1940 const FILETIME *lpLastWriteTime )
1942 struct set_file_time_request *req = get_req_buffer();
1944 req->handle = hFile;
1945 if (lpLastAccessTime)
1946 req->access_time = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL);
1947 else
1948 req->access_time = 0; /* FIXME */
1949 if (lpLastWriteTime)
1950 req->write_time = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL);
1951 else
1952 req->write_time = 0; /* FIXME */
1953 return !server_call( REQ_SET_FILE_TIME );
1957 /**************************************************************************
1958 * LockFile (KERNEL32.511)
1960 BOOL WINAPI LockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1961 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1963 struct lock_file_request *req = get_req_buffer();
1965 req->handle = hFile;
1966 req->offset_low = dwFileOffsetLow;
1967 req->offset_high = dwFileOffsetHigh;
1968 req->count_low = nNumberOfBytesToLockLow;
1969 req->count_high = nNumberOfBytesToLockHigh;
1970 return !server_call( REQ_LOCK_FILE );
1973 /**************************************************************************
1974 * LockFileEx [KERNEL32.512]
1976 * Locks a byte range within an open file for shared or exclusive access.
1978 * RETURNS
1979 * success: TRUE
1980 * failure: FALSE
1981 * NOTES
1983 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1985 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1986 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1987 LPOVERLAPPED pOverlapped )
1989 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1990 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
1991 pOverlapped);
1992 if (reserved == 0)
1993 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1994 else
1996 ERR("reserved == %ld: Supposed to be 0??\n", reserved);
1997 SetLastError(ERROR_INVALID_PARAMETER);
2000 return FALSE;
2004 /**************************************************************************
2005 * UnlockFile (KERNEL32.703)
2007 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
2008 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
2010 struct unlock_file_request *req = get_req_buffer();
2012 req->handle = hFile;
2013 req->offset_low = dwFileOffsetLow;
2014 req->offset_high = dwFileOffsetHigh;
2015 req->count_low = nNumberOfBytesToUnlockLow;
2016 req->count_high = nNumberOfBytesToUnlockHigh;
2017 return !server_call( REQ_UNLOCK_FILE );
2021 /**************************************************************************
2022 * UnlockFileEx (KERNEL32.705)
2024 BOOL WINAPI UnlockFileEx(
2025 HFILE hFile,
2026 DWORD dwReserved,
2027 DWORD nNumberOfBytesToUnlockLow,
2028 DWORD nNumberOfBytesToUnlockHigh,
2029 LPOVERLAPPED lpOverlapped
2032 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2033 hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh,
2034 lpOverlapped);
2035 if (dwReserved == 0)
2036 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2037 else
2039 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved);
2040 SetLastError(ERROR_INVALID_PARAMETER);
2043 return FALSE;
2047 #if 0
2049 struct DOS_FILE_LOCK {
2050 struct DOS_FILE_LOCK * next;
2051 DWORD base;
2052 DWORD len;
2053 DWORD processId;
2054 FILE_OBJECT * dos_file;
2055 /* char * unix_name;*/
2058 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2060 static DOS_FILE_LOCK *locks = NULL;
2061 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2064 /* Locks need to be mirrored because unix file locking is based
2065 * on the pid. Inside of wine there can be multiple WINE processes
2066 * that share the same unix pid.
2067 * Read's and writes should check these locks also - not sure
2068 * how critical that is at this point (FIXME).
2071 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2073 DOS_FILE_LOCK *curr;
2074 DWORD processId;
2076 processId = GetCurrentProcessId();
2078 /* check if lock overlaps a current lock for the same file */
2079 #if 0
2080 for (curr = locks; curr; curr = curr->next) {
2081 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2082 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2083 return TRUE;/* region is identic */
2084 if ((f->l_start < (curr->base + curr->len)) &&
2085 ((f->l_start + f->l_len) > curr->base)) {
2086 /* region overlaps */
2087 return FALSE;
2091 #endif
2093 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(DOS_FILE_LOCK) );
2094 curr->processId = GetCurrentProcessId();
2095 curr->base = f->l_start;
2096 curr->len = f->l_len;
2097 /* curr->unix_name = HEAP_strdupA( GetProcessHeap(), 0, file->unix_name);*/
2098 curr->next = locks;
2099 curr->dos_file = file;
2100 locks = curr;
2101 return TRUE;
2104 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2106 DWORD processId;
2107 DOS_FILE_LOCK **curr;
2108 DOS_FILE_LOCK *rem;
2110 processId = GetCurrentProcessId();
2111 curr = &locks;
2112 while (*curr) {
2113 if ((*curr)->dos_file == file) {
2114 rem = *curr;
2115 *curr = (*curr)->next;
2116 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2117 HeapFree( GetProcessHeap(), 0, rem );
2119 else
2120 curr = &(*curr)->next;
2124 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2126 DWORD processId;
2127 DOS_FILE_LOCK **curr;
2128 DOS_FILE_LOCK *rem;
2130 processId = GetCurrentProcessId();
2131 for (curr = &locks; *curr; curr = &(*curr)->next) {
2132 if ((*curr)->processId == processId &&
2133 (*curr)->dos_file == file &&
2134 (*curr)->base == f->l_start &&
2135 (*curr)->len == f->l_len) {
2136 /* this is the same lock */
2137 rem = *curr;
2138 *curr = (*curr)->next;
2139 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2140 HeapFree( GetProcessHeap(), 0, rem );
2141 return TRUE;
2144 /* no matching lock found */
2145 return FALSE;
2149 /**************************************************************************
2150 * LockFile (KERNEL32.511)
2152 BOOL WINAPI LockFile(
2153 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2154 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2156 struct flock f;
2157 FILE_OBJECT *file;
2159 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2160 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2161 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2163 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2164 FIXME("Unimplemented bytes > 32bits\n");
2165 return FALSE;
2168 f.l_start = dwFileOffsetLow;
2169 f.l_len = nNumberOfBytesToLockLow;
2170 f.l_whence = SEEK_SET;
2171 f.l_pid = 0;
2172 f.l_type = F_WRLCK;
2174 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2176 /* shadow locks internally */
2177 if (!DOS_AddLock(file, &f)) {
2178 SetLastError( ERROR_LOCK_VIOLATION );
2179 return FALSE;
2182 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2183 #ifdef USE_UNIX_LOCKS
2184 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2185 if (errno == EACCES || errno == EAGAIN) {
2186 SetLastError( ERROR_LOCK_VIOLATION );
2188 else {
2189 FILE_SetDosError();
2191 /* remove our internal copy of the lock */
2192 DOS_RemoveLock(file, &f);
2193 return FALSE;
2195 #endif
2196 return TRUE;
2200 /**************************************************************************
2201 * UnlockFile (KERNEL32.703)
2203 BOOL WINAPI UnlockFile(
2204 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2205 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2207 FILE_OBJECT *file;
2208 struct flock f;
2210 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2211 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2212 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2214 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2215 WARN("Unimplemented bytes > 32bits\n");
2216 return FALSE;
2219 f.l_start = dwFileOffsetLow;
2220 f.l_len = nNumberOfBytesToUnlockLow;
2221 f.l_whence = SEEK_SET;
2222 f.l_pid = 0;
2223 f.l_type = F_UNLCK;
2225 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2227 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2229 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2230 #ifdef USE_UNIX_LOCKS
2231 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2232 FILE_SetDosError();
2233 return FALSE;
2235 #endif
2236 return TRUE;
2238 #endif
2240 /**************************************************************************
2241 * GetFileAttributesExA [KERNEL32.874]
2243 BOOL WINAPI GetFileAttributesExA(
2244 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2245 LPVOID lpFileInformation)
2247 DOS_FULL_NAME full_name;
2248 BY_HANDLE_FILE_INFORMATION info;
2250 if (lpFileName == NULL) return FALSE;
2251 if (lpFileInformation == NULL) return FALSE;
2253 if (fInfoLevelId == GetFileExInfoStandard) {
2254 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2255 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2256 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2257 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2259 lpFad->dwFileAttributes = info.dwFileAttributes;
2260 lpFad->ftCreationTime = info.ftCreationTime;
2261 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2262 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2263 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2264 lpFad->nFileSizeLow = info.nFileSizeLow;
2266 else {
2267 FIXME("invalid info level %d!\n", fInfoLevelId);
2268 return FALSE;
2271 return TRUE;
2275 /**************************************************************************
2276 * GetFileAttributesExW [KERNEL32.875]
2278 BOOL WINAPI GetFileAttributesExW(
2279 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2280 LPVOID lpFileInformation)
2282 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2283 BOOL res =
2284 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2285 HeapFree( GetProcessHeap(), 0, nameA );
2286 return res;