Replace strcasecmp() with lstrcmpiA() to help compilation with Windows
[wine] / dlls / ntdll / file.c
1 /*
2  * Copyright 1999, 2000 Juergen Schmied
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
32 #endif
33 #ifdef HAVE_LINUX_MAJOR_H
34 # include <linux/major.h>
35 #endif
36 #ifdef HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
38 #endif
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_UTIME_H
46 # include <utime.h>
47 #endif
48 #ifdef STATFS_DEFINED_BY_SYS_VFS
49 # include <sys/vfs.h>
50 #else
51 # ifdef STATFS_DEFINED_BY_SYS_MOUNT
52 #  include <sys/mount.h>
53 # else
54 #  ifdef STATFS_DEFINED_BY_SYS_STATFS
55 #   include <sys/statfs.h>
56 #  endif
57 # endif
58 #endif
59
60 #define NONAMELESSUNION
61 #define NONAMELESSSTRUCT
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
64 #include "wine/server.h"
65 #include "async.h"
66 #include "ntdll_misc.h"
67
68 #include "winternl.h"
69 #include "winioctl.h"
70
71 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
72
73 mode_t FILE_umask = 0;
74
75 #define SECSPERDAY         86400
76 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
77
78 /**************************************************************************
79  *                 NtOpenFile                           [NTDLL.@]
80  *                 ZwOpenFile                           [NTDLL.@]
81  *
82  * Open a file.
83  *
84  * PARAMS
85  *  handle    [O] Variable that receives the file handle on return
86  *  access    [I] Access desired by the caller to the file
87  *  attr      [I] Structue describing the file to be opened
88  *  io        [O] Receives details about the result of the operation
89  *  sharing   [I] Type of shared access the caller requires
90  *  options   [I] Options for the file open
91  *
92  * RETURNS
93  *  Success: 0. FileHandle and IoStatusBlock are updated.
94  *  Failure: An NTSTATUS error code describing the error.
95  */
96 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
97                             POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
98                             ULONG sharing, ULONG options )
99 {
100     return NtCreateFile( handle, access, attr, io, NULL, 0,
101                          sharing, FILE_OPEN, options, NULL, 0 );
102 }
103
104 /**************************************************************************
105  *              NtCreateFile                            [NTDLL.@]
106  *              ZwCreateFile                            [NTDLL.@]
107  *
108  * Either create a new file or directory, or open an existing file, device,
109  * directory or volume.
110  *
111  * PARAMS
112  *      handle       [O] Points to a variable which receives the file handle on return
113  *      access       [I] Desired access to the file
114  *      attr         [I] Structure describing the file
115  *      io           [O] Receives information about the operation on return
116  *      alloc_size   [I] Initial size of the file in bytes
117  *      attributes   [I] Attributes to create the file with
118  *      sharing      [I] Type of shared access the caller would like to the file
119  *      disposition  [I] Specifies what to do, depending on whether the file already exists
120  *      options      [I] Options for creating a new file
121  *      ea_buffer    [I] Undocumented
122  *      ea_length    [I] Undocumented
123  *
124  * RETURNS
125  *  Success: 0. handle and io are updated.
126  *  Failure: An NTSTATUS error code describing the error.
127  */
128 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
129                               PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
130                               ULONG attributes, ULONG sharing, ULONG disposition,
131                               ULONG options, PVOID ea_buffer, ULONG ea_length )
132 {
133     static const WCHAR pipeW[] = {'\\','?','?','\\','p','i','p','e','\\'};
134     ANSI_STRING unix_name;
135     int created = FALSE;
136
137     TRACE("handle=%p access=%08lx name=%s objattr=%08lx root=%p sec=%p io=%p alloc_size=%p\n"
138           "attr=%08lx sharing=%08lx disp=%ld options=%08lx ea=%p.0x%08lx\n",
139           handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
140           attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
141           attributes, sharing, disposition, options, ea_buffer, ea_length );
142
143     if (attr->RootDirectory)
144     {
145         FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
146         return STATUS_OBJECT_NAME_NOT_FOUND;
147     }
148     if (alloc_size) FIXME( "alloc_size not supported\n" );
149
150     /* check for named pipe */
151
152     if (attr->ObjectName->Length > sizeof(pipeW) &&
153         !memicmpW( attr->ObjectName->Buffer, pipeW, sizeof(pipeW)/sizeof(WCHAR) ))
154     {
155         SERVER_START_REQ( open_named_pipe )
156         {
157             req->access = access;
158             req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
159             wine_server_add_data( req, attr->ObjectName->Buffer + 4,
160                                   attr->ObjectName->Length - 4*sizeof(WCHAR) );
161             io->u.Status = wine_server_call( req );
162             *handle = reply->handle;
163         }
164         SERVER_END_REQ;
165         return io->u.Status;
166     }
167
168     io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
169                                               !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
170
171     if (io->u.Status == STATUS_NO_SUCH_FILE &&
172         disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
173     {
174         created = TRUE;
175         io->u.Status = STATUS_SUCCESS;
176     }
177
178     if (io->u.Status == STATUS_SUCCESS)
179     {
180         SERVER_START_REQ( create_file )
181         {
182             req->access     = access;
183             req->inherit    = (attr->Attributes & OBJ_INHERIT) != 0;
184             req->sharing    = sharing;
185             req->create     = disposition;
186             req->options    = options;
187             req->attrs      = attributes;
188             wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
189             io->u.Status = wine_server_call( req );
190             *handle = reply->handle;
191         }
192         SERVER_END_REQ;
193         RtlFreeAnsiString( &unix_name );
194     }
195     else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), io->u.Status );
196
197     if (io->u.Status == STATUS_SUCCESS)
198     {
199         if (created) io->Information = FILE_CREATED;
200         else switch(disposition)
201         {
202         case FILE_SUPERSEDE:
203             io->Information = FILE_SUPERSEDED;
204             break;
205         case FILE_CREATE:
206             io->Information = FILE_CREATED;
207             break;
208         case FILE_OPEN:
209         case FILE_OPEN_IF:
210             io->Information = FILE_OPENED;
211             break;
212         case FILE_OVERWRITE:
213         case FILE_OVERWRITE_IF:
214             io->Information = FILE_OVERWRITTEN;
215             break;
216         }
217     }
218
219     return io->u.Status;
220 }
221
222 /***********************************************************************
223  *                  Asynchronous file I/O                              *
224  */
225 static DWORD fileio_get_async_count(const async_private *ovp);
226 static void CALLBACK fileio_call_completion_func(ULONG_PTR data);
227 static void fileio_async_cleanup(async_private *ovp);
228
229 static async_ops fileio_async_ops =
230 {
231     fileio_get_async_count,        /* get_count */
232     fileio_call_completion_func,   /* call_completion */
233     fileio_async_cleanup           /* cleanup */
234 };
235
236 static async_ops fileio_nocomp_async_ops =
237 {
238     fileio_get_async_count,        /* get_count */
239     NULL,                          /* call_completion */
240     fileio_async_cleanup           /* cleanup */
241 };
242
243 typedef struct async_fileio
244 {
245     struct async_private             async;
246     PIO_APC_ROUTINE                  apc;
247     void*                            apc_user;
248     char                             *buffer;
249     unsigned int                     count;
250     unsigned long                    offset;
251     enum fd_type                     fd_type;
252 } async_fileio;
253
254 static DWORD fileio_get_async_count(const struct async_private *ovp)
255 {
256     async_fileio *fileio = (async_fileio*) ovp;
257
258     if (fileio->count < fileio->async.iosb->Information)
259         return 0;
260     return fileio->count - fileio->async.iosb->Information;
261 }
262
263 static void CALLBACK fileio_call_completion_func(ULONG_PTR data)
264 {
265     async_fileio *ovp = (async_fileio*) data;
266     TRACE("data: %p\n", ovp);
267
268     ovp->apc( ovp->apc_user, ovp->async.iosb, ovp->async.iosb->Information );
269
270     fileio_async_cleanup( &ovp->async );
271 }
272
273 static void fileio_async_cleanup( struct async_private *ovp )
274 {
275     RtlFreeHeap( GetProcessHeap(), 0, ovp );
276 }
277
278 /***********************************************************************
279  *           FILE_GetNtStatus(void)
280  *
281  * Retrieve the Nt Status code from errno.
282  * Try to be consistent with FILE_SetDosError().
283  */
284 NTSTATUS FILE_GetNtStatus(void)
285 {
286     int err = errno;
287
288     TRACE( "errno = %d\n", errno );
289     switch (err)
290     {
291     case EAGAIN:    return STATUS_SHARING_VIOLATION;
292     case EBADF:     return STATUS_INVALID_HANDLE;
293     case ENOSPC:    return STATUS_DISK_FULL;
294     case EPERM:
295     case EROFS:
296     case EACCES:    return STATUS_ACCESS_DENIED;
297     case ENOTDIR:   return STATUS_OBJECT_PATH_NOT_FOUND;
298     case ENOENT:    return STATUS_OBJECT_NAME_NOT_FOUND;
299     case EISDIR:    return STATUS_FILE_IS_A_DIRECTORY;
300     case EMFILE:
301     case ENFILE:    return STATUS_TOO_MANY_OPENED_FILES;
302     case EINVAL:    return STATUS_INVALID_PARAMETER;
303     case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
304     case EPIPE:     return STATUS_PIPE_BROKEN;
305     case EIO:       return STATUS_DEVICE_NOT_READY;
306 #ifdef ENOMEDIUM
307     case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
308 #endif
309     case ENOTTY:
310     case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
311     case ENOEXEC:   /* ?? */
312     case ESPIPE:    /* ?? */
313     case EEXIST:    /* ?? */
314     default:
315         FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
316         return STATUS_UNSUCCESSFUL;
317     }
318 }
319
320 /***********************************************************************
321  *             FILE_AsyncReadService      (INTERNAL)
322  *
323  *  This function is called while the client is waiting on the
324  *  server, so we can't make any server calls here.
325  */
326 static void FILE_AsyncReadService(async_private *ovp)
327 {
328     async_fileio *fileio = (async_fileio*) ovp;
329     IO_STATUS_BLOCK*  io_status = fileio->async.iosb;
330     int result;
331     int already = io_status->Information;
332
333     TRACE("%p %p\n", io_status, fileio->buffer );
334
335     /* check to see if the data is ready (non-blocking) */
336
337     if ( fileio->fd_type == FD_TYPE_SOCKET )
338         result = read(ovp->fd, &fileio->buffer[already], fileio->count - already);
339     else
340     {
341         result = pread(ovp->fd, &fileio->buffer[already], fileio->count - already,
342                        fileio->offset + already);
343         if ((result < 0) && (errno == ESPIPE))
344             result = read(ovp->fd, &fileio->buffer[already], fileio->count - already);
345     }
346
347     if ((result < 0) && ((errno == EAGAIN) || (errno == EINTR)))
348     {
349         TRACE("Deferred read %d\n",errno);
350         io_status->u.Status = STATUS_PENDING;
351         return;
352     }
353
354     /* check to see if the transfer is complete */
355     if (result < 0)
356     {
357         io_status->u.Status = FILE_GetNtStatus();
358         return;
359     }
360     else if (result == 0)
361     {
362         io_status->u.Status = io_status->Information ? STATUS_SUCCESS : STATUS_END_OF_FILE;
363         return;
364     }
365
366     io_status->Information += result;
367     if (io_status->Information >= fileio->count || fileio->fd_type == FD_TYPE_SOCKET )
368         io_status->u.Status = STATUS_SUCCESS;
369     else
370         io_status->u.Status = STATUS_PENDING;
371
372     TRACE("read %d more bytes %ld/%d so far\n",
373           result, io_status->Information, fileio->count);
374 }
375
376
377 /******************************************************************************
378  *  NtReadFile                                  [NTDLL.@]
379  *  ZwReadFile                                  [NTDLL.@]
380  *
381  * Read from an open file handle.
382  *
383  * PARAMS
384  *  FileHandle    [I] Handle returned from ZwOpenFile() or ZwCreateFile()
385  *  Event         [I] Event to signal upon completion (or NULL)
386  *  ApcRoutine    [I] Callback to call upon completion (or NULL)
387  *  ApcContext    [I] Context for ApcRoutine (or NULL)
388  *  IoStatusBlock [O] Receives information about the operation on return
389  *  Buffer        [O] Destination for the data read
390  *  Length        [I] Size of Buffer
391  *  ByteOffset    [O] Destination for the new file pointer position (or NULL)
392  *  Key           [O] Function unknown (may be NULL)
393  *
394  * RETURNS
395  *  Success: 0. IoStatusBlock is updated, and the Information member contains
396  *           The number of bytes read.
397  *  Failure: An NTSTATUS error code describing the error.
398  */
399 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
400                            PIO_APC_ROUTINE apc, void* apc_user,
401                            PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
402                            PLARGE_INTEGER offset, PULONG key)
403 {
404     int unix_handle, flags;
405     enum fd_type type;
406
407     TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
408           hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
409
410     io_status->Information = 0;
411     io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_READ, &unix_handle, &type, &flags );
412     if (io_status->u.Status) return io_status->u.Status;
413
414     if (flags & FD_FLAG_RECV_SHUTDOWN)
415     {
416         wine_server_release_fd( hFile, unix_handle );
417         return STATUS_PIPE_DISCONNECTED;
418     }
419
420     if (flags & FD_FLAG_TIMEOUT)
421     {
422         if (hEvent)
423         {
424             /* this shouldn't happen, but check it */
425             FIXME("NIY-hEvent\n");
426             wine_server_release_fd( hFile, unix_handle );
427             return STATUS_NOT_IMPLEMENTED;
428         }
429         io_status->u.Status = NtCreateEvent(&hEvent, SYNCHRONIZE, NULL, 0, 0);
430         if (io_status->u.Status)
431         {
432             wine_server_release_fd( hFile, unix_handle );
433             return io_status->u.Status;
434         }
435     }
436
437     if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
438     {
439         async_fileio*   ovp;
440         NTSTATUS ret;
441
442         if (!(ovp = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
443         {
444             wine_server_release_fd( hFile, unix_handle );
445             return STATUS_NO_MEMORY;
446         }
447         ovp->async.ops = (apc ? &fileio_async_ops : &fileio_nocomp_async_ops );
448         ovp->async.handle = hFile;
449         ovp->async.fd = unix_handle;  /* FIXME */
450         ovp->async.type = ASYNC_TYPE_READ;
451         ovp->async.func = FILE_AsyncReadService;
452         ovp->async.event = hEvent;
453         ovp->async.iosb = io_status;
454         ovp->count = length;
455         if ( offset == NULL ) 
456             ovp->offset = 0;
457         else
458         {
459             ovp->offset = offset->u.LowPart;
460             if (offset->u.HighPart) FIXME("NIY-high part\n");
461         } 
462         ovp->apc = apc;
463         ovp->apc_user = apc_user;
464         ovp->buffer = buffer;
465         ovp->fd_type = type;
466
467         io_status->Information = 0;
468         ret = register_new_async(&ovp->async);
469         if (ret != STATUS_SUCCESS)
470             return ret;
471         if (flags & FD_FLAG_TIMEOUT)
472         {
473             NtWaitForSingleObject(hEvent, TRUE, NULL);
474             NtClose(hEvent);
475         }
476         else
477         {
478             LARGE_INTEGER   timeout;
479
480             /* let some APC be run, this will read some already pending data */
481             timeout.u.LowPart = timeout.u.HighPart = 0;
482             NtDelayExecution( TRUE, &timeout );
483         }
484         return io_status->u.Status;
485     }
486
487     if (offset)
488     {
489         FILE_POSITION_INFORMATION   fpi;
490
491         fpi.CurrentByteOffset = *offset;
492         io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi), 
493                                                    FilePositionInformation);
494         if (io_status->u.Status)
495         {
496             wine_server_release_fd( hFile, unix_handle );
497             return io_status->u.Status;
498         }
499     }
500     /* code for synchronous reads */
501     while ((io_status->Information = read( unix_handle, buffer, length )) == -1)
502     {
503         if ((errno == EAGAIN) || (errno == EINTR)) continue;
504         if (errno == EFAULT) FIXME( "EFAULT handling broken for now\n" );
505         io_status->u.Status = FILE_GetNtStatus();
506         break;
507     }
508     wine_server_release_fd( hFile, unix_handle );
509     return io_status->u.Status;
510 }
511
512 /***********************************************************************
513  *             FILE_AsyncWriteService      (INTERNAL)
514  *
515  *  This function is called while the client is waiting on the
516  *  server, so we can't make any server calls here.
517  */
518 static void FILE_AsyncWriteService(struct async_private *ovp)
519 {
520     async_fileio *fileio = (async_fileio *) ovp;
521     PIO_STATUS_BLOCK io_status = fileio->async.iosb;
522     int result;
523     int already = io_status->Information;
524
525     TRACE("(%p %p)\n",io_status,fileio->buffer);
526
527     /* write some data (non-blocking) */
528
529     if ( fileio->fd_type == FD_TYPE_SOCKET )
530         result = write(ovp->fd, &fileio->buffer[already], fileio->count - already);
531     else
532     {
533         result = pwrite(ovp->fd, &fileio->buffer[already], fileio->count - already,
534                         fileio->offset + already);
535         if ((result < 0) && (errno == ESPIPE))
536             result = write(ovp->fd, &fileio->buffer[already], fileio->count - already);
537     }
538
539     if ((result < 0) && ((errno == EAGAIN) || (errno == EINTR)))
540     {
541         io_status->u.Status = STATUS_PENDING;
542         return;
543     }
544
545     /* check to see if the transfer is complete */
546     if (result < 0)
547     {
548         io_status->u.Status = FILE_GetNtStatus();
549         return;
550     }
551
552     io_status->Information += result;
553     io_status->u.Status = (io_status->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
554     TRACE("wrote %d more bytes %ld/%d so far\n",result,io_status->Information,fileio->count);
555 }
556
557 /******************************************************************************
558  *  NtWriteFile                                 [NTDLL.@]
559  *  ZwWriteFile                                 [NTDLL.@]
560  *
561  * Write to an open file handle.
562  *
563  * PARAMS
564  *  FileHandle    [I] Handle returned from ZwOpenFile() or ZwCreateFile()
565  *  Event         [I] Event to signal upon completion (or NULL)
566  *  ApcRoutine    [I] Callback to call upon completion (or NULL)
567  *  ApcContext    [I] Context for ApcRoutine (or NULL)
568  *  IoStatusBlock [O] Receives information about the operation on return
569  *  Buffer        [I] Source for the data to write
570  *  Length        [I] Size of Buffer
571  *  ByteOffset    [O] Destination for the new file pointer position (or NULL)
572  *  Key           [O] Function unknown (may be NULL)
573  *
574  * RETURNS
575  *  Success: 0. IoStatusBlock is updated, and the Information member contains
576  *           The number of bytes written.
577  *  Failure: An NTSTATUS error code describing the error.
578  */
579 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
580                             PIO_APC_ROUTINE apc, void* apc_user,
581                             PIO_STATUS_BLOCK io_status, 
582                             const void* buffer, ULONG length,
583                             PLARGE_INTEGER offset, PULONG key)
584 {
585     int unix_handle, flags;
586     enum fd_type type;
587
588     TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p)!\n",
589           hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
590
591     TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
592           hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
593
594     io_status->Information = 0;
595     io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_WRITE, &unix_handle, &type, &flags );
596     if (io_status->u.Status) return io_status->u.Status;
597
598     if (flags & FD_FLAG_SEND_SHUTDOWN)
599     {
600         wine_server_release_fd( hFile, unix_handle );
601         return STATUS_PIPE_DISCONNECTED;
602     }
603
604     if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
605     {
606         async_fileio*   ovp;
607         NTSTATUS ret;
608
609         if (!(ovp = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
610         {
611             wine_server_release_fd( hFile, unix_handle );
612             return STATUS_NO_MEMORY;
613         }
614         ovp->async.ops = (apc ? &fileio_async_ops : &fileio_nocomp_async_ops );
615         ovp->async.handle = hFile;
616         ovp->async.fd = unix_handle;  /* FIXME */
617         ovp->async.type = ASYNC_TYPE_WRITE;
618         ovp->async.func = FILE_AsyncWriteService;
619         ovp->async.event = hEvent;
620         ovp->async.iosb = io_status;
621         ovp->count = length;
622         if (offset) {
623             ovp->offset = offset->u.LowPart;
624             if (offset->u.HighPart) FIXME("NIY-high part\n");
625         } else {
626             ovp->offset = 0;
627         }
628         ovp->apc = apc;
629         ovp->apc_user = apc_user;
630         ovp->buffer = (void*)buffer;
631         ovp->fd_type = type;
632
633         io_status->Information = 0;
634         ret = register_new_async(&ovp->async);
635         if (ret != STATUS_SUCCESS)
636             return ret;
637         if (flags & FD_FLAG_TIMEOUT)
638         {
639             NtWaitForSingleObject(hEvent, TRUE, NULL);
640             NtClose(hEvent);
641         }
642         else
643         {
644             LARGE_INTEGER   timeout;
645
646             /* let some APC be run, this will write as much data as possible */
647             timeout.u.LowPart = timeout.u.HighPart = 0;
648             NtDelayExecution( TRUE, &timeout );
649         }
650         return io_status->u.Status;
651     }
652
653     if (offset)
654     {
655         FILE_POSITION_INFORMATION   fpi;
656
657         fpi.CurrentByteOffset = *offset;
658         io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
659                                                    FilePositionInformation);
660         if (io_status->u.Status)
661         {
662             wine_server_release_fd( hFile, unix_handle );
663             return io_status->u.Status;
664         }
665     }
666
667     /* synchronous file write */
668     while ((io_status->Information = write( unix_handle, buffer, length )) == -1)
669     {
670         if ((errno == EAGAIN) || (errno == EINTR)) continue;
671         if (errno == EFAULT) FIXME( "EFAULT handling broken for now\n" );
672         if (errno == ENOSPC) io_status->u.Status = STATUS_DISK_FULL;
673         else io_status->u.Status = FILE_GetNtStatus();
674         break;
675     }
676     wine_server_release_fd( hFile, unix_handle );
677     return io_status->u.Status;
678 }
679
680 /**************************************************************************
681  *              NtDeviceIoControlFile                   [NTDLL.@]
682  *              ZwDeviceIoControlFile                   [NTDLL.@]
683  *
684  * Perform an I/O control operation on an open file handle.
685  *
686  * PARAMS
687  *  DeviceHandle     [I] Handle returned from ZwOpenFile() or ZwCreateFile()
688  *  Event            [I] Event to signal upon completion (or NULL)
689  *  ApcRoutine       [I] Callback to call upon completion (or NULL)
690  *  ApcContext       [I] Context for ApcRoutine (or NULL)
691  *  IoStatusBlock    [O] Receives information about the operation on return
692  *  IoControlCode    [I] Control code for the operation to perform
693  *  InputBuffer      [I] Source for any input data required (or NULL)
694  *  InputBufferSize  [I] Size of InputBuffer
695  *  OutputBuffer     [O] Source for any output data returned (or NULL)
696  *  OutputBufferSize [I] Size of OutputBuffer
697  *
698  * RETURNS
699  *  Success: 0. IoStatusBlock is updated.
700  *  Failure: An NTSTATUS error code describing the error.
701  */
702 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE DeviceHandle, HANDLE hEvent,
703                                       PIO_APC_ROUTINE UserApcRoutine, 
704                                       PVOID UserApcContext,
705                                       PIO_STATUS_BLOCK IoStatusBlock,
706                                       ULONG IoControlCode,
707                                       PVOID InputBuffer,
708                                       ULONG InputBufferSize,
709                                       PVOID OutputBuffer,
710                                       ULONG OutputBufferSize)
711 {
712     TRACE("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx)\n",
713           DeviceHandle, hEvent, UserApcRoutine, UserApcContext,
714           IoStatusBlock, IoControlCode, 
715           InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize);
716
717     if (CDROM_DeviceIoControl(DeviceHandle, hEvent,
718                               UserApcRoutine, UserApcContext,
719                               IoStatusBlock, IoControlCode,
720                               InputBuffer, InputBufferSize,
721                               OutputBuffer, OutputBufferSize) == STATUS_NO_SUCH_DEVICE)
722     {
723         /* it wasn't a CDROM */
724         FIXME("Unimplemented dwIoControlCode=%08lx\n", IoControlCode);
725         IoStatusBlock->u.Status = STATUS_NOT_IMPLEMENTED;
726         IoStatusBlock->Information = 0;
727         if (hEvent) NtSetEvent(hEvent, NULL);
728     }
729     return IoStatusBlock->u.Status;
730 }
731
732 /******************************************************************************
733  * NtFsControlFile [NTDLL.@]
734  * ZwFsControlFile [NTDLL.@]
735  */
736 NTSTATUS WINAPI NtFsControlFile(
737         IN HANDLE DeviceHandle,
738         IN HANDLE Event OPTIONAL,
739         IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
740         IN PVOID ApcContext OPTIONAL,
741         OUT PIO_STATUS_BLOCK IoStatusBlock,
742         IN ULONG IoControlCode,
743         IN PVOID InputBuffer,
744         IN ULONG InputBufferSize,
745         OUT PVOID OutputBuffer,
746         IN ULONG OutputBufferSize)
747 {
748         FIXME("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx): stub\n",
749         DeviceHandle,Event,ApcRoutine,ApcContext,IoStatusBlock,IoControlCode,
750         InputBuffer,InputBufferSize,OutputBuffer,OutputBufferSize);
751         return 0;
752 }
753
754 /******************************************************************************
755  *  NtSetVolumeInformationFile          [NTDLL.@]
756  *  ZwSetVolumeInformationFile          [NTDLL.@]
757  *
758  * Set volume information for an open file handle.
759  *
760  * PARAMS
761  *  FileHandle         [I] Handle returned from ZwOpenFile() or ZwCreateFile()
762  *  IoStatusBlock      [O] Receives information about the operation on return
763  *  FsInformation      [I] Source for volume information
764  *  Length             [I] Size of FsInformation
765  *  FsInformationClass [I] Type of volume information to set
766  *
767  * RETURNS
768  *  Success: 0. IoStatusBlock is updated.
769  *  Failure: An NTSTATUS error code describing the error.
770  */
771 NTSTATUS WINAPI NtSetVolumeInformationFile(
772         IN HANDLE FileHandle,
773         PIO_STATUS_BLOCK IoStatusBlock,
774         PVOID FsInformation,
775         ULONG Length,
776         FS_INFORMATION_CLASS FsInformationClass)
777 {
778         FIXME("(%p,%p,%p,0x%08lx,0x%08x) stub\n",
779         FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
780         return 0;
781 }
782
783 /******************************************************************************
784  *  NtQueryInformationFile              [NTDLL.@]
785  *  ZwQueryInformationFile              [NTDLL.@]
786  *
787  * Get information about an open file handle.
788  *
789  * PARAMS
790  *  hFile    [I] Handle returned from ZwOpenFile() or ZwCreateFile()
791  *  io       [O] Receives information about the operation on return
792  *  ptr      [O] Destination for file information
793  *  len      [I] Size of FileInformation
794  *  class    [I] Type of file information to get
795  *
796  * RETURNS
797  *  Success: 0. IoStatusBlock and FileInformation are updated.
798  *  Failure: An NTSTATUS error code describing the error.
799  */
800 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
801                                         PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
802 {
803     static const size_t info_sizes[] =
804     {
805         0,
806         sizeof(FILE_DIRECTORY_INFORMATION),            /* FileDirectoryInformation */
807         sizeof(FILE_FULL_DIRECTORY_INFORMATION),       /* FileFullDirectoryInformation */
808         sizeof(FILE_BOTH_DIRECTORY_INFORMATION),       /* FileBothDirectoryInformation */
809         sizeof(FILE_BASIC_INFORMATION),                /* FileBasicInformation */
810         sizeof(FILE_STANDARD_INFORMATION),             /* FileStandardInformation */
811         sizeof(FILE_INTERNAL_INFORMATION),             /* FileInternalInformation */
812         sizeof(FILE_EA_INFORMATION),                   /* FileEaInformation */
813         sizeof(FILE_ACCESS_INFORMATION),               /* FileAccessInformation */
814         sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR),   /* FileNameInformation */
815         sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
816         0,                                             /* FileLinkInformation */
817         sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR),  /* FileNamesInformation */
818         sizeof(FILE_DISPOSITION_INFORMATION),          /* FileDispositionInformation */
819         sizeof(FILE_POSITION_INFORMATION),             /* FilePositionInformation */
820         sizeof(FILE_FULL_EA_INFORMATION),              /* FileFullEaInformation */
821         sizeof(FILE_MODE_INFORMATION),                 /* FileModeInformation */
822         sizeof(FILE_ALIGNMENT_INFORMATION),            /* FileAlignmentInformation */
823         sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR),    /* FileAllInformation */
824         sizeof(FILE_ALLOCATION_INFORMATION),           /* FileAllocationInformation */
825         sizeof(FILE_END_OF_FILE_INFORMATION),          /* FileEndOfFileInformation */
826         0,                                             /* FileAlternateNameInformation */
827         sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
828         0,                                             /* FilePipeInformation */
829         0,                                             /* FilePipeLocalInformation */
830         0,                                             /* FilePipeRemoteInformation */
831         0,                                             /* FileMailslotQueryInformation */
832         0,                                             /* FileMailslotSetInformation */
833         0,                                             /* FileCompressionInformation */
834         0,                                             /* FileObjectIdInformation */
835         0,                                             /* FileCompletionInformation */
836         0,                                             /* FileMoveClusterInformation */
837         0,                                             /* FileQuotaInformation */
838         0,                                             /* FileReparsePointInformation */
839         0,                                             /* FileNetworkOpenInformation */
840         0,                                             /* FileAttributeTagInformation */
841         0                                              /* FileTrackingInformation */
842     };
843
844     struct stat st;
845     int fd;
846
847     TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", hFile, io, ptr, len, class);
848
849     io->Information = 0;
850
851     if (class <= 0 || class >= FileMaximumInformation)
852         return io->u.Status = STATUS_INVALID_INFO_CLASS;
853     if (!info_sizes[class])
854     {
855         FIXME("Unsupported class (%d)\n", class);
856         return io->u.Status = STATUS_NOT_IMPLEMENTED;
857     }
858     if (len < info_sizes[class])
859         return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
860
861     if ((io->u.Status = wine_server_handle_to_fd( hFile, 0, &fd, NULL, NULL )))
862         return io->u.Status;
863
864     switch (class)
865     {
866     case FileBasicInformation:
867         {
868             FILE_BASIC_INFORMATION *info = ptr;
869
870             if (fstat( fd, &st ) == -1)
871                 io->u.Status = FILE_GetNtStatus();
872             else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
873                 io->u.Status = STATUS_INVALID_INFO_CLASS;
874             else
875             {
876                 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
877                 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
878                 if (!(st.st_mode & S_IWUSR)) info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
879                 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
880                 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
881                 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
882                 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
883             }
884         }
885         break;
886     case FileStandardInformation:
887         {
888             FILE_STANDARD_INFORMATION *info = ptr;
889
890             if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
891             else
892             {
893                 if ((info->Directory = S_ISDIR(st.st_mode)))
894                 {
895                     info->AllocationSize.QuadPart = 0;
896                     info->EndOfFile.QuadPart      = 0;
897                     info->NumberOfLinks           = 1;
898                     info->DeletePending           = FALSE;
899                 }
900                 else
901                 {
902                     info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
903                     info->EndOfFile.QuadPart      = st.st_size;
904                     info->NumberOfLinks           = st.st_nlink;
905                     info->DeletePending           = FALSE; /* FIXME */
906                 }
907             }
908         }
909         break;
910     case FilePositionInformation:
911         {
912             FILE_POSITION_INFORMATION *info = ptr;
913             off_t res = lseek( fd, 0, SEEK_CUR );
914             if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
915             else info->CurrentByteOffset.QuadPart = res;
916         }
917         break;
918     case FileInternalInformation:
919         {
920             FILE_INTERNAL_INFORMATION *info = ptr;
921
922             if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
923             else info->IndexNumber.QuadPart = st.st_ino;
924         }
925         break;
926     case FileEaInformation:
927         {
928             FILE_EA_INFORMATION *info = ptr;
929             info->EaSize = 0;
930         }
931         break;
932     case FileEndOfFileInformation:
933         {
934             FILE_END_OF_FILE_INFORMATION *info = ptr;
935
936             if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
937             else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
938         }
939         break;
940     case FileAllInformation:
941         {
942             FILE_ALL_INFORMATION *info = ptr;
943
944             if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
945             else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
946                 io->u.Status = STATUS_INVALID_INFO_CLASS;
947             else
948             {
949                 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
950                 {
951                     info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
952                     info->StandardInformation.AllocationSize.QuadPart = 0;
953                     info->StandardInformation.EndOfFile.QuadPart      = 0;
954                     info->StandardInformation.NumberOfLinks           = 1;
955                     info->StandardInformation.DeletePending           = FALSE;
956                 }
957                 else
958                 {
959                     info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
960                     info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
961                     info->StandardInformation.EndOfFile.QuadPart      = st.st_size;
962                     info->StandardInformation.NumberOfLinks           = st.st_nlink;
963                     info->StandardInformation.DeletePending           = FALSE; /* FIXME */
964                 }
965                 if (!(st.st_mode & S_IWUSR))
966                     info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
967                 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
968                 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
969                 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
970                 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
971                 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
972                 info->EaInformation.EaSize = 0;
973                 info->AccessInformation.AccessFlags = 0;  /* FIXME */
974                 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
975                 info->ModeInformation.Mode = 0;  /* FIXME */
976                 info->AlignmentInformation.AlignmentRequirement = 1;  /* FIXME */
977                 info->NameInformation.FileNameLength = 0;
978                 io->Information = sizeof(*info) - sizeof(WCHAR);
979             }
980         }
981         break;
982     default:
983         FIXME("Unsupported class (%d)\n", class);
984         io->u.Status = STATUS_NOT_IMPLEMENTED;
985         break;
986     }
987     wine_server_release_fd( hFile, fd );
988     if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
989     return io->u.Status;
990 }
991
992 /******************************************************************************
993  *  NtSetInformationFile                [NTDLL.@]
994  *  ZwSetInformationFile                [NTDLL.@]
995  *
996  * Set information about an open file handle.
997  *
998  * PARAMS
999  *  handle  [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1000  *  io      [O] Receives information about the operation on return
1001  *  ptr     [I] Source for file information
1002  *  len     [I] Size of FileInformation
1003  *  class   [I] Type of file information to set
1004  *
1005  * RETURNS
1006  *  Success: 0. io is updated.
1007  *  Failure: An NTSTATUS error code describing the error.
1008  */
1009 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1010                                      PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1011 {
1012     int fd;
1013
1014     TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", handle, io, ptr, len, class);
1015
1016     if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL )))
1017         return io->u.Status;
1018
1019     io->u.Status = STATUS_SUCCESS;
1020     switch (class)
1021     {
1022     case FileBasicInformation:
1023         if (len >= sizeof(FILE_BASIC_INFORMATION))
1024         {
1025             struct stat st;
1026             const FILE_BASIC_INFORMATION *info = ptr;
1027
1028 #ifdef HAVE_FUTIMES
1029             if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1030             {
1031                 ULONGLONG sec, nsec;
1032                 struct timeval tv[2];
1033
1034                 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1035                 {
1036
1037                     tv[0].tv_sec = tv[0].tv_usec = 0;
1038                     tv[1].tv_sec = tv[1].tv_usec = 0;
1039                     if (!fstat( fd, &st ))
1040                     {
1041                         tv[0].tv_sec = st.st_atime;
1042                         tv[1].tv_sec = st.st_mtime;
1043                     }
1044                 }
1045                 if (info->LastAccessTime.QuadPart)
1046                 {
1047                     sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1048                     tv[0].tv_sec = sec - SECS_1601_TO_1970;
1049                     tv[0].tv_usec = (UINT)nsec / 10;
1050                 }
1051                 if (info->LastWriteTime.QuadPart)
1052                 {
1053                     sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1054                     tv[1].tv_sec = sec - SECS_1601_TO_1970;
1055                     tv[1].tv_usec = (UINT)nsec / 10;
1056                 }
1057                 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1058             }
1059 #endif  /* HAVE_FUTIMES */
1060             if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1061             {
1062                 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1063                 else
1064                 {
1065                     if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1066                     {
1067                         st.st_mode &= ~0222; /* clear write permission bits */
1068                     }
1069                     else
1070                     {
1071                         /* add write permission only where we already have read permission */
1072                         st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1073                     }
1074                     if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1075                 }
1076             }
1077         }
1078         else io->u.Status = STATUS_INVALID_PARAMETER_3;
1079         break;
1080
1081     case FilePositionInformation:
1082         if (len >= sizeof(FILE_POSITION_INFORMATION))
1083         {
1084             const FILE_POSITION_INFORMATION *info = ptr;
1085
1086             if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1087                 io->u.Status = FILE_GetNtStatus();
1088         }
1089         else io->u.Status = STATUS_INVALID_PARAMETER_3;
1090         break;
1091
1092     case FileEndOfFileInformation:
1093         if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1094         {
1095             struct stat st;
1096             const FILE_END_OF_FILE_INFORMATION *info = ptr;
1097
1098             /* first try normal truncate */
1099             if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1100
1101             /* now check for the need to extend the file */
1102             if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1103             {
1104                 static const char zero;
1105
1106                 /* extend the file one byte beyond the requested size and then truncate it */
1107                 /* this should work around ftruncate implementations that can't extend files */
1108                 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1109                     ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1110             }
1111             io->u.Status = FILE_GetNtStatus();
1112         }
1113         else io->u.Status = STATUS_INVALID_PARAMETER_3;
1114         break;
1115
1116     default:
1117         FIXME("Unsupported class (%d)\n", class);
1118         io->u.Status = STATUS_NOT_IMPLEMENTED;
1119         break;
1120     }
1121     wine_server_release_fd( handle, fd );
1122     io->Information = 0;
1123     return io->u.Status;
1124 }
1125
1126
1127 /******************************************************************************
1128  *              NtQueryFullAttributesFile   (NTDLL.@)
1129  */
1130 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1131                                            FILE_NETWORK_OPEN_INFORMATION *info )
1132 {
1133     ANSI_STRING unix_name;
1134     NTSTATUS status;
1135
1136     if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1137                                               !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1138     {
1139         struct stat st;
1140
1141         if (stat( unix_name.Buffer, &st ) == -1)
1142             status = FILE_GetNtStatus();
1143         else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1144             status = STATUS_INVALID_INFO_CLASS;
1145         else
1146         {
1147             if (S_ISDIR(st.st_mode))
1148             {
1149                 info->FileAttributes          = FILE_ATTRIBUTE_DIRECTORY;
1150                 info->AllocationSize.QuadPart = 0;
1151                 info->EndOfFile.QuadPart      = 0;
1152             }
1153             else
1154             {
1155                 info->FileAttributes          = FILE_ATTRIBUTE_ARCHIVE;
1156                 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1157                 info->EndOfFile.QuadPart      = st.st_size;
1158             }
1159             if (!(st.st_mode & S_IWUSR)) info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1160             RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1161             RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1162             RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1163             RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1164             if (DIR_is_hidden_file( attr->ObjectName ))
1165                 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1166         }
1167         RtlFreeAnsiString( &unix_name );
1168     }
1169     else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), status );
1170     return status;
1171 }
1172
1173
1174 /******************************************************************************
1175  *              NtQueryAttributesFile   (NTDLL.@)
1176  *              ZwQueryAttributesFile   (NTDLL.@)
1177  */
1178 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1179 {
1180     FILE_NETWORK_OPEN_INFORMATION full_info;
1181     NTSTATUS status;
1182
1183     if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1184     {
1185         info->CreationTime.QuadPart   = full_info.CreationTime.QuadPart;
1186         info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1187         info->LastWriteTime.QuadPart  = full_info.LastWriteTime.QuadPart;
1188         info->ChangeTime.QuadPart     = full_info.ChangeTime.QuadPart;
1189         info->FileAttributes          = full_info.FileAttributes;
1190     }
1191     return status;
1192 }
1193
1194
1195 /******************************************************************************
1196  *  NtQueryVolumeInformationFile                [NTDLL.@]
1197  *  ZwQueryVolumeInformationFile                [NTDLL.@]
1198  *
1199  * Get volume information for an open file handle.
1200  *
1201  * PARAMS
1202  *  handle      [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1203  *  io          [O] Receives information about the operation on return
1204  *  buffer      [O] Destination for volume information
1205  *  length      [I] Size of FsInformation
1206  *  info_class  [I] Type of volume information to set
1207  *
1208  * RETURNS
1209  *  Success: 0. io and buffer are updated.
1210  *  Failure: An NTSTATUS error code describing the error.
1211  */
1212 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1213                                               PVOID buffer, ULONG length,
1214                                               FS_INFORMATION_CLASS info_class )
1215 {
1216     int fd;
1217     struct stat st;
1218
1219     if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL )) != STATUS_SUCCESS)
1220         return io->u.Status;
1221
1222     io->u.Status = STATUS_NOT_IMPLEMENTED;
1223     io->Information = 0;
1224
1225     switch( info_class )
1226     {
1227     case FileFsVolumeInformation:
1228         FIXME( "%p: volume info not supported\n", handle );
1229         break;
1230     case FileFsLabelInformation:
1231         FIXME( "%p: label info not supported\n", handle );
1232         break;
1233     case FileFsSizeInformation:
1234         if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1235             io->u.Status = STATUS_BUFFER_TOO_SMALL;
1236         else
1237         {
1238             FILE_FS_SIZE_INFORMATION *info = buffer;
1239             struct statvfs stvfs;
1240
1241             if (fstat( fd, &st ) < 0)
1242             {
1243                 io->u.Status = FILE_GetNtStatus();
1244                 break;
1245             }
1246             if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1247             {
1248                 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1249                 break;
1250             }
1251             if (fstatvfs( fd, &stvfs ) < 0) io->u.Status = FILE_GetNtStatus();
1252             else
1253             {
1254                 info->TotalAllocationUnits.QuadPart = stvfs.f_blocks;
1255                 info->AvailableAllocationUnits.QuadPart = stvfs.f_bavail;
1256                 info->SectorsPerAllocationUnit = 1;
1257                 info->BytesPerSector = stvfs.f_frsize;
1258                 io->Information = sizeof(*info);
1259                 io->u.Status = STATUS_SUCCESS;
1260             }
1261         }
1262         break;
1263     case FileFsDeviceInformation:
1264         if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1265             io->u.Status = STATUS_BUFFER_TOO_SMALL;
1266         else
1267         {
1268             FILE_FS_DEVICE_INFORMATION *info = buffer;
1269
1270             info->Characteristics = 0;
1271             if (fstat( fd, &st ) < 0)
1272             {
1273                 io->u.Status = FILE_GetNtStatus();
1274                 break;
1275             }
1276             if (S_ISCHR( st.st_mode ))
1277             {
1278                 info->DeviceType = FILE_DEVICE_UNKNOWN;
1279 #ifdef linux
1280                 switch(major(st.st_rdev))
1281                 {
1282                 case MEM_MAJOR:
1283                     info->DeviceType = FILE_DEVICE_NULL;
1284                     break;
1285                 case TTY_MAJOR:
1286                     info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1287                     break;
1288                 case LP_MAJOR:
1289                     info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1290                     break;
1291                 }
1292 #endif
1293             }
1294             else if (S_ISBLK( st.st_mode ))
1295             {
1296                 info->DeviceType = FILE_DEVICE_DISK;
1297             }
1298             else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1299             {
1300                 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1301             }
1302             else  /* regular file or directory */
1303             {
1304 #if defined(linux) && defined(HAVE_FSTATFS)
1305                 struct statfs stfs;
1306
1307                 /* check for floppy disk */
1308                 if (major(st.st_dev) == FLOPPY_MAJOR)
1309                     info->Characteristics |= FILE_REMOVABLE_MEDIA;
1310
1311                 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1312                 switch (stfs.f_type)
1313                 {
1314                 case 0x9660:      /* iso9660 */
1315                 case 0x15013346:  /* udf */
1316                     info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1317                     info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1318                     break;
1319                 case 0x6969:  /* nfs */
1320                 case 0x517B:  /* smbfs */
1321                 case 0x564c:  /* ncpfs */
1322                     info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1323                     info->Characteristics |= FILE_REMOTE_DEVICE;
1324                     break;
1325                 case 0x01021994:  /* tmpfs */
1326                 case 0x28cd3d45:  /* cramfs */
1327                 case 0x1373:      /* devfs */
1328                 case 0x9fa0:      /* procfs */
1329                     info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1330                     break;
1331                 default:
1332                     info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1333                     break;
1334                 }
1335 #else
1336                 static int warned;
1337                 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1338                 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1339 #endif
1340                 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1341             }
1342             io->Information = sizeof(*info);
1343             io->u.Status = STATUS_SUCCESS;
1344         }
1345         break;
1346     case FileFsAttributeInformation:
1347         FIXME( "%p: attribute info not supported\n", handle );
1348         break;
1349     case FileFsControlInformation:
1350         FIXME( "%p: control info not supported\n", handle );
1351         break;
1352     case FileFsFullSizeInformation:
1353         FIXME( "%p: full size info not supported\n", handle );
1354         break;
1355     case FileFsObjectIdInformation:
1356         FIXME( "%p: object id info not supported\n", handle );
1357         break;
1358     case FileFsMaximumInformation:
1359         FIXME( "%p: maximum info not supported\n", handle );
1360         break;
1361     default:
1362         io->u.Status = STATUS_INVALID_PARAMETER;
1363         break;
1364     }
1365     wine_server_release_fd( handle, fd );
1366     return io->u.Status;
1367 }
1368
1369
1370 /******************************************************************
1371  *              NtFlushBuffersFile  (NTDLL.@)
1372  *
1373  * Flush any buffered data on an open file handle.
1374  *
1375  * PARAMS
1376  *  FileHandle         [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1377  *  IoStatusBlock      [O] Receives information about the operation on return
1378  *
1379  * RETURNS
1380  *  Success: 0. IoStatusBlock is updated.
1381  *  Failure: An NTSTATUS error code describing the error.
1382  */
1383 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1384 {
1385     NTSTATUS ret;
1386     HANDLE hEvent = NULL;
1387
1388     SERVER_START_REQ( flush_file )
1389     {
1390         req->handle = hFile;
1391         ret = wine_server_call( req );
1392         hEvent = reply->event;
1393     }
1394     SERVER_END_REQ;
1395     if (!ret && hEvent)
1396     {
1397         ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1398         NtClose( hEvent );
1399     }
1400     return ret;
1401 }
1402
1403 /******************************************************************
1404  *              NtLockFile       (NTDLL.@)
1405  *
1406  *
1407  */
1408 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1409                             PIO_APC_ROUTINE apc, void* apc_user,
1410                             PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1411                             PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1412                             BOOLEAN exclusive )
1413 {
1414     NTSTATUS    ret;
1415     HANDLE      handle;
1416     BOOLEAN     async;
1417
1418     if (apc || io_status || key)
1419     {
1420         FIXME("Unimplemented yet parameter\n");
1421         return STATUS_NOT_IMPLEMENTED;
1422     }
1423
1424     for (;;)
1425     {
1426         SERVER_START_REQ( lock_file )
1427         {
1428             req->handle      = hFile;
1429             req->offset_low  = offset->u.LowPart;
1430             req->offset_high = offset->u.HighPart;
1431             req->count_low   = count->u.LowPart;
1432             req->count_high  = count->u.HighPart;
1433             req->shared      = !exclusive;
1434             req->wait        = !dont_wait;
1435             ret = wine_server_call( req );
1436             handle = reply->handle;
1437             async  = reply->overlapped;
1438         }
1439         SERVER_END_REQ;
1440         if (ret != STATUS_PENDING)
1441         {
1442             if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1443             return ret;
1444         }
1445
1446         if (async)
1447         {
1448             FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1449             if (handle) NtClose( handle );
1450             return STATUS_PENDING;
1451         }
1452         if (handle)
1453         {
1454             NtWaitForSingleObject( handle, FALSE, NULL );
1455             NtClose( handle );
1456         }
1457         else
1458         {
1459             LARGE_INTEGER time;
1460     
1461             /* Unix lock conflict, sleep a bit and retry */
1462             time.QuadPart = 100 * (ULONGLONG)10000;
1463             time.QuadPart = -time.QuadPart;
1464             NtDelayExecution( FALSE, &time );
1465         }
1466     }
1467 }
1468
1469
1470 /******************************************************************
1471  *              NtUnlockFile    (NTDLL.@)
1472  *
1473  *
1474  */
1475 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
1476                               PLARGE_INTEGER offset, PLARGE_INTEGER count,
1477                               PULONG key )
1478 {
1479     NTSTATUS status;
1480
1481     TRACE( "%p %lx%08lx %lx%08lx\n",
1482            hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
1483
1484     if (io_status || key)
1485     {
1486         FIXME("Unimplemented yet parameter\n");
1487         return STATUS_NOT_IMPLEMENTED;
1488     }
1489
1490     SERVER_START_REQ( unlock_file )
1491     {
1492         req->handle      = hFile;
1493         req->offset_low  = offset->u.LowPart;
1494         req->offset_high = offset->u.HighPart;
1495         req->count_low   = count->u.LowPart;
1496         req->count_high  = count->u.HighPart;
1497         status = wine_server_call( req );
1498     }
1499     SERVER_END_REQ;
1500     return status;
1501 }