oleaut32: Add ARM support to DispCallFunc().
[wine.git] / dlls / ntdll / file.c
blobca2afa0e89e78821776be4d59350f7c9db98c55f
1 /*
2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_LINUX_MAJOR_H
31 # include <linux/major.h>
32 #endif
33 #ifdef HAVE_SYS_STATVFS_H
34 # include <sys/statvfs.h>
35 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYS_SYSCALL_H
40 # include <sys/syscall.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
50 #endif
51 #ifdef HAVE_POLL_H
52 #include <poll.h>
53 #endif
54 #ifdef HAVE_SYS_POLL_H
55 #include <sys/poll.h>
56 #endif
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef MAJOR_IN_MKDEV
61 # include <sys/mkdev.h>
62 #elif defined(MAJOR_IN_SYSMACROS)
63 # include <sys/sysmacros.h>
64 #endif
65 #ifdef HAVE_UTIME_H
66 # include <utime.h>
67 #endif
68 #ifdef HAVE_SYS_VFS_H
69 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
70 #define list SYSLIST
71 #define list_next SYSLIST_NEXT
72 #define list_prev SYSLIST_PREV
73 #define list_head SYSLIST_HEAD
74 #define list_tail SYSLIST_TAIL
75 #define list_move_tail SYSLIST_MOVE_TAIL
76 #define list_remove SYSLIST_REMOVE
77 # include <sys/vfs.h>
78 #undef list
79 #undef list_next
80 #undef list_prev
81 #undef list_head
82 #undef list_tail
83 #undef list_move_tail
84 #undef list_remove
85 #endif
86 #ifdef HAVE_SYS_MOUNT_H
87 # include <sys/mount.h>
88 #endif
89 #ifdef HAVE_SYS_STATFS_H
90 # include <sys/statfs.h>
91 #endif
92 #ifdef HAVE_TERMIOS_H
93 #include <termios.h>
94 #endif
95 #ifdef HAVE_VALGRIND_MEMCHECK_H
96 # include <valgrind/memcheck.h>
97 #endif
99 #include "ntstatus.h"
100 #define WIN32_NO_STATUS
101 #define NONAMELESSUNION
102 #include "wine/unicode.h"
103 #include "wine/debug.h"
104 #include "wine/server.h"
105 #include "ntdll_misc.h"
107 #include "winternl.h"
108 #include "winioctl.h"
109 #include "ddk/ntddk.h"
110 #include "ddk/ntddser.h"
112 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
113 WINE_DECLARE_DEBUG_CHANNEL(winediag);
115 mode_t FILE_umask = 0;
117 #define SECSPERDAY 86400
118 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
120 #define FILE_WRITE_TO_END_OF_FILE ((LONGLONG)-1)
121 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
123 static const WCHAR ntfsW[] = {'N','T','F','S'};
125 /* fetch the attributes of a file */
126 static inline ULONG get_file_attributes( const struct stat *st )
128 ULONG attr;
130 if (S_ISDIR(st->st_mode))
131 attr = FILE_ATTRIBUTE_DIRECTORY;
132 else
133 attr = FILE_ATTRIBUTE_ARCHIVE;
134 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
135 attr |= FILE_ATTRIBUTE_READONLY;
136 return attr;
139 /* get the stat info and file attributes for a file (by file descriptor) */
140 int fd_get_file_info( int fd, struct stat *st, ULONG *attr )
142 int ret;
144 *attr = 0;
145 ret = fstat( fd, st );
146 if (ret == -1) return ret;
147 *attr |= get_file_attributes( st );
148 return ret;
151 /* get the stat info and file attributes for a file (by name) */
152 int get_file_info( const char *path, struct stat *st, ULONG *attr )
154 int ret;
156 *attr = 0;
157 ret = lstat( path, st );
158 if (ret == -1) return ret;
159 if (S_ISLNK( st->st_mode ))
161 ret = stat( path, st );
162 if (ret == -1) return ret;
163 /* is a symbolic link and a directory, consider these "reparse points" */
164 if (S_ISDIR( st->st_mode )) *attr |= FILE_ATTRIBUTE_REPARSE_POINT;
166 *attr |= get_file_attributes( st );
167 return ret;
170 /**************************************************************************
171 * FILE_CreateFile (internal)
172 * Open a file.
174 * Parameter set fully identical with NtCreateFile
176 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
177 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
178 ULONG attributes, ULONG sharing, ULONG disposition,
179 ULONG options, PVOID ea_buffer, ULONG ea_length )
181 ANSI_STRING unix_name;
182 BOOL created = FALSE;
184 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
185 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
186 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
187 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
188 attributes, sharing, disposition, options, ea_buffer, ea_length );
190 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
192 if (alloc_size) FIXME( "alloc_size not supported\n" );
194 if (options & FILE_OPEN_BY_FILE_ID)
195 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
196 else
197 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
199 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
201 SERVER_START_REQ( open_file_object )
203 req->access = access;
204 req->attributes = attr->Attributes;
205 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
206 req->sharing = sharing;
207 req->options = options;
208 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
209 io->u.Status = wine_server_call( req );
210 *handle = wine_server_ptr_handle( reply->handle );
212 SERVER_END_REQ;
213 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
214 return io->u.Status;
217 if (io->u.Status == STATUS_NO_SUCH_FILE &&
218 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
220 created = TRUE;
221 io->u.Status = STATUS_SUCCESS;
224 if (io->u.Status == STATUS_SUCCESS)
226 static UNICODE_STRING empty_string;
227 OBJECT_ATTRIBUTES unix_attr = *attr;
228 data_size_t len;
229 struct object_attributes *objattr;
231 unix_attr.ObjectName = &empty_string; /* we send the unix name instead */
232 if ((io->u.Status = alloc_object_attributes( &unix_attr, &objattr, &len )))
234 RtlFreeAnsiString( &unix_name );
235 return io->u.Status;
238 SERVER_START_REQ( create_file )
240 req->access = access;
241 req->sharing = sharing;
242 req->create = disposition;
243 req->options = options;
244 req->attrs = attributes;
245 wine_server_add_data( req, objattr, len );
246 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
247 io->u.Status = wine_server_call( req );
248 *handle = wine_server_ptr_handle( reply->handle );
250 SERVER_END_REQ;
251 RtlFreeHeap( GetProcessHeap(), 0, objattr );
252 RtlFreeAnsiString( &unix_name );
254 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
256 if (io->u.Status == STATUS_SUCCESS)
258 if (created) io->Information = FILE_CREATED;
259 else switch(disposition)
261 case FILE_SUPERSEDE:
262 io->Information = FILE_SUPERSEDED;
263 break;
264 case FILE_CREATE:
265 io->Information = FILE_CREATED;
266 break;
267 case FILE_OPEN:
268 case FILE_OPEN_IF:
269 io->Information = FILE_OPENED;
270 break;
271 case FILE_OVERWRITE:
272 case FILE_OVERWRITE_IF:
273 io->Information = FILE_OVERWRITTEN;
274 break;
277 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
279 static int once;
280 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
283 return io->u.Status;
286 /**************************************************************************
287 * NtOpenFile [NTDLL.@]
288 * ZwOpenFile [NTDLL.@]
290 * Open a file.
292 * PARAMS
293 * handle [O] Variable that receives the file handle on return
294 * access [I] Access desired by the caller to the file
295 * attr [I] Structure describing the file to be opened
296 * io [O] Receives details about the result of the operation
297 * sharing [I] Type of shared access the caller requires
298 * options [I] Options for the file open
300 * RETURNS
301 * Success: 0. FileHandle and IoStatusBlock are updated.
302 * Failure: An NTSTATUS error code describing the error.
304 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
305 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
306 ULONG sharing, ULONG options )
308 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
309 sharing, FILE_OPEN, options, NULL, 0 );
312 /**************************************************************************
313 * NtCreateFile [NTDLL.@]
314 * ZwCreateFile [NTDLL.@]
316 * Either create a new file or directory, or open an existing file, device,
317 * directory or volume.
319 * PARAMS
320 * handle [O] Points to a variable which receives the file handle on return
321 * access [I] Desired access to the file
322 * attr [I] Structure describing the file
323 * io [O] Receives information about the operation on return
324 * alloc_size [I] Initial size of the file in bytes
325 * attributes [I] Attributes to create the file with
326 * sharing [I] Type of shared access the caller would like to the file
327 * disposition [I] Specifies what to do, depending on whether the file already exists
328 * options [I] Options for creating a new file
329 * ea_buffer [I] Pointer to an extended attributes buffer
330 * ea_length [I] Length of ea_buffer
332 * RETURNS
333 * Success: 0. handle and io are updated.
334 * Failure: An NTSTATUS error code describing the error.
336 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
337 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
338 ULONG attributes, ULONG sharing, ULONG disposition,
339 ULONG options, PVOID ea_buffer, ULONG ea_length )
341 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
342 sharing, disposition, options, ea_buffer, ea_length );
345 /***********************************************************************
346 * Asynchronous file I/O *
349 typedef NTSTATUS async_callback_t( void *user, IO_STATUS_BLOCK *io, NTSTATUS status );
351 struct async_fileio
353 async_callback_t *callback; /* must be the first field */
354 struct async_fileio *next;
355 HANDLE handle;
358 struct async_fileio_read
360 struct async_fileio io;
361 char* buffer;
362 unsigned int already;
363 unsigned int count;
364 BOOL avail_mode;
367 struct async_fileio_write
369 struct async_fileio io;
370 const char *buffer;
371 unsigned int already;
372 unsigned int count;
375 struct async_irp
377 struct async_fileio io;
378 HANDLE event; /* async event */
379 void *buffer; /* buffer for output */
380 ULONG size; /* size of buffer */
383 static struct async_fileio *fileio_freelist;
385 static void release_fileio( struct async_fileio *io )
387 for (;;)
389 struct async_fileio *next = fileio_freelist;
390 io->next = next;
391 if (interlocked_cmpxchg_ptr( (void **)&fileio_freelist, io, next ) == next) return;
395 static struct async_fileio *alloc_fileio( DWORD size, async_callback_t callback, HANDLE handle )
397 /* first free remaining previous fileinfos */
399 struct async_fileio *io = interlocked_xchg_ptr( (void **)&fileio_freelist, NULL );
401 while (io)
403 struct async_fileio *next = io->next;
404 RtlFreeHeap( GetProcessHeap(), 0, io );
405 io = next;
408 if ((io = RtlAllocateHeap( GetProcessHeap(), 0, size )))
410 io->callback = callback;
411 io->handle = handle;
413 return io;
416 static async_data_t server_async( HANDLE handle, struct async_fileio *user, HANDLE event,
417 PIO_APC_ROUTINE apc, void *apc_context, IO_STATUS_BLOCK *io )
419 async_data_t async;
420 async.handle = wine_server_obj_handle( handle );
421 async.user = wine_server_client_ptr( user );
422 async.iosb = wine_server_client_ptr( io );
423 async.event = wine_server_obj_handle( event );
424 async.apc = wine_server_client_ptr( apc );
425 async.apc_context = wine_server_client_ptr( apc_context );
426 return async;
429 /* callback for irp async I/O completion */
430 static NTSTATUS irp_completion( void *user, IO_STATUS_BLOCK *io, NTSTATUS status )
432 struct async_irp *async = user;
433 ULONG information = 0;
435 if (status == STATUS_ALERTED)
437 SERVER_START_REQ( get_async_result )
439 req->user_arg = wine_server_client_ptr( async );
440 wine_server_set_reply( req, async->buffer, async->size );
441 status = virtual_locked_server_call( req );
442 information = reply->size;
444 SERVER_END_REQ;
446 if (status != STATUS_PENDING)
448 io->u.Status = status;
449 io->Information = information;
450 release_fileio( &async->io );
452 return status;
455 /***********************************************************************
456 * FILE_GetNtStatus(void)
458 * Retrieve the Nt Status code from errno.
459 * Try to be consistent with FILE_SetDosError().
461 NTSTATUS FILE_GetNtStatus(void)
463 int err = errno;
465 TRACE( "errno = %d\n", errno );
466 switch (err)
468 case EAGAIN: return STATUS_SHARING_VIOLATION;
469 case EBADF: return STATUS_INVALID_HANDLE;
470 case EBUSY: return STATUS_DEVICE_BUSY;
471 case ENOSPC: return STATUS_DISK_FULL;
472 case EPERM:
473 case EROFS:
474 case EACCES: return STATUS_ACCESS_DENIED;
475 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
476 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
477 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
478 case EMFILE:
479 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
480 case EINVAL: return STATUS_INVALID_PARAMETER;
481 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
482 case EPIPE: return STATUS_PIPE_DISCONNECTED;
483 case EIO: return STATUS_DEVICE_NOT_READY;
484 #ifdef ENOMEDIUM
485 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
486 #endif
487 case ENXIO: return STATUS_NO_SUCH_DEVICE;
488 case ENOTTY:
489 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
490 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
491 case EFAULT: return STATUS_ACCESS_VIOLATION;
492 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
493 #ifdef ETIME /* Missing on FreeBSD */
494 case ETIME: return STATUS_IO_TIMEOUT;
495 #endif
496 case ENOEXEC: /* ?? */
497 case EEXIST: /* ?? */
498 default:
499 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
500 return STATUS_UNSUCCESSFUL;
504 /***********************************************************************
505 * FILE_AsyncReadService (INTERNAL)
507 static NTSTATUS FILE_AsyncReadService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
509 struct async_fileio_read *fileio = user;
510 int fd, needs_close, result;
512 switch (status)
514 case STATUS_ALERTED: /* got some new data */
515 /* check to see if the data is ready (non-blocking) */
516 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
517 &needs_close, NULL, NULL )))
518 break;
520 result = virtual_locked_read(fd, &fileio->buffer[fileio->already], fileio->count-fileio->already);
521 if (needs_close) close( fd );
523 if (result < 0)
525 if (errno == EAGAIN || errno == EINTR)
526 status = STATUS_PENDING;
527 else /* check to see if the transfer is complete */
528 status = FILE_GetNtStatus();
530 else if (result == 0)
532 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
534 else
536 fileio->already += result;
537 if (fileio->already >= fileio->count || fileio->avail_mode)
538 status = STATUS_SUCCESS;
539 else
540 status = STATUS_PENDING;
542 break;
544 case STATUS_TIMEOUT:
545 case STATUS_IO_TIMEOUT:
546 if (fileio->already) status = STATUS_SUCCESS;
547 break;
549 if (status != STATUS_PENDING)
551 iosb->u.Status = status;
552 iosb->Information = fileio->already;
553 release_fileio( &fileio->io );
555 return status;
558 /* do a read call through the server */
559 static NTSTATUS server_read_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
560 IO_STATUS_BLOCK *io, void *buffer, ULONG size,
561 LARGE_INTEGER *offset, ULONG *key )
563 struct async_irp *async;
564 NTSTATUS status;
565 HANDLE wait_handle;
566 ULONG options;
568 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
569 return STATUS_NO_MEMORY;
571 async->event = event;
572 async->buffer = buffer;
573 async->size = size;
575 SERVER_START_REQ( read )
577 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
578 req->pos = offset ? offset->QuadPart : 0;
579 wine_server_set_reply( req, buffer, size );
580 status = virtual_locked_server_call( req );
581 wait_handle = wine_server_ptr_handle( reply->wait );
582 options = reply->options;
583 if (wait_handle && status != STATUS_PENDING)
585 io->u.Status = status;
586 io->Information = wine_server_reply_size( reply );
589 SERVER_END_REQ;
591 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
593 if (wait_handle)
595 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
596 status = io->u.Status;
599 return status;
602 /* do a write call through the server */
603 static NTSTATUS server_write_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
604 IO_STATUS_BLOCK *io, const void *buffer, ULONG size,
605 LARGE_INTEGER *offset, ULONG *key )
607 struct async_irp *async;
608 NTSTATUS status;
609 HANDLE wait_handle;
610 ULONG options;
612 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
613 return STATUS_NO_MEMORY;
615 async->event = event;
616 async->buffer = NULL;
617 async->size = 0;
619 SERVER_START_REQ( write )
621 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
622 req->pos = offset ? offset->QuadPart : 0;
623 wine_server_add_data( req, buffer, size );
624 status = wine_server_call( req );
625 wait_handle = wine_server_ptr_handle( reply->wait );
626 options = reply->options;
627 if (wait_handle && status != STATUS_PENDING)
629 io->u.Status = status;
630 io->Information = reply->size;
633 SERVER_END_REQ;
635 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
637 if (wait_handle)
639 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
640 status = io->u.Status;
643 return status;
646 struct io_timeouts
648 int interval; /* max interval between two bytes */
649 int total; /* total timeout for the whole operation */
650 int end_time; /* absolute time of end of operation */
653 /* retrieve the I/O timeouts to use for a given handle */
654 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
655 struct io_timeouts *timeouts )
657 NTSTATUS status = STATUS_SUCCESS;
659 timeouts->interval = timeouts->total = -1;
661 switch(type)
663 case FD_TYPE_SERIAL:
665 /* GetCommTimeouts */
666 SERIAL_TIMEOUTS st;
667 IO_STATUS_BLOCK io;
669 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
670 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
671 if (status) break;
673 if (is_read)
675 if (st.ReadIntervalTimeout)
676 timeouts->interval = st.ReadIntervalTimeout;
678 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
680 timeouts->total = st.ReadTotalTimeoutConstant;
681 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
682 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
684 else if (st.ReadIntervalTimeout == MAXDWORD)
685 timeouts->interval = timeouts->total = 0;
687 else /* write */
689 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
691 timeouts->total = st.WriteTotalTimeoutConstant;
692 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
693 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
697 break;
698 case FD_TYPE_MAILSLOT:
699 if (is_read)
701 timeouts->interval = 0; /* return as soon as we got something */
702 SERVER_START_REQ( set_mailslot_info )
704 req->handle = wine_server_obj_handle( handle );
705 req->flags = 0;
706 if (!(status = wine_server_call( req )) &&
707 reply->read_timeout != TIMEOUT_INFINITE)
708 timeouts->total = reply->read_timeout / -10000;
710 SERVER_END_REQ;
712 break;
713 case FD_TYPE_SOCKET:
714 case FD_TYPE_CHAR:
715 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
716 break;
717 default:
718 break;
720 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
721 return STATUS_SUCCESS;
725 /* retrieve the timeout for the next wait, in milliseconds */
726 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
728 int ret = -1;
730 if (timeouts->total != -1)
732 ret = timeouts->end_time - NtGetTickCount();
733 if (ret < 0) ret = 0;
735 if (already && timeouts->interval != -1)
737 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
739 return ret;
743 /* retrieve the avail_mode flag for async reads */
744 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
746 NTSTATUS status = STATUS_SUCCESS;
748 switch(type)
750 case FD_TYPE_SERIAL:
752 /* GetCommTimeouts */
753 SERIAL_TIMEOUTS st;
754 IO_STATUS_BLOCK io;
756 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
757 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
758 if (status) break;
759 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
760 !st.ReadTotalTimeoutConstant &&
761 st.ReadIntervalTimeout == MAXDWORD);
763 break;
764 case FD_TYPE_MAILSLOT:
765 case FD_TYPE_SOCKET:
766 case FD_TYPE_CHAR:
767 *avail_mode = TRUE;
768 break;
769 default:
770 *avail_mode = FALSE;
771 break;
773 return status;
776 /* register an async I/O for a file read; helper for NtReadFile */
777 static NTSTATUS register_async_file_read( HANDLE handle, HANDLE event,
778 PIO_APC_ROUTINE apc, void *apc_user,
779 IO_STATUS_BLOCK *iosb, void *buffer,
780 ULONG already, ULONG length, BOOL avail_mode )
782 struct async_fileio_read *fileio;
783 NTSTATUS status;
785 if (!(fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), FILE_AsyncReadService, handle )))
786 return STATUS_NO_MEMORY;
788 fileio->already = already;
789 fileio->count = length;
790 fileio->buffer = buffer;
791 fileio->avail_mode = avail_mode;
793 SERVER_START_REQ( register_async )
795 req->type = ASYNC_TYPE_READ;
796 req->count = length;
797 req->async = server_async( handle, &fileio->io, event, apc, apc_user, iosb );
798 status = wine_server_call( req );
800 SERVER_END_REQ;
802 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
803 return status;
807 /******************************************************************************
808 * NtReadFile [NTDLL.@]
809 * ZwReadFile [NTDLL.@]
811 * Read from an open file handle.
813 * PARAMS
814 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
815 * Event [I] Event to signal upon completion (or NULL)
816 * ApcRoutine [I] Callback to call upon completion (or NULL)
817 * ApcContext [I] Context for ApcRoutine (or NULL)
818 * IoStatusBlock [O] Receives information about the operation on return
819 * Buffer [O] Destination for the data read
820 * Length [I] Size of Buffer
821 * ByteOffset [O] Destination for the new file pointer position (or NULL)
822 * Key [O] Function unknown (may be NULL)
824 * RETURNS
825 * Success: 0. IoStatusBlock is updated, and the Information member contains
826 * The number of bytes read.
827 * Failure: An NTSTATUS error code describing the error.
829 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
830 PIO_APC_ROUTINE apc, void* apc_user,
831 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
832 PLARGE_INTEGER offset, PULONG key)
834 int result, unix_handle, needs_close;
835 unsigned int options;
836 struct io_timeouts timeouts;
837 NTSTATUS status;
838 ULONG total = 0;
839 enum server_fd_type type;
840 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
841 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
843 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
844 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
846 if (!io_status) return STATUS_ACCESS_VIOLATION;
848 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
849 &needs_close, &type, &options );
850 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
852 if (!virtual_check_buffer_for_write( buffer, length )) return STATUS_ACCESS_VIOLATION;
854 if (status == STATUS_BAD_DEVICE_TYPE)
855 return server_read_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
857 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
859 if (type == FD_TYPE_FILE)
861 if (async_read && (!offset || offset->QuadPart < 0))
863 status = STATUS_INVALID_PARAMETER;
864 goto done;
867 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
869 /* async I/O doesn't make sense on regular files */
870 while ((result = virtual_locked_pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
872 if (errno != EINTR)
874 status = FILE_GetNtStatus();
875 goto done;
878 if (!async_read)
879 /* update file pointer position */
880 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
882 total = result;
883 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
884 goto done;
887 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
889 if (async_read && (!offset || offset->QuadPart < 0))
891 status = STATUS_INVALID_PARAMETER;
892 goto done;
896 if (type == FD_TYPE_SERIAL && async_read && length)
898 /* an asynchronous serial port read with a read interval timeout needs to
899 skip the synchronous read to make sure that the server starts the read
900 interval timer after the first read */
901 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts ))) goto err;
902 if (timeouts.interval)
904 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
905 buffer, total, length, FALSE );
906 goto err;
910 for (;;)
912 if ((result = virtual_locked_read( unix_handle, (char *)buffer + total, length - total )) >= 0)
914 total += result;
915 if (!result || total == length)
917 if (total)
919 status = STATUS_SUCCESS;
920 goto done;
922 switch (type)
924 case FD_TYPE_FILE:
925 case FD_TYPE_CHAR:
926 case FD_TYPE_DEVICE:
927 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
928 goto done;
929 case FD_TYPE_SERIAL:
930 if (!length)
932 status = STATUS_SUCCESS;
933 goto done;
935 break;
936 default:
937 status = STATUS_PIPE_BROKEN;
938 goto err;
941 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
943 else if (errno != EAGAIN)
945 if (errno == EINTR) continue;
946 if (!total) status = FILE_GetNtStatus();
947 goto err;
950 if (async_read)
952 BOOL avail_mode;
954 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
955 goto err;
956 if (total && avail_mode)
958 status = STATUS_SUCCESS;
959 goto done;
961 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
962 buffer, total, length, avail_mode );
963 goto err;
965 else /* synchronous read, wait for the fd to become ready */
967 struct pollfd pfd;
968 int ret, timeout;
970 if (!timeout_init_done)
972 timeout_init_done = TRUE;
973 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
974 goto err;
975 if (hEvent) NtResetEvent( hEvent, NULL );
977 timeout = get_next_io_timeout( &timeouts, total );
979 pfd.fd = unix_handle;
980 pfd.events = POLLIN;
982 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
984 if (total) /* return with what we got so far */
985 status = STATUS_SUCCESS;
986 else
987 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
988 goto done;
990 if (ret == -1 && errno != EINTR)
992 status = FILE_GetNtStatus();
993 goto done;
995 /* will now restart the read */
999 done:
1000 send_completion = cvalue != 0;
1002 err:
1003 if (needs_close) close( unix_handle );
1004 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
1006 io_status->u.Status = status;
1007 io_status->Information = total;
1008 TRACE("= SUCCESS (%u)\n", total);
1009 if (hEvent) NtSetEvent( hEvent, NULL );
1010 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1011 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1013 else
1015 TRACE("= 0x%08x\n", status);
1016 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1019 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1021 return status;
1025 /******************************************************************************
1026 * NtReadFileScatter [NTDLL.@]
1027 * ZwReadFileScatter [NTDLL.@]
1029 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1030 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1031 ULONG length, PLARGE_INTEGER offset, PULONG key )
1033 int result, unix_handle, needs_close;
1034 unsigned int options;
1035 NTSTATUS status;
1036 ULONG pos = 0, total = 0;
1037 enum server_fd_type type;
1038 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1039 BOOL send_completion = FALSE;
1041 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1042 file, event, apc, apc_user, io_status, segments, length, offset, key);
1044 if (length % page_size) return STATUS_INVALID_PARAMETER;
1045 if (!io_status) return STATUS_ACCESS_VIOLATION;
1047 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1048 &needs_close, &type, &options );
1049 if (status) return status;
1051 if ((type != FD_TYPE_FILE) ||
1052 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1053 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1055 status = STATUS_INVALID_PARAMETER;
1056 goto error;
1059 while (length)
1061 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1062 result = pread( unix_handle, (char *)segments->Buffer + pos,
1063 page_size - pos, offset->QuadPart + total );
1064 else
1065 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1067 if (result == -1)
1069 if (errno == EINTR) continue;
1070 status = FILE_GetNtStatus();
1071 break;
1073 if (!result)
1075 status = STATUS_END_OF_FILE;
1076 break;
1078 total += result;
1079 length -= result;
1080 if ((pos += result) == page_size)
1082 pos = 0;
1083 segments++;
1087 send_completion = cvalue != 0;
1089 error:
1090 if (needs_close) close( unix_handle );
1091 if (status == STATUS_SUCCESS)
1093 io_status->u.Status = status;
1094 io_status->Information = total;
1095 TRACE("= SUCCESS (%u)\n", total);
1096 if (event) NtSetEvent( event, NULL );
1097 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1098 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1100 else
1102 TRACE("= 0x%08x\n", status);
1103 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1106 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1108 return status;
1112 /***********************************************************************
1113 * FILE_AsyncWriteService (INTERNAL)
1115 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1117 struct async_fileio_write *fileio = user;
1118 int result, fd, needs_close;
1119 enum server_fd_type type;
1121 switch (status)
1123 case STATUS_ALERTED:
1124 /* write some data (non-blocking) */
1125 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1126 &needs_close, &type, NULL )))
1127 break;
1129 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1130 result = send( fd, fileio->buffer, 0, 0 );
1131 else
1132 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1134 if (needs_close) close( fd );
1136 if (result < 0)
1138 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1139 else status = FILE_GetNtStatus();
1141 else
1143 fileio->already += result;
1144 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1146 break;
1148 case STATUS_TIMEOUT:
1149 case STATUS_IO_TIMEOUT:
1150 if (fileio->already) status = STATUS_SUCCESS;
1151 break;
1153 if (status != STATUS_PENDING)
1155 iosb->u.Status = status;
1156 iosb->Information = fileio->already;
1157 release_fileio( &fileio->io );
1159 return status;
1162 static NTSTATUS set_pending_write( HANDLE device )
1164 NTSTATUS status;
1166 SERVER_START_REQ( set_serial_info )
1168 req->handle = wine_server_obj_handle( device );
1169 req->flags = SERIALINFO_PENDING_WRITE;
1170 status = wine_server_call( req );
1172 SERVER_END_REQ;
1173 return status;
1176 /******************************************************************************
1177 * NtWriteFile [NTDLL.@]
1178 * ZwWriteFile [NTDLL.@]
1180 * Write to an open file handle.
1182 * PARAMS
1183 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1184 * Event [I] Event to signal upon completion (or NULL)
1185 * ApcRoutine [I] Callback to call upon completion (or NULL)
1186 * ApcContext [I] Context for ApcRoutine (or NULL)
1187 * IoStatusBlock [O] Receives information about the operation on return
1188 * Buffer [I] Source for the data to write
1189 * Length [I] Size of Buffer
1190 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1191 * Key [O] Function unknown (may be NULL)
1193 * RETURNS
1194 * Success: 0. IoStatusBlock is updated, and the Information member contains
1195 * The number of bytes written.
1196 * Failure: An NTSTATUS error code describing the error.
1198 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1199 PIO_APC_ROUTINE apc, void* apc_user,
1200 PIO_STATUS_BLOCK io_status,
1201 const void* buffer, ULONG length,
1202 PLARGE_INTEGER offset, PULONG key)
1204 int result, unix_handle, needs_close;
1205 unsigned int options;
1206 struct io_timeouts timeouts;
1207 NTSTATUS status;
1208 ULONG total = 0;
1209 enum server_fd_type type;
1210 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1211 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1212 LARGE_INTEGER offset_eof;
1214 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1215 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1217 if (!io_status) return STATUS_ACCESS_VIOLATION;
1219 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1220 &needs_close, &type, &options );
1221 if (status == STATUS_ACCESS_DENIED)
1223 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1224 &needs_close, &type, &options );
1225 append_write = TRUE;
1227 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
1229 if (!virtual_check_buffer_for_read( buffer, length ))
1231 status = STATUS_INVALID_USER_BUFFER;
1232 goto done;
1235 if (status == STATUS_BAD_DEVICE_TYPE)
1236 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1238 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1240 if (type == FD_TYPE_FILE)
1242 if (async_write &&
1243 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1245 status = STATUS_INVALID_PARAMETER;
1246 goto done;
1249 if (append_write)
1251 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1252 offset = &offset_eof;
1255 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1257 off_t off = offset->QuadPart;
1259 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1261 struct stat st;
1263 if (fstat( unix_handle, &st ) == -1)
1265 status = FILE_GetNtStatus();
1266 goto done;
1268 off = st.st_size;
1270 else if (offset->QuadPart < 0)
1272 status = STATUS_INVALID_PARAMETER;
1273 goto done;
1276 /* async I/O doesn't make sense on regular files */
1277 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1279 if (errno != EINTR)
1281 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1282 else status = FILE_GetNtStatus();
1283 goto done;
1287 if (!async_write)
1288 /* update file pointer position */
1289 lseek( unix_handle, off + result, SEEK_SET );
1291 total = result;
1292 status = STATUS_SUCCESS;
1293 goto done;
1296 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1298 if (async_write &&
1299 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1301 status = STATUS_INVALID_PARAMETER;
1302 goto done;
1306 for (;;)
1308 /* zero-length writes on sockets may not work with plain write(2) */
1309 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1310 result = send( unix_handle, buffer, 0, 0 );
1311 else
1312 result = write( unix_handle, (const char *)buffer + total, length - total );
1314 if (result >= 0)
1316 total += result;
1317 if (total == length)
1319 status = STATUS_SUCCESS;
1320 goto done;
1322 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1324 else if (errno != EAGAIN)
1326 if (errno == EINTR) continue;
1327 if (!total)
1329 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1330 else status = FILE_GetNtStatus();
1332 goto err;
1335 if (async_write)
1337 struct async_fileio_write *fileio;
1339 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), FILE_AsyncWriteService, hFile );
1340 if (!fileio)
1342 status = STATUS_NO_MEMORY;
1343 goto err;
1345 fileio->already = total;
1346 fileio->count = length;
1347 fileio->buffer = buffer;
1349 SERVER_START_REQ( register_async )
1351 req->type = ASYNC_TYPE_WRITE;
1352 req->count = length;
1353 req->async = server_async( hFile, &fileio->io, hEvent, apc, apc_user, io_status );
1354 status = wine_server_call( req );
1356 SERVER_END_REQ;
1358 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1359 goto err;
1361 else /* synchronous write, wait for the fd to become ready */
1363 struct pollfd pfd;
1364 int ret, timeout;
1366 if (!timeout_init_done)
1368 timeout_init_done = TRUE;
1369 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1370 goto err;
1371 if (hEvent) NtResetEvent( hEvent, NULL );
1373 timeout = get_next_io_timeout( &timeouts, total );
1375 pfd.fd = unix_handle;
1376 pfd.events = POLLOUT;
1378 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1380 /* return with what we got so far */
1381 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1382 goto done;
1384 if (ret == -1 && errno != EINTR)
1386 status = FILE_GetNtStatus();
1387 goto done;
1389 /* will now restart the write */
1393 done:
1394 send_completion = cvalue != 0;
1396 err:
1397 if (needs_close) close( unix_handle );
1399 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1400 set_pending_write( hFile );
1402 if (status == STATUS_SUCCESS)
1404 io_status->u.Status = status;
1405 io_status->Information = total;
1406 TRACE("= SUCCESS (%u)\n", total);
1407 if (hEvent) NtSetEvent( hEvent, NULL );
1408 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1409 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1411 else
1413 TRACE("= 0x%08x\n", status);
1414 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1417 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1419 return status;
1423 /******************************************************************************
1424 * NtWriteFileGather [NTDLL.@]
1425 * ZwWriteFileGather [NTDLL.@]
1427 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1428 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1429 ULONG length, PLARGE_INTEGER offset, PULONG key )
1431 int result, unix_handle, needs_close;
1432 unsigned int options;
1433 NTSTATUS status;
1434 ULONG pos = 0, total = 0;
1435 enum server_fd_type type;
1436 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1437 BOOL send_completion = FALSE;
1439 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1440 file, event, apc, apc_user, io_status, segments, length, offset, key);
1442 if (length % page_size) return STATUS_INVALID_PARAMETER;
1443 if (!io_status) return STATUS_ACCESS_VIOLATION;
1445 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1446 &needs_close, &type, &options );
1447 if (status) return status;
1449 if ((type != FD_TYPE_FILE) ||
1450 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1451 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1453 status = STATUS_INVALID_PARAMETER;
1454 goto error;
1457 while (length)
1459 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1460 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1461 page_size - pos, offset->QuadPart + total );
1462 else
1463 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1465 if (result == -1)
1467 if (errno == EINTR) continue;
1468 if (errno == EFAULT)
1470 status = STATUS_INVALID_USER_BUFFER;
1471 goto error;
1473 status = FILE_GetNtStatus();
1474 break;
1476 if (!result)
1478 status = STATUS_DISK_FULL;
1479 break;
1481 total += result;
1482 length -= result;
1483 if ((pos += result) == page_size)
1485 pos = 0;
1486 segments++;
1490 send_completion = cvalue != 0;
1492 error:
1493 if (needs_close) close( unix_handle );
1494 if (status == STATUS_SUCCESS)
1496 io_status->u.Status = status;
1497 io_status->Information = total;
1498 TRACE("= SUCCESS (%u)\n", total);
1499 if (event) NtSetEvent( event, NULL );
1500 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1501 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1503 else
1505 TRACE("= 0x%08x\n", status);
1506 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1509 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1511 return status;
1515 /* do an ioctl call through the server */
1516 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1517 PIO_APC_ROUTINE apc, PVOID apc_context,
1518 IO_STATUS_BLOCK *io, ULONG code,
1519 const void *in_buffer, ULONG in_size,
1520 PVOID out_buffer, ULONG out_size )
1522 struct async_irp *async;
1523 NTSTATUS status;
1524 HANDLE wait_handle;
1525 ULONG options;
1527 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
1528 return STATUS_NO_MEMORY;
1529 async->event = event;
1530 async->buffer = out_buffer;
1531 async->size = out_size;
1533 SERVER_START_REQ( ioctl )
1535 req->code = code;
1536 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
1537 wine_server_add_data( req, in_buffer, in_size );
1538 if ((code & 3) != METHOD_BUFFERED)
1539 wine_server_add_data( req, out_buffer, out_size );
1540 wine_server_set_reply( req, out_buffer, out_size );
1541 status = virtual_locked_server_call( req );
1542 wait_handle = wine_server_ptr_handle( reply->wait );
1543 options = reply->options;
1544 if (wait_handle && status != STATUS_PENDING)
1546 io->u.Status = status;
1547 io->Information = wine_server_reply_size( reply );
1550 SERVER_END_REQ;
1552 if (status == STATUS_NOT_SUPPORTED)
1553 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1554 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1556 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1558 if (wait_handle)
1560 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1561 status = io->u.Status;
1564 return status;
1567 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1568 * server */
1569 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1570 ULONG in_size)
1572 #ifdef VALGRIND_MAKE_MEM_DEFINED
1573 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1574 do { \
1575 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1576 if ((size) >= FIELD_OFFSET(t, f2)) \
1577 VALGRIND_MAKE_MEM_DEFINED( \
1578 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1579 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1580 } while (0)
1582 switch (code)
1584 case FSCTL_PIPE_WAIT:
1585 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1586 break;
1588 #endif
1592 /**************************************************************************
1593 * NtDeviceIoControlFile [NTDLL.@]
1594 * ZwDeviceIoControlFile [NTDLL.@]
1596 * Perform an I/O control operation on an open file handle.
1598 * PARAMS
1599 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1600 * event [I] Event to signal upon completion (or NULL)
1601 * apc [I] Callback to call upon completion (or NULL)
1602 * apc_context [I] Context for ApcRoutine (or NULL)
1603 * io [O] Receives information about the operation on return
1604 * code [I] Control code for the operation to perform
1605 * in_buffer [I] Source for any input data required (or NULL)
1606 * in_size [I] Size of InputBuffer
1607 * out_buffer [O] Source for any output data returned (or NULL)
1608 * out_size [I] Size of OutputBuffer
1610 * RETURNS
1611 * Success: 0. IoStatusBlock is updated.
1612 * Failure: An NTSTATUS error code describing the error.
1614 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1615 PIO_APC_ROUTINE apc, PVOID apc_context,
1616 PIO_STATUS_BLOCK io, ULONG code,
1617 PVOID in_buffer, ULONG in_size,
1618 PVOID out_buffer, ULONG out_size)
1620 ULONG device = (code >> 16);
1621 NTSTATUS status = STATUS_NOT_SUPPORTED;
1623 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1624 handle, event, apc, apc_context, io, code,
1625 in_buffer, in_size, out_buffer, out_size);
1627 switch(device)
1629 case FILE_DEVICE_DISK:
1630 case FILE_DEVICE_CD_ROM:
1631 case FILE_DEVICE_DVD:
1632 case FILE_DEVICE_CONTROLLER:
1633 case FILE_DEVICE_MASS_STORAGE:
1634 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1635 in_buffer, in_size, out_buffer, out_size);
1636 break;
1637 case FILE_DEVICE_SERIAL_PORT:
1638 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1639 in_buffer, in_size, out_buffer, out_size);
1640 break;
1641 case FILE_DEVICE_TAPE:
1642 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1643 in_buffer, in_size, out_buffer, out_size);
1644 break;
1647 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1648 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1649 in_buffer, in_size, out_buffer, out_size );
1651 if (status != STATUS_PENDING) io->u.Status = status;
1652 return status;
1656 /**************************************************************************
1657 * NtFsControlFile [NTDLL.@]
1658 * ZwFsControlFile [NTDLL.@]
1660 * Perform a file system control operation on an open file handle.
1662 * PARAMS
1663 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1664 * event [I] Event to signal upon completion (or NULL)
1665 * apc [I] Callback to call upon completion (or NULL)
1666 * apc_context [I] Context for ApcRoutine (or NULL)
1667 * io [O] Receives information about the operation on return
1668 * code [I] Control code for the operation to perform
1669 * in_buffer [I] Source for any input data required (or NULL)
1670 * in_size [I] Size of InputBuffer
1671 * out_buffer [O] Source for any output data returned (or NULL)
1672 * out_size [I] Size of OutputBuffer
1674 * RETURNS
1675 * Success: 0. IoStatusBlock is updated.
1676 * Failure: An NTSTATUS error code describing the error.
1678 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1679 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1680 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1682 NTSTATUS status;
1684 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1685 handle, event, apc, apc_context, io, code,
1686 in_buffer, in_size, out_buffer, out_size);
1688 if (!io) return STATUS_INVALID_PARAMETER;
1690 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1692 switch(code)
1694 case FSCTL_DISMOUNT_VOLUME:
1695 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1696 in_buffer, in_size, out_buffer, out_size );
1697 if (!status) status = DIR_unmount_device( handle );
1698 return status;
1700 case FSCTL_PIPE_DISCONNECT:
1701 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1702 in_buffer, in_size, out_buffer, out_size );
1703 if (!status)
1705 int fd = server_remove_fd_from_cache( handle );
1706 if (fd != -1) close( fd );
1708 return status;
1710 case FSCTL_PIPE_IMPERSONATE:
1711 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1712 status = RtlImpersonateSelf( SecurityImpersonation );
1713 break;
1715 case FSCTL_IS_VOLUME_MOUNTED:
1716 case FSCTL_LOCK_VOLUME:
1717 case FSCTL_UNLOCK_VOLUME:
1718 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1719 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1720 status = STATUS_SUCCESS;
1721 break;
1723 case FSCTL_GET_RETRIEVAL_POINTERS:
1725 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1727 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1729 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1731 buffer->ExtentCount = 1;
1732 buffer->StartingVcn.QuadPart = 1;
1733 buffer->Extents[0].NextVcn.QuadPart = 0;
1734 buffer->Extents[0].Lcn.QuadPart = 0;
1735 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1736 status = STATUS_SUCCESS;
1738 else
1740 io->Information = 0;
1741 status = STATUS_BUFFER_TOO_SMALL;
1743 break;
1745 case FSCTL_SET_SPARSE:
1746 TRACE("FSCTL_SET_SPARSE: Ignoring request\n");
1747 io->Information = 0;
1748 status = STATUS_SUCCESS;
1749 break;
1750 default:
1751 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1752 in_buffer, in_size, out_buffer, out_size );
1755 if (status != STATUS_PENDING) io->u.Status = status;
1756 return status;
1760 struct read_changes_fileio
1762 struct async_fileio io;
1763 void *buffer;
1764 ULONG buffer_size;
1765 ULONG data_size;
1766 char data[1];
1769 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1771 struct read_changes_fileio *fileio = user;
1772 int size = 0;
1774 if (status == STATUS_ALERTED)
1776 SERVER_START_REQ( read_change )
1778 req->handle = wine_server_obj_handle( fileio->io.handle );
1779 wine_server_set_reply( req, fileio->data, fileio->data_size );
1780 status = wine_server_call( req );
1781 size = wine_server_reply_size( reply );
1783 SERVER_END_REQ;
1785 if (status == STATUS_SUCCESS && fileio->buffer)
1787 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1788 int i, left = fileio->buffer_size;
1789 DWORD *last_entry_offset = NULL;
1790 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1792 while (size && left >= sizeof(*pfni))
1794 /* convert to an NT style path */
1795 for (i = 0; i < event->len; i++)
1796 if (event->name[i] == '/') event->name[i] = '\\';
1798 pfni->Action = event->action;
1799 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1800 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1801 last_entry_offset = &pfni->NextEntryOffset;
1803 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1805 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1806 pfni->FileNameLength *= sizeof(WCHAR);
1807 pfni->NextEntryOffset = i;
1808 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1809 left -= i;
1811 i = (offsetof(struct filesystem_event, name[event->len])
1812 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1813 event = (struct filesystem_event*)((char*)event + i);
1814 size -= i;
1817 if (size)
1819 status = STATUS_NOTIFY_ENUM_DIR;
1820 size = 0;
1822 else
1824 if (last_entry_offset) *last_entry_offset = 0;
1825 size = fileio->buffer_size - left;
1828 else
1830 status = STATUS_NOTIFY_ENUM_DIR;
1831 size = 0;
1835 if (status != STATUS_PENDING)
1837 iosb->u.Status = status;
1838 iosb->Information = size;
1839 release_fileio( &fileio->io );
1841 return status;
1844 #define FILE_NOTIFY_ALL ( \
1845 FILE_NOTIFY_CHANGE_FILE_NAME | \
1846 FILE_NOTIFY_CHANGE_DIR_NAME | \
1847 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1848 FILE_NOTIFY_CHANGE_SIZE | \
1849 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1850 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1851 FILE_NOTIFY_CHANGE_CREATION | \
1852 FILE_NOTIFY_CHANGE_SECURITY )
1854 /******************************************************************************
1855 * NtNotifyChangeDirectoryFile [NTDLL.@]
1857 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1858 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1859 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1861 struct read_changes_fileio *fileio;
1862 NTSTATUS status;
1863 ULONG size = max( 4096, buffer_size );
1865 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1866 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1868 if (!iosb) return STATUS_ACCESS_VIOLATION;
1869 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1871 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1872 read_changes_apc, handle );
1873 if (!fileio) return STATUS_NO_MEMORY;
1875 fileio->buffer = buffer;
1876 fileio->buffer_size = buffer_size;
1877 fileio->data_size = size;
1879 SERVER_START_REQ( read_directory_changes )
1881 req->filter = filter;
1882 req->want_data = (buffer != NULL);
1883 req->subtree = subtree;
1884 req->async = server_async( handle, &fileio->io, event, apc, apc_context, iosb );
1885 status = wine_server_call( req );
1887 SERVER_END_REQ;
1889 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1890 return status;
1893 /******************************************************************************
1894 * NtSetVolumeInformationFile [NTDLL.@]
1895 * ZwSetVolumeInformationFile [NTDLL.@]
1897 * Set volume information for an open file handle.
1899 * PARAMS
1900 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1901 * IoStatusBlock [O] Receives information about the operation on return
1902 * FsInformation [I] Source for volume information
1903 * Length [I] Size of FsInformation
1904 * FsInformationClass [I] Type of volume information to set
1906 * RETURNS
1907 * Success: 0. IoStatusBlock is updated.
1908 * Failure: An NTSTATUS error code describing the error.
1910 NTSTATUS WINAPI NtSetVolumeInformationFile(
1911 IN HANDLE FileHandle,
1912 PIO_STATUS_BLOCK IoStatusBlock,
1913 PVOID FsInformation,
1914 ULONG Length,
1915 FS_INFORMATION_CLASS FsInformationClass)
1917 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1918 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1919 return 0;
1922 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1923 static int futimens( int fd, const struct timespec spec[2] )
1925 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1927 #define HAVE_FUTIMENS
1928 #endif /* __ANDROID__ */
1930 #ifndef UTIME_OMIT
1931 #define UTIME_OMIT ((1 << 30) - 2)
1932 #endif
1934 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1936 NTSTATUS status = STATUS_SUCCESS;
1938 #ifdef HAVE_FUTIMENS
1939 struct timespec tv[2];
1941 tv[0].tv_sec = tv[1].tv_sec = 0;
1942 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1943 if (atime->QuadPart)
1945 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1946 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1948 if (mtime->QuadPart)
1950 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1951 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1953 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1955 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1956 struct timeval tv[2];
1957 struct stat st;
1959 if (!atime->QuadPart || !mtime->QuadPart)
1962 tv[0].tv_sec = tv[0].tv_usec = 0;
1963 tv[1].tv_sec = tv[1].tv_usec = 0;
1964 if (!fstat( fd, &st ))
1966 tv[0].tv_sec = st.st_atime;
1967 tv[1].tv_sec = st.st_mtime;
1968 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1969 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1970 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1971 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1972 #endif
1973 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1974 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1975 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1976 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1977 #endif
1980 if (atime->QuadPart)
1982 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1983 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1985 if (mtime->QuadPart)
1987 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1988 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1990 #ifdef HAVE_FUTIMES
1991 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1992 #elif defined(HAVE_FUTIMESAT)
1993 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1994 #endif
1996 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1997 FIXME( "setting file times not supported\n" );
1998 status = STATUS_NOT_IMPLEMENTED;
1999 #endif
2000 return status;
2003 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
2004 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
2006 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
2007 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
2008 RtlSecondsSince1970ToTime( st->st_atime, atime );
2009 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2010 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
2011 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2012 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
2013 #endif
2014 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2015 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2016 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2017 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2018 #endif
2019 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2020 atime->QuadPart += st->st_atim.tv_nsec / 100;
2021 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2022 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2023 #endif
2024 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2025 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2026 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2027 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2028 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2029 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2030 #endif
2031 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2032 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2033 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2034 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2035 #endif
2036 #else
2037 *creation = *mtime;
2038 #endif
2041 /* fill in the file information that depends on the stat and attribute info */
2042 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2043 FILE_INFORMATION_CLASS class )
2045 switch (class)
2047 case FileBasicInformation:
2049 FILE_BASIC_INFORMATION *info = ptr;
2051 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2052 &info->LastAccessTime, &info->CreationTime );
2053 info->FileAttributes = attr;
2055 break;
2056 case FileStandardInformation:
2058 FILE_STANDARD_INFORMATION *info = ptr;
2060 if ((info->Directory = S_ISDIR(st->st_mode)))
2062 info->AllocationSize.QuadPart = 0;
2063 info->EndOfFile.QuadPart = 0;
2064 info->NumberOfLinks = 1;
2066 else
2068 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2069 info->EndOfFile.QuadPart = st->st_size;
2070 info->NumberOfLinks = st->st_nlink;
2073 break;
2074 case FileInternalInformation:
2076 FILE_INTERNAL_INFORMATION *info = ptr;
2077 info->IndexNumber.QuadPart = st->st_ino;
2079 break;
2080 case FileEndOfFileInformation:
2082 FILE_END_OF_FILE_INFORMATION *info = ptr;
2083 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2085 break;
2086 case FileAllInformation:
2088 FILE_ALL_INFORMATION *info = ptr;
2089 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2090 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2091 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2093 break;
2094 /* all directory structures start with the FileDirectoryInformation layout */
2095 case FileBothDirectoryInformation:
2096 case FileFullDirectoryInformation:
2097 case FileDirectoryInformation:
2099 FILE_DIRECTORY_INFORMATION *info = ptr;
2101 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2102 &info->LastAccessTime, &info->CreationTime );
2103 if (S_ISDIR(st->st_mode))
2105 info->AllocationSize.QuadPart = 0;
2106 info->EndOfFile.QuadPart = 0;
2108 else
2110 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2111 info->EndOfFile.QuadPart = st->st_size;
2113 info->FileAttributes = attr;
2115 break;
2116 case FileIdFullDirectoryInformation:
2118 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2119 info->FileId.QuadPart = st->st_ino;
2120 fill_file_info( st, attr, info, FileDirectoryInformation );
2122 break;
2123 case FileIdBothDirectoryInformation:
2125 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2126 info->FileId.QuadPart = st->st_ino;
2127 fill_file_info( st, attr, info, FileDirectoryInformation );
2129 break;
2130 case FileIdGlobalTxDirectoryInformation:
2132 FILE_ID_GLOBAL_TX_DIR_INFORMATION *info = ptr;
2133 info->FileId.QuadPart = st->st_ino;
2134 fill_file_info( st, attr, info, FileDirectoryInformation );
2136 break;
2138 default:
2139 return STATUS_INVALID_INFO_CLASS;
2141 return STATUS_SUCCESS;
2144 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2146 data_size_t size = 1024;
2147 NTSTATUS ret;
2148 char *name;
2150 for (;;)
2152 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2153 if (!name) return STATUS_NO_MEMORY;
2154 unix_name->MaximumLength = size + 1;
2156 SERVER_START_REQ( get_handle_unix_name )
2158 req->handle = wine_server_obj_handle( handle );
2159 wine_server_set_reply( req, name, size );
2160 ret = wine_server_call( req );
2161 size = reply->name_len;
2163 SERVER_END_REQ;
2165 if (!ret)
2167 name[size] = 0;
2168 unix_name->Buffer = name;
2169 unix_name->Length = size;
2170 break;
2172 RtlFreeHeap( GetProcessHeap(), 0, name );
2173 if (ret != STATUS_BUFFER_OVERFLOW) break;
2175 return ret;
2178 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2180 UNICODE_STRING nt_name;
2181 NTSTATUS status;
2183 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2185 const WCHAR *ptr = nt_name.Buffer;
2186 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2188 /* Skip the volume mount point. */
2189 while (ptr != end && *ptr == '\\') ++ptr;
2190 while (ptr != end && *ptr != '\\') ++ptr;
2191 while (ptr != end && *ptr == '\\') ++ptr;
2192 while (ptr != end && *ptr != '\\') ++ptr;
2194 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2195 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2196 else *name_len = info->FileNameLength;
2198 memcpy( info->FileName, ptr, *name_len );
2199 RtlFreeUnicodeString( &nt_name );
2202 return status;
2205 /******************************************************************************
2206 * NtQueryInformationFile [NTDLL.@]
2207 * ZwQueryInformationFile [NTDLL.@]
2209 * Get information about an open file handle.
2211 * PARAMS
2212 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2213 * io [O] Receives information about the operation on return
2214 * ptr [O] Destination for file information
2215 * len [I] Size of FileInformation
2216 * class [I] Type of file information to get
2218 * RETURNS
2219 * Success: 0. IoStatusBlock and FileInformation are updated.
2220 * Failure: An NTSTATUS error code describing the error.
2222 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2223 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2225 static const size_t info_sizes[] =
2228 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2229 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2230 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2231 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2232 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2233 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2234 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2235 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2236 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2237 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2238 0, /* FileLinkInformation */
2239 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2240 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2241 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2242 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2243 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2244 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2245 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2246 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2247 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2248 0, /* FileAlternateNameInformation */
2249 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2250 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2251 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2252 0, /* FilePipeRemoteInformation */
2253 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2254 0, /* FileMailslotSetInformation */
2255 0, /* FileCompressionInformation */
2256 0, /* FileObjectIdInformation */
2257 0, /* FileCompletionInformation */
2258 0, /* FileMoveClusterInformation */
2259 0, /* FileQuotaInformation */
2260 0, /* FileReparsePointInformation */
2261 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2262 0, /* FileAttributeTagInformation */
2263 0, /* FileTrackingInformation */
2264 0, /* FileIdBothDirectoryInformation */
2265 0, /* FileIdFullDirectoryInformation */
2266 0, /* FileValidDataLengthInformation */
2267 0, /* FileShortNameInformation */
2268 0, /* FileIoCompletionNotificationInformation, */
2269 0, /* FileIoStatusBlockRangeInformation */
2270 0, /* FileIoPriorityHintInformation */
2271 0, /* FileSfioReserveInformation */
2272 0, /* FileSfioVolumeInformation */
2273 0, /* FileHardLinkInformation */
2274 0, /* FileProcessIdsUsingFileInformation */
2275 0, /* FileNormalizedNameInformation */
2276 0, /* FileNetworkPhysicalNameInformation */
2277 0, /* FileIdGlobalTxDirectoryInformation */
2278 0, /* FileIsRemoteDeviceInformation */
2279 0, /* FileAttributeCacheInformation */
2280 0, /* FileNumaNodeInformation */
2281 0, /* FileStandardLinkInformation */
2282 0, /* FileRemoteProtocolInformation */
2283 0, /* FileRenameInformationBypassAccessCheck */
2284 0, /* FileLinkInformationBypassAccessCheck */
2285 0, /* FileVolumeNameInformation */
2286 sizeof(FILE_ID_INFORMATION), /* FileIdInformation */
2287 0, /* FileIdExtdDirectoryInformation */
2288 0, /* FileReplaceCompletionInformation */
2289 0, /* FileHardLinkFullIdInformation */
2290 0, /* FileIdExtdBothDirectoryInformation */
2293 struct stat st;
2294 int fd, needs_close = FALSE;
2295 ULONG attr;
2297 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2299 io->Information = 0;
2301 if (class <= 0 || class >= FileMaximumInformation)
2302 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2303 if (!info_sizes[class])
2305 FIXME("Unsupported class (%d)\n", class);
2306 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2308 if (len < info_sizes[class])
2309 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2311 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2313 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2314 return io->u.Status;
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 FileAccessInformation:
2359 FILE_ACCESS_INFORMATION *info = ptr;
2360 SERVER_START_REQ( get_object_info )
2362 req->handle = wine_server_obj_handle( hFile );
2363 io->u.Status = wine_server_call( req );
2364 if (io->u.Status == STATUS_SUCCESS)
2365 info->AccessFlags = reply->access;
2367 SERVER_END_REQ;
2369 break;
2370 case FileEndOfFileInformation:
2371 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2372 else fill_file_info( &st, attr, ptr, class );
2373 break;
2374 case FileAllInformation:
2376 FILE_ALL_INFORMATION *info = ptr;
2377 ANSI_STRING unix_name;
2379 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2380 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2381 io->u.Status = STATUS_INVALID_INFO_CLASS;
2382 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2384 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2386 fill_file_info( &st, attr, info, FileAllInformation );
2387 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2388 info->EaInformation.EaSize = 0;
2389 info->AccessInformation.AccessFlags = 0; /* FIXME */
2390 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2391 info->ModeInformation.Mode = 0; /* FIXME */
2392 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2394 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2395 RtlFreeAnsiString( &unix_name );
2396 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2399 break;
2400 case FileMailslotQueryInformation:
2402 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2404 SERVER_START_REQ( set_mailslot_info )
2406 req->handle = wine_server_obj_handle( hFile );
2407 req->flags = 0;
2408 io->u.Status = wine_server_call( req );
2409 if( io->u.Status == STATUS_SUCCESS )
2411 info->MaximumMessageSize = reply->max_msgsize;
2412 info->MailslotQuota = 0;
2413 info->NextMessageSize = 0;
2414 info->MessagesAvailable = 0;
2415 info->ReadTimeout.QuadPart = reply->read_timeout;
2418 SERVER_END_REQ;
2419 if (!io->u.Status)
2421 char *tmpbuf;
2422 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2423 if (size > 0x10000) size = 0x10000;
2424 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2426 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2428 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2429 info->MessagesAvailable = (res > 0);
2430 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2431 if (needs_close) close( fd );
2433 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2437 break;
2438 case FilePipeInformation:
2440 FILE_PIPE_INFORMATION* pi = ptr;
2442 SERVER_START_REQ( get_named_pipe_info )
2444 req->handle = wine_server_obj_handle( hFile );
2445 if (!(io->u.Status = wine_server_call( req )))
2447 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2448 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2449 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2450 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2453 SERVER_END_REQ;
2455 break;
2456 case FilePipeLocalInformation:
2458 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2460 SERVER_START_REQ( get_named_pipe_info )
2462 req->handle = wine_server_obj_handle( hFile );
2463 if (!(io->u.Status = wine_server_call( req )))
2465 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2466 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2467 switch (reply->sharing)
2469 case FILE_SHARE_READ:
2470 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2471 break;
2472 case FILE_SHARE_WRITE:
2473 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2474 break;
2475 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2476 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2477 break;
2479 pli->MaximumInstances = reply->maxinstances;
2480 pli->CurrentInstances = reply->instances;
2481 pli->InboundQuota = reply->insize;
2482 pli->ReadDataAvailable = 0; /* FIXME */
2483 pli->OutboundQuota = reply->outsize;
2484 pli->WriteQuotaAvailable = 0; /* FIXME */
2485 pli->NamedPipeState = 0; /* FIXME */
2486 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2487 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2490 SERVER_END_REQ;
2492 break;
2493 case FileNameInformation:
2495 FILE_NAME_INFORMATION *info = ptr;
2496 ANSI_STRING unix_name;
2498 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2500 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2501 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2502 RtlFreeAnsiString( &unix_name );
2503 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2506 break;
2507 case FileNetworkOpenInformation:
2509 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2510 ANSI_STRING unix_name;
2512 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2514 ULONG attributes;
2515 struct stat st;
2517 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2518 io->u.Status = FILE_GetNtStatus();
2519 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2520 io->u.Status = STATUS_INVALID_INFO_CLASS;
2521 else
2523 FILE_BASIC_INFORMATION basic;
2524 FILE_STANDARD_INFORMATION std;
2526 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2527 fill_file_info( &st, attributes, &std, FileStandardInformation );
2529 info->CreationTime = basic.CreationTime;
2530 info->LastAccessTime = basic.LastAccessTime;
2531 info->LastWriteTime = basic.LastWriteTime;
2532 info->ChangeTime = basic.ChangeTime;
2533 info->AllocationSize = std.AllocationSize;
2534 info->EndOfFile = std.EndOfFile;
2535 info->FileAttributes = basic.FileAttributes;
2537 RtlFreeAnsiString( &unix_name );
2540 break;
2541 case FileIdInformation:
2542 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2543 else
2545 FILE_ID_INFORMATION *info = ptr;
2546 info->VolumeSerialNumber = 0; /* FIXME */
2547 memset( &info->FileId, 0, sizeof(info->FileId) );
2548 *(ULONGLONG *)&info->FileId = st.st_ino;
2550 break;
2551 default:
2552 FIXME("Unsupported class (%d)\n", class);
2553 io->u.Status = STATUS_NOT_IMPLEMENTED;
2554 break;
2556 if (needs_close) close( fd );
2557 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2558 return io->u.Status;
2561 /******************************************************************************
2562 * NtSetInformationFile [NTDLL.@]
2563 * ZwSetInformationFile [NTDLL.@]
2565 * Set information about an open file handle.
2567 * PARAMS
2568 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2569 * io [O] Receives information about the operation on return
2570 * ptr [I] Source for file information
2571 * len [I] Size of FileInformation
2572 * class [I] Type of file information to set
2574 * RETURNS
2575 * Success: 0. io is updated.
2576 * Failure: An NTSTATUS error code describing the error.
2578 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2579 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2581 int fd, needs_close;
2583 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2585 io->u.Status = STATUS_SUCCESS;
2586 switch (class)
2588 case FileBasicInformation:
2589 if (len >= sizeof(FILE_BASIC_INFORMATION))
2591 struct stat st;
2592 const FILE_BASIC_INFORMATION *info = ptr;
2594 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2595 return io->u.Status;
2597 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2598 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2600 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2602 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2603 else
2605 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2607 if (S_ISDIR( st.st_mode))
2608 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2609 else
2610 st.st_mode &= ~0222; /* clear write permission bits */
2612 else
2614 /* add write permission only where we already have read permission */
2615 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2617 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2621 if (needs_close) close( fd );
2623 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2624 break;
2626 case FilePositionInformation:
2627 if (len >= sizeof(FILE_POSITION_INFORMATION))
2629 const FILE_POSITION_INFORMATION *info = ptr;
2631 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2632 return io->u.Status;
2634 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2635 io->u.Status = FILE_GetNtStatus();
2637 if (needs_close) close( fd );
2639 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2640 break;
2642 case FileEndOfFileInformation:
2643 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2645 struct stat st;
2646 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2648 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2649 return io->u.Status;
2651 /* first try normal truncate */
2652 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2654 /* now check for the need to extend the file */
2655 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2657 static const char zero;
2659 /* extend the file one byte beyond the requested size and then truncate it */
2660 /* this should work around ftruncate implementations that can't extend files */
2661 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2662 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2664 io->u.Status = FILE_GetNtStatus();
2666 if (needs_close) close( fd );
2668 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2669 break;
2671 case FilePipeInformation:
2672 if (len >= sizeof(FILE_PIPE_INFORMATION))
2674 FILE_PIPE_INFORMATION *info = ptr;
2676 if ((info->CompletionMode | info->ReadMode) & ~1)
2678 io->u.Status = STATUS_INVALID_PARAMETER;
2679 break;
2682 SERVER_START_REQ( set_named_pipe_info )
2684 req->handle = wine_server_obj_handle( handle );
2685 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2686 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2687 io->u.Status = wine_server_call( req );
2689 SERVER_END_REQ;
2691 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2692 break;
2694 case FileMailslotSetInformation:
2696 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2698 SERVER_START_REQ( set_mailslot_info )
2700 req->handle = wine_server_obj_handle( handle );
2701 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2702 req->read_timeout = info->ReadTimeout.QuadPart;
2703 io->u.Status = wine_server_call( req );
2705 SERVER_END_REQ;
2707 break;
2709 case FileCompletionInformation:
2710 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2712 FILE_COMPLETION_INFORMATION *info = ptr;
2714 SERVER_START_REQ( set_completion_info )
2716 req->handle = wine_server_obj_handle( handle );
2717 req->chandle = wine_server_obj_handle( info->CompletionPort );
2718 req->ckey = info->CompletionKey;
2719 io->u.Status = wine_server_call( req );
2721 SERVER_END_REQ;
2722 } else
2723 io->u.Status = STATUS_INVALID_PARAMETER_3;
2724 break;
2726 case FileAllInformation:
2727 io->u.Status = STATUS_INVALID_INFO_CLASS;
2728 break;
2730 case FileValidDataLengthInformation:
2731 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2733 struct stat st;
2734 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2736 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2737 return io->u.Status;
2739 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2740 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2741 io->u.Status = STATUS_INVALID_PARAMETER;
2742 else
2744 #ifdef HAVE_FALLOCATE
2745 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2747 NTSTATUS status = FILE_GetNtStatus();
2748 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2749 else io->u.Status = status;
2751 #else
2752 FIXME( "setting valid data length not supported\n" );
2753 #endif
2755 if (needs_close) close( fd );
2757 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2758 break;
2760 case FileDispositionInformation:
2761 if (len >= sizeof(FILE_DISPOSITION_INFORMATION))
2763 FILE_DISPOSITION_INFORMATION *info = ptr;
2765 SERVER_START_REQ( set_fd_disp_info )
2767 req->handle = wine_server_obj_handle( handle );
2768 req->unlink = info->DoDeleteFile;
2769 io->u.Status = wine_server_call( req );
2771 SERVER_END_REQ;
2772 } else
2773 io->u.Status = STATUS_INVALID_PARAMETER_3;
2774 break;
2776 case FileRenameInformation:
2777 if (len >= sizeof(FILE_RENAME_INFORMATION))
2779 FILE_RENAME_INFORMATION *info = ptr;
2780 UNICODE_STRING name_str;
2781 OBJECT_ATTRIBUTES attr;
2782 ANSI_STRING unix_name;
2784 name_str.Buffer = info->FileName;
2785 name_str.Length = info->FileNameLength;
2786 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2788 attr.Length = sizeof(attr);
2789 attr.ObjectName = &name_str;
2790 attr.RootDirectory = info->RootDir;
2791 attr.Attributes = OBJ_CASE_INSENSITIVE;
2793 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2794 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2795 break;
2797 if (!info->Replace && io->u.Status == STATUS_SUCCESS)
2799 RtlFreeAnsiString( &unix_name );
2800 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2801 break;
2804 SERVER_START_REQ( set_fd_name_info )
2806 req->handle = wine_server_obj_handle( handle );
2807 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2808 req->link = FALSE;
2809 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2810 io->u.Status = wine_server_call( req );
2812 SERVER_END_REQ;
2814 RtlFreeAnsiString( &unix_name );
2816 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2817 break;
2819 case FileLinkInformation:
2820 if (len >= sizeof(FILE_LINK_INFORMATION))
2822 FILE_LINK_INFORMATION *info = ptr;
2823 UNICODE_STRING name_str;
2824 OBJECT_ATTRIBUTES attr;
2825 ANSI_STRING unix_name;
2827 name_str.Buffer = info->FileName;
2828 name_str.Length = info->FileNameLength;
2829 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2831 attr.Length = sizeof(attr);
2832 attr.ObjectName = &name_str;
2833 attr.RootDirectory = info->RootDirectory;
2834 attr.Attributes = OBJ_CASE_INSENSITIVE;
2836 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2837 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2838 break;
2840 if (!info->ReplaceIfExists && io->u.Status == STATUS_SUCCESS)
2842 RtlFreeAnsiString( &unix_name );
2843 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2844 break;
2847 SERVER_START_REQ( set_fd_name_info )
2849 req->handle = wine_server_obj_handle( handle );
2850 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2851 req->link = TRUE;
2852 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2853 io->u.Status = wine_server_call( req );
2855 SERVER_END_REQ;
2857 RtlFreeAnsiString( &unix_name );
2859 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2860 break;
2862 default:
2863 FIXME("Unsupported class (%d)\n", class);
2864 io->u.Status = STATUS_NOT_IMPLEMENTED;
2865 break;
2867 io->Information = 0;
2868 return io->u.Status;
2872 /******************************************************************************
2873 * NtQueryFullAttributesFile (NTDLL.@)
2875 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2876 FILE_NETWORK_OPEN_INFORMATION *info )
2878 ANSI_STRING unix_name;
2879 NTSTATUS status;
2881 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2883 ULONG attributes;
2884 struct stat st;
2886 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2887 status = FILE_GetNtStatus();
2888 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2889 status = STATUS_INVALID_INFO_CLASS;
2890 else
2892 FILE_BASIC_INFORMATION basic;
2893 FILE_STANDARD_INFORMATION std;
2895 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2896 fill_file_info( &st, attributes, &std, FileStandardInformation );
2898 info->CreationTime = basic.CreationTime;
2899 info->LastAccessTime = basic.LastAccessTime;
2900 info->LastWriteTime = basic.LastWriteTime;
2901 info->ChangeTime = basic.ChangeTime;
2902 info->AllocationSize = std.AllocationSize;
2903 info->EndOfFile = std.EndOfFile;
2904 info->FileAttributes = basic.FileAttributes;
2905 if (DIR_is_hidden_file( attr->ObjectName ))
2906 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2908 RtlFreeAnsiString( &unix_name );
2910 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2911 return status;
2915 /******************************************************************************
2916 * NtQueryAttributesFile (NTDLL.@)
2917 * ZwQueryAttributesFile (NTDLL.@)
2919 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2921 ANSI_STRING unix_name;
2922 NTSTATUS status;
2924 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2926 ULONG attributes;
2927 struct stat st;
2929 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2930 status = FILE_GetNtStatus();
2931 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2932 status = STATUS_INVALID_INFO_CLASS;
2933 else
2935 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2936 if (DIR_is_hidden_file( attr->ObjectName ))
2937 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2939 RtlFreeAnsiString( &unix_name );
2941 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2942 return status;
2946 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2947 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2948 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2949 unsigned int flags )
2951 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2953 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2954 /* Don't assume read-only, let the mount options set it below */
2955 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2957 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2958 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2960 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2961 info->Characteristics |= FILE_REMOTE_DEVICE;
2963 else if (!strcmp("procfs", fstypename))
2964 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2965 else
2966 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2968 if (flags & MNT_RDONLY)
2969 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2971 if (!(flags & MNT_LOCAL))
2973 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2974 info->Characteristics |= FILE_REMOTE_DEVICE;
2977 #endif
2979 static inline BOOL is_device_placeholder( int fd )
2981 static const char wine_placeholder[] = "Wine device placeholder";
2982 char buffer[sizeof(wine_placeholder)-1];
2984 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2985 return FALSE;
2986 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2989 /******************************************************************************
2990 * get_device_info
2992 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2994 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2996 struct stat st;
2998 info->Characteristics = 0;
2999 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
3000 if (S_ISCHR( st.st_mode ))
3002 info->DeviceType = FILE_DEVICE_UNKNOWN;
3003 #ifdef linux
3004 switch(major(st.st_rdev))
3006 case MEM_MAJOR:
3007 info->DeviceType = FILE_DEVICE_NULL;
3008 break;
3009 case TTY_MAJOR:
3010 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
3011 break;
3012 case LP_MAJOR:
3013 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
3014 break;
3015 case SCSI_TAPE_MAJOR:
3016 info->DeviceType = FILE_DEVICE_TAPE;
3017 break;
3019 #endif
3021 else if (S_ISBLK( st.st_mode ))
3023 info->DeviceType = FILE_DEVICE_DISK;
3025 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
3027 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
3029 else if (is_device_placeholder( fd ))
3031 info->DeviceType = FILE_DEVICE_DISK;
3033 else /* regular file or directory */
3035 #if defined(linux) && defined(HAVE_FSTATFS)
3036 struct statfs stfs;
3038 /* check for floppy disk */
3039 if (major(st.st_dev) == FLOPPY_MAJOR)
3040 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3042 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
3043 switch (stfs.f_type)
3045 case 0x9660: /* iso9660 */
3046 case 0x9fa1: /* supermount */
3047 case 0x15013346: /* udf */
3048 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3049 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3050 break;
3051 case 0x6969: /* nfs */
3052 case 0xff534d42: /* cifs */
3053 case 0xfe534d42: /* smb2 */
3054 case 0x517b: /* smbfs */
3055 case 0x564c: /* ncpfs */
3056 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3057 info->Characteristics |= FILE_REMOTE_DEVICE;
3058 break;
3059 case 0x01021994: /* tmpfs */
3060 case 0x28cd3d45: /* cramfs */
3061 case 0x1373: /* devfs */
3062 case 0x9fa0: /* procfs */
3063 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3064 break;
3065 default:
3066 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3067 break;
3069 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3070 struct statfs stfs;
3072 if (fstatfs( fd, &stfs ) < 0)
3073 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3074 else
3075 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
3076 #elif defined(__NetBSD__)
3077 struct statvfs stfs;
3079 if (fstatvfs( fd, &stfs) < 0)
3080 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3081 else
3082 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
3083 #elif defined(sun)
3084 /* Use dkio to work out device types */
3086 # include <sys/dkio.h>
3087 # include <sys/vtoc.h>
3088 struct dk_cinfo dkinf;
3089 int retval = ioctl(fd, DKIOCINFO, &dkinf);
3090 if(retval==-1){
3091 WARN("Unable to get disk device type information - assuming a disk like device\n");
3092 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3094 switch (dkinf.dki_ctype)
3096 case DKC_CDROM:
3097 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3098 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3099 break;
3100 case DKC_NCRFLOPPY:
3101 case DKC_SMSFLOPPY:
3102 case DKC_INTEL82072:
3103 case DKC_INTEL82077:
3104 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3105 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3106 break;
3107 case DKC_MD:
3108 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3109 break;
3110 default:
3111 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3114 #else
3115 static int warned;
3116 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3117 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3118 #endif
3119 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3121 return STATUS_SUCCESS;
3125 /******************************************************************************
3126 * NtQueryVolumeInformationFile [NTDLL.@]
3127 * ZwQueryVolumeInformationFile [NTDLL.@]
3129 * Get volume information for an open file handle.
3131 * PARAMS
3132 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3133 * io [O] Receives information about the operation on return
3134 * buffer [O] Destination for volume information
3135 * length [I] Size of FsInformation
3136 * info_class [I] Type of volume information to set
3138 * RETURNS
3139 * Success: 0. io and buffer are updated.
3140 * Failure: An NTSTATUS error code describing the error.
3142 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3143 PVOID buffer, ULONG length,
3144 FS_INFORMATION_CLASS info_class )
3146 int fd, needs_close;
3147 struct stat st;
3148 static int once;
3150 io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL );
3151 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
3153 SERVER_START_REQ( get_volume_info )
3155 req->handle = wine_server_obj_handle( handle );
3156 req->info_class = info_class;
3157 wine_server_set_reply( req, buffer, length );
3158 io->u.Status = wine_server_call( req );
3159 if (!io->u.Status) io->Information = wine_server_reply_size( reply );
3161 SERVER_END_REQ;
3162 return io->u.Status;
3164 else if (io->u.Status) return io->u.Status;
3166 io->u.Status = STATUS_NOT_IMPLEMENTED;
3167 io->Information = 0;
3169 switch( info_class )
3171 case FileFsVolumeInformation:
3172 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3173 break;
3174 case FileFsLabelInformation:
3175 FIXME( "%p: label info not supported\n", handle );
3176 break;
3177 case FileFsSizeInformation:
3178 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3179 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3180 else
3182 FILE_FS_SIZE_INFORMATION *info = buffer;
3184 if (fstat( fd, &st ) < 0)
3186 io->u.Status = FILE_GetNtStatus();
3187 break;
3189 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3191 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3193 else
3195 ULONGLONG bsize;
3196 /* Linux's fstatvfs is buggy */
3197 #if !defined(linux) || !defined(HAVE_FSTATFS)
3198 struct statvfs stfs;
3200 if (fstatvfs( fd, &stfs ) < 0)
3202 io->u.Status = FILE_GetNtStatus();
3203 break;
3205 bsize = stfs.f_frsize;
3206 #else
3207 struct statfs stfs;
3208 if (fstatfs( fd, &stfs ) < 0)
3210 io->u.Status = FILE_GetNtStatus();
3211 break;
3213 bsize = stfs.f_bsize;
3214 #endif
3215 if (bsize == 2048) /* assume CD-ROM */
3217 info->BytesPerSector = 2048;
3218 info->SectorsPerAllocationUnit = 1;
3220 else
3222 info->BytesPerSector = 512;
3223 info->SectorsPerAllocationUnit = 8;
3225 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3226 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3227 io->Information = sizeof(*info);
3228 io->u.Status = STATUS_SUCCESS;
3231 break;
3232 case FileFsDeviceInformation:
3233 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3234 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3235 else
3237 FILE_FS_DEVICE_INFORMATION *info = buffer;
3239 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3240 io->Information = sizeof(*info);
3242 break;
3243 case FileFsAttributeInformation:
3244 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3245 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3246 else
3248 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3250 FIXME( "%p: faking attribute info\n", handle );
3251 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3252 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3253 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3254 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3255 info->FileSystemNameLength = sizeof(ntfsW);
3256 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3258 io->Information = sizeof(*info);
3259 io->u.Status = STATUS_SUCCESS;
3261 break;
3262 case FileFsControlInformation:
3263 FIXME( "%p: control info not supported\n", handle );
3264 break;
3265 case FileFsFullSizeInformation:
3266 FIXME( "%p: full size info not supported\n", handle );
3267 break;
3268 case FileFsObjectIdInformation:
3269 FIXME( "%p: object id info not supported\n", handle );
3270 break;
3271 case FileFsMaximumInformation:
3272 FIXME( "%p: maximum info not supported\n", handle );
3273 break;
3274 default:
3275 io->u.Status = STATUS_INVALID_PARAMETER;
3276 break;
3278 if (needs_close) close( fd );
3279 return io->u.Status;
3283 /******************************************************************
3284 * NtQueryEaFile (NTDLL.@)
3286 * Read extended attributes from NTFS files.
3288 * PARAMS
3289 * hFile [I] File handle, must be opened with FILE_READ_EA access
3290 * iosb [O] Receives information about the operation on return
3291 * buffer [O] Output buffer
3292 * length [I] Length of output buffer
3293 * single_entry [I] Only read and return one entry
3294 * ea_list [I] Optional list with names of EAs to return
3295 * ea_list_len [I] Length of ea_list in bytes
3296 * ea_index [I] Optional pointer to 1-based index of attribute to return
3297 * restart [I] restart EA scan
3299 * RETURNS
3300 * Success: 0. Atrributes read into buffer
3301 * Failure: An NTSTATUS error code describing the error.
3303 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3304 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3305 PULONG ea_index, BOOLEAN restart )
3307 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3308 hFile, iosb, buffer, length, single_entry, ea_list,
3309 ea_list_len, ea_index, restart);
3310 return STATUS_ACCESS_DENIED;
3314 /******************************************************************
3315 * NtSetEaFile (NTDLL.@)
3317 * Update extended attributes for NTFS files.
3319 * PARAMS
3320 * hFile [I] File handle, must be opened with FILE_READ_EA access
3321 * iosb [O] Receives information about the operation on return
3322 * buffer [I] Buffer with EA information
3323 * length [I] Length of buffer
3325 * RETURNS
3326 * Success: 0. Attributes are updated
3327 * Failure: An NTSTATUS error code describing the error.
3329 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3331 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3332 return STATUS_ACCESS_DENIED;
3336 /******************************************************************
3337 * NtFlushBuffersFile (NTDLL.@)
3339 * Flush any buffered data on an open file handle.
3341 * PARAMS
3342 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3343 * IoStatusBlock [O] Receives information about the operation on return
3345 * RETURNS
3346 * Success: 0. IoStatusBlock is updated.
3347 * Failure: An NTSTATUS error code describing the error.
3349 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3351 NTSTATUS ret;
3352 HANDLE hEvent = NULL;
3353 enum server_fd_type type;
3354 int fd, needs_close;
3356 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3357 if (ret == STATUS_ACCESS_DENIED)
3358 ret = server_get_unix_fd( hFile, FILE_APPEND_DATA, &fd, &needs_close, &type, NULL );
3360 if (!ret && type == FD_TYPE_SERIAL)
3362 ret = COMM_FlushBuffersFile( fd );
3364 else if (ret != STATUS_ACCESS_DENIED)
3366 SERVER_START_REQ( flush )
3368 req->async = server_async( hFile, NULL, NULL, NULL, NULL, IoStatusBlock );
3369 ret = wine_server_call( req );
3370 hEvent = wine_server_ptr_handle( reply->event );
3372 SERVER_END_REQ;
3374 if (hEvent)
3376 NtWaitForSingleObject( hEvent, FALSE, NULL );
3377 ret = STATUS_SUCCESS;
3381 if (needs_close) close( fd );
3382 return ret;
3385 /******************************************************************
3386 * NtLockFile (NTDLL.@)
3390 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3391 PIO_APC_ROUTINE apc, void* apc_user,
3392 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3393 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3394 BOOLEAN exclusive )
3396 NTSTATUS ret;
3397 HANDLE handle;
3398 BOOLEAN async;
3399 static BOOLEAN warn = TRUE;
3401 if (apc || io_status || key)
3403 FIXME("Unimplemented yet parameter\n");
3404 return STATUS_NOT_IMPLEMENTED;
3407 if (apc_user && warn)
3409 FIXME("I/O completion on lock not implemented yet\n");
3410 warn = FALSE;
3413 for (;;)
3415 SERVER_START_REQ( lock_file )
3417 req->handle = wine_server_obj_handle( hFile );
3418 req->offset = offset->QuadPart;
3419 req->count = count->QuadPart;
3420 req->shared = !exclusive;
3421 req->wait = !dont_wait;
3422 ret = wine_server_call( req );
3423 handle = wine_server_ptr_handle( reply->handle );
3424 async = reply->overlapped;
3426 SERVER_END_REQ;
3427 if (ret != STATUS_PENDING)
3429 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3430 return ret;
3433 if (async)
3435 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3436 if (handle) NtClose( handle );
3437 return STATUS_PENDING;
3439 if (handle)
3441 NtWaitForSingleObject( handle, FALSE, NULL );
3442 NtClose( handle );
3444 else
3446 LARGE_INTEGER time;
3448 /* Unix lock conflict, sleep a bit and retry */
3449 time.QuadPart = 100 * (ULONGLONG)10000;
3450 time.QuadPart = -time.QuadPart;
3451 NtDelayExecution( FALSE, &time );
3457 /******************************************************************
3458 * NtUnlockFile (NTDLL.@)
3462 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3463 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3464 PULONG key )
3466 NTSTATUS status;
3468 TRACE( "%p %x%08x %x%08x\n",
3469 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3471 if (io_status || key)
3473 FIXME("Unimplemented yet parameter\n");
3474 return STATUS_NOT_IMPLEMENTED;
3477 SERVER_START_REQ( unlock_file )
3479 req->handle = wine_server_obj_handle( hFile );
3480 req->offset = offset->QuadPart;
3481 req->count = count->QuadPart;
3482 status = wine_server_call( req );
3484 SERVER_END_REQ;
3485 return status;
3488 /******************************************************************
3489 * NtCreateNamedPipeFile (NTDLL.@)
3493 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3494 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3495 ULONG sharing, ULONG dispo, ULONG options,
3496 ULONG pipe_type, ULONG read_mode,
3497 ULONG completion_mode, ULONG max_inst,
3498 ULONG inbound_quota, ULONG outbound_quota,
3499 PLARGE_INTEGER timeout)
3501 NTSTATUS status;
3502 data_size_t len;
3503 struct object_attributes *objattr;
3505 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3506 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3507 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3508 outbound_quota, timeout);
3510 if (!attr) return STATUS_INVALID_PARAMETER;
3512 /* assume we only get relative timeout */
3513 if (timeout->QuadPart > 0)
3514 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3516 if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
3518 SERVER_START_REQ( create_named_pipe )
3520 req->access = access;
3521 req->options = options;
3522 req->sharing = sharing;
3523 req->flags =
3524 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3525 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3526 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3527 req->maxinstances = max_inst;
3528 req->outsize = outbound_quota;
3529 req->insize = inbound_quota;
3530 req->timeout = timeout->QuadPart;
3531 wine_server_add_data( req, objattr, len );
3532 status = wine_server_call( req );
3533 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3535 SERVER_END_REQ;
3537 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3538 return status;
3541 /******************************************************************
3542 * NtDeleteFile (NTDLL.@)
3546 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3548 NTSTATUS status;
3549 HANDLE hFile;
3550 IO_STATUS_BLOCK io;
3552 TRACE("%p\n", ObjectAttributes);
3553 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3554 ObjectAttributes, &io, NULL, 0,
3555 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3556 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3557 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3558 return status;
3561 /******************************************************************
3562 * NtCancelIoFileEx (NTDLL.@)
3566 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3568 TRACE("%p %p %p\n", hFile, iosb, io_status );
3570 SERVER_START_REQ( cancel_async )
3572 req->handle = wine_server_obj_handle( hFile );
3573 req->iosb = wine_server_client_ptr( iosb );
3574 req->only_thread = FALSE;
3575 io_status->u.Status = wine_server_call( req );
3577 SERVER_END_REQ;
3579 return io_status->u.Status;
3582 /******************************************************************
3583 * NtCancelIoFile (NTDLL.@)
3587 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3589 TRACE("%p %p\n", hFile, io_status );
3591 SERVER_START_REQ( cancel_async )
3593 req->handle = wine_server_obj_handle( hFile );
3594 req->iosb = 0;
3595 req->only_thread = TRUE;
3596 io_status->u.Status = wine_server_call( req );
3598 SERVER_END_REQ;
3600 return io_status->u.Status;
3603 /******************************************************************************
3604 * NtCreateMailslotFile [NTDLL.@]
3605 * ZwCreateMailslotFile [NTDLL.@]
3607 * PARAMS
3608 * pHandle [O] pointer to receive the handle created
3609 * DesiredAccess [I] access mode (read, write, etc)
3610 * ObjectAttributes [I] fully qualified NT path of the mailslot
3611 * IoStatusBlock [O] receives completion status and other info
3612 * CreateOptions [I]
3613 * MailslotQuota [I]
3614 * MaxMessageSize [I]
3615 * TimeOut [I]
3617 * RETURNS
3618 * An NT status code
3620 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3621 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3622 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3623 PLARGE_INTEGER TimeOut)
3625 LARGE_INTEGER timeout;
3626 NTSTATUS ret;
3627 data_size_t len;
3628 struct object_attributes *objattr;
3630 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3631 pHandle, DesiredAccess, attr, IoStatusBlock,
3632 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3634 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3635 if (!attr) return STATUS_INVALID_PARAMETER;
3637 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3640 * For a NULL TimeOut pointer set the default timeout value
3642 if (!TimeOut)
3643 timeout.QuadPart = -1;
3644 else
3645 timeout.QuadPart = TimeOut->QuadPart;
3647 SERVER_START_REQ( create_mailslot )
3649 req->access = DesiredAccess;
3650 req->max_msgsize = MaxMessageSize;
3651 req->read_timeout = timeout.QuadPart;
3652 wine_server_add_data( req, objattr, len );
3653 ret = wine_server_call( req );
3654 if( ret == STATUS_SUCCESS )
3655 *pHandle = wine_server_ptr_handle( reply->handle );
3657 SERVER_END_REQ;
3659 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3660 return ret;