kernel32/tests: Add some more tests for write watches.
[wine.git] / dlls / ntdll / file.c
blob0381e558ff68b49f6d0a5a5ab0c73dbf4ccb2ec3
1 /*
2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_LINUX_MAJOR_H
31 # include <linux/major.h>
32 #endif
33 #ifdef HAVE_SYS_STATVFS_H
34 # include <sys/statvfs.h>
35 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYS_SYSCALL_H
40 # include <sys/syscall.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
50 #endif
51 #ifdef HAVE_POLL_H
52 #include <poll.h>
53 #endif
54 #ifdef HAVE_SYS_POLL_H
55 #include <sys/poll.h>
56 #endif
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef MAJOR_IN_MKDEV
61 # include <sys/mkdev.h>
62 #elif defined(MAJOR_IN_SYSMACROS)
63 # include <sys/sysmacros.h>
64 #endif
65 #ifdef HAVE_UTIME_H
66 # include <utime.h>
67 #endif
68 #ifdef HAVE_SYS_VFS_H
69 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
70 #define list SYSLIST
71 #define list_next SYSLIST_NEXT
72 #define list_prev SYSLIST_PREV
73 #define list_head SYSLIST_HEAD
74 #define list_tail SYSLIST_TAIL
75 #define list_move_tail SYSLIST_MOVE_TAIL
76 #define list_remove SYSLIST_REMOVE
77 # include <sys/vfs.h>
78 #undef list
79 #undef list_next
80 #undef list_prev
81 #undef list_head
82 #undef list_tail
83 #undef list_move_tail
84 #undef list_remove
85 #endif
86 #ifdef HAVE_SYS_MOUNT_H
87 # include <sys/mount.h>
88 #endif
89 #ifdef HAVE_SYS_STATFS_H
90 # include <sys/statfs.h>
91 #endif
92 #ifdef HAVE_TERMIOS_H
93 #include <termios.h>
94 #endif
95 #ifdef HAVE_VALGRIND_MEMCHECK_H
96 # include <valgrind/memcheck.h>
97 #endif
99 #include "ntstatus.h"
100 #define WIN32_NO_STATUS
101 #define NONAMELESSUNION
102 #include "wine/unicode.h"
103 #include "wine/debug.h"
104 #include "wine/server.h"
105 #include "ntdll_misc.h"
107 #include "winternl.h"
108 #include "winioctl.h"
109 #include "ddk/ntddk.h"
110 #include "ddk/ntddser.h"
112 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
113 WINE_DECLARE_DEBUG_CHANNEL(winediag);
115 mode_t FILE_umask = 0;
117 #define SECSPERDAY 86400
118 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
120 #define FILE_WRITE_TO_END_OF_FILE ((LONGLONG)-1)
121 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
123 static const WCHAR ntfsW[] = {'N','T','F','S'};
125 /* fetch the attributes of a file */
126 static inline ULONG get_file_attributes( const struct stat *st )
128 ULONG attr;
130 if (S_ISDIR(st->st_mode))
131 attr = FILE_ATTRIBUTE_DIRECTORY;
132 else
133 attr = FILE_ATTRIBUTE_ARCHIVE;
134 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
135 attr |= FILE_ATTRIBUTE_READONLY;
136 return attr;
139 /* get the stat info and file attributes for a file (by file descriptor) */
140 int fd_get_file_info( int fd, struct stat *st, ULONG *attr )
142 int ret;
144 *attr = 0;
145 ret = fstat( fd, st );
146 if (ret == -1) return ret;
147 *attr |= get_file_attributes( st );
148 return ret;
151 /* get the stat info and file attributes for a file (by name) */
152 int get_file_info( const char *path, struct stat *st, ULONG *attr )
154 int ret;
156 *attr = 0;
157 ret = lstat( path, st );
158 if (ret == -1) return ret;
159 if (S_ISLNK( st->st_mode ))
161 ret = stat( path, st );
162 if (ret == -1) return ret;
163 /* is a symbolic link and a directory, consider these "reparse points" */
164 if (S_ISDIR( st->st_mode )) *attr |= FILE_ATTRIBUTE_REPARSE_POINT;
166 *attr |= get_file_attributes( st );
167 return ret;
170 /**************************************************************************
171 * FILE_CreateFile (internal)
172 * Open a file.
174 * Parameter set fully identical with NtCreateFile
176 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
177 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
178 ULONG attributes, ULONG sharing, ULONG disposition,
179 ULONG options, PVOID ea_buffer, ULONG ea_length )
181 ANSI_STRING unix_name;
182 BOOL created = FALSE;
184 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
185 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
186 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
187 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
188 attributes, sharing, disposition, options, ea_buffer, ea_length );
190 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
192 if (alloc_size) FIXME( "alloc_size not supported\n" );
194 if (options & FILE_OPEN_BY_FILE_ID)
195 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
196 else
197 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
199 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
201 SERVER_START_REQ( open_file_object )
203 req->access = access;
204 req->attributes = attr->Attributes;
205 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
206 req->sharing = sharing;
207 req->options = options;
208 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
209 io->u.Status = wine_server_call( req );
210 *handle = wine_server_ptr_handle( reply->handle );
212 SERVER_END_REQ;
213 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
214 return io->u.Status;
217 if (io->u.Status == STATUS_NO_SUCH_FILE &&
218 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
220 created = TRUE;
221 io->u.Status = STATUS_SUCCESS;
224 if (io->u.Status == STATUS_SUCCESS)
226 static UNICODE_STRING empty_string;
227 OBJECT_ATTRIBUTES unix_attr = *attr;
228 data_size_t len;
229 struct object_attributes *objattr;
231 unix_attr.ObjectName = &empty_string; /* we send the unix name instead */
232 if ((io->u.Status = alloc_object_attributes( &unix_attr, &objattr, &len )))
234 RtlFreeAnsiString( &unix_name );
235 return io->u.Status;
238 SERVER_START_REQ( create_file )
240 req->access = access;
241 req->sharing = sharing;
242 req->create = disposition;
243 req->options = options;
244 req->attrs = attributes;
245 wine_server_add_data( req, objattr, len );
246 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
247 io->u.Status = wine_server_call( req );
248 *handle = wine_server_ptr_handle( reply->handle );
250 SERVER_END_REQ;
251 RtlFreeHeap( GetProcessHeap(), 0, objattr );
252 RtlFreeAnsiString( &unix_name );
254 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
256 if (io->u.Status == STATUS_SUCCESS)
258 if (created) io->Information = FILE_CREATED;
259 else switch(disposition)
261 case FILE_SUPERSEDE:
262 io->Information = FILE_SUPERSEDED;
263 break;
264 case FILE_CREATE:
265 io->Information = FILE_CREATED;
266 break;
267 case FILE_OPEN:
268 case FILE_OPEN_IF:
269 io->Information = FILE_OPENED;
270 break;
271 case FILE_OVERWRITE:
272 case FILE_OVERWRITE_IF:
273 io->Information = FILE_OVERWRITTEN;
274 break;
277 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
279 static int once;
280 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
283 return io->u.Status;
286 /**************************************************************************
287 * NtOpenFile [NTDLL.@]
288 * ZwOpenFile [NTDLL.@]
290 * Open a file.
292 * PARAMS
293 * handle [O] Variable that receives the file handle on return
294 * access [I] Access desired by the caller to the file
295 * attr [I] Structure describing the file to be opened
296 * io [O] Receives details about the result of the operation
297 * sharing [I] Type of shared access the caller requires
298 * options [I] Options for the file open
300 * RETURNS
301 * Success: 0. FileHandle and IoStatusBlock are updated.
302 * Failure: An NTSTATUS error code describing the error.
304 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
305 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
306 ULONG sharing, ULONG options )
308 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
309 sharing, FILE_OPEN, options, NULL, 0 );
312 /**************************************************************************
313 * NtCreateFile [NTDLL.@]
314 * ZwCreateFile [NTDLL.@]
316 * Either create a new file or directory, or open an existing file, device,
317 * directory or volume.
319 * PARAMS
320 * handle [O] Points to a variable which receives the file handle on return
321 * access [I] Desired access to the file
322 * attr [I] Structure describing the file
323 * io [O] Receives information about the operation on return
324 * alloc_size [I] Initial size of the file in bytes
325 * attributes [I] Attributes to create the file with
326 * sharing [I] Type of shared access the caller would like to the file
327 * disposition [I] Specifies what to do, depending on whether the file already exists
328 * options [I] Options for creating a new file
329 * ea_buffer [I] Pointer to an extended attributes buffer
330 * ea_length [I] Length of ea_buffer
332 * RETURNS
333 * Success: 0. handle and io are updated.
334 * Failure: An NTSTATUS error code describing the error.
336 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
337 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
338 ULONG attributes, ULONG sharing, ULONG disposition,
339 ULONG options, PVOID ea_buffer, ULONG ea_length )
341 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
342 sharing, disposition, options, ea_buffer, ea_length );
345 /***********************************************************************
346 * Asynchronous file I/O *
349 typedef NTSTATUS async_callback_t( void *user, IO_STATUS_BLOCK *io, NTSTATUS status );
351 struct async_fileio
353 async_callback_t *callback; /* must be the first field */
354 struct async_fileio *next;
355 HANDLE handle;
358 struct async_fileio_read
360 struct async_fileio io;
361 char* buffer;
362 unsigned int already;
363 unsigned int count;
364 BOOL avail_mode;
367 struct async_fileio_write
369 struct async_fileio io;
370 const char *buffer;
371 unsigned int already;
372 unsigned int count;
375 struct async_irp
377 struct async_fileio io;
378 HANDLE event; /* async event */
379 void *buffer; /* buffer for output */
380 ULONG size; /* size of buffer */
383 static struct async_fileio *fileio_freelist;
385 static void release_fileio( struct async_fileio *io )
387 for (;;)
389 struct async_fileio *next = fileio_freelist;
390 io->next = next;
391 if (interlocked_cmpxchg_ptr( (void **)&fileio_freelist, io, next ) == next) return;
395 static struct async_fileio *alloc_fileio( DWORD size, async_callback_t callback, HANDLE handle )
397 /* first free remaining previous fileinfos */
399 struct async_fileio *io = interlocked_xchg_ptr( (void **)&fileio_freelist, NULL );
401 while (io)
403 struct async_fileio *next = io->next;
404 RtlFreeHeap( GetProcessHeap(), 0, io );
405 io = next;
408 if ((io = RtlAllocateHeap( GetProcessHeap(), 0, size )))
410 io->callback = callback;
411 io->handle = handle;
413 return io;
416 static async_data_t server_async( HANDLE handle, struct async_fileio *user, HANDLE event,
417 PIO_APC_ROUTINE apc, void *apc_context, IO_STATUS_BLOCK *io )
419 async_data_t async;
420 async.handle = wine_server_obj_handle( handle );
421 async.user = wine_server_client_ptr( user );
422 async.iosb = wine_server_client_ptr( io );
423 async.event = wine_server_obj_handle( event );
424 async.apc = wine_server_client_ptr( apc );
425 async.apc_context = wine_server_client_ptr( apc_context );
426 return async;
429 /* callback for irp async I/O completion */
430 static NTSTATUS irp_completion( void *user, IO_STATUS_BLOCK *io, NTSTATUS status )
432 struct async_irp *async = user;
433 ULONG information = 0;
435 if (status == STATUS_ALERTED)
437 SERVER_START_REQ( get_async_result )
439 req->user_arg = wine_server_client_ptr( async );
440 wine_server_set_reply( req, async->buffer, async->size );
441 status = wine_server_call( req );
442 information = reply->size;
444 SERVER_END_REQ;
446 if (status != STATUS_PENDING)
448 io->u.Status = status;
449 io->Information = information;
450 release_fileio( &async->io );
452 return status;
455 /***********************************************************************
456 * FILE_GetNtStatus(void)
458 * Retrieve the Nt Status code from errno.
459 * Try to be consistent with FILE_SetDosError().
461 NTSTATUS FILE_GetNtStatus(void)
463 int err = errno;
465 TRACE( "errno = %d\n", errno );
466 switch (err)
468 case EAGAIN: return STATUS_SHARING_VIOLATION;
469 case EBADF: return STATUS_INVALID_HANDLE;
470 case EBUSY: return STATUS_DEVICE_BUSY;
471 case ENOSPC: return STATUS_DISK_FULL;
472 case EPERM:
473 case EROFS:
474 case EACCES: return STATUS_ACCESS_DENIED;
475 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
476 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
477 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
478 case EMFILE:
479 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
480 case EINVAL: return STATUS_INVALID_PARAMETER;
481 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
482 case EPIPE: return STATUS_PIPE_DISCONNECTED;
483 case EIO: return STATUS_DEVICE_NOT_READY;
484 #ifdef ENOMEDIUM
485 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
486 #endif
487 case ENXIO: return STATUS_NO_SUCH_DEVICE;
488 case ENOTTY:
489 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
490 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
491 case EFAULT: return STATUS_ACCESS_VIOLATION;
492 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
493 #ifdef ETIME /* Missing on FreeBSD */
494 case ETIME: return STATUS_IO_TIMEOUT;
495 #endif
496 case ENOEXEC: /* ?? */
497 case EEXIST: /* ?? */
498 default:
499 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
500 return STATUS_UNSUCCESSFUL;
504 /***********************************************************************
505 * FILE_AsyncReadService (INTERNAL)
507 static NTSTATUS FILE_AsyncReadService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
509 struct async_fileio_read *fileio = user;
510 int fd, needs_close, result;
512 switch (status)
514 case STATUS_ALERTED: /* got some new data */
515 /* check to see if the data is ready (non-blocking) */
516 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
517 &needs_close, NULL, NULL )))
518 break;
520 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
521 if (needs_close) close( fd );
523 if (result < 0)
525 if (errno == EAGAIN || errno == EINTR)
526 status = STATUS_PENDING;
527 else /* check to see if the transfer is complete */
528 status = FILE_GetNtStatus();
530 else if (result == 0)
532 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
534 else
536 fileio->already += result;
537 if (fileio->already >= fileio->count || fileio->avail_mode)
538 status = STATUS_SUCCESS;
539 else
540 status = STATUS_PENDING;
542 break;
544 case STATUS_TIMEOUT:
545 case STATUS_IO_TIMEOUT:
546 if (fileio->already) status = STATUS_SUCCESS;
547 break;
549 if (status != STATUS_PENDING)
551 iosb->u.Status = status;
552 iosb->Information = fileio->already;
553 release_fileio( &fileio->io );
555 return status;
558 /* do a read call through the server */
559 static NTSTATUS server_read_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
560 IO_STATUS_BLOCK *io, void *buffer, ULONG size,
561 LARGE_INTEGER *offset, ULONG *key )
563 struct async_irp *async;
564 NTSTATUS status;
565 HANDLE wait_handle;
566 ULONG options;
568 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
569 return STATUS_NO_MEMORY;
571 async->event = event;
572 async->buffer = buffer;
573 async->size = size;
575 SERVER_START_REQ( read )
577 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
578 req->pos = offset ? offset->QuadPart : 0;
579 wine_server_set_reply( req, buffer, size );
580 status = wine_server_call( req );
581 wait_handle = wine_server_ptr_handle( reply->wait );
582 options = reply->options;
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_PIPE:
715 case FD_TYPE_CHAR:
716 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
717 break;
718 default:
719 break;
721 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
722 return STATUS_SUCCESS;
726 /* retrieve the timeout for the next wait, in milliseconds */
727 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
729 int ret = -1;
731 if (timeouts->total != -1)
733 ret = timeouts->end_time - NtGetTickCount();
734 if (ret < 0) ret = 0;
736 if (already && timeouts->interval != -1)
738 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
740 return ret;
744 /* retrieve the avail_mode flag for async reads */
745 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
747 NTSTATUS status = STATUS_SUCCESS;
749 switch(type)
751 case FD_TYPE_SERIAL:
753 /* GetCommTimeouts */
754 SERIAL_TIMEOUTS st;
755 IO_STATUS_BLOCK io;
757 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
758 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
759 if (status) break;
760 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
761 !st.ReadTotalTimeoutConstant &&
762 st.ReadIntervalTimeout == MAXDWORD);
764 break;
765 case FD_TYPE_MAILSLOT:
766 case FD_TYPE_SOCKET:
767 case FD_TYPE_PIPE:
768 case FD_TYPE_CHAR:
769 *avail_mode = TRUE;
770 break;
771 default:
772 *avail_mode = FALSE;
773 break;
775 return status;
778 /* register an async I/O for a file read; helper for NtReadFile */
779 static NTSTATUS register_async_file_read( HANDLE handle, HANDLE event,
780 PIO_APC_ROUTINE apc, void *apc_user,
781 IO_STATUS_BLOCK *iosb, void *buffer,
782 ULONG already, ULONG length, BOOL avail_mode )
784 struct async_fileio_read *fileio;
785 NTSTATUS status;
787 if (!(fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), FILE_AsyncReadService, handle )))
788 return STATUS_NO_MEMORY;
790 fileio->already = already;
791 fileio->count = length;
792 fileio->buffer = buffer;
793 fileio->avail_mode = avail_mode;
795 SERVER_START_REQ( register_async )
797 req->type = ASYNC_TYPE_READ;
798 req->count = length;
799 req->async = server_async( handle, &fileio->io, event, apc, apc_user, iosb );
800 status = wine_server_call( req );
802 SERVER_END_REQ;
804 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
805 return status;
809 /******************************************************************************
810 * NtReadFile [NTDLL.@]
811 * ZwReadFile [NTDLL.@]
813 * Read from an open file handle.
815 * PARAMS
816 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
817 * Event [I] Event to signal upon completion (or NULL)
818 * ApcRoutine [I] Callback to call upon completion (or NULL)
819 * ApcContext [I] Context for ApcRoutine (or NULL)
820 * IoStatusBlock [O] Receives information about the operation on return
821 * Buffer [O] Destination for the data read
822 * Length [I] Size of Buffer
823 * ByteOffset [O] Destination for the new file pointer position (or NULL)
824 * Key [O] Function unknown (may be NULL)
826 * RETURNS
827 * Success: 0. IoStatusBlock is updated, and the Information member contains
828 * The number of bytes read.
829 * Failure: An NTSTATUS error code describing the error.
831 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
832 PIO_APC_ROUTINE apc, void* apc_user,
833 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
834 PLARGE_INTEGER offset, PULONG key)
836 int result, unix_handle, needs_close;
837 unsigned int options;
838 struct io_timeouts timeouts;
839 NTSTATUS status;
840 ULONG total = 0;
841 enum server_fd_type type;
842 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
843 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
845 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
846 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
848 if (!io_status) return STATUS_ACCESS_VIOLATION;
850 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
851 &needs_close, &type, &options );
852 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
854 if (!virtual_check_buffer_for_write( buffer, length )) return STATUS_ACCESS_VIOLATION;
856 if (status == STATUS_BAD_DEVICE_TYPE)
857 return server_read_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
859 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
861 if (type == FD_TYPE_FILE)
863 if (async_read && (!offset || offset->QuadPart < 0))
865 status = STATUS_INVALID_PARAMETER;
866 goto done;
869 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
871 /* async I/O doesn't make sense on regular files */
872 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
874 if (errno != EINTR)
876 status = FILE_GetNtStatus();
877 goto done;
880 if (!async_read)
881 /* update file pointer position */
882 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
884 total = result;
885 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
886 goto done;
889 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
891 if (async_read && (!offset || offset->QuadPart < 0))
893 status = STATUS_INVALID_PARAMETER;
894 goto done;
898 if (type == FD_TYPE_SERIAL && async_read && length)
900 /* an asynchronous serial port read with a read interval timeout needs to
901 skip the synchronous read to make sure that the server starts the read
902 interval timer after the first read */
903 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts ))) goto err;
904 if (timeouts.interval)
906 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
907 buffer, total, length, FALSE );
908 goto err;
912 for (;;)
914 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
916 total += result;
917 if (!result || total == length)
919 if (total)
921 status = STATUS_SUCCESS;
922 goto done;
924 switch (type)
926 case FD_TYPE_FILE:
927 case FD_TYPE_CHAR:
928 case FD_TYPE_DEVICE:
929 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
930 goto done;
931 case FD_TYPE_SERIAL:
932 if (!length)
934 status = STATUS_SUCCESS;
935 goto done;
937 break;
938 default:
939 status = STATUS_PIPE_BROKEN;
940 goto done;
943 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
945 else if (errno != EAGAIN)
947 if (errno == EINTR) continue;
948 if (!total) status = FILE_GetNtStatus();
949 goto done;
952 if (async_read)
954 BOOL avail_mode;
956 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
957 goto err;
958 if (total && avail_mode)
960 status = STATUS_SUCCESS;
961 goto done;
963 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
964 buffer, total, length, avail_mode );
965 goto err;
967 else /* synchronous read, wait for the fd to become ready */
969 struct pollfd pfd;
970 int ret, timeout;
972 if (!timeout_init_done)
974 timeout_init_done = TRUE;
975 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
976 goto err;
977 if (hEvent) NtResetEvent( hEvent, NULL );
979 timeout = get_next_io_timeout( &timeouts, total );
981 pfd.fd = unix_handle;
982 pfd.events = POLLIN;
984 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
986 if (total) /* return with what we got so far */
987 status = STATUS_SUCCESS;
988 else
989 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
990 goto done;
992 if (ret == -1 && errno != EINTR)
994 status = FILE_GetNtStatus();
995 goto done;
997 /* will now restart the read */
1001 done:
1002 send_completion = cvalue != 0;
1004 err:
1005 if (needs_close) close( unix_handle );
1006 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
1008 io_status->u.Status = status;
1009 io_status->Information = total;
1010 TRACE("= SUCCESS (%u)\n", total);
1011 if (hEvent) NtSetEvent( hEvent, NULL );
1012 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1013 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1015 else
1017 TRACE("= 0x%08x\n", status);
1018 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1021 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1023 return status;
1027 /******************************************************************************
1028 * NtReadFileScatter [NTDLL.@]
1029 * ZwReadFileScatter [NTDLL.@]
1031 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1032 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1033 ULONG length, PLARGE_INTEGER offset, PULONG key )
1035 int result, unix_handle, needs_close;
1036 unsigned int options;
1037 NTSTATUS status;
1038 ULONG pos = 0, total = 0;
1039 enum server_fd_type type;
1040 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1041 BOOL send_completion = FALSE;
1043 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1044 file, event, apc, apc_user, io_status, segments, length, offset, key);
1046 if (length % page_size) return STATUS_INVALID_PARAMETER;
1047 if (!io_status) return STATUS_ACCESS_VIOLATION;
1049 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1050 &needs_close, &type, &options );
1051 if (status) return status;
1053 if ((type != FD_TYPE_FILE) ||
1054 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1055 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1057 status = STATUS_INVALID_PARAMETER;
1058 goto error;
1061 while (length)
1063 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1064 result = pread( unix_handle, (char *)segments->Buffer + pos,
1065 page_size - pos, offset->QuadPart + total );
1066 else
1067 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1069 if (result == -1)
1071 if (errno == EINTR) continue;
1072 status = FILE_GetNtStatus();
1073 break;
1075 if (!result)
1077 status = STATUS_END_OF_FILE;
1078 break;
1080 total += result;
1081 length -= result;
1082 if ((pos += result) == page_size)
1084 pos = 0;
1085 segments++;
1089 send_completion = cvalue != 0;
1091 error:
1092 if (needs_close) close( unix_handle );
1093 if (status == STATUS_SUCCESS)
1095 io_status->u.Status = status;
1096 io_status->Information = total;
1097 TRACE("= SUCCESS (%u)\n", total);
1098 if (event) NtSetEvent( event, NULL );
1099 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1100 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1102 else
1104 TRACE("= 0x%08x\n", status);
1105 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1108 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1110 return status;
1114 /***********************************************************************
1115 * FILE_AsyncWriteService (INTERNAL)
1117 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1119 struct async_fileio_write *fileio = user;
1120 int result, fd, needs_close;
1121 enum server_fd_type type;
1123 switch (status)
1125 case STATUS_ALERTED:
1126 /* write some data (non-blocking) */
1127 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1128 &needs_close, &type, NULL )))
1129 break;
1131 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1132 result = send( fd, fileio->buffer, 0, 0 );
1133 else
1134 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1136 if (needs_close) close( fd );
1138 if (result < 0)
1140 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1141 else status = FILE_GetNtStatus();
1143 else
1145 fileio->already += result;
1146 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1148 break;
1150 case STATUS_TIMEOUT:
1151 case STATUS_IO_TIMEOUT:
1152 if (fileio->already) status = STATUS_SUCCESS;
1153 break;
1155 if (status != STATUS_PENDING)
1157 iosb->u.Status = status;
1158 iosb->Information = fileio->already;
1159 release_fileio( &fileio->io );
1161 return status;
1164 static NTSTATUS set_pending_write( HANDLE device )
1166 NTSTATUS status;
1168 SERVER_START_REQ( set_serial_info )
1170 req->handle = wine_server_obj_handle( device );
1171 req->flags = SERIALINFO_PENDING_WRITE;
1172 status = wine_server_call( req );
1174 SERVER_END_REQ;
1175 return status;
1178 /******************************************************************************
1179 * NtWriteFile [NTDLL.@]
1180 * ZwWriteFile [NTDLL.@]
1182 * Write to an open file handle.
1184 * PARAMS
1185 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1186 * Event [I] Event to signal upon completion (or NULL)
1187 * ApcRoutine [I] Callback to call upon completion (or NULL)
1188 * ApcContext [I] Context for ApcRoutine (or NULL)
1189 * IoStatusBlock [O] Receives information about the operation on return
1190 * Buffer [I] Source for the data to write
1191 * Length [I] Size of Buffer
1192 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1193 * Key [O] Function unknown (may be NULL)
1195 * RETURNS
1196 * Success: 0. IoStatusBlock is updated, and the Information member contains
1197 * The number of bytes written.
1198 * Failure: An NTSTATUS error code describing the error.
1200 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1201 PIO_APC_ROUTINE apc, void* apc_user,
1202 PIO_STATUS_BLOCK io_status,
1203 const void* buffer, ULONG length,
1204 PLARGE_INTEGER offset, PULONG key)
1206 int result, unix_handle, needs_close;
1207 unsigned int options;
1208 struct io_timeouts timeouts;
1209 NTSTATUS status;
1210 ULONG total = 0;
1211 enum server_fd_type type;
1212 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1213 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1214 LARGE_INTEGER offset_eof;
1216 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1217 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1219 if (!io_status) return STATUS_ACCESS_VIOLATION;
1221 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1222 &needs_close, &type, &options );
1223 if (status == STATUS_ACCESS_DENIED)
1225 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1226 &needs_close, &type, &options );
1227 append_write = TRUE;
1229 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
1231 if (!virtual_check_buffer_for_read( buffer, length ))
1233 status = STATUS_INVALID_USER_BUFFER;
1234 goto done;
1237 if (status == STATUS_BAD_DEVICE_TYPE)
1238 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1240 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1242 if (type == FD_TYPE_FILE)
1244 if (async_write &&
1245 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1247 status = STATUS_INVALID_PARAMETER;
1248 goto done;
1251 if (append_write)
1253 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1254 offset = &offset_eof;
1257 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1259 off_t off = offset->QuadPart;
1261 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1263 struct stat st;
1265 if (fstat( unix_handle, &st ) == -1)
1267 status = FILE_GetNtStatus();
1268 goto done;
1270 off = st.st_size;
1272 else if (offset->QuadPart < 0)
1274 status = STATUS_INVALID_PARAMETER;
1275 goto done;
1278 /* async I/O doesn't make sense on regular files */
1279 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1281 if (errno != EINTR)
1283 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1284 else status = FILE_GetNtStatus();
1285 goto done;
1289 if (!async_write)
1290 /* update file pointer position */
1291 lseek( unix_handle, off + result, SEEK_SET );
1293 total = result;
1294 status = STATUS_SUCCESS;
1295 goto done;
1298 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1300 if (async_write &&
1301 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1303 status = STATUS_INVALID_PARAMETER;
1304 goto done;
1308 for (;;)
1310 /* zero-length writes on sockets may not work with plain write(2) */
1311 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1312 result = send( unix_handle, buffer, 0, 0 );
1313 else
1314 result = write( unix_handle, (const char *)buffer + total, length - total );
1316 if (result >= 0)
1318 total += result;
1319 if (total == length)
1321 status = STATUS_SUCCESS;
1322 goto done;
1324 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1326 else if (errno != EAGAIN)
1328 if (errno == EINTR) continue;
1329 if (!total)
1331 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1332 else status = FILE_GetNtStatus();
1334 goto done;
1337 if (async_write)
1339 struct async_fileio_write *fileio;
1341 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), FILE_AsyncWriteService, hFile );
1342 if (!fileio)
1344 status = STATUS_NO_MEMORY;
1345 goto err;
1347 fileio->already = total;
1348 fileio->count = length;
1349 fileio->buffer = buffer;
1351 SERVER_START_REQ( register_async )
1353 req->type = ASYNC_TYPE_WRITE;
1354 req->count = length;
1355 req->async = server_async( hFile, &fileio->io, hEvent, apc, apc_user, io_status );
1356 status = wine_server_call( req );
1358 SERVER_END_REQ;
1360 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1361 goto err;
1363 else /* synchronous write, wait for the fd to become ready */
1365 struct pollfd pfd;
1366 int ret, timeout;
1368 if (!timeout_init_done)
1370 timeout_init_done = TRUE;
1371 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1372 goto err;
1373 if (hEvent) NtResetEvent( hEvent, NULL );
1375 timeout = get_next_io_timeout( &timeouts, total );
1377 pfd.fd = unix_handle;
1378 pfd.events = POLLOUT;
1380 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1382 /* return with what we got so far */
1383 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1384 goto done;
1386 if (ret == -1 && errno != EINTR)
1388 status = FILE_GetNtStatus();
1389 goto done;
1391 /* will now restart the write */
1395 done:
1396 send_completion = cvalue != 0;
1398 err:
1399 if (needs_close) close( unix_handle );
1401 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1402 set_pending_write( hFile );
1404 if (status == STATUS_SUCCESS)
1406 io_status->u.Status = status;
1407 io_status->Information = total;
1408 TRACE("= SUCCESS (%u)\n", total);
1409 if (hEvent) NtSetEvent( hEvent, NULL );
1410 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1411 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1413 else
1415 TRACE("= 0x%08x\n", status);
1416 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1419 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1421 return status;
1425 /******************************************************************************
1426 * NtWriteFileGather [NTDLL.@]
1427 * ZwWriteFileGather [NTDLL.@]
1429 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1430 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1431 ULONG length, PLARGE_INTEGER offset, PULONG key )
1433 int result, unix_handle, needs_close;
1434 unsigned int options;
1435 NTSTATUS status;
1436 ULONG pos = 0, total = 0;
1437 enum server_fd_type type;
1438 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1439 BOOL send_completion = FALSE;
1441 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1442 file, event, apc, apc_user, io_status, segments, length, offset, key);
1444 if (length % page_size) return STATUS_INVALID_PARAMETER;
1445 if (!io_status) return STATUS_ACCESS_VIOLATION;
1447 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1448 &needs_close, &type, &options );
1449 if (status) return status;
1451 if ((type != FD_TYPE_FILE) ||
1452 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1453 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1455 status = STATUS_INVALID_PARAMETER;
1456 goto error;
1459 while (length)
1461 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1462 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1463 page_size - pos, offset->QuadPart + total );
1464 else
1465 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1467 if (result == -1)
1469 if (errno == EINTR) continue;
1470 if (errno == EFAULT)
1472 status = STATUS_INVALID_USER_BUFFER;
1473 goto error;
1475 status = FILE_GetNtStatus();
1476 break;
1478 if (!result)
1480 status = STATUS_DISK_FULL;
1481 break;
1483 total += result;
1484 length -= result;
1485 if ((pos += result) == page_size)
1487 pos = 0;
1488 segments++;
1492 send_completion = cvalue != 0;
1494 error:
1495 if (needs_close) close( unix_handle );
1496 if (status == STATUS_SUCCESS)
1498 io_status->u.Status = status;
1499 io_status->Information = total;
1500 TRACE("= SUCCESS (%u)\n", total);
1501 if (event) NtSetEvent( event, NULL );
1502 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1503 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1505 else
1507 TRACE("= 0x%08x\n", status);
1508 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1511 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1513 return status;
1517 /* do an ioctl call through the server */
1518 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1519 PIO_APC_ROUTINE apc, PVOID apc_context,
1520 IO_STATUS_BLOCK *io, ULONG code,
1521 const void *in_buffer, ULONG in_size,
1522 PVOID out_buffer, ULONG out_size )
1524 struct async_irp *async;
1525 NTSTATUS status;
1526 HANDLE wait_handle;
1527 ULONG options;
1529 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
1530 return STATUS_NO_MEMORY;
1531 async->event = event;
1532 async->buffer = out_buffer;
1533 async->size = out_size;
1535 SERVER_START_REQ( ioctl )
1537 req->code = code;
1538 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
1539 wine_server_add_data( req, in_buffer, in_size );
1540 if ((code & 3) != METHOD_BUFFERED)
1541 wine_server_add_data( req, out_buffer, out_size );
1542 wine_server_set_reply( req, out_buffer, out_size );
1543 status = wine_server_call( req );
1544 wait_handle = wine_server_ptr_handle( reply->wait );
1545 options = reply->options;
1546 if (wait_handle && status != STATUS_PENDING)
1548 io->u.Status = status;
1549 io->Information = wine_server_reply_size( reply );
1552 SERVER_END_REQ;
1554 if (status == STATUS_NOT_SUPPORTED)
1555 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1556 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1558 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1560 if (wait_handle)
1562 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1563 status = io->u.Status;
1566 return status;
1569 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1570 * server */
1571 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1572 ULONG in_size)
1574 #ifdef VALGRIND_MAKE_MEM_DEFINED
1575 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1576 do { \
1577 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1578 if ((size) >= FIELD_OFFSET(t, f2)) \
1579 VALGRIND_MAKE_MEM_DEFINED( \
1580 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1581 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1582 } while (0)
1584 switch (code)
1586 case FSCTL_PIPE_WAIT:
1587 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1588 break;
1590 #endif
1594 /**************************************************************************
1595 * NtDeviceIoControlFile [NTDLL.@]
1596 * ZwDeviceIoControlFile [NTDLL.@]
1598 * Perform an I/O control operation on an open file handle.
1600 * PARAMS
1601 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1602 * event [I] Event to signal upon completion (or NULL)
1603 * apc [I] Callback to call upon completion (or NULL)
1604 * apc_context [I] Context for ApcRoutine (or NULL)
1605 * io [O] Receives information about the operation on return
1606 * code [I] Control code for the operation to perform
1607 * in_buffer [I] Source for any input data required (or NULL)
1608 * in_size [I] Size of InputBuffer
1609 * out_buffer [O] Source for any output data returned (or NULL)
1610 * out_size [I] Size of OutputBuffer
1612 * RETURNS
1613 * Success: 0. IoStatusBlock is updated.
1614 * Failure: An NTSTATUS error code describing the error.
1616 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1617 PIO_APC_ROUTINE apc, PVOID apc_context,
1618 PIO_STATUS_BLOCK io, ULONG code,
1619 PVOID in_buffer, ULONG in_size,
1620 PVOID out_buffer, ULONG out_size)
1622 ULONG device = (code >> 16);
1623 NTSTATUS status = STATUS_NOT_SUPPORTED;
1625 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1626 handle, event, apc, apc_context, io, code,
1627 in_buffer, in_size, out_buffer, out_size);
1629 switch(device)
1631 case FILE_DEVICE_DISK:
1632 case FILE_DEVICE_CD_ROM:
1633 case FILE_DEVICE_DVD:
1634 case FILE_DEVICE_CONTROLLER:
1635 case FILE_DEVICE_MASS_STORAGE:
1636 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1637 in_buffer, in_size, out_buffer, out_size);
1638 break;
1639 case FILE_DEVICE_SERIAL_PORT:
1640 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1641 in_buffer, in_size, out_buffer, out_size);
1642 break;
1643 case FILE_DEVICE_TAPE:
1644 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1645 in_buffer, in_size, out_buffer, out_size);
1646 break;
1649 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1650 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1651 in_buffer, in_size, out_buffer, out_size );
1653 if (status != STATUS_PENDING) io->u.Status = status;
1654 return status;
1658 /**************************************************************************
1659 * NtFsControlFile [NTDLL.@]
1660 * ZwFsControlFile [NTDLL.@]
1662 * Perform a file system control operation on an open file handle.
1664 * PARAMS
1665 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1666 * event [I] Event to signal upon completion (or NULL)
1667 * apc [I] Callback to call upon completion (or NULL)
1668 * apc_context [I] Context for ApcRoutine (or NULL)
1669 * io [O] Receives information about the operation on return
1670 * code [I] Control code for the operation to perform
1671 * in_buffer [I] Source for any input data required (or NULL)
1672 * in_size [I] Size of InputBuffer
1673 * out_buffer [O] Source for any output data returned (or NULL)
1674 * out_size [I] Size of OutputBuffer
1676 * RETURNS
1677 * Success: 0. IoStatusBlock is updated.
1678 * Failure: An NTSTATUS error code describing the error.
1680 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1681 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1682 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1684 NTSTATUS status;
1686 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1687 handle, event, apc, apc_context, io, code,
1688 in_buffer, in_size, out_buffer, out_size);
1690 if (!io) return STATUS_INVALID_PARAMETER;
1692 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1694 switch(code)
1696 case FSCTL_DISMOUNT_VOLUME:
1697 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1698 in_buffer, in_size, out_buffer, out_size );
1699 if (!status) status = DIR_unmount_device( handle );
1700 return status;
1702 case FSCTL_PIPE_PEEK:
1704 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1705 int avail = 0, fd, needs_close;
1707 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1709 status = STATUS_INFO_LENGTH_MISMATCH;
1710 break;
1713 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1715 if (status == STATUS_BAD_DEVICE_TYPE)
1716 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1717 in_buffer, in_size, out_buffer, out_size );
1718 break;
1721 #ifdef FIONREAD
1722 if (ioctl( fd, FIONREAD, &avail ) != 0)
1724 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1725 if (needs_close) close( fd );
1726 status = FILE_GetNtStatus();
1727 break;
1729 #endif
1730 if (!avail) /* check for closed pipe */
1732 struct pollfd pollfd;
1733 int ret;
1735 pollfd.fd = fd;
1736 pollfd.events = POLLIN;
1737 pollfd.revents = 0;
1738 ret = poll( &pollfd, 1, 0 );
1739 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1741 if (needs_close) close( fd );
1742 status = STATUS_PIPE_BROKEN;
1743 break;
1746 buffer->NamedPipeState = 0; /* FIXME */
1747 buffer->ReadDataAvailable = avail;
1748 buffer->NumberOfMessages = 0; /* FIXME */
1749 buffer->MessageLength = 0; /* FIXME */
1750 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1751 status = STATUS_SUCCESS;
1752 if (avail)
1754 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1755 if (data_size)
1757 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1758 if (res >= 0) io->Information += res;
1761 if (needs_close) close( fd );
1763 break;
1765 case FSCTL_PIPE_DISCONNECT:
1766 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1767 in_buffer, in_size, out_buffer, out_size );
1768 if (!status)
1770 int fd = server_remove_fd_from_cache( handle );
1771 if (fd != -1) close( fd );
1773 return status;
1775 case FSCTL_PIPE_IMPERSONATE:
1776 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1777 status = RtlImpersonateSelf( SecurityImpersonation );
1778 break;
1780 case FSCTL_IS_VOLUME_MOUNTED:
1781 case FSCTL_LOCK_VOLUME:
1782 case FSCTL_UNLOCK_VOLUME:
1783 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1784 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1785 status = STATUS_SUCCESS;
1786 break;
1788 case FSCTL_GET_RETRIEVAL_POINTERS:
1790 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1792 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1794 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1796 buffer->ExtentCount = 1;
1797 buffer->StartingVcn.QuadPart = 1;
1798 buffer->Extents[0].NextVcn.QuadPart = 0;
1799 buffer->Extents[0].Lcn.QuadPart = 0;
1800 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1801 status = STATUS_SUCCESS;
1803 else
1805 io->Information = 0;
1806 status = STATUS_BUFFER_TOO_SMALL;
1808 break;
1810 case FSCTL_SET_SPARSE:
1811 TRACE("FSCTL_SET_SPARSE: Ignoring request\n");
1812 io->Information = 0;
1813 status = STATUS_SUCCESS;
1814 break;
1815 default:
1816 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1817 in_buffer, in_size, out_buffer, out_size );
1820 if (status != STATUS_PENDING) io->u.Status = status;
1821 return status;
1825 struct read_changes_fileio
1827 struct async_fileio io;
1828 void *buffer;
1829 ULONG buffer_size;
1830 ULONG data_size;
1831 char data[1];
1834 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1836 struct read_changes_fileio *fileio = user;
1837 int size = 0;
1839 if (status == STATUS_ALERTED)
1841 SERVER_START_REQ( read_change )
1843 req->handle = wine_server_obj_handle( fileio->io.handle );
1844 wine_server_set_reply( req, fileio->data, fileio->data_size );
1845 status = wine_server_call( req );
1846 size = wine_server_reply_size( reply );
1848 SERVER_END_REQ;
1850 if (status == STATUS_SUCCESS && fileio->buffer)
1852 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1853 int i, left = fileio->buffer_size;
1854 DWORD *last_entry_offset = NULL;
1855 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1857 while (size && left >= sizeof(*pfni))
1859 /* convert to an NT style path */
1860 for (i = 0; i < event->len; i++)
1861 if (event->name[i] == '/') event->name[i] = '\\';
1863 pfni->Action = event->action;
1864 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1865 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1866 last_entry_offset = &pfni->NextEntryOffset;
1868 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1870 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1871 pfni->FileNameLength *= sizeof(WCHAR);
1872 pfni->NextEntryOffset = i;
1873 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1874 left -= i;
1876 i = (offsetof(struct filesystem_event, name[event->len])
1877 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1878 event = (struct filesystem_event*)((char*)event + i);
1879 size -= i;
1882 if (size)
1884 status = STATUS_NOTIFY_ENUM_DIR;
1885 size = 0;
1887 else
1889 if (last_entry_offset) *last_entry_offset = 0;
1890 size = fileio->buffer_size - left;
1893 else
1895 status = STATUS_NOTIFY_ENUM_DIR;
1896 size = 0;
1900 if (status != STATUS_PENDING)
1902 iosb->u.Status = status;
1903 iosb->Information = size;
1904 release_fileio( &fileio->io );
1906 return status;
1909 #define FILE_NOTIFY_ALL ( \
1910 FILE_NOTIFY_CHANGE_FILE_NAME | \
1911 FILE_NOTIFY_CHANGE_DIR_NAME | \
1912 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1913 FILE_NOTIFY_CHANGE_SIZE | \
1914 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1915 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1916 FILE_NOTIFY_CHANGE_CREATION | \
1917 FILE_NOTIFY_CHANGE_SECURITY )
1919 /******************************************************************************
1920 * NtNotifyChangeDirectoryFile [NTDLL.@]
1922 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1923 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1924 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1926 struct read_changes_fileio *fileio;
1927 NTSTATUS status;
1928 ULONG size = max( 4096, buffer_size );
1930 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1931 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1933 if (!iosb) return STATUS_ACCESS_VIOLATION;
1934 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1936 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1937 read_changes_apc, handle );
1938 if (!fileio) return STATUS_NO_MEMORY;
1940 fileio->buffer = buffer;
1941 fileio->buffer_size = buffer_size;
1942 fileio->data_size = size;
1944 SERVER_START_REQ( read_directory_changes )
1946 req->filter = filter;
1947 req->want_data = (buffer != NULL);
1948 req->subtree = subtree;
1949 req->async = server_async( handle, &fileio->io, event, apc, apc_context, iosb );
1950 status = wine_server_call( req );
1952 SERVER_END_REQ;
1954 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1955 return status;
1958 /******************************************************************************
1959 * NtSetVolumeInformationFile [NTDLL.@]
1960 * ZwSetVolumeInformationFile [NTDLL.@]
1962 * Set volume information for an open file handle.
1964 * PARAMS
1965 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1966 * IoStatusBlock [O] Receives information about the operation on return
1967 * FsInformation [I] Source for volume information
1968 * Length [I] Size of FsInformation
1969 * FsInformationClass [I] Type of volume information to set
1971 * RETURNS
1972 * Success: 0. IoStatusBlock is updated.
1973 * Failure: An NTSTATUS error code describing the error.
1975 NTSTATUS WINAPI NtSetVolumeInformationFile(
1976 IN HANDLE FileHandle,
1977 PIO_STATUS_BLOCK IoStatusBlock,
1978 PVOID FsInformation,
1979 ULONG Length,
1980 FS_INFORMATION_CLASS FsInformationClass)
1982 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1983 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1984 return 0;
1987 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1988 static int futimens( int fd, const struct timespec spec[2] )
1990 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1992 #define HAVE_FUTIMENS
1993 #endif /* __ANDROID__ */
1995 #ifndef UTIME_OMIT
1996 #define UTIME_OMIT ((1 << 30) - 2)
1997 #endif
1999 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
2001 NTSTATUS status = STATUS_SUCCESS;
2003 #ifdef HAVE_FUTIMENS
2004 struct timespec tv[2];
2006 tv[0].tv_sec = tv[1].tv_sec = 0;
2007 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
2008 if (atime->QuadPart)
2010 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
2011 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
2013 if (mtime->QuadPart)
2015 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
2016 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
2018 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
2020 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
2021 struct timeval tv[2];
2022 struct stat st;
2024 if (!atime->QuadPart || !mtime->QuadPart)
2027 tv[0].tv_sec = tv[0].tv_usec = 0;
2028 tv[1].tv_sec = tv[1].tv_usec = 0;
2029 if (!fstat( fd, &st ))
2031 tv[0].tv_sec = st.st_atime;
2032 tv[1].tv_sec = st.st_mtime;
2033 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2034 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
2035 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2036 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
2037 #endif
2038 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2039 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
2040 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2041 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
2042 #endif
2045 if (atime->QuadPart)
2047 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
2048 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
2050 if (mtime->QuadPart)
2052 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
2053 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
2055 #ifdef HAVE_FUTIMES
2056 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
2057 #elif defined(HAVE_FUTIMESAT)
2058 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
2059 #endif
2061 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
2062 FIXME( "setting file times not supported\n" );
2063 status = STATUS_NOT_IMPLEMENTED;
2064 #endif
2065 return status;
2068 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
2069 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
2071 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
2072 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
2073 RtlSecondsSince1970ToTime( st->st_atime, atime );
2074 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2075 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
2076 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2077 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
2078 #endif
2079 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2080 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2081 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2082 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2083 #endif
2084 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2085 atime->QuadPart += st->st_atim.tv_nsec / 100;
2086 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2087 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2088 #endif
2089 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2090 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2091 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2092 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2093 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2094 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2095 #endif
2096 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2097 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2098 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2099 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2100 #endif
2101 #else
2102 *creation = *mtime;
2103 #endif
2106 /* fill in the file information that depends on the stat and attribute info */
2107 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2108 FILE_INFORMATION_CLASS class )
2110 switch (class)
2112 case FileBasicInformation:
2114 FILE_BASIC_INFORMATION *info = ptr;
2116 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2117 &info->LastAccessTime, &info->CreationTime );
2118 info->FileAttributes = attr;
2120 break;
2121 case FileStandardInformation:
2123 FILE_STANDARD_INFORMATION *info = ptr;
2125 if ((info->Directory = S_ISDIR(st->st_mode)))
2127 info->AllocationSize.QuadPart = 0;
2128 info->EndOfFile.QuadPart = 0;
2129 info->NumberOfLinks = 1;
2131 else
2133 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2134 info->EndOfFile.QuadPart = st->st_size;
2135 info->NumberOfLinks = st->st_nlink;
2138 break;
2139 case FileInternalInformation:
2141 FILE_INTERNAL_INFORMATION *info = ptr;
2142 info->IndexNumber.QuadPart = st->st_ino;
2144 break;
2145 case FileEndOfFileInformation:
2147 FILE_END_OF_FILE_INFORMATION *info = ptr;
2148 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2150 break;
2151 case FileAllInformation:
2153 FILE_ALL_INFORMATION *info = ptr;
2154 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2155 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2156 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2158 break;
2159 /* all directory structures start with the FileDirectoryInformation layout */
2160 case FileBothDirectoryInformation:
2161 case FileFullDirectoryInformation:
2162 case FileDirectoryInformation:
2164 FILE_DIRECTORY_INFORMATION *info = ptr;
2166 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2167 &info->LastAccessTime, &info->CreationTime );
2168 if (S_ISDIR(st->st_mode))
2170 info->AllocationSize.QuadPart = 0;
2171 info->EndOfFile.QuadPart = 0;
2173 else
2175 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2176 info->EndOfFile.QuadPart = st->st_size;
2178 info->FileAttributes = attr;
2180 break;
2181 case FileIdFullDirectoryInformation:
2183 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2184 info->FileId.QuadPart = st->st_ino;
2185 fill_file_info( st, attr, info, FileDirectoryInformation );
2187 break;
2188 case FileIdBothDirectoryInformation:
2190 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2191 info->FileId.QuadPart = st->st_ino;
2192 fill_file_info( st, attr, info, FileDirectoryInformation );
2194 break;
2195 case FileIdGlobalTxDirectoryInformation:
2197 FILE_ID_GLOBAL_TX_DIR_INFORMATION *info = ptr;
2198 info->FileId.QuadPart = st->st_ino;
2199 fill_file_info( st, attr, info, FileDirectoryInformation );
2201 break;
2203 default:
2204 return STATUS_INVALID_INFO_CLASS;
2206 return STATUS_SUCCESS;
2209 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2211 data_size_t size = 1024;
2212 NTSTATUS ret;
2213 char *name;
2215 for (;;)
2217 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2218 if (!name) return STATUS_NO_MEMORY;
2219 unix_name->MaximumLength = size + 1;
2221 SERVER_START_REQ( get_handle_unix_name )
2223 req->handle = wine_server_obj_handle( handle );
2224 wine_server_set_reply( req, name, size );
2225 ret = wine_server_call( req );
2226 size = reply->name_len;
2228 SERVER_END_REQ;
2230 if (!ret)
2232 name[size] = 0;
2233 unix_name->Buffer = name;
2234 unix_name->Length = size;
2235 break;
2237 RtlFreeHeap( GetProcessHeap(), 0, name );
2238 if (ret != STATUS_BUFFER_OVERFLOW) break;
2240 return ret;
2243 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2245 UNICODE_STRING nt_name;
2246 NTSTATUS status;
2248 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2250 const WCHAR *ptr = nt_name.Buffer;
2251 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2253 /* Skip the volume mount point. */
2254 while (ptr != end && *ptr == '\\') ++ptr;
2255 while (ptr != end && *ptr != '\\') ++ptr;
2256 while (ptr != end && *ptr == '\\') ++ptr;
2257 while (ptr != end && *ptr != '\\') ++ptr;
2259 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2260 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2261 else *name_len = info->FileNameLength;
2263 memcpy( info->FileName, ptr, *name_len );
2264 RtlFreeUnicodeString( &nt_name );
2267 return status;
2270 /******************************************************************************
2271 * NtQueryInformationFile [NTDLL.@]
2272 * ZwQueryInformationFile [NTDLL.@]
2274 * Get information about an open file handle.
2276 * PARAMS
2277 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2278 * io [O] Receives information about the operation on return
2279 * ptr [O] Destination for file information
2280 * len [I] Size of FileInformation
2281 * class [I] Type of file information to get
2283 * RETURNS
2284 * Success: 0. IoStatusBlock and FileInformation are updated.
2285 * Failure: An NTSTATUS error code describing the error.
2287 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2288 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2290 static const size_t info_sizes[] =
2293 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2294 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2295 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2296 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2297 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2298 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2299 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2300 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2301 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2302 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2303 0, /* FileLinkInformation */
2304 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2305 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2306 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2307 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2308 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2309 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2310 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2311 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2312 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2313 0, /* FileAlternateNameInformation */
2314 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2315 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2316 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2317 0, /* FilePipeRemoteInformation */
2318 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2319 0, /* FileMailslotSetInformation */
2320 0, /* FileCompressionInformation */
2321 0, /* FileObjectIdInformation */
2322 0, /* FileCompletionInformation */
2323 0, /* FileMoveClusterInformation */
2324 0, /* FileQuotaInformation */
2325 0, /* FileReparsePointInformation */
2326 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2327 0, /* FileAttributeTagInformation */
2328 0, /* FileTrackingInformation */
2329 0, /* FileIdBothDirectoryInformation */
2330 0, /* FileIdFullDirectoryInformation */
2331 0, /* FileValidDataLengthInformation */
2332 0, /* FileShortNameInformation */
2333 0, /* FileIoCompletionNotificationInformation, */
2334 0, /* FileIoStatusBlockRangeInformation */
2335 0, /* FileIoPriorityHintInformation */
2336 0, /* FileSfioReserveInformation */
2337 0, /* FileSfioVolumeInformation */
2338 0, /* FileHardLinkInformation */
2339 0, /* FileProcessIdsUsingFileInformation */
2340 0, /* FileNormalizedNameInformation */
2341 0, /* FileNetworkPhysicalNameInformation */
2342 0, /* FileIdGlobalTxDirectoryInformation */
2343 0, /* FileIsRemoteDeviceInformation */
2344 0, /* FileAttributeCacheInformation */
2345 0, /* FileNumaNodeInformation */
2346 0, /* FileStandardLinkInformation */
2347 0, /* FileRemoteProtocolInformation */
2348 0, /* FileRenameInformationBypassAccessCheck */
2349 0, /* FileLinkInformationBypassAccessCheck */
2350 0, /* FileVolumeNameInformation */
2351 sizeof(FILE_ID_INFORMATION), /* FileIdInformation */
2352 0, /* FileIdExtdDirectoryInformation */
2353 0, /* FileReplaceCompletionInformation */
2354 0, /* FileHardLinkFullIdInformation */
2355 0, /* FileIdExtdBothDirectoryInformation */
2358 struct stat st;
2359 int fd, needs_close = FALSE;
2360 ULONG attr;
2362 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2364 io->Information = 0;
2366 if (class <= 0 || class >= FileMaximumInformation)
2367 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2368 if (!info_sizes[class])
2370 FIXME("Unsupported class (%d)\n", class);
2371 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2373 if (len < info_sizes[class])
2374 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2376 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2378 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2379 return io->u.Status;
2382 switch (class)
2384 case FileBasicInformation:
2385 if (fd_get_file_info( fd, &st, &attr ) == -1)
2386 io->u.Status = FILE_GetNtStatus();
2387 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2388 io->u.Status = STATUS_INVALID_INFO_CLASS;
2389 else
2390 fill_file_info( &st, attr, ptr, class );
2391 break;
2392 case FileStandardInformation:
2394 FILE_STANDARD_INFORMATION *info = ptr;
2396 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2397 else
2399 fill_file_info( &st, attr, info, class );
2400 info->DeletePending = FALSE; /* FIXME */
2403 break;
2404 case FilePositionInformation:
2406 FILE_POSITION_INFORMATION *info = ptr;
2407 off_t res = lseek( fd, 0, SEEK_CUR );
2408 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2409 else info->CurrentByteOffset.QuadPart = res;
2411 break;
2412 case FileInternalInformation:
2413 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2414 else fill_file_info( &st, attr, ptr, class );
2415 break;
2416 case FileEaInformation:
2418 FILE_EA_INFORMATION *info = ptr;
2419 info->EaSize = 0;
2421 break;
2422 case FileAccessInformation:
2424 FILE_ACCESS_INFORMATION *info = ptr;
2425 SERVER_START_REQ( get_object_info )
2427 req->handle = wine_server_obj_handle( hFile );
2428 io->u.Status = wine_server_call( req );
2429 if (io->u.Status == STATUS_SUCCESS)
2430 info->AccessFlags = reply->access;
2432 SERVER_END_REQ;
2434 break;
2435 case FileEndOfFileInformation:
2436 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2437 else fill_file_info( &st, attr, ptr, class );
2438 break;
2439 case FileAllInformation:
2441 FILE_ALL_INFORMATION *info = ptr;
2442 ANSI_STRING unix_name;
2444 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2445 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2446 io->u.Status = STATUS_INVALID_INFO_CLASS;
2447 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2449 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2451 fill_file_info( &st, attr, info, FileAllInformation );
2452 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2453 info->EaInformation.EaSize = 0;
2454 info->AccessInformation.AccessFlags = 0; /* FIXME */
2455 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2456 info->ModeInformation.Mode = 0; /* FIXME */
2457 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2459 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2460 RtlFreeAnsiString( &unix_name );
2461 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2464 break;
2465 case FileMailslotQueryInformation:
2467 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2469 SERVER_START_REQ( set_mailslot_info )
2471 req->handle = wine_server_obj_handle( hFile );
2472 req->flags = 0;
2473 io->u.Status = wine_server_call( req );
2474 if( io->u.Status == STATUS_SUCCESS )
2476 info->MaximumMessageSize = reply->max_msgsize;
2477 info->MailslotQuota = 0;
2478 info->NextMessageSize = 0;
2479 info->MessagesAvailable = 0;
2480 info->ReadTimeout.QuadPart = reply->read_timeout;
2483 SERVER_END_REQ;
2484 if (!io->u.Status)
2486 char *tmpbuf;
2487 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2488 if (size > 0x10000) size = 0x10000;
2489 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2491 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2493 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2494 info->MessagesAvailable = (res > 0);
2495 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2496 if (needs_close) close( fd );
2498 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2502 break;
2503 case FilePipeInformation:
2505 FILE_PIPE_INFORMATION* pi = ptr;
2507 SERVER_START_REQ( get_named_pipe_info )
2509 req->handle = wine_server_obj_handle( hFile );
2510 if (!(io->u.Status = wine_server_call( req )))
2512 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2513 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2514 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2515 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2518 SERVER_END_REQ;
2520 break;
2521 case FilePipeLocalInformation:
2523 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2525 SERVER_START_REQ( get_named_pipe_info )
2527 req->handle = wine_server_obj_handle( hFile );
2528 if (!(io->u.Status = wine_server_call( req )))
2530 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2531 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2532 switch (reply->sharing)
2534 case FILE_SHARE_READ:
2535 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2536 break;
2537 case FILE_SHARE_WRITE:
2538 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2539 break;
2540 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2541 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2542 break;
2544 pli->MaximumInstances = reply->maxinstances;
2545 pli->CurrentInstances = reply->instances;
2546 pli->InboundQuota = reply->insize;
2547 pli->ReadDataAvailable = 0; /* FIXME */
2548 pli->OutboundQuota = reply->outsize;
2549 pli->WriteQuotaAvailable = 0; /* FIXME */
2550 pli->NamedPipeState = 0; /* FIXME */
2551 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2552 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2555 SERVER_END_REQ;
2557 break;
2558 case FileNameInformation:
2560 FILE_NAME_INFORMATION *info = ptr;
2561 ANSI_STRING unix_name;
2563 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2565 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2566 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2567 RtlFreeAnsiString( &unix_name );
2568 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2571 break;
2572 case FileNetworkOpenInformation:
2574 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2575 ANSI_STRING unix_name;
2577 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2579 ULONG attributes;
2580 struct stat st;
2582 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2583 io->u.Status = FILE_GetNtStatus();
2584 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2585 io->u.Status = STATUS_INVALID_INFO_CLASS;
2586 else
2588 FILE_BASIC_INFORMATION basic;
2589 FILE_STANDARD_INFORMATION std;
2591 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2592 fill_file_info( &st, attributes, &std, FileStandardInformation );
2594 info->CreationTime = basic.CreationTime;
2595 info->LastAccessTime = basic.LastAccessTime;
2596 info->LastWriteTime = basic.LastWriteTime;
2597 info->ChangeTime = basic.ChangeTime;
2598 info->AllocationSize = std.AllocationSize;
2599 info->EndOfFile = std.EndOfFile;
2600 info->FileAttributes = basic.FileAttributes;
2602 RtlFreeAnsiString( &unix_name );
2605 break;
2606 case FileIdInformation:
2607 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2608 else
2610 FILE_ID_INFORMATION *info = ptr;
2611 info->VolumeSerialNumber = 0; /* FIXME */
2612 memset( &info->FileId, 0, sizeof(info->FileId) );
2613 *(ULONGLONG *)&info->FileId = st.st_ino;
2615 break;
2616 default:
2617 FIXME("Unsupported class (%d)\n", class);
2618 io->u.Status = STATUS_NOT_IMPLEMENTED;
2619 break;
2621 if (needs_close) close( fd );
2622 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2623 return io->u.Status;
2626 /******************************************************************************
2627 * NtSetInformationFile [NTDLL.@]
2628 * ZwSetInformationFile [NTDLL.@]
2630 * Set information about an open file handle.
2632 * PARAMS
2633 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2634 * io [O] Receives information about the operation on return
2635 * ptr [I] Source for file information
2636 * len [I] Size of FileInformation
2637 * class [I] Type of file information to set
2639 * RETURNS
2640 * Success: 0. io is updated.
2641 * Failure: An NTSTATUS error code describing the error.
2643 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2644 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2646 int fd, needs_close;
2648 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2650 io->u.Status = STATUS_SUCCESS;
2651 switch (class)
2653 case FileBasicInformation:
2654 if (len >= sizeof(FILE_BASIC_INFORMATION))
2656 struct stat st;
2657 const FILE_BASIC_INFORMATION *info = ptr;
2659 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2660 return io->u.Status;
2662 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2663 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2665 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2667 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2668 else
2670 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2672 if (S_ISDIR( st.st_mode))
2673 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2674 else
2675 st.st_mode &= ~0222; /* clear write permission bits */
2677 else
2679 /* add write permission only where we already have read permission */
2680 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2682 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2686 if (needs_close) close( fd );
2688 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2689 break;
2691 case FilePositionInformation:
2692 if (len >= sizeof(FILE_POSITION_INFORMATION))
2694 const FILE_POSITION_INFORMATION *info = ptr;
2696 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2697 return io->u.Status;
2699 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2700 io->u.Status = FILE_GetNtStatus();
2702 if (needs_close) close( fd );
2704 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2705 break;
2707 case FileEndOfFileInformation:
2708 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2710 struct stat st;
2711 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2713 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2714 return io->u.Status;
2716 /* first try normal truncate */
2717 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2719 /* now check for the need to extend the file */
2720 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2722 static const char zero;
2724 /* extend the file one byte beyond the requested size and then truncate it */
2725 /* this should work around ftruncate implementations that can't extend files */
2726 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2727 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2729 io->u.Status = FILE_GetNtStatus();
2731 if (needs_close) close( fd );
2733 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2734 break;
2736 case FilePipeInformation:
2737 if (len >= sizeof(FILE_PIPE_INFORMATION))
2739 FILE_PIPE_INFORMATION *info = ptr;
2741 if ((info->CompletionMode | info->ReadMode) & ~1)
2743 io->u.Status = STATUS_INVALID_PARAMETER;
2744 break;
2747 SERVER_START_REQ( set_named_pipe_info )
2749 req->handle = wine_server_obj_handle( handle );
2750 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2751 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2752 io->u.Status = wine_server_call( req );
2754 SERVER_END_REQ;
2756 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2757 break;
2759 case FileMailslotSetInformation:
2761 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2763 SERVER_START_REQ( set_mailslot_info )
2765 req->handle = wine_server_obj_handle( handle );
2766 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2767 req->read_timeout = info->ReadTimeout.QuadPart;
2768 io->u.Status = wine_server_call( req );
2770 SERVER_END_REQ;
2772 break;
2774 case FileCompletionInformation:
2775 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2777 FILE_COMPLETION_INFORMATION *info = ptr;
2779 SERVER_START_REQ( set_completion_info )
2781 req->handle = wine_server_obj_handle( handle );
2782 req->chandle = wine_server_obj_handle( info->CompletionPort );
2783 req->ckey = info->CompletionKey;
2784 io->u.Status = wine_server_call( req );
2786 SERVER_END_REQ;
2787 } else
2788 io->u.Status = STATUS_INVALID_PARAMETER_3;
2789 break;
2791 case FileAllInformation:
2792 io->u.Status = STATUS_INVALID_INFO_CLASS;
2793 break;
2795 case FileValidDataLengthInformation:
2796 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2798 struct stat st;
2799 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2801 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2802 return io->u.Status;
2804 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2805 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2806 io->u.Status = STATUS_INVALID_PARAMETER;
2807 else
2809 #ifdef HAVE_FALLOCATE
2810 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2812 NTSTATUS status = FILE_GetNtStatus();
2813 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2814 else io->u.Status = status;
2816 #else
2817 FIXME( "setting valid data length not supported\n" );
2818 #endif
2820 if (needs_close) close( fd );
2822 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2823 break;
2825 case FileDispositionInformation:
2826 if (len >= sizeof(FILE_DISPOSITION_INFORMATION))
2828 FILE_DISPOSITION_INFORMATION *info = ptr;
2830 SERVER_START_REQ( set_fd_disp_info )
2832 req->handle = wine_server_obj_handle( handle );
2833 req->unlink = info->DoDeleteFile;
2834 io->u.Status = wine_server_call( req );
2836 SERVER_END_REQ;
2837 } else
2838 io->u.Status = STATUS_INVALID_PARAMETER_3;
2839 break;
2841 case FileRenameInformation:
2842 if (len >= sizeof(FILE_RENAME_INFORMATION))
2844 FILE_RENAME_INFORMATION *info = ptr;
2845 UNICODE_STRING name_str;
2846 OBJECT_ATTRIBUTES attr;
2847 ANSI_STRING unix_name;
2849 name_str.Buffer = info->FileName;
2850 name_str.Length = info->FileNameLength;
2851 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2853 attr.Length = sizeof(attr);
2854 attr.ObjectName = &name_str;
2855 attr.RootDirectory = info->RootDir;
2856 attr.Attributes = OBJ_CASE_INSENSITIVE;
2858 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2859 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2860 break;
2862 if (!info->Replace && io->u.Status == STATUS_SUCCESS)
2864 RtlFreeAnsiString( &unix_name );
2865 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2866 break;
2869 SERVER_START_REQ( set_fd_name_info )
2871 req->handle = wine_server_obj_handle( handle );
2872 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2873 req->link = FALSE;
2874 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2875 io->u.Status = wine_server_call( req );
2877 SERVER_END_REQ;
2879 RtlFreeAnsiString( &unix_name );
2881 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2882 break;
2884 case FileLinkInformation:
2885 if (len >= sizeof(FILE_LINK_INFORMATION))
2887 FILE_LINK_INFORMATION *info = ptr;
2888 UNICODE_STRING name_str;
2889 OBJECT_ATTRIBUTES attr;
2890 ANSI_STRING unix_name;
2892 name_str.Buffer = info->FileName;
2893 name_str.Length = info->FileNameLength;
2894 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2896 attr.Length = sizeof(attr);
2897 attr.ObjectName = &name_str;
2898 attr.RootDirectory = info->RootDirectory;
2899 attr.Attributes = OBJ_CASE_INSENSITIVE;
2901 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2902 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2903 break;
2905 if (!info->ReplaceIfExists && io->u.Status == STATUS_SUCCESS)
2907 RtlFreeAnsiString( &unix_name );
2908 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2909 break;
2912 SERVER_START_REQ( set_fd_name_info )
2914 req->handle = wine_server_obj_handle( handle );
2915 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2916 req->link = TRUE;
2917 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2918 io->u.Status = wine_server_call( req );
2920 SERVER_END_REQ;
2922 RtlFreeAnsiString( &unix_name );
2924 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2925 break;
2927 default:
2928 FIXME("Unsupported class (%d)\n", class);
2929 io->u.Status = STATUS_NOT_IMPLEMENTED;
2930 break;
2932 io->Information = 0;
2933 return io->u.Status;
2937 /******************************************************************************
2938 * NtQueryFullAttributesFile (NTDLL.@)
2940 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2941 FILE_NETWORK_OPEN_INFORMATION *info )
2943 ANSI_STRING unix_name;
2944 NTSTATUS status;
2946 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2948 ULONG attributes;
2949 struct stat st;
2951 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2952 status = FILE_GetNtStatus();
2953 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2954 status = STATUS_INVALID_INFO_CLASS;
2955 else
2957 FILE_BASIC_INFORMATION basic;
2958 FILE_STANDARD_INFORMATION std;
2960 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2961 fill_file_info( &st, attributes, &std, FileStandardInformation );
2963 info->CreationTime = basic.CreationTime;
2964 info->LastAccessTime = basic.LastAccessTime;
2965 info->LastWriteTime = basic.LastWriteTime;
2966 info->ChangeTime = basic.ChangeTime;
2967 info->AllocationSize = std.AllocationSize;
2968 info->EndOfFile = std.EndOfFile;
2969 info->FileAttributes = basic.FileAttributes;
2970 if (DIR_is_hidden_file( attr->ObjectName ))
2971 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2973 RtlFreeAnsiString( &unix_name );
2975 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2976 return status;
2980 /******************************************************************************
2981 * NtQueryAttributesFile (NTDLL.@)
2982 * ZwQueryAttributesFile (NTDLL.@)
2984 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2986 ANSI_STRING unix_name;
2987 NTSTATUS status;
2989 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2991 ULONG attributes;
2992 struct stat st;
2994 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2995 status = FILE_GetNtStatus();
2996 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2997 status = STATUS_INVALID_INFO_CLASS;
2998 else
3000 status = fill_file_info( &st, attributes, info, FileBasicInformation );
3001 if (DIR_is_hidden_file( attr->ObjectName ))
3002 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
3004 RtlFreeAnsiString( &unix_name );
3006 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
3007 return status;
3011 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3012 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
3013 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
3014 unsigned int flags )
3016 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
3018 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3019 /* Don't assume read-only, let the mount options set it below */
3020 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3022 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
3023 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
3025 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3026 info->Characteristics |= FILE_REMOTE_DEVICE;
3028 else if (!strcmp("procfs", fstypename))
3029 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3030 else
3031 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3033 if (flags & MNT_RDONLY)
3034 info->Characteristics |= FILE_READ_ONLY_DEVICE;
3036 if (!(flags & MNT_LOCAL))
3038 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3039 info->Characteristics |= FILE_REMOTE_DEVICE;
3042 #endif
3044 static inline BOOL is_device_placeholder( int fd )
3046 static const char wine_placeholder[] = "Wine device placeholder";
3047 char buffer[sizeof(wine_placeholder)-1];
3049 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
3050 return FALSE;
3051 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
3054 /******************************************************************************
3055 * get_device_info
3057 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
3059 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
3061 struct stat st;
3063 info->Characteristics = 0;
3064 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
3065 if (S_ISCHR( st.st_mode ))
3067 info->DeviceType = FILE_DEVICE_UNKNOWN;
3068 #ifdef linux
3069 switch(major(st.st_rdev))
3071 case MEM_MAJOR:
3072 info->DeviceType = FILE_DEVICE_NULL;
3073 break;
3074 case TTY_MAJOR:
3075 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
3076 break;
3077 case LP_MAJOR:
3078 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
3079 break;
3080 case SCSI_TAPE_MAJOR:
3081 info->DeviceType = FILE_DEVICE_TAPE;
3082 break;
3084 #endif
3086 else if (S_ISBLK( st.st_mode ))
3088 info->DeviceType = FILE_DEVICE_DISK;
3090 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
3092 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
3094 else if (is_device_placeholder( fd ))
3096 info->DeviceType = FILE_DEVICE_DISK;
3098 else /* regular file or directory */
3100 #if defined(linux) && defined(HAVE_FSTATFS)
3101 struct statfs stfs;
3103 /* check for floppy disk */
3104 if (major(st.st_dev) == FLOPPY_MAJOR)
3105 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3107 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
3108 switch (stfs.f_type)
3110 case 0x9660: /* iso9660 */
3111 case 0x9fa1: /* supermount */
3112 case 0x15013346: /* udf */
3113 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3114 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3115 break;
3116 case 0x6969: /* nfs */
3117 case 0x517B: /* smbfs */
3118 case 0x564c: /* ncpfs */
3119 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3120 info->Characteristics |= FILE_REMOTE_DEVICE;
3121 break;
3122 case 0x01021994: /* tmpfs */
3123 case 0x28cd3d45: /* cramfs */
3124 case 0x1373: /* devfs */
3125 case 0x9fa0: /* procfs */
3126 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3127 break;
3128 default:
3129 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3130 break;
3132 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3133 struct statfs stfs;
3135 if (fstatfs( fd, &stfs ) < 0)
3136 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3137 else
3138 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
3139 #elif defined(__NetBSD__)
3140 struct statvfs stfs;
3142 if (fstatvfs( fd, &stfs) < 0)
3143 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3144 else
3145 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
3146 #elif defined(sun)
3147 /* Use dkio to work out device types */
3149 # include <sys/dkio.h>
3150 # include <sys/vtoc.h>
3151 struct dk_cinfo dkinf;
3152 int retval = ioctl(fd, DKIOCINFO, &dkinf);
3153 if(retval==-1){
3154 WARN("Unable to get disk device type information - assuming a disk like device\n");
3155 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3157 switch (dkinf.dki_ctype)
3159 case DKC_CDROM:
3160 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3161 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3162 break;
3163 case DKC_NCRFLOPPY:
3164 case DKC_SMSFLOPPY:
3165 case DKC_INTEL82072:
3166 case DKC_INTEL82077:
3167 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3168 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3169 break;
3170 case DKC_MD:
3171 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3172 break;
3173 default:
3174 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3177 #else
3178 static int warned;
3179 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3180 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3181 #endif
3182 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3184 return STATUS_SUCCESS;
3188 /******************************************************************************
3189 * NtQueryVolumeInformationFile [NTDLL.@]
3190 * ZwQueryVolumeInformationFile [NTDLL.@]
3192 * Get volume information for an open file handle.
3194 * PARAMS
3195 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3196 * io [O] Receives information about the operation on return
3197 * buffer [O] Destination for volume information
3198 * length [I] Size of FsInformation
3199 * info_class [I] Type of volume information to set
3201 * RETURNS
3202 * Success: 0. io and buffer are updated.
3203 * Failure: An NTSTATUS error code describing the error.
3205 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3206 PVOID buffer, ULONG length,
3207 FS_INFORMATION_CLASS info_class )
3209 int fd, needs_close;
3210 struct stat st;
3211 static int once;
3213 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
3214 return io->u.Status;
3216 io->u.Status = STATUS_NOT_IMPLEMENTED;
3217 io->Information = 0;
3219 switch( info_class )
3221 case FileFsVolumeInformation:
3222 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3223 break;
3224 case FileFsLabelInformation:
3225 FIXME( "%p: label info not supported\n", handle );
3226 break;
3227 case FileFsSizeInformation:
3228 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3229 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3230 else
3232 FILE_FS_SIZE_INFORMATION *info = buffer;
3234 if (fstat( fd, &st ) < 0)
3236 io->u.Status = FILE_GetNtStatus();
3237 break;
3239 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3241 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3243 else
3245 ULONGLONG bsize;
3246 /* Linux's fstatvfs is buggy */
3247 #if !defined(linux) || !defined(HAVE_FSTATFS)
3248 struct statvfs stfs;
3250 if (fstatvfs( fd, &stfs ) < 0)
3252 io->u.Status = FILE_GetNtStatus();
3253 break;
3255 bsize = stfs.f_frsize;
3256 #else
3257 struct statfs stfs;
3258 if (fstatfs( fd, &stfs ) < 0)
3260 io->u.Status = FILE_GetNtStatus();
3261 break;
3263 bsize = stfs.f_bsize;
3264 #endif
3265 if (bsize == 2048) /* assume CD-ROM */
3267 info->BytesPerSector = 2048;
3268 info->SectorsPerAllocationUnit = 1;
3270 else
3272 info->BytesPerSector = 512;
3273 info->SectorsPerAllocationUnit = 8;
3275 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3276 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3277 io->Information = sizeof(*info);
3278 io->u.Status = STATUS_SUCCESS;
3281 break;
3282 case FileFsDeviceInformation:
3283 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3284 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3285 else
3287 FILE_FS_DEVICE_INFORMATION *info = buffer;
3289 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3290 io->Information = sizeof(*info);
3292 break;
3293 case FileFsAttributeInformation:
3294 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3295 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3296 else
3298 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3300 FIXME( "%p: faking attribute info\n", handle );
3301 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3302 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3303 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3304 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3305 info->FileSystemNameLength = sizeof(ntfsW);
3306 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3308 io->Information = sizeof(*info);
3309 io->u.Status = STATUS_SUCCESS;
3311 break;
3312 case FileFsControlInformation:
3313 FIXME( "%p: control info not supported\n", handle );
3314 break;
3315 case FileFsFullSizeInformation:
3316 FIXME( "%p: full size info not supported\n", handle );
3317 break;
3318 case FileFsObjectIdInformation:
3319 FIXME( "%p: object id info not supported\n", handle );
3320 break;
3321 case FileFsMaximumInformation:
3322 FIXME( "%p: maximum info not supported\n", handle );
3323 break;
3324 default:
3325 io->u.Status = STATUS_INVALID_PARAMETER;
3326 break;
3328 if (needs_close) close( fd );
3329 return io->u.Status;
3333 /******************************************************************
3334 * NtQueryEaFile (NTDLL.@)
3336 * Read extended attributes from NTFS files.
3338 * PARAMS
3339 * hFile [I] File handle, must be opened with FILE_READ_EA access
3340 * iosb [O] Receives information about the operation on return
3341 * buffer [O] Output buffer
3342 * length [I] Length of output buffer
3343 * single_entry [I] Only read and return one entry
3344 * ea_list [I] Optional list with names of EAs to return
3345 * ea_list_len [I] Length of ea_list in bytes
3346 * ea_index [I] Optional pointer to 1-based index of attribute to return
3347 * restart [I] restart EA scan
3349 * RETURNS
3350 * Success: 0. Atrributes read into buffer
3351 * Failure: An NTSTATUS error code describing the error.
3353 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3354 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3355 PULONG ea_index, BOOLEAN restart )
3357 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3358 hFile, iosb, buffer, length, single_entry, ea_list,
3359 ea_list_len, ea_index, restart);
3360 return STATUS_ACCESS_DENIED;
3364 /******************************************************************
3365 * NtSetEaFile (NTDLL.@)
3367 * Update extended attributes for NTFS files.
3369 * PARAMS
3370 * hFile [I] File handle, must be opened with FILE_READ_EA access
3371 * iosb [O] Receives information about the operation on return
3372 * buffer [I] Buffer with EA information
3373 * length [I] Length of buffer
3375 * RETURNS
3376 * Success: 0. Attributes are updated
3377 * Failure: An NTSTATUS error code describing the error.
3379 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3381 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3382 return STATUS_ACCESS_DENIED;
3386 /******************************************************************
3387 * NtFlushBuffersFile (NTDLL.@)
3389 * Flush any buffered data on an open file handle.
3391 * PARAMS
3392 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3393 * IoStatusBlock [O] Receives information about the operation on return
3395 * RETURNS
3396 * Success: 0. IoStatusBlock is updated.
3397 * Failure: An NTSTATUS error code describing the error.
3399 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3401 NTSTATUS ret;
3402 HANDLE hEvent = NULL;
3403 enum server_fd_type type;
3404 int fd, needs_close;
3406 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3407 if (ret == STATUS_ACCESS_DENIED)
3408 ret = server_get_unix_fd( hFile, FILE_APPEND_DATA, &fd, &needs_close, &type, NULL );
3410 if (!ret && type == FD_TYPE_SERIAL)
3412 ret = COMM_FlushBuffersFile( fd );
3414 else if (ret != STATUS_ACCESS_DENIED)
3416 SERVER_START_REQ( flush )
3418 req->async = server_async( hFile, NULL, NULL, NULL, NULL, IoStatusBlock );
3419 ret = wine_server_call( req );
3420 hEvent = wine_server_ptr_handle( reply->event );
3422 SERVER_END_REQ;
3424 if (hEvent)
3426 NtWaitForSingleObject( hEvent, FALSE, NULL );
3427 ret = STATUS_SUCCESS;
3431 if (needs_close) close( fd );
3432 return ret;
3435 /******************************************************************
3436 * NtLockFile (NTDLL.@)
3440 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3441 PIO_APC_ROUTINE apc, void* apc_user,
3442 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3443 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3444 BOOLEAN exclusive )
3446 NTSTATUS ret;
3447 HANDLE handle;
3448 BOOLEAN async;
3449 static BOOLEAN warn = TRUE;
3451 if (apc || io_status || key)
3453 FIXME("Unimplemented yet parameter\n");
3454 return STATUS_NOT_IMPLEMENTED;
3457 if (apc_user && warn)
3459 FIXME("I/O completion on lock not implemented yet\n");
3460 warn = FALSE;
3463 for (;;)
3465 SERVER_START_REQ( lock_file )
3467 req->handle = wine_server_obj_handle( hFile );
3468 req->offset = offset->QuadPart;
3469 req->count = count->QuadPart;
3470 req->shared = !exclusive;
3471 req->wait = !dont_wait;
3472 ret = wine_server_call( req );
3473 handle = wine_server_ptr_handle( reply->handle );
3474 async = reply->overlapped;
3476 SERVER_END_REQ;
3477 if (ret != STATUS_PENDING)
3479 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3480 return ret;
3483 if (async)
3485 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3486 if (handle) NtClose( handle );
3487 return STATUS_PENDING;
3489 if (handle)
3491 NtWaitForSingleObject( handle, FALSE, NULL );
3492 NtClose( handle );
3494 else
3496 LARGE_INTEGER time;
3498 /* Unix lock conflict, sleep a bit and retry */
3499 time.QuadPart = 100 * (ULONGLONG)10000;
3500 time.QuadPart = -time.QuadPart;
3501 NtDelayExecution( FALSE, &time );
3507 /******************************************************************
3508 * NtUnlockFile (NTDLL.@)
3512 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3513 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3514 PULONG key )
3516 NTSTATUS status;
3518 TRACE( "%p %x%08x %x%08x\n",
3519 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3521 if (io_status || key)
3523 FIXME("Unimplemented yet parameter\n");
3524 return STATUS_NOT_IMPLEMENTED;
3527 SERVER_START_REQ( unlock_file )
3529 req->handle = wine_server_obj_handle( hFile );
3530 req->offset = offset->QuadPart;
3531 req->count = count->QuadPart;
3532 status = wine_server_call( req );
3534 SERVER_END_REQ;
3535 return status;
3538 /******************************************************************
3539 * NtCreateNamedPipeFile (NTDLL.@)
3543 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3544 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3545 ULONG sharing, ULONG dispo, ULONG options,
3546 ULONG pipe_type, ULONG read_mode,
3547 ULONG completion_mode, ULONG max_inst,
3548 ULONG inbound_quota, ULONG outbound_quota,
3549 PLARGE_INTEGER timeout)
3551 NTSTATUS status;
3552 data_size_t len;
3553 struct object_attributes *objattr;
3555 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3556 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3557 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3558 outbound_quota, timeout);
3560 if (!attr) return STATUS_INVALID_PARAMETER;
3562 /* assume we only get relative timeout */
3563 if (timeout->QuadPart > 0)
3564 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3566 if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
3568 SERVER_START_REQ( create_named_pipe )
3570 req->access = access;
3571 req->options = options;
3572 req->sharing = sharing;
3573 req->flags =
3574 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3575 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3576 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3577 req->maxinstances = max_inst;
3578 req->outsize = outbound_quota;
3579 req->insize = inbound_quota;
3580 req->timeout = timeout->QuadPart;
3581 wine_server_add_data( req, objattr, len );
3582 status = wine_server_call( req );
3583 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3585 SERVER_END_REQ;
3587 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3588 return status;
3591 /******************************************************************
3592 * NtDeleteFile (NTDLL.@)
3596 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3598 NTSTATUS status;
3599 HANDLE hFile;
3600 IO_STATUS_BLOCK io;
3602 TRACE("%p\n", ObjectAttributes);
3603 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3604 ObjectAttributes, &io, NULL, 0,
3605 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3606 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3607 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3608 return status;
3611 /******************************************************************
3612 * NtCancelIoFileEx (NTDLL.@)
3616 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3618 TRACE("%p %p %p\n", hFile, iosb, io_status );
3620 SERVER_START_REQ( cancel_async )
3622 req->handle = wine_server_obj_handle( hFile );
3623 req->iosb = wine_server_client_ptr( iosb );
3624 req->only_thread = FALSE;
3625 io_status->u.Status = wine_server_call( req );
3627 SERVER_END_REQ;
3629 return io_status->u.Status;
3632 /******************************************************************
3633 * NtCancelIoFile (NTDLL.@)
3637 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3639 TRACE("%p %p\n", hFile, io_status );
3641 SERVER_START_REQ( cancel_async )
3643 req->handle = wine_server_obj_handle( hFile );
3644 req->iosb = 0;
3645 req->only_thread = TRUE;
3646 io_status->u.Status = wine_server_call( req );
3648 SERVER_END_REQ;
3650 return io_status->u.Status;
3653 /******************************************************************************
3654 * NtCreateMailslotFile [NTDLL.@]
3655 * ZwCreateMailslotFile [NTDLL.@]
3657 * PARAMS
3658 * pHandle [O] pointer to receive the handle created
3659 * DesiredAccess [I] access mode (read, write, etc)
3660 * ObjectAttributes [I] fully qualified NT path of the mailslot
3661 * IoStatusBlock [O] receives completion status and other info
3662 * CreateOptions [I]
3663 * MailslotQuota [I]
3664 * MaxMessageSize [I]
3665 * TimeOut [I]
3667 * RETURNS
3668 * An NT status code
3670 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3671 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3672 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3673 PLARGE_INTEGER TimeOut)
3675 LARGE_INTEGER timeout;
3676 NTSTATUS ret;
3677 data_size_t len;
3678 struct object_attributes *objattr;
3680 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3681 pHandle, DesiredAccess, attr, IoStatusBlock,
3682 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3684 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3685 if (!attr) return STATUS_INVALID_PARAMETER;
3687 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3690 * For a NULL TimeOut pointer set the default timeout value
3692 if (!TimeOut)
3693 timeout.QuadPart = -1;
3694 else
3695 timeout.QuadPart = TimeOut->QuadPart;
3697 SERVER_START_REQ( create_mailslot )
3699 req->access = DesiredAccess;
3700 req->max_msgsize = MaxMessageSize;
3701 req->read_timeout = timeout.QuadPart;
3702 wine_server_add_data( req, objattr, len );
3703 ret = wine_server_call( req );
3704 if( ret == STATUS_SUCCESS )
3705 *pHandle = wine_server_ptr_handle( reply->handle );
3707 SERVER_END_REQ;
3709 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3710 return ret;