Use shell icon cache instead of an own IExtractIcon implementation.
[wine] / dlls / ntdll / file.c
1 /*
2  * Copyright 1999, 2000 Juergen Schmied
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
32 #endif
33 #ifdef HAVE_LINUX_MAJOR_H
34 # include <linux/major.h>
35 #endif
36 #ifdef HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
38 #endif
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_UTIME_H
46 # include <utime.h>
47 #endif
48 #ifdef STATFS_DEFINED_BY_SYS_VFS
49 # include <sys/vfs.h>
50 #else
51 # ifdef STATFS_DEFINED_BY_SYS_MOUNT
52 #  include <sys/mount.h>
53 # else
54 #  ifdef STATFS_DEFINED_BY_SYS_STATFS
55 #   include <sys/statfs.h>
56 #  endif
57 # endif
58 #endif
59
60 #define NONAMELESSUNION
61 #define NONAMELESSSTRUCT
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
64 #include "thread.h"
65 #include "wine/server.h"
66 #include "ntdll_misc.h"
67
68 #include "winternl.h"
69 #include "winioctl.h"
70
71 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
72
73 mode_t FILE_umask = 0;
74
75 #define SECSPERDAY         86400
76 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
77
78 /**************************************************************************
79  *                 NtOpenFile                           [NTDLL.@]
80  *                 ZwOpenFile                           [NTDLL.@]
81  *
82  * Open a file.
83  *
84  * PARAMS
85  *  handle    [O] Variable that receives the file handle on return
86  *  access    [I] Access desired by the caller to the file
87  *  attr      [I] Structure describing the file to be opened
88  *  io        [O] Receives details about the result of the operation
89  *  sharing   [I] Type of shared access the caller requires
90  *  options   [I] Options for the file open
91  *
92  * RETURNS
93  *  Success: 0. FileHandle and IoStatusBlock are updated.
94  *  Failure: An NTSTATUS error code describing the error.
95  */
96 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
97                             POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
98                             ULONG sharing, ULONG options )
99 {
100     return NtCreateFile( handle, access, attr, io, NULL, 0,
101                          sharing, FILE_OPEN, options, NULL, 0 );
102 }
103
104 /**************************************************************************
105  *              NtCreateFile                            [NTDLL.@]
106  *              ZwCreateFile                            [NTDLL.@]
107  *
108  * Either create a new file or directory, or open an existing file, device,
109  * directory or volume.
110  *
111  * PARAMS
112  *      handle       [O] Points to a variable which receives the file handle on return
113  *      access       [I] Desired access to the file
114  *      attr         [I] Structure describing the file
115  *      io           [O] Receives information about the operation on return
116  *      alloc_size   [I] Initial size of the file in bytes
117  *      attributes   [I] Attributes to create the file with
118  *      sharing      [I] Type of shared access the caller would like to the file
119  *      disposition  [I] Specifies what to do, depending on whether the file already exists
120  *      options      [I] Options for creating a new file
121  *      ea_buffer    [I] Pointer to an extended attributes buffer
122  *      ea_length    [I] Length of ea_buffer
123  *
124  * RETURNS
125  *  Success: 0. handle and io are updated.
126  *  Failure: An NTSTATUS error code describing the error.
127  */
128 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
129                               PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
130                               ULONG attributes, ULONG sharing, ULONG disposition,
131                               ULONG options, PVOID ea_buffer, ULONG ea_length )
132 {
133     static const WCHAR pipeW[] = {'\\','?','?','\\','p','i','p','e','\\'};
134     static const WCHAR mailslotW[] = {'\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
135     ANSI_STRING unix_name;
136     int created = FALSE;
137
138     TRACE("handle=%p access=%08lx name=%s objattr=%08lx root=%p sec=%p io=%p alloc_size=%p\n"
139           "attr=%08lx sharing=%08lx disp=%ld options=%08lx ea=%p.0x%08lx\n",
140           handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
141           attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
142           attributes, sharing, disposition, options, ea_buffer, ea_length );
143
144     if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
145
146     if (attr->RootDirectory)
147     {
148         FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
149         return STATUS_OBJECT_NAME_NOT_FOUND;
150     }
151     if (alloc_size) FIXME( "alloc_size not supported\n" );
152
153     /* check for named pipe */
154
155     if (attr->ObjectName->Length > sizeof(pipeW) &&
156         !memicmpW( attr->ObjectName->Buffer, pipeW, sizeof(pipeW)/sizeof(WCHAR) ))
157     {
158         SERVER_START_REQ( open_named_pipe )
159         {
160             req->access = access;
161             req->flags = options;
162             req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
163             wine_server_add_data( req, attr->ObjectName->Buffer + 4,
164                                   attr->ObjectName->Length - 4*sizeof(WCHAR) );
165             io->u.Status = wine_server_call( req );
166             *handle = reply->handle;
167         }
168         SERVER_END_REQ;
169         return io->u.Status;
170     }
171
172     /* check for mailslot */
173
174     if (attr->ObjectName->Length > sizeof(mailslotW) &&
175         !memicmpW( attr->ObjectName->Buffer, mailslotW, sizeof(mailslotW)/sizeof(WCHAR) ))
176     {
177         SERVER_START_REQ( open_mailslot )
178         {
179             req->access = access & GENERIC_WRITE;
180             req->sharing = sharing;
181             req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
182             wine_server_add_data( req, attr->ObjectName->Buffer + 4,
183                                   attr->ObjectName->Length - 4*sizeof(WCHAR) );
184             io->u.Status = wine_server_call( req );
185             *handle = reply->handle;
186         }
187         SERVER_END_REQ;
188         return io->u.Status;
189     }
190
191     io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
192                                               !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
193
194     if (io->u.Status == STATUS_NO_SUCH_FILE &&
195         disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
196     {
197         created = TRUE;
198         io->u.Status = STATUS_SUCCESS;
199     }
200
201     if (io->u.Status == STATUS_SUCCESS)
202     {
203         SERVER_START_REQ( create_file )
204         {
205             req->access     = access;
206             req->inherit    = (attr->Attributes & OBJ_INHERIT) != 0;
207             req->sharing    = sharing;
208             req->create     = disposition;
209             req->options    = options;
210             req->attrs      = attributes;
211             wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
212             io->u.Status = wine_server_call( req );
213             *handle = reply->handle;
214         }
215         SERVER_END_REQ;
216         RtlFreeAnsiString( &unix_name );
217     }
218     else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), io->u.Status );
219
220     if (io->u.Status == STATUS_SUCCESS)
221     {
222         if (created) io->Information = FILE_CREATED;
223         else switch(disposition)
224         {
225         case FILE_SUPERSEDE:
226             io->Information = FILE_SUPERSEDED;
227             break;
228         case FILE_CREATE:
229             io->Information = FILE_CREATED;
230             break;
231         case FILE_OPEN:
232         case FILE_OPEN_IF:
233             io->Information = FILE_OPENED;
234             break;
235         case FILE_OVERWRITE:
236         case FILE_OVERWRITE_IF:
237             io->Information = FILE_OVERWRITTEN;
238             break;
239         }
240     }
241
242     return io->u.Status;
243 }
244
245 /***********************************************************************
246  *                  Asynchronous file I/O                              *
247  */
248 static void WINAPI FILE_AsyncReadService(void*, PIO_STATUS_BLOCK, ULONG);
249 static void WINAPI FILE_AsyncWriteService(void*, PIO_STATUS_BLOCK, ULONG);
250
251 typedef struct async_fileio
252 {
253     HANDLE              handle;
254     PIO_APC_ROUTINE     apc;
255     void*               apc_user;
256     char*               buffer;
257     unsigned int        count;
258     off_t               offset;
259     int                 queue_apc_on_error;
260     BOOL                avail_mode;
261     int                 fd;
262     HANDLE              event;
263 } async_fileio;
264
265 static void fileio_terminate(async_fileio *fileio, IO_STATUS_BLOCK* iosb)
266 {
267     TRACE("data: %p\n", fileio);
268
269     wine_server_release_fd( fileio->handle, fileio->fd );
270     if ( fileio->event != INVALID_HANDLE_VALUE )
271         NtSetEvent( fileio->event, NULL );
272
273     if (fileio->apc && 
274         (iosb->u.Status == STATUS_SUCCESS || fileio->queue_apc_on_error))
275         fileio->apc( fileio->apc_user, iosb, iosb->Information );
276
277     RtlFreeHeap( GetProcessHeap(), 0, fileio );
278 }
279
280
281 static ULONG fileio_queue_async(async_fileio* fileio, IO_STATUS_BLOCK* iosb, 
282                                 BOOL do_read)
283 {
284     PIO_APC_ROUTINE     apc = do_read ? FILE_AsyncReadService : FILE_AsyncWriteService;
285     NTSTATUS            status;
286
287     SERVER_START_REQ( register_async )
288     {
289         req->handle = fileio->handle;
290         req->io_apc = apc;
291         req->io_sb = iosb;
292         req->io_user = fileio;
293         req->type = do_read ? ASYNC_TYPE_READ : ASYNC_TYPE_WRITE;
294         req->count = (fileio->count < iosb->Information) ? 
295             0 : fileio->count - iosb->Information;
296         status = wine_server_call( req );
297     }
298     SERVER_END_REQ;
299
300     if ( status ) iosb->u.Status = status;
301     if ( iosb->u.Status != STATUS_PENDING )
302     {
303         (apc)( fileio, iosb, iosb->u.Status );
304         return iosb->u.Status;
305     }
306     NtCurrentTeb()->num_async_io++;
307     return STATUS_SUCCESS;
308 }
309
310 /***********************************************************************
311  *           FILE_GetNtStatus(void)
312  *
313  * Retrieve the Nt Status code from errno.
314  * Try to be consistent with FILE_SetDosError().
315  */
316 NTSTATUS FILE_GetNtStatus(void)
317 {
318     int err = errno;
319
320     TRACE( "errno = %d\n", errno );
321     switch (err)
322     {
323     case EAGAIN:    return STATUS_SHARING_VIOLATION;
324     case EBADF:     return STATUS_INVALID_HANDLE;
325     case ENOSPC:    return STATUS_DISK_FULL;
326     case EPERM:
327     case EROFS:
328     case EACCES:    return STATUS_ACCESS_DENIED;
329     case ENOTDIR:   return STATUS_OBJECT_PATH_NOT_FOUND;
330     case ENOENT:    return STATUS_OBJECT_NAME_NOT_FOUND;
331     case EISDIR:    return STATUS_FILE_IS_A_DIRECTORY;
332     case EMFILE:
333     case ENFILE:    return STATUS_TOO_MANY_OPENED_FILES;
334     case EINVAL:    return STATUS_INVALID_PARAMETER;
335     case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
336     case EPIPE:     return STATUS_PIPE_BROKEN;
337     case EIO:       return STATUS_DEVICE_NOT_READY;
338 #ifdef ENOMEDIUM
339     case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
340 #endif
341     case ENOTTY:
342     case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
343     case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
344     case ENOEXEC:   /* ?? */
345     case ESPIPE:    /* ?? */
346     case EEXIST:    /* ?? */
347     default:
348         FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
349         return STATUS_UNSUCCESSFUL;
350     }
351 }
352
353 /***********************************************************************
354  *             FILE_AsyncReadService      (INTERNAL)
355  *
356  *  This function is called while the client is waiting on the
357  *  server, so we can't make any server calls here.
358  */
359 static void WINAPI FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
360 {
361     async_fileio *fileio = (async_fileio*)user;
362     int result;
363     int already = iosb->Information;
364
365     TRACE("%p %p %lu\n", iosb, fileio->buffer, status);
366
367     switch (status)
368     {
369     case STATUS_ALERTED: /* got some new data */
370         if (iosb->u.Status != STATUS_PENDING) FIXME("unexpected status %08lx\n", iosb->u.Status);
371         /* check to see if the data is ready (non-blocking) */
372         if ( fileio->avail_mode )
373             result = read(fileio->fd, &fileio->buffer[already], 
374                           fileio->count - already);
375         else
376         {
377             result = pread(fileio->fd, &fileio->buffer[already],
378                            fileio->count - already,
379                            fileio->offset + already);
380             if ((result < 0) && (errno == ESPIPE))
381                 result = read(fileio->fd, &fileio->buffer[already], 
382                               fileio->count - already);
383         }
384
385         if (result < 0)
386         {
387             if (errno == EAGAIN || errno == EINTR)
388             {
389                 TRACE("Deferred read %d\n", errno);
390                 iosb->u.Status = STATUS_PENDING;
391             }
392             else /* check to see if the transfer is complete */
393                 iosb->u.Status = FILE_GetNtStatus();
394         }
395         else if (result == 0)
396         {
397             iosb->u.Status = iosb->Information ? STATUS_SUCCESS : STATUS_END_OF_FILE;
398         }
399         else
400         {
401             iosb->Information += result;
402             if (iosb->Information >= fileio->count || fileio->avail_mode)
403                 iosb->u.Status = STATUS_SUCCESS;
404             else
405             {
406                 /* if we only have to read the available data, and none is available,
407                  * simply cancel the request. If data was available, it has been read
408                  * while in by previous call (NtDelayExecution)
409                  */
410                 iosb->u.Status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
411             }
412
413             TRACE("read %d more bytes %ld/%d so far (%s)\n",
414                   result, iosb->Information, fileio->count, 
415                   (iosb->u.Status == STATUS_SUCCESS) ? "success" : "pending");
416         }
417         /* queue another async operation ? */
418         if (iosb->u.Status == STATUS_PENDING)
419             fileio_queue_async(fileio, iosb, TRUE);
420         else
421             fileio_terminate(fileio, iosb);
422         break;
423     default:
424         iosb->u.Status = status;
425         fileio_terminate(fileio, iosb);
426         break;
427     }
428 }
429
430
431 /******************************************************************************
432  *  NtReadFile                                  [NTDLL.@]
433  *  ZwReadFile                                  [NTDLL.@]
434  *
435  * Read from an open file handle.
436  *
437  * PARAMS
438  *  FileHandle    [I] Handle returned from ZwOpenFile() or ZwCreateFile()
439  *  Event         [I] Event to signal upon completion (or NULL)
440  *  ApcRoutine    [I] Callback to call upon completion (or NULL)
441  *  ApcContext    [I] Context for ApcRoutine (or NULL)
442  *  IoStatusBlock [O] Receives information about the operation on return
443  *  Buffer        [O] Destination for the data read
444  *  Length        [I] Size of Buffer
445  *  ByteOffset    [O] Destination for the new file pointer position (or NULL)
446  *  Key           [O] Function unknown (may be NULL)
447  *
448  * RETURNS
449  *  Success: 0. IoStatusBlock is updated, and the Information member contains
450  *           The number of bytes read.
451  *  Failure: An NTSTATUS error code describing the error.
452  */
453 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
454                            PIO_APC_ROUTINE apc, void* apc_user,
455                            PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
456                            PLARGE_INTEGER offset, PULONG key)
457 {
458     int unix_handle, flags;
459
460     TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
461           hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
462
463     if (!io_status) return STATUS_ACCESS_VIOLATION;
464
465     io_status->Information = 0;
466     io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_READ, &unix_handle, &flags );
467     if (io_status->u.Status) return io_status->u.Status;
468
469     if (flags & FD_FLAG_RECV_SHUTDOWN)
470     {
471         wine_server_release_fd( hFile, unix_handle );
472         return STATUS_PIPE_DISCONNECTED;
473     }
474
475     if (flags & FD_FLAG_TIMEOUT)
476     {
477         if (hEvent)
478         {
479             /* this shouldn't happen, but check it */
480             FIXME("NIY-hEvent\n");
481             wine_server_release_fd( hFile, unix_handle );
482             return STATUS_NOT_IMPLEMENTED;
483         }
484         io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
485         if (io_status->u.Status)
486         {
487             wine_server_release_fd( hFile, unix_handle );
488             return io_status->u.Status;
489         }
490     }
491
492     if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
493     {
494         async_fileio*   fileio;
495         NTSTATUS ret;
496
497         if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
498         {
499             wine_server_release_fd( hFile, unix_handle );
500             if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
501             return STATUS_NO_MEMORY;
502         }
503         fileio->handle = hFile;
504         fileio->count = length;
505         if ( offset == NULL ) 
506             fileio->offset = 0;
507         else
508         {
509             fileio->offset = offset->QuadPart;
510             if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
511                 FIXME("High part of offset is lost\n");
512         } 
513         fileio->apc = apc;
514         fileio->apc_user = apc_user;
515         fileio->buffer = buffer;
516         fileio->queue_apc_on_error = 0;
517         fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
518         fileio->fd = unix_handle;  /* FIXME */
519         fileio->event = hEvent;
520         NtResetEvent(hEvent, NULL);
521
522         io_status->u.Status = STATUS_PENDING;
523         ret = fileio_queue_async(fileio, io_status, TRUE);
524         if (ret != STATUS_SUCCESS)
525         {
526             wine_server_release_fd( hFile, unix_handle );
527             if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
528             RtlFreeHeap(GetProcessHeap(), 0, fileio);
529             return ret;
530         }
531         if (flags & FD_FLAG_TIMEOUT)
532         {
533             ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
534             NtClose(hEvent);
535             if (ret != STATUS_USER_APC)
536                 fileio->queue_apc_on_error = 1;
537         }
538         else
539         {
540             LARGE_INTEGER   timeout;
541
542             /* let some APC be run, this will read some already pending data */
543             timeout.u.LowPart = timeout.u.HighPart = 0;
544             ret = NtDelayExecution( TRUE, &timeout );
545             /* the apc didn't run and therefore the completion routine now
546              * needs to be sent errors.
547              * Note that there is no race between setting this flag and
548              * returning errors because apc's are run only during alertable
549              * waits */
550             if (ret != STATUS_USER_APC)
551                 fileio->queue_apc_on_error = 1;
552         }
553         TRACE("= 0x%08lx\n", io_status->u.Status);
554         return io_status->u.Status;
555     }
556
557     if (offset)
558     {
559         FILE_POSITION_INFORMATION   fpi;
560
561         fpi.CurrentByteOffset = *offset;
562         io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi), 
563                                                    FilePositionInformation);
564         if (io_status->u.Status)
565         {
566             wine_server_release_fd( hFile, unix_handle );
567             return io_status->u.Status;
568         }
569     }
570     /* code for synchronous reads */
571     while ((io_status->Information = read( unix_handle, buffer, length )) == -1)
572     {
573         if ((errno == EAGAIN) || (errno == EINTR)) continue;
574         if (errno == EFAULT)
575         {
576             io_status->Information = 0;
577             io_status->u.Status = STATUS_ACCESS_VIOLATION;
578         }
579         else io_status->u.Status = FILE_GetNtStatus();
580         break;
581     }
582     wine_server_release_fd( hFile, unix_handle );
583     TRACE("= 0x%08lx\n", io_status->u.Status);
584     return io_status->u.Status;
585 }
586
587 /***********************************************************************
588  *             FILE_AsyncWriteService      (INTERNAL)
589  *
590  *  This function is called while the client is waiting on the
591  *  server, so we can't make any server calls here.
592  */
593 static void WINAPI FILE_AsyncWriteService(void *ovp, IO_STATUS_BLOCK *iosb, ULONG status)
594 {
595     async_fileio *fileio = (async_fileio *) ovp;
596     int result;
597     int already = iosb->Information;
598
599     TRACE("(%p %p %lu)\n",iosb, fileio->buffer, status);
600
601     switch (status)
602     {
603     case STATUS_ALERTED:
604         /* write some data (non-blocking) */
605         if ( fileio->avail_mode )
606             result = write(fileio->fd, &fileio->buffer[already], 
607                            fileio->count - already);
608         else
609         {
610             result = pwrite(fileio->fd, &fileio->buffer[already], 
611                             fileio->count - already, fileio->offset + already);
612             if ((result < 0) && (errno == ESPIPE))
613                 result = write(fileio->fd, &fileio->buffer[already], 
614                                fileio->count - already);
615         }
616
617         if (result < 0)
618         {
619             if (errno == EAGAIN || errno == EINTR) iosb->u.Status = STATUS_PENDING;
620             else iosb->u.Status = FILE_GetNtStatus();
621         }
622         else
623         {
624             iosb->Information += result;
625             iosb->u.Status = (iosb->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
626             TRACE("wrote %d more bytes %ld/%d so far\n", 
627                   result, iosb->Information, fileio->count);
628         }
629         if (iosb->u.Status == STATUS_PENDING)
630             fileio_queue_async(fileio, iosb, FALSE);
631         else
632             fileio_terminate(fileio, iosb);
633         break;
634     default:
635         iosb->u.Status = status;
636         fileio_terminate(fileio, iosb);
637         break;
638     }
639 }
640
641 /******************************************************************************
642  *  NtWriteFile                                 [NTDLL.@]
643  *  ZwWriteFile                                 [NTDLL.@]
644  *
645  * Write to an open file handle.
646  *
647  * PARAMS
648  *  FileHandle    [I] Handle returned from ZwOpenFile() or ZwCreateFile()
649  *  Event         [I] Event to signal upon completion (or NULL)
650  *  ApcRoutine    [I] Callback to call upon completion (or NULL)
651  *  ApcContext    [I] Context for ApcRoutine (or NULL)
652  *  IoStatusBlock [O] Receives information about the operation on return
653  *  Buffer        [I] Source for the data to write
654  *  Length        [I] Size of Buffer
655  *  ByteOffset    [O] Destination for the new file pointer position (or NULL)
656  *  Key           [O] Function unknown (may be NULL)
657  *
658  * RETURNS
659  *  Success: 0. IoStatusBlock is updated, and the Information member contains
660  *           The number of bytes written.
661  *  Failure: An NTSTATUS error code describing the error.
662  */
663 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
664                             PIO_APC_ROUTINE apc, void* apc_user,
665                             PIO_STATUS_BLOCK io_status, 
666                             const void* buffer, ULONG length,
667                             PLARGE_INTEGER offset, PULONG key)
668 {
669     int unix_handle, flags;
670
671     TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p)!\n",
672           hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
673
674     if (!io_status) return STATUS_ACCESS_VIOLATION;
675
676     io_status->Information = 0;
677     io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_WRITE, &unix_handle, &flags );
678     if (io_status->u.Status) return io_status->u.Status;
679
680     if (flags & FD_FLAG_SEND_SHUTDOWN)
681     {
682         wine_server_release_fd( hFile, unix_handle );
683         return STATUS_PIPE_DISCONNECTED;
684     }
685
686     if (flags & FD_FLAG_TIMEOUT)
687     {
688         if (hEvent)
689         {
690             /* this shouldn't happen, but check it */
691             FIXME("NIY-hEvent\n");
692             wine_server_release_fd( hFile, unix_handle );
693             return STATUS_NOT_IMPLEMENTED;
694         }
695         io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
696         if (io_status->u.Status)
697         {
698             wine_server_release_fd( hFile, unix_handle );
699             return io_status->u.Status;
700         }
701     }
702
703     if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
704     {
705         async_fileio*   fileio;
706         NTSTATUS ret;
707
708         if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
709         {
710             wine_server_release_fd( hFile, unix_handle );
711             if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
712             return STATUS_NO_MEMORY;
713         }
714         fileio->handle = hFile;
715         fileio->count = length;
716         if (offset)
717         {
718             fileio->offset = offset->QuadPart;
719             if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
720                 FIXME("High part of offset is lost\n");
721         }
722         else  
723         {
724             fileio->offset = 0;
725         }
726         fileio->apc = apc;
727         fileio->apc_user = apc_user;
728         fileio->buffer = (void*)buffer;
729         fileio->queue_apc_on_error = 0;
730         fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
731         fileio->fd = unix_handle;  /* FIXME */
732         fileio->event = hEvent;
733         NtResetEvent(hEvent, NULL);
734
735         io_status->Information = 0;
736         io_status->u.Status = STATUS_PENDING;
737         ret = fileio_queue_async(fileio, io_status, FALSE);
738         if (ret != STATUS_SUCCESS)
739         {
740             wine_server_release_fd( hFile, unix_handle );
741             if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
742             RtlFreeHeap(GetProcessHeap(), 0, fileio);
743             return ret;
744         }
745         if (flags & FD_FLAG_TIMEOUT)
746         {
747             ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
748             NtClose(hEvent);
749             if (ret != STATUS_USER_APC)
750                 fileio->queue_apc_on_error = 1;
751         }
752         else
753         {
754             LARGE_INTEGER   timeout;
755
756             /* let some APC be run, this will write as much data as possible */
757             timeout.u.LowPart = timeout.u.HighPart = 0;
758             ret = NtDelayExecution( TRUE, &timeout );
759             /* the apc didn't run and therefore the completion routine now
760              * needs to be sent errors.
761              * Note that there is no race between setting this flag and
762              * returning errors because apc's are run only during alertable
763              * waits */
764             if (ret != STATUS_USER_APC)
765                 fileio->queue_apc_on_error = 1;
766         }
767         return io_status->u.Status;
768     }
769
770     if (offset)
771     {
772         FILE_POSITION_INFORMATION   fpi;
773
774         fpi.CurrentByteOffset = *offset;
775         io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
776                                                    FilePositionInformation);
777         if (io_status->u.Status)
778         {
779             wine_server_release_fd( hFile, unix_handle );
780             return io_status->u.Status;
781         }
782     }
783
784     /* synchronous file write */
785     while ((io_status->Information = write( unix_handle, buffer, length )) == -1)
786     {
787         if ((errno == EAGAIN) || (errno == EINTR)) continue;
788         if (errno == EFAULT)
789         {
790             io_status->Information = 0;
791             io_status->u.Status = STATUS_INVALID_USER_BUFFER;
792         }
793         else if (errno == ENOSPC) io_status->u.Status = STATUS_DISK_FULL;
794         else io_status->u.Status = FILE_GetNtStatus();
795         break;
796     }
797     wine_server_release_fd( hFile, unix_handle );
798     return io_status->u.Status;
799 }
800
801 /**************************************************************************
802  *              NtDeviceIoControlFile                   [NTDLL.@]
803  *              ZwDeviceIoControlFile                   [NTDLL.@]
804  *
805  * Perform an I/O control operation on an open file handle.
806  *
807  * PARAMS
808  *  DeviceHandle     [I] Handle returned from ZwOpenFile() or ZwCreateFile()
809  *  Event            [I] Event to signal upon completion (or NULL)
810  *  ApcRoutine       [I] Callback to call upon completion (or NULL)
811  *  ApcContext       [I] Context for ApcRoutine (or NULL)
812  *  IoStatusBlock    [O] Receives information about the operation on return
813  *  IoControlCode    [I] Control code for the operation to perform
814  *  InputBuffer      [I] Source for any input data required (or NULL)
815  *  InputBufferSize  [I] Size of InputBuffer
816  *  OutputBuffer     [O] Source for any output data returned (or NULL)
817  *  OutputBufferSize [I] Size of OutputBuffer
818  *
819  * RETURNS
820  *  Success: 0. IoStatusBlock is updated.
821  *  Failure: An NTSTATUS error code describing the error.
822  */
823 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE DeviceHandle, HANDLE hEvent,
824                                       PIO_APC_ROUTINE UserApcRoutine, 
825                                       PVOID UserApcContext,
826                                       PIO_STATUS_BLOCK IoStatusBlock,
827                                       ULONG IoControlCode,
828                                       PVOID InputBuffer,
829                                       ULONG InputBufferSize,
830                                       PVOID OutputBuffer,
831                                       ULONG OutputBufferSize)
832 {
833     TRACE("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx)\n",
834           DeviceHandle, hEvent, UserApcRoutine, UserApcContext,
835           IoStatusBlock, IoControlCode, 
836           InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize);
837
838     if (CDROM_DeviceIoControl(DeviceHandle, hEvent,
839                               UserApcRoutine, UserApcContext,
840                               IoStatusBlock, IoControlCode,
841                               InputBuffer, InputBufferSize,
842                               OutputBuffer, OutputBufferSize) == STATUS_NO_SUCH_DEVICE)
843     {
844         /* it wasn't a CDROM */
845         FIXME("Unimplemented dwIoControlCode=%08lx\n", IoControlCode);
846         IoStatusBlock->u.Status = STATUS_NOT_IMPLEMENTED;
847         IoStatusBlock->Information = 0;
848         if (hEvent) NtSetEvent(hEvent, NULL);
849     }
850     return IoStatusBlock->u.Status;
851 }
852
853 /******************************************************************************
854  * NtFsControlFile [NTDLL.@]
855  * ZwFsControlFile [NTDLL.@]
856  */
857 NTSTATUS WINAPI NtFsControlFile(
858         IN HANDLE DeviceHandle,
859         IN HANDLE Event OPTIONAL,
860         IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
861         IN PVOID ApcContext OPTIONAL,
862         OUT PIO_STATUS_BLOCK IoStatusBlock,
863         IN ULONG IoControlCode,
864         IN PVOID InputBuffer,
865         IN ULONG InputBufferSize,
866         OUT PVOID OutputBuffer,
867         IN ULONG OutputBufferSize)
868 {
869         FIXME("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx): stub\n",
870         DeviceHandle,Event,ApcRoutine,ApcContext,IoStatusBlock,IoControlCode,
871         InputBuffer,InputBufferSize,OutputBuffer,OutputBufferSize);
872         return 0;
873 }
874
875 /******************************************************************************
876  *  NtSetVolumeInformationFile          [NTDLL.@]
877  *  ZwSetVolumeInformationFile          [NTDLL.@]
878  *
879  * Set volume information for an open file handle.
880  *
881  * PARAMS
882  *  FileHandle         [I] Handle returned from ZwOpenFile() or ZwCreateFile()
883  *  IoStatusBlock      [O] Receives information about the operation on return
884  *  FsInformation      [I] Source for volume information
885  *  Length             [I] Size of FsInformation
886  *  FsInformationClass [I] Type of volume information to set
887  *
888  * RETURNS
889  *  Success: 0. IoStatusBlock is updated.
890  *  Failure: An NTSTATUS error code describing the error.
891  */
892 NTSTATUS WINAPI NtSetVolumeInformationFile(
893         IN HANDLE FileHandle,
894         PIO_STATUS_BLOCK IoStatusBlock,
895         PVOID FsInformation,
896         ULONG Length,
897         FS_INFORMATION_CLASS FsInformationClass)
898 {
899         FIXME("(%p,%p,%p,0x%08lx,0x%08x) stub\n",
900         FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
901         return 0;
902 }
903
904 /******************************************************************************
905  *  NtQueryInformationFile              [NTDLL.@]
906  *  ZwQueryInformationFile              [NTDLL.@]
907  *
908  * Get information about an open file handle.
909  *
910  * PARAMS
911  *  hFile    [I] Handle returned from ZwOpenFile() or ZwCreateFile()
912  *  io       [O] Receives information about the operation on return
913  *  ptr      [O] Destination for file information
914  *  len      [I] Size of FileInformation
915  *  class    [I] Type of file information to get
916  *
917  * RETURNS
918  *  Success: 0. IoStatusBlock and FileInformation are updated.
919  *  Failure: An NTSTATUS error code describing the error.
920  */
921 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
922                                         PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
923 {
924     static const size_t info_sizes[] =
925     {
926         0,
927         sizeof(FILE_DIRECTORY_INFORMATION),            /* FileDirectoryInformation */
928         sizeof(FILE_FULL_DIRECTORY_INFORMATION),       /* FileFullDirectoryInformation */
929         sizeof(FILE_BOTH_DIRECTORY_INFORMATION),       /* FileBothDirectoryInformation */
930         sizeof(FILE_BASIC_INFORMATION),                /* FileBasicInformation */
931         sizeof(FILE_STANDARD_INFORMATION),             /* FileStandardInformation */
932         sizeof(FILE_INTERNAL_INFORMATION),             /* FileInternalInformation */
933         sizeof(FILE_EA_INFORMATION),                   /* FileEaInformation */
934         sizeof(FILE_ACCESS_INFORMATION),               /* FileAccessInformation */
935         sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR),   /* FileNameInformation */
936         sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
937         0,                                             /* FileLinkInformation */
938         sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR),  /* FileNamesInformation */
939         sizeof(FILE_DISPOSITION_INFORMATION),          /* FileDispositionInformation */
940         sizeof(FILE_POSITION_INFORMATION),             /* FilePositionInformation */
941         sizeof(FILE_FULL_EA_INFORMATION),              /* FileFullEaInformation */
942         sizeof(FILE_MODE_INFORMATION),                 /* FileModeInformation */
943         sizeof(FILE_ALIGNMENT_INFORMATION),            /* FileAlignmentInformation */
944         sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR),    /* FileAllInformation */
945         sizeof(FILE_ALLOCATION_INFORMATION),           /* FileAllocationInformation */
946         sizeof(FILE_END_OF_FILE_INFORMATION),          /* FileEndOfFileInformation */
947         0,                                             /* FileAlternateNameInformation */
948         sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
949         0,                                             /* FilePipeInformation */
950         0,                                             /* FilePipeLocalInformation */
951         0,                                             /* FilePipeRemoteInformation */
952         sizeof(FILE_MAILSLOT_QUERY_INFORMATION),       /* FileMailslotQueryInformation */
953         0,                                             /* FileMailslotSetInformation */
954         0,                                             /* FileCompressionInformation */
955         0,                                             /* FileObjectIdInformation */
956         0,                                             /* FileCompletionInformation */
957         0,                                             /* FileMoveClusterInformation */
958         0,                                             /* FileQuotaInformation */
959         0,                                             /* FileReparsePointInformation */
960         0,                                             /* FileNetworkOpenInformation */
961         0,                                             /* FileAttributeTagInformation */
962         0                                              /* FileTrackingInformation */
963     };
964
965     struct stat st;
966     int fd;
967
968     TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", hFile, io, ptr, len, class);
969
970     io->Information = 0;
971
972     if (class <= 0 || class >= FileMaximumInformation)
973         return io->u.Status = STATUS_INVALID_INFO_CLASS;
974     if (!info_sizes[class])
975     {
976         FIXME("Unsupported class (%d)\n", class);
977         return io->u.Status = STATUS_NOT_IMPLEMENTED;
978     }
979     if (len < info_sizes[class])
980         return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
981
982     if ((io->u.Status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
983         return io->u.Status;
984
985     switch (class)
986     {
987     case FileBasicInformation:
988         {
989             FILE_BASIC_INFORMATION *info = ptr;
990
991             if (fstat( fd, &st ) == -1)
992                 io->u.Status = FILE_GetNtStatus();
993             else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
994                 io->u.Status = STATUS_INVALID_INFO_CLASS;
995             else
996             {
997                 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
998                 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
999                 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1000                     info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1001                 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1002                 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1003                 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1004                 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1005             }
1006         }
1007         break;
1008     case FileStandardInformation:
1009         {
1010             FILE_STANDARD_INFORMATION *info = ptr;
1011
1012             if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1013             else
1014             {
1015                 if ((info->Directory = S_ISDIR(st.st_mode)))
1016                 {
1017                     info->AllocationSize.QuadPart = 0;
1018                     info->EndOfFile.QuadPart      = 0;
1019                     info->NumberOfLinks           = 1;
1020                     info->DeletePending           = FALSE;
1021                 }
1022                 else
1023                 {
1024                     info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1025                     info->EndOfFile.QuadPart      = st.st_size;
1026                     info->NumberOfLinks           = st.st_nlink;
1027                     info->DeletePending           = FALSE; /* FIXME */
1028                 }
1029             }
1030         }
1031         break;
1032     case FilePositionInformation:
1033         {
1034             FILE_POSITION_INFORMATION *info = ptr;
1035             off_t res = lseek( fd, 0, SEEK_CUR );
1036             if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1037             else info->CurrentByteOffset.QuadPart = res;
1038         }
1039         break;
1040     case FileInternalInformation:
1041         {
1042             FILE_INTERNAL_INFORMATION *info = ptr;
1043
1044             if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1045             else info->IndexNumber.QuadPart = st.st_ino;
1046         }
1047         break;
1048     case FileEaInformation:
1049         {
1050             FILE_EA_INFORMATION *info = ptr;
1051             info->EaSize = 0;
1052         }
1053         break;
1054     case FileEndOfFileInformation:
1055         {
1056             FILE_END_OF_FILE_INFORMATION *info = ptr;
1057
1058             if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1059             else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1060         }
1061         break;
1062     case FileAllInformation:
1063         {
1064             FILE_ALL_INFORMATION *info = ptr;
1065
1066             if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1067             else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1068                 io->u.Status = STATUS_INVALID_INFO_CLASS;
1069             else
1070             {
1071                 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1072                 {
1073                     info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1074                     info->StandardInformation.AllocationSize.QuadPart = 0;
1075                     info->StandardInformation.EndOfFile.QuadPart      = 0;
1076                     info->StandardInformation.NumberOfLinks           = 1;
1077                     info->StandardInformation.DeletePending           = FALSE;
1078                 }
1079                 else
1080                 {
1081                     info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1082                     info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1083                     info->StandardInformation.EndOfFile.QuadPart      = st.st_size;
1084                     info->StandardInformation.NumberOfLinks           = st.st_nlink;
1085                     info->StandardInformation.DeletePending           = FALSE; /* FIXME */
1086                 }
1087                 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1088                     info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1089                 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1090                 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1091                 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1092                 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1093                 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1094                 info->EaInformation.EaSize = 0;
1095                 info->AccessInformation.AccessFlags = 0;  /* FIXME */
1096                 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1097                 info->ModeInformation.Mode = 0;  /* FIXME */
1098                 info->AlignmentInformation.AlignmentRequirement = 1;  /* FIXME */
1099                 info->NameInformation.FileNameLength = 0;
1100                 io->Information = sizeof(*info) - sizeof(WCHAR);
1101             }
1102         }
1103         break;
1104     case FileMailslotQueryInformation:
1105         {
1106             FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1107
1108             SERVER_START_REQ( set_mailslot_info )
1109             {
1110                 req->handle = hFile;
1111                 req->flags = 0;
1112                 io->u.Status = wine_server_call( req );
1113                 if( io->u.Status == STATUS_SUCCESS )
1114                 {
1115                     info->MaximumMessageSize = reply->max_msgsize;
1116                     info->MailslotQuota = 0;
1117                     info->NextMessageSize = reply->next_msgsize;
1118                     info->MessagesAvailable = reply->msg_count;
1119                     info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1120                 }
1121             }
1122             SERVER_END_REQ;
1123         }
1124         break;
1125     default:
1126         FIXME("Unsupported class (%d)\n", class);
1127         io->u.Status = STATUS_NOT_IMPLEMENTED;
1128         break;
1129     }
1130     wine_server_release_fd( hFile, fd );
1131     if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1132     return io->u.Status;
1133 }
1134
1135 /******************************************************************************
1136  *  NtSetInformationFile                [NTDLL.@]
1137  *  ZwSetInformationFile                [NTDLL.@]
1138  *
1139  * Set information about an open file handle.
1140  *
1141  * PARAMS
1142  *  handle  [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1143  *  io      [O] Receives information about the operation on return
1144  *  ptr     [I] Source for file information
1145  *  len     [I] Size of FileInformation
1146  *  class   [I] Type of file information to set
1147  *
1148  * RETURNS
1149  *  Success: 0. io is updated.
1150  *  Failure: An NTSTATUS error code describing the error.
1151  */
1152 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1153                                      PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1154 {
1155     int fd;
1156
1157     TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", handle, io, ptr, len, class);
1158
1159     if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )))
1160         return io->u.Status;
1161
1162     io->u.Status = STATUS_SUCCESS;
1163     switch (class)
1164     {
1165     case FileBasicInformation:
1166         if (len >= sizeof(FILE_BASIC_INFORMATION))
1167         {
1168             struct stat st;
1169             const FILE_BASIC_INFORMATION *info = ptr;
1170
1171             if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1172             {
1173                 ULONGLONG sec, nsec;
1174                 struct timeval tv[2];
1175
1176                 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1177                 {
1178
1179                     tv[0].tv_sec = tv[0].tv_usec = 0;
1180                     tv[1].tv_sec = tv[1].tv_usec = 0;
1181                     if (!fstat( fd, &st ))
1182                     {
1183                         tv[0].tv_sec = st.st_atime;
1184                         tv[1].tv_sec = st.st_mtime;
1185                     }
1186                 }
1187                 if (info->LastAccessTime.QuadPart)
1188                 {
1189                     sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1190                     tv[0].tv_sec = sec - SECS_1601_TO_1970;
1191                     tv[0].tv_usec = (UINT)nsec / 10;
1192                 }
1193                 if (info->LastWriteTime.QuadPart)
1194                 {
1195                     sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1196                     tv[1].tv_sec = sec - SECS_1601_TO_1970;
1197                     tv[1].tv_usec = (UINT)nsec / 10;
1198                 }
1199                 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1200             }
1201
1202             if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1203             {
1204                 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1205                 else
1206                 {
1207                     if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1208                     {
1209                         if (S_ISDIR( st.st_mode))
1210                             WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1211                         else
1212                             st.st_mode &= ~0222; /* clear write permission bits */
1213                     }
1214                     else
1215                     {
1216                         /* add write permission only where we already have read permission */
1217                         st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1218                     }
1219                     if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1220                 }
1221             }
1222         }
1223         else io->u.Status = STATUS_INVALID_PARAMETER_3;
1224         break;
1225
1226     case FilePositionInformation:
1227         if (len >= sizeof(FILE_POSITION_INFORMATION))
1228         {
1229             const FILE_POSITION_INFORMATION *info = ptr;
1230
1231             if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1232                 io->u.Status = FILE_GetNtStatus();
1233         }
1234         else io->u.Status = STATUS_INVALID_PARAMETER_3;
1235         break;
1236
1237     case FileEndOfFileInformation:
1238         if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1239         {
1240             struct stat st;
1241             const FILE_END_OF_FILE_INFORMATION *info = ptr;
1242
1243             /* first try normal truncate */
1244             if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1245
1246             /* now check for the need to extend the file */
1247             if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1248             {
1249                 static const char zero;
1250
1251                 /* extend the file one byte beyond the requested size and then truncate it */
1252                 /* this should work around ftruncate implementations that can't extend files */
1253                 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1254                     ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1255             }
1256             io->u.Status = FILE_GetNtStatus();
1257         }
1258         else io->u.Status = STATUS_INVALID_PARAMETER_3;
1259         break;
1260
1261     case FileMailslotSetInformation:
1262         {
1263             FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1264
1265             SERVER_START_REQ( set_mailslot_info )
1266             {
1267                 req->handle = handle;
1268                 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1269                 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1270                 io->u.Status = wine_server_call( req );
1271             }
1272             SERVER_END_REQ;
1273         }
1274         break;
1275
1276     default:
1277         FIXME("Unsupported class (%d)\n", class);
1278         io->u.Status = STATUS_NOT_IMPLEMENTED;
1279         break;
1280     }
1281     wine_server_release_fd( handle, fd );
1282     io->Information = 0;
1283     return io->u.Status;
1284 }
1285
1286
1287 /******************************************************************************
1288  *              NtQueryFullAttributesFile   (NTDLL.@)
1289  */
1290 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1291                                            FILE_NETWORK_OPEN_INFORMATION *info )
1292 {
1293     ANSI_STRING unix_name;
1294     NTSTATUS status;
1295
1296     if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1297                                               !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1298     {
1299         struct stat st;
1300
1301         if (stat( unix_name.Buffer, &st ) == -1)
1302             status = FILE_GetNtStatus();
1303         else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1304             status = STATUS_INVALID_INFO_CLASS;
1305         else
1306         {
1307             if (S_ISDIR(st.st_mode))
1308             {
1309                 info->FileAttributes          = FILE_ATTRIBUTE_DIRECTORY;
1310                 info->AllocationSize.QuadPart = 0;
1311                 info->EndOfFile.QuadPart      = 0;
1312             }
1313             else
1314             {
1315                 info->FileAttributes          = FILE_ATTRIBUTE_ARCHIVE;
1316                 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1317                 info->EndOfFile.QuadPart      = st.st_size;
1318             }
1319             if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1320                 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1321             RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1322             RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1323             RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1324             RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1325             if (DIR_is_hidden_file( attr->ObjectName ))
1326                 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1327         }
1328         RtlFreeAnsiString( &unix_name );
1329     }
1330     else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), status );
1331     return status;
1332 }
1333
1334
1335 /******************************************************************************
1336  *              NtQueryAttributesFile   (NTDLL.@)
1337  *              ZwQueryAttributesFile   (NTDLL.@)
1338  */
1339 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1340 {
1341     FILE_NETWORK_OPEN_INFORMATION full_info;
1342     NTSTATUS status;
1343
1344     if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1345     {
1346         info->CreationTime.QuadPart   = full_info.CreationTime.QuadPart;
1347         info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1348         info->LastWriteTime.QuadPart  = full_info.LastWriteTime.QuadPart;
1349         info->ChangeTime.QuadPart     = full_info.ChangeTime.QuadPart;
1350         info->FileAttributes          = full_info.FileAttributes;
1351     }
1352     return status;
1353 }
1354
1355
1356 /******************************************************************************
1357  *  NtQueryVolumeInformationFile                [NTDLL.@]
1358  *  ZwQueryVolumeInformationFile                [NTDLL.@]
1359  *
1360  * Get volume information for an open file handle.
1361  *
1362  * PARAMS
1363  *  handle      [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1364  *  io          [O] Receives information about the operation on return
1365  *  buffer      [O] Destination for volume information
1366  *  length      [I] Size of FsInformation
1367  *  info_class  [I] Type of volume information to set
1368  *
1369  * RETURNS
1370  *  Success: 0. io and buffer are updated.
1371  *  Failure: An NTSTATUS error code describing the error.
1372  */
1373 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1374                                               PVOID buffer, ULONG length,
1375                                               FS_INFORMATION_CLASS info_class )
1376 {
1377     int fd;
1378     struct stat st;
1379
1380     if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )) != STATUS_SUCCESS)
1381         return io->u.Status;
1382
1383     io->u.Status = STATUS_NOT_IMPLEMENTED;
1384     io->Information = 0;
1385
1386     switch( info_class )
1387     {
1388     case FileFsVolumeInformation:
1389         FIXME( "%p: volume info not supported\n", handle );
1390         break;
1391     case FileFsLabelInformation:
1392         FIXME( "%p: label info not supported\n", handle );
1393         break;
1394     case FileFsSizeInformation:
1395         if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1396             io->u.Status = STATUS_BUFFER_TOO_SMALL;
1397         else
1398         {
1399             FILE_FS_SIZE_INFORMATION *info = buffer;
1400
1401             if (fstat( fd, &st ) < 0)
1402             {
1403                 io->u.Status = FILE_GetNtStatus();
1404                 break;
1405             }
1406             if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1407             {
1408                 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1409             }
1410             else
1411             {
1412                 /* Linux's fstatvfs is buggy */
1413 #if !defined(linux) || !defined(HAVE_FSTATFS)
1414                 struct statvfs stfs;
1415
1416                 if (fstatvfs( fd, &stfs ) < 0)
1417                 {
1418                     io->u.Status = FILE_GetNtStatus();
1419                     break;
1420                 }
1421                 info->BytesPerSector = stfs.f_frsize;
1422 #else
1423                 struct statfs stfs;
1424                 if (fstatfs( fd, &stfs ) < 0)
1425                 {
1426                     io->u.Status = FILE_GetNtStatus();
1427                     break;
1428                 }
1429                 info->BytesPerSector = stfs.f_bsize;
1430 #endif
1431                 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1432                 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1433                 info->SectorsPerAllocationUnit = 1;
1434                 io->Information = sizeof(*info);
1435                 io->u.Status = STATUS_SUCCESS;
1436             }
1437         }
1438         break;
1439     case FileFsDeviceInformation:
1440         if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1441             io->u.Status = STATUS_BUFFER_TOO_SMALL;
1442         else
1443         {
1444             FILE_FS_DEVICE_INFORMATION *info = buffer;
1445
1446             info->Characteristics = 0;
1447             if (fstat( fd, &st ) < 0)
1448             {
1449                 io->u.Status = FILE_GetNtStatus();
1450                 break;
1451             }
1452             if (S_ISCHR( st.st_mode ))
1453             {
1454                 info->DeviceType = FILE_DEVICE_UNKNOWN;
1455 #ifdef linux
1456                 switch(major(st.st_rdev))
1457                 {
1458                 case MEM_MAJOR:
1459                     info->DeviceType = FILE_DEVICE_NULL;
1460                     break;
1461                 case TTY_MAJOR:
1462                     info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1463                     break;
1464                 case LP_MAJOR:
1465                     info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1466                     break;
1467                 }
1468 #endif
1469             }
1470             else if (S_ISBLK( st.st_mode ))
1471             {
1472                 info->DeviceType = FILE_DEVICE_DISK;
1473             }
1474             else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1475             {
1476                 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1477             }
1478             else  /* regular file or directory */
1479             {
1480 #if defined(linux) && defined(HAVE_FSTATFS)
1481                 struct statfs stfs;
1482
1483                 /* check for floppy disk */
1484                 if (major(st.st_dev) == FLOPPY_MAJOR)
1485                     info->Characteristics |= FILE_REMOVABLE_MEDIA;
1486
1487                 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1488                 switch (stfs.f_type)
1489                 {
1490                 case 0x9660:      /* iso9660 */
1491                 case 0x15013346:  /* udf */
1492                     info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1493                     info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1494                     break;
1495                 case 0x6969:  /* nfs */
1496                 case 0x517B:  /* smbfs */
1497                 case 0x564c:  /* ncpfs */
1498                     info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1499                     info->Characteristics |= FILE_REMOTE_DEVICE;
1500                     break;
1501                 case 0x01021994:  /* tmpfs */
1502                 case 0x28cd3d45:  /* cramfs */
1503                 case 0x1373:      /* devfs */
1504                 case 0x9fa0:      /* procfs */
1505                     info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1506                     break;
1507                 default:
1508                     info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1509                     break;
1510                 }
1511 #elif defined(__FreeBSD__)
1512                 struct statfs stfs;
1513
1514                 /* The proper way to do this in FreeBSD seems to be with the
1515                  * name rather than the type, since their linux-compatible
1516                  * fstatfs call converts the name to one of the Linux types.
1517                  */
1518                 if (fstatfs( fd, &stfs ) < 0)
1519                     info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1520                 else if (!strncmp("cd9660", stfs.f_fstypename,
1521                  sizeof(stfs.f_fstypename)))
1522                 {
1523                     info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1524                     /* Don't assume read-only, let the mount options set it
1525                      * below
1526                      */
1527                     info->Characteristics |= FILE_REMOVABLE_MEDIA;
1528                 }
1529                 else if (!strncmp("nfs", stfs.f_fstypename,
1530                  sizeof(stfs.f_fstypename)))
1531                 {
1532                     info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1533                     info->Characteristics |= FILE_REMOTE_DEVICE;
1534                 }
1535                 else if (!strncmp("nwfs", stfs.f_fstypename,
1536                  sizeof(stfs.f_fstypename)))
1537                 {
1538                     info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1539                     info->Characteristics |= FILE_REMOTE_DEVICE;
1540                 }
1541                 else if (!strncmp("procfs", stfs.f_fstypename,
1542                  sizeof(stfs.f_fstypename)))
1543                     info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1544                 else
1545                     info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1546                 if (stfs.f_flags & MNT_RDONLY)
1547                     info->Characteristics |= FILE_READ_ONLY_DEVICE;
1548                 if (!(stfs.f_flags & MNT_LOCAL))
1549                 {
1550                     info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1551                     info->Characteristics |= FILE_REMOTE_DEVICE;
1552                 }
1553 #elif defined (__APPLE__)
1554 # include <IOKit/IOKitLib.h>
1555 # include <CoreFoundation/CFNumber.h> /* for kCFBooleanTrue, kCFBooleanFalse */
1556 # include <paths.h>
1557                 struct statfs stfs;
1558                 
1559                 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1560                 
1561                 if (fstatfs( fd, &stfs ) < 0) break;
1562
1563                 /* stfs.f_type is reserved (always set to 0) so use IOKit */
1564                 kern_return_t kernResult = KERN_FAILURE; 
1565                 mach_port_t masterPort;
1566                 
1567                 char bsdName[6]; /* disk#\0 */
1568                 const char *name = stfs.f_mntfromname + strlen(_PATH_DEV);
1569                 memcpy( bsdName, name, min(strlen(name)+1,sizeof(bsdName)) );
1570                 bsdName[sizeof(bsdName)-1] = 0;
1571
1572                 kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
1573
1574                 if (kernResult == KERN_SUCCESS)
1575                 {
1576                     CFMutableDictionaryRef matching = IOBSDNameMatching(masterPort, 0, bsdName);
1577                     
1578                     if (matching)
1579                     {
1580                         CFMutableDictionaryRef properties;
1581                         io_service_t devService = IOServiceGetMatchingService(masterPort, matching);
1582                         
1583                         if (IORegistryEntryCreateCFProperties(devService, 
1584                                                                 &properties,
1585                                                                 kCFAllocatorDefault, 0) != KERN_SUCCESS)
1586                                                                     break;
1587                         if ( CFEqual(
1588                                         CFDictionaryGetValue(properties, CFSTR("Removable")),
1589                                         kCFBooleanTrue)
1590                             ) info->Characteristics |= FILE_REMOVABLE_MEDIA;
1591                             
1592                         if ( CFEqual(
1593                                         CFDictionaryGetValue(properties, CFSTR("Writable")),
1594                                         kCFBooleanFalse)
1595                             ) info->Characteristics |= FILE_READ_ONLY_DEVICE;
1596
1597                         /*
1598                             NB : mounted disk image (.img/.dmg) don't provide specific type
1599                         */
1600                         CFStringRef type;
1601                         if ( (type = CFDictionaryGetValue(properties, CFSTR("Type"))) )
1602                         {
1603                             if ( CFStringCompare(type, CFSTR("CD-ROM"), 0) == kCFCompareEqualTo
1604                                 || CFStringCompare(type, CFSTR("DVD-ROM"), 0) == kCFCompareEqualTo
1605                             )
1606                             {
1607                                 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1608                             }
1609                         } 
1610                         
1611                         if (properties)
1612                             CFRelease(properties);
1613                     }
1614                 }
1615 #elif defined(sun)
1616                 /* Use dkio to work out device types */
1617                 {
1618 # include <sys/dkio.h>
1619 # include <sys/vtoc.h>
1620                     struct dk_cinfo dkinf;
1621                     int retval = ioctl(fd, DKIOCINFO, &dkinf);
1622                     if(retval==-1){
1623                         WARN("Unable to get disk device type information - assuming a disk like device\n");
1624                         info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1625                     }
1626                     switch (dkinf.dki_ctype)
1627                     {
1628                     case DKC_CDROM:
1629                         info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1630                         info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1631                         break;
1632                     case DKC_NCRFLOPPY:
1633                     case DKC_SMSFLOPPY:
1634                     case DKC_INTEL82072:
1635                     case DKC_INTEL82077:
1636                         info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1637                         info->Characteristics |= FILE_REMOVABLE_MEDIA;
1638                         break;
1639                     case DKC_MD:
1640                         info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1641                         break;
1642                     default:
1643                         info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1644                     }
1645                 }
1646 #else
1647                 static int warned;
1648                 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1649                 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1650 #endif
1651                 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1652             }
1653             io->Information = sizeof(*info);
1654             io->u.Status = STATUS_SUCCESS;
1655         }
1656         break;
1657     case FileFsAttributeInformation:
1658         FIXME( "%p: attribute info not supported\n", handle );
1659         break;
1660     case FileFsControlInformation:
1661         FIXME( "%p: control info not supported\n", handle );
1662         break;
1663     case FileFsFullSizeInformation:
1664         FIXME( "%p: full size info not supported\n", handle );
1665         break;
1666     case FileFsObjectIdInformation:
1667         FIXME( "%p: object id info not supported\n", handle );
1668         break;
1669     case FileFsMaximumInformation:
1670         FIXME( "%p: maximum info not supported\n", handle );
1671         break;
1672     default:
1673         io->u.Status = STATUS_INVALID_PARAMETER;
1674         break;
1675     }
1676     wine_server_release_fd( handle, fd );
1677     return io->u.Status;
1678 }
1679
1680
1681 /******************************************************************
1682  *              NtFlushBuffersFile  (NTDLL.@)
1683  *
1684  * Flush any buffered data on an open file handle.
1685  *
1686  * PARAMS
1687  *  FileHandle         [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1688  *  IoStatusBlock      [O] Receives information about the operation on return
1689  *
1690  * RETURNS
1691  *  Success: 0. IoStatusBlock is updated.
1692  *  Failure: An NTSTATUS error code describing the error.
1693  */
1694 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1695 {
1696     NTSTATUS ret;
1697     HANDLE hEvent = NULL;
1698
1699     SERVER_START_REQ( flush_file )
1700     {
1701         req->handle = hFile;
1702         ret = wine_server_call( req );
1703         hEvent = reply->event;
1704     }
1705     SERVER_END_REQ;
1706     if (!ret && hEvent)
1707     {
1708         ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1709         NtClose( hEvent );
1710     }
1711     return ret;
1712 }
1713
1714 /******************************************************************
1715  *              NtLockFile       (NTDLL.@)
1716  *
1717  *
1718  */
1719 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1720                             PIO_APC_ROUTINE apc, void* apc_user,
1721                             PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1722                             PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1723                             BOOLEAN exclusive )
1724 {
1725     NTSTATUS    ret;
1726     HANDLE      handle;
1727     BOOLEAN     async;
1728
1729     if (apc || io_status || key)
1730     {
1731         FIXME("Unimplemented yet parameter\n");
1732         return STATUS_NOT_IMPLEMENTED;
1733     }
1734
1735     for (;;)
1736     {
1737         SERVER_START_REQ( lock_file )
1738         {
1739             req->handle      = hFile;
1740             req->offset_low  = offset->u.LowPart;
1741             req->offset_high = offset->u.HighPart;
1742             req->count_low   = count->u.LowPart;
1743             req->count_high  = count->u.HighPart;
1744             req->shared      = !exclusive;
1745             req->wait        = !dont_wait;
1746             ret = wine_server_call( req );
1747             handle = reply->handle;
1748             async  = reply->overlapped;
1749         }
1750         SERVER_END_REQ;
1751         if (ret != STATUS_PENDING)
1752         {
1753             if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1754             return ret;
1755         }
1756
1757         if (async)
1758         {
1759             FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1760             if (handle) NtClose( handle );
1761             return STATUS_PENDING;
1762         }
1763         if (handle)
1764         {
1765             NtWaitForSingleObject( handle, FALSE, NULL );
1766             NtClose( handle );
1767         }
1768         else
1769         {
1770             LARGE_INTEGER time;
1771     
1772             /* Unix lock conflict, sleep a bit and retry */
1773             time.QuadPart = 100 * (ULONGLONG)10000;
1774             time.QuadPart = -time.QuadPart;
1775             NtDelayExecution( FALSE, &time );
1776         }
1777     }
1778 }
1779
1780
1781 /******************************************************************
1782  *              NtUnlockFile    (NTDLL.@)
1783  *
1784  *
1785  */
1786 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
1787                               PLARGE_INTEGER offset, PLARGE_INTEGER count,
1788                               PULONG key )
1789 {
1790     NTSTATUS status;
1791
1792     TRACE( "%p %lx%08lx %lx%08lx\n",
1793            hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
1794
1795     if (io_status || key)
1796     {
1797         FIXME("Unimplemented yet parameter\n");
1798         return STATUS_NOT_IMPLEMENTED;
1799     }
1800
1801     SERVER_START_REQ( unlock_file )
1802     {
1803         req->handle      = hFile;
1804         req->offset_low  = offset->u.LowPart;
1805         req->offset_high = offset->u.HighPart;
1806         req->count_low   = count->u.LowPart;
1807         req->count_high  = count->u.HighPart;
1808         status = wine_server_call( req );
1809     }
1810     SERVER_END_REQ;
1811     return status;
1812 }
1813
1814 /******************************************************************
1815  *              NtCreateNamedPipeFile    (NTDLL.@)
1816  *
1817  *
1818  */
1819 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
1820                                        POBJECT_ATTRIBUTES oa, PIO_STATUS_BLOCK iosb,
1821                                        ULONG sharing, ULONG dispo, ULONG options,
1822                                        ULONG pipe_type, ULONG read_mode, 
1823                                        ULONG completion_mode, ULONG max_inst,
1824                                        ULONG inbound_quota, ULONG outbound_quota,
1825                                        PLARGE_INTEGER timeout)
1826 {
1827     NTSTATUS    status;
1828     static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1829
1830     TRACE("(%p %lx %p %p %lx %ld %lx %ld %ld %ld %ld %ld %ld %p): stub\n",
1831           handle, access, oa, iosb, sharing, dispo, options, pipe_type,
1832           read_mode, completion_mode, max_inst, inbound_quota, outbound_quota,
1833           timeout);
1834
1835     if (oa->ObjectName->Length < sizeof(leadin) ||
1836         strncmpiW( oa->ObjectName->Buffer, 
1837                    leadin, sizeof(leadin)/sizeof(leadin[0]) ))
1838         return STATUS_OBJECT_NAME_INVALID;
1839     /* assume we only get relative timeout, and storable in a DWORD as ms */
1840     if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
1841         FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
1842
1843     SERVER_START_REQ( create_named_pipe )
1844     {
1845         req->options = options; /* FIXME not used in server yet !!!! */
1846         req->flags = 
1847             (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
1848             (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ  : 0 |
1849             (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE  : 0;
1850         req->maxinstances = max_inst;
1851         req->outsize = outbound_quota;
1852         req->insize  = inbound_quota;
1853         req->timeout = timeout->QuadPart / -10000;
1854         req->inherit = (oa->Attributes & OBJ_INHERIT) != 0;
1855         wine_server_add_data( req, oa->ObjectName->Buffer + 4, 
1856                               oa->ObjectName->Length - 4 * sizeof(WCHAR) );
1857         status = wine_server_call( req );
1858         if (!status) *handle = reply->handle;
1859     }
1860     SERVER_END_REQ;
1861     return status;
1862 }
1863
1864 /******************************************************************
1865  *              NtDeleteFile    (NTDLL.@)
1866  *
1867  *
1868  */
1869 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
1870 {
1871     NTSTATUS status;
1872     HANDLE hFile;
1873     IO_STATUS_BLOCK io;
1874
1875     TRACE("%p\n", ObjectAttributes);
1876     status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE, ObjectAttributes, 
1877                            &io, NULL, 0,
1878                            FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 
1879                            FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
1880     if (status == STATUS_SUCCESS) status = NtClose(hFile);
1881     return status;
1882 }
1883
1884 /******************************************************************
1885  *              NtCancelIoFile    (NTDLL.@)
1886  *
1887  *
1888  */
1889 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
1890 {
1891     LARGE_INTEGER timeout;
1892
1893     TRACE("%p %p\n", hFile, io_status );
1894
1895     SERVER_START_REQ( cancel_async )
1896     {
1897         req->handle = hFile;
1898         wine_server_call( req );
1899     }
1900     SERVER_END_REQ;
1901     /* Let some APC be run, so that we can run the remaining APCs on hFile
1902      * either the cancelation of the pending one, but also the execution
1903      * of the queued APC, but not yet run. This is needed to ensure proper
1904      * clean-up of allocated data.
1905      */
1906     timeout.u.LowPart = timeout.u.HighPart = 0;
1907     return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
1908 }
1909
1910 /******************************************************************************
1911  *  NtCreateMailslotFile        [NTDLL.@]
1912  *  ZwCreateMailslotFile        [NTDLL.@]
1913  *
1914  * PARAMS
1915  *  pHandle          [O] pointer to receive the handle created
1916  *  DesiredAccess    [I] access mode (read, write, etc)
1917  *  ObjectAttributes [I] fully qualified NT path of the mailslot
1918  *  IoStatusBlock    [O] receives completion status and other info
1919  *  CreateOptions    [I]
1920  *  MailslotQuota    [I]
1921  *  MaxMessageSize   [I]
1922  *  TimeOut          [I]
1923  *
1924  * RETURNS
1925  *  An NT status code
1926  */
1927 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
1928      POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
1929      ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
1930      PLARGE_INTEGER TimeOut)
1931 {
1932     static const WCHAR leadin[] = {
1933         '\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
1934     NTSTATUS ret;
1935
1936     TRACE("%p %08lx %p %p %08lx %08lx %08lx %p\n",
1937               pHandle, DesiredAccess, attr, IoStatusBlock,
1938               CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
1939
1940     if (attr->ObjectName->Length < sizeof(leadin) ||
1941         strncmpiW( attr->ObjectName->Buffer, 
1942                    leadin, sizeof(leadin)/sizeof(leadin[0]) ))
1943     {
1944         return STATUS_OBJECT_NAME_INVALID;
1945     }
1946
1947     SERVER_START_REQ( create_mailslot )
1948     {
1949         req->max_msgsize = MaxMessageSize;
1950         req->read_timeout = TimeOut->QuadPart / -10000;
1951         req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1952         wine_server_add_data( req, attr->ObjectName->Buffer + 4,
1953                               attr->ObjectName->Length - 4*sizeof(WCHAR) );
1954         ret = wine_server_call( req );
1955         if( ret == STATUS_SUCCESS )
1956             *pHandle = reply->handle;
1957     }
1958     SERVER_END_REQ;
1959  
1960     return ret;
1961 }