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