Got rid of the Wine internal lstrcpy* functions and of winestring.h.
[wine/gsoc_dplay.git] / files / file.c
blob11971621cd4c7303309efe827146768d1a608d0d
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 "extended" 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 "drive.h"
39 #include "file.h"
40 #include "global.h"
41 #include "heap.h"
42 #include "msdos.h"
43 #include "ldt.h"
44 #include "task.h"
45 #include "wincon.h"
46 #include "debugtools.h"
48 #include "server.h"
50 DEFAULT_DEBUG_CHANNEL(file);
52 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
53 #define MAP_ANON MAP_ANONYMOUS
54 #endif
56 /* Size of per-process table of DOS handles */
57 #define DOS_TABLE_SIZE 256
59 static HANDLE dos_handles[DOS_TABLE_SIZE];
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 WARN( "unknown file error: %s", strerror(save_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_GetUnixHandle
323 * Retrieve the Unix handle corresponding to a file handle.
325 int FILE_GetUnixHandle( HANDLE handle, DWORD access )
327 int unix_handle = -1;
328 if (access == GENERIC_READ)
330 struct get_read_fd_request *req = get_req_buffer();
331 req->handle = handle;
332 server_call_fd( REQ_GET_READ_FD, -1, &unix_handle );
334 else if (access == GENERIC_WRITE)
336 struct get_write_fd_request *req = get_req_buffer();
337 req->handle = handle;
338 server_call_fd( REQ_GET_WRITE_FD, -1, &unix_handle );
340 else ERR( "bad access %08lx\n", access );
341 return unix_handle;
345 /*************************************************************************
346 * FILE_OpenConsole
348 * Open a handle to the current process console.
350 static HANDLE FILE_OpenConsole( BOOL output, DWORD access, LPSECURITY_ATTRIBUTES sa )
352 int ret = -1;
354 SERVER_START_REQ
356 struct open_console_request *req = server_alloc_req( sizeof(*req), 0 );
358 req->output = output;
359 req->access = access;
360 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
361 SetLastError(0);
362 if (!server_call( REQ_OPEN_CONSOLE )) ret = req->handle;
364 SERVER_END_REQ;
365 return ret;
369 /***********************************************************************
370 * FILE_CreateFile
372 * Implementation of CreateFile. Takes a Unix path name.
374 HANDLE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
375 LPSECURITY_ATTRIBUTES sa, DWORD creation,
376 DWORD attributes, HANDLE template, BOOL fail_read_only )
378 DWORD err;
379 HANDLE ret;
380 size_t len = strlen(filename);
382 if (len > REQUEST_MAX_VAR_SIZE)
384 FIXME("filename '%s' too long\n", filename );
385 SetLastError( ERROR_INVALID_PARAMETER );
386 return -1;
389 restart:
390 SERVER_START_REQ
392 struct create_file_request *req = server_alloc_req( sizeof(*req), len );
393 req->access = access;
394 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
395 req->sharing = sharing;
396 req->create = creation;
397 req->attrs = attributes;
398 memcpy( server_data_ptr(req), filename, len );
399 SetLastError(0);
400 err = server_call( REQ_CREATE_FILE );
401 ret = req->handle;
403 SERVER_END_REQ;
405 /* If write access failed, retry without GENERIC_WRITE */
407 if ((ret == -1) && !fail_read_only && (access & GENERIC_WRITE))
409 if ((err == STATUS_MEDIA_WRITE_PROTECTED) || (err == STATUS_ACCESS_DENIED))
411 TRACE("Write access failed for file '%s', trying without "
412 "write access\n", filename);
413 access &= ~GENERIC_WRITE;
414 goto restart;
418 if (ret == -1)
419 WARN("Unable to create file '%s' (GLE %ld)\n", filename,
420 GetLastError());
422 return ret;
426 /***********************************************************************
427 * FILE_CreateDevice
429 * Same as FILE_CreateFile but for a device
431 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
433 HFILE ret;
434 SERVER_START_REQ
436 struct create_device_request *req = server_alloc_req( sizeof(*req), 0 );
438 req->access = access;
439 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
440 req->id = client_id;
441 SetLastError(0);
442 server_call( REQ_CREATE_DEVICE );
443 ret = req->handle;
445 SERVER_END_REQ;
446 return ret;
450 /*************************************************************************
451 * CreateFileA [KERNEL32.45] Creates or opens a file or other object
453 * Creates or opens an object, and returns a handle that can be used to
454 * access that object.
456 * PARAMS
458 * filename [I] pointer to filename to be accessed
459 * access [I] access mode requested
460 * sharing [I] share mode
461 * sa [I] pointer to security attributes
462 * creation [I] how to create the file
463 * attributes [I] attributes for newly created file
464 * template [I] handle to file with extended attributes to copy
466 * RETURNS
467 * Success: Open handle to specified file
468 * Failure: INVALID_HANDLE_VALUE
470 * NOTES
471 * Should call SetLastError() on failure.
473 * BUGS
475 * Doesn't support character devices, pipes, template files, or a
476 * lot of the 'attributes' flags yet.
478 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
479 LPSECURITY_ATTRIBUTES sa, DWORD creation,
480 DWORD attributes, HANDLE template )
482 DOS_FULL_NAME full_name;
484 if (!filename)
486 SetLastError( ERROR_INVALID_PARAMETER );
487 return HFILE_ERROR;
489 TRACE("%s %s%s%s%s%s%s%s\n",filename,
490 ((access & GENERIC_READ)==GENERIC_READ)?"GENERIC_READ ":"",
491 ((access & GENERIC_WRITE)==GENERIC_WRITE)?"GENERIC_WRITE ":"",
492 (!access)?"QUERY_ACCESS ":"",
493 ((sharing & FILE_SHARE_READ)==FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
494 ((sharing & FILE_SHARE_WRITE)==FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
495 ((sharing & FILE_SHARE_DELETE)==FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
496 (creation ==CREATE_NEW)?"CREATE_NEW":
497 (creation ==CREATE_ALWAYS)?"CREATE_ALWAYS ":
498 (creation ==OPEN_EXISTING)?"OPEN_EXISTING ":
499 (creation ==OPEN_ALWAYS)?"OPEN_ALWAYS ":
500 (creation ==TRUNCATE_EXISTING)?"TRUNCATE_EXISTING ":"");
502 /* If the name starts with '\\?\', ignore the first 4 chars. */
503 if (!strncmp(filename, "\\\\?\\", 4))
505 filename += 4;
506 if (!strncmp(filename, "UNC\\", 4))
508 FIXME("UNC name (%s) not supported.\n", filename );
509 SetLastError( ERROR_PATH_NOT_FOUND );
510 return HFILE_ERROR;
514 if (!strncmp(filename, "\\\\.\\", 4)) {
515 if (!DOSFS_GetDevice( filename ))
516 return DEVICE_Open( filename+4, access, sa );
517 else
518 filename+=4; /* fall into DOSFS_Device case below */
521 /* If the name still starts with '\\', it's a UNC name. */
522 if (!strncmp(filename, "\\\\", 2))
524 FIXME("UNC name (%s) not supported.\n", filename );
525 SetLastError( ERROR_PATH_NOT_FOUND );
526 return HFILE_ERROR;
529 /* If the name contains a DOS wild card (* or ?), do no create a file */
530 if(strchr(filename,'*') || strchr(filename,'?'))
531 return HFILE_ERROR;
533 /* Open a console for CONIN$ or CONOUT$ */
534 if (!strcasecmp(filename, "CONIN$")) return FILE_OpenConsole( FALSE, access, sa );
535 if (!strcasecmp(filename, "CONOUT$")) return FILE_OpenConsole( TRUE, access, sa );
537 if (DOSFS_GetDevice( filename ))
539 HFILE ret;
541 TRACE("opening device '%s'\n", filename );
543 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
544 return ret;
546 /* Do not silence this please. It is a critical error. -MM */
547 ERR("Couldn't open device '%s'!\n",filename);
548 SetLastError( ERROR_FILE_NOT_FOUND );
549 return HFILE_ERROR;
552 /* check for filename, don't check for last entry if creating */
553 if (!DOSFS_GetFullName( filename,
554 (creation == OPEN_EXISTING) ||
555 (creation == TRUNCATE_EXISTING),
556 &full_name )) {
557 WARN("Unable to get full filename from '%s' (GLE %ld)\n",
558 filename, GetLastError());
559 return HFILE_ERROR;
562 return FILE_CreateFile( full_name.long_name, access, sharing,
563 sa, creation, attributes, template,
564 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
569 /*************************************************************************
570 * CreateFileW (KERNEL32.48)
572 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
573 LPSECURITY_ATTRIBUTES sa, DWORD creation,
574 DWORD attributes, HANDLE template)
576 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
577 HANDLE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
578 HeapFree( GetProcessHeap(), 0, afn );
579 return res;
583 /***********************************************************************
584 * FILE_FillInfo
586 * Fill a file information from a struct stat.
588 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
590 if (S_ISDIR(st->st_mode))
591 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
592 else
593 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
594 if (!(st->st_mode & S_IWUSR))
595 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
597 RtlSecondsSince1970ToTime( st->st_mtime, &info->ftCreationTime );
598 RtlSecondsSince1970ToTime( st->st_mtime, &info->ftLastWriteTime );
599 RtlSecondsSince1970ToTime( st->st_atime, &info->ftLastAccessTime );
601 info->dwVolumeSerialNumber = 0; /* FIXME */
602 info->nFileSizeHigh = 0;
603 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
604 info->nNumberOfLinks = st->st_nlink;
605 info->nFileIndexHigh = 0;
606 info->nFileIndexLow = st->st_ino;
610 /***********************************************************************
611 * FILE_Stat
613 * Stat a Unix path name. Return TRUE if OK.
615 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
617 struct stat st;
619 if (lstat( unixName, &st ) == -1)
621 FILE_SetDosError();
622 return FALSE;
624 if (!S_ISLNK(st.st_mode)) FILE_FillInfo( &st, info );
625 else
627 /* do a "real" stat to find out
628 about the type of the symlink destination */
629 if (stat( unixName, &st ) == -1)
631 FILE_SetDosError();
632 return FALSE;
634 FILE_FillInfo( &st, info );
635 info->dwFileAttributes |= FILE_ATTRIBUTE_SYMLINK;
637 return TRUE;
641 /***********************************************************************
642 * GetFileInformationByHandle (KERNEL32.219)
644 DWORD WINAPI GetFileInformationByHandle( HANDLE hFile,
645 BY_HANDLE_FILE_INFORMATION *info )
647 DWORD ret;
648 if (!info) return 0;
650 SERVER_START_REQ
652 struct get_file_info_request *req = server_alloc_req( sizeof(*req), 0 );
653 req->handle = hFile;
654 if ((ret = !server_call( REQ_GET_FILE_INFO )))
656 RtlSecondsSince1970ToTime( req->write_time, &info->ftCreationTime );
657 RtlSecondsSince1970ToTime( req->write_time, &info->ftLastWriteTime );
658 RtlSecondsSince1970ToTime( req->access_time, &info->ftLastAccessTime );
659 info->dwFileAttributes = req->attr;
660 info->dwVolumeSerialNumber = req->serial;
661 info->nFileSizeHigh = req->size_high;
662 info->nFileSizeLow = req->size_low;
663 info->nNumberOfLinks = req->links;
664 info->nFileIndexHigh = req->index_high;
665 info->nFileIndexLow = req->index_low;
668 SERVER_END_REQ;
669 return ret;
673 /**************************************************************************
674 * GetFileAttributes16 (KERNEL.420)
676 DWORD WINAPI GetFileAttributes16( LPCSTR name )
678 return GetFileAttributesA( name );
682 /**************************************************************************
683 * GetFileAttributesA (KERNEL32.217)
685 DWORD WINAPI GetFileAttributesA( LPCSTR name )
687 DOS_FULL_NAME full_name;
688 BY_HANDLE_FILE_INFORMATION info;
690 if (name == NULL || *name=='\0') return -1;
692 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
693 if (!FILE_Stat( full_name.long_name, &info )) return -1;
694 return info.dwFileAttributes;
698 /**************************************************************************
699 * GetFileAttributesW (KERNEL32.218)
701 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
703 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
704 DWORD res = GetFileAttributesA( nameA );
705 HeapFree( GetProcessHeap(), 0, nameA );
706 return res;
710 /***********************************************************************
711 * GetFileSize (KERNEL32.220)
713 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
715 BY_HANDLE_FILE_INFORMATION info;
716 if (!GetFileInformationByHandle( hFile, &info )) return 0;
717 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
718 return info.nFileSizeLow;
722 /***********************************************************************
723 * GetFileTime (KERNEL32.221)
725 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
726 FILETIME *lpLastAccessTime,
727 FILETIME *lpLastWriteTime )
729 BY_HANDLE_FILE_INFORMATION info;
730 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
731 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
732 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
733 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
734 return TRUE;
737 /***********************************************************************
738 * CompareFileTime (KERNEL32.28)
740 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
742 if (!x || !y) return -1;
744 if (x->dwHighDateTime > y->dwHighDateTime)
745 return 1;
746 if (x->dwHighDateTime < y->dwHighDateTime)
747 return -1;
748 if (x->dwLowDateTime > y->dwLowDateTime)
749 return 1;
750 if (x->dwLowDateTime < y->dwLowDateTime)
751 return -1;
752 return 0;
755 /***********************************************************************
756 * FILE_GetTempFileName : utility for GetTempFileName
758 static UINT FILE_GetTempFileName( LPCSTR path, LPCSTR prefix, UINT unique,
759 LPSTR buffer, BOOL isWin16 )
761 static UINT unique_temp;
762 DOS_FULL_NAME full_name;
763 int i;
764 LPSTR p;
765 UINT num;
767 if ( !path || !prefix || !buffer ) return 0;
769 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
770 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
772 strcpy( buffer, path );
773 p = buffer + strlen(buffer);
775 /* add a \, if there isn't one and path is more than just the drive letter ... */
776 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
777 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
779 if (isWin16) *p++ = '~';
780 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
781 sprintf( p, "%04x.tmp", num );
783 /* Now try to create it */
785 if (!unique)
789 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
790 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
791 if (handle != INVALID_HANDLE_VALUE)
792 { /* We created it */
793 TRACE("created %s\n",
794 buffer);
795 CloseHandle( handle );
796 break;
798 if (GetLastError() != ERROR_FILE_EXISTS)
799 break; /* No need to go on */
800 num++;
801 sprintf( p, "%04x.tmp", num );
802 } while (num != (unique & 0xffff));
805 /* Get the full path name */
807 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
809 /* Check if we have write access in the directory */
810 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
811 if (access( full_name.long_name, W_OK ) == -1)
812 WARN("returns '%s', which doesn't seem to be writeable.\n",
813 buffer);
815 TRACE("returning %s\n", buffer );
816 return unique ? unique : num;
820 /***********************************************************************
821 * GetTempFileNameA (KERNEL32.290)
823 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
824 LPSTR buffer)
826 return FILE_GetTempFileName(path, prefix, unique, buffer, FALSE);
829 /***********************************************************************
830 * GetTempFileNameW (KERNEL32.291)
832 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
833 LPWSTR buffer )
835 LPSTR patha,prefixa;
836 char buffera[144];
837 UINT ret;
839 if (!path) return 0;
840 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
841 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
842 ret = FILE_GetTempFileName( patha, prefixa, unique, buffera, FALSE );
843 MultiByteToWideChar( CP_ACP, 0, buffera, -1, buffer, MAX_PATH );
844 HeapFree( GetProcessHeap(), 0, patha );
845 HeapFree( GetProcessHeap(), 0, prefixa );
846 return ret;
850 /***********************************************************************
851 * GetTempFileName16 (KERNEL.97)
853 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
854 LPSTR buffer )
856 char temppath[144];
858 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
859 drive |= DRIVE_GetCurrentDrive() + 'A';
861 if ((drive & TF_FORCEDRIVE) &&
862 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
864 drive &= ~TF_FORCEDRIVE;
865 WARN("invalid drive %d specified\n", drive );
868 if (drive & TF_FORCEDRIVE)
869 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
870 else
871 GetTempPathA( 132, temppath );
872 return (UINT16)FILE_GetTempFileName( temppath, prefix, unique, buffer, TRUE );
875 /***********************************************************************
876 * FILE_DoOpenFile
878 * Implementation of OpenFile16() and OpenFile32().
880 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
881 BOOL win32 )
883 HFILE hFileRet;
884 FILETIME filetime;
885 WORD filedatetime[2];
886 DOS_FULL_NAME full_name;
887 DWORD access, sharing;
888 char *p;
890 if (!ofs) return HFILE_ERROR;
892 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
893 ((mode & 0x3 )==OF_READ)?"OF_READ":
894 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
895 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
896 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
897 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
898 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
899 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
900 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
901 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
902 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
903 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
904 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
905 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
906 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
907 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
908 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
909 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
913 ofs->cBytes = sizeof(OFSTRUCT);
914 ofs->nErrCode = 0;
915 if (mode & OF_REOPEN) name = ofs->szPathName;
917 if (!name) {
918 ERR("called with `name' set to NULL ! Please debug.\n");
919 return HFILE_ERROR;
922 TRACE("%s %04x\n", name, mode );
924 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
925 Are there any cases where getting the path here is wrong?
926 Uwe Bonnes 1997 Apr 2 */
927 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
928 ofs->szPathName, NULL )) goto error;
929 FILE_ConvertOFMode( mode, &access, &sharing );
931 /* OF_PARSE simply fills the structure */
933 if (mode & OF_PARSE)
935 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
936 != DRIVE_REMOVABLE);
937 TRACE("(%s): OF_PARSE, res = '%s'\n",
938 name, ofs->szPathName );
939 return 0;
942 /* OF_CREATE is completely different from all other options, so
943 handle it first */
945 if (mode & OF_CREATE)
947 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
948 sharing, NULL, CREATE_ALWAYS,
949 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
950 goto error;
951 goto success;
954 /* If OF_SEARCH is set, ignore the given path */
956 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
958 /* First try the file name as is */
959 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
960 /* Now remove the path */
961 if (name[0] && (name[1] == ':')) name += 2;
962 if ((p = strrchr( name, '\\' ))) name = p + 1;
963 if ((p = strrchr( name, '/' ))) name = p + 1;
964 if (!name[0]) goto not_found;
967 /* Now look for the file */
969 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
971 found:
972 TRACE("found %s = %s\n",
973 full_name.long_name, full_name.short_name );
974 lstrcpynA( ofs->szPathName, full_name.short_name,
975 sizeof(ofs->szPathName) );
977 if (mode & OF_SHARE_EXCLUSIVE)
978 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
979 on the file <tempdir>/_ins0432._mp to determine how
980 far installation has proceeded.
981 _ins0432._mp is an executable and while running the
982 application expects the open with OF_SHARE_ to fail*/
983 /* Probable FIXME:
984 As our loader closes the files after loading the executable,
985 we can't find the running executable with FILE_InUse.
986 Perhaps the loader should keep the file open.
987 Recheck against how Win handles that case */
989 char *last = strrchr(full_name.long_name,'/');
990 if (!last)
991 last = full_name.long_name - 1;
992 if (GetModuleHandle16(last+1))
994 TRACE("Denying shared open for %s\n",full_name.long_name);
995 return HFILE_ERROR;
999 if (mode & OF_DELETE)
1001 if (unlink( full_name.long_name ) == -1) goto not_found;
1002 TRACE("(%s): OF_DELETE return = OK\n", name);
1003 return 1;
1006 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
1007 NULL, OPEN_EXISTING, 0, -1,
1008 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
1009 if (hFileRet == HFILE_ERROR) goto not_found;
1011 GetFileTime( hFileRet, NULL, NULL, &filetime );
1012 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
1013 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
1015 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
1017 CloseHandle( hFileRet );
1018 WARN("(%s): OF_VERIFY failed\n", name );
1019 /* FIXME: what error here? */
1020 SetLastError( ERROR_FILE_NOT_FOUND );
1021 goto error;
1024 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
1026 success: /* We get here if the open was successful */
1027 TRACE("(%s): OK, return = %d\n", name, hFileRet );
1028 if (win32)
1030 if (mode & OF_EXIST) /* Return the handle, but close it first */
1031 CloseHandle( hFileRet );
1033 else
1035 hFileRet = Win32HandleToDosFileHandle( hFileRet );
1036 if (hFileRet == HFILE_ERROR16) goto error;
1037 if (mode & OF_EXIST) /* Return the handle, but close it first */
1038 _lclose16( hFileRet );
1040 return hFileRet;
1042 not_found: /* We get here if the file does not exist */
1043 WARN("'%s' not found or sharing violation\n", name );
1044 SetLastError( ERROR_FILE_NOT_FOUND );
1045 /* fall through */
1047 error: /* We get here if there was an error opening the file */
1048 ofs->nErrCode = GetLastError();
1049 WARN("(%s): return = HFILE_ERROR error= %d\n",
1050 name,ofs->nErrCode );
1051 return HFILE_ERROR;
1055 /***********************************************************************
1056 * OpenFile16 (KERNEL.74)
1058 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
1060 return FILE_DoOpenFile( name, ofs, mode, FALSE );
1064 /***********************************************************************
1065 * OpenFile (KERNEL32.396)
1067 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
1069 return FILE_DoOpenFile( name, ofs, mode, TRUE );
1073 /***********************************************************************
1074 * FILE_InitProcessDosHandles
1076 * Allocates the default DOS handles for a process. Called either by
1077 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
1079 static void FILE_InitProcessDosHandles( void )
1081 dos_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
1082 dos_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
1083 dos_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
1084 dos_handles[3] = GetStdHandle(STD_ERROR_HANDLE);
1085 dos_handles[4] = GetStdHandle(STD_ERROR_HANDLE);
1088 /***********************************************************************
1089 * Win32HandleToDosFileHandle (KERNEL32.21)
1091 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1092 * longer valid after this function (even on failure).
1094 * Note: this is not exactly right, since on Win95 the Win32 handles
1095 * are on top of DOS handles and we do it the other way
1096 * around. Should be good enough though.
1098 HFILE WINAPI Win32HandleToDosFileHandle( HANDLE handle )
1100 int i;
1102 if (!handle || (handle == INVALID_HANDLE_VALUE))
1103 return HFILE_ERROR;
1105 for (i = 5; i < DOS_TABLE_SIZE; i++)
1106 if (!dos_handles[i])
1108 dos_handles[i] = handle;
1109 TRACE("Got %d for h32 %d\n", i, handle );
1110 return (HFILE)i;
1112 CloseHandle( handle );
1113 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1114 return HFILE_ERROR;
1118 /***********************************************************************
1119 * DosFileHandleToWin32Handle (KERNEL32.20)
1121 * Return the Win32 handle for a DOS handle.
1123 * Note: this is not exactly right, since on Win95 the Win32 handles
1124 * are on top of DOS handles and we do it the other way
1125 * around. Should be good enough though.
1127 HANDLE WINAPI DosFileHandleToWin32Handle( HFILE handle )
1129 HFILE16 hfile = (HFILE16)handle;
1130 if (hfile < 5 && !dos_handles[hfile]) FILE_InitProcessDosHandles();
1131 if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
1133 SetLastError( ERROR_INVALID_HANDLE );
1134 return INVALID_HANDLE_VALUE;
1136 return dos_handles[hfile];
1140 /***********************************************************************
1141 * DisposeLZ32Handle (KERNEL32.22)
1143 * Note: this is not entirely correct, we should only close the
1144 * 32-bit handle and not the 16-bit one, but we cannot do
1145 * this because of the way our DOS handles are implemented.
1146 * It shouldn't break anything though.
1148 void WINAPI DisposeLZ32Handle( HANDLE handle )
1150 int i;
1152 if (!handle || (handle == INVALID_HANDLE_VALUE)) return;
1154 for (i = 5; i < DOS_TABLE_SIZE; i++)
1155 if (dos_handles[i] == handle)
1157 dos_handles[i] = 0;
1158 CloseHandle( handle );
1159 break;
1164 /***********************************************************************
1165 * FILE_Dup2
1167 * dup2() function for DOS handles.
1169 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1171 HANDLE new_handle;
1173 if (hFile1 < 5 && !dos_handles[hFile1]) FILE_InitProcessDosHandles();
1175 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) || !dos_handles[hFile1])
1177 SetLastError( ERROR_INVALID_HANDLE );
1178 return HFILE_ERROR16;
1180 if (hFile2 < 5)
1182 FIXME("stdio handle closed, need proper conversion\n" );
1183 SetLastError( ERROR_INVALID_HANDLE );
1184 return HFILE_ERROR16;
1186 if (!DuplicateHandle( GetCurrentProcess(), dos_handles[hFile1],
1187 GetCurrentProcess(), &new_handle,
1188 0, FALSE, DUPLICATE_SAME_ACCESS ))
1189 return HFILE_ERROR16;
1190 if (dos_handles[hFile2]) CloseHandle( dos_handles[hFile2] );
1191 dos_handles[hFile2] = new_handle;
1192 return hFile2;
1196 /***********************************************************************
1197 * _lclose16 (KERNEL.81)
1199 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1201 if (hFile < 5)
1203 FIXME("stdio handle closed, need proper conversion\n" );
1204 SetLastError( ERROR_INVALID_HANDLE );
1205 return HFILE_ERROR16;
1207 if ((hFile >= DOS_TABLE_SIZE) || !dos_handles[hFile])
1209 SetLastError( ERROR_INVALID_HANDLE );
1210 return HFILE_ERROR16;
1212 TRACE("%d (handle32=%d)\n", hFile, dos_handles[hFile] );
1213 CloseHandle( dos_handles[hFile] );
1214 dos_handles[hFile] = 0;
1215 return 0;
1219 /***********************************************************************
1220 * _lclose (KERNEL32.592)
1222 HFILE WINAPI _lclose( HFILE hFile )
1224 TRACE("handle %d\n", hFile );
1225 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1228 /***********************************************************************
1229 * GetOverlappedResult (KERNEL32.360)
1231 BOOL WINAPI GetOverlappedResult(HANDLE hFile,LPOVERLAPPED lpOverlapped,
1232 LPDWORD lpNumberOfBytesTransferred,
1233 BOOL bWait)
1235 /* Since all i/o is currently synchronous,
1236 * return true, assuming ReadFile/WriteFile
1237 * have completed the operation */
1238 FIXME("NO Asynch I/O, assuming Read/Write succeeded\n" );
1239 return TRUE;
1242 /***********************************************************************
1243 * ReadFile (KERNEL32.428)
1245 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1246 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1248 int unix_handle, result;
1250 TRACE("%d %p %ld\n", hFile, buffer, bytesToRead );
1252 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1253 if (!bytesToRead) return TRUE;
1255 if ( overlapped ) {
1256 SetLastError ( ERROR_INVALID_PARAMETER );
1257 return FALSE;
1260 unix_handle = FILE_GetUnixHandle( hFile, GENERIC_READ );
1261 if (unix_handle == -1) return FALSE;
1262 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1264 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1265 if ((errno == EFAULT) && !VIRTUAL_HandleFault( buffer )) continue;
1266 FILE_SetDosError();
1267 break;
1269 close( unix_handle );
1270 if (result == -1) return FALSE;
1271 if (bytesRead) *bytesRead = result;
1272 return TRUE;
1276 /***********************************************************************
1277 * WriteFile (KERNEL32.578)
1279 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1280 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1282 int unix_handle, result;
1284 TRACE("%d %p %ld\n", hFile, buffer, bytesToWrite );
1286 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1287 if (!bytesToWrite) return TRUE;
1289 if ( overlapped ) {
1290 SetLastError ( ERROR_INVALID_PARAMETER );
1291 return FALSE;
1294 unix_handle = FILE_GetUnixHandle( hFile, GENERIC_WRITE );
1295 if (unix_handle == -1) return FALSE;
1296 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1298 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1299 if ((errno == EFAULT) && !VIRTUAL_HandleFault( buffer )) continue;
1300 if (errno == ENOSPC)
1301 SetLastError( ERROR_DISK_FULL );
1302 else
1303 FILE_SetDosError();
1304 break;
1306 close( unix_handle );
1307 if (result == -1) return FALSE;
1308 if (bytesWritten) *bytesWritten = result;
1309 return TRUE;
1313 /***********************************************************************
1314 * WIN16_hread
1316 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1318 LONG maxlen;
1320 TRACE("%d %08lx %ld\n",
1321 hFile, (DWORD)buffer, count );
1323 /* Some programs pass a count larger than the allocated buffer */
1324 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1325 if (count > maxlen) count = maxlen;
1326 return _lread(DosFileHandleToWin32Handle(hFile), PTR_SEG_TO_LIN(buffer), count );
1330 /***********************************************************************
1331 * WIN16_lread
1333 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1335 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1339 /***********************************************************************
1340 * _lread (KERNEL32.596)
1342 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1344 DWORD result;
1345 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1346 return result;
1350 /***********************************************************************
1351 * _lread16 (KERNEL.82)
1353 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1355 return (UINT16)_lread(DosFileHandleToWin32Handle(hFile), buffer, (LONG)count );
1359 /***********************************************************************
1360 * _lcreat16 (KERNEL.83)
1362 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1364 return Win32HandleToDosFileHandle( _lcreat( path, attr ) );
1368 /***********************************************************************
1369 * _lcreat (KERNEL32.593)
1371 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1373 /* Mask off all flags not explicitly allowed by the doc */
1374 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1375 TRACE("%s %02x\n", path, attr );
1376 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1377 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1378 CREATE_ALWAYS, attr, -1 );
1382 /***********************************************************************
1383 * SetFilePointer (KERNEL32.492)
1385 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword,
1386 DWORD method )
1388 DWORD ret = 0xffffffff;
1390 if (highword &&
1391 ((distance >= 0 && *highword != 0) || (distance < 0 && *highword != -1)))
1393 FIXME("64-bit offsets not supported yet\n"
1394 "SetFilePointer(%08x,%08lx,%08lx,%08lx)\n",
1395 hFile,distance,*highword,method);
1396 SetLastError( ERROR_INVALID_PARAMETER );
1397 return ret;
1399 TRACE("handle %d offset %ld origin %ld\n",
1400 hFile, distance, method );
1402 SERVER_START_REQ
1404 struct set_file_pointer_request *req = server_alloc_req( sizeof(*req), 0 );
1405 req->handle = hFile;
1406 req->low = distance;
1407 req->high = highword ? *highword : (distance >= 0) ? 0 : -1;
1408 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1409 req->whence = method;
1410 SetLastError( 0 );
1411 if (!server_call( REQ_SET_FILE_POINTER ))
1413 ret = req->new_low;
1414 if (highword) *highword = req->new_high;
1417 SERVER_END_REQ;
1418 return ret;
1422 /***********************************************************************
1423 * _llseek16 (KERNEL.84)
1425 * FIXME:
1426 * Seeking before the start of the file should be allowed for _llseek16,
1427 * but cause subsequent I/O operations to fail (cf. interrupt list)
1430 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1432 return SetFilePointer( DosFileHandleToWin32Handle(hFile), lOffset, NULL, nOrigin );
1436 /***********************************************************************
1437 * _llseek (KERNEL32.594)
1439 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1441 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1445 /***********************************************************************
1446 * _lopen16 (KERNEL.85)
1448 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1450 return Win32HandleToDosFileHandle( _lopen( path, mode ) );
1454 /***********************************************************************
1455 * _lopen (KERNEL32.595)
1457 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1459 DWORD access, sharing;
1461 TRACE("('%s',%04x)\n", path, mode );
1462 FILE_ConvertOFMode( mode, &access, &sharing );
1463 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1467 /***********************************************************************
1468 * _lwrite16 (KERNEL.86)
1470 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1472 return (UINT16)_hwrite( DosFileHandleToWin32Handle(hFile), buffer, (LONG)count );
1475 /***********************************************************************
1476 * _lwrite (KERNEL32.761)
1478 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1480 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1484 /***********************************************************************
1485 * _hread16 (KERNEL.349)
1487 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1489 return _lread( DosFileHandleToWin32Handle(hFile), buffer, count );
1493 /***********************************************************************
1494 * _hread (KERNEL32.590)
1496 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1498 return _lread( hFile, buffer, count );
1502 /***********************************************************************
1503 * _hwrite16 (KERNEL.350)
1505 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1507 return _hwrite( DosFileHandleToWin32Handle(hFile), buffer, count );
1511 /***********************************************************************
1512 * _hwrite (KERNEL32.591)
1514 * experimentation yields that _lwrite:
1515 * o truncates the file at the current position with
1516 * a 0 len write
1517 * o returns 0 on a 0 length write
1518 * o works with console handles
1521 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1523 DWORD result;
1525 TRACE("%d %p %ld\n", handle, buffer, count );
1527 if (!count)
1529 /* Expand or truncate at current position */
1530 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1531 return 0;
1533 if (!WriteFile( handle, buffer, count, &result, NULL ))
1534 return HFILE_ERROR;
1535 return result;
1539 /***********************************************************************
1540 * SetHandleCount16 (KERNEL.199)
1542 UINT16 WINAPI SetHandleCount16( UINT16 count )
1544 HGLOBAL16 hPDB = GetCurrentPDB16();
1545 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1546 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1548 TRACE("(%d)\n", count );
1550 if (count < 20) count = 20; /* No point in going below 20 */
1551 else if (count > 254) count = 254;
1553 if (count == 20)
1555 if (pdb->nbFiles > 20)
1557 memcpy( pdb->fileHandles, files, 20 );
1558 GlobalFree16( pdb->hFileHandles );
1559 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1560 GlobalHandleToSel16( hPDB ) );
1561 pdb->hFileHandles = 0;
1562 pdb->nbFiles = 20;
1565 else /* More than 20, need a new file handles table */
1567 BYTE *newfiles;
1568 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1569 if (!newhandle)
1571 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1572 return pdb->nbFiles;
1574 newfiles = (BYTE *)GlobalLock16( newhandle );
1576 if (count > pdb->nbFiles)
1578 memcpy( newfiles, files, pdb->nbFiles );
1579 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1581 else memcpy( newfiles, files, count );
1582 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1583 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1584 pdb->hFileHandles = newhandle;
1585 pdb->nbFiles = count;
1587 return pdb->nbFiles;
1591 /*************************************************************************
1592 * SetHandleCount (KERNEL32.494)
1594 UINT WINAPI SetHandleCount( UINT count )
1596 return min( 256, count );
1600 /***********************************************************************
1601 * FlushFileBuffers (KERNEL32.133)
1603 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
1605 BOOL ret;
1606 SERVER_START_REQ
1608 struct flush_file_request *req = server_alloc_req( sizeof(*req), 0 );
1609 req->handle = hFile;
1610 ret = !server_call( REQ_FLUSH_FILE );
1612 SERVER_END_REQ;
1613 return ret;
1617 /**************************************************************************
1618 * SetEndOfFile (KERNEL32.483)
1620 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1622 BOOL ret;
1623 SERVER_START_REQ
1625 struct truncate_file_request *req = server_alloc_req( sizeof(*req), 0 );
1626 req->handle = hFile;
1627 ret = !server_call( REQ_TRUNCATE_FILE );
1629 SERVER_END_REQ;
1630 return ret;
1634 /***********************************************************************
1635 * DeleteFile16 (KERNEL.146)
1637 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1639 return DeleteFileA( path );
1643 /***********************************************************************
1644 * DeleteFileA (KERNEL32.71)
1646 BOOL WINAPI DeleteFileA( LPCSTR path )
1648 DOS_FULL_NAME full_name;
1650 TRACE("'%s'\n", path );
1652 if (!*path)
1654 ERR("Empty path passed\n");
1655 return FALSE;
1657 if (DOSFS_GetDevice( path ))
1659 WARN("cannot remove DOS device '%s'!\n", path);
1660 SetLastError( ERROR_FILE_NOT_FOUND );
1661 return FALSE;
1664 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1665 if (unlink( full_name.long_name ) == -1)
1667 FILE_SetDosError();
1668 return FALSE;
1670 return TRUE;
1674 /***********************************************************************
1675 * DeleteFileW (KERNEL32.72)
1677 BOOL WINAPI DeleteFileW( LPCWSTR path )
1679 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1680 BOOL ret = DeleteFileA( xpath );
1681 HeapFree( GetProcessHeap(), 0, xpath );
1682 return ret;
1686 /***********************************************************************
1687 * GetFileType (KERNEL32.222)
1689 DWORD WINAPI GetFileType( HANDLE hFile )
1691 DWORD ret = FILE_TYPE_UNKNOWN;
1692 SERVER_START_REQ
1694 struct get_file_info_request *req = server_alloc_req( sizeof(*req), 0 );
1695 req->handle = hFile;
1696 if (!server_call( REQ_GET_FILE_INFO )) ret = req->type;
1698 SERVER_END_REQ;
1699 return ret;
1703 /**************************************************************************
1704 * MoveFileExA (KERNEL32.???)
1706 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1708 DOS_FULL_NAME full_name1, full_name2;
1710 TRACE("(%s,%s,%04lx)\n", fn1, fn2, flag);
1712 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1714 if (fn2) /* !fn2 means delete fn1 */
1716 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1718 /* target exists, check if we may overwrite */
1719 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1721 /* FIXME: Use right error code */
1722 SetLastError( ERROR_ACCESS_DENIED );
1723 return FALSE;
1726 else if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1728 /* Source name and target path are valid */
1730 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1732 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1733 Perhaps we should queue these command and execute it
1734 when exiting... What about using on_exit(2)
1736 FIXME("Please move existing file '%s' to file '%s' when Wine has finished\n",
1737 full_name1.long_name, full_name2.long_name);
1738 return TRUE;
1741 if (full_name1.drive != full_name2.drive)
1743 /* use copy, if allowed */
1744 if (!(flag & MOVEFILE_COPY_ALLOWED))
1746 /* FIXME: Use right error code */
1747 SetLastError( ERROR_FILE_EXISTS );
1748 return FALSE;
1750 return CopyFileA( fn1, fn2, !(flag & MOVEFILE_REPLACE_EXISTING) );
1752 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1754 FILE_SetDosError();
1755 return FALSE;
1757 return TRUE;
1759 else /* fn2 == NULL means delete source */
1761 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1763 if (flag & MOVEFILE_COPY_ALLOWED) {
1764 WARN("Illegal flag\n");
1765 SetLastError( ERROR_GEN_FAILURE );
1766 return FALSE;
1768 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1769 Perhaps we should queue these command and execute it
1770 when exiting... What about using on_exit(2)
1772 FIXME("Please delete file '%s' when Wine has finished\n",
1773 full_name1.long_name);
1774 return TRUE;
1777 if (unlink( full_name1.long_name ) == -1)
1779 FILE_SetDosError();
1780 return FALSE;
1782 return TRUE; /* successfully deleted */
1786 /**************************************************************************
1787 * MoveFileExW (KERNEL32.???)
1789 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1791 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1792 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1793 BOOL res = MoveFileExA( afn1, afn2, flag );
1794 HeapFree( GetProcessHeap(), 0, afn1 );
1795 HeapFree( GetProcessHeap(), 0, afn2 );
1796 return res;
1800 /**************************************************************************
1801 * MoveFileA (KERNEL32.387)
1803 * Move file or directory
1805 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1807 DOS_FULL_NAME full_name1, full_name2;
1808 struct stat fstat;
1810 TRACE("(%s,%s)\n", fn1, fn2 );
1812 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1813 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 )) {
1814 /* The new name must not already exist */
1815 SetLastError(ERROR_ALREADY_EXISTS);
1816 return FALSE;
1818 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1820 if (full_name1.drive == full_name2.drive) /* move */
1821 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1823 FILE_SetDosError();
1824 return FALSE;
1826 else return TRUE;
1827 else /*copy */ {
1828 if (stat( full_name1.long_name, &fstat ))
1830 WARN("Invalid source file %s\n",
1831 full_name1.long_name);
1832 FILE_SetDosError();
1833 return FALSE;
1835 if (S_ISDIR(fstat.st_mode)) {
1836 /* No Move for directories across file systems */
1837 /* FIXME: Use right error code */
1838 SetLastError( ERROR_GEN_FAILURE );
1839 return FALSE;
1841 else
1842 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1847 /**************************************************************************
1848 * MoveFileW (KERNEL32.390)
1850 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1852 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1853 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1854 BOOL res = MoveFileA( afn1, afn2 );
1855 HeapFree( GetProcessHeap(), 0, afn1 );
1856 HeapFree( GetProcessHeap(), 0, afn2 );
1857 return res;
1861 /**************************************************************************
1862 * CopyFileA (KERNEL32.36)
1864 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1866 HFILE h1, h2;
1867 BY_HANDLE_FILE_INFORMATION info;
1868 UINT count;
1869 BOOL ret = FALSE;
1870 int mode;
1871 char buffer[2048];
1873 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1874 if (!GetFileInformationByHandle( h1, &info ))
1876 CloseHandle( h1 );
1877 return FALSE;
1879 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1880 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1881 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1882 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1884 CloseHandle( h1 );
1885 return FALSE;
1887 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1889 char *p = buffer;
1890 while (count > 0)
1892 INT res = _lwrite( h2, p, count );
1893 if (res <= 0) goto done;
1894 p += res;
1895 count -= res;
1898 ret = TRUE;
1899 done:
1900 CloseHandle( h1 );
1901 CloseHandle( h2 );
1902 return ret;
1906 /**************************************************************************
1907 * CopyFileW (KERNEL32.37)
1909 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1911 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1912 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1913 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1914 HeapFree( GetProcessHeap(), 0, sourceA );
1915 HeapFree( GetProcessHeap(), 0, destA );
1916 return ret;
1920 /**************************************************************************
1921 * CopyFileExA (KERNEL32.858)
1923 * This implementation ignores most of the extra parameters passed-in into
1924 * the "ex" version of the method and calls the CopyFile method.
1925 * It will have to be fixed eventually.
1927 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1928 LPCSTR destFilename,
1929 LPPROGRESS_ROUTINE progressRoutine,
1930 LPVOID appData,
1931 LPBOOL cancelFlagPointer,
1932 DWORD copyFlags)
1934 BOOL failIfExists = FALSE;
1937 * Interpret the only flag that CopyFile can interpret.
1939 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1941 failIfExists = TRUE;
1944 return CopyFileA(sourceFilename, destFilename, failIfExists);
1947 /**************************************************************************
1948 * CopyFileExW (KERNEL32.859)
1950 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1951 LPCWSTR destFilename,
1952 LPPROGRESS_ROUTINE progressRoutine,
1953 LPVOID appData,
1954 LPBOOL cancelFlagPointer,
1955 DWORD copyFlags)
1957 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1958 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1960 BOOL ret = CopyFileExA(sourceA,
1961 destA,
1962 progressRoutine,
1963 appData,
1964 cancelFlagPointer,
1965 copyFlags);
1967 HeapFree( GetProcessHeap(), 0, sourceA );
1968 HeapFree( GetProcessHeap(), 0, destA );
1970 return ret;
1974 /***********************************************************************
1975 * SetFileTime (KERNEL32.650)
1977 BOOL WINAPI SetFileTime( HANDLE hFile,
1978 const FILETIME *lpCreationTime,
1979 const FILETIME *lpLastAccessTime,
1980 const FILETIME *lpLastWriteTime )
1982 BOOL ret;
1983 SERVER_START_REQ
1985 struct set_file_time_request *req = server_alloc_req( sizeof(*req), 0 );
1986 req->handle = hFile;
1987 if (lpLastAccessTime)
1988 RtlTimeToSecondsSince1970( lpLastAccessTime, (DWORD *)&req->access_time );
1989 else
1990 req->access_time = 0; /* FIXME */
1991 if (lpLastWriteTime)
1992 RtlTimeToSecondsSince1970( lpLastWriteTime, (DWORD *)&req->write_time );
1993 else
1994 req->write_time = 0; /* FIXME */
1995 ret = !server_call( REQ_SET_FILE_TIME );
1997 SERVER_END_REQ;
1998 return ret;
2002 /**************************************************************************
2003 * LockFile (KERNEL32.511)
2005 BOOL WINAPI LockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
2006 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
2008 BOOL ret;
2009 SERVER_START_REQ
2011 struct lock_file_request *req = server_alloc_req( sizeof(*req), 0 );
2013 req->handle = hFile;
2014 req->offset_low = dwFileOffsetLow;
2015 req->offset_high = dwFileOffsetHigh;
2016 req->count_low = nNumberOfBytesToLockLow;
2017 req->count_high = nNumberOfBytesToLockHigh;
2018 ret = !server_call( REQ_LOCK_FILE );
2020 SERVER_END_REQ;
2021 return ret;
2024 /**************************************************************************
2025 * LockFileEx [KERNEL32.512]
2027 * Locks a byte range within an open file for shared or exclusive access.
2029 * RETURNS
2030 * success: TRUE
2031 * failure: FALSE
2032 * NOTES
2034 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
2036 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
2037 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
2038 LPOVERLAPPED pOverlapped )
2040 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2041 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
2042 pOverlapped);
2043 if (reserved == 0)
2044 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2045 else
2047 ERR("reserved == %ld: Supposed to be 0??\n", reserved);
2048 SetLastError(ERROR_INVALID_PARAMETER);
2051 return FALSE;
2055 /**************************************************************************
2056 * UnlockFile (KERNEL32.703)
2058 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
2059 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
2061 BOOL ret;
2062 SERVER_START_REQ
2064 struct unlock_file_request *req = server_alloc_req( sizeof(*req), 0 );
2066 req->handle = hFile;
2067 req->offset_low = dwFileOffsetLow;
2068 req->offset_high = dwFileOffsetHigh;
2069 req->count_low = nNumberOfBytesToUnlockLow;
2070 req->count_high = nNumberOfBytesToUnlockHigh;
2071 ret = !server_call( REQ_UNLOCK_FILE );
2073 SERVER_END_REQ;
2074 return ret;
2078 /**************************************************************************
2079 * UnlockFileEx (KERNEL32.705)
2081 BOOL WINAPI UnlockFileEx(
2082 HFILE hFile,
2083 DWORD dwReserved,
2084 DWORD nNumberOfBytesToUnlockLow,
2085 DWORD nNumberOfBytesToUnlockHigh,
2086 LPOVERLAPPED lpOverlapped
2089 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2090 hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh,
2091 lpOverlapped);
2092 if (dwReserved == 0)
2093 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2094 else
2096 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved);
2097 SetLastError(ERROR_INVALID_PARAMETER);
2100 return FALSE;
2104 #if 0
2106 struct DOS_FILE_LOCK {
2107 struct DOS_FILE_LOCK * next;
2108 DWORD base;
2109 DWORD len;
2110 DWORD processId;
2111 FILE_OBJECT * dos_file;
2112 /* char * unix_name;*/
2115 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2117 static DOS_FILE_LOCK *locks = NULL;
2118 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2121 /* Locks need to be mirrored because unix file locking is based
2122 * on the pid. Inside of wine there can be multiple WINE processes
2123 * that share the same unix pid.
2124 * Read's and writes should check these locks also - not sure
2125 * how critical that is at this point (FIXME).
2128 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2130 DOS_FILE_LOCK *curr;
2131 DWORD processId;
2133 processId = GetCurrentProcessId();
2135 /* check if lock overlaps a current lock for the same file */
2136 #if 0
2137 for (curr = locks; curr; curr = curr->next) {
2138 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2139 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2140 return TRUE;/* region is identic */
2141 if ((f->l_start < (curr->base + curr->len)) &&
2142 ((f->l_start + f->l_len) > curr->base)) {
2143 /* region overlaps */
2144 return FALSE;
2148 #endif
2150 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(DOS_FILE_LOCK) );
2151 curr->processId = GetCurrentProcessId();
2152 curr->base = f->l_start;
2153 curr->len = f->l_len;
2154 /* curr->unix_name = HEAP_strdupA( GetProcessHeap(), 0, file->unix_name);*/
2155 curr->next = locks;
2156 curr->dos_file = file;
2157 locks = curr;
2158 return TRUE;
2161 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2163 DWORD processId;
2164 DOS_FILE_LOCK **curr;
2165 DOS_FILE_LOCK *rem;
2167 processId = GetCurrentProcessId();
2168 curr = &locks;
2169 while (*curr) {
2170 if ((*curr)->dos_file == file) {
2171 rem = *curr;
2172 *curr = (*curr)->next;
2173 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2174 HeapFree( GetProcessHeap(), 0, rem );
2176 else
2177 curr = &(*curr)->next;
2181 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2183 DWORD processId;
2184 DOS_FILE_LOCK **curr;
2185 DOS_FILE_LOCK *rem;
2187 processId = GetCurrentProcessId();
2188 for (curr = &locks; *curr; curr = &(*curr)->next) {
2189 if ((*curr)->processId == processId &&
2190 (*curr)->dos_file == file &&
2191 (*curr)->base == f->l_start &&
2192 (*curr)->len == f->l_len) {
2193 /* this is the same lock */
2194 rem = *curr;
2195 *curr = (*curr)->next;
2196 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2197 HeapFree( GetProcessHeap(), 0, rem );
2198 return TRUE;
2201 /* no matching lock found */
2202 return FALSE;
2206 /**************************************************************************
2207 * LockFile (KERNEL32.511)
2209 BOOL WINAPI LockFile(
2210 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2211 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2213 struct flock f;
2214 FILE_OBJECT *file;
2216 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2217 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2218 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2220 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2221 FIXME("Unimplemented bytes > 32bits\n");
2222 return FALSE;
2225 f.l_start = dwFileOffsetLow;
2226 f.l_len = nNumberOfBytesToLockLow;
2227 f.l_whence = SEEK_SET;
2228 f.l_pid = 0;
2229 f.l_type = F_WRLCK;
2231 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2233 /* shadow locks internally */
2234 if (!DOS_AddLock(file, &f)) {
2235 SetLastError( ERROR_LOCK_VIOLATION );
2236 return FALSE;
2239 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2240 #ifdef USE_UNIX_LOCKS
2241 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2242 if (errno == EACCES || errno == EAGAIN) {
2243 SetLastError( ERROR_LOCK_VIOLATION );
2245 else {
2246 FILE_SetDosError();
2248 /* remove our internal copy of the lock */
2249 DOS_RemoveLock(file, &f);
2250 return FALSE;
2252 #endif
2253 return TRUE;
2257 /**************************************************************************
2258 * UnlockFile (KERNEL32.703)
2260 BOOL WINAPI UnlockFile(
2261 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2262 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2264 FILE_OBJECT *file;
2265 struct flock f;
2267 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2268 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2269 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2271 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2272 WARN("Unimplemented bytes > 32bits\n");
2273 return FALSE;
2276 f.l_start = dwFileOffsetLow;
2277 f.l_len = nNumberOfBytesToUnlockLow;
2278 f.l_whence = SEEK_SET;
2279 f.l_pid = 0;
2280 f.l_type = F_UNLCK;
2282 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2284 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2286 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2287 #ifdef USE_UNIX_LOCKS
2288 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2289 FILE_SetDosError();
2290 return FALSE;
2292 #endif
2293 return TRUE;
2295 #endif
2297 /**************************************************************************
2298 * GetFileAttributesExA [KERNEL32.874]
2300 BOOL WINAPI GetFileAttributesExA(
2301 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2302 LPVOID lpFileInformation)
2304 DOS_FULL_NAME full_name;
2305 BY_HANDLE_FILE_INFORMATION info;
2307 if (lpFileName == NULL) return FALSE;
2308 if (lpFileInformation == NULL) return FALSE;
2310 if (fInfoLevelId == GetFileExInfoStandard) {
2311 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2312 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2313 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2314 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2316 lpFad->dwFileAttributes = info.dwFileAttributes;
2317 lpFad->ftCreationTime = info.ftCreationTime;
2318 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2319 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2320 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2321 lpFad->nFileSizeLow = info.nFileSizeLow;
2323 else {
2324 FIXME("invalid info level %d!\n", fInfoLevelId);
2325 return FALSE;
2328 return TRUE;
2332 /**************************************************************************
2333 * GetFileAttributesExW [KERNEL32.875]
2335 BOOL WINAPI GetFileAttributesExW(
2336 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2337 LPVOID lpFileInformation)
2339 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2340 BOOL res =
2341 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2342 HeapFree( GetProcessHeap(), 0, nameA );
2343 return res;