hid: Stub HidP_TranslateUsagesToI8042ScanCodes.
[wine.git] / dlls / ntdll / file.c
blob7fbde5095d66d6e1e3bdd10c8d216d53b41d323c
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 struct async_fileio
351 struct async_fileio *next;
352 HANDLE handle;
353 PIO_APC_ROUTINE apc;
354 void *apc_arg;
357 struct async_fileio_read
359 struct async_fileio io;
360 char* buffer;
361 unsigned int already;
362 unsigned int count;
363 BOOL avail_mode;
366 struct async_fileio_write
368 struct async_fileio io;
369 const char *buffer;
370 unsigned int already;
371 unsigned int count;
374 struct async_irp
376 struct async_fileio io;
377 HANDLE event; /* async event */
378 void *buffer; /* buffer for output */
379 ULONG size; /* size of buffer */
382 static struct async_fileio *fileio_freelist;
384 static void release_fileio( struct async_fileio *io )
386 for (;;)
388 struct async_fileio *next = fileio_freelist;
389 io->next = next;
390 if (interlocked_cmpxchg_ptr( (void **)&fileio_freelist, io, next ) == next) return;
394 static struct async_fileio *alloc_fileio( DWORD size, HANDLE handle, PIO_APC_ROUTINE apc, void *arg )
396 /* first free remaining previous fileinfos */
398 struct async_fileio *io = interlocked_xchg_ptr( (void **)&fileio_freelist, NULL );
400 while (io)
402 struct async_fileio *next = io->next;
403 RtlFreeHeap( GetProcessHeap(), 0, io );
404 io = next;
407 if ((io = RtlAllocateHeap( GetProcessHeap(), 0, size )))
409 io->handle = handle;
410 io->apc = apc;
411 io->apc_arg = arg;
413 return io;
416 /* callback for irp async I/O completion */
417 static NTSTATUS irp_completion( void *user, IO_STATUS_BLOCK *io, NTSTATUS status, void **apc, void **arg )
419 struct async_irp *async = user;
421 if (status == STATUS_ALERTED)
423 SERVER_START_REQ( get_irp_result )
425 req->handle = wine_server_obj_handle( async->io.handle );
426 req->user_arg = wine_server_client_ptr( async );
427 wine_server_set_reply( req, async->buffer, async->size );
428 status = wine_server_call( req );
429 if (status != STATUS_PENDING) io->Information = reply->size;
431 SERVER_END_REQ;
433 if (status != STATUS_PENDING)
435 io->u.Status = status;
436 *apc = async->io.apc;
437 *arg = async->io.apc_arg;
438 release_fileio( &async->io );
440 return status;
443 /***********************************************************************
444 * FILE_GetNtStatus(void)
446 * Retrieve the Nt Status code from errno.
447 * Try to be consistent with FILE_SetDosError().
449 NTSTATUS FILE_GetNtStatus(void)
451 int err = errno;
453 TRACE( "errno = %d\n", errno );
454 switch (err)
456 case EAGAIN: return STATUS_SHARING_VIOLATION;
457 case EBADF: return STATUS_INVALID_HANDLE;
458 case EBUSY: return STATUS_DEVICE_BUSY;
459 case ENOSPC: return STATUS_DISK_FULL;
460 case EPERM:
461 case EROFS:
462 case EACCES: return STATUS_ACCESS_DENIED;
463 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
464 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
465 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
466 case EMFILE:
467 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
468 case EINVAL: return STATUS_INVALID_PARAMETER;
469 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
470 case EPIPE: return STATUS_PIPE_DISCONNECTED;
471 case EIO: return STATUS_DEVICE_NOT_READY;
472 #ifdef ENOMEDIUM
473 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
474 #endif
475 case ENXIO: return STATUS_NO_SUCH_DEVICE;
476 case ENOTTY:
477 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
478 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
479 case EFAULT: return STATUS_ACCESS_VIOLATION;
480 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
481 #ifdef ETIME /* Missing on FreeBSD */
482 case ETIME: return STATUS_IO_TIMEOUT;
483 #endif
484 case ENOEXEC: /* ?? */
485 case EEXIST: /* ?? */
486 default:
487 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
488 return STATUS_UNSUCCESSFUL;
492 /***********************************************************************
493 * FILE_AsyncReadService (INTERNAL)
495 static NTSTATUS FILE_AsyncReadService( void *user, IO_STATUS_BLOCK *iosb,
496 NTSTATUS status, void **apc, void **arg )
498 struct async_fileio_read *fileio = user;
499 int fd, needs_close, result;
501 switch (status)
503 case STATUS_ALERTED: /* got some new data */
504 /* check to see if the data is ready (non-blocking) */
505 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
506 &needs_close, NULL, NULL )))
507 break;
509 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
510 if (needs_close) close( fd );
512 if (result < 0)
514 if (errno == EAGAIN || errno == EINTR)
515 status = STATUS_PENDING;
516 else /* check to see if the transfer is complete */
517 status = FILE_GetNtStatus();
519 else if (result == 0)
521 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
523 else
525 fileio->already += result;
526 if (fileio->already >= fileio->count || fileio->avail_mode)
527 status = STATUS_SUCCESS;
528 else
529 status = STATUS_PENDING;
531 break;
533 case STATUS_TIMEOUT:
534 case STATUS_IO_TIMEOUT:
535 if (fileio->already) status = STATUS_SUCCESS;
536 break;
538 if (status != STATUS_PENDING)
540 iosb->u.Status = status;
541 iosb->Information = fileio->already;
542 *apc = fileio->io.apc;
543 *arg = fileio->io.apc_arg;
544 release_fileio( &fileio->io );
546 return status;
549 /* do a read call through the server */
550 static NTSTATUS server_read_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
551 IO_STATUS_BLOCK *io, void *buffer, ULONG size,
552 LARGE_INTEGER *offset, ULONG *key )
554 struct async_irp *async;
555 NTSTATUS status;
556 HANDLE wait_handle;
557 ULONG options;
558 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
560 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), handle, apc, apc_context )))
561 return STATUS_NO_MEMORY;
563 async->event = event;
564 async->buffer = buffer;
565 async->size = size;
567 SERVER_START_REQ( read )
569 req->blocking = !apc && !event && !cvalue;
570 req->async.handle = wine_server_obj_handle( handle );
571 req->async.callback = wine_server_client_ptr( irp_completion );
572 req->async.iosb = wine_server_client_ptr( io );
573 req->async.arg = wine_server_client_ptr( async );
574 req->async.event = wine_server_obj_handle( event );
575 req->async.cvalue = cvalue;
576 req->pos = offset ? offset->QuadPart : 0;
577 wine_server_set_reply( req, buffer, size );
578 status = wine_server_call( req );
579 wait_handle = wine_server_ptr_handle( reply->wait );
580 options = reply->options;
581 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
583 SERVER_END_REQ;
585 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
587 if (wait_handle)
589 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
590 status = io->u.Status;
591 NtClose( wait_handle );
594 return status;
597 /* do a write call through the server */
598 static NTSTATUS server_write_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
599 IO_STATUS_BLOCK *io, const void *buffer, ULONG size,
600 LARGE_INTEGER *offset, ULONG *key )
602 struct async_irp *async;
603 NTSTATUS status;
604 HANDLE wait_handle;
605 ULONG options;
606 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
608 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), handle, apc, apc_context )))
609 return STATUS_NO_MEMORY;
611 async->event = event;
612 async->buffer = NULL;
613 async->size = 0;
615 SERVER_START_REQ( write )
617 req->blocking = !apc && !event && !cvalue;
618 req->async.handle = wine_server_obj_handle( handle );
619 req->async.callback = wine_server_client_ptr( irp_completion );
620 req->async.iosb = wine_server_client_ptr( io );
621 req->async.arg = wine_server_client_ptr( async );
622 req->async.event = wine_server_obj_handle( event );
623 req->async.cvalue = cvalue;
624 req->pos = offset ? offset->QuadPart : 0;
625 wine_server_add_data( req, buffer, size );
626 status = wine_server_call( req );
627 wait_handle = wine_server_ptr_handle( reply->wait );
628 options = reply->options;
629 if (status != STATUS_PENDING) io->Information = reply->size;
631 SERVER_END_REQ;
633 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
635 if (wait_handle)
637 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
638 status = io->u.Status;
639 NtClose( wait_handle );
642 return status;
645 struct io_timeouts
647 int interval; /* max interval between two bytes */
648 int total; /* total timeout for the whole operation */
649 int end_time; /* absolute time of end of operation */
652 /* retrieve the I/O timeouts to use for a given handle */
653 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
654 struct io_timeouts *timeouts )
656 NTSTATUS status = STATUS_SUCCESS;
658 timeouts->interval = timeouts->total = -1;
660 switch(type)
662 case FD_TYPE_SERIAL:
664 /* GetCommTimeouts */
665 SERIAL_TIMEOUTS st;
666 IO_STATUS_BLOCK io;
668 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
669 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
670 if (status) break;
672 if (is_read)
674 if (st.ReadIntervalTimeout)
675 timeouts->interval = st.ReadIntervalTimeout;
677 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
679 timeouts->total = st.ReadTotalTimeoutConstant;
680 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
681 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
683 else if (st.ReadIntervalTimeout == MAXDWORD)
684 timeouts->interval = timeouts->total = 0;
686 else /* write */
688 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
690 timeouts->total = st.WriteTotalTimeoutConstant;
691 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
692 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
696 break;
697 case FD_TYPE_MAILSLOT:
698 if (is_read)
700 timeouts->interval = 0; /* return as soon as we got something */
701 SERVER_START_REQ( set_mailslot_info )
703 req->handle = wine_server_obj_handle( handle );
704 req->flags = 0;
705 if (!(status = wine_server_call( req )) &&
706 reply->read_timeout != TIMEOUT_INFINITE)
707 timeouts->total = reply->read_timeout / -10000;
709 SERVER_END_REQ;
711 break;
712 case FD_TYPE_SOCKET:
713 case FD_TYPE_PIPE:
714 case FD_TYPE_CHAR:
715 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
716 break;
717 default:
718 break;
720 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
721 return STATUS_SUCCESS;
725 /* retrieve the timeout for the next wait, in milliseconds */
726 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
728 int ret = -1;
730 if (timeouts->total != -1)
732 ret = timeouts->end_time - NtGetTickCount();
733 if (ret < 0) ret = 0;
735 if (already && timeouts->interval != -1)
737 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
739 return ret;
743 /* retrieve the avail_mode flag for async reads */
744 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
746 NTSTATUS status = STATUS_SUCCESS;
748 switch(type)
750 case FD_TYPE_SERIAL:
752 /* GetCommTimeouts */
753 SERIAL_TIMEOUTS st;
754 IO_STATUS_BLOCK io;
756 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
757 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
758 if (status) break;
759 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
760 !st.ReadTotalTimeoutConstant &&
761 st.ReadIntervalTimeout == MAXDWORD);
763 break;
764 case FD_TYPE_MAILSLOT:
765 case FD_TYPE_SOCKET:
766 case FD_TYPE_PIPE:
767 case FD_TYPE_CHAR:
768 *avail_mode = TRUE;
769 break;
770 default:
771 *avail_mode = FALSE;
772 break;
774 return status;
777 /* register an async I/O for a file read; helper for NtReadFile */
778 static NTSTATUS register_async_file_read( HANDLE handle, HANDLE event,
779 PIO_APC_ROUTINE apc, void *apc_user,
780 IO_STATUS_BLOCK *iosb, void *buffer,
781 ULONG already, ULONG length, BOOL avail_mode )
783 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
784 struct async_fileio_read *fileio;
785 NTSTATUS status;
787 if (!(fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), handle, apc, apc_user )))
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.handle = wine_server_obj_handle( handle );
800 req->async.event = wine_server_obj_handle( event );
801 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
802 req->async.iosb = wine_server_client_ptr( iosb );
803 req->async.arg = wine_server_client_ptr( fileio );
804 req->async.cvalue = cvalue;
805 status = wine_server_call( req );
807 SERVER_END_REQ;
809 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
810 return status;
814 /******************************************************************************
815 * NtReadFile [NTDLL.@]
816 * ZwReadFile [NTDLL.@]
818 * Read from an open file handle.
820 * PARAMS
821 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
822 * Event [I] Event to signal upon completion (or NULL)
823 * ApcRoutine [I] Callback to call upon completion (or NULL)
824 * ApcContext [I] Context for ApcRoutine (or NULL)
825 * IoStatusBlock [O] Receives information about the operation on return
826 * Buffer [O] Destination for the data read
827 * Length [I] Size of Buffer
828 * ByteOffset [O] Destination for the new file pointer position (or NULL)
829 * Key [O] Function unknown (may be NULL)
831 * RETURNS
832 * Success: 0. IoStatusBlock is updated, and the Information member contains
833 * The number of bytes read.
834 * Failure: An NTSTATUS error code describing the error.
836 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
837 PIO_APC_ROUTINE apc, void* apc_user,
838 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
839 PLARGE_INTEGER offset, PULONG key)
841 int result, unix_handle, needs_close;
842 unsigned int options;
843 struct io_timeouts timeouts;
844 NTSTATUS status;
845 ULONG total = 0;
846 enum server_fd_type type;
847 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
848 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
850 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
851 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
853 if (!io_status) return STATUS_ACCESS_VIOLATION;
855 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
856 &needs_close, &type, &options );
857 if (status == STATUS_BAD_DEVICE_TYPE)
858 return server_read_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
860 if (status) return status;
862 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
864 if (!virtual_check_buffer_for_write( buffer, length ))
866 status = STATUS_ACCESS_VIOLATION;
867 goto done;
870 if (type == FD_TYPE_FILE)
872 if (async_read && (!offset || offset->QuadPart < 0))
874 status = STATUS_INVALID_PARAMETER;
875 goto done;
878 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
880 /* async I/O doesn't make sense on regular files */
881 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
883 if (errno != EINTR)
885 status = FILE_GetNtStatus();
886 goto done;
889 if (!async_read)
890 /* update file pointer position */
891 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
893 total = result;
894 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
895 goto done;
898 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
900 if (async_read && (!offset || offset->QuadPart < 0))
902 status = STATUS_INVALID_PARAMETER;
903 goto done;
907 if (type == FD_TYPE_SERIAL && async_read && length)
909 /* an asynchronous serial port read with a read interval timeout needs to
910 skip the synchronous read to make sure that the server starts the read
911 interval timer after the first read */
912 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts ))) goto err;
913 if (timeouts.interval)
915 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
916 buffer, total, length, FALSE );
917 goto err;
921 for (;;)
923 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
925 total += result;
926 if (!result || total == length)
928 if (total)
930 status = STATUS_SUCCESS;
931 goto done;
933 switch (type)
935 case FD_TYPE_FILE:
936 case FD_TYPE_CHAR:
937 case FD_TYPE_DEVICE:
938 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
939 goto done;
940 case FD_TYPE_SERIAL:
941 if (!length)
943 status = STATUS_SUCCESS;
944 goto done;
946 break;
947 default:
948 status = STATUS_PIPE_BROKEN;
949 goto done;
952 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
954 else if (errno != EAGAIN)
956 if (errno == EINTR) continue;
957 if (!total) status = FILE_GetNtStatus();
958 goto done;
961 if (async_read)
963 BOOL avail_mode;
965 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
966 goto err;
967 if (total && avail_mode)
969 status = STATUS_SUCCESS;
970 goto done;
972 status = register_async_file_read( hFile, hEvent, apc, apc_user, io_status,
973 buffer, total, length, avail_mode );
974 goto err;
976 else /* synchronous read, wait for the fd to become ready */
978 struct pollfd pfd;
979 int ret, timeout;
981 if (!timeout_init_done)
983 timeout_init_done = TRUE;
984 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
985 goto err;
986 if (hEvent) NtResetEvent( hEvent, NULL );
988 timeout = get_next_io_timeout( &timeouts, total );
990 pfd.fd = unix_handle;
991 pfd.events = POLLIN;
993 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
995 if (total) /* return with what we got so far */
996 status = STATUS_SUCCESS;
997 else
998 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
999 goto done;
1001 if (ret == -1 && errno != EINTR)
1003 status = FILE_GetNtStatus();
1004 goto done;
1006 /* will now restart the read */
1010 done:
1011 send_completion = cvalue != 0;
1013 err:
1014 if (needs_close) close( unix_handle );
1015 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
1017 io_status->u.Status = status;
1018 io_status->Information = total;
1019 TRACE("= SUCCESS (%u)\n", total);
1020 if (hEvent) NtSetEvent( hEvent, NULL );
1021 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1022 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1024 else
1026 TRACE("= 0x%08x\n", status);
1027 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1030 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1032 return status;
1036 /******************************************************************************
1037 * NtReadFileScatter [NTDLL.@]
1038 * ZwReadFileScatter [NTDLL.@]
1040 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1041 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1042 ULONG length, PLARGE_INTEGER offset, PULONG key )
1044 int result, unix_handle, needs_close;
1045 unsigned int options;
1046 NTSTATUS status;
1047 ULONG pos = 0, total = 0;
1048 enum server_fd_type type;
1049 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1050 BOOL send_completion = FALSE;
1052 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1053 file, event, apc, apc_user, io_status, segments, length, offset, key);
1055 if (length % page_size) return STATUS_INVALID_PARAMETER;
1056 if (!io_status) return STATUS_ACCESS_VIOLATION;
1058 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1059 &needs_close, &type, &options );
1060 if (status) return status;
1062 if ((type != FD_TYPE_FILE) ||
1063 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1064 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1066 status = STATUS_INVALID_PARAMETER;
1067 goto error;
1070 while (length)
1072 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1073 result = pread( unix_handle, (char *)segments->Buffer + pos,
1074 page_size - pos, offset->QuadPart + total );
1075 else
1076 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1078 if (result == -1)
1080 if (errno == EINTR) continue;
1081 status = FILE_GetNtStatus();
1082 break;
1084 if (!result)
1086 status = STATUS_END_OF_FILE;
1087 break;
1089 total += result;
1090 length -= result;
1091 if ((pos += result) == page_size)
1093 pos = 0;
1094 segments++;
1098 send_completion = cvalue != 0;
1100 error:
1101 if (needs_close) close( unix_handle );
1102 if (status == STATUS_SUCCESS)
1104 io_status->u.Status = status;
1105 io_status->Information = total;
1106 TRACE("= SUCCESS (%u)\n", total);
1107 if (event) NtSetEvent( event, NULL );
1108 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1109 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1111 else
1113 TRACE("= 0x%08x\n", status);
1114 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1117 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1119 return status;
1123 /***********************************************************************
1124 * FILE_AsyncWriteService (INTERNAL)
1126 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb,
1127 NTSTATUS status, void **apc, void **arg )
1129 struct async_fileio_write *fileio = user;
1130 int result, fd, needs_close;
1131 enum server_fd_type type;
1133 switch (status)
1135 case STATUS_ALERTED:
1136 /* write some data (non-blocking) */
1137 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1138 &needs_close, &type, NULL )))
1139 break;
1141 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1142 result = send( fd, fileio->buffer, 0, 0 );
1143 else
1144 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1146 if (needs_close) close( fd );
1148 if (result < 0)
1150 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1151 else status = FILE_GetNtStatus();
1153 else
1155 fileio->already += result;
1156 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1158 break;
1160 case STATUS_TIMEOUT:
1161 case STATUS_IO_TIMEOUT:
1162 if (fileio->already) status = STATUS_SUCCESS;
1163 break;
1165 if (status != STATUS_PENDING)
1167 iosb->u.Status = status;
1168 iosb->Information = fileio->already;
1169 *apc = fileio->io.apc;
1170 *arg = fileio->io.apc_arg;
1171 release_fileio( &fileio->io );
1173 return status;
1176 static NTSTATUS set_pending_write( HANDLE device )
1178 NTSTATUS status;
1180 SERVER_START_REQ( set_serial_info )
1182 req->handle = wine_server_obj_handle( device );
1183 req->flags = SERIALINFO_PENDING_WRITE;
1184 status = wine_server_call( req );
1186 SERVER_END_REQ;
1187 return status;
1190 /******************************************************************************
1191 * NtWriteFile [NTDLL.@]
1192 * ZwWriteFile [NTDLL.@]
1194 * Write to an open file handle.
1196 * PARAMS
1197 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1198 * Event [I] Event to signal upon completion (or NULL)
1199 * ApcRoutine [I] Callback to call upon completion (or NULL)
1200 * ApcContext [I] Context for ApcRoutine (or NULL)
1201 * IoStatusBlock [O] Receives information about the operation on return
1202 * Buffer [I] Source for the data to write
1203 * Length [I] Size of Buffer
1204 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1205 * Key [O] Function unknown (may be NULL)
1207 * RETURNS
1208 * Success: 0. IoStatusBlock is updated, and the Information member contains
1209 * The number of bytes written.
1210 * Failure: An NTSTATUS error code describing the error.
1212 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1213 PIO_APC_ROUTINE apc, void* apc_user,
1214 PIO_STATUS_BLOCK io_status,
1215 const void* buffer, ULONG length,
1216 PLARGE_INTEGER offset, PULONG key)
1218 int result, unix_handle, needs_close;
1219 unsigned int options;
1220 struct io_timeouts timeouts;
1221 NTSTATUS status;
1222 ULONG total = 0;
1223 enum server_fd_type type;
1224 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1225 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1226 LARGE_INTEGER offset_eof;
1228 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1229 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1231 if (!io_status) return STATUS_ACCESS_VIOLATION;
1233 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1234 &needs_close, &type, &options );
1235 if (status == STATUS_BAD_DEVICE_TYPE)
1236 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1238 if (status == STATUS_ACCESS_DENIED)
1240 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1241 &needs_close, &type, &options );
1242 append_write = TRUE;
1244 if (status) return status;
1246 if (!virtual_check_buffer_for_read( buffer, length ))
1248 status = STATUS_INVALID_USER_BUFFER;
1249 goto done;
1252 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1254 if (type == FD_TYPE_FILE)
1256 if (async_write &&
1257 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1259 status = STATUS_INVALID_PARAMETER;
1260 goto done;
1263 if (append_write)
1265 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1266 offset = &offset_eof;
1269 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1271 off_t off = offset->QuadPart;
1273 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1275 struct stat st;
1277 if (fstat( unix_handle, &st ) == -1)
1279 status = FILE_GetNtStatus();
1280 goto done;
1282 off = st.st_size;
1284 else if (offset->QuadPart < 0)
1286 status = STATUS_INVALID_PARAMETER;
1287 goto done;
1290 /* async I/O doesn't make sense on regular files */
1291 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1293 if (errno != EINTR)
1295 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1296 else status = FILE_GetNtStatus();
1297 goto done;
1301 if (!async_write)
1302 /* update file pointer position */
1303 lseek( unix_handle, off + result, SEEK_SET );
1305 total = result;
1306 status = STATUS_SUCCESS;
1307 goto done;
1310 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1312 if (async_write &&
1313 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1315 status = STATUS_INVALID_PARAMETER;
1316 goto done;
1320 for (;;)
1322 /* zero-length writes on sockets may not work with plain write(2) */
1323 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1324 result = send( unix_handle, buffer, 0, 0 );
1325 else
1326 result = write( unix_handle, (const char *)buffer + total, length - total );
1328 if (result >= 0)
1330 total += result;
1331 if (total == length)
1333 status = STATUS_SUCCESS;
1334 goto done;
1336 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1338 else if (errno != EAGAIN)
1340 if (errno == EINTR) continue;
1341 if (!total)
1343 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1344 else status = FILE_GetNtStatus();
1346 goto done;
1349 if (async_write)
1351 struct async_fileio_write *fileio;
1353 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), hFile, apc, apc_user );
1354 if (!fileio)
1356 status = STATUS_NO_MEMORY;
1357 goto err;
1359 fileio->already = total;
1360 fileio->count = length;
1361 fileio->buffer = buffer;
1363 SERVER_START_REQ( register_async )
1365 req->type = ASYNC_TYPE_WRITE;
1366 req->count = length;
1367 req->async.handle = wine_server_obj_handle( hFile );
1368 req->async.event = wine_server_obj_handle( hEvent );
1369 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1370 req->async.iosb = wine_server_client_ptr( io_status );
1371 req->async.arg = wine_server_client_ptr( fileio );
1372 req->async.cvalue = cvalue;
1373 status = wine_server_call( req );
1375 SERVER_END_REQ;
1377 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1378 goto err;
1380 else /* synchronous write, wait for the fd to become ready */
1382 struct pollfd pfd;
1383 int ret, timeout;
1385 if (!timeout_init_done)
1387 timeout_init_done = TRUE;
1388 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1389 goto err;
1390 if (hEvent) NtResetEvent( hEvent, NULL );
1392 timeout = get_next_io_timeout( &timeouts, total );
1394 pfd.fd = unix_handle;
1395 pfd.events = POLLOUT;
1397 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1399 /* return with what we got so far */
1400 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1401 goto done;
1403 if (ret == -1 && errno != EINTR)
1405 status = FILE_GetNtStatus();
1406 goto done;
1408 /* will now restart the write */
1412 done:
1413 send_completion = cvalue != 0;
1415 err:
1416 if (needs_close) close( unix_handle );
1418 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1419 set_pending_write( hFile );
1421 if (status == STATUS_SUCCESS)
1423 io_status->u.Status = status;
1424 io_status->Information = total;
1425 TRACE("= SUCCESS (%u)\n", total);
1426 if (hEvent) NtSetEvent( hEvent, NULL );
1427 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1428 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1430 else
1432 TRACE("= 0x%08x\n", status);
1433 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1436 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1438 return status;
1442 /******************************************************************************
1443 * NtWriteFileGather [NTDLL.@]
1444 * ZwWriteFileGather [NTDLL.@]
1446 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1447 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1448 ULONG length, PLARGE_INTEGER offset, PULONG key )
1450 int result, unix_handle, needs_close;
1451 unsigned int options;
1452 NTSTATUS status;
1453 ULONG pos = 0, total = 0;
1454 enum server_fd_type type;
1455 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1456 BOOL send_completion = FALSE;
1458 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1459 file, event, apc, apc_user, io_status, segments, length, offset, key);
1461 if (length % page_size) return STATUS_INVALID_PARAMETER;
1462 if (!io_status) return STATUS_ACCESS_VIOLATION;
1464 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1465 &needs_close, &type, &options );
1466 if (status) return status;
1468 if ((type != FD_TYPE_FILE) ||
1469 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1470 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1472 status = STATUS_INVALID_PARAMETER;
1473 goto error;
1476 while (length)
1478 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1479 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1480 page_size - pos, offset->QuadPart + total );
1481 else
1482 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1484 if (result == -1)
1486 if (errno == EINTR) continue;
1487 if (errno == EFAULT)
1489 status = STATUS_INVALID_USER_BUFFER;
1490 goto error;
1492 status = FILE_GetNtStatus();
1493 break;
1495 if (!result)
1497 status = STATUS_DISK_FULL;
1498 break;
1500 total += result;
1501 length -= result;
1502 if ((pos += result) == page_size)
1504 pos = 0;
1505 segments++;
1509 send_completion = cvalue != 0;
1511 error:
1512 if (needs_close) close( unix_handle );
1513 if (status == STATUS_SUCCESS)
1515 io_status->u.Status = status;
1516 io_status->Information = total;
1517 TRACE("= SUCCESS (%u)\n", total);
1518 if (event) NtSetEvent( event, NULL );
1519 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1520 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1522 else
1524 TRACE("= 0x%08x\n", status);
1525 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1528 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1530 return status;
1534 /* do an ioctl call through the server */
1535 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1536 PIO_APC_ROUTINE apc, PVOID apc_context,
1537 IO_STATUS_BLOCK *io, ULONG code,
1538 const void *in_buffer, ULONG in_size,
1539 PVOID out_buffer, ULONG out_size )
1541 struct async_irp *async;
1542 NTSTATUS status;
1543 HANDLE wait_handle;
1544 ULONG options;
1545 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1547 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), handle, apc, apc_context )))
1548 return STATUS_NO_MEMORY;
1549 async->event = event;
1550 async->buffer = out_buffer;
1551 async->size = out_size;
1553 SERVER_START_REQ( ioctl )
1555 req->code = code;
1556 req->blocking = !apc && !event && !cvalue;
1557 req->async.handle = wine_server_obj_handle( handle );
1558 req->async.callback = wine_server_client_ptr( irp_completion );
1559 req->async.iosb = wine_server_client_ptr( io );
1560 req->async.arg = wine_server_client_ptr( async );
1561 req->async.event = wine_server_obj_handle( event );
1562 req->async.cvalue = cvalue;
1563 wine_server_add_data( req, in_buffer, in_size );
1564 wine_server_set_reply( req, out_buffer, out_size );
1565 status = wine_server_call( req );
1566 wait_handle = wine_server_ptr_handle( reply->wait );
1567 options = reply->options;
1568 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1570 SERVER_END_REQ;
1572 if (status == STATUS_NOT_SUPPORTED)
1573 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1574 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1576 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1578 if (wait_handle)
1580 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1581 status = io->u.Status;
1582 NtClose( wait_handle );
1585 return status;
1588 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1589 * server */
1590 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1591 ULONG in_size)
1593 #ifdef VALGRIND_MAKE_MEM_DEFINED
1594 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1595 do { \
1596 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1597 if ((size) >= FIELD_OFFSET(t, f2)) \
1598 VALGRIND_MAKE_MEM_DEFINED( \
1599 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1600 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1601 } while (0)
1603 switch (code)
1605 case FSCTL_PIPE_WAIT:
1606 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1607 break;
1609 #endif
1613 /**************************************************************************
1614 * NtDeviceIoControlFile [NTDLL.@]
1615 * ZwDeviceIoControlFile [NTDLL.@]
1617 * Perform an I/O control operation on an open file handle.
1619 * PARAMS
1620 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1621 * event [I] Event to signal upon completion (or NULL)
1622 * apc [I] Callback to call upon completion (or NULL)
1623 * apc_context [I] Context for ApcRoutine (or NULL)
1624 * io [O] Receives information about the operation on return
1625 * code [I] Control code for the operation to perform
1626 * in_buffer [I] Source for any input data required (or NULL)
1627 * in_size [I] Size of InputBuffer
1628 * out_buffer [O] Source for any output data returned (or NULL)
1629 * out_size [I] Size of OutputBuffer
1631 * RETURNS
1632 * Success: 0. IoStatusBlock is updated.
1633 * Failure: An NTSTATUS error code describing the error.
1635 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1636 PIO_APC_ROUTINE apc, PVOID apc_context,
1637 PIO_STATUS_BLOCK io, ULONG code,
1638 PVOID in_buffer, ULONG in_size,
1639 PVOID out_buffer, ULONG out_size)
1641 ULONG device = (code >> 16);
1642 NTSTATUS status = STATUS_NOT_SUPPORTED;
1644 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1645 handle, event, apc, apc_context, io, code,
1646 in_buffer, in_size, out_buffer, out_size);
1648 switch(device)
1650 case FILE_DEVICE_DISK:
1651 case FILE_DEVICE_CD_ROM:
1652 case FILE_DEVICE_DVD:
1653 case FILE_DEVICE_CONTROLLER:
1654 case FILE_DEVICE_MASS_STORAGE:
1655 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1656 in_buffer, in_size, out_buffer, out_size);
1657 break;
1658 case FILE_DEVICE_SERIAL_PORT:
1659 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1660 in_buffer, in_size, out_buffer, out_size);
1661 break;
1662 case FILE_DEVICE_TAPE:
1663 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1664 in_buffer, in_size, out_buffer, out_size);
1665 break;
1668 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1669 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1670 in_buffer, in_size, out_buffer, out_size );
1672 if (status != STATUS_PENDING) io->u.Status = status;
1673 return status;
1677 /**************************************************************************
1678 * NtFsControlFile [NTDLL.@]
1679 * ZwFsControlFile [NTDLL.@]
1681 * Perform a file system control operation on an open file handle.
1683 * PARAMS
1684 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1685 * event [I] Event to signal upon completion (or NULL)
1686 * apc [I] Callback to call upon completion (or NULL)
1687 * apc_context [I] Context for ApcRoutine (or NULL)
1688 * io [O] Receives information about the operation on return
1689 * code [I] Control code for the operation to perform
1690 * in_buffer [I] Source for any input data required (or NULL)
1691 * in_size [I] Size of InputBuffer
1692 * out_buffer [O] Source for any output data returned (or NULL)
1693 * out_size [I] Size of OutputBuffer
1695 * RETURNS
1696 * Success: 0. IoStatusBlock is updated.
1697 * Failure: An NTSTATUS error code describing the error.
1699 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1700 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1701 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1703 NTSTATUS status;
1705 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1706 handle, event, apc, apc_context, io, code,
1707 in_buffer, in_size, out_buffer, out_size);
1709 if (!io) return STATUS_INVALID_PARAMETER;
1711 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1713 switch(code)
1715 case FSCTL_DISMOUNT_VOLUME:
1716 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1717 in_buffer, in_size, out_buffer, out_size );
1718 if (!status) status = DIR_unmount_device( handle );
1719 break;
1721 case FSCTL_PIPE_PEEK:
1723 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1724 int avail = 0, fd, needs_close;
1726 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1728 status = STATUS_INFO_LENGTH_MISMATCH;
1729 break;
1732 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1733 break;
1735 #ifdef FIONREAD
1736 if (ioctl( fd, FIONREAD, &avail ) != 0)
1738 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1739 if (needs_close) close( fd );
1740 status = FILE_GetNtStatus();
1741 break;
1743 #endif
1744 if (!avail) /* check for closed pipe */
1746 struct pollfd pollfd;
1747 int ret;
1749 pollfd.fd = fd;
1750 pollfd.events = POLLIN;
1751 pollfd.revents = 0;
1752 ret = poll( &pollfd, 1, 0 );
1753 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1755 if (needs_close) close( fd );
1756 status = STATUS_PIPE_BROKEN;
1757 break;
1760 buffer->NamedPipeState = 0; /* FIXME */
1761 buffer->ReadDataAvailable = avail;
1762 buffer->NumberOfMessages = 0; /* FIXME */
1763 buffer->MessageLength = 0; /* FIXME */
1764 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1765 status = STATUS_SUCCESS;
1766 if (avail)
1768 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1769 if (data_size)
1771 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1772 if (res >= 0) io->Information += res;
1775 if (needs_close) close( fd );
1777 break;
1779 case FSCTL_PIPE_DISCONNECT:
1780 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1781 in_buffer, in_size, out_buffer, out_size );
1782 if (!status)
1784 int fd = server_remove_fd_from_cache( handle );
1785 if (fd != -1) close( fd );
1787 break;
1789 case FSCTL_PIPE_LISTEN:
1790 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1791 in_buffer, in_size, out_buffer, out_size );
1792 return status;
1794 case FSCTL_PIPE_IMPERSONATE:
1795 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1796 status = RtlImpersonateSelf( SecurityImpersonation );
1797 break;
1799 case FSCTL_IS_VOLUME_MOUNTED:
1800 case FSCTL_LOCK_VOLUME:
1801 case FSCTL_UNLOCK_VOLUME:
1802 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1803 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1804 status = STATUS_SUCCESS;
1805 break;
1807 case FSCTL_GET_RETRIEVAL_POINTERS:
1809 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1811 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1813 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1815 buffer->ExtentCount = 1;
1816 buffer->StartingVcn.QuadPart = 1;
1817 buffer->Extents[0].NextVcn.QuadPart = 0;
1818 buffer->Extents[0].Lcn.QuadPart = 0;
1819 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1820 status = STATUS_SUCCESS;
1822 else
1824 io->Information = 0;
1825 status = STATUS_BUFFER_TOO_SMALL;
1827 break;
1829 case FSCTL_SET_SPARSE:
1830 TRACE("FSCTL_SET_SPARSE: Ignoring request\n");
1831 io->Information = 0;
1832 status = STATUS_SUCCESS;
1833 break;
1834 case FSCTL_PIPE_WAIT:
1835 default:
1836 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1837 in_buffer, in_size, out_buffer, out_size );
1838 break;
1841 if (status != STATUS_PENDING) io->u.Status = status;
1842 return status;
1846 struct read_changes_fileio
1848 struct async_fileio io;
1849 void *buffer;
1850 ULONG buffer_size;
1851 ULONG data_size;
1852 char data[1];
1855 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb,
1856 NTSTATUS status, void **apc, void **arg )
1858 struct read_changes_fileio *fileio = user;
1859 int size = 0;
1861 if (status == STATUS_ALERTED)
1863 SERVER_START_REQ( read_change )
1865 req->handle = wine_server_obj_handle( fileio->io.handle );
1866 wine_server_set_reply( req, fileio->data, fileio->data_size );
1867 status = wine_server_call( req );
1868 size = wine_server_reply_size( reply );
1870 SERVER_END_REQ;
1872 if (status == STATUS_SUCCESS && fileio->buffer)
1874 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1875 int i, left = fileio->buffer_size;
1876 DWORD *last_entry_offset = NULL;
1877 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1879 while (size && left >= sizeof(*pfni))
1881 /* convert to an NT style path */
1882 for (i = 0; i < event->len; i++)
1883 if (event->name[i] == '/') event->name[i] = '\\';
1885 pfni->Action = event->action;
1886 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1887 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1888 last_entry_offset = &pfni->NextEntryOffset;
1890 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1892 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1893 pfni->FileNameLength *= sizeof(WCHAR);
1894 pfni->NextEntryOffset = i;
1895 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1896 left -= i;
1898 i = (offsetof(struct filesystem_event, name[event->len])
1899 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1900 event = (struct filesystem_event*)((char*)event + i);
1901 size -= i;
1904 if (size)
1906 status = STATUS_NOTIFY_ENUM_DIR;
1907 size = 0;
1909 else
1911 if (last_entry_offset) *last_entry_offset = 0;
1912 size = fileio->buffer_size - left;
1915 else
1917 status = STATUS_NOTIFY_ENUM_DIR;
1918 size = 0;
1922 if (status != STATUS_PENDING)
1924 iosb->u.Status = status;
1925 iosb->Information = size;
1926 *apc = fileio->io.apc;
1927 *arg = fileio->io.apc_arg;
1928 release_fileio( &fileio->io );
1930 return status;
1933 #define FILE_NOTIFY_ALL ( \
1934 FILE_NOTIFY_CHANGE_FILE_NAME | \
1935 FILE_NOTIFY_CHANGE_DIR_NAME | \
1936 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1937 FILE_NOTIFY_CHANGE_SIZE | \
1938 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1939 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1940 FILE_NOTIFY_CHANGE_CREATION | \
1941 FILE_NOTIFY_CHANGE_SECURITY )
1943 /******************************************************************************
1944 * NtNotifyChangeDirectoryFile [NTDLL.@]
1946 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1947 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1948 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1950 struct read_changes_fileio *fileio;
1951 NTSTATUS status;
1952 ULONG size = max( 4096, buffer_size );
1953 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1955 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1956 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1958 if (!iosb) return STATUS_ACCESS_VIOLATION;
1959 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1961 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1962 handle, apc, apc_context );
1963 if (!fileio) return STATUS_NO_MEMORY;
1965 fileio->buffer = buffer;
1966 fileio->buffer_size = buffer_size;
1967 fileio->data_size = size;
1969 SERVER_START_REQ( read_directory_changes )
1971 req->filter = filter;
1972 req->want_data = (buffer != NULL);
1973 req->subtree = subtree;
1974 req->async.handle = wine_server_obj_handle( handle );
1975 req->async.callback = wine_server_client_ptr( read_changes_apc );
1976 req->async.iosb = wine_server_client_ptr( iosb );
1977 req->async.arg = wine_server_client_ptr( fileio );
1978 req->async.event = wine_server_obj_handle( event );
1979 req->async.cvalue = cvalue;
1980 status = wine_server_call( req );
1982 SERVER_END_REQ;
1984 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1985 return status;
1988 /******************************************************************************
1989 * NtSetVolumeInformationFile [NTDLL.@]
1990 * ZwSetVolumeInformationFile [NTDLL.@]
1992 * Set volume information for an open file handle.
1994 * PARAMS
1995 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1996 * IoStatusBlock [O] Receives information about the operation on return
1997 * FsInformation [I] Source for volume information
1998 * Length [I] Size of FsInformation
1999 * FsInformationClass [I] Type of volume information to set
2001 * RETURNS
2002 * Success: 0. IoStatusBlock is updated.
2003 * Failure: An NTSTATUS error code describing the error.
2005 NTSTATUS WINAPI NtSetVolumeInformationFile(
2006 IN HANDLE FileHandle,
2007 PIO_STATUS_BLOCK IoStatusBlock,
2008 PVOID FsInformation,
2009 ULONG Length,
2010 FS_INFORMATION_CLASS FsInformationClass)
2012 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
2013 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
2014 return 0;
2017 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
2018 static int futimens( int fd, const struct timespec spec[2] )
2020 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
2022 #define HAVE_FUTIMENS
2023 #endif /* __ANDROID__ */
2025 #ifndef UTIME_OMIT
2026 #define UTIME_OMIT ((1 << 30) - 2)
2027 #endif
2029 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
2031 NTSTATUS status = STATUS_SUCCESS;
2033 #ifdef HAVE_FUTIMENS
2034 struct timespec tv[2];
2036 tv[0].tv_sec = tv[1].tv_sec = 0;
2037 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
2038 if (atime->QuadPart)
2040 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
2041 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
2043 if (mtime->QuadPart)
2045 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
2046 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
2048 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
2050 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
2051 struct timeval tv[2];
2052 struct stat st;
2054 if (!atime->QuadPart || !mtime->QuadPart)
2057 tv[0].tv_sec = tv[0].tv_usec = 0;
2058 tv[1].tv_sec = tv[1].tv_usec = 0;
2059 if (!fstat( fd, &st ))
2061 tv[0].tv_sec = st.st_atime;
2062 tv[1].tv_sec = st.st_mtime;
2063 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2064 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
2065 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2066 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
2067 #endif
2068 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2069 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
2070 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2071 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
2072 #endif
2075 if (atime->QuadPart)
2077 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
2078 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
2080 if (mtime->QuadPart)
2082 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
2083 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
2085 #ifdef HAVE_FUTIMES
2086 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
2087 #elif defined(HAVE_FUTIMESAT)
2088 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
2089 #endif
2091 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
2092 FIXME( "setting file times not supported\n" );
2093 status = STATUS_NOT_IMPLEMENTED;
2094 #endif
2095 return status;
2098 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
2099 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
2101 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
2102 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
2103 RtlSecondsSince1970ToTime( st->st_atime, atime );
2104 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2105 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
2106 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2107 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
2108 #endif
2109 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2110 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2111 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2112 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2113 #endif
2114 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2115 atime->QuadPart += st->st_atim.tv_nsec / 100;
2116 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2117 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2118 #endif
2119 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2120 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2121 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2122 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2123 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2124 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2125 #endif
2126 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2127 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2128 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2129 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2130 #endif
2131 #else
2132 *creation = *mtime;
2133 #endif
2136 /* fill in the file information that depends on the stat and attribute info */
2137 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2138 FILE_INFORMATION_CLASS class )
2140 switch (class)
2142 case FileBasicInformation:
2144 FILE_BASIC_INFORMATION *info = ptr;
2146 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2147 &info->LastAccessTime, &info->CreationTime );
2148 info->FileAttributes = attr;
2150 break;
2151 case FileStandardInformation:
2153 FILE_STANDARD_INFORMATION *info = ptr;
2155 if ((info->Directory = S_ISDIR(st->st_mode)))
2157 info->AllocationSize.QuadPart = 0;
2158 info->EndOfFile.QuadPart = 0;
2159 info->NumberOfLinks = 1;
2161 else
2163 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2164 info->EndOfFile.QuadPart = st->st_size;
2165 info->NumberOfLinks = st->st_nlink;
2168 break;
2169 case FileInternalInformation:
2171 FILE_INTERNAL_INFORMATION *info = ptr;
2172 info->IndexNumber.QuadPart = st->st_ino;
2174 break;
2175 case FileEndOfFileInformation:
2177 FILE_END_OF_FILE_INFORMATION *info = ptr;
2178 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2180 break;
2181 case FileAllInformation:
2183 FILE_ALL_INFORMATION *info = ptr;
2184 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2185 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2186 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2188 break;
2189 /* all directory structures start with the FileDirectoryInformation layout */
2190 case FileBothDirectoryInformation:
2191 case FileFullDirectoryInformation:
2192 case FileDirectoryInformation:
2194 FILE_DIRECTORY_INFORMATION *info = ptr;
2196 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2197 &info->LastAccessTime, &info->CreationTime );
2198 if (S_ISDIR(st->st_mode))
2200 info->AllocationSize.QuadPart = 0;
2201 info->EndOfFile.QuadPart = 0;
2203 else
2205 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2206 info->EndOfFile.QuadPart = st->st_size;
2208 info->FileAttributes = attr;
2210 break;
2211 case FileIdFullDirectoryInformation:
2213 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2214 info->FileId.QuadPart = st->st_ino;
2215 fill_file_info( st, attr, info, FileDirectoryInformation );
2217 break;
2218 case FileIdBothDirectoryInformation:
2220 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2221 info->FileId.QuadPart = st->st_ino;
2222 fill_file_info( st, attr, info, FileDirectoryInformation );
2224 break;
2225 case FileIdGlobalTxDirectoryInformation:
2227 FILE_ID_GLOBAL_TX_DIR_INFORMATION *info = ptr;
2228 info->FileId.QuadPart = st->st_ino;
2229 fill_file_info( st, attr, info, FileDirectoryInformation );
2231 break;
2233 default:
2234 return STATUS_INVALID_INFO_CLASS;
2236 return STATUS_SUCCESS;
2239 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2241 data_size_t size = 1024;
2242 NTSTATUS ret;
2243 char *name;
2245 for (;;)
2247 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2248 if (!name) return STATUS_NO_MEMORY;
2249 unix_name->MaximumLength = size + 1;
2251 SERVER_START_REQ( get_handle_unix_name )
2253 req->handle = wine_server_obj_handle( handle );
2254 wine_server_set_reply( req, name, size );
2255 ret = wine_server_call( req );
2256 size = reply->name_len;
2258 SERVER_END_REQ;
2260 if (!ret)
2262 name[size] = 0;
2263 unix_name->Buffer = name;
2264 unix_name->Length = size;
2265 break;
2267 RtlFreeHeap( GetProcessHeap(), 0, name );
2268 if (ret != STATUS_BUFFER_OVERFLOW) break;
2270 return ret;
2273 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2275 UNICODE_STRING nt_name;
2276 NTSTATUS status;
2278 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2280 const WCHAR *ptr = nt_name.Buffer;
2281 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2283 /* Skip the volume mount point. */
2284 while (ptr != end && *ptr == '\\') ++ptr;
2285 while (ptr != end && *ptr != '\\') ++ptr;
2286 while (ptr != end && *ptr == '\\') ++ptr;
2287 while (ptr != end && *ptr != '\\') ++ptr;
2289 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2290 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2291 else *name_len = info->FileNameLength;
2293 memcpy( info->FileName, ptr, *name_len );
2294 RtlFreeUnicodeString( &nt_name );
2297 return status;
2300 /******************************************************************************
2301 * NtQueryInformationFile [NTDLL.@]
2302 * ZwQueryInformationFile [NTDLL.@]
2304 * Get information about an open file handle.
2306 * PARAMS
2307 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2308 * io [O] Receives information about the operation on return
2309 * ptr [O] Destination for file information
2310 * len [I] Size of FileInformation
2311 * class [I] Type of file information to get
2313 * RETURNS
2314 * Success: 0. IoStatusBlock and FileInformation are updated.
2315 * Failure: An NTSTATUS error code describing the error.
2317 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2318 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2320 static const size_t info_sizes[] =
2323 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2324 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2325 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2326 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2327 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2328 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2329 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2330 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2331 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2332 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2333 0, /* FileLinkInformation */
2334 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2335 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2336 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2337 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2338 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2339 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2340 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2341 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2342 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2343 0, /* FileAlternateNameInformation */
2344 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2345 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2346 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2347 0, /* FilePipeRemoteInformation */
2348 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2349 0, /* FileMailslotSetInformation */
2350 0, /* FileCompressionInformation */
2351 0, /* FileObjectIdInformation */
2352 0, /* FileCompletionInformation */
2353 0, /* FileMoveClusterInformation */
2354 0, /* FileQuotaInformation */
2355 0, /* FileReparsePointInformation */
2356 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2357 0, /* FileAttributeTagInformation */
2358 0, /* FileTrackingInformation */
2359 0, /* FileIdBothDirectoryInformation */
2360 0, /* FileIdFullDirectoryInformation */
2361 0, /* FileValidDataLengthInformation */
2362 0, /* FileShortNameInformation */
2363 0, /* FileIoCompletionNotificationInformation, */
2364 0, /* FileIoStatusBlockRangeInformation */
2365 0, /* FileIoPriorityHintInformation */
2366 0, /* FileSfioReserveInformation */
2367 0, /* FileSfioVolumeInformation */
2368 0, /* FileHardLinkInformation */
2369 0, /* FileProcessIdsUsingFileInformation */
2370 0, /* FileNormalizedNameInformation */
2371 0, /* FileNetworkPhysicalNameInformation */
2372 0, /* FileIdGlobalTxDirectoryInformation */
2373 0, /* FileIsRemoteDeviceInformation */
2374 0, /* FileAttributeCacheInformation */
2375 0, /* FileNumaNodeInformation */
2376 0, /* FileStandardLinkInformation */
2377 0, /* FileRemoteProtocolInformation */
2378 0, /* FileReplaceCompletionInformation */
2381 struct stat st;
2382 int fd, needs_close = FALSE;
2383 ULONG attr;
2385 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2387 io->Information = 0;
2389 if (class <= 0 || class >= FileMaximumInformation)
2390 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2391 if (!info_sizes[class])
2393 FIXME("Unsupported class (%d)\n", class);
2394 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2396 if (len < info_sizes[class])
2397 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2399 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2401 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2402 return io->u.Status;
2405 switch (class)
2407 case FileBasicInformation:
2408 if (fd_get_file_info( fd, &st, &attr ) == -1)
2409 io->u.Status = FILE_GetNtStatus();
2410 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2411 io->u.Status = STATUS_INVALID_INFO_CLASS;
2412 else
2413 fill_file_info( &st, attr, ptr, class );
2414 break;
2415 case FileStandardInformation:
2417 FILE_STANDARD_INFORMATION *info = ptr;
2419 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2420 else
2422 fill_file_info( &st, attr, info, class );
2423 info->DeletePending = FALSE; /* FIXME */
2426 break;
2427 case FilePositionInformation:
2429 FILE_POSITION_INFORMATION *info = ptr;
2430 off_t res = lseek( fd, 0, SEEK_CUR );
2431 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2432 else info->CurrentByteOffset.QuadPart = res;
2434 break;
2435 case FileInternalInformation:
2436 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2437 else fill_file_info( &st, attr, ptr, class );
2438 break;
2439 case FileEaInformation:
2441 FILE_EA_INFORMATION *info = ptr;
2442 info->EaSize = 0;
2444 break;
2445 case FileEndOfFileInformation:
2446 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2447 else fill_file_info( &st, attr, ptr, class );
2448 break;
2449 case FileAllInformation:
2451 FILE_ALL_INFORMATION *info = ptr;
2452 ANSI_STRING unix_name;
2454 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2455 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2456 io->u.Status = STATUS_INVALID_INFO_CLASS;
2457 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2459 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2461 fill_file_info( &st, attr, info, FileAllInformation );
2462 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2463 info->EaInformation.EaSize = 0;
2464 info->AccessInformation.AccessFlags = 0; /* FIXME */
2465 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2466 info->ModeInformation.Mode = 0; /* FIXME */
2467 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2469 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2470 RtlFreeAnsiString( &unix_name );
2471 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2474 break;
2475 case FileMailslotQueryInformation:
2477 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2479 SERVER_START_REQ( set_mailslot_info )
2481 req->handle = wine_server_obj_handle( hFile );
2482 req->flags = 0;
2483 io->u.Status = wine_server_call( req );
2484 if( io->u.Status == STATUS_SUCCESS )
2486 info->MaximumMessageSize = reply->max_msgsize;
2487 info->MailslotQuota = 0;
2488 info->NextMessageSize = 0;
2489 info->MessagesAvailable = 0;
2490 info->ReadTimeout.QuadPart = reply->read_timeout;
2493 SERVER_END_REQ;
2494 if (!io->u.Status)
2496 char *tmpbuf;
2497 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2498 if (size > 0x10000) size = 0x10000;
2499 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2501 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2503 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2504 info->MessagesAvailable = (res > 0);
2505 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2506 if (needs_close) close( fd );
2508 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2512 break;
2513 case FilePipeInformation:
2515 FILE_PIPE_INFORMATION* pi = ptr;
2517 SERVER_START_REQ( get_named_pipe_info )
2519 req->handle = wine_server_obj_handle( hFile );
2520 if (!(io->u.Status = wine_server_call( req )))
2522 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2523 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2524 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2525 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2528 SERVER_END_REQ;
2530 break;
2531 case FilePipeLocalInformation:
2533 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2535 SERVER_START_REQ( get_named_pipe_info )
2537 req->handle = wine_server_obj_handle( hFile );
2538 if (!(io->u.Status = wine_server_call( req )))
2540 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2541 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2542 switch (reply->sharing)
2544 case FILE_SHARE_READ:
2545 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2546 break;
2547 case FILE_SHARE_WRITE:
2548 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2549 break;
2550 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2551 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2552 break;
2554 pli->MaximumInstances = reply->maxinstances;
2555 pli->CurrentInstances = reply->instances;
2556 pli->InboundQuota = reply->insize;
2557 pli->ReadDataAvailable = 0; /* FIXME */
2558 pli->OutboundQuota = reply->outsize;
2559 pli->WriteQuotaAvailable = 0; /* FIXME */
2560 pli->NamedPipeState = 0; /* FIXME */
2561 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2562 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2565 SERVER_END_REQ;
2567 break;
2568 case FileNameInformation:
2570 FILE_NAME_INFORMATION *info = ptr;
2571 ANSI_STRING unix_name;
2573 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2575 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2576 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2577 RtlFreeAnsiString( &unix_name );
2578 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2581 break;
2582 case FileNetworkOpenInformation:
2584 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2585 ANSI_STRING unix_name;
2587 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2589 ULONG attributes;
2590 struct stat st;
2592 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2593 io->u.Status = FILE_GetNtStatus();
2594 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2595 io->u.Status = STATUS_INVALID_INFO_CLASS;
2596 else
2598 FILE_BASIC_INFORMATION basic;
2599 FILE_STANDARD_INFORMATION std;
2601 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2602 fill_file_info( &st, attributes, &std, FileStandardInformation );
2604 info->CreationTime = basic.CreationTime;
2605 info->LastAccessTime = basic.LastAccessTime;
2606 info->LastWriteTime = basic.LastWriteTime;
2607 info->ChangeTime = basic.ChangeTime;
2608 info->AllocationSize = std.AllocationSize;
2609 info->EndOfFile = std.EndOfFile;
2610 info->FileAttributes = basic.FileAttributes;
2612 RtlFreeAnsiString( &unix_name );
2615 break;
2616 default:
2617 FIXME("Unsupported class (%d)\n", class);
2618 io->u.Status = STATUS_NOT_IMPLEMENTED;
2619 break;
2621 if (needs_close) close( fd );
2622 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2623 return io->u.Status;
2626 /******************************************************************************
2627 * NtSetInformationFile [NTDLL.@]
2628 * ZwSetInformationFile [NTDLL.@]
2630 * Set information about an open file handle.
2632 * PARAMS
2633 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2634 * io [O] Receives information about the operation on return
2635 * ptr [I] Source for file information
2636 * len [I] Size of FileInformation
2637 * class [I] Type of file information to set
2639 * RETURNS
2640 * Success: 0. io is updated.
2641 * Failure: An NTSTATUS error code describing the error.
2643 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2644 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2646 int fd, needs_close;
2648 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2650 io->u.Status = STATUS_SUCCESS;
2651 switch (class)
2653 case FileBasicInformation:
2654 if (len >= sizeof(FILE_BASIC_INFORMATION))
2656 struct stat st;
2657 const FILE_BASIC_INFORMATION *info = ptr;
2659 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2660 return io->u.Status;
2662 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2663 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2665 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2667 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2668 else
2670 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2672 if (S_ISDIR( st.st_mode))
2673 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2674 else
2675 st.st_mode &= ~0222; /* clear write permission bits */
2677 else
2679 /* add write permission only where we already have read permission */
2680 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2682 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2686 if (needs_close) close( fd );
2688 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2689 break;
2691 case FilePositionInformation:
2692 if (len >= sizeof(FILE_POSITION_INFORMATION))
2694 const FILE_POSITION_INFORMATION *info = ptr;
2696 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2697 return io->u.Status;
2699 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2700 io->u.Status = FILE_GetNtStatus();
2702 if (needs_close) close( fd );
2704 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2705 break;
2707 case FileEndOfFileInformation:
2708 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2710 struct stat st;
2711 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2713 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2714 return io->u.Status;
2716 /* first try normal truncate */
2717 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2719 /* now check for the need to extend the file */
2720 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2722 static const char zero;
2724 /* extend the file one byte beyond the requested size and then truncate it */
2725 /* this should work around ftruncate implementations that can't extend files */
2726 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2727 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2729 io->u.Status = FILE_GetNtStatus();
2731 if (needs_close) close( fd );
2733 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2734 break;
2736 case FilePipeInformation:
2737 if (len >= sizeof(FILE_PIPE_INFORMATION))
2739 FILE_PIPE_INFORMATION *info = ptr;
2741 if ((info->CompletionMode | info->ReadMode) & ~1)
2743 io->u.Status = STATUS_INVALID_PARAMETER;
2744 break;
2747 SERVER_START_REQ( set_named_pipe_info )
2749 req->handle = wine_server_obj_handle( handle );
2750 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2751 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2752 io->u.Status = wine_server_call( req );
2754 SERVER_END_REQ;
2756 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2757 break;
2759 case FileMailslotSetInformation:
2761 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2763 SERVER_START_REQ( set_mailslot_info )
2765 req->handle = wine_server_obj_handle( handle );
2766 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2767 req->read_timeout = info->ReadTimeout.QuadPart;
2768 io->u.Status = wine_server_call( req );
2770 SERVER_END_REQ;
2772 break;
2774 case FileCompletionInformation:
2775 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2777 FILE_COMPLETION_INFORMATION *info = ptr;
2779 SERVER_START_REQ( set_completion_info )
2781 req->handle = wine_server_obj_handle( handle );
2782 req->chandle = wine_server_obj_handle( info->CompletionPort );
2783 req->ckey = info->CompletionKey;
2784 io->u.Status = wine_server_call( req );
2786 SERVER_END_REQ;
2787 } else
2788 io->u.Status = STATUS_INVALID_PARAMETER_3;
2789 break;
2791 case FileAllInformation:
2792 io->u.Status = STATUS_INVALID_INFO_CLASS;
2793 break;
2795 case FileValidDataLengthInformation:
2796 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2798 struct stat st;
2799 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2801 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2802 return io->u.Status;
2804 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2805 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2806 io->u.Status = STATUS_INVALID_PARAMETER;
2807 else
2809 #ifdef HAVE_FALLOCATE
2810 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2812 NTSTATUS status = FILE_GetNtStatus();
2813 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2814 else io->u.Status = status;
2816 #else
2817 FIXME( "setting valid data length not supported\n" );
2818 #endif
2820 if (needs_close) close( fd );
2822 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2823 break;
2825 case FileDispositionInformation:
2826 if (len >= sizeof(FILE_DISPOSITION_INFORMATION))
2828 FILE_DISPOSITION_INFORMATION *info = ptr;
2830 SERVER_START_REQ( set_fd_disp_info )
2832 req->handle = wine_server_obj_handle( handle );
2833 req->unlink = info->DoDeleteFile;
2834 io->u.Status = wine_server_call( req );
2836 SERVER_END_REQ;
2837 } else
2838 io->u.Status = STATUS_INVALID_PARAMETER_3;
2839 break;
2841 case FileRenameInformation:
2842 if (len >= sizeof(FILE_RENAME_INFORMATION))
2844 FILE_RENAME_INFORMATION *info = ptr;
2845 UNICODE_STRING name_str;
2846 OBJECT_ATTRIBUTES attr;
2847 ANSI_STRING unix_name;
2849 name_str.Buffer = info->FileName;
2850 name_str.Length = info->FileNameLength;
2851 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2853 attr.Length = sizeof(attr);
2854 attr.ObjectName = &name_str;
2855 attr.RootDirectory = info->RootDir;
2856 attr.Attributes = OBJ_CASE_INSENSITIVE;
2858 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2859 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2860 break;
2862 if (!info->Replace && io->u.Status == STATUS_SUCCESS)
2864 RtlFreeAnsiString( &unix_name );
2865 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2866 break;
2869 SERVER_START_REQ( set_fd_name_info )
2871 req->handle = wine_server_obj_handle( handle );
2872 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2873 req->link = FALSE;
2874 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2875 io->u.Status = wine_server_call( req );
2877 SERVER_END_REQ;
2879 RtlFreeAnsiString( &unix_name );
2881 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2882 break;
2884 case FileLinkInformation:
2885 if (len >= sizeof(FILE_LINK_INFORMATION))
2887 FILE_LINK_INFORMATION *info = ptr;
2888 UNICODE_STRING name_str;
2889 OBJECT_ATTRIBUTES attr;
2890 ANSI_STRING unix_name;
2892 name_str.Buffer = info->FileName;
2893 name_str.Length = info->FileNameLength;
2894 name_str.MaximumLength = info->FileNameLength + sizeof(WCHAR);
2896 attr.Length = sizeof(attr);
2897 attr.ObjectName = &name_str;
2898 attr.RootDirectory = info->RootDirectory;
2899 attr.Attributes = OBJ_CASE_INSENSITIVE;
2901 io->u.Status = nt_to_unix_file_name_attr( &attr, &unix_name, FILE_OPEN_IF );
2902 if (io->u.Status != STATUS_SUCCESS && io->u.Status != STATUS_NO_SUCH_FILE)
2903 break;
2905 if (!info->ReplaceIfExists && io->u.Status == STATUS_SUCCESS)
2907 RtlFreeAnsiString( &unix_name );
2908 io->u.Status = STATUS_OBJECT_NAME_COLLISION;
2909 break;
2912 SERVER_START_REQ( set_fd_name_info )
2914 req->handle = wine_server_obj_handle( handle );
2915 req->rootdir = wine_server_obj_handle( attr.RootDirectory );
2916 req->link = TRUE;
2917 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
2918 io->u.Status = wine_server_call( req );
2920 SERVER_END_REQ;
2922 RtlFreeAnsiString( &unix_name );
2924 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2925 break;
2927 default:
2928 FIXME("Unsupported class (%d)\n", class);
2929 io->u.Status = STATUS_NOT_IMPLEMENTED;
2930 break;
2932 io->Information = 0;
2933 return io->u.Status;
2937 /******************************************************************************
2938 * NtQueryFullAttributesFile (NTDLL.@)
2940 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2941 FILE_NETWORK_OPEN_INFORMATION *info )
2943 ANSI_STRING unix_name;
2944 NTSTATUS status;
2946 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2948 ULONG attributes;
2949 struct stat st;
2951 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2952 status = FILE_GetNtStatus();
2953 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2954 status = STATUS_INVALID_INFO_CLASS;
2955 else
2957 FILE_BASIC_INFORMATION basic;
2958 FILE_STANDARD_INFORMATION std;
2960 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2961 fill_file_info( &st, attributes, &std, FileStandardInformation );
2963 info->CreationTime = basic.CreationTime;
2964 info->LastAccessTime = basic.LastAccessTime;
2965 info->LastWriteTime = basic.LastWriteTime;
2966 info->ChangeTime = basic.ChangeTime;
2967 info->AllocationSize = std.AllocationSize;
2968 info->EndOfFile = std.EndOfFile;
2969 info->FileAttributes = basic.FileAttributes;
2970 if (DIR_is_hidden_file( attr->ObjectName ))
2971 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2973 RtlFreeAnsiString( &unix_name );
2975 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2976 return status;
2980 /******************************************************************************
2981 * NtQueryAttributesFile (NTDLL.@)
2982 * ZwQueryAttributesFile (NTDLL.@)
2984 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2986 ANSI_STRING unix_name;
2987 NTSTATUS status;
2989 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2991 ULONG attributes;
2992 struct stat st;
2994 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2995 status = FILE_GetNtStatus();
2996 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2997 status = STATUS_INVALID_INFO_CLASS;
2998 else
3000 status = fill_file_info( &st, attributes, info, FileBasicInformation );
3001 if (DIR_is_hidden_file( attr->ObjectName ))
3002 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
3004 RtlFreeAnsiString( &unix_name );
3006 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
3007 return status;
3011 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3012 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
3013 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
3014 unsigned int flags )
3016 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
3018 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3019 /* Don't assume read-only, let the mount options set it below */
3020 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3022 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
3023 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
3025 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3026 info->Characteristics |= FILE_REMOTE_DEVICE;
3028 else if (!strcmp("procfs", fstypename))
3029 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3030 else
3031 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3033 if (flags & MNT_RDONLY)
3034 info->Characteristics |= FILE_READ_ONLY_DEVICE;
3036 if (!(flags & MNT_LOCAL))
3038 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3039 info->Characteristics |= FILE_REMOTE_DEVICE;
3042 #endif
3044 static inline BOOL is_device_placeholder( int fd )
3046 static const char wine_placeholder[] = "Wine device placeholder";
3047 char buffer[sizeof(wine_placeholder)-1];
3049 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
3050 return FALSE;
3051 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
3054 /******************************************************************************
3055 * get_device_info
3057 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
3059 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
3061 struct stat st;
3063 info->Characteristics = 0;
3064 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
3065 if (S_ISCHR( st.st_mode ))
3067 info->DeviceType = FILE_DEVICE_UNKNOWN;
3068 #ifdef linux
3069 switch(major(st.st_rdev))
3071 case MEM_MAJOR:
3072 info->DeviceType = FILE_DEVICE_NULL;
3073 break;
3074 case TTY_MAJOR:
3075 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
3076 break;
3077 case LP_MAJOR:
3078 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
3079 break;
3080 case SCSI_TAPE_MAJOR:
3081 info->DeviceType = FILE_DEVICE_TAPE;
3082 break;
3084 #endif
3086 else if (S_ISBLK( st.st_mode ))
3088 info->DeviceType = FILE_DEVICE_DISK;
3090 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
3092 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
3094 else if (is_device_placeholder( fd ))
3096 info->DeviceType = FILE_DEVICE_DISK;
3098 else /* regular file or directory */
3100 #if defined(linux) && defined(HAVE_FSTATFS)
3101 struct statfs stfs;
3103 /* check for floppy disk */
3104 if (major(st.st_dev) == FLOPPY_MAJOR)
3105 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3107 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
3108 switch (stfs.f_type)
3110 case 0x9660: /* iso9660 */
3111 case 0x9fa1: /* supermount */
3112 case 0x15013346: /* udf */
3113 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3114 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3115 break;
3116 case 0x6969: /* nfs */
3117 case 0x517B: /* smbfs */
3118 case 0x564c: /* ncpfs */
3119 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
3120 info->Characteristics |= FILE_REMOTE_DEVICE;
3121 break;
3122 case 0x01021994: /* tmpfs */
3123 case 0x28cd3d45: /* cramfs */
3124 case 0x1373: /* devfs */
3125 case 0x9fa0: /* procfs */
3126 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3127 break;
3128 default:
3129 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3130 break;
3132 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
3133 struct statfs stfs;
3135 if (fstatfs( fd, &stfs ) < 0)
3136 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3137 else
3138 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
3139 #elif defined(__NetBSD__)
3140 struct statvfs stfs;
3142 if (fstatvfs( fd, &stfs) < 0)
3143 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3144 else
3145 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
3146 #elif defined(sun)
3147 /* Use dkio to work out device types */
3149 # include <sys/dkio.h>
3150 # include <sys/vtoc.h>
3151 struct dk_cinfo dkinf;
3152 int retval = ioctl(fd, DKIOCINFO, &dkinf);
3153 if(retval==-1){
3154 WARN("Unable to get disk device type information - assuming a disk like device\n");
3155 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3157 switch (dkinf.dki_ctype)
3159 case DKC_CDROM:
3160 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3161 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3162 break;
3163 case DKC_NCRFLOPPY:
3164 case DKC_SMSFLOPPY:
3165 case DKC_INTEL82072:
3166 case DKC_INTEL82077:
3167 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3168 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3169 break;
3170 case DKC_MD:
3171 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3172 break;
3173 default:
3174 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3177 #else
3178 static int warned;
3179 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3180 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3181 #endif
3182 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3184 return STATUS_SUCCESS;
3188 /******************************************************************************
3189 * NtQueryVolumeInformationFile [NTDLL.@]
3190 * ZwQueryVolumeInformationFile [NTDLL.@]
3192 * Get volume information for an open file handle.
3194 * PARAMS
3195 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3196 * io [O] Receives information about the operation on return
3197 * buffer [O] Destination for volume information
3198 * length [I] Size of FsInformation
3199 * info_class [I] Type of volume information to set
3201 * RETURNS
3202 * Success: 0. io and buffer are updated.
3203 * Failure: An NTSTATUS error code describing the error.
3205 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3206 PVOID buffer, ULONG length,
3207 FS_INFORMATION_CLASS info_class )
3209 int fd, needs_close;
3210 struct stat st;
3211 static int once;
3213 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
3214 return io->u.Status;
3216 io->u.Status = STATUS_NOT_IMPLEMENTED;
3217 io->Information = 0;
3219 switch( info_class )
3221 case FileFsVolumeInformation:
3222 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3223 break;
3224 case FileFsLabelInformation:
3225 FIXME( "%p: label info not supported\n", handle );
3226 break;
3227 case FileFsSizeInformation:
3228 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3229 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3230 else
3232 FILE_FS_SIZE_INFORMATION *info = buffer;
3234 if (fstat( fd, &st ) < 0)
3236 io->u.Status = FILE_GetNtStatus();
3237 break;
3239 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3241 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3243 else
3245 ULONGLONG bsize;
3246 /* Linux's fstatvfs is buggy */
3247 #if !defined(linux) || !defined(HAVE_FSTATFS)
3248 struct statvfs stfs;
3250 if (fstatvfs( fd, &stfs ) < 0)
3252 io->u.Status = FILE_GetNtStatus();
3253 break;
3255 bsize = stfs.f_frsize;
3256 #else
3257 struct statfs stfs;
3258 if (fstatfs( fd, &stfs ) < 0)
3260 io->u.Status = FILE_GetNtStatus();
3261 break;
3263 bsize = stfs.f_bsize;
3264 #endif
3265 if (bsize == 2048) /* assume CD-ROM */
3267 info->BytesPerSector = 2048;
3268 info->SectorsPerAllocationUnit = 1;
3270 else
3272 info->BytesPerSector = 512;
3273 info->SectorsPerAllocationUnit = 8;
3275 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3276 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3277 io->Information = sizeof(*info);
3278 io->u.Status = STATUS_SUCCESS;
3281 break;
3282 case FileFsDeviceInformation:
3283 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3284 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3285 else
3287 FILE_FS_DEVICE_INFORMATION *info = buffer;
3289 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3290 io->Information = sizeof(*info);
3292 break;
3293 case FileFsAttributeInformation:
3294 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3295 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3296 else
3298 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3300 FIXME( "%p: faking attribute info\n", handle );
3301 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3302 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3303 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3304 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3305 info->FileSystemNameLength = sizeof(ntfsW);
3306 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3308 io->Information = sizeof(*info);
3309 io->u.Status = STATUS_SUCCESS;
3311 break;
3312 case FileFsControlInformation:
3313 FIXME( "%p: control info not supported\n", handle );
3314 break;
3315 case FileFsFullSizeInformation:
3316 FIXME( "%p: full size info not supported\n", handle );
3317 break;
3318 case FileFsObjectIdInformation:
3319 FIXME( "%p: object id info not supported\n", handle );
3320 break;
3321 case FileFsMaximumInformation:
3322 FIXME( "%p: maximum info not supported\n", handle );
3323 break;
3324 default:
3325 io->u.Status = STATUS_INVALID_PARAMETER;
3326 break;
3328 if (needs_close) close( fd );
3329 return io->u.Status;
3333 /******************************************************************
3334 * NtQueryEaFile (NTDLL.@)
3336 * Read extended attributes from NTFS files.
3338 * PARAMS
3339 * hFile [I] File handle, must be opened with FILE_READ_EA access
3340 * iosb [O] Receives information about the operation on return
3341 * buffer [O] Output buffer
3342 * length [I] Length of output buffer
3343 * single_entry [I] Only read and return one entry
3344 * ea_list [I] Optional list with names of EAs to return
3345 * ea_list_len [I] Length of ea_list in bytes
3346 * ea_index [I] Optional pointer to 1-based index of attribute to return
3347 * restart [I] restart EA scan
3349 * RETURNS
3350 * Success: 0. Atrributes read into buffer
3351 * Failure: An NTSTATUS error code describing the error.
3353 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3354 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3355 PULONG ea_index, BOOLEAN restart )
3357 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3358 hFile, iosb, buffer, length, single_entry, ea_list,
3359 ea_list_len, ea_index, restart);
3360 return STATUS_ACCESS_DENIED;
3364 /******************************************************************
3365 * NtSetEaFile (NTDLL.@)
3367 * Update extended attributes for NTFS files.
3369 * PARAMS
3370 * hFile [I] File handle, must be opened with FILE_READ_EA access
3371 * iosb [O] Receives information about the operation on return
3372 * buffer [I] Buffer with EA information
3373 * length [I] Length of buffer
3375 * RETURNS
3376 * Success: 0. Attributes are updated
3377 * Failure: An NTSTATUS error code describing the error.
3379 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3381 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3382 return STATUS_ACCESS_DENIED;
3386 /******************************************************************
3387 * NtFlushBuffersFile (NTDLL.@)
3389 * Flush any buffered data on an open file handle.
3391 * PARAMS
3392 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3393 * IoStatusBlock [O] Receives information about the operation on return
3395 * RETURNS
3396 * Success: 0. IoStatusBlock is updated.
3397 * Failure: An NTSTATUS error code describing the error.
3399 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3401 NTSTATUS ret;
3402 HANDLE hEvent = NULL;
3403 enum server_fd_type type;
3404 int fd, needs_close;
3406 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3408 if (!ret && type == FD_TYPE_SERIAL)
3410 ret = COMM_FlushBuffersFile( fd );
3412 else
3414 SERVER_START_REQ( flush )
3416 req->blocking = 1; /* always blocking */
3417 req->async.handle = wine_server_obj_handle( hFile );
3418 req->async.iosb = wine_server_client_ptr( IoStatusBlock );
3419 ret = wine_server_call( req );
3420 hEvent = wine_server_ptr_handle( reply->event );
3422 SERVER_END_REQ;
3424 if (hEvent)
3426 NtWaitForSingleObject( hEvent, FALSE, NULL );
3427 NtClose( hEvent );
3428 ret = STATUS_SUCCESS;
3432 if (needs_close) close( fd );
3433 return ret;
3436 /******************************************************************
3437 * NtLockFile (NTDLL.@)
3441 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3442 PIO_APC_ROUTINE apc, void* apc_user,
3443 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3444 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3445 BOOLEAN exclusive )
3447 NTSTATUS ret;
3448 HANDLE handle;
3449 BOOLEAN async;
3450 static BOOLEAN warn = TRUE;
3452 if (apc || io_status || key)
3454 FIXME("Unimplemented yet parameter\n");
3455 return STATUS_NOT_IMPLEMENTED;
3458 if (apc_user && warn)
3460 FIXME("I/O completion on lock not implemented yet\n");
3461 warn = FALSE;
3464 for (;;)
3466 SERVER_START_REQ( lock_file )
3468 req->handle = wine_server_obj_handle( hFile );
3469 req->offset = offset->QuadPart;
3470 req->count = count->QuadPart;
3471 req->shared = !exclusive;
3472 req->wait = !dont_wait;
3473 ret = wine_server_call( req );
3474 handle = wine_server_ptr_handle( reply->handle );
3475 async = reply->overlapped;
3477 SERVER_END_REQ;
3478 if (ret != STATUS_PENDING)
3480 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3481 return ret;
3484 if (async)
3486 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3487 if (handle) NtClose( handle );
3488 return STATUS_PENDING;
3490 if (handle)
3492 NtWaitForSingleObject( handle, FALSE, NULL );
3493 NtClose( handle );
3495 else
3497 LARGE_INTEGER time;
3499 /* Unix lock conflict, sleep a bit and retry */
3500 time.QuadPart = 100 * (ULONGLONG)10000;
3501 time.QuadPart = -time.QuadPart;
3502 NtDelayExecution( FALSE, &time );
3508 /******************************************************************
3509 * NtUnlockFile (NTDLL.@)
3513 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3514 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3515 PULONG key )
3517 NTSTATUS status;
3519 TRACE( "%p %x%08x %x%08x\n",
3520 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3522 if (io_status || key)
3524 FIXME("Unimplemented yet parameter\n");
3525 return STATUS_NOT_IMPLEMENTED;
3528 SERVER_START_REQ( unlock_file )
3530 req->handle = wine_server_obj_handle( hFile );
3531 req->offset = offset->QuadPart;
3532 req->count = count->QuadPart;
3533 status = wine_server_call( req );
3535 SERVER_END_REQ;
3536 return status;
3539 /******************************************************************
3540 * NtCreateNamedPipeFile (NTDLL.@)
3544 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3545 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3546 ULONG sharing, ULONG dispo, ULONG options,
3547 ULONG pipe_type, ULONG read_mode,
3548 ULONG completion_mode, ULONG max_inst,
3549 ULONG inbound_quota, ULONG outbound_quota,
3550 PLARGE_INTEGER timeout)
3552 NTSTATUS status;
3553 data_size_t len;
3554 struct object_attributes *objattr;
3556 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3557 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3558 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3559 outbound_quota, timeout);
3561 if (!attr) return STATUS_INVALID_PARAMETER;
3563 /* assume we only get relative timeout */
3564 if (timeout->QuadPart > 0)
3565 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3567 if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
3569 SERVER_START_REQ( create_named_pipe )
3571 req->access = access;
3572 req->options = options;
3573 req->sharing = sharing;
3574 req->flags =
3575 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3576 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3577 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3578 req->maxinstances = max_inst;
3579 req->outsize = outbound_quota;
3580 req->insize = inbound_quota;
3581 req->timeout = timeout->QuadPart;
3582 wine_server_add_data( req, objattr, len );
3583 status = wine_server_call( req );
3584 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3586 SERVER_END_REQ;
3588 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3589 return status;
3592 /******************************************************************
3593 * NtDeleteFile (NTDLL.@)
3597 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3599 NTSTATUS status;
3600 HANDLE hFile;
3601 IO_STATUS_BLOCK io;
3603 TRACE("%p\n", ObjectAttributes);
3604 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3605 ObjectAttributes, &io, NULL, 0,
3606 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3607 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3608 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3609 return status;
3612 /******************************************************************
3613 * NtCancelIoFileEx (NTDLL.@)
3617 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3619 TRACE("%p %p %p\n", hFile, iosb, io_status );
3621 SERVER_START_REQ( cancel_async )
3623 req->handle = wine_server_obj_handle( hFile );
3624 req->iosb = wine_server_client_ptr( iosb );
3625 req->only_thread = FALSE;
3626 io_status->u.Status = wine_server_call( req );
3628 SERVER_END_REQ;
3630 return io_status->u.Status;
3633 /******************************************************************
3634 * NtCancelIoFile (NTDLL.@)
3638 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3640 TRACE("%p %p\n", hFile, io_status );
3642 SERVER_START_REQ( cancel_async )
3644 req->handle = wine_server_obj_handle( hFile );
3645 req->iosb = 0;
3646 req->only_thread = TRUE;
3647 io_status->u.Status = wine_server_call( req );
3649 SERVER_END_REQ;
3651 return io_status->u.Status;
3654 /******************************************************************************
3655 * NtCreateMailslotFile [NTDLL.@]
3656 * ZwCreateMailslotFile [NTDLL.@]
3658 * PARAMS
3659 * pHandle [O] pointer to receive the handle created
3660 * DesiredAccess [I] access mode (read, write, etc)
3661 * ObjectAttributes [I] fully qualified NT path of the mailslot
3662 * IoStatusBlock [O] receives completion status and other info
3663 * CreateOptions [I]
3664 * MailslotQuota [I]
3665 * MaxMessageSize [I]
3666 * TimeOut [I]
3668 * RETURNS
3669 * An NT status code
3671 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3672 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3673 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3674 PLARGE_INTEGER TimeOut)
3676 LARGE_INTEGER timeout;
3677 NTSTATUS ret;
3678 data_size_t len;
3679 struct object_attributes *objattr;
3681 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3682 pHandle, DesiredAccess, attr, IoStatusBlock,
3683 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3685 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3686 if (!attr) return STATUS_INVALID_PARAMETER;
3688 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3691 * For a NULL TimeOut pointer set the default timeout value
3693 if (!TimeOut)
3694 timeout.QuadPart = -1;
3695 else
3696 timeout.QuadPart = TimeOut->QuadPart;
3698 SERVER_START_REQ( create_mailslot )
3700 req->access = DesiredAccess;
3701 req->max_msgsize = MaxMessageSize;
3702 req->read_timeout = timeout.QuadPart;
3703 wine_server_add_data( req, objattr, len );
3704 ret = wine_server_call( req );
3705 if( ret == STATUS_SUCCESS )
3706 *pHandle = wine_server_ptr_handle( reply->handle );
3708 SERVER_END_REQ;
3710 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3711 return ret;