ntoskrnl.exe: Add Ex[p]InterlockedFlushSList.
[wine.git] / dlls / ntdll / file.c
blob3dafdcfb4402081e255e3deed3121a694cc702e5
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, ret_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 ret_status = async_read && type == FD_TYPE_FILE && status == STATUS_SUCCESS
1017 ? STATUS_PENDING : status;
1019 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total, ret_status == STATUS_PENDING );
1020 return ret_status;
1024 /******************************************************************************
1025 * NtReadFileScatter [NTDLL.@]
1026 * ZwReadFileScatter [NTDLL.@]
1028 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1029 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1030 ULONG length, PLARGE_INTEGER offset, PULONG key )
1032 int result, unix_handle, needs_close;
1033 unsigned int options;
1034 NTSTATUS status;
1035 ULONG pos = 0, total = 0;
1036 enum server_fd_type type;
1037 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1038 BOOL send_completion = FALSE;
1040 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1041 file, event, apc, apc_user, io_status, segments, length, offset, key);
1043 if (!io_status) return STATUS_ACCESS_VIOLATION;
1045 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1046 &needs_close, &type, &options );
1047 if (status) return status;
1049 if ((type != FD_TYPE_FILE) ||
1050 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1051 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1053 status = STATUS_INVALID_PARAMETER;
1054 goto error;
1057 while (length)
1059 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1060 result = pread( unix_handle, (char *)segments->Buffer + pos,
1061 min( length - pos, page_size - pos ), offset->QuadPart + total );
1062 else
1063 result = read( unix_handle, (char *)segments->Buffer + pos, min( length - pos, page_size - pos ) );
1065 if (result == -1)
1067 if (errno == EINTR) continue;
1068 status = FILE_GetNtStatus();
1069 break;
1071 if (!result) break;
1072 total += result;
1073 length -= result;
1074 if ((pos += result) == page_size)
1076 pos = 0;
1077 segments++;
1081 if (total == 0) status = STATUS_END_OF_FILE;
1083 send_completion = cvalue != 0;
1085 if (needs_close) close( unix_handle );
1087 io_status->u.Status = status;
1088 io_status->Information = total;
1089 TRACE("= 0x%08x (%u)\n", status, total);
1090 if (event) NtSetEvent( event, NULL );
1091 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1092 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1093 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total, TRUE );
1095 return STATUS_PENDING;
1097 error:
1098 if (needs_close) close( unix_handle );
1100 TRACE("= 0x%08x\n", status);
1101 if (event) NtResetEvent( event, NULL );
1103 return status;
1107 /***********************************************************************
1108 * FILE_AsyncWriteService (INTERNAL)
1110 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1112 struct async_fileio_write *fileio = user;
1113 int result, fd, needs_close;
1114 enum server_fd_type type;
1116 switch (status)
1118 case STATUS_ALERTED:
1119 /* write some data (non-blocking) */
1120 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1121 &needs_close, &type, NULL )))
1122 break;
1124 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1125 result = send( fd, fileio->buffer, 0, 0 );
1126 else
1127 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1129 if (needs_close) close( fd );
1131 if (result < 0)
1133 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1134 else status = FILE_GetNtStatus();
1136 else
1138 fileio->already += result;
1139 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1141 break;
1143 case STATUS_TIMEOUT:
1144 case STATUS_IO_TIMEOUT:
1145 if (fileio->already) status = STATUS_SUCCESS;
1146 break;
1148 if (status != STATUS_PENDING)
1150 iosb->u.Status = status;
1151 iosb->Information = fileio->already;
1152 release_fileio( &fileio->io );
1154 return status;
1157 static NTSTATUS set_pending_write( HANDLE device )
1159 NTSTATUS status;
1161 SERVER_START_REQ( set_serial_info )
1163 req->handle = wine_server_obj_handle( device );
1164 req->flags = SERIALINFO_PENDING_WRITE;
1165 status = wine_server_call( req );
1167 SERVER_END_REQ;
1168 return status;
1171 /******************************************************************************
1172 * NtWriteFile [NTDLL.@]
1173 * ZwWriteFile [NTDLL.@]
1175 * Write to an open file handle.
1177 * PARAMS
1178 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1179 * Event [I] Event to signal upon completion (or NULL)
1180 * ApcRoutine [I] Callback to call upon completion (or NULL)
1181 * ApcContext [I] Context for ApcRoutine (or NULL)
1182 * IoStatusBlock [O] Receives information about the operation on return
1183 * Buffer [I] Source for the data to write
1184 * Length [I] Size of Buffer
1185 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1186 * Key [O] Function unknown (may be NULL)
1188 * RETURNS
1189 * Success: 0. IoStatusBlock is updated, and the Information member contains
1190 * The number of bytes written.
1191 * Failure: An NTSTATUS error code describing the error.
1193 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1194 PIO_APC_ROUTINE apc, void* apc_user,
1195 PIO_STATUS_BLOCK io_status,
1196 const void* buffer, ULONG length,
1197 PLARGE_INTEGER offset, PULONG key)
1199 int result, unix_handle, needs_close;
1200 unsigned int options;
1201 struct io_timeouts timeouts;
1202 NTSTATUS status, ret_status;
1203 ULONG total = 0;
1204 enum server_fd_type type;
1205 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1206 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1207 LARGE_INTEGER offset_eof;
1209 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)\n",
1210 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1212 if (!io_status) return STATUS_ACCESS_VIOLATION;
1214 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1215 &needs_close, &type, &options );
1216 if (status == STATUS_ACCESS_DENIED)
1218 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1219 &needs_close, &type, &options );
1220 append_write = TRUE;
1222 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
1224 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1226 if (!virtual_check_buffer_for_read( buffer, length ))
1228 status = STATUS_INVALID_USER_BUFFER;
1229 goto done;
1232 if (status == STATUS_BAD_DEVICE_TYPE)
1233 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1235 if (type == FD_TYPE_FILE)
1237 if (async_write &&
1238 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1240 status = STATUS_INVALID_PARAMETER;
1241 goto done;
1244 if (append_write)
1246 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1247 offset = &offset_eof;
1250 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1252 off_t off = offset->QuadPart;
1254 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1256 struct stat st;
1258 if (fstat( unix_handle, &st ) == -1)
1260 status = FILE_GetNtStatus();
1261 goto done;
1263 off = st.st_size;
1265 else if (offset->QuadPart < 0)
1267 status = STATUS_INVALID_PARAMETER;
1268 goto done;
1271 /* async I/O doesn't make sense on regular files */
1272 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1274 if (errno != EINTR)
1276 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1277 else status = FILE_GetNtStatus();
1278 goto done;
1282 if (!async_write)
1283 /* update file pointer position */
1284 lseek( unix_handle, off + result, SEEK_SET );
1286 total = result;
1287 status = STATUS_SUCCESS;
1288 goto done;
1291 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1293 if (async_write &&
1294 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1296 status = STATUS_INVALID_PARAMETER;
1297 goto done;
1301 for (;;)
1303 /* zero-length writes on sockets may not work with plain write(2) */
1304 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1305 result = send( unix_handle, buffer, 0, 0 );
1306 else
1307 result = write( unix_handle, (const char *)buffer + total, length - total );
1309 if (result >= 0)
1311 total += result;
1312 if (total == length)
1314 status = STATUS_SUCCESS;
1315 goto done;
1317 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1319 else if (errno != EAGAIN)
1321 if (errno == EINTR) continue;
1322 if (!total)
1324 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1325 else status = FILE_GetNtStatus();
1327 goto err;
1330 if (async_write)
1332 struct async_fileio_write *fileio;
1334 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), FILE_AsyncWriteService, hFile );
1335 if (!fileio)
1337 status = STATUS_NO_MEMORY;
1338 goto err;
1340 fileio->already = total;
1341 fileio->count = length;
1342 fileio->buffer = buffer;
1344 SERVER_START_REQ( register_async )
1346 req->type = ASYNC_TYPE_WRITE;
1347 req->count = length;
1348 req->async = server_async( hFile, &fileio->io, hEvent, apc, apc_user, io_status );
1349 status = wine_server_call( req );
1351 SERVER_END_REQ;
1353 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1354 goto err;
1356 else /* synchronous write, wait for the fd to become ready */
1358 struct pollfd pfd;
1359 int ret, timeout;
1361 if (!timeout_init_done)
1363 timeout_init_done = TRUE;
1364 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1365 goto err;
1366 if (hEvent) NtResetEvent( hEvent, NULL );
1368 timeout = get_next_io_timeout( &timeouts, total );
1370 pfd.fd = unix_handle;
1371 pfd.events = POLLOUT;
1373 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1375 /* return with what we got so far */
1376 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1377 goto done;
1379 if (ret == -1 && errno != EINTR)
1381 status = FILE_GetNtStatus();
1382 goto done;
1384 /* will now restart the write */
1388 done:
1389 send_completion = cvalue != 0;
1391 err:
1392 if (needs_close) close( unix_handle );
1394 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1395 set_pending_write( hFile );
1397 if (status == STATUS_SUCCESS)
1399 io_status->u.Status = status;
1400 io_status->Information = total;
1401 TRACE("= SUCCESS (%u)\n", total);
1402 if (hEvent) NtSetEvent( hEvent, NULL );
1403 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1404 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1406 else
1408 TRACE("= 0x%08x\n", status);
1409 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1412 ret_status = async_write && type == FD_TYPE_FILE && status == STATUS_SUCCESS ? STATUS_PENDING : status;
1413 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total, ret_status == STATUS_PENDING );
1415 return ret_status;
1419 /******************************************************************************
1420 * NtWriteFileGather [NTDLL.@]
1421 * ZwWriteFileGather [NTDLL.@]
1423 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1424 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1425 ULONG length, PLARGE_INTEGER offset, PULONG key )
1427 int result, unix_handle, needs_close;
1428 unsigned int options;
1429 NTSTATUS status;
1430 ULONG pos = 0, total = 0;
1431 enum server_fd_type type;
1432 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1433 BOOL send_completion = FALSE;
1435 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1436 file, event, apc, apc_user, io_status, segments, length, offset, key);
1438 if (length % page_size) return STATUS_INVALID_PARAMETER;
1439 if (!io_status) return STATUS_ACCESS_VIOLATION;
1441 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1442 &needs_close, &type, &options );
1443 if (status) return status;
1445 if ((type != FD_TYPE_FILE) ||
1446 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1447 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1449 status = STATUS_INVALID_PARAMETER;
1450 goto error;
1453 while (length)
1455 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1456 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1457 page_size - pos, offset->QuadPart + total );
1458 else
1459 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1461 if (result == -1)
1463 if (errno == EINTR) continue;
1464 if (errno == EFAULT)
1466 status = STATUS_INVALID_USER_BUFFER;
1467 goto error;
1469 status = FILE_GetNtStatus();
1470 break;
1472 if (!result)
1474 status = STATUS_DISK_FULL;
1475 break;
1477 total += result;
1478 length -= result;
1479 if ((pos += result) == page_size)
1481 pos = 0;
1482 segments++;
1486 send_completion = cvalue != 0;
1488 error:
1489 if (needs_close) close( unix_handle );
1490 if (status == STATUS_SUCCESS)
1492 io_status->u.Status = status;
1493 io_status->Information = total;
1494 TRACE("= SUCCESS (%u)\n", total);
1495 if (event) NtSetEvent( event, NULL );
1496 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1497 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1499 else
1501 TRACE("= 0x%08x\n", status);
1502 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1505 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total, FALSE );
1507 return status;
1511 /* do an ioctl call through the server */
1512 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1513 PIO_APC_ROUTINE apc, PVOID apc_context,
1514 IO_STATUS_BLOCK *io, ULONG code,
1515 const void *in_buffer, ULONG in_size,
1516 PVOID out_buffer, ULONG out_size )
1518 struct async_irp *async;
1519 NTSTATUS status;
1520 HANDLE wait_handle;
1521 ULONG options;
1523 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
1524 return STATUS_NO_MEMORY;
1525 async->buffer = out_buffer;
1526 async->size = out_size;
1528 SERVER_START_REQ( ioctl )
1530 req->code = code;
1531 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
1532 wine_server_add_data( req, in_buffer, in_size );
1533 if ((code & 3) != METHOD_BUFFERED)
1534 wine_server_add_data( req, out_buffer, out_size );
1535 wine_server_set_reply( req, out_buffer, out_size );
1536 status = virtual_locked_server_call( req );
1537 wait_handle = wine_server_ptr_handle( reply->wait );
1538 options = reply->options;
1539 if (wait_handle && status != STATUS_PENDING)
1541 io->u.Status = status;
1542 io->Information = wine_server_reply_size( reply );
1545 SERVER_END_REQ;
1547 if (status == STATUS_NOT_SUPPORTED)
1548 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1549 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1551 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1553 if (wait_handle)
1555 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1556 status = io->u.Status;
1559 return status;
1562 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1563 * server */
1564 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1565 ULONG in_size)
1567 #ifdef VALGRIND_MAKE_MEM_DEFINED
1568 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1569 do { \
1570 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1571 if ((size) >= FIELD_OFFSET(t, f2)) \
1572 VALGRIND_MAKE_MEM_DEFINED( \
1573 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1574 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1575 } while (0)
1577 switch (code)
1579 case FSCTL_PIPE_WAIT:
1580 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1581 break;
1583 #endif
1587 /**************************************************************************
1588 * NtDeviceIoControlFile [NTDLL.@]
1589 * ZwDeviceIoControlFile [NTDLL.@]
1591 * Perform an I/O control operation on an open file handle.
1593 * PARAMS
1594 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1595 * event [I] Event to signal upon completion (or NULL)
1596 * apc [I] Callback to call upon completion (or NULL)
1597 * apc_context [I] Context for ApcRoutine (or NULL)
1598 * io [O] Receives information about the operation on return
1599 * code [I] Control code for the operation to perform
1600 * in_buffer [I] Source for any input data required (or NULL)
1601 * in_size [I] Size of InputBuffer
1602 * out_buffer [O] Source for any output data returned (or NULL)
1603 * out_size [I] Size of OutputBuffer
1605 * RETURNS
1606 * Success: 0. IoStatusBlock is updated.
1607 * Failure: An NTSTATUS error code describing the error.
1609 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1610 PIO_APC_ROUTINE apc, PVOID apc_context,
1611 PIO_STATUS_BLOCK io, ULONG code,
1612 PVOID in_buffer, ULONG in_size,
1613 PVOID out_buffer, ULONG out_size)
1615 ULONG device = (code >> 16);
1616 NTSTATUS status = STATUS_NOT_SUPPORTED;
1618 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1619 handle, event, apc, apc_context, io, code,
1620 in_buffer, in_size, out_buffer, out_size);
1622 switch(device)
1624 case FILE_DEVICE_DISK:
1625 case FILE_DEVICE_CD_ROM:
1626 case FILE_DEVICE_DVD:
1627 case FILE_DEVICE_CONTROLLER:
1628 case FILE_DEVICE_MASS_STORAGE:
1629 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1630 in_buffer, in_size, out_buffer, out_size);
1631 break;
1632 case FILE_DEVICE_SERIAL_PORT:
1633 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1634 in_buffer, in_size, out_buffer, out_size);
1635 break;
1636 case FILE_DEVICE_TAPE:
1637 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1638 in_buffer, in_size, out_buffer, out_size);
1639 break;
1642 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1643 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1644 in_buffer, in_size, out_buffer, out_size );
1646 if (status != STATUS_PENDING) io->u.Status = status;
1647 return status;
1651 /**************************************************************************
1652 * NtFsControlFile [NTDLL.@]
1653 * ZwFsControlFile [NTDLL.@]
1655 * Perform a file system control operation on an open file handle.
1657 * PARAMS
1658 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1659 * event [I] Event to signal upon completion (or NULL)
1660 * apc [I] Callback to call upon completion (or NULL)
1661 * apc_context [I] Context for ApcRoutine (or NULL)
1662 * io [O] Receives information about the operation on return
1663 * code [I] Control code for the operation to perform
1664 * in_buffer [I] Source for any input data required (or NULL)
1665 * in_size [I] Size of InputBuffer
1666 * out_buffer [O] Source for any output data returned (or NULL)
1667 * out_size [I] Size of OutputBuffer
1669 * RETURNS
1670 * Success: 0. IoStatusBlock is updated.
1671 * Failure: An NTSTATUS error code describing the error.
1673 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1674 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1675 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1677 NTSTATUS status;
1679 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1680 handle, event, apc, apc_context, io, code,
1681 in_buffer, in_size, out_buffer, out_size);
1683 if (!io) return STATUS_INVALID_PARAMETER;
1685 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1687 switch(code)
1689 case FSCTL_DISMOUNT_VOLUME:
1690 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1691 in_buffer, in_size, out_buffer, out_size );
1692 if (!status) status = DIR_unmount_device( handle );
1693 return status;
1695 case FSCTL_PIPE_IMPERSONATE:
1696 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1697 status = RtlImpersonateSelf( SecurityImpersonation );
1698 break;
1700 case FSCTL_IS_VOLUME_MOUNTED:
1701 case FSCTL_LOCK_VOLUME:
1702 case FSCTL_UNLOCK_VOLUME:
1703 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1704 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1705 status = STATUS_SUCCESS;
1706 break;
1708 case FSCTL_GET_RETRIEVAL_POINTERS:
1710 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1712 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1714 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1716 buffer->ExtentCount = 1;
1717 buffer->StartingVcn.QuadPart = 1;
1718 buffer->Extents[0].NextVcn.QuadPart = 0;
1719 buffer->Extents[0].Lcn.QuadPart = 0;
1720 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1721 status = STATUS_SUCCESS;
1723 else
1725 io->Information = 0;
1726 status = STATUS_BUFFER_TOO_SMALL;
1728 break;
1730 case FSCTL_SET_SPARSE:
1731 TRACE("FSCTL_SET_SPARSE: Ignoring request\n");
1732 io->Information = 0;
1733 status = STATUS_SUCCESS;
1734 break;
1735 default:
1736 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1737 in_buffer, in_size, out_buffer, out_size );
1740 if (status != STATUS_PENDING) io->u.Status = status;
1741 return status;
1745 struct read_changes_fileio
1747 struct async_fileio io;
1748 void *buffer;
1749 ULONG buffer_size;
1750 ULONG data_size;
1751 char data[1];
1754 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1756 struct read_changes_fileio *fileio = user;
1757 int size = 0;
1759 if (status == STATUS_ALERTED)
1761 SERVER_START_REQ( read_change )
1763 req->handle = wine_server_obj_handle( fileio->io.handle );
1764 wine_server_set_reply( req, fileio->data, fileio->data_size );
1765 status = wine_server_call( req );
1766 size = wine_server_reply_size( reply );
1768 SERVER_END_REQ;
1770 if (status == STATUS_SUCCESS && fileio->buffer)
1772 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1773 int i, left = fileio->buffer_size;
1774 DWORD *last_entry_offset = NULL;
1775 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1777 while (size && left >= sizeof(*pfni))
1779 /* convert to an NT style path */
1780 for (i = 0; i < event->len; i++)
1781 if (event->name[i] == '/') event->name[i] = '\\';
1783 pfni->Action = event->action;
1784 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1785 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1786 last_entry_offset = &pfni->NextEntryOffset;
1788 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1790 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1791 pfni->FileNameLength *= sizeof(WCHAR);
1792 pfni->NextEntryOffset = i;
1793 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1794 left -= i;
1796 i = (offsetof(struct filesystem_event, name[event->len])
1797 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1798 event = (struct filesystem_event*)((char*)event + i);
1799 size -= i;
1802 if (size)
1804 status = STATUS_NOTIFY_ENUM_DIR;
1805 size = 0;
1807 else
1809 if (last_entry_offset) *last_entry_offset = 0;
1810 size = fileio->buffer_size - left;
1813 else
1815 status = STATUS_NOTIFY_ENUM_DIR;
1816 size = 0;
1820 if (status != STATUS_PENDING)
1822 iosb->u.Status = status;
1823 iosb->Information = size;
1824 release_fileio( &fileio->io );
1826 return status;
1829 #define FILE_NOTIFY_ALL ( \
1830 FILE_NOTIFY_CHANGE_FILE_NAME | \
1831 FILE_NOTIFY_CHANGE_DIR_NAME | \
1832 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1833 FILE_NOTIFY_CHANGE_SIZE | \
1834 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1835 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1836 FILE_NOTIFY_CHANGE_CREATION | \
1837 FILE_NOTIFY_CHANGE_SECURITY )
1839 /******************************************************************************
1840 * NtNotifyChangeDirectoryFile [NTDLL.@]
1842 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1843 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1844 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1846 struct read_changes_fileio *fileio;
1847 NTSTATUS status;
1848 ULONG size = max( 4096, buffer_size );
1850 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1851 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1853 if (!iosb) return STATUS_ACCESS_VIOLATION;
1854 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1856 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1857 read_changes_apc, handle );
1858 if (!fileio) return STATUS_NO_MEMORY;
1860 fileio->buffer = buffer;
1861 fileio->buffer_size = buffer_size;
1862 fileio->data_size = size;
1864 SERVER_START_REQ( read_directory_changes )
1866 req->filter = filter;
1867 req->want_data = (buffer != NULL);
1868 req->subtree = subtree;
1869 req->async = server_async( handle, &fileio->io, event, apc, apc_context, iosb );
1870 status = wine_server_call( req );
1872 SERVER_END_REQ;
1874 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1875 return status;
1878 /******************************************************************************
1879 * NtSetVolumeInformationFile [NTDLL.@]
1880 * ZwSetVolumeInformationFile [NTDLL.@]
1882 * Set volume information for an open file handle.
1884 * PARAMS
1885 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1886 * IoStatusBlock [O] Receives information about the operation on return
1887 * FsInformation [I] Source for volume information
1888 * Length [I] Size of FsInformation
1889 * FsInformationClass [I] Type of volume information to set
1891 * RETURNS
1892 * Success: 0. IoStatusBlock is updated.
1893 * Failure: An NTSTATUS error code describing the error.
1895 NTSTATUS WINAPI NtSetVolumeInformationFile(
1896 IN HANDLE FileHandle,
1897 PIO_STATUS_BLOCK IoStatusBlock,
1898 PVOID FsInformation,
1899 ULONG Length,
1900 FS_INFORMATION_CLASS FsInformationClass)
1902 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1903 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1904 return 0;
1907 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1908 static int futimens( int fd, const struct timespec spec[2] )
1910 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1912 #define HAVE_FUTIMENS
1913 #endif /* __ANDROID__ */
1915 #ifndef UTIME_OMIT
1916 #define UTIME_OMIT ((1 << 30) - 2)
1917 #endif
1919 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1921 NTSTATUS status = STATUS_SUCCESS;
1923 #ifdef HAVE_FUTIMENS
1924 struct timespec tv[2];
1926 tv[0].tv_sec = tv[1].tv_sec = 0;
1927 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1928 if (atime->QuadPart)
1930 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1931 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1933 if (mtime->QuadPart)
1935 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1936 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1938 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1940 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1941 struct timeval tv[2];
1942 struct stat st;
1944 if (!atime->QuadPart || !mtime->QuadPart)
1947 tv[0].tv_sec = tv[0].tv_usec = 0;
1948 tv[1].tv_sec = tv[1].tv_usec = 0;
1949 if (!fstat( fd, &st ))
1951 tv[0].tv_sec = st.st_atime;
1952 tv[1].tv_sec = st.st_mtime;
1953 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1954 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1955 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1956 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1957 #endif
1958 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1959 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1960 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1961 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1962 #endif
1965 if (atime->QuadPart)
1967 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1968 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1970 if (mtime->QuadPart)
1972 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1973 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1975 #ifdef HAVE_FUTIMES
1976 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1977 #elif defined(HAVE_FUTIMESAT)
1978 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1979 #endif
1981 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1982 FIXME( "setting file times not supported\n" );
1983 status = STATUS_NOT_IMPLEMENTED;
1984 #endif
1985 return status;
1988 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1989 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1991 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1992 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1993 RtlSecondsSince1970ToTime( st->st_atime, atime );
1994 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1995 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1996 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1997 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
1998 #endif
1999 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2000 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2001 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2002 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2003 #endif
2004 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2005 atime->QuadPart += st->st_atim.tv_nsec / 100;
2006 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2007 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2008 #endif
2009 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2010 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2011 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2012 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2013 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2014 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2015 #endif
2016 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2017 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2018 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2019 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2020 #endif
2021 #else
2022 *creation = *mtime;
2023 #endif
2026 /* fill in the file information that depends on the stat and attribute info */
2027 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2028 FILE_INFORMATION_CLASS class )
2030 switch (class)
2032 case FileBasicInformation:
2034 FILE_BASIC_INFORMATION *info = ptr;
2036 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2037 &info->LastAccessTime, &info->CreationTime );
2038 info->FileAttributes = attr;
2040 break;
2041 case FileStandardInformation:
2043 FILE_STANDARD_INFORMATION *info = ptr;
2045 if ((info->Directory = S_ISDIR(st->st_mode)))
2047 info->AllocationSize.QuadPart = 0;
2048 info->EndOfFile.QuadPart = 0;
2049 info->NumberOfLinks = 1;
2051 else
2053 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2054 info->EndOfFile.QuadPart = st->st_size;
2055 info->NumberOfLinks = st->st_nlink;
2058 break;
2059 case FileInternalInformation:
2061 FILE_INTERNAL_INFORMATION *info = ptr;
2062 info->IndexNumber.QuadPart = st->st_ino;
2064 break;
2065 case FileEndOfFileInformation:
2067 FILE_END_OF_FILE_INFORMATION *info = ptr;
2068 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2070 break;
2071 case FileAllInformation:
2073 FILE_ALL_INFORMATION *info = ptr;
2074 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2075 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2076 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2078 break;
2079 /* all directory structures start with the FileDirectoryInformation layout */
2080 case FileBothDirectoryInformation:
2081 case FileFullDirectoryInformation:
2082 case FileDirectoryInformation:
2084 FILE_DIRECTORY_INFORMATION *info = ptr;
2086 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2087 &info->LastAccessTime, &info->CreationTime );
2088 if (S_ISDIR(st->st_mode))
2090 info->AllocationSize.QuadPart = 0;
2091 info->EndOfFile.QuadPart = 0;
2093 else
2095 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2096 info->EndOfFile.QuadPart = st->st_size;
2098 info->FileAttributes = attr;
2100 break;
2101 case FileIdFullDirectoryInformation:
2103 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2104 info->FileId.QuadPart = st->st_ino;
2105 fill_file_info( st, attr, info, FileDirectoryInformation );
2107 break;
2108 case FileIdBothDirectoryInformation:
2110 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2111 info->FileId.QuadPart = st->st_ino;
2112 fill_file_info( st, attr, info, FileDirectoryInformation );
2114 break;
2115 case FileIdGlobalTxDirectoryInformation:
2117 FILE_ID_GLOBAL_TX_DIR_INFORMATION *info = ptr;
2118 info->FileId.QuadPart = st->st_ino;
2119 fill_file_info( st, attr, info, FileDirectoryInformation );
2121 break;
2123 default:
2124 return STATUS_INVALID_INFO_CLASS;
2126 return STATUS_SUCCESS;
2129 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2131 data_size_t size = 1024;
2132 NTSTATUS ret;
2133 char *name;
2135 for (;;)
2137 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2138 if (!name) return STATUS_NO_MEMORY;
2139 unix_name->MaximumLength = size + 1;
2141 SERVER_START_REQ( get_handle_unix_name )
2143 req->handle = wine_server_obj_handle( handle );
2144 wine_server_set_reply( req, name, size );
2145 ret = wine_server_call( req );
2146 size = reply->name_len;
2148 SERVER_END_REQ;
2150 if (!ret)
2152 name[size] = 0;
2153 unix_name->Buffer = name;
2154 unix_name->Length = size;
2155 break;
2157 RtlFreeHeap( GetProcessHeap(), 0, name );
2158 if (ret != STATUS_BUFFER_OVERFLOW) break;
2160 return ret;
2163 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2165 UNICODE_STRING nt_name;
2166 NTSTATUS status;
2168 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2170 const WCHAR *ptr = nt_name.Buffer;
2171 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2173 /* Skip the volume mount point. */
2174 while (ptr != end && *ptr == '\\') ++ptr;
2175 while (ptr != end && *ptr != '\\') ++ptr;
2176 while (ptr != end && *ptr == '\\') ++ptr;
2177 while (ptr != end && *ptr != '\\') ++ptr;
2179 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2180 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2181 else *name_len = info->FileNameLength;
2183 memcpy( info->FileName, ptr, *name_len );
2184 RtlFreeUnicodeString( &nt_name );
2187 return status;
2190 static NTSTATUS server_get_file_info( HANDLE handle, IO_STATUS_BLOCK *io, void *buffer,
2191 ULONG length, FILE_INFORMATION_CLASS info_class )
2193 SERVER_START_REQ( get_file_info )
2195 req->handle = wine_server_obj_handle( handle );
2196 req->info_class = info_class;
2197 wine_server_set_reply( req, buffer, length );
2198 io->u.Status = wine_server_call( req );
2199 io->Information = wine_server_reply_size( reply );
2201 SERVER_END_REQ;
2202 if (io->u.Status == STATUS_NOT_IMPLEMENTED)
2203 FIXME( "Unsupported info class %x\n", info_class );
2204 return io->u.Status;
2208 /******************************************************************************
2209 * NtQueryInformationFile [NTDLL.@]
2210 * ZwQueryInformationFile [NTDLL.@]
2212 * Get information about an open file handle.
2214 * PARAMS
2215 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2216 * io [O] Receives information about the operation on return
2217 * ptr [O] Destination for file information
2218 * len [I] Size of FileInformation
2219 * class [I] Type of file information to get
2221 * RETURNS
2222 * Success: 0. IoStatusBlock and FileInformation are updated.
2223 * Failure: An NTSTATUS error code describing the error.
2225 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2226 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2228 static const size_t info_sizes[] =
2231 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2232 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2233 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2234 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2235 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2236 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2237 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2238 0, /* FileAccessInformation */
2239 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2240 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2241 0, /* FileLinkInformation */
2242 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2243 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2244 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2245 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2246 0, /* FileModeInformation */
2247 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2248 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2249 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2250 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2251 0, /* FileAlternateNameInformation */
2252 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2253 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2254 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2255 0, /* FilePipeRemoteInformation */
2256 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2257 0, /* FileMailslotSetInformation */
2258 0, /* FileCompressionInformation */
2259 0, /* FileObjectIdInformation */
2260 0, /* FileCompletionInformation */
2261 0, /* FileMoveClusterInformation */
2262 0, /* FileQuotaInformation */
2263 0, /* FileReparsePointInformation */
2264 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2265 0, /* FileAttributeTagInformation */
2266 0, /* FileTrackingInformation */
2267 0, /* FileIdBothDirectoryInformation */
2268 0, /* FileIdFullDirectoryInformation */
2269 0, /* FileValidDataLengthInformation */
2270 0, /* FileShortNameInformation */
2271 0, /* FileIoCompletionNotificationInformation, */
2272 0, /* FileIoStatusBlockRangeInformation */
2273 0, /* FileIoPriorityHintInformation */
2274 0, /* FileSfioReserveInformation */
2275 0, /* FileSfioVolumeInformation */
2276 0, /* FileHardLinkInformation */
2277 0, /* FileProcessIdsUsingFileInformation */
2278 0, /* FileNormalizedNameInformation */
2279 0, /* FileNetworkPhysicalNameInformation */
2280 0, /* FileIdGlobalTxDirectoryInformation */
2281 0, /* FileIsRemoteDeviceInformation */
2282 0, /* FileAttributeCacheInformation */
2283 0, /* FileNumaNodeInformation */
2284 0, /* FileStandardLinkInformation */
2285 0, /* FileRemoteProtocolInformation */
2286 0, /* FileRenameInformationBypassAccessCheck */
2287 0, /* FileLinkInformationBypassAccessCheck */
2288 0, /* FileVolumeNameInformation */
2289 sizeof(FILE_ID_INFORMATION), /* FileIdInformation */
2290 0, /* FileIdExtdDirectoryInformation */
2291 0, /* FileReplaceCompletionInformation */
2292 0, /* FileHardLinkFullIdInformation */
2293 0, /* FileIdExtdBothDirectoryInformation */
2296 struct stat st;
2297 int fd, needs_close = FALSE;
2298 ULONG attr;
2300 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2302 io->Information = 0;
2304 if (class <= 0 || class >= FileMaximumInformation)
2305 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2306 if (!info_sizes[class])
2307 return server_get_file_info( hFile, io, ptr, len, class );
2308 if (len < info_sizes[class])
2309 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2311 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2313 if (io->u.Status != STATUS_BAD_DEVICE_TYPE) return io->u.Status;
2314 return server_get_file_info( hFile, io, ptr, len, class );
2317 switch (class)
2319 case FileBasicInformation:
2320 if (fd_get_file_info( fd, &st, &attr ) == -1)
2321 io->u.Status = FILE_GetNtStatus();
2322 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2323 io->u.Status = STATUS_INVALID_INFO_CLASS;
2324 else
2325 fill_file_info( &st, attr, ptr, class );
2326 break;
2327 case FileStandardInformation:
2329 FILE_STANDARD_INFORMATION *info = ptr;
2331 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2332 else
2334 fill_file_info( &st, attr, info, class );
2335 info->DeletePending = FALSE; /* FIXME */
2338 break;
2339 case FilePositionInformation:
2341 FILE_POSITION_INFORMATION *info = ptr;
2342 off_t res = lseek( fd, 0, SEEK_CUR );
2343 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2344 else info->CurrentByteOffset.QuadPart = res;
2346 break;
2347 case FileInternalInformation:
2348 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2349 else fill_file_info( &st, attr, ptr, class );
2350 break;
2351 case FileEaInformation:
2353 FILE_EA_INFORMATION *info = ptr;
2354 info->EaSize = 0;
2356 break;
2357 case FileEndOfFileInformation:
2358 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2359 else fill_file_info( &st, attr, ptr, class );
2360 break;
2361 case FileAllInformation:
2363 FILE_ALL_INFORMATION *info = ptr;
2364 ANSI_STRING unix_name;
2366 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2367 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2368 io->u.Status = STATUS_INVALID_INFO_CLASS;
2369 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2371 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2373 fill_file_info( &st, attr, info, FileAllInformation );
2374 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2375 info->EaInformation.EaSize = 0;
2376 info->AccessInformation.AccessFlags = 0; /* FIXME */
2377 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2378 info->ModeInformation.Mode = 0; /* FIXME */
2379 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2381 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2382 RtlFreeAnsiString( &unix_name );
2383 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2386 break;
2387 case FileMailslotQueryInformation:
2389 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2391 SERVER_START_REQ( set_mailslot_info )
2393 req->handle = wine_server_obj_handle( hFile );
2394 req->flags = 0;
2395 io->u.Status = wine_server_call( req );
2396 if( io->u.Status == STATUS_SUCCESS )
2398 info->MaximumMessageSize = reply->max_msgsize;
2399 info->MailslotQuota = 0;
2400 info->NextMessageSize = 0;
2401 info->MessagesAvailable = 0;
2402 info->ReadTimeout.QuadPart = reply->read_timeout;
2405 SERVER_END_REQ;
2406 if (!io->u.Status)
2408 char *tmpbuf;
2409 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2410 if (size > 0x10000) size = 0x10000;
2411 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2413 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2415 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2416 info->MessagesAvailable = (res > 0);
2417 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2418 if (needs_close) close( fd );
2420 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2424 break;
2425 case FileNameInformation:
2427 FILE_NAME_INFORMATION *info = ptr;
2428 ANSI_STRING unix_name;
2430 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2432 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2433 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2434 RtlFreeAnsiString( &unix_name );
2435 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2438 break;
2439 case FileNetworkOpenInformation:
2441 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2442 ANSI_STRING unix_name;
2444 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2446 ULONG attributes;
2447 struct stat st;
2449 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2450 io->u.Status = FILE_GetNtStatus();
2451 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2452 io->u.Status = STATUS_INVALID_INFO_CLASS;
2453 else
2455 FILE_BASIC_INFORMATION basic;
2456 FILE_STANDARD_INFORMATION std;
2458 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2459 fill_file_info( &st, attributes, &std, FileStandardInformation );
2461 info->CreationTime = basic.CreationTime;
2462 info->LastAccessTime = basic.LastAccessTime;
2463 info->LastWriteTime = basic.LastWriteTime;
2464 info->ChangeTime = basic.ChangeTime;
2465 info->AllocationSize = std.AllocationSize;
2466 info->EndOfFile = std.EndOfFile;
2467 info->FileAttributes = basic.FileAttributes;
2469 RtlFreeAnsiString( &unix_name );
2472 break;
2473 case FileIdInformation:
2474 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2475 else
2477 FILE_ID_INFORMATION *info = ptr;
2478 info->VolumeSerialNumber = 0; /* FIXME */
2479 memset( &info->FileId, 0, sizeof(info->FileId) );
2480 *(ULONGLONG *)&info->FileId = st.st_ino;
2482 break;
2483 default:
2484 FIXME("Unsupported class (%d)\n", class);
2485 io->u.Status = STATUS_NOT_IMPLEMENTED;
2486 break;
2488 if (needs_close) close( fd );
2489 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2490 return io->u.Status;
2493 /******************************************************************************
2494 * NtSetInformationFile [NTDLL.@]
2495 * ZwSetInformationFile [NTDLL.@]
2497 * Set information about an open file handle.
2499 * PARAMS
2500 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2501 * io [O] Receives information about the operation on return
2502 * ptr [I] Source for file information
2503 * len [I] Size of FileInformation
2504 * class [I] Type of file information to set
2506 * RETURNS
2507 * Success: 0. io is updated.
2508 * Failure: An NTSTATUS error code describing the error.
2510 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2511 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2513 int fd, needs_close;
2515 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2517 io->u.Status = STATUS_SUCCESS;
2518 switch (class)
2520 case FileBasicInformation:
2521 if (len >= sizeof(FILE_BASIC_INFORMATION))
2523 struct stat st;
2524 const FILE_BASIC_INFORMATION *info = ptr;
2526 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2527 return io->u.Status;
2529 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2530 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2532 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2534 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2535 else
2537 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2539 if (S_ISDIR( st.st_mode))
2540 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2541 else
2542 st.st_mode &= ~0222; /* clear write permission bits */
2544 else
2546 /* add write permission only where we already have read permission */
2547 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2549 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2553 if (needs_close) close( fd );
2555 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2556 break;
2558 case FilePositionInformation:
2559 if (len >= sizeof(FILE_POSITION_INFORMATION))
2561 const FILE_POSITION_INFORMATION *info = ptr;
2563 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2564 return io->u.Status;
2566 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2567 io->u.Status = FILE_GetNtStatus();
2569 if (needs_close) close( fd );
2571 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2572 break;
2574 case FileEndOfFileInformation:
2575 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2577 struct stat st;
2578 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2580 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2581 return io->u.Status;
2583 /* first try normal truncate */
2584 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2586 /* now check for the need to extend the file */
2587 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2589 static const char zero;
2591 /* extend the file one byte beyond the requested size and then truncate it */
2592 /* this should work around ftruncate implementations that can't extend files */
2593 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2594 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2596 io->u.Status = FILE_GetNtStatus();
2598 if (needs_close) close( fd );
2600 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2601 break;
2603 case FilePipeInformation:
2604 if (len >= sizeof(FILE_PIPE_INFORMATION))
2606 FILE_PIPE_INFORMATION *info = ptr;
2608 if ((info->CompletionMode | info->ReadMode) & ~1)
2610 io->u.Status = STATUS_INVALID_PARAMETER;
2611 break;
2614 SERVER_START_REQ( set_named_pipe_info )
2616 req->handle = wine_server_obj_handle( handle );
2617 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2618 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2619 io->u.Status = wine_server_call( req );
2621 SERVER_END_REQ;
2623 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2624 break;
2626 case FileMailslotSetInformation:
2628 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2630 SERVER_START_REQ( set_mailslot_info )
2632 req->handle = wine_server_obj_handle( handle );
2633 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2634 req->read_timeout = info->ReadTimeout.QuadPart;
2635 io->u.Status = wine_server_call( req );
2637 SERVER_END_REQ;
2639 break;
2641 case FileCompletionInformation:
2642 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2644 FILE_COMPLETION_INFORMATION *info = ptr;
2646 SERVER_START_REQ( set_completion_info )
2648 req->handle = wine_server_obj_handle( handle );
2649 req->chandle = wine_server_obj_handle( info->CompletionPort );
2650 req->ckey = info->CompletionKey;
2651 io->u.Status = wine_server_call( req );
2653 SERVER_END_REQ;
2654 } else
2655 io->u.Status = STATUS_INVALID_PARAMETER_3;
2656 break;
2658 case FileIoCompletionNotificationInformation:
2659 if (len >= sizeof(FILE_IO_COMPLETION_NOTIFICATION_INFORMATION))
2661 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION *info = ptr;
2663 if (info->Flags & FILE_SKIP_SET_USER_EVENT_ON_FAST_IO)
2664 FIXME( "FILE_SKIP_SET_USER_EVENT_ON_FAST_IO not supported\n" );
2666 SERVER_START_REQ( set_fd_completion_mode )
2668 req->handle = wine_server_obj_handle( handle );
2669 req->flags = info->Flags;
2670 io->u.Status = wine_server_call( req );
2672 SERVER_END_REQ;
2673 } else
2674 io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2675 break;
2677 case FileIoPriorityHintInformation:
2678 if (len >= sizeof(FILE_IO_PRIORITY_HINT_INFO))
2680 FILE_IO_PRIORITY_HINT_INFO *info = ptr;
2681 if (info->PriorityHint < MaximumIoPriorityHintType)
2682 TRACE( "ignoring FileIoPriorityHintInformation %u\n", info->PriorityHint );
2683 else
2684 io->u.Status = STATUS_INVALID_PARAMETER;
2686 else io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2687 break;
2689 case FileAllInformation:
2690 io->u.Status = STATUS_INVALID_INFO_CLASS;
2691 break;
2693 case FileValidDataLengthInformation:
2694 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2696 struct stat st;
2697 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2699 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2700 return io->u.Status;
2702 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2703 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2704 io->u.Status = STATUS_INVALID_PARAMETER;
2705 else
2707 #ifdef HAVE_FALLOCATE
2708 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2710 NTSTATUS status = FILE_GetNtStatus();
2711 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2712 else io->u.Status = status;
2714 #else
2715 FIXME( "setting valid data length not supported\n" );
2716 #endif
2718 if (needs_close) close( fd );
2720 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2721 break;
2723 case FileDispositionInformation:
2724 if (len >= sizeof(FILE_DISPOSITION_INFORMATION))
2726 FILE_DISPOSITION_INFORMATION *info = ptr;
2728 SERVER_START_REQ( set_fd_disp_info )
2730 req->handle = wine_server_obj_handle( handle );
2731 req->unlink = info->DoDeleteFile;
2732 io->u.Status = wine_server_call( req );
2734 SERVER_END_REQ;
2735 } else
2736 io->u.Status = STATUS_INVALID_PARAMETER_3;
2737 break;
2739 case FileRenameInformation:
2740 if (len >= sizeof(FILE_RENAME_INFORMATION))
2742 FILE_RENAME_INFORMATION *info = ptr;
2743 UNICODE_STRING name_str;
2744 OBJECT_ATTRIBUTES attr;
2745 ANSI_STRING unix_name;
2747 name_str.Buffer = info->FileName;
2748 name_str.Length = info->FileNameLength;
2749 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2751 attr.Length = sizeof(attr);
2752 attr.ObjectName = &name_str;
2753 attr.RootDirectory = info->RootDir;
2754 attr.Attributes = OBJ_CASE_INSENSITIVE;
2756 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2757 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2758 break;
2760 if (!info->Replace && io->u.Status == STATUS_SUCCESS)
2762 RtlFreeAnsiString( &unix_name );
2763 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2764 break;
2767 SERVER_START_REQ( set_fd_name_info )
2769 req->handle = wine_server_obj_handle( handle );
2770 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2771 req->link = FALSE;
2772 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2773 io->u.Status = wine_server_call( req );
2775 SERVER_END_REQ;
2777 RtlFreeAnsiString( &unix_name );
2779 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2780 break;
2782 case FileLinkInformation:
2783 if (len >= sizeof(FILE_LINK_INFORMATION))
2785 FILE_LINK_INFORMATION *info = ptr;
2786 UNICODE_STRING name_str;
2787 OBJECT_ATTRIBUTES attr;
2788 ANSI_STRING unix_name;
2790 name_str.Buffer = info->FileName;
2791 name_str.Length = info->FileNameLength;
2792 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2794 attr.Length = sizeof(attr);
2795 attr.ObjectName = &name_str;
2796 attr.RootDirectory = info->RootDirectory;
2797 attr.Attributes = OBJ_CASE_INSENSITIVE;
2799 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2800 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2801 break;
2803 if (!info->ReplaceIfExists && io->u.Status == STATUS_SUCCESS)
2805 RtlFreeAnsiString( &unix_name );
2806 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2807 break;
2810 SERVER_START_REQ( set_fd_name_info )
2812 req->handle = wine_server_obj_handle( handle );
2813 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2814 req->link = TRUE;
2815 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2816 io->u.Status = wine_server_call( req );
2818 SERVER_END_REQ;
2820 RtlFreeAnsiString( &unix_name );
2822 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2823 break;
2825 default:
2826 FIXME("Unsupported class (%d)\n", class);
2827 io->u.Status = STATUS_NOT_IMPLEMENTED;
2828 break;
2830 io->Information = 0;
2831 return io->u.Status;
2835 /******************************************************************************
2836 * NtQueryFullAttributesFile (NTDLL.@)
2838 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2839 FILE_NETWORK_OPEN_INFORMATION *info )
2841 ANSI_STRING unix_name;
2842 NTSTATUS status;
2844 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2846 ULONG attributes;
2847 struct stat st;
2849 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2850 status = FILE_GetNtStatus();
2851 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2852 status = STATUS_INVALID_INFO_CLASS;
2853 else
2855 FILE_BASIC_INFORMATION basic;
2856 FILE_STANDARD_INFORMATION std;
2858 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2859 fill_file_info( &st, attributes, &std, FileStandardInformation );
2861 info->CreationTime = basic.CreationTime;
2862 info->LastAccessTime = basic.LastAccessTime;
2863 info->LastWriteTime = basic.LastWriteTime;
2864 info->ChangeTime = basic.ChangeTime;
2865 info->AllocationSize = std.AllocationSize;
2866 info->EndOfFile = std.EndOfFile;
2867 info->FileAttributes = basic.FileAttributes;
2868 if (DIR_is_hidden_file( attr->ObjectName ))
2869 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2871 RtlFreeAnsiString( &unix_name );
2873 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2874 return status;
2878 /******************************************************************************
2879 * NtQueryAttributesFile (NTDLL.@)
2880 * ZwQueryAttributesFile (NTDLL.@)
2882 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2884 ANSI_STRING unix_name;
2885 NTSTATUS status;
2887 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2889 ULONG attributes;
2890 struct stat st;
2892 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2893 status = FILE_GetNtStatus();
2894 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2895 status = STATUS_INVALID_INFO_CLASS;
2896 else
2898 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2899 if (DIR_is_hidden_file( attr->ObjectName ))
2900 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2902 RtlFreeAnsiString( &unix_name );
2904 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2905 return status;
2909 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2910 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2911 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2912 unsigned int flags )
2914 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2916 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2917 /* Don't assume read-only, let the mount options set it below */
2918 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2920 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2921 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2923 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2924 info->Characteristics |= FILE_REMOTE_DEVICE;
2926 else if (!strcmp("procfs", fstypename))
2927 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2928 else
2929 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2931 if (flags & MNT_RDONLY)
2932 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2934 if (!(flags & MNT_LOCAL))
2936 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2937 info->Characteristics |= FILE_REMOTE_DEVICE;
2940 #endif
2942 static inline BOOL is_device_placeholder( int fd )
2944 static const char wine_placeholder[] = "Wine device placeholder";
2945 char buffer[sizeof(wine_placeholder)-1];
2947 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2948 return FALSE;
2949 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2952 /******************************************************************************
2953 * get_device_info
2955 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2957 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2959 struct stat st;
2961 info->Characteristics = 0;
2962 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2963 if (S_ISCHR( st.st_mode ))
2965 info->DeviceType = FILE_DEVICE_UNKNOWN;
2966 #ifdef linux
2967 switch(major(st.st_rdev))
2969 case MEM_MAJOR:
2970 info->DeviceType = FILE_DEVICE_NULL;
2971 break;
2972 case TTY_MAJOR:
2973 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2974 break;
2975 case LP_MAJOR:
2976 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2977 break;
2978 case SCSI_TAPE_MAJOR:
2979 info->DeviceType = FILE_DEVICE_TAPE;
2980 break;
2982 #endif
2984 else if (S_ISBLK( st.st_mode ))
2986 info->DeviceType = FILE_DEVICE_DISK;
2988 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2990 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2992 else if (is_device_placeholder( fd ))
2994 info->DeviceType = FILE_DEVICE_DISK;
2996 else /* regular file or directory */
2998 #if defined(linux) && defined(HAVE_FSTATFS)
2999 struct statfs stfs;
3001 /* check for floppy disk */
3002 if (major(st.st_dev) == FLOPPY_MAJOR)
3003 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3005 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
3006 switch (stfs.f_type)
3008 case 0x9660: /* iso9660 */
3009 case 0x9fa1: /* supermount */
3010 case 0x15013346: /* udf */
3011 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3012 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3013 break;
3014 case 0x6969: /* nfs */
3015 case 0xff534d42: /* cifs */
3016 case 0xfe534d42: /* smb2 */
3017 case 0x517b: /* smbfs */
3018 case 0x564c: /* ncpfs */
3019 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3020 info->Characteristics |= FILE_REMOTE_DEVICE;
3021 break;
3022 case 0x01021994: /* tmpfs */
3023 case 0x28cd3d45: /* cramfs */
3024 case 0x1373: /* devfs */
3025 case 0x9fa0: /* procfs */
3026 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3027 break;
3028 default:
3029 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3030 break;
3032 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3033 struct statfs stfs;
3035 if (fstatfs( fd, &stfs ) < 0)
3036 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3037 else
3038 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
3039 #elif defined(__NetBSD__)
3040 struct statvfs stfs;
3042 if (fstatvfs( fd, &stfs) < 0)
3043 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3044 else
3045 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
3046 #elif defined(sun)
3047 /* Use dkio to work out device types */
3049 # include <sys/dkio.h>
3050 # include <sys/vtoc.h>
3051 struct dk_cinfo dkinf;
3052 int retval = ioctl(fd, DKIOCINFO, &dkinf);
3053 if(retval==-1){
3054 WARN("Unable to get disk device type information - assuming a disk like device\n");
3055 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3057 switch (dkinf.dki_ctype)
3059 case DKC_CDROM:
3060 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3061 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3062 break;
3063 case DKC_NCRFLOPPY:
3064 case DKC_SMSFLOPPY:
3065 case DKC_INTEL82072:
3066 case DKC_INTEL82077:
3067 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3068 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3069 break;
3070 case DKC_MD:
3071 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3072 break;
3073 default:
3074 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3077 #else
3078 static int warned;
3079 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3080 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3081 #endif
3082 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3084 return STATUS_SUCCESS;
3088 /******************************************************************************
3089 * NtQueryVolumeInformationFile [NTDLL.@]
3090 * ZwQueryVolumeInformationFile [NTDLL.@]
3092 * Get volume information for an open file handle.
3094 * PARAMS
3095 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3096 * io [O] Receives information about the operation on return
3097 * buffer [O] Destination for volume information
3098 * length [I] Size of FsInformation
3099 * info_class [I] Type of volume information to set
3101 * RETURNS
3102 * Success: 0. io and buffer are updated.
3103 * Failure: An NTSTATUS error code describing the error.
3105 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3106 PVOID buffer, ULONG length,
3107 FS_INFORMATION_CLASS info_class )
3109 int fd, needs_close;
3110 struct stat st;
3111 static int once;
3113 io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL );
3114 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
3116 SERVER_START_REQ( get_volume_info )
3118 req->handle = wine_server_obj_handle( handle );
3119 req->info_class = info_class;
3120 wine_server_set_reply( req, buffer, length );
3121 io->u.Status = wine_server_call( req );
3122 if (!io->u.Status) io->Information = wine_server_reply_size( reply );
3124 SERVER_END_REQ;
3125 return io->u.Status;
3127 else if (io->u.Status) return io->u.Status;
3129 io->u.Status = STATUS_NOT_IMPLEMENTED;
3130 io->Information = 0;
3132 switch( info_class )
3134 case FileFsVolumeInformation:
3135 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3136 break;
3137 case FileFsLabelInformation:
3138 FIXME( "%p: label info not supported\n", handle );
3139 break;
3140 case FileFsSizeInformation:
3141 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3142 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3143 else
3145 FILE_FS_SIZE_INFORMATION *info = buffer;
3147 if (fstat( fd, &st ) < 0)
3149 io->u.Status = FILE_GetNtStatus();
3150 break;
3152 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3154 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3156 else
3158 ULONGLONG bsize;
3159 /* Linux's fstatvfs is buggy */
3160 #if !defined(linux) || !defined(HAVE_FSTATFS)
3161 struct statvfs stfs;
3163 if (fstatvfs( fd, &stfs ) < 0)
3165 io->u.Status = FILE_GetNtStatus();
3166 break;
3168 bsize = stfs.f_frsize;
3169 #else
3170 struct statfs stfs;
3171 if (fstatfs( fd, &stfs ) < 0)
3173 io->u.Status = FILE_GetNtStatus();
3174 break;
3176 bsize = stfs.f_bsize;
3177 #endif
3178 if (bsize == 2048) /* assume CD-ROM */
3180 info->BytesPerSector = 2048;
3181 info->SectorsPerAllocationUnit = 1;
3183 else
3185 info->BytesPerSector = 512;
3186 info->SectorsPerAllocationUnit = 8;
3188 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3189 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3190 io->Information = sizeof(*info);
3191 io->u.Status = STATUS_SUCCESS;
3194 break;
3195 case FileFsDeviceInformation:
3196 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3197 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3198 else
3200 FILE_FS_DEVICE_INFORMATION *info = buffer;
3202 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3203 io->Information = sizeof(*info);
3205 break;
3206 case FileFsAttributeInformation:
3207 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[ARRAY_SIZE( ntfsW )] ))
3208 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3209 else
3211 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3213 FIXME( "%p: faking attribute info\n", handle );
3214 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3215 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3216 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3217 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3218 info->FileSystemNameLength = sizeof(ntfsW);
3219 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3221 io->Information = sizeof(*info);
3222 io->u.Status = STATUS_SUCCESS;
3224 break;
3225 case FileFsControlInformation:
3226 FIXME( "%p: control info not supported\n", handle );
3227 break;
3228 case FileFsFullSizeInformation:
3229 FIXME( "%p: full size info not supported\n", handle );
3230 break;
3231 case FileFsObjectIdInformation:
3232 FIXME( "%p: object id info not supported\n", handle );
3233 break;
3234 case FileFsMaximumInformation:
3235 FIXME( "%p: maximum info not supported\n", handle );
3236 break;
3237 default:
3238 io->u.Status = STATUS_INVALID_PARAMETER;
3239 break;
3241 if (needs_close) close( fd );
3242 return io->u.Status;
3246 /******************************************************************
3247 * NtQueryEaFile (NTDLL.@)
3249 * Read extended attributes from NTFS files.
3251 * PARAMS
3252 * hFile [I] File handle, must be opened with FILE_READ_EA access
3253 * iosb [O] Receives information about the operation on return
3254 * buffer [O] Output buffer
3255 * length [I] Length of output buffer
3256 * single_entry [I] Only read and return one entry
3257 * ea_list [I] Optional list with names of EAs to return
3258 * ea_list_len [I] Length of ea_list in bytes
3259 * ea_index [I] Optional pointer to 1-based index of attribute to return
3260 * restart [I] restart EA scan
3262 * RETURNS
3263 * Success: 0. Atrributes read into buffer
3264 * Failure: An NTSTATUS error code describing the error.
3266 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3267 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3268 PULONG ea_index, BOOLEAN restart )
3270 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3271 hFile, iosb, buffer, length, single_entry, ea_list,
3272 ea_list_len, ea_index, restart);
3273 return STATUS_ACCESS_DENIED;
3277 /******************************************************************
3278 * NtSetEaFile (NTDLL.@)
3280 * Update extended attributes for NTFS files.
3282 * PARAMS
3283 * hFile [I] File handle, must be opened with FILE_READ_EA access
3284 * iosb [O] Receives information about the operation on return
3285 * buffer [I] Buffer with EA information
3286 * length [I] Length of buffer
3288 * RETURNS
3289 * Success: 0. Attributes are updated
3290 * Failure: An NTSTATUS error code describing the error.
3292 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3294 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3295 return STATUS_ACCESS_DENIED;
3299 /******************************************************************
3300 * NtFlushBuffersFile (NTDLL.@)
3302 * Flush any buffered data on an open file handle.
3304 * PARAMS
3305 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3306 * IoStatusBlock [O] Receives information about the operation on return
3308 * RETURNS
3309 * Success: 0. IoStatusBlock is updated.
3310 * Failure: An NTSTATUS error code describing the error.
3312 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK *io )
3314 NTSTATUS ret;
3315 HANDLE wait_handle;
3316 enum server_fd_type type;
3317 int fd, needs_close;
3319 if (!io || !virtual_check_buffer_for_write( io, sizeof(*io) )) return STATUS_ACCESS_VIOLATION;
3321 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3322 if (ret == STATUS_ACCESS_DENIED)
3323 ret = server_get_unix_fd( hFile, FILE_APPEND_DATA, &fd, &needs_close, &type, NULL );
3325 if (!ret && type == FD_TYPE_SERIAL)
3327 ret = COMM_FlushBuffersFile( fd );
3329 else if (ret != STATUS_ACCESS_DENIED)
3331 struct async_irp *async;
3333 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, hFile )))
3334 return STATUS_NO_MEMORY;
3335 async->buffer = NULL;
3336 async->size = 0;
3338 SERVER_START_REQ( flush )
3340 req->async = server_async( hFile, &async->io, NULL, NULL, NULL, io );
3341 ret = wine_server_call( req );
3342 wait_handle = wine_server_ptr_handle( reply->event );
3343 if (wait_handle && ret != STATUS_PENDING)
3345 io->u.Status = ret;
3346 io->Information = 0;
3349 SERVER_END_REQ;
3351 if (ret != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
3353 if (wait_handle)
3355 NtWaitForSingleObject( wait_handle, FALSE, NULL );
3356 ret = io->u.Status;
3360 if (needs_close) close( fd );
3361 return ret;
3364 /******************************************************************
3365 * NtLockFile (NTDLL.@)
3369 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3370 PIO_APC_ROUTINE apc, void* apc_user,
3371 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3372 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3373 BOOLEAN exclusive )
3375 NTSTATUS ret;
3376 HANDLE handle;
3377 BOOLEAN async;
3378 static BOOLEAN warn = TRUE;
3380 if (apc || io_status || key)
3382 FIXME("Unimplemented yet parameter\n");
3383 return STATUS_NOT_IMPLEMENTED;
3386 if (apc_user && warn)
3388 FIXME("I/O completion on lock not implemented yet\n");
3389 warn = FALSE;
3392 for (;;)
3394 SERVER_START_REQ( lock_file )
3396 req->handle = wine_server_obj_handle( hFile );
3397 req->offset = offset->QuadPart;
3398 req->count = count->QuadPart;
3399 req->shared = !exclusive;
3400 req->wait = !dont_wait;
3401 ret = wine_server_call( req );
3402 handle = wine_server_ptr_handle( reply->handle );
3403 async = reply->overlapped;
3405 SERVER_END_REQ;
3406 if (ret != STATUS_PENDING)
3408 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3409 return ret;
3412 if (async)
3414 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3415 if (handle) NtClose( handle );
3416 return STATUS_PENDING;
3418 if (handle)
3420 NtWaitForSingleObject( handle, FALSE, NULL );
3421 NtClose( handle );
3423 else
3425 LARGE_INTEGER time;
3427 /* Unix lock conflict, sleep a bit and retry */
3428 time.QuadPart = 100 * (ULONGLONG)10000;
3429 time.QuadPart = -time.QuadPart;
3430 NtDelayExecution( FALSE, &time );
3436 /******************************************************************
3437 * NtUnlockFile (NTDLL.@)
3441 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3442 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3443 PULONG key )
3445 NTSTATUS status;
3447 TRACE( "%p %x%08x %x%08x\n",
3448 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3450 if (io_status || key)
3452 FIXME("Unimplemented yet parameter\n");
3453 return STATUS_NOT_IMPLEMENTED;
3456 SERVER_START_REQ( unlock_file )
3458 req->handle = wine_server_obj_handle( hFile );
3459 req->offset = offset->QuadPart;
3460 req->count = count->QuadPart;
3461 status = wine_server_call( req );
3463 SERVER_END_REQ;
3464 return status;
3467 /******************************************************************
3468 * NtCreateNamedPipeFile (NTDLL.@)
3472 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3473 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3474 ULONG sharing, ULONG dispo, ULONG options,
3475 ULONG pipe_type, ULONG read_mode,
3476 ULONG completion_mode, ULONG max_inst,
3477 ULONG inbound_quota, ULONG outbound_quota,
3478 PLARGE_INTEGER timeout)
3480 NTSTATUS status;
3481 data_size_t len;
3482 struct object_attributes *objattr;
3484 if (!attr) return STATUS_INVALID_PARAMETER;
3486 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3487 handle, access, debugstr_us(attr->ObjectName), iosb, sharing, dispo,
3488 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3489 outbound_quota, timeout);
3491 /* assume we only get relative timeout */
3492 if (timeout->QuadPart > 0)
3493 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3495 if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
3497 SERVER_START_REQ( create_named_pipe )
3499 req->access = access;
3500 req->options = options;
3501 req->sharing = sharing;
3502 req->flags =
3503 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3504 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3505 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3506 req->maxinstances = max_inst;
3507 req->outsize = outbound_quota;
3508 req->insize = inbound_quota;
3509 req->timeout = timeout->QuadPart;
3510 wine_server_add_data( req, objattr, len );
3511 status = wine_server_call( req );
3512 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3514 SERVER_END_REQ;
3516 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3517 return status;
3520 /******************************************************************
3521 * NtDeleteFile (NTDLL.@)
3525 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3527 NTSTATUS status;
3528 HANDLE hFile;
3529 IO_STATUS_BLOCK io;
3531 TRACE("%p\n", ObjectAttributes);
3532 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3533 ObjectAttributes, &io, NULL, 0,
3534 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3535 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3536 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3537 return status;
3540 /******************************************************************
3541 * NtCancelIoFileEx (NTDLL.@)
3545 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3547 TRACE("%p %p %p\n", hFile, iosb, io_status );
3549 SERVER_START_REQ( cancel_async )
3551 req->handle = wine_server_obj_handle( hFile );
3552 req->iosb = wine_server_client_ptr( iosb );
3553 req->only_thread = FALSE;
3554 io_status->u.Status = wine_server_call( req );
3556 SERVER_END_REQ;
3558 return io_status->u.Status;
3561 /******************************************************************
3562 * NtCancelIoFile (NTDLL.@)
3566 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3568 TRACE("%p %p\n", hFile, io_status );
3570 SERVER_START_REQ( cancel_async )
3572 req->handle = wine_server_obj_handle( hFile );
3573 req->iosb = 0;
3574 req->only_thread = TRUE;
3575 io_status->u.Status = wine_server_call( req );
3577 SERVER_END_REQ;
3579 return io_status->u.Status;
3582 /******************************************************************************
3583 * NtCreateMailslotFile [NTDLL.@]
3584 * ZwCreateMailslotFile [NTDLL.@]
3586 * PARAMS
3587 * pHandle [O] pointer to receive the handle created
3588 * DesiredAccess [I] access mode (read, write, etc)
3589 * ObjectAttributes [I] fully qualified NT path of the mailslot
3590 * IoStatusBlock [O] receives completion status and other info
3591 * CreateOptions [I]
3592 * MailslotQuota [I]
3593 * MaxMessageSize [I]
3594 * TimeOut [I]
3596 * RETURNS
3597 * An NT status code
3599 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3600 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3601 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3602 PLARGE_INTEGER TimeOut)
3604 LARGE_INTEGER timeout;
3605 NTSTATUS ret;
3606 data_size_t len;
3607 struct object_attributes *objattr;
3609 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3610 pHandle, DesiredAccess, attr, IoStatusBlock,
3611 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3613 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3614 if (!attr) return STATUS_INVALID_PARAMETER;
3616 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3619 * For a NULL TimeOut pointer set the default timeout value
3621 if (!TimeOut)
3622 timeout.QuadPart = -1;
3623 else
3624 timeout.QuadPart = TimeOut->QuadPart;
3626 SERVER_START_REQ( create_mailslot )
3628 req->access = DesiredAccess;
3629 req->max_msgsize = MaxMessageSize;
3630 req->read_timeout = timeout.QuadPart;
3631 wine_server_add_data( req, objattr, len );
3632 ret = wine_server_call( req );
3633 if( ret == STATUS_SUCCESS )
3634 *pHandle = wine_server_ptr_handle( reply->handle );
3636 SERVER_END_REQ;
3638 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3639 return ret;