comctl32/listbox: Use a helper to set the selected item state.
[wine.git] / dlls / ntdll / file.c
bloba43fe71108d911e24ff93dd5ad2112ff261ce780
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 void *buffer; /* buffer for output */
379 ULONG size; /* size of buffer */
382 static struct async_fileio *fileio_freelist;
384 static void release_fileio( struct async_fileio *io )
386 for (;;)
388 struct async_fileio *next = fileio_freelist;
389 io->next = next;
390 if (interlocked_cmpxchg_ptr( (void **)&fileio_freelist, io, next ) == next) return;
394 static struct async_fileio *alloc_fileio( DWORD size, async_callback_t callback, HANDLE handle )
396 /* first free remaining previous fileinfos */
398 struct async_fileio *io = interlocked_xchg_ptr( (void **)&fileio_freelist, NULL );
400 while (io)
402 struct async_fileio *next = io->next;
403 RtlFreeHeap( GetProcessHeap(), 0, io );
404 io = next;
407 if ((io = RtlAllocateHeap( GetProcessHeap(), 0, size )))
409 io->callback = callback;
410 io->handle = handle;
412 return io;
415 static async_data_t server_async( HANDLE handle, struct async_fileio *user, HANDLE event,
416 PIO_APC_ROUTINE apc, void *apc_context, IO_STATUS_BLOCK *io )
418 async_data_t async;
419 async.handle = wine_server_obj_handle( handle );
420 async.user = wine_server_client_ptr( user );
421 async.iosb = wine_server_client_ptr( io );
422 async.event = wine_server_obj_handle( event );
423 async.apc = wine_server_client_ptr( apc );
424 async.apc_context = wine_server_client_ptr( apc_context );
425 return async;
428 /* callback for irp async I/O completion */
429 static NTSTATUS irp_completion( void *user, IO_STATUS_BLOCK *io, NTSTATUS status )
431 struct async_irp *async = user;
432 ULONG information = 0;
434 if (status == STATUS_ALERTED)
436 SERVER_START_REQ( get_async_result )
438 req->user_arg = wine_server_client_ptr( async );
439 wine_server_set_reply( req, async->buffer, async->size );
440 status = virtual_locked_server_call( req );
441 information = reply->size;
443 SERVER_END_REQ;
445 if (status != STATUS_PENDING)
447 io->u.Status = status;
448 io->Information = information;
449 release_fileio( &async->io );
451 return status;
454 /***********************************************************************
455 * FILE_GetNtStatus(void)
457 * Retrieve the Nt Status code from errno.
458 * Try to be consistent with FILE_SetDosError().
460 NTSTATUS FILE_GetNtStatus(void)
462 int err = errno;
464 TRACE( "errno = %d\n", errno );
465 switch (err)
467 case EAGAIN: return STATUS_SHARING_VIOLATION;
468 case EBADF: return STATUS_INVALID_HANDLE;
469 case EBUSY: return STATUS_DEVICE_BUSY;
470 case ENOSPC: return STATUS_DISK_FULL;
471 case EPERM:
472 case EROFS:
473 case EACCES: return STATUS_ACCESS_DENIED;
474 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
475 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
476 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
477 case EMFILE:
478 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
479 case EINVAL: return STATUS_INVALID_PARAMETER;
480 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
481 case EPIPE: return STATUS_PIPE_DISCONNECTED;
482 case EIO: return STATUS_DEVICE_NOT_READY;
483 #ifdef ENOMEDIUM
484 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
485 #endif
486 case ENXIO: return STATUS_NO_SUCH_DEVICE;
487 case ENOTTY:
488 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
489 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
490 case EFAULT: return STATUS_ACCESS_VIOLATION;
491 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
492 #ifdef ETIME /* Missing on FreeBSD */
493 case ETIME: return STATUS_IO_TIMEOUT;
494 #endif
495 case ENOEXEC: /* ?? */
496 case EEXIST: /* ?? */
497 default:
498 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
499 return STATUS_UNSUCCESSFUL;
503 /***********************************************************************
504 * FILE_AsyncReadService (INTERNAL)
506 static NTSTATUS FILE_AsyncReadService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
508 struct async_fileio_read *fileio = user;
509 int fd, needs_close, result;
511 switch (status)
513 case STATUS_ALERTED: /* got some new data */
514 /* check to see if the data is ready (non-blocking) */
515 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
516 &needs_close, NULL, NULL )))
517 break;
519 result = virtual_locked_read(fd, &fileio->buffer[fileio->already], fileio->count-fileio->already);
520 if (needs_close) close( fd );
522 if (result < 0)
524 if (errno == EAGAIN || errno == EINTR)
525 status = STATUS_PENDING;
526 else /* check to see if the transfer is complete */
527 status = FILE_GetNtStatus();
529 else if (result == 0)
531 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
533 else
535 fileio->already += result;
536 if (fileio->already >= fileio->count || fileio->avail_mode)
537 status = STATUS_SUCCESS;
538 else
539 status = STATUS_PENDING;
541 break;
543 case STATUS_TIMEOUT:
544 case STATUS_IO_TIMEOUT:
545 if (fileio->already) status = STATUS_SUCCESS;
546 break;
548 if (status != STATUS_PENDING)
550 iosb->u.Status = status;
551 iosb->Information = fileio->already;
552 release_fileio( &fileio->io );
554 return status;
557 /* do a read call through the server */
558 static NTSTATUS server_read_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
559 IO_STATUS_BLOCK *io, void *buffer, ULONG size,
560 LARGE_INTEGER *offset, ULONG *key )
562 struct async_irp *async;
563 NTSTATUS status;
564 HANDLE wait_handle;
565 ULONG options;
567 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
568 return STATUS_NO_MEMORY;
570 async->buffer = buffer;
571 async->size = size;
573 SERVER_START_REQ( read )
575 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
576 req->pos = offset ? offset->QuadPart : 0;
577 wine_server_set_reply( req, buffer, size );
578 status = virtual_locked_server_call( req );
579 wait_handle = wine_server_ptr_handle( reply->wait );
580 options = reply->options;
581 if (wait_handle && status != STATUS_PENDING)
583 io->u.Status = status;
584 io->Information = wine_server_reply_size( reply );
587 SERVER_END_REQ;
589 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
591 if (wait_handle)
593 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
594 status = io->u.Status;
597 return status;
600 /* do a write call through the server */
601 static NTSTATUS server_write_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
602 IO_STATUS_BLOCK *io, const void *buffer, ULONG size,
603 LARGE_INTEGER *offset, ULONG *key )
605 struct async_irp *async;
606 NTSTATUS status;
607 HANDLE wait_handle;
608 ULONG options;
610 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
611 return STATUS_NO_MEMORY;
613 async->buffer = NULL;
614 async->size = 0;
616 SERVER_START_REQ( write )
618 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
619 req->pos = offset ? offset->QuadPart : 0;
620 wine_server_add_data( req, buffer, size );
621 status = wine_server_call( req );
622 wait_handle = wine_server_ptr_handle( reply->wait );
623 options = reply->options;
624 if (wait_handle && status != STATUS_PENDING)
626 io->u.Status = status;
627 io->Information = reply->size;
630 SERVER_END_REQ;
632 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
634 if (wait_handle)
636 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
637 status = io->u.Status;
640 return status;
643 struct io_timeouts
645 int interval; /* max interval between two bytes */
646 int total; /* total timeout for the whole operation */
647 int end_time; /* absolute time of end of operation */
650 /* retrieve the I/O timeouts to use for a given handle */
651 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
652 struct io_timeouts *timeouts )
654 NTSTATUS status = STATUS_SUCCESS;
656 timeouts->interval = timeouts->total = -1;
658 switch(type)
660 case FD_TYPE_SERIAL:
662 /* GetCommTimeouts */
663 SERIAL_TIMEOUTS st;
664 IO_STATUS_BLOCK io;
666 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
667 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
668 if (status) break;
670 if (is_read)
672 if (st.ReadIntervalTimeout)
673 timeouts->interval = st.ReadIntervalTimeout;
675 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
677 timeouts->total = st.ReadTotalTimeoutConstant;
678 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
679 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
681 else if (st.ReadIntervalTimeout == MAXDWORD)
682 timeouts->interval = timeouts->total = 0;
684 else /* write */
686 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
688 timeouts->total = st.WriteTotalTimeoutConstant;
689 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
690 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
694 break;
695 case FD_TYPE_MAILSLOT:
696 if (is_read)
698 timeouts->interval = 0; /* return as soon as we got something */
699 SERVER_START_REQ( set_mailslot_info )
701 req->handle = wine_server_obj_handle( handle );
702 req->flags = 0;
703 if (!(status = wine_server_call( req )) &&
704 reply->read_timeout != TIMEOUT_INFINITE)
705 timeouts->total = reply->read_timeout / -10000;
707 SERVER_END_REQ;
709 break;
710 case FD_TYPE_SOCKET:
711 case FD_TYPE_CHAR:
712 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
713 break;
714 default:
715 break;
717 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
718 return STATUS_SUCCESS;
722 /* retrieve the timeout for the next wait, in milliseconds */
723 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
725 int ret = -1;
727 if (timeouts->total != -1)
729 ret = timeouts->end_time - NtGetTickCount();
730 if (ret < 0) ret = 0;
732 if (already && timeouts->interval != -1)
734 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
736 return ret;
740 /* retrieve the avail_mode flag for async reads */
741 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
743 NTSTATUS status = STATUS_SUCCESS;
745 switch(type)
747 case FD_TYPE_SERIAL:
749 /* GetCommTimeouts */
750 SERIAL_TIMEOUTS st;
751 IO_STATUS_BLOCK io;
753 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
754 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
755 if (status) break;
756 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
757 !st.ReadTotalTimeoutConstant &&
758 st.ReadIntervalTimeout == MAXDWORD);
760 break;
761 case FD_TYPE_MAILSLOT:
762 case FD_TYPE_SOCKET:
763 case FD_TYPE_CHAR:
764 *avail_mode = TRUE;
765 break;
766 default:
767 *avail_mode = FALSE;
768 break;
770 return status;
773 /* register an async I/O for a file read; helper for NtReadFile */
774 static NTSTATUS register_async_file_read( HANDLE handle, HANDLE event,
775 PIO_APC_ROUTINE apc, void *apc_user,
776 IO_STATUS_BLOCK *iosb, void *buffer,
777 ULONG already, ULONG length, BOOL avail_mode )
779 struct async_fileio_read *fileio;
780 NTSTATUS status;
782 if (!(fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), FILE_AsyncReadService, handle )))
783 return STATUS_NO_MEMORY;
785 fileio->already = already;
786 fileio->count = length;
787 fileio->buffer = buffer;
788 fileio->avail_mode = avail_mode;
790 SERVER_START_REQ( register_async )
792 req->type = ASYNC_TYPE_READ;
793 req->count = length;
794 req->async = server_async( handle, &fileio->io, event, apc, apc_user, iosb );
795 status = wine_server_call( req );
797 SERVER_END_REQ;
799 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
800 return status;
804 /******************************************************************************
805 * NtReadFile [NTDLL.@]
806 * ZwReadFile [NTDLL.@]
808 * Read from an open file handle.
810 * PARAMS
811 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
812 * Event [I] Event to signal upon completion (or NULL)
813 * ApcRoutine [I] Callback to call upon completion (or NULL)
814 * ApcContext [I] Context for ApcRoutine (or NULL)
815 * IoStatusBlock [O] Receives information about the operation on return
816 * Buffer [O] Destination for the data read
817 * Length [I] Size of Buffer
818 * ByteOffset [O] Destination for the new file pointer position (or NULL)
819 * Key [O] Function unknown (may be NULL)
821 * RETURNS
822 * Success: 0. IoStatusBlock is updated, and the Information member contains
823 * The number of bytes read.
824 * Failure: An NTSTATUS error code describing the error.
826 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
827 PIO_APC_ROUTINE apc, void* apc_user,
828 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
829 PLARGE_INTEGER offset, PULONG key)
831 int result, unix_handle, needs_close;
832 unsigned int options;
833 struct io_timeouts timeouts;
834 NTSTATUS status;
835 ULONG total = 0;
836 enum server_fd_type type;
837 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
838 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
840 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)\n",
841 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
843 if (!io_status) return STATUS_ACCESS_VIOLATION;
845 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
846 &needs_close, &type, &options );
847 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
849 if (!virtual_check_buffer_for_write( buffer, length )) return STATUS_ACCESS_VIOLATION;
851 if (status == STATUS_BAD_DEVICE_TYPE)
852 return server_read_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
854 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
856 if (type == FD_TYPE_FILE)
858 if (async_read && (!offset || offset->QuadPart < 0))
860 status = STATUS_INVALID_PARAMETER;
861 goto done;
864 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
866 /* async I/O doesn't make sense on regular files */
867 while ((result = virtual_locked_pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
869 if (errno != EINTR)
871 status = FILE_GetNtStatus();
872 goto done;
875 if (!async_read)
876 /* update file pointer position */
877 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
879 total = result;
880 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
881 goto done;
884 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
886 if (async_read && (!offset || offset->QuadPart < 0))
888 status = STATUS_INVALID_PARAMETER;
889 goto done;
893 if (type == FD_TYPE_SERIAL && async_read && length)
895 /* an asynchronous serial port read with a read interval timeout needs to
896 skip the synchronous read to make sure that the server starts the read
897 interval timer after the first read */
898 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts ))) goto err;
899 if (timeouts.interval)
901 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
902 buffer, total, length, FALSE );
903 goto err;
907 for (;;)
909 if ((result = virtual_locked_read( unix_handle, (char *)buffer + total, length - total )) >= 0)
911 total += result;
912 if (!result || total == length)
914 if (total)
916 status = STATUS_SUCCESS;
917 goto done;
919 switch (type)
921 case FD_TYPE_FILE:
922 case FD_TYPE_CHAR:
923 case FD_TYPE_DEVICE:
924 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
925 goto done;
926 case FD_TYPE_SERIAL:
927 if (!length)
929 status = STATUS_SUCCESS;
930 goto done;
932 break;
933 default:
934 status = STATUS_PIPE_BROKEN;
935 goto err;
938 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
940 else if (errno != EAGAIN)
942 if (errno == EINTR) continue;
943 if (!total) status = FILE_GetNtStatus();
944 goto err;
947 if (async_read)
949 BOOL avail_mode;
951 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
952 goto err;
953 if (total && avail_mode)
955 status = STATUS_SUCCESS;
956 goto done;
958 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
959 buffer, total, length, avail_mode );
960 goto err;
962 else /* synchronous read, wait for the fd to become ready */
964 struct pollfd pfd;
965 int ret, timeout;
967 if (!timeout_init_done)
969 timeout_init_done = TRUE;
970 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
971 goto err;
972 if (hEvent) NtResetEvent( hEvent, NULL );
974 timeout = get_next_io_timeout( &timeouts, total );
976 pfd.fd = unix_handle;
977 pfd.events = POLLIN;
979 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
981 if (total) /* return with what we got so far */
982 status = STATUS_SUCCESS;
983 else
984 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
985 goto done;
987 if (ret == -1 && errno != EINTR)
989 status = FILE_GetNtStatus();
990 goto done;
992 /* will now restart the read */
996 done:
997 send_completion = cvalue != 0;
999 err:
1000 if (needs_close) close( unix_handle );
1001 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
1003 io_status->u.Status = status;
1004 io_status->Information = total;
1005 TRACE("= SUCCESS (%u)\n", total);
1006 if (hEvent) NtSetEvent( hEvent, NULL );
1007 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1008 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1010 else
1012 TRACE("= 0x%08x\n", status);
1013 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1016 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1018 return status;
1022 /******************************************************************************
1023 * NtReadFileScatter [NTDLL.@]
1024 * ZwReadFileScatter [NTDLL.@]
1026 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1027 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1028 ULONG length, PLARGE_INTEGER offset, PULONG key )
1030 int result, unix_handle, needs_close;
1031 unsigned int options;
1032 NTSTATUS status;
1033 ULONG pos = 0, total = 0;
1034 enum server_fd_type type;
1035 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1036 BOOL send_completion = FALSE;
1038 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1039 file, event, apc, apc_user, io_status, segments, length, offset, key);
1041 if (!io_status) return STATUS_ACCESS_VIOLATION;
1043 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1044 &needs_close, &type, &options );
1045 if (status) return status;
1047 if ((type != FD_TYPE_FILE) ||
1048 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1049 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1051 status = STATUS_INVALID_PARAMETER;
1052 goto error;
1055 while (length)
1057 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1058 result = pread( unix_handle, (char *)segments->Buffer + pos,
1059 min( length - pos, page_size - pos ), offset->QuadPart + total );
1060 else
1061 result = read( unix_handle, (char *)segments->Buffer + pos, min( length - pos, page_size - pos ) );
1063 if (result == -1)
1065 if (errno == EINTR) continue;
1066 status = FILE_GetNtStatus();
1067 break;
1069 if (!result) break;
1070 total += result;
1071 length -= result;
1072 if ((pos += result) == page_size)
1074 pos = 0;
1075 segments++;
1079 if (total == 0) status = STATUS_END_OF_FILE;
1081 send_completion = cvalue != 0;
1083 if (needs_close) close( unix_handle );
1085 io_status->u.Status = status;
1086 io_status->Information = total;
1087 TRACE("= 0x%08x (%u)\n", status, total);
1088 if (event) NtSetEvent( event, NULL );
1089 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1090 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1091 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1093 return STATUS_PENDING;
1095 error:
1096 if (needs_close) close( unix_handle );
1098 TRACE("= 0x%08x\n", status);
1099 if (event) NtResetEvent( event, NULL );
1101 return status;
1105 /***********************************************************************
1106 * FILE_AsyncWriteService (INTERNAL)
1108 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1110 struct async_fileio_write *fileio = user;
1111 int result, fd, needs_close;
1112 enum server_fd_type type;
1114 switch (status)
1116 case STATUS_ALERTED:
1117 /* write some data (non-blocking) */
1118 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1119 &needs_close, &type, NULL )))
1120 break;
1122 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1123 result = send( fd, fileio->buffer, 0, 0 );
1124 else
1125 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1127 if (needs_close) close( fd );
1129 if (result < 0)
1131 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1132 else status = FILE_GetNtStatus();
1134 else
1136 fileio->already += result;
1137 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1139 break;
1141 case STATUS_TIMEOUT:
1142 case STATUS_IO_TIMEOUT:
1143 if (fileio->already) status = STATUS_SUCCESS;
1144 break;
1146 if (status != STATUS_PENDING)
1148 iosb->u.Status = status;
1149 iosb->Information = fileio->already;
1150 release_fileio( &fileio->io );
1152 return status;
1155 static NTSTATUS set_pending_write( HANDLE device )
1157 NTSTATUS status;
1159 SERVER_START_REQ( set_serial_info )
1161 req->handle = wine_server_obj_handle( device );
1162 req->flags = SERIALINFO_PENDING_WRITE;
1163 status = wine_server_call( req );
1165 SERVER_END_REQ;
1166 return status;
1169 /******************************************************************************
1170 * NtWriteFile [NTDLL.@]
1171 * ZwWriteFile [NTDLL.@]
1173 * Write to an open file handle.
1175 * PARAMS
1176 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1177 * Event [I] Event to signal upon completion (or NULL)
1178 * ApcRoutine [I] Callback to call upon completion (or NULL)
1179 * ApcContext [I] Context for ApcRoutine (or NULL)
1180 * IoStatusBlock [O] Receives information about the operation on return
1181 * Buffer [I] Source for the data to write
1182 * Length [I] Size of Buffer
1183 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1184 * Key [O] Function unknown (may be NULL)
1186 * RETURNS
1187 * Success: 0. IoStatusBlock is updated, and the Information member contains
1188 * The number of bytes written.
1189 * Failure: An NTSTATUS error code describing the error.
1191 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1192 PIO_APC_ROUTINE apc, void* apc_user,
1193 PIO_STATUS_BLOCK io_status,
1194 const void* buffer, ULONG length,
1195 PLARGE_INTEGER offset, PULONG key)
1197 int result, unix_handle, needs_close;
1198 unsigned int options;
1199 struct io_timeouts timeouts;
1200 NTSTATUS status;
1201 ULONG total = 0;
1202 enum server_fd_type type;
1203 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1204 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1205 LARGE_INTEGER offset_eof;
1207 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)\n",
1208 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1210 if (!io_status) return STATUS_ACCESS_VIOLATION;
1212 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1213 &needs_close, &type, &options );
1214 if (status == STATUS_ACCESS_DENIED)
1216 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1217 &needs_close, &type, &options );
1218 append_write = TRUE;
1220 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
1222 if (!virtual_check_buffer_for_read( buffer, length ))
1224 status = STATUS_INVALID_USER_BUFFER;
1225 goto done;
1228 if (status == STATUS_BAD_DEVICE_TYPE)
1229 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1231 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1233 if (type == FD_TYPE_FILE)
1235 if (async_write &&
1236 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1238 status = STATUS_INVALID_PARAMETER;
1239 goto done;
1242 if (append_write)
1244 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1245 offset = &offset_eof;
1248 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1250 off_t off = offset->QuadPart;
1252 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1254 struct stat st;
1256 if (fstat( unix_handle, &st ) == -1)
1258 status = FILE_GetNtStatus();
1259 goto done;
1261 off = st.st_size;
1263 else if (offset->QuadPart < 0)
1265 status = STATUS_INVALID_PARAMETER;
1266 goto done;
1269 /* async I/O doesn't make sense on regular files */
1270 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1272 if (errno != EINTR)
1274 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1275 else status = FILE_GetNtStatus();
1276 goto done;
1280 if (!async_write)
1281 /* update file pointer position */
1282 lseek( unix_handle, off + result, SEEK_SET );
1284 total = result;
1285 status = STATUS_SUCCESS;
1286 goto done;
1289 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1291 if (async_write &&
1292 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1294 status = STATUS_INVALID_PARAMETER;
1295 goto done;
1299 for (;;)
1301 /* zero-length writes on sockets may not work with plain write(2) */
1302 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1303 result = send( unix_handle, buffer, 0, 0 );
1304 else
1305 result = write( unix_handle, (const char *)buffer + total, length - total );
1307 if (result >= 0)
1309 total += result;
1310 if (total == length)
1312 status = STATUS_SUCCESS;
1313 goto done;
1315 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1317 else if (errno != EAGAIN)
1319 if (errno == EINTR) continue;
1320 if (!total)
1322 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1323 else status = FILE_GetNtStatus();
1325 goto err;
1328 if (async_write)
1330 struct async_fileio_write *fileio;
1332 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), FILE_AsyncWriteService, hFile );
1333 if (!fileio)
1335 status = STATUS_NO_MEMORY;
1336 goto err;
1338 fileio->already = total;
1339 fileio->count = length;
1340 fileio->buffer = buffer;
1342 SERVER_START_REQ( register_async )
1344 req->type = ASYNC_TYPE_WRITE;
1345 req->count = length;
1346 req->async = server_async( hFile, &fileio->io, hEvent, apc, apc_user, io_status );
1347 status = wine_server_call( req );
1349 SERVER_END_REQ;
1351 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1352 goto err;
1354 else /* synchronous write, wait for the fd to become ready */
1356 struct pollfd pfd;
1357 int ret, timeout;
1359 if (!timeout_init_done)
1361 timeout_init_done = TRUE;
1362 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1363 goto err;
1364 if (hEvent) NtResetEvent( hEvent, NULL );
1366 timeout = get_next_io_timeout( &timeouts, total );
1368 pfd.fd = unix_handle;
1369 pfd.events = POLLOUT;
1371 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1373 /* return with what we got so far */
1374 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1375 goto done;
1377 if (ret == -1 && errno != EINTR)
1379 status = FILE_GetNtStatus();
1380 goto done;
1382 /* will now restart the write */
1386 done:
1387 send_completion = cvalue != 0;
1389 err:
1390 if (needs_close) close( unix_handle );
1392 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1393 set_pending_write( hFile );
1395 if (status == STATUS_SUCCESS)
1397 io_status->u.Status = status;
1398 io_status->Information = total;
1399 TRACE("= SUCCESS (%u)\n", total);
1400 if (hEvent) NtSetEvent( hEvent, NULL );
1401 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1402 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1404 else
1406 TRACE("= 0x%08x\n", status);
1407 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1410 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1412 return status;
1416 /******************************************************************************
1417 * NtWriteFileGather [NTDLL.@]
1418 * ZwWriteFileGather [NTDLL.@]
1420 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1421 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1422 ULONG length, PLARGE_INTEGER offset, PULONG key )
1424 int result, unix_handle, needs_close;
1425 unsigned int options;
1426 NTSTATUS status;
1427 ULONG pos = 0, total = 0;
1428 enum server_fd_type type;
1429 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1430 BOOL send_completion = FALSE;
1432 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1433 file, event, apc, apc_user, io_status, segments, length, offset, key);
1435 if (length % page_size) return STATUS_INVALID_PARAMETER;
1436 if (!io_status) return STATUS_ACCESS_VIOLATION;
1438 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1439 &needs_close, &type, &options );
1440 if (status) return status;
1442 if ((type != FD_TYPE_FILE) ||
1443 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1444 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1446 status = STATUS_INVALID_PARAMETER;
1447 goto error;
1450 while (length)
1452 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1453 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1454 page_size - pos, offset->QuadPart + total );
1455 else
1456 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1458 if (result == -1)
1460 if (errno == EINTR) continue;
1461 if (errno == EFAULT)
1463 status = STATUS_INVALID_USER_BUFFER;
1464 goto error;
1466 status = FILE_GetNtStatus();
1467 break;
1469 if (!result)
1471 status = STATUS_DISK_FULL;
1472 break;
1474 total += result;
1475 length -= result;
1476 if ((pos += result) == page_size)
1478 pos = 0;
1479 segments++;
1483 send_completion = cvalue != 0;
1485 error:
1486 if (needs_close) close( unix_handle );
1487 if (status == STATUS_SUCCESS)
1489 io_status->u.Status = status;
1490 io_status->Information = total;
1491 TRACE("= SUCCESS (%u)\n", total);
1492 if (event) NtSetEvent( event, NULL );
1493 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1494 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1496 else
1498 TRACE("= 0x%08x\n", status);
1499 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1502 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1504 return status;
1508 /* do an ioctl call through the server */
1509 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1510 PIO_APC_ROUTINE apc, PVOID apc_context,
1511 IO_STATUS_BLOCK *io, ULONG code,
1512 const void *in_buffer, ULONG in_size,
1513 PVOID out_buffer, ULONG out_size )
1515 struct async_irp *async;
1516 NTSTATUS status;
1517 HANDLE wait_handle;
1518 ULONG options;
1520 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
1521 return STATUS_NO_MEMORY;
1522 async->buffer = out_buffer;
1523 async->size = out_size;
1525 SERVER_START_REQ( ioctl )
1527 req->code = code;
1528 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
1529 wine_server_add_data( req, in_buffer, in_size );
1530 if ((code & 3) != METHOD_BUFFERED)
1531 wine_server_add_data( req, out_buffer, out_size );
1532 wine_server_set_reply( req, out_buffer, out_size );
1533 status = virtual_locked_server_call( req );
1534 wait_handle = wine_server_ptr_handle( reply->wait );
1535 options = reply->options;
1536 if (wait_handle && status != STATUS_PENDING)
1538 io->u.Status = status;
1539 io->Information = wine_server_reply_size( reply );
1542 SERVER_END_REQ;
1544 if (status == STATUS_NOT_SUPPORTED)
1545 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1546 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1548 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1550 if (wait_handle)
1552 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1553 status = io->u.Status;
1556 return status;
1559 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1560 * server */
1561 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1562 ULONG in_size)
1564 #ifdef VALGRIND_MAKE_MEM_DEFINED
1565 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1566 do { \
1567 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1568 if ((size) >= FIELD_OFFSET(t, f2)) \
1569 VALGRIND_MAKE_MEM_DEFINED( \
1570 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1571 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1572 } while (0)
1574 switch (code)
1576 case FSCTL_PIPE_WAIT:
1577 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1578 break;
1580 #endif
1584 /**************************************************************************
1585 * NtDeviceIoControlFile [NTDLL.@]
1586 * ZwDeviceIoControlFile [NTDLL.@]
1588 * Perform an I/O control operation on an open file handle.
1590 * PARAMS
1591 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1592 * event [I] Event to signal upon completion (or NULL)
1593 * apc [I] Callback to call upon completion (or NULL)
1594 * apc_context [I] Context for ApcRoutine (or NULL)
1595 * io [O] Receives information about the operation on return
1596 * code [I] Control code for the operation to perform
1597 * in_buffer [I] Source for any input data required (or NULL)
1598 * in_size [I] Size of InputBuffer
1599 * out_buffer [O] Source for any output data returned (or NULL)
1600 * out_size [I] Size of OutputBuffer
1602 * RETURNS
1603 * Success: 0. IoStatusBlock is updated.
1604 * Failure: An NTSTATUS error code describing the error.
1606 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1607 PIO_APC_ROUTINE apc, PVOID apc_context,
1608 PIO_STATUS_BLOCK io, ULONG code,
1609 PVOID in_buffer, ULONG in_size,
1610 PVOID out_buffer, ULONG out_size)
1612 ULONG device = (code >> 16);
1613 NTSTATUS status = STATUS_NOT_SUPPORTED;
1615 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1616 handle, event, apc, apc_context, io, code,
1617 in_buffer, in_size, out_buffer, out_size);
1619 switch(device)
1621 case FILE_DEVICE_DISK:
1622 case FILE_DEVICE_CD_ROM:
1623 case FILE_DEVICE_DVD:
1624 case FILE_DEVICE_CONTROLLER:
1625 case FILE_DEVICE_MASS_STORAGE:
1626 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1627 in_buffer, in_size, out_buffer, out_size);
1628 break;
1629 case FILE_DEVICE_SERIAL_PORT:
1630 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1631 in_buffer, in_size, out_buffer, out_size);
1632 break;
1633 case FILE_DEVICE_TAPE:
1634 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1635 in_buffer, in_size, out_buffer, out_size);
1636 break;
1639 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1640 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1641 in_buffer, in_size, out_buffer, out_size );
1643 if (status != STATUS_PENDING) io->u.Status = status;
1644 return status;
1648 /**************************************************************************
1649 * NtFsControlFile [NTDLL.@]
1650 * ZwFsControlFile [NTDLL.@]
1652 * Perform a file system control operation on an open file handle.
1654 * PARAMS
1655 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1656 * event [I] Event to signal upon completion (or NULL)
1657 * apc [I] Callback to call upon completion (or NULL)
1658 * apc_context [I] Context for ApcRoutine (or NULL)
1659 * io [O] Receives information about the operation on return
1660 * code [I] Control code for the operation to perform
1661 * in_buffer [I] Source for any input data required (or NULL)
1662 * in_size [I] Size of InputBuffer
1663 * out_buffer [O] Source for any output data returned (or NULL)
1664 * out_size [I] Size of OutputBuffer
1666 * RETURNS
1667 * Success: 0. IoStatusBlock is updated.
1668 * Failure: An NTSTATUS error code describing the error.
1670 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1671 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1672 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1674 NTSTATUS status;
1676 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1677 handle, event, apc, apc_context, io, code,
1678 in_buffer, in_size, out_buffer, out_size);
1680 if (!io) return STATUS_INVALID_PARAMETER;
1682 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1684 switch(code)
1686 case FSCTL_DISMOUNT_VOLUME:
1687 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1688 in_buffer, in_size, out_buffer, out_size );
1689 if (!status) status = DIR_unmount_device( handle );
1690 return status;
1692 case FSCTL_PIPE_IMPERSONATE:
1693 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1694 status = RtlImpersonateSelf( SecurityImpersonation );
1695 break;
1697 case FSCTL_IS_VOLUME_MOUNTED:
1698 case FSCTL_LOCK_VOLUME:
1699 case FSCTL_UNLOCK_VOLUME:
1700 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1701 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1702 status = STATUS_SUCCESS;
1703 break;
1705 case FSCTL_GET_RETRIEVAL_POINTERS:
1707 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1709 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1711 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1713 buffer->ExtentCount = 1;
1714 buffer->StartingVcn.QuadPart = 1;
1715 buffer->Extents[0].NextVcn.QuadPart = 0;
1716 buffer->Extents[0].Lcn.QuadPart = 0;
1717 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1718 status = STATUS_SUCCESS;
1720 else
1722 io->Information = 0;
1723 status = STATUS_BUFFER_TOO_SMALL;
1725 break;
1727 case FSCTL_SET_SPARSE:
1728 TRACE("FSCTL_SET_SPARSE: Ignoring request\n");
1729 io->Information = 0;
1730 status = STATUS_SUCCESS;
1731 break;
1732 default:
1733 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1734 in_buffer, in_size, out_buffer, out_size );
1737 if (status != STATUS_PENDING) io->u.Status = status;
1738 return status;
1742 struct read_changes_fileio
1744 struct async_fileio io;
1745 void *buffer;
1746 ULONG buffer_size;
1747 ULONG data_size;
1748 char data[1];
1751 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1753 struct read_changes_fileio *fileio = user;
1754 int size = 0;
1756 if (status == STATUS_ALERTED)
1758 SERVER_START_REQ( read_change )
1760 req->handle = wine_server_obj_handle( fileio->io.handle );
1761 wine_server_set_reply( req, fileio->data, fileio->data_size );
1762 status = wine_server_call( req );
1763 size = wine_server_reply_size( reply );
1765 SERVER_END_REQ;
1767 if (status == STATUS_SUCCESS && fileio->buffer)
1769 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1770 int i, left = fileio->buffer_size;
1771 DWORD *last_entry_offset = NULL;
1772 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1774 while (size && left >= sizeof(*pfni))
1776 /* convert to an NT style path */
1777 for (i = 0; i < event->len; i++)
1778 if (event->name[i] == '/') event->name[i] = '\\';
1780 pfni->Action = event->action;
1781 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1782 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1783 last_entry_offset = &pfni->NextEntryOffset;
1785 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1787 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1788 pfni->FileNameLength *= sizeof(WCHAR);
1789 pfni->NextEntryOffset = i;
1790 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1791 left -= i;
1793 i = (offsetof(struct filesystem_event, name[event->len])
1794 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1795 event = (struct filesystem_event*)((char*)event + i);
1796 size -= i;
1799 if (size)
1801 status = STATUS_NOTIFY_ENUM_DIR;
1802 size = 0;
1804 else
1806 if (last_entry_offset) *last_entry_offset = 0;
1807 size = fileio->buffer_size - left;
1810 else
1812 status = STATUS_NOTIFY_ENUM_DIR;
1813 size = 0;
1817 if (status != STATUS_PENDING)
1819 iosb->u.Status = status;
1820 iosb->Information = size;
1821 release_fileio( &fileio->io );
1823 return status;
1826 #define FILE_NOTIFY_ALL ( \
1827 FILE_NOTIFY_CHANGE_FILE_NAME | \
1828 FILE_NOTIFY_CHANGE_DIR_NAME | \
1829 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1830 FILE_NOTIFY_CHANGE_SIZE | \
1831 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1832 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1833 FILE_NOTIFY_CHANGE_CREATION | \
1834 FILE_NOTIFY_CHANGE_SECURITY )
1836 /******************************************************************************
1837 * NtNotifyChangeDirectoryFile [NTDLL.@]
1839 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1840 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1841 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1843 struct read_changes_fileio *fileio;
1844 NTSTATUS status;
1845 ULONG size = max( 4096, buffer_size );
1847 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1848 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1850 if (!iosb) return STATUS_ACCESS_VIOLATION;
1851 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1853 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1854 read_changes_apc, handle );
1855 if (!fileio) return STATUS_NO_MEMORY;
1857 fileio->buffer = buffer;
1858 fileio->buffer_size = buffer_size;
1859 fileio->data_size = size;
1861 SERVER_START_REQ( read_directory_changes )
1863 req->filter = filter;
1864 req->want_data = (buffer != NULL);
1865 req->subtree = subtree;
1866 req->async = server_async( handle, &fileio->io, event, apc, apc_context, iosb );
1867 status = wine_server_call( req );
1869 SERVER_END_REQ;
1871 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1872 return status;
1875 /******************************************************************************
1876 * NtSetVolumeInformationFile [NTDLL.@]
1877 * ZwSetVolumeInformationFile [NTDLL.@]
1879 * Set volume information for an open file handle.
1881 * PARAMS
1882 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1883 * IoStatusBlock [O] Receives information about the operation on return
1884 * FsInformation [I] Source for volume information
1885 * Length [I] Size of FsInformation
1886 * FsInformationClass [I] Type of volume information to set
1888 * RETURNS
1889 * Success: 0. IoStatusBlock is updated.
1890 * Failure: An NTSTATUS error code describing the error.
1892 NTSTATUS WINAPI NtSetVolumeInformationFile(
1893 IN HANDLE FileHandle,
1894 PIO_STATUS_BLOCK IoStatusBlock,
1895 PVOID FsInformation,
1896 ULONG Length,
1897 FS_INFORMATION_CLASS FsInformationClass)
1899 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1900 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1901 return 0;
1904 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1905 static int futimens( int fd, const struct timespec spec[2] )
1907 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1909 #define HAVE_FUTIMENS
1910 #endif /* __ANDROID__ */
1912 #ifndef UTIME_OMIT
1913 #define UTIME_OMIT ((1 << 30) - 2)
1914 #endif
1916 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1918 NTSTATUS status = STATUS_SUCCESS;
1920 #ifdef HAVE_FUTIMENS
1921 struct timespec tv[2];
1923 tv[0].tv_sec = tv[1].tv_sec = 0;
1924 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1925 if (atime->QuadPart)
1927 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1928 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1930 if (mtime->QuadPart)
1932 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1933 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1935 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1937 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1938 struct timeval tv[2];
1939 struct stat st;
1941 if (!atime->QuadPart || !mtime->QuadPart)
1944 tv[0].tv_sec = tv[0].tv_usec = 0;
1945 tv[1].tv_sec = tv[1].tv_usec = 0;
1946 if (!fstat( fd, &st ))
1948 tv[0].tv_sec = st.st_atime;
1949 tv[1].tv_sec = st.st_mtime;
1950 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1951 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1952 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1953 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1954 #endif
1955 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1956 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1957 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1958 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1959 #endif
1962 if (atime->QuadPart)
1964 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1965 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1967 if (mtime->QuadPart)
1969 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1970 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1972 #ifdef HAVE_FUTIMES
1973 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1974 #elif defined(HAVE_FUTIMESAT)
1975 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1976 #endif
1978 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1979 FIXME( "setting file times not supported\n" );
1980 status = STATUS_NOT_IMPLEMENTED;
1981 #endif
1982 return status;
1985 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1986 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1988 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1989 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1990 RtlSecondsSince1970ToTime( st->st_atime, atime );
1991 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1992 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1993 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1994 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
1995 #endif
1996 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1997 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
1998 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1999 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2000 #endif
2001 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2002 atime->QuadPart += st->st_atim.tv_nsec / 100;
2003 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2004 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2005 #endif
2006 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2007 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2008 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2009 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2010 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2011 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2012 #endif
2013 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2014 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2015 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2016 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2017 #endif
2018 #else
2019 *creation = *mtime;
2020 #endif
2023 /* fill in the file information that depends on the stat and attribute info */
2024 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2025 FILE_INFORMATION_CLASS class )
2027 switch (class)
2029 case FileBasicInformation:
2031 FILE_BASIC_INFORMATION *info = ptr;
2033 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2034 &info->LastAccessTime, &info->CreationTime );
2035 info->FileAttributes = attr;
2037 break;
2038 case FileStandardInformation:
2040 FILE_STANDARD_INFORMATION *info = ptr;
2042 if ((info->Directory = S_ISDIR(st->st_mode)))
2044 info->AllocationSize.QuadPart = 0;
2045 info->EndOfFile.QuadPart = 0;
2046 info->NumberOfLinks = 1;
2048 else
2050 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2051 info->EndOfFile.QuadPart = st->st_size;
2052 info->NumberOfLinks = st->st_nlink;
2055 break;
2056 case FileInternalInformation:
2058 FILE_INTERNAL_INFORMATION *info = ptr;
2059 info->IndexNumber.QuadPart = st->st_ino;
2061 break;
2062 case FileEndOfFileInformation:
2064 FILE_END_OF_FILE_INFORMATION *info = ptr;
2065 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2067 break;
2068 case FileAllInformation:
2070 FILE_ALL_INFORMATION *info = ptr;
2071 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2072 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2073 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2075 break;
2076 /* all directory structures start with the FileDirectoryInformation layout */
2077 case FileBothDirectoryInformation:
2078 case FileFullDirectoryInformation:
2079 case FileDirectoryInformation:
2081 FILE_DIRECTORY_INFORMATION *info = ptr;
2083 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2084 &info->LastAccessTime, &info->CreationTime );
2085 if (S_ISDIR(st->st_mode))
2087 info->AllocationSize.QuadPart = 0;
2088 info->EndOfFile.QuadPart = 0;
2090 else
2092 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2093 info->EndOfFile.QuadPart = st->st_size;
2095 info->FileAttributes = attr;
2097 break;
2098 case FileIdFullDirectoryInformation:
2100 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2101 info->FileId.QuadPart = st->st_ino;
2102 fill_file_info( st, attr, info, FileDirectoryInformation );
2104 break;
2105 case FileIdBothDirectoryInformation:
2107 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2108 info->FileId.QuadPart = st->st_ino;
2109 fill_file_info( st, attr, info, FileDirectoryInformation );
2111 break;
2112 case FileIdGlobalTxDirectoryInformation:
2114 FILE_ID_GLOBAL_TX_DIR_INFORMATION *info = ptr;
2115 info->FileId.QuadPart = st->st_ino;
2116 fill_file_info( st, attr, info, FileDirectoryInformation );
2118 break;
2120 default:
2121 return STATUS_INVALID_INFO_CLASS;
2123 return STATUS_SUCCESS;
2126 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2128 data_size_t size = 1024;
2129 NTSTATUS ret;
2130 char *name;
2132 for (;;)
2134 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2135 if (!name) return STATUS_NO_MEMORY;
2136 unix_name->MaximumLength = size + 1;
2138 SERVER_START_REQ( get_handle_unix_name )
2140 req->handle = wine_server_obj_handle( handle );
2141 wine_server_set_reply( req, name, size );
2142 ret = wine_server_call( req );
2143 size = reply->name_len;
2145 SERVER_END_REQ;
2147 if (!ret)
2149 name[size] = 0;
2150 unix_name->Buffer = name;
2151 unix_name->Length = size;
2152 break;
2154 RtlFreeHeap( GetProcessHeap(), 0, name );
2155 if (ret != STATUS_BUFFER_OVERFLOW) break;
2157 return ret;
2160 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2162 UNICODE_STRING nt_name;
2163 NTSTATUS status;
2165 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2167 const WCHAR *ptr = nt_name.Buffer;
2168 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2170 /* Skip the volume mount point. */
2171 while (ptr != end && *ptr == '\\') ++ptr;
2172 while (ptr != end && *ptr != '\\') ++ptr;
2173 while (ptr != end && *ptr == '\\') ++ptr;
2174 while (ptr != end && *ptr != '\\') ++ptr;
2176 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2177 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2178 else *name_len = info->FileNameLength;
2180 memcpy( info->FileName, ptr, *name_len );
2181 RtlFreeUnicodeString( &nt_name );
2184 return status;
2187 static NTSTATUS server_get_file_info( HANDLE handle, IO_STATUS_BLOCK *io, void *buffer,
2188 ULONG length, FILE_INFORMATION_CLASS info_class )
2190 SERVER_START_REQ( get_file_info )
2192 req->handle = wine_server_obj_handle( handle );
2193 req->info_class = info_class;
2194 wine_server_set_reply( req, buffer, length );
2195 io->u.Status = wine_server_call( req );
2196 io->Information = wine_server_reply_size( reply );
2198 SERVER_END_REQ;
2199 if (io->u.Status == STATUS_NOT_IMPLEMENTED)
2200 FIXME( "Unsupported info class %x\n", info_class );
2201 return io->u.Status;
2205 /******************************************************************************
2206 * NtQueryInformationFile [NTDLL.@]
2207 * ZwQueryInformationFile [NTDLL.@]
2209 * Get information about an open file handle.
2211 * PARAMS
2212 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2213 * io [O] Receives information about the operation on return
2214 * ptr [O] Destination for file information
2215 * len [I] Size of FileInformation
2216 * class [I] Type of file information to get
2218 * RETURNS
2219 * Success: 0. IoStatusBlock and FileInformation are updated.
2220 * Failure: An NTSTATUS error code describing the error.
2222 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2223 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2225 static const size_t info_sizes[] =
2228 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2229 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2230 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2231 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2232 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2233 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2234 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2235 0, /* FileAccessInformation */
2236 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2237 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2238 0, /* FileLinkInformation */
2239 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2240 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2241 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2242 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2243 0, /* FileModeInformation */
2244 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2245 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2246 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2247 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2248 0, /* FileAlternateNameInformation */
2249 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2250 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2251 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2252 0, /* FilePipeRemoteInformation */
2253 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2254 0, /* FileMailslotSetInformation */
2255 0, /* FileCompressionInformation */
2256 0, /* FileObjectIdInformation */
2257 0, /* FileCompletionInformation */
2258 0, /* FileMoveClusterInformation */
2259 0, /* FileQuotaInformation */
2260 0, /* FileReparsePointInformation */
2261 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2262 0, /* FileAttributeTagInformation */
2263 0, /* FileTrackingInformation */
2264 0, /* FileIdBothDirectoryInformation */
2265 0, /* FileIdFullDirectoryInformation */
2266 0, /* FileValidDataLengthInformation */
2267 0, /* FileShortNameInformation */
2268 0, /* FileIoCompletionNotificationInformation, */
2269 0, /* FileIoStatusBlockRangeInformation */
2270 0, /* FileIoPriorityHintInformation */
2271 0, /* FileSfioReserveInformation */
2272 0, /* FileSfioVolumeInformation */
2273 0, /* FileHardLinkInformation */
2274 0, /* FileProcessIdsUsingFileInformation */
2275 0, /* FileNormalizedNameInformation */
2276 0, /* FileNetworkPhysicalNameInformation */
2277 0, /* FileIdGlobalTxDirectoryInformation */
2278 0, /* FileIsRemoteDeviceInformation */
2279 0, /* FileAttributeCacheInformation */
2280 0, /* FileNumaNodeInformation */
2281 0, /* FileStandardLinkInformation */
2282 0, /* FileRemoteProtocolInformation */
2283 0, /* FileRenameInformationBypassAccessCheck */
2284 0, /* FileLinkInformationBypassAccessCheck */
2285 0, /* FileVolumeNameInformation */
2286 sizeof(FILE_ID_INFORMATION), /* FileIdInformation */
2287 0, /* FileIdExtdDirectoryInformation */
2288 0, /* FileReplaceCompletionInformation */
2289 0, /* FileHardLinkFullIdInformation */
2290 0, /* FileIdExtdBothDirectoryInformation */
2293 struct stat st;
2294 int fd, needs_close = FALSE;
2295 ULONG attr;
2297 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2299 io->Information = 0;
2301 if (class <= 0 || class >= FileMaximumInformation)
2302 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2303 if (!info_sizes[class])
2304 return server_get_file_info( hFile, io, ptr, len, class );
2305 if (len < info_sizes[class])
2306 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2308 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2310 if (io->u.Status != STATUS_BAD_DEVICE_TYPE) return io->u.Status;
2311 return server_get_file_info( hFile, io, ptr, len, class );
2314 switch (class)
2316 case FileBasicInformation:
2317 if (fd_get_file_info( fd, &st, &attr ) == -1)
2318 io->u.Status = FILE_GetNtStatus();
2319 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2320 io->u.Status = STATUS_INVALID_INFO_CLASS;
2321 else
2322 fill_file_info( &st, attr, ptr, class );
2323 break;
2324 case FileStandardInformation:
2326 FILE_STANDARD_INFORMATION *info = ptr;
2328 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2329 else
2331 fill_file_info( &st, attr, info, class );
2332 info->DeletePending = FALSE; /* FIXME */
2335 break;
2336 case FilePositionInformation:
2338 FILE_POSITION_INFORMATION *info = ptr;
2339 off_t res = lseek( fd, 0, SEEK_CUR );
2340 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2341 else info->CurrentByteOffset.QuadPart = res;
2343 break;
2344 case FileInternalInformation:
2345 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2346 else fill_file_info( &st, attr, ptr, class );
2347 break;
2348 case FileEaInformation:
2350 FILE_EA_INFORMATION *info = ptr;
2351 info->EaSize = 0;
2353 break;
2354 case FileEndOfFileInformation:
2355 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2356 else fill_file_info( &st, attr, ptr, class );
2357 break;
2358 case FileAllInformation:
2360 FILE_ALL_INFORMATION *info = ptr;
2361 ANSI_STRING unix_name;
2363 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2364 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2365 io->u.Status = STATUS_INVALID_INFO_CLASS;
2366 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2368 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2370 fill_file_info( &st, attr, info, FileAllInformation );
2371 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2372 info->EaInformation.EaSize = 0;
2373 info->AccessInformation.AccessFlags = 0; /* FIXME */
2374 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2375 info->ModeInformation.Mode = 0; /* FIXME */
2376 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2378 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2379 RtlFreeAnsiString( &unix_name );
2380 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2383 break;
2384 case FileMailslotQueryInformation:
2386 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2388 SERVER_START_REQ( set_mailslot_info )
2390 req->handle = wine_server_obj_handle( hFile );
2391 req->flags = 0;
2392 io->u.Status = wine_server_call( req );
2393 if( io->u.Status == STATUS_SUCCESS )
2395 info->MaximumMessageSize = reply->max_msgsize;
2396 info->MailslotQuota = 0;
2397 info->NextMessageSize = 0;
2398 info->MessagesAvailable = 0;
2399 info->ReadTimeout.QuadPart = reply->read_timeout;
2402 SERVER_END_REQ;
2403 if (!io->u.Status)
2405 char *tmpbuf;
2406 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2407 if (size > 0x10000) size = 0x10000;
2408 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2410 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2412 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2413 info->MessagesAvailable = (res > 0);
2414 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2415 if (needs_close) close( fd );
2417 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2421 break;
2422 case FileNameInformation:
2424 FILE_NAME_INFORMATION *info = ptr;
2425 ANSI_STRING unix_name;
2427 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2429 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2430 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2431 RtlFreeAnsiString( &unix_name );
2432 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2435 break;
2436 case FileNetworkOpenInformation:
2438 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2439 ANSI_STRING unix_name;
2441 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2443 ULONG attributes;
2444 struct stat st;
2446 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2447 io->u.Status = FILE_GetNtStatus();
2448 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2449 io->u.Status = STATUS_INVALID_INFO_CLASS;
2450 else
2452 FILE_BASIC_INFORMATION basic;
2453 FILE_STANDARD_INFORMATION std;
2455 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2456 fill_file_info( &st, attributes, &std, FileStandardInformation );
2458 info->CreationTime = basic.CreationTime;
2459 info->LastAccessTime = basic.LastAccessTime;
2460 info->LastWriteTime = basic.LastWriteTime;
2461 info->ChangeTime = basic.ChangeTime;
2462 info->AllocationSize = std.AllocationSize;
2463 info->EndOfFile = std.EndOfFile;
2464 info->FileAttributes = basic.FileAttributes;
2466 RtlFreeAnsiString( &unix_name );
2469 break;
2470 case FileIdInformation:
2471 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2472 else
2474 FILE_ID_INFORMATION *info = ptr;
2475 info->VolumeSerialNumber = 0; /* FIXME */
2476 memset( &info->FileId, 0, sizeof(info->FileId) );
2477 *(ULONGLONG *)&info->FileId = st.st_ino;
2479 break;
2480 default:
2481 FIXME("Unsupported class (%d)\n", class);
2482 io->u.Status = STATUS_NOT_IMPLEMENTED;
2483 break;
2485 if (needs_close) close( fd );
2486 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2487 return io->u.Status;
2490 /******************************************************************************
2491 * NtSetInformationFile [NTDLL.@]
2492 * ZwSetInformationFile [NTDLL.@]
2494 * Set information about an open file handle.
2496 * PARAMS
2497 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2498 * io [O] Receives information about the operation on return
2499 * ptr [I] Source for file information
2500 * len [I] Size of FileInformation
2501 * class [I] Type of file information to set
2503 * RETURNS
2504 * Success: 0. io is updated.
2505 * Failure: An NTSTATUS error code describing the error.
2507 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2508 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2510 int fd, needs_close;
2512 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2514 io->u.Status = STATUS_SUCCESS;
2515 switch (class)
2517 case FileBasicInformation:
2518 if (len >= sizeof(FILE_BASIC_INFORMATION))
2520 struct stat st;
2521 const FILE_BASIC_INFORMATION *info = ptr;
2523 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2524 return io->u.Status;
2526 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2527 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2529 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2531 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2532 else
2534 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2536 if (S_ISDIR( st.st_mode))
2537 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2538 else
2539 st.st_mode &= ~0222; /* clear write permission bits */
2541 else
2543 /* add write permission only where we already have read permission */
2544 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2546 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2550 if (needs_close) close( fd );
2552 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2553 break;
2555 case FilePositionInformation:
2556 if (len >= sizeof(FILE_POSITION_INFORMATION))
2558 const FILE_POSITION_INFORMATION *info = ptr;
2560 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2561 return io->u.Status;
2563 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2564 io->u.Status = FILE_GetNtStatus();
2566 if (needs_close) close( fd );
2568 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2569 break;
2571 case FileEndOfFileInformation:
2572 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2574 struct stat st;
2575 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2577 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2578 return io->u.Status;
2580 /* first try normal truncate */
2581 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2583 /* now check for the need to extend the file */
2584 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2586 static const char zero;
2588 /* extend the file one byte beyond the requested size and then truncate it */
2589 /* this should work around ftruncate implementations that can't extend files */
2590 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2591 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2593 io->u.Status = FILE_GetNtStatus();
2595 if (needs_close) close( fd );
2597 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2598 break;
2600 case FilePipeInformation:
2601 if (len >= sizeof(FILE_PIPE_INFORMATION))
2603 FILE_PIPE_INFORMATION *info = ptr;
2605 if ((info->CompletionMode | info->ReadMode) & ~1)
2607 io->u.Status = STATUS_INVALID_PARAMETER;
2608 break;
2611 SERVER_START_REQ( set_named_pipe_info )
2613 req->handle = wine_server_obj_handle( handle );
2614 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2615 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2616 io->u.Status = wine_server_call( req );
2618 SERVER_END_REQ;
2620 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2621 break;
2623 case FileMailslotSetInformation:
2625 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2627 SERVER_START_REQ( set_mailslot_info )
2629 req->handle = wine_server_obj_handle( handle );
2630 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2631 req->read_timeout = info->ReadTimeout.QuadPart;
2632 io->u.Status = wine_server_call( req );
2634 SERVER_END_REQ;
2636 break;
2638 case FileCompletionInformation:
2639 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2641 FILE_COMPLETION_INFORMATION *info = ptr;
2643 SERVER_START_REQ( set_completion_info )
2645 req->handle = wine_server_obj_handle( handle );
2646 req->chandle = wine_server_obj_handle( info->CompletionPort );
2647 req->ckey = info->CompletionKey;
2648 io->u.Status = wine_server_call( req );
2650 SERVER_END_REQ;
2651 } else
2652 io->u.Status = STATUS_INVALID_PARAMETER_3;
2653 break;
2655 case FileIoCompletionNotificationInformation:
2656 if (len >= sizeof(FILE_IO_COMPLETION_NOTIFICATION_INFORMATION))
2658 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION *info = ptr;
2660 if (info->Flags & FILE_SKIP_SET_USER_EVENT_ON_FAST_IO)
2661 FIXME( "FILE_SKIP_SET_USER_EVENT_ON_FAST_IO not supported\n" );
2663 SERVER_START_REQ( set_fd_completion_mode )
2665 req->handle = wine_server_obj_handle( handle );
2666 req->flags = info->Flags;
2667 io->u.Status = wine_server_call( req );
2669 SERVER_END_REQ;
2670 } else
2671 io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2672 break;
2674 case FileIoPriorityHintInformation:
2675 if (len >= sizeof(FILE_IO_PRIORITY_HINT_INFO))
2677 FILE_IO_PRIORITY_HINT_INFO *info = ptr;
2678 if (info->PriorityHint < MaximumIoPriorityHintType)
2679 TRACE( "ignoring FileIoPriorityHintInformation %u\n", info->PriorityHint );
2680 else
2681 io->u.Status = STATUS_INVALID_PARAMETER;
2683 else io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2684 break;
2686 case FileAllInformation:
2687 io->u.Status = STATUS_INVALID_INFO_CLASS;
2688 break;
2690 case FileValidDataLengthInformation:
2691 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2693 struct stat st;
2694 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2696 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2697 return io->u.Status;
2699 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2700 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2701 io->u.Status = STATUS_INVALID_PARAMETER;
2702 else
2704 #ifdef HAVE_FALLOCATE
2705 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2707 NTSTATUS status = FILE_GetNtStatus();
2708 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2709 else io->u.Status = status;
2711 #else
2712 FIXME( "setting valid data length not supported\n" );
2713 #endif
2715 if (needs_close) close( fd );
2717 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2718 break;
2720 case FileDispositionInformation:
2721 if (len >= sizeof(FILE_DISPOSITION_INFORMATION))
2723 FILE_DISPOSITION_INFORMATION *info = ptr;
2725 SERVER_START_REQ( set_fd_disp_info )
2727 req->handle = wine_server_obj_handle( handle );
2728 req->unlink = info->DoDeleteFile;
2729 io->u.Status = wine_server_call( req );
2731 SERVER_END_REQ;
2732 } else
2733 io->u.Status = STATUS_INVALID_PARAMETER_3;
2734 break;
2736 case FileRenameInformation:
2737 if (len >= sizeof(FILE_RENAME_INFORMATION))
2739 FILE_RENAME_INFORMATION *info = ptr;
2740 UNICODE_STRING name_str;
2741 OBJECT_ATTRIBUTES attr;
2742 ANSI_STRING unix_name;
2744 name_str.Buffer = info->FileName;
2745 name_str.Length = info->FileNameLength;
2746 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2748 attr.Length = sizeof(attr);
2749 attr.ObjectName = &name_str;
2750 attr.RootDirectory = info->RootDir;
2751 attr.Attributes = OBJ_CASE_INSENSITIVE;
2753 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2754 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2755 break;
2757 if (!info->Replace && io->u.Status == STATUS_SUCCESS)
2759 RtlFreeAnsiString( &unix_name );
2760 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2761 break;
2764 SERVER_START_REQ( set_fd_name_info )
2766 req->handle = wine_server_obj_handle( handle );
2767 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2768 req->link = FALSE;
2769 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2770 io->u.Status = wine_server_call( req );
2772 SERVER_END_REQ;
2774 RtlFreeAnsiString( &unix_name );
2776 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2777 break;
2779 case FileLinkInformation:
2780 if (len >= sizeof(FILE_LINK_INFORMATION))
2782 FILE_LINK_INFORMATION *info = ptr;
2783 UNICODE_STRING name_str;
2784 OBJECT_ATTRIBUTES attr;
2785 ANSI_STRING unix_name;
2787 name_str.Buffer = info->FileName;
2788 name_str.Length = info->FileNameLength;
2789 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2791 attr.Length = sizeof(attr);
2792 attr.ObjectName = &name_str;
2793 attr.RootDirectory = info->RootDirectory;
2794 attr.Attributes = OBJ_CASE_INSENSITIVE;
2796 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2797 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2798 break;
2800 if (!info->ReplaceIfExists && io->u.Status == STATUS_SUCCESS)
2802 RtlFreeAnsiString( &unix_name );
2803 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2804 break;
2807 SERVER_START_REQ( set_fd_name_info )
2809 req->handle = wine_server_obj_handle( handle );
2810 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2811 req->link = TRUE;
2812 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2813 io->u.Status = wine_server_call( req );
2815 SERVER_END_REQ;
2817 RtlFreeAnsiString( &unix_name );
2819 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2820 break;
2822 default:
2823 FIXME("Unsupported class (%d)\n", class);
2824 io->u.Status = STATUS_NOT_IMPLEMENTED;
2825 break;
2827 io->Information = 0;
2828 return io->u.Status;
2832 /******************************************************************************
2833 * NtQueryFullAttributesFile (NTDLL.@)
2835 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2836 FILE_NETWORK_OPEN_INFORMATION *info )
2838 ANSI_STRING unix_name;
2839 NTSTATUS status;
2841 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2843 ULONG attributes;
2844 struct stat st;
2846 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2847 status = FILE_GetNtStatus();
2848 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2849 status = STATUS_INVALID_INFO_CLASS;
2850 else
2852 FILE_BASIC_INFORMATION basic;
2853 FILE_STANDARD_INFORMATION std;
2855 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2856 fill_file_info( &st, attributes, &std, FileStandardInformation );
2858 info->CreationTime = basic.CreationTime;
2859 info->LastAccessTime = basic.LastAccessTime;
2860 info->LastWriteTime = basic.LastWriteTime;
2861 info->ChangeTime = basic.ChangeTime;
2862 info->AllocationSize = std.AllocationSize;
2863 info->EndOfFile = std.EndOfFile;
2864 info->FileAttributes = basic.FileAttributes;
2865 if (DIR_is_hidden_file( attr->ObjectName ))
2866 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2868 RtlFreeAnsiString( &unix_name );
2870 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2871 return status;
2875 /******************************************************************************
2876 * NtQueryAttributesFile (NTDLL.@)
2877 * ZwQueryAttributesFile (NTDLL.@)
2879 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2881 ANSI_STRING unix_name;
2882 NTSTATUS status;
2884 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2886 ULONG attributes;
2887 struct stat st;
2889 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2890 status = FILE_GetNtStatus();
2891 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2892 status = STATUS_INVALID_INFO_CLASS;
2893 else
2895 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2896 if (DIR_is_hidden_file( attr->ObjectName ))
2897 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2899 RtlFreeAnsiString( &unix_name );
2901 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2902 return status;
2906 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2907 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2908 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2909 unsigned int flags )
2911 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2913 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2914 /* Don't assume read-only, let the mount options set it below */
2915 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2917 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2918 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2920 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2921 info->Characteristics |= FILE_REMOTE_DEVICE;
2923 else if (!strcmp("procfs", fstypename))
2924 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2925 else
2926 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2928 if (flags & MNT_RDONLY)
2929 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2931 if (!(flags & MNT_LOCAL))
2933 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2934 info->Characteristics |= FILE_REMOTE_DEVICE;
2937 #endif
2939 static inline BOOL is_device_placeholder( int fd )
2941 static const char wine_placeholder[] = "Wine device placeholder";
2942 char buffer[sizeof(wine_placeholder)-1];
2944 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2945 return FALSE;
2946 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2949 /******************************************************************************
2950 * get_device_info
2952 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2954 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2956 struct stat st;
2958 info->Characteristics = 0;
2959 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2960 if (S_ISCHR( st.st_mode ))
2962 info->DeviceType = FILE_DEVICE_UNKNOWN;
2963 #ifdef linux
2964 switch(major(st.st_rdev))
2966 case MEM_MAJOR:
2967 info->DeviceType = FILE_DEVICE_NULL;
2968 break;
2969 case TTY_MAJOR:
2970 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2971 break;
2972 case LP_MAJOR:
2973 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2974 break;
2975 case SCSI_TAPE_MAJOR:
2976 info->DeviceType = FILE_DEVICE_TAPE;
2977 break;
2979 #endif
2981 else if (S_ISBLK( st.st_mode ))
2983 info->DeviceType = FILE_DEVICE_DISK;
2985 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2987 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2989 else if (is_device_placeholder( fd ))
2991 info->DeviceType = FILE_DEVICE_DISK;
2993 else /* regular file or directory */
2995 #if defined(linux) && defined(HAVE_FSTATFS)
2996 struct statfs stfs;
2998 /* check for floppy disk */
2999 if (major(st.st_dev) == FLOPPY_MAJOR)
3000 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3002 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
3003 switch (stfs.f_type)
3005 case 0x9660: /* iso9660 */
3006 case 0x9fa1: /* supermount */
3007 case 0x15013346: /* udf */
3008 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3009 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3010 break;
3011 case 0x6969: /* nfs */
3012 case 0xff534d42: /* cifs */
3013 case 0xfe534d42: /* smb2 */
3014 case 0x517b: /* smbfs */
3015 case 0x564c: /* ncpfs */
3016 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3017 info->Characteristics |= FILE_REMOTE_DEVICE;
3018 break;
3019 case 0x01021994: /* tmpfs */
3020 case 0x28cd3d45: /* cramfs */
3021 case 0x1373: /* devfs */
3022 case 0x9fa0: /* procfs */
3023 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3024 break;
3025 default:
3026 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3027 break;
3029 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3030 struct statfs stfs;
3032 if (fstatfs( fd, &stfs ) < 0)
3033 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3034 else
3035 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
3036 #elif defined(__NetBSD__)
3037 struct statvfs stfs;
3039 if (fstatvfs( fd, &stfs) < 0)
3040 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3041 else
3042 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
3043 #elif defined(sun)
3044 /* Use dkio to work out device types */
3046 # include <sys/dkio.h>
3047 # include <sys/vtoc.h>
3048 struct dk_cinfo dkinf;
3049 int retval = ioctl(fd, DKIOCINFO, &dkinf);
3050 if(retval==-1){
3051 WARN("Unable to get disk device type information - assuming a disk like device\n");
3052 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3054 switch (dkinf.dki_ctype)
3056 case DKC_CDROM:
3057 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3058 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3059 break;
3060 case DKC_NCRFLOPPY:
3061 case DKC_SMSFLOPPY:
3062 case DKC_INTEL82072:
3063 case DKC_INTEL82077:
3064 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3065 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3066 break;
3067 case DKC_MD:
3068 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3069 break;
3070 default:
3071 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3074 #else
3075 static int warned;
3076 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3077 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3078 #endif
3079 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3081 return STATUS_SUCCESS;
3085 /******************************************************************************
3086 * NtQueryVolumeInformationFile [NTDLL.@]
3087 * ZwQueryVolumeInformationFile [NTDLL.@]
3089 * Get volume information for an open file handle.
3091 * PARAMS
3092 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3093 * io [O] Receives information about the operation on return
3094 * buffer [O] Destination for volume information
3095 * length [I] Size of FsInformation
3096 * info_class [I] Type of volume information to set
3098 * RETURNS
3099 * Success: 0. io and buffer are updated.
3100 * Failure: An NTSTATUS error code describing the error.
3102 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3103 PVOID buffer, ULONG length,
3104 FS_INFORMATION_CLASS info_class )
3106 int fd, needs_close;
3107 struct stat st;
3108 static int once;
3110 io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL );
3111 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
3113 SERVER_START_REQ( get_volume_info )
3115 req->handle = wine_server_obj_handle( handle );
3116 req->info_class = info_class;
3117 wine_server_set_reply( req, buffer, length );
3118 io->u.Status = wine_server_call( req );
3119 if (!io->u.Status) io->Information = wine_server_reply_size( reply );
3121 SERVER_END_REQ;
3122 return io->u.Status;
3124 else if (io->u.Status) return io->u.Status;
3126 io->u.Status = STATUS_NOT_IMPLEMENTED;
3127 io->Information = 0;
3129 switch( info_class )
3131 case FileFsVolumeInformation:
3132 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3133 break;
3134 case FileFsLabelInformation:
3135 FIXME( "%p: label info not supported\n", handle );
3136 break;
3137 case FileFsSizeInformation:
3138 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3139 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3140 else
3142 FILE_FS_SIZE_INFORMATION *info = buffer;
3144 if (fstat( fd, &st ) < 0)
3146 io->u.Status = FILE_GetNtStatus();
3147 break;
3149 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3151 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3153 else
3155 ULONGLONG bsize;
3156 /* Linux's fstatvfs is buggy */
3157 #if !defined(linux) || !defined(HAVE_FSTATFS)
3158 struct statvfs stfs;
3160 if (fstatvfs( fd, &stfs ) < 0)
3162 io->u.Status = FILE_GetNtStatus();
3163 break;
3165 bsize = stfs.f_frsize;
3166 #else
3167 struct statfs stfs;
3168 if (fstatfs( fd, &stfs ) < 0)
3170 io->u.Status = FILE_GetNtStatus();
3171 break;
3173 bsize = stfs.f_bsize;
3174 #endif
3175 if (bsize == 2048) /* assume CD-ROM */
3177 info->BytesPerSector = 2048;
3178 info->SectorsPerAllocationUnit = 1;
3180 else
3182 info->BytesPerSector = 512;
3183 info->SectorsPerAllocationUnit = 8;
3185 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3186 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3187 io->Information = sizeof(*info);
3188 io->u.Status = STATUS_SUCCESS;
3191 break;
3192 case FileFsDeviceInformation:
3193 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3194 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3195 else
3197 FILE_FS_DEVICE_INFORMATION *info = buffer;
3199 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3200 io->Information = sizeof(*info);
3202 break;
3203 case FileFsAttributeInformation:
3204 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[ARRAY_SIZE( ntfsW )] ))
3205 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3206 else
3208 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3210 FIXME( "%p: faking attribute info\n", handle );
3211 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3212 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3213 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3214 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3215 info->FileSystemNameLength = sizeof(ntfsW);
3216 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3218 io->Information = sizeof(*info);
3219 io->u.Status = STATUS_SUCCESS;
3221 break;
3222 case FileFsControlInformation:
3223 FIXME( "%p: control info not supported\n", handle );
3224 break;
3225 case FileFsFullSizeInformation:
3226 FIXME( "%p: full size info not supported\n", handle );
3227 break;
3228 case FileFsObjectIdInformation:
3229 FIXME( "%p: object id info not supported\n", handle );
3230 break;
3231 case FileFsMaximumInformation:
3232 FIXME( "%p: maximum info not supported\n", handle );
3233 break;
3234 default:
3235 io->u.Status = STATUS_INVALID_PARAMETER;
3236 break;
3238 if (needs_close) close( fd );
3239 return io->u.Status;
3243 /******************************************************************
3244 * NtQueryEaFile (NTDLL.@)
3246 * Read extended attributes from NTFS files.
3248 * PARAMS
3249 * hFile [I] File handle, must be opened with FILE_READ_EA access
3250 * iosb [O] Receives information about the operation on return
3251 * buffer [O] Output buffer
3252 * length [I] Length of output buffer
3253 * single_entry [I] Only read and return one entry
3254 * ea_list [I] Optional list with names of EAs to return
3255 * ea_list_len [I] Length of ea_list in bytes
3256 * ea_index [I] Optional pointer to 1-based index of attribute to return
3257 * restart [I] restart EA scan
3259 * RETURNS
3260 * Success: 0. Atrributes read into buffer
3261 * Failure: An NTSTATUS error code describing the error.
3263 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3264 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3265 PULONG ea_index, BOOLEAN restart )
3267 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3268 hFile, iosb, buffer, length, single_entry, ea_list,
3269 ea_list_len, ea_index, restart);
3270 return STATUS_ACCESS_DENIED;
3274 /******************************************************************
3275 * NtSetEaFile (NTDLL.@)
3277 * Update extended attributes for NTFS files.
3279 * PARAMS
3280 * hFile [I] File handle, must be opened with FILE_READ_EA access
3281 * iosb [O] Receives information about the operation on return
3282 * buffer [I] Buffer with EA information
3283 * length [I] Length of buffer
3285 * RETURNS
3286 * Success: 0. Attributes are updated
3287 * Failure: An NTSTATUS error code describing the error.
3289 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3291 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3292 return STATUS_ACCESS_DENIED;
3296 /******************************************************************
3297 * NtFlushBuffersFile (NTDLL.@)
3299 * Flush any buffered data on an open file handle.
3301 * PARAMS
3302 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3303 * IoStatusBlock [O] Receives information about the operation on return
3305 * RETURNS
3306 * Success: 0. IoStatusBlock is updated.
3307 * Failure: An NTSTATUS error code describing the error.
3309 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK *io )
3311 NTSTATUS ret;
3312 HANDLE wait_handle;
3313 enum server_fd_type type;
3314 int fd, needs_close;
3316 if (!io || !virtual_check_buffer_for_write( io, sizeof(*io) )) return STATUS_ACCESS_VIOLATION;
3318 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3319 if (ret == STATUS_ACCESS_DENIED)
3320 ret = server_get_unix_fd( hFile, FILE_APPEND_DATA, &fd, &needs_close, &type, NULL );
3322 if (!ret && type == FD_TYPE_SERIAL)
3324 ret = COMM_FlushBuffersFile( fd );
3326 else if (ret != STATUS_ACCESS_DENIED)
3328 struct async_irp *async;
3330 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, hFile )))
3331 return STATUS_NO_MEMORY;
3332 async->buffer = NULL;
3333 async->size = 0;
3335 SERVER_START_REQ( flush )
3337 req->async = server_async( hFile, &async->io, NULL, NULL, NULL, io );
3338 ret = wine_server_call( req );
3339 wait_handle = wine_server_ptr_handle( reply->event );
3340 if (wait_handle && ret != STATUS_PENDING)
3342 io->u.Status = ret;
3343 io->Information = 0;
3346 SERVER_END_REQ;
3348 if (ret != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
3350 if (wait_handle)
3352 NtWaitForSingleObject( wait_handle, FALSE, NULL );
3353 ret = io->u.Status;
3357 if (needs_close) close( fd );
3358 return ret;
3361 /******************************************************************
3362 * NtLockFile (NTDLL.@)
3366 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3367 PIO_APC_ROUTINE apc, void* apc_user,
3368 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3369 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3370 BOOLEAN exclusive )
3372 NTSTATUS ret;
3373 HANDLE handle;
3374 BOOLEAN async;
3375 static BOOLEAN warn = TRUE;
3377 if (apc || io_status || key)
3379 FIXME("Unimplemented yet parameter\n");
3380 return STATUS_NOT_IMPLEMENTED;
3383 if (apc_user && warn)
3385 FIXME("I/O completion on lock not implemented yet\n");
3386 warn = FALSE;
3389 for (;;)
3391 SERVER_START_REQ( lock_file )
3393 req->handle = wine_server_obj_handle( hFile );
3394 req->offset = offset->QuadPart;
3395 req->count = count->QuadPart;
3396 req->shared = !exclusive;
3397 req->wait = !dont_wait;
3398 ret = wine_server_call( req );
3399 handle = wine_server_ptr_handle( reply->handle );
3400 async = reply->overlapped;
3402 SERVER_END_REQ;
3403 if (ret != STATUS_PENDING)
3405 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3406 return ret;
3409 if (async)
3411 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3412 if (handle) NtClose( handle );
3413 return STATUS_PENDING;
3415 if (handle)
3417 NtWaitForSingleObject( handle, FALSE, NULL );
3418 NtClose( handle );
3420 else
3422 LARGE_INTEGER time;
3424 /* Unix lock conflict, sleep a bit and retry */
3425 time.QuadPart = 100 * (ULONGLONG)10000;
3426 time.QuadPart = -time.QuadPart;
3427 NtDelayExecution( FALSE, &time );
3433 /******************************************************************
3434 * NtUnlockFile (NTDLL.@)
3438 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3439 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3440 PULONG key )
3442 NTSTATUS status;
3444 TRACE( "%p %x%08x %x%08x\n",
3445 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3447 if (io_status || key)
3449 FIXME("Unimplemented yet parameter\n");
3450 return STATUS_NOT_IMPLEMENTED;
3453 SERVER_START_REQ( unlock_file )
3455 req->handle = wine_server_obj_handle( hFile );
3456 req->offset = offset->QuadPart;
3457 req->count = count->QuadPart;
3458 status = wine_server_call( req );
3460 SERVER_END_REQ;
3461 return status;
3464 /******************************************************************
3465 * NtCreateNamedPipeFile (NTDLL.@)
3469 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3470 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3471 ULONG sharing, ULONG dispo, ULONG options,
3472 ULONG pipe_type, ULONG read_mode,
3473 ULONG completion_mode, ULONG max_inst,
3474 ULONG inbound_quota, ULONG outbound_quota,
3475 PLARGE_INTEGER timeout)
3477 NTSTATUS status;
3478 data_size_t len;
3479 struct object_attributes *objattr;
3481 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3482 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3483 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3484 outbound_quota, timeout);
3486 if (!attr) return STATUS_INVALID_PARAMETER;
3488 /* assume we only get relative timeout */
3489 if (timeout->QuadPart > 0)
3490 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3492 if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
3494 SERVER_START_REQ( create_named_pipe )
3496 req->access = access;
3497 req->options = options;
3498 req->sharing = sharing;
3499 req->flags =
3500 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3501 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3502 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3503 req->maxinstances = max_inst;
3504 req->outsize = outbound_quota;
3505 req->insize = inbound_quota;
3506 req->timeout = timeout->QuadPart;
3507 wine_server_add_data( req, objattr, len );
3508 status = wine_server_call( req );
3509 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3511 SERVER_END_REQ;
3513 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3514 return status;
3517 /******************************************************************
3518 * NtDeleteFile (NTDLL.@)
3522 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3524 NTSTATUS status;
3525 HANDLE hFile;
3526 IO_STATUS_BLOCK io;
3528 TRACE("%p\n", ObjectAttributes);
3529 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3530 ObjectAttributes, &io, NULL, 0,
3531 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3532 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3533 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3534 return status;
3537 /******************************************************************
3538 * NtCancelIoFileEx (NTDLL.@)
3542 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3544 TRACE("%p %p %p\n", hFile, iosb, io_status );
3546 SERVER_START_REQ( cancel_async )
3548 req->handle = wine_server_obj_handle( hFile );
3549 req->iosb = wine_server_client_ptr( iosb );
3550 req->only_thread = FALSE;
3551 io_status->u.Status = wine_server_call( req );
3553 SERVER_END_REQ;
3555 return io_status->u.Status;
3558 /******************************************************************
3559 * NtCancelIoFile (NTDLL.@)
3563 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3565 TRACE("%p %p\n", hFile, io_status );
3567 SERVER_START_REQ( cancel_async )
3569 req->handle = wine_server_obj_handle( hFile );
3570 req->iosb = 0;
3571 req->only_thread = TRUE;
3572 io_status->u.Status = wine_server_call( req );
3574 SERVER_END_REQ;
3576 return io_status->u.Status;
3579 /******************************************************************************
3580 * NtCreateMailslotFile [NTDLL.@]
3581 * ZwCreateMailslotFile [NTDLL.@]
3583 * PARAMS
3584 * pHandle [O] pointer to receive the handle created
3585 * DesiredAccess [I] access mode (read, write, etc)
3586 * ObjectAttributes [I] fully qualified NT path of the mailslot
3587 * IoStatusBlock [O] receives completion status and other info
3588 * CreateOptions [I]
3589 * MailslotQuota [I]
3590 * MaxMessageSize [I]
3591 * TimeOut [I]
3593 * RETURNS
3594 * An NT status code
3596 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3597 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3598 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3599 PLARGE_INTEGER TimeOut)
3601 LARGE_INTEGER timeout;
3602 NTSTATUS ret;
3603 data_size_t len;
3604 struct object_attributes *objattr;
3606 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3607 pHandle, DesiredAccess, attr, IoStatusBlock,
3608 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3610 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3611 if (!attr) return STATUS_INVALID_PARAMETER;
3613 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3616 * For a NULL TimeOut pointer set the default timeout value
3618 if (!TimeOut)
3619 timeout.QuadPart = -1;
3620 else
3621 timeout.QuadPart = TimeOut->QuadPart;
3623 SERVER_START_REQ( create_mailslot )
3625 req->access = DesiredAccess;
3626 req->max_msgsize = MaxMessageSize;
3627 req->read_timeout = timeout.QuadPart;
3628 wine_server_add_data( req, objattr, len );
3629 ret = wine_server_call( req );
3630 if( ret == STATUS_SUCCESS )
3631 *pHandle = wine_server_ptr_handle( reply->handle );
3633 SERVER_END_REQ;
3635 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3636 return ret;