ntdll: Assert when trying to replace an exiting file descriptor in fd_cache.
[wine/multimedia.git] / dlls / ntdll / file.c
blobb07e5f932ccdda2cc109f38bbf797a92a8dfa093
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 #define NONAMELESSUNION
80 #define NONAMELESSSTRUCT
81 #include "ntstatus.h"
82 #define WIN32_NO_STATUS
83 #include "wine/unicode.h"
84 #include "wine/debug.h"
85 #include "wine/server.h"
86 #include "ntdll_misc.h"
88 #include "winternl.h"
89 #include "winioctl.h"
90 #include "ddk/ntddk.h"
91 #include "ddk/ntddser.h"
93 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
94 WINE_DECLARE_DEBUG_CHANNEL(winediag);
96 mode_t FILE_umask = 0;
98 #define SECSPERDAY 86400
99 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
101 #define FILE_WRITE_TO_END_OF_FILE ((LONGLONG)-1)
102 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
104 static const WCHAR ntfsW[] = {'N','T','F','S'};
106 /* fetch the attributes of a file */
107 static inline ULONG get_file_attributes( const struct stat *st )
109 ULONG attr;
111 if (S_ISDIR(st->st_mode))
112 attr = FILE_ATTRIBUTE_DIRECTORY;
113 else
114 attr = FILE_ATTRIBUTE_ARCHIVE;
115 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
116 attr |= FILE_ATTRIBUTE_READONLY;
117 return attr;
120 /* get the stat info and file attributes for a file (by file descriptor) */
121 int fd_get_file_info( int fd, struct stat *st, ULONG *attr )
123 int ret;
125 *attr = 0;
126 ret = fstat( fd, st );
127 if (ret == -1) return ret;
128 *attr |= get_file_attributes( st );
129 return ret;
132 /* get the stat info and file attributes for a file (by name) */
133 int get_file_info( const char *path, struct stat *st, ULONG *attr )
135 int ret;
137 *attr = 0;
138 ret = lstat( path, st );
139 if (ret == -1) return ret;
140 if (S_ISLNK( st->st_mode ))
142 ret = stat( path, st );
143 if (ret == -1) return ret;
144 /* is a symbolic link and a directory, consider these "reparse points" */
145 if (S_ISDIR( st->st_mode )) *attr |= FILE_ATTRIBUTE_REPARSE_POINT;
147 *attr |= get_file_attributes( st );
148 return ret;
151 /**************************************************************************
152 * FILE_CreateFile (internal)
153 * Open a file.
155 * Parameter set fully identical with NtCreateFile
157 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
158 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
159 ULONG attributes, ULONG sharing, ULONG disposition,
160 ULONG options, PVOID ea_buffer, ULONG ea_length )
162 ANSI_STRING unix_name;
163 BOOL created = FALSE;
165 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
166 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
167 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
168 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
169 attributes, sharing, disposition, options, ea_buffer, ea_length );
171 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
173 if (alloc_size) FIXME( "alloc_size not supported\n" );
175 if (options & FILE_OPEN_BY_FILE_ID)
176 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
177 else
178 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
180 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
182 SERVER_START_REQ( open_file_object )
184 req->access = access;
185 req->attributes = attr->Attributes;
186 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
187 req->sharing = sharing;
188 req->options = options;
189 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
190 io->u.Status = wine_server_call( req );
191 *handle = wine_server_ptr_handle( reply->handle );
193 SERVER_END_REQ;
194 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
195 return io->u.Status;
198 if (io->u.Status == STATUS_NO_SUCH_FILE &&
199 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
201 created = TRUE;
202 io->u.Status = STATUS_SUCCESS;
205 if (io->u.Status == STATUS_SUCCESS)
207 struct security_descriptor *sd;
208 struct object_attributes objattr;
210 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
211 objattr.name_len = 0;
212 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
213 if (io->u.Status != STATUS_SUCCESS)
215 RtlFreeAnsiString( &unix_name );
216 return io->u.Status;
219 SERVER_START_REQ( create_file )
221 req->access = access;
222 req->attributes = attr->Attributes;
223 req->sharing = sharing;
224 req->create = disposition;
225 req->options = options;
226 req->attrs = attributes;
227 wine_server_add_data( req, &objattr, sizeof(objattr) );
228 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
229 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
230 io->u.Status = wine_server_call( req );
231 *handle = wine_server_ptr_handle( reply->handle );
233 SERVER_END_REQ;
234 NTDLL_free_struct_sd( sd );
235 RtlFreeAnsiString( &unix_name );
237 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
239 if (io->u.Status == STATUS_SUCCESS)
241 if (created) io->Information = FILE_CREATED;
242 else switch(disposition)
244 case FILE_SUPERSEDE:
245 io->Information = FILE_SUPERSEDED;
246 break;
247 case FILE_CREATE:
248 io->Information = FILE_CREATED;
249 break;
250 case FILE_OPEN:
251 case FILE_OPEN_IF:
252 io->Information = FILE_OPENED;
253 break;
254 case FILE_OVERWRITE:
255 case FILE_OVERWRITE_IF:
256 io->Information = FILE_OVERWRITTEN;
257 break;
260 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
262 static int once;
263 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
266 return io->u.Status;
269 /**************************************************************************
270 * NtOpenFile [NTDLL.@]
271 * ZwOpenFile [NTDLL.@]
273 * Open a file.
275 * PARAMS
276 * handle [O] Variable that receives the file handle on return
277 * access [I] Access desired by the caller to the file
278 * attr [I] Structure describing the file to be opened
279 * io [O] Receives details about the result of the operation
280 * sharing [I] Type of shared access the caller requires
281 * options [I] Options for the file open
283 * RETURNS
284 * Success: 0. FileHandle and IoStatusBlock are updated.
285 * Failure: An NTSTATUS error code describing the error.
287 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
288 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
289 ULONG sharing, ULONG options )
291 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
292 sharing, FILE_OPEN, options, NULL, 0 );
295 /**************************************************************************
296 * NtCreateFile [NTDLL.@]
297 * ZwCreateFile [NTDLL.@]
299 * Either create a new file or directory, or open an existing file, device,
300 * directory or volume.
302 * PARAMS
303 * handle [O] Points to a variable which receives the file handle on return
304 * access [I] Desired access to the file
305 * attr [I] Structure describing the file
306 * io [O] Receives information about the operation on return
307 * alloc_size [I] Initial size of the file in bytes
308 * attributes [I] Attributes to create the file with
309 * sharing [I] Type of shared access the caller would like to the file
310 * disposition [I] Specifies what to do, depending on whether the file already exists
311 * options [I] Options for creating a new file
312 * ea_buffer [I] Pointer to an extended attributes buffer
313 * ea_length [I] Length of ea_buffer
315 * RETURNS
316 * Success: 0. handle and io are updated.
317 * Failure: An NTSTATUS error code describing the error.
319 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
320 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
321 ULONG attributes, ULONG sharing, ULONG disposition,
322 ULONG options, PVOID ea_buffer, ULONG ea_length )
324 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
325 sharing, disposition, options, ea_buffer, ea_length );
328 /***********************************************************************
329 * Asynchronous file I/O *
332 struct async_fileio
334 struct async_fileio *next;
335 HANDLE handle;
336 PIO_APC_ROUTINE apc;
337 void *apc_arg;
340 struct async_fileio_read
342 struct async_fileio io;
343 char* buffer;
344 unsigned int already;
345 unsigned int count;
346 BOOL avail_mode;
349 struct async_fileio_write
351 struct async_fileio io;
352 const char *buffer;
353 unsigned int already;
354 unsigned int count;
357 static struct async_fileio *fileio_freelist;
359 static void release_fileio( struct async_fileio *io )
361 for (;;)
363 struct async_fileio *next = fileio_freelist;
364 io->next = next;
365 if (interlocked_cmpxchg_ptr( (void **)&fileio_freelist, io, next ) == next) return;
369 static struct async_fileio *alloc_fileio( DWORD size, HANDLE handle, PIO_APC_ROUTINE apc, void *arg )
371 /* first free remaining previous fileinfos */
373 struct async_fileio *io = interlocked_xchg_ptr( (void **)&fileio_freelist, NULL );
375 while (io)
377 struct async_fileio *next = io->next;
378 RtlFreeHeap( GetProcessHeap(), 0, io );
379 io = next;
382 if ((io = RtlAllocateHeap( GetProcessHeap(), 0, size )))
384 io->handle = handle;
385 io->apc = apc;
386 io->apc_arg = arg;
388 return io;
391 /***********************************************************************
392 * FILE_GetNtStatus(void)
394 * Retrieve the Nt Status code from errno.
395 * Try to be consistent with FILE_SetDosError().
397 NTSTATUS FILE_GetNtStatus(void)
399 int err = errno;
401 TRACE( "errno = %d\n", errno );
402 switch (err)
404 case EAGAIN: return STATUS_SHARING_VIOLATION;
405 case EBADF: return STATUS_INVALID_HANDLE;
406 case EBUSY: return STATUS_DEVICE_BUSY;
407 case ENOSPC: return STATUS_DISK_FULL;
408 case EPERM:
409 case EROFS:
410 case EACCES: return STATUS_ACCESS_DENIED;
411 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
412 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
413 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
414 case EMFILE:
415 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
416 case EINVAL: return STATUS_INVALID_PARAMETER;
417 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
418 case EPIPE: return STATUS_PIPE_DISCONNECTED;
419 case EIO: return STATUS_DEVICE_NOT_READY;
420 #ifdef ENOMEDIUM
421 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
422 #endif
423 case ENXIO: return STATUS_NO_SUCH_DEVICE;
424 case ENOTTY:
425 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
426 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
427 case EFAULT: return STATUS_ACCESS_VIOLATION;
428 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
429 #ifdef ETIME /* Missing on FreeBSD */
430 case ETIME: return STATUS_IO_TIMEOUT;
431 #endif
432 case ENOEXEC: /* ?? */
433 case EEXIST: /* ?? */
434 default:
435 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
436 return STATUS_UNSUCCESSFUL;
440 /***********************************************************************
441 * FILE_AsyncReadService (INTERNAL)
443 static NTSTATUS FILE_AsyncReadService( void *user, IO_STATUS_BLOCK *iosb,
444 NTSTATUS status, void **apc, void **arg )
446 struct async_fileio_read *fileio = user;
447 int fd, needs_close, result;
449 switch (status)
451 case STATUS_ALERTED: /* got some new data */
452 /* check to see if the data is ready (non-blocking) */
453 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
454 &needs_close, NULL, NULL )))
455 break;
457 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
458 if (needs_close) close( fd );
460 if (result < 0)
462 if (errno == EAGAIN || errno == EINTR)
463 status = STATUS_PENDING;
464 else /* check to see if the transfer is complete */
465 status = FILE_GetNtStatus();
467 else if (result == 0)
469 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
471 else
473 fileio->already += result;
474 if (fileio->already >= fileio->count || fileio->avail_mode)
475 status = STATUS_SUCCESS;
476 else
478 /* if we only have to read the available data, and none is available,
479 * simply cancel the request. If data was available, it has been read
480 * while in by previous call (NtDelayExecution)
482 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
485 break;
487 case STATUS_TIMEOUT:
488 case STATUS_IO_TIMEOUT:
489 if (fileio->already) status = STATUS_SUCCESS;
490 break;
492 if (status != STATUS_PENDING)
494 iosb->u.Status = status;
495 iosb->Information = fileio->already;
496 *apc = fileio->io.apc;
497 *arg = fileio->io.apc_arg;
498 release_fileio( &fileio->io );
500 return status;
503 struct io_timeouts
505 int interval; /* max interval between two bytes */
506 int total; /* total timeout for the whole operation */
507 int end_time; /* absolute time of end of operation */
510 /* retrieve the I/O timeouts to use for a given handle */
511 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
512 struct io_timeouts *timeouts )
514 NTSTATUS status = STATUS_SUCCESS;
516 timeouts->interval = timeouts->total = -1;
518 switch(type)
520 case FD_TYPE_SERIAL:
522 /* GetCommTimeouts */
523 SERIAL_TIMEOUTS st;
524 IO_STATUS_BLOCK io;
526 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
527 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
528 if (status) break;
530 if (is_read)
532 if (st.ReadIntervalTimeout)
533 timeouts->interval = st.ReadIntervalTimeout;
535 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
537 timeouts->total = st.ReadTotalTimeoutConstant;
538 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
539 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
541 else if (st.ReadIntervalTimeout == MAXDWORD)
542 timeouts->interval = timeouts->total = 0;
544 else /* write */
546 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
548 timeouts->total = st.WriteTotalTimeoutConstant;
549 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
550 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
554 break;
555 case FD_TYPE_MAILSLOT:
556 if (is_read)
558 timeouts->interval = 0; /* return as soon as we got something */
559 SERVER_START_REQ( set_mailslot_info )
561 req->handle = wine_server_obj_handle( handle );
562 req->flags = 0;
563 if (!(status = wine_server_call( req )) &&
564 reply->read_timeout != TIMEOUT_INFINITE)
565 timeouts->total = reply->read_timeout / -10000;
567 SERVER_END_REQ;
569 break;
570 case FD_TYPE_SOCKET:
571 case FD_TYPE_PIPE:
572 case FD_TYPE_CHAR:
573 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
574 break;
575 default:
576 break;
578 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
579 return STATUS_SUCCESS;
583 /* retrieve the timeout for the next wait, in milliseconds */
584 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
586 int ret = -1;
588 if (timeouts->total != -1)
590 ret = timeouts->end_time - NtGetTickCount();
591 if (ret < 0) ret = 0;
593 if (already && timeouts->interval != -1)
595 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
597 return ret;
601 /* retrieve the avail_mode flag for async reads */
602 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
604 NTSTATUS status = STATUS_SUCCESS;
606 switch(type)
608 case FD_TYPE_SERIAL:
610 /* GetCommTimeouts */
611 SERIAL_TIMEOUTS st;
612 IO_STATUS_BLOCK io;
614 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
615 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
616 if (status) break;
617 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
618 !st.ReadTotalTimeoutConstant &&
619 st.ReadIntervalTimeout == MAXDWORD);
621 break;
622 case FD_TYPE_MAILSLOT:
623 case FD_TYPE_SOCKET:
624 case FD_TYPE_PIPE:
625 case FD_TYPE_CHAR:
626 *avail_mode = TRUE;
627 break;
628 default:
629 *avail_mode = FALSE;
630 break;
632 return status;
636 /******************************************************************************
637 * NtReadFile [NTDLL.@]
638 * ZwReadFile [NTDLL.@]
640 * Read from an open file handle.
642 * PARAMS
643 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
644 * Event [I] Event to signal upon completion (or NULL)
645 * ApcRoutine [I] Callback to call upon completion (or NULL)
646 * ApcContext [I] Context for ApcRoutine (or NULL)
647 * IoStatusBlock [O] Receives information about the operation on return
648 * Buffer [O] Destination for the data read
649 * Length [I] Size of Buffer
650 * ByteOffset [O] Destination for the new file pointer position (or NULL)
651 * Key [O] Function unknown (may be NULL)
653 * RETURNS
654 * Success: 0. IoStatusBlock is updated, and the Information member contains
655 * The number of bytes read.
656 * Failure: An NTSTATUS error code describing the error.
658 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
659 PIO_APC_ROUTINE apc, void* apc_user,
660 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
661 PLARGE_INTEGER offset, PULONG key)
663 int result, unix_handle, needs_close;
664 unsigned int options;
665 struct io_timeouts timeouts;
666 NTSTATUS status;
667 ULONG total = 0;
668 enum server_fd_type type;
669 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
670 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
672 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
673 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
675 if (!io_status) return STATUS_ACCESS_VIOLATION;
677 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
678 &needs_close, &type, &options );
679 if (status) return status;
681 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
683 if (!virtual_check_buffer_for_write( buffer, length ))
685 status = STATUS_ACCESS_VIOLATION;
686 goto done;
689 if (type == FD_TYPE_FILE)
691 if (async_read && (!offset || offset->QuadPart < 0))
693 status = STATUS_INVALID_PARAMETER;
694 goto done;
697 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
699 /* async I/O doesn't make sense on regular files */
700 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
702 if (errno != EINTR)
704 status = FILE_GetNtStatus();
705 goto done;
708 if (!async_read)
709 /* update file pointer position */
710 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
712 total = result;
713 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
714 goto done;
717 else if (type == FD_TYPE_SERIAL)
719 if (async_read && (!offset || offset->QuadPart < 0))
721 status = STATUS_INVALID_PARAMETER;
722 goto done;
726 for (;;)
728 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
730 total += result;
731 if (!result || total == length)
733 if (total)
735 status = STATUS_SUCCESS;
736 goto done;
738 switch (type)
740 case FD_TYPE_FILE:
741 case FD_TYPE_CHAR:
742 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
743 goto done;
744 case FD_TYPE_SERIAL:
745 break;
746 default:
747 status = STATUS_PIPE_BROKEN;
748 goto done;
751 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
753 else if (errno != EAGAIN)
755 if (errno == EINTR) continue;
756 if (!total) status = FILE_GetNtStatus();
757 goto done;
760 if (async_read)
762 struct async_fileio_read *fileio;
763 BOOL avail_mode;
765 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
766 goto err;
767 if (total && avail_mode)
769 status = STATUS_SUCCESS;
770 goto done;
773 fileio = (struct async_fileio_read *)alloc_fileio( sizeof(*fileio), hFile, apc, apc_user );
774 if (!fileio)
776 status = STATUS_NO_MEMORY;
777 goto err;
779 fileio->already = total;
780 fileio->count = length;
781 fileio->buffer = buffer;
782 fileio->avail_mode = avail_mode;
784 SERVER_START_REQ( register_async )
786 req->type = ASYNC_TYPE_READ;
787 req->count = length;
788 req->async.handle = wine_server_obj_handle( hFile );
789 req->async.event = wine_server_obj_handle( hEvent );
790 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
791 req->async.iosb = wine_server_client_ptr( io_status );
792 req->async.arg = wine_server_client_ptr( fileio );
793 req->async.cvalue = cvalue;
794 status = wine_server_call( req );
796 SERVER_END_REQ;
798 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
799 goto err;
801 else /* synchronous read, wait for the fd to become ready */
803 struct pollfd pfd;
804 int ret, timeout;
806 if (!timeout_init_done)
808 timeout_init_done = TRUE;
809 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
810 goto err;
811 if (hEvent) NtResetEvent( hEvent, NULL );
813 timeout = get_next_io_timeout( &timeouts, total );
815 pfd.fd = unix_handle;
816 pfd.events = POLLIN;
818 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
820 if (total) /* return with what we got so far */
821 status = STATUS_SUCCESS;
822 else
823 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
824 goto done;
826 if (ret == -1 && errno != EINTR)
828 status = FILE_GetNtStatus();
829 goto done;
831 /* will now restart the read */
835 done:
836 send_completion = cvalue != 0;
838 err:
839 if (needs_close) close( unix_handle );
840 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
842 io_status->u.Status = status;
843 io_status->Information = total;
844 TRACE("= SUCCESS (%u)\n", total);
845 if (hEvent) NtSetEvent( hEvent, NULL );
846 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
847 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
849 else
851 TRACE("= 0x%08x\n", status);
852 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
855 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
857 return status;
861 /******************************************************************************
862 * NtReadFileScatter [NTDLL.@]
863 * ZwReadFileScatter [NTDLL.@]
865 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
866 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
867 ULONG length, PLARGE_INTEGER offset, PULONG key )
869 int result, unix_handle, needs_close;
870 unsigned int options;
871 NTSTATUS status;
872 ULONG pos = 0, total = 0;
873 enum server_fd_type type;
874 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
875 BOOL send_completion = FALSE;
877 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
878 file, event, apc, apc_user, io_status, segments, length, offset, key);
880 if (length % page_size) return STATUS_INVALID_PARAMETER;
881 if (!io_status) return STATUS_ACCESS_VIOLATION;
883 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
884 &needs_close, &type, &options );
885 if (status) return status;
887 if ((type != FD_TYPE_FILE) ||
888 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
889 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
891 status = STATUS_INVALID_PARAMETER;
892 goto error;
895 while (length)
897 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
898 result = pread( unix_handle, (char *)segments->Buffer + pos,
899 page_size - pos, offset->QuadPart + total );
900 else
901 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
903 if (result == -1)
905 if (errno == EINTR) continue;
906 status = FILE_GetNtStatus();
907 break;
909 if (!result)
911 status = STATUS_END_OF_FILE;
912 break;
914 total += result;
915 length -= result;
916 if ((pos += result) == page_size)
918 pos = 0;
919 segments++;
923 send_completion = cvalue != 0;
925 error:
926 if (needs_close) close( unix_handle );
927 if (status == STATUS_SUCCESS)
929 io_status->u.Status = status;
930 io_status->Information = total;
931 TRACE("= SUCCESS (%u)\n", total);
932 if (event) NtSetEvent( event, NULL );
933 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
934 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
936 else
938 TRACE("= 0x%08x\n", status);
939 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
942 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
944 return status;
948 /***********************************************************************
949 * FILE_AsyncWriteService (INTERNAL)
951 static NTSTATUS FILE_AsyncWriteService( void *user, IO_STATUS_BLOCK *iosb,
952 NTSTATUS status, void **apc, void **arg )
954 struct async_fileio_write *fileio = user;
955 int result, fd, needs_close;
956 enum server_fd_type type;
958 switch (status)
960 case STATUS_ALERTED:
961 /* write some data (non-blocking) */
962 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
963 &needs_close, &type, NULL )))
964 break;
966 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
967 result = send( fd, fileio->buffer, 0, 0 );
968 else
969 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
971 if (needs_close) close( fd );
973 if (result < 0)
975 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
976 else status = FILE_GetNtStatus();
978 else
980 fileio->already += result;
981 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
983 break;
985 case STATUS_TIMEOUT:
986 case STATUS_IO_TIMEOUT:
987 if (fileio->already) status = STATUS_SUCCESS;
988 break;
990 if (status != STATUS_PENDING)
992 iosb->u.Status = status;
993 iosb->Information = fileio->already;
994 *apc = fileio->io.apc;
995 *arg = fileio->io.apc_arg;
996 release_fileio( &fileio->io );
998 return status;
1001 static NTSTATUS set_pending_write( HANDLE device )
1003 NTSTATUS status;
1005 SERVER_START_REQ( set_serial_info )
1007 req->handle = wine_server_obj_handle( device );
1008 req->flags = SERIALINFO_PENDING_WRITE;
1009 status = wine_server_call( req );
1011 SERVER_END_REQ;
1012 return status;
1015 /******************************************************************************
1016 * NtWriteFile [NTDLL.@]
1017 * ZwWriteFile [NTDLL.@]
1019 * Write to an open file handle.
1021 * PARAMS
1022 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1023 * Event [I] Event to signal upon completion (or NULL)
1024 * ApcRoutine [I] Callback to call upon completion (or NULL)
1025 * ApcContext [I] Context for ApcRoutine (or NULL)
1026 * IoStatusBlock [O] Receives information about the operation on return
1027 * Buffer [I] Source for the data to write
1028 * Length [I] Size of Buffer
1029 * ByteOffset [O] Destination for the new file pointer position (or NULL)
1030 * Key [O] Function unknown (may be NULL)
1032 * RETURNS
1033 * Success: 0. IoStatusBlock is updated, and the Information member contains
1034 * The number of bytes written.
1035 * Failure: An NTSTATUS error code describing the error.
1037 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
1038 PIO_APC_ROUTINE apc, void* apc_user,
1039 PIO_STATUS_BLOCK io_status,
1040 const void* buffer, ULONG length,
1041 PLARGE_INTEGER offset, PULONG key)
1043 int result, unix_handle, needs_close;
1044 unsigned int options;
1045 struct io_timeouts timeouts;
1046 NTSTATUS status;
1047 ULONG total = 0;
1048 enum server_fd_type type;
1049 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1050 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
1051 LARGE_INTEGER offset_eof;
1053 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
1054 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
1056 if (!io_status) return STATUS_ACCESS_VIOLATION;
1058 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
1059 &needs_close, &type, &options );
1060 if (status == STATUS_ACCESS_DENIED)
1062 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
1063 &needs_close, &type, &options );
1064 append_write = TRUE;
1066 if (status) return status;
1068 if (!virtual_check_buffer_for_read( buffer, length ))
1070 status = STATUS_INVALID_USER_BUFFER;
1071 goto done;
1074 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1076 if (type == FD_TYPE_FILE)
1078 if (async_write &&
1079 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1081 status = STATUS_INVALID_PARAMETER;
1082 goto done;
1085 if (append_write)
1087 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1088 offset = &offset_eof;
1091 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1093 off_t off = offset->QuadPart;
1095 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1097 struct stat st;
1099 if (fstat( unix_handle, &st ) == -1)
1101 status = FILE_GetNtStatus();
1102 goto done;
1104 off = st.st_size;
1106 else if (offset->QuadPart < 0)
1108 status = STATUS_INVALID_PARAMETER;
1109 goto done;
1112 /* async I/O doesn't make sense on regular files */
1113 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1115 if (errno != EINTR)
1117 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1118 else status = FILE_GetNtStatus();
1119 goto done;
1123 if (!async_write)
1124 /* update file pointer position */
1125 lseek( unix_handle, off + result, SEEK_SET );
1127 total = result;
1128 status = STATUS_SUCCESS;
1129 goto done;
1132 else if (type == FD_TYPE_SERIAL)
1134 if (async_write &&
1135 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1137 status = STATUS_INVALID_PARAMETER;
1138 goto done;
1142 for (;;)
1144 /* zero-length writes on sockets may not work with plain write(2) */
1145 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1146 result = send( unix_handle, buffer, 0, 0 );
1147 else
1148 result = write( unix_handle, (const char *)buffer + total, length - total );
1150 if (result >= 0)
1152 total += result;
1153 if (total == length)
1155 status = STATUS_SUCCESS;
1156 goto done;
1158 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1160 else if (errno != EAGAIN)
1162 if (errno == EINTR) continue;
1163 if (!total)
1165 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1166 else status = FILE_GetNtStatus();
1168 goto done;
1171 if (async_write)
1173 struct async_fileio_write *fileio;
1175 fileio = (struct async_fileio_write *)alloc_fileio( sizeof(*fileio), hFile, apc, apc_user );
1176 if (!fileio)
1178 status = STATUS_NO_MEMORY;
1179 goto err;
1181 fileio->already = total;
1182 fileio->count = length;
1183 fileio->buffer = buffer;
1185 SERVER_START_REQ( register_async )
1187 req->type = ASYNC_TYPE_WRITE;
1188 req->count = length;
1189 req->async.handle = wine_server_obj_handle( hFile );
1190 req->async.event = wine_server_obj_handle( hEvent );
1191 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1192 req->async.iosb = wine_server_client_ptr( io_status );
1193 req->async.arg = wine_server_client_ptr( fileio );
1194 req->async.cvalue = cvalue;
1195 status = wine_server_call( req );
1197 SERVER_END_REQ;
1199 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1200 goto err;
1202 else /* synchronous write, wait for the fd to become ready */
1204 struct pollfd pfd;
1205 int ret, timeout;
1207 if (!timeout_init_done)
1209 timeout_init_done = TRUE;
1210 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1211 goto err;
1212 if (hEvent) NtResetEvent( hEvent, NULL );
1214 timeout = get_next_io_timeout( &timeouts, total );
1216 pfd.fd = unix_handle;
1217 pfd.events = POLLOUT;
1219 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1221 /* return with what we got so far */
1222 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1223 goto done;
1225 if (ret == -1 && errno != EINTR)
1227 status = FILE_GetNtStatus();
1228 goto done;
1230 /* will now restart the write */
1234 done:
1235 send_completion = cvalue != 0;
1237 err:
1238 if (needs_close) close( unix_handle );
1240 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1241 set_pending_write( hFile );
1243 if (status == STATUS_SUCCESS)
1245 io_status->u.Status = status;
1246 io_status->Information = total;
1247 TRACE("= SUCCESS (%u)\n", total);
1248 if (hEvent) NtSetEvent( hEvent, NULL );
1249 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1250 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1252 else
1254 TRACE("= 0x%08x\n", status);
1255 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1258 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1260 return status;
1264 /******************************************************************************
1265 * NtWriteFileGather [NTDLL.@]
1266 * ZwWriteFileGather [NTDLL.@]
1268 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1269 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1270 ULONG length, PLARGE_INTEGER offset, PULONG key )
1272 int result, unix_handle, needs_close;
1273 unsigned int options;
1274 NTSTATUS status;
1275 ULONG pos = 0, total = 0;
1276 enum server_fd_type type;
1277 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1278 BOOL send_completion = FALSE;
1280 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1281 file, event, apc, apc_user, io_status, segments, length, offset, key);
1283 if (length % page_size) return STATUS_INVALID_PARAMETER;
1284 if (!io_status) return STATUS_ACCESS_VIOLATION;
1286 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1287 &needs_close, &type, &options );
1288 if (status) return status;
1290 if ((type != FD_TYPE_FILE) ||
1291 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1292 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1294 status = STATUS_INVALID_PARAMETER;
1295 goto error;
1298 while (length)
1300 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1301 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1302 page_size - pos, offset->QuadPart + total );
1303 else
1304 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1306 if (result == -1)
1308 if (errno == EINTR) continue;
1309 if (errno == EFAULT)
1311 status = STATUS_INVALID_USER_BUFFER;
1312 goto error;
1314 status = FILE_GetNtStatus();
1315 break;
1317 if (!result)
1319 status = STATUS_DISK_FULL;
1320 break;
1322 total += result;
1323 length -= result;
1324 if ((pos += result) == page_size)
1326 pos = 0;
1327 segments++;
1331 send_completion = cvalue != 0;
1333 error:
1334 if (needs_close) close( unix_handle );
1335 if (status == STATUS_SUCCESS)
1337 io_status->u.Status = status;
1338 io_status->Information = total;
1339 TRACE("= SUCCESS (%u)\n", total);
1340 if (event) NtSetEvent( event, NULL );
1341 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1342 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1344 else
1346 TRACE("= 0x%08x\n", status);
1347 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1350 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1352 return status;
1356 struct async_ioctl
1358 struct async_fileio io;
1359 HANDLE event; /* async event */
1360 void *buffer; /* buffer for output */
1361 ULONG size; /* size of buffer */
1364 /* callback for ioctl async I/O completion */
1365 static NTSTATUS ioctl_completion( void *user, IO_STATUS_BLOCK *io,
1366 NTSTATUS status, void **apc, void **arg )
1368 struct async_ioctl *async = user;
1370 if (status == STATUS_ALERTED)
1372 SERVER_START_REQ( get_ioctl_result )
1374 req->handle = wine_server_obj_handle( async->io.handle );
1375 req->user_arg = wine_server_client_ptr( async );
1376 wine_server_set_reply( req, async->buffer, async->size );
1377 status = wine_server_call( req );
1378 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1380 SERVER_END_REQ;
1382 if (status != STATUS_PENDING)
1384 io->u.Status = status;
1385 *apc = async->io.apc;
1386 *arg = async->io.apc_arg;
1387 release_fileio( &async->io );
1389 return status;
1392 /* do an ioctl call through the server */
1393 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1394 PIO_APC_ROUTINE apc, PVOID apc_context,
1395 IO_STATUS_BLOCK *io, ULONG code,
1396 const void *in_buffer, ULONG in_size,
1397 PVOID out_buffer, ULONG out_size )
1399 struct async_ioctl *async;
1400 NTSTATUS status;
1401 HANDLE wait_handle;
1402 ULONG options;
1403 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1405 if (!(async = (struct async_ioctl *)alloc_fileio( sizeof(*async), handle, apc, apc_context )))
1406 return STATUS_NO_MEMORY;
1407 async->event = event;
1408 async->buffer = out_buffer;
1409 async->size = out_size;
1411 SERVER_START_REQ( ioctl )
1413 req->code = code;
1414 req->blocking = !apc && !event && !cvalue;
1415 req->async.handle = wine_server_obj_handle( handle );
1416 req->async.callback = wine_server_client_ptr( ioctl_completion );
1417 req->async.iosb = wine_server_client_ptr( io );
1418 req->async.arg = wine_server_client_ptr( async );
1419 req->async.event = wine_server_obj_handle( event );
1420 req->async.cvalue = cvalue;
1421 wine_server_add_data( req, in_buffer, in_size );
1422 wine_server_set_reply( req, out_buffer, out_size );
1423 status = wine_server_call( req );
1424 wait_handle = wine_server_ptr_handle( reply->wait );
1425 options = reply->options;
1426 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1428 SERVER_END_REQ;
1430 if (status == STATUS_NOT_SUPPORTED)
1431 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1432 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1434 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1436 if (wait_handle)
1438 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1439 status = io->u.Status;
1440 NtClose( wait_handle );
1443 return status;
1446 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1447 * server */
1448 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1449 ULONG in_size)
1451 #ifdef VALGRIND_MAKE_MEM_DEFINED
1452 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1453 do { \
1454 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1455 if ((size) >= FIELD_OFFSET(t, f2)) \
1456 VALGRIND_MAKE_MEM_DEFINED( \
1457 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1458 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1459 } while (0)
1461 switch (code)
1463 case FSCTL_PIPE_WAIT:
1464 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1465 break;
1467 #endif
1471 /**************************************************************************
1472 * NtDeviceIoControlFile [NTDLL.@]
1473 * ZwDeviceIoControlFile [NTDLL.@]
1475 * Perform an I/O control operation on an open file handle.
1477 * PARAMS
1478 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1479 * event [I] Event to signal upon completion (or NULL)
1480 * apc [I] Callback to call upon completion (or NULL)
1481 * apc_context [I] Context for ApcRoutine (or NULL)
1482 * io [O] Receives information about the operation on return
1483 * code [I] Control code for the operation to perform
1484 * in_buffer [I] Source for any input data required (or NULL)
1485 * in_size [I] Size of InputBuffer
1486 * out_buffer [O] Source for any output data returned (or NULL)
1487 * out_size [I] Size of OutputBuffer
1489 * RETURNS
1490 * Success: 0. IoStatusBlock is updated.
1491 * Failure: An NTSTATUS error code describing the error.
1493 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1494 PIO_APC_ROUTINE apc, PVOID apc_context,
1495 PIO_STATUS_BLOCK io, ULONG code,
1496 PVOID in_buffer, ULONG in_size,
1497 PVOID out_buffer, ULONG out_size)
1499 ULONG device = (code >> 16);
1500 NTSTATUS status = STATUS_NOT_SUPPORTED;
1502 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1503 handle, event, apc, apc_context, io, code,
1504 in_buffer, in_size, out_buffer, out_size);
1506 switch(device)
1508 case FILE_DEVICE_DISK:
1509 case FILE_DEVICE_CD_ROM:
1510 case FILE_DEVICE_DVD:
1511 case FILE_DEVICE_CONTROLLER:
1512 case FILE_DEVICE_MASS_STORAGE:
1513 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1514 in_buffer, in_size, out_buffer, out_size);
1515 break;
1516 case FILE_DEVICE_SERIAL_PORT:
1517 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1518 in_buffer, in_size, out_buffer, out_size);
1519 break;
1520 case FILE_DEVICE_TAPE:
1521 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1522 in_buffer, in_size, out_buffer, out_size);
1523 break;
1526 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1527 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1528 in_buffer, in_size, out_buffer, out_size );
1530 if (status != STATUS_PENDING) io->u.Status = status;
1531 return status;
1535 /**************************************************************************
1536 * NtFsControlFile [NTDLL.@]
1537 * ZwFsControlFile [NTDLL.@]
1539 * Perform a file system control operation on an open file handle.
1541 * PARAMS
1542 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1543 * event [I] Event to signal upon completion (or NULL)
1544 * apc [I] Callback to call upon completion (or NULL)
1545 * apc_context [I] Context for ApcRoutine (or NULL)
1546 * io [O] Receives information about the operation on return
1547 * code [I] Control code for the operation to perform
1548 * in_buffer [I] Source for any input data required (or NULL)
1549 * in_size [I] Size of InputBuffer
1550 * out_buffer [O] Source for any output data returned (or NULL)
1551 * out_size [I] Size of OutputBuffer
1553 * RETURNS
1554 * Success: 0. IoStatusBlock is updated.
1555 * Failure: An NTSTATUS error code describing the error.
1557 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1558 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1559 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1561 NTSTATUS status;
1563 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1564 handle, event, apc, apc_context, io, code,
1565 in_buffer, in_size, out_buffer, out_size);
1567 if (!io) return STATUS_INVALID_PARAMETER;
1569 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1571 switch(code)
1573 case FSCTL_DISMOUNT_VOLUME:
1574 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1575 in_buffer, in_size, out_buffer, out_size );
1576 if (!status) status = DIR_unmount_device( handle );
1577 break;
1579 case FSCTL_PIPE_PEEK:
1581 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1582 int avail = 0, fd, needs_close;
1584 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1586 status = STATUS_INFO_LENGTH_MISMATCH;
1587 break;
1590 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1591 break;
1593 #ifdef FIONREAD
1594 if (ioctl( fd, FIONREAD, &avail ) != 0)
1596 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1597 if (needs_close) close( fd );
1598 status = FILE_GetNtStatus();
1599 break;
1601 #endif
1602 if (!avail) /* check for closed pipe */
1604 struct pollfd pollfd;
1605 int ret;
1607 pollfd.fd = fd;
1608 pollfd.events = POLLIN;
1609 pollfd.revents = 0;
1610 ret = poll( &pollfd, 1, 0 );
1611 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1613 if (needs_close) close( fd );
1614 status = STATUS_PIPE_BROKEN;
1615 break;
1618 buffer->NamedPipeState = 0; /* FIXME */
1619 buffer->ReadDataAvailable = avail;
1620 buffer->NumberOfMessages = 0; /* FIXME */
1621 buffer->MessageLength = 0; /* FIXME */
1622 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1623 status = STATUS_SUCCESS;
1624 if (avail)
1626 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1627 if (data_size)
1629 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1630 if (res >= 0) io->Information += res;
1633 if (needs_close) close( fd );
1635 break;
1637 case FSCTL_PIPE_DISCONNECT:
1638 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1639 in_buffer, in_size, out_buffer, out_size );
1640 if (!status)
1642 int fd = server_remove_fd_from_cache( handle );
1643 if (fd != -1) close( fd );
1645 break;
1647 case FSCTL_PIPE_IMPERSONATE:
1648 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1649 status = RtlImpersonateSelf( SecurityImpersonation );
1650 break;
1652 case FSCTL_IS_VOLUME_MOUNTED:
1653 case FSCTL_LOCK_VOLUME:
1654 case FSCTL_UNLOCK_VOLUME:
1655 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1656 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1657 status = STATUS_SUCCESS;
1658 break;
1660 case FSCTL_GET_RETRIEVAL_POINTERS:
1662 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1664 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1666 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1668 buffer->ExtentCount = 1;
1669 buffer->StartingVcn.QuadPart = 1;
1670 buffer->Extents[0].NextVcn.QuadPart = 0;
1671 buffer->Extents[0].Lcn.QuadPart = 0;
1672 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1673 status = STATUS_SUCCESS;
1675 else
1677 io->Information = 0;
1678 status = STATUS_BUFFER_TOO_SMALL;
1680 break;
1682 case FSCTL_PIPE_LISTEN:
1683 case FSCTL_PIPE_WAIT:
1684 default:
1685 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1686 in_buffer, in_size, out_buffer, out_size );
1687 break;
1690 if (status != STATUS_PENDING) io->u.Status = status;
1691 return status;
1695 struct read_changes_fileio
1697 struct async_fileio io;
1698 void *buffer;
1699 ULONG buffer_size;
1700 ULONG data_size;
1701 char data[1];
1704 static NTSTATUS read_changes_apc( void *user, IO_STATUS_BLOCK *iosb,
1705 NTSTATUS status, void **apc, void **arg )
1707 struct read_changes_fileio *fileio = user;
1708 NTSTATUS ret;
1709 int size;
1711 SERVER_START_REQ( read_change )
1713 req->handle = wine_server_obj_handle( fileio->io.handle );
1714 wine_server_set_reply( req, fileio->data, fileio->data_size );
1715 ret = wine_server_call( req );
1716 size = wine_server_reply_size( reply );
1718 SERVER_END_REQ;
1720 if (ret == STATUS_SUCCESS && fileio->buffer)
1722 FILE_NOTIFY_INFORMATION *pfni = fileio->buffer;
1723 int i, left = fileio->buffer_size;
1724 DWORD *last_entry_offset = NULL;
1725 struct filesystem_event *event = (struct filesystem_event*)fileio->data;
1727 while (size && left >= sizeof(*pfni))
1729 /* convert to an NT style path */
1730 for (i=0; i<event->len; i++)
1731 if (event->name[i] == '/') event->name[i] = '\\';
1733 pfni->Action = event->action;
1734 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
1735 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
1736 last_entry_offset = &pfni->NextEntryOffset;
1738 if (pfni->FileNameLength == -1 || pfni->FileNameLength == -2) break;
1740 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
1741 pfni->FileNameLength *= sizeof(WCHAR);
1742 pfni->NextEntryOffset = i;
1743 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
1744 left -= i;
1746 i = (offsetof(struct filesystem_event, name[event->len])
1747 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1748 event = (struct filesystem_event*)((char*)event + i);
1749 size -= i;
1752 if (size)
1754 ret = STATUS_NOTIFY_ENUM_DIR;
1755 size = 0;
1757 else
1759 *last_entry_offset = 0;
1760 size = fileio->buffer_size - left;
1763 else
1765 ret = STATUS_NOTIFY_ENUM_DIR;
1766 size = 0;
1769 iosb->u.Status = ret;
1770 iosb->Information = size;
1771 *apc = fileio->io.apc;
1772 *arg = fileio->io.apc_arg;
1773 release_fileio( &fileio->io );
1774 return ret;
1777 #define FILE_NOTIFY_ALL ( \
1778 FILE_NOTIFY_CHANGE_FILE_NAME | \
1779 FILE_NOTIFY_CHANGE_DIR_NAME | \
1780 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1781 FILE_NOTIFY_CHANGE_SIZE | \
1782 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1783 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1784 FILE_NOTIFY_CHANGE_CREATION | \
1785 FILE_NOTIFY_CHANGE_SECURITY )
1787 /******************************************************************************
1788 * NtNotifyChangeDirectoryFile [NTDLL.@]
1790 NTSTATUS WINAPI NtNotifyChangeDirectoryFile( HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1791 void *apc_context, PIO_STATUS_BLOCK iosb, void *buffer,
1792 ULONG buffer_size, ULONG filter, BOOLEAN subtree )
1794 struct read_changes_fileio *fileio;
1795 NTSTATUS status;
1796 ULONG size = max( 4096, buffer_size );
1797 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1799 TRACE( "%p %p %p %p %p %p %u %u %d\n",
1800 handle, event, apc, apc_context, iosb, buffer, buffer_size, filter, subtree );
1802 if (!iosb) return STATUS_ACCESS_VIOLATION;
1803 if (filter == 0 || (filter & ~FILE_NOTIFY_ALL)) return STATUS_INVALID_PARAMETER;
1805 fileio = (struct read_changes_fileio *)alloc_fileio( offsetof(struct read_changes_fileio, data[size]),
1806 handle, apc, apc_context );
1807 if (!fileio) return STATUS_NO_MEMORY;
1809 fileio->buffer = buffer;
1810 fileio->buffer_size = buffer_size;
1811 fileio->data_size = size;
1813 SERVER_START_REQ( read_directory_changes )
1815 req->filter = filter;
1816 req->want_data = (buffer != NULL);
1817 req->subtree = subtree;
1818 req->async.handle = wine_server_obj_handle( handle );
1819 req->async.callback = wine_server_client_ptr( read_changes_apc );
1820 req->async.iosb = wine_server_client_ptr( iosb );
1821 req->async.arg = wine_server_client_ptr( fileio );
1822 req->async.event = wine_server_obj_handle( event );
1823 req->async.cvalue = cvalue;
1824 status = wine_server_call( req );
1826 SERVER_END_REQ;
1828 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1829 return status;
1832 /******************************************************************************
1833 * NtSetVolumeInformationFile [NTDLL.@]
1834 * ZwSetVolumeInformationFile [NTDLL.@]
1836 * Set volume information for an open file handle.
1838 * PARAMS
1839 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1840 * IoStatusBlock [O] Receives information about the operation on return
1841 * FsInformation [I] Source for volume information
1842 * Length [I] Size of FsInformation
1843 * FsInformationClass [I] Type of volume information to set
1845 * RETURNS
1846 * Success: 0. IoStatusBlock is updated.
1847 * Failure: An NTSTATUS error code describing the error.
1849 NTSTATUS WINAPI NtSetVolumeInformationFile(
1850 IN HANDLE FileHandle,
1851 PIO_STATUS_BLOCK IoStatusBlock,
1852 PVOID FsInformation,
1853 ULONG Length,
1854 FS_INFORMATION_CLASS FsInformationClass)
1856 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1857 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1858 return 0;
1861 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1862 static int futimens( int fd, const struct timespec spec[2] )
1864 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1866 #define HAVE_FUTIMENS
1867 #endif /* __ANDROID__ */
1869 #ifndef UTIME_OMIT
1870 #define UTIME_OMIT ((1 << 30) - 2)
1871 #endif
1873 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1875 NTSTATUS status = STATUS_SUCCESS;
1877 #ifdef HAVE_FUTIMENS
1878 struct timespec tv[2];
1880 tv[0].tv_sec = tv[1].tv_sec = 0;
1881 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1882 if (atime->QuadPart)
1884 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1885 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1887 if (mtime->QuadPart)
1889 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1890 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1892 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1894 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1895 struct timeval tv[2];
1896 struct stat st;
1898 if (!atime->QuadPart || !mtime->QuadPart)
1901 tv[0].tv_sec = tv[0].tv_usec = 0;
1902 tv[1].tv_sec = tv[1].tv_usec = 0;
1903 if (!fstat( fd, &st ))
1905 tv[0].tv_sec = st.st_atime;
1906 tv[1].tv_sec = st.st_mtime;
1907 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1908 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1909 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1910 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1911 #endif
1912 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1913 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1914 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1915 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1916 #endif
1919 if (atime->QuadPart)
1921 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1922 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1924 if (mtime->QuadPart)
1926 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1927 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1929 #ifdef HAVE_FUTIMES
1930 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1931 #elif defined(HAVE_FUTIMESAT)
1932 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1933 #endif
1935 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1936 FIXME( "setting file times not supported\n" );
1937 status = STATUS_NOT_IMPLEMENTED;
1938 #endif
1939 return status;
1942 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1943 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1945 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1946 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1947 RtlSecondsSince1970ToTime( st->st_atime, atime );
1948 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1949 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1950 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1951 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
1952 #endif
1953 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1954 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
1955 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1956 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
1957 #endif
1958 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1959 atime->QuadPart += st->st_atim.tv_nsec / 100;
1960 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1961 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
1962 #endif
1963 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1964 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
1965 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
1966 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
1967 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1968 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
1969 #endif
1970 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
1971 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
1972 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
1973 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
1974 #endif
1975 #else
1976 *creation = *mtime;
1977 #endif
1980 /* fill in the file information that depends on the stat and attribute info */
1981 NTSTATUS fill_file_info( const struct stat *st, ULONG attr, void *ptr,
1982 FILE_INFORMATION_CLASS class )
1984 switch (class)
1986 case FileBasicInformation:
1988 FILE_BASIC_INFORMATION *info = ptr;
1990 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1991 &info->LastAccessTime, &info->CreationTime );
1992 info->FileAttributes = attr;
1994 break;
1995 case FileStandardInformation:
1997 FILE_STANDARD_INFORMATION *info = ptr;
1999 if ((info->Directory = S_ISDIR(st->st_mode)))
2001 info->AllocationSize.QuadPart = 0;
2002 info->EndOfFile.QuadPart = 0;
2003 info->NumberOfLinks = 1;
2005 else
2007 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2008 info->EndOfFile.QuadPart = st->st_size;
2009 info->NumberOfLinks = st->st_nlink;
2012 break;
2013 case FileInternalInformation:
2015 FILE_INTERNAL_INFORMATION *info = ptr;
2016 info->IndexNumber.QuadPart = st->st_ino;
2018 break;
2019 case FileEndOfFileInformation:
2021 FILE_END_OF_FILE_INFORMATION *info = ptr;
2022 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
2024 break;
2025 case FileAllInformation:
2027 FILE_ALL_INFORMATION *info = ptr;
2028 fill_file_info( st, attr, &info->BasicInformation, FileBasicInformation );
2029 fill_file_info( st, attr, &info->StandardInformation, FileStandardInformation );
2030 fill_file_info( st, attr, &info->InternalInformation, FileInternalInformation );
2032 break;
2033 /* all directory structures start with the FileDirectoryInformation layout */
2034 case FileBothDirectoryInformation:
2035 case FileFullDirectoryInformation:
2036 case FileDirectoryInformation:
2038 FILE_DIRECTORY_INFORMATION *info = ptr;
2040 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
2041 &info->LastAccessTime, &info->CreationTime );
2042 if (S_ISDIR(st->st_mode))
2044 info->AllocationSize.QuadPart = 0;
2045 info->EndOfFile.QuadPart = 0;
2047 else
2049 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
2050 info->EndOfFile.QuadPart = st->st_size;
2052 info->FileAttributes = attr;
2054 break;
2055 case FileIdFullDirectoryInformation:
2057 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
2058 info->FileId.QuadPart = st->st_ino;
2059 fill_file_info( st, attr, info, FileDirectoryInformation );
2061 break;
2062 case FileIdBothDirectoryInformation:
2064 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
2065 info->FileId.QuadPart = st->st_ino;
2066 fill_file_info( st, attr, info, FileDirectoryInformation );
2068 break;
2070 default:
2071 return STATUS_INVALID_INFO_CLASS;
2073 return STATUS_SUCCESS;
2076 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
2078 data_size_t size = 1024;
2079 NTSTATUS ret;
2080 char *name;
2082 for (;;)
2084 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
2085 if (!name) return STATUS_NO_MEMORY;
2086 unix_name->MaximumLength = size + 1;
2088 SERVER_START_REQ( get_handle_unix_name )
2090 req->handle = wine_server_obj_handle( handle );
2091 wine_server_set_reply( req, name, size );
2092 ret = wine_server_call( req );
2093 size = reply->name_len;
2095 SERVER_END_REQ;
2097 if (!ret)
2099 name[size] = 0;
2100 unix_name->Buffer = name;
2101 unix_name->Length = size;
2102 break;
2104 RtlFreeHeap( GetProcessHeap(), 0, name );
2105 if (ret != STATUS_BUFFER_OVERFLOW) break;
2107 return ret;
2110 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
2112 UNICODE_STRING nt_name;
2113 NTSTATUS status;
2115 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
2117 const WCHAR *ptr = nt_name.Buffer;
2118 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
2120 /* Skip the volume mount point. */
2121 while (ptr != end && *ptr == '\\') ++ptr;
2122 while (ptr != end && *ptr != '\\') ++ptr;
2123 while (ptr != end && *ptr == '\\') ++ptr;
2124 while (ptr != end && *ptr != '\\') ++ptr;
2126 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
2127 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
2128 else *name_len = info->FileNameLength;
2130 memcpy( info->FileName, ptr, *name_len );
2131 RtlFreeUnicodeString( &nt_name );
2134 return status;
2137 /******************************************************************************
2138 * NtQueryInformationFile [NTDLL.@]
2139 * ZwQueryInformationFile [NTDLL.@]
2141 * Get information about an open file handle.
2143 * PARAMS
2144 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2145 * io [O] Receives information about the operation on return
2146 * ptr [O] Destination for file information
2147 * len [I] Size of FileInformation
2148 * class [I] Type of file information to get
2150 * RETURNS
2151 * Success: 0. IoStatusBlock and FileInformation are updated.
2152 * Failure: An NTSTATUS error code describing the error.
2154 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
2155 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
2157 static const size_t info_sizes[] =
2160 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
2161 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
2162 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
2163 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
2164 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
2165 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
2166 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
2167 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
2168 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
2169 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
2170 0, /* FileLinkInformation */
2171 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
2172 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
2173 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
2174 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
2175 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
2176 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
2177 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
2178 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
2179 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
2180 0, /* FileAlternateNameInformation */
2181 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
2182 sizeof(FILE_PIPE_INFORMATION), /* FilePipeInformation */
2183 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
2184 0, /* FilePipeRemoteInformation */
2185 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
2186 0, /* FileMailslotSetInformation */
2187 0, /* FileCompressionInformation */
2188 0, /* FileObjectIdInformation */
2189 0, /* FileCompletionInformation */
2190 0, /* FileMoveClusterInformation */
2191 0, /* FileQuotaInformation */
2192 0, /* FileReparsePointInformation */
2193 0, /* FileNetworkOpenInformation */
2194 0, /* FileAttributeTagInformation */
2195 0, /* FileTrackingInformation */
2196 0, /* FileIdBothDirectoryInformation */
2197 0, /* FileIdFullDirectoryInformation */
2198 0, /* FileValidDataLengthInformation */
2199 0, /* FileShortNameInformation */
2203 0, /* FileSfioReserveInformation */
2204 0, /* FileSfioVolumeInformation */
2205 0, /* FileHardLinkInformation */
2207 0, /* FileNormalizedNameInformation */
2209 0, /* FileIdGlobalTxDirectoryInformation */
2213 0 /* FileStandardLinkInformation */
2216 struct stat st;
2217 int fd, needs_close = FALSE;
2218 ULONG attr;
2220 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2222 io->Information = 0;
2224 if (class <= 0 || class >= FileMaximumInformation)
2225 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2226 if (!info_sizes[class])
2228 FIXME("Unsupported class (%d)\n", class);
2229 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2231 if (len < info_sizes[class])
2232 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2234 if (class != FilePipeInformation && class != FilePipeLocalInformation)
2236 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2237 return io->u.Status;
2240 switch (class)
2242 case FileBasicInformation:
2243 if (fd_get_file_info( fd, &st, &attr ) == -1)
2244 io->u.Status = FILE_GetNtStatus();
2245 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2246 io->u.Status = STATUS_INVALID_INFO_CLASS;
2247 else
2248 fill_file_info( &st, attr, ptr, class );
2249 break;
2250 case FileStandardInformation:
2252 FILE_STANDARD_INFORMATION *info = ptr;
2254 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2255 else
2257 fill_file_info( &st, attr, info, class );
2258 info->DeletePending = FALSE; /* FIXME */
2261 break;
2262 case FilePositionInformation:
2264 FILE_POSITION_INFORMATION *info = ptr;
2265 off_t res = lseek( fd, 0, SEEK_CUR );
2266 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2267 else info->CurrentByteOffset.QuadPart = res;
2269 break;
2270 case FileInternalInformation:
2271 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2272 else fill_file_info( &st, attr, ptr, class );
2273 break;
2274 case FileEaInformation:
2276 FILE_EA_INFORMATION *info = ptr;
2277 info->EaSize = 0;
2279 break;
2280 case FileEndOfFileInformation:
2281 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2282 else fill_file_info( &st, attr, ptr, class );
2283 break;
2284 case FileAllInformation:
2286 FILE_ALL_INFORMATION *info = ptr;
2287 ANSI_STRING unix_name;
2289 if (fd_get_file_info( fd, &st, &attr ) == -1) io->u.Status = FILE_GetNtStatus();
2290 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2291 io->u.Status = STATUS_INVALID_INFO_CLASS;
2292 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2294 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2296 fill_file_info( &st, attr, info, FileAllInformation );
2297 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2298 info->EaInformation.EaSize = 0;
2299 info->AccessInformation.AccessFlags = 0; /* FIXME */
2300 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2301 info->ModeInformation.Mode = 0; /* FIXME */
2302 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2304 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2305 RtlFreeAnsiString( &unix_name );
2306 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2309 break;
2310 case FileMailslotQueryInformation:
2312 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2314 SERVER_START_REQ( set_mailslot_info )
2316 req->handle = wine_server_obj_handle( hFile );
2317 req->flags = 0;
2318 io->u.Status = wine_server_call( req );
2319 if( io->u.Status == STATUS_SUCCESS )
2321 info->MaximumMessageSize = reply->max_msgsize;
2322 info->MailslotQuota = 0;
2323 info->NextMessageSize = 0;
2324 info->MessagesAvailable = 0;
2325 info->ReadTimeout.QuadPart = reply->read_timeout;
2328 SERVER_END_REQ;
2329 if (!io->u.Status)
2331 char *tmpbuf;
2332 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2333 if (size > 0x10000) size = 0x10000;
2334 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2336 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2338 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2339 info->MessagesAvailable = (res > 0);
2340 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2341 if (needs_close) close( fd );
2343 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2347 break;
2348 case FilePipeInformation:
2350 FILE_PIPE_INFORMATION* pi = ptr;
2352 SERVER_START_REQ( get_named_pipe_info )
2354 req->handle = wine_server_obj_handle( hFile );
2355 if (!(io->u.Status = wine_server_call( req )))
2357 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2358 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2359 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2360 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2363 SERVER_END_REQ;
2365 break;
2366 case FilePipeLocalInformation:
2368 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2370 SERVER_START_REQ( get_named_pipe_info )
2372 req->handle = wine_server_obj_handle( hFile );
2373 if (!(io->u.Status = wine_server_call( req )))
2375 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2376 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2377 switch (reply->sharing)
2379 case FILE_SHARE_READ:
2380 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2381 break;
2382 case FILE_SHARE_WRITE:
2383 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2384 break;
2385 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2386 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2387 break;
2389 pli->MaximumInstances = reply->maxinstances;
2390 pli->CurrentInstances = reply->instances;
2391 pli->InboundQuota = reply->insize;
2392 pli->ReadDataAvailable = 0; /* FIXME */
2393 pli->OutboundQuota = reply->outsize;
2394 pli->WriteQuotaAvailable = 0; /* FIXME */
2395 pli->NamedPipeState = 0; /* FIXME */
2396 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2397 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2400 SERVER_END_REQ;
2402 break;
2403 case FileNameInformation:
2405 FILE_NAME_INFORMATION *info = ptr;
2406 ANSI_STRING unix_name;
2408 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2410 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2411 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2412 RtlFreeAnsiString( &unix_name );
2413 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2416 break;
2417 default:
2418 FIXME("Unsupported class (%d)\n", class);
2419 io->u.Status = STATUS_NOT_IMPLEMENTED;
2420 break;
2422 if (needs_close) close( fd );
2423 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2424 return io->u.Status;
2427 /******************************************************************************
2428 * NtSetInformationFile [NTDLL.@]
2429 * ZwSetInformationFile [NTDLL.@]
2431 * Set information about an open file handle.
2433 * PARAMS
2434 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2435 * io [O] Receives information about the operation on return
2436 * ptr [I] Source for file information
2437 * len [I] Size of FileInformation
2438 * class [I] Type of file information to set
2440 * RETURNS
2441 * Success: 0. io is updated.
2442 * Failure: An NTSTATUS error code describing the error.
2444 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2445 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2447 int fd, needs_close;
2449 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2451 io->u.Status = STATUS_SUCCESS;
2452 switch (class)
2454 case FileBasicInformation:
2455 if (len >= sizeof(FILE_BASIC_INFORMATION))
2457 struct stat st;
2458 const FILE_BASIC_INFORMATION *info = ptr;
2460 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2461 return io->u.Status;
2463 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2464 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2466 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2468 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2469 else
2471 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2473 if (S_ISDIR( st.st_mode))
2474 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2475 else
2476 st.st_mode &= ~0222; /* clear write permission bits */
2478 else
2480 /* add write permission only where we already have read permission */
2481 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2483 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2487 if (needs_close) close( fd );
2489 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2490 break;
2492 case FilePositionInformation:
2493 if (len >= sizeof(FILE_POSITION_INFORMATION))
2495 const FILE_POSITION_INFORMATION *info = ptr;
2497 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2498 return io->u.Status;
2500 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2501 io->u.Status = FILE_GetNtStatus();
2503 if (needs_close) close( fd );
2505 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2506 break;
2508 case FileEndOfFileInformation:
2509 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2511 struct stat st;
2512 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2514 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2515 return io->u.Status;
2517 /* first try normal truncate */
2518 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2520 /* now check for the need to extend the file */
2521 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2523 static const char zero;
2525 /* extend the file one byte beyond the requested size and then truncate it */
2526 /* this should work around ftruncate implementations that can't extend files */
2527 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2528 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2530 io->u.Status = FILE_GetNtStatus();
2532 if (needs_close) close( fd );
2534 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2535 break;
2537 case FilePipeInformation:
2538 if (len >= sizeof(FILE_PIPE_INFORMATION))
2540 FILE_PIPE_INFORMATION *info = ptr;
2542 if ((info->CompletionMode | info->ReadMode) & ~1)
2544 io->u.Status = STATUS_INVALID_PARAMETER;
2545 break;
2548 SERVER_START_REQ( set_named_pipe_info )
2550 req->handle = wine_server_obj_handle( handle );
2551 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2552 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2553 io->u.Status = wine_server_call( req );
2555 SERVER_END_REQ;
2557 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2558 break;
2560 case FileMailslotSetInformation:
2562 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2564 SERVER_START_REQ( set_mailslot_info )
2566 req->handle = wine_server_obj_handle( handle );
2567 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2568 req->read_timeout = info->ReadTimeout.QuadPart;
2569 io->u.Status = wine_server_call( req );
2571 SERVER_END_REQ;
2573 break;
2575 case FileCompletionInformation:
2576 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2578 FILE_COMPLETION_INFORMATION *info = ptr;
2580 SERVER_START_REQ( set_completion_info )
2582 req->handle = wine_server_obj_handle( handle );
2583 req->chandle = wine_server_obj_handle( info->CompletionPort );
2584 req->ckey = info->CompletionKey;
2585 io->u.Status = wine_server_call( req );
2587 SERVER_END_REQ;
2588 } else
2589 io->u.Status = STATUS_INVALID_PARAMETER_3;
2590 break;
2592 case FileAllInformation:
2593 io->u.Status = STATUS_INVALID_INFO_CLASS;
2594 break;
2596 case FileValidDataLengthInformation:
2597 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2599 struct stat st;
2600 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2602 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2603 return io->u.Status;
2605 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2606 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2607 io->u.Status = STATUS_INVALID_PARAMETER;
2608 else
2610 #ifdef HAVE_FALLOCATE
2611 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2613 NTSTATUS status = FILE_GetNtStatus();
2614 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2615 else io->u.Status = status;
2617 #else
2618 FIXME( "setting valid data length not supported\n" );
2619 #endif
2621 if (needs_close) close( fd );
2623 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2624 break;
2626 default:
2627 FIXME("Unsupported class (%d)\n", class);
2628 io->u.Status = STATUS_NOT_IMPLEMENTED;
2629 break;
2631 io->Information = 0;
2632 return io->u.Status;
2636 /******************************************************************************
2637 * NtQueryFullAttributesFile (NTDLL.@)
2639 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2640 FILE_NETWORK_OPEN_INFORMATION *info )
2642 ANSI_STRING unix_name;
2643 NTSTATUS status;
2645 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2647 ULONG attributes;
2648 struct stat st;
2650 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2651 status = FILE_GetNtStatus();
2652 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2653 status = STATUS_INVALID_INFO_CLASS;
2654 else
2656 FILE_BASIC_INFORMATION basic;
2657 FILE_STANDARD_INFORMATION std;
2659 fill_file_info( &st, attributes, &basic, FileBasicInformation );
2660 fill_file_info( &st, attributes, &std, FileStandardInformation );
2662 info->CreationTime = basic.CreationTime;
2663 info->LastAccessTime = basic.LastAccessTime;
2664 info->LastWriteTime = basic.LastWriteTime;
2665 info->ChangeTime = basic.ChangeTime;
2666 info->AllocationSize = std.AllocationSize;
2667 info->EndOfFile = std.EndOfFile;
2668 info->FileAttributes = basic.FileAttributes;
2669 if (DIR_is_hidden_file( attr->ObjectName ))
2670 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2672 RtlFreeAnsiString( &unix_name );
2674 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2675 return status;
2679 /******************************************************************************
2680 * NtQueryAttributesFile (NTDLL.@)
2681 * ZwQueryAttributesFile (NTDLL.@)
2683 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2685 ANSI_STRING unix_name;
2686 NTSTATUS status;
2688 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2690 ULONG attributes;
2691 struct stat st;
2693 if (get_file_info( unix_name.Buffer, &st, &attributes ) == -1)
2694 status = FILE_GetNtStatus();
2695 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2696 status = STATUS_INVALID_INFO_CLASS;
2697 else
2699 status = fill_file_info( &st, attributes, info, FileBasicInformation );
2700 if (DIR_is_hidden_file( attr->ObjectName ))
2701 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2703 RtlFreeAnsiString( &unix_name );
2705 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2706 return status;
2710 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2711 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2712 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2713 unsigned int flags )
2715 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2717 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2718 /* Don't assume read-only, let the mount options set it below */
2719 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2721 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2722 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2724 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2725 info->Characteristics |= FILE_REMOTE_DEVICE;
2727 else if (!strcmp("procfs", fstypename))
2728 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2729 else
2730 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2732 if (flags & MNT_RDONLY)
2733 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2735 if (!(flags & MNT_LOCAL))
2737 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2738 info->Characteristics |= FILE_REMOTE_DEVICE;
2741 #endif
2743 static inline BOOL is_device_placeholder( int fd )
2745 static const char wine_placeholder[] = "Wine device placeholder";
2746 char buffer[sizeof(wine_placeholder)-1];
2748 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2749 return FALSE;
2750 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2753 /******************************************************************************
2754 * get_device_info
2756 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2758 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2760 struct stat st;
2762 info->Characteristics = 0;
2763 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2764 if (S_ISCHR( st.st_mode ))
2766 info->DeviceType = FILE_DEVICE_UNKNOWN;
2767 #ifdef linux
2768 switch(major(st.st_rdev))
2770 case MEM_MAJOR:
2771 info->DeviceType = FILE_DEVICE_NULL;
2772 break;
2773 case TTY_MAJOR:
2774 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2775 break;
2776 case LP_MAJOR:
2777 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2778 break;
2779 case SCSI_TAPE_MAJOR:
2780 info->DeviceType = FILE_DEVICE_TAPE;
2781 break;
2783 #endif
2785 else if (S_ISBLK( st.st_mode ))
2787 info->DeviceType = FILE_DEVICE_DISK;
2789 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2791 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2793 else if (is_device_placeholder( fd ))
2795 info->DeviceType = FILE_DEVICE_DISK;
2797 else /* regular file or directory */
2799 #if defined(linux) && defined(HAVE_FSTATFS)
2800 struct statfs stfs;
2802 /* check for floppy disk */
2803 if (major(st.st_dev) == FLOPPY_MAJOR)
2804 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2806 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2807 switch (stfs.f_type)
2809 case 0x9660: /* iso9660 */
2810 case 0x9fa1: /* supermount */
2811 case 0x15013346: /* udf */
2812 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2813 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2814 break;
2815 case 0x6969: /* nfs */
2816 case 0x517B: /* smbfs */
2817 case 0x564c: /* ncpfs */
2818 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2819 info->Characteristics |= FILE_REMOTE_DEVICE;
2820 break;
2821 case 0x01021994: /* tmpfs */
2822 case 0x28cd3d45: /* cramfs */
2823 case 0x1373: /* devfs */
2824 case 0x9fa0: /* procfs */
2825 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2826 break;
2827 default:
2828 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2829 break;
2831 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2832 struct statfs stfs;
2834 if (fstatfs( fd, &stfs ) < 0)
2835 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2836 else
2837 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2838 #elif defined(__NetBSD__)
2839 struct statvfs stfs;
2841 if (fstatvfs( fd, &stfs) < 0)
2842 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2843 else
2844 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2845 #elif defined(sun)
2846 /* Use dkio to work out device types */
2848 # include <sys/dkio.h>
2849 # include <sys/vtoc.h>
2850 struct dk_cinfo dkinf;
2851 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2852 if(retval==-1){
2853 WARN("Unable to get disk device type information - assuming a disk like device\n");
2854 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2856 switch (dkinf.dki_ctype)
2858 case DKC_CDROM:
2859 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2860 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2861 break;
2862 case DKC_NCRFLOPPY:
2863 case DKC_SMSFLOPPY:
2864 case DKC_INTEL82072:
2865 case DKC_INTEL82077:
2866 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2867 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2868 break;
2869 case DKC_MD:
2870 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2871 break;
2872 default:
2873 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2876 #else
2877 static int warned;
2878 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2879 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2880 #endif
2881 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2883 return STATUS_SUCCESS;
2887 /******************************************************************************
2888 * NtQueryVolumeInformationFile [NTDLL.@]
2889 * ZwQueryVolumeInformationFile [NTDLL.@]
2891 * Get volume information for an open file handle.
2893 * PARAMS
2894 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2895 * io [O] Receives information about the operation on return
2896 * buffer [O] Destination for volume information
2897 * length [I] Size of FsInformation
2898 * info_class [I] Type of volume information to set
2900 * RETURNS
2901 * Success: 0. io and buffer are updated.
2902 * Failure: An NTSTATUS error code describing the error.
2904 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2905 PVOID buffer, ULONG length,
2906 FS_INFORMATION_CLASS info_class )
2908 int fd, needs_close;
2909 struct stat st;
2910 static int once;
2912 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2913 return io->u.Status;
2915 io->u.Status = STATUS_NOT_IMPLEMENTED;
2916 io->Information = 0;
2918 switch( info_class )
2920 case FileFsVolumeInformation:
2921 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2922 break;
2923 case FileFsLabelInformation:
2924 FIXME( "%p: label info not supported\n", handle );
2925 break;
2926 case FileFsSizeInformation:
2927 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2928 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2929 else
2931 FILE_FS_SIZE_INFORMATION *info = buffer;
2933 if (fstat( fd, &st ) < 0)
2935 io->u.Status = FILE_GetNtStatus();
2936 break;
2938 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2940 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2942 else
2944 ULONGLONG bsize;
2945 /* Linux's fstatvfs is buggy */
2946 #if !defined(linux) || !defined(HAVE_FSTATFS)
2947 struct statvfs stfs;
2949 if (fstatvfs( fd, &stfs ) < 0)
2951 io->u.Status = FILE_GetNtStatus();
2952 break;
2954 bsize = stfs.f_frsize;
2955 #else
2956 struct statfs stfs;
2957 if (fstatfs( fd, &stfs ) < 0)
2959 io->u.Status = FILE_GetNtStatus();
2960 break;
2962 bsize = stfs.f_bsize;
2963 #endif
2964 if (bsize == 2048) /* assume CD-ROM */
2966 info->BytesPerSector = 2048;
2967 info->SectorsPerAllocationUnit = 1;
2969 else
2971 info->BytesPerSector = 512;
2972 info->SectorsPerAllocationUnit = 8;
2974 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2975 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2976 io->Information = sizeof(*info);
2977 io->u.Status = STATUS_SUCCESS;
2980 break;
2981 case FileFsDeviceInformation:
2982 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2983 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2984 else
2986 FILE_FS_DEVICE_INFORMATION *info = buffer;
2988 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2989 io->Information = sizeof(*info);
2991 break;
2992 case FileFsAttributeInformation:
2993 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
2994 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2995 else
2997 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
2999 FIXME( "%p: faking attribute info\n", handle );
3000 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
3001 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
3002 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
3003 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
3004 info->FileSystemNameLength = sizeof(ntfsW);
3005 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
3007 io->Information = sizeof(*info);
3008 io->u.Status = STATUS_SUCCESS;
3010 break;
3011 case FileFsControlInformation:
3012 FIXME( "%p: control info not supported\n", handle );
3013 break;
3014 case FileFsFullSizeInformation:
3015 FIXME( "%p: full size info not supported\n", handle );
3016 break;
3017 case FileFsObjectIdInformation:
3018 FIXME( "%p: object id info not supported\n", handle );
3019 break;
3020 case FileFsMaximumInformation:
3021 FIXME( "%p: maximum info not supported\n", handle );
3022 break;
3023 default:
3024 io->u.Status = STATUS_INVALID_PARAMETER;
3025 break;
3027 if (needs_close) close( fd );
3028 return io->u.Status;
3032 /******************************************************************
3033 * NtQueryEaFile (NTDLL.@)
3035 * Read extended attributes from NTFS files.
3037 * PARAMS
3038 * hFile [I] File handle, must be opened with FILE_READ_EA access
3039 * iosb [O] Receives information about the operation on return
3040 * buffer [O] Output buffer
3041 * length [I] Length of output buffer
3042 * single_entry [I] Only read and return one entry
3043 * ea_list [I] Optional list with names of EAs to return
3044 * ea_list_len [I] Length of ea_list in bytes
3045 * ea_index [I] Optional pointer to 1-based index of attribute to return
3046 * restart [I] restart EA scan
3048 * RETURNS
3049 * Success: 0. Atrributes read into buffer
3050 * Failure: An NTSTATUS error code describing the error.
3052 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
3053 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
3054 PULONG ea_index, BOOLEAN restart )
3056 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
3057 hFile, iosb, buffer, length, single_entry, ea_list,
3058 ea_list_len, ea_index, restart);
3059 return STATUS_ACCESS_DENIED;
3063 /******************************************************************
3064 * NtSetEaFile (NTDLL.@)
3066 * Update extended attributes for NTFS files.
3068 * PARAMS
3069 * hFile [I] File handle, must be opened with FILE_READ_EA access
3070 * iosb [O] Receives information about the operation on return
3071 * buffer [I] Buffer with EA information
3072 * length [I] Length of buffer
3074 * RETURNS
3075 * Success: 0. Attributes are updated
3076 * Failure: An NTSTATUS error code describing the error.
3078 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
3080 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
3081 return STATUS_ACCESS_DENIED;
3085 /******************************************************************
3086 * NtFlushBuffersFile (NTDLL.@)
3088 * Flush any buffered data on an open file handle.
3090 * PARAMS
3091 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
3092 * IoStatusBlock [O] Receives information about the operation on return
3094 * RETURNS
3095 * Success: 0. IoStatusBlock is updated.
3096 * Failure: An NTSTATUS error code describing the error.
3098 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
3100 NTSTATUS ret;
3101 HANDLE hEvent = NULL;
3102 enum server_fd_type type;
3103 int fd, needs_close;
3105 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
3107 if (!ret && type == FD_TYPE_SERIAL)
3109 ret = COMM_FlushBuffersFile( fd );
3111 else
3113 SERVER_START_REQ( flush_file )
3115 req->handle = wine_server_obj_handle( hFile );
3116 ret = wine_server_call( req );
3117 hEvent = wine_server_ptr_handle( reply->event );
3119 SERVER_END_REQ;
3120 if (!ret && hEvent)
3122 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
3123 NtClose( hEvent );
3127 if (needs_close) close( fd );
3128 return ret;
3131 /******************************************************************
3132 * NtLockFile (NTDLL.@)
3136 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
3137 PIO_APC_ROUTINE apc, void* apc_user,
3138 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
3139 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
3140 BOOLEAN exclusive )
3142 NTSTATUS ret;
3143 HANDLE handle;
3144 BOOLEAN async;
3145 static BOOLEAN warn = TRUE;
3147 if (apc || io_status || key)
3149 FIXME("Unimplemented yet parameter\n");
3150 return STATUS_NOT_IMPLEMENTED;
3153 if (apc_user && warn)
3155 FIXME("I/O completion on lock not implemented yet\n");
3156 warn = FALSE;
3159 for (;;)
3161 SERVER_START_REQ( lock_file )
3163 req->handle = wine_server_obj_handle( hFile );
3164 req->offset = offset->QuadPart;
3165 req->count = count->QuadPart;
3166 req->shared = !exclusive;
3167 req->wait = !dont_wait;
3168 ret = wine_server_call( req );
3169 handle = wine_server_ptr_handle( reply->handle );
3170 async = reply->overlapped;
3172 SERVER_END_REQ;
3173 if (ret != STATUS_PENDING)
3175 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
3176 return ret;
3179 if (async)
3181 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
3182 if (handle) NtClose( handle );
3183 return STATUS_PENDING;
3185 if (handle)
3187 NtWaitForSingleObject( handle, FALSE, NULL );
3188 NtClose( handle );
3190 else
3192 LARGE_INTEGER time;
3194 /* Unix lock conflict, sleep a bit and retry */
3195 time.QuadPart = 100 * (ULONGLONG)10000;
3196 time.QuadPart = -time.QuadPart;
3197 NtDelayExecution( FALSE, &time );
3203 /******************************************************************
3204 * NtUnlockFile (NTDLL.@)
3208 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3209 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3210 PULONG key )
3212 NTSTATUS status;
3214 TRACE( "%p %x%08x %x%08x\n",
3215 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3217 if (io_status || key)
3219 FIXME("Unimplemented yet parameter\n");
3220 return STATUS_NOT_IMPLEMENTED;
3223 SERVER_START_REQ( unlock_file )
3225 req->handle = wine_server_obj_handle( hFile );
3226 req->offset = offset->QuadPart;
3227 req->count = count->QuadPart;
3228 status = wine_server_call( req );
3230 SERVER_END_REQ;
3231 return status;
3234 /******************************************************************
3235 * NtCreateNamedPipeFile (NTDLL.@)
3239 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3240 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3241 ULONG sharing, ULONG dispo, ULONG options,
3242 ULONG pipe_type, ULONG read_mode,
3243 ULONG completion_mode, ULONG max_inst,
3244 ULONG inbound_quota, ULONG outbound_quota,
3245 PLARGE_INTEGER timeout)
3247 struct security_descriptor *sd = NULL;
3248 struct object_attributes objattr;
3249 NTSTATUS status;
3251 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3252 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3253 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3254 outbound_quota, timeout);
3256 /* assume we only get relative timeout */
3257 if (timeout->QuadPart > 0)
3258 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3260 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
3261 objattr.sd_len = 0;
3262 objattr.name_len = attr->ObjectName->Length;
3264 status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
3265 if (status != STATUS_SUCCESS) return status;
3267 SERVER_START_REQ( create_named_pipe )
3269 req->access = access;
3270 req->attributes = attr->Attributes;
3271 req->options = options;
3272 req->sharing = sharing;
3273 req->flags =
3274 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3275 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3276 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3277 req->maxinstances = max_inst;
3278 req->outsize = outbound_quota;
3279 req->insize = inbound_quota;
3280 req->timeout = timeout->QuadPart;
3281 wine_server_add_data( req, &objattr, sizeof(objattr) );
3282 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
3283 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
3284 status = wine_server_call( req );
3285 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3287 SERVER_END_REQ;
3289 NTDLL_free_struct_sd( sd );
3290 return status;
3293 /******************************************************************
3294 * NtDeleteFile (NTDLL.@)
3298 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3300 NTSTATUS status;
3301 HANDLE hFile;
3302 IO_STATUS_BLOCK io;
3304 TRACE("%p\n", ObjectAttributes);
3305 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3306 ObjectAttributes, &io, NULL, 0,
3307 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3308 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3309 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3310 return status;
3313 /******************************************************************
3314 * NtCancelIoFileEx (NTDLL.@)
3318 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3320 LARGE_INTEGER timeout;
3322 TRACE("%p %p %p\n", hFile, iosb, io_status );
3324 SERVER_START_REQ( cancel_async )
3326 req->handle = wine_server_obj_handle( hFile );
3327 req->iosb = wine_server_client_ptr( iosb );
3328 req->only_thread = FALSE;
3329 io_status->u.Status = wine_server_call( req );
3331 SERVER_END_REQ;
3332 if (io_status->u.Status)
3333 return io_status->u.Status;
3335 /* Let some APC be run, so that we can run the remaining APCs on hFile
3336 * either the cancelation of the pending one, but also the execution
3337 * of the queued APC, but not yet run. This is needed to ensure proper
3338 * clean-up of allocated data.
3340 timeout.QuadPart = 0;
3341 NtDelayExecution( TRUE, &timeout );
3342 return io_status->u.Status;
3345 /******************************************************************
3346 * NtCancelIoFile (NTDLL.@)
3350 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3352 LARGE_INTEGER timeout;
3354 TRACE("%p %p\n", hFile, io_status );
3356 SERVER_START_REQ( cancel_async )
3358 req->handle = wine_server_obj_handle( hFile );
3359 req->iosb = 0;
3360 req->only_thread = TRUE;
3361 io_status->u.Status = wine_server_call( req );
3363 SERVER_END_REQ;
3364 if (io_status->u.Status)
3365 return io_status->u.Status;
3367 /* Let some APC be run, so that we can run the remaining APCs on hFile
3368 * either the cancelation of the pending one, but also the execution
3369 * of the queued APC, but not yet run. This is needed to ensure proper
3370 * clean-up of allocated data.
3372 timeout.QuadPart = 0;
3373 NtDelayExecution( TRUE, &timeout );
3374 return io_status->u.Status;
3377 /******************************************************************************
3378 * NtCreateMailslotFile [NTDLL.@]
3379 * ZwCreateMailslotFile [NTDLL.@]
3381 * PARAMS
3382 * pHandle [O] pointer to receive the handle created
3383 * DesiredAccess [I] access mode (read, write, etc)
3384 * ObjectAttributes [I] fully qualified NT path of the mailslot
3385 * IoStatusBlock [O] receives completion status and other info
3386 * CreateOptions [I]
3387 * MailslotQuota [I]
3388 * MaxMessageSize [I]
3389 * TimeOut [I]
3391 * RETURNS
3392 * An NT status code
3394 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3395 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3396 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3397 PLARGE_INTEGER TimeOut)
3399 LARGE_INTEGER timeout;
3400 NTSTATUS ret;
3402 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3403 pHandle, DesiredAccess, attr, IoStatusBlock,
3404 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3406 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3407 if (!attr) return STATUS_INVALID_PARAMETER;
3408 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3411 * For a NULL TimeOut pointer set the default timeout value
3413 if (!TimeOut)
3414 timeout.QuadPart = -1;
3415 else
3416 timeout.QuadPart = TimeOut->QuadPart;
3418 SERVER_START_REQ( create_mailslot )
3420 req->access = DesiredAccess;
3421 req->attributes = attr->Attributes;
3422 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3423 req->max_msgsize = MaxMessageSize;
3424 req->read_timeout = timeout.QuadPart;
3425 wine_server_add_data( req, attr->ObjectName->Buffer,
3426 attr->ObjectName->Length );
3427 ret = wine_server_call( req );
3428 if( ret == STATUS_SUCCESS )
3429 *pHandle = wine_server_ptr_handle( reply->handle );
3431 SERVER_END_REQ;
3433 return ret;