winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / ntdll / file.c
blob92e83dde6b676ce7920fcd4e7e9403cb8be8d5f2
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 static struct async_fileio *fileio_freelist;
358 static void release_fileio( struct async_fileio *io )
360 for (;;)
362 struct async_fileio *next = fileio_freelist;
363 io->next = next;
364 if (interlocked_cmpxchg_ptr( (void **)&fileio_freelist, io, next ) == next) return;
368 static struct async_fileio *alloc_fileio( DWORD size, HANDLE handle, PIO_APC_ROUTINE apc, void *arg )
370 /* first free remaining previous fileinfos */
372 struct async_fileio *io = interlocked_xchg_ptr( (void **)&fileio_freelist, NULL );
374 while (io)
376 struct async_fileio *next = io->next;
377 RtlFreeHeap( GetProcessHeap(), 0, io );
378 io = next;
381 if ((io = RtlAllocateHeap( GetProcessHeap(), 0, size )))
383 io->handle = handle;
384 io->apc = apc;
385 io->apc_arg = arg;
387 return io;
390 /***********************************************************************
391 * FILE_GetNtStatus(void)
393 * Retrieve the Nt Status code from errno.
394 * Try to be consistent with FILE_SetDosError().
396 NTSTATUS FILE_GetNtStatus(void)
398 int err = errno;
400 TRACE( "errno = %d\n", errno );
401 switch (err)
403 case EAGAIN: return STATUS_SHARING_VIOLATION;
404 case EBADF: return STATUS_INVALID_HANDLE;
405 case EBUSY: return STATUS_DEVICE_BUSY;
406 case ENOSPC: return STATUS_DISK_FULL;
407 case EPERM:
408 case EROFS:
409 case EACCES: return STATUS_ACCESS_DENIED;
410 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
411 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
412 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
413 case EMFILE:
414 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
415 case EINVAL: return STATUS_INVALID_PARAMETER;
416 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
417 case EPIPE: return STATUS_PIPE_DISCONNECTED;
418 case EIO: return STATUS_DEVICE_NOT_READY;
419 #ifdef ENOMEDIUM
420 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
421 #endif
422 case ENXIO: return STATUS_NO_SUCH_DEVICE;
423 case ENOTTY:
424 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
425 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
426 case EFAULT: return STATUS_ACCESS_VIOLATION;
427 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
428 #ifdef ETIME /* Missing on FreeBSD */
429 case ETIME: return STATUS_IO_TIMEOUT;
430 #endif
431 case ENOEXEC: /* ?? */
432 case EEXIST: /* ?? */
433 default:
434 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
435 return STATUS_UNSUCCESSFUL;
439 /***********************************************************************
440 * FILE_AsyncReadService (INTERNAL)
442 static NTSTATUS FILE_AsyncReadService( void *user, IO_STATUS_BLOCK *iosb,
443 NTSTATUS status, void **apc, void **arg )
445 struct async_fileio_read *fileio = user;
446 int fd, needs_close, result;
448 switch (status)
450 case STATUS_ALERTED: /* got some new data */
451 /* check to see if the data is ready (non-blocking) */
452 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
453 &needs_close, NULL, NULL )))
454 break;
456 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
457 if (needs_close) close( fd );
459 if (result < 0)
461 if (errno == EAGAIN || errno == EINTR)
462 status = STATUS_PENDING;
463 else /* check to see if the transfer is complete */
464 status = FILE_GetNtStatus();
466 else if (result == 0)
468 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
470 else
472 fileio->already += result;
473 if (fileio->already >= fileio->count || fileio->avail_mode)
474 status = STATUS_SUCCESS;
475 else
477 /* if we only have to read the available data, and none is available,
478 * simply cancel the request. If data was available, it has been read
479 * while in by previous call (NtDelayExecution)
481 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
484 break;
486 case STATUS_TIMEOUT:
487 case STATUS_IO_TIMEOUT:
488 if (fileio->already) status = STATUS_SUCCESS;
489 break;
491 if (status != STATUS_PENDING)
493 iosb->u.Status = status;
494 iosb->Information = fileio->already;
495 *apc = fileio->io.apc;
496 *arg = fileio->io.apc_arg;
497 release_fileio( &fileio->io );
499 return status;
502 struct io_timeouts
504 int interval; /* max interval between two bytes */
505 int total; /* total timeout for the whole operation */
506 int end_time; /* absolute time of end of operation */
509 /* retrieve the I/O timeouts to use for a given handle */
510 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
511 struct io_timeouts *timeouts )
513 NTSTATUS status = STATUS_SUCCESS;
515 timeouts->interval = timeouts->total = -1;
517 switch(type)
519 case FD_TYPE_SERIAL:
521 /* GetCommTimeouts */
522 SERIAL_TIMEOUTS st;
523 IO_STATUS_BLOCK io;
525 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
526 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
527 if (status) break;
529 if (is_read)
531 if (st.ReadIntervalTimeout)
532 timeouts->interval = st.ReadIntervalTimeout;
534 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
536 timeouts->total = st.ReadTotalTimeoutConstant;
537 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
538 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
540 else if (st.ReadIntervalTimeout == MAXDWORD)
541 timeouts->interval = timeouts->total = 0;
543 else /* write */
545 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
547 timeouts->total = st.WriteTotalTimeoutConstant;
548 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
549 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
553 break;
554 case FD_TYPE_MAILSLOT:
555 if (is_read)
557 timeouts->interval = 0; /* return as soon as we got something */
558 SERVER_START_REQ( set_mailslot_info )
560 req->handle = wine_server_obj_handle( handle );
561 req->flags = 0;
562 if (!(status = wine_server_call( req )) &&
563 reply->read_timeout != TIMEOUT_INFINITE)
564 timeouts->total = reply->read_timeout / -10000;
566 SERVER_END_REQ;
568 break;
569 case FD_TYPE_SOCKET:
570 case FD_TYPE_PIPE:
571 case FD_TYPE_CHAR:
572 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
573 break;
574 default:
575 break;
577 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
578 return STATUS_SUCCESS;
582 /* retrieve the timeout for the next wait, in milliseconds */
583 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
585 int ret = -1;
587 if (timeouts->total != -1)
589 ret = timeouts->end_time - NtGetTickCount();
590 if (ret < 0) ret = 0;
592 if (already && timeouts->interval != -1)
594 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
596 return ret;
600 /* retrieve the avail_mode flag for async reads */
601 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
603 NTSTATUS status = STATUS_SUCCESS;
605 switch(type)
607 case FD_TYPE_SERIAL:
609 /* GetCommTimeouts */
610 SERIAL_TIMEOUTS st;
611 IO_STATUS_BLOCK io;
613 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
614 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
615 if (status) break;
616 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
617 !st.ReadTotalTimeoutConstant &&
618 st.ReadIntervalTimeout == MAXDWORD);
620 break;
621 case FD_TYPE_MAILSLOT:
622 case FD_TYPE_SOCKET:
623 case FD_TYPE_PIPE:
624 case FD_TYPE_CHAR:
625 *avail_mode = TRUE;
626 break;
627 default:
628 *avail_mode = FALSE;
629 break;
631 return status;
635 /******************************************************************************
636 * NtReadFile [NTDLL.@]
637 * ZwReadFile [NTDLL.@]
639 * Read from an open file handle.
641 * PARAMS
642 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
643 * Event [I] Event to signal upon completion (or NULL)
644 * ApcRoutine [I] Callback to call upon completion (or NULL)
645 * ApcContext [I] Context for ApcRoutine (or NULL)
646 * IoStatusBlock [O] Receives information about the operation on return
647 * Buffer [O] Destination for the data read
648 * Length [I] Size of Buffer
649 * ByteOffset [O] Destination for the new file pointer position (or NULL)
650 * Key [O] Function unknown (may be NULL)
652 * RETURNS
653 * Success: 0. IoStatusBlock is updated, and the Information member contains
654 * The number of bytes read.
655 * Failure: An NTSTATUS error code describing the error.
657 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
658 PIO_APC_ROUTINE apc, void* apc_user,
659 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
660 PLARGE_INTEGER offset, PULONG key)
662 int result, unix_handle, needs_close;
663 unsigned int options;
664 struct io_timeouts timeouts;
665 NTSTATUS status;
666 ULONG total = 0;
667 enum server_fd_type type;
668 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
669 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
671 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
672 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
674 if (!io_status) return STATUS_ACCESS_VIOLATION;
676 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
677 &needs_close, &type, &options );
678 if (status) return status;
680 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
682 if (!virtual_check_buffer_for_write( buffer, length ))
684 status = STATUS_ACCESS_VIOLATION;
685 goto done;
688 if (type == FD_TYPE_FILE)
690 if (async_read && (!offset || offset->QuadPart < 0))
692 status = STATUS_INVALID_PARAMETER;
693 goto done;
696 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
698 /* async I/O doesn't make sense on regular files */
699 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
701 if (errno != EINTR)
703 status = FILE_GetNtStatus();
704 goto done;
707 if (!async_read)
708 /* update file pointer position */
709 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
711 total = result;
712 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
713 goto done;
716 else if (type == FD_TYPE_SERIAL)
718 if (async_read && (!offset || offset->QuadPart < 0))
720 status = STATUS_INVALID_PARAMETER;
721 goto done;
725 for (;;)
727 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
729 total += result;
730 if (!result || total == length)
732 if (total)
734 status = STATUS_SUCCESS;
735 goto done;
737 switch (type)
739 case FD_TYPE_FILE:
740 case FD_TYPE_CHAR:
741 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
742 goto done;
743 case FD_TYPE_SERIAL:
744 break;
745 default:
746 status = STATUS_PIPE_BROKEN;
747 goto done;
750 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
752 else if (errno != EAGAIN)
754 if (errno == EINTR) continue;
755 if (!total) status = FILE_GetNtStatus();
756 goto done;
759 if (async_read)
761 struct async_fileio_read *fileio;
762 BOOL avail_mode;
764 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
765 goto err;
766 if (total && avail_mode)
768 status = STATUS_SUCCESS;
769 goto done;
772 fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), hFile, apc, apc_user );
773 if (!fileio)
775 status = STATUS_NO_MEMORY;
776 goto err;
778 fileio->already = total;
779 fileio->count = length;
780 fileio->buffer = buffer;
781 fileio->avail_mode = avail_mode;
783 SERVER_START_REQ( register_async )
785 req->type = ASYNC_TYPE_READ;
786 req->count = length;
787 req->async.handle = wine_server_obj_handle( hFile );
788 req->async.event = wine_server_obj_handle( hEvent );
789 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
790 req->async.iosb = wine_server_client_ptr( io_status );
791 req->async.arg = wine_server_client_ptr( fileio );
792 req->async.cvalue = cvalue;
793 status = wine_server_call( req );
795 SERVER_END_REQ;
797 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
798 goto err;
800 else /* synchronous read, wait for the fd to become ready */
802 struct pollfd pfd;
803 int ret, timeout;
805 if (!timeout_init_done)
807 timeout_init_done = TRUE;
808 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
809 goto err;
810 if (hEvent) NtResetEvent( hEvent, NULL );
812 timeout = get_next_io_timeout( &timeouts, total );
814 pfd.fd = unix_handle;
815 pfd.events = POLLIN;
817 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
819 if (total) /* return with what we got so far */
820 status = STATUS_SUCCESS;
821 else
822 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
823 goto done;
825 if (ret == -1 && errno != EINTR)
827 status = FILE_GetNtStatus();
828 goto done;
830 /* will now restart the read */
834 done:
835 send_completion = cvalue != 0;
837 err:
838 if (needs_close) close( unix_handle );
839 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
841 io_status->u.Status = status;
842 io_status->Information = total;
843 TRACE("= SUCCESS (%u)\n", total);
844 if (hEvent) NtSetEvent( hEvent, NULL );
845 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
846 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
848 else
850 TRACE("= 0x%08x\n", status);
851 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
854 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
856 return status;
860 /******************************************************************************
861 * NtReadFileScatter [NTDLL.@]
862 * ZwReadFileScatter [NTDLL.@]
864 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
865 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
866 ULONG length, PLARGE_INTEGER offset, PULONG key )
868 int result, unix_handle, needs_close;
869 unsigned int options;
870 NTSTATUS status;
871 ULONG pos = 0, total = 0;
872 enum server_fd_type type;
873 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
874 BOOL send_completion = FALSE;
876 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
877 file, event, apc, apc_user, io_status, segments, length, offset, key);
879 if (length % page_size) return STATUS_INVALID_PARAMETER;
880 if (!io_status) return STATUS_ACCESS_VIOLATION;
882 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
883 &needs_close, &type, &options );
884 if (status) return status;
886 if ((type != FD_TYPE_FILE) ||
887 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
888 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
890 status = STATUS_INVALID_PARAMETER;
891 goto error;
894 while (length)
896 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
897 result = pread( unix_handle, (char *)segments->Buffer + pos,
898 page_size - pos, offset->QuadPart + total );
899 else
900 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
902 if (result == -1)
904 if (errno == EINTR) continue;
905 status = FILE_GetNtStatus();
906 break;
908 if (!result)
910 status = STATUS_END_OF_FILE;
911 break;
913 total += result;
914 length -= result;
915 if ((pos += result) == page_size)
917 pos = 0;
918 segments++;
922 send_completion = cvalue != 0;
924 error:
925 if (needs_close) close( unix_handle );
926 if (status == STATUS_SUCCESS)
928 io_status->u.Status = status;
929 io_status->Information = total;
930 TRACE("= SUCCESS (%u)\n", total);
931 if (event) NtSetEvent( event, NULL );
932 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
933 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
935 else
937 TRACE("= 0x%08x\n", status);
938 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
941 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
943 return status;
947 /***********************************************************************
948 * FILE_AsyncWriteService (INTERNAL)
950 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb,
951 NTSTATUS status, void **apc, void **arg )
953 struct async_fileio_write *fileio = user;
954 int result, fd, needs_close;
955 enum server_fd_type type;
957 switch (status)
959 case STATUS_ALERTED:
960 /* write some data (non-blocking) */
961 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
962 &needs_close, &type, NULL )))
963 break;
965 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
966 result = send( fd, fileio->buffer, 0, 0 );
967 else
968 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
970 if (needs_close) close( fd );
972 if (result < 0)
974 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
975 else status = FILE_GetNtStatus();
977 else
979 fileio->already += result;
980 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
982 break;
984 case STATUS_TIMEOUT:
985 case STATUS_IO_TIMEOUT:
986 if (fileio->already) status = STATUS_SUCCESS;
987 break;
989 if (status != STATUS_PENDING)
991 iosb->u.Status = status;
992 iosb->Information = fileio->already;
993 *apc = fileio->io.apc;
994 *arg = fileio->io.apc_arg;
995 release_fileio( &fileio->io );
997 return status;
1000 static NTSTATUS set_pending_write( HANDLE device )
1002 NTSTATUS status;
1004 SERVER_START_REQ( set_serial_info )
1006 req->handle = wine_server_obj_handle( device );
1007 req->flags = SERIALINFO_PENDING_WRITE;
1008 status = wine_server_call( req );
1010 SERVER_END_REQ;
1011 return status;
1014 /******************************************************************************
1015 * NtWriteFile [NTDLL.@]
1016 * ZwWriteFile [NTDLL.@]
1018 * Write to an open file handle.
1020 * PARAMS
1021 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1022 * Event [I] Event to signal upon completion (or NULL)
1023 * ApcRoutine [I] Callback to call upon completion (or NULL)
1024 * ApcContext [I] Context for ApcRoutine (or NULL)
1025 * IoStatusBlock [O] Receives information about the operation on return
1026 * Buffer [I] Source for the data to write
1027 * Length [I] Size of Buffer
1028 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1029 * Key [O] Function unknown (may be NULL)
1031 * RETURNS
1032 * Success: 0. IoStatusBlock is updated, and the Information member contains
1033 * The number of bytes written.
1034 * Failure: An NTSTATUS error code describing the error.
1036 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1037 PIO_APC_ROUTINE apc, void* apc_user,
1038 PIO_STATUS_BLOCK io_status,
1039 const void* buffer, ULONG length,
1040 PLARGE_INTEGER offset, PULONG key)
1042 int result, unix_handle, needs_close;
1043 unsigned int options;
1044 struct io_timeouts timeouts;
1045 NTSTATUS status;
1046 ULONG total = 0;
1047 enum server_fd_type type;
1048 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1049 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1050 LARGE_INTEGER offset_eof;
1052 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1053 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1055 if (!io_status) return STATUS_ACCESS_VIOLATION;
1057 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1058 &needs_close, &type, &options );
1059 if (status == STATUS_ACCESS_DENIED)
1061 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1062 &needs_close, &type, &options );
1063 append_write = TRUE;
1065 if (status) return status;
1067 if (!virtual_check_buffer_for_read( buffer, length ))
1069 status = STATUS_INVALID_USER_BUFFER;
1070 goto done;
1073 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1075 if (type == FD_TYPE_FILE)
1077 if (async_write &&
1078 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1080 status = STATUS_INVALID_PARAMETER;
1081 goto done;
1084 if (append_write)
1086 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1087 offset = &offset_eof;
1090 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1092 off_t off = offset->QuadPart;
1094 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1096 struct stat st;
1098 if (fstat( unix_handle, &st ) == -1)
1100 status = FILE_GetNtStatus();
1101 goto done;
1103 off = st.st_size;
1105 else if (offset->QuadPart < 0)
1107 status = STATUS_INVALID_PARAMETER;
1108 goto done;
1111 /* async I/O doesn't make sense on regular files */
1112 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1114 if (errno != EINTR)
1116 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1117 else status = FILE_GetNtStatus();
1118 goto done;
1122 if (!async_write)
1123 /* update file pointer position */
1124 lseek( unix_handle, off + result, SEEK_SET );
1126 total = result;
1127 status = STATUS_SUCCESS;
1128 goto done;
1131 else if (type == FD_TYPE_SERIAL)
1133 if (async_write &&
1134 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1136 status = STATUS_INVALID_PARAMETER;
1137 goto done;
1141 for (;;)
1143 /* zero-length writes on sockets may not work with plain write(2) */
1144 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1145 result = send( unix_handle, buffer, 0, 0 );
1146 else
1147 result = write( unix_handle, (const char *)buffer + total, length - total );
1149 if (result >= 0)
1151 total += result;
1152 if (total == length)
1154 status = STATUS_SUCCESS;
1155 goto done;
1157 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1159 else if (errno != EAGAIN)
1161 if (errno == EINTR) continue;
1162 if (!total)
1164 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1165 else status = FILE_GetNtStatus();
1167 goto done;
1170 if (async_write)
1172 struct async_fileio_write *fileio;
1174 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), hFile, apc, apc_user );
1175 if (!fileio)
1177 status = STATUS_NO_MEMORY;
1178 goto err;
1180 fileio->already = total;
1181 fileio->count = length;
1182 fileio->buffer = buffer;
1184 SERVER_START_REQ( register_async )
1186 req->type = ASYNC_TYPE_WRITE;
1187 req->count = length;
1188 req->async.handle = wine_server_obj_handle( hFile );
1189 req->async.event = wine_server_obj_handle( hEvent );
1190 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1191 req->async.iosb = wine_server_client_ptr( io_status );
1192 req->async.arg = wine_server_client_ptr( fileio );
1193 req->async.cvalue = cvalue;
1194 status = wine_server_call( req );
1196 SERVER_END_REQ;
1198 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1199 goto err;
1201 else /* synchronous write, wait for the fd to become ready */
1203 struct pollfd pfd;
1204 int ret, timeout;
1206 if (!timeout_init_done)
1208 timeout_init_done = TRUE;
1209 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1210 goto err;
1211 if (hEvent) NtResetEvent( hEvent, NULL );
1213 timeout = get_next_io_timeout( &timeouts, total );
1215 pfd.fd = unix_handle;
1216 pfd.events = POLLOUT;
1218 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1220 /* return with what we got so far */
1221 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1222 goto done;
1224 if (ret == -1 && errno != EINTR)
1226 status = FILE_GetNtStatus();
1227 goto done;
1229 /* will now restart the write */
1233 done:
1234 send_completion = cvalue != 0;
1236 err:
1237 if (needs_close) close( unix_handle );
1239 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1240 set_pending_write( hFile );
1242 if (status == STATUS_SUCCESS)
1244 io_status->u.Status = status;
1245 io_status->Information = total;
1246 TRACE("= SUCCESS (%u)\n", total);
1247 if (hEvent) NtSetEvent( hEvent, NULL );
1248 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1249 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1251 else
1253 TRACE("= 0x%08x\n", status);
1254 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1257 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1259 return status;
1263 /******************************************************************************
1264 * NtWriteFileGather [NTDLL.@]
1265 * ZwWriteFileGather [NTDLL.@]
1267 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1268 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1269 ULONG length, PLARGE_INTEGER offset, PULONG key )
1271 int result, unix_handle, needs_close;
1272 unsigned int options;
1273 NTSTATUS status;
1274 ULONG pos = 0, total = 0;
1275 enum server_fd_type type;
1276 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1277 BOOL send_completion = FALSE;
1279 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1280 file, event, apc, apc_user, io_status, segments, length, offset, key);
1282 if (length % page_size) return STATUS_INVALID_PARAMETER;
1283 if (!io_status) return STATUS_ACCESS_VIOLATION;
1285 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1286 &needs_close, &type, &options );
1287 if (status) return status;
1289 if ((type != FD_TYPE_FILE) ||
1290 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1291 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1293 status = STATUS_INVALID_PARAMETER;
1294 goto error;
1297 while (length)
1299 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1300 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1301 page_size - pos, offset->QuadPart + total );
1302 else
1303 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1305 if (result == -1)
1307 if (errno == EINTR) continue;
1308 if (errno == EFAULT)
1310 status = STATUS_INVALID_USER_BUFFER;
1311 goto error;
1313 status = FILE_GetNtStatus();
1314 break;
1316 if (!result)
1318 status = STATUS_DISK_FULL;
1319 break;
1321 total += result;
1322 length -= result;
1323 if ((pos += result) == page_size)
1325 pos = 0;
1326 segments++;
1330 send_completion = cvalue != 0;
1332 error:
1333 if (needs_close) close( unix_handle );
1334 if (status == STATUS_SUCCESS)
1336 io_status->u.Status = status;
1337 io_status->Information = total;
1338 TRACE("= SUCCESS (%u)\n", total);
1339 if (event) NtSetEvent( event, NULL );
1340 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1341 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1343 else
1345 TRACE("= 0x%08x\n", status);
1346 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1349 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1351 return status;
1355 struct async_ioctl
1357 struct async_fileio io;
1358 HANDLE event; /* async event */
1359 void *buffer; /* buffer for output */
1360 ULONG size; /* size of buffer */
1363 /* callback for ioctl async I/O completion */
1364 static NTSTATUS ioctl_completion( void *user, IO_STATUS_BLOCK *io,
1365 NTSTATUS status, void **apc, void **arg )
1367 struct async_ioctl *async = user;
1369 if (status == STATUS_ALERTED)
1371 SERVER_START_REQ( get_ioctl_result )
1373 req->handle = wine_server_obj_handle( async->io.handle );
1374 req->user_arg = wine_server_client_ptr( async );
1375 wine_server_set_reply( req, async->buffer, async->size );
1376 status = wine_server_call( req );
1377 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1379 SERVER_END_REQ;
1381 if (status != STATUS_PENDING)
1383 io->u.Status = status;
1384 *apc = async->io.apc;
1385 *arg = async->io.apc_arg;
1386 release_fileio( &async->io );
1388 return status;
1391 /* do an ioctl call through the server */
1392 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1393 PIO_APC_ROUTINE apc, PVOID apc_context,
1394 IO_STATUS_BLOCK *io, ULONG code,
1395 const void *in_buffer, ULONG in_size,
1396 PVOID out_buffer, ULONG out_size )
1398 struct async_ioctl *async;
1399 NTSTATUS status;
1400 HANDLE wait_handle;
1401 ULONG options;
1402 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1404 if (!(async = (struct async_ioctl *)alloc_fileio( sizeof(*async), handle, apc, apc_context )))
1405 return STATUS_NO_MEMORY;
1406 async->event = event;
1407 async->buffer = out_buffer;
1408 async->size = out_size;
1410 SERVER_START_REQ( ioctl )
1412 req->code = code;
1413 req->blocking = !apc && !event && !cvalue;
1414 req->async.handle = wine_server_obj_handle( handle );
1415 req->async.callback = wine_server_client_ptr( ioctl_completion );
1416 req->async.iosb = wine_server_client_ptr( io );
1417 req->async.arg = wine_server_client_ptr( async );
1418 req->async.event = wine_server_obj_handle( event );
1419 req->async.cvalue = cvalue;
1420 wine_server_add_data( req, in_buffer, in_size );
1421 wine_server_set_reply( req, out_buffer, out_size );
1422 status = wine_server_call( req );
1423 wait_handle = wine_server_ptr_handle( reply->wait );
1424 options = reply->options;
1425 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1427 SERVER_END_REQ;
1429 if (status == STATUS_NOT_SUPPORTED)
1430 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1431 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1433 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1435 if (wait_handle)
1437 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1438 status = io->u.Status;
1439 NtClose( wait_handle );
1442 return status;
1445 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1446 * server */
1447 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1448 ULONG in_size)
1450 #ifdef VALGRIND_MAKE_MEM_DEFINED
1451 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1452 do { \
1453 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1454 if ((size) >= FIELD_OFFSET(t, f2)) \
1455 VALGRIND_MAKE_MEM_DEFINED( \
1456 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1457 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1458 } while (0)
1460 switch (code)
1462 case FSCTL_PIPE_WAIT:
1463 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1464 break;
1466 #endif
1470 /**************************************************************************
1471 * NtDeviceIoControlFile [NTDLL.@]
1472 * ZwDeviceIoControlFile [NTDLL.@]
1474 * Perform an I/O control operation on an open file handle.
1476 * PARAMS
1477 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1478 * event [I] Event to signal upon completion (or NULL)
1479 * apc [I] Callback to call upon completion (or NULL)
1480 * apc_context [I] Context for ApcRoutine (or NULL)
1481 * io [O] Receives information about the operation on return
1482 * code [I] Control code for the operation to perform
1483 * in_buffer [I] Source for any input data required (or NULL)
1484 * in_size [I] Size of InputBuffer
1485 * out_buffer [O] Source for any output data returned (or NULL)
1486 * out_size [I] Size of OutputBuffer
1488 * RETURNS
1489 * Success: 0. IoStatusBlock is updated.
1490 * Failure: An NTSTATUS error code describing the error.
1492 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1493 PIO_APC_ROUTINE apc, PVOID apc_context,
1494 PIO_STATUS_BLOCK io, ULONG code,
1495 PVOID in_buffer, ULONG in_size,
1496 PVOID out_buffer, ULONG out_size)
1498 ULONG device = (code >> 16);
1499 NTSTATUS status = STATUS_NOT_SUPPORTED;
1501 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1502 handle, event, apc, apc_context, io, code,
1503 in_buffer, in_size, out_buffer, out_size);
1505 switch(device)
1507 case FILE_DEVICE_DISK:
1508 case FILE_DEVICE_CD_ROM:
1509 case FILE_DEVICE_DVD:
1510 case FILE_DEVICE_CONTROLLER:
1511 case FILE_DEVICE_MASS_STORAGE:
1512 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1513 in_buffer, in_size, out_buffer, out_size);
1514 break;
1515 case FILE_DEVICE_SERIAL_PORT:
1516 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1517 in_buffer, in_size, out_buffer, out_size);
1518 break;
1519 case FILE_DEVICE_TAPE:
1520 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1521 in_buffer, in_size, out_buffer, out_size);
1522 break;
1525 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1526 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1527 in_buffer, in_size, out_buffer, out_size );
1529 if (status != STATUS_PENDING) io->u.Status = status;
1530 return status;
1534 /**************************************************************************
1535 * NtFsControlFile [NTDLL.@]
1536 * ZwFsControlFile [NTDLL.@]
1538 * Perform a file system control operation on an open file handle.
1540 * PARAMS
1541 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1542 * event [I] Event to signal upon completion (or NULL)
1543 * apc [I] Callback to call upon completion (or NULL)
1544 * apc_context [I] Context for ApcRoutine (or NULL)
1545 * io [O] Receives information about the operation on return
1546 * code [I] Control code for the operation to perform
1547 * in_buffer [I] Source for any input data required (or NULL)
1548 * in_size [I] Size of InputBuffer
1549 * out_buffer [O] Source for any output data returned (or NULL)
1550 * out_size [I] Size of OutputBuffer
1552 * RETURNS
1553 * Success: 0. IoStatusBlock is updated.
1554 * Failure: An NTSTATUS error code describing the error.
1556 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1557 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1558 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1560 NTSTATUS status;
1562 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1563 handle, event, apc, apc_context, io, code,
1564 in_buffer, in_size, out_buffer, out_size);
1566 if (!io) return STATUS_INVALID_PARAMETER;
1568 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1570 switch(code)
1572 case FSCTL_DISMOUNT_VOLUME:
1573 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1574 in_buffer, in_size, out_buffer, out_size );
1575 if (!status) status = DIR_unmount_device( handle );
1576 break;
1578 case FSCTL_PIPE_PEEK:
1580 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1581 int avail = 0, fd, needs_close;
1583 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1585 status = STATUS_INFO_LENGTH_MISMATCH;
1586 break;
1589 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1590 break;
1592 #ifdef FIONREAD
1593 if (ioctl( fd, FIONREAD, &avail ) != 0)
1595 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1596 if (needs_close) close( fd );
1597 status = FILE_GetNtStatus();
1598 break;
1600 #endif
1601 if (!avail) /* check for closed pipe */
1603 struct pollfd pollfd;
1604 int ret;
1606 pollfd.fd = fd;
1607 pollfd.events = POLLIN;
1608 pollfd.revents = 0;
1609 ret = poll( &pollfd, 1, 0 );
1610 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1612 if (needs_close) close( fd );
1613 status = STATUS_PIPE_BROKEN;
1614 break;
1617 buffer->NamedPipeState = 0; /* FIXME */
1618 buffer->ReadDataAvailable = avail;
1619 buffer->NumberOfMessages = 0; /* FIXME */
1620 buffer->MessageLength = 0; /* FIXME */
1621 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1622 status = STATUS_SUCCESS;
1623 if (avail)
1625 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1626 if (data_size)
1628 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1629 if (res >= 0) io->Information += res;
1632 if (needs_close) close( fd );
1634 break;
1636 case FSCTL_PIPE_DISCONNECT:
1637 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1638 in_buffer, in_size, out_buffer, out_size );
1639 if (!status)
1641 int fd = server_remove_fd_from_cache( handle );
1642 if (fd != -1) close( fd );
1644 break;
1646 case FSCTL_PIPE_IMPERSONATE:
1647 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1648 status = RtlImpersonateSelf( SecurityImpersonation );
1649 break;
1651 case FSCTL_IS_VOLUME_MOUNTED:
1652 case FSCTL_LOCK_VOLUME:
1653 case FSCTL_UNLOCK_VOLUME:
1654 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1655 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1656 status = STATUS_SUCCESS;
1657 break;
1659 case FSCTL_GET_RETRIEVAL_POINTERS:
1661 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1663 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1665 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1667 buffer->ExtentCount = 1;
1668 buffer->StartingVcn.QuadPart = 1;
1669 buffer->Extents[0].NextVcn.QuadPart = 0;
1670 buffer->Extents[0].Lcn.QuadPart = 0;
1671 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1672 status = STATUS_SUCCESS;
1674 else
1676 io->Information = 0;
1677 status = STATUS_BUFFER_TOO_SMALL;
1679 break;
1681 case FSCTL_PIPE_LISTEN:
1682 case FSCTL_PIPE_WAIT:
1683 default:
1684 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1685 in_buffer, in_size, out_buffer, out_size );
1686 break;
1689 if (status != STATUS_PENDING) io->u.Status = status;
1690 return status;
1694 struct read_changes_fileio
1696 struct async_fileio io;
1697 void *buffer;
1698 ULONG buffer_size;
1699 ULONG data_size;
1700 char data[1];
1703 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb,
1704 NTSTATUS status, void **apc, void **arg )
1706 struct read_changes_fileio *fileio = user;
1707 NTSTATUS ret;
1708 int size;
1710 SERVER_START_REQ( read_change )
1712 req->handle = wine_server_obj_handle( fileio->io.handle );
1713 wine_server_set_reply( req, fileio->data, fileio->data_size );
1714 ret = wine_server_call( req );
1715 size = wine_server_reply_size( reply );
1717 SERVER_END_REQ;
1719 if (ret == STATUS_SUCCESS && fileio->buffer)
1721 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1722 int i, left = fileio->buffer_size;
1723 DWORD *last_entry_offset = NULL;
1724 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1726 while (size && left >= sizeof(*pfni))
1728 /* convert to an NT style path */
1729 for (i=0; i<event->len; i++)
1730 if (event->name[i] == '/') event->name[i] = '\\';
1732 pfni->Action = event->action;
1733 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1734 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1735 last_entry_offset = &pfni->NextEntryOffset;
1737 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1739 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1740 pfni->FileNameLength *= sizeof(WCHAR);
1741 pfni->NextEntryOffset = i;
1742 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1743 left -= i;
1745 i = (offsetof(struct filesystem_event, name[event->len])
1746 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1747 event = (struct filesystem_event*)((char*)event + i);
1748 size -= i;
1751 if (size)
1753 ret = STATUS_NOTIFY_ENUM_DIR;
1754 size = 0;
1756 else
1758 *last_entry_offset = 0;
1759 size = fileio->buffer_size - left;
1762 else
1764 ret = STATUS_NOTIFY_ENUM_DIR;
1765 size = 0;
1768 iosb->u.Status = ret;
1769 iosb->Information = size;
1770 *apc = fileio->io.apc;
1771 *arg = fileio->io.apc_arg;
1772 release_fileio( &fileio->io );
1773 return ret;
1776 #define FILE_NOTIFY_ALL ( \
1777 FILE_NOTIFY_CHANGE_FILE_NAME | \
1778 FILE_NOTIFY_CHANGE_DIR_NAME | \
1779 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1780 FILE_NOTIFY_CHANGE_SIZE | \
1781 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1782 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1783 FILE_NOTIFY_CHANGE_CREATION | \
1784 FILE_NOTIFY_CHANGE_SECURITY )
1786 /******************************************************************************
1787 * NtNotifyChangeDirectoryFile [NTDLL.@]
1789 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1790 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1791 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1793 struct read_changes_fileio *fileio;
1794 NTSTATUS status;
1795 ULONG size = max( 4096, buffer_size );
1796 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1798 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1799 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1801 if (!iosb) return STATUS_ACCESS_VIOLATION;
1802 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1804 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1805 handle, apc, apc_context );
1806 if (!fileio) return STATUS_NO_MEMORY;
1808 fileio->buffer = buffer;
1809 fileio->buffer_size = buffer_size;
1810 fileio->data_size = size;
1812 SERVER_START_REQ( read_directory_changes )
1814 req->filter = filter;
1815 req->want_data = (buffer != NULL);
1816 req->subtree = subtree;
1817 req->async.handle = wine_server_obj_handle( handle );
1818 req->async.callback = wine_server_client_ptr( read_changes_apc );
1819 req->async.iosb = wine_server_client_ptr( iosb );
1820 req->async.arg = wine_server_client_ptr( fileio );
1821 req->async.event = wine_server_obj_handle( event );
1822 req->async.cvalue = cvalue;
1823 status = wine_server_call( req );
1825 SERVER_END_REQ;
1827 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1828 return status;
1831 /******************************************************************************
1832 * NtSetVolumeInformationFile [NTDLL.@]
1833 * ZwSetVolumeInformationFile [NTDLL.@]
1835 * Set volume information for an open file handle.
1837 * PARAMS
1838 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1839 * IoStatusBlock [O] Receives information about the operation on return
1840 * FsInformation [I] Source for volume information
1841 * Length [I] Size of FsInformation
1842 * FsInformationClass [I] Type of volume information to set
1844 * RETURNS
1845 * Success: 0. IoStatusBlock is updated.
1846 * Failure: An NTSTATUS error code describing the error.
1848 NTSTATUS WINAPI NtSetVolumeInformationFile(
1849 IN HANDLE FileHandle,
1850 PIO_STATUS_BLOCK IoStatusBlock,
1851 PVOID FsInformation,
1852 ULONG Length,
1853 FS_INFORMATION_CLASS FsInformationClass)
1855 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1856 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1857 return 0;
1860 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1861 static int futimens( int fd, const struct timespec spec[2] )
1863 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1865 #define HAVE_FUTIMENS
1866 #endif /* __ANDROID__ */
1868 #ifndef UTIME_OMIT
1869 #define UTIME_OMIT ((1 << 30) - 2)
1870 #endif
1872 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1874 NTSTATUS status = STATUS_SUCCESS;
1876 #ifdef HAVE_FUTIMENS
1877 struct timespec tv[2];
1879 tv[0].tv_sec = tv[1].tv_sec = 0;
1880 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1881 if (atime->QuadPart)
1883 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1884 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1886 if (mtime->QuadPart)
1888 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1889 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1891 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1893 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1894 struct timeval tv[2];
1895 struct stat st;
1897 if (!atime->QuadPart || !mtime->QuadPart)
1900 tv[0].tv_sec = tv[0].tv_usec = 0;
1901 tv[1].tv_sec = tv[1].tv_usec = 0;
1902 if (!fstat( fd, &st ))
1904 tv[0].tv_sec = st.st_atime;
1905 tv[1].tv_sec = st.st_mtime;
1906 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1907 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1908 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1909 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1910 #endif
1911 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1912 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1913 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1914 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1915 #endif
1918 if (atime->QuadPart)
1920 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1921 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1923 if (mtime->QuadPart)
1925 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1926 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1928 #ifdef HAVE_FUTIMES
1929 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1930 #elif defined(HAVE_FUTIMESAT)
1931 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1932 #endif
1934 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1935 FIXME( "setting file times not supported\n" );
1936 status = STATUS_NOT_IMPLEMENTED;
1937 #endif
1938 return status;
1941 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1942 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1944 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1945 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1946 RtlSecondsSince1970ToTime( st->st_atime, atime );
1947 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1948 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1949 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1950 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
1951 #endif
1952 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1953 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
1954 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1955 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
1956 #endif
1957 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1958 atime->QuadPart += st->st_atim.tv_nsec / 100;
1959 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1960 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
1961 #endif
1962 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1963 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
1964 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
1965 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
1966 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1967 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
1968 #endif
1969 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
1970 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
1971 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
1972 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
1973 #endif
1974 #else
1975 *creation = *mtime;
1976 #endif
1979 /* fill in the file information that depends on the stat and attribute info */
1980 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
1981 FILE_INFORMATION_CLASS class )
1983 switch (class)
1985 case FileBasicInformation:
1987 FILE_BASIC_INFORMATION *info = ptr;
1989 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1990 &info->LastAccessTime, &info->CreationTime );
1991 info->FileAttributes = attr;
1993 break;
1994 case FileStandardInformation:
1996 FILE_STANDARD_INFORMATION *info = ptr;
1998 if ((info->Directory = S_ISDIR(st->st_mode)))
2000 info->AllocationSize.QuadPart = 0;
2001 info->EndOfFile.QuadPart = 0;
2002 info->NumberOfLinks = 1;
2004 else
2006 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2007 info->EndOfFile.QuadPart = st->st_size;
2008 info->NumberOfLinks = st->st_nlink;
2011 break;
2012 case FileInternalInformation:
2014 FILE_INTERNAL_INFORMATION *info = ptr;
2015 info->IndexNumber.QuadPart = st->st_ino;
2017 break;
2018 case FileEndOfFileInformation:
2020 FILE_END_OF_FILE_INFORMATION *info = ptr;
2021 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2023 break;
2024 case FileAllInformation:
2026 FILE_ALL_INFORMATION *info = ptr;
2027 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2028 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2029 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2031 break;
2032 /* all directory structures start with the FileDirectoryInformation layout */
2033 case FileBothDirectoryInformation:
2034 case FileFullDirectoryInformation:
2035 case FileDirectoryInformation:
2037 FILE_DIRECTORY_INFORMATION *info = ptr;
2039 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2040 &info->LastAccessTime, &info->CreationTime );
2041 if (S_ISDIR(st->st_mode))
2043 info->AllocationSize.QuadPart = 0;
2044 info->EndOfFile.QuadPart = 0;
2046 else
2048 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2049 info->EndOfFile.QuadPart = st->st_size;
2051 info->FileAttributes = attr;
2053 break;
2054 case FileIdFullDirectoryInformation:
2056 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2057 info->FileId.QuadPart = st->st_ino;
2058 fill_file_info( st, attr, info, FileDirectoryInformation );
2060 break;
2061 case FileIdBothDirectoryInformation:
2063 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2064 info->FileId.QuadPart = st->st_ino;
2065 fill_file_info( st, attr, info, FileDirectoryInformation );
2067 break;
2069 default:
2070 return STATUS_INVALID_INFO_CLASS;
2072 return STATUS_SUCCESS;
2075 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2077 data_size_t size = 1024;
2078 NTSTATUS ret;
2079 char *name;
2081 for (;;)
2083 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2084 if (!name) return STATUS_NO_MEMORY;
2085 unix_name->MaximumLength = size + 1;
2087 SERVER_START_REQ( get_handle_unix_name )
2089 req->handle = wine_server_obj_handle( handle );
2090 wine_server_set_reply( req, name, size );
2091 ret = wine_server_call( req );
2092 size = reply->name_len;
2094 SERVER_END_REQ;
2096 if (!ret)
2098 name[size] = 0;
2099 unix_name->Buffer = name;
2100 unix_name->Length = size;
2101 break;
2103 RtlFreeHeap( GetProcessHeap(), 0, name );
2104 if (ret != STATUS_BUFFER_OVERFLOW) break;
2106 return ret;
2109 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2111 UNICODE_STRING nt_name;
2112 NTSTATUS status;
2114 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2116 const WCHAR *ptr = nt_name.Buffer;
2117 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2119 /* Skip the volume mount point. */
2120 while (ptr != end && *ptr == '\\') ++ptr;
2121 while (ptr != end && *ptr != '\\') ++ptr;
2122 while (ptr != end && *ptr == '\\') ++ptr;
2123 while (ptr != end && *ptr != '\\') ++ptr;
2125 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2126 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2127 else *name_len = info->FileNameLength;
2129 memcpy( info->FileName, ptr, *name_len );
2130 RtlFreeUnicodeString( &nt_name );
2133 return status;
2136 /******************************************************************************
2137 * NtQueryInformationFile [NTDLL.@]
2138 * ZwQueryInformationFile [NTDLL.@]
2140 * Get information about an open file handle.
2142 * PARAMS
2143 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2144 * io [O] Receives information about the operation on return
2145 * ptr [O] Destination for file information
2146 * len [I] Size of FileInformation
2147 * class [I] Type of file information to get
2149 * RETURNS
2150 * Success: 0. IoStatusBlock and FileInformation are updated.
2151 * Failure: An NTSTATUS error code describing the error.
2153 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2154 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2156 static const size_t info_sizes[] =
2159 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2160 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2161 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2162 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2163 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2164 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2165 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2166 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2167 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2168 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2169 0, /* FileLinkInformation */
2170 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2171 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2172 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2173 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2174 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2175 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2176 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2177 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2178 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2179 0, /* FileAlternateNameInformation */
2180 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2181 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2182 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2183 0, /* FilePipeRemoteInformation */
2184 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2185 0, /* FileMailslotSetInformation */
2186 0, /* FileCompressionInformation */
2187 0, /* FileObjectIdInformation */
2188 0, /* FileCompletionInformation */
2189 0, /* FileMoveClusterInformation */
2190 0, /* FileQuotaInformation */
2191 0, /* FileReparsePointInformation */
2192 sizeof(FILE_NETWORK_OPEN_INFORMATION), /* FileNetworkOpenInformation */
2193 0, /* FileAttributeTagInformation */
2194 0, /* FileTrackingInformation */
2195 0, /* FileIdBothDirectoryInformation */
2196 0, /* FileIdFullDirectoryInformation */
2197 0, /* FileValidDataLengthInformation */
2198 0, /* FileShortNameInformation */
2202 0, /* FileSfioReserveInformation */
2203 0, /* FileSfioVolumeInformation */
2204 0, /* FileHardLinkInformation */
2206 0, /* FileNormalizedNameInformation */
2208 0, /* FileIdGlobalTxDirectoryInformation */
2212 0 /* FileStandardLinkInformation */
2215 struct stat st;
2216 int fd, needs_close = FALSE;
2217 ULONG attr;
2219 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2221 io->Information = 0;
2223 if (class <= 0 || class >= FileMaximumInformation)
2224 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2225 if (!info_sizes[class])
2227 FIXME("Unsupported class (%d)\n", class);
2228 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2230 if (len < info_sizes[class])
2231 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2233 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2235 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2236 return io->u.Status;
2239 switch (class)
2241 case FileBasicInformation:
2242 if (fd_get_file_info( fd, &st, &attr ) == -1)
2243 io->u.Status = FILE_GetNtStatus();
2244 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2245 io->u.Status = STATUS_INVALID_INFO_CLASS;
2246 else
2247 fill_file_info( &st, attr, ptr, class );
2248 break;
2249 case FileStandardInformation:
2251 FILE_STANDARD_INFORMATION *info = ptr;
2253 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2254 else
2256 fill_file_info( &st, attr, info, class );
2257 info->DeletePending = FALSE; /* FIXME */
2260 break;
2261 case FilePositionInformation:
2263 FILE_POSITION_INFORMATION *info = ptr;
2264 off_t res = lseek( fd, 0, SEEK_CUR );
2265 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2266 else info->CurrentByteOffset.QuadPart = res;
2268 break;
2269 case FileInternalInformation:
2270 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2271 else fill_file_info( &st, attr, ptr, class );
2272 break;
2273 case FileEaInformation:
2275 FILE_EA_INFORMATION *info = ptr;
2276 info->EaSize = 0;
2278 break;
2279 case FileEndOfFileInformation:
2280 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2281 else fill_file_info( &st, attr, ptr, class );
2282 break;
2283 case FileAllInformation:
2285 FILE_ALL_INFORMATION *info = ptr;
2286 ANSI_STRING unix_name;
2288 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2289 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2290 io->u.Status = STATUS_INVALID_INFO_CLASS;
2291 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2293 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2295 fill_file_info( &st, attr, info, FileAllInformation );
2296 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2297 info->EaInformation.EaSize = 0;
2298 info->AccessInformation.AccessFlags = 0; /* FIXME */
2299 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2300 info->ModeInformation.Mode = 0; /* FIXME */
2301 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2303 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2304 RtlFreeAnsiString( &unix_name );
2305 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2308 break;
2309 case FileMailslotQueryInformation:
2311 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2313 SERVER_START_REQ( set_mailslot_info )
2315 req->handle = wine_server_obj_handle( hFile );
2316 req->flags = 0;
2317 io->u.Status = wine_server_call( req );
2318 if( io->u.Status == STATUS_SUCCESS )
2320 info->MaximumMessageSize = reply->max_msgsize;
2321 info->MailslotQuota = 0;
2322 info->NextMessageSize = 0;
2323 info->MessagesAvailable = 0;
2324 info->ReadTimeout.QuadPart = reply->read_timeout;
2327 SERVER_END_REQ;
2328 if (!io->u.Status)
2330 char *tmpbuf;
2331 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2332 if (size > 0x10000) size = 0x10000;
2333 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2335 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2337 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2338 info->MessagesAvailable = (res > 0);
2339 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2340 if (needs_close) close( fd );
2342 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2346 break;
2347 case FilePipeInformation:
2349 FILE_PIPE_INFORMATION* pi = ptr;
2351 SERVER_START_REQ( get_named_pipe_info )
2353 req->handle = wine_server_obj_handle( hFile );
2354 if (!(io->u.Status = wine_server_call( req )))
2356 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2357 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2358 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2359 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2362 SERVER_END_REQ;
2364 break;
2365 case FilePipeLocalInformation:
2367 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2369 SERVER_START_REQ( get_named_pipe_info )
2371 req->handle = wine_server_obj_handle( hFile );
2372 if (!(io->u.Status = wine_server_call( req )))
2374 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2375 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2376 switch (reply->sharing)
2378 case FILE_SHARE_READ:
2379 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2380 break;
2381 case FILE_SHARE_WRITE:
2382 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2383 break;
2384 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2385 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2386 break;
2388 pli->MaximumInstances = reply->maxinstances;
2389 pli->CurrentInstances = reply->instances;
2390 pli->InboundQuota = reply->insize;
2391 pli->ReadDataAvailable = 0; /* FIXME */
2392 pli->OutboundQuota = reply->outsize;
2393 pli->WriteQuotaAvailable = 0; /* FIXME */
2394 pli->NamedPipeState = 0; /* FIXME */
2395 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2396 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2399 SERVER_END_REQ;
2401 break;
2402 case FileNameInformation:
2404 FILE_NAME_INFORMATION *info = ptr;
2405 ANSI_STRING unix_name;
2407 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2409 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2410 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2411 RtlFreeAnsiString( &unix_name );
2412 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2415 break;
2416 case FileNetworkOpenInformation:
2418 FILE_NETWORK_OPEN_INFORMATION *info = ptr;
2419 ANSI_STRING unix_name;
2421 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2423 ULONG attributes;
2424 struct stat st;
2426 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2427 io->u.Status = FILE_GetNtStatus();
2428 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2429 io->u.Status = STATUS_INVALID_INFO_CLASS;
2430 else
2432 FILE_BASIC_INFORMATION basic;
2433 FILE_STANDARD_INFORMATION std;
2435 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2436 fill_file_info( &st, attributes, &std, FileStandardInformation );
2438 info->CreationTime = basic.CreationTime;
2439 info->LastAccessTime = basic.LastAccessTime;
2440 info->LastWriteTime = basic.LastWriteTime;
2441 info->ChangeTime = basic.ChangeTime;
2442 info->AllocationSize = std.AllocationSize;
2443 info->EndOfFile = std.EndOfFile;
2444 info->FileAttributes = basic.FileAttributes;
2446 RtlFreeAnsiString( &unix_name );
2449 break;
2450 default:
2451 FIXME("Unsupported class (%d)\n", class);
2452 io->u.Status = STATUS_NOT_IMPLEMENTED;
2453 break;
2455 if (needs_close) close( fd );
2456 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2457 return io->u.Status;
2460 /******************************************************************************
2461 * NtSetInformationFile [NTDLL.@]
2462 * ZwSetInformationFile [NTDLL.@]
2464 * Set information about an open file handle.
2466 * PARAMS
2467 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2468 * io [O] Receives information about the operation on return
2469 * ptr [I] Source for file information
2470 * len [I] Size of FileInformation
2471 * class [I] Type of file information to set
2473 * RETURNS
2474 * Success: 0. io is updated.
2475 * Failure: An NTSTATUS error code describing the error.
2477 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2478 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2480 int fd, needs_close;
2482 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2484 io->u.Status = STATUS_SUCCESS;
2485 switch (class)
2487 case FileBasicInformation:
2488 if (len >= sizeof(FILE_BASIC_INFORMATION))
2490 struct stat st;
2491 const FILE_BASIC_INFORMATION *info = ptr;
2493 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2494 return io->u.Status;
2496 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2497 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2499 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2501 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2502 else
2504 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2506 if (S_ISDIR( st.st_mode))
2507 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2508 else
2509 st.st_mode &= ~0222; /* clear write permission bits */
2511 else
2513 /* add write permission only where we already have read permission */
2514 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2516 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2520 if (needs_close) close( fd );
2522 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2523 break;
2525 case FilePositionInformation:
2526 if (len >= sizeof(FILE_POSITION_INFORMATION))
2528 const FILE_POSITION_INFORMATION *info = ptr;
2530 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2531 return io->u.Status;
2533 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2534 io->u.Status = FILE_GetNtStatus();
2536 if (needs_close) close( fd );
2538 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2539 break;
2541 case FileEndOfFileInformation:
2542 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2544 struct stat st;
2545 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2547 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2548 return io->u.Status;
2550 /* first try normal truncate */
2551 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2553 /* now check for the need to extend the file */
2554 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2556 static const char zero;
2558 /* extend the file one byte beyond the requested size and then truncate it */
2559 /* this should work around ftruncate implementations that can't extend files */
2560 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2561 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2563 io->u.Status = FILE_GetNtStatus();
2565 if (needs_close) close( fd );
2567 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2568 break;
2570 case FilePipeInformation:
2571 if (len >= sizeof(FILE_PIPE_INFORMATION))
2573 FILE_PIPE_INFORMATION *info = ptr;
2575 if ((info->CompletionMode | info->ReadMode) & ~1)
2577 io->u.Status = STATUS_INVALID_PARAMETER;
2578 break;
2581 SERVER_START_REQ( set_named_pipe_info )
2583 req->handle = wine_server_obj_handle( handle );
2584 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2585 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2586 io->u.Status = wine_server_call( req );
2588 SERVER_END_REQ;
2590 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2591 break;
2593 case FileMailslotSetInformation:
2595 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2597 SERVER_START_REQ( set_mailslot_info )
2599 req->handle = wine_server_obj_handle( handle );
2600 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2601 req->read_timeout = info->ReadTimeout.QuadPart;
2602 io->u.Status = wine_server_call( req );
2604 SERVER_END_REQ;
2606 break;
2608 case FileCompletionInformation:
2609 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2611 FILE_COMPLETION_INFORMATION *info = ptr;
2613 SERVER_START_REQ( set_completion_info )
2615 req->handle = wine_server_obj_handle( handle );
2616 req->chandle = wine_server_obj_handle( info->CompletionPort );
2617 req->ckey = info->CompletionKey;
2618 io->u.Status = wine_server_call( req );
2620 SERVER_END_REQ;
2621 } else
2622 io->u.Status = STATUS_INVALID_PARAMETER_3;
2623 break;
2625 case FileAllInformation:
2626 io->u.Status = STATUS_INVALID_INFO_CLASS;
2627 break;
2629 case FileValidDataLengthInformation:
2630 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2632 struct stat st;
2633 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2635 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2636 return io->u.Status;
2638 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2639 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2640 io->u.Status = STATUS_INVALID_PARAMETER;
2641 else
2643 #ifdef HAVE_FALLOCATE
2644 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2646 NTSTATUS status = FILE_GetNtStatus();
2647 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2648 else io->u.Status = status;
2650 #else
2651 FIXME( "setting valid data length not supported\n" );
2652 #endif
2654 if (needs_close) close( fd );
2656 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2657 break;
2659 default:
2660 FIXME("Unsupported class (%d)\n", class);
2661 io->u.Status = STATUS_NOT_IMPLEMENTED;
2662 break;
2664 io->Information = 0;
2665 return io->u.Status;
2669 /******************************************************************************
2670 * NtQueryFullAttributesFile (NTDLL.@)
2672 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2673 FILE_NETWORK_OPEN_INFORMATION *info )
2675 ANSI_STRING unix_name;
2676 NTSTATUS status;
2678 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2680 ULONG attributes;
2681 struct stat st;
2683 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2684 status = FILE_GetNtStatus();
2685 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2686 status = STATUS_INVALID_INFO_CLASS;
2687 else
2689 FILE_BASIC_INFORMATION basic;
2690 FILE_STANDARD_INFORMATION std;
2692 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2693 fill_file_info( &st, attributes, &std, FileStandardInformation );
2695 info->CreationTime = basic.CreationTime;
2696 info->LastAccessTime = basic.LastAccessTime;
2697 info->LastWriteTime = basic.LastWriteTime;
2698 info->ChangeTime = basic.ChangeTime;
2699 info->AllocationSize = std.AllocationSize;
2700 info->EndOfFile = std.EndOfFile;
2701 info->FileAttributes = basic.FileAttributes;
2702 if (DIR_is_hidden_file( attr->ObjectName ))
2703 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2705 RtlFreeAnsiString( &unix_name );
2707 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2708 return status;
2712 /******************************************************************************
2713 * NtQueryAttributesFile (NTDLL.@)
2714 * ZwQueryAttributesFile (NTDLL.@)
2716 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2718 ANSI_STRING unix_name;
2719 NTSTATUS status;
2721 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2723 ULONG attributes;
2724 struct stat st;
2726 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2727 status = FILE_GetNtStatus();
2728 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2729 status = STATUS_INVALID_INFO_CLASS;
2730 else
2732 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2733 if (DIR_is_hidden_file( attr->ObjectName ))
2734 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2736 RtlFreeAnsiString( &unix_name );
2738 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2739 return status;
2743 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2744 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2745 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2746 unsigned int flags )
2748 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2750 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2751 /* Don't assume read-only, let the mount options set it below */
2752 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2754 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2755 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2757 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2758 info->Characteristics |= FILE_REMOTE_DEVICE;
2760 else if (!strcmp("procfs", fstypename))
2761 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2762 else
2763 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2765 if (flags & MNT_RDONLY)
2766 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2768 if (!(flags & MNT_LOCAL))
2770 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2771 info->Characteristics |= FILE_REMOTE_DEVICE;
2774 #endif
2776 static inline BOOL is_device_placeholder( int fd )
2778 static const char wine_placeholder[] = "Wine device placeholder";
2779 char buffer[sizeof(wine_placeholder)-1];
2781 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2782 return FALSE;
2783 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2786 /******************************************************************************
2787 * get_device_info
2789 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2791 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2793 struct stat st;
2795 info->Characteristics = 0;
2796 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2797 if (S_ISCHR( st.st_mode ))
2799 info->DeviceType = FILE_DEVICE_UNKNOWN;
2800 #ifdef linux
2801 switch(major(st.st_rdev))
2803 case MEM_MAJOR:
2804 info->DeviceType = FILE_DEVICE_NULL;
2805 break;
2806 case TTY_MAJOR:
2807 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2808 break;
2809 case LP_MAJOR:
2810 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2811 break;
2812 case SCSI_TAPE_MAJOR:
2813 info->DeviceType = FILE_DEVICE_TAPE;
2814 break;
2816 #endif
2818 else if (S_ISBLK( st.st_mode ))
2820 info->DeviceType = FILE_DEVICE_DISK;
2822 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2824 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2826 else if (is_device_placeholder( fd ))
2828 info->DeviceType = FILE_DEVICE_DISK;
2830 else /* regular file or directory */
2832 #if defined(linux) && defined(HAVE_FSTATFS)
2833 struct statfs stfs;
2835 /* check for floppy disk */
2836 if (major(st.st_dev) == FLOPPY_MAJOR)
2837 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2839 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2840 switch (stfs.f_type)
2842 case 0x9660: /* iso9660 */
2843 case 0x9fa1: /* supermount */
2844 case 0x15013346: /* udf */
2845 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2846 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2847 break;
2848 case 0x6969: /* nfs */
2849 case 0x517B: /* smbfs */
2850 case 0x564c: /* ncpfs */
2851 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2852 info->Characteristics |= FILE_REMOTE_DEVICE;
2853 break;
2854 case 0x01021994: /* tmpfs */
2855 case 0x28cd3d45: /* cramfs */
2856 case 0x1373: /* devfs */
2857 case 0x9fa0: /* procfs */
2858 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2859 break;
2860 default:
2861 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2862 break;
2864 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2865 struct statfs stfs;
2867 if (fstatfs( fd, &stfs ) < 0)
2868 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2869 else
2870 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2871 #elif defined(__NetBSD__)
2872 struct statvfs stfs;
2874 if (fstatvfs( fd, &stfs) < 0)
2875 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2876 else
2877 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2878 #elif defined(sun)
2879 /* Use dkio to work out device types */
2881 # include <sys/dkio.h>
2882 # include <sys/vtoc.h>
2883 struct dk_cinfo dkinf;
2884 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2885 if(retval==-1){
2886 WARN("Unable to get disk device type information - assuming a disk like device\n");
2887 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2889 switch (dkinf.dki_ctype)
2891 case DKC_CDROM:
2892 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2893 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2894 break;
2895 case DKC_NCRFLOPPY:
2896 case DKC_SMSFLOPPY:
2897 case DKC_INTEL82072:
2898 case DKC_INTEL82077:
2899 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2900 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2901 break;
2902 case DKC_MD:
2903 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2904 break;
2905 default:
2906 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2909 #else
2910 static int warned;
2911 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2912 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2913 #endif
2914 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2916 return STATUS_SUCCESS;
2920 /******************************************************************************
2921 * NtQueryVolumeInformationFile [NTDLL.@]
2922 * ZwQueryVolumeInformationFile [NTDLL.@]
2924 * Get volume information for an open file handle.
2926 * PARAMS
2927 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2928 * io [O] Receives information about the operation on return
2929 * buffer [O] Destination for volume information
2930 * length [I] Size of FsInformation
2931 * info_class [I] Type of volume information to set
2933 * RETURNS
2934 * Success: 0. io and buffer are updated.
2935 * Failure: An NTSTATUS error code describing the error.
2937 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2938 PVOID buffer, ULONG length,
2939 FS_INFORMATION_CLASS info_class )
2941 int fd, needs_close;
2942 struct stat st;
2943 static int once;
2945 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2946 return io->u.Status;
2948 io->u.Status = STATUS_NOT_IMPLEMENTED;
2949 io->Information = 0;
2951 switch( info_class )
2953 case FileFsVolumeInformation:
2954 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2955 break;
2956 case FileFsLabelInformation:
2957 FIXME( "%p: label info not supported\n", handle );
2958 break;
2959 case FileFsSizeInformation:
2960 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2961 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2962 else
2964 FILE_FS_SIZE_INFORMATION *info = buffer;
2966 if (fstat( fd, &st ) < 0)
2968 io->u.Status = FILE_GetNtStatus();
2969 break;
2971 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2973 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2975 else
2977 ULONGLONG bsize;
2978 /* Linux's fstatvfs is buggy */
2979 #if !defined(linux) || !defined(HAVE_FSTATFS)
2980 struct statvfs stfs;
2982 if (fstatvfs( fd, &stfs ) < 0)
2984 io->u.Status = FILE_GetNtStatus();
2985 break;
2987 bsize = stfs.f_frsize;
2988 #else
2989 struct statfs stfs;
2990 if (fstatfs( fd, &stfs ) < 0)
2992 io->u.Status = FILE_GetNtStatus();
2993 break;
2995 bsize = stfs.f_bsize;
2996 #endif
2997 if (bsize == 2048) /* assume CD-ROM */
2999 info->BytesPerSector = 2048;
3000 info->SectorsPerAllocationUnit = 1;
3002 else
3004 info->BytesPerSector = 512;
3005 info->SectorsPerAllocationUnit = 8;
3007 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3008 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
3009 io->Information = sizeof(*info);
3010 io->u.Status = STATUS_SUCCESS;
3013 break;
3014 case FileFsDeviceInformation:
3015 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
3016 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3017 else
3019 FILE_FS_DEVICE_INFORMATION *info = buffer;
3021 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
3022 io->Information = sizeof(*info);
3024 break;
3025 case FileFsAttributeInformation:
3026 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
3027 io->u.Status = STATUS_BUFFER_TOO_SMALL;
3028 else
3030 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
3032 FIXME( "%p: faking attribute info\n", handle );
3033 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3034 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3035 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3036 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3037 info->FileSystemNameLength = sizeof(ntfsW);
3038 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3040 io->Information = sizeof(*info);
3041 io->u.Status = STATUS_SUCCESS;
3043 break;
3044 case FileFsControlInformation:
3045 FIXME( "%p: control info not supported\n", handle );
3046 break;
3047 case FileFsFullSizeInformation:
3048 FIXME( "%p: full size info not supported\n", handle );
3049 break;
3050 case FileFsObjectIdInformation:
3051 FIXME( "%p: object id info not supported\n", handle );
3052 break;
3053 case FileFsMaximumInformation:
3054 FIXME( "%p: maximum info not supported\n", handle );
3055 break;
3056 default:
3057 io->u.Status = STATUS_INVALID_PARAMETER;
3058 break;
3060 if (needs_close) close( fd );
3061 return io->u.Status;
3065 /******************************************************************
3066 * NtQueryEaFile (NTDLL.@)
3068 * Read extended attributes from NTFS files.
3070 * PARAMS
3071 * hFile [I] File handle, must be opened with FILE_READ_EA access
3072 * iosb [O] Receives information about the operation on return
3073 * buffer [O] Output buffer
3074 * length [I] Length of output buffer
3075 * single_entry [I] Only read and return one entry
3076 * ea_list [I] Optional list with names of EAs to return
3077 * ea_list_len [I] Length of ea_list in bytes
3078 * ea_index [I] Optional pointer to 1-based index of attribute to return
3079 * restart [I] restart EA scan
3081 * RETURNS
3082 * Success: 0. Atrributes read into buffer
3083 * Failure: An NTSTATUS error code describing the error.
3085 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3086 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3087 PULONG ea_index, BOOLEAN restart )
3089 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3090 hFile, iosb, buffer, length, single_entry, ea_list,
3091 ea_list_len, ea_index, restart);
3092 return STATUS_ACCESS_DENIED;
3096 /******************************************************************
3097 * NtSetEaFile (NTDLL.@)
3099 * Update extended attributes for NTFS files.
3101 * PARAMS
3102 * hFile [I] File handle, must be opened with FILE_READ_EA access
3103 * iosb [O] Receives information about the operation on return
3104 * buffer [I] Buffer with EA information
3105 * length [I] Length of buffer
3107 * RETURNS
3108 * Success: 0. Attributes are updated
3109 * Failure: An NTSTATUS error code describing the error.
3111 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3113 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3114 return STATUS_ACCESS_DENIED;
3118 /******************************************************************
3119 * NtFlushBuffersFile (NTDLL.@)
3121 * Flush any buffered data on an open file handle.
3123 * PARAMS
3124 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3125 * IoStatusBlock [O] Receives information about the operation on return
3127 * RETURNS
3128 * Success: 0. IoStatusBlock is updated.
3129 * Failure: An NTSTATUS error code describing the error.
3131 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3133 NTSTATUS ret;
3134 HANDLE hEvent = NULL;
3135 enum server_fd_type type;
3136 int fd, needs_close;
3138 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3140 if (!ret && type == FD_TYPE_SERIAL)
3142 ret = COMM_FlushBuffersFile( fd );
3144 else
3146 SERVER_START_REQ( flush_file )
3148 req->handle = wine_server_obj_handle( hFile );
3149 ret = wine_server_call( req );
3150 hEvent = wine_server_ptr_handle( reply->event );
3152 SERVER_END_REQ;
3153 if (!ret && hEvent)
3155 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
3156 NtClose( hEvent );
3160 if (needs_close) close( fd );
3161 return ret;
3164 /******************************************************************
3165 * NtLockFile (NTDLL.@)
3169 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3170 PIO_APC_ROUTINE apc, void* apc_user,
3171 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3172 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3173 BOOLEAN exclusive )
3175 NTSTATUS ret;
3176 HANDLE handle;
3177 BOOLEAN async;
3178 static BOOLEAN warn = TRUE;
3180 if (apc || io_status || key)
3182 FIXME("Unimplemented yet parameter\n");
3183 return STATUS_NOT_IMPLEMENTED;
3186 if (apc_user && warn)
3188 FIXME("I/O completion on lock not implemented yet\n");
3189 warn = FALSE;
3192 for (;;)
3194 SERVER_START_REQ( lock_file )
3196 req->handle = wine_server_obj_handle( hFile );
3197 req->offset = offset->QuadPart;
3198 req->count = count->QuadPart;
3199 req->shared = !exclusive;
3200 req->wait = !dont_wait;
3201 ret = wine_server_call( req );
3202 handle = wine_server_ptr_handle( reply->handle );
3203 async = reply->overlapped;
3205 SERVER_END_REQ;
3206 if (ret != STATUS_PENDING)
3208 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3209 return ret;
3212 if (async)
3214 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3215 if (handle) NtClose( handle );
3216 return STATUS_PENDING;
3218 if (handle)
3220 NtWaitForSingleObject( handle, FALSE, NULL );
3221 NtClose( handle );
3223 else
3225 LARGE_INTEGER time;
3227 /* Unix lock conflict, sleep a bit and retry */
3228 time.QuadPart = 100 * (ULONGLONG)10000;
3229 time.QuadPart = -time.QuadPart;
3230 NtDelayExecution( FALSE, &time );
3236 /******************************************************************
3237 * NtUnlockFile (NTDLL.@)
3241 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3242 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3243 PULONG key )
3245 NTSTATUS status;
3247 TRACE( "%p %x%08x %x%08x\n",
3248 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3250 if (io_status || key)
3252 FIXME("Unimplemented yet parameter\n");
3253 return STATUS_NOT_IMPLEMENTED;
3256 SERVER_START_REQ( unlock_file )
3258 req->handle = wine_server_obj_handle( hFile );
3259 req->offset = offset->QuadPart;
3260 req->count = count->QuadPart;
3261 status = wine_server_call( req );
3263 SERVER_END_REQ;
3264 return status;
3267 /******************************************************************
3268 * NtCreateNamedPipeFile (NTDLL.@)
3272 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3273 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3274 ULONG sharing, ULONG dispo, ULONG options,
3275 ULONG pipe_type, ULONG read_mode,
3276 ULONG completion_mode, ULONG max_inst,
3277 ULONG inbound_quota, ULONG outbound_quota,
3278 PLARGE_INTEGER timeout)
3280 struct security_descriptor *sd = NULL;
3281 struct object_attributes objattr;
3282 NTSTATUS status;
3284 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3285 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3286 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3287 outbound_quota, timeout);
3289 /* assume we only get relative timeout */
3290 if (timeout->QuadPart > 0)
3291 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3293 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
3294 objattr.sd_len = 0;
3295 objattr.name_len = attr->ObjectName->Length;
3297 status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
3298 if (status != STATUS_SUCCESS) return status;
3300 SERVER_START_REQ( create_named_pipe )
3302 req->access = access;
3303 req->attributes = attr->Attributes;
3304 req->options = options;
3305 req->sharing = sharing;
3306 req->flags =
3307 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3308 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3309 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3310 req->maxinstances = max_inst;
3311 req->outsize = outbound_quota;
3312 req->insize = inbound_quota;
3313 req->timeout = timeout->QuadPart;
3314 wine_server_add_data( req, &objattr, sizeof(objattr) );
3315 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
3316 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
3317 status = wine_server_call( req );
3318 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3320 SERVER_END_REQ;
3322 NTDLL_free_struct_sd( sd );
3323 return status;
3326 /******************************************************************
3327 * NtDeleteFile (NTDLL.@)
3331 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3333 NTSTATUS status;
3334 HANDLE hFile;
3335 IO_STATUS_BLOCK io;
3337 TRACE("%p\n", ObjectAttributes);
3338 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3339 ObjectAttributes, &io, NULL, 0,
3340 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3341 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3342 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3343 return status;
3346 /******************************************************************
3347 * NtCancelIoFileEx (NTDLL.@)
3351 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3353 LARGE_INTEGER timeout;
3355 TRACE("%p %p %p\n", hFile, iosb, io_status );
3357 SERVER_START_REQ( cancel_async )
3359 req->handle = wine_server_obj_handle( hFile );
3360 req->iosb = wine_server_client_ptr( iosb );
3361 req->only_thread = FALSE;
3362 io_status->u.Status = wine_server_call( req );
3364 SERVER_END_REQ;
3365 if (io_status->u.Status)
3366 return io_status->u.Status;
3368 /* Let some APC be run, so that we can run the remaining APCs on hFile
3369 * either the cancelation of the pending one, but also the execution
3370 * of the queued APC, but not yet run. This is needed to ensure proper
3371 * clean-up of allocated data.
3373 timeout.QuadPart = 0;
3374 NtDelayExecution( TRUE, &timeout );
3375 return io_status->u.Status;
3378 /******************************************************************
3379 * NtCancelIoFile (NTDLL.@)
3383 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3385 LARGE_INTEGER timeout;
3387 TRACE("%p %p\n", hFile, io_status );
3389 SERVER_START_REQ( cancel_async )
3391 req->handle = wine_server_obj_handle( hFile );
3392 req->iosb = 0;
3393 req->only_thread = TRUE;
3394 io_status->u.Status = wine_server_call( req );
3396 SERVER_END_REQ;
3397 if (io_status->u.Status)
3398 return io_status->u.Status;
3400 /* Let some APC be run, so that we can run the remaining APCs on hFile
3401 * either the cancelation of the pending one, but also the execution
3402 * of the queued APC, but not yet run. This is needed to ensure proper
3403 * clean-up of allocated data.
3405 timeout.QuadPart = 0;
3406 NtDelayExecution( TRUE, &timeout );
3407 return io_status->u.Status;
3410 /******************************************************************************
3411 * NtCreateMailslotFile [NTDLL.@]
3412 * ZwCreateMailslotFile [NTDLL.@]
3414 * PARAMS
3415 * pHandle [O] pointer to receive the handle created
3416 * DesiredAccess [I] access mode (read, write, etc)
3417 * ObjectAttributes [I] fully qualified NT path of the mailslot
3418 * IoStatusBlock [O] receives completion status and other info
3419 * CreateOptions [I]
3420 * MailslotQuota [I]
3421 * MaxMessageSize [I]
3422 * TimeOut [I]
3424 * RETURNS
3425 * An NT status code
3427 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3428 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3429 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3430 PLARGE_INTEGER TimeOut)
3432 LARGE_INTEGER timeout;
3433 NTSTATUS ret;
3435 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3436 pHandle, DesiredAccess, attr, IoStatusBlock,
3437 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3439 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3440 if (!attr) return STATUS_INVALID_PARAMETER;
3441 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3444 * For a NULL TimeOut pointer set the default timeout value
3446 if (!TimeOut)
3447 timeout.QuadPart = -1;
3448 else
3449 timeout.QuadPart = TimeOut->QuadPart;
3451 SERVER_START_REQ( create_mailslot )
3453 req->access = DesiredAccess;
3454 req->attributes = attr->Attributes;
3455 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3456 req->max_msgsize = MaxMessageSize;
3457 req->read_timeout = timeout.QuadPart;
3458 wine_server_add_data( req, attr->ObjectName->Buffer,
3459 attr->ObjectName->Length );
3460 ret = wine_server_call( req );
3461 if( ret == STATUS_SUCCESS )
3462 *pHandle = wine_server_ptr_handle( reply->handle );
3464 SERVER_END_REQ;
3466 return ret;