ntdll: Fix handling of async cancellation for directory changes.
[wine.git] / dlls / ntdll / file.c
blob43e52c6a0ae65afe7055c8d0010755c821bc94ee
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 HAVE_UTIME_H
61 # include <utime.h>
62 #endif
63 #ifdef HAVE_SYS_VFS_H
64 # include <sys/vfs.h>
65 #endif
66 #ifdef HAVE_SYS_MOUNT_H
67 # include <sys/mount.h>
68 #endif
69 #ifdef HAVE_SYS_STATFS_H
70 # include <sys/statfs.h>
71 #endif
72 #ifdef HAVE_TERMIOS_H
73 #include <termios.h>
74 #endif
75 #ifdef HAVE_VALGRIND_MEMCHECK_H
76 # include <valgrind/memcheck.h>
77 #endif
79 #include "ntstatus.h"
80 #define WIN32_NO_STATUS
81 #define NONAMELESSUNION
82 #include "wine/unicode.h"
83 #include "wine/debug.h"
84 #include "wine/server.h"
85 #include "ntdll_misc.h"
87 #include "winternl.h"
88 #include "winioctl.h"
89 #include "ddk/ntddk.h"
90 #include "ddk/ntddser.h"
92 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
93 WINE_DECLARE_DEBUG_CHANNEL(winediag);
95 mode_t FILE_umask = 0;
97 #define SECSPERDAY 86400
98 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
100 #define FILE_WRITE_TO_END_OF_FILE ((LONGLONG)-1)
101 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
103 static const WCHAR ntfsW[] = {'N','T','F','S'};
105 /* fetch the attributes of a file */
106 static inline ULONG get_file_attributes( const struct stat *st )
108 ULONG attr;
110 if (S_ISDIR(st->st_mode))
111 attr = FILE_ATTRIBUTE_DIRECTORY;
112 else
113 attr = FILE_ATTRIBUTE_ARCHIVE;
114 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
115 attr |= FILE_ATTRIBUTE_READONLY;
116 return attr;
119 /* get the stat info and file attributes for a file (by file descriptor) */
120 int fd_get_file_info( int fd, struct stat *st, ULONG *attr )
122 int ret;
124 *attr = 0;
125 ret = fstat( fd, st );
126 if (ret == -1) return ret;
127 *attr |= get_file_attributes( st );
128 return ret;
131 /* get the stat info and file attributes for a file (by name) */
132 int get_file_info( const char *path, struct stat *st, ULONG *attr )
134 int ret;
136 *attr = 0;
137 ret = lstat( path, st );
138 if (ret == -1) return ret;
139 if (S_ISLNK( st->st_mode ))
141 ret = stat( path, st );
142 if (ret == -1) return ret;
143 /* is a symbolic link and a directory, consider these "reparse points" */
144 if (S_ISDIR( st->st_mode )) *attr |= FILE_ATTRIBUTE_REPARSE_POINT;
146 *attr |= get_file_attributes( st );
147 return ret;
150 /**************************************************************************
151 * FILE_CreateFile (internal)
152 * Open a file.
154 * Parameter set fully identical with NtCreateFile
156 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
157 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
158 ULONG attributes, ULONG sharing, ULONG disposition,
159 ULONG options, PVOID ea_buffer, ULONG ea_length )
161 ANSI_STRING unix_name;
162 BOOL created = FALSE;
164 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
165 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
166 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
167 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
168 attributes, sharing, disposition, options, ea_buffer, ea_length );
170 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
172 if (alloc_size) FIXME( "alloc_size not supported\n" );
174 if (options & FILE_OPEN_BY_FILE_ID)
175 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
176 else
177 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
179 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
181 SERVER_START_REQ( open_file_object )
183 req->access = access;
184 req->attributes = attr->Attributes;
185 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
186 req->sharing = sharing;
187 req->options = options;
188 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
189 io->u.Status = wine_server_call( req );
190 *handle = wine_server_ptr_handle( reply->handle );
192 SERVER_END_REQ;
193 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
194 return io->u.Status;
197 if (io->u.Status == STATUS_NO_SUCH_FILE &&
198 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
200 created = TRUE;
201 io->u.Status = STATUS_SUCCESS;
204 if (io->u.Status == STATUS_SUCCESS)
206 struct security_descriptor *sd;
207 struct object_attributes objattr;
209 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
210 objattr.name_len = 0;
211 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
212 if (io->u.Status != STATUS_SUCCESS)
214 RtlFreeAnsiString( &unix_name );
215 return io->u.Status;
218 SERVER_START_REQ( create_file )
220 req->access = access;
221 req->attributes = attr->Attributes;
222 req->sharing = sharing;
223 req->create = disposition;
224 req->options = options;
225 req->attrs = attributes;
226 wine_server_add_data( req, &objattr, sizeof(objattr) );
227 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
228 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
229 io->u.Status = wine_server_call( req );
230 *handle = wine_server_ptr_handle( reply->handle );
232 SERVER_END_REQ;
233 NTDLL_free_struct_sd( sd );
234 RtlFreeAnsiString( &unix_name );
236 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
238 if (io->u.Status == STATUS_SUCCESS)
240 if (created) io->Information = FILE_CREATED;
241 else switch(disposition)
243 case FILE_SUPERSEDE:
244 io->Information = FILE_SUPERSEDED;
245 break;
246 case FILE_CREATE:
247 io->Information = FILE_CREATED;
248 break;
249 case FILE_OPEN:
250 case FILE_OPEN_IF:
251 io->Information = FILE_OPENED;
252 break;
253 case FILE_OVERWRITE:
254 case FILE_OVERWRITE_IF:
255 io->Information = FILE_OVERWRITTEN;
256 break;
259 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
261 static int once;
262 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
265 return io->u.Status;
268 /**************************************************************************
269 * NtOpenFile [NTDLL.@]
270 * ZwOpenFile [NTDLL.@]
272 * Open a file.
274 * PARAMS
275 * handle [O] Variable that receives the file handle on return
276 * access [I] Access desired by the caller to the file
277 * attr [I] Structure describing the file to be opened
278 * io [O] Receives details about the result of the operation
279 * sharing [I] Type of shared access the caller requires
280 * options [I] Options for the file open
282 * RETURNS
283 * Success: 0. FileHandle and IoStatusBlock are updated.
284 * Failure: An NTSTATUS error code describing the error.
286 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
287 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
288 ULONG sharing, ULONG options )
290 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
291 sharing, FILE_OPEN, options, NULL, 0 );
294 /**************************************************************************
295 * NtCreateFile [NTDLL.@]
296 * ZwCreateFile [NTDLL.@]
298 * Either create a new file or directory, or open an existing file, device,
299 * directory or volume.
301 * PARAMS
302 * handle [O] Points to a variable which receives the file handle on return
303 * access [I] Desired access to the file
304 * attr [I] Structure describing the file
305 * io [O] Receives information about the operation on return
306 * alloc_size [I] Initial size of the file in bytes
307 * attributes [I] Attributes to create the file with
308 * sharing [I] Type of shared access the caller would like to the file
309 * disposition [I] Specifies what to do, depending on whether the file already exists
310 * options [I] Options for creating a new file
311 * ea_buffer [I] Pointer to an extended attributes buffer
312 * ea_length [I] Length of ea_buffer
314 * RETURNS
315 * Success: 0. handle and io are updated.
316 * Failure: An NTSTATUS error code describing the error.
318 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
319 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
320 ULONG attributes, ULONG sharing, ULONG disposition,
321 ULONG options, PVOID ea_buffer, ULONG ea_length )
323 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
324 sharing, disposition, options, ea_buffer, ea_length );
327 /***********************************************************************
328 * Asynchronous file I/O *
331 struct async_fileio
333 struct async_fileio *next;
334 HANDLE handle;
335 PIO_APC_ROUTINE apc;
336 void *apc_arg;
339 struct async_fileio_read
341 struct async_fileio io;
342 char* buffer;
343 unsigned int already;
344 unsigned int count;
345 BOOL avail_mode;
348 struct async_fileio_write
350 struct async_fileio io;
351 const char *buffer;
352 unsigned int already;
353 unsigned int count;
356 struct async_irp
358 struct async_fileio io;
359 HANDLE event; /* async event */
360 void *buffer; /* buffer for output */
361 ULONG size; /* size of buffer */
364 static struct async_fileio *fileio_freelist;
366 static void release_fileio( struct async_fileio *io )
368 for (;;)
370 struct async_fileio *next = fileio_freelist;
371 io->next = next;
372 if (interlocked_cmpxchg_ptr( (void **)&fileio_freelist, io, next ) == next) return;
376 static struct async_fileio *alloc_fileio( DWORD size, HANDLE handle, PIO_APC_ROUTINE apc, void *arg )
378 /* first free remaining previous fileinfos */
380 struct async_fileio *io = interlocked_xchg_ptr( (void **)&fileio_freelist, NULL );
382 while (io)
384 struct async_fileio *next = io->next;
385 RtlFreeHeap( GetProcessHeap(), 0, io );
386 io = next;
389 if ((io = RtlAllocateHeap( GetProcessHeap(), 0, size )))
391 io->handle = handle;
392 io->apc = apc;
393 io->apc_arg = arg;
395 return io;
398 /* callback for irp async I/O completion */
399 static NTSTATUS irp_completion( void *user, IO_STATUS_BLOCK *io, NTSTATUS status, void **apc, void **arg )
401 struct async_irp *async = user;
403 if (status == STATUS_ALERTED)
405 SERVER_START_REQ( get_irp_result )
407 req->handle = wine_server_obj_handle( async->io.handle );
408 req->user_arg = wine_server_client_ptr( async );
409 wine_server_set_reply( req, async->buffer, async->size );
410 status = wine_server_call( req );
411 if (status != STATUS_PENDING) io->Information = reply->size;
413 SERVER_END_REQ;
415 if (status != STATUS_PENDING)
417 io->u.Status = status;
418 *apc = async->io.apc;
419 *arg = async->io.apc_arg;
420 release_fileio( &async->io );
422 return status;
425 /***********************************************************************
426 * FILE_GetNtStatus(void)
428 * Retrieve the Nt Status code from errno.
429 * Try to be consistent with FILE_SetDosError().
431 NTSTATUS FILE_GetNtStatus(void)
433 int err = errno;
435 TRACE( "errno = %d\n", errno );
436 switch (err)
438 case EAGAIN: return STATUS_SHARING_VIOLATION;
439 case EBADF: return STATUS_INVALID_HANDLE;
440 case EBUSY: return STATUS_DEVICE_BUSY;
441 case ENOSPC: return STATUS_DISK_FULL;
442 case EPERM:
443 case EROFS:
444 case EACCES: return STATUS_ACCESS_DENIED;
445 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
446 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
447 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
448 case EMFILE:
449 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
450 case EINVAL: return STATUS_INVALID_PARAMETER;
451 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
452 case EPIPE: return STATUS_PIPE_DISCONNECTED;
453 case EIO: return STATUS_DEVICE_NOT_READY;
454 #ifdef ENOMEDIUM
455 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
456 #endif
457 case ENXIO: return STATUS_NO_SUCH_DEVICE;
458 case ENOTTY:
459 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
460 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
461 case EFAULT: return STATUS_ACCESS_VIOLATION;
462 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
463 #ifdef ETIME /* Missing on FreeBSD */
464 case ETIME: return STATUS_IO_TIMEOUT;
465 #endif
466 case ENOEXEC: /* ?? */
467 case EEXIST: /* ?? */
468 default:
469 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
470 return STATUS_UNSUCCESSFUL;
474 /***********************************************************************
475 * FILE_AsyncReadService (INTERNAL)
477 static NTSTATUS FILE_AsyncReadService( void *user, IO_STATUS_BLOCK *iosb,
478 NTSTATUS status, void **apc, void **arg )
480 struct async_fileio_read *fileio = user;
481 int fd, needs_close, result;
483 switch (status)
485 case STATUS_ALERTED: /* got some new data */
486 /* check to see if the data is ready (non-blocking) */
487 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
488 &needs_close, NULL, NULL )))
489 break;
491 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
492 if (needs_close) close( fd );
494 if (result < 0)
496 if (errno == EAGAIN || errno == EINTR)
497 status = STATUS_PENDING;
498 else /* check to see if the transfer is complete */
499 status = FILE_GetNtStatus();
501 else if (result == 0)
503 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
505 else
507 fileio->already += result;
508 if (fileio->already >= fileio->count || fileio->avail_mode)
509 status = STATUS_SUCCESS;
510 else
512 /* if we only have to read the available data, and none is available,
513 * simply cancel the request. If data was available, it has been read
514 * while in by previous call (NtDelayExecution)
516 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
519 break;
521 case STATUS_TIMEOUT:
522 case STATUS_IO_TIMEOUT:
523 if (fileio->already) status = STATUS_SUCCESS;
524 break;
526 if (status != STATUS_PENDING)
528 iosb->u.Status = status;
529 iosb->Information = fileio->already;
530 *apc = fileio->io.apc;
531 *arg = fileio->io.apc_arg;
532 release_fileio( &fileio->io );
534 return status;
537 /* do a read call through the server */
538 static NTSTATUS server_read_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
539 IO_STATUS_BLOCK *io, void *buffer, ULONG size,
540 LARGE_INTEGER *offset, ULONG *key )
542 struct async_irp *async;
543 NTSTATUS status;
544 HANDLE wait_handle;
545 ULONG options;
546 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
548 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), handle, apc, apc_context )))
549 return STATUS_NO_MEMORY;
551 async->event = event;
552 async->buffer = buffer;
553 async->size = size;
555 SERVER_START_REQ( read )
557 req->blocking = !apc && !event && !cvalue;
558 req->async.handle = wine_server_obj_handle( handle );
559 req->async.callback = wine_server_client_ptr( irp_completion );
560 req->async.iosb = wine_server_client_ptr( io );
561 req->async.arg = wine_server_client_ptr( async );
562 req->async.event = wine_server_obj_handle( event );
563 req->async.cvalue = cvalue;
564 req->pos = offset ? offset->QuadPart : 0;
565 wine_server_set_reply( req, buffer, size );
566 status = wine_server_call( req );
567 wait_handle = wine_server_ptr_handle( reply->wait );
568 options = reply->options;
569 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
571 SERVER_END_REQ;
573 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
575 if (wait_handle)
577 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
578 status = io->u.Status;
579 NtClose( wait_handle );
582 return status;
585 /* do a write call through the server */
586 static NTSTATUS server_write_file( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, void *apc_context,
587 IO_STATUS_BLOCK *io, const void *buffer, ULONG size,
588 LARGE_INTEGER *offset, ULONG *key )
590 struct async_irp *async;
591 NTSTATUS status;
592 HANDLE wait_handle;
593 ULONG options;
594 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
596 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), handle, apc, apc_context )))
597 return STATUS_NO_MEMORY;
599 async->event = event;
600 async->buffer = NULL;
601 async->size = 0;
603 SERVER_START_REQ( write )
605 req->blocking = !apc && !event && !cvalue;
606 req->async.handle = wine_server_obj_handle( handle );
607 req->async.callback = wine_server_client_ptr( irp_completion );
608 req->async.iosb = wine_server_client_ptr( io );
609 req->async.arg = wine_server_client_ptr( async );
610 req->async.event = wine_server_obj_handle( event );
611 req->async.cvalue = cvalue;
612 req->pos = offset ? offset->QuadPart : 0;
613 wine_server_add_data( req, buffer, size );
614 status = wine_server_call( req );
615 wait_handle = wine_server_ptr_handle( reply->wait );
616 options = reply->options;
617 if (status != STATUS_PENDING) io->Information = reply->size;
619 SERVER_END_REQ;
621 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
623 if (wait_handle)
625 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
626 status = io->u.Status;
627 NtClose( wait_handle );
630 return status;
633 struct io_timeouts
635 int interval; /* max interval between two bytes */
636 int total; /* total timeout for the whole operation */
637 int end_time; /* absolute time of end of operation */
640 /* retrieve the I/O timeouts to use for a given handle */
641 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
642 struct io_timeouts *timeouts )
644 NTSTATUS status = STATUS_SUCCESS;
646 timeouts->interval = timeouts->total = -1;
648 switch(type)
650 case FD_TYPE_SERIAL:
652 /* GetCommTimeouts */
653 SERIAL_TIMEOUTS st;
654 IO_STATUS_BLOCK io;
656 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
657 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
658 if (status) break;
660 if (is_read)
662 if (st.ReadIntervalTimeout)
663 timeouts->interval = st.ReadIntervalTimeout;
665 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
667 timeouts->total = st.ReadTotalTimeoutConstant;
668 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
669 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
671 else if (st.ReadIntervalTimeout == MAXDWORD)
672 timeouts->interval = timeouts->total = 0;
674 else /* write */
676 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
678 timeouts->total = st.WriteTotalTimeoutConstant;
679 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
680 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
684 break;
685 case FD_TYPE_MAILSLOT:
686 if (is_read)
688 timeouts->interval = 0; /* return as soon as we got something */
689 SERVER_START_REQ( set_mailslot_info )
691 req->handle = wine_server_obj_handle( handle );
692 req->flags = 0;
693 if (!(status = wine_server_call( req )) &&
694 reply->read_timeout != TIMEOUT_INFINITE)
695 timeouts->total = reply->read_timeout / -10000;
697 SERVER_END_REQ;
699 break;
700 case FD_TYPE_SOCKET:
701 case FD_TYPE_PIPE:
702 case FD_TYPE_CHAR:
703 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
704 break;
705 default:
706 break;
708 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
709 return STATUS_SUCCESS;
713 /* retrieve the timeout for the next wait, in milliseconds */
714 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
716 int ret = -1;
718 if (timeouts->total != -1)
720 ret = timeouts->end_time - NtGetTickCount();
721 if (ret < 0) ret = 0;
723 if (already && timeouts->interval != -1)
725 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
727 return ret;
731 /* retrieve the avail_mode flag for async reads */
732 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
734 NTSTATUS status = STATUS_SUCCESS;
736 switch(type)
738 case FD_TYPE_SERIAL:
740 /* GetCommTimeouts */
741 SERIAL_TIMEOUTS st;
742 IO_STATUS_BLOCK io;
744 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
745 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
746 if (status) break;
747 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
748 !st.ReadTotalTimeoutConstant &&
749 st.ReadIntervalTimeout == MAXDWORD);
751 break;
752 case FD_TYPE_MAILSLOT:
753 case FD_TYPE_SOCKET:
754 case FD_TYPE_PIPE:
755 case FD_TYPE_CHAR:
756 *avail_mode = TRUE;
757 break;
758 default:
759 *avail_mode = FALSE;
760 break;
762 return status;
766 /******************************************************************************
767 * NtReadFile [NTDLL.@]
768 * ZwReadFile [NTDLL.@]
770 * Read from an open file handle.
772 * PARAMS
773 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
774 * Event [I] Event to signal upon completion (or NULL)
775 * ApcRoutine [I] Callback to call upon completion (or NULL)
776 * ApcContext [I] Context for ApcRoutine (or NULL)
777 * IoStatusBlock [O] Receives information about the operation on return
778 * Buffer [O] Destination for the data read
779 * Length [I] Size of Buffer
780 * ByteOffset [O] Destination for the new file pointer position (or NULL)
781 * Key [O] Function unknown (may be NULL)
783 * RETURNS
784 * Success: 0. IoStatusBlock is updated, and the Information member contains
785 * The number of bytes read.
786 * Failure: An NTSTATUS error code describing the error.
788 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
789 PIO_APC_ROUTINE apc, void* apc_user,
790 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
791 PLARGE_INTEGER offset, PULONG key)
793 int result, unix_handle, needs_close;
794 unsigned int options;
795 struct io_timeouts timeouts;
796 NTSTATUS status;
797 ULONG total = 0;
798 enum server_fd_type type;
799 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
800 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
802 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
803 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
805 if (!io_status) return STATUS_ACCESS_VIOLATION;
807 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
808 &needs_close, &type, &options );
809 if (status == STATUS_BAD_DEVICE_TYPE)
810 return server_read_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
812 if (status) return status;
814 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
816 if (!virtual_check_buffer_for_write( buffer, length ))
818 status = STATUS_ACCESS_VIOLATION;
819 goto done;
822 if (type == FD_TYPE_FILE)
824 if (async_read && (!offset || offset->QuadPart < 0))
826 status = STATUS_INVALID_PARAMETER;
827 goto done;
830 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
832 /* async I/O doesn't make sense on regular files */
833 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
835 if (errno != EINTR)
837 status = FILE_GetNtStatus();
838 goto done;
841 if (!async_read)
842 /* update file pointer position */
843 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
845 total = result;
846 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
847 goto done;
850 else if (type == FD_TYPE_SERIAL)
852 if (async_read && (!offset || offset->QuadPart < 0))
854 status = STATUS_INVALID_PARAMETER;
855 goto done;
859 for (;;)
861 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
863 total += result;
864 if (!result || total == length)
866 if (total)
868 status = STATUS_SUCCESS;
869 goto done;
871 switch (type)
873 case FD_TYPE_FILE:
874 case FD_TYPE_CHAR:
875 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
876 goto done;
877 case FD_TYPE_SERIAL:
878 break;
879 default:
880 status = STATUS_PIPE_BROKEN;
881 goto done;
884 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
886 else if (errno != EAGAIN)
888 if (errno == EINTR) continue;
889 if (!total) status = FILE_GetNtStatus();
890 goto done;
893 if (async_read)
895 struct async_fileio_read *fileio;
896 BOOL avail_mode;
898 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
899 goto err;
900 if (total && avail_mode)
902 status = STATUS_SUCCESS;
903 goto done;
906 fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), hFile, apc, apc_user );
907 if (!fileio)
909 status = STATUS_NO_MEMORY;
910 goto err;
912 fileio->already = total;
913 fileio->count = length;
914 fileio->buffer = buffer;
915 fileio->avail_mode = avail_mode;
917 SERVER_START_REQ( register_async )
919 req->type = ASYNC_TYPE_READ;
920 req->count = length;
921 req->async.handle = wine_server_obj_handle( hFile );
922 req->async.event = wine_server_obj_handle( hEvent );
923 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
924 req->async.iosb = wine_server_client_ptr( io_status );
925 req->async.arg = wine_server_client_ptr( fileio );
926 req->async.cvalue = cvalue;
927 status = wine_server_call( req );
929 SERVER_END_REQ;
931 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
932 goto err;
934 else /* synchronous read, wait for the fd to become ready */
936 struct pollfd pfd;
937 int ret, timeout;
939 if (!timeout_init_done)
941 timeout_init_done = TRUE;
942 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
943 goto err;
944 if (hEvent) NtResetEvent( hEvent, NULL );
946 timeout = get_next_io_timeout( &timeouts, total );
948 pfd.fd = unix_handle;
949 pfd.events = POLLIN;
951 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
953 if (total) /* return with what we got so far */
954 status = STATUS_SUCCESS;
955 else
956 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
957 goto done;
959 if (ret == -1 && errno != EINTR)
961 status = FILE_GetNtStatus();
962 goto done;
964 /* will now restart the read */
968 done:
969 send_completion = cvalue != 0;
971 err:
972 if (needs_close) close( unix_handle );
973 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
975 io_status->u.Status = status;
976 io_status->Information = total;
977 TRACE("= SUCCESS (%u)\n", total);
978 if (hEvent) NtSetEvent( hEvent, NULL );
979 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
980 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
982 else
984 TRACE("= 0x%08x\n", status);
985 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
988 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
990 return status;
994 /******************************************************************************
995 * NtReadFileScatter [NTDLL.@]
996 * ZwReadFileScatter [NTDLL.@]
998 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
999 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1000 ULONG length, PLARGE_INTEGER offset, PULONG key )
1002 int result, unix_handle, needs_close;
1003 unsigned int options;
1004 NTSTATUS status;
1005 ULONG pos = 0, total = 0;
1006 enum server_fd_type type;
1007 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1008 BOOL send_completion = FALSE;
1010 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1011 file, event, apc, apc_user, io_status, segments, length, offset, key);
1013 if (length % page_size) return STATUS_INVALID_PARAMETER;
1014 if (!io_status) return STATUS_ACCESS_VIOLATION;
1016 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1017 &needs_close, &type, &options );
1018 if (status) return status;
1020 if ((type != FD_TYPE_FILE) ||
1021 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1022 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1024 status = STATUS_INVALID_PARAMETER;
1025 goto error;
1028 while (length)
1030 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1031 result = pread( unix_handle, (char *)segments->Buffer + pos,
1032 page_size - pos, offset->QuadPart + total );
1033 else
1034 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1036 if (result == -1)
1038 if (errno == EINTR) continue;
1039 status = FILE_GetNtStatus();
1040 break;
1042 if (!result)
1044 status = STATUS_END_OF_FILE;
1045 break;
1047 total += result;
1048 length -= result;
1049 if ((pos += result) == page_size)
1051 pos = 0;
1052 segments++;
1056 send_completion = cvalue != 0;
1058 error:
1059 if (needs_close) close( unix_handle );
1060 if (status == STATUS_SUCCESS)
1062 io_status->u.Status = status;
1063 io_status->Information = total;
1064 TRACE("= SUCCESS (%u)\n", total);
1065 if (event) NtSetEvent( event, NULL );
1066 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1067 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1069 else
1071 TRACE("= 0x%08x\n", status);
1072 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1075 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1077 return status;
1081 /***********************************************************************
1082 * FILE_AsyncWriteService (INTERNAL)
1084 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb,
1085 NTSTATUS status, void **apc, void **arg )
1087 struct async_fileio_write *fileio = user;
1088 int result, fd, needs_close;
1089 enum server_fd_type type;
1091 switch (status)
1093 case STATUS_ALERTED:
1094 /* write some data (non-blocking) */
1095 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1096 &needs_close, &type, NULL )))
1097 break;
1099 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1100 result = send( fd, fileio->buffer, 0, 0 );
1101 else
1102 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1104 if (needs_close) close( fd );
1106 if (result < 0)
1108 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1109 else status = FILE_GetNtStatus();
1111 else
1113 fileio->already += result;
1114 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1116 break;
1118 case STATUS_TIMEOUT:
1119 case STATUS_IO_TIMEOUT:
1120 if (fileio->already) status = STATUS_SUCCESS;
1121 break;
1123 if (status != STATUS_PENDING)
1125 iosb->u.Status = status;
1126 iosb->Information = fileio->already;
1127 *apc = fileio->io.apc;
1128 *arg = fileio->io.apc_arg;
1129 release_fileio( &fileio->io );
1131 return status;
1134 static NTSTATUS set_pending_write( HANDLE device )
1136 NTSTATUS status;
1138 SERVER_START_REQ( set_serial_info )
1140 req->handle = wine_server_obj_handle( device );
1141 req->flags = SERIALINFO_PENDING_WRITE;
1142 status = wine_server_call( req );
1144 SERVER_END_REQ;
1145 return status;
1148 /******************************************************************************
1149 * NtWriteFile [NTDLL.@]
1150 * ZwWriteFile [NTDLL.@]
1152 * Write to an open file handle.
1154 * PARAMS
1155 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1156 * Event [I] Event to signal upon completion (or NULL)
1157 * ApcRoutine [I] Callback to call upon completion (or NULL)
1158 * ApcContext [I] Context for ApcRoutine (or NULL)
1159 * IoStatusBlock [O] Receives information about the operation on return
1160 * Buffer [I] Source for the data to write
1161 * Length [I] Size of Buffer
1162 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1163 * Key [O] Function unknown (may be NULL)
1165 * RETURNS
1166 * Success: 0. IoStatusBlock is updated, and the Information member contains
1167 * The number of bytes written.
1168 * Failure: An NTSTATUS error code describing the error.
1170 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1171 PIO_APC_ROUTINE apc, void* apc_user,
1172 PIO_STATUS_BLOCK io_status,
1173 const void* buffer, ULONG length,
1174 PLARGE_INTEGER offset, PULONG key)
1176 int result, unix_handle, needs_close;
1177 unsigned int options;
1178 struct io_timeouts timeouts;
1179 NTSTATUS status;
1180 ULONG total = 0;
1181 enum server_fd_type type;
1182 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1183 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1184 LARGE_INTEGER offset_eof;
1186 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1187 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1189 if (!io_status) return STATUS_ACCESS_VIOLATION;
1191 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1192 &needs_close, &type, &options );
1193 if (status == STATUS_BAD_DEVICE_TYPE)
1194 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1196 if (status == STATUS_ACCESS_DENIED)
1198 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1199 &needs_close, &type, &options );
1200 append_write = TRUE;
1202 if (status) return status;
1204 if (!virtual_check_buffer_for_read( buffer, length ))
1206 status = STATUS_INVALID_USER_BUFFER;
1207 goto done;
1210 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1212 if (type == FD_TYPE_FILE)
1214 if (async_write &&
1215 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1217 status = STATUS_INVALID_PARAMETER;
1218 goto done;
1221 if (append_write)
1223 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1224 offset = &offset_eof;
1227 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1229 off_t off = offset->QuadPart;
1231 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1233 struct stat st;
1235 if (fstat( unix_handle, &st ) == -1)
1237 status = FILE_GetNtStatus();
1238 goto done;
1240 off = st.st_size;
1242 else if (offset->QuadPart < 0)
1244 status = STATUS_INVALID_PARAMETER;
1245 goto done;
1248 /* async I/O doesn't make sense on regular files */
1249 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1251 if (errno != EINTR)
1253 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1254 else status = FILE_GetNtStatus();
1255 goto done;
1259 if (!async_write)
1260 /* update file pointer position */
1261 lseek( unix_handle, off + result, SEEK_SET );
1263 total = result;
1264 status = STATUS_SUCCESS;
1265 goto done;
1268 else if (type == FD_TYPE_SERIAL)
1270 if (async_write &&
1271 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1273 status = STATUS_INVALID_PARAMETER;
1274 goto done;
1278 for (;;)
1280 /* zero-length writes on sockets may not work with plain write(2) */
1281 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1282 result = send( unix_handle, buffer, 0, 0 );
1283 else
1284 result = write( unix_handle, (const char *)buffer + total, length - total );
1286 if (result >= 0)
1288 total += result;
1289 if (total == length)
1291 status = STATUS_SUCCESS;
1292 goto done;
1294 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1296 else if (errno != EAGAIN)
1298 if (errno == EINTR) continue;
1299 if (!total)
1301 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1302 else status = FILE_GetNtStatus();
1304 goto done;
1307 if (async_write)
1309 struct async_fileio_write *fileio;
1311 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), hFile, apc, apc_user );
1312 if (!fileio)
1314 status = STATUS_NO_MEMORY;
1315 goto err;
1317 fileio->already = total;
1318 fileio->count = length;
1319 fileio->buffer = buffer;
1321 SERVER_START_REQ( register_async )
1323 req->type = ASYNC_TYPE_WRITE;
1324 req->count = length;
1325 req->async.handle = wine_server_obj_handle( hFile );
1326 req->async.event = wine_server_obj_handle( hEvent );
1327 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1328 req->async.iosb = wine_server_client_ptr( io_status );
1329 req->async.arg = wine_server_client_ptr( fileio );
1330 req->async.cvalue = cvalue;
1331 status = wine_server_call( req );
1333 SERVER_END_REQ;
1335 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1336 goto err;
1338 else /* synchronous write, wait for the fd to become ready */
1340 struct pollfd pfd;
1341 int ret, timeout;
1343 if (!timeout_init_done)
1345 timeout_init_done = TRUE;
1346 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1347 goto err;
1348 if (hEvent) NtResetEvent( hEvent, NULL );
1350 timeout = get_next_io_timeout( &timeouts, total );
1352 pfd.fd = unix_handle;
1353 pfd.events = POLLOUT;
1355 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1357 /* return with what we got so far */
1358 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1359 goto done;
1361 if (ret == -1 && errno != EINTR)
1363 status = FILE_GetNtStatus();
1364 goto done;
1366 /* will now restart the write */
1370 done:
1371 send_completion = cvalue != 0;
1373 err:
1374 if (needs_close) close( unix_handle );
1376 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1377 set_pending_write( hFile );
1379 if (status == STATUS_SUCCESS)
1381 io_status->u.Status = status;
1382 io_status->Information = total;
1383 TRACE("= SUCCESS (%u)\n", total);
1384 if (hEvent) NtSetEvent( hEvent, NULL );
1385 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1386 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1388 else
1390 TRACE("= 0x%08x\n", status);
1391 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1394 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1396 return status;
1400 /******************************************************************************
1401 * NtWriteFileGather [NTDLL.@]
1402 * ZwWriteFileGather [NTDLL.@]
1404 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1405 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1406 ULONG length, PLARGE_INTEGER offset, PULONG key )
1408 int result, unix_handle, needs_close;
1409 unsigned int options;
1410 NTSTATUS status;
1411 ULONG pos = 0, total = 0;
1412 enum server_fd_type type;
1413 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1414 BOOL send_completion = FALSE;
1416 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1417 file, event, apc, apc_user, io_status, segments, length, offset, key);
1419 if (length % page_size) return STATUS_INVALID_PARAMETER;
1420 if (!io_status) return STATUS_ACCESS_VIOLATION;
1422 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1423 &needs_close, &type, &options );
1424 if (status) return status;
1426 if ((type != FD_TYPE_FILE) ||
1427 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1428 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1430 status = STATUS_INVALID_PARAMETER;
1431 goto error;
1434 while (length)
1436 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1437 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1438 page_size - pos, offset->QuadPart + total );
1439 else
1440 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1442 if (result == -1)
1444 if (errno == EINTR) continue;
1445 if (errno == EFAULT)
1447 status = STATUS_INVALID_USER_BUFFER;
1448 goto error;
1450 status = FILE_GetNtStatus();
1451 break;
1453 if (!result)
1455 status = STATUS_DISK_FULL;
1456 break;
1458 total += result;
1459 length -= result;
1460 if ((pos += result) == page_size)
1462 pos = 0;
1463 segments++;
1467 send_completion = cvalue != 0;
1469 error:
1470 if (needs_close) close( unix_handle );
1471 if (status == STATUS_SUCCESS)
1473 io_status->u.Status = status;
1474 io_status->Information = total;
1475 TRACE("= SUCCESS (%u)\n", total);
1476 if (event) NtSetEvent( event, NULL );
1477 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1478 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1480 else
1482 TRACE("= 0x%08x\n", status);
1483 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1486 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1488 return status;
1492 /* do an ioctl call through the server */
1493 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1494 PIO_APC_ROUTINE apc, PVOID apc_context,
1495 IO_STATUS_BLOCK *io, ULONG code,
1496 const void *in_buffer, ULONG in_size,
1497 PVOID out_buffer, ULONG out_size )
1499 struct async_irp *async;
1500 NTSTATUS status;
1501 HANDLE wait_handle;
1502 ULONG options;
1503 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1505 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), handle, apc, apc_context )))
1506 return STATUS_NO_MEMORY;
1507 async->event = event;
1508 async->buffer = out_buffer;
1509 async->size = out_size;
1511 SERVER_START_REQ( ioctl )
1513 req->code = code;
1514 req->blocking = !apc && !event && !cvalue;
1515 req->async.handle = wine_server_obj_handle( handle );
1516 req->async.callback = wine_server_client_ptr( irp_completion );
1517 req->async.iosb = wine_server_client_ptr( io );
1518 req->async.arg = wine_server_client_ptr( async );
1519 req->async.event = wine_server_obj_handle( event );
1520 req->async.cvalue = cvalue;
1521 wine_server_add_data( req, in_buffer, in_size );
1522 wine_server_set_reply( req, out_buffer, out_size );
1523 status = wine_server_call( req );
1524 wait_handle = wine_server_ptr_handle( reply->wait );
1525 options = reply->options;
1526 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1528 SERVER_END_REQ;
1530 if (status == STATUS_NOT_SUPPORTED)
1531 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1532 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1534 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1536 if (wait_handle)
1538 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1539 status = io->u.Status;
1540 NtClose( wait_handle );
1543 return status;
1546 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1547 * server */
1548 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1549 ULONG in_size)
1551 #ifdef VALGRIND_MAKE_MEM_DEFINED
1552 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1553 do { \
1554 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1555 if ((size) >= FIELD_OFFSET(t, f2)) \
1556 VALGRIND_MAKE_MEM_DEFINED( \
1557 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1558 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1559 } while (0)
1561 switch (code)
1563 case FSCTL_PIPE_WAIT:
1564 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1565 break;
1567 #endif
1571 /**************************************************************************
1572 * NtDeviceIoControlFile [NTDLL.@]
1573 * ZwDeviceIoControlFile [NTDLL.@]
1575 * Perform an I/O control operation on an open file handle.
1577 * PARAMS
1578 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1579 * event [I] Event to signal upon completion (or NULL)
1580 * apc [I] Callback to call upon completion (or NULL)
1581 * apc_context [I] Context for ApcRoutine (or NULL)
1582 * io [O] Receives information about the operation on return
1583 * code [I] Control code for the operation to perform
1584 * in_buffer [I] Source for any input data required (or NULL)
1585 * in_size [I] Size of InputBuffer
1586 * out_buffer [O] Source for any output data returned (or NULL)
1587 * out_size [I] Size of OutputBuffer
1589 * RETURNS
1590 * Success: 0. IoStatusBlock is updated.
1591 * Failure: An NTSTATUS error code describing the error.
1593 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1594 PIO_APC_ROUTINE apc, PVOID apc_context,
1595 PIO_STATUS_BLOCK io, ULONG code,
1596 PVOID in_buffer, ULONG in_size,
1597 PVOID out_buffer, ULONG out_size)
1599 ULONG device = (code >> 16);
1600 NTSTATUS status = STATUS_NOT_SUPPORTED;
1602 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1603 handle, event, apc, apc_context, io, code,
1604 in_buffer, in_size, out_buffer, out_size);
1606 switch(device)
1608 case FILE_DEVICE_DISK:
1609 case FILE_DEVICE_CD_ROM:
1610 case FILE_DEVICE_DVD:
1611 case FILE_DEVICE_CONTROLLER:
1612 case FILE_DEVICE_MASS_STORAGE:
1613 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1614 in_buffer, in_size, out_buffer, out_size);
1615 break;
1616 case FILE_DEVICE_SERIAL_PORT:
1617 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1618 in_buffer, in_size, out_buffer, out_size);
1619 break;
1620 case FILE_DEVICE_TAPE:
1621 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1622 in_buffer, in_size, out_buffer, out_size);
1623 break;
1626 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1627 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1628 in_buffer, in_size, out_buffer, out_size );
1630 if (status != STATUS_PENDING) io->u.Status = status;
1631 return status;
1635 /**************************************************************************
1636 * NtFsControlFile [NTDLL.@]
1637 * ZwFsControlFile [NTDLL.@]
1639 * Perform a file system control operation on an open file handle.
1641 * PARAMS
1642 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1643 * event [I] Event to signal upon completion (or NULL)
1644 * apc [I] Callback to call upon completion (or NULL)
1645 * apc_context [I] Context for ApcRoutine (or NULL)
1646 * io [O] Receives information about the operation on return
1647 * code [I] Control code for the operation to perform
1648 * in_buffer [I] Source for any input data required (or NULL)
1649 * in_size [I] Size of InputBuffer
1650 * out_buffer [O] Source for any output data returned (or NULL)
1651 * out_size [I] Size of OutputBuffer
1653 * RETURNS
1654 * Success: 0. IoStatusBlock is updated.
1655 * Failure: An NTSTATUS error code describing the error.
1657 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1658 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1659 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1661 NTSTATUS status;
1663 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1664 handle, event, apc, apc_context, io, code,
1665 in_buffer, in_size, out_buffer, out_size);
1667 if (!io) return STATUS_INVALID_PARAMETER;
1669 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1671 switch(code)
1673 case FSCTL_DISMOUNT_VOLUME:
1674 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1675 in_buffer, in_size, out_buffer, out_size );
1676 if (!status) status = DIR_unmount_device( handle );
1677 break;
1679 case FSCTL_PIPE_PEEK:
1681 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1682 int avail = 0, fd, needs_close;
1684 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1686 status = STATUS_INFO_LENGTH_MISMATCH;
1687 break;
1690 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1691 break;
1693 #ifdef FIONREAD
1694 if (ioctl( fd, FIONREAD, &avail ) != 0)
1696 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1697 if (needs_close) close( fd );
1698 status = FILE_GetNtStatus();
1699 break;
1701 #endif
1702 if (!avail) /* check for closed pipe */
1704 struct pollfd pollfd;
1705 int ret;
1707 pollfd.fd = fd;
1708 pollfd.events = POLLIN;
1709 pollfd.revents = 0;
1710 ret = poll( &pollfd, 1, 0 );
1711 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1713 if (needs_close) close( fd );
1714 status = STATUS_PIPE_BROKEN;
1715 break;
1718 buffer->NamedPipeState = 0; /* FIXME */
1719 buffer->ReadDataAvailable = avail;
1720 buffer->NumberOfMessages = 0; /* FIXME */
1721 buffer->MessageLength = 0; /* FIXME */
1722 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1723 status = STATUS_SUCCESS;
1724 if (avail)
1726 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1727 if (data_size)
1729 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1730 if (res >= 0) io->Information += res;
1733 if (needs_close) close( fd );
1735 break;
1737 case FSCTL_PIPE_DISCONNECT:
1738 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1739 in_buffer, in_size, out_buffer, out_size );
1740 if (!status)
1742 int fd = server_remove_fd_from_cache( handle );
1743 if (fd != -1) close( fd );
1745 break;
1747 case FSCTL_PIPE_IMPERSONATE:
1748 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1749 status = RtlImpersonateSelf( SecurityImpersonation );
1750 break;
1752 case FSCTL_IS_VOLUME_MOUNTED:
1753 case FSCTL_LOCK_VOLUME:
1754 case FSCTL_UNLOCK_VOLUME:
1755 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1756 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1757 status = STATUS_SUCCESS;
1758 break;
1760 case FSCTL_GET_RETRIEVAL_POINTERS:
1762 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1764 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1766 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1768 buffer->ExtentCount = 1;
1769 buffer->StartingVcn.QuadPart = 1;
1770 buffer->Extents[0].NextVcn.QuadPart = 0;
1771 buffer->Extents[0].Lcn.QuadPart = 0;
1772 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1773 status = STATUS_SUCCESS;
1775 else
1777 io->Information = 0;
1778 status = STATUS_BUFFER_TOO_SMALL;
1780 break;
1782 case FSCTL_PIPE_LISTEN:
1783 case FSCTL_PIPE_WAIT:
1784 default:
1785 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1786 in_buffer, in_size, out_buffer, out_size );
1787 break;
1790 if (status != STATUS_PENDING) io->u.Status = status;
1791 return status;
1795 struct read_changes_fileio
1797 struct async_fileio io;
1798 void *buffer;
1799 ULONG buffer_size;
1800 ULONG data_size;
1801 char data[1];
1804 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb,
1805 NTSTATUS status, void **apc, void **arg )
1807 struct read_changes_fileio *fileio = user;
1808 int size = 0;
1810 if (status == STATUS_ALERTED)
1812 SERVER_START_REQ( read_change )
1814 req->handle = wine_server_obj_handle( fileio->io.handle );
1815 wine_server_set_reply( req, fileio->data, fileio->data_size );
1816 status = wine_server_call( req );
1817 size = wine_server_reply_size( reply );
1819 SERVER_END_REQ;
1821 if (status == STATUS_SUCCESS && fileio->buffer)
1823 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1824 int i, left = fileio->buffer_size;
1825 DWORD *last_entry_offset = NULL;
1826 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1828 while (size && left >= sizeof(*pfni))
1830 /* convert to an NT style path */
1831 for (i = 0; i < event->len; i++)
1832 if (event->name[i] == '/') event->name[i] = '\\';
1834 pfni->Action = event->action;
1835 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1836 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1837 last_entry_offset = &pfni->NextEntryOffset;
1839 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1841 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1842 pfni->FileNameLength *= sizeof(WCHAR);
1843 pfni->NextEntryOffset = i;
1844 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1845 left -= i;
1847 i = (offsetof(struct filesystem_event, name[event->len])
1848 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1849 event = (struct filesystem_event*)((char*)event + i);
1850 size -= i;
1853 if (size)
1855 status = STATUS_NOTIFY_ENUM_DIR;
1856 size = 0;
1858 else
1860 if (last_entry_offset) *last_entry_offset = 0;
1861 size = fileio->buffer_size - left;
1864 else
1866 status = STATUS_NOTIFY_ENUM_DIR;
1867 size = 0;
1871 if (status != STATUS_PENDING)
1873 iosb->u.Status = status;
1874 iosb->Information = size;
1875 *apc = fileio->io.apc;
1876 *arg = fileio->io.apc_arg;
1877 release_fileio( &fileio->io );
1879 return status;
1882 #define FILE_NOTIFY_ALL ( \
1883 FILE_NOTIFY_CHANGE_FILE_NAME | \
1884 FILE_NOTIFY_CHANGE_DIR_NAME | \
1885 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1886 FILE_NOTIFY_CHANGE_SIZE | \
1887 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1888 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1889 FILE_NOTIFY_CHANGE_CREATION | \
1890 FILE_NOTIFY_CHANGE_SECURITY )
1892 /******************************************************************************
1893 * NtNotifyChangeDirectoryFile [NTDLL.@]
1895 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1896 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1897 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1899 struct read_changes_fileio *fileio;
1900 NTSTATUS status;
1901 ULONG size = max( 4096, buffer_size );
1902 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1904 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1905 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1907 if (!iosb) return STATUS_ACCESS_VIOLATION;
1908 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1910 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1911 handle, apc, apc_context );
1912 if (!fileio) return STATUS_NO_MEMORY;
1914 fileio->buffer = buffer;
1915 fileio->buffer_size = buffer_size;
1916 fileio->data_size = size;
1918 SERVER_START_REQ( read_directory_changes )
1920 req->filter = filter;
1921 req->want_data = (buffer != NULL);
1922 req->subtree = subtree;
1923 req->async.handle = wine_server_obj_handle( handle );
1924 req->async.callback = wine_server_client_ptr( read_changes_apc );
1925 req->async.iosb = wine_server_client_ptr( iosb );
1926 req->async.arg = wine_server_client_ptr( fileio );
1927 req->async.event = wine_server_obj_handle( event );
1928 req->async.cvalue = cvalue;
1929 status = wine_server_call( req );
1931 SERVER_END_REQ;
1933 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1934 return status;
1937 /******************************************************************************
1938 * NtSetVolumeInformationFile [NTDLL.@]
1939 * ZwSetVolumeInformationFile [NTDLL.@]
1941 * Set volume information for an open file handle.
1943 * PARAMS
1944 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1945 * IoStatusBlock [O] Receives information about the operation on return
1946 * FsInformation [I] Source for volume information
1947 * Length [I] Size of FsInformation
1948 * FsInformationClass [I] Type of volume information to set
1950 * RETURNS
1951 * Success: 0. IoStatusBlock is updated.
1952 * Failure: An NTSTATUS error code describing the error.
1954 NTSTATUS WINAPI NtSetVolumeInformationFile(
1955 IN HANDLE FileHandle,
1956 PIO_STATUS_BLOCK IoStatusBlock,
1957 PVOID FsInformation,
1958 ULONG Length,
1959 FS_INFORMATION_CLASS FsInformationClass)
1961 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1962 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1963 return 0;
1966 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1967 static int futimens( int fd, const struct timespec spec[2] )
1969 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1971 #define HAVE_FUTIMENS
1972 #endif /* __ANDROID__ */
1974 #ifndef UTIME_OMIT
1975 #define UTIME_OMIT ((1 << 30) - 2)
1976 #endif
1978 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1980 NTSTATUS status = STATUS_SUCCESS;
1982 #ifdef HAVE_FUTIMENS
1983 struct timespec tv[2];
1985 tv[0].tv_sec = tv[1].tv_sec = 0;
1986 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1987 if (atime->QuadPart)
1989 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1990 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1992 if (mtime->QuadPart)
1994 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1995 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1997 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1999 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
2000 struct timeval tv[2];
2001 struct stat st;
2003 if (!atime->QuadPart || !mtime->QuadPart)
2006 tv[0].tv_sec = tv[0].tv_usec = 0;
2007 tv[1].tv_sec = tv[1].tv_usec = 0;
2008 if (!fstat( fd, &st ))
2010 tv[0].tv_sec = st.st_atime;
2011 tv[1].tv_sec = st.st_mtime;
2012 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2013 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
2014 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2015 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
2016 #endif
2017 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2018 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
2019 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2020 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
2021 #endif
2024 if (atime->QuadPart)
2026 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
2027 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
2029 if (mtime->QuadPart)
2031 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
2032 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
2034 #ifdef HAVE_FUTIMES
2035 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
2036 #elif defined(HAVE_FUTIMESAT)
2037 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
2038 #endif
2040 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
2041 FIXME( "setting file times not supported\n" );
2042 status = STATUS_NOT_IMPLEMENTED;
2043 #endif
2044 return status;
2047 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
2048 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
2050 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
2051 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
2052 RtlSecondsSince1970ToTime( st->st_atime, atime );
2053 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2054 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
2055 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2056 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
2057 #endif
2058 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2059 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2060 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2061 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2062 #endif
2063 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2064 atime->QuadPart += st->st_atim.tv_nsec / 100;
2065 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2066 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2067 #endif
2068 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2069 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2070 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2071 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2072 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2073 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2074 #endif
2075 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2076 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2077 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2078 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2079 #endif
2080 #else
2081 *creation = *mtime;
2082 #endif
2085 /* fill in the file information that depends on the stat and attribute info */
2086 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2087 FILE_INFORMATION_CLASS class )
2089 switch (class)
2091 case FileBasicInformation:
2093 FILE_BASIC_INFORMATION *info = ptr;
2095 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2096 &info->LastAccessTime, &info->CreationTime );
2097 info->FileAttributes = attr;
2099 break;
2100 case FileStandardInformation:
2102 FILE_STANDARD_INFORMATION *info = ptr;
2104 if ((info->Directory = S_ISDIR(st->st_mode)))
2106 info->AllocationSize.QuadPart = 0;
2107 info->EndOfFile.QuadPart = 0;
2108 info->NumberOfLinks = 1;
2110 else
2112 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2113 info->EndOfFile.QuadPart = st->st_size;
2114 info->NumberOfLinks = st->st_nlink;
2117 break;
2118 case FileInternalInformation:
2120 FILE_INTERNAL_INFORMATION *info = ptr;
2121 info->IndexNumber.QuadPart = st->st_ino;
2123 break;
2124 case FileEndOfFileInformation:
2126 FILE_END_OF_FILE_INFORMATION *info = ptr;
2127 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2129 break;
2130 case FileAllInformation:
2132 FILE_ALL_INFORMATION *info = ptr;
2133 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2134 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2135 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2137 break;
2138 /* all directory structures start with the FileDirectoryInformation layout */
2139 case FileBothDirectoryInformation:
2140 case FileFullDirectoryInformation:
2141 case FileDirectoryInformation:
2143 FILE_DIRECTORY_INFORMATION *info = ptr;
2145 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2146 &info->LastAccessTime, &info->CreationTime );
2147 if (S_ISDIR(st->st_mode))
2149 info->AllocationSize.QuadPart = 0;
2150 info->EndOfFile.QuadPart = 0;
2152 else
2154 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2155 info->EndOfFile.QuadPart = st->st_size;
2157 info->FileAttributes = attr;
2159 break;
2160 case FileIdFullDirectoryInformation:
2162 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2163 info->FileId.QuadPart = st->st_ino;
2164 fill_file_info( st, attr, info, FileDirectoryInformation );
2166 break;
2167 case FileIdBothDirectoryInformation:
2169 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2170 info->FileId.QuadPart = st->st_ino;
2171 fill_file_info( st, attr, info, FileDirectoryInformation );
2173 break;
2175 default:
2176 return STATUS_INVALID_INFO_CLASS;
2178 return STATUS_SUCCESS;
2181 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2183 data_size_t size = 1024;
2184 NTSTATUS ret;
2185 char *name;
2187 for (;;)
2189 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2190 if (!name) return STATUS_NO_MEMORY;
2191 unix_name->MaximumLength = size + 1;
2193 SERVER_START_REQ( get_handle_unix_name )
2195 req->handle = wine_server_obj_handle( handle );
2196 wine_server_set_reply( req, name, size );
2197 ret = wine_server_call( req );
2198 size = reply->name_len;
2200 SERVER_END_REQ;
2202 if (!ret)
2204 name[size] = 0;
2205 unix_name->Buffer = name;
2206 unix_name->Length = size;
2207 break;
2209 RtlFreeHeap( GetProcessHeap(), 0, name );
2210 if (ret != STATUS_BUFFER_OVERFLOW) break;
2212 return ret;
2215 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2217 UNICODE_STRING nt_name;
2218 NTSTATUS status;
2220 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2222 const WCHAR *ptr = nt_name.Buffer;
2223 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2225 /* Skip the volume mount point. */
2226 while (ptr != end && *ptr == '\\') ++ptr;
2227 while (ptr != end && *ptr != '\\') ++ptr;
2228 while (ptr != end && *ptr == '\\') ++ptr;
2229 while (ptr != end && *ptr != '\\') ++ptr;
2231 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2232 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2233 else *name_len = info->FileNameLength;
2235 memcpy( info->FileName, ptr, *name_len );
2236 RtlFreeUnicodeString( &nt_name );
2239 return status;
2242 /******************************************************************************
2243 * NtQueryInformationFile [NTDLL.@]
2244 * ZwQueryInformationFile [NTDLL.@]
2246 * Get information about an open file handle.
2248 * PARAMS
2249 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2250 * io [O] Receives information about the operation on return
2251 * ptr [O] Destination for file information
2252 * len [I] Size of FileInformation
2253 * class [I] Type of file information to get
2255 * RETURNS
2256 * Success: 0. IoStatusBlock and FileInformation are updated.
2257 * Failure: An NTSTATUS error code describing the error.
2259 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2260 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2262 static const size_t info_sizes[] =
2265 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2266 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2267 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2268 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2269 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2270 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2271 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2272 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2273 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2274 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2275 0, /* FileLinkInformation */
2276 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2277 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2278 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2279 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2280 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2281 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2282 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2283 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2284 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2285 0, /* FileAlternateNameInformation */
2286 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2287 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2288 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2289 0, /* FilePipeRemoteInformation */
2290 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2291 0, /* FileMailslotSetInformation */
2292 0, /* FileCompressionInformation */
2293 0, /* FileObjectIdInformation */
2294 0, /* FileCompletionInformation */
2295 0, /* FileMoveClusterInformation */
2296 0, /* FileQuotaInformation */
2297 0, /* FileReparsePointInformation */
2298 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2299 0, /* FileAttributeTagInformation */
2300 0, /* FileTrackingInformation */
2301 0, /* FileIdBothDirectoryInformation */
2302 0, /* FileIdFullDirectoryInformation */
2303 0, /* FileValidDataLengthInformation */
2304 0, /* FileShortNameInformation */
2305 0, /* FileIoCompletionNotificationInformation, */
2306 0, /* FileIoStatusBlockRangeInformation */
2307 0, /* FileIoPriorityHintInformation */
2308 0, /* FileSfioReserveInformation */
2309 0, /* FileSfioVolumeInformation */
2310 0, /* FileHardLinkInformation */
2311 0, /* FileProcessIdsUsingFileInformation */
2312 0, /* FileNormalizedNameInformation */
2313 0, /* FileNetworkPhysicalNameInformation */
2314 0, /* FileIdGlobalTxDirectoryInformation */
2315 0, /* FileIsRemoteDeviceInformation */
2316 0, /* FileAttributeCacheInformation */
2317 0, /* FileNumaNodeInformation */
2318 0, /* FileStandardLinkInformation */
2319 0, /* FileRemoteProtocolInformation */
2320 0, /* FileReplaceCompletionInformation */
2323 struct stat st;
2324 int fd, needs_close = FALSE;
2325 ULONG attr;
2327 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2329 io->Information = 0;
2331 if (class <= 0 || class >= FileMaximumInformation)
2332 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2333 if (!info_sizes[class])
2335 FIXME("Unsupported class (%d)\n", class);
2336 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2338 if (len < info_sizes[class])
2339 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2341 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2343 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2344 return io->u.Status;
2347 switch (class)
2349 case FileBasicInformation:
2350 if (fd_get_file_info( fd, &st, &attr ) == -1)
2351 io->u.Status = FILE_GetNtStatus();
2352 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2353 io->u.Status = STATUS_INVALID_INFO_CLASS;
2354 else
2355 fill_file_info( &st, attr, ptr, class );
2356 break;
2357 case FileStandardInformation:
2359 FILE_STANDARD_INFORMATION *info = ptr;
2361 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2362 else
2364 fill_file_info( &st, attr, info, class );
2365 info->DeletePending = FALSE; /* FIXME */
2368 break;
2369 case FilePositionInformation:
2371 FILE_POSITION_INFORMATION *info = ptr;
2372 off_t res = lseek( fd, 0, SEEK_CUR );
2373 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2374 else info->CurrentByteOffset.QuadPart = res;
2376 break;
2377 case FileInternalInformation:
2378 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2379 else fill_file_info( &st, attr, ptr, class );
2380 break;
2381 case FileEaInformation:
2383 FILE_EA_INFORMATION *info = ptr;
2384 info->EaSize = 0;
2386 break;
2387 case FileEndOfFileInformation:
2388 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2389 else fill_file_info( &st, attr, ptr, class );
2390 break;
2391 case FileAllInformation:
2393 FILE_ALL_INFORMATION *info = ptr;
2394 ANSI_STRING unix_name;
2396 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2397 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2398 io->u.Status = STATUS_INVALID_INFO_CLASS;
2399 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2401 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2403 fill_file_info( &st, attr, info, FileAllInformation );
2404 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2405 info->EaInformation.EaSize = 0;
2406 info->AccessInformation.AccessFlags = 0; /* FIXME */
2407 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2408 info->ModeInformation.Mode = 0; /* FIXME */
2409 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2411 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2412 RtlFreeAnsiString( &unix_name );
2413 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2416 break;
2417 case FileMailslotQueryInformation:
2419 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2421 SERVER_START_REQ( set_mailslot_info )
2423 req->handle = wine_server_obj_handle( hFile );
2424 req->flags = 0;
2425 io->u.Status = wine_server_call( req );
2426 if( io->u.Status == STATUS_SUCCESS )
2428 info->MaximumMessageSize = reply->max_msgsize;
2429 info->MailslotQuota = 0;
2430 info->NextMessageSize = 0;
2431 info->MessagesAvailable = 0;
2432 info->ReadTimeout.QuadPart = reply->read_timeout;
2435 SERVER_END_REQ;
2436 if (!io->u.Status)
2438 char *tmpbuf;
2439 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2440 if (size > 0x10000) size = 0x10000;
2441 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2443 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2445 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2446 info->MessagesAvailable = (res > 0);
2447 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2448 if (needs_close) close( fd );
2450 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2454 break;
2455 case FilePipeInformation:
2457 FILE_PIPE_INFORMATION* pi = ptr;
2459 SERVER_START_REQ( get_named_pipe_info )
2461 req->handle = wine_server_obj_handle( hFile );
2462 if (!(io->u.Status = wine_server_call( req )))
2464 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2465 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2466 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2467 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2470 SERVER_END_REQ;
2472 break;
2473 case FilePipeLocalInformation:
2475 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2477 SERVER_START_REQ( get_named_pipe_info )
2479 req->handle = wine_server_obj_handle( hFile );
2480 if (!(io->u.Status = wine_server_call( req )))
2482 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2483 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2484 switch (reply->sharing)
2486 case FILE_SHARE_READ:
2487 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2488 break;
2489 case FILE_SHARE_WRITE:
2490 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2491 break;
2492 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2493 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2494 break;
2496 pli->MaximumInstances = reply->maxinstances;
2497 pli->CurrentInstances = reply->instances;
2498 pli->InboundQuota = reply->insize;
2499 pli->ReadDataAvailable = 0; /* FIXME */
2500 pli->OutboundQuota = reply->outsize;
2501 pli->WriteQuotaAvailable = 0; /* FIXME */
2502 pli->NamedPipeState = 0; /* FIXME */
2503 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2504 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2507 SERVER_END_REQ;
2509 break;
2510 case FileNameInformation:
2512 FILE_NAME_INFORMATION *info = ptr;
2513 ANSI_STRING unix_name;
2515 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2517 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2518 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2519 RtlFreeAnsiString( &unix_name );
2520 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2523 break;
2524 case FileNetworkOpenInformation:
2526 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2527 ANSI_STRING unix_name;
2529 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2531 ULONG attributes;
2532 struct stat st;
2534 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2535 io->u.Status = FILE_GetNtStatus();
2536 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2537 io->u.Status = STATUS_INVALID_INFO_CLASS;
2538 else
2540 FILE_BASIC_INFORMATION basic;
2541 FILE_STANDARD_INFORMATION std;
2543 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2544 fill_file_info( &st, attributes, &std, FileStandardInformation );
2546 info->CreationTime = basic.CreationTime;
2547 info->LastAccessTime = basic.LastAccessTime;
2548 info->LastWriteTime = basic.LastWriteTime;
2549 info->ChangeTime = basic.ChangeTime;
2550 info->AllocationSize = std.AllocationSize;
2551 info->EndOfFile = std.EndOfFile;
2552 info->FileAttributes = basic.FileAttributes;
2554 RtlFreeAnsiString( &unix_name );
2557 break;
2558 default:
2559 FIXME("Unsupported class (%d)\n", class);
2560 io->u.Status = STATUS_NOT_IMPLEMENTED;
2561 break;
2563 if (needs_close) close( fd );
2564 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2565 return io->u.Status;
2568 /******************************************************************************
2569 * NtSetInformationFile [NTDLL.@]
2570 * ZwSetInformationFile [NTDLL.@]
2572 * Set information about an open file handle.
2574 * PARAMS
2575 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2576 * io [O] Receives information about the operation on return
2577 * ptr [I] Source for file information
2578 * len [I] Size of FileInformation
2579 * class [I] Type of file information to set
2581 * RETURNS
2582 * Success: 0. io is updated.
2583 * Failure: An NTSTATUS error code describing the error.
2585 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2586 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2588 int fd, needs_close;
2590 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2592 io->u.Status = STATUS_SUCCESS;
2593 switch (class)
2595 case FileBasicInformation:
2596 if (len >= sizeof(FILE_BASIC_INFORMATION))
2598 struct stat st;
2599 const FILE_BASIC_INFORMATION *info = ptr;
2601 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2602 return io->u.Status;
2604 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2605 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2607 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2609 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2610 else
2612 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2614 if (S_ISDIR( st.st_mode))
2615 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2616 else
2617 st.st_mode &= ~0222; /* clear write permission bits */
2619 else
2621 /* add write permission only where we already have read permission */
2622 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2624 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2628 if (needs_close) close( fd );
2630 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2631 break;
2633 case FilePositionInformation:
2634 if (len >= sizeof(FILE_POSITION_INFORMATION))
2636 const FILE_POSITION_INFORMATION *info = ptr;
2638 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2639 return io->u.Status;
2641 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2642 io->u.Status = FILE_GetNtStatus();
2644 if (needs_close) close( fd );
2646 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2647 break;
2649 case FileEndOfFileInformation:
2650 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2652 struct stat st;
2653 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2655 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2656 return io->u.Status;
2658 /* first try normal truncate */
2659 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2661 /* now check for the need to extend the file */
2662 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2664 static const char zero;
2666 /* extend the file one byte beyond the requested size and then truncate it */
2667 /* this should work around ftruncate implementations that can't extend files */
2668 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2669 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2671 io->u.Status = FILE_GetNtStatus();
2673 if (needs_close) close( fd );
2675 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2676 break;
2678 case FilePipeInformation:
2679 if (len >= sizeof(FILE_PIPE_INFORMATION))
2681 FILE_PIPE_INFORMATION *info = ptr;
2683 if ((info->CompletionMode | info->ReadMode) & ~1)
2685 io->u.Status = STATUS_INVALID_PARAMETER;
2686 break;
2689 SERVER_START_REQ( set_named_pipe_info )
2691 req->handle = wine_server_obj_handle( handle );
2692 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2693 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2694 io->u.Status = wine_server_call( req );
2696 SERVER_END_REQ;
2698 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2699 break;
2701 case FileMailslotSetInformation:
2703 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2705 SERVER_START_REQ( set_mailslot_info )
2707 req->handle = wine_server_obj_handle( handle );
2708 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2709 req->read_timeout = info->ReadTimeout.QuadPart;
2710 io->u.Status = wine_server_call( req );
2712 SERVER_END_REQ;
2714 break;
2716 case FileCompletionInformation:
2717 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2719 FILE_COMPLETION_INFORMATION *info = ptr;
2721 SERVER_START_REQ( set_completion_info )
2723 req->handle = wine_server_obj_handle( handle );
2724 req->chandle = wine_server_obj_handle( info->CompletionPort );
2725 req->ckey = info->CompletionKey;
2726 io->u.Status = wine_server_call( req );
2728 SERVER_END_REQ;
2729 } else
2730 io->u.Status = STATUS_INVALID_PARAMETER_3;
2731 break;
2733 case FileAllInformation:
2734 io->u.Status = STATUS_INVALID_INFO_CLASS;
2735 break;
2737 case FileValidDataLengthInformation:
2738 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2740 struct stat st;
2741 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2743 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2744 return io->u.Status;
2746 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2747 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2748 io->u.Status = STATUS_INVALID_PARAMETER;
2749 else
2751 #ifdef HAVE_FALLOCATE
2752 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2754 NTSTATUS status = FILE_GetNtStatus();
2755 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2756 else io->u.Status = status;
2758 #else
2759 FIXME( "setting valid data length not supported\n" );
2760 #endif
2762 if (needs_close) close( fd );
2764 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2765 break;
2767 default:
2768 FIXME("Unsupported class (%d)\n", class);
2769 io->u.Status = STATUS_NOT_IMPLEMENTED;
2770 break;
2772 io->Information = 0;
2773 return io->u.Status;
2777 /******************************************************************************
2778 * NtQueryFullAttributesFile (NTDLL.@)
2780 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2781 FILE_NETWORK_OPEN_INFORMATION *info )
2783 ANSI_STRING unix_name;
2784 NTSTATUS status;
2786 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2788 ULONG attributes;
2789 struct stat st;
2791 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2792 status = FILE_GetNtStatus();
2793 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2794 status = STATUS_INVALID_INFO_CLASS;
2795 else
2797 FILE_BASIC_INFORMATION basic;
2798 FILE_STANDARD_INFORMATION std;
2800 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2801 fill_file_info( &st, attributes, &std, FileStandardInformation );
2803 info->CreationTime = basic.CreationTime;
2804 info->LastAccessTime = basic.LastAccessTime;
2805 info->LastWriteTime = basic.LastWriteTime;
2806 info->ChangeTime = basic.ChangeTime;
2807 info->AllocationSize = std.AllocationSize;
2808 info->EndOfFile = std.EndOfFile;
2809 info->FileAttributes = basic.FileAttributes;
2810 if (DIR_is_hidden_file( attr->ObjectName ))
2811 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2813 RtlFreeAnsiString( &unix_name );
2815 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2816 return status;
2820 /******************************************************************************
2821 * NtQueryAttributesFile (NTDLL.@)
2822 * ZwQueryAttributesFile (NTDLL.@)
2824 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2826 ANSI_STRING unix_name;
2827 NTSTATUS status;
2829 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2831 ULONG attributes;
2832 struct stat st;
2834 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2835 status = FILE_GetNtStatus();
2836 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2837 status = STATUS_INVALID_INFO_CLASS;
2838 else
2840 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2841 if (DIR_is_hidden_file( attr->ObjectName ))
2842 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2844 RtlFreeAnsiString( &unix_name );
2846 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2847 return status;
2851 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2852 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2853 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2854 unsigned int flags )
2856 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2858 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2859 /* Don't assume read-only, let the mount options set it below */
2860 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2862 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2863 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2865 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2866 info->Characteristics |= FILE_REMOTE_DEVICE;
2868 else if (!strcmp("procfs", fstypename))
2869 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2870 else
2871 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2873 if (flags & MNT_RDONLY)
2874 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2876 if (!(flags & MNT_LOCAL))
2878 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2879 info->Characteristics |= FILE_REMOTE_DEVICE;
2882 #endif
2884 static inline BOOL is_device_placeholder( int fd )
2886 static const char wine_placeholder[] = "Wine device placeholder";
2887 char buffer[sizeof(wine_placeholder)-1];
2889 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2890 return FALSE;
2891 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2894 /******************************************************************************
2895 * get_device_info
2897 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2899 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2901 struct stat st;
2903 info->Characteristics = 0;
2904 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2905 if (S_ISCHR( st.st_mode ))
2907 info->DeviceType = FILE_DEVICE_UNKNOWN;
2908 #ifdef linux
2909 switch(major(st.st_rdev))
2911 case MEM_MAJOR:
2912 info->DeviceType = FILE_DEVICE_NULL;
2913 break;
2914 case TTY_MAJOR:
2915 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2916 break;
2917 case LP_MAJOR:
2918 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2919 break;
2920 case SCSI_TAPE_MAJOR:
2921 info->DeviceType = FILE_DEVICE_TAPE;
2922 break;
2924 #endif
2926 else if (S_ISBLK( st.st_mode ))
2928 info->DeviceType = FILE_DEVICE_DISK;
2930 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2932 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2934 else if (is_device_placeholder( fd ))
2936 info->DeviceType = FILE_DEVICE_DISK;
2938 else /* regular file or directory */
2940 #if defined(linux) && defined(HAVE_FSTATFS)
2941 struct statfs stfs;
2943 /* check for floppy disk */
2944 if (major(st.st_dev) == FLOPPY_MAJOR)
2945 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2947 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2948 switch (stfs.f_type)
2950 case 0x9660: /* iso9660 */
2951 case 0x9fa1: /* supermount */
2952 case 0x15013346: /* udf */
2953 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2954 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2955 break;
2956 case 0x6969: /* nfs */
2957 case 0x517B: /* smbfs */
2958 case 0x564c: /* ncpfs */
2959 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2960 info->Characteristics |= FILE_REMOTE_DEVICE;
2961 break;
2962 case 0x01021994: /* tmpfs */
2963 case 0x28cd3d45: /* cramfs */
2964 case 0x1373: /* devfs */
2965 case 0x9fa0: /* procfs */
2966 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2967 break;
2968 default:
2969 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2970 break;
2972 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2973 struct statfs stfs;
2975 if (fstatfs( fd, &stfs ) < 0)
2976 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2977 else
2978 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2979 #elif defined(__NetBSD__)
2980 struct statvfs stfs;
2982 if (fstatvfs( fd, &stfs) < 0)
2983 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2984 else
2985 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2986 #elif defined(sun)
2987 /* Use dkio to work out device types */
2989 # include <sys/dkio.h>
2990 # include <sys/vtoc.h>
2991 struct dk_cinfo dkinf;
2992 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2993 if(retval==-1){
2994 WARN("Unable to get disk device type information - assuming a disk like device\n");
2995 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2997 switch (dkinf.dki_ctype)
2999 case DKC_CDROM:
3000 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3001 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3002 break;
3003 case DKC_NCRFLOPPY:
3004 case DKC_SMSFLOPPY:
3005 case DKC_INTEL82072:
3006 case DKC_INTEL82077:
3007 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3008 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3009 break;
3010 case DKC_MD:
3011 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3012 break;
3013 default:
3014 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3017 #else
3018 static int warned;
3019 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3020 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3021 #endif
3022 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3024 return STATUS_SUCCESS;
3028 /******************************************************************************
3029 * NtQueryVolumeInformationFile [NTDLL.@]
3030 * ZwQueryVolumeInformationFile [NTDLL.@]
3032 * Get volume information for an open file handle.
3034 * PARAMS
3035 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3036 * io [O] Receives information about the operation on return
3037 * buffer [O] Destination for volume information
3038 * length [I] Size of FsInformation
3039 * info_class [I] Type of volume information to set
3041 * RETURNS
3042 * Success: 0. io and buffer are updated.
3043 * Failure: An NTSTATUS error code describing the error.
3045 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3046 PVOID buffer, ULONG length,
3047 FS_INFORMATION_CLASS info_class )
3049 int fd, needs_close;
3050 struct stat st;
3051 static int once;
3053 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
3054 return io->u.Status;
3056 io->u.Status = STATUS_NOT_IMPLEMENTED;
3057 io->Information = 0;
3059 switch( info_class )
3061 case FileFsVolumeInformation:
3062 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3063 break;
3064 case FileFsLabelInformation:
3065 FIXME( "%p: label info not supported\n", handle );
3066 break;
3067 case FileFsSizeInformation:
3068 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3069 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3070 else
3072 FILE_FS_SIZE_INFORMATION *info = buffer;
3074 if (fstat( fd, &st ) < 0)
3076 io->u.Status = FILE_GetNtStatus();
3077 break;
3079 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3081 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3083 else
3085 ULONGLONG bsize;
3086 /* Linux's fstatvfs is buggy */
3087 #if !defined(linux) || !defined(HAVE_FSTATFS)
3088 struct statvfs stfs;
3090 if (fstatvfs( fd, &stfs ) < 0)
3092 io->u.Status = FILE_GetNtStatus();
3093 break;
3095 bsize = stfs.f_frsize;
3096 #else
3097 struct statfs stfs;
3098 if (fstatfs( fd, &stfs ) < 0)
3100 io->u.Status = FILE_GetNtStatus();
3101 break;
3103 bsize = stfs.f_bsize;
3104 #endif
3105 if (bsize == 2048) /* assume CD-ROM */
3107 info->BytesPerSector = 2048;
3108 info->SectorsPerAllocationUnit = 1;
3110 else
3112 info->BytesPerSector = 512;
3113 info->SectorsPerAllocationUnit = 8;
3115 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3116 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3117 io->Information = sizeof(*info);
3118 io->u.Status = STATUS_SUCCESS;
3121 break;
3122 case FileFsDeviceInformation:
3123 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3124 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3125 else
3127 FILE_FS_DEVICE_INFORMATION *info = buffer;
3129 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3130 io->Information = sizeof(*info);
3132 break;
3133 case FileFsAttributeInformation:
3134 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3135 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3136 else
3138 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3140 FIXME( "%p: faking attribute info\n", handle );
3141 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3142 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3143 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3144 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3145 info->FileSystemNameLength = sizeof(ntfsW);
3146 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3148 io->Information = sizeof(*info);
3149 io->u.Status = STATUS_SUCCESS;
3151 break;
3152 case FileFsControlInformation:
3153 FIXME( "%p: control info not supported\n", handle );
3154 break;
3155 case FileFsFullSizeInformation:
3156 FIXME( "%p: full size info not supported\n", handle );
3157 break;
3158 case FileFsObjectIdInformation:
3159 FIXME( "%p: object id info not supported\n", handle );
3160 break;
3161 case FileFsMaximumInformation:
3162 FIXME( "%p: maximum info not supported\n", handle );
3163 break;
3164 default:
3165 io->u.Status = STATUS_INVALID_PARAMETER;
3166 break;
3168 if (needs_close) close( fd );
3169 return io->u.Status;
3173 /******************************************************************
3174 * NtQueryEaFile (NTDLL.@)
3176 * Read extended attributes from NTFS files.
3178 * PARAMS
3179 * hFile [I] File handle, must be opened with FILE_READ_EA access
3180 * iosb [O] Receives information about the operation on return
3181 * buffer [O] Output buffer
3182 * length [I] Length of output buffer
3183 * single_entry [I] Only read and return one entry
3184 * ea_list [I] Optional list with names of EAs to return
3185 * ea_list_len [I] Length of ea_list in bytes
3186 * ea_index [I] Optional pointer to 1-based index of attribute to return
3187 * restart [I] restart EA scan
3189 * RETURNS
3190 * Success: 0. Atrributes read into buffer
3191 * Failure: An NTSTATUS error code describing the error.
3193 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3194 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3195 PULONG ea_index, BOOLEAN restart )
3197 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3198 hFile, iosb, buffer, length, single_entry, ea_list,
3199 ea_list_len, ea_index, restart);
3200 return STATUS_ACCESS_DENIED;
3204 /******************************************************************
3205 * NtSetEaFile (NTDLL.@)
3207 * Update extended attributes for NTFS files.
3209 * PARAMS
3210 * hFile [I] File handle, must be opened with FILE_READ_EA access
3211 * iosb [O] Receives information about the operation on return
3212 * buffer [I] Buffer with EA information
3213 * length [I] Length of buffer
3215 * RETURNS
3216 * Success: 0. Attributes are updated
3217 * Failure: An NTSTATUS error code describing the error.
3219 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3221 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3222 return STATUS_ACCESS_DENIED;
3226 /******************************************************************
3227 * NtFlushBuffersFile (NTDLL.@)
3229 * Flush any buffered data on an open file handle.
3231 * PARAMS
3232 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3233 * IoStatusBlock [O] Receives information about the operation on return
3235 * RETURNS
3236 * Success: 0. IoStatusBlock is updated.
3237 * Failure: An NTSTATUS error code describing the error.
3239 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3241 NTSTATUS ret;
3242 HANDLE hEvent = NULL;
3243 enum server_fd_type type;
3244 int fd, needs_close;
3246 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3248 if (!ret && type == FD_TYPE_SERIAL)
3250 ret = COMM_FlushBuffersFile( fd );
3252 else
3254 SERVER_START_REQ( flush )
3256 req->blocking = 1; /* always blocking */
3257 req->async.handle = wine_server_obj_handle( hFile );
3258 req->async.iosb = wine_server_client_ptr( IoStatusBlock );
3259 ret = wine_server_call( req );
3260 hEvent = wine_server_ptr_handle( reply->event );
3262 SERVER_END_REQ;
3264 if (hEvent)
3266 NtWaitForSingleObject( hEvent, FALSE, NULL );
3267 NtClose( hEvent );
3268 ret = STATUS_SUCCESS;
3272 if (needs_close) close( fd );
3273 return ret;
3276 /******************************************************************
3277 * NtLockFile (NTDLL.@)
3281 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3282 PIO_APC_ROUTINE apc, void* apc_user,
3283 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3284 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3285 BOOLEAN exclusive )
3287 NTSTATUS ret;
3288 HANDLE handle;
3289 BOOLEAN async;
3290 static BOOLEAN warn = TRUE;
3292 if (apc || io_status || key)
3294 FIXME("Unimplemented yet parameter\n");
3295 return STATUS_NOT_IMPLEMENTED;
3298 if (apc_user && warn)
3300 FIXME("I/O completion on lock not implemented yet\n");
3301 warn = FALSE;
3304 for (;;)
3306 SERVER_START_REQ( lock_file )
3308 req->handle = wine_server_obj_handle( hFile );
3309 req->offset = offset->QuadPart;
3310 req->count = count->QuadPart;
3311 req->shared = !exclusive;
3312 req->wait = !dont_wait;
3313 ret = wine_server_call( req );
3314 handle = wine_server_ptr_handle( reply->handle );
3315 async = reply->overlapped;
3317 SERVER_END_REQ;
3318 if (ret != STATUS_PENDING)
3320 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3321 return ret;
3324 if (async)
3326 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3327 if (handle) NtClose( handle );
3328 return STATUS_PENDING;
3330 if (handle)
3332 NtWaitForSingleObject( handle, FALSE, NULL );
3333 NtClose( handle );
3335 else
3337 LARGE_INTEGER time;
3339 /* Unix lock conflict, sleep a bit and retry */
3340 time.QuadPart = 100 * (ULONGLONG)10000;
3341 time.QuadPart = -time.QuadPart;
3342 NtDelayExecution( FALSE, &time );
3348 /******************************************************************
3349 * NtUnlockFile (NTDLL.@)
3353 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3354 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3355 PULONG key )
3357 NTSTATUS status;
3359 TRACE( "%p %x%08x %x%08x\n",
3360 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3362 if (io_status || key)
3364 FIXME("Unimplemented yet parameter\n");
3365 return STATUS_NOT_IMPLEMENTED;
3368 SERVER_START_REQ( unlock_file )
3370 req->handle = wine_server_obj_handle( hFile );
3371 req->offset = offset->QuadPart;
3372 req->count = count->QuadPart;
3373 status = wine_server_call( req );
3375 SERVER_END_REQ;
3376 return status;
3379 /******************************************************************
3380 * NtCreateNamedPipeFile (NTDLL.@)
3384 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3385 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3386 ULONG sharing, ULONG dispo, ULONG options,
3387 ULONG pipe_type, ULONG read_mode,
3388 ULONG completion_mode, ULONG max_inst,
3389 ULONG inbound_quota, ULONG outbound_quota,
3390 PLARGE_INTEGER timeout)
3392 struct security_descriptor *sd = NULL;
3393 struct object_attributes objattr;
3394 NTSTATUS status;
3396 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3397 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3398 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3399 outbound_quota, timeout);
3401 /* assume we only get relative timeout */
3402 if (timeout->QuadPart > 0)
3403 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3405 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
3406 objattr.sd_len = 0;
3407 objattr.name_len = attr->ObjectName->Length;
3409 status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
3410 if (status != STATUS_SUCCESS) return status;
3412 SERVER_START_REQ( create_named_pipe )
3414 req->access = access;
3415 req->attributes = attr->Attributes;
3416 req->options = options;
3417 req->sharing = sharing;
3418 req->flags =
3419 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3420 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3421 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3422 req->maxinstances = max_inst;
3423 req->outsize = outbound_quota;
3424 req->insize = inbound_quota;
3425 req->timeout = timeout->QuadPart;
3426 wine_server_add_data( req, &objattr, sizeof(objattr) );
3427 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
3428 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
3429 status = wine_server_call( req );
3430 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3432 SERVER_END_REQ;
3434 NTDLL_free_struct_sd( sd );
3435 return status;
3438 /******************************************************************
3439 * NtDeleteFile (NTDLL.@)
3443 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3445 NTSTATUS status;
3446 HANDLE hFile;
3447 IO_STATUS_BLOCK io;
3449 TRACE("%p\n", ObjectAttributes);
3450 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3451 ObjectAttributes, &io, NULL, 0,
3452 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3453 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3454 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3455 return status;
3458 /******************************************************************
3459 * NtCancelIoFileEx (NTDLL.@)
3463 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3465 TRACE("%p %p %p\n", hFile, iosb, io_status );
3467 SERVER_START_REQ( cancel_async )
3469 req->handle = wine_server_obj_handle( hFile );
3470 req->iosb = wine_server_client_ptr( iosb );
3471 req->only_thread = FALSE;
3472 io_status->u.Status = wine_server_call( req );
3474 SERVER_END_REQ;
3476 return io_status->u.Status;
3479 /******************************************************************
3480 * NtCancelIoFile (NTDLL.@)
3484 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3486 TRACE("%p %p\n", hFile, io_status );
3488 SERVER_START_REQ( cancel_async )
3490 req->handle = wine_server_obj_handle( hFile );
3491 req->iosb = 0;
3492 req->only_thread = TRUE;
3493 io_status->u.Status = wine_server_call( req );
3495 SERVER_END_REQ;
3497 return io_status->u.Status;
3500 /******************************************************************************
3501 * NtCreateMailslotFile [NTDLL.@]
3502 * ZwCreateMailslotFile [NTDLL.@]
3504 * PARAMS
3505 * pHandle [O] pointer to receive the handle created
3506 * DesiredAccess [I] access mode (read, write, etc)
3507 * ObjectAttributes [I] fully qualified NT path of the mailslot
3508 * IoStatusBlock [O] receives completion status and other info
3509 * CreateOptions [I]
3510 * MailslotQuota [I]
3511 * MaxMessageSize [I]
3512 * TimeOut [I]
3514 * RETURNS
3515 * An NT status code
3517 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3518 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3519 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3520 PLARGE_INTEGER TimeOut)
3522 LARGE_INTEGER timeout;
3523 NTSTATUS ret;
3525 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3526 pHandle, DesiredAccess, attr, IoStatusBlock,
3527 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3529 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3530 if (!attr) return STATUS_INVALID_PARAMETER;
3531 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3534 * For a NULL TimeOut pointer set the default timeout value
3536 if (!TimeOut)
3537 timeout.QuadPart = -1;
3538 else
3539 timeout.QuadPart = TimeOut->QuadPart;
3541 SERVER_START_REQ( create_mailslot )
3543 req->access = DesiredAccess;
3544 req->attributes = attr->Attributes;
3545 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3546 req->max_msgsize = MaxMessageSize;
3547 req->read_timeout = timeout.QuadPart;
3548 wine_server_add_data( req, attr->ObjectName->Buffer,
3549 attr->ObjectName->Length );
3550 ret = wine_server_call( req );
3551 if( ret == STATUS_SUCCESS )
3552 *pHandle = wine_server_ptr_handle( reply->handle );
3554 SERVER_END_REQ;
3556 return ret;