ntdll: Return system info from NtQuerySystemInformation on Linux.
[wine.git] / dlls / ntdll / file.c
blobb9d24251f229caea7704ba2934afa225a79d8fc8
1 /*
2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_LINUX_MAJOR_H
31 # include <linux/major.h>
32 #endif
33 #ifdef HAVE_SYS_STATVFS_H
34 # include <sys/statvfs.h>
35 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYS_SYSCALL_H
40 # include <sys/syscall.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
50 #endif
51 #ifdef HAVE_POLL_H
52 #include <poll.h>
53 #endif
54 #ifdef HAVE_SYS_POLL_H
55 #include <sys/poll.h>
56 #endif
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef MAJOR_IN_MKDEV
61 # include <sys/mkdev.h>
62 #elif defined(MAJOR_IN_SYSMACROS)
63 # include <sys/sysmacros.h>
64 #endif
65 #ifdef HAVE_UTIME_H
66 # include <utime.h>
67 #endif
68 #ifdef HAVE_SYS_VFS_H
69 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
70 #define list SYSLIST
71 #define list_next SYSLIST_NEXT
72 #define list_prev SYSLIST_PREV
73 #define list_head SYSLIST_HEAD
74 #define list_tail SYSLIST_TAIL
75 #define list_move_tail SYSLIST_MOVE_TAIL
76 #define list_remove SYSLIST_REMOVE
77 # include <sys/vfs.h>
78 #undef list
79 #undef list_next
80 #undef list_prev
81 #undef list_head
82 #undef list_tail
83 #undef list_move_tail
84 #undef list_remove
85 #endif
86 #ifdef HAVE_SYS_MOUNT_H
87 # include <sys/mount.h>
88 #endif
89 #ifdef HAVE_SYS_STATFS_H
90 # include <sys/statfs.h>
91 #endif
92 #ifdef HAVE_TERMIOS_H
93 #include <termios.h>
94 #endif
95 #ifdef HAVE_VALGRIND_MEMCHECK_H
96 # include <valgrind/memcheck.h>
97 #endif
99 #include "ntstatus.h"
100 #define WIN32_NO_STATUS
101 #define NONAMELESSUNION
102 #include "wine/unicode.h"
103 #include "wine/debug.h"
104 #include "wine/server.h"
105 #include "ntdll_misc.h"
107 #include "winternl.h"
108 #include "winioctl.h"
109 #include "ddk/ntddk.h"
110 #include "ddk/ntddser.h"
112 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
113 WINE_DECLARE_DEBUG_CHANNEL(winediag);
115 mode_t FILE_umask = 0;
117 #define SECSPERDAY 86400
118 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
120 #define FILE_WRITE_TO_END_OF_FILE ((LONGLONG)-1)
121 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
123 static const WCHAR ntfsW[] = {'N','T','F','S'};
125 /* fetch the attributes of a file */
126 static inline ULONG get_file_attributes( const struct stat *st )
128 ULONG attr;
130 if (S_ISDIR(st->st_mode))
131 attr = FILE_ATTRIBUTE_DIRECTORY;
132 else
133 attr = FILE_ATTRIBUTE_ARCHIVE;
134 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
135 attr |= FILE_ATTRIBUTE_READONLY;
136 return attr;
139 /* get the stat info and file attributes for a file (by file descriptor) */
140 int fd_get_file_info( int fd, struct stat *st, ULONG *attr )
142 int ret;
144 *attr = 0;
145 ret = fstat( fd, st );
146 if (ret == -1) return ret;
147 *attr |= get_file_attributes( st );
148 return ret;
151 /* get the stat info and file attributes for a file (by name) */
152 int get_file_info( const char *path, struct stat *st, ULONG *attr )
154 int ret;
156 *attr = 0;
157 ret = lstat( path, st );
158 if (ret == -1) return ret;
159 if (S_ISLNK( st->st_mode ))
161 ret = stat( path, st );
162 if (ret == -1) return ret;
163 /* is a symbolic link and a directory, consider these "reparse points" */
164 if (S_ISDIR( st->st_mode )) *attr |= FILE_ATTRIBUTE_REPARSE_POINT;
166 *attr |= get_file_attributes( st );
167 return ret;
170 /**************************************************************************
171 * FILE_CreateFile (internal)
172 * Open a file.
174 * Parameter set fully identical with NtCreateFile
176 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
177 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
178 ULONG attributes, ULONG sharing, ULONG disposition,
179 ULONG options, PVOID ea_buffer, ULONG ea_length )
181 ANSI_STRING unix_name;
182 BOOL created = FALSE;
184 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
185 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
186 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
187 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
188 attributes, sharing, disposition, options, ea_buffer, ea_length );
190 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
192 if (alloc_size) FIXME( "alloc_size not supported\n" );
194 if (options & FILE_OPEN_BY_FILE_ID)
195 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
196 else
197 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
199 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
201 SERVER_START_REQ( open_file_object )
203 req->access = access;
204 req->attributes = attr->Attributes;
205 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
206 req->sharing = sharing;
207 req->options = options;
208 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
209 io->u.Status = wine_server_call( req );
210 *handle = wine_server_ptr_handle( reply->handle );
212 SERVER_END_REQ;
213 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
214 return io->u.Status;
217 if (io->u.Status == STATUS_NO_SUCH_FILE &&
218 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
220 created = TRUE;
221 io->u.Status = STATUS_SUCCESS;
224 if (io->u.Status == STATUS_SUCCESS)
226 static UNICODE_STRING empty_string;
227 OBJECT_ATTRIBUTES unix_attr = *attr;
228 data_size_t len;
229 struct object_attributes *objattr;
231 unix_attr.ObjectName = &empty_string; /* we send the unix name instead */
232 if ((io->u.Status = alloc_object_attributes( &unix_attr, &objattr, &len )))
234 RtlFreeAnsiString( &unix_name );
235 return io->u.Status;
238 SERVER_START_REQ( create_file )
240 req->access = access;
241 req->sharing = sharing;
242 req->create = disposition;
243 req->options = options;
244 req->attrs = attributes;
245 wine_server_add_data( req, objattr, len );
246 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
247 io->u.Status = wine_server_call( req );
248 *handle = wine_server_ptr_handle( reply->handle );
250 SERVER_END_REQ;
251 RtlFreeHeap( GetProcessHeap(), 0, objattr );
252 RtlFreeAnsiString( &unix_name );
254 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
256 if (io->u.Status == STATUS_SUCCESS)
258 if (created) io->Information = FILE_CREATED;
259 else switch(disposition)
261 case FILE_SUPERSEDE:
262 io->Information = FILE_SUPERSEDED;
263 break;
264 case FILE_CREATE:
265 io->Information = FILE_CREATED;
266 break;
267 case FILE_OPEN:
268 case FILE_OPEN_IF:
269 io->Information = FILE_OPENED;
270 break;
271 case FILE_OVERWRITE:
272 case FILE_OVERWRITE_IF:
273 io->Information = FILE_OVERWRITTEN;
274 break;
277 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
279 static int once;
280 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
283 return io->u.Status;
286 /**************************************************************************
287 * NtOpenFile [NTDLL.@]
288 * ZwOpenFile [NTDLL.@]
290 * Open a file.
292 * PARAMS
293 * handle [O] Variable that receives the file handle on return
294 * access [I] Access desired by the caller to the file
295 * attr [I] Structure describing the file to be opened
296 * io [O] Receives details about the result of the operation
297 * sharing [I] Type of shared access the caller requires
298 * options [I] Options for the file open
300 * RETURNS
301 * Success: 0. FileHandle and IoStatusBlock are updated.
302 * Failure: An NTSTATUS error code describing the error.
304 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
305 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
306 ULONG sharing, ULONG options )
308 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
309 sharing, FILE_OPEN, options, NULL, 0 );
312 /**************************************************************************
313 * NtCreateFile [NTDLL.@]
314 * ZwCreateFile [NTDLL.@]
316 * Either create a new file or directory, or open an existing file, device,
317 * directory or volume.
319 * PARAMS
320 * handle [O] Points to a variable which receives the file handle on return
321 * access [I] Desired access to the file
322 * attr [I] Structure describing the file
323 * io [O] Receives information about the operation on return
324 * alloc_size [I] Initial size of the file in bytes
325 * attributes [I] Attributes to create the file with
326 * sharing [I] Type of shared access the caller would like to the file
327 * disposition [I] Specifies what to do, depending on whether the file already exists
328 * options [I] Options for creating a new file
329 * ea_buffer [I] Pointer to an extended attributes buffer
330 * ea_length [I] Length of ea_buffer
332 * RETURNS
333 * Success: 0. handle and io are updated.
334 * Failure: An NTSTATUS error code describing the error.
336 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
337 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
338 ULONG attributes, ULONG sharing, ULONG disposition,
339 ULONG options, PVOID ea_buffer, ULONG ea_length )
341 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
342 sharing, disposition, options, ea_buffer, ea_length );
345 /***********************************************************************
346 * Asynchronous file I/O *
349 typedef NTSTATUS async_callback_t( void *user, IO_STATUS_BLOCK *io, NTSTATUS status );
351 struct async_fileio
353 async_callback_t *callback; /* must be the first field */
354 struct async_fileio *next;
355 HANDLE handle;
358 struct async_fileio_read
360 struct async_fileio io;
361 char* buffer;
362 unsigned int already;
363 unsigned int count;
364 BOOL avail_mode;
367 struct async_fileio_write
369 struct async_fileio io;
370 const char *buffer;
371 unsigned int already;
372 unsigned int count;
375 struct async_irp
377 struct async_fileio io;
378 HANDLE event; /* async event */
379 void *buffer; /* buffer for output */
380 ULONG size; /* size of buffer */
383 static struct async_fileio *fileio_freelist;
385 static void release_fileio( struct async_fileio *io )
387 for (;;)
389 struct async_fileio *next = fileio_freelist;
390 io->next = next;
391 if (interlocked_cmpxchg_ptr( (void **)&fileio_freelist, io, next ) == next) return;
395 static struct async_fileio *alloc_fileio( DWORD size, async_callback_t callback, HANDLE handle )
397 /* first free remaining previous fileinfos */
399 struct async_fileio *io = interlocked_xchg_ptr( (void **)&fileio_freelist, NULL );
401 while (io)
403 struct async_fileio *next = io->next;
404 RtlFreeHeap( GetProcessHeap(), 0, io );
405 io = next;
408 if ((io = RtlAllocateHeap( GetProcessHeap(), 0, size )))
410 io->callback = callback;
411 io->handle = handle;
413 return io;
416 static async_data_t server_async( HANDLE handle, struct async_fileio *user, HANDLE event,
417 PIO_APC_ROUTINE apc, void *apc_context, IO_STATUS_BLOCK *io )
419 async_data_t async;
420 async.handle = wine_server_obj_handle( handle );
421 async.user = wine_server_client_ptr( user );
422 async.iosb = wine_server_client_ptr( io );
423 async.event = wine_server_obj_handle( event );
424 async.apc = wine_server_client_ptr( apc );
425 async.apc_context = wine_server_client_ptr( apc_context );
426 return async;
429 /* callback for irp async I/O completion */
430 static NTSTATUS irp_completion( void *user, IO_STATUS_BLOCK *io, NTSTATUS status )
432 struct async_irp *async = user;
433 ULONG information = 0;
435 if (status == STATUS_ALERTED)
437 SERVER_START_REQ( get_async_result )
439 req->user_arg = wine_server_client_ptr( async );
440 wine_server_set_reply( req, async->buffer, async->size );
441 status = virtual_locked_server_call( req );
442 information = reply->size;
444 SERVER_END_REQ;
446 if (status != STATUS_PENDING)
448 io->u.Status = status;
449 io->Information = information;
450 release_fileio( &async->io );
452 return status;
455 /***********************************************************************
456 * FILE_GetNtStatus(void)
458 * Retrieve the Nt Status code from errno.
459 * Try to be consistent with FILE_SetDosError().
461 NTSTATUS FILE_GetNtStatus(void)
463 int err = errno;
465 TRACE( "errno = %d\n", errno );
466 switch (err)
468 case EAGAIN: return STATUS_SHARING_VIOLATION;
469 case EBADF: return STATUS_INVALID_HANDLE;
470 case EBUSY: return STATUS_DEVICE_BUSY;
471 case ENOSPC: return STATUS_DISK_FULL;
472 case EPERM:
473 case EROFS:
474 case EACCES: return STATUS_ACCESS_DENIED;
475 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
476 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
477 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
478 case EMFILE:
479 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
480 case EINVAL: return STATUS_INVALID_PARAMETER;
481 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
482 case EPIPE: return STATUS_PIPE_DISCONNECTED;
483 case EIO: return STATUS_DEVICE_NOT_READY;
484 #ifdef ENOMEDIUM
485 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
486 #endif
487 case ENXIO: return STATUS_NO_SUCH_DEVICE;
488 case ENOTTY:
489 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
490 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
491 case EFAULT: return STATUS_ACCESS_VIOLATION;
492 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
493 #ifdef ETIME /* Missing on FreeBSD */
494 case ETIME: return STATUS_IO_TIMEOUT;
495 #endif
496 case ENOEXEC: /* ?? */
497 case EEXIST: /* ?? */
498 default:
499 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
500 return STATUS_UNSUCCESSFUL;
504 /***********************************************************************
505 * FILE_AsyncReadService (INTERNAL)
507 static NTSTATUS FILE_AsyncReadService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
509 struct async_fileio_read *fileio = user;
510 int fd, needs_close, result;
512 switch (status)
514 case STATUS_ALERTED: /* got some new data */
515 /* check to see if the data is ready (non-blocking) */
516 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
517 &needs_close, NULL, NULL )))
518 break;
520 result = virtual_locked_read(fd, &fileio->buffer[fileio->already], fileio->count-fileio->already);
521 if (needs_close) close( fd );
523 if (result < 0)
525 if (errno == EAGAIN || errno == EINTR)
526 status = STATUS_PENDING;
527 else /* check to see if the transfer is complete */
528 status = FILE_GetNtStatus();
530 else if (result == 0)
532 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
534 else
536 fileio->already += result;
537 if (fileio->already >= fileio->count || fileio->avail_mode)
538 status = STATUS_SUCCESS;
539 else
540 status = STATUS_PENDING;
542 break;
544 case STATUS_TIMEOUT:
545 case STATUS_IO_TIMEOUT:
546 if (fileio->already) status = STATUS_SUCCESS;
547 break;
549 if (status != STATUS_PENDING)
551 iosb->u.Status = status;
552 iosb->Information = fileio->already;
553 release_fileio( &fileio->io );
555 return status;
558 /* do a read call through the server */
559 static NTSTATUS server_read_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
560 IO_STATUS_BLOCK *io, void *buffer, ULONG size,
561 LARGE_INTEGER *offset, ULONG *key )
563 struct async_irp *async;
564 NTSTATUS status;
565 HANDLE wait_handle;
566 ULONG options;
568 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
569 return STATUS_NO_MEMORY;
571 async->event = event;
572 async->buffer = buffer;
573 async->size = size;
575 SERVER_START_REQ( read )
577 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
578 req->pos = offset ? offset->QuadPart : 0;
579 wine_server_set_reply( req, buffer, size );
580 status = virtual_locked_server_call( req );
581 wait_handle = wine_server_ptr_handle( reply->wait );
582 options = reply->options;
583 if (wait_handle && status != STATUS_PENDING)
585 io->u.Status = status;
586 io->Information = wine_server_reply_size( reply );
589 SERVER_END_REQ;
591 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
593 if (wait_handle)
595 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
596 status = io->u.Status;
599 return status;
602 /* do a write call through the server */
603 static NTSTATUS server_write_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
604 IO_STATUS_BLOCK *io, const void *buffer, ULONG size,
605 LARGE_INTEGER *offset, ULONG *key )
607 struct async_irp *async;
608 NTSTATUS status;
609 HANDLE wait_handle;
610 ULONG options;
612 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
613 return STATUS_NO_MEMORY;
615 async->event = event;
616 async->buffer = NULL;
617 async->size = 0;
619 SERVER_START_REQ( write )
621 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
622 req->pos = offset ? offset->QuadPart : 0;
623 wine_server_add_data( req, buffer, size );
624 status = wine_server_call( req );
625 wait_handle = wine_server_ptr_handle( reply->wait );
626 options = reply->options;
627 if (wait_handle && status != STATUS_PENDING)
629 io->u.Status = status;
630 io->Information = reply->size;
633 SERVER_END_REQ;
635 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
637 if (wait_handle)
639 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
640 status = io->u.Status;
643 return status;
646 struct io_timeouts
648 int interval; /* max interval between two bytes */
649 int total; /* total timeout for the whole operation */
650 int end_time; /* absolute time of end of operation */
653 /* retrieve the I/O timeouts to use for a given handle */
654 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
655 struct io_timeouts *timeouts )
657 NTSTATUS status = STATUS_SUCCESS;
659 timeouts->interval = timeouts->total = -1;
661 switch(type)
663 case FD_TYPE_SERIAL:
665 /* GetCommTimeouts */
666 SERIAL_TIMEOUTS st;
667 IO_STATUS_BLOCK io;
669 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
670 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
671 if (status) break;
673 if (is_read)
675 if (st.ReadIntervalTimeout)
676 timeouts->interval = st.ReadIntervalTimeout;
678 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
680 timeouts->total = st.ReadTotalTimeoutConstant;
681 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
682 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
684 else if (st.ReadIntervalTimeout == MAXDWORD)
685 timeouts->interval = timeouts->total = 0;
687 else /* write */
689 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
691 timeouts->total = st.WriteTotalTimeoutConstant;
692 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
693 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
697 break;
698 case FD_TYPE_MAILSLOT:
699 if (is_read)
701 timeouts->interval = 0; /* return as soon as we got something */
702 SERVER_START_REQ( set_mailslot_info )
704 req->handle = wine_server_obj_handle( handle );
705 req->flags = 0;
706 if (!(status = wine_server_call( req )) &&
707 reply->read_timeout != TIMEOUT_INFINITE)
708 timeouts->total = reply->read_timeout / -10000;
710 SERVER_END_REQ;
712 break;
713 case FD_TYPE_SOCKET:
714 case FD_TYPE_CHAR:
715 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
716 break;
717 default:
718 break;
720 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
721 return STATUS_SUCCESS;
725 /* retrieve the timeout for the next wait, in milliseconds */
726 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
728 int ret = -1;
730 if (timeouts->total != -1)
732 ret = timeouts->end_time - NtGetTickCount();
733 if (ret < 0) ret = 0;
735 if (already && timeouts->interval != -1)
737 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
739 return ret;
743 /* retrieve the avail_mode flag for async reads */
744 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
746 NTSTATUS status = STATUS_SUCCESS;
748 switch(type)
750 case FD_TYPE_SERIAL:
752 /* GetCommTimeouts */
753 SERIAL_TIMEOUTS st;
754 IO_STATUS_BLOCK io;
756 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
757 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
758 if (status) break;
759 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
760 !st.ReadTotalTimeoutConstant &&
761 st.ReadIntervalTimeout == MAXDWORD);
763 break;
764 case FD_TYPE_MAILSLOT:
765 case FD_TYPE_SOCKET:
766 case FD_TYPE_CHAR:
767 *avail_mode = TRUE;
768 break;
769 default:
770 *avail_mode = FALSE;
771 break;
773 return status;
776 /* register an async I/O for a file read; helper for NtReadFile */
777 static NTSTATUS register_async_file_read( HANDLE handle, HANDLE event,
778 PIO_APC_ROUTINE apc, void *apc_user,
779 IO_STATUS_BLOCK *iosb, void *buffer,
780 ULONG already, ULONG length, BOOL avail_mode )
782 struct async_fileio_read *fileio;
783 NTSTATUS status;
785 if (!(fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), FILE_AsyncReadService, handle )))
786 return STATUS_NO_MEMORY;
788 fileio->already = already;
789 fileio->count = length;
790 fileio->buffer = buffer;
791 fileio->avail_mode = avail_mode;
793 SERVER_START_REQ( register_async )
795 req->type = ASYNC_TYPE_READ;
796 req->count = length;
797 req->async = server_async( handle, &fileio->io, event, apc, apc_user, iosb );
798 status = wine_server_call( req );
800 SERVER_END_REQ;
802 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
803 return status;
807 /******************************************************************************
808 * NtReadFile [NTDLL.@]
809 * ZwReadFile [NTDLL.@]
811 * Read from an open file handle.
813 * PARAMS
814 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
815 * Event [I] Event to signal upon completion (or NULL)
816 * ApcRoutine [I] Callback to call upon completion (or NULL)
817 * ApcContext [I] Context for ApcRoutine (or NULL)
818 * IoStatusBlock [O] Receives information about the operation on return
819 * Buffer [O] Destination for the data read
820 * Length [I] Size of Buffer
821 * ByteOffset [O] Destination for the new file pointer position (or NULL)
822 * Key [O] Function unknown (may be NULL)
824 * RETURNS
825 * Success: 0. IoStatusBlock is updated, and the Information member contains
826 * The number of bytes read.
827 * Failure: An NTSTATUS error code describing the error.
829 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
830 PIO_APC_ROUTINE apc, void* apc_user,
831 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
832 PLARGE_INTEGER offset, PULONG key)
834 int result, unix_handle, needs_close;
835 unsigned int options;
836 struct io_timeouts timeouts;
837 NTSTATUS status;
838 ULONG total = 0;
839 enum server_fd_type type;
840 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
841 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
843 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
844 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
846 if (!io_status) return STATUS_ACCESS_VIOLATION;
848 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
849 &needs_close, &type, &options );
850 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
852 if (!virtual_check_buffer_for_write( buffer, length )) return STATUS_ACCESS_VIOLATION;
854 if (status == STATUS_BAD_DEVICE_TYPE)
855 return server_read_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
857 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
859 if (type == FD_TYPE_FILE)
861 if (async_read && (!offset || offset->QuadPart < 0))
863 status = STATUS_INVALID_PARAMETER;
864 goto done;
867 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
869 /* async I/O doesn't make sense on regular files */
870 while ((result = virtual_locked_pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
872 if (errno != EINTR)
874 status = FILE_GetNtStatus();
875 goto done;
878 if (!async_read)
879 /* update file pointer position */
880 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
882 total = result;
883 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
884 goto done;
887 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
889 if (async_read && (!offset || offset->QuadPart < 0))
891 status = STATUS_INVALID_PARAMETER;
892 goto done;
896 if (type == FD_TYPE_SERIAL && async_read && length)
898 /* an asynchronous serial port read with a read interval timeout needs to
899 skip the synchronous read to make sure that the server starts the read
900 interval timer after the first read */
901 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts ))) goto err;
902 if (timeouts.interval)
904 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
905 buffer, total, length, FALSE );
906 goto err;
910 for (;;)
912 if ((result = virtual_locked_read( unix_handle, (char *)buffer + total, length - total )) >= 0)
914 total += result;
915 if (!result || total == length)
917 if (total)
919 status = STATUS_SUCCESS;
920 goto done;
922 switch (type)
924 case FD_TYPE_FILE:
925 case FD_TYPE_CHAR:
926 case FD_TYPE_DEVICE:
927 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
928 goto done;
929 case FD_TYPE_SERIAL:
930 if (!length)
932 status = STATUS_SUCCESS;
933 goto done;
935 break;
936 default:
937 status = STATUS_PIPE_BROKEN;
938 goto err;
941 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
943 else if (errno != EAGAIN)
945 if (errno == EINTR) continue;
946 if (!total) status = FILE_GetNtStatus();
947 goto err;
950 if (async_read)
952 BOOL avail_mode;
954 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
955 goto err;
956 if (total && avail_mode)
958 status = STATUS_SUCCESS;
959 goto done;
961 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
962 buffer, total, length, avail_mode );
963 goto err;
965 else /* synchronous read, wait for the fd to become ready */
967 struct pollfd pfd;
968 int ret, timeout;
970 if (!timeout_init_done)
972 timeout_init_done = TRUE;
973 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
974 goto err;
975 if (hEvent) NtResetEvent( hEvent, NULL );
977 timeout = get_next_io_timeout( &timeouts, total );
979 pfd.fd = unix_handle;
980 pfd.events = POLLIN;
982 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
984 if (total) /* return with what we got so far */
985 status = STATUS_SUCCESS;
986 else
987 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
988 goto done;
990 if (ret == -1 && errno != EINTR)
992 status = FILE_GetNtStatus();
993 goto done;
995 /* will now restart the read */
999 done:
1000 send_completion = cvalue != 0;
1002 err:
1003 if (needs_close) close( unix_handle );
1004 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
1006 io_status->u.Status = status;
1007 io_status->Information = total;
1008 TRACE("= SUCCESS (%u)\n", total);
1009 if (hEvent) NtSetEvent( hEvent, NULL );
1010 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1011 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1013 else
1015 TRACE("= 0x%08x\n", status);
1016 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1019 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1021 return status;
1025 /******************************************************************************
1026 * NtReadFileScatter [NTDLL.@]
1027 * ZwReadFileScatter [NTDLL.@]
1029 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1030 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1031 ULONG length, PLARGE_INTEGER offset, PULONG key )
1033 int result, unix_handle, needs_close;
1034 unsigned int options;
1035 NTSTATUS status;
1036 ULONG pos = 0, total = 0;
1037 enum server_fd_type type;
1038 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1039 BOOL send_completion = FALSE;
1041 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1042 file, event, apc, apc_user, io_status, segments, length, offset, key);
1044 if (!io_status) return STATUS_ACCESS_VIOLATION;
1046 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1047 &needs_close, &type, &options );
1048 if (status) return status;
1050 if ((type != FD_TYPE_FILE) ||
1051 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1052 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1054 status = STATUS_INVALID_PARAMETER;
1055 goto error;
1058 while (length)
1060 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1061 result = pread( unix_handle, (char *)segments->Buffer + pos,
1062 min( length - pos, page_size - pos ), offset->QuadPart + total );
1063 else
1064 result = read( unix_handle, (char *)segments->Buffer + pos, min( length - pos, page_size - pos ) );
1066 if (result == -1)
1068 if (errno == EINTR) continue;
1069 status = FILE_GetNtStatus();
1070 break;
1072 if (!result) break;
1073 total += result;
1074 length -= result;
1075 if ((pos += result) == page_size)
1077 pos = 0;
1078 segments++;
1082 if (total == 0) status = STATUS_END_OF_FILE;
1084 send_completion = cvalue != 0;
1086 if (needs_close) close( unix_handle );
1088 io_status->u.Status = status;
1089 io_status->Information = total;
1090 TRACE("= 0x%08x (%u)\n", status, total);
1091 if (event) NtSetEvent( event, NULL );
1092 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1093 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1094 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1096 return STATUS_PENDING;
1098 error:
1099 if (needs_close) close( unix_handle );
1101 TRACE("= 0x%08x\n", status);
1102 if (event) NtResetEvent( event, NULL );
1104 return status;
1108 /***********************************************************************
1109 * FILE_AsyncWriteService (INTERNAL)
1111 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1113 struct async_fileio_write *fileio = user;
1114 int result, fd, needs_close;
1115 enum server_fd_type type;
1117 switch (status)
1119 case STATUS_ALERTED:
1120 /* write some data (non-blocking) */
1121 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1122 &needs_close, &type, NULL )))
1123 break;
1125 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1126 result = send( fd, fileio->buffer, 0, 0 );
1127 else
1128 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1130 if (needs_close) close( fd );
1132 if (result < 0)
1134 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1135 else status = FILE_GetNtStatus();
1137 else
1139 fileio->already += result;
1140 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1142 break;
1144 case STATUS_TIMEOUT:
1145 case STATUS_IO_TIMEOUT:
1146 if (fileio->already) status = STATUS_SUCCESS;
1147 break;
1149 if (status != STATUS_PENDING)
1151 iosb->u.Status = status;
1152 iosb->Information = fileio->already;
1153 release_fileio( &fileio->io );
1155 return status;
1158 static NTSTATUS set_pending_write( HANDLE device )
1160 NTSTATUS status;
1162 SERVER_START_REQ( set_serial_info )
1164 req->handle = wine_server_obj_handle( device );
1165 req->flags = SERIALINFO_PENDING_WRITE;
1166 status = wine_server_call( req );
1168 SERVER_END_REQ;
1169 return status;
1172 /******************************************************************************
1173 * NtWriteFile [NTDLL.@]
1174 * ZwWriteFile [NTDLL.@]
1176 * Write to an open file handle.
1178 * PARAMS
1179 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1180 * Event [I] Event to signal upon completion (or NULL)
1181 * ApcRoutine [I] Callback to call upon completion (or NULL)
1182 * ApcContext [I] Context for ApcRoutine (or NULL)
1183 * IoStatusBlock [O] Receives information about the operation on return
1184 * Buffer [I] Source for the data to write
1185 * Length [I] Size of Buffer
1186 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1187 * Key [O] Function unknown (may be NULL)
1189 * RETURNS
1190 * Success: 0. IoStatusBlock is updated, and the Information member contains
1191 * The number of bytes written.
1192 * Failure: An NTSTATUS error code describing the error.
1194 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1195 PIO_APC_ROUTINE apc, void* apc_user,
1196 PIO_STATUS_BLOCK io_status,
1197 const void* buffer, ULONG length,
1198 PLARGE_INTEGER offset, PULONG key)
1200 int result, unix_handle, needs_close;
1201 unsigned int options;
1202 struct io_timeouts timeouts;
1203 NTSTATUS status;
1204 ULONG total = 0;
1205 enum server_fd_type type;
1206 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1207 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1208 LARGE_INTEGER offset_eof;
1210 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1211 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1213 if (!io_status) return STATUS_ACCESS_VIOLATION;
1215 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1216 &needs_close, &type, &options );
1217 if (status == STATUS_ACCESS_DENIED)
1219 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1220 &needs_close, &type, &options );
1221 append_write = TRUE;
1223 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
1225 if (!virtual_check_buffer_for_read( buffer, length ))
1227 status = STATUS_INVALID_USER_BUFFER;
1228 goto done;
1231 if (status == STATUS_BAD_DEVICE_TYPE)
1232 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1234 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1236 if (type == FD_TYPE_FILE)
1238 if (async_write &&
1239 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1241 status = STATUS_INVALID_PARAMETER;
1242 goto done;
1245 if (append_write)
1247 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1248 offset = &offset_eof;
1251 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1253 off_t off = offset->QuadPart;
1255 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1257 struct stat st;
1259 if (fstat( unix_handle, &st ) == -1)
1261 status = FILE_GetNtStatus();
1262 goto done;
1264 off = st.st_size;
1266 else if (offset->QuadPart < 0)
1268 status = STATUS_INVALID_PARAMETER;
1269 goto done;
1272 /* async I/O doesn't make sense on regular files */
1273 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1275 if (errno != EINTR)
1277 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1278 else status = FILE_GetNtStatus();
1279 goto done;
1283 if (!async_write)
1284 /* update file pointer position */
1285 lseek( unix_handle, off + result, SEEK_SET );
1287 total = result;
1288 status = STATUS_SUCCESS;
1289 goto done;
1292 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1294 if (async_write &&
1295 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1297 status = STATUS_INVALID_PARAMETER;
1298 goto done;
1302 for (;;)
1304 /* zero-length writes on sockets may not work with plain write(2) */
1305 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_SOCKET))
1306 result = send( unix_handle, buffer, 0, 0 );
1307 else
1308 result = write( unix_handle, (const char *)buffer + total, length - total );
1310 if (result >= 0)
1312 total += result;
1313 if (total == length)
1315 status = STATUS_SUCCESS;
1316 goto done;
1318 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1320 else if (errno != EAGAIN)
1322 if (errno == EINTR) continue;
1323 if (!total)
1325 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1326 else status = FILE_GetNtStatus();
1328 goto err;
1331 if (async_write)
1333 struct async_fileio_write *fileio;
1335 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), FILE_AsyncWriteService, hFile );
1336 if (!fileio)
1338 status = STATUS_NO_MEMORY;
1339 goto err;
1341 fileio->already = total;
1342 fileio->count = length;
1343 fileio->buffer = buffer;
1345 SERVER_START_REQ( register_async )
1347 req->type = ASYNC_TYPE_WRITE;
1348 req->count = length;
1349 req->async = server_async( hFile, &fileio->io, hEvent, apc, apc_user, io_status );
1350 status = wine_server_call( req );
1352 SERVER_END_REQ;
1354 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1355 goto err;
1357 else /* synchronous write, wait for the fd to become ready */
1359 struct pollfd pfd;
1360 int ret, timeout;
1362 if (!timeout_init_done)
1364 timeout_init_done = TRUE;
1365 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1366 goto err;
1367 if (hEvent) NtResetEvent( hEvent, NULL );
1369 timeout = get_next_io_timeout( &timeouts, total );
1371 pfd.fd = unix_handle;
1372 pfd.events = POLLOUT;
1374 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1376 /* return with what we got so far */
1377 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1378 goto done;
1380 if (ret == -1 && errno != EINTR)
1382 status = FILE_GetNtStatus();
1383 goto done;
1385 /* will now restart the write */
1389 done:
1390 send_completion = cvalue != 0;
1392 err:
1393 if (needs_close) close( unix_handle );
1395 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1396 set_pending_write( hFile );
1398 if (status == STATUS_SUCCESS)
1400 io_status->u.Status = status;
1401 io_status->Information = total;
1402 TRACE("= SUCCESS (%u)\n", total);
1403 if (hEvent) NtSetEvent( hEvent, NULL );
1404 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1405 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1407 else
1409 TRACE("= 0x%08x\n", status);
1410 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1413 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1415 return status;
1419 /******************************************************************************
1420 * NtWriteFileGather [NTDLL.@]
1421 * ZwWriteFileGather [NTDLL.@]
1423 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1424 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1425 ULONG length, PLARGE_INTEGER offset, PULONG key )
1427 int result, unix_handle, needs_close;
1428 unsigned int options;
1429 NTSTATUS status;
1430 ULONG pos = 0, total = 0;
1431 enum server_fd_type type;
1432 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1433 BOOL send_completion = FALSE;
1435 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1436 file, event, apc, apc_user, io_status, segments, length, offset, key);
1438 if (length % page_size) return STATUS_INVALID_PARAMETER;
1439 if (!io_status) return STATUS_ACCESS_VIOLATION;
1441 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1442 &needs_close, &type, &options );
1443 if (status) return status;
1445 if ((type != FD_TYPE_FILE) ||
1446 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1447 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1449 status = STATUS_INVALID_PARAMETER;
1450 goto error;
1453 while (length)
1455 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1456 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1457 page_size - pos, offset->QuadPart + total );
1458 else
1459 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1461 if (result == -1)
1463 if (errno == EINTR) continue;
1464 if (errno == EFAULT)
1466 status = STATUS_INVALID_USER_BUFFER;
1467 goto error;
1469 status = FILE_GetNtStatus();
1470 break;
1472 if (!result)
1474 status = STATUS_DISK_FULL;
1475 break;
1477 total += result;
1478 length -= result;
1479 if ((pos += result) == page_size)
1481 pos = 0;
1482 segments++;
1486 send_completion = cvalue != 0;
1488 error:
1489 if (needs_close) close( unix_handle );
1490 if (status == STATUS_SUCCESS)
1492 io_status->u.Status = status;
1493 io_status->Information = total;
1494 TRACE("= SUCCESS (%u)\n", total);
1495 if (event) NtSetEvent( event, NULL );
1496 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1497 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1499 else
1501 TRACE("= 0x%08x\n", status);
1502 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1505 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1507 return status;
1511 /* do an ioctl call through the server */
1512 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1513 PIO_APC_ROUTINE apc, PVOID apc_context,
1514 IO_STATUS_BLOCK *io, ULONG code,
1515 const void *in_buffer, ULONG in_size,
1516 PVOID out_buffer, ULONG out_size )
1518 struct async_irp *async;
1519 NTSTATUS status;
1520 HANDLE wait_handle;
1521 ULONG options;
1523 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
1524 return STATUS_NO_MEMORY;
1525 async->event = event;
1526 async->buffer = out_buffer;
1527 async->size = out_size;
1529 SERVER_START_REQ( ioctl )
1531 req->code = code;
1532 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
1533 wine_server_add_data( req, in_buffer, in_size );
1534 if ((code & 3) != METHOD_BUFFERED)
1535 wine_server_add_data( req, out_buffer, out_size );
1536 wine_server_set_reply( req, out_buffer, out_size );
1537 status = virtual_locked_server_call( req );
1538 wait_handle = wine_server_ptr_handle( reply->wait );
1539 options = reply->options;
1540 if (wait_handle && status != STATUS_PENDING)
1542 io->u.Status = status;
1543 io->Information = wine_server_reply_size( reply );
1546 SERVER_END_REQ;
1548 if (status == STATUS_NOT_SUPPORTED)
1549 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1550 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1552 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1554 if (wait_handle)
1556 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1557 status = io->u.Status;
1560 return status;
1563 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1564 * server */
1565 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1566 ULONG in_size)
1568 #ifdef VALGRIND_MAKE_MEM_DEFINED
1569 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1570 do { \
1571 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1572 if ((size) >= FIELD_OFFSET(t, f2)) \
1573 VALGRIND_MAKE_MEM_DEFINED( \
1574 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1575 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1576 } while (0)
1578 switch (code)
1580 case FSCTL_PIPE_WAIT:
1581 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1582 break;
1584 #endif
1588 /**************************************************************************
1589 * NtDeviceIoControlFile [NTDLL.@]
1590 * ZwDeviceIoControlFile [NTDLL.@]
1592 * Perform an I/O control operation on an open file handle.
1594 * PARAMS
1595 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1596 * event [I] Event to signal upon completion (or NULL)
1597 * apc [I] Callback to call upon completion (or NULL)
1598 * apc_context [I] Context for ApcRoutine (or NULL)
1599 * io [O] Receives information about the operation on return
1600 * code [I] Control code for the operation to perform
1601 * in_buffer [I] Source for any input data required (or NULL)
1602 * in_size [I] Size of InputBuffer
1603 * out_buffer [O] Source for any output data returned (or NULL)
1604 * out_size [I] Size of OutputBuffer
1606 * RETURNS
1607 * Success: 0. IoStatusBlock is updated.
1608 * Failure: An NTSTATUS error code describing the error.
1610 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1611 PIO_APC_ROUTINE apc, PVOID apc_context,
1612 PIO_STATUS_BLOCK io, ULONG code,
1613 PVOID in_buffer, ULONG in_size,
1614 PVOID out_buffer, ULONG out_size)
1616 ULONG device = (code >> 16);
1617 NTSTATUS status = STATUS_NOT_SUPPORTED;
1619 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1620 handle, event, apc, apc_context, io, code,
1621 in_buffer, in_size, out_buffer, out_size);
1623 switch(device)
1625 case FILE_DEVICE_DISK:
1626 case FILE_DEVICE_CD_ROM:
1627 case FILE_DEVICE_DVD:
1628 case FILE_DEVICE_CONTROLLER:
1629 case FILE_DEVICE_MASS_STORAGE:
1630 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1631 in_buffer, in_size, out_buffer, out_size);
1632 break;
1633 case FILE_DEVICE_SERIAL_PORT:
1634 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1635 in_buffer, in_size, out_buffer, out_size);
1636 break;
1637 case FILE_DEVICE_TAPE:
1638 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1639 in_buffer, in_size, out_buffer, out_size);
1640 break;
1643 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1644 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1645 in_buffer, in_size, out_buffer, out_size );
1647 if (status != STATUS_PENDING) io->u.Status = status;
1648 return status;
1652 /**************************************************************************
1653 * NtFsControlFile [NTDLL.@]
1654 * ZwFsControlFile [NTDLL.@]
1656 * Perform a file system control operation on an open file handle.
1658 * PARAMS
1659 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1660 * event [I] Event to signal upon completion (or NULL)
1661 * apc [I] Callback to call upon completion (or NULL)
1662 * apc_context [I] Context for ApcRoutine (or NULL)
1663 * io [O] Receives information about the operation on return
1664 * code [I] Control code for the operation to perform
1665 * in_buffer [I] Source for any input data required (or NULL)
1666 * in_size [I] Size of InputBuffer
1667 * out_buffer [O] Source for any output data returned (or NULL)
1668 * out_size [I] Size of OutputBuffer
1670 * RETURNS
1671 * Success: 0. IoStatusBlock is updated.
1672 * Failure: An NTSTATUS error code describing the error.
1674 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1675 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1676 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1678 NTSTATUS status;
1680 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1681 handle, event, apc, apc_context, io, code,
1682 in_buffer, in_size, out_buffer, out_size);
1684 if (!io) return STATUS_INVALID_PARAMETER;
1686 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1688 switch(code)
1690 case FSCTL_DISMOUNT_VOLUME:
1691 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1692 in_buffer, in_size, out_buffer, out_size );
1693 if (!status) status = DIR_unmount_device( handle );
1694 return status;
1696 case FSCTL_PIPE_DISCONNECT:
1697 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1698 in_buffer, in_size, out_buffer, out_size );
1699 if (!status)
1701 int fd = server_remove_fd_from_cache( handle );
1702 if (fd != -1) close( fd );
1704 return status;
1706 case FSCTL_PIPE_IMPERSONATE:
1707 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1708 status = RtlImpersonateSelf( SecurityImpersonation );
1709 break;
1711 case FSCTL_IS_VOLUME_MOUNTED:
1712 case FSCTL_LOCK_VOLUME:
1713 case FSCTL_UNLOCK_VOLUME:
1714 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1715 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1716 status = STATUS_SUCCESS;
1717 break;
1719 case FSCTL_GET_RETRIEVAL_POINTERS:
1721 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1723 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1725 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1727 buffer->ExtentCount = 1;
1728 buffer->StartingVcn.QuadPart = 1;
1729 buffer->Extents[0].NextVcn.QuadPart = 0;
1730 buffer->Extents[0].Lcn.QuadPart = 0;
1731 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1732 status = STATUS_SUCCESS;
1734 else
1736 io->Information = 0;
1737 status = STATUS_BUFFER_TOO_SMALL;
1739 break;
1741 case FSCTL_SET_SPARSE:
1742 TRACE("FSCTL_SET_SPARSE: Ignoring request\n");
1743 io->Information = 0;
1744 status = STATUS_SUCCESS;
1745 break;
1746 default:
1747 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1748 in_buffer, in_size, out_buffer, out_size );
1751 if (status != STATUS_PENDING) io->u.Status = status;
1752 return status;
1756 struct read_changes_fileio
1758 struct async_fileio io;
1759 void *buffer;
1760 ULONG buffer_size;
1761 ULONG data_size;
1762 char data[1];
1765 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1767 struct read_changes_fileio *fileio = user;
1768 int size = 0;
1770 if (status == STATUS_ALERTED)
1772 SERVER_START_REQ( read_change )
1774 req->handle = wine_server_obj_handle( fileio->io.handle );
1775 wine_server_set_reply( req, fileio->data, fileio->data_size );
1776 status = wine_server_call( req );
1777 size = wine_server_reply_size( reply );
1779 SERVER_END_REQ;
1781 if (status == STATUS_SUCCESS && fileio->buffer)
1783 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1784 int i, left = fileio->buffer_size;
1785 DWORD *last_entry_offset = NULL;
1786 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1788 while (size && left >= sizeof(*pfni))
1790 /* convert to an NT style path */
1791 for (i = 0; i < event->len; i++)
1792 if (event->name[i] == '/') event->name[i] = '\\';
1794 pfni->Action = event->action;
1795 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1796 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1797 last_entry_offset = &pfni->NextEntryOffset;
1799 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1801 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1802 pfni->FileNameLength *= sizeof(WCHAR);
1803 pfni->NextEntryOffset = i;
1804 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1805 left -= i;
1807 i = (offsetof(struct filesystem_event, name[event->len])
1808 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1809 event = (struct filesystem_event*)((char*)event + i);
1810 size -= i;
1813 if (size)
1815 status = STATUS_NOTIFY_ENUM_DIR;
1816 size = 0;
1818 else
1820 if (last_entry_offset) *last_entry_offset = 0;
1821 size = fileio->buffer_size - left;
1824 else
1826 status = STATUS_NOTIFY_ENUM_DIR;
1827 size = 0;
1831 if (status != STATUS_PENDING)
1833 iosb->u.Status = status;
1834 iosb->Information = size;
1835 release_fileio( &fileio->io );
1837 return status;
1840 #define FILE_NOTIFY_ALL ( \
1841 FILE_NOTIFY_CHANGE_FILE_NAME | \
1842 FILE_NOTIFY_CHANGE_DIR_NAME | \
1843 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1844 FILE_NOTIFY_CHANGE_SIZE | \
1845 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1846 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1847 FILE_NOTIFY_CHANGE_CREATION | \
1848 FILE_NOTIFY_CHANGE_SECURITY )
1850 /******************************************************************************
1851 * NtNotifyChangeDirectoryFile [NTDLL.@]
1853 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1854 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1855 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1857 struct read_changes_fileio *fileio;
1858 NTSTATUS status;
1859 ULONG size = max( 4096, buffer_size );
1861 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1862 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1864 if (!iosb) return STATUS_ACCESS_VIOLATION;
1865 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1867 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1868 read_changes_apc, handle );
1869 if (!fileio) return STATUS_NO_MEMORY;
1871 fileio->buffer = buffer;
1872 fileio->buffer_size = buffer_size;
1873 fileio->data_size = size;
1875 SERVER_START_REQ( read_directory_changes )
1877 req->filter = filter;
1878 req->want_data = (buffer != NULL);
1879 req->subtree = subtree;
1880 req->async = server_async( handle, &fileio->io, event, apc, apc_context, iosb );
1881 status = wine_server_call( req );
1883 SERVER_END_REQ;
1885 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1886 return status;
1889 /******************************************************************************
1890 * NtSetVolumeInformationFile [NTDLL.@]
1891 * ZwSetVolumeInformationFile [NTDLL.@]
1893 * Set volume information for an open file handle.
1895 * PARAMS
1896 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1897 * IoStatusBlock [O] Receives information about the operation on return
1898 * FsInformation [I] Source for volume information
1899 * Length [I] Size of FsInformation
1900 * FsInformationClass [I] Type of volume information to set
1902 * RETURNS
1903 * Success: 0. IoStatusBlock is updated.
1904 * Failure: An NTSTATUS error code describing the error.
1906 NTSTATUS WINAPI NtSetVolumeInformationFile(
1907 IN HANDLE FileHandle,
1908 PIO_STATUS_BLOCK IoStatusBlock,
1909 PVOID FsInformation,
1910 ULONG Length,
1911 FS_INFORMATION_CLASS FsInformationClass)
1913 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1914 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1915 return 0;
1918 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1919 static int futimens( int fd, const struct timespec spec[2] )
1921 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1923 #define HAVE_FUTIMENS
1924 #endif /* __ANDROID__ */
1926 #ifndef UTIME_OMIT
1927 #define UTIME_OMIT ((1 << 30) - 2)
1928 #endif
1930 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1932 NTSTATUS status = STATUS_SUCCESS;
1934 #ifdef HAVE_FUTIMENS
1935 struct timespec tv[2];
1937 tv[0].tv_sec = tv[1].tv_sec = 0;
1938 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1939 if (atime->QuadPart)
1941 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1942 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1944 if (mtime->QuadPart)
1946 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1947 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1949 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1951 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1952 struct timeval tv[2];
1953 struct stat st;
1955 if (!atime->QuadPart || !mtime->QuadPart)
1958 tv[0].tv_sec = tv[0].tv_usec = 0;
1959 tv[1].tv_sec = tv[1].tv_usec = 0;
1960 if (!fstat( fd, &st ))
1962 tv[0].tv_sec = st.st_atime;
1963 tv[1].tv_sec = st.st_mtime;
1964 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1965 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1966 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1967 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1968 #endif
1969 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1970 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1971 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1972 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1973 #endif
1976 if (atime->QuadPart)
1978 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1979 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1981 if (mtime->QuadPart)
1983 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1984 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1986 #ifdef HAVE_FUTIMES
1987 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1988 #elif defined(HAVE_FUTIMESAT)
1989 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1990 #endif
1992 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1993 FIXME( "setting file times not supported\n" );
1994 status = STATUS_NOT_IMPLEMENTED;
1995 #endif
1996 return status;
1999 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
2000 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
2002 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
2003 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
2004 RtlSecondsSince1970ToTime( st->st_atime, atime );
2005 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2006 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
2007 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2008 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
2009 #endif
2010 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2011 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2012 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2013 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2014 #endif
2015 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2016 atime->QuadPart += st->st_atim.tv_nsec / 100;
2017 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2018 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2019 #endif
2020 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2021 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2022 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2023 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2024 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2025 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2026 #endif
2027 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2028 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2029 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2030 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2031 #endif
2032 #else
2033 *creation = *mtime;
2034 #endif
2037 /* fill in the file information that depends on the stat and attribute info */
2038 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2039 FILE_INFORMATION_CLASS class )
2041 switch (class)
2043 case FileBasicInformation:
2045 FILE_BASIC_INFORMATION *info = ptr;
2047 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2048 &info->LastAccessTime, &info->CreationTime );
2049 info->FileAttributes = attr;
2051 break;
2052 case FileStandardInformation:
2054 FILE_STANDARD_INFORMATION *info = ptr;
2056 if ((info->Directory = S_ISDIR(st->st_mode)))
2058 info->AllocationSize.QuadPart = 0;
2059 info->EndOfFile.QuadPart = 0;
2060 info->NumberOfLinks = 1;
2062 else
2064 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2065 info->EndOfFile.QuadPart = st->st_size;
2066 info->NumberOfLinks = st->st_nlink;
2069 break;
2070 case FileInternalInformation:
2072 FILE_INTERNAL_INFORMATION *info = ptr;
2073 info->IndexNumber.QuadPart = st->st_ino;
2075 break;
2076 case FileEndOfFileInformation:
2078 FILE_END_OF_FILE_INFORMATION *info = ptr;
2079 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2081 break;
2082 case FileAllInformation:
2084 FILE_ALL_INFORMATION *info = ptr;
2085 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2086 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2087 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2089 break;
2090 /* all directory structures start with the FileDirectoryInformation layout */
2091 case FileBothDirectoryInformation:
2092 case FileFullDirectoryInformation:
2093 case FileDirectoryInformation:
2095 FILE_DIRECTORY_INFORMATION *info = ptr;
2097 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2098 &info->LastAccessTime, &info->CreationTime );
2099 if (S_ISDIR(st->st_mode))
2101 info->AllocationSize.QuadPart = 0;
2102 info->EndOfFile.QuadPart = 0;
2104 else
2106 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2107 info->EndOfFile.QuadPart = st->st_size;
2109 info->FileAttributes = attr;
2111 break;
2112 case FileIdFullDirectoryInformation:
2114 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2115 info->FileId.QuadPart = st->st_ino;
2116 fill_file_info( st, attr, info, FileDirectoryInformation );
2118 break;
2119 case FileIdBothDirectoryInformation:
2121 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2122 info->FileId.QuadPart = st->st_ino;
2123 fill_file_info( st, attr, info, FileDirectoryInformation );
2125 break;
2126 case FileIdGlobalTxDirectoryInformation:
2128 FILE_ID_GLOBAL_TX_DIR_INFORMATION *info = ptr;
2129 info->FileId.QuadPart = st->st_ino;
2130 fill_file_info( st, attr, info, FileDirectoryInformation );
2132 break;
2134 default:
2135 return STATUS_INVALID_INFO_CLASS;
2137 return STATUS_SUCCESS;
2140 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2142 data_size_t size = 1024;
2143 NTSTATUS ret;
2144 char *name;
2146 for (;;)
2148 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2149 if (!name) return STATUS_NO_MEMORY;
2150 unix_name->MaximumLength = size + 1;
2152 SERVER_START_REQ( get_handle_unix_name )
2154 req->handle = wine_server_obj_handle( handle );
2155 wine_server_set_reply( req, name, size );
2156 ret = wine_server_call( req );
2157 size = reply->name_len;
2159 SERVER_END_REQ;
2161 if (!ret)
2163 name[size] = 0;
2164 unix_name->Buffer = name;
2165 unix_name->Length = size;
2166 break;
2168 RtlFreeHeap( GetProcessHeap(), 0, name );
2169 if (ret != STATUS_BUFFER_OVERFLOW) break;
2171 return ret;
2174 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2176 UNICODE_STRING nt_name;
2177 NTSTATUS status;
2179 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2181 const WCHAR *ptr = nt_name.Buffer;
2182 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2184 /* Skip the volume mount point. */
2185 while (ptr != end && *ptr == '\\') ++ptr;
2186 while (ptr != end && *ptr != '\\') ++ptr;
2187 while (ptr != end && *ptr == '\\') ++ptr;
2188 while (ptr != end && *ptr != '\\') ++ptr;
2190 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2191 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2192 else *name_len = info->FileNameLength;
2194 memcpy( info->FileName, ptr, *name_len );
2195 RtlFreeUnicodeString( &nt_name );
2198 return status;
2201 static NTSTATUS server_get_file_info( HANDLE handle, IO_STATUS_BLOCK *io, void *buffer,
2202 ULONG length, FILE_INFORMATION_CLASS info_class )
2204 SERVER_START_REQ( get_file_info )
2206 req->handle = wine_server_obj_handle( handle );
2207 req->info_class = info_class;
2208 wine_server_set_reply( req, buffer, length );
2209 io->u.Status = wine_server_call( req );
2210 io->Information = wine_server_reply_size( reply );
2212 SERVER_END_REQ;
2213 return io->u.Status;
2217 /******************************************************************************
2218 * NtQueryInformationFile [NTDLL.@]
2219 * ZwQueryInformationFile [NTDLL.@]
2221 * Get information about an open file handle.
2223 * PARAMS
2224 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2225 * io [O] Receives information about the operation on return
2226 * ptr [O] Destination for file information
2227 * len [I] Size of FileInformation
2228 * class [I] Type of file information to get
2230 * RETURNS
2231 * Success: 0. IoStatusBlock and FileInformation are updated.
2232 * Failure: An NTSTATUS error code describing the error.
2234 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2235 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2237 static const size_t info_sizes[] =
2240 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2241 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2242 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2243 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2244 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2245 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2246 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2247 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2248 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2249 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2250 0, /* FileLinkInformation */
2251 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2252 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2253 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2254 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2255 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2256 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2257 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2258 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2259 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2260 0, /* FileAlternateNameInformation */
2261 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2262 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2263 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2264 0, /* FilePipeRemoteInformation */
2265 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2266 0, /* FileMailslotSetInformation */
2267 0, /* FileCompressionInformation */
2268 0, /* FileObjectIdInformation */
2269 0, /* FileCompletionInformation */
2270 0, /* FileMoveClusterInformation */
2271 0, /* FileQuotaInformation */
2272 0, /* FileReparsePointInformation */
2273 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2274 0, /* FileAttributeTagInformation */
2275 0, /* FileTrackingInformation */
2276 0, /* FileIdBothDirectoryInformation */
2277 0, /* FileIdFullDirectoryInformation */
2278 0, /* FileValidDataLengthInformation */
2279 0, /* FileShortNameInformation */
2280 0, /* FileIoCompletionNotificationInformation, */
2281 0, /* FileIoStatusBlockRangeInformation */
2282 0, /* FileIoPriorityHintInformation */
2283 0, /* FileSfioReserveInformation */
2284 0, /* FileSfioVolumeInformation */
2285 0, /* FileHardLinkInformation */
2286 0, /* FileProcessIdsUsingFileInformation */
2287 0, /* FileNormalizedNameInformation */
2288 0, /* FileNetworkPhysicalNameInformation */
2289 0, /* FileIdGlobalTxDirectoryInformation */
2290 0, /* FileIsRemoteDeviceInformation */
2291 0, /* FileAttributeCacheInformation */
2292 0, /* FileNumaNodeInformation */
2293 0, /* FileStandardLinkInformation */
2294 0, /* FileRemoteProtocolInformation */
2295 0, /* FileRenameInformationBypassAccessCheck */
2296 0, /* FileLinkInformationBypassAccessCheck */
2297 0, /* FileVolumeNameInformation */
2298 sizeof(FILE_ID_INFORMATION), /* FileIdInformation */
2299 0, /* FileIdExtdDirectoryInformation */
2300 0, /* FileReplaceCompletionInformation */
2301 0, /* FileHardLinkFullIdInformation */
2302 0, /* FileIdExtdBothDirectoryInformation */
2305 struct stat st;
2306 int fd, needs_close = FALSE;
2307 ULONG attr;
2309 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2311 io->Information = 0;
2313 if (class <= 0 || class >= FileMaximumInformation)
2314 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2315 if (!info_sizes[class])
2317 FIXME("Unsupported class (%d)\n", class);
2318 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2320 if (len < info_sizes[class])
2321 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2323 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2325 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2327 if (io->u.Status != STATUS_BAD_DEVICE_TYPE) return io->u.Status;
2328 return server_get_file_info( hFile, io, ptr, len, class );
2332 switch (class)
2334 case FileBasicInformation:
2335 if (fd_get_file_info( fd, &st, &attr ) == -1)
2336 io->u.Status = FILE_GetNtStatus();
2337 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2338 io->u.Status = STATUS_INVALID_INFO_CLASS;
2339 else
2340 fill_file_info( &st, attr, ptr, class );
2341 break;
2342 case FileStandardInformation:
2344 FILE_STANDARD_INFORMATION *info = ptr;
2346 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2347 else
2349 fill_file_info( &st, attr, info, class );
2350 info->DeletePending = FALSE; /* FIXME */
2353 break;
2354 case FilePositionInformation:
2356 FILE_POSITION_INFORMATION *info = ptr;
2357 off_t res = lseek( fd, 0, SEEK_CUR );
2358 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2359 else info->CurrentByteOffset.QuadPart = res;
2361 break;
2362 case FileInternalInformation:
2363 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2364 else fill_file_info( &st, attr, ptr, class );
2365 break;
2366 case FileEaInformation:
2368 FILE_EA_INFORMATION *info = ptr;
2369 info->EaSize = 0;
2371 break;
2372 case FileAccessInformation:
2374 FILE_ACCESS_INFORMATION *info = ptr;
2375 SERVER_START_REQ( get_object_info )
2377 req->handle = wine_server_obj_handle( hFile );
2378 io->u.Status = wine_server_call( req );
2379 if (io->u.Status == STATUS_SUCCESS)
2380 info->AccessFlags = reply->access;
2382 SERVER_END_REQ;
2384 break;
2385 case FileEndOfFileInformation:
2386 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2387 else fill_file_info( &st, attr, ptr, class );
2388 break;
2389 case FileAllInformation:
2391 FILE_ALL_INFORMATION *info = ptr;
2392 ANSI_STRING unix_name;
2394 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2395 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2396 io->u.Status = STATUS_INVALID_INFO_CLASS;
2397 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2399 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2401 fill_file_info( &st, attr, info, FileAllInformation );
2402 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2403 info->EaInformation.EaSize = 0;
2404 info->AccessInformation.AccessFlags = 0; /* FIXME */
2405 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2406 info->ModeInformation.Mode = 0; /* FIXME */
2407 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2409 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2410 RtlFreeAnsiString( &unix_name );
2411 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2414 break;
2415 case FileMailslotQueryInformation:
2417 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2419 SERVER_START_REQ( set_mailslot_info )
2421 req->handle = wine_server_obj_handle( hFile );
2422 req->flags = 0;
2423 io->u.Status = wine_server_call( req );
2424 if( io->u.Status == STATUS_SUCCESS )
2426 info->MaximumMessageSize = reply->max_msgsize;
2427 info->MailslotQuota = 0;
2428 info->NextMessageSize = 0;
2429 info->MessagesAvailable = 0;
2430 info->ReadTimeout.QuadPart = reply->read_timeout;
2433 SERVER_END_REQ;
2434 if (!io->u.Status)
2436 char *tmpbuf;
2437 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2438 if (size > 0x10000) size = 0x10000;
2439 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2441 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2443 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2444 info->MessagesAvailable = (res > 0);
2445 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2446 if (needs_close) close( fd );
2448 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2452 break;
2453 case FilePipeInformation:
2455 FILE_PIPE_INFORMATION* pi = ptr;
2457 SERVER_START_REQ( get_named_pipe_info )
2459 req->handle = wine_server_obj_handle( hFile );
2460 if (!(io->u.Status = wine_server_call( req )))
2462 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2463 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2464 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2465 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2468 SERVER_END_REQ;
2470 break;
2471 case FilePipeLocalInformation:
2473 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2475 SERVER_START_REQ( get_named_pipe_info )
2477 req->handle = wine_server_obj_handle( hFile );
2478 if (!(io->u.Status = wine_server_call( req )))
2480 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2481 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2482 switch (reply->sharing)
2484 case FILE_SHARE_READ:
2485 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2486 break;
2487 case FILE_SHARE_WRITE:
2488 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2489 break;
2490 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2491 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2492 break;
2494 pli->MaximumInstances = reply->maxinstances;
2495 pli->CurrentInstances = reply->instances;
2496 pli->InboundQuota = reply->insize;
2497 pli->ReadDataAvailable = 0; /* FIXME */
2498 pli->OutboundQuota = reply->outsize;
2499 pli->WriteQuotaAvailable = 0; /* FIXME */
2500 pli->NamedPipeState = 0; /* FIXME */
2501 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2502 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2505 SERVER_END_REQ;
2507 break;
2508 case FileNameInformation:
2510 FILE_NAME_INFORMATION *info = ptr;
2511 ANSI_STRING unix_name;
2513 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2515 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2516 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2517 RtlFreeAnsiString( &unix_name );
2518 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2521 break;
2522 case FileNetworkOpenInformation:
2524 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2525 ANSI_STRING unix_name;
2527 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2529 ULONG attributes;
2530 struct stat st;
2532 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2533 io->u.Status = FILE_GetNtStatus();
2534 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2535 io->u.Status = STATUS_INVALID_INFO_CLASS;
2536 else
2538 FILE_BASIC_INFORMATION basic;
2539 FILE_STANDARD_INFORMATION std;
2541 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2542 fill_file_info( &st, attributes, &std, FileStandardInformation );
2544 info->CreationTime = basic.CreationTime;
2545 info->LastAccessTime = basic.LastAccessTime;
2546 info->LastWriteTime = basic.LastWriteTime;
2547 info->ChangeTime = basic.ChangeTime;
2548 info->AllocationSize = std.AllocationSize;
2549 info->EndOfFile = std.EndOfFile;
2550 info->FileAttributes = basic.FileAttributes;
2552 RtlFreeAnsiString( &unix_name );
2555 break;
2556 case FileIdInformation:
2557 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2558 else
2560 FILE_ID_INFORMATION *info = ptr;
2561 info->VolumeSerialNumber = 0; /* FIXME */
2562 memset( &info->FileId, 0, sizeof(info->FileId) );
2563 *(ULONGLONG *)&info->FileId = st.st_ino;
2565 break;
2566 default:
2567 FIXME("Unsupported class (%d)\n", class);
2568 io->u.Status = STATUS_NOT_IMPLEMENTED;
2569 break;
2571 if (needs_close) close( fd );
2572 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2573 return io->u.Status;
2576 /******************************************************************************
2577 * NtSetInformationFile [NTDLL.@]
2578 * ZwSetInformationFile [NTDLL.@]
2580 * Set information about an open file handle.
2582 * PARAMS
2583 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2584 * io [O] Receives information about the operation on return
2585 * ptr [I] Source for file information
2586 * len [I] Size of FileInformation
2587 * class [I] Type of file information to set
2589 * RETURNS
2590 * Success: 0. io is updated.
2591 * Failure: An NTSTATUS error code describing the error.
2593 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2594 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2596 int fd, needs_close;
2598 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2600 io->u.Status = STATUS_SUCCESS;
2601 switch (class)
2603 case FileBasicInformation:
2604 if (len >= sizeof(FILE_BASIC_INFORMATION))
2606 struct stat st;
2607 const FILE_BASIC_INFORMATION *info = ptr;
2609 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2610 return io->u.Status;
2612 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2613 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2615 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2617 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2618 else
2620 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2622 if (S_ISDIR( st.st_mode))
2623 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2624 else
2625 st.st_mode &= ~0222; /* clear write permission bits */
2627 else
2629 /* add write permission only where we already have read permission */
2630 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2632 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2636 if (needs_close) close( fd );
2638 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2639 break;
2641 case FilePositionInformation:
2642 if (len >= sizeof(FILE_POSITION_INFORMATION))
2644 const FILE_POSITION_INFORMATION *info = ptr;
2646 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2647 return io->u.Status;
2649 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2650 io->u.Status = FILE_GetNtStatus();
2652 if (needs_close) close( fd );
2654 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2655 break;
2657 case FileEndOfFileInformation:
2658 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2660 struct stat st;
2661 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2663 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2664 return io->u.Status;
2666 /* first try normal truncate */
2667 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2669 /* now check for the need to extend the file */
2670 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2672 static const char zero;
2674 /* extend the file one byte beyond the requested size and then truncate it */
2675 /* this should work around ftruncate implementations that can't extend files */
2676 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2677 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2679 io->u.Status = FILE_GetNtStatus();
2681 if (needs_close) close( fd );
2683 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2684 break;
2686 case FilePipeInformation:
2687 if (len >= sizeof(FILE_PIPE_INFORMATION))
2689 FILE_PIPE_INFORMATION *info = ptr;
2691 if ((info->CompletionMode | info->ReadMode) & ~1)
2693 io->u.Status = STATUS_INVALID_PARAMETER;
2694 break;
2697 SERVER_START_REQ( set_named_pipe_info )
2699 req->handle = wine_server_obj_handle( handle );
2700 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2701 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2702 io->u.Status = wine_server_call( req );
2704 SERVER_END_REQ;
2706 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2707 break;
2709 case FileMailslotSetInformation:
2711 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2713 SERVER_START_REQ( set_mailslot_info )
2715 req->handle = wine_server_obj_handle( handle );
2716 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2717 req->read_timeout = info->ReadTimeout.QuadPart;
2718 io->u.Status = wine_server_call( req );
2720 SERVER_END_REQ;
2722 break;
2724 case FileCompletionInformation:
2725 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2727 FILE_COMPLETION_INFORMATION *info = ptr;
2729 SERVER_START_REQ( set_completion_info )
2731 req->handle = wine_server_obj_handle( handle );
2732 req->chandle = wine_server_obj_handle( info->CompletionPort );
2733 req->ckey = info->CompletionKey;
2734 io->u.Status = wine_server_call( req );
2736 SERVER_END_REQ;
2737 } else
2738 io->u.Status = STATUS_INVALID_PARAMETER_3;
2739 break;
2741 case FileAllInformation:
2742 io->u.Status = STATUS_INVALID_INFO_CLASS;
2743 break;
2745 case FileValidDataLengthInformation:
2746 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2748 struct stat st;
2749 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2751 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2752 return io->u.Status;
2754 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2755 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2756 io->u.Status = STATUS_INVALID_PARAMETER;
2757 else
2759 #ifdef HAVE_FALLOCATE
2760 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2762 NTSTATUS status = FILE_GetNtStatus();
2763 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2764 else io->u.Status = status;
2766 #else
2767 FIXME( "setting valid data length not supported\n" );
2768 #endif
2770 if (needs_close) close( fd );
2772 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2773 break;
2775 case FileDispositionInformation:
2776 if (len >= sizeof(FILE_DISPOSITION_INFORMATION))
2778 FILE_DISPOSITION_INFORMATION *info = ptr;
2780 SERVER_START_REQ( set_fd_disp_info )
2782 req->handle = wine_server_obj_handle( handle );
2783 req->unlink = info->DoDeleteFile;
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 FileRenameInformation:
2792 if (len >= sizeof(FILE_RENAME_INFORMATION))
2794 FILE_RENAME_INFORMATION *info = ptr;
2795 UNICODE_STRING name_str;
2796 OBJECT_ATTRIBUTES attr;
2797 ANSI_STRING unix_name;
2799 name_str.Buffer = info->FileName;
2800 name_str.Length = info->FileNameLength;
2801 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2803 attr.Length = sizeof(attr);
2804 attr.ObjectName = &name_str;
2805 attr.RootDirectory = info->RootDir;
2806 attr.Attributes = OBJ_CASE_INSENSITIVE;
2808 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2809 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2810 break;
2812 if (!info->Replace && io->u.Status == STATUS_SUCCESS)
2814 RtlFreeAnsiString( &unix_name );
2815 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2816 break;
2819 SERVER_START_REQ( set_fd_name_info )
2821 req->handle = wine_server_obj_handle( handle );
2822 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2823 req->link = FALSE;
2824 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2825 io->u.Status = wine_server_call( req );
2827 SERVER_END_REQ;
2829 RtlFreeAnsiString( &unix_name );
2831 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2832 break;
2834 case FileLinkInformation:
2835 if (len >= sizeof(FILE_LINK_INFORMATION))
2837 FILE_LINK_INFORMATION *info = ptr;
2838 UNICODE_STRING name_str;
2839 OBJECT_ATTRIBUTES attr;
2840 ANSI_STRING unix_name;
2842 name_str.Buffer = info->FileName;
2843 name_str.Length = info->FileNameLength;
2844 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2846 attr.Length = sizeof(attr);
2847 attr.ObjectName = &name_str;
2848 attr.RootDirectory = info->RootDirectory;
2849 attr.Attributes = OBJ_CASE_INSENSITIVE;
2851 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2852 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2853 break;
2855 if (!info->ReplaceIfExists && io->u.Status == STATUS_SUCCESS)
2857 RtlFreeAnsiString( &unix_name );
2858 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2859 break;
2862 SERVER_START_REQ( set_fd_name_info )
2864 req->handle = wine_server_obj_handle( handle );
2865 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2866 req->link = TRUE;
2867 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2868 io->u.Status = wine_server_call( req );
2870 SERVER_END_REQ;
2872 RtlFreeAnsiString( &unix_name );
2874 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2875 break;
2877 default:
2878 FIXME("Unsupported class (%d)\n", class);
2879 io->u.Status = STATUS_NOT_IMPLEMENTED;
2880 break;
2882 io->Information = 0;
2883 return io->u.Status;
2887 /******************************************************************************
2888 * NtQueryFullAttributesFile (NTDLL.@)
2890 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2891 FILE_NETWORK_OPEN_INFORMATION *info )
2893 ANSI_STRING unix_name;
2894 NTSTATUS status;
2896 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2898 ULONG attributes;
2899 struct stat st;
2901 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2902 status = FILE_GetNtStatus();
2903 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2904 status = STATUS_INVALID_INFO_CLASS;
2905 else
2907 FILE_BASIC_INFORMATION basic;
2908 FILE_STANDARD_INFORMATION std;
2910 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2911 fill_file_info( &st, attributes, &std, FileStandardInformation );
2913 info->CreationTime = basic.CreationTime;
2914 info->LastAccessTime = basic.LastAccessTime;
2915 info->LastWriteTime = basic.LastWriteTime;
2916 info->ChangeTime = basic.ChangeTime;
2917 info->AllocationSize = std.AllocationSize;
2918 info->EndOfFile = std.EndOfFile;
2919 info->FileAttributes = basic.FileAttributes;
2920 if (DIR_is_hidden_file( attr->ObjectName ))
2921 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2923 RtlFreeAnsiString( &unix_name );
2925 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2926 return status;
2930 /******************************************************************************
2931 * NtQueryAttributesFile (NTDLL.@)
2932 * ZwQueryAttributesFile (NTDLL.@)
2934 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2936 ANSI_STRING unix_name;
2937 NTSTATUS status;
2939 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2941 ULONG attributes;
2942 struct stat st;
2944 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2945 status = FILE_GetNtStatus();
2946 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2947 status = STATUS_INVALID_INFO_CLASS;
2948 else
2950 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2951 if (DIR_is_hidden_file( attr->ObjectName ))
2952 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2954 RtlFreeAnsiString( &unix_name );
2956 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2957 return status;
2961 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2962 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2963 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2964 unsigned int flags )
2966 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2968 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2969 /* Don't assume read-only, let the mount options set it below */
2970 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2972 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2973 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2975 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2976 info->Characteristics |= FILE_REMOTE_DEVICE;
2978 else if (!strcmp("procfs", fstypename))
2979 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2980 else
2981 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2983 if (flags & MNT_RDONLY)
2984 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2986 if (!(flags & MNT_LOCAL))
2988 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2989 info->Characteristics |= FILE_REMOTE_DEVICE;
2992 #endif
2994 static inline BOOL is_device_placeholder( int fd )
2996 static const char wine_placeholder[] = "Wine device placeholder";
2997 char buffer[sizeof(wine_placeholder)-1];
2999 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
3000 return FALSE;
3001 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
3004 /******************************************************************************
3005 * get_device_info
3007 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
3009 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
3011 struct stat st;
3013 info->Characteristics = 0;
3014 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
3015 if (S_ISCHR( st.st_mode ))
3017 info->DeviceType = FILE_DEVICE_UNKNOWN;
3018 #ifdef linux
3019 switch(major(st.st_rdev))
3021 case MEM_MAJOR:
3022 info->DeviceType = FILE_DEVICE_NULL;
3023 break;
3024 case TTY_MAJOR:
3025 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
3026 break;
3027 case LP_MAJOR:
3028 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
3029 break;
3030 case SCSI_TAPE_MAJOR:
3031 info->DeviceType = FILE_DEVICE_TAPE;
3032 break;
3034 #endif
3036 else if (S_ISBLK( st.st_mode ))
3038 info->DeviceType = FILE_DEVICE_DISK;
3040 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
3042 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
3044 else if (is_device_placeholder( fd ))
3046 info->DeviceType = FILE_DEVICE_DISK;
3048 else /* regular file or directory */
3050 #if defined(linux) && defined(HAVE_FSTATFS)
3051 struct statfs stfs;
3053 /* check for floppy disk */
3054 if (major(st.st_dev) == FLOPPY_MAJOR)
3055 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3057 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
3058 switch (stfs.f_type)
3060 case 0x9660: /* iso9660 */
3061 case 0x9fa1: /* supermount */
3062 case 0x15013346: /* udf */
3063 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3064 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3065 break;
3066 case 0x6969: /* nfs */
3067 case 0xff534d42: /* cifs */
3068 case 0xfe534d42: /* smb2 */
3069 case 0x517b: /* smbfs */
3070 case 0x564c: /* ncpfs */
3071 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3072 info->Characteristics |= FILE_REMOTE_DEVICE;
3073 break;
3074 case 0x01021994: /* tmpfs */
3075 case 0x28cd3d45: /* cramfs */
3076 case 0x1373: /* devfs */
3077 case 0x9fa0: /* procfs */
3078 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3079 break;
3080 default:
3081 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3082 break;
3084 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3085 struct statfs stfs;
3087 if (fstatfs( fd, &stfs ) < 0)
3088 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3089 else
3090 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
3091 #elif defined(__NetBSD__)
3092 struct statvfs stfs;
3094 if (fstatvfs( fd, &stfs) < 0)
3095 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3096 else
3097 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
3098 #elif defined(sun)
3099 /* Use dkio to work out device types */
3101 # include <sys/dkio.h>
3102 # include <sys/vtoc.h>
3103 struct dk_cinfo dkinf;
3104 int retval = ioctl(fd, DKIOCINFO, &dkinf);
3105 if(retval==-1){
3106 WARN("Unable to get disk device type information - assuming a disk like device\n");
3107 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3109 switch (dkinf.dki_ctype)
3111 case DKC_CDROM:
3112 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3113 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3114 break;
3115 case DKC_NCRFLOPPY:
3116 case DKC_SMSFLOPPY:
3117 case DKC_INTEL82072:
3118 case DKC_INTEL82077:
3119 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3120 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3121 break;
3122 case DKC_MD:
3123 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3124 break;
3125 default:
3126 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3129 #else
3130 static int warned;
3131 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3132 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3133 #endif
3134 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3136 return STATUS_SUCCESS;
3140 /******************************************************************************
3141 * NtQueryVolumeInformationFile [NTDLL.@]
3142 * ZwQueryVolumeInformationFile [NTDLL.@]
3144 * Get volume information for an open file handle.
3146 * PARAMS
3147 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3148 * io [O] Receives information about the operation on return
3149 * buffer [O] Destination for volume information
3150 * length [I] Size of FsInformation
3151 * info_class [I] Type of volume information to set
3153 * RETURNS
3154 * Success: 0. io and buffer are updated.
3155 * Failure: An NTSTATUS error code describing the error.
3157 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3158 PVOID buffer, ULONG length,
3159 FS_INFORMATION_CLASS info_class )
3161 int fd, needs_close;
3162 struct stat st;
3163 static int once;
3165 io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL );
3166 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
3168 SERVER_START_REQ( get_volume_info )
3170 req->handle = wine_server_obj_handle( handle );
3171 req->info_class = info_class;
3172 wine_server_set_reply( req, buffer, length );
3173 io->u.Status = wine_server_call( req );
3174 if (!io->u.Status) io->Information = wine_server_reply_size( reply );
3176 SERVER_END_REQ;
3177 return io->u.Status;
3179 else if (io->u.Status) return io->u.Status;
3181 io->u.Status = STATUS_NOT_IMPLEMENTED;
3182 io->Information = 0;
3184 switch( info_class )
3186 case FileFsVolumeInformation:
3187 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3188 break;
3189 case FileFsLabelInformation:
3190 FIXME( "%p: label info not supported\n", handle );
3191 break;
3192 case FileFsSizeInformation:
3193 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3194 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3195 else
3197 FILE_FS_SIZE_INFORMATION *info = buffer;
3199 if (fstat( fd, &st ) < 0)
3201 io->u.Status = FILE_GetNtStatus();
3202 break;
3204 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3206 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3208 else
3210 ULONGLONG bsize;
3211 /* Linux's fstatvfs is buggy */
3212 #if !defined(linux) || !defined(HAVE_FSTATFS)
3213 struct statvfs stfs;
3215 if (fstatvfs( fd, &stfs ) < 0)
3217 io->u.Status = FILE_GetNtStatus();
3218 break;
3220 bsize = stfs.f_frsize;
3221 #else
3222 struct statfs stfs;
3223 if (fstatfs( fd, &stfs ) < 0)
3225 io->u.Status = FILE_GetNtStatus();
3226 break;
3228 bsize = stfs.f_bsize;
3229 #endif
3230 if (bsize == 2048) /* assume CD-ROM */
3232 info->BytesPerSector = 2048;
3233 info->SectorsPerAllocationUnit = 1;
3235 else
3237 info->BytesPerSector = 512;
3238 info->SectorsPerAllocationUnit = 8;
3240 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3241 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3242 io->Information = sizeof(*info);
3243 io->u.Status = STATUS_SUCCESS;
3246 break;
3247 case FileFsDeviceInformation:
3248 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3249 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3250 else
3252 FILE_FS_DEVICE_INFORMATION *info = buffer;
3254 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3255 io->Information = sizeof(*info);
3257 break;
3258 case FileFsAttributeInformation:
3259 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3260 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3261 else
3263 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3265 FIXME( "%p: faking attribute info\n", handle );
3266 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3267 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3268 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3269 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3270 info->FileSystemNameLength = sizeof(ntfsW);
3271 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3273 io->Information = sizeof(*info);
3274 io->u.Status = STATUS_SUCCESS;
3276 break;
3277 case FileFsControlInformation:
3278 FIXME( "%p: control info not supported\n", handle );
3279 break;
3280 case FileFsFullSizeInformation:
3281 FIXME( "%p: full size info not supported\n", handle );
3282 break;
3283 case FileFsObjectIdInformation:
3284 FIXME( "%p: object id info not supported\n", handle );
3285 break;
3286 case FileFsMaximumInformation:
3287 FIXME( "%p: maximum info not supported\n", handle );
3288 break;
3289 default:
3290 io->u.Status = STATUS_INVALID_PARAMETER;
3291 break;
3293 if (needs_close) close( fd );
3294 return io->u.Status;
3298 /******************************************************************
3299 * NtQueryEaFile (NTDLL.@)
3301 * Read extended attributes from NTFS files.
3303 * PARAMS
3304 * hFile [I] File handle, must be opened with FILE_READ_EA access
3305 * iosb [O] Receives information about the operation on return
3306 * buffer [O] Output buffer
3307 * length [I] Length of output buffer
3308 * single_entry [I] Only read and return one entry
3309 * ea_list [I] Optional list with names of EAs to return
3310 * ea_list_len [I] Length of ea_list in bytes
3311 * ea_index [I] Optional pointer to 1-based index of attribute to return
3312 * restart [I] restart EA scan
3314 * RETURNS
3315 * Success: 0. Atrributes read into buffer
3316 * Failure: An NTSTATUS error code describing the error.
3318 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3319 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3320 PULONG ea_index, BOOLEAN restart )
3322 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3323 hFile, iosb, buffer, length, single_entry, ea_list,
3324 ea_list_len, ea_index, restart);
3325 return STATUS_ACCESS_DENIED;
3329 /******************************************************************
3330 * NtSetEaFile (NTDLL.@)
3332 * Update extended attributes for NTFS files.
3334 * PARAMS
3335 * hFile [I] File handle, must be opened with FILE_READ_EA access
3336 * iosb [O] Receives information about the operation on return
3337 * buffer [I] Buffer with EA information
3338 * length [I] Length of buffer
3340 * RETURNS
3341 * Success: 0. Attributes are updated
3342 * Failure: An NTSTATUS error code describing the error.
3344 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3346 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3347 return STATUS_ACCESS_DENIED;
3351 /******************************************************************
3352 * NtFlushBuffersFile (NTDLL.@)
3354 * Flush any buffered data on an open file handle.
3356 * PARAMS
3357 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3358 * IoStatusBlock [O] Receives information about the operation on return
3360 * RETURNS
3361 * Success: 0. IoStatusBlock is updated.
3362 * Failure: An NTSTATUS error code describing the error.
3364 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3366 NTSTATUS ret;
3367 HANDLE hEvent = NULL;
3368 enum server_fd_type type;
3369 int fd, needs_close;
3371 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3372 if (ret == STATUS_ACCESS_DENIED)
3373 ret = server_get_unix_fd( hFile, FILE_APPEND_DATA, &fd, &needs_close, &type, NULL );
3375 if (!ret && type == FD_TYPE_SERIAL)
3377 ret = COMM_FlushBuffersFile( fd );
3379 else if (ret != STATUS_ACCESS_DENIED)
3381 SERVER_START_REQ( flush )
3383 req->async = server_async( hFile, NULL, NULL, NULL, NULL, IoStatusBlock );
3384 ret = wine_server_call( req );
3385 hEvent = wine_server_ptr_handle( reply->event );
3387 SERVER_END_REQ;
3389 if (hEvent)
3391 NtWaitForSingleObject( hEvent, FALSE, NULL );
3392 ret = STATUS_SUCCESS;
3396 if (needs_close) close( fd );
3397 return ret;
3400 /******************************************************************
3401 * NtLockFile (NTDLL.@)
3405 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3406 PIO_APC_ROUTINE apc, void* apc_user,
3407 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3408 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3409 BOOLEAN exclusive )
3411 NTSTATUS ret;
3412 HANDLE handle;
3413 BOOLEAN async;
3414 static BOOLEAN warn = TRUE;
3416 if (apc || io_status || key)
3418 FIXME("Unimplemented yet parameter\n");
3419 return STATUS_NOT_IMPLEMENTED;
3422 if (apc_user && warn)
3424 FIXME("I/O completion on lock not implemented yet\n");
3425 warn = FALSE;
3428 for (;;)
3430 SERVER_START_REQ( lock_file )
3432 req->handle = wine_server_obj_handle( hFile );
3433 req->offset = offset->QuadPart;
3434 req->count = count->QuadPart;
3435 req->shared = !exclusive;
3436 req->wait = !dont_wait;
3437 ret = wine_server_call( req );
3438 handle = wine_server_ptr_handle( reply->handle );
3439 async = reply->overlapped;
3441 SERVER_END_REQ;
3442 if (ret != STATUS_PENDING)
3444 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3445 return ret;
3448 if (async)
3450 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3451 if (handle) NtClose( handle );
3452 return STATUS_PENDING;
3454 if (handle)
3456 NtWaitForSingleObject( handle, FALSE, NULL );
3457 NtClose( handle );
3459 else
3461 LARGE_INTEGER time;
3463 /* Unix lock conflict, sleep a bit and retry */
3464 time.QuadPart = 100 * (ULONGLONG)10000;
3465 time.QuadPart = -time.QuadPart;
3466 NtDelayExecution( FALSE, &time );
3472 /******************************************************************
3473 * NtUnlockFile (NTDLL.@)
3477 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3478 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3479 PULONG key )
3481 NTSTATUS status;
3483 TRACE( "%p %x%08x %x%08x\n",
3484 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3486 if (io_status || key)
3488 FIXME("Unimplemented yet parameter\n");
3489 return STATUS_NOT_IMPLEMENTED;
3492 SERVER_START_REQ( unlock_file )
3494 req->handle = wine_server_obj_handle( hFile );
3495 req->offset = offset->QuadPart;
3496 req->count = count->QuadPart;
3497 status = wine_server_call( req );
3499 SERVER_END_REQ;
3500 return status;
3503 /******************************************************************
3504 * NtCreateNamedPipeFile (NTDLL.@)
3508 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3509 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3510 ULONG sharing, ULONG dispo, ULONG options,
3511 ULONG pipe_type, ULONG read_mode,
3512 ULONG completion_mode, ULONG max_inst,
3513 ULONG inbound_quota, ULONG outbound_quota,
3514 PLARGE_INTEGER timeout)
3516 NTSTATUS status;
3517 data_size_t len;
3518 struct object_attributes *objattr;
3520 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3521 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3522 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3523 outbound_quota, timeout);
3525 if (!attr) return STATUS_INVALID_PARAMETER;
3527 /* assume we only get relative timeout */
3528 if (timeout->QuadPart > 0)
3529 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3531 if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
3533 SERVER_START_REQ( create_named_pipe )
3535 req->access = access;
3536 req->options = options;
3537 req->sharing = sharing;
3538 req->flags =
3539 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3540 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3541 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3542 req->maxinstances = max_inst;
3543 req->outsize = outbound_quota;
3544 req->insize = inbound_quota;
3545 req->timeout = timeout->QuadPart;
3546 wine_server_add_data( req, objattr, len );
3547 status = wine_server_call( req );
3548 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3550 SERVER_END_REQ;
3552 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3553 return status;
3556 /******************************************************************
3557 * NtDeleteFile (NTDLL.@)
3561 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3563 NTSTATUS status;
3564 HANDLE hFile;
3565 IO_STATUS_BLOCK io;
3567 TRACE("%p\n", ObjectAttributes);
3568 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3569 ObjectAttributes, &io, NULL, 0,
3570 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3571 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3572 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3573 return status;
3576 /******************************************************************
3577 * NtCancelIoFileEx (NTDLL.@)
3581 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3583 TRACE("%p %p %p\n", hFile, iosb, io_status );
3585 SERVER_START_REQ( cancel_async )
3587 req->handle = wine_server_obj_handle( hFile );
3588 req->iosb = wine_server_client_ptr( iosb );
3589 req->only_thread = FALSE;
3590 io_status->u.Status = wine_server_call( req );
3592 SERVER_END_REQ;
3594 return io_status->u.Status;
3597 /******************************************************************
3598 * NtCancelIoFile (NTDLL.@)
3602 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3604 TRACE("%p %p\n", hFile, io_status );
3606 SERVER_START_REQ( cancel_async )
3608 req->handle = wine_server_obj_handle( hFile );
3609 req->iosb = 0;
3610 req->only_thread = TRUE;
3611 io_status->u.Status = wine_server_call( req );
3613 SERVER_END_REQ;
3615 return io_status->u.Status;
3618 /******************************************************************************
3619 * NtCreateMailslotFile [NTDLL.@]
3620 * ZwCreateMailslotFile [NTDLL.@]
3622 * PARAMS
3623 * pHandle [O] pointer to receive the handle created
3624 * DesiredAccess [I] access mode (read, write, etc)
3625 * ObjectAttributes [I] fully qualified NT path of the mailslot
3626 * IoStatusBlock [O] receives completion status and other info
3627 * CreateOptions [I]
3628 * MailslotQuota [I]
3629 * MaxMessageSize [I]
3630 * TimeOut [I]
3632 * RETURNS
3633 * An NT status code
3635 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3636 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3637 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3638 PLARGE_INTEGER TimeOut)
3640 LARGE_INTEGER timeout;
3641 NTSTATUS ret;
3642 data_size_t len;
3643 struct object_attributes *objattr;
3645 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3646 pHandle, DesiredAccess, attr, IoStatusBlock,
3647 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3649 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3650 if (!attr) return STATUS_INVALID_PARAMETER;
3652 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3655 * For a NULL TimeOut pointer set the default timeout value
3657 if (!TimeOut)
3658 timeout.QuadPart = -1;
3659 else
3660 timeout.QuadPart = TimeOut->QuadPart;
3662 SERVER_START_REQ( create_mailslot )
3664 req->access = DesiredAccess;
3665 req->max_msgsize = MaxMessageSize;
3666 req->read_timeout = timeout.QuadPart;
3667 wine_server_add_data( req, objattr, len );
3668 ret = wine_server_call( req );
3669 if( ret == STATUS_SUCCESS )
3670 *pHandle = wine_server_ptr_handle( reply->handle );
3672 SERVER_END_REQ;
3674 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3675 return ret;