ntdll/tests: Dump entire VM_COUNTERS structure.
[wine.git] / dlls / ntdll / file.c
blobfd5a690b7dde91d8c7533fbc44626ba8c4ad776d
1 /*
2 * Copyright 1999, 2000 Juergen Schmied
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
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_LINUX_MAJOR_H
31 # include <linux/major.h>
32 #endif
33 #ifdef HAVE_SYS_STATVFS_H
34 # include <sys/statvfs.h>
35 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYS_SYSCALL_H
40 # include <sys/syscall.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
50 #endif
51 #ifdef HAVE_POLL_H
52 #include <poll.h>
53 #endif
54 #ifdef HAVE_SYS_POLL_H
55 #include <sys/poll.h>
56 #endif
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef MAJOR_IN_MKDEV
61 # include <sys/mkdev.h>
62 #elif defined(MAJOR_IN_SYSMACROS)
63 # include <sys/sysmacros.h>
64 #endif
65 #ifdef HAVE_UTIME_H
66 # include <utime.h>
67 #endif
68 #ifdef HAVE_SYS_VFS_H
69 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
70 #define list SYSLIST
71 #define list_next SYSLIST_NEXT
72 #define list_prev SYSLIST_PREV
73 #define list_head SYSLIST_HEAD
74 #define list_tail SYSLIST_TAIL
75 #define list_move_tail SYSLIST_MOVE_TAIL
76 #define list_remove SYSLIST_REMOVE
77 # include <sys/vfs.h>
78 #undef list
79 #undef list_next
80 #undef list_prev
81 #undef list_head
82 #undef list_tail
83 #undef list_move_tail
84 #undef list_remove
85 #endif
86 #ifdef HAVE_SYS_MOUNT_H
87 # include <sys/mount.h>
88 #endif
89 #ifdef HAVE_SYS_STATFS_H
90 # include <sys/statfs.h>
91 #endif
92 #ifdef HAVE_TERMIOS_H
93 #include <termios.h>
94 #endif
95 #ifdef HAVE_VALGRIND_MEMCHECK_H
96 # include <valgrind/memcheck.h>
97 #endif
99 #include "ntstatus.h"
100 #define WIN32_NO_STATUS
101 #define NONAMELESSUNION
102 #include "wine/unicode.h"
103 #include "wine/debug.h"
104 #include "wine/server.h"
105 #include "ntdll_misc.h"
107 #include "winternl.h"
108 #include "winioctl.h"
109 #include "ddk/ntddk.h"
110 #include "ddk/ntddser.h"
112 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
113 WINE_DECLARE_DEBUG_CHANNEL(winediag);
115 mode_t FILE_umask = 0;
117 #define SECSPERDAY 86400
118 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
120 #define FILE_WRITE_TO_END_OF_FILE ((LONGLONG)-1)
121 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
123 static const WCHAR ntfsW[] = {'N','T','F','S'};
125 /* fetch the attributes of a file */
126 static inline ULONG get_file_attributes( const struct stat *st )
128 ULONG attr;
130 if (S_ISDIR(st->st_mode))
131 attr = FILE_ATTRIBUTE_DIRECTORY;
132 else
133 attr = FILE_ATTRIBUTE_ARCHIVE;
134 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
135 attr |= FILE_ATTRIBUTE_READONLY;
136 return attr;
139 /* get the stat info and file attributes for a file (by file descriptor) */
140 int fd_get_file_info( int fd, struct stat *st, ULONG *attr )
142 int ret;
144 *attr = 0;
145 ret = fstat( fd, st );
146 if (ret == -1) return ret;
147 *attr |= get_file_attributes( st );
148 return ret;
151 /* get the stat info and file attributes for a file (by name) */
152 int get_file_info( const char *path, struct stat *st, ULONG *attr )
154 int ret;
156 *attr = 0;
157 ret = lstat( path, st );
158 if (ret == -1) return ret;
159 if (S_ISLNK( st->st_mode ))
161 ret = stat( path, st );
162 if (ret == -1) return ret;
163 /* is a symbolic link and a directory, consider these "reparse points" */
164 if (S_ISDIR( st->st_mode )) *attr |= FILE_ATTRIBUTE_REPARSE_POINT;
166 *attr |= get_file_attributes( st );
167 return ret;
170 /**************************************************************************
171 * FILE_CreateFile (internal)
172 * Open a file.
174 * Parameter set fully identical with NtCreateFile
176 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
177 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
178 ULONG attributes, ULONG sharing, ULONG disposition,
179 ULONG options, PVOID ea_buffer, ULONG ea_length )
181 ANSI_STRING unix_name;
182 BOOL created = FALSE;
184 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
185 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
186 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
187 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
188 attributes, sharing, disposition, options, ea_buffer, ea_length );
190 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
192 if (alloc_size) FIXME( "alloc_size not supported\n" );
194 if (options & FILE_OPEN_BY_FILE_ID)
195 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
196 else
197 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
199 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
201 SERVER_START_REQ( open_file_object )
203 req->access = access;
204 req->attributes = attr->Attributes;
205 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
206 req->sharing = sharing;
207 req->options = options;
208 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
209 io->u.Status = wine_server_call( req );
210 *handle = wine_server_ptr_handle( reply->handle );
212 SERVER_END_REQ;
213 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
214 return io->u.Status;
217 if (io->u.Status == STATUS_NO_SUCH_FILE &&
218 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
220 created = TRUE;
221 io->u.Status = STATUS_SUCCESS;
224 if (io->u.Status == STATUS_SUCCESS)
226 static UNICODE_STRING empty_string;
227 OBJECT_ATTRIBUTES unix_attr = *attr;
228 data_size_t len;
229 struct object_attributes *objattr;
231 unix_attr.ObjectName = &empty_string; /* we send the unix name instead */
232 if ((io->u.Status = alloc_object_attributes( &unix_attr, &objattr, &len )))
234 RtlFreeAnsiString( &unix_name );
235 return io->u.Status;
238 SERVER_START_REQ( create_file )
240 req->access = access;
241 req->sharing = sharing;
242 req->create = disposition;
243 req->options = options;
244 req->attrs = attributes;
245 wine_server_add_data( req, objattr, len );
246 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
247 io->u.Status = wine_server_call( req );
248 *handle = wine_server_ptr_handle( reply->handle );
250 SERVER_END_REQ;
251 RtlFreeHeap( GetProcessHeap(), 0, objattr );
252 RtlFreeAnsiString( &unix_name );
254 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
256 if (io->u.Status == STATUS_SUCCESS)
258 if (created) io->Information = FILE_CREATED;
259 else switch(disposition)
261 case FILE_SUPERSEDE:
262 io->Information = FILE_SUPERSEDED;
263 break;
264 case FILE_CREATE:
265 io->Information = FILE_CREATED;
266 break;
267 case FILE_OPEN:
268 case FILE_OPEN_IF:
269 io->Information = FILE_OPENED;
270 break;
271 case FILE_OVERWRITE:
272 case FILE_OVERWRITE_IF:
273 io->Information = FILE_OVERWRITTEN;
274 break;
277 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
279 static int once;
280 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
283 return io->u.Status;
286 /**************************************************************************
287 * NtOpenFile [NTDLL.@]
288 * ZwOpenFile [NTDLL.@]
290 * Open a file.
292 * PARAMS
293 * handle [O] Variable that receives the file handle on return
294 * access [I] Access desired by the caller to the file
295 * attr [I] Structure describing the file to be opened
296 * io [O] Receives details about the result of the operation
297 * sharing [I] Type of shared access the caller requires
298 * options [I] Options for the file open
300 * RETURNS
301 * Success: 0. FileHandle and IoStatusBlock are updated.
302 * Failure: An NTSTATUS error code describing the error.
304 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
305 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
306 ULONG sharing, ULONG options )
308 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
309 sharing, FILE_OPEN, options, NULL, 0 );
312 /**************************************************************************
313 * NtCreateFile [NTDLL.@]
314 * ZwCreateFile [NTDLL.@]
316 * Either create a new file or directory, or open an existing file, device,
317 * directory or volume.
319 * PARAMS
320 * handle [O] Points to a variable which receives the file handle on return
321 * access [I] Desired access to the file
322 * attr [I] Structure describing the file
323 * io [O] Receives information about the operation on return
324 * alloc_size [I] Initial size of the file in bytes
325 * attributes [I] Attributes to create the file with
326 * sharing [I] Type of shared access the caller would like to the file
327 * disposition [I] Specifies what to do, depending on whether the file already exists
328 * options [I] Options for creating a new file
329 * ea_buffer [I] Pointer to an extended attributes buffer
330 * ea_length [I] Length of ea_buffer
332 * RETURNS
333 * Success: 0. handle and io are updated.
334 * Failure: An NTSTATUS error code describing the error.
336 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
337 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
338 ULONG attributes, ULONG sharing, ULONG disposition,
339 ULONG options, PVOID ea_buffer, ULONG ea_length )
341 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
342 sharing, disposition, options, ea_buffer, ea_length );
345 /***********************************************************************
346 * Asynchronous file I/O *
349 typedef NTSTATUS async_callback_t( void *user, IO_STATUS_BLOCK *io, NTSTATUS status );
351 struct async_fileio
353 async_callback_t *callback; /* must be the first field */
354 struct async_fileio *next;
355 HANDLE handle;
358 struct async_fileio_read
360 struct async_fileio io;
361 char* buffer;
362 unsigned int already;
363 unsigned int count;
364 BOOL avail_mode;
367 struct async_fileio_write
369 struct async_fileio io;
370 const char *buffer;
371 unsigned int already;
372 unsigned int count;
375 struct async_irp
377 struct async_fileio io;
378 HANDLE event; /* async event */
379 void *buffer; /* buffer for output */
380 ULONG size; /* size of buffer */
383 static struct async_fileio *fileio_freelist;
385 static void release_fileio( struct async_fileio *io )
387 for (;;)
389 struct async_fileio *next = fileio_freelist;
390 io->next = next;
391 if (interlocked_cmpxchg_ptr( (void **)&fileio_freelist, io, next ) == next) return;
395 static struct async_fileio *alloc_fileio( DWORD size, async_callback_t callback, HANDLE handle )
397 /* first free remaining previous fileinfos */
399 struct async_fileio *io = interlocked_xchg_ptr( (void **)&fileio_freelist, NULL );
401 while (io)
403 struct async_fileio *next = io->next;
404 RtlFreeHeap( GetProcessHeap(), 0, io );
405 io = next;
408 if ((io = RtlAllocateHeap( GetProcessHeap(), 0, size )))
410 io->callback = callback;
411 io->handle = handle;
413 return io;
416 static async_data_t server_async( HANDLE handle, struct async_fileio *user, HANDLE event,
417 PIO_APC_ROUTINE apc, void *apc_context, IO_STATUS_BLOCK *io )
419 async_data_t async;
420 async.handle = wine_server_obj_handle( handle );
421 async.user = wine_server_client_ptr( user );
422 async.iosb = wine_server_client_ptr( io );
423 async.event = wine_server_obj_handle( event );
424 async.apc = wine_server_client_ptr( apc );
425 async.apc_context = wine_server_client_ptr( apc_context );
426 return async;
429 /* callback for irp async I/O completion */
430 static NTSTATUS irp_completion( void *user, IO_STATUS_BLOCK *io, NTSTATUS status )
432 struct async_irp *async = user;
433 ULONG information = 0;
435 if (status == STATUS_ALERTED)
437 SERVER_START_REQ( get_async_result )
439 req->user_arg = wine_server_client_ptr( async );
440 wine_server_set_reply( req, async->buffer, async->size );
441 status = wine_server_call( req );
442 information = reply->size;
444 SERVER_END_REQ;
446 if (status != STATUS_PENDING)
448 io->u.Status = status;
449 io->Information = information;
450 release_fileio( &async->io );
452 return status;
455 /***********************************************************************
456 * FILE_GetNtStatus(void)
458 * Retrieve the Nt Status code from errno.
459 * Try to be consistent with FILE_SetDosError().
461 NTSTATUS FILE_GetNtStatus(void)
463 int err = errno;
465 TRACE( "errno = %d\n", errno );
466 switch (err)
468 case EAGAIN: return STATUS_SHARING_VIOLATION;
469 case EBADF: return STATUS_INVALID_HANDLE;
470 case EBUSY: return STATUS_DEVICE_BUSY;
471 case ENOSPC: return STATUS_DISK_FULL;
472 case EPERM:
473 case EROFS:
474 case EACCES: return STATUS_ACCESS_DENIED;
475 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
476 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
477 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
478 case EMFILE:
479 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
480 case EINVAL: return STATUS_INVALID_PARAMETER;
481 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
482 case EPIPE: return STATUS_PIPE_DISCONNECTED;
483 case EIO: return STATUS_DEVICE_NOT_READY;
484 #ifdef ENOMEDIUM
485 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
486 #endif
487 case ENXIO: return STATUS_NO_SUCH_DEVICE;
488 case ENOTTY:
489 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
490 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
491 case EFAULT: return STATUS_ACCESS_VIOLATION;
492 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
493 #ifdef ETIME /* Missing on FreeBSD */
494 case ETIME: return STATUS_IO_TIMEOUT;
495 #endif
496 case ENOEXEC: /* ?? */
497 case EEXIST: /* ?? */
498 default:
499 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
500 return STATUS_UNSUCCESSFUL;
504 /***********************************************************************
505 * FILE_AsyncReadService (INTERNAL)
507 static NTSTATUS FILE_AsyncReadService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
509 struct async_fileio_read *fileio = user;
510 int fd, needs_close, result;
512 switch (status)
514 case STATUS_ALERTED: /* got some new data */
515 /* check to see if the data is ready (non-blocking) */
516 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
517 &needs_close, NULL, NULL )))
518 break;
520 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
521 if (needs_close) close( fd );
523 if (result < 0)
525 if (errno == EAGAIN || errno == EINTR)
526 status = STATUS_PENDING;
527 else /* check to see if the transfer is complete */
528 status = FILE_GetNtStatus();
530 else if (result == 0)
532 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
534 else
536 fileio->already += result;
537 if (fileio->already >= fileio->count || fileio->avail_mode)
538 status = STATUS_SUCCESS;
539 else
540 status = STATUS_PENDING;
542 break;
544 case STATUS_TIMEOUT:
545 case STATUS_IO_TIMEOUT:
546 if (fileio->already) status = STATUS_SUCCESS;
547 break;
549 if (status != STATUS_PENDING)
551 iosb->u.Status = status;
552 iosb->Information = fileio->already;
553 release_fileio( &fileio->io );
555 return status;
558 /* do a read call through the server */
559 static NTSTATUS server_read_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
560 IO_STATUS_BLOCK *io, void *buffer, ULONG size,
561 LARGE_INTEGER *offset, ULONG *key )
563 struct async_irp *async;
564 NTSTATUS status;
565 HANDLE wait_handle;
566 ULONG options;
568 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
569 return STATUS_NO_MEMORY;
571 async->event = event;
572 async->buffer = buffer;
573 async->size = size;
575 SERVER_START_REQ( read )
577 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
578 req->pos = offset ? offset->QuadPart : 0;
579 wine_server_set_reply( req, buffer, size );
580 status = wine_server_call( req );
581 wait_handle = wine_server_ptr_handle( reply->wait );
582 options = reply->options;
584 SERVER_END_REQ;
586 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
588 if (wait_handle)
590 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
591 status = io->u.Status;
592 NtClose( wait_handle );
595 return status;
598 /* do a write call through the server */
599 static NTSTATUS server_write_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
600 IO_STATUS_BLOCK *io, const void *buffer, ULONG size,
601 LARGE_INTEGER *offset, ULONG *key )
603 struct async_irp *async;
604 NTSTATUS status;
605 HANDLE wait_handle;
606 ULONG options;
608 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
609 return STATUS_NO_MEMORY;
611 async->event = event;
612 async->buffer = NULL;
613 async->size = 0;
615 SERVER_START_REQ( write )
617 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
618 req->pos = offset ? offset->QuadPart : 0;
619 wine_server_add_data( req, buffer, size );
620 status = wine_server_call( req );
621 wait_handle = wine_server_ptr_handle( reply->wait );
622 options = reply->options;
624 SERVER_END_REQ;
626 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
628 if (wait_handle)
630 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
631 status = io->u.Status;
632 NtClose( wait_handle );
635 return status;
638 struct io_timeouts
640 int interval; /* max interval between two bytes */
641 int total; /* total timeout for the whole operation */
642 int end_time; /* absolute time of end of operation */
645 /* retrieve the I/O timeouts to use for a given handle */
646 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
647 struct io_timeouts *timeouts )
649 NTSTATUS status = STATUS_SUCCESS;
651 timeouts->interval = timeouts->total = -1;
653 switch(type)
655 case FD_TYPE_SERIAL:
657 /* GetCommTimeouts */
658 SERIAL_TIMEOUTS st;
659 IO_STATUS_BLOCK io;
661 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
662 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
663 if (status) break;
665 if (is_read)
667 if (st.ReadIntervalTimeout)
668 timeouts->interval = st.ReadIntervalTimeout;
670 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
672 timeouts->total = st.ReadTotalTimeoutConstant;
673 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
674 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
676 else if (st.ReadIntervalTimeout == MAXDWORD)
677 timeouts->interval = timeouts->total = 0;
679 else /* write */
681 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
683 timeouts->total = st.WriteTotalTimeoutConstant;
684 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
685 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
689 break;
690 case FD_TYPE_MAILSLOT:
691 if (is_read)
693 timeouts->interval = 0; /* return as soon as we got something */
694 SERVER_START_REQ( set_mailslot_info )
696 req->handle = wine_server_obj_handle( handle );
697 req->flags = 0;
698 if (!(status = wine_server_call( req )) &&
699 reply->read_timeout != TIMEOUT_INFINITE)
700 timeouts->total = reply->read_timeout / -10000;
702 SERVER_END_REQ;
704 break;
705 case FD_TYPE_SOCKET:
706 case FD_TYPE_PIPE:
707 case FD_TYPE_CHAR:
708 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
709 break;
710 default:
711 break;
713 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
714 return STATUS_SUCCESS;
718 /* retrieve the timeout for the next wait, in milliseconds */
719 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
721 int ret = -1;
723 if (timeouts->total != -1)
725 ret = timeouts->end_time - NtGetTickCount();
726 if (ret < 0) ret = 0;
728 if (already && timeouts->interval != -1)
730 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
732 return ret;
736 /* retrieve the avail_mode flag for async reads */
737 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
739 NTSTATUS status = STATUS_SUCCESS;
741 switch(type)
743 case FD_TYPE_SERIAL:
745 /* GetCommTimeouts */
746 SERIAL_TIMEOUTS st;
747 IO_STATUS_BLOCK io;
749 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
750 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
751 if (status) break;
752 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
753 !st.ReadTotalTimeoutConstant &&
754 st.ReadIntervalTimeout == MAXDWORD);
756 break;
757 case FD_TYPE_MAILSLOT:
758 case FD_TYPE_SOCKET:
759 case FD_TYPE_PIPE:
760 case FD_TYPE_CHAR:
761 *avail_mode = TRUE;
762 break;
763 default:
764 *avail_mode = FALSE;
765 break;
767 return status;
770 /* register an async I/O for a file read; helper for NtReadFile */
771 static NTSTATUS register_async_file_read( HANDLE handle, HANDLE event,
772 PIO_APC_ROUTINE apc, void *apc_user,
773 IO_STATUS_BLOCK *iosb, void *buffer,
774 ULONG already, ULONG length, BOOL avail_mode )
776 struct async_fileio_read *fileio;
777 NTSTATUS status;
779 if (!(fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), FILE_AsyncReadService, handle )))
780 return STATUS_NO_MEMORY;
782 fileio->already = already;
783 fileio->count = length;
784 fileio->buffer = buffer;
785 fileio->avail_mode = avail_mode;
787 SERVER_START_REQ( register_async )
789 req->type = ASYNC_TYPE_READ;
790 req->count = length;
791 req->async = server_async( handle, &fileio->io, event, apc, apc_user, iosb );
792 status = wine_server_call( req );
794 SERVER_END_REQ;
796 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
797 return status;
801 /******************************************************************************
802 * NtReadFile [NTDLL.@]
803 * ZwReadFile [NTDLL.@]
805 * Read from an open file handle.
807 * PARAMS
808 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
809 * Event [I] Event to signal upon completion (or NULL)
810 * ApcRoutine [I] Callback to call upon completion (or NULL)
811 * ApcContext [I] Context for ApcRoutine (or NULL)
812 * IoStatusBlock [O] Receives information about the operation on return
813 * Buffer [O] Destination for the data read
814 * Length [I] Size of Buffer
815 * ByteOffset [O] Destination for the new file pointer position (or NULL)
816 * Key [O] Function unknown (may be NULL)
818 * RETURNS
819 * Success: 0. IoStatusBlock is updated, and the Information member contains
820 * The number of bytes read.
821 * Failure: An NTSTATUS error code describing the error.
823 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
824 PIO_APC_ROUTINE apc, void* apc_user,
825 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
826 PLARGE_INTEGER offset, PULONG key)
828 int result, unix_handle, needs_close;
829 unsigned int options;
830 struct io_timeouts timeouts;
831 NTSTATUS status;
832 ULONG total = 0;
833 enum server_fd_type type;
834 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
835 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
837 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
838 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
840 if (!io_status) return STATUS_ACCESS_VIOLATION;
842 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
843 &needs_close, &type, &options );
844 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
846 if (!virtual_check_buffer_for_write( buffer, length )) return STATUS_ACCESS_VIOLATION;
848 if (status == STATUS_BAD_DEVICE_TYPE)
849 return server_read_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
851 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
853 if (type == FD_TYPE_FILE)
855 if (async_read && (!offset || offset->QuadPart < 0))
857 status = STATUS_INVALID_PARAMETER;
858 goto done;
861 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
863 /* async I/O doesn't make sense on regular files */
864 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
866 if (errno != EINTR)
868 status = FILE_GetNtStatus();
869 goto done;
872 if (!async_read)
873 /* update file pointer position */
874 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
876 total = result;
877 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
878 goto done;
881 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
883 if (async_read && (!offset || offset->QuadPart < 0))
885 status = STATUS_INVALID_PARAMETER;
886 goto done;
890 if (type == FD_TYPE_SERIAL && async_read && length)
892 /* an asynchronous serial port read with a read interval timeout needs to
893 skip the synchronous read to make sure that the server starts the read
894 interval timer after the first read */
895 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts ))) goto err;
896 if (timeouts.interval)
898 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
899 buffer, total, length, FALSE );
900 goto err;
904 for (;;)
906 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
908 total += result;
909 if (!result || total == length)
911 if (total)
913 status = STATUS_SUCCESS;
914 goto done;
916 switch (type)
918 case FD_TYPE_FILE:
919 case FD_TYPE_CHAR:
920 case FD_TYPE_DEVICE:
921 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
922 goto done;
923 case FD_TYPE_SERIAL:
924 if (!length)
926 status = STATUS_SUCCESS;
927 goto done;
929 break;
930 default:
931 status = STATUS_PIPE_BROKEN;
932 goto done;
935 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
937 else if (errno != EAGAIN)
939 if (errno == EINTR) continue;
940 if (!total) status = FILE_GetNtStatus();
941 goto done;
944 if (async_read)
946 BOOL avail_mode;
948 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
949 goto err;
950 if (total && avail_mode)
952 status = STATUS_SUCCESS;
953 goto done;
955 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
956 buffer, total, length, avail_mode );
957 goto err;
959 else /* synchronous read, wait for the fd to become ready */
961 struct pollfd pfd;
962 int ret, timeout;
964 if (!timeout_init_done)
966 timeout_init_done = TRUE;
967 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
968 goto err;
969 if (hEvent) NtResetEvent( hEvent, NULL );
971 timeout = get_next_io_timeout( &timeouts, total );
973 pfd.fd = unix_handle;
974 pfd.events = POLLIN;
976 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
978 if (total) /* return with what we got so far */
979 status = STATUS_SUCCESS;
980 else
981 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
982 goto done;
984 if (ret == -1 && errno != EINTR)
986 status = FILE_GetNtStatus();
987 goto done;
989 /* will now restart the read */
993 done:
994 send_completion = cvalue != 0;
996 err:
997 if (needs_close) close( unix_handle );
998 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
1000 io_status->u.Status = status;
1001 io_status->Information = total;
1002 TRACE("= SUCCESS (%u)\n", total);
1003 if (hEvent) NtSetEvent( hEvent, NULL );
1004 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1005 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1007 else
1009 TRACE("= 0x%08x\n", status);
1010 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1013 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1015 return status;
1019 /******************************************************************************
1020 * NtReadFileScatter [NTDLL.@]
1021 * ZwReadFileScatter [NTDLL.@]
1023 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1024 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1025 ULONG length, PLARGE_INTEGER offset, PULONG key )
1027 int result, unix_handle, needs_close;
1028 unsigned int options;
1029 NTSTATUS status;
1030 ULONG pos = 0, total = 0;
1031 enum server_fd_type type;
1032 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1033 BOOL send_completion = FALSE;
1035 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1036 file, event, apc, apc_user, io_status, segments, length, offset, key);
1038 if (length % page_size) return STATUS_INVALID_PARAMETER;
1039 if (!io_status) return STATUS_ACCESS_VIOLATION;
1041 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1042 &needs_close, &type, &options );
1043 if (status) return status;
1045 if ((type != FD_TYPE_FILE) ||
1046 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1047 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1049 status = STATUS_INVALID_PARAMETER;
1050 goto error;
1053 while (length)
1055 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1056 result = pread( unix_handle, (char *)segments->Buffer + pos,
1057 page_size - pos, offset->QuadPart + total );
1058 else
1059 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1061 if (result == -1)
1063 if (errno == EINTR) continue;
1064 status = FILE_GetNtStatus();
1065 break;
1067 if (!result)
1069 status = STATUS_END_OF_FILE;
1070 break;
1072 total += result;
1073 length -= result;
1074 if ((pos += result) == page_size)
1076 pos = 0;
1077 segments++;
1081 send_completion = cvalue != 0;
1083 error:
1084 if (needs_close) close( unix_handle );
1085 if (status == STATUS_SUCCESS)
1087 io_status->u.Status = status;
1088 io_status->Information = total;
1089 TRACE("= SUCCESS (%u)\n", total);
1090 if (event) NtSetEvent( event, NULL );
1091 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1092 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1094 else
1096 TRACE("= 0x%08x\n", status);
1097 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1100 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1102 return status;
1106 /***********************************************************************
1107 * FILE_AsyncWriteService (INTERNAL)
1109 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1111 struct async_fileio_write *fileio = user;
1112 int result, fd, needs_close;
1113 enum server_fd_type type;
1115 switch (status)
1117 case STATUS_ALERTED:
1118 /* write some data (non-blocking) */
1119 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1120 &needs_close, &type, NULL )))
1121 break;
1123 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1124 result = send( fd, fileio->buffer, 0, 0 );
1125 else
1126 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1128 if (needs_close) close( fd );
1130 if (result < 0)
1132 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1133 else status = FILE_GetNtStatus();
1135 else
1137 fileio->already += result;
1138 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1140 break;
1142 case STATUS_TIMEOUT:
1143 case STATUS_IO_TIMEOUT:
1144 if (fileio->already) status = STATUS_SUCCESS;
1145 break;
1147 if (status != STATUS_PENDING)
1149 iosb->u.Status = status;
1150 iosb->Information = fileio->already;
1151 release_fileio( &fileio->io );
1153 return status;
1156 static NTSTATUS set_pending_write( HANDLE device )
1158 NTSTATUS status;
1160 SERVER_START_REQ( set_serial_info )
1162 req->handle = wine_server_obj_handle( device );
1163 req->flags = SERIALINFO_PENDING_WRITE;
1164 status = wine_server_call( req );
1166 SERVER_END_REQ;
1167 return status;
1170 /******************************************************************************
1171 * NtWriteFile [NTDLL.@]
1172 * ZwWriteFile [NTDLL.@]
1174 * Write to an open file handle.
1176 * PARAMS
1177 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1178 * Event [I] Event to signal upon completion (or NULL)
1179 * ApcRoutine [I] Callback to call upon completion (or NULL)
1180 * ApcContext [I] Context for ApcRoutine (or NULL)
1181 * IoStatusBlock [O] Receives information about the operation on return
1182 * Buffer [I] Source for the data to write
1183 * Length [I] Size of Buffer
1184 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1185 * Key [O] Function unknown (may be NULL)
1187 * RETURNS
1188 * Success: 0. IoStatusBlock is updated, and the Information member contains
1189 * The number of bytes written.
1190 * Failure: An NTSTATUS error code describing the error.
1192 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1193 PIO_APC_ROUTINE apc, void* apc_user,
1194 PIO_STATUS_BLOCK io_status,
1195 const void* buffer, ULONG length,
1196 PLARGE_INTEGER offset, PULONG key)
1198 int result, unix_handle, needs_close;
1199 unsigned int options;
1200 struct io_timeouts timeouts;
1201 NTSTATUS status;
1202 ULONG total = 0;
1203 enum server_fd_type type;
1204 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1205 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1206 LARGE_INTEGER offset_eof;
1208 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1209 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1211 if (!io_status) return STATUS_ACCESS_VIOLATION;
1213 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1214 &needs_close, &type, &options );
1215 if (status == STATUS_ACCESS_DENIED)
1217 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1218 &needs_close, &type, &options );
1219 append_write = TRUE;
1221 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
1223 if (!virtual_check_buffer_for_read( buffer, length ))
1225 status = STATUS_INVALID_USER_BUFFER;
1226 goto done;
1229 if (status == STATUS_BAD_DEVICE_TYPE)
1230 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1232 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1234 if (type == FD_TYPE_FILE)
1236 if (async_write &&
1237 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1239 status = STATUS_INVALID_PARAMETER;
1240 goto done;
1243 if (append_write)
1245 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1246 offset = &offset_eof;
1249 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1251 off_t off = offset->QuadPart;
1253 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1255 struct stat st;
1257 if (fstat( unix_handle, &st ) == -1)
1259 status = FILE_GetNtStatus();
1260 goto done;
1262 off = st.st_size;
1264 else if (offset->QuadPart < 0)
1266 status = STATUS_INVALID_PARAMETER;
1267 goto done;
1270 /* async I/O doesn't make sense on regular files */
1271 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1273 if (errno != EINTR)
1275 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1276 else status = FILE_GetNtStatus();
1277 goto done;
1281 if (!async_write)
1282 /* update file pointer position */
1283 lseek( unix_handle, off + result, SEEK_SET );
1285 total = result;
1286 status = STATUS_SUCCESS;
1287 goto done;
1290 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1292 if (async_write &&
1293 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1295 status = STATUS_INVALID_PARAMETER;
1296 goto done;
1300 for (;;)
1302 /* zero-length writes on sockets may not work with plain write(2) */
1303 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1304 result = send( unix_handle, buffer, 0, 0 );
1305 else
1306 result = write( unix_handle, (const char *)buffer + total, length - total );
1308 if (result >= 0)
1310 total += result;
1311 if (total == length)
1313 status = STATUS_SUCCESS;
1314 goto done;
1316 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1318 else if (errno != EAGAIN)
1320 if (errno == EINTR) continue;
1321 if (!total)
1323 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1324 else status = FILE_GetNtStatus();
1326 goto done;
1329 if (async_write)
1331 struct async_fileio_write *fileio;
1333 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), FILE_AsyncWriteService, hFile );
1334 if (!fileio)
1336 status = STATUS_NO_MEMORY;
1337 goto err;
1339 fileio->already = total;
1340 fileio->count = length;
1341 fileio->buffer = buffer;
1343 SERVER_START_REQ( register_async )
1345 req->type = ASYNC_TYPE_WRITE;
1346 req->count = length;
1347 req->async = server_async( hFile, &fileio->io, hEvent, apc, apc_user, io_status );
1348 status = wine_server_call( req );
1350 SERVER_END_REQ;
1352 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1353 goto err;
1355 else /* synchronous write, wait for the fd to become ready */
1357 struct pollfd pfd;
1358 int ret, timeout;
1360 if (!timeout_init_done)
1362 timeout_init_done = TRUE;
1363 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1364 goto err;
1365 if (hEvent) NtResetEvent( hEvent, NULL );
1367 timeout = get_next_io_timeout( &timeouts, total );
1369 pfd.fd = unix_handle;
1370 pfd.events = POLLOUT;
1372 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1374 /* return with what we got so far */
1375 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1376 goto done;
1378 if (ret == -1 && errno != EINTR)
1380 status = FILE_GetNtStatus();
1381 goto done;
1383 /* will now restart the write */
1387 done:
1388 send_completion = cvalue != 0;
1390 err:
1391 if (needs_close) close( unix_handle );
1393 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1394 set_pending_write( hFile );
1396 if (status == STATUS_SUCCESS)
1398 io_status->u.Status = status;
1399 io_status->Information = total;
1400 TRACE("= SUCCESS (%u)\n", total);
1401 if (hEvent) NtSetEvent( hEvent, NULL );
1402 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1403 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1405 else
1407 TRACE("= 0x%08x\n", status);
1408 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1411 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1413 return status;
1417 /******************************************************************************
1418 * NtWriteFileGather [NTDLL.@]
1419 * ZwWriteFileGather [NTDLL.@]
1421 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1422 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1423 ULONG length, PLARGE_INTEGER offset, PULONG key )
1425 int result, unix_handle, needs_close;
1426 unsigned int options;
1427 NTSTATUS status;
1428 ULONG pos = 0, total = 0;
1429 enum server_fd_type type;
1430 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1431 BOOL send_completion = FALSE;
1433 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1434 file, event, apc, apc_user, io_status, segments, length, offset, key);
1436 if (length % page_size) return STATUS_INVALID_PARAMETER;
1437 if (!io_status) return STATUS_ACCESS_VIOLATION;
1439 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1440 &needs_close, &type, &options );
1441 if (status) return status;
1443 if ((type != FD_TYPE_FILE) ||
1444 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1445 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1447 status = STATUS_INVALID_PARAMETER;
1448 goto error;
1451 while (length)
1453 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1454 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1455 page_size - pos, offset->QuadPart + total );
1456 else
1457 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1459 if (result == -1)
1461 if (errno == EINTR) continue;
1462 if (errno == EFAULT)
1464 status = STATUS_INVALID_USER_BUFFER;
1465 goto error;
1467 status = FILE_GetNtStatus();
1468 break;
1470 if (!result)
1472 status = STATUS_DISK_FULL;
1473 break;
1475 total += result;
1476 length -= result;
1477 if ((pos += result) == page_size)
1479 pos = 0;
1480 segments++;
1484 send_completion = cvalue != 0;
1486 error:
1487 if (needs_close) close( unix_handle );
1488 if (status == STATUS_SUCCESS)
1490 io_status->u.Status = status;
1491 io_status->Information = total;
1492 TRACE("= SUCCESS (%u)\n", total);
1493 if (event) NtSetEvent( event, NULL );
1494 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1495 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1497 else
1499 TRACE("= 0x%08x\n", status);
1500 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1503 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1505 return status;
1509 /* do an ioctl call through the server */
1510 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1511 PIO_APC_ROUTINE apc, PVOID apc_context,
1512 IO_STATUS_BLOCK *io, ULONG code,
1513 const void *in_buffer, ULONG in_size,
1514 PVOID out_buffer, ULONG out_size )
1516 struct async_irp *async;
1517 NTSTATUS status;
1518 HANDLE wait_handle;
1519 ULONG options;
1521 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
1522 return STATUS_NO_MEMORY;
1523 async->event = event;
1524 async->buffer = out_buffer;
1525 async->size = out_size;
1527 SERVER_START_REQ( ioctl )
1529 req->code = code;
1530 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
1531 wine_server_add_data( req, in_buffer, in_size );
1532 if ((code & 3) != METHOD_BUFFERED)
1533 wine_server_add_data( req, out_buffer, out_size );
1534 wine_server_set_reply( req, out_buffer, out_size );
1535 status = wine_server_call( req );
1536 wait_handle = wine_server_ptr_handle( reply->wait );
1537 options = reply->options;
1538 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1540 SERVER_END_REQ;
1542 if (status == STATUS_NOT_SUPPORTED)
1543 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1544 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1546 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1548 if (wait_handle)
1550 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1551 status = io->u.Status;
1552 NtClose( wait_handle );
1555 return status;
1558 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1559 * server */
1560 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1561 ULONG in_size)
1563 #ifdef VALGRIND_MAKE_MEM_DEFINED
1564 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1565 do { \
1566 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1567 if ((size) >= FIELD_OFFSET(t, f2)) \
1568 VALGRIND_MAKE_MEM_DEFINED( \
1569 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1570 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1571 } while (0)
1573 switch (code)
1575 case FSCTL_PIPE_WAIT:
1576 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1577 break;
1579 #endif
1583 /**************************************************************************
1584 * NtDeviceIoControlFile [NTDLL.@]
1585 * ZwDeviceIoControlFile [NTDLL.@]
1587 * Perform an I/O control operation on an open file handle.
1589 * PARAMS
1590 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1591 * event [I] Event to signal upon completion (or NULL)
1592 * apc [I] Callback to call upon completion (or NULL)
1593 * apc_context [I] Context for ApcRoutine (or NULL)
1594 * io [O] Receives information about the operation on return
1595 * code [I] Control code for the operation to perform
1596 * in_buffer [I] Source for any input data required (or NULL)
1597 * in_size [I] Size of InputBuffer
1598 * out_buffer [O] Source for any output data returned (or NULL)
1599 * out_size [I] Size of OutputBuffer
1601 * RETURNS
1602 * Success: 0. IoStatusBlock is updated.
1603 * Failure: An NTSTATUS error code describing the error.
1605 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1606 PIO_APC_ROUTINE apc, PVOID apc_context,
1607 PIO_STATUS_BLOCK io, ULONG code,
1608 PVOID in_buffer, ULONG in_size,
1609 PVOID out_buffer, ULONG out_size)
1611 ULONG device = (code >> 16);
1612 NTSTATUS status = STATUS_NOT_SUPPORTED;
1614 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1615 handle, event, apc, apc_context, io, code,
1616 in_buffer, in_size, out_buffer, out_size);
1618 switch(device)
1620 case FILE_DEVICE_DISK:
1621 case FILE_DEVICE_CD_ROM:
1622 case FILE_DEVICE_DVD:
1623 case FILE_DEVICE_CONTROLLER:
1624 case FILE_DEVICE_MASS_STORAGE:
1625 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1626 in_buffer, in_size, out_buffer, out_size);
1627 break;
1628 case FILE_DEVICE_SERIAL_PORT:
1629 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1630 in_buffer, in_size, out_buffer, out_size);
1631 break;
1632 case FILE_DEVICE_TAPE:
1633 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1634 in_buffer, in_size, out_buffer, out_size);
1635 break;
1638 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1639 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1640 in_buffer, in_size, out_buffer, out_size );
1642 if (status != STATUS_PENDING) io->u.Status = status;
1643 return status;
1647 /**************************************************************************
1648 * NtFsControlFile [NTDLL.@]
1649 * ZwFsControlFile [NTDLL.@]
1651 * Perform a file system control operation on an open file handle.
1653 * PARAMS
1654 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1655 * event [I] Event to signal upon completion (or NULL)
1656 * apc [I] Callback to call upon completion (or NULL)
1657 * apc_context [I] Context for ApcRoutine (or NULL)
1658 * io [O] Receives information about the operation on return
1659 * code [I] Control code for the operation to perform
1660 * in_buffer [I] Source for any input data required (or NULL)
1661 * in_size [I] Size of InputBuffer
1662 * out_buffer [O] Source for any output data returned (or NULL)
1663 * out_size [I] Size of OutputBuffer
1665 * RETURNS
1666 * Success: 0. IoStatusBlock is updated.
1667 * Failure: An NTSTATUS error code describing the error.
1669 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1670 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1671 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1673 NTSTATUS status;
1675 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1676 handle, event, apc, apc_context, io, code,
1677 in_buffer, in_size, out_buffer, out_size);
1679 if (!io) return STATUS_INVALID_PARAMETER;
1681 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1683 switch(code)
1685 case FSCTL_DISMOUNT_VOLUME:
1686 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1687 in_buffer, in_size, out_buffer, out_size );
1688 if (!status) status = DIR_unmount_device( handle );
1689 break;
1691 case FSCTL_PIPE_PEEK:
1693 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1694 int avail = 0, fd, needs_close;
1696 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1698 status = STATUS_INFO_LENGTH_MISMATCH;
1699 break;
1702 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1704 if (status == STATUS_BAD_DEVICE_TYPE)
1705 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1706 in_buffer, in_size, out_buffer, out_size );
1707 break;
1710 #ifdef FIONREAD
1711 if (ioctl( fd, FIONREAD, &avail ) != 0)
1713 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1714 if (needs_close) close( fd );
1715 status = FILE_GetNtStatus();
1716 break;
1718 #endif
1719 if (!avail) /* check for closed pipe */
1721 struct pollfd pollfd;
1722 int ret;
1724 pollfd.fd = fd;
1725 pollfd.events = POLLIN;
1726 pollfd.revents = 0;
1727 ret = poll( &pollfd, 1, 0 );
1728 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1730 if (needs_close) close( fd );
1731 status = STATUS_PIPE_BROKEN;
1732 break;
1735 buffer->NamedPipeState = 0; /* FIXME */
1736 buffer->ReadDataAvailable = avail;
1737 buffer->NumberOfMessages = 0; /* FIXME */
1738 buffer->MessageLength = 0; /* FIXME */
1739 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1740 status = STATUS_SUCCESS;
1741 if (avail)
1743 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1744 if (data_size)
1746 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1747 if (res >= 0) io->Information += res;
1750 if (needs_close) close( fd );
1752 break;
1754 case FSCTL_PIPE_DISCONNECT:
1755 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1756 in_buffer, in_size, out_buffer, out_size );
1757 if (!status)
1759 int fd = server_remove_fd_from_cache( handle );
1760 if (fd != -1) close( fd );
1762 break;
1764 case FSCTL_PIPE_LISTEN:
1765 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1766 in_buffer, in_size, out_buffer, out_size );
1767 return status;
1769 case FSCTL_PIPE_IMPERSONATE:
1770 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1771 status = RtlImpersonateSelf( SecurityImpersonation );
1772 break;
1774 case FSCTL_IS_VOLUME_MOUNTED:
1775 case FSCTL_LOCK_VOLUME:
1776 case FSCTL_UNLOCK_VOLUME:
1777 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1778 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1779 status = STATUS_SUCCESS;
1780 break;
1782 case FSCTL_GET_RETRIEVAL_POINTERS:
1784 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1786 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1788 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1790 buffer->ExtentCount = 1;
1791 buffer->StartingVcn.QuadPart = 1;
1792 buffer->Extents[0].NextVcn.QuadPart = 0;
1793 buffer->Extents[0].Lcn.QuadPart = 0;
1794 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1795 status = STATUS_SUCCESS;
1797 else
1799 io->Information = 0;
1800 status = STATUS_BUFFER_TOO_SMALL;
1802 break;
1804 case FSCTL_SET_SPARSE:
1805 TRACE("FSCTL_SET_SPARSE: Ignoring request\n");
1806 io->Information = 0;
1807 status = STATUS_SUCCESS;
1808 break;
1809 case FSCTL_PIPE_WAIT:
1810 default:
1811 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1812 in_buffer, in_size, out_buffer, out_size );
1813 break;
1816 if (status != STATUS_PENDING) io->u.Status = status;
1817 return status;
1821 struct read_changes_fileio
1823 struct async_fileio io;
1824 void *buffer;
1825 ULONG buffer_size;
1826 ULONG data_size;
1827 char data[1];
1830 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1832 struct read_changes_fileio *fileio = user;
1833 int size = 0;
1835 if (status == STATUS_ALERTED)
1837 SERVER_START_REQ( read_change )
1839 req->handle = wine_server_obj_handle( fileio->io.handle );
1840 wine_server_set_reply( req, fileio->data, fileio->data_size );
1841 status = wine_server_call( req );
1842 size = wine_server_reply_size( reply );
1844 SERVER_END_REQ;
1846 if (status == STATUS_SUCCESS && fileio->buffer)
1848 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1849 int i, left = fileio->buffer_size;
1850 DWORD *last_entry_offset = NULL;
1851 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1853 while (size && left >= sizeof(*pfni))
1855 /* convert to an NT style path */
1856 for (i = 0; i < event->len; i++)
1857 if (event->name[i] == '/') event->name[i] = '\\';
1859 pfni->Action = event->action;
1860 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1861 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1862 last_entry_offset = &pfni->NextEntryOffset;
1864 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1866 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1867 pfni->FileNameLength *= sizeof(WCHAR);
1868 pfni->NextEntryOffset = i;
1869 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1870 left -= i;
1872 i = (offsetof(struct filesystem_event, name[event->len])
1873 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1874 event = (struct filesystem_event*)((char*)event + i);
1875 size -= i;
1878 if (size)
1880 status = STATUS_NOTIFY_ENUM_DIR;
1881 size = 0;
1883 else
1885 if (last_entry_offset) *last_entry_offset = 0;
1886 size = fileio->buffer_size - left;
1889 else
1891 status = STATUS_NOTIFY_ENUM_DIR;
1892 size = 0;
1896 if (status != STATUS_PENDING)
1898 iosb->u.Status = status;
1899 iosb->Information = size;
1900 release_fileio( &fileio->io );
1902 return status;
1905 #define FILE_NOTIFY_ALL ( \
1906 FILE_NOTIFY_CHANGE_FILE_NAME | \
1907 FILE_NOTIFY_CHANGE_DIR_NAME | \
1908 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1909 FILE_NOTIFY_CHANGE_SIZE | \
1910 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1911 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1912 FILE_NOTIFY_CHANGE_CREATION | \
1913 FILE_NOTIFY_CHANGE_SECURITY )
1915 /******************************************************************************
1916 * NtNotifyChangeDirectoryFile [NTDLL.@]
1918 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1919 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1920 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1922 struct read_changes_fileio *fileio;
1923 NTSTATUS status;
1924 ULONG size = max( 4096, buffer_size );
1926 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1927 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1929 if (!iosb) return STATUS_ACCESS_VIOLATION;
1930 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1932 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1933 read_changes_apc, handle );
1934 if (!fileio) return STATUS_NO_MEMORY;
1936 fileio->buffer = buffer;
1937 fileio->buffer_size = buffer_size;
1938 fileio->data_size = size;
1940 SERVER_START_REQ( read_directory_changes )
1942 req->filter = filter;
1943 req->want_data = (buffer != NULL);
1944 req->subtree = subtree;
1945 req->async = server_async( handle, &fileio->io, event, apc, apc_context, iosb );
1946 status = wine_server_call( req );
1948 SERVER_END_REQ;
1950 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1951 return status;
1954 /******************************************************************************
1955 * NtSetVolumeInformationFile [NTDLL.@]
1956 * ZwSetVolumeInformationFile [NTDLL.@]
1958 * Set volume information for an open file handle.
1960 * PARAMS
1961 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1962 * IoStatusBlock [O] Receives information about the operation on return
1963 * FsInformation [I] Source for volume information
1964 * Length [I] Size of FsInformation
1965 * FsInformationClass [I] Type of volume information to set
1967 * RETURNS
1968 * Success: 0. IoStatusBlock is updated.
1969 * Failure: An NTSTATUS error code describing the error.
1971 NTSTATUS WINAPI NtSetVolumeInformationFile(
1972 IN HANDLE FileHandle,
1973 PIO_STATUS_BLOCK IoStatusBlock,
1974 PVOID FsInformation,
1975 ULONG Length,
1976 FS_INFORMATION_CLASS FsInformationClass)
1978 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1979 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1980 return 0;
1983 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1984 static int futimens( int fd, const struct timespec spec[2] )
1986 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1988 #define HAVE_FUTIMENS
1989 #endif /* __ANDROID__ */
1991 #ifndef UTIME_OMIT
1992 #define UTIME_OMIT ((1 << 30) - 2)
1993 #endif
1995 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1997 NTSTATUS status = STATUS_SUCCESS;
1999 #ifdef HAVE_FUTIMENS
2000 struct timespec tv[2];
2002 tv[0].tv_sec = tv[1].tv_sec = 0;
2003 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
2004 if (atime->QuadPart)
2006 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
2007 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
2009 if (mtime->QuadPart)
2011 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
2012 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
2014 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
2016 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
2017 struct timeval tv[2];
2018 struct stat st;
2020 if (!atime->QuadPart || !mtime->QuadPart)
2023 tv[0].tv_sec = tv[0].tv_usec = 0;
2024 tv[1].tv_sec = tv[1].tv_usec = 0;
2025 if (!fstat( fd, &st ))
2027 tv[0].tv_sec = st.st_atime;
2028 tv[1].tv_sec = st.st_mtime;
2029 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2030 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
2031 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2032 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
2033 #endif
2034 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2035 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
2036 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2037 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
2038 #endif
2041 if (atime->QuadPart)
2043 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
2044 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
2046 if (mtime->QuadPart)
2048 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
2049 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
2051 #ifdef HAVE_FUTIMES
2052 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
2053 #elif defined(HAVE_FUTIMESAT)
2054 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
2055 #endif
2057 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
2058 FIXME( "setting file times not supported\n" );
2059 status = STATUS_NOT_IMPLEMENTED;
2060 #endif
2061 return status;
2064 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
2065 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
2067 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
2068 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
2069 RtlSecondsSince1970ToTime( st->st_atime, atime );
2070 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2071 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
2072 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2073 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
2074 #endif
2075 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2076 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2077 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2078 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2079 #endif
2080 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2081 atime->QuadPart += st->st_atim.tv_nsec / 100;
2082 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2083 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2084 #endif
2085 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2086 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2087 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2088 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2089 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2090 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2091 #endif
2092 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2093 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2094 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2095 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2096 #endif
2097 #else
2098 *creation = *mtime;
2099 #endif
2102 /* fill in the file information that depends on the stat and attribute info */
2103 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2104 FILE_INFORMATION_CLASS class )
2106 switch (class)
2108 case FileBasicInformation:
2110 FILE_BASIC_INFORMATION *info = ptr;
2112 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2113 &info->LastAccessTime, &info->CreationTime );
2114 info->FileAttributes = attr;
2116 break;
2117 case FileStandardInformation:
2119 FILE_STANDARD_INFORMATION *info = ptr;
2121 if ((info->Directory = S_ISDIR(st->st_mode)))
2123 info->AllocationSize.QuadPart = 0;
2124 info->EndOfFile.QuadPart = 0;
2125 info->NumberOfLinks = 1;
2127 else
2129 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2130 info->EndOfFile.QuadPart = st->st_size;
2131 info->NumberOfLinks = st->st_nlink;
2134 break;
2135 case FileInternalInformation:
2137 FILE_INTERNAL_INFORMATION *info = ptr;
2138 info->IndexNumber.QuadPart = st->st_ino;
2140 break;
2141 case FileEndOfFileInformation:
2143 FILE_END_OF_FILE_INFORMATION *info = ptr;
2144 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2146 break;
2147 case FileAllInformation:
2149 FILE_ALL_INFORMATION *info = ptr;
2150 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2151 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2152 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2154 break;
2155 /* all directory structures start with the FileDirectoryInformation layout */
2156 case FileBothDirectoryInformation:
2157 case FileFullDirectoryInformation:
2158 case FileDirectoryInformation:
2160 FILE_DIRECTORY_INFORMATION *info = ptr;
2162 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2163 &info->LastAccessTime, &info->CreationTime );
2164 if (S_ISDIR(st->st_mode))
2166 info->AllocationSize.QuadPart = 0;
2167 info->EndOfFile.QuadPart = 0;
2169 else
2171 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2172 info->EndOfFile.QuadPart = st->st_size;
2174 info->FileAttributes = attr;
2176 break;
2177 case FileIdFullDirectoryInformation:
2179 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2180 info->FileId.QuadPart = st->st_ino;
2181 fill_file_info( st, attr, info, FileDirectoryInformation );
2183 break;
2184 case FileIdBothDirectoryInformation:
2186 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2187 info->FileId.QuadPart = st->st_ino;
2188 fill_file_info( st, attr, info, FileDirectoryInformation );
2190 break;
2191 case FileIdGlobalTxDirectoryInformation:
2193 FILE_ID_GLOBAL_TX_DIR_INFORMATION *info = ptr;
2194 info->FileId.QuadPart = st->st_ino;
2195 fill_file_info( st, attr, info, FileDirectoryInformation );
2197 break;
2199 default:
2200 return STATUS_INVALID_INFO_CLASS;
2202 return STATUS_SUCCESS;
2205 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2207 data_size_t size = 1024;
2208 NTSTATUS ret;
2209 char *name;
2211 for (;;)
2213 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2214 if (!name) return STATUS_NO_MEMORY;
2215 unix_name->MaximumLength = size + 1;
2217 SERVER_START_REQ( get_handle_unix_name )
2219 req->handle = wine_server_obj_handle( handle );
2220 wine_server_set_reply( req, name, size );
2221 ret = wine_server_call( req );
2222 size = reply->name_len;
2224 SERVER_END_REQ;
2226 if (!ret)
2228 name[size] = 0;
2229 unix_name->Buffer = name;
2230 unix_name->Length = size;
2231 break;
2233 RtlFreeHeap( GetProcessHeap(), 0, name );
2234 if (ret != STATUS_BUFFER_OVERFLOW) break;
2236 return ret;
2239 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2241 UNICODE_STRING nt_name;
2242 NTSTATUS status;
2244 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2246 const WCHAR *ptr = nt_name.Buffer;
2247 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2249 /* Skip the volume mount point. */
2250 while (ptr != end && *ptr == '\\') ++ptr;
2251 while (ptr != end && *ptr != '\\') ++ptr;
2252 while (ptr != end && *ptr == '\\') ++ptr;
2253 while (ptr != end && *ptr != '\\') ++ptr;
2255 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2256 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2257 else *name_len = info->FileNameLength;
2259 memcpy( info->FileName, ptr, *name_len );
2260 RtlFreeUnicodeString( &nt_name );
2263 return status;
2266 /******************************************************************************
2267 * NtQueryInformationFile [NTDLL.@]
2268 * ZwQueryInformationFile [NTDLL.@]
2270 * Get information about an open file handle.
2272 * PARAMS
2273 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2274 * io [O] Receives information about the operation on return
2275 * ptr [O] Destination for file information
2276 * len [I] Size of FileInformation
2277 * class [I] Type of file information to get
2279 * RETURNS
2280 * Success: 0. IoStatusBlock and FileInformation are updated.
2281 * Failure: An NTSTATUS error code describing the error.
2283 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2284 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2286 static const size_t info_sizes[] =
2289 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2290 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2291 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2292 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2293 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2294 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2295 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2296 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2297 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2298 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2299 0, /* FileLinkInformation */
2300 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2301 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2302 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2303 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2304 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2305 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2306 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2307 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2308 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2309 0, /* FileAlternateNameInformation */
2310 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2311 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2312 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2313 0, /* FilePipeRemoteInformation */
2314 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2315 0, /* FileMailslotSetInformation */
2316 0, /* FileCompressionInformation */
2317 0, /* FileObjectIdInformation */
2318 0, /* FileCompletionInformation */
2319 0, /* FileMoveClusterInformation */
2320 0, /* FileQuotaInformation */
2321 0, /* FileReparsePointInformation */
2322 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2323 0, /* FileAttributeTagInformation */
2324 0, /* FileTrackingInformation */
2325 0, /* FileIdBothDirectoryInformation */
2326 0, /* FileIdFullDirectoryInformation */
2327 0, /* FileValidDataLengthInformation */
2328 0, /* FileShortNameInformation */
2329 0, /* FileIoCompletionNotificationInformation, */
2330 0, /* FileIoStatusBlockRangeInformation */
2331 0, /* FileIoPriorityHintInformation */
2332 0, /* FileSfioReserveInformation */
2333 0, /* FileSfioVolumeInformation */
2334 0, /* FileHardLinkInformation */
2335 0, /* FileProcessIdsUsingFileInformation */
2336 0, /* FileNormalizedNameInformation */
2337 0, /* FileNetworkPhysicalNameInformation */
2338 0, /* FileIdGlobalTxDirectoryInformation */
2339 0, /* FileIsRemoteDeviceInformation */
2340 0, /* FileAttributeCacheInformation */
2341 0, /* FileNumaNodeInformation */
2342 0, /* FileStandardLinkInformation */
2343 0, /* FileRemoteProtocolInformation */
2344 0, /* FileRenameInformationBypassAccessCheck */
2345 0, /* FileLinkInformationBypassAccessCheck */
2346 0, /* FileVolumeNameInformation */
2347 sizeof(FILE_ID_INFORMATION), /* FileIdInformation */
2348 0, /* FileIdExtdDirectoryInformation */
2349 0, /* FileReplaceCompletionInformation */
2350 0, /* FileHardLinkFullIdInformation */
2351 0, /* FileIdExtdBothDirectoryInformation */
2354 struct stat st;
2355 int fd, needs_close = FALSE;
2356 ULONG attr;
2358 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2360 io->Information = 0;
2362 if (class <= 0 || class >= FileMaximumInformation)
2363 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2364 if (!info_sizes[class])
2366 FIXME("Unsupported class (%d)\n", class);
2367 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2369 if (len < info_sizes[class])
2370 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2372 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2374 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2375 return io->u.Status;
2378 switch (class)
2380 case FileBasicInformation:
2381 if (fd_get_file_info( fd, &st, &attr ) == -1)
2382 io->u.Status = FILE_GetNtStatus();
2383 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2384 io->u.Status = STATUS_INVALID_INFO_CLASS;
2385 else
2386 fill_file_info( &st, attr, ptr, class );
2387 break;
2388 case FileStandardInformation:
2390 FILE_STANDARD_INFORMATION *info = ptr;
2392 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2393 else
2395 fill_file_info( &st, attr, info, class );
2396 info->DeletePending = FALSE; /* FIXME */
2399 break;
2400 case FilePositionInformation:
2402 FILE_POSITION_INFORMATION *info = ptr;
2403 off_t res = lseek( fd, 0, SEEK_CUR );
2404 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2405 else info->CurrentByteOffset.QuadPart = res;
2407 break;
2408 case FileInternalInformation:
2409 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2410 else fill_file_info( &st, attr, ptr, class );
2411 break;
2412 case FileEaInformation:
2414 FILE_EA_INFORMATION *info = ptr;
2415 info->EaSize = 0;
2417 break;
2418 case FileAccessInformation:
2420 FILE_ACCESS_INFORMATION *info = ptr;
2421 SERVER_START_REQ( get_object_info )
2423 req->handle = wine_server_obj_handle( hFile );
2424 io->u.Status = wine_server_call( req );
2425 if (io->u.Status == STATUS_SUCCESS)
2426 info->AccessFlags = reply->access;
2428 SERVER_END_REQ;
2430 break;
2431 case FileEndOfFileInformation:
2432 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2433 else fill_file_info( &st, attr, ptr, class );
2434 break;
2435 case FileAllInformation:
2437 FILE_ALL_INFORMATION *info = ptr;
2438 ANSI_STRING unix_name;
2440 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2441 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2442 io->u.Status = STATUS_INVALID_INFO_CLASS;
2443 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2445 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2447 fill_file_info( &st, attr, info, FileAllInformation );
2448 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2449 info->EaInformation.EaSize = 0;
2450 info->AccessInformation.AccessFlags = 0; /* FIXME */
2451 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2452 info->ModeInformation.Mode = 0; /* FIXME */
2453 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2455 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2456 RtlFreeAnsiString( &unix_name );
2457 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2460 break;
2461 case FileMailslotQueryInformation:
2463 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2465 SERVER_START_REQ( set_mailslot_info )
2467 req->handle = wine_server_obj_handle( hFile );
2468 req->flags = 0;
2469 io->u.Status = wine_server_call( req );
2470 if( io->u.Status == STATUS_SUCCESS )
2472 info->MaximumMessageSize = reply->max_msgsize;
2473 info->MailslotQuota = 0;
2474 info->NextMessageSize = 0;
2475 info->MessagesAvailable = 0;
2476 info->ReadTimeout.QuadPart = reply->read_timeout;
2479 SERVER_END_REQ;
2480 if (!io->u.Status)
2482 char *tmpbuf;
2483 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2484 if (size > 0x10000) size = 0x10000;
2485 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2487 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2489 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2490 info->MessagesAvailable = (res > 0);
2491 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2492 if (needs_close) close( fd );
2494 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2498 break;
2499 case FilePipeInformation:
2501 FILE_PIPE_INFORMATION* pi = ptr;
2503 SERVER_START_REQ( get_named_pipe_info )
2505 req->handle = wine_server_obj_handle( hFile );
2506 if (!(io->u.Status = wine_server_call( req )))
2508 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2509 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2510 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2511 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2514 SERVER_END_REQ;
2516 break;
2517 case FilePipeLocalInformation:
2519 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2521 SERVER_START_REQ( get_named_pipe_info )
2523 req->handle = wine_server_obj_handle( hFile );
2524 if (!(io->u.Status = wine_server_call( req )))
2526 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2527 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2528 switch (reply->sharing)
2530 case FILE_SHARE_READ:
2531 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2532 break;
2533 case FILE_SHARE_WRITE:
2534 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2535 break;
2536 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2537 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2538 break;
2540 pli->MaximumInstances = reply->maxinstances;
2541 pli->CurrentInstances = reply->instances;
2542 pli->InboundQuota = reply->insize;
2543 pli->ReadDataAvailable = 0; /* FIXME */
2544 pli->OutboundQuota = reply->outsize;
2545 pli->WriteQuotaAvailable = 0; /* FIXME */
2546 pli->NamedPipeState = 0; /* FIXME */
2547 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2548 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2551 SERVER_END_REQ;
2553 break;
2554 case FileNameInformation:
2556 FILE_NAME_INFORMATION *info = ptr;
2557 ANSI_STRING unix_name;
2559 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2561 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2562 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2563 RtlFreeAnsiString( &unix_name );
2564 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2567 break;
2568 case FileNetworkOpenInformation:
2570 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2571 ANSI_STRING unix_name;
2573 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2575 ULONG attributes;
2576 struct stat st;
2578 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2579 io->u.Status = FILE_GetNtStatus();
2580 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2581 io->u.Status = STATUS_INVALID_INFO_CLASS;
2582 else
2584 FILE_BASIC_INFORMATION basic;
2585 FILE_STANDARD_INFORMATION std;
2587 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2588 fill_file_info( &st, attributes, &std, FileStandardInformation );
2590 info->CreationTime = basic.CreationTime;
2591 info->LastAccessTime = basic.LastAccessTime;
2592 info->LastWriteTime = basic.LastWriteTime;
2593 info->ChangeTime = basic.ChangeTime;
2594 info->AllocationSize = std.AllocationSize;
2595 info->EndOfFile = std.EndOfFile;
2596 info->FileAttributes = basic.FileAttributes;
2598 RtlFreeAnsiString( &unix_name );
2601 break;
2602 case FileIdInformation:
2603 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2604 else
2606 FILE_ID_INFORMATION *info = ptr;
2607 info->VolumeSerialNumber = 0; /* FIXME */
2608 memset( &info->FileId, 0, sizeof(info->FileId) );
2609 *(ULONGLONG *)&info->FileId = st.st_ino;
2611 break;
2612 default:
2613 FIXME("Unsupported class (%d)\n", class);
2614 io->u.Status = STATUS_NOT_IMPLEMENTED;
2615 break;
2617 if (needs_close) close( fd );
2618 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2619 return io->u.Status;
2622 /******************************************************************************
2623 * NtSetInformationFile [NTDLL.@]
2624 * ZwSetInformationFile [NTDLL.@]
2626 * Set information about an open file handle.
2628 * PARAMS
2629 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2630 * io [O] Receives information about the operation on return
2631 * ptr [I] Source for file information
2632 * len [I] Size of FileInformation
2633 * class [I] Type of file information to set
2635 * RETURNS
2636 * Success: 0. io is updated.
2637 * Failure: An NTSTATUS error code describing the error.
2639 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2640 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2642 int fd, needs_close;
2644 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2646 io->u.Status = STATUS_SUCCESS;
2647 switch (class)
2649 case FileBasicInformation:
2650 if (len >= sizeof(FILE_BASIC_INFORMATION))
2652 struct stat st;
2653 const FILE_BASIC_INFORMATION *info = ptr;
2655 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2656 return io->u.Status;
2658 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2659 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2661 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2663 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2664 else
2666 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2668 if (S_ISDIR( st.st_mode))
2669 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2670 else
2671 st.st_mode &= ~0222; /* clear write permission bits */
2673 else
2675 /* add write permission only where we already have read permission */
2676 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2678 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2682 if (needs_close) close( fd );
2684 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2685 break;
2687 case FilePositionInformation:
2688 if (len >= sizeof(FILE_POSITION_INFORMATION))
2690 const FILE_POSITION_INFORMATION *info = ptr;
2692 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2693 return io->u.Status;
2695 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2696 io->u.Status = FILE_GetNtStatus();
2698 if (needs_close) close( fd );
2700 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2701 break;
2703 case FileEndOfFileInformation:
2704 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2706 struct stat st;
2707 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2709 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2710 return io->u.Status;
2712 /* first try normal truncate */
2713 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2715 /* now check for the need to extend the file */
2716 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2718 static const char zero;
2720 /* extend the file one byte beyond the requested size and then truncate it */
2721 /* this should work around ftruncate implementations that can't extend files */
2722 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2723 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2725 io->u.Status = FILE_GetNtStatus();
2727 if (needs_close) close( fd );
2729 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2730 break;
2732 case FilePipeInformation:
2733 if (len >= sizeof(FILE_PIPE_INFORMATION))
2735 FILE_PIPE_INFORMATION *info = ptr;
2737 if ((info->CompletionMode | info->ReadMode) & ~1)
2739 io->u.Status = STATUS_INVALID_PARAMETER;
2740 break;
2743 SERVER_START_REQ( set_named_pipe_info )
2745 req->handle = wine_server_obj_handle( handle );
2746 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2747 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2748 io->u.Status = wine_server_call( req );
2750 SERVER_END_REQ;
2752 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2753 break;
2755 case FileMailslotSetInformation:
2757 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2759 SERVER_START_REQ( set_mailslot_info )
2761 req->handle = wine_server_obj_handle( handle );
2762 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2763 req->read_timeout = info->ReadTimeout.QuadPart;
2764 io->u.Status = wine_server_call( req );
2766 SERVER_END_REQ;
2768 break;
2770 case FileCompletionInformation:
2771 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2773 FILE_COMPLETION_INFORMATION *info = ptr;
2775 SERVER_START_REQ( set_completion_info )
2777 req->handle = wine_server_obj_handle( handle );
2778 req->chandle = wine_server_obj_handle( info->CompletionPort );
2779 req->ckey = info->CompletionKey;
2780 io->u.Status = wine_server_call( req );
2782 SERVER_END_REQ;
2783 } else
2784 io->u.Status = STATUS_INVALID_PARAMETER_3;
2785 break;
2787 case FileAllInformation:
2788 io->u.Status = STATUS_INVALID_INFO_CLASS;
2789 break;
2791 case FileValidDataLengthInformation:
2792 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2794 struct stat st;
2795 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2797 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2798 return io->u.Status;
2800 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2801 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2802 io->u.Status = STATUS_INVALID_PARAMETER;
2803 else
2805 #ifdef HAVE_FALLOCATE
2806 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2808 NTSTATUS status = FILE_GetNtStatus();
2809 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2810 else io->u.Status = status;
2812 #else
2813 FIXME( "setting valid data length not supported\n" );
2814 #endif
2816 if (needs_close) close( fd );
2818 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2819 break;
2821 case FileDispositionInformation:
2822 if (len >= sizeof(FILE_DISPOSITION_INFORMATION))
2824 FILE_DISPOSITION_INFORMATION *info = ptr;
2826 SERVER_START_REQ( set_fd_disp_info )
2828 req->handle = wine_server_obj_handle( handle );
2829 req->unlink = info->DoDeleteFile;
2830 io->u.Status = wine_server_call( req );
2832 SERVER_END_REQ;
2833 } else
2834 io->u.Status = STATUS_INVALID_PARAMETER_3;
2835 break;
2837 case FileRenameInformation:
2838 if (len >= sizeof(FILE_RENAME_INFORMATION))
2840 FILE_RENAME_INFORMATION *info = ptr;
2841 UNICODE_STRING name_str;
2842 OBJECT_ATTRIBUTES attr;
2843 ANSI_STRING unix_name;
2845 name_str.Buffer = info->FileName;
2846 name_str.Length = info->FileNameLength;
2847 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2849 attr.Length = sizeof(attr);
2850 attr.ObjectName = &name_str;
2851 attr.RootDirectory = info->RootDir;
2852 attr.Attributes = OBJ_CASE_INSENSITIVE;
2854 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2855 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2856 break;
2858 if (!info->Replace && io->u.Status == STATUS_SUCCESS)
2860 RtlFreeAnsiString( &unix_name );
2861 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2862 break;
2865 SERVER_START_REQ( set_fd_name_info )
2867 req->handle = wine_server_obj_handle( handle );
2868 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2869 req->link = FALSE;
2870 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2871 io->u.Status = wine_server_call( req );
2873 SERVER_END_REQ;
2875 RtlFreeAnsiString( &unix_name );
2877 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2878 break;
2880 case FileLinkInformation:
2881 if (len >= sizeof(FILE_LINK_INFORMATION))
2883 FILE_LINK_INFORMATION *info = ptr;
2884 UNICODE_STRING name_str;
2885 OBJECT_ATTRIBUTES attr;
2886 ANSI_STRING unix_name;
2888 name_str.Buffer = info->FileName;
2889 name_str.Length = info->FileNameLength;
2890 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2892 attr.Length = sizeof(attr);
2893 attr.ObjectName = &name_str;
2894 attr.RootDirectory = info->RootDirectory;
2895 attr.Attributes = OBJ_CASE_INSENSITIVE;
2897 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2898 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2899 break;
2901 if (!info->ReplaceIfExists && io->u.Status == STATUS_SUCCESS)
2903 RtlFreeAnsiString( &unix_name );
2904 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2905 break;
2908 SERVER_START_REQ( set_fd_name_info )
2910 req->handle = wine_server_obj_handle( handle );
2911 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2912 req->link = TRUE;
2913 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2914 io->u.Status = wine_server_call( req );
2916 SERVER_END_REQ;
2918 RtlFreeAnsiString( &unix_name );
2920 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2921 break;
2923 default:
2924 FIXME("Unsupported class (%d)\n", class);
2925 io->u.Status = STATUS_NOT_IMPLEMENTED;
2926 break;
2928 io->Information = 0;
2929 return io->u.Status;
2933 /******************************************************************************
2934 * NtQueryFullAttributesFile (NTDLL.@)
2936 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2937 FILE_NETWORK_OPEN_INFORMATION *info )
2939 ANSI_STRING unix_name;
2940 NTSTATUS status;
2942 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2944 ULONG attributes;
2945 struct stat st;
2947 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2948 status = FILE_GetNtStatus();
2949 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2950 status = STATUS_INVALID_INFO_CLASS;
2951 else
2953 FILE_BASIC_INFORMATION basic;
2954 FILE_STANDARD_INFORMATION std;
2956 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2957 fill_file_info( &st, attributes, &std, FileStandardInformation );
2959 info->CreationTime = basic.CreationTime;
2960 info->LastAccessTime = basic.LastAccessTime;
2961 info->LastWriteTime = basic.LastWriteTime;
2962 info->ChangeTime = basic.ChangeTime;
2963 info->AllocationSize = std.AllocationSize;
2964 info->EndOfFile = std.EndOfFile;
2965 info->FileAttributes = basic.FileAttributes;
2966 if (DIR_is_hidden_file( attr->ObjectName ))
2967 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2969 RtlFreeAnsiString( &unix_name );
2971 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2972 return status;
2976 /******************************************************************************
2977 * NtQueryAttributesFile (NTDLL.@)
2978 * ZwQueryAttributesFile (NTDLL.@)
2980 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2982 ANSI_STRING unix_name;
2983 NTSTATUS status;
2985 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2987 ULONG attributes;
2988 struct stat st;
2990 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2991 status = FILE_GetNtStatus();
2992 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2993 status = STATUS_INVALID_INFO_CLASS;
2994 else
2996 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2997 if (DIR_is_hidden_file( attr->ObjectName ))
2998 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
3000 RtlFreeAnsiString( &unix_name );
3002 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
3003 return status;
3007 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3008 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
3009 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
3010 unsigned int flags )
3012 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
3014 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3015 /* Don't assume read-only, let the mount options set it below */
3016 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3018 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
3019 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
3021 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3022 info->Characteristics |= FILE_REMOTE_DEVICE;
3024 else if (!strcmp("procfs", fstypename))
3025 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3026 else
3027 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3029 if (flags & MNT_RDONLY)
3030 info->Characteristics |= FILE_READ_ONLY_DEVICE;
3032 if (!(flags & MNT_LOCAL))
3034 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3035 info->Characteristics |= FILE_REMOTE_DEVICE;
3038 #endif
3040 static inline BOOL is_device_placeholder( int fd )
3042 static const char wine_placeholder[] = "Wine device placeholder";
3043 char buffer[sizeof(wine_placeholder)-1];
3045 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
3046 return FALSE;
3047 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
3050 /******************************************************************************
3051 * get_device_info
3053 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
3055 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
3057 struct stat st;
3059 info->Characteristics = 0;
3060 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
3061 if (S_ISCHR( st.st_mode ))
3063 info->DeviceType = FILE_DEVICE_UNKNOWN;
3064 #ifdef linux
3065 switch(major(st.st_rdev))
3067 case MEM_MAJOR:
3068 info->DeviceType = FILE_DEVICE_NULL;
3069 break;
3070 case TTY_MAJOR:
3071 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
3072 break;
3073 case LP_MAJOR:
3074 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
3075 break;
3076 case SCSI_TAPE_MAJOR:
3077 info->DeviceType = FILE_DEVICE_TAPE;
3078 break;
3080 #endif
3082 else if (S_ISBLK( st.st_mode ))
3084 info->DeviceType = FILE_DEVICE_DISK;
3086 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
3088 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
3090 else if (is_device_placeholder( fd ))
3092 info->DeviceType = FILE_DEVICE_DISK;
3094 else /* regular file or directory */
3096 #if defined(linux) && defined(HAVE_FSTATFS)
3097 struct statfs stfs;
3099 /* check for floppy disk */
3100 if (major(st.st_dev) == FLOPPY_MAJOR)
3101 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3103 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
3104 switch (stfs.f_type)
3106 case 0x9660: /* iso9660 */
3107 case 0x9fa1: /* supermount */
3108 case 0x15013346: /* udf */
3109 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3110 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3111 break;
3112 case 0x6969: /* nfs */
3113 case 0x517B: /* smbfs */
3114 case 0x564c: /* ncpfs */
3115 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3116 info->Characteristics |= FILE_REMOTE_DEVICE;
3117 break;
3118 case 0x01021994: /* tmpfs */
3119 case 0x28cd3d45: /* cramfs */
3120 case 0x1373: /* devfs */
3121 case 0x9fa0: /* procfs */
3122 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3123 break;
3124 default:
3125 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3126 break;
3128 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3129 struct statfs stfs;
3131 if (fstatfs( fd, &stfs ) < 0)
3132 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3133 else
3134 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
3135 #elif defined(__NetBSD__)
3136 struct statvfs stfs;
3138 if (fstatvfs( fd, &stfs) < 0)
3139 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3140 else
3141 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
3142 #elif defined(sun)
3143 /* Use dkio to work out device types */
3145 # include <sys/dkio.h>
3146 # include <sys/vtoc.h>
3147 struct dk_cinfo dkinf;
3148 int retval = ioctl(fd, DKIOCINFO, &dkinf);
3149 if(retval==-1){
3150 WARN("Unable to get disk device type information - assuming a disk like device\n");
3151 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3153 switch (dkinf.dki_ctype)
3155 case DKC_CDROM:
3156 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3157 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3158 break;
3159 case DKC_NCRFLOPPY:
3160 case DKC_SMSFLOPPY:
3161 case DKC_INTEL82072:
3162 case DKC_INTEL82077:
3163 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3164 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3165 break;
3166 case DKC_MD:
3167 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3168 break;
3169 default:
3170 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3173 #else
3174 static int warned;
3175 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3176 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3177 #endif
3178 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3180 return STATUS_SUCCESS;
3184 /******************************************************************************
3185 * NtQueryVolumeInformationFile [NTDLL.@]
3186 * ZwQueryVolumeInformationFile [NTDLL.@]
3188 * Get volume information for an open file handle.
3190 * PARAMS
3191 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3192 * io [O] Receives information about the operation on return
3193 * buffer [O] Destination for volume information
3194 * length [I] Size of FsInformation
3195 * info_class [I] Type of volume information to set
3197 * RETURNS
3198 * Success: 0. io and buffer are updated.
3199 * Failure: An NTSTATUS error code describing the error.
3201 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3202 PVOID buffer, ULONG length,
3203 FS_INFORMATION_CLASS info_class )
3205 int fd, needs_close;
3206 struct stat st;
3207 static int once;
3209 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
3210 return io->u.Status;
3212 io->u.Status = STATUS_NOT_IMPLEMENTED;
3213 io->Information = 0;
3215 switch( info_class )
3217 case FileFsVolumeInformation:
3218 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3219 break;
3220 case FileFsLabelInformation:
3221 FIXME( "%p: label info not supported\n", handle );
3222 break;
3223 case FileFsSizeInformation:
3224 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3225 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3226 else
3228 FILE_FS_SIZE_INFORMATION *info = buffer;
3230 if (fstat( fd, &st ) < 0)
3232 io->u.Status = FILE_GetNtStatus();
3233 break;
3235 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3237 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3239 else
3241 ULONGLONG bsize;
3242 /* Linux's fstatvfs is buggy */
3243 #if !defined(linux) || !defined(HAVE_FSTATFS)
3244 struct statvfs stfs;
3246 if (fstatvfs( fd, &stfs ) < 0)
3248 io->u.Status = FILE_GetNtStatus();
3249 break;
3251 bsize = stfs.f_frsize;
3252 #else
3253 struct statfs stfs;
3254 if (fstatfs( fd, &stfs ) < 0)
3256 io->u.Status = FILE_GetNtStatus();
3257 break;
3259 bsize = stfs.f_bsize;
3260 #endif
3261 if (bsize == 2048) /* assume CD-ROM */
3263 info->BytesPerSector = 2048;
3264 info->SectorsPerAllocationUnit = 1;
3266 else
3268 info->BytesPerSector = 512;
3269 info->SectorsPerAllocationUnit = 8;
3271 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3272 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3273 io->Information = sizeof(*info);
3274 io->u.Status = STATUS_SUCCESS;
3277 break;
3278 case FileFsDeviceInformation:
3279 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3280 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3281 else
3283 FILE_FS_DEVICE_INFORMATION *info = buffer;
3285 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3286 io->Information = sizeof(*info);
3288 break;
3289 case FileFsAttributeInformation:
3290 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3291 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3292 else
3294 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3296 FIXME( "%p: faking attribute info\n", handle );
3297 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3298 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3299 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3300 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3301 info->FileSystemNameLength = sizeof(ntfsW);
3302 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3304 io->Information = sizeof(*info);
3305 io->u.Status = STATUS_SUCCESS;
3307 break;
3308 case FileFsControlInformation:
3309 FIXME( "%p: control info not supported\n", handle );
3310 break;
3311 case FileFsFullSizeInformation:
3312 FIXME( "%p: full size info not supported\n", handle );
3313 break;
3314 case FileFsObjectIdInformation:
3315 FIXME( "%p: object id info not supported\n", handle );
3316 break;
3317 case FileFsMaximumInformation:
3318 FIXME( "%p: maximum info not supported\n", handle );
3319 break;
3320 default:
3321 io->u.Status = STATUS_INVALID_PARAMETER;
3322 break;
3324 if (needs_close) close( fd );
3325 return io->u.Status;
3329 /******************************************************************
3330 * NtQueryEaFile (NTDLL.@)
3332 * Read extended attributes from NTFS files.
3334 * PARAMS
3335 * hFile [I] File handle, must be opened with FILE_READ_EA access
3336 * iosb [O] Receives information about the operation on return
3337 * buffer [O] Output buffer
3338 * length [I] Length of output buffer
3339 * single_entry [I] Only read and return one entry
3340 * ea_list [I] Optional list with names of EAs to return
3341 * ea_list_len [I] Length of ea_list in bytes
3342 * ea_index [I] Optional pointer to 1-based index of attribute to return
3343 * restart [I] restart EA scan
3345 * RETURNS
3346 * Success: 0. Atrributes read into buffer
3347 * Failure: An NTSTATUS error code describing the error.
3349 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3350 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3351 PULONG ea_index, BOOLEAN restart )
3353 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3354 hFile, iosb, buffer, length, single_entry, ea_list,
3355 ea_list_len, ea_index, restart);
3356 return STATUS_ACCESS_DENIED;
3360 /******************************************************************
3361 * NtSetEaFile (NTDLL.@)
3363 * Update extended attributes for NTFS files.
3365 * PARAMS
3366 * hFile [I] File handle, must be opened with FILE_READ_EA access
3367 * iosb [O] Receives information about the operation on return
3368 * buffer [I] Buffer with EA information
3369 * length [I] Length of buffer
3371 * RETURNS
3372 * Success: 0. Attributes are updated
3373 * Failure: An NTSTATUS error code describing the error.
3375 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3377 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3378 return STATUS_ACCESS_DENIED;
3382 /******************************************************************
3383 * NtFlushBuffersFile (NTDLL.@)
3385 * Flush any buffered data on an open file handle.
3387 * PARAMS
3388 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3389 * IoStatusBlock [O] Receives information about the operation on return
3391 * RETURNS
3392 * Success: 0. IoStatusBlock is updated.
3393 * Failure: An NTSTATUS error code describing the error.
3395 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3397 NTSTATUS ret;
3398 HANDLE hEvent = NULL;
3399 enum server_fd_type type;
3400 int fd, needs_close;
3402 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3403 if (ret == STATUS_ACCESS_DENIED)
3404 ret = server_get_unix_fd( hFile, FILE_APPEND_DATA, &fd, &needs_close, &type, NULL );
3406 if (!ret && type == FD_TYPE_SERIAL)
3408 ret = COMM_FlushBuffersFile( fd );
3410 else if (ret != STATUS_ACCESS_DENIED)
3412 SERVER_START_REQ( flush )
3414 req->async = server_async( hFile, NULL, NULL, NULL, NULL, IoStatusBlock );
3415 ret = wine_server_call( req );
3416 hEvent = wine_server_ptr_handle( reply->event );
3418 SERVER_END_REQ;
3420 if (hEvent)
3422 NtWaitForSingleObject( hEvent, FALSE, NULL );
3423 NtClose( hEvent );
3424 ret = STATUS_SUCCESS;
3428 if (needs_close) close( fd );
3429 return ret;
3432 /******************************************************************
3433 * NtLockFile (NTDLL.@)
3437 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3438 PIO_APC_ROUTINE apc, void* apc_user,
3439 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3440 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3441 BOOLEAN exclusive )
3443 NTSTATUS ret;
3444 HANDLE handle;
3445 BOOLEAN async;
3446 static BOOLEAN warn = TRUE;
3448 if (apc || io_status || key)
3450 FIXME("Unimplemented yet parameter\n");
3451 return STATUS_NOT_IMPLEMENTED;
3454 if (apc_user && warn)
3456 FIXME("I/O completion on lock not implemented yet\n");
3457 warn = FALSE;
3460 for (;;)
3462 SERVER_START_REQ( lock_file )
3464 req->handle = wine_server_obj_handle( hFile );
3465 req->offset = offset->QuadPart;
3466 req->count = count->QuadPart;
3467 req->shared = !exclusive;
3468 req->wait = !dont_wait;
3469 ret = wine_server_call( req );
3470 handle = wine_server_ptr_handle( reply->handle );
3471 async = reply->overlapped;
3473 SERVER_END_REQ;
3474 if (ret != STATUS_PENDING)
3476 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3477 return ret;
3480 if (async)
3482 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3483 if (handle) NtClose( handle );
3484 return STATUS_PENDING;
3486 if (handle)
3488 NtWaitForSingleObject( handle, FALSE, NULL );
3489 NtClose( handle );
3491 else
3493 LARGE_INTEGER time;
3495 /* Unix lock conflict, sleep a bit and retry */
3496 time.QuadPart = 100 * (ULONGLONG)10000;
3497 time.QuadPart = -time.QuadPart;
3498 NtDelayExecution( FALSE, &time );
3504 /******************************************************************
3505 * NtUnlockFile (NTDLL.@)
3509 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3510 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3511 PULONG key )
3513 NTSTATUS status;
3515 TRACE( "%p %x%08x %x%08x\n",
3516 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3518 if (io_status || key)
3520 FIXME("Unimplemented yet parameter\n");
3521 return STATUS_NOT_IMPLEMENTED;
3524 SERVER_START_REQ( unlock_file )
3526 req->handle = wine_server_obj_handle( hFile );
3527 req->offset = offset->QuadPart;
3528 req->count = count->QuadPart;
3529 status = wine_server_call( req );
3531 SERVER_END_REQ;
3532 return status;
3535 /******************************************************************
3536 * NtCreateNamedPipeFile (NTDLL.@)
3540 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3541 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3542 ULONG sharing, ULONG dispo, ULONG options,
3543 ULONG pipe_type, ULONG read_mode,
3544 ULONG completion_mode, ULONG max_inst,
3545 ULONG inbound_quota, ULONG outbound_quota,
3546 PLARGE_INTEGER timeout)
3548 NTSTATUS status;
3549 data_size_t len;
3550 struct object_attributes *objattr;
3552 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3553 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3554 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3555 outbound_quota, timeout);
3557 if (!attr) return STATUS_INVALID_PARAMETER;
3559 /* assume we only get relative timeout */
3560 if (timeout->QuadPart > 0)
3561 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3563 if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
3565 SERVER_START_REQ( create_named_pipe )
3567 req->access = access;
3568 req->options = options;
3569 req->sharing = sharing;
3570 req->flags =
3571 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3572 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3573 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3574 req->maxinstances = max_inst;
3575 req->outsize = outbound_quota;
3576 req->insize = inbound_quota;
3577 req->timeout = timeout->QuadPart;
3578 wine_server_add_data( req, objattr, len );
3579 status = wine_server_call( req );
3580 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3582 SERVER_END_REQ;
3584 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3585 return status;
3588 /******************************************************************
3589 * NtDeleteFile (NTDLL.@)
3593 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3595 NTSTATUS status;
3596 HANDLE hFile;
3597 IO_STATUS_BLOCK io;
3599 TRACE("%p\n", ObjectAttributes);
3600 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3601 ObjectAttributes, &io, NULL, 0,
3602 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3603 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3604 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3605 return status;
3608 /******************************************************************
3609 * NtCancelIoFileEx (NTDLL.@)
3613 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3615 TRACE("%p %p %p\n", hFile, iosb, io_status );
3617 SERVER_START_REQ( cancel_async )
3619 req->handle = wine_server_obj_handle( hFile );
3620 req->iosb = wine_server_client_ptr( iosb );
3621 req->only_thread = FALSE;
3622 io_status->u.Status = wine_server_call( req );
3624 SERVER_END_REQ;
3626 return io_status->u.Status;
3629 /******************************************************************
3630 * NtCancelIoFile (NTDLL.@)
3634 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3636 TRACE("%p %p\n", hFile, io_status );
3638 SERVER_START_REQ( cancel_async )
3640 req->handle = wine_server_obj_handle( hFile );
3641 req->iosb = 0;
3642 req->only_thread = TRUE;
3643 io_status->u.Status = wine_server_call( req );
3645 SERVER_END_REQ;
3647 return io_status->u.Status;
3650 /******************************************************************************
3651 * NtCreateMailslotFile [NTDLL.@]
3652 * ZwCreateMailslotFile [NTDLL.@]
3654 * PARAMS
3655 * pHandle [O] pointer to receive the handle created
3656 * DesiredAccess [I] access mode (read, write, etc)
3657 * ObjectAttributes [I] fully qualified NT path of the mailslot
3658 * IoStatusBlock [O] receives completion status and other info
3659 * CreateOptions [I]
3660 * MailslotQuota [I]
3661 * MaxMessageSize [I]
3662 * TimeOut [I]
3664 * RETURNS
3665 * An NT status code
3667 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3668 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3669 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3670 PLARGE_INTEGER TimeOut)
3672 LARGE_INTEGER timeout;
3673 NTSTATUS ret;
3674 data_size_t len;
3675 struct object_attributes *objattr;
3677 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3678 pHandle, DesiredAccess, attr, IoStatusBlock,
3679 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3681 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3682 if (!attr) return STATUS_INVALID_PARAMETER;
3684 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3687 * For a NULL TimeOut pointer set the default timeout value
3689 if (!TimeOut)
3690 timeout.QuadPart = -1;
3691 else
3692 timeout.QuadPart = TimeOut->QuadPart;
3694 SERVER_START_REQ( create_mailslot )
3696 req->access = DesiredAccess;
3697 req->max_msgsize = MaxMessageSize;
3698 req->read_timeout = timeout.QuadPart;
3699 wine_server_add_data( req, objattr, len );
3700 ret = wine_server_call( req );
3701 if( ret == STATUS_SUCCESS )
3702 *pHandle = wine_server_ptr_handle( reply->handle );
3704 SERVER_END_REQ;
3706 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3707 return ret;