ntdll: Don't require full-page reads in NtReadFileScatter.
[wine.git] / dlls / ntdll / file.c
blob570e46c0a7a3bc3e182b3c1ea31d5fa7d62d8b2c
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 = virtual_locked_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 = virtual_locked_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 = virtual_locked_server_call( req );
581 wait_handle = wine_server_ptr_handle( reply->wait );
582 options = reply->options;
583 if (wait_handle && status != STATUS_PENDING)
585 io->u.Status = status;
586 io->Information = wine_server_reply_size( reply );
589 SERVER_END_REQ;
591 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
593 if (wait_handle)
595 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
596 status = io->u.Status;
599 return status;
602 /* do a write call through the server */
603 static NTSTATUS server_write_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
604 IO_STATUS_BLOCK *io, const void *buffer, ULONG size,
605 LARGE_INTEGER *offset, ULONG *key )
607 struct async_irp *async;
608 NTSTATUS status;
609 HANDLE wait_handle;
610 ULONG options;
612 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
613 return STATUS_NO_MEMORY;
615 async->event = event;
616 async->buffer = NULL;
617 async->size = 0;
619 SERVER_START_REQ( write )
621 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
622 req->pos = offset ? offset->QuadPart : 0;
623 wine_server_add_data( req, buffer, size );
624 status = wine_server_call( req );
625 wait_handle = wine_server_ptr_handle( reply->wait );
626 options = reply->options;
627 if (wait_handle && status != STATUS_PENDING)
629 io->u.Status = status;
630 io->Information = reply->size;
633 SERVER_END_REQ;
635 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
637 if (wait_handle)
639 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
640 status = io->u.Status;
643 return status;
646 struct io_timeouts
648 int interval; /* max interval between two bytes */
649 int total; /* total timeout for the whole operation */
650 int end_time; /* absolute time of end of operation */
653 /* retrieve the I/O timeouts to use for a given handle */
654 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
655 struct io_timeouts *timeouts )
657 NTSTATUS status = STATUS_SUCCESS;
659 timeouts->interval = timeouts->total = -1;
661 switch(type)
663 case FD_TYPE_SERIAL:
665 /* GetCommTimeouts */
666 SERIAL_TIMEOUTS st;
667 IO_STATUS_BLOCK io;
669 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
670 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
671 if (status) break;
673 if (is_read)
675 if (st.ReadIntervalTimeout)
676 timeouts->interval = st.ReadIntervalTimeout;
678 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
680 timeouts->total = st.ReadTotalTimeoutConstant;
681 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
682 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
684 else if (st.ReadIntervalTimeout == MAXDWORD)
685 timeouts->interval = timeouts->total = 0;
687 else /* write */
689 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
691 timeouts->total = st.WriteTotalTimeoutConstant;
692 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
693 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
697 break;
698 case FD_TYPE_MAILSLOT:
699 if (is_read)
701 timeouts->interval = 0; /* return as soon as we got something */
702 SERVER_START_REQ( set_mailslot_info )
704 req->handle = wine_server_obj_handle( handle );
705 req->flags = 0;
706 if (!(status = wine_server_call( req )) &&
707 reply->read_timeout != TIMEOUT_INFINITE)
708 timeouts->total = reply->read_timeout / -10000;
710 SERVER_END_REQ;
712 break;
713 case FD_TYPE_SOCKET:
714 case FD_TYPE_CHAR:
715 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
716 break;
717 default:
718 break;
720 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
721 return STATUS_SUCCESS;
725 /* retrieve the timeout for the next wait, in milliseconds */
726 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
728 int ret = -1;
730 if (timeouts->total != -1)
732 ret = timeouts->end_time - NtGetTickCount();
733 if (ret < 0) ret = 0;
735 if (already && timeouts->interval != -1)
737 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
739 return ret;
743 /* retrieve the avail_mode flag for async reads */
744 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
746 NTSTATUS status = STATUS_SUCCESS;
748 switch(type)
750 case FD_TYPE_SERIAL:
752 /* GetCommTimeouts */
753 SERIAL_TIMEOUTS st;
754 IO_STATUS_BLOCK io;
756 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
757 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
758 if (status) break;
759 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
760 !st.ReadTotalTimeoutConstant &&
761 st.ReadIntervalTimeout == MAXDWORD);
763 break;
764 case FD_TYPE_MAILSLOT:
765 case FD_TYPE_SOCKET:
766 case FD_TYPE_CHAR:
767 *avail_mode = TRUE;
768 break;
769 default:
770 *avail_mode = FALSE;
771 break;
773 return status;
776 /* register an async I/O for a file read; helper for NtReadFile */
777 static NTSTATUS register_async_file_read( HANDLE handle, HANDLE event,
778 PIO_APC_ROUTINE apc, void *apc_user,
779 IO_STATUS_BLOCK *iosb, void *buffer,
780 ULONG already, ULONG length, BOOL avail_mode )
782 struct async_fileio_read *fileio;
783 NTSTATUS status;
785 if (!(fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), FILE_AsyncReadService, handle )))
786 return STATUS_NO_MEMORY;
788 fileio->already = already;
789 fileio->count = length;
790 fileio->buffer = buffer;
791 fileio->avail_mode = avail_mode;
793 SERVER_START_REQ( register_async )
795 req->type = ASYNC_TYPE_READ;
796 req->count = length;
797 req->async = server_async( handle, &fileio->io, event, apc, apc_user, iosb );
798 status = wine_server_call( req );
800 SERVER_END_REQ;
802 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
803 return status;
807 /******************************************************************************
808 * NtReadFile [NTDLL.@]
809 * ZwReadFile [NTDLL.@]
811 * Read from an open file handle.
813 * PARAMS
814 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
815 * Event [I] Event to signal upon completion (or NULL)
816 * ApcRoutine [I] Callback to call upon completion (or NULL)
817 * ApcContext [I] Context for ApcRoutine (or NULL)
818 * IoStatusBlock [O] Receives information about the operation on return
819 * Buffer [O] Destination for the data read
820 * Length [I] Size of Buffer
821 * ByteOffset [O] Destination for the new file pointer position (or NULL)
822 * Key [O] Function unknown (may be NULL)
824 * RETURNS
825 * Success: 0. IoStatusBlock is updated, and the Information member contains
826 * The number of bytes read.
827 * Failure: An NTSTATUS error code describing the error.
829 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
830 PIO_APC_ROUTINE apc, void* apc_user,
831 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
832 PLARGE_INTEGER offset, PULONG key)
834 int result, unix_handle, needs_close;
835 unsigned int options;
836 struct io_timeouts timeouts;
837 NTSTATUS status;
838 ULONG total = 0;
839 enum server_fd_type type;
840 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
841 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
843 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
844 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
846 if (!io_status) return STATUS_ACCESS_VIOLATION;
848 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
849 &needs_close, &type, &options );
850 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
852 if (!virtual_check_buffer_for_write( buffer, length )) return STATUS_ACCESS_VIOLATION;
854 if (status == STATUS_BAD_DEVICE_TYPE)
855 return server_read_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
857 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
859 if (type == FD_TYPE_FILE)
861 if (async_read && (!offset || offset->QuadPart < 0))
863 status = STATUS_INVALID_PARAMETER;
864 goto done;
867 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
869 /* async I/O doesn't make sense on regular files */
870 while ((result = virtual_locked_pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
872 if (errno != EINTR)
874 status = FILE_GetNtStatus();
875 goto done;
878 if (!async_read)
879 /* update file pointer position */
880 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
882 total = result;
883 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
884 goto done;
887 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
889 if (async_read && (!offset || offset->QuadPart < 0))
891 status = STATUS_INVALID_PARAMETER;
892 goto done;
896 if (type == FD_TYPE_SERIAL && async_read && length)
898 /* an asynchronous serial port read with a read interval timeout needs to
899 skip the synchronous read to make sure that the server starts the read
900 interval timer after the first read */
901 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts ))) goto err;
902 if (timeouts.interval)
904 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
905 buffer, total, length, FALSE );
906 goto err;
910 for (;;)
912 if ((result = virtual_locked_read( unix_handle, (char *)buffer + total, length - total )) >= 0)
914 total += result;
915 if (!result || total == length)
917 if (total)
919 status = STATUS_SUCCESS;
920 goto done;
922 switch (type)
924 case FD_TYPE_FILE:
925 case FD_TYPE_CHAR:
926 case FD_TYPE_DEVICE:
927 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
928 goto done;
929 case FD_TYPE_SERIAL:
930 if (!length)
932 status = STATUS_SUCCESS;
933 goto done;
935 break;
936 default:
937 status = STATUS_PIPE_BROKEN;
938 goto err;
941 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
943 else if (errno != EAGAIN)
945 if (errno == EINTR) continue;
946 if (!total) status = FILE_GetNtStatus();
947 goto err;
950 if (async_read)
952 BOOL avail_mode;
954 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
955 goto err;
956 if (total && avail_mode)
958 status = STATUS_SUCCESS;
959 goto done;
961 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
962 buffer, total, length, avail_mode );
963 goto err;
965 else /* synchronous read, wait for the fd to become ready */
967 struct pollfd pfd;
968 int ret, timeout;
970 if (!timeout_init_done)
972 timeout_init_done = TRUE;
973 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
974 goto err;
975 if (hEvent) NtResetEvent( hEvent, NULL );
977 timeout = get_next_io_timeout( &timeouts, total );
979 pfd.fd = unix_handle;
980 pfd.events = POLLIN;
982 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
984 if (total) /* return with what we got so far */
985 status = STATUS_SUCCESS;
986 else
987 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
988 goto done;
990 if (ret == -1 && errno != EINTR)
992 status = FILE_GetNtStatus();
993 goto done;
995 /* will now restart the read */
999 done:
1000 send_completion = cvalue != 0;
1002 err:
1003 if (needs_close) close( unix_handle );
1004 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
1006 io_status->u.Status = status;
1007 io_status->Information = total;
1008 TRACE("= SUCCESS (%u)\n", total);
1009 if (hEvent) NtSetEvent( hEvent, NULL );
1010 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1011 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1013 else
1015 TRACE("= 0x%08x\n", status);
1016 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1019 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1021 return status;
1025 /******************************************************************************
1026 * NtReadFileScatter [NTDLL.@]
1027 * ZwReadFileScatter [NTDLL.@]
1029 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1030 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1031 ULONG length, PLARGE_INTEGER offset, PULONG key )
1033 int result, unix_handle, needs_close;
1034 unsigned int options;
1035 NTSTATUS status;
1036 ULONG pos = 0, total = 0;
1037 enum server_fd_type type;
1038 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1039 BOOL send_completion = FALSE;
1041 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1042 file, event, apc, apc_user, io_status, segments, length, offset, key);
1044 if (!io_status) return STATUS_ACCESS_VIOLATION;
1046 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1047 &needs_close, &type, &options );
1048 if (status) return status;
1050 if ((type != FD_TYPE_FILE) ||
1051 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1052 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1054 status = STATUS_INVALID_PARAMETER;
1055 goto error;
1058 while (length)
1060 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1061 result = pread( unix_handle, (char *)segments->Buffer + pos,
1062 min( length - pos, page_size - pos ), offset->QuadPart + total );
1063 else
1064 result = read( unix_handle, (char *)segments->Buffer + pos, min( length - pos, page_size - pos ) );
1066 if (result == -1)
1068 if (errno == EINTR) continue;
1069 status = FILE_GetNtStatus();
1070 break;
1072 if (!result) break;
1073 total += result;
1074 length -= result;
1075 if ((pos += result) == page_size)
1077 pos = 0;
1078 segments++;
1082 if (total == 0) status = STATUS_END_OF_FILE;
1084 send_completion = cvalue != 0;
1086 if (needs_close) close( unix_handle );
1088 io_status->u.Status = status;
1089 io_status->Information = total;
1090 TRACE("= 0x%08x (%u)\n", status, total);
1091 if (event) NtSetEvent( event, NULL );
1092 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1093 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1094 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1096 return STATUS_PENDING;
1098 error:
1099 if (needs_close) close( unix_handle );
1101 TRACE("= 0x%08x\n", status);
1102 if (event) NtResetEvent( event, NULL );
1104 return status;
1108 /***********************************************************************
1109 * FILE_AsyncWriteService (INTERNAL)
1111 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1113 struct async_fileio_write *fileio = user;
1114 int result, fd, needs_close;
1115 enum server_fd_type type;
1117 switch (status)
1119 case STATUS_ALERTED:
1120 /* write some data (non-blocking) */
1121 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1122 &needs_close, &type, NULL )))
1123 break;
1125 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1126 result = send( fd, fileio->buffer, 0, 0 );
1127 else
1128 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1130 if (needs_close) close( fd );
1132 if (result < 0)
1134 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1135 else status = FILE_GetNtStatus();
1137 else
1139 fileio->already += result;
1140 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1142 break;
1144 case STATUS_TIMEOUT:
1145 case STATUS_IO_TIMEOUT:
1146 if (fileio->already) status = STATUS_SUCCESS;
1147 break;
1149 if (status != STATUS_PENDING)
1151 iosb->u.Status = status;
1152 iosb->Information = fileio->already;
1153 release_fileio( &fileio->io );
1155 return status;
1158 static NTSTATUS set_pending_write( HANDLE device )
1160 NTSTATUS status;
1162 SERVER_START_REQ( set_serial_info )
1164 req->handle = wine_server_obj_handle( device );
1165 req->flags = SERIALINFO_PENDING_WRITE;
1166 status = wine_server_call( req );
1168 SERVER_END_REQ;
1169 return status;
1172 /******************************************************************************
1173 * NtWriteFile [NTDLL.@]
1174 * ZwWriteFile [NTDLL.@]
1176 * Write to an open file handle.
1178 * PARAMS
1179 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1180 * Event [I] Event to signal upon completion (or NULL)
1181 * ApcRoutine [I] Callback to call upon completion (or NULL)
1182 * ApcContext [I] Context for ApcRoutine (or NULL)
1183 * IoStatusBlock [O] Receives information about the operation on return
1184 * Buffer [I] Source for the data to write
1185 * Length [I] Size of Buffer
1186 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1187 * Key [O] Function unknown (may be NULL)
1189 * RETURNS
1190 * Success: 0. IoStatusBlock is updated, and the Information member contains
1191 * The number of bytes written.
1192 * Failure: An NTSTATUS error code describing the error.
1194 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1195 PIO_APC_ROUTINE apc, void* apc_user,
1196 PIO_STATUS_BLOCK io_status,
1197 const void* buffer, ULONG length,
1198 PLARGE_INTEGER offset, PULONG key)
1200 int result, unix_handle, needs_close;
1201 unsigned int options;
1202 struct io_timeouts timeouts;
1203 NTSTATUS status;
1204 ULONG total = 0;
1205 enum server_fd_type type;
1206 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1207 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1208 LARGE_INTEGER offset_eof;
1210 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1211 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1213 if (!io_status) return STATUS_ACCESS_VIOLATION;
1215 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1216 &needs_close, &type, &options );
1217 if (status == STATUS_ACCESS_DENIED)
1219 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1220 &needs_close, &type, &options );
1221 append_write = TRUE;
1223 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
1225 if (!virtual_check_buffer_for_read( buffer, length ))
1227 status = STATUS_INVALID_USER_BUFFER;
1228 goto done;
1231 if (status == STATUS_BAD_DEVICE_TYPE)
1232 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1234 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1236 if (type == FD_TYPE_FILE)
1238 if (async_write &&
1239 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1241 status = STATUS_INVALID_PARAMETER;
1242 goto done;
1245 if (append_write)
1247 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1248 offset = &offset_eof;
1251 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1253 off_t off = offset->QuadPart;
1255 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1257 struct stat st;
1259 if (fstat( unix_handle, &st ) == -1)
1261 status = FILE_GetNtStatus();
1262 goto done;
1264 off = st.st_size;
1266 else if (offset->QuadPart < 0)
1268 status = STATUS_INVALID_PARAMETER;
1269 goto done;
1272 /* async I/O doesn't make sense on regular files */
1273 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1275 if (errno != EINTR)
1277 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1278 else status = FILE_GetNtStatus();
1279 goto done;
1283 if (!async_write)
1284 /* update file pointer position */
1285 lseek( unix_handle, off + result, SEEK_SET );
1287 total = result;
1288 status = STATUS_SUCCESS;
1289 goto done;
1292 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1294 if (async_write &&
1295 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1297 status = STATUS_INVALID_PARAMETER;
1298 goto done;
1302 for (;;)
1304 /* zero-length writes on sockets may not work with plain write(2) */
1305 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1306 result = send( unix_handle, buffer, 0, 0 );
1307 else
1308 result = write( unix_handle, (const char *)buffer + total, length - total );
1310 if (result >= 0)
1312 total += result;
1313 if (total == length)
1315 status = STATUS_SUCCESS;
1316 goto done;
1318 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1320 else if (errno != EAGAIN)
1322 if (errno == EINTR) continue;
1323 if (!total)
1325 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1326 else status = FILE_GetNtStatus();
1328 goto err;
1331 if (async_write)
1333 struct async_fileio_write *fileio;
1335 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), FILE_AsyncWriteService, hFile );
1336 if (!fileio)
1338 status = STATUS_NO_MEMORY;
1339 goto err;
1341 fileio->already = total;
1342 fileio->count = length;
1343 fileio->buffer = buffer;
1345 SERVER_START_REQ( register_async )
1347 req->type = ASYNC_TYPE_WRITE;
1348 req->count = length;
1349 req->async = server_async( hFile, &fileio->io, hEvent, apc, apc_user, io_status );
1350 status = wine_server_call( req );
1352 SERVER_END_REQ;
1354 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1355 goto err;
1357 else /* synchronous write, wait for the fd to become ready */
1359 struct pollfd pfd;
1360 int ret, timeout;
1362 if (!timeout_init_done)
1364 timeout_init_done = TRUE;
1365 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1366 goto err;
1367 if (hEvent) NtResetEvent( hEvent, NULL );
1369 timeout = get_next_io_timeout( &timeouts, total );
1371 pfd.fd = unix_handle;
1372 pfd.events = POLLOUT;
1374 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1376 /* return with what we got so far */
1377 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1378 goto done;
1380 if (ret == -1 && errno != EINTR)
1382 status = FILE_GetNtStatus();
1383 goto done;
1385 /* will now restart the write */
1389 done:
1390 send_completion = cvalue != 0;
1392 err:
1393 if (needs_close) close( unix_handle );
1395 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1396 set_pending_write( hFile );
1398 if (status == STATUS_SUCCESS)
1400 io_status->u.Status = status;
1401 io_status->Information = total;
1402 TRACE("= SUCCESS (%u)\n", total);
1403 if (hEvent) NtSetEvent( hEvent, NULL );
1404 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1405 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1407 else
1409 TRACE("= 0x%08x\n", status);
1410 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1413 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1415 return status;
1419 /******************************************************************************
1420 * NtWriteFileGather [NTDLL.@]
1421 * ZwWriteFileGather [NTDLL.@]
1423 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1424 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1425 ULONG length, PLARGE_INTEGER offset, PULONG key )
1427 int result, unix_handle, needs_close;
1428 unsigned int options;
1429 NTSTATUS status;
1430 ULONG pos = 0, total = 0;
1431 enum server_fd_type type;
1432 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1433 BOOL send_completion = FALSE;
1435 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1436 file, event, apc, apc_user, io_status, segments, length, offset, key);
1438 if (length % page_size) return STATUS_INVALID_PARAMETER;
1439 if (!io_status) return STATUS_ACCESS_VIOLATION;
1441 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1442 &needs_close, &type, &options );
1443 if (status) return status;
1445 if ((type != FD_TYPE_FILE) ||
1446 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1447 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1449 status = STATUS_INVALID_PARAMETER;
1450 goto error;
1453 while (length)
1455 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1456 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1457 page_size - pos, offset->QuadPart + total );
1458 else
1459 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1461 if (result == -1)
1463 if (errno == EINTR) continue;
1464 if (errno == EFAULT)
1466 status = STATUS_INVALID_USER_BUFFER;
1467 goto error;
1469 status = FILE_GetNtStatus();
1470 break;
1472 if (!result)
1474 status = STATUS_DISK_FULL;
1475 break;
1477 total += result;
1478 length -= result;
1479 if ((pos += result) == page_size)
1481 pos = 0;
1482 segments++;
1486 send_completion = cvalue != 0;
1488 error:
1489 if (needs_close) close( unix_handle );
1490 if (status == STATUS_SUCCESS)
1492 io_status->u.Status = status;
1493 io_status->Information = total;
1494 TRACE("= SUCCESS (%u)\n", total);
1495 if (event) NtSetEvent( event, NULL );
1496 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1497 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1499 else
1501 TRACE("= 0x%08x\n", status);
1502 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1505 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1507 return status;
1511 /* do an ioctl call through the server */
1512 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1513 PIO_APC_ROUTINE apc, PVOID apc_context,
1514 IO_STATUS_BLOCK *io, ULONG code,
1515 const void *in_buffer, ULONG in_size,
1516 PVOID out_buffer, ULONG out_size )
1518 struct async_irp *async;
1519 NTSTATUS status;
1520 HANDLE wait_handle;
1521 ULONG options;
1523 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
1524 return STATUS_NO_MEMORY;
1525 async->event = event;
1526 async->buffer = out_buffer;
1527 async->size = out_size;
1529 SERVER_START_REQ( ioctl )
1531 req->code = code;
1532 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
1533 wine_server_add_data( req, in_buffer, in_size );
1534 if ((code & 3) != METHOD_BUFFERED)
1535 wine_server_add_data( req, out_buffer, out_size );
1536 wine_server_set_reply( req, out_buffer, out_size );
1537 status = virtual_locked_server_call( req );
1538 wait_handle = wine_server_ptr_handle( reply->wait );
1539 options = reply->options;
1540 if (wait_handle && status != STATUS_PENDING)
1542 io->u.Status = status;
1543 io->Information = wine_server_reply_size( reply );
1546 SERVER_END_REQ;
1548 if (status == STATUS_NOT_SUPPORTED)
1549 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1550 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1552 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1554 if (wait_handle)
1556 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1557 status = io->u.Status;
1560 return status;
1563 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1564 * server */
1565 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1566 ULONG in_size)
1568 #ifdef VALGRIND_MAKE_MEM_DEFINED
1569 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1570 do { \
1571 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1572 if ((size) >= FIELD_OFFSET(t, f2)) \
1573 VALGRIND_MAKE_MEM_DEFINED( \
1574 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1575 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1576 } while (0)
1578 switch (code)
1580 case FSCTL_PIPE_WAIT:
1581 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1582 break;
1584 #endif
1588 /**************************************************************************
1589 * NtDeviceIoControlFile [NTDLL.@]
1590 * ZwDeviceIoControlFile [NTDLL.@]
1592 * Perform an I/O control operation on an open file handle.
1594 * PARAMS
1595 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1596 * event [I] Event to signal upon completion (or NULL)
1597 * apc [I] Callback to call upon completion (or NULL)
1598 * apc_context [I] Context for ApcRoutine (or NULL)
1599 * io [O] Receives information about the operation on return
1600 * code [I] Control code for the operation to perform
1601 * in_buffer [I] Source for any input data required (or NULL)
1602 * in_size [I] Size of InputBuffer
1603 * out_buffer [O] Source for any output data returned (or NULL)
1604 * out_size [I] Size of OutputBuffer
1606 * RETURNS
1607 * Success: 0. IoStatusBlock is updated.
1608 * Failure: An NTSTATUS error code describing the error.
1610 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1611 PIO_APC_ROUTINE apc, PVOID apc_context,
1612 PIO_STATUS_BLOCK io, ULONG code,
1613 PVOID in_buffer, ULONG in_size,
1614 PVOID out_buffer, ULONG out_size)
1616 ULONG device = (code >> 16);
1617 NTSTATUS status = STATUS_NOT_SUPPORTED;
1619 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1620 handle, event, apc, apc_context, io, code,
1621 in_buffer, in_size, out_buffer, out_size);
1623 switch(device)
1625 case FILE_DEVICE_DISK:
1626 case FILE_DEVICE_CD_ROM:
1627 case FILE_DEVICE_DVD:
1628 case FILE_DEVICE_CONTROLLER:
1629 case FILE_DEVICE_MASS_STORAGE:
1630 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1631 in_buffer, in_size, out_buffer, out_size);
1632 break;
1633 case FILE_DEVICE_SERIAL_PORT:
1634 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1635 in_buffer, in_size, out_buffer, out_size);
1636 break;
1637 case FILE_DEVICE_TAPE:
1638 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1639 in_buffer, in_size, out_buffer, out_size);
1640 break;
1643 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1644 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1645 in_buffer, in_size, out_buffer, out_size );
1647 if (status != STATUS_PENDING) io->u.Status = status;
1648 return status;
1652 /**************************************************************************
1653 * NtFsControlFile [NTDLL.@]
1654 * ZwFsControlFile [NTDLL.@]
1656 * Perform a file system control operation on an open file handle.
1658 * PARAMS
1659 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1660 * event [I] Event to signal upon completion (or NULL)
1661 * apc [I] Callback to call upon completion (or NULL)
1662 * apc_context [I] Context for ApcRoutine (or NULL)
1663 * io [O] Receives information about the operation on return
1664 * code [I] Control code for the operation to perform
1665 * in_buffer [I] Source for any input data required (or NULL)
1666 * in_size [I] Size of InputBuffer
1667 * out_buffer [O] Source for any output data returned (or NULL)
1668 * out_size [I] Size of OutputBuffer
1670 * RETURNS
1671 * Success: 0. IoStatusBlock is updated.
1672 * Failure: An NTSTATUS error code describing the error.
1674 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1675 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1676 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1678 NTSTATUS status;
1680 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1681 handle, event, apc, apc_context, io, code,
1682 in_buffer, in_size, out_buffer, out_size);
1684 if (!io) return STATUS_INVALID_PARAMETER;
1686 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1688 switch(code)
1690 case FSCTL_DISMOUNT_VOLUME:
1691 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1692 in_buffer, in_size, out_buffer, out_size );
1693 if (!status) status = DIR_unmount_device( handle );
1694 return status;
1696 case FSCTL_PIPE_DISCONNECT:
1697 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1698 in_buffer, in_size, out_buffer, out_size );
1699 if (!status)
1701 int fd = server_remove_fd_from_cache( handle );
1702 if (fd != -1) close( fd );
1704 return status;
1706 case FSCTL_PIPE_IMPERSONATE:
1707 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1708 status = RtlImpersonateSelf( SecurityImpersonation );
1709 break;
1711 case FSCTL_IS_VOLUME_MOUNTED:
1712 case FSCTL_LOCK_VOLUME:
1713 case FSCTL_UNLOCK_VOLUME:
1714 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1715 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1716 status = STATUS_SUCCESS;
1717 break;
1719 case FSCTL_GET_RETRIEVAL_POINTERS:
1721 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1723 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1725 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1727 buffer->ExtentCount = 1;
1728 buffer->StartingVcn.QuadPart = 1;
1729 buffer->Extents[0].NextVcn.QuadPart = 0;
1730 buffer->Extents[0].Lcn.QuadPart = 0;
1731 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1732 status = STATUS_SUCCESS;
1734 else
1736 io->Information = 0;
1737 status = STATUS_BUFFER_TOO_SMALL;
1739 break;
1741 case FSCTL_SET_SPARSE:
1742 TRACE("FSCTL_SET_SPARSE: Ignoring request\n");
1743 io->Information = 0;
1744 status = STATUS_SUCCESS;
1745 break;
1746 default:
1747 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1748 in_buffer, in_size, out_buffer, out_size );
1751 if (status != STATUS_PENDING) io->u.Status = status;
1752 return status;
1756 struct read_changes_fileio
1758 struct async_fileio io;
1759 void *buffer;
1760 ULONG buffer_size;
1761 ULONG data_size;
1762 char data[1];
1765 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1767 struct read_changes_fileio *fileio = user;
1768 int size = 0;
1770 if (status == STATUS_ALERTED)
1772 SERVER_START_REQ( read_change )
1774 req->handle = wine_server_obj_handle( fileio->io.handle );
1775 wine_server_set_reply( req, fileio->data, fileio->data_size );
1776 status = wine_server_call( req );
1777 size = wine_server_reply_size( reply );
1779 SERVER_END_REQ;
1781 if (status == STATUS_SUCCESS && fileio->buffer)
1783 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1784 int i, left = fileio->buffer_size;
1785 DWORD *last_entry_offset = NULL;
1786 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1788 while (size && left >= sizeof(*pfni))
1790 /* convert to an NT style path */
1791 for (i = 0; i < event->len; i++)
1792 if (event->name[i] == '/') event->name[i] = '\\';
1794 pfni->Action = event->action;
1795 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1796 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1797 last_entry_offset = &pfni->NextEntryOffset;
1799 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1801 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1802 pfni->FileNameLength *= sizeof(WCHAR);
1803 pfni->NextEntryOffset = i;
1804 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1805 left -= i;
1807 i = (offsetof(struct filesystem_event, name[event->len])
1808 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1809 event = (struct filesystem_event*)((char*)event + i);
1810 size -= i;
1813 if (size)
1815 status = STATUS_NOTIFY_ENUM_DIR;
1816 size = 0;
1818 else
1820 if (last_entry_offset) *last_entry_offset = 0;
1821 size = fileio->buffer_size - left;
1824 else
1826 status = STATUS_NOTIFY_ENUM_DIR;
1827 size = 0;
1831 if (status != STATUS_PENDING)
1833 iosb->u.Status = status;
1834 iosb->Information = size;
1835 release_fileio( &fileio->io );
1837 return status;
1840 #define FILE_NOTIFY_ALL ( \
1841 FILE_NOTIFY_CHANGE_FILE_NAME | \
1842 FILE_NOTIFY_CHANGE_DIR_NAME | \
1843 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1844 FILE_NOTIFY_CHANGE_SIZE | \
1845 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1846 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1847 FILE_NOTIFY_CHANGE_CREATION | \
1848 FILE_NOTIFY_CHANGE_SECURITY )
1850 /******************************************************************************
1851 * NtNotifyChangeDirectoryFile [NTDLL.@]
1853 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1854 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1855 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1857 struct read_changes_fileio *fileio;
1858 NTSTATUS status;
1859 ULONG size = max( 4096, buffer_size );
1861 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1862 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1864 if (!iosb) return STATUS_ACCESS_VIOLATION;
1865 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1867 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1868 read_changes_apc, handle );
1869 if (!fileio) return STATUS_NO_MEMORY;
1871 fileio->buffer = buffer;
1872 fileio->buffer_size = buffer_size;
1873 fileio->data_size = size;
1875 SERVER_START_REQ( read_directory_changes )
1877 req->filter = filter;
1878 req->want_data = (buffer != NULL);
1879 req->subtree = subtree;
1880 req->async = server_async( handle, &fileio->io, event, apc, apc_context, iosb );
1881 status = wine_server_call( req );
1883 SERVER_END_REQ;
1885 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1886 return status;
1889 /******************************************************************************
1890 * NtSetVolumeInformationFile [NTDLL.@]
1891 * ZwSetVolumeInformationFile [NTDLL.@]
1893 * Set volume information for an open file handle.
1895 * PARAMS
1896 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1897 * IoStatusBlock [O] Receives information about the operation on return
1898 * FsInformation [I] Source for volume information
1899 * Length [I] Size of FsInformation
1900 * FsInformationClass [I] Type of volume information to set
1902 * RETURNS
1903 * Success: 0. IoStatusBlock is updated.
1904 * Failure: An NTSTATUS error code describing the error.
1906 NTSTATUS WINAPI NtSetVolumeInformationFile(
1907 IN HANDLE FileHandle,
1908 PIO_STATUS_BLOCK IoStatusBlock,
1909 PVOID FsInformation,
1910 ULONG Length,
1911 FS_INFORMATION_CLASS FsInformationClass)
1913 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1914 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1915 return 0;
1918 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1919 static int futimens( int fd, const struct timespec spec[2] )
1921 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1923 #define HAVE_FUTIMENS
1924 #endif /* __ANDROID__ */
1926 #ifndef UTIME_OMIT
1927 #define UTIME_OMIT ((1 << 30) - 2)
1928 #endif
1930 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1932 NTSTATUS status = STATUS_SUCCESS;
1934 #ifdef HAVE_FUTIMENS
1935 struct timespec tv[2];
1937 tv[0].tv_sec = tv[1].tv_sec = 0;
1938 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1939 if (atime->QuadPart)
1941 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1942 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1944 if (mtime->QuadPart)
1946 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1947 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1949 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1951 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1952 struct timeval tv[2];
1953 struct stat st;
1955 if (!atime->QuadPart || !mtime->QuadPart)
1958 tv[0].tv_sec = tv[0].tv_usec = 0;
1959 tv[1].tv_sec = tv[1].tv_usec = 0;
1960 if (!fstat( fd, &st ))
1962 tv[0].tv_sec = st.st_atime;
1963 tv[1].tv_sec = st.st_mtime;
1964 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1965 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1966 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1967 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1968 #endif
1969 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1970 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1971 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1972 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1973 #endif
1976 if (atime->QuadPart)
1978 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1979 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1981 if (mtime->QuadPart)
1983 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1984 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1986 #ifdef HAVE_FUTIMES
1987 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1988 #elif defined(HAVE_FUTIMESAT)
1989 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1990 #endif
1992 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1993 FIXME( "setting file times not supported\n" );
1994 status = STATUS_NOT_IMPLEMENTED;
1995 #endif
1996 return status;
1999 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
2000 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
2002 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
2003 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
2004 RtlSecondsSince1970ToTime( st->st_atime, atime );
2005 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2006 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
2007 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2008 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
2009 #endif
2010 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2011 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2012 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2013 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2014 #endif
2015 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2016 atime->QuadPart += st->st_atim.tv_nsec / 100;
2017 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2018 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2019 #endif
2020 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2021 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2022 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2023 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2024 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2025 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2026 #endif
2027 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2028 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2029 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2030 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2031 #endif
2032 #else
2033 *creation = *mtime;
2034 #endif
2037 /* fill in the file information that depends on the stat and attribute info */
2038 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2039 FILE_INFORMATION_CLASS class )
2041 switch (class)
2043 case FileBasicInformation:
2045 FILE_BASIC_INFORMATION *info = ptr;
2047 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2048 &info->LastAccessTime, &info->CreationTime );
2049 info->FileAttributes = attr;
2051 break;
2052 case FileStandardInformation:
2054 FILE_STANDARD_INFORMATION *info = ptr;
2056 if ((info->Directory = S_ISDIR(st->st_mode)))
2058 info->AllocationSize.QuadPart = 0;
2059 info->EndOfFile.QuadPart = 0;
2060 info->NumberOfLinks = 1;
2062 else
2064 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2065 info->EndOfFile.QuadPart = st->st_size;
2066 info->NumberOfLinks = st->st_nlink;
2069 break;
2070 case FileInternalInformation:
2072 FILE_INTERNAL_INFORMATION *info = ptr;
2073 info->IndexNumber.QuadPart = st->st_ino;
2075 break;
2076 case FileEndOfFileInformation:
2078 FILE_END_OF_FILE_INFORMATION *info = ptr;
2079 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2081 break;
2082 case FileAllInformation:
2084 FILE_ALL_INFORMATION *info = ptr;
2085 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2086 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2087 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2089 break;
2090 /* all directory structures start with the FileDirectoryInformation layout */
2091 case FileBothDirectoryInformation:
2092 case FileFullDirectoryInformation:
2093 case FileDirectoryInformation:
2095 FILE_DIRECTORY_INFORMATION *info = ptr;
2097 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2098 &info->LastAccessTime, &info->CreationTime );
2099 if (S_ISDIR(st->st_mode))
2101 info->AllocationSize.QuadPart = 0;
2102 info->EndOfFile.QuadPart = 0;
2104 else
2106 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2107 info->EndOfFile.QuadPart = st->st_size;
2109 info->FileAttributes = attr;
2111 break;
2112 case FileIdFullDirectoryInformation:
2114 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2115 info->FileId.QuadPart = st->st_ino;
2116 fill_file_info( st, attr, info, FileDirectoryInformation );
2118 break;
2119 case FileIdBothDirectoryInformation:
2121 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2122 info->FileId.QuadPart = st->st_ino;
2123 fill_file_info( st, attr, info, FileDirectoryInformation );
2125 break;
2126 case FileIdGlobalTxDirectoryInformation:
2128 FILE_ID_GLOBAL_TX_DIR_INFORMATION *info = ptr;
2129 info->FileId.QuadPart = st->st_ino;
2130 fill_file_info( st, attr, info, FileDirectoryInformation );
2132 break;
2134 default:
2135 return STATUS_INVALID_INFO_CLASS;
2137 return STATUS_SUCCESS;
2140 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2142 data_size_t size = 1024;
2143 NTSTATUS ret;
2144 char *name;
2146 for (;;)
2148 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2149 if (!name) return STATUS_NO_MEMORY;
2150 unix_name->MaximumLength = size + 1;
2152 SERVER_START_REQ( get_handle_unix_name )
2154 req->handle = wine_server_obj_handle( handle );
2155 wine_server_set_reply( req, name, size );
2156 ret = wine_server_call( req );
2157 size = reply->name_len;
2159 SERVER_END_REQ;
2161 if (!ret)
2163 name[size] = 0;
2164 unix_name->Buffer = name;
2165 unix_name->Length = size;
2166 break;
2168 RtlFreeHeap( GetProcessHeap(), 0, name );
2169 if (ret != STATUS_BUFFER_OVERFLOW) break;
2171 return ret;
2174 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2176 UNICODE_STRING nt_name;
2177 NTSTATUS status;
2179 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2181 const WCHAR *ptr = nt_name.Buffer;
2182 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2184 /* Skip the volume mount point. */
2185 while (ptr != end && *ptr == '\\') ++ptr;
2186 while (ptr != end && *ptr != '\\') ++ptr;
2187 while (ptr != end && *ptr == '\\') ++ptr;
2188 while (ptr != end && *ptr != '\\') ++ptr;
2190 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2191 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2192 else *name_len = info->FileNameLength;
2194 memcpy( info->FileName, ptr, *name_len );
2195 RtlFreeUnicodeString( &nt_name );
2198 return status;
2201 /******************************************************************************
2202 * NtQueryInformationFile [NTDLL.@]
2203 * ZwQueryInformationFile [NTDLL.@]
2205 * Get information about an open file handle.
2207 * PARAMS
2208 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2209 * io [O] Receives information about the operation on return
2210 * ptr [O] Destination for file information
2211 * len [I] Size of FileInformation
2212 * class [I] Type of file information to get
2214 * RETURNS
2215 * Success: 0. IoStatusBlock and FileInformation are updated.
2216 * Failure: An NTSTATUS error code describing the error.
2218 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2219 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2221 static const size_t info_sizes[] =
2224 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2225 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2226 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2227 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2228 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2229 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2230 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2231 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2232 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2233 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2234 0, /* FileLinkInformation */
2235 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2236 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2237 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2238 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2239 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2240 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2241 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2242 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2243 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2244 0, /* FileAlternateNameInformation */
2245 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2246 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2247 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2248 0, /* FilePipeRemoteInformation */
2249 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2250 0, /* FileMailslotSetInformation */
2251 0, /* FileCompressionInformation */
2252 0, /* FileObjectIdInformation */
2253 0, /* FileCompletionInformation */
2254 0, /* FileMoveClusterInformation */
2255 0, /* FileQuotaInformation */
2256 0, /* FileReparsePointInformation */
2257 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2258 0, /* FileAttributeTagInformation */
2259 0, /* FileTrackingInformation */
2260 0, /* FileIdBothDirectoryInformation */
2261 0, /* FileIdFullDirectoryInformation */
2262 0, /* FileValidDataLengthInformation */
2263 0, /* FileShortNameInformation */
2264 0, /* FileIoCompletionNotificationInformation, */
2265 0, /* FileIoStatusBlockRangeInformation */
2266 0, /* FileIoPriorityHintInformation */
2267 0, /* FileSfioReserveInformation */
2268 0, /* FileSfioVolumeInformation */
2269 0, /* FileHardLinkInformation */
2270 0, /* FileProcessIdsUsingFileInformation */
2271 0, /* FileNormalizedNameInformation */
2272 0, /* FileNetworkPhysicalNameInformation */
2273 0, /* FileIdGlobalTxDirectoryInformation */
2274 0, /* FileIsRemoteDeviceInformation */
2275 0, /* FileAttributeCacheInformation */
2276 0, /* FileNumaNodeInformation */
2277 0, /* FileStandardLinkInformation */
2278 0, /* FileRemoteProtocolInformation */
2279 0, /* FileRenameInformationBypassAccessCheck */
2280 0, /* FileLinkInformationBypassAccessCheck */
2281 0, /* FileVolumeNameInformation */
2282 sizeof(FILE_ID_INFORMATION), /* FileIdInformation */
2283 0, /* FileIdExtdDirectoryInformation */
2284 0, /* FileReplaceCompletionInformation */
2285 0, /* FileHardLinkFullIdInformation */
2286 0, /* FileIdExtdBothDirectoryInformation */
2289 struct stat st;
2290 int fd, needs_close = FALSE;
2291 ULONG attr;
2293 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2295 io->Information = 0;
2297 if (class <= 0 || class >= FileMaximumInformation)
2298 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2299 if (!info_sizes[class])
2301 FIXME("Unsupported class (%d)\n", class);
2302 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2304 if (len < info_sizes[class])
2305 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2307 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2309 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2310 return io->u.Status;
2313 switch (class)
2315 case FileBasicInformation:
2316 if (fd_get_file_info( fd, &st, &attr ) == -1)
2317 io->u.Status = FILE_GetNtStatus();
2318 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2319 io->u.Status = STATUS_INVALID_INFO_CLASS;
2320 else
2321 fill_file_info( &st, attr, ptr, class );
2322 break;
2323 case FileStandardInformation:
2325 FILE_STANDARD_INFORMATION *info = ptr;
2327 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2328 else
2330 fill_file_info( &st, attr, info, class );
2331 info->DeletePending = FALSE; /* FIXME */
2334 break;
2335 case FilePositionInformation:
2337 FILE_POSITION_INFORMATION *info = ptr;
2338 off_t res = lseek( fd, 0, SEEK_CUR );
2339 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2340 else info->CurrentByteOffset.QuadPart = res;
2342 break;
2343 case FileInternalInformation:
2344 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2345 else fill_file_info( &st, attr, ptr, class );
2346 break;
2347 case FileEaInformation:
2349 FILE_EA_INFORMATION *info = ptr;
2350 info->EaSize = 0;
2352 break;
2353 case FileAccessInformation:
2355 FILE_ACCESS_INFORMATION *info = ptr;
2356 SERVER_START_REQ( get_object_info )
2358 req->handle = wine_server_obj_handle( hFile );
2359 io->u.Status = wine_server_call( req );
2360 if (io->u.Status == STATUS_SUCCESS)
2361 info->AccessFlags = reply->access;
2363 SERVER_END_REQ;
2365 break;
2366 case FileEndOfFileInformation:
2367 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2368 else fill_file_info( &st, attr, ptr, class );
2369 break;
2370 case FileAllInformation:
2372 FILE_ALL_INFORMATION *info = ptr;
2373 ANSI_STRING unix_name;
2375 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2376 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2377 io->u.Status = STATUS_INVALID_INFO_CLASS;
2378 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2380 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2382 fill_file_info( &st, attr, info, FileAllInformation );
2383 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2384 info->EaInformation.EaSize = 0;
2385 info->AccessInformation.AccessFlags = 0; /* FIXME */
2386 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2387 info->ModeInformation.Mode = 0; /* FIXME */
2388 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2390 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2391 RtlFreeAnsiString( &unix_name );
2392 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2395 break;
2396 case FileMailslotQueryInformation:
2398 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2400 SERVER_START_REQ( set_mailslot_info )
2402 req->handle = wine_server_obj_handle( hFile );
2403 req->flags = 0;
2404 io->u.Status = wine_server_call( req );
2405 if( io->u.Status == STATUS_SUCCESS )
2407 info->MaximumMessageSize = reply->max_msgsize;
2408 info->MailslotQuota = 0;
2409 info->NextMessageSize = 0;
2410 info->MessagesAvailable = 0;
2411 info->ReadTimeout.QuadPart = reply->read_timeout;
2414 SERVER_END_REQ;
2415 if (!io->u.Status)
2417 char *tmpbuf;
2418 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2419 if (size > 0x10000) size = 0x10000;
2420 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2422 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2424 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2425 info->MessagesAvailable = (res > 0);
2426 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2427 if (needs_close) close( fd );
2429 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2433 break;
2434 case FilePipeInformation:
2436 FILE_PIPE_INFORMATION* pi = ptr;
2438 SERVER_START_REQ( get_named_pipe_info )
2440 req->handle = wine_server_obj_handle( hFile );
2441 if (!(io->u.Status = wine_server_call( req )))
2443 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2444 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2445 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2446 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2449 SERVER_END_REQ;
2451 break;
2452 case FilePipeLocalInformation:
2454 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2456 SERVER_START_REQ( get_named_pipe_info )
2458 req->handle = wine_server_obj_handle( hFile );
2459 if (!(io->u.Status = wine_server_call( req )))
2461 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2462 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2463 switch (reply->sharing)
2465 case FILE_SHARE_READ:
2466 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2467 break;
2468 case FILE_SHARE_WRITE:
2469 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2470 break;
2471 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2472 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2473 break;
2475 pli->MaximumInstances = reply->maxinstances;
2476 pli->CurrentInstances = reply->instances;
2477 pli->InboundQuota = reply->insize;
2478 pli->ReadDataAvailable = 0; /* FIXME */
2479 pli->OutboundQuota = reply->outsize;
2480 pli->WriteQuotaAvailable = 0; /* FIXME */
2481 pli->NamedPipeState = 0; /* FIXME */
2482 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2483 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2486 SERVER_END_REQ;
2488 break;
2489 case FileNameInformation:
2491 FILE_NAME_INFORMATION *info = ptr;
2492 ANSI_STRING unix_name;
2494 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2496 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2497 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2498 RtlFreeAnsiString( &unix_name );
2499 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2502 break;
2503 case FileNetworkOpenInformation:
2505 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2506 ANSI_STRING unix_name;
2508 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2510 ULONG attributes;
2511 struct stat st;
2513 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2514 io->u.Status = FILE_GetNtStatus();
2515 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2516 io->u.Status = STATUS_INVALID_INFO_CLASS;
2517 else
2519 FILE_BASIC_INFORMATION basic;
2520 FILE_STANDARD_INFORMATION std;
2522 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2523 fill_file_info( &st, attributes, &std, FileStandardInformation );
2525 info->CreationTime = basic.CreationTime;
2526 info->LastAccessTime = basic.LastAccessTime;
2527 info->LastWriteTime = basic.LastWriteTime;
2528 info->ChangeTime = basic.ChangeTime;
2529 info->AllocationSize = std.AllocationSize;
2530 info->EndOfFile = std.EndOfFile;
2531 info->FileAttributes = basic.FileAttributes;
2533 RtlFreeAnsiString( &unix_name );
2536 break;
2537 case FileIdInformation:
2538 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2539 else
2541 FILE_ID_INFORMATION *info = ptr;
2542 info->VolumeSerialNumber = 0; /* FIXME */
2543 memset( &info->FileId, 0, sizeof(info->FileId) );
2544 *(ULONGLONG *)&info->FileId = st.st_ino;
2546 break;
2547 default:
2548 FIXME("Unsupported class (%d)\n", class);
2549 io->u.Status = STATUS_NOT_IMPLEMENTED;
2550 break;
2552 if (needs_close) close( fd );
2553 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2554 return io->u.Status;
2557 /******************************************************************************
2558 * NtSetInformationFile [NTDLL.@]
2559 * ZwSetInformationFile [NTDLL.@]
2561 * Set information about an open file handle.
2563 * PARAMS
2564 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2565 * io [O] Receives information about the operation on return
2566 * ptr [I] Source for file information
2567 * len [I] Size of FileInformation
2568 * class [I] Type of file information to set
2570 * RETURNS
2571 * Success: 0. io is updated.
2572 * Failure: An NTSTATUS error code describing the error.
2574 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2575 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2577 int fd, needs_close;
2579 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2581 io->u.Status = STATUS_SUCCESS;
2582 switch (class)
2584 case FileBasicInformation:
2585 if (len >= sizeof(FILE_BASIC_INFORMATION))
2587 struct stat st;
2588 const FILE_BASIC_INFORMATION *info = ptr;
2590 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2591 return io->u.Status;
2593 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2594 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2596 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2598 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2599 else
2601 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2603 if (S_ISDIR( st.st_mode))
2604 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2605 else
2606 st.st_mode &= ~0222; /* clear write permission bits */
2608 else
2610 /* add write permission only where we already have read permission */
2611 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2613 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2617 if (needs_close) close( fd );
2619 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2620 break;
2622 case FilePositionInformation:
2623 if (len >= sizeof(FILE_POSITION_INFORMATION))
2625 const FILE_POSITION_INFORMATION *info = ptr;
2627 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2628 return io->u.Status;
2630 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2631 io->u.Status = FILE_GetNtStatus();
2633 if (needs_close) close( fd );
2635 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2636 break;
2638 case FileEndOfFileInformation:
2639 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2641 struct stat st;
2642 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2644 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2645 return io->u.Status;
2647 /* first try normal truncate */
2648 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2650 /* now check for the need to extend the file */
2651 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2653 static const char zero;
2655 /* extend the file one byte beyond the requested size and then truncate it */
2656 /* this should work around ftruncate implementations that can't extend files */
2657 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2658 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2660 io->u.Status = FILE_GetNtStatus();
2662 if (needs_close) close( fd );
2664 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2665 break;
2667 case FilePipeInformation:
2668 if (len >= sizeof(FILE_PIPE_INFORMATION))
2670 FILE_PIPE_INFORMATION *info = ptr;
2672 if ((info->CompletionMode | info->ReadMode) & ~1)
2674 io->u.Status = STATUS_INVALID_PARAMETER;
2675 break;
2678 SERVER_START_REQ( set_named_pipe_info )
2680 req->handle = wine_server_obj_handle( handle );
2681 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2682 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2683 io->u.Status = wine_server_call( req );
2685 SERVER_END_REQ;
2687 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2688 break;
2690 case FileMailslotSetInformation:
2692 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2694 SERVER_START_REQ( set_mailslot_info )
2696 req->handle = wine_server_obj_handle( handle );
2697 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2698 req->read_timeout = info->ReadTimeout.QuadPart;
2699 io->u.Status = wine_server_call( req );
2701 SERVER_END_REQ;
2703 break;
2705 case FileCompletionInformation:
2706 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2708 FILE_COMPLETION_INFORMATION *info = ptr;
2710 SERVER_START_REQ( set_completion_info )
2712 req->handle = wine_server_obj_handle( handle );
2713 req->chandle = wine_server_obj_handle( info->CompletionPort );
2714 req->ckey = info->CompletionKey;
2715 io->u.Status = wine_server_call( req );
2717 SERVER_END_REQ;
2718 } else
2719 io->u.Status = STATUS_INVALID_PARAMETER_3;
2720 break;
2722 case FileAllInformation:
2723 io->u.Status = STATUS_INVALID_INFO_CLASS;
2724 break;
2726 case FileValidDataLengthInformation:
2727 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2729 struct stat st;
2730 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2732 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2733 return io->u.Status;
2735 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2736 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2737 io->u.Status = STATUS_INVALID_PARAMETER;
2738 else
2740 #ifdef HAVE_FALLOCATE
2741 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2743 NTSTATUS status = FILE_GetNtStatus();
2744 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2745 else io->u.Status = status;
2747 #else
2748 FIXME( "setting valid data length not supported\n" );
2749 #endif
2751 if (needs_close) close( fd );
2753 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2754 break;
2756 case FileDispositionInformation:
2757 if (len >= sizeof(FILE_DISPOSITION_INFORMATION))
2759 FILE_DISPOSITION_INFORMATION *info = ptr;
2761 SERVER_START_REQ( set_fd_disp_info )
2763 req->handle = wine_server_obj_handle( handle );
2764 req->unlink = info->DoDeleteFile;
2765 io->u.Status = wine_server_call( req );
2767 SERVER_END_REQ;
2768 } else
2769 io->u.Status = STATUS_INVALID_PARAMETER_3;
2770 break;
2772 case FileRenameInformation:
2773 if (len >= sizeof(FILE_RENAME_INFORMATION))
2775 FILE_RENAME_INFORMATION *info = ptr;
2776 UNICODE_STRING name_str;
2777 OBJECT_ATTRIBUTES attr;
2778 ANSI_STRING unix_name;
2780 name_str.Buffer = info->FileName;
2781 name_str.Length = info->FileNameLength;
2782 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2784 attr.Length = sizeof(attr);
2785 attr.ObjectName = &name_str;
2786 attr.RootDirectory = info->RootDir;
2787 attr.Attributes = OBJ_CASE_INSENSITIVE;
2789 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2790 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2791 break;
2793 if (!info->Replace && io->u.Status == STATUS_SUCCESS)
2795 RtlFreeAnsiString( &unix_name );
2796 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2797 break;
2800 SERVER_START_REQ( set_fd_name_info )
2802 req->handle = wine_server_obj_handle( handle );
2803 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2804 req->link = FALSE;
2805 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2806 io->u.Status = wine_server_call( req );
2808 SERVER_END_REQ;
2810 RtlFreeAnsiString( &unix_name );
2812 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2813 break;
2815 case FileLinkInformation:
2816 if (len >= sizeof(FILE_LINK_INFORMATION))
2818 FILE_LINK_INFORMATION *info = ptr;
2819 UNICODE_STRING name_str;
2820 OBJECT_ATTRIBUTES attr;
2821 ANSI_STRING unix_name;
2823 name_str.Buffer = info->FileName;
2824 name_str.Length = info->FileNameLength;
2825 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2827 attr.Length = sizeof(attr);
2828 attr.ObjectName = &name_str;
2829 attr.RootDirectory = info->RootDirectory;
2830 attr.Attributes = OBJ_CASE_INSENSITIVE;
2832 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2833 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2834 break;
2836 if (!info->ReplaceIfExists && io->u.Status == STATUS_SUCCESS)
2838 RtlFreeAnsiString( &unix_name );
2839 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2840 break;
2843 SERVER_START_REQ( set_fd_name_info )
2845 req->handle = wine_server_obj_handle( handle );
2846 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2847 req->link = TRUE;
2848 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2849 io->u.Status = wine_server_call( req );
2851 SERVER_END_REQ;
2853 RtlFreeAnsiString( &unix_name );
2855 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2856 break;
2858 default:
2859 FIXME("Unsupported class (%d)\n", class);
2860 io->u.Status = STATUS_NOT_IMPLEMENTED;
2861 break;
2863 io->Information = 0;
2864 return io->u.Status;
2868 /******************************************************************************
2869 * NtQueryFullAttributesFile (NTDLL.@)
2871 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2872 FILE_NETWORK_OPEN_INFORMATION *info )
2874 ANSI_STRING unix_name;
2875 NTSTATUS status;
2877 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2879 ULONG attributes;
2880 struct stat st;
2882 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2883 status = FILE_GetNtStatus();
2884 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2885 status = STATUS_INVALID_INFO_CLASS;
2886 else
2888 FILE_BASIC_INFORMATION basic;
2889 FILE_STANDARD_INFORMATION std;
2891 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2892 fill_file_info( &st, attributes, &std, FileStandardInformation );
2894 info->CreationTime = basic.CreationTime;
2895 info->LastAccessTime = basic.LastAccessTime;
2896 info->LastWriteTime = basic.LastWriteTime;
2897 info->ChangeTime = basic.ChangeTime;
2898 info->AllocationSize = std.AllocationSize;
2899 info->EndOfFile = std.EndOfFile;
2900 info->FileAttributes = basic.FileAttributes;
2901 if (DIR_is_hidden_file( attr->ObjectName ))
2902 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2904 RtlFreeAnsiString( &unix_name );
2906 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2907 return status;
2911 /******************************************************************************
2912 * NtQueryAttributesFile (NTDLL.@)
2913 * ZwQueryAttributesFile (NTDLL.@)
2915 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2917 ANSI_STRING unix_name;
2918 NTSTATUS status;
2920 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2922 ULONG attributes;
2923 struct stat st;
2925 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2926 status = FILE_GetNtStatus();
2927 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2928 status = STATUS_INVALID_INFO_CLASS;
2929 else
2931 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2932 if (DIR_is_hidden_file( attr->ObjectName ))
2933 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2935 RtlFreeAnsiString( &unix_name );
2937 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2938 return status;
2942 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2943 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2944 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2945 unsigned int flags )
2947 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2949 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2950 /* Don't assume read-only, let the mount options set it below */
2951 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2953 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2954 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2956 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2957 info->Characteristics |= FILE_REMOTE_DEVICE;
2959 else if (!strcmp("procfs", fstypename))
2960 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2961 else
2962 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2964 if (flags & MNT_RDONLY)
2965 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2967 if (!(flags & MNT_LOCAL))
2969 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2970 info->Characteristics |= FILE_REMOTE_DEVICE;
2973 #endif
2975 static inline BOOL is_device_placeholder( int fd )
2977 static const char wine_placeholder[] = "Wine device placeholder";
2978 char buffer[sizeof(wine_placeholder)-1];
2980 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2981 return FALSE;
2982 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2985 /******************************************************************************
2986 * get_device_info
2988 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2990 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2992 struct stat st;
2994 info->Characteristics = 0;
2995 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2996 if (S_ISCHR( st.st_mode ))
2998 info->DeviceType = FILE_DEVICE_UNKNOWN;
2999 #ifdef linux
3000 switch(major(st.st_rdev))
3002 case MEM_MAJOR:
3003 info->DeviceType = FILE_DEVICE_NULL;
3004 break;
3005 case TTY_MAJOR:
3006 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
3007 break;
3008 case LP_MAJOR:
3009 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
3010 break;
3011 case SCSI_TAPE_MAJOR:
3012 info->DeviceType = FILE_DEVICE_TAPE;
3013 break;
3015 #endif
3017 else if (S_ISBLK( st.st_mode ))
3019 info->DeviceType = FILE_DEVICE_DISK;
3021 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
3023 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
3025 else if (is_device_placeholder( fd ))
3027 info->DeviceType = FILE_DEVICE_DISK;
3029 else /* regular file or directory */
3031 #if defined(linux) && defined(HAVE_FSTATFS)
3032 struct statfs stfs;
3034 /* check for floppy disk */
3035 if (major(st.st_dev) == FLOPPY_MAJOR)
3036 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3038 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
3039 switch (stfs.f_type)
3041 case 0x9660: /* iso9660 */
3042 case 0x9fa1: /* supermount */
3043 case 0x15013346: /* udf */
3044 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3045 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3046 break;
3047 case 0x6969: /* nfs */
3048 case 0xff534d42: /* cifs */
3049 case 0xfe534d42: /* smb2 */
3050 case 0x517b: /* smbfs */
3051 case 0x564c: /* ncpfs */
3052 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3053 info->Characteristics |= FILE_REMOTE_DEVICE;
3054 break;
3055 case 0x01021994: /* tmpfs */
3056 case 0x28cd3d45: /* cramfs */
3057 case 0x1373: /* devfs */
3058 case 0x9fa0: /* procfs */
3059 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3060 break;
3061 default:
3062 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3063 break;
3065 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3066 struct statfs stfs;
3068 if (fstatfs( fd, &stfs ) < 0)
3069 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3070 else
3071 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
3072 #elif defined(__NetBSD__)
3073 struct statvfs stfs;
3075 if (fstatvfs( fd, &stfs) < 0)
3076 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3077 else
3078 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
3079 #elif defined(sun)
3080 /* Use dkio to work out device types */
3082 # include <sys/dkio.h>
3083 # include <sys/vtoc.h>
3084 struct dk_cinfo dkinf;
3085 int retval = ioctl(fd, DKIOCINFO, &dkinf);
3086 if(retval==-1){
3087 WARN("Unable to get disk device type information - assuming a disk like device\n");
3088 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3090 switch (dkinf.dki_ctype)
3092 case DKC_CDROM:
3093 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3094 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3095 break;
3096 case DKC_NCRFLOPPY:
3097 case DKC_SMSFLOPPY:
3098 case DKC_INTEL82072:
3099 case DKC_INTEL82077:
3100 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3101 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3102 break;
3103 case DKC_MD:
3104 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3105 break;
3106 default:
3107 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3110 #else
3111 static int warned;
3112 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3113 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3114 #endif
3115 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3117 return STATUS_SUCCESS;
3121 /******************************************************************************
3122 * NtQueryVolumeInformationFile [NTDLL.@]
3123 * ZwQueryVolumeInformationFile [NTDLL.@]
3125 * Get volume information for an open file handle.
3127 * PARAMS
3128 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3129 * io [O] Receives information about the operation on return
3130 * buffer [O] Destination for volume information
3131 * length [I] Size of FsInformation
3132 * info_class [I] Type of volume information to set
3134 * RETURNS
3135 * Success: 0. io and buffer are updated.
3136 * Failure: An NTSTATUS error code describing the error.
3138 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3139 PVOID buffer, ULONG length,
3140 FS_INFORMATION_CLASS info_class )
3142 int fd, needs_close;
3143 struct stat st;
3144 static int once;
3146 io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL );
3147 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
3149 SERVER_START_REQ( get_volume_info )
3151 req->handle = wine_server_obj_handle( handle );
3152 req->info_class = info_class;
3153 wine_server_set_reply( req, buffer, length );
3154 io->u.Status = wine_server_call( req );
3155 if (!io->u.Status) io->Information = wine_server_reply_size( reply );
3157 SERVER_END_REQ;
3158 return io->u.Status;
3160 else if (io->u.Status) return io->u.Status;
3162 io->u.Status = STATUS_NOT_IMPLEMENTED;
3163 io->Information = 0;
3165 switch( info_class )
3167 case FileFsVolumeInformation:
3168 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3169 break;
3170 case FileFsLabelInformation:
3171 FIXME( "%p: label info not supported\n", handle );
3172 break;
3173 case FileFsSizeInformation:
3174 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3175 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3176 else
3178 FILE_FS_SIZE_INFORMATION *info = buffer;
3180 if (fstat( fd, &st ) < 0)
3182 io->u.Status = FILE_GetNtStatus();
3183 break;
3185 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3187 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3189 else
3191 ULONGLONG bsize;
3192 /* Linux's fstatvfs is buggy */
3193 #if !defined(linux) || !defined(HAVE_FSTATFS)
3194 struct statvfs stfs;
3196 if (fstatvfs( fd, &stfs ) < 0)
3198 io->u.Status = FILE_GetNtStatus();
3199 break;
3201 bsize = stfs.f_frsize;
3202 #else
3203 struct statfs stfs;
3204 if (fstatfs( fd, &stfs ) < 0)
3206 io->u.Status = FILE_GetNtStatus();
3207 break;
3209 bsize = stfs.f_bsize;
3210 #endif
3211 if (bsize == 2048) /* assume CD-ROM */
3213 info->BytesPerSector = 2048;
3214 info->SectorsPerAllocationUnit = 1;
3216 else
3218 info->BytesPerSector = 512;
3219 info->SectorsPerAllocationUnit = 8;
3221 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3222 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3223 io->Information = sizeof(*info);
3224 io->u.Status = STATUS_SUCCESS;
3227 break;
3228 case FileFsDeviceInformation:
3229 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3230 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3231 else
3233 FILE_FS_DEVICE_INFORMATION *info = buffer;
3235 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3236 io->Information = sizeof(*info);
3238 break;
3239 case FileFsAttributeInformation:
3240 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3241 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3242 else
3244 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3246 FIXME( "%p: faking attribute info\n", handle );
3247 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3248 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3249 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3250 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3251 info->FileSystemNameLength = sizeof(ntfsW);
3252 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3254 io->Information = sizeof(*info);
3255 io->u.Status = STATUS_SUCCESS;
3257 break;
3258 case FileFsControlInformation:
3259 FIXME( "%p: control info not supported\n", handle );
3260 break;
3261 case FileFsFullSizeInformation:
3262 FIXME( "%p: full size info not supported\n", handle );
3263 break;
3264 case FileFsObjectIdInformation:
3265 FIXME( "%p: object id info not supported\n", handle );
3266 break;
3267 case FileFsMaximumInformation:
3268 FIXME( "%p: maximum info not supported\n", handle );
3269 break;
3270 default:
3271 io->u.Status = STATUS_INVALID_PARAMETER;
3272 break;
3274 if (needs_close) close( fd );
3275 return io->u.Status;
3279 /******************************************************************
3280 * NtQueryEaFile (NTDLL.@)
3282 * Read extended attributes from NTFS files.
3284 * PARAMS
3285 * hFile [I] File handle, must be opened with FILE_READ_EA access
3286 * iosb [O] Receives information about the operation on return
3287 * buffer [O] Output buffer
3288 * length [I] Length of output buffer
3289 * single_entry [I] Only read and return one entry
3290 * ea_list [I] Optional list with names of EAs to return
3291 * ea_list_len [I] Length of ea_list in bytes
3292 * ea_index [I] Optional pointer to 1-based index of attribute to return
3293 * restart [I] restart EA scan
3295 * RETURNS
3296 * Success: 0. Atrributes read into buffer
3297 * Failure: An NTSTATUS error code describing the error.
3299 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3300 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3301 PULONG ea_index, BOOLEAN restart )
3303 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3304 hFile, iosb, buffer, length, single_entry, ea_list,
3305 ea_list_len, ea_index, restart);
3306 return STATUS_ACCESS_DENIED;
3310 /******************************************************************
3311 * NtSetEaFile (NTDLL.@)
3313 * Update extended attributes for NTFS files.
3315 * PARAMS
3316 * hFile [I] File handle, must be opened with FILE_READ_EA access
3317 * iosb [O] Receives information about the operation on return
3318 * buffer [I] Buffer with EA information
3319 * length [I] Length of buffer
3321 * RETURNS
3322 * Success: 0. Attributes are updated
3323 * Failure: An NTSTATUS error code describing the error.
3325 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3327 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3328 return STATUS_ACCESS_DENIED;
3332 /******************************************************************
3333 * NtFlushBuffersFile (NTDLL.@)
3335 * Flush any buffered data on an open file handle.
3337 * PARAMS
3338 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3339 * IoStatusBlock [O] Receives information about the operation on return
3341 * RETURNS
3342 * Success: 0. IoStatusBlock is updated.
3343 * Failure: An NTSTATUS error code describing the error.
3345 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3347 NTSTATUS ret;
3348 HANDLE hEvent = NULL;
3349 enum server_fd_type type;
3350 int fd, needs_close;
3352 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3353 if (ret == STATUS_ACCESS_DENIED)
3354 ret = server_get_unix_fd( hFile, FILE_APPEND_DATA, &fd, &needs_close, &type, NULL );
3356 if (!ret && type == FD_TYPE_SERIAL)
3358 ret = COMM_FlushBuffersFile( fd );
3360 else if (ret != STATUS_ACCESS_DENIED)
3362 SERVER_START_REQ( flush )
3364 req->async = server_async( hFile, NULL, NULL, NULL, NULL, IoStatusBlock );
3365 ret = wine_server_call( req );
3366 hEvent = wine_server_ptr_handle( reply->event );
3368 SERVER_END_REQ;
3370 if (hEvent)
3372 NtWaitForSingleObject( hEvent, FALSE, NULL );
3373 ret = STATUS_SUCCESS;
3377 if (needs_close) close( fd );
3378 return ret;
3381 /******************************************************************
3382 * NtLockFile (NTDLL.@)
3386 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3387 PIO_APC_ROUTINE apc, void* apc_user,
3388 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3389 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3390 BOOLEAN exclusive )
3392 NTSTATUS ret;
3393 HANDLE handle;
3394 BOOLEAN async;
3395 static BOOLEAN warn = TRUE;
3397 if (apc || io_status || key)
3399 FIXME("Unimplemented yet parameter\n");
3400 return STATUS_NOT_IMPLEMENTED;
3403 if (apc_user && warn)
3405 FIXME("I/O completion on lock not implemented yet\n");
3406 warn = FALSE;
3409 for (;;)
3411 SERVER_START_REQ( lock_file )
3413 req->handle = wine_server_obj_handle( hFile );
3414 req->offset = offset->QuadPart;
3415 req->count = count->QuadPart;
3416 req->shared = !exclusive;
3417 req->wait = !dont_wait;
3418 ret = wine_server_call( req );
3419 handle = wine_server_ptr_handle( reply->handle );
3420 async = reply->overlapped;
3422 SERVER_END_REQ;
3423 if (ret != STATUS_PENDING)
3425 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3426 return ret;
3429 if (async)
3431 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3432 if (handle) NtClose( handle );
3433 return STATUS_PENDING;
3435 if (handle)
3437 NtWaitForSingleObject( handle, FALSE, NULL );
3438 NtClose( handle );
3440 else
3442 LARGE_INTEGER time;
3444 /* Unix lock conflict, sleep a bit and retry */
3445 time.QuadPart = 100 * (ULONGLONG)10000;
3446 time.QuadPart = -time.QuadPart;
3447 NtDelayExecution( FALSE, &time );
3453 /******************************************************************
3454 * NtUnlockFile (NTDLL.@)
3458 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3459 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3460 PULONG key )
3462 NTSTATUS status;
3464 TRACE( "%p %x%08x %x%08x\n",
3465 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3467 if (io_status || key)
3469 FIXME("Unimplemented yet parameter\n");
3470 return STATUS_NOT_IMPLEMENTED;
3473 SERVER_START_REQ( unlock_file )
3475 req->handle = wine_server_obj_handle( hFile );
3476 req->offset = offset->QuadPart;
3477 req->count = count->QuadPart;
3478 status = wine_server_call( req );
3480 SERVER_END_REQ;
3481 return status;
3484 /******************************************************************
3485 * NtCreateNamedPipeFile (NTDLL.@)
3489 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3490 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3491 ULONG sharing, ULONG dispo, ULONG options,
3492 ULONG pipe_type, ULONG read_mode,
3493 ULONG completion_mode, ULONG max_inst,
3494 ULONG inbound_quota, ULONG outbound_quota,
3495 PLARGE_INTEGER timeout)
3497 NTSTATUS status;
3498 data_size_t len;
3499 struct object_attributes *objattr;
3501 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3502 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3503 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3504 outbound_quota, timeout);
3506 if (!attr) return STATUS_INVALID_PARAMETER;
3508 /* assume we only get relative timeout */
3509 if (timeout->QuadPart > 0)
3510 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3512 if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
3514 SERVER_START_REQ( create_named_pipe )
3516 req->access = access;
3517 req->options = options;
3518 req->sharing = sharing;
3519 req->flags =
3520 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3521 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3522 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3523 req->maxinstances = max_inst;
3524 req->outsize = outbound_quota;
3525 req->insize = inbound_quota;
3526 req->timeout = timeout->QuadPart;
3527 wine_server_add_data( req, objattr, len );
3528 status = wine_server_call( req );
3529 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3531 SERVER_END_REQ;
3533 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3534 return status;
3537 /******************************************************************
3538 * NtDeleteFile (NTDLL.@)
3542 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3544 NTSTATUS status;
3545 HANDLE hFile;
3546 IO_STATUS_BLOCK io;
3548 TRACE("%p\n", ObjectAttributes);
3549 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3550 ObjectAttributes, &io, NULL, 0,
3551 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3552 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3553 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3554 return status;
3557 /******************************************************************
3558 * NtCancelIoFileEx (NTDLL.@)
3562 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3564 TRACE("%p %p %p\n", hFile, iosb, io_status );
3566 SERVER_START_REQ( cancel_async )
3568 req->handle = wine_server_obj_handle( hFile );
3569 req->iosb = wine_server_client_ptr( iosb );
3570 req->only_thread = FALSE;
3571 io_status->u.Status = wine_server_call( req );
3573 SERVER_END_REQ;
3575 return io_status->u.Status;
3578 /******************************************************************
3579 * NtCancelIoFile (NTDLL.@)
3583 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3585 TRACE("%p %p\n", hFile, io_status );
3587 SERVER_START_REQ( cancel_async )
3589 req->handle = wine_server_obj_handle( hFile );
3590 req->iosb = 0;
3591 req->only_thread = TRUE;
3592 io_status->u.Status = wine_server_call( req );
3594 SERVER_END_REQ;
3596 return io_status->u.Status;
3599 /******************************************************************************
3600 * NtCreateMailslotFile [NTDLL.@]
3601 * ZwCreateMailslotFile [NTDLL.@]
3603 * PARAMS
3604 * pHandle [O] pointer to receive the handle created
3605 * DesiredAccess [I] access mode (read, write, etc)
3606 * ObjectAttributes [I] fully qualified NT path of the mailslot
3607 * IoStatusBlock [O] receives completion status and other info
3608 * CreateOptions [I]
3609 * MailslotQuota [I]
3610 * MaxMessageSize [I]
3611 * TimeOut [I]
3613 * RETURNS
3614 * An NT status code
3616 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3617 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3618 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3619 PLARGE_INTEGER TimeOut)
3621 LARGE_INTEGER timeout;
3622 NTSTATUS ret;
3623 data_size_t len;
3624 struct object_attributes *objattr;
3626 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3627 pHandle, DesiredAccess, attr, IoStatusBlock,
3628 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3630 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3631 if (!attr) return STATUS_INVALID_PARAMETER;
3633 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3636 * For a NULL TimeOut pointer set the default timeout value
3638 if (!TimeOut)
3639 timeout.QuadPart = -1;
3640 else
3641 timeout.QuadPart = TimeOut->QuadPart;
3643 SERVER_START_REQ( create_mailslot )
3645 req->access = DesiredAccess;
3646 req->max_msgsize = MaxMessageSize;
3647 req->read_timeout = timeout.QuadPart;
3648 wine_server_add_data( req, objattr, len );
3649 ret = wine_server_call( req );
3650 if( ret == STATUS_SUCCESS )
3651 *pHandle = wine_server_ptr_handle( reply->handle );
3653 SERVER_END_REQ;
3655 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3656 return ret;