winspool.drv: Avoid bitwise operation.
[wine.git] / dlls / ntdll / file.c
blob35fd2c1beb0ab04eb10cfae1fe4e42a595152232
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_PIPE:
715 case FD_TYPE_CHAR:
716 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
717 break;
718 default:
719 break;
721 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
722 return STATUS_SUCCESS;
726 /* retrieve the timeout for the next wait, in milliseconds */
727 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
729 int ret = -1;
731 if (timeouts->total != -1)
733 ret = timeouts->end_time - NtGetTickCount();
734 if (ret < 0) ret = 0;
736 if (already && timeouts->interval != -1)
738 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
740 return ret;
744 /* retrieve the avail_mode flag for async reads */
745 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
747 NTSTATUS status = STATUS_SUCCESS;
749 switch(type)
751 case FD_TYPE_SERIAL:
753 /* GetCommTimeouts */
754 SERIAL_TIMEOUTS st;
755 IO_STATUS_BLOCK io;
757 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
758 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
759 if (status) break;
760 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
761 !st.ReadTotalTimeoutConstant &&
762 st.ReadIntervalTimeout == MAXDWORD);
764 break;
765 case FD_TYPE_MAILSLOT:
766 case FD_TYPE_SOCKET:
767 case FD_TYPE_PIPE:
768 case FD_TYPE_CHAR:
769 *avail_mode = TRUE;
770 break;
771 default:
772 *avail_mode = FALSE;
773 break;
775 return status;
778 /* register an async I/O for a file read; helper for NtReadFile */
779 static NTSTATUS register_async_file_read( HANDLE handle, HANDLE event,
780 PIO_APC_ROUTINE apc, void *apc_user,
781 IO_STATUS_BLOCK *iosb, void *buffer,
782 ULONG already, ULONG length, BOOL avail_mode )
784 struct async_fileio_read *fileio;
785 NTSTATUS status;
787 if (!(fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), FILE_AsyncReadService, handle )))
788 return STATUS_NO_MEMORY;
790 fileio->already = already;
791 fileio->count = length;
792 fileio->buffer = buffer;
793 fileio->avail_mode = avail_mode;
795 SERVER_START_REQ( register_async )
797 req->type = ASYNC_TYPE_READ;
798 req->count = length;
799 req->async = server_async( handle, &fileio->io, event, apc, apc_user, iosb );
800 status = wine_server_call( req );
802 SERVER_END_REQ;
804 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
805 return status;
809 /******************************************************************************
810 * NtReadFile [NTDLL.@]
811 * ZwReadFile [NTDLL.@]
813 * Read from an open file handle.
815 * PARAMS
816 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
817 * Event [I] Event to signal upon completion (or NULL)
818 * ApcRoutine [I] Callback to call upon completion (or NULL)
819 * ApcContext [I] Context for ApcRoutine (or NULL)
820 * IoStatusBlock [O] Receives information about the operation on return
821 * Buffer [O] Destination for the data read
822 * Length [I] Size of Buffer
823 * ByteOffset [O] Destination for the new file pointer position (or NULL)
824 * Key [O] Function unknown (may be NULL)
826 * RETURNS
827 * Success: 0. IoStatusBlock is updated, and the Information member contains
828 * The number of bytes read.
829 * Failure: An NTSTATUS error code describing the error.
831 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
832 PIO_APC_ROUTINE apc, void* apc_user,
833 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
834 PLARGE_INTEGER offset, PULONG key)
836 int result, unix_handle, needs_close;
837 unsigned int options;
838 struct io_timeouts timeouts;
839 NTSTATUS status;
840 ULONG total = 0;
841 enum server_fd_type type;
842 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
843 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
845 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
846 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
848 if (!io_status) return STATUS_ACCESS_VIOLATION;
850 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
851 &needs_close, &type, &options );
852 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
854 if (!virtual_check_buffer_for_write( buffer, length )) return STATUS_ACCESS_VIOLATION;
856 if (status == STATUS_BAD_DEVICE_TYPE)
857 return server_read_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
859 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
861 if (type == FD_TYPE_FILE)
863 if (async_read && (!offset || offset->QuadPart < 0))
865 status = STATUS_INVALID_PARAMETER;
866 goto done;
869 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
871 /* async I/O doesn't make sense on regular files */
872 while ((result = virtual_locked_pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
874 if (errno != EINTR)
876 status = FILE_GetNtStatus();
877 goto done;
880 if (!async_read)
881 /* update file pointer position */
882 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
884 total = result;
885 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
886 goto done;
889 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
891 if (async_read && (!offset || offset->QuadPart < 0))
893 status = STATUS_INVALID_PARAMETER;
894 goto done;
898 if (type == FD_TYPE_SERIAL && async_read && length)
900 /* an asynchronous serial port read with a read interval timeout needs to
901 skip the synchronous read to make sure that the server starts the read
902 interval timer after the first read */
903 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts ))) goto err;
904 if (timeouts.interval)
906 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
907 buffer, total, length, FALSE );
908 goto err;
912 for (;;)
914 if ((result = virtual_locked_read( unix_handle, (char *)buffer + total, length - total )) >= 0)
916 total += result;
917 if (!result || total == length)
919 if (total)
921 status = STATUS_SUCCESS;
922 goto done;
924 switch (type)
926 case FD_TYPE_FILE:
927 case FD_TYPE_CHAR:
928 case FD_TYPE_DEVICE:
929 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
930 goto done;
931 case FD_TYPE_SERIAL:
932 if (!length)
934 status = STATUS_SUCCESS;
935 goto done;
937 break;
938 default:
939 status = STATUS_PIPE_BROKEN;
940 goto err;
943 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
945 else if (errno != EAGAIN)
947 if (errno == EINTR) continue;
948 if (!total) status = FILE_GetNtStatus();
949 goto err;
952 if (async_read)
954 BOOL avail_mode;
956 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
957 goto err;
958 if (total && avail_mode)
960 status = STATUS_SUCCESS;
961 goto done;
963 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
964 buffer, total, length, avail_mode );
965 goto err;
967 else /* synchronous read, wait for the fd to become ready */
969 struct pollfd pfd;
970 int ret, timeout;
972 if (!timeout_init_done)
974 timeout_init_done = TRUE;
975 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
976 goto err;
977 if (hEvent) NtResetEvent( hEvent, NULL );
979 timeout = get_next_io_timeout( &timeouts, total );
981 pfd.fd = unix_handle;
982 pfd.events = POLLIN;
984 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
986 if (total) /* return with what we got so far */
987 status = STATUS_SUCCESS;
988 else
989 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
990 goto done;
992 if (ret == -1 && errno != EINTR)
994 status = FILE_GetNtStatus();
995 goto done;
997 /* will now restart the read */
1001 done:
1002 send_completion = cvalue != 0;
1004 err:
1005 if (needs_close) close( unix_handle );
1006 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
1008 io_status->u.Status = status;
1009 io_status->Information = total;
1010 TRACE("= SUCCESS (%u)\n", total);
1011 if (hEvent) NtSetEvent( hEvent, NULL );
1012 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1013 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1015 else
1017 TRACE("= 0x%08x\n", status);
1018 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1021 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1023 return status;
1027 /******************************************************************************
1028 * NtReadFileScatter [NTDLL.@]
1029 * ZwReadFileScatter [NTDLL.@]
1031 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1032 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1033 ULONG length, PLARGE_INTEGER offset, PULONG key )
1035 int result, unix_handle, needs_close;
1036 unsigned int options;
1037 NTSTATUS status;
1038 ULONG pos = 0, total = 0;
1039 enum server_fd_type type;
1040 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1041 BOOL send_completion = FALSE;
1043 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1044 file, event, apc, apc_user, io_status, segments, length, offset, key);
1046 if (length % page_size) return STATUS_INVALID_PARAMETER;
1047 if (!io_status) return STATUS_ACCESS_VIOLATION;
1049 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1050 &needs_close, &type, &options );
1051 if (status) return status;
1053 if ((type != FD_TYPE_FILE) ||
1054 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1055 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1057 status = STATUS_INVALID_PARAMETER;
1058 goto error;
1061 while (length)
1063 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1064 result = pread( unix_handle, (char *)segments->Buffer + pos,
1065 page_size - pos, offset->QuadPart + total );
1066 else
1067 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1069 if (result == -1)
1071 if (errno == EINTR) continue;
1072 status = FILE_GetNtStatus();
1073 break;
1075 if (!result)
1077 status = STATUS_END_OF_FILE;
1078 break;
1080 total += result;
1081 length -= result;
1082 if ((pos += result) == page_size)
1084 pos = 0;
1085 segments++;
1089 send_completion = cvalue != 0;
1091 error:
1092 if (needs_close) close( unix_handle );
1093 if (status == STATUS_SUCCESS)
1095 io_status->u.Status = status;
1096 io_status->Information = total;
1097 TRACE("= SUCCESS (%u)\n", total);
1098 if (event) NtSetEvent( event, NULL );
1099 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1100 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1102 else
1104 TRACE("= 0x%08x\n", status);
1105 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1108 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1110 return status;
1114 /***********************************************************************
1115 * FILE_AsyncWriteService (INTERNAL)
1117 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1119 struct async_fileio_write *fileio = user;
1120 int result, fd, needs_close;
1121 enum server_fd_type type;
1123 switch (status)
1125 case STATUS_ALERTED:
1126 /* write some data (non-blocking) */
1127 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1128 &needs_close, &type, NULL )))
1129 break;
1131 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1132 result = send( fd, fileio->buffer, 0, 0 );
1133 else
1134 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1136 if (needs_close) close( fd );
1138 if (result < 0)
1140 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1141 else status = FILE_GetNtStatus();
1143 else
1145 fileio->already += result;
1146 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1148 break;
1150 case STATUS_TIMEOUT:
1151 case STATUS_IO_TIMEOUT:
1152 if (fileio->already) status = STATUS_SUCCESS;
1153 break;
1155 if (status != STATUS_PENDING)
1157 iosb->u.Status = status;
1158 iosb->Information = fileio->already;
1159 release_fileio( &fileio->io );
1161 return status;
1164 static NTSTATUS set_pending_write( HANDLE device )
1166 NTSTATUS status;
1168 SERVER_START_REQ( set_serial_info )
1170 req->handle = wine_server_obj_handle( device );
1171 req->flags = SERIALINFO_PENDING_WRITE;
1172 status = wine_server_call( req );
1174 SERVER_END_REQ;
1175 return status;
1178 /******************************************************************************
1179 * NtWriteFile [NTDLL.@]
1180 * ZwWriteFile [NTDLL.@]
1182 * Write to an open file handle.
1184 * PARAMS
1185 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1186 * Event [I] Event to signal upon completion (or NULL)
1187 * ApcRoutine [I] Callback to call upon completion (or NULL)
1188 * ApcContext [I] Context for ApcRoutine (or NULL)
1189 * IoStatusBlock [O] Receives information about the operation on return
1190 * Buffer [I] Source for the data to write
1191 * Length [I] Size of Buffer
1192 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1193 * Key [O] Function unknown (may be NULL)
1195 * RETURNS
1196 * Success: 0. IoStatusBlock is updated, and the Information member contains
1197 * The number of bytes written.
1198 * Failure: An NTSTATUS error code describing the error.
1200 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1201 PIO_APC_ROUTINE apc, void* apc_user,
1202 PIO_STATUS_BLOCK io_status,
1203 const void* buffer, ULONG length,
1204 PLARGE_INTEGER offset, PULONG key)
1206 int result, unix_handle, needs_close;
1207 unsigned int options;
1208 struct io_timeouts timeouts;
1209 NTSTATUS status;
1210 ULONG total = 0;
1211 enum server_fd_type type;
1212 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1213 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1214 LARGE_INTEGER offset_eof;
1216 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1217 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1219 if (!io_status) return STATUS_ACCESS_VIOLATION;
1221 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1222 &needs_close, &type, &options );
1223 if (status == STATUS_ACCESS_DENIED)
1225 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1226 &needs_close, &type, &options );
1227 append_write = TRUE;
1229 if (status && status != STATUS_BAD_DEVICE_TYPE) return status;
1231 if (!virtual_check_buffer_for_read( buffer, length ))
1233 status = STATUS_INVALID_USER_BUFFER;
1234 goto done;
1237 if (status == STATUS_BAD_DEVICE_TYPE)
1238 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1240 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1242 if (type == FD_TYPE_FILE)
1244 if (async_write &&
1245 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1247 status = STATUS_INVALID_PARAMETER;
1248 goto done;
1251 if (append_write)
1253 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1254 offset = &offset_eof;
1257 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1259 off_t off = offset->QuadPart;
1261 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1263 struct stat st;
1265 if (fstat( unix_handle, &st ) == -1)
1267 status = FILE_GetNtStatus();
1268 goto done;
1270 off = st.st_size;
1272 else if (offset->QuadPart < 0)
1274 status = STATUS_INVALID_PARAMETER;
1275 goto done;
1278 /* async I/O doesn't make sense on regular files */
1279 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1281 if (errno != EINTR)
1283 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1284 else status = FILE_GetNtStatus();
1285 goto done;
1289 if (!async_write)
1290 /* update file pointer position */
1291 lseek( unix_handle, off + result, SEEK_SET );
1293 total = result;
1294 status = STATUS_SUCCESS;
1295 goto done;
1298 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1300 if (async_write &&
1301 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1303 status = STATUS_INVALID_PARAMETER;
1304 goto done;
1308 for (;;)
1310 /* zero-length writes on sockets may not work with plain write(2) */
1311 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1312 result = send( unix_handle, buffer, 0, 0 );
1313 else
1314 result = write( unix_handle, (const char *)buffer + total, length - total );
1316 if (result >= 0)
1318 total += result;
1319 if (total == length)
1321 status = STATUS_SUCCESS;
1322 goto done;
1324 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1326 else if (errno != EAGAIN)
1328 if (errno == EINTR) continue;
1329 if (!total)
1331 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1332 else status = FILE_GetNtStatus();
1334 goto err;
1337 if (async_write)
1339 struct async_fileio_write *fileio;
1341 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), FILE_AsyncWriteService, hFile );
1342 if (!fileio)
1344 status = STATUS_NO_MEMORY;
1345 goto err;
1347 fileio->already = total;
1348 fileio->count = length;
1349 fileio->buffer = buffer;
1351 SERVER_START_REQ( register_async )
1353 req->type = ASYNC_TYPE_WRITE;
1354 req->count = length;
1355 req->async = server_async( hFile, &fileio->io, hEvent, apc, apc_user, io_status );
1356 status = wine_server_call( req );
1358 SERVER_END_REQ;
1360 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1361 goto err;
1363 else /* synchronous write, wait for the fd to become ready */
1365 struct pollfd pfd;
1366 int ret, timeout;
1368 if (!timeout_init_done)
1370 timeout_init_done = TRUE;
1371 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1372 goto err;
1373 if (hEvent) NtResetEvent( hEvent, NULL );
1375 timeout = get_next_io_timeout( &timeouts, total );
1377 pfd.fd = unix_handle;
1378 pfd.events = POLLOUT;
1380 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1382 /* return with what we got so far */
1383 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1384 goto done;
1386 if (ret == -1 && errno != EINTR)
1388 status = FILE_GetNtStatus();
1389 goto done;
1391 /* will now restart the write */
1395 done:
1396 send_completion = cvalue != 0;
1398 err:
1399 if (needs_close) close( unix_handle );
1401 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1402 set_pending_write( hFile );
1404 if (status == STATUS_SUCCESS)
1406 io_status->u.Status = status;
1407 io_status->Information = total;
1408 TRACE("= SUCCESS (%u)\n", total);
1409 if (hEvent) NtSetEvent( hEvent, NULL );
1410 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1411 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1413 else
1415 TRACE("= 0x%08x\n", status);
1416 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1419 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1421 return status;
1425 /******************************************************************************
1426 * NtWriteFileGather [NTDLL.@]
1427 * ZwWriteFileGather [NTDLL.@]
1429 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1430 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1431 ULONG length, PLARGE_INTEGER offset, PULONG key )
1433 int result, unix_handle, needs_close;
1434 unsigned int options;
1435 NTSTATUS status;
1436 ULONG pos = 0, total = 0;
1437 enum server_fd_type type;
1438 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1439 BOOL send_completion = FALSE;
1441 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1442 file, event, apc, apc_user, io_status, segments, length, offset, key);
1444 if (length % page_size) return STATUS_INVALID_PARAMETER;
1445 if (!io_status) return STATUS_ACCESS_VIOLATION;
1447 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1448 &needs_close, &type, &options );
1449 if (status) return status;
1451 if ((type != FD_TYPE_FILE) ||
1452 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1453 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1455 status = STATUS_INVALID_PARAMETER;
1456 goto error;
1459 while (length)
1461 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1462 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1463 page_size - pos, offset->QuadPart + total );
1464 else
1465 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1467 if (result == -1)
1469 if (errno == EINTR) continue;
1470 if (errno == EFAULT)
1472 status = STATUS_INVALID_USER_BUFFER;
1473 goto error;
1475 status = FILE_GetNtStatus();
1476 break;
1478 if (!result)
1480 status = STATUS_DISK_FULL;
1481 break;
1483 total += result;
1484 length -= result;
1485 if ((pos += result) == page_size)
1487 pos = 0;
1488 segments++;
1492 send_completion = cvalue != 0;
1494 error:
1495 if (needs_close) close( unix_handle );
1496 if (status == STATUS_SUCCESS)
1498 io_status->u.Status = status;
1499 io_status->Information = total;
1500 TRACE("= SUCCESS (%u)\n", total);
1501 if (event) NtSetEvent( event, NULL );
1502 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1503 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1505 else
1507 TRACE("= 0x%08x\n", status);
1508 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1511 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1513 return status;
1517 /* do an ioctl call through the server */
1518 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1519 PIO_APC_ROUTINE apc, PVOID apc_context,
1520 IO_STATUS_BLOCK *io, ULONG code,
1521 const void *in_buffer, ULONG in_size,
1522 PVOID out_buffer, ULONG out_size )
1524 struct async_irp *async;
1525 NTSTATUS status;
1526 HANDLE wait_handle;
1527 ULONG options;
1529 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), irp_completion, handle )))
1530 return STATUS_NO_MEMORY;
1531 async->event = event;
1532 async->buffer = out_buffer;
1533 async->size = out_size;
1535 SERVER_START_REQ( ioctl )
1537 req->code = code;
1538 req->async = server_async( handle, &async->io, event, apc, apc_context, io );
1539 wine_server_add_data( req, in_buffer, in_size );
1540 if ((code & 3) != METHOD_BUFFERED)
1541 wine_server_add_data( req, out_buffer, out_size );
1542 wine_server_set_reply( req, out_buffer, out_size );
1543 status = virtual_locked_server_call( req );
1544 wait_handle = wine_server_ptr_handle( reply->wait );
1545 options = reply->options;
1546 if (wait_handle && status != STATUS_PENDING)
1548 io->u.Status = status;
1549 io->Information = wine_server_reply_size( reply );
1552 SERVER_END_REQ;
1554 if (status == STATUS_NOT_SUPPORTED)
1555 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1556 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1558 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1560 if (wait_handle)
1562 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1563 status = io->u.Status;
1566 return status;
1569 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1570 * server */
1571 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1572 ULONG in_size)
1574 #ifdef VALGRIND_MAKE_MEM_DEFINED
1575 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1576 do { \
1577 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1578 if ((size) >= FIELD_OFFSET(t, f2)) \
1579 VALGRIND_MAKE_MEM_DEFINED( \
1580 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1581 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1582 } while (0)
1584 switch (code)
1586 case FSCTL_PIPE_WAIT:
1587 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1588 break;
1590 #endif
1594 /**************************************************************************
1595 * NtDeviceIoControlFile [NTDLL.@]
1596 * ZwDeviceIoControlFile [NTDLL.@]
1598 * Perform an I/O control operation on an open file handle.
1600 * PARAMS
1601 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1602 * event [I] Event to signal upon completion (or NULL)
1603 * apc [I] Callback to call upon completion (or NULL)
1604 * apc_context [I] Context for ApcRoutine (or NULL)
1605 * io [O] Receives information about the operation on return
1606 * code [I] Control code for the operation to perform
1607 * in_buffer [I] Source for any input data required (or NULL)
1608 * in_size [I] Size of InputBuffer
1609 * out_buffer [O] Source for any output data returned (or NULL)
1610 * out_size [I] Size of OutputBuffer
1612 * RETURNS
1613 * Success: 0. IoStatusBlock is updated.
1614 * Failure: An NTSTATUS error code describing the error.
1616 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1617 PIO_APC_ROUTINE apc, PVOID apc_context,
1618 PIO_STATUS_BLOCK io, ULONG code,
1619 PVOID in_buffer, ULONG in_size,
1620 PVOID out_buffer, ULONG out_size)
1622 ULONG device = (code >> 16);
1623 NTSTATUS status = STATUS_NOT_SUPPORTED;
1625 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1626 handle, event, apc, apc_context, io, code,
1627 in_buffer, in_size, out_buffer, out_size);
1629 switch(device)
1631 case FILE_DEVICE_DISK:
1632 case FILE_DEVICE_CD_ROM:
1633 case FILE_DEVICE_DVD:
1634 case FILE_DEVICE_CONTROLLER:
1635 case FILE_DEVICE_MASS_STORAGE:
1636 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1637 in_buffer, in_size, out_buffer, out_size);
1638 break;
1639 case FILE_DEVICE_SERIAL_PORT:
1640 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1641 in_buffer, in_size, out_buffer, out_size);
1642 break;
1643 case FILE_DEVICE_TAPE:
1644 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1645 in_buffer, in_size, out_buffer, out_size);
1646 break;
1649 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1650 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1651 in_buffer, in_size, out_buffer, out_size );
1653 if (status != STATUS_PENDING) io->u.Status = status;
1654 return status;
1658 /**************************************************************************
1659 * NtFsControlFile [NTDLL.@]
1660 * ZwFsControlFile [NTDLL.@]
1662 * Perform a file system control operation on an open file handle.
1664 * PARAMS
1665 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1666 * event [I] Event to signal upon completion (or NULL)
1667 * apc [I] Callback to call upon completion (or NULL)
1668 * apc_context [I] Context for ApcRoutine (or NULL)
1669 * io [O] Receives information about the operation on return
1670 * code [I] Control code for the operation to perform
1671 * in_buffer [I] Source for any input data required (or NULL)
1672 * in_size [I] Size of InputBuffer
1673 * out_buffer [O] Source for any output data returned (or NULL)
1674 * out_size [I] Size of OutputBuffer
1676 * RETURNS
1677 * Success: 0. IoStatusBlock is updated.
1678 * Failure: An NTSTATUS error code describing the error.
1680 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1681 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1682 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1684 NTSTATUS status;
1686 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1687 handle, event, apc, apc_context, io, code,
1688 in_buffer, in_size, out_buffer, out_size);
1690 if (!io) return STATUS_INVALID_PARAMETER;
1692 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1694 switch(code)
1696 case FSCTL_DISMOUNT_VOLUME:
1697 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1698 in_buffer, in_size, out_buffer, out_size );
1699 if (!status) status = DIR_unmount_device( handle );
1700 return status;
1702 case FSCTL_PIPE_DISCONNECT:
1703 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1704 in_buffer, in_size, out_buffer, out_size );
1705 if (!status)
1707 int fd = server_remove_fd_from_cache( handle );
1708 if (fd != -1) close( fd );
1710 return status;
1712 case FSCTL_PIPE_IMPERSONATE:
1713 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1714 status = RtlImpersonateSelf( SecurityImpersonation );
1715 break;
1717 case FSCTL_IS_VOLUME_MOUNTED:
1718 case FSCTL_LOCK_VOLUME:
1719 case FSCTL_UNLOCK_VOLUME:
1720 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1721 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1722 status = STATUS_SUCCESS;
1723 break;
1725 case FSCTL_GET_RETRIEVAL_POINTERS:
1727 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1729 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1731 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1733 buffer->ExtentCount = 1;
1734 buffer->StartingVcn.QuadPart = 1;
1735 buffer->Extents[0].NextVcn.QuadPart = 0;
1736 buffer->Extents[0].Lcn.QuadPart = 0;
1737 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1738 status = STATUS_SUCCESS;
1740 else
1742 io->Information = 0;
1743 status = STATUS_BUFFER_TOO_SMALL;
1745 break;
1747 case FSCTL_SET_SPARSE:
1748 TRACE("FSCTL_SET_SPARSE: Ignoring request\n");
1749 io->Information = 0;
1750 status = STATUS_SUCCESS;
1751 break;
1752 default:
1753 return server_ioctl_file( handle, event, apc, apc_context, io, code,
1754 in_buffer, in_size, out_buffer, out_size );
1757 if (status != STATUS_PENDING) io->u.Status = status;
1758 return status;
1762 struct read_changes_fileio
1764 struct async_fileio io;
1765 void *buffer;
1766 ULONG buffer_size;
1767 ULONG data_size;
1768 char data[1];
1771 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status )
1773 struct read_changes_fileio *fileio = user;
1774 int size = 0;
1776 if (status == STATUS_ALERTED)
1778 SERVER_START_REQ( read_change )
1780 req->handle = wine_server_obj_handle( fileio->io.handle );
1781 wine_server_set_reply( req, fileio->data, fileio->data_size );
1782 status = wine_server_call( req );
1783 size = wine_server_reply_size( reply );
1785 SERVER_END_REQ;
1787 if (status == STATUS_SUCCESS && fileio->buffer)
1789 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1790 int i, left = fileio->buffer_size;
1791 DWORD *last_entry_offset = NULL;
1792 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1794 while (size && left >= sizeof(*pfni))
1796 /* convert to an NT style path */
1797 for (i = 0; i < event->len; i++)
1798 if (event->name[i] == '/') event->name[i] = '\\';
1800 pfni->Action = event->action;
1801 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1802 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1803 last_entry_offset = &pfni->NextEntryOffset;
1805 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1807 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1808 pfni->FileNameLength *= sizeof(WCHAR);
1809 pfni->NextEntryOffset = i;
1810 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1811 left -= i;
1813 i = (offsetof(struct filesystem_event, name[event->len])
1814 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1815 event = (struct filesystem_event*)((char*)event + i);
1816 size -= i;
1819 if (size)
1821 status = STATUS_NOTIFY_ENUM_DIR;
1822 size = 0;
1824 else
1826 if (last_entry_offset) *last_entry_offset = 0;
1827 size = fileio->buffer_size - left;
1830 else
1832 status = STATUS_NOTIFY_ENUM_DIR;
1833 size = 0;
1837 if (status != STATUS_PENDING)
1839 iosb->u.Status = status;
1840 iosb->Information = size;
1841 release_fileio( &fileio->io );
1843 return status;
1846 #define FILE_NOTIFY_ALL ( \
1847 FILE_NOTIFY_CHANGE_FILE_NAME | \
1848 FILE_NOTIFY_CHANGE_DIR_NAME | \
1849 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1850 FILE_NOTIFY_CHANGE_SIZE | \
1851 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1852 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1853 FILE_NOTIFY_CHANGE_CREATION | \
1854 FILE_NOTIFY_CHANGE_SECURITY )
1856 /******************************************************************************
1857 * NtNotifyChangeDirectoryFile [NTDLL.@]
1859 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1860 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1861 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1863 struct read_changes_fileio *fileio;
1864 NTSTATUS status;
1865 ULONG size = max( 4096, buffer_size );
1867 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1868 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1870 if (!iosb) return STATUS_ACCESS_VIOLATION;
1871 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1873 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1874 read_changes_apc, handle );
1875 if (!fileio) return STATUS_NO_MEMORY;
1877 fileio->buffer = buffer;
1878 fileio->buffer_size = buffer_size;
1879 fileio->data_size = size;
1881 SERVER_START_REQ( read_directory_changes )
1883 req->filter = filter;
1884 req->want_data = (buffer != NULL);
1885 req->subtree = subtree;
1886 req->async = server_async( handle, &fileio->io, event, apc, apc_context, iosb );
1887 status = wine_server_call( req );
1889 SERVER_END_REQ;
1891 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1892 return status;
1895 /******************************************************************************
1896 * NtSetVolumeInformationFile [NTDLL.@]
1897 * ZwSetVolumeInformationFile [NTDLL.@]
1899 * Set volume information for an open file handle.
1901 * PARAMS
1902 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1903 * IoStatusBlock [O] Receives information about the operation on return
1904 * FsInformation [I] Source for volume information
1905 * Length [I] Size of FsInformation
1906 * FsInformationClass [I] Type of volume information to set
1908 * RETURNS
1909 * Success: 0. IoStatusBlock is updated.
1910 * Failure: An NTSTATUS error code describing the error.
1912 NTSTATUS WINAPI NtSetVolumeInformationFile(
1913 IN HANDLE FileHandle,
1914 PIO_STATUS_BLOCK IoStatusBlock,
1915 PVOID FsInformation,
1916 ULONG Length,
1917 FS_INFORMATION_CLASS FsInformationClass)
1919 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1920 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1921 return 0;
1924 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1925 static int futimens( int fd, const struct timespec spec[2] )
1927 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1929 #define HAVE_FUTIMENS
1930 #endif /* __ANDROID__ */
1932 #ifndef UTIME_OMIT
1933 #define UTIME_OMIT ((1 << 30) - 2)
1934 #endif
1936 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1938 NTSTATUS status = STATUS_SUCCESS;
1940 #ifdef HAVE_FUTIMENS
1941 struct timespec tv[2];
1943 tv[0].tv_sec = tv[1].tv_sec = 0;
1944 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1945 if (atime->QuadPart)
1947 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1948 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1950 if (mtime->QuadPart)
1952 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1953 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1955 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1957 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1958 struct timeval tv[2];
1959 struct stat st;
1961 if (!atime->QuadPart || !mtime->QuadPart)
1964 tv[0].tv_sec = tv[0].tv_usec = 0;
1965 tv[1].tv_sec = tv[1].tv_usec = 0;
1966 if (!fstat( fd, &st ))
1968 tv[0].tv_sec = st.st_atime;
1969 tv[1].tv_sec = st.st_mtime;
1970 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1971 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1972 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1973 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1974 #endif
1975 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1976 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1977 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1978 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1979 #endif
1982 if (atime->QuadPart)
1984 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1985 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1987 if (mtime->QuadPart)
1989 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1990 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1992 #ifdef HAVE_FUTIMES
1993 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1994 #elif defined(HAVE_FUTIMESAT)
1995 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1996 #endif
1998 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1999 FIXME( "setting file times not supported\n" );
2000 status = STATUS_NOT_IMPLEMENTED;
2001 #endif
2002 return status;
2005 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
2006 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
2008 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
2009 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
2010 RtlSecondsSince1970ToTime( st->st_atime, atime );
2011 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2012 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
2013 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2014 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
2015 #endif
2016 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2017 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2018 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2019 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2020 #endif
2021 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2022 atime->QuadPart += st->st_atim.tv_nsec / 100;
2023 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2024 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2025 #endif
2026 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2027 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2028 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2029 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2030 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2031 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2032 #endif
2033 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2034 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2035 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2036 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2037 #endif
2038 #else
2039 *creation = *mtime;
2040 #endif
2043 /* fill in the file information that depends on the stat and attribute info */
2044 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2045 FILE_INFORMATION_CLASS class )
2047 switch (class)
2049 case FileBasicInformation:
2051 FILE_BASIC_INFORMATION *info = ptr;
2053 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2054 &info->LastAccessTime, &info->CreationTime );
2055 info->FileAttributes = attr;
2057 break;
2058 case FileStandardInformation:
2060 FILE_STANDARD_INFORMATION *info = ptr;
2062 if ((info->Directory = S_ISDIR(st->st_mode)))
2064 info->AllocationSize.QuadPart = 0;
2065 info->EndOfFile.QuadPart = 0;
2066 info->NumberOfLinks = 1;
2068 else
2070 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2071 info->EndOfFile.QuadPart = st->st_size;
2072 info->NumberOfLinks = st->st_nlink;
2075 break;
2076 case FileInternalInformation:
2078 FILE_INTERNAL_INFORMATION *info = ptr;
2079 info->IndexNumber.QuadPart = st->st_ino;
2081 break;
2082 case FileEndOfFileInformation:
2084 FILE_END_OF_FILE_INFORMATION *info = ptr;
2085 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2087 break;
2088 case FileAllInformation:
2090 FILE_ALL_INFORMATION *info = ptr;
2091 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2092 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2093 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2095 break;
2096 /* all directory structures start with the FileDirectoryInformation layout */
2097 case FileBothDirectoryInformation:
2098 case FileFullDirectoryInformation:
2099 case FileDirectoryInformation:
2101 FILE_DIRECTORY_INFORMATION *info = ptr;
2103 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2104 &info->LastAccessTime, &info->CreationTime );
2105 if (S_ISDIR(st->st_mode))
2107 info->AllocationSize.QuadPart = 0;
2108 info->EndOfFile.QuadPart = 0;
2110 else
2112 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2113 info->EndOfFile.QuadPart = st->st_size;
2115 info->FileAttributes = attr;
2117 break;
2118 case FileIdFullDirectoryInformation:
2120 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2121 info->FileId.QuadPart = st->st_ino;
2122 fill_file_info( st, attr, info, FileDirectoryInformation );
2124 break;
2125 case FileIdBothDirectoryInformation:
2127 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2128 info->FileId.QuadPart = st->st_ino;
2129 fill_file_info( st, attr, info, FileDirectoryInformation );
2131 break;
2132 case FileIdGlobalTxDirectoryInformation:
2134 FILE_ID_GLOBAL_TX_DIR_INFORMATION *info = ptr;
2135 info->FileId.QuadPart = st->st_ino;
2136 fill_file_info( st, attr, info, FileDirectoryInformation );
2138 break;
2140 default:
2141 return STATUS_INVALID_INFO_CLASS;
2143 return STATUS_SUCCESS;
2146 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2148 data_size_t size = 1024;
2149 NTSTATUS ret;
2150 char *name;
2152 for (;;)
2154 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2155 if (!name) return STATUS_NO_MEMORY;
2156 unix_name->MaximumLength = size + 1;
2158 SERVER_START_REQ( get_handle_unix_name )
2160 req->handle = wine_server_obj_handle( handle );
2161 wine_server_set_reply( req, name, size );
2162 ret = wine_server_call( req );
2163 size = reply->name_len;
2165 SERVER_END_REQ;
2167 if (!ret)
2169 name[size] = 0;
2170 unix_name->Buffer = name;
2171 unix_name->Length = size;
2172 break;
2174 RtlFreeHeap( GetProcessHeap(), 0, name );
2175 if (ret != STATUS_BUFFER_OVERFLOW) break;
2177 return ret;
2180 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2182 UNICODE_STRING nt_name;
2183 NTSTATUS status;
2185 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2187 const WCHAR *ptr = nt_name.Buffer;
2188 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2190 /* Skip the volume mount point. */
2191 while (ptr != end && *ptr == '\\') ++ptr;
2192 while (ptr != end && *ptr != '\\') ++ptr;
2193 while (ptr != end && *ptr == '\\') ++ptr;
2194 while (ptr != end && *ptr != '\\') ++ptr;
2196 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2197 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2198 else *name_len = info->FileNameLength;
2200 memcpy( info->FileName, ptr, *name_len );
2201 RtlFreeUnicodeString( &nt_name );
2204 return status;
2207 /******************************************************************************
2208 * NtQueryInformationFile [NTDLL.@]
2209 * ZwQueryInformationFile [NTDLL.@]
2211 * Get information about an open file handle.
2213 * PARAMS
2214 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2215 * io [O] Receives information about the operation on return
2216 * ptr [O] Destination for file information
2217 * len [I] Size of FileInformation
2218 * class [I] Type of file information to get
2220 * RETURNS
2221 * Success: 0. IoStatusBlock and FileInformation are updated.
2222 * Failure: An NTSTATUS error code describing the error.
2224 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2225 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2227 static const size_t info_sizes[] =
2230 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2231 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2232 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2233 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2234 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2235 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2236 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2237 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2238 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2239 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2240 0, /* FileLinkInformation */
2241 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2242 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2243 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2244 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2245 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2246 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2247 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2248 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2249 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2250 0, /* FileAlternateNameInformation */
2251 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2252 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2253 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2254 0, /* FilePipeRemoteInformation */
2255 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2256 0, /* FileMailslotSetInformation */
2257 0, /* FileCompressionInformation */
2258 0, /* FileObjectIdInformation */
2259 0, /* FileCompletionInformation */
2260 0, /* FileMoveClusterInformation */
2261 0, /* FileQuotaInformation */
2262 0, /* FileReparsePointInformation */
2263 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2264 0, /* FileAttributeTagInformation */
2265 0, /* FileTrackingInformation */
2266 0, /* FileIdBothDirectoryInformation */
2267 0, /* FileIdFullDirectoryInformation */
2268 0, /* FileValidDataLengthInformation */
2269 0, /* FileShortNameInformation */
2270 0, /* FileIoCompletionNotificationInformation, */
2271 0, /* FileIoStatusBlockRangeInformation */
2272 0, /* FileIoPriorityHintInformation */
2273 0, /* FileSfioReserveInformation */
2274 0, /* FileSfioVolumeInformation */
2275 0, /* FileHardLinkInformation */
2276 0, /* FileProcessIdsUsingFileInformation */
2277 0, /* FileNormalizedNameInformation */
2278 0, /* FileNetworkPhysicalNameInformation */
2279 0, /* FileIdGlobalTxDirectoryInformation */
2280 0, /* FileIsRemoteDeviceInformation */
2281 0, /* FileAttributeCacheInformation */
2282 0, /* FileNumaNodeInformation */
2283 0, /* FileStandardLinkInformation */
2284 0, /* FileRemoteProtocolInformation */
2285 0, /* FileRenameInformationBypassAccessCheck */
2286 0, /* FileLinkInformationBypassAccessCheck */
2287 0, /* FileVolumeNameInformation */
2288 sizeof(FILE_ID_INFORMATION), /* FileIdInformation */
2289 0, /* FileIdExtdDirectoryInformation */
2290 0, /* FileReplaceCompletionInformation */
2291 0, /* FileHardLinkFullIdInformation */
2292 0, /* FileIdExtdBothDirectoryInformation */
2295 struct stat st;
2296 int fd, needs_close = FALSE;
2297 ULONG attr;
2299 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2301 io->Information = 0;
2303 if (class <= 0 || class >= FileMaximumInformation)
2304 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2305 if (!info_sizes[class])
2307 FIXME("Unsupported class (%d)\n", class);
2308 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2310 if (len < info_sizes[class])
2311 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2313 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2315 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2316 return io->u.Status;
2319 switch (class)
2321 case FileBasicInformation:
2322 if (fd_get_file_info( fd, &st, &attr ) == -1)
2323 io->u.Status = FILE_GetNtStatus();
2324 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2325 io->u.Status = STATUS_INVALID_INFO_CLASS;
2326 else
2327 fill_file_info( &st, attr, ptr, class );
2328 break;
2329 case FileStandardInformation:
2331 FILE_STANDARD_INFORMATION *info = ptr;
2333 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2334 else
2336 fill_file_info( &st, attr, info, class );
2337 info->DeletePending = FALSE; /* FIXME */
2340 break;
2341 case FilePositionInformation:
2343 FILE_POSITION_INFORMATION *info = ptr;
2344 off_t res = lseek( fd, 0, SEEK_CUR );
2345 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2346 else info->CurrentByteOffset.QuadPart = res;
2348 break;
2349 case FileInternalInformation:
2350 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2351 else fill_file_info( &st, attr, ptr, class );
2352 break;
2353 case FileEaInformation:
2355 FILE_EA_INFORMATION *info = ptr;
2356 info->EaSize = 0;
2358 break;
2359 case FileAccessInformation:
2361 FILE_ACCESS_INFORMATION *info = ptr;
2362 SERVER_START_REQ( get_object_info )
2364 req->handle = wine_server_obj_handle( hFile );
2365 io->u.Status = wine_server_call( req );
2366 if (io->u.Status == STATUS_SUCCESS)
2367 info->AccessFlags = reply->access;
2369 SERVER_END_REQ;
2371 break;
2372 case FileEndOfFileInformation:
2373 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2374 else fill_file_info( &st, attr, ptr, class );
2375 break;
2376 case FileAllInformation:
2378 FILE_ALL_INFORMATION *info = ptr;
2379 ANSI_STRING unix_name;
2381 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2382 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2383 io->u.Status = STATUS_INVALID_INFO_CLASS;
2384 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2386 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2388 fill_file_info( &st, attr, info, FileAllInformation );
2389 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2390 info->EaInformation.EaSize = 0;
2391 info->AccessInformation.AccessFlags = 0; /* FIXME */
2392 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2393 info->ModeInformation.Mode = 0; /* FIXME */
2394 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2396 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2397 RtlFreeAnsiString( &unix_name );
2398 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2401 break;
2402 case FileMailslotQueryInformation:
2404 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2406 SERVER_START_REQ( set_mailslot_info )
2408 req->handle = wine_server_obj_handle( hFile );
2409 req->flags = 0;
2410 io->u.Status = wine_server_call( req );
2411 if( io->u.Status == STATUS_SUCCESS )
2413 info->MaximumMessageSize = reply->max_msgsize;
2414 info->MailslotQuota = 0;
2415 info->NextMessageSize = 0;
2416 info->MessagesAvailable = 0;
2417 info->ReadTimeout.QuadPart = reply->read_timeout;
2420 SERVER_END_REQ;
2421 if (!io->u.Status)
2423 char *tmpbuf;
2424 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2425 if (size > 0x10000) size = 0x10000;
2426 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2428 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2430 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2431 info->MessagesAvailable = (res > 0);
2432 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2433 if (needs_close) close( fd );
2435 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2439 break;
2440 case FilePipeInformation:
2442 FILE_PIPE_INFORMATION* pi = ptr;
2444 SERVER_START_REQ( get_named_pipe_info )
2446 req->handle = wine_server_obj_handle( hFile );
2447 if (!(io->u.Status = wine_server_call( req )))
2449 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2450 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2451 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2452 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2455 SERVER_END_REQ;
2457 break;
2458 case FilePipeLocalInformation:
2460 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2462 SERVER_START_REQ( get_named_pipe_info )
2464 req->handle = wine_server_obj_handle( hFile );
2465 if (!(io->u.Status = wine_server_call( req )))
2467 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2468 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2469 switch (reply->sharing)
2471 case FILE_SHARE_READ:
2472 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2473 break;
2474 case FILE_SHARE_WRITE:
2475 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2476 break;
2477 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2478 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2479 break;
2481 pli->MaximumInstances = reply->maxinstances;
2482 pli->CurrentInstances = reply->instances;
2483 pli->InboundQuota = reply->insize;
2484 pli->ReadDataAvailable = 0; /* FIXME */
2485 pli->OutboundQuota = reply->outsize;
2486 pli->WriteQuotaAvailable = 0; /* FIXME */
2487 pli->NamedPipeState = 0; /* FIXME */
2488 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2489 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2492 SERVER_END_REQ;
2494 break;
2495 case FileNameInformation:
2497 FILE_NAME_INFORMATION *info = ptr;
2498 ANSI_STRING unix_name;
2500 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2502 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2503 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2504 RtlFreeAnsiString( &unix_name );
2505 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2508 break;
2509 case FileNetworkOpenInformation:
2511 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2512 ANSI_STRING unix_name;
2514 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2516 ULONG attributes;
2517 struct stat st;
2519 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2520 io->u.Status = FILE_GetNtStatus();
2521 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2522 io->u.Status = STATUS_INVALID_INFO_CLASS;
2523 else
2525 FILE_BASIC_INFORMATION basic;
2526 FILE_STANDARD_INFORMATION std;
2528 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2529 fill_file_info( &st, attributes, &std, FileStandardInformation );
2531 info->CreationTime = basic.CreationTime;
2532 info->LastAccessTime = basic.LastAccessTime;
2533 info->LastWriteTime = basic.LastWriteTime;
2534 info->ChangeTime = basic.ChangeTime;
2535 info->AllocationSize = std.AllocationSize;
2536 info->EndOfFile = std.EndOfFile;
2537 info->FileAttributes = basic.FileAttributes;
2539 RtlFreeAnsiString( &unix_name );
2542 break;
2543 case FileIdInformation:
2544 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2545 else
2547 FILE_ID_INFORMATION *info = ptr;
2548 info->VolumeSerialNumber = 0; /* FIXME */
2549 memset( &info->FileId, 0, sizeof(info->FileId) );
2550 *(ULONGLONG *)&info->FileId = st.st_ino;
2552 break;
2553 default:
2554 FIXME("Unsupported class (%d)\n", class);
2555 io->u.Status = STATUS_NOT_IMPLEMENTED;
2556 break;
2558 if (needs_close) close( fd );
2559 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2560 return io->u.Status;
2563 /******************************************************************************
2564 * NtSetInformationFile [NTDLL.@]
2565 * ZwSetInformationFile [NTDLL.@]
2567 * Set information about an open file handle.
2569 * PARAMS
2570 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2571 * io [O] Receives information about the operation on return
2572 * ptr [I] Source for file information
2573 * len [I] Size of FileInformation
2574 * class [I] Type of file information to set
2576 * RETURNS
2577 * Success: 0. io is updated.
2578 * Failure: An NTSTATUS error code describing the error.
2580 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2581 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2583 int fd, needs_close;
2585 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2587 io->u.Status = STATUS_SUCCESS;
2588 switch (class)
2590 case FileBasicInformation:
2591 if (len >= sizeof(FILE_BASIC_INFORMATION))
2593 struct stat st;
2594 const FILE_BASIC_INFORMATION *info = ptr;
2596 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2597 return io->u.Status;
2599 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2600 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2602 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2604 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2605 else
2607 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2609 if (S_ISDIR( st.st_mode))
2610 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2611 else
2612 st.st_mode &= ~0222; /* clear write permission bits */
2614 else
2616 /* add write permission only where we already have read permission */
2617 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2619 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2623 if (needs_close) close( fd );
2625 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2626 break;
2628 case FilePositionInformation:
2629 if (len >= sizeof(FILE_POSITION_INFORMATION))
2631 const FILE_POSITION_INFORMATION *info = ptr;
2633 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2634 return io->u.Status;
2636 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2637 io->u.Status = FILE_GetNtStatus();
2639 if (needs_close) close( fd );
2641 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2642 break;
2644 case FileEndOfFileInformation:
2645 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2647 struct stat st;
2648 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2650 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2651 return io->u.Status;
2653 /* first try normal truncate */
2654 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2656 /* now check for the need to extend the file */
2657 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2659 static const char zero;
2661 /* extend the file one byte beyond the requested size and then truncate it */
2662 /* this should work around ftruncate implementations that can't extend files */
2663 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2664 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2666 io->u.Status = FILE_GetNtStatus();
2668 if (needs_close) close( fd );
2670 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2671 break;
2673 case FilePipeInformation:
2674 if (len >= sizeof(FILE_PIPE_INFORMATION))
2676 FILE_PIPE_INFORMATION *info = ptr;
2678 if ((info->CompletionMode | info->ReadMode) & ~1)
2680 io->u.Status = STATUS_INVALID_PARAMETER;
2681 break;
2684 SERVER_START_REQ( set_named_pipe_info )
2686 req->handle = wine_server_obj_handle( handle );
2687 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2688 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2689 io->u.Status = wine_server_call( req );
2691 SERVER_END_REQ;
2693 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2694 break;
2696 case FileMailslotSetInformation:
2698 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2700 SERVER_START_REQ( set_mailslot_info )
2702 req->handle = wine_server_obj_handle( handle );
2703 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2704 req->read_timeout = info->ReadTimeout.QuadPart;
2705 io->u.Status = wine_server_call( req );
2707 SERVER_END_REQ;
2709 break;
2711 case FileCompletionInformation:
2712 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2714 FILE_COMPLETION_INFORMATION *info = ptr;
2716 SERVER_START_REQ( set_completion_info )
2718 req->handle = wine_server_obj_handle( handle );
2719 req->chandle = wine_server_obj_handle( info->CompletionPort );
2720 req->ckey = info->CompletionKey;
2721 io->u.Status = wine_server_call( req );
2723 SERVER_END_REQ;
2724 } else
2725 io->u.Status = STATUS_INVALID_PARAMETER_3;
2726 break;
2728 case FileAllInformation:
2729 io->u.Status = STATUS_INVALID_INFO_CLASS;
2730 break;
2732 case FileValidDataLengthInformation:
2733 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2735 struct stat st;
2736 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2738 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2739 return io->u.Status;
2741 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2742 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2743 io->u.Status = STATUS_INVALID_PARAMETER;
2744 else
2746 #ifdef HAVE_FALLOCATE
2747 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2749 NTSTATUS status = FILE_GetNtStatus();
2750 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2751 else io->u.Status = status;
2753 #else
2754 FIXME( "setting valid data length not supported\n" );
2755 #endif
2757 if (needs_close) close( fd );
2759 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2760 break;
2762 case FileDispositionInformation:
2763 if (len >= sizeof(FILE_DISPOSITION_INFORMATION))
2765 FILE_DISPOSITION_INFORMATION *info = ptr;
2767 SERVER_START_REQ( set_fd_disp_info )
2769 req->handle = wine_server_obj_handle( handle );
2770 req->unlink = info->DoDeleteFile;
2771 io->u.Status = wine_server_call( req );
2773 SERVER_END_REQ;
2774 } else
2775 io->u.Status = STATUS_INVALID_PARAMETER_3;
2776 break;
2778 case FileRenameInformation:
2779 if (len >= sizeof(FILE_RENAME_INFORMATION))
2781 FILE_RENAME_INFORMATION *info = ptr;
2782 UNICODE_STRING name_str;
2783 OBJECT_ATTRIBUTES attr;
2784 ANSI_STRING unix_name;
2786 name_str.Buffer = info->FileName;
2787 name_str.Length = info->FileNameLength;
2788 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2790 attr.Length = sizeof(attr);
2791 attr.ObjectName = &name_str;
2792 attr.RootDirectory = info->RootDir;
2793 attr.Attributes = OBJ_CASE_INSENSITIVE;
2795 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2796 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2797 break;
2799 if (!info->Replace && io->u.Status == STATUS_SUCCESS)
2801 RtlFreeAnsiString( &unix_name );
2802 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2803 break;
2806 SERVER_START_REQ( set_fd_name_info )
2808 req->handle = wine_server_obj_handle( handle );
2809 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2810 req->link = FALSE;
2811 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2812 io->u.Status = wine_server_call( req );
2814 SERVER_END_REQ;
2816 RtlFreeAnsiString( &unix_name );
2818 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2819 break;
2821 case FileLinkInformation:
2822 if (len >= sizeof(FILE_LINK_INFORMATION))
2824 FILE_LINK_INFORMATION *info = ptr;
2825 UNICODE_STRING name_str;
2826 OBJECT_ATTRIBUTES attr;
2827 ANSI_STRING unix_name;
2829 name_str.Buffer = info->FileName;
2830 name_str.Length = info->FileNameLength;
2831 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2833 attr.Length = sizeof(attr);
2834 attr.ObjectName = &name_str;
2835 attr.RootDirectory = info->RootDirectory;
2836 attr.Attributes = OBJ_CASE_INSENSITIVE;
2838 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2839 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2840 break;
2842 if (!info->ReplaceIfExists && io->u.Status == STATUS_SUCCESS)
2844 RtlFreeAnsiString( &unix_name );
2845 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2846 break;
2849 SERVER_START_REQ( set_fd_name_info )
2851 req->handle = wine_server_obj_handle( handle );
2852 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2853 req->link = TRUE;
2854 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2855 io->u.Status = wine_server_call( req );
2857 SERVER_END_REQ;
2859 RtlFreeAnsiString( &unix_name );
2861 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2862 break;
2864 default:
2865 FIXME("Unsupported class (%d)\n", class);
2866 io->u.Status = STATUS_NOT_IMPLEMENTED;
2867 break;
2869 io->Information = 0;
2870 return io->u.Status;
2874 /******************************************************************************
2875 * NtQueryFullAttributesFile (NTDLL.@)
2877 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2878 FILE_NETWORK_OPEN_INFORMATION *info )
2880 ANSI_STRING unix_name;
2881 NTSTATUS status;
2883 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2885 ULONG attributes;
2886 struct stat st;
2888 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2889 status = FILE_GetNtStatus();
2890 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2891 status = STATUS_INVALID_INFO_CLASS;
2892 else
2894 FILE_BASIC_INFORMATION basic;
2895 FILE_STANDARD_INFORMATION std;
2897 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2898 fill_file_info( &st, attributes, &std, FileStandardInformation );
2900 info->CreationTime = basic.CreationTime;
2901 info->LastAccessTime = basic.LastAccessTime;
2902 info->LastWriteTime = basic.LastWriteTime;
2903 info->ChangeTime = basic.ChangeTime;
2904 info->AllocationSize = std.AllocationSize;
2905 info->EndOfFile = std.EndOfFile;
2906 info->FileAttributes = basic.FileAttributes;
2907 if (DIR_is_hidden_file( attr->ObjectName ))
2908 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2910 RtlFreeAnsiString( &unix_name );
2912 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2913 return status;
2917 /******************************************************************************
2918 * NtQueryAttributesFile (NTDLL.@)
2919 * ZwQueryAttributesFile (NTDLL.@)
2921 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2923 ANSI_STRING unix_name;
2924 NTSTATUS status;
2926 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2928 ULONG attributes;
2929 struct stat st;
2931 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2932 status = FILE_GetNtStatus();
2933 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2934 status = STATUS_INVALID_INFO_CLASS;
2935 else
2937 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2938 if (DIR_is_hidden_file( attr->ObjectName ))
2939 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2941 RtlFreeAnsiString( &unix_name );
2943 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2944 return status;
2948 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2949 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2950 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2951 unsigned int flags )
2953 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2955 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2956 /* Don't assume read-only, let the mount options set it below */
2957 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2959 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2960 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2962 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2963 info->Characteristics |= FILE_REMOTE_DEVICE;
2965 else if (!strcmp("procfs", fstypename))
2966 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2967 else
2968 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2970 if (flags & MNT_RDONLY)
2971 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2973 if (!(flags & MNT_LOCAL))
2975 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2976 info->Characteristics |= FILE_REMOTE_DEVICE;
2979 #endif
2981 static inline BOOL is_device_placeholder( int fd )
2983 static const char wine_placeholder[] = "Wine device placeholder";
2984 char buffer[sizeof(wine_placeholder)-1];
2986 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2987 return FALSE;
2988 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2991 /******************************************************************************
2992 * get_device_info
2994 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2996 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2998 struct stat st;
3000 info->Characteristics = 0;
3001 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
3002 if (S_ISCHR( st.st_mode ))
3004 info->DeviceType = FILE_DEVICE_UNKNOWN;
3005 #ifdef linux
3006 switch(major(st.st_rdev))
3008 case MEM_MAJOR:
3009 info->DeviceType = FILE_DEVICE_NULL;
3010 break;
3011 case TTY_MAJOR:
3012 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
3013 break;
3014 case LP_MAJOR:
3015 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
3016 break;
3017 case SCSI_TAPE_MAJOR:
3018 info->DeviceType = FILE_DEVICE_TAPE;
3019 break;
3021 #endif
3023 else if (S_ISBLK( st.st_mode ))
3025 info->DeviceType = FILE_DEVICE_DISK;
3027 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
3029 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
3031 else if (is_device_placeholder( fd ))
3033 info->DeviceType = FILE_DEVICE_DISK;
3035 else /* regular file or directory */
3037 #if defined(linux) && defined(HAVE_FSTATFS)
3038 struct statfs stfs;
3040 /* check for floppy disk */
3041 if (major(st.st_dev) == FLOPPY_MAJOR)
3042 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3044 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
3045 switch (stfs.f_type)
3047 case 0x9660: /* iso9660 */
3048 case 0x9fa1: /* supermount */
3049 case 0x15013346: /* udf */
3050 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3051 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3052 break;
3053 case 0x6969: /* nfs */
3054 case 0xff534d42: /* cifs */
3055 case 0xfe534d42: /* smb2 */
3056 case 0x517b: /* smbfs */
3057 case 0x564c: /* ncpfs */
3058 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3059 info->Characteristics |= FILE_REMOTE_DEVICE;
3060 break;
3061 case 0x01021994: /* tmpfs */
3062 case 0x28cd3d45: /* cramfs */
3063 case 0x1373: /* devfs */
3064 case 0x9fa0: /* procfs */
3065 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3066 break;
3067 default:
3068 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3069 break;
3071 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3072 struct statfs stfs;
3074 if (fstatfs( fd, &stfs ) < 0)
3075 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3076 else
3077 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
3078 #elif defined(__NetBSD__)
3079 struct statvfs stfs;
3081 if (fstatvfs( fd, &stfs) < 0)
3082 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3083 else
3084 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
3085 #elif defined(sun)
3086 /* Use dkio to work out device types */
3088 # include <sys/dkio.h>
3089 # include <sys/vtoc.h>
3090 struct dk_cinfo dkinf;
3091 int retval = ioctl(fd, DKIOCINFO, &dkinf);
3092 if(retval==-1){
3093 WARN("Unable to get disk device type information - assuming a disk like device\n");
3094 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3096 switch (dkinf.dki_ctype)
3098 case DKC_CDROM:
3099 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3100 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3101 break;
3102 case DKC_NCRFLOPPY:
3103 case DKC_SMSFLOPPY:
3104 case DKC_INTEL82072:
3105 case DKC_INTEL82077:
3106 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3107 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3108 break;
3109 case DKC_MD:
3110 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3111 break;
3112 default:
3113 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3116 #else
3117 static int warned;
3118 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3119 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3120 #endif
3121 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3123 return STATUS_SUCCESS;
3127 /******************************************************************************
3128 * NtQueryVolumeInformationFile [NTDLL.@]
3129 * ZwQueryVolumeInformationFile [NTDLL.@]
3131 * Get volume information for an open file handle.
3133 * PARAMS
3134 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3135 * io [O] Receives information about the operation on return
3136 * buffer [O] Destination for volume information
3137 * length [I] Size of FsInformation
3138 * info_class [I] Type of volume information to set
3140 * RETURNS
3141 * Success: 0. io and buffer are updated.
3142 * Failure: An NTSTATUS error code describing the error.
3144 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3145 PVOID buffer, ULONG length,
3146 FS_INFORMATION_CLASS info_class )
3148 int fd, needs_close;
3149 struct stat st;
3150 static int once;
3152 io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL );
3153 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
3155 SERVER_START_REQ( get_volume_info )
3157 req->handle = wine_server_obj_handle( handle );
3158 req->info_class = info_class;
3159 wine_server_set_reply( req, buffer, length );
3160 io->u.Status = wine_server_call( req );
3161 if (!io->u.Status) io->Information = wine_server_reply_size( reply );
3163 SERVER_END_REQ;
3164 return io->u.Status;
3166 else if (io->u.Status) return io->u.Status;
3168 io->u.Status = STATUS_NOT_IMPLEMENTED;
3169 io->Information = 0;
3171 switch( info_class )
3173 case FileFsVolumeInformation:
3174 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3175 break;
3176 case FileFsLabelInformation:
3177 FIXME( "%p: label info not supported\n", handle );
3178 break;
3179 case FileFsSizeInformation:
3180 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3181 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3182 else
3184 FILE_FS_SIZE_INFORMATION *info = buffer;
3186 if (fstat( fd, &st ) < 0)
3188 io->u.Status = FILE_GetNtStatus();
3189 break;
3191 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3193 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3195 else
3197 ULONGLONG bsize;
3198 /* Linux's fstatvfs is buggy */
3199 #if !defined(linux) || !defined(HAVE_FSTATFS)
3200 struct statvfs stfs;
3202 if (fstatvfs( fd, &stfs ) < 0)
3204 io->u.Status = FILE_GetNtStatus();
3205 break;
3207 bsize = stfs.f_frsize;
3208 #else
3209 struct statfs stfs;
3210 if (fstatfs( fd, &stfs ) < 0)
3212 io->u.Status = FILE_GetNtStatus();
3213 break;
3215 bsize = stfs.f_bsize;
3216 #endif
3217 if (bsize == 2048) /* assume CD-ROM */
3219 info->BytesPerSector = 2048;
3220 info->SectorsPerAllocationUnit = 1;
3222 else
3224 info->BytesPerSector = 512;
3225 info->SectorsPerAllocationUnit = 8;
3227 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3228 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3229 io->Information = sizeof(*info);
3230 io->u.Status = STATUS_SUCCESS;
3233 break;
3234 case FileFsDeviceInformation:
3235 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3236 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3237 else
3239 FILE_FS_DEVICE_INFORMATION *info = buffer;
3241 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3242 io->Information = sizeof(*info);
3244 break;
3245 case FileFsAttributeInformation:
3246 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3247 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3248 else
3250 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3252 FIXME( "%p: faking attribute info\n", handle );
3253 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3254 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3255 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3256 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3257 info->FileSystemNameLength = sizeof(ntfsW);
3258 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3260 io->Information = sizeof(*info);
3261 io->u.Status = STATUS_SUCCESS;
3263 break;
3264 case FileFsControlInformation:
3265 FIXME( "%p: control info not supported\n", handle );
3266 break;
3267 case FileFsFullSizeInformation:
3268 FIXME( "%p: full size info not supported\n", handle );
3269 break;
3270 case FileFsObjectIdInformation:
3271 FIXME( "%p: object id info not supported\n", handle );
3272 break;
3273 case FileFsMaximumInformation:
3274 FIXME( "%p: maximum info not supported\n", handle );
3275 break;
3276 default:
3277 io->u.Status = STATUS_INVALID_PARAMETER;
3278 break;
3280 if (needs_close) close( fd );
3281 return io->u.Status;
3285 /******************************************************************
3286 * NtQueryEaFile (NTDLL.@)
3288 * Read extended attributes from NTFS files.
3290 * PARAMS
3291 * hFile [I] File handle, must be opened with FILE_READ_EA access
3292 * iosb [O] Receives information about the operation on return
3293 * buffer [O] Output buffer
3294 * length [I] Length of output buffer
3295 * single_entry [I] Only read and return one entry
3296 * ea_list [I] Optional list with names of EAs to return
3297 * ea_list_len [I] Length of ea_list in bytes
3298 * ea_index [I] Optional pointer to 1-based index of attribute to return
3299 * restart [I] restart EA scan
3301 * RETURNS
3302 * Success: 0. Atrributes read into buffer
3303 * Failure: An NTSTATUS error code describing the error.
3305 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3306 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3307 PULONG ea_index, BOOLEAN restart )
3309 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3310 hFile, iosb, buffer, length, single_entry, ea_list,
3311 ea_list_len, ea_index, restart);
3312 return STATUS_ACCESS_DENIED;
3316 /******************************************************************
3317 * NtSetEaFile (NTDLL.@)
3319 * Update extended attributes for NTFS files.
3321 * PARAMS
3322 * hFile [I] File handle, must be opened with FILE_READ_EA access
3323 * iosb [O] Receives information about the operation on return
3324 * buffer [I] Buffer with EA information
3325 * length [I] Length of buffer
3327 * RETURNS
3328 * Success: 0. Attributes are updated
3329 * Failure: An NTSTATUS error code describing the error.
3331 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3333 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3334 return STATUS_ACCESS_DENIED;
3338 /******************************************************************
3339 * NtFlushBuffersFile (NTDLL.@)
3341 * Flush any buffered data on an open file handle.
3343 * PARAMS
3344 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3345 * IoStatusBlock [O] Receives information about the operation on return
3347 * RETURNS
3348 * Success: 0. IoStatusBlock is updated.
3349 * Failure: An NTSTATUS error code describing the error.
3351 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3353 NTSTATUS ret;
3354 HANDLE hEvent = NULL;
3355 enum server_fd_type type;
3356 int fd, needs_close;
3358 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3359 if (ret == STATUS_ACCESS_DENIED)
3360 ret = server_get_unix_fd( hFile, FILE_APPEND_DATA, &fd, &needs_close, &type, NULL );
3362 if (!ret && type == FD_TYPE_SERIAL)
3364 ret = COMM_FlushBuffersFile( fd );
3366 else if (ret != STATUS_ACCESS_DENIED)
3368 SERVER_START_REQ( flush )
3370 req->async = server_async( hFile, NULL, NULL, NULL, NULL, IoStatusBlock );
3371 ret = wine_server_call( req );
3372 hEvent = wine_server_ptr_handle( reply->event );
3374 SERVER_END_REQ;
3376 if (hEvent)
3378 NtWaitForSingleObject( hEvent, FALSE, NULL );
3379 ret = STATUS_SUCCESS;
3383 if (needs_close) close( fd );
3384 return ret;
3387 /******************************************************************
3388 * NtLockFile (NTDLL.@)
3392 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3393 PIO_APC_ROUTINE apc, void* apc_user,
3394 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3395 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3396 BOOLEAN exclusive )
3398 NTSTATUS ret;
3399 HANDLE handle;
3400 BOOLEAN async;
3401 static BOOLEAN warn = TRUE;
3403 if (apc || io_status || key)
3405 FIXME("Unimplemented yet parameter\n");
3406 return STATUS_NOT_IMPLEMENTED;
3409 if (apc_user && warn)
3411 FIXME("I/O completion on lock not implemented yet\n");
3412 warn = FALSE;
3415 for (;;)
3417 SERVER_START_REQ( lock_file )
3419 req->handle = wine_server_obj_handle( hFile );
3420 req->offset = offset->QuadPart;
3421 req->count = count->QuadPart;
3422 req->shared = !exclusive;
3423 req->wait = !dont_wait;
3424 ret = wine_server_call( req );
3425 handle = wine_server_ptr_handle( reply->handle );
3426 async = reply->overlapped;
3428 SERVER_END_REQ;
3429 if (ret != STATUS_PENDING)
3431 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3432 return ret;
3435 if (async)
3437 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3438 if (handle) NtClose( handle );
3439 return STATUS_PENDING;
3441 if (handle)
3443 NtWaitForSingleObject( handle, FALSE, NULL );
3444 NtClose( handle );
3446 else
3448 LARGE_INTEGER time;
3450 /* Unix lock conflict, sleep a bit and retry */
3451 time.QuadPart = 100 * (ULONGLONG)10000;
3452 time.QuadPart = -time.QuadPart;
3453 NtDelayExecution( FALSE, &time );
3459 /******************************************************************
3460 * NtUnlockFile (NTDLL.@)
3464 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3465 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3466 PULONG key )
3468 NTSTATUS status;
3470 TRACE( "%p %x%08x %x%08x\n",
3471 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3473 if (io_status || key)
3475 FIXME("Unimplemented yet parameter\n");
3476 return STATUS_NOT_IMPLEMENTED;
3479 SERVER_START_REQ( unlock_file )
3481 req->handle = wine_server_obj_handle( hFile );
3482 req->offset = offset->QuadPart;
3483 req->count = count->QuadPart;
3484 status = wine_server_call( req );
3486 SERVER_END_REQ;
3487 return status;
3490 /******************************************************************
3491 * NtCreateNamedPipeFile (NTDLL.@)
3495 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3496 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3497 ULONG sharing, ULONG dispo, ULONG options,
3498 ULONG pipe_type, ULONG read_mode,
3499 ULONG completion_mode, ULONG max_inst,
3500 ULONG inbound_quota, ULONG outbound_quota,
3501 PLARGE_INTEGER timeout)
3503 NTSTATUS status;
3504 data_size_t len;
3505 struct object_attributes *objattr;
3507 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3508 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3509 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3510 outbound_quota, timeout);
3512 if (!attr) return STATUS_INVALID_PARAMETER;
3514 /* assume we only get relative timeout */
3515 if (timeout->QuadPart > 0)
3516 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3518 if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
3520 SERVER_START_REQ( create_named_pipe )
3522 req->access = access;
3523 req->options = options;
3524 req->sharing = sharing;
3525 req->flags =
3526 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3527 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3528 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3529 req->maxinstances = max_inst;
3530 req->outsize = outbound_quota;
3531 req->insize = inbound_quota;
3532 req->timeout = timeout->QuadPart;
3533 wine_server_add_data( req, objattr, len );
3534 status = wine_server_call( req );
3535 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3537 SERVER_END_REQ;
3539 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3540 return status;
3543 /******************************************************************
3544 * NtDeleteFile (NTDLL.@)
3548 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3550 NTSTATUS status;
3551 HANDLE hFile;
3552 IO_STATUS_BLOCK io;
3554 TRACE("%p\n", ObjectAttributes);
3555 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3556 ObjectAttributes, &io, NULL, 0,
3557 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3558 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3559 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3560 return status;
3563 /******************************************************************
3564 * NtCancelIoFileEx (NTDLL.@)
3568 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3570 TRACE("%p %p %p\n", hFile, iosb, io_status );
3572 SERVER_START_REQ( cancel_async )
3574 req->handle = wine_server_obj_handle( hFile );
3575 req->iosb = wine_server_client_ptr( iosb );
3576 req->only_thread = FALSE;
3577 io_status->u.Status = wine_server_call( req );
3579 SERVER_END_REQ;
3581 return io_status->u.Status;
3584 /******************************************************************
3585 * NtCancelIoFile (NTDLL.@)
3589 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3591 TRACE("%p %p\n", hFile, io_status );
3593 SERVER_START_REQ( cancel_async )
3595 req->handle = wine_server_obj_handle( hFile );
3596 req->iosb = 0;
3597 req->only_thread = TRUE;
3598 io_status->u.Status = wine_server_call( req );
3600 SERVER_END_REQ;
3602 return io_status->u.Status;
3605 /******************************************************************************
3606 * NtCreateMailslotFile [NTDLL.@]
3607 * ZwCreateMailslotFile [NTDLL.@]
3609 * PARAMS
3610 * pHandle [O] pointer to receive the handle created
3611 * DesiredAccess [I] access mode (read, write, etc)
3612 * ObjectAttributes [I] fully qualified NT path of the mailslot
3613 * IoStatusBlock [O] receives completion status and other info
3614 * CreateOptions [I]
3615 * MailslotQuota [I]
3616 * MaxMessageSize [I]
3617 * TimeOut [I]
3619 * RETURNS
3620 * An NT status code
3622 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3623 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3624 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3625 PLARGE_INTEGER TimeOut)
3627 LARGE_INTEGER timeout;
3628 NTSTATUS ret;
3629 data_size_t len;
3630 struct object_attributes *objattr;
3632 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3633 pHandle, DesiredAccess, attr, IoStatusBlock,
3634 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3636 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3637 if (!attr) return STATUS_INVALID_PARAMETER;
3639 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3642 * For a NULL TimeOut pointer set the default timeout value
3644 if (!TimeOut)
3645 timeout.QuadPart = -1;
3646 else
3647 timeout.QuadPart = TimeOut->QuadPart;
3649 SERVER_START_REQ( create_mailslot )
3651 req->access = DesiredAccess;
3652 req->max_msgsize = MaxMessageSize;
3653 req->read_timeout = timeout.QuadPart;
3654 wine_server_add_data( req, objattr, len );
3655 ret = wine_server_call( req );
3656 if( ret == STATUS_SUCCESS )
3657 *pHandle = wine_server_ptr_handle( reply->handle );
3659 SERVER_END_REQ;
3661 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3662 return ret;