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