shell32/tests: Do not leak shell folder references.
[wine.git] / dlls / ntdll / file.c
blobd081750bdc85c7902fa52b0e159d7daa2a57227a
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 || type == FD_TYPE_DEVICE)
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 case FD_TYPE_DEVICE:
876 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
877 goto done;
878 case FD_TYPE_SERIAL:
879 break;
880 default:
881 status = STATUS_PIPE_BROKEN;
882 goto done;
885 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
887 else if (errno != EAGAIN)
889 if (errno == EINTR) continue;
890 if (!total) status = FILE_GetNtStatus();
891 goto done;
894 if (async_read)
896 struct async_fileio_read *fileio;
897 BOOL avail_mode;
899 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
900 goto err;
901 if (total && avail_mode)
903 status = STATUS_SUCCESS;
904 goto done;
907 fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), hFile, apc, apc_user );
908 if (!fileio)
910 status = STATUS_NO_MEMORY;
911 goto err;
913 fileio->already = total;
914 fileio->count = length;
915 fileio->buffer = buffer;
916 fileio->avail_mode = avail_mode;
918 SERVER_START_REQ( register_async )
920 req->type = ASYNC_TYPE_READ;
921 req->count = length;
922 req->async.handle = wine_server_obj_handle( hFile );
923 req->async.event = wine_server_obj_handle( hEvent );
924 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
925 req->async.iosb = wine_server_client_ptr( io_status );
926 req->async.arg = wine_server_client_ptr( fileio );
927 req->async.cvalue = cvalue;
928 status = wine_server_call( req );
930 SERVER_END_REQ;
932 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
933 goto err;
935 else /* synchronous read, wait for the fd to become ready */
937 struct pollfd pfd;
938 int ret, timeout;
940 if (!timeout_init_done)
942 timeout_init_done = TRUE;
943 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
944 goto err;
945 if (hEvent) NtResetEvent( hEvent, NULL );
947 timeout = get_next_io_timeout( &timeouts, total );
949 pfd.fd = unix_handle;
950 pfd.events = POLLIN;
952 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
954 if (total) /* return with what we got so far */
955 status = STATUS_SUCCESS;
956 else
957 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
958 goto done;
960 if (ret == -1 && errno != EINTR)
962 status = FILE_GetNtStatus();
963 goto done;
965 /* will now restart the read */
969 done:
970 send_completion = cvalue != 0;
972 err:
973 if (needs_close) close( unix_handle );
974 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
976 io_status->u.Status = status;
977 io_status->Information = total;
978 TRACE("= SUCCESS (%u)\n", total);
979 if (hEvent) NtSetEvent( hEvent, NULL );
980 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
981 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
983 else
985 TRACE("= 0x%08x\n", status);
986 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
989 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
991 return status;
995 /******************************************************************************
996 * NtReadFileScatter [NTDLL.@]
997 * ZwReadFileScatter [NTDLL.@]
999 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1000 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1001 ULONG length, PLARGE_INTEGER offset, PULONG key )
1003 int result, unix_handle, needs_close;
1004 unsigned int options;
1005 NTSTATUS status;
1006 ULONG pos = 0, total = 0;
1007 enum server_fd_type type;
1008 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1009 BOOL send_completion = FALSE;
1011 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1012 file, event, apc, apc_user, io_status, segments, length, offset, key);
1014 if (length % page_size) return STATUS_INVALID_PARAMETER;
1015 if (!io_status) return STATUS_ACCESS_VIOLATION;
1017 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
1018 &needs_close, &type, &options );
1019 if (status) return status;
1021 if ((type != FD_TYPE_FILE) ||
1022 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1023 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1025 status = STATUS_INVALID_PARAMETER;
1026 goto error;
1029 while (length)
1031 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1032 result = pread( unix_handle, (char *)segments->Buffer + pos,
1033 page_size - pos, offset->QuadPart + total );
1034 else
1035 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1037 if (result == -1)
1039 if (errno == EINTR) continue;
1040 status = FILE_GetNtStatus();
1041 break;
1043 if (!result)
1045 status = STATUS_END_OF_FILE;
1046 break;
1048 total += result;
1049 length -= result;
1050 if ((pos += result) == page_size)
1052 pos = 0;
1053 segments++;
1057 send_completion = cvalue != 0;
1059 error:
1060 if (needs_close) close( unix_handle );
1061 if (status == STATUS_SUCCESS)
1063 io_status->u.Status = status;
1064 io_status->Information = total;
1065 TRACE("= SUCCESS (%u)\n", total);
1066 if (event) NtSetEvent( event, NULL );
1067 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1068 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1070 else
1072 TRACE("= 0x%08x\n", status);
1073 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1076 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1078 return status;
1082 /***********************************************************************
1083 * FILE_AsyncWriteService (INTERNAL)
1085 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb,
1086 NTSTATUS status, void **apc, void **arg )
1088 struct async_fileio_write *fileio = user;
1089 int result, fd, needs_close;
1090 enum server_fd_type type;
1092 switch (status)
1094 case STATUS_ALERTED:
1095 /* write some data (non-blocking) */
1096 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
1097 &needs_close, &type, NULL )))
1098 break;
1100 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1101 result = send( fd, fileio->buffer, 0, 0 );
1102 else
1103 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
1105 if (needs_close) close( fd );
1107 if (result < 0)
1109 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
1110 else status = FILE_GetNtStatus();
1112 else
1114 fileio->already += result;
1115 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
1117 break;
1119 case STATUS_TIMEOUT:
1120 case STATUS_IO_TIMEOUT:
1121 if (fileio->already) status = STATUS_SUCCESS;
1122 break;
1124 if (status != STATUS_PENDING)
1126 iosb->u.Status = status;
1127 iosb->Information = fileio->already;
1128 *apc = fileio->io.apc;
1129 *arg = fileio->io.apc_arg;
1130 release_fileio( &fileio->io );
1132 return status;
1135 static NTSTATUS set_pending_write( HANDLE device )
1137 NTSTATUS status;
1139 SERVER_START_REQ( set_serial_info )
1141 req->handle = wine_server_obj_handle( device );
1142 req->flags = SERIALINFO_PENDING_WRITE;
1143 status = wine_server_call( req );
1145 SERVER_END_REQ;
1146 return status;
1149 /******************************************************************************
1150 * NtWriteFile [NTDLL.@]
1151 * ZwWriteFile [NTDLL.@]
1153 * Write to an open file handle.
1155 * PARAMS
1156 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1157 * Event [I] Event to signal upon completion (or NULL)
1158 * ApcRoutine [I] Callback to call upon completion (or NULL)
1159 * ApcContext [I] Context for ApcRoutine (or NULL)
1160 * IoStatusBlock [O] Receives information about the operation on return
1161 * Buffer [I] Source for the data to write
1162 * Length [I] Size of Buffer
1163 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1164 * Key [O] Function unknown (may be NULL)
1166 * RETURNS
1167 * Success: 0. IoStatusBlock is updated, and the Information member contains
1168 * The number of bytes written.
1169 * Failure: An NTSTATUS error code describing the error.
1171 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1172 PIO_APC_ROUTINE apc, void* apc_user,
1173 PIO_STATUS_BLOCK io_status,
1174 const void* buffer, ULONG length,
1175 PLARGE_INTEGER offset, PULONG key)
1177 int result, unix_handle, needs_close;
1178 unsigned int options;
1179 struct io_timeouts timeouts;
1180 NTSTATUS status;
1181 ULONG total = 0;
1182 enum server_fd_type type;
1183 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1184 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1185 LARGE_INTEGER offset_eof;
1187 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1188 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1190 if (!io_status) return STATUS_ACCESS_VIOLATION;
1192 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1193 &needs_close, &type, &options );
1194 if (status == STATUS_BAD_DEVICE_TYPE)
1195 return server_write_file( hFile, hEvent, apc, apc_user, io_status, buffer, length, offset, key );
1197 if (status == STATUS_ACCESS_DENIED)
1199 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1200 &needs_close, &type, &options );
1201 append_write = TRUE;
1203 if (status) return status;
1205 if (!virtual_check_buffer_for_read( buffer, length ))
1207 status = STATUS_INVALID_USER_BUFFER;
1208 goto done;
1211 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1213 if (type == FD_TYPE_FILE)
1215 if (async_write &&
1216 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1218 status = STATUS_INVALID_PARAMETER;
1219 goto done;
1222 if (append_write)
1224 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1225 offset = &offset_eof;
1228 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1230 off_t off = offset->QuadPart;
1232 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1234 struct stat st;
1236 if (fstat( unix_handle, &st ) == -1)
1238 status = FILE_GetNtStatus();
1239 goto done;
1241 off = st.st_size;
1243 else if (offset->QuadPart < 0)
1245 status = STATUS_INVALID_PARAMETER;
1246 goto done;
1249 /* async I/O doesn't make sense on regular files */
1250 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1252 if (errno != EINTR)
1254 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1255 else status = FILE_GetNtStatus();
1256 goto done;
1260 if (!async_write)
1261 /* update file pointer position */
1262 lseek( unix_handle, off + result, SEEK_SET );
1264 total = result;
1265 status = STATUS_SUCCESS;
1266 goto done;
1269 else if (type == FD_TYPE_SERIAL || type == FD_TYPE_DEVICE)
1271 if (async_write &&
1272 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1274 status = STATUS_INVALID_PARAMETER;
1275 goto done;
1279 for (;;)
1281 /* zero-length writes on sockets may not work with plain write(2) */
1282 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1283 result = send( unix_handle, buffer, 0, 0 );
1284 else
1285 result = write( unix_handle, (const char *)buffer + total, length - total );
1287 if (result >= 0)
1289 total += result;
1290 if (total == length)
1292 status = STATUS_SUCCESS;
1293 goto done;
1295 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1297 else if (errno != EAGAIN)
1299 if (errno == EINTR) continue;
1300 if (!total)
1302 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1303 else status = FILE_GetNtStatus();
1305 goto done;
1308 if (async_write)
1310 struct async_fileio_write *fileio;
1312 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), hFile, apc, apc_user );
1313 if (!fileio)
1315 status = STATUS_NO_MEMORY;
1316 goto err;
1318 fileio->already = total;
1319 fileio->count = length;
1320 fileio->buffer = buffer;
1322 SERVER_START_REQ( register_async )
1324 req->type = ASYNC_TYPE_WRITE;
1325 req->count = length;
1326 req->async.handle = wine_server_obj_handle( hFile );
1327 req->async.event = wine_server_obj_handle( hEvent );
1328 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1329 req->async.iosb = wine_server_client_ptr( io_status );
1330 req->async.arg = wine_server_client_ptr( fileio );
1331 req->async.cvalue = cvalue;
1332 status = wine_server_call( req );
1334 SERVER_END_REQ;
1336 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1337 goto err;
1339 else /* synchronous write, wait for the fd to become ready */
1341 struct pollfd pfd;
1342 int ret, timeout;
1344 if (!timeout_init_done)
1346 timeout_init_done = TRUE;
1347 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1348 goto err;
1349 if (hEvent) NtResetEvent( hEvent, NULL );
1351 timeout = get_next_io_timeout( &timeouts, total );
1353 pfd.fd = unix_handle;
1354 pfd.events = POLLOUT;
1356 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1358 /* return with what we got so far */
1359 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1360 goto done;
1362 if (ret == -1 && errno != EINTR)
1364 status = FILE_GetNtStatus();
1365 goto done;
1367 /* will now restart the write */
1371 done:
1372 send_completion = cvalue != 0;
1374 err:
1375 if (needs_close) close( unix_handle );
1377 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1378 set_pending_write( hFile );
1380 if (status == STATUS_SUCCESS)
1382 io_status->u.Status = status;
1383 io_status->Information = total;
1384 TRACE("= SUCCESS (%u)\n", total);
1385 if (hEvent) NtSetEvent( hEvent, NULL );
1386 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1387 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1389 else
1391 TRACE("= 0x%08x\n", status);
1392 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1395 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1397 return status;
1401 /******************************************************************************
1402 * NtWriteFileGather [NTDLL.@]
1403 * ZwWriteFileGather [NTDLL.@]
1405 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1406 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1407 ULONG length, PLARGE_INTEGER offset, PULONG key )
1409 int result, unix_handle, needs_close;
1410 unsigned int options;
1411 NTSTATUS status;
1412 ULONG pos = 0, total = 0;
1413 enum server_fd_type type;
1414 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1415 BOOL send_completion = FALSE;
1417 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1418 file, event, apc, apc_user, io_status, segments, length, offset, key);
1420 if (length % page_size) return STATUS_INVALID_PARAMETER;
1421 if (!io_status) return STATUS_ACCESS_VIOLATION;
1423 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1424 &needs_close, &type, &options );
1425 if (status) return status;
1427 if ((type != FD_TYPE_FILE) ||
1428 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1429 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1431 status = STATUS_INVALID_PARAMETER;
1432 goto error;
1435 while (length)
1437 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1438 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1439 page_size - pos, offset->QuadPart + total );
1440 else
1441 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1443 if (result == -1)
1445 if (errno == EINTR) continue;
1446 if (errno == EFAULT)
1448 status = STATUS_INVALID_USER_BUFFER;
1449 goto error;
1451 status = FILE_GetNtStatus();
1452 break;
1454 if (!result)
1456 status = STATUS_DISK_FULL;
1457 break;
1459 total += result;
1460 length -= result;
1461 if ((pos += result) == page_size)
1463 pos = 0;
1464 segments++;
1468 send_completion = cvalue != 0;
1470 error:
1471 if (needs_close) close( unix_handle );
1472 if (status == STATUS_SUCCESS)
1474 io_status->u.Status = status;
1475 io_status->Information = total;
1476 TRACE("= SUCCESS (%u)\n", total);
1477 if (event) NtSetEvent( event, NULL );
1478 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1479 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1481 else
1483 TRACE("= 0x%08x\n", status);
1484 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1487 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1489 return status;
1493 /* do an ioctl call through the server */
1494 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1495 PIO_APC_ROUTINE apc, PVOID apc_context,
1496 IO_STATUS_BLOCK *io, ULONG code,
1497 const void *in_buffer, ULONG in_size,
1498 PVOID out_buffer, ULONG out_size )
1500 struct async_irp *async;
1501 NTSTATUS status;
1502 HANDLE wait_handle;
1503 ULONG options;
1504 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1506 if (!(async = (struct async_irp *)alloc_fileio( sizeof(*async), handle, apc, apc_context )))
1507 return STATUS_NO_MEMORY;
1508 async->event = event;
1509 async->buffer = out_buffer;
1510 async->size = out_size;
1512 SERVER_START_REQ( ioctl )
1514 req->code = code;
1515 req->blocking = !apc && !event && !cvalue;
1516 req->async.handle = wine_server_obj_handle( handle );
1517 req->async.callback = wine_server_client_ptr( irp_completion );
1518 req->async.iosb = wine_server_client_ptr( io );
1519 req->async.arg = wine_server_client_ptr( async );
1520 req->async.event = wine_server_obj_handle( event );
1521 req->async.cvalue = cvalue;
1522 wine_server_add_data( req, in_buffer, in_size );
1523 wine_server_set_reply( req, out_buffer, out_size );
1524 status = wine_server_call( req );
1525 wait_handle = wine_server_ptr_handle( reply->wait );
1526 options = reply->options;
1527 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1529 SERVER_END_REQ;
1531 if (status == STATUS_NOT_SUPPORTED)
1532 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1533 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1535 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1537 if (wait_handle)
1539 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1540 status = io->u.Status;
1541 NtClose( wait_handle );
1544 return status;
1547 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1548 * server */
1549 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1550 ULONG in_size)
1552 #ifdef VALGRIND_MAKE_MEM_DEFINED
1553 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1554 do { \
1555 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1556 if ((size) >= FIELD_OFFSET(t, f2)) \
1557 VALGRIND_MAKE_MEM_DEFINED( \
1558 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1559 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1560 } while (0)
1562 switch (code)
1564 case FSCTL_PIPE_WAIT:
1565 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1566 break;
1568 #endif
1572 /**************************************************************************
1573 * NtDeviceIoControlFile [NTDLL.@]
1574 * ZwDeviceIoControlFile [NTDLL.@]
1576 * Perform an I/O control operation on an open file handle.
1578 * PARAMS
1579 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1580 * event [I] Event to signal upon completion (or NULL)
1581 * apc [I] Callback to call upon completion (or NULL)
1582 * apc_context [I] Context for ApcRoutine (or NULL)
1583 * io [O] Receives information about the operation on return
1584 * code [I] Control code for the operation to perform
1585 * in_buffer [I] Source for any input data required (or NULL)
1586 * in_size [I] Size of InputBuffer
1587 * out_buffer [O] Source for any output data returned (or NULL)
1588 * out_size [I] Size of OutputBuffer
1590 * RETURNS
1591 * Success: 0. IoStatusBlock is updated.
1592 * Failure: An NTSTATUS error code describing the error.
1594 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1595 PIO_APC_ROUTINE apc, PVOID apc_context,
1596 PIO_STATUS_BLOCK io, ULONG code,
1597 PVOID in_buffer, ULONG in_size,
1598 PVOID out_buffer, ULONG out_size)
1600 ULONG device = (code >> 16);
1601 NTSTATUS status = STATUS_NOT_SUPPORTED;
1603 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1604 handle, event, apc, apc_context, io, code,
1605 in_buffer, in_size, out_buffer, out_size);
1607 switch(device)
1609 case FILE_DEVICE_DISK:
1610 case FILE_DEVICE_CD_ROM:
1611 case FILE_DEVICE_DVD:
1612 case FILE_DEVICE_CONTROLLER:
1613 case FILE_DEVICE_MASS_STORAGE:
1614 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1615 in_buffer, in_size, out_buffer, out_size);
1616 break;
1617 case FILE_DEVICE_SERIAL_PORT:
1618 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1619 in_buffer, in_size, out_buffer, out_size);
1620 break;
1621 case FILE_DEVICE_TAPE:
1622 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1623 in_buffer, in_size, out_buffer, out_size);
1624 break;
1627 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1628 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1629 in_buffer, in_size, out_buffer, out_size );
1631 if (status != STATUS_PENDING) io->u.Status = status;
1632 return status;
1636 /**************************************************************************
1637 * NtFsControlFile [NTDLL.@]
1638 * ZwFsControlFile [NTDLL.@]
1640 * Perform a file system control operation on an open file handle.
1642 * PARAMS
1643 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1644 * event [I] Event to signal upon completion (or NULL)
1645 * apc [I] Callback to call upon completion (or NULL)
1646 * apc_context [I] Context for ApcRoutine (or NULL)
1647 * io [O] Receives information about the operation on return
1648 * code [I] Control code for the operation to perform
1649 * in_buffer [I] Source for any input data required (or NULL)
1650 * in_size [I] Size of InputBuffer
1651 * out_buffer [O] Source for any output data returned (or NULL)
1652 * out_size [I] Size of OutputBuffer
1654 * RETURNS
1655 * Success: 0. IoStatusBlock is updated.
1656 * Failure: An NTSTATUS error code describing the error.
1658 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1659 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1660 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1662 NTSTATUS status;
1664 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1665 handle, event, apc, apc_context, io, code,
1666 in_buffer, in_size, out_buffer, out_size);
1668 if (!io) return STATUS_INVALID_PARAMETER;
1670 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1672 switch(code)
1674 case FSCTL_DISMOUNT_VOLUME:
1675 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1676 in_buffer, in_size, out_buffer, out_size );
1677 if (!status) status = DIR_unmount_device( handle );
1678 break;
1680 case FSCTL_PIPE_PEEK:
1682 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1683 int avail = 0, fd, needs_close;
1685 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1687 status = STATUS_INFO_LENGTH_MISMATCH;
1688 break;
1691 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1692 break;
1694 #ifdef FIONREAD
1695 if (ioctl( fd, FIONREAD, &avail ) != 0)
1697 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1698 if (needs_close) close( fd );
1699 status = FILE_GetNtStatus();
1700 break;
1702 #endif
1703 if (!avail) /* check for closed pipe */
1705 struct pollfd pollfd;
1706 int ret;
1708 pollfd.fd = fd;
1709 pollfd.events = POLLIN;
1710 pollfd.revents = 0;
1711 ret = poll( &pollfd, 1, 0 );
1712 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1714 if (needs_close) close( fd );
1715 status = STATUS_PIPE_BROKEN;
1716 break;
1719 buffer->NamedPipeState = 0; /* FIXME */
1720 buffer->ReadDataAvailable = avail;
1721 buffer->NumberOfMessages = 0; /* FIXME */
1722 buffer->MessageLength = 0; /* FIXME */
1723 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1724 status = STATUS_SUCCESS;
1725 if (avail)
1727 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1728 if (data_size)
1730 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1731 if (res >= 0) io->Information += res;
1734 if (needs_close) close( fd );
1736 break;
1738 case FSCTL_PIPE_DISCONNECT:
1739 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1740 in_buffer, in_size, out_buffer, out_size );
1741 if (!status)
1743 int fd = server_remove_fd_from_cache( handle );
1744 if (fd != -1) close( fd );
1746 break;
1748 case FSCTL_PIPE_IMPERSONATE:
1749 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1750 status = RtlImpersonateSelf( SecurityImpersonation );
1751 break;
1753 case FSCTL_IS_VOLUME_MOUNTED:
1754 case FSCTL_LOCK_VOLUME:
1755 case FSCTL_UNLOCK_VOLUME:
1756 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1757 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1758 status = STATUS_SUCCESS;
1759 break;
1761 case FSCTL_GET_RETRIEVAL_POINTERS:
1763 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1765 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1767 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1769 buffer->ExtentCount = 1;
1770 buffer->StartingVcn.QuadPart = 1;
1771 buffer->Extents[0].NextVcn.QuadPart = 0;
1772 buffer->Extents[0].Lcn.QuadPart = 0;
1773 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1774 status = STATUS_SUCCESS;
1776 else
1778 io->Information = 0;
1779 status = STATUS_BUFFER_TOO_SMALL;
1781 break;
1783 case FSCTL_PIPE_LISTEN:
1784 case FSCTL_PIPE_WAIT:
1785 default:
1786 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1787 in_buffer, in_size, out_buffer, out_size );
1788 break;
1791 if (status != STATUS_PENDING) io->u.Status = status;
1792 return status;
1796 struct read_changes_fileio
1798 struct async_fileio io;
1799 void *buffer;
1800 ULONG buffer_size;
1801 ULONG data_size;
1802 char data[1];
1805 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb,
1806 NTSTATUS status, void **apc, void **arg )
1808 struct read_changes_fileio *fileio = user;
1809 int size = 0;
1811 if (status == STATUS_ALERTED)
1813 SERVER_START_REQ( read_change )
1815 req->handle = wine_server_obj_handle( fileio->io.handle );
1816 wine_server_set_reply( req, fileio->data, fileio->data_size );
1817 status = wine_server_call( req );
1818 size = wine_server_reply_size( reply );
1820 SERVER_END_REQ;
1822 if (status == STATUS_SUCCESS && fileio->buffer)
1824 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1825 int i, left = fileio->buffer_size;
1826 DWORD *last_entry_offset = NULL;
1827 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1829 while (size && left >= sizeof(*pfni))
1831 /* convert to an NT style path */
1832 for (i = 0; i < event->len; i++)
1833 if (event->name[i] == '/') event->name[i] = '\\';
1835 pfni->Action = event->action;
1836 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1837 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1838 last_entry_offset = &pfni->NextEntryOffset;
1840 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1842 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1843 pfni->FileNameLength *= sizeof(WCHAR);
1844 pfni->NextEntryOffset = i;
1845 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1846 left -= i;
1848 i = (offsetof(struct filesystem_event, name[event->len])
1849 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1850 event = (struct filesystem_event*)((char*)event + i);
1851 size -= i;
1854 if (size)
1856 status = STATUS_NOTIFY_ENUM_DIR;
1857 size = 0;
1859 else
1861 if (last_entry_offset) *last_entry_offset = 0;
1862 size = fileio->buffer_size - left;
1865 else
1867 status = STATUS_NOTIFY_ENUM_DIR;
1868 size = 0;
1872 if (status != STATUS_PENDING)
1874 iosb->u.Status = status;
1875 iosb->Information = size;
1876 *apc = fileio->io.apc;
1877 *arg = fileio->io.apc_arg;
1878 release_fileio( &fileio->io );
1880 return status;
1883 #define FILE_NOTIFY_ALL ( \
1884 FILE_NOTIFY_CHANGE_FILE_NAME | \
1885 FILE_NOTIFY_CHANGE_DIR_NAME | \
1886 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1887 FILE_NOTIFY_CHANGE_SIZE | \
1888 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1889 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1890 FILE_NOTIFY_CHANGE_CREATION | \
1891 FILE_NOTIFY_CHANGE_SECURITY )
1893 /******************************************************************************
1894 * NtNotifyChangeDirectoryFile [NTDLL.@]
1896 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1897 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1898 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1900 struct read_changes_fileio *fileio;
1901 NTSTATUS status;
1902 ULONG size = max( 4096, buffer_size );
1903 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1905 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1906 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1908 if (!iosb) return STATUS_ACCESS_VIOLATION;
1909 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1911 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1912 handle, apc, apc_context );
1913 if (!fileio) return STATUS_NO_MEMORY;
1915 fileio->buffer = buffer;
1916 fileio->buffer_size = buffer_size;
1917 fileio->data_size = size;
1919 SERVER_START_REQ( read_directory_changes )
1921 req->filter = filter;
1922 req->want_data = (buffer != NULL);
1923 req->subtree = subtree;
1924 req->async.handle = wine_server_obj_handle( handle );
1925 req->async.callback = wine_server_client_ptr( read_changes_apc );
1926 req->async.iosb = wine_server_client_ptr( iosb );
1927 req->async.arg = wine_server_client_ptr( fileio );
1928 req->async.event = wine_server_obj_handle( event );
1929 req->async.cvalue = cvalue;
1930 status = wine_server_call( req );
1932 SERVER_END_REQ;
1934 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1935 return status;
1938 /******************************************************************************
1939 * NtSetVolumeInformationFile [NTDLL.@]
1940 * ZwSetVolumeInformationFile [NTDLL.@]
1942 * Set volume information for an open file handle.
1944 * PARAMS
1945 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1946 * IoStatusBlock [O] Receives information about the operation on return
1947 * FsInformation [I] Source for volume information
1948 * Length [I] Size of FsInformation
1949 * FsInformationClass [I] Type of volume information to set
1951 * RETURNS
1952 * Success: 0. IoStatusBlock is updated.
1953 * Failure: An NTSTATUS error code describing the error.
1955 NTSTATUS WINAPI NtSetVolumeInformationFile(
1956 IN HANDLE FileHandle,
1957 PIO_STATUS_BLOCK IoStatusBlock,
1958 PVOID FsInformation,
1959 ULONG Length,
1960 FS_INFORMATION_CLASS FsInformationClass)
1962 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1963 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1964 return 0;
1967 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1968 static int futimens( int fd, const struct timespec spec[2] )
1970 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1972 #define HAVE_FUTIMENS
1973 #endif /* __ANDROID__ */
1975 #ifndef UTIME_OMIT
1976 #define UTIME_OMIT ((1 << 30) - 2)
1977 #endif
1979 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1981 NTSTATUS status = STATUS_SUCCESS;
1983 #ifdef HAVE_FUTIMENS
1984 struct timespec tv[2];
1986 tv[0].tv_sec = tv[1].tv_sec = 0;
1987 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1988 if (atime->QuadPart)
1990 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1991 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1993 if (mtime->QuadPart)
1995 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1996 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1998 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
2000 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
2001 struct timeval tv[2];
2002 struct stat st;
2004 if (!atime->QuadPart || !mtime->QuadPart)
2007 tv[0].tv_sec = tv[0].tv_usec = 0;
2008 tv[1].tv_sec = tv[1].tv_usec = 0;
2009 if (!fstat( fd, &st ))
2011 tv[0].tv_sec = st.st_atime;
2012 tv[1].tv_sec = st.st_mtime;
2013 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2014 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
2015 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2016 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
2017 #endif
2018 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2019 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
2020 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2021 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
2022 #endif
2025 if (atime->QuadPart)
2027 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
2028 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
2030 if (mtime->QuadPart)
2032 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
2033 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
2035 #ifdef HAVE_FUTIMES
2036 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
2037 #elif defined(HAVE_FUTIMESAT)
2038 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
2039 #endif
2041 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
2042 FIXME( "setting file times not supported\n" );
2043 status = STATUS_NOT_IMPLEMENTED;
2044 #endif
2045 return status;
2048 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
2049 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
2051 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
2052 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
2053 RtlSecondsSince1970ToTime( st->st_atime, atime );
2054 #ifdef HAVE_STRUCT_STAT_ST_MTIM
2055 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
2056 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
2057 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
2058 #endif
2059 #ifdef HAVE_STRUCT_STAT_ST_CTIM
2060 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
2061 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2062 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
2063 #endif
2064 #ifdef HAVE_STRUCT_STAT_ST_ATIM
2065 atime->QuadPart += st->st_atim.tv_nsec / 100;
2066 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
2067 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
2068 #endif
2069 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2070 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
2071 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
2072 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
2073 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
2074 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
2075 #endif
2076 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
2077 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
2078 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
2079 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
2080 #endif
2081 #else
2082 *creation = *mtime;
2083 #endif
2086 /* fill in the file information that depends on the stat and attribute info */
2087 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
2088 FILE_INFORMATION_CLASS class )
2090 switch (class)
2092 case FileBasicInformation:
2094 FILE_BASIC_INFORMATION *info = ptr;
2096 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2097 &info->LastAccessTime, &info->CreationTime );
2098 info->FileAttributes = attr;
2100 break;
2101 case FileStandardInformation:
2103 FILE_STANDARD_INFORMATION *info = ptr;
2105 if ((info->Directory = S_ISDIR(st->st_mode)))
2107 info->AllocationSize.QuadPart = 0;
2108 info->EndOfFile.QuadPart = 0;
2109 info->NumberOfLinks = 1;
2111 else
2113 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2114 info->EndOfFile.QuadPart = st->st_size;
2115 info->NumberOfLinks = st->st_nlink;
2118 break;
2119 case FileInternalInformation:
2121 FILE_INTERNAL_INFORMATION *info = ptr;
2122 info->IndexNumber.QuadPart = st->st_ino;
2124 break;
2125 case FileEndOfFileInformation:
2127 FILE_END_OF_FILE_INFORMATION *info = ptr;
2128 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2130 break;
2131 case FileAllInformation:
2133 FILE_ALL_INFORMATION *info = ptr;
2134 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2135 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2136 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2138 break;
2139 /* all directory structures start with the FileDirectoryInformation layout */
2140 case FileBothDirectoryInformation:
2141 case FileFullDirectoryInformation:
2142 case FileDirectoryInformation:
2144 FILE_DIRECTORY_INFORMATION *info = ptr;
2146 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2147 &info->LastAccessTime, &info->CreationTime );
2148 if (S_ISDIR(st->st_mode))
2150 info->AllocationSize.QuadPart = 0;
2151 info->EndOfFile.QuadPart = 0;
2153 else
2155 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2156 info->EndOfFile.QuadPart = st->st_size;
2158 info->FileAttributes = attr;
2160 break;
2161 case FileIdFullDirectoryInformation:
2163 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2164 info->FileId.QuadPart = st->st_ino;
2165 fill_file_info( st, attr, info, FileDirectoryInformation );
2167 break;
2168 case FileIdBothDirectoryInformation:
2170 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2171 info->FileId.QuadPart = st->st_ino;
2172 fill_file_info( st, attr, info, FileDirectoryInformation );
2174 break;
2176 default:
2177 return STATUS_INVALID_INFO_CLASS;
2179 return STATUS_SUCCESS;
2182 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2184 data_size_t size = 1024;
2185 NTSTATUS ret;
2186 char *name;
2188 for (;;)
2190 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2191 if (!name) return STATUS_NO_MEMORY;
2192 unix_name->MaximumLength = size + 1;
2194 SERVER_START_REQ( get_handle_unix_name )
2196 req->handle = wine_server_obj_handle( handle );
2197 wine_server_set_reply( req, name, size );
2198 ret = wine_server_call( req );
2199 size = reply->name_len;
2201 SERVER_END_REQ;
2203 if (!ret)
2205 name[size] = 0;
2206 unix_name->Buffer = name;
2207 unix_name->Length = size;
2208 break;
2210 RtlFreeHeap( GetProcessHeap(), 0, name );
2211 if (ret != STATUS_BUFFER_OVERFLOW) break;
2213 return ret;
2216 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2218 UNICODE_STRING nt_name;
2219 NTSTATUS status;
2221 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2223 const WCHAR *ptr = nt_name.Buffer;
2224 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2226 /* Skip the volume mount point. */
2227 while (ptr != end && *ptr == '\\') ++ptr;
2228 while (ptr != end && *ptr != '\\') ++ptr;
2229 while (ptr != end && *ptr == '\\') ++ptr;
2230 while (ptr != end && *ptr != '\\') ++ptr;
2232 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2233 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2234 else *name_len = info->FileNameLength;
2236 memcpy( info->FileName, ptr, *name_len );
2237 RtlFreeUnicodeString( &nt_name );
2240 return status;
2243 /******************************************************************************
2244 * NtQueryInformationFile [NTDLL.@]
2245 * ZwQueryInformationFile [NTDLL.@]
2247 * Get information about an open file handle.
2249 * PARAMS
2250 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2251 * io [O] Receives information about the operation on return
2252 * ptr [O] Destination for file information
2253 * len [I] Size of FileInformation
2254 * class [I] Type of file information to get
2256 * RETURNS
2257 * Success: 0. IoStatusBlock and FileInformation are updated.
2258 * Failure: An NTSTATUS error code describing the error.
2260 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2261 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2263 static const size_t info_sizes[] =
2266 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2267 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2268 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2269 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2270 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2271 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2272 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2273 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2274 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2275 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2276 0, /* FileLinkInformation */
2277 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2278 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2279 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2280 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2281 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2282 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2283 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2284 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2285 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2286 0, /* FileAlternateNameInformation */
2287 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2288 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2289 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2290 0, /* FilePipeRemoteInformation */
2291 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2292 0, /* FileMailslotSetInformation */
2293 0, /* FileCompressionInformation */
2294 0, /* FileObjectIdInformation */
2295 0, /* FileCompletionInformation */
2296 0, /* FileMoveClusterInformation */
2297 0, /* FileQuotaInformation */
2298 0, /* FileReparsePointInformation */
2299 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2300 0, /* FileAttributeTagInformation */
2301 0, /* FileTrackingInformation */
2302 0, /* FileIdBothDirectoryInformation */
2303 0, /* FileIdFullDirectoryInformation */
2304 0, /* FileValidDataLengthInformation */
2305 0, /* FileShortNameInformation */
2306 0, /* FileIoCompletionNotificationInformation, */
2307 0, /* FileIoStatusBlockRangeInformation */
2308 0, /* FileIoPriorityHintInformation */
2309 0, /* FileSfioReserveInformation */
2310 0, /* FileSfioVolumeInformation */
2311 0, /* FileHardLinkInformation */
2312 0, /* FileProcessIdsUsingFileInformation */
2313 0, /* FileNormalizedNameInformation */
2314 0, /* FileNetworkPhysicalNameInformation */
2315 0, /* FileIdGlobalTxDirectoryInformation */
2316 0, /* FileIsRemoteDeviceInformation */
2317 0, /* FileAttributeCacheInformation */
2318 0, /* FileNumaNodeInformation */
2319 0, /* FileStandardLinkInformation */
2320 0, /* FileRemoteProtocolInformation */
2321 0, /* FileReplaceCompletionInformation */
2324 struct stat st;
2325 int fd, needs_close = FALSE;
2326 ULONG attr;
2328 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2330 io->Information = 0;
2332 if (class <= 0 || class >= FileMaximumInformation)
2333 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2334 if (!info_sizes[class])
2336 FIXME("Unsupported class (%d)\n", class);
2337 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2339 if (len < info_sizes[class])
2340 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2342 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2344 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2345 return io->u.Status;
2348 switch (class)
2350 case FileBasicInformation:
2351 if (fd_get_file_info( fd, &st, &attr ) == -1)
2352 io->u.Status = FILE_GetNtStatus();
2353 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2354 io->u.Status = STATUS_INVALID_INFO_CLASS;
2355 else
2356 fill_file_info( &st, attr, ptr, class );
2357 break;
2358 case FileStandardInformation:
2360 FILE_STANDARD_INFORMATION *info = ptr;
2362 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2363 else
2365 fill_file_info( &st, attr, info, class );
2366 info->DeletePending = FALSE; /* FIXME */
2369 break;
2370 case FilePositionInformation:
2372 FILE_POSITION_INFORMATION *info = ptr;
2373 off_t res = lseek( fd, 0, SEEK_CUR );
2374 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2375 else info->CurrentByteOffset.QuadPart = res;
2377 break;
2378 case FileInternalInformation:
2379 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2380 else fill_file_info( &st, attr, ptr, class );
2381 break;
2382 case FileEaInformation:
2384 FILE_EA_INFORMATION *info = ptr;
2385 info->EaSize = 0;
2387 break;
2388 case FileEndOfFileInformation:
2389 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2390 else fill_file_info( &st, attr, ptr, class );
2391 break;
2392 case FileAllInformation:
2394 FILE_ALL_INFORMATION *info = ptr;
2395 ANSI_STRING unix_name;
2397 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2398 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2399 io->u.Status = STATUS_INVALID_INFO_CLASS;
2400 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2402 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2404 fill_file_info( &st, attr, info, FileAllInformation );
2405 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2406 info->EaInformation.EaSize = 0;
2407 info->AccessInformation.AccessFlags = 0; /* FIXME */
2408 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2409 info->ModeInformation.Mode = 0; /* FIXME */
2410 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2412 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2413 RtlFreeAnsiString( &unix_name );
2414 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2417 break;
2418 case FileMailslotQueryInformation:
2420 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2422 SERVER_START_REQ( set_mailslot_info )
2424 req->handle = wine_server_obj_handle( hFile );
2425 req->flags = 0;
2426 io->u.Status = wine_server_call( req );
2427 if( io->u.Status == STATUS_SUCCESS )
2429 info->MaximumMessageSize = reply->max_msgsize;
2430 info->MailslotQuota = 0;
2431 info->NextMessageSize = 0;
2432 info->MessagesAvailable = 0;
2433 info->ReadTimeout.QuadPart = reply->read_timeout;
2436 SERVER_END_REQ;
2437 if (!io->u.Status)
2439 char *tmpbuf;
2440 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2441 if (size > 0x10000) size = 0x10000;
2442 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2444 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2446 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2447 info->MessagesAvailable = (res > 0);
2448 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2449 if (needs_close) close( fd );
2451 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2455 break;
2456 case FilePipeInformation:
2458 FILE_PIPE_INFORMATION* pi = ptr;
2460 SERVER_START_REQ( get_named_pipe_info )
2462 req->handle = wine_server_obj_handle( hFile );
2463 if (!(io->u.Status = wine_server_call( req )))
2465 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2466 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2467 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2468 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2471 SERVER_END_REQ;
2473 break;
2474 case FilePipeLocalInformation:
2476 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2478 SERVER_START_REQ( get_named_pipe_info )
2480 req->handle = wine_server_obj_handle( hFile );
2481 if (!(io->u.Status = wine_server_call( req )))
2483 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2484 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2485 switch (reply->sharing)
2487 case FILE_SHARE_READ:
2488 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2489 break;
2490 case FILE_SHARE_WRITE:
2491 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2492 break;
2493 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2494 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2495 break;
2497 pli->MaximumInstances = reply->maxinstances;
2498 pli->CurrentInstances = reply->instances;
2499 pli->InboundQuota = reply->insize;
2500 pli->ReadDataAvailable = 0; /* FIXME */
2501 pli->OutboundQuota = reply->outsize;
2502 pli->WriteQuotaAvailable = 0; /* FIXME */
2503 pli->NamedPipeState = 0; /* FIXME */
2504 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2505 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2508 SERVER_END_REQ;
2510 break;
2511 case FileNameInformation:
2513 FILE_NAME_INFORMATION *info = ptr;
2514 ANSI_STRING unix_name;
2516 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2518 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2519 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2520 RtlFreeAnsiString( &unix_name );
2521 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2524 break;
2525 case FileNetworkOpenInformation:
2527 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2528 ANSI_STRING unix_name;
2530 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2532 ULONG attributes;
2533 struct stat st;
2535 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2536 io->u.Status = FILE_GetNtStatus();
2537 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2538 io->u.Status = STATUS_INVALID_INFO_CLASS;
2539 else
2541 FILE_BASIC_INFORMATION basic;
2542 FILE_STANDARD_INFORMATION std;
2544 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2545 fill_file_info( &st, attributes, &std, FileStandardInformation );
2547 info->CreationTime = basic.CreationTime;
2548 info->LastAccessTime = basic.LastAccessTime;
2549 info->LastWriteTime = basic.LastWriteTime;
2550 info->ChangeTime = basic.ChangeTime;
2551 info->AllocationSize = std.AllocationSize;
2552 info->EndOfFile = std.EndOfFile;
2553 info->FileAttributes = basic.FileAttributes;
2555 RtlFreeAnsiString( &unix_name );
2558 break;
2559 default:
2560 FIXME("Unsupported class (%d)\n", class);
2561 io->u.Status = STATUS_NOT_IMPLEMENTED;
2562 break;
2564 if (needs_close) close( fd );
2565 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2566 return io->u.Status;
2569 /******************************************************************************
2570 * NtSetInformationFile [NTDLL.@]
2571 * ZwSetInformationFile [NTDLL.@]
2573 * Set information about an open file handle.
2575 * PARAMS
2576 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2577 * io [O] Receives information about the operation on return
2578 * ptr [I] Source for file information
2579 * len [I] Size of FileInformation
2580 * class [I] Type of file information to set
2582 * RETURNS
2583 * Success: 0. io is updated.
2584 * Failure: An NTSTATUS error code describing the error.
2586 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2587 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2589 int fd, needs_close;
2591 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2593 io->u.Status = STATUS_SUCCESS;
2594 switch (class)
2596 case FileBasicInformation:
2597 if (len >= sizeof(FILE_BASIC_INFORMATION))
2599 struct stat st;
2600 const FILE_BASIC_INFORMATION *info = ptr;
2602 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2603 return io->u.Status;
2605 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2606 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2608 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2610 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2611 else
2613 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2615 if (S_ISDIR( st.st_mode))
2616 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2617 else
2618 st.st_mode &= ~0222; /* clear write permission bits */
2620 else
2622 /* add write permission only where we already have read permission */
2623 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2625 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2629 if (needs_close) close( fd );
2631 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2632 break;
2634 case FilePositionInformation:
2635 if (len >= sizeof(FILE_POSITION_INFORMATION))
2637 const FILE_POSITION_INFORMATION *info = ptr;
2639 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2640 return io->u.Status;
2642 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2643 io->u.Status = FILE_GetNtStatus();
2645 if (needs_close) close( fd );
2647 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2648 break;
2650 case FileEndOfFileInformation:
2651 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2653 struct stat st;
2654 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2656 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2657 return io->u.Status;
2659 /* first try normal truncate */
2660 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2662 /* now check for the need to extend the file */
2663 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2665 static const char zero;
2667 /* extend the file one byte beyond the requested size and then truncate it */
2668 /* this should work around ftruncate implementations that can't extend files */
2669 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2670 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2672 io->u.Status = FILE_GetNtStatus();
2674 if (needs_close) close( fd );
2676 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2677 break;
2679 case FilePipeInformation:
2680 if (len >= sizeof(FILE_PIPE_INFORMATION))
2682 FILE_PIPE_INFORMATION *info = ptr;
2684 if ((info->CompletionMode | info->ReadMode) & ~1)
2686 io->u.Status = STATUS_INVALID_PARAMETER;
2687 break;
2690 SERVER_START_REQ( set_named_pipe_info )
2692 req->handle = wine_server_obj_handle( handle );
2693 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2694 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2695 io->u.Status = wine_server_call( req );
2697 SERVER_END_REQ;
2699 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2700 break;
2702 case FileMailslotSetInformation:
2704 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2706 SERVER_START_REQ( set_mailslot_info )
2708 req->handle = wine_server_obj_handle( handle );
2709 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2710 req->read_timeout = info->ReadTimeout.QuadPart;
2711 io->u.Status = wine_server_call( req );
2713 SERVER_END_REQ;
2715 break;
2717 case FileCompletionInformation:
2718 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2720 FILE_COMPLETION_INFORMATION *info = ptr;
2722 SERVER_START_REQ( set_completion_info )
2724 req->handle = wine_server_obj_handle( handle );
2725 req->chandle = wine_server_obj_handle( info->CompletionPort );
2726 req->ckey = info->CompletionKey;
2727 io->u.Status = wine_server_call( req );
2729 SERVER_END_REQ;
2730 } else
2731 io->u.Status = STATUS_INVALID_PARAMETER_3;
2732 break;
2734 case FileAllInformation:
2735 io->u.Status = STATUS_INVALID_INFO_CLASS;
2736 break;
2738 case FileValidDataLengthInformation:
2739 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2741 struct stat st;
2742 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2744 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2745 return io->u.Status;
2747 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2748 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2749 io->u.Status = STATUS_INVALID_PARAMETER;
2750 else
2752 #ifdef HAVE_FALLOCATE
2753 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2755 NTSTATUS status = FILE_GetNtStatus();
2756 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2757 else io->u.Status = status;
2759 #else
2760 FIXME( "setting valid data length not supported\n" );
2761 #endif
2763 if (needs_close) close( fd );
2765 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2766 break;
2768 default:
2769 FIXME("Unsupported class (%d)\n", class);
2770 io->u.Status = STATUS_NOT_IMPLEMENTED;
2771 break;
2773 io->Information = 0;
2774 return io->u.Status;
2778 /******************************************************************************
2779 * NtQueryFullAttributesFile (NTDLL.@)
2781 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2782 FILE_NETWORK_OPEN_INFORMATION *info )
2784 ANSI_STRING unix_name;
2785 NTSTATUS status;
2787 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2789 ULONG attributes;
2790 struct stat st;
2792 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2793 status = FILE_GetNtStatus();
2794 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2795 status = STATUS_INVALID_INFO_CLASS;
2796 else
2798 FILE_BASIC_INFORMATION basic;
2799 FILE_STANDARD_INFORMATION std;
2801 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2802 fill_file_info( &st, attributes, &std, FileStandardInformation );
2804 info->CreationTime = basic.CreationTime;
2805 info->LastAccessTime = basic.LastAccessTime;
2806 info->LastWriteTime = basic.LastWriteTime;
2807 info->ChangeTime = basic.ChangeTime;
2808 info->AllocationSize = std.AllocationSize;
2809 info->EndOfFile = std.EndOfFile;
2810 info->FileAttributes = basic.FileAttributes;
2811 if (DIR_is_hidden_file( attr->ObjectName ))
2812 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2814 RtlFreeAnsiString( &unix_name );
2816 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2817 return status;
2821 /******************************************************************************
2822 * NtQueryAttributesFile (NTDLL.@)
2823 * ZwQueryAttributesFile (NTDLL.@)
2825 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2827 ANSI_STRING unix_name;
2828 NTSTATUS status;
2830 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2832 ULONG attributes;
2833 struct stat st;
2835 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2836 status = FILE_GetNtStatus();
2837 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2838 status = STATUS_INVALID_INFO_CLASS;
2839 else
2841 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2842 if (DIR_is_hidden_file( attr->ObjectName ))
2843 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2845 RtlFreeAnsiString( &unix_name );
2847 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2848 return status;
2852 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2853 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2854 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2855 unsigned int flags )
2857 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2859 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2860 /* Don't assume read-only, let the mount options set it below */
2861 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2863 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2864 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2866 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2867 info->Characteristics |= FILE_REMOTE_DEVICE;
2869 else if (!strcmp("procfs", fstypename))
2870 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2871 else
2872 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2874 if (flags & MNT_RDONLY)
2875 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2877 if (!(flags & MNT_LOCAL))
2879 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2880 info->Characteristics |= FILE_REMOTE_DEVICE;
2883 #endif
2885 static inline BOOL is_device_placeholder( int fd )
2887 static const char wine_placeholder[] = "Wine device placeholder";
2888 char buffer[sizeof(wine_placeholder)-1];
2890 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2891 return FALSE;
2892 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2895 /******************************************************************************
2896 * get_device_info
2898 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2900 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2902 struct stat st;
2904 info->Characteristics = 0;
2905 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2906 if (S_ISCHR( st.st_mode ))
2908 info->DeviceType = FILE_DEVICE_UNKNOWN;
2909 #ifdef linux
2910 switch(major(st.st_rdev))
2912 case MEM_MAJOR:
2913 info->DeviceType = FILE_DEVICE_NULL;
2914 break;
2915 case TTY_MAJOR:
2916 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2917 break;
2918 case LP_MAJOR:
2919 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2920 break;
2921 case SCSI_TAPE_MAJOR:
2922 info->DeviceType = FILE_DEVICE_TAPE;
2923 break;
2925 #endif
2927 else if (S_ISBLK( st.st_mode ))
2929 info->DeviceType = FILE_DEVICE_DISK;
2931 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2933 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2935 else if (is_device_placeholder( fd ))
2937 info->DeviceType = FILE_DEVICE_DISK;
2939 else /* regular file or directory */
2941 #if defined(linux) && defined(HAVE_FSTATFS)
2942 struct statfs stfs;
2944 /* check for floppy disk */
2945 if (major(st.st_dev) == FLOPPY_MAJOR)
2946 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2948 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2949 switch (stfs.f_type)
2951 case 0x9660: /* iso9660 */
2952 case 0x9fa1: /* supermount */
2953 case 0x15013346: /* udf */
2954 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2955 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2956 break;
2957 case 0x6969: /* nfs */
2958 case 0x517B: /* smbfs */
2959 case 0x564c: /* ncpfs */
2960 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2961 info->Characteristics |= FILE_REMOTE_DEVICE;
2962 break;
2963 case 0x01021994: /* tmpfs */
2964 case 0x28cd3d45: /* cramfs */
2965 case 0x1373: /* devfs */
2966 case 0x9fa0: /* procfs */
2967 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2968 break;
2969 default:
2970 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2971 break;
2973 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2974 struct statfs stfs;
2976 if (fstatfs( fd, &stfs ) < 0)
2977 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2978 else
2979 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2980 #elif defined(__NetBSD__)
2981 struct statvfs stfs;
2983 if (fstatvfs( fd, &stfs) < 0)
2984 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2985 else
2986 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2987 #elif defined(sun)
2988 /* Use dkio to work out device types */
2990 # include <sys/dkio.h>
2991 # include <sys/vtoc.h>
2992 struct dk_cinfo dkinf;
2993 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2994 if(retval==-1){
2995 WARN("Unable to get disk device type information - assuming a disk like device\n");
2996 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2998 switch (dkinf.dki_ctype)
3000 case DKC_CDROM:
3001 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
3002 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
3003 break;
3004 case DKC_NCRFLOPPY:
3005 case DKC_SMSFLOPPY:
3006 case DKC_INTEL82072:
3007 case DKC_INTEL82077:
3008 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3009 info->Characteristics |= FILE_REMOVABLE_MEDIA;
3010 break;
3011 case DKC_MD:
3012 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
3013 break;
3014 default:
3015 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3018 #else
3019 static int warned;
3020 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
3021 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
3022 #endif
3023 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
3025 return STATUS_SUCCESS;
3029 /******************************************************************************
3030 * NtQueryVolumeInformationFile [NTDLL.@]
3031 * ZwQueryVolumeInformationFile [NTDLL.@]
3033 * Get volume information for an open file handle.
3035 * PARAMS
3036 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3037 * io [O] Receives information about the operation on return
3038 * buffer [O] Destination for volume information
3039 * length [I] Size of FsInformation
3040 * info_class [I] Type of volume information to set
3042 * RETURNS
3043 * Success: 0. io and buffer are updated.
3044 * Failure: An NTSTATUS error code describing the error.
3046 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
3047 PVOID buffer, ULONG length,
3048 FS_INFORMATION_CLASS info_class )
3050 int fd, needs_close;
3051 struct stat st;
3052 static int once;
3054 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
3055 return io->u.Status;
3057 io->u.Status = STATUS_NOT_IMPLEMENTED;
3058 io->Information = 0;
3060 switch( info_class )
3062 case FileFsVolumeInformation:
3063 if (!once++) FIXME( "%p: volume info not supported\n", handle );
3064 break;
3065 case FileFsLabelInformation:
3066 FIXME( "%p: label info not supported\n", handle );
3067 break;
3068 case FileFsSizeInformation:
3069 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
3070 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3071 else
3073 FILE_FS_SIZE_INFORMATION *info = buffer;
3075 if (fstat( fd, &st ) < 0)
3077 io->u.Status = FILE_GetNtStatus();
3078 break;
3080 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
3082 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
3084 else
3086 ULONGLONG bsize;
3087 /* Linux's fstatvfs is buggy */
3088 #if !defined(linux) || !defined(HAVE_FSTATFS)
3089 struct statvfs stfs;
3091 if (fstatvfs( fd, &stfs ) < 0)
3093 io->u.Status = FILE_GetNtStatus();
3094 break;
3096 bsize = stfs.f_frsize;
3097 #else
3098 struct statfs stfs;
3099 if (fstatfs( fd, &stfs ) < 0)
3101 io->u.Status = FILE_GetNtStatus();
3102 break;
3104 bsize = stfs.f_bsize;
3105 #endif
3106 if (bsize == 2048) /* assume CD-ROM */
3108 info->BytesPerSector = 2048;
3109 info->SectorsPerAllocationUnit = 1;
3111 else
3113 info->BytesPerSector = 512;
3114 info->SectorsPerAllocationUnit = 8;
3116 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3117 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3118 io->Information = sizeof(*info);
3119 io->u.Status = STATUS_SUCCESS;
3122 break;
3123 case FileFsDeviceInformation:
3124 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3125 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3126 else
3128 FILE_FS_DEVICE_INFORMATION *info = buffer;
3130 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3131 io->Information = sizeof(*info);
3133 break;
3134 case FileFsAttributeInformation:
3135 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3136 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3137 else
3139 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3141 FIXME( "%p: faking attribute info\n", handle );
3142 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3143 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3144 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3145 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3146 info->FileSystemNameLength = sizeof(ntfsW);
3147 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3149 io->Information = sizeof(*info);
3150 io->u.Status = STATUS_SUCCESS;
3152 break;
3153 case FileFsControlInformation:
3154 FIXME( "%p: control info not supported\n", handle );
3155 break;
3156 case FileFsFullSizeInformation:
3157 FIXME( "%p: full size info not supported\n", handle );
3158 break;
3159 case FileFsObjectIdInformation:
3160 FIXME( "%p: object id info not supported\n", handle );
3161 break;
3162 case FileFsMaximumInformation:
3163 FIXME( "%p: maximum info not supported\n", handle );
3164 break;
3165 default:
3166 io->u.Status = STATUS_INVALID_PARAMETER;
3167 break;
3169 if (needs_close) close( fd );
3170 return io->u.Status;
3174 /******************************************************************
3175 * NtQueryEaFile (NTDLL.@)
3177 * Read extended attributes from NTFS files.
3179 * PARAMS
3180 * hFile [I] File handle, must be opened with FILE_READ_EA access
3181 * iosb [O] Receives information about the operation on return
3182 * buffer [O] Output buffer
3183 * length [I] Length of output buffer
3184 * single_entry [I] Only read and return one entry
3185 * ea_list [I] Optional list with names of EAs to return
3186 * ea_list_len [I] Length of ea_list in bytes
3187 * ea_index [I] Optional pointer to 1-based index of attribute to return
3188 * restart [I] restart EA scan
3190 * RETURNS
3191 * Success: 0. Atrributes read into buffer
3192 * Failure: An NTSTATUS error code describing the error.
3194 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3195 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3196 PULONG ea_index, BOOLEAN restart )
3198 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3199 hFile, iosb, buffer, length, single_entry, ea_list,
3200 ea_list_len, ea_index, restart);
3201 return STATUS_ACCESS_DENIED;
3205 /******************************************************************
3206 * NtSetEaFile (NTDLL.@)
3208 * Update extended attributes for NTFS files.
3210 * PARAMS
3211 * hFile [I] File handle, must be opened with FILE_READ_EA access
3212 * iosb [O] Receives information about the operation on return
3213 * buffer [I] Buffer with EA information
3214 * length [I] Length of buffer
3216 * RETURNS
3217 * Success: 0. Attributes are updated
3218 * Failure: An NTSTATUS error code describing the error.
3220 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3222 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3223 return STATUS_ACCESS_DENIED;
3227 /******************************************************************
3228 * NtFlushBuffersFile (NTDLL.@)
3230 * Flush any buffered data on an open file handle.
3232 * PARAMS
3233 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3234 * IoStatusBlock [O] Receives information about the operation on return
3236 * RETURNS
3237 * Success: 0. IoStatusBlock is updated.
3238 * Failure: An NTSTATUS error code describing the error.
3240 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3242 NTSTATUS ret;
3243 HANDLE hEvent = NULL;
3244 enum server_fd_type type;
3245 int fd, needs_close;
3247 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3249 if (!ret && type == FD_TYPE_SERIAL)
3251 ret = COMM_FlushBuffersFile( fd );
3253 else
3255 SERVER_START_REQ( flush )
3257 req->blocking = 1; /* always blocking */
3258 req->async.handle = wine_server_obj_handle( hFile );
3259 req->async.iosb = wine_server_client_ptr( IoStatusBlock );
3260 ret = wine_server_call( req );
3261 hEvent = wine_server_ptr_handle( reply->event );
3263 SERVER_END_REQ;
3265 if (hEvent)
3267 NtWaitForSingleObject( hEvent, FALSE, NULL );
3268 NtClose( hEvent );
3269 ret = STATUS_SUCCESS;
3273 if (needs_close) close( fd );
3274 return ret;
3277 /******************************************************************
3278 * NtLockFile (NTDLL.@)
3282 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3283 PIO_APC_ROUTINE apc, void* apc_user,
3284 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3285 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3286 BOOLEAN exclusive )
3288 NTSTATUS ret;
3289 HANDLE handle;
3290 BOOLEAN async;
3291 static BOOLEAN warn = TRUE;
3293 if (apc || io_status || key)
3295 FIXME("Unimplemented yet parameter\n");
3296 return STATUS_NOT_IMPLEMENTED;
3299 if (apc_user && warn)
3301 FIXME("I/O completion on lock not implemented yet\n");
3302 warn = FALSE;
3305 for (;;)
3307 SERVER_START_REQ( lock_file )
3309 req->handle = wine_server_obj_handle( hFile );
3310 req->offset = offset->QuadPart;
3311 req->count = count->QuadPart;
3312 req->shared = !exclusive;
3313 req->wait = !dont_wait;
3314 ret = wine_server_call( req );
3315 handle = wine_server_ptr_handle( reply->handle );
3316 async = reply->overlapped;
3318 SERVER_END_REQ;
3319 if (ret != STATUS_PENDING)
3321 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3322 return ret;
3325 if (async)
3327 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3328 if (handle) NtClose( handle );
3329 return STATUS_PENDING;
3331 if (handle)
3333 NtWaitForSingleObject( handle, FALSE, NULL );
3334 NtClose( handle );
3336 else
3338 LARGE_INTEGER time;
3340 /* Unix lock conflict, sleep a bit and retry */
3341 time.QuadPart = 100 * (ULONGLONG)10000;
3342 time.QuadPart = -time.QuadPart;
3343 NtDelayExecution( FALSE, &time );
3349 /******************************************************************
3350 * NtUnlockFile (NTDLL.@)
3354 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3355 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3356 PULONG key )
3358 NTSTATUS status;
3360 TRACE( "%p %x%08x %x%08x\n",
3361 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3363 if (io_status || key)
3365 FIXME("Unimplemented yet parameter\n");
3366 return STATUS_NOT_IMPLEMENTED;
3369 SERVER_START_REQ( unlock_file )
3371 req->handle = wine_server_obj_handle( hFile );
3372 req->offset = offset->QuadPart;
3373 req->count = count->QuadPart;
3374 status = wine_server_call( req );
3376 SERVER_END_REQ;
3377 return status;
3380 /******************************************************************
3381 * NtCreateNamedPipeFile (NTDLL.@)
3385 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3386 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3387 ULONG sharing, ULONG dispo, ULONG options,
3388 ULONG pipe_type, ULONG read_mode,
3389 ULONG completion_mode, ULONG max_inst,
3390 ULONG inbound_quota, ULONG outbound_quota,
3391 PLARGE_INTEGER timeout)
3393 struct security_descriptor *sd = NULL;
3394 struct object_attributes objattr;
3395 NTSTATUS status;
3397 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3398 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3399 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3400 outbound_quota, timeout);
3402 /* assume we only get relative timeout */
3403 if (timeout->QuadPart > 0)
3404 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3406 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
3407 objattr.sd_len = 0;
3408 objattr.name_len = attr->ObjectName->Length;
3410 status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
3411 if (status != STATUS_SUCCESS) return status;
3413 SERVER_START_REQ( create_named_pipe )
3415 req->access = access;
3416 req->attributes = attr->Attributes;
3417 req->options = options;
3418 req->sharing = sharing;
3419 req->flags =
3420 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3421 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3422 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3423 req->maxinstances = max_inst;
3424 req->outsize = outbound_quota;
3425 req->insize = inbound_quota;
3426 req->timeout = timeout->QuadPart;
3427 wine_server_add_data( req, &objattr, sizeof(objattr) );
3428 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
3429 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
3430 status = wine_server_call( req );
3431 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3433 SERVER_END_REQ;
3435 NTDLL_free_struct_sd( sd );
3436 return status;
3439 /******************************************************************
3440 * NtDeleteFile (NTDLL.@)
3444 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3446 NTSTATUS status;
3447 HANDLE hFile;
3448 IO_STATUS_BLOCK io;
3450 TRACE("%p\n", ObjectAttributes);
3451 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3452 ObjectAttributes, &io, NULL, 0,
3453 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3454 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3455 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3456 return status;
3459 /******************************************************************
3460 * NtCancelIoFileEx (NTDLL.@)
3464 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3466 TRACE("%p %p %p\n", hFile, iosb, io_status );
3468 SERVER_START_REQ( cancel_async )
3470 req->handle = wine_server_obj_handle( hFile );
3471 req->iosb = wine_server_client_ptr( iosb );
3472 req->only_thread = FALSE;
3473 io_status->u.Status = wine_server_call( req );
3475 SERVER_END_REQ;
3477 return io_status->u.Status;
3480 /******************************************************************
3481 * NtCancelIoFile (NTDLL.@)
3485 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3487 TRACE("%p %p\n", hFile, io_status );
3489 SERVER_START_REQ( cancel_async )
3491 req->handle = wine_server_obj_handle( hFile );
3492 req->iosb = 0;
3493 req->only_thread = TRUE;
3494 io_status->u.Status = wine_server_call( req );
3496 SERVER_END_REQ;
3498 return io_status->u.Status;
3501 /******************************************************************************
3502 * NtCreateMailslotFile [NTDLL.@]
3503 * ZwCreateMailslotFile [NTDLL.@]
3505 * PARAMS
3506 * pHandle [O] pointer to receive the handle created
3507 * DesiredAccess [I] access mode (read, write, etc)
3508 * ObjectAttributes [I] fully qualified NT path of the mailslot
3509 * IoStatusBlock [O] receives completion status and other info
3510 * CreateOptions [I]
3511 * MailslotQuota [I]
3512 * MaxMessageSize [I]
3513 * TimeOut [I]
3515 * RETURNS
3516 * An NT status code
3518 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3519 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3520 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3521 PLARGE_INTEGER TimeOut)
3523 LARGE_INTEGER timeout;
3524 NTSTATUS ret;
3526 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3527 pHandle, DesiredAccess, attr, IoStatusBlock,
3528 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3530 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3531 if (!attr) return STATUS_INVALID_PARAMETER;
3532 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3535 * For a NULL TimeOut pointer set the default timeout value
3537 if (!TimeOut)
3538 timeout.QuadPart = -1;
3539 else
3540 timeout.QuadPart = TimeOut->QuadPart;
3542 SERVER_START_REQ( create_mailslot )
3544 req->access = DesiredAccess;
3545 req->attributes = attr->Attributes;
3546 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3547 req->max_msgsize = MaxMessageSize;
3548 req->read_timeout = timeout.QuadPart;
3549 wine_server_add_data( req, attr->ObjectName->Buffer,
3550 attr->ObjectName->Length );
3551 ret = wine_server_call( req );
3552 if( ret == STATUS_SUCCESS )
3553 *pHandle = wine_server_ptr_handle( reply->handle );
3555 SERVER_END_REQ;
3557 return ret;