Check whether senderQ is NULL before trying to lock it.
[wine.git] / files / file.c
blobbb1d18c76c547e90567569842d7c21cd7086cc9d
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 "wine/winestring.h"
39 #include "drive.h"
40 #include "file.h"
41 #include "global.h"
42 #include "heap.h"
43 #include "msdos.h"
44 #include "ldt.h"
45 #include "task.h"
46 #include "wincon.h"
47 #include "debugtools.h"
49 #include "server.h"
51 DEFAULT_DEBUG_CHANNEL(file);
53 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
54 #define MAP_ANON MAP_ANONYMOUS
55 #endif
57 /* Size of per-process table of DOS handles */
58 #define DOS_TABLE_SIZE 256
60 static HANDLE dos_handles[DOS_TABLE_SIZE];
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 case ENOEXEC:
296 SetLastError( ERROR_BAD_FORMAT );
297 break;
298 default:
299 WARN( "unknown file error: %s", strerror(save_errno) );
300 SetLastError( ERROR_GEN_FAILURE );
301 break;
303 errno = save_errno;
307 /***********************************************************************
308 * FILE_DupUnixHandle
310 * Duplicate a Unix handle into a task handle.
312 HFILE FILE_DupUnixHandle( int fd, DWORD access )
314 struct alloc_file_handle_request *req = get_req_buffer();
315 req->access = access;
316 server_call_fd( REQ_ALLOC_FILE_HANDLE, fd, NULL );
317 return req->handle;
321 /***********************************************************************
322 * FILE_GetUnixHandle
324 * Retrieve the Unix handle corresponding to a file handle.
326 int FILE_GetUnixHandle( HANDLE handle, DWORD access )
328 int unix_handle = -1;
329 if (access == GENERIC_READ)
331 struct get_read_fd_request *req = get_req_buffer();
332 req->handle = handle;
333 server_call_fd( REQ_GET_READ_FD, -1, &unix_handle );
335 else if (access == GENERIC_WRITE)
337 struct get_write_fd_request *req = get_req_buffer();
338 req->handle = handle;
339 server_call_fd( REQ_GET_WRITE_FD, -1, &unix_handle );
341 else ERR( "bad access %08lx\n", access );
342 return unix_handle;
346 /*************************************************************************
347 * FILE_OpenConsole
349 * Open a handle to the current process console.
351 static HANDLE FILE_OpenConsole( BOOL output, DWORD access, LPSECURITY_ATTRIBUTES sa )
353 int ret = -1;
355 SERVER_START_REQ
357 struct open_console_request *req = server_alloc_req( sizeof(*req), 0 );
359 req->output = output;
360 req->access = access;
361 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
362 SetLastError(0);
363 if (!server_call( REQ_OPEN_CONSOLE )) ret = req->handle;
365 SERVER_END_REQ;
366 return ret;
370 /***********************************************************************
371 * FILE_CreateFile
373 * Implementation of CreateFile. Takes a Unix path name.
375 HANDLE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
376 LPSECURITY_ATTRIBUTES sa, DWORD creation,
377 DWORD attributes, HANDLE template, BOOL fail_read_only )
379 DWORD err;
380 HANDLE ret;
381 size_t len = strlen(filename);
383 if (len > REQUEST_MAX_VAR_SIZE)
385 FIXME("filename '%s' too long\n", filename );
386 SetLastError( ERROR_INVALID_PARAMETER );
387 return -1;
390 restart:
391 SERVER_START_REQ
393 struct create_file_request *req = server_alloc_req( sizeof(*req), len );
394 req->access = access;
395 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
396 req->sharing = sharing;
397 req->create = creation;
398 req->attrs = attributes;
399 memcpy( server_data_ptr(req), filename, len );
400 SetLastError(0);
401 err = server_call( REQ_CREATE_FILE );
402 ret = req->handle;
404 SERVER_END_REQ;
406 /* If write access failed, retry without GENERIC_WRITE */
408 if ((ret == -1) && !fail_read_only && (access & GENERIC_WRITE))
410 if ((err == STATUS_MEDIA_WRITE_PROTECTED) || (err == STATUS_ACCESS_DENIED))
412 TRACE("Write access failed for file '%s', trying without "
413 "write access\n", filename);
414 access &= ~GENERIC_WRITE;
415 goto restart;
419 if (ret == -1)
420 WARN("Unable to create file '%s' (GLE %ld)\n", filename,
421 GetLastError());
423 return ret;
427 /***********************************************************************
428 * FILE_CreateDevice
430 * Same as FILE_CreateFile but for a device
432 HFILE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
434 HFILE ret;
435 SERVER_START_REQ
437 struct create_device_request *req = server_alloc_req( sizeof(*req), 0 );
439 req->access = access;
440 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
441 req->id = client_id;
442 SetLastError(0);
443 server_call( REQ_CREATE_DEVICE );
444 ret = req->handle;
446 SERVER_END_REQ;
447 return ret;
451 /*************************************************************************
452 * CreateFileA [KERNEL32.45] Creates or opens a file or other object
454 * Creates or opens an object, and returns a handle that can be used to
455 * access that object.
457 * PARAMS
459 * filename [I] pointer to filename to be accessed
460 * access [I] access mode requested
461 * sharing [I] share mode
462 * sa [I] pointer to security attributes
463 * creation [I] how to create the file
464 * attributes [I] attributes for newly created file
465 * template [I] handle to file with extended attributes to copy
467 * RETURNS
468 * Success: Open handle to specified file
469 * Failure: INVALID_HANDLE_VALUE
471 * NOTES
472 * Should call SetLastError() on failure.
474 * BUGS
476 * Doesn't support character devices, pipes, template files, or a
477 * lot of the 'attributes' flags yet.
479 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
480 LPSECURITY_ATTRIBUTES sa, DWORD creation,
481 DWORD attributes, HANDLE template )
483 DOS_FULL_NAME full_name;
485 if (!filename)
487 SetLastError( ERROR_INVALID_PARAMETER );
488 return HFILE_ERROR;
490 TRACE("%s %s%s%s%s%s%s%s\n",filename,
491 ((access & GENERIC_READ)==GENERIC_READ)?"GENERIC_READ ":"",
492 ((access & GENERIC_WRITE)==GENERIC_WRITE)?"GENERIC_WRITE ":"",
493 (!access)?"QUERY_ACCESS ":"",
494 ((sharing & FILE_SHARE_READ)==FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
495 ((sharing & FILE_SHARE_WRITE)==FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
496 ((sharing & FILE_SHARE_DELETE)==FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
497 (creation ==CREATE_NEW)?"CREATE_NEW":
498 (creation ==CREATE_ALWAYS)?"CREATE_ALWAYS ":
499 (creation ==OPEN_EXISTING)?"OPEN_EXISTING ":
500 (creation ==OPEN_ALWAYS)?"OPEN_ALWAYS ":
501 (creation ==TRUNCATE_EXISTING)?"TRUNCATE_EXISTING ":"");
503 /* If the name starts with '\\?\', ignore the first 4 chars. */
504 if (!strncmp(filename, "\\\\?\\", 4))
506 filename += 4;
507 if (!strncmp(filename, "UNC\\", 4))
509 FIXME("UNC name (%s) not supported.\n", filename );
510 SetLastError( ERROR_PATH_NOT_FOUND );
511 return HFILE_ERROR;
515 if (!strncmp(filename, "\\\\.\\", 4)) {
516 if (!DOSFS_GetDevice( filename ))
517 return DEVICE_Open( filename+4, access, sa );
518 else
519 filename+=4; /* fall into DOSFS_Device case below */
522 /* If the name still starts with '\\', it's a UNC name. */
523 if (!strncmp(filename, "\\\\", 2))
525 FIXME("UNC name (%s) not supported.\n", filename );
526 SetLastError( ERROR_PATH_NOT_FOUND );
527 return HFILE_ERROR;
530 /* If the name contains a DOS wild card (* or ?), do no create a file */
531 if(strchr(filename,'*') || strchr(filename,'?'))
532 return HFILE_ERROR;
534 /* Open a console for CONIN$ or CONOUT$ */
535 if (!strcasecmp(filename, "CONIN$")) return FILE_OpenConsole( FALSE, access, sa );
536 if (!strcasecmp(filename, "CONOUT$")) return FILE_OpenConsole( TRUE, access, sa );
538 if (DOSFS_GetDevice( filename ))
540 HFILE ret;
542 TRACE("opening device '%s'\n", filename );
544 if (HFILE_ERROR!=(ret=DOSFS_OpenDevice( filename, access )))
545 return ret;
547 /* Do not silence this please. It is a critical error. -MM */
548 ERR("Couldn't open device '%s'!\n",filename);
549 SetLastError( ERROR_FILE_NOT_FOUND );
550 return HFILE_ERROR;
553 /* check for filename, don't check for last entry if creating */
554 if (!DOSFS_GetFullName( filename,
555 (creation == OPEN_EXISTING) ||
556 (creation == TRUNCATE_EXISTING),
557 &full_name )) {
558 WARN("Unable to get full filename from '%s' (GLE %ld)\n",
559 filename, GetLastError());
560 return HFILE_ERROR;
563 return FILE_CreateFile( full_name.long_name, access, sharing,
564 sa, creation, attributes, template,
565 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
570 /*************************************************************************
571 * CreateFileW (KERNEL32.48)
573 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
574 LPSECURITY_ATTRIBUTES sa, DWORD creation,
575 DWORD attributes, HANDLE template)
577 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
578 HANDLE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
579 HeapFree( GetProcessHeap(), 0, afn );
580 return res;
584 /***********************************************************************
585 * FILE_FillInfo
587 * Fill a file information from a struct stat.
589 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
591 if (S_ISDIR(st->st_mode))
592 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
593 else
594 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
595 if (!(st->st_mode & S_IWUSR))
596 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
598 RtlSecondsSince1970ToTime( st->st_mtime, &info->ftCreationTime );
599 RtlSecondsSince1970ToTime( st->st_mtime, &info->ftLastWriteTime );
600 RtlSecondsSince1970ToTime( st->st_atime, &info->ftLastAccessTime );
602 info->dwVolumeSerialNumber = 0; /* FIXME */
603 info->nFileSizeHigh = 0;
604 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
605 info->nNumberOfLinks = st->st_nlink;
606 info->nFileIndexHigh = 0;
607 info->nFileIndexLow = st->st_ino;
611 /***********************************************************************
612 * FILE_Stat
614 * Stat a Unix path name. Return TRUE if OK.
616 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
618 struct stat st;
620 if (lstat( unixName, &st ) == -1)
622 FILE_SetDosError();
623 return FALSE;
625 if (!S_ISLNK(st.st_mode)) FILE_FillInfo( &st, info );
626 else
628 /* do a "real" stat to find out
629 about the type of the symlink destination */
630 if (stat( unixName, &st ) == -1)
632 FILE_SetDosError();
633 return FALSE;
635 FILE_FillInfo( &st, info );
636 info->dwFileAttributes |= FILE_ATTRIBUTE_SYMLINK;
638 return TRUE;
642 /***********************************************************************
643 * GetFileInformationByHandle (KERNEL32.219)
645 DWORD WINAPI GetFileInformationByHandle( HANDLE hFile,
646 BY_HANDLE_FILE_INFORMATION *info )
648 DWORD ret;
649 if (!info) return 0;
651 SERVER_START_REQ
653 struct get_file_info_request *req = server_alloc_req( sizeof(*req), 0 );
654 req->handle = hFile;
655 if ((ret = !server_call( REQ_GET_FILE_INFO )))
657 RtlSecondsSince1970ToTime( req->write_time, &info->ftCreationTime );
658 RtlSecondsSince1970ToTime( req->write_time, &info->ftLastWriteTime );
659 RtlSecondsSince1970ToTime( req->access_time, &info->ftLastAccessTime );
660 info->dwFileAttributes = req->attr;
661 info->dwVolumeSerialNumber = req->serial;
662 info->nFileSizeHigh = req->size_high;
663 info->nFileSizeLow = req->size_low;
664 info->nNumberOfLinks = req->links;
665 info->nFileIndexHigh = req->index_high;
666 info->nFileIndexLow = req->index_low;
669 SERVER_END_REQ;
670 return ret;
674 /**************************************************************************
675 * GetFileAttributes16 (KERNEL.420)
677 DWORD WINAPI GetFileAttributes16( LPCSTR name )
679 return GetFileAttributesA( name );
683 /**************************************************************************
684 * GetFileAttributesA (KERNEL32.217)
686 DWORD WINAPI GetFileAttributesA( LPCSTR name )
688 DOS_FULL_NAME full_name;
689 BY_HANDLE_FILE_INFORMATION info;
691 if (name == NULL || *name=='\0') return -1;
693 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
694 if (!FILE_Stat( full_name.long_name, &info )) return -1;
695 return info.dwFileAttributes;
699 /**************************************************************************
700 * GetFileAttributesW (KERNEL32.218)
702 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
704 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
705 DWORD res = GetFileAttributesA( nameA );
706 HeapFree( GetProcessHeap(), 0, nameA );
707 return res;
711 /***********************************************************************
712 * GetFileSize (KERNEL32.220)
714 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
716 BY_HANDLE_FILE_INFORMATION info;
717 if (!GetFileInformationByHandle( hFile, &info )) return 0;
718 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
719 return info.nFileSizeLow;
723 /***********************************************************************
724 * GetFileTime (KERNEL32.221)
726 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
727 FILETIME *lpLastAccessTime,
728 FILETIME *lpLastWriteTime )
730 BY_HANDLE_FILE_INFORMATION info;
731 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
732 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
733 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
734 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
735 return TRUE;
738 /***********************************************************************
739 * CompareFileTime (KERNEL32.28)
741 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
743 if (!x || !y) return -1;
745 if (x->dwHighDateTime > y->dwHighDateTime)
746 return 1;
747 if (x->dwHighDateTime < y->dwHighDateTime)
748 return -1;
749 if (x->dwLowDateTime > y->dwLowDateTime)
750 return 1;
751 if (x->dwLowDateTime < y->dwLowDateTime)
752 return -1;
753 return 0;
756 /***********************************************************************
757 * FILE_GetTempFileName : utility for GetTempFileName
759 static UINT FILE_GetTempFileName( LPCSTR path, LPCSTR prefix, UINT unique,
760 LPSTR buffer, BOOL isWin16 )
762 static UINT unique_temp;
763 DOS_FULL_NAME full_name;
764 int i;
765 LPSTR p;
766 UINT num;
768 if ( !path || !prefix || !buffer ) return 0;
770 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
771 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
773 strcpy( buffer, path );
774 p = buffer + strlen(buffer);
776 /* add a \, if there isn't one and path is more than just the drive letter ... */
777 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
778 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
780 if (isWin16) *p++ = '~';
781 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
782 sprintf( p, "%04x.tmp", num );
784 /* Now try to create it */
786 if (!unique)
790 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
791 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
792 if (handle != INVALID_HANDLE_VALUE)
793 { /* We created it */
794 TRACE("created %s\n",
795 buffer);
796 CloseHandle( handle );
797 break;
799 if (GetLastError() != ERROR_FILE_EXISTS)
800 break; /* No need to go on */
801 num++;
802 sprintf( p, "%04x.tmp", num );
803 } while (num != (unique & 0xffff));
806 /* Get the full path name */
808 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
810 /* Check if we have write access in the directory */
811 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
812 if (access( full_name.long_name, W_OK ) == -1)
813 WARN("returns '%s', which doesn't seem to be writeable.\n",
814 buffer);
816 TRACE("returning %s\n", buffer );
817 return unique ? unique : num;
821 /***********************************************************************
822 * GetTempFileNameA (KERNEL32.290)
824 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
825 LPSTR buffer)
827 return FILE_GetTempFileName(path, prefix, unique, buffer, FALSE);
830 /***********************************************************************
831 * GetTempFileNameW (KERNEL32.291)
833 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
834 LPWSTR buffer )
836 LPSTR patha,prefixa;
837 char buffera[144];
838 UINT ret;
840 if (!path) return 0;
841 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
842 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
843 ret = FILE_GetTempFileName( patha, prefixa, unique, buffera, FALSE );
844 lstrcpyAtoW( buffer, buffera );
845 HeapFree( GetProcessHeap(), 0, patha );
846 HeapFree( GetProcessHeap(), 0, prefixa );
847 return ret;
851 /***********************************************************************
852 * GetTempFileName16 (KERNEL.97)
854 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
855 LPSTR buffer )
857 char temppath[144];
859 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
860 drive |= DRIVE_GetCurrentDrive() + 'A';
862 if ((drive & TF_FORCEDRIVE) &&
863 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
865 drive &= ~TF_FORCEDRIVE;
866 WARN("invalid drive %d specified\n", drive );
869 if (drive & TF_FORCEDRIVE)
870 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
871 else
872 GetTempPathA( 132, temppath );
873 return (UINT16)FILE_GetTempFileName( temppath, prefix, unique, buffer, TRUE );
876 /***********************************************************************
877 * FILE_DoOpenFile
879 * Implementation of OpenFile16() and OpenFile32().
881 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
882 BOOL win32 )
884 HFILE hFileRet;
885 FILETIME filetime;
886 WORD filedatetime[2];
887 DOS_FULL_NAME full_name;
888 DWORD access, sharing;
889 char *p;
891 if (!ofs) return HFILE_ERROR;
893 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
894 ((mode & 0x3 )==OF_READ)?"OF_READ":
895 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
896 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
897 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
898 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
899 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
900 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
901 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
902 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
903 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
904 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
905 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
906 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
907 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
908 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
909 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
910 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
914 ofs->cBytes = sizeof(OFSTRUCT);
915 ofs->nErrCode = 0;
916 if (mode & OF_REOPEN) name = ofs->szPathName;
918 if (!name) {
919 ERR("called with `name' set to NULL ! Please debug.\n");
920 return HFILE_ERROR;
923 TRACE("%s %04x\n", name, mode );
925 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
926 Are there any cases where getting the path here is wrong?
927 Uwe Bonnes 1997 Apr 2 */
928 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
929 ofs->szPathName, NULL )) goto error;
930 FILE_ConvertOFMode( mode, &access, &sharing );
932 /* OF_PARSE simply fills the structure */
934 if (mode & OF_PARSE)
936 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
937 != DRIVE_REMOVABLE);
938 TRACE("(%s): OF_PARSE, res = '%s'\n",
939 name, ofs->szPathName );
940 return 0;
943 /* OF_CREATE is completely different from all other options, so
944 handle it first */
946 if (mode & OF_CREATE)
948 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
949 sharing, NULL, CREATE_ALWAYS,
950 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
951 goto error;
952 goto success;
955 /* If OF_SEARCH is set, ignore the given path */
957 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
959 /* First try the file name as is */
960 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
961 /* Now remove the path */
962 if (name[0] && (name[1] == ':')) name += 2;
963 if ((p = strrchr( name, '\\' ))) name = p + 1;
964 if ((p = strrchr( name, '/' ))) name = p + 1;
965 if (!name[0]) goto not_found;
968 /* Now look for the file */
970 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
972 found:
973 TRACE("found %s = %s\n",
974 full_name.long_name, full_name.short_name );
975 lstrcpynA( ofs->szPathName, full_name.short_name,
976 sizeof(ofs->szPathName) );
978 if (mode & OF_SHARE_EXCLUSIVE)
979 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
980 on the file <tempdir>/_ins0432._mp to determine how
981 far installation has proceeded.
982 _ins0432._mp is an executable and while running the
983 application expects the open with OF_SHARE_ to fail*/
984 /* Probable FIXME:
985 As our loader closes the files after loading the executable,
986 we can't find the running executable with FILE_InUse.
987 Perhaps the loader should keep the file open.
988 Recheck against how Win handles that case */
990 char *last = strrchr(full_name.long_name,'/');
991 if (!last)
992 last = full_name.long_name - 1;
993 if (GetModuleHandle16(last+1))
995 TRACE("Denying shared open for %s\n",full_name.long_name);
996 return HFILE_ERROR;
1000 if (mode & OF_DELETE)
1002 if (unlink( full_name.long_name ) == -1) goto not_found;
1003 TRACE("(%s): OF_DELETE return = OK\n", name);
1004 return 1;
1007 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
1008 NULL, OPEN_EXISTING, 0, -1,
1009 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
1010 if (hFileRet == HFILE_ERROR) goto not_found;
1012 GetFileTime( hFileRet, NULL, NULL, &filetime );
1013 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
1014 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
1016 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
1018 CloseHandle( hFileRet );
1019 WARN("(%s): OF_VERIFY failed\n", name );
1020 /* FIXME: what error here? */
1021 SetLastError( ERROR_FILE_NOT_FOUND );
1022 goto error;
1025 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
1027 success: /* We get here if the open was successful */
1028 TRACE("(%s): OK, return = %d\n", name, hFileRet );
1029 if (win32)
1031 if (mode & OF_EXIST) /* Return the handle, but close it first */
1032 CloseHandle( hFileRet );
1034 else
1036 hFileRet = FILE_AllocDosHandle( hFileRet );
1037 if (hFileRet == HFILE_ERROR16) goto error;
1038 if (mode & OF_EXIST) /* Return the handle, but close it first */
1039 _lclose16( hFileRet );
1041 return hFileRet;
1043 not_found: /* We get here if the file does not exist */
1044 WARN("'%s' not found or sharing violation\n", name );
1045 SetLastError( ERROR_FILE_NOT_FOUND );
1046 /* fall through */
1048 error: /* We get here if there was an error opening the file */
1049 ofs->nErrCode = GetLastError();
1050 WARN("(%s): return = HFILE_ERROR error= %d\n",
1051 name,ofs->nErrCode );
1052 return HFILE_ERROR;
1056 /***********************************************************************
1057 * OpenFile16 (KERNEL.74)
1059 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
1061 return FILE_DoOpenFile( name, ofs, mode, FALSE );
1065 /***********************************************************************
1066 * OpenFile (KERNEL32.396)
1068 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
1070 return FILE_DoOpenFile( name, ofs, mode, TRUE );
1074 /***********************************************************************
1075 * FILE_InitProcessDosHandles
1077 * Allocates the default DOS handles for a process. Called either by
1078 * AllocDosHandle below or by the DOSVM stuff.
1080 static void FILE_InitProcessDosHandles( void )
1082 dos_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
1083 dos_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
1084 dos_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
1085 dos_handles[3] = GetStdHandle(STD_ERROR_HANDLE);
1086 dos_handles[4] = GetStdHandle(STD_ERROR_HANDLE);
1089 /***********************************************************************
1090 * FILE_AllocDosHandle
1092 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
1093 * longer valid after this function (even on failure).
1095 HFILE16 FILE_AllocDosHandle( HANDLE handle )
1097 int i;
1099 if (!handle || (handle == INVALID_HANDLE_VALUE))
1100 return INVALID_HANDLE_VALUE16;
1102 for (i = 5; i < DOS_TABLE_SIZE; i++)
1103 if (!dos_handles[i])
1105 dos_handles[i] = handle;
1106 TRACE("Got %d for h32 %d\n", i, handle );
1107 return i;
1109 CloseHandle( handle );
1110 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1111 return INVALID_HANDLE_VALUE16;
1115 /***********************************************************************
1116 * FILE_GetHandle
1118 * Return the Win32 handle for a DOS handle.
1120 HANDLE FILE_GetHandle( HFILE16 hfile )
1122 if (hfile < 5 && !dos_handles[hfile]) FILE_InitProcessDosHandles();
1123 if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
1125 SetLastError( ERROR_INVALID_HANDLE );
1126 return INVALID_HANDLE_VALUE;
1128 return dos_handles[hfile];
1132 /***********************************************************************
1133 * FILE_Dup2
1135 * dup2() function for DOS handles.
1137 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1139 HANDLE new_handle;
1141 if (hFile1 < 5 && !dos_handles[hFile1]) FILE_InitProcessDosHandles();
1143 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) || !dos_handles[hFile1])
1145 SetLastError( ERROR_INVALID_HANDLE );
1146 return HFILE_ERROR16;
1148 if (hFile2 < 5)
1150 FIXME("stdio handle closed, need proper conversion\n" );
1151 SetLastError( ERROR_INVALID_HANDLE );
1152 return HFILE_ERROR16;
1154 if (!DuplicateHandle( GetCurrentProcess(), dos_handles[hFile1],
1155 GetCurrentProcess(), &new_handle,
1156 0, FALSE, DUPLICATE_SAME_ACCESS ))
1157 return HFILE_ERROR16;
1158 if (dos_handles[hFile2]) CloseHandle( dos_handles[hFile2] );
1159 dos_handles[hFile2] = new_handle;
1160 return hFile2;
1164 /***********************************************************************
1165 * _lclose16 (KERNEL.81)
1167 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1169 if (hFile < 5)
1171 FIXME("stdio handle closed, need proper conversion\n" );
1172 SetLastError( ERROR_INVALID_HANDLE );
1173 return HFILE_ERROR16;
1175 if ((hFile >= DOS_TABLE_SIZE) || !dos_handles[hFile])
1177 SetLastError( ERROR_INVALID_HANDLE );
1178 return HFILE_ERROR16;
1180 TRACE("%d (handle32=%d)\n", hFile, dos_handles[hFile] );
1181 CloseHandle( dos_handles[hFile] );
1182 dos_handles[hFile] = 0;
1183 return 0;
1187 /***********************************************************************
1188 * _lclose (KERNEL32.592)
1190 HFILE WINAPI _lclose( HFILE hFile )
1192 TRACE("handle %d\n", hFile );
1193 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1196 /***********************************************************************
1197 * GetOverlappedResult (KERNEL32.360)
1199 BOOL WINAPI GetOverlappedResult(HANDLE hFile,LPOVERLAPPED lpOverlapped,
1200 LPDWORD lpNumberOfBytesTransferred,
1201 BOOL bWait)
1203 /* Since all i/o is currently synchronous,
1204 * return true, assuming ReadFile/WriteFile
1205 * have completed the operation */
1206 FIXME("NO Asynch I/O, assuming Read/Write succeeded\n" );
1207 return TRUE;
1210 /***********************************************************************
1211 * ReadFile (KERNEL32.428)
1213 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1214 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1216 int unix_handle, result;
1218 TRACE("%d %p %ld\n", hFile, buffer, bytesToRead );
1220 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1221 if (!bytesToRead) return TRUE;
1223 if ( overlapped ) {
1224 SetLastError ( ERROR_INVALID_PARAMETER );
1225 return FALSE;
1228 unix_handle = FILE_GetUnixHandle( hFile, GENERIC_READ );
1229 if (unix_handle == -1) return FALSE;
1230 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1232 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1233 if ((errno == EFAULT) && !VIRTUAL_HandleFault( buffer )) continue;
1234 FILE_SetDosError();
1235 break;
1237 close( unix_handle );
1238 if (result == -1) return FALSE;
1239 if (bytesRead) *bytesRead = result;
1240 return TRUE;
1244 /***********************************************************************
1245 * WriteFile (KERNEL32.578)
1247 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1248 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1250 int unix_handle, result;
1252 TRACE("%d %p %ld\n", hFile, buffer, bytesToWrite );
1254 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1255 if (!bytesToWrite) return TRUE;
1257 if ( overlapped ) {
1258 SetLastError ( ERROR_INVALID_PARAMETER );
1259 return FALSE;
1262 unix_handle = FILE_GetUnixHandle( hFile, GENERIC_WRITE );
1263 if (unix_handle == -1) return FALSE;
1264 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1266 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1267 if ((errno == EFAULT) && !VIRTUAL_HandleFault( buffer )) continue;
1268 if (errno == ENOSPC)
1269 SetLastError( ERROR_DISK_FULL );
1270 else
1271 FILE_SetDosError();
1272 break;
1274 close( unix_handle );
1275 if (result == -1) return FALSE;
1276 if (bytesWritten) *bytesWritten = result;
1277 return TRUE;
1281 /***********************************************************************
1282 * WIN16_hread
1284 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1286 LONG maxlen;
1288 TRACE("%d %08lx %ld\n",
1289 hFile, (DWORD)buffer, count );
1291 /* Some programs pass a count larger than the allocated buffer */
1292 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1293 if (count > maxlen) count = maxlen;
1294 return _lread(FILE_GetHandle(hFile), PTR_SEG_TO_LIN(buffer), count );
1298 /***********************************************************************
1299 * WIN16_lread
1301 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1303 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1307 /***********************************************************************
1308 * _lread (KERNEL32.596)
1310 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1312 DWORD result;
1313 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1314 return result;
1318 /***********************************************************************
1319 * _lread16 (KERNEL.82)
1321 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1323 return (UINT16)_lread(FILE_GetHandle(hFile), buffer, (LONG)count );
1327 /***********************************************************************
1328 * _lcreat16 (KERNEL.83)
1330 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1332 return FILE_AllocDosHandle( _lcreat( path, attr ) );
1336 /***********************************************************************
1337 * _lcreat (KERNEL32.593)
1339 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1341 /* Mask off all flags not explicitly allowed by the doc */
1342 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1343 TRACE("%s %02x\n", path, attr );
1344 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1345 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1346 CREATE_ALWAYS, attr, -1 );
1350 /***********************************************************************
1351 * SetFilePointer (KERNEL32.492)
1353 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword,
1354 DWORD method )
1356 DWORD ret = 0xffffffff;
1358 if (highword &&
1359 ((distance >= 0 && *highword != 0) || (distance < 0 && *highword != -1)))
1361 FIXME("64-bit offsets not supported yet\n"
1362 "SetFilePointer(%08x,%08lx,%08lx,%08lx)\n",
1363 hFile,distance,*highword,method);
1364 SetLastError( ERROR_INVALID_PARAMETER );
1365 return ret;
1367 TRACE("handle %d offset %ld origin %ld\n",
1368 hFile, distance, method );
1370 SERVER_START_REQ
1372 struct set_file_pointer_request *req = server_alloc_req( sizeof(*req), 0 );
1373 req->handle = hFile;
1374 req->low = distance;
1375 req->high = highword ? *highword : (distance >= 0) ? 0 : -1;
1376 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1377 req->whence = method;
1378 SetLastError( 0 );
1379 if (!server_call( REQ_SET_FILE_POINTER ))
1381 ret = req->new_low;
1382 if (highword) *highword = req->new_high;
1385 SERVER_END_REQ;
1386 return ret;
1390 /***********************************************************************
1391 * _llseek16 (KERNEL.84)
1393 * FIXME:
1394 * Seeking before the start of the file should be allowed for _llseek16,
1395 * but cause subsequent I/O operations to fail (cf. interrupt list)
1398 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1400 return SetFilePointer( FILE_GetHandle(hFile), lOffset, NULL, nOrigin );
1404 /***********************************************************************
1405 * _llseek (KERNEL32.594)
1407 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1409 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1413 /***********************************************************************
1414 * _lopen16 (KERNEL.85)
1416 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1418 return FILE_AllocDosHandle( _lopen( path, mode ) );
1422 /***********************************************************************
1423 * _lopen (KERNEL32.595)
1425 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1427 DWORD access, sharing;
1429 TRACE("('%s',%04x)\n", path, mode );
1430 FILE_ConvertOFMode( mode, &access, &sharing );
1431 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1435 /***********************************************************************
1436 * _lwrite16 (KERNEL.86)
1438 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1440 return (UINT16)_hwrite( FILE_GetHandle(hFile), buffer, (LONG)count );
1443 /***********************************************************************
1444 * _lwrite (KERNEL32.761)
1446 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1448 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1452 /***********************************************************************
1453 * _hread16 (KERNEL.349)
1455 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1457 return _lread( FILE_GetHandle(hFile), buffer, count );
1461 /***********************************************************************
1462 * _hread (KERNEL32.590)
1464 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1466 return _lread( hFile, buffer, count );
1470 /***********************************************************************
1471 * _hwrite16 (KERNEL.350)
1473 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1475 return _hwrite( FILE_GetHandle(hFile), buffer, count );
1479 /***********************************************************************
1480 * _hwrite (KERNEL32.591)
1482 * experimentation yields that _lwrite:
1483 * o truncates the file at the current position with
1484 * a 0 len write
1485 * o returns 0 on a 0 length write
1486 * o works with console handles
1489 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1491 DWORD result;
1493 TRACE("%d %p %ld\n", handle, buffer, count );
1495 if (!count)
1497 /* Expand or truncate at current position */
1498 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1499 return 0;
1501 if (!WriteFile( handle, buffer, count, &result, NULL ))
1502 return HFILE_ERROR;
1503 return result;
1507 /***********************************************************************
1508 * SetHandleCount16 (KERNEL.199)
1510 UINT16 WINAPI SetHandleCount16( UINT16 count )
1512 HGLOBAL16 hPDB = GetCurrentPDB16();
1513 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1514 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1516 TRACE("(%d)\n", count );
1518 if (count < 20) count = 20; /* No point in going below 20 */
1519 else if (count > 254) count = 254;
1521 if (count == 20)
1523 if (pdb->nbFiles > 20)
1525 memcpy( pdb->fileHandles, files, 20 );
1526 GlobalFree16( pdb->hFileHandles );
1527 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1528 GlobalHandleToSel16( hPDB ) );
1529 pdb->hFileHandles = 0;
1530 pdb->nbFiles = 20;
1533 else /* More than 20, need a new file handles table */
1535 BYTE *newfiles;
1536 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1537 if (!newhandle)
1539 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1540 return pdb->nbFiles;
1542 newfiles = (BYTE *)GlobalLock16( newhandle );
1544 if (count > pdb->nbFiles)
1546 memcpy( newfiles, files, pdb->nbFiles );
1547 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1549 else memcpy( newfiles, files, count );
1550 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1551 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1552 pdb->hFileHandles = newhandle;
1553 pdb->nbFiles = count;
1555 return pdb->nbFiles;
1559 /*************************************************************************
1560 * SetHandleCount (KERNEL32.494)
1562 UINT WINAPI SetHandleCount( UINT count )
1564 return min( 256, count );
1568 /***********************************************************************
1569 * FlushFileBuffers (KERNEL32.133)
1571 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
1573 BOOL ret;
1574 SERVER_START_REQ
1576 struct flush_file_request *req = server_alloc_req( sizeof(*req), 0 );
1577 req->handle = hFile;
1578 ret = !server_call( REQ_FLUSH_FILE );
1580 SERVER_END_REQ;
1581 return ret;
1585 /**************************************************************************
1586 * SetEndOfFile (KERNEL32.483)
1588 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1590 BOOL ret;
1591 SERVER_START_REQ
1593 struct truncate_file_request *req = server_alloc_req( sizeof(*req), 0 );
1594 req->handle = hFile;
1595 ret = !server_call( REQ_TRUNCATE_FILE );
1597 SERVER_END_REQ;
1598 return ret;
1602 /***********************************************************************
1603 * DeleteFile16 (KERNEL.146)
1605 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1607 return DeleteFileA( path );
1611 /***********************************************************************
1612 * DeleteFileA (KERNEL32.71)
1614 BOOL WINAPI DeleteFileA( LPCSTR path )
1616 DOS_FULL_NAME full_name;
1618 TRACE("'%s'\n", path );
1620 if (!*path)
1622 ERR("Empty path passed\n");
1623 return FALSE;
1625 if (DOSFS_GetDevice( path ))
1627 WARN("cannot remove DOS device '%s'!\n", path);
1628 SetLastError( ERROR_FILE_NOT_FOUND );
1629 return FALSE;
1632 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1633 if (unlink( full_name.long_name ) == -1)
1635 FILE_SetDosError();
1636 return FALSE;
1638 return TRUE;
1642 /***********************************************************************
1643 * DeleteFileW (KERNEL32.72)
1645 BOOL WINAPI DeleteFileW( LPCWSTR path )
1647 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1648 BOOL ret = DeleteFileA( xpath );
1649 HeapFree( GetProcessHeap(), 0, xpath );
1650 return ret;
1654 /***********************************************************************
1655 * GetFileType (KERNEL32.222)
1657 DWORD WINAPI GetFileType( HANDLE hFile )
1659 DWORD ret = FILE_TYPE_UNKNOWN;
1660 SERVER_START_REQ
1662 struct get_file_info_request *req = server_alloc_req( sizeof(*req), 0 );
1663 req->handle = hFile;
1664 if (!server_call( REQ_GET_FILE_INFO )) ret = req->type;
1666 SERVER_END_REQ;
1667 return ret;
1671 /**************************************************************************
1672 * MoveFileExA (KERNEL32.???)
1674 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1676 DOS_FULL_NAME full_name1, full_name2;
1678 TRACE("(%s,%s,%04lx)\n", fn1, fn2, flag);
1680 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1682 if (fn2) /* !fn2 means delete fn1 */
1684 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1686 /* target exists, check if we may overwrite */
1687 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1689 /* FIXME: Use right error code */
1690 SetLastError( ERROR_ACCESS_DENIED );
1691 return FALSE;
1694 else if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1696 /* Source name and target path are valid */
1698 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1700 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1701 Perhaps we should queue these command and execute it
1702 when exiting... What about using on_exit(2)
1704 FIXME("Please move existing file '%s' to file '%s' when Wine has finished\n",
1705 full_name1.long_name, full_name2.long_name);
1706 return TRUE;
1709 if (full_name1.drive != full_name2.drive)
1711 /* use copy, if allowed */
1712 if (!(flag & MOVEFILE_COPY_ALLOWED))
1714 /* FIXME: Use right error code */
1715 SetLastError( ERROR_FILE_EXISTS );
1716 return FALSE;
1718 return CopyFileA( fn1, fn2, !(flag & MOVEFILE_REPLACE_EXISTING) );
1720 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1722 FILE_SetDosError();
1723 return FALSE;
1725 return TRUE;
1727 else /* fn2 == NULL means delete source */
1729 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1731 if (flag & MOVEFILE_COPY_ALLOWED) {
1732 WARN("Illegal flag\n");
1733 SetLastError( ERROR_GEN_FAILURE );
1734 return FALSE;
1736 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1737 Perhaps we should queue these command and execute it
1738 when exiting... What about using on_exit(2)
1740 FIXME("Please delete file '%s' when Wine has finished\n",
1741 full_name1.long_name);
1742 return TRUE;
1745 if (unlink( full_name1.long_name ) == -1)
1747 FILE_SetDosError();
1748 return FALSE;
1750 return TRUE; /* successfully deleted */
1754 /**************************************************************************
1755 * MoveFileExW (KERNEL32.???)
1757 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1759 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1760 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1761 BOOL res = MoveFileExA( afn1, afn2, flag );
1762 HeapFree( GetProcessHeap(), 0, afn1 );
1763 HeapFree( GetProcessHeap(), 0, afn2 );
1764 return res;
1768 /**************************************************************************
1769 * MoveFileA (KERNEL32.387)
1771 * Move file or directory
1773 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1775 DOS_FULL_NAME full_name1, full_name2;
1776 struct stat fstat;
1778 TRACE("(%s,%s)\n", fn1, fn2 );
1780 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1781 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 )) {
1782 /* The new name must not already exist */
1783 SetLastError(ERROR_ALREADY_EXISTS);
1784 return FALSE;
1786 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1788 if (full_name1.drive == full_name2.drive) /* move */
1789 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1791 FILE_SetDosError();
1792 return FALSE;
1794 else return TRUE;
1795 else /*copy */ {
1796 if (stat( full_name1.long_name, &fstat ))
1798 WARN("Invalid source file %s\n",
1799 full_name1.long_name);
1800 FILE_SetDosError();
1801 return FALSE;
1803 if (S_ISDIR(fstat.st_mode)) {
1804 /* No Move for directories across file systems */
1805 /* FIXME: Use right error code */
1806 SetLastError( ERROR_GEN_FAILURE );
1807 return FALSE;
1809 else
1810 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1815 /**************************************************************************
1816 * MoveFileW (KERNEL32.390)
1818 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1820 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1821 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1822 BOOL res = MoveFileA( afn1, afn2 );
1823 HeapFree( GetProcessHeap(), 0, afn1 );
1824 HeapFree( GetProcessHeap(), 0, afn2 );
1825 return res;
1829 /**************************************************************************
1830 * CopyFileA (KERNEL32.36)
1832 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1834 HFILE h1, h2;
1835 BY_HANDLE_FILE_INFORMATION info;
1836 UINT count;
1837 BOOL ret = FALSE;
1838 int mode;
1839 char buffer[2048];
1841 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1842 if (!GetFileInformationByHandle( h1, &info ))
1844 CloseHandle( h1 );
1845 return FALSE;
1847 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1848 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1849 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1850 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1852 CloseHandle( h1 );
1853 return FALSE;
1855 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1857 char *p = buffer;
1858 while (count > 0)
1860 INT res = _lwrite( h2, p, count );
1861 if (res <= 0) goto done;
1862 p += res;
1863 count -= res;
1866 ret = TRUE;
1867 done:
1868 CloseHandle( h1 );
1869 CloseHandle( h2 );
1870 return ret;
1874 /**************************************************************************
1875 * CopyFileW (KERNEL32.37)
1877 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1879 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1880 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1881 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1882 HeapFree( GetProcessHeap(), 0, sourceA );
1883 HeapFree( GetProcessHeap(), 0, destA );
1884 return ret;
1888 /**************************************************************************
1889 * CopyFileExA (KERNEL32.858)
1891 * This implementation ignores most of the extra parameters passed-in into
1892 * the "ex" version of the method and calls the CopyFile method.
1893 * It will have to be fixed eventually.
1895 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1896 LPCSTR destFilename,
1897 LPPROGRESS_ROUTINE progressRoutine,
1898 LPVOID appData,
1899 LPBOOL cancelFlagPointer,
1900 DWORD copyFlags)
1902 BOOL failIfExists = FALSE;
1905 * Interpret the only flag that CopyFile can interpret.
1907 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1909 failIfExists = TRUE;
1912 return CopyFileA(sourceFilename, destFilename, failIfExists);
1915 /**************************************************************************
1916 * CopyFileExW (KERNEL32.859)
1918 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1919 LPCWSTR destFilename,
1920 LPPROGRESS_ROUTINE progressRoutine,
1921 LPVOID appData,
1922 LPBOOL cancelFlagPointer,
1923 DWORD copyFlags)
1925 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1926 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1928 BOOL ret = CopyFileExA(sourceA,
1929 destA,
1930 progressRoutine,
1931 appData,
1932 cancelFlagPointer,
1933 copyFlags);
1935 HeapFree( GetProcessHeap(), 0, sourceA );
1936 HeapFree( GetProcessHeap(), 0, destA );
1938 return ret;
1942 /***********************************************************************
1943 * SetFileTime (KERNEL32.650)
1945 BOOL WINAPI SetFileTime( HANDLE hFile,
1946 const FILETIME *lpCreationTime,
1947 const FILETIME *lpLastAccessTime,
1948 const FILETIME *lpLastWriteTime )
1950 BOOL ret;
1951 SERVER_START_REQ
1953 struct set_file_time_request *req = server_alloc_req( sizeof(*req), 0 );
1954 req->handle = hFile;
1955 if (lpLastAccessTime)
1956 RtlTimeToSecondsSince1970( lpLastAccessTime, (DWORD *)&req->access_time );
1957 else
1958 req->access_time = 0; /* FIXME */
1959 if (lpLastWriteTime)
1960 RtlTimeToSecondsSince1970( lpLastWriteTime, (DWORD *)&req->write_time );
1961 else
1962 req->write_time = 0; /* FIXME */
1963 ret = !server_call( REQ_SET_FILE_TIME );
1965 SERVER_END_REQ;
1966 return ret;
1970 /**************************************************************************
1971 * LockFile (KERNEL32.511)
1973 BOOL WINAPI LockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1974 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1976 BOOL ret;
1977 SERVER_START_REQ
1979 struct lock_file_request *req = server_alloc_req( sizeof(*req), 0 );
1981 req->handle = hFile;
1982 req->offset_low = dwFileOffsetLow;
1983 req->offset_high = dwFileOffsetHigh;
1984 req->count_low = nNumberOfBytesToLockLow;
1985 req->count_high = nNumberOfBytesToLockHigh;
1986 ret = !server_call( REQ_LOCK_FILE );
1988 SERVER_END_REQ;
1989 return ret;
1992 /**************************************************************************
1993 * LockFileEx [KERNEL32.512]
1995 * Locks a byte range within an open file for shared or exclusive access.
1997 * RETURNS
1998 * success: TRUE
1999 * failure: FALSE
2000 * NOTES
2002 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
2004 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
2005 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
2006 LPOVERLAPPED pOverlapped )
2008 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2009 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
2010 pOverlapped);
2011 if (reserved == 0)
2012 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2013 else
2015 ERR("reserved == %ld: Supposed to be 0??\n", reserved);
2016 SetLastError(ERROR_INVALID_PARAMETER);
2019 return FALSE;
2023 /**************************************************************************
2024 * UnlockFile (KERNEL32.703)
2026 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
2027 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
2029 BOOL ret;
2030 SERVER_START_REQ
2032 struct unlock_file_request *req = server_alloc_req( sizeof(*req), 0 );
2034 req->handle = hFile;
2035 req->offset_low = dwFileOffsetLow;
2036 req->offset_high = dwFileOffsetHigh;
2037 req->count_low = nNumberOfBytesToUnlockLow;
2038 req->count_high = nNumberOfBytesToUnlockHigh;
2039 ret = !server_call( REQ_UNLOCK_FILE );
2041 SERVER_END_REQ;
2042 return ret;
2046 /**************************************************************************
2047 * UnlockFileEx (KERNEL32.705)
2049 BOOL WINAPI UnlockFileEx(
2050 HFILE hFile,
2051 DWORD dwReserved,
2052 DWORD nNumberOfBytesToUnlockLow,
2053 DWORD nNumberOfBytesToUnlockHigh,
2054 LPOVERLAPPED lpOverlapped
2057 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2058 hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh,
2059 lpOverlapped);
2060 if (dwReserved == 0)
2061 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2062 else
2064 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved);
2065 SetLastError(ERROR_INVALID_PARAMETER);
2068 return FALSE;
2072 #if 0
2074 struct DOS_FILE_LOCK {
2075 struct DOS_FILE_LOCK * next;
2076 DWORD base;
2077 DWORD len;
2078 DWORD processId;
2079 FILE_OBJECT * dos_file;
2080 /* char * unix_name;*/
2083 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2085 static DOS_FILE_LOCK *locks = NULL;
2086 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2089 /* Locks need to be mirrored because unix file locking is based
2090 * on the pid. Inside of wine there can be multiple WINE processes
2091 * that share the same unix pid.
2092 * Read's and writes should check these locks also - not sure
2093 * how critical that is at this point (FIXME).
2096 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2098 DOS_FILE_LOCK *curr;
2099 DWORD processId;
2101 processId = GetCurrentProcessId();
2103 /* check if lock overlaps a current lock for the same file */
2104 #if 0
2105 for (curr = locks; curr; curr = curr->next) {
2106 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2107 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2108 return TRUE;/* region is identic */
2109 if ((f->l_start < (curr->base + curr->len)) &&
2110 ((f->l_start + f->l_len) > curr->base)) {
2111 /* region overlaps */
2112 return FALSE;
2116 #endif
2118 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(DOS_FILE_LOCK) );
2119 curr->processId = GetCurrentProcessId();
2120 curr->base = f->l_start;
2121 curr->len = f->l_len;
2122 /* curr->unix_name = HEAP_strdupA( GetProcessHeap(), 0, file->unix_name);*/
2123 curr->next = locks;
2124 curr->dos_file = file;
2125 locks = curr;
2126 return TRUE;
2129 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2131 DWORD processId;
2132 DOS_FILE_LOCK **curr;
2133 DOS_FILE_LOCK *rem;
2135 processId = GetCurrentProcessId();
2136 curr = &locks;
2137 while (*curr) {
2138 if ((*curr)->dos_file == file) {
2139 rem = *curr;
2140 *curr = (*curr)->next;
2141 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2142 HeapFree( GetProcessHeap(), 0, rem );
2144 else
2145 curr = &(*curr)->next;
2149 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2151 DWORD processId;
2152 DOS_FILE_LOCK **curr;
2153 DOS_FILE_LOCK *rem;
2155 processId = GetCurrentProcessId();
2156 for (curr = &locks; *curr; curr = &(*curr)->next) {
2157 if ((*curr)->processId == processId &&
2158 (*curr)->dos_file == file &&
2159 (*curr)->base == f->l_start &&
2160 (*curr)->len == f->l_len) {
2161 /* this is the same lock */
2162 rem = *curr;
2163 *curr = (*curr)->next;
2164 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2165 HeapFree( GetProcessHeap(), 0, rem );
2166 return TRUE;
2169 /* no matching lock found */
2170 return FALSE;
2174 /**************************************************************************
2175 * LockFile (KERNEL32.511)
2177 BOOL WINAPI LockFile(
2178 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2179 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2181 struct flock f;
2182 FILE_OBJECT *file;
2184 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2185 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2186 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2188 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2189 FIXME("Unimplemented bytes > 32bits\n");
2190 return FALSE;
2193 f.l_start = dwFileOffsetLow;
2194 f.l_len = nNumberOfBytesToLockLow;
2195 f.l_whence = SEEK_SET;
2196 f.l_pid = 0;
2197 f.l_type = F_WRLCK;
2199 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2201 /* shadow locks internally */
2202 if (!DOS_AddLock(file, &f)) {
2203 SetLastError( ERROR_LOCK_VIOLATION );
2204 return FALSE;
2207 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2208 #ifdef USE_UNIX_LOCKS
2209 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2210 if (errno == EACCES || errno == EAGAIN) {
2211 SetLastError( ERROR_LOCK_VIOLATION );
2213 else {
2214 FILE_SetDosError();
2216 /* remove our internal copy of the lock */
2217 DOS_RemoveLock(file, &f);
2218 return FALSE;
2220 #endif
2221 return TRUE;
2225 /**************************************************************************
2226 * UnlockFile (KERNEL32.703)
2228 BOOL WINAPI UnlockFile(
2229 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2230 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2232 FILE_OBJECT *file;
2233 struct flock f;
2235 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2236 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2237 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2239 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2240 WARN("Unimplemented bytes > 32bits\n");
2241 return FALSE;
2244 f.l_start = dwFileOffsetLow;
2245 f.l_len = nNumberOfBytesToUnlockLow;
2246 f.l_whence = SEEK_SET;
2247 f.l_pid = 0;
2248 f.l_type = F_UNLCK;
2250 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2252 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2254 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2255 #ifdef USE_UNIX_LOCKS
2256 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2257 FILE_SetDosError();
2258 return FALSE;
2260 #endif
2261 return TRUE;
2263 #endif
2265 /**************************************************************************
2266 * GetFileAttributesExA [KERNEL32.874]
2268 BOOL WINAPI GetFileAttributesExA(
2269 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2270 LPVOID lpFileInformation)
2272 DOS_FULL_NAME full_name;
2273 BY_HANDLE_FILE_INFORMATION info;
2275 if (lpFileName == NULL) return FALSE;
2276 if (lpFileInformation == NULL) return FALSE;
2278 if (fInfoLevelId == GetFileExInfoStandard) {
2279 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2280 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2281 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2282 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2284 lpFad->dwFileAttributes = info.dwFileAttributes;
2285 lpFad->ftCreationTime = info.ftCreationTime;
2286 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2287 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2288 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2289 lpFad->nFileSizeLow = info.nFileSizeLow;
2291 else {
2292 FIXME("invalid info level %d!\n", fInfoLevelId);
2293 return FALSE;
2296 return TRUE;
2300 /**************************************************************************
2301 * GetFileAttributesExW [KERNEL32.875]
2303 BOOL WINAPI GetFileAttributesExW(
2304 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2305 LPVOID lpFileInformation)
2307 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2308 BOOL res =
2309 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2310 HeapFree( GetProcessHeap(), 0, nameA );
2311 return res;