ntdll: Add support for FILE_APPEND_DATA to NtWriteFile.
[wine.git] / dlls / ntdll / file.c
bloba8c25d60d30fb300f03dfd2cfdc1e4d61cf01ca0
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_TIME_H
40 # include <sys/time.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_SYS_FILIO_H
46 # include <sys/filio.h>
47 #endif
48 #ifdef HAVE_POLL_H
49 #include <poll.h>
50 #endif
51 #ifdef HAVE_SYS_POLL_H
52 #include <sys/poll.h>
53 #endif
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
56 #endif
57 #ifdef HAVE_UTIME_H
58 # include <utime.h>
59 #endif
60 #ifdef HAVE_SYS_VFS_H
61 # include <sys/vfs.h>
62 #endif
63 #ifdef HAVE_SYS_MOUNT_H
64 # include <sys/mount.h>
65 #endif
66 #ifdef HAVE_SYS_STATFS_H
67 # include <sys/statfs.h>
68 #endif
69 #ifdef HAVE_TERMIOS_H
70 #include <termios.h>
71 #endif
72 #ifdef HAVE_VALGRIND_MEMCHECK_H
73 # include <valgrind/memcheck.h>
74 #endif
76 #define NONAMELESSUNION
77 #define NONAMELESSSTRUCT
78 #include "ntstatus.h"
79 #define WIN32_NO_STATUS
80 #include "wine/unicode.h"
81 #include "wine/debug.h"
82 #include "wine/server.h"
83 #include "ntdll_misc.h"
85 #include "winternl.h"
86 #include "winioctl.h"
87 #include "ddk/ntddk.h"
88 #include "ddk/ntddser.h"
90 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
91 WINE_DECLARE_DEBUG_CHANNEL(winediag);
93 mode_t FILE_umask = 0;
95 #define SECSPERDAY 86400
96 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
98 static const WCHAR ntfsW[] = {'N','T','F','S'};
100 /**************************************************************************
101 * FILE_CreateFile (internal)
102 * Open a file.
104 * Parameter set fully identical with NtCreateFile
106 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
107 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
108 ULONG attributes, ULONG sharing, ULONG disposition,
109 ULONG options, PVOID ea_buffer, ULONG ea_length )
111 ANSI_STRING unix_name;
112 int created = FALSE;
114 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
115 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
116 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
117 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
118 attributes, sharing, disposition, options, ea_buffer, ea_length );
120 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
122 if (alloc_size) FIXME( "alloc_size not supported\n" );
124 if (options & FILE_OPEN_BY_FILE_ID)
125 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
126 else
127 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
129 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
131 SERVER_START_REQ( open_file_object )
133 req->access = access;
134 req->attributes = attr->Attributes;
135 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
136 req->sharing = sharing;
137 req->options = options;
138 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
139 io->u.Status = wine_server_call( req );
140 *handle = wine_server_ptr_handle( reply->handle );
142 SERVER_END_REQ;
143 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
144 return io->u.Status;
147 if (io->u.Status == STATUS_NO_SUCH_FILE &&
148 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
150 created = TRUE;
151 io->u.Status = STATUS_SUCCESS;
154 if (io->u.Status == STATUS_SUCCESS)
156 struct security_descriptor *sd;
157 struct object_attributes objattr;
159 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
160 objattr.name_len = 0;
161 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
162 if (io->u.Status != STATUS_SUCCESS)
164 RtlFreeAnsiString( &unix_name );
165 return io->u.Status;
168 SERVER_START_REQ( create_file )
170 req->access = access;
171 req->attributes = attr->Attributes;
172 req->sharing = sharing;
173 req->create = disposition;
174 req->options = options;
175 req->attrs = attributes;
176 wine_server_add_data( req, &objattr, sizeof(objattr) );
177 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
178 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
179 io->u.Status = wine_server_call( req );
180 *handle = wine_server_ptr_handle( reply->handle );
182 SERVER_END_REQ;
183 NTDLL_free_struct_sd( sd );
184 RtlFreeAnsiString( &unix_name );
186 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
188 if (io->u.Status == STATUS_SUCCESS)
190 if (created) io->Information = FILE_CREATED;
191 else switch(disposition)
193 case FILE_SUPERSEDE:
194 io->Information = FILE_SUPERSEDED;
195 break;
196 case FILE_CREATE:
197 io->Information = FILE_CREATED;
198 break;
199 case FILE_OPEN:
200 case FILE_OPEN_IF:
201 io->Information = FILE_OPENED;
202 break;
203 case FILE_OVERWRITE:
204 case FILE_OVERWRITE_IF:
205 io->Information = FILE_OVERWRITTEN;
206 break;
209 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
211 static int once;
212 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
215 return io->u.Status;
218 /**************************************************************************
219 * NtOpenFile [NTDLL.@]
220 * ZwOpenFile [NTDLL.@]
222 * Open a file.
224 * PARAMS
225 * handle [O] Variable that receives the file handle on return
226 * access [I] Access desired by the caller to the file
227 * attr [I] Structure describing the file to be opened
228 * io [O] Receives details about the result of the operation
229 * sharing [I] Type of shared access the caller requires
230 * options [I] Options for the file open
232 * RETURNS
233 * Success: 0. FileHandle and IoStatusBlock are updated.
234 * Failure: An NTSTATUS error code describing the error.
236 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
237 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
238 ULONG sharing, ULONG options )
240 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
241 sharing, FILE_OPEN, options, NULL, 0 );
244 /**************************************************************************
245 * NtCreateFile [NTDLL.@]
246 * ZwCreateFile [NTDLL.@]
248 * Either create a new file or directory, or open an existing file, device,
249 * directory or volume.
251 * PARAMS
252 * handle [O] Points to a variable which receives the file handle on return
253 * access [I] Desired access to the file
254 * attr [I] Structure describing the file
255 * io [O] Receives information about the operation on return
256 * alloc_size [I] Initial size of the file in bytes
257 * attributes [I] Attributes to create the file with
258 * sharing [I] Type of shared access the caller would like to the file
259 * disposition [I] Specifies what to do, depending on whether the file already exists
260 * options [I] Options for creating a new file
261 * ea_buffer [I] Pointer to an extended attributes buffer
262 * ea_length [I] Length of ea_buffer
264 * RETURNS
265 * Success: 0. handle and io are updated.
266 * Failure: An NTSTATUS error code describing the error.
268 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
269 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
270 ULONG attributes, ULONG sharing, ULONG disposition,
271 ULONG options, PVOID ea_buffer, ULONG ea_length )
273 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
274 sharing, disposition, options, ea_buffer, ea_length );
277 /***********************************************************************
278 * Asynchronous file I/O *
281 struct async_fileio
283 HANDLE handle;
284 PIO_APC_ROUTINE apc;
285 void *apc_arg;
288 typedef struct
290 struct async_fileio io;
291 char* buffer;
292 unsigned int already;
293 unsigned int count;
294 BOOL avail_mode;
295 } async_fileio_read;
297 typedef struct
299 struct async_fileio io;
300 const char *buffer;
301 unsigned int already;
302 unsigned int count;
303 } async_fileio_write;
306 /* callback for file I/O user APC */
307 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
309 struct async_fileio *async = arg;
310 if (async->apc) async->apc( async->apc_arg, io, reserved );
311 RtlFreeHeap( GetProcessHeap(), 0, async );
314 /***********************************************************************
315 * FILE_GetNtStatus(void)
317 * Retrieve the Nt Status code from errno.
318 * Try to be consistent with FILE_SetDosError().
320 NTSTATUS FILE_GetNtStatus(void)
322 int err = errno;
324 TRACE( "errno = %d\n", errno );
325 switch (err)
327 case EAGAIN: return STATUS_SHARING_VIOLATION;
328 case EBADF: return STATUS_INVALID_HANDLE;
329 case EBUSY: return STATUS_DEVICE_BUSY;
330 case ENOSPC: return STATUS_DISK_FULL;
331 case EPERM:
332 case EROFS:
333 case EACCES: return STATUS_ACCESS_DENIED;
334 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
335 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
336 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
337 case EMFILE:
338 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
339 case EINVAL: return STATUS_INVALID_PARAMETER;
340 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
341 case EPIPE: return STATUS_PIPE_DISCONNECTED;
342 case EIO: return STATUS_DEVICE_NOT_READY;
343 #ifdef ENOMEDIUM
344 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
345 #endif
346 case ENXIO: return STATUS_NO_SUCH_DEVICE;
347 case ENOTTY:
348 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
349 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
350 case EFAULT: return STATUS_ACCESS_VIOLATION;
351 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
352 #ifdef ETIME /* Missing on FreeBSD */
353 case ETIME: return STATUS_IO_TIMEOUT;
354 #endif
355 case ENOEXEC: /* ?? */
356 case EEXIST: /* ?? */
357 default:
358 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
359 return STATUS_UNSUCCESSFUL;
363 /***********************************************************************
364 * FILE_AsyncReadService (INTERNAL)
366 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc)
368 async_fileio_read *fileio = user;
369 int fd, needs_close, result;
371 switch (status)
373 case STATUS_ALERTED: /* got some new data */
374 /* check to see if the data is ready (non-blocking) */
375 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
376 &needs_close, NULL, NULL )))
377 break;
379 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
380 if (needs_close) close( fd );
382 if (result < 0)
384 if (errno == EAGAIN || errno == EINTR)
385 status = STATUS_PENDING;
386 else /* check to see if the transfer is complete */
387 status = FILE_GetNtStatus();
389 else if (result == 0)
391 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
393 else
395 fileio->already += result;
396 if (fileio->already >= fileio->count || fileio->avail_mode)
397 status = STATUS_SUCCESS;
398 else
400 /* if we only have to read the available data, and none is available,
401 * simply cancel the request. If data was available, it has been read
402 * while in by previous call (NtDelayExecution)
404 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
407 break;
409 case STATUS_TIMEOUT:
410 case STATUS_IO_TIMEOUT:
411 if (fileio->already) status = STATUS_SUCCESS;
412 break;
414 if (status != STATUS_PENDING)
416 iosb->u.Status = status;
417 iosb->Information = fileio->already;
418 *apc = fileio_apc;
420 return status;
423 struct io_timeouts
425 int interval; /* max interval between two bytes */
426 int total; /* total timeout for the whole operation */
427 int end_time; /* absolute time of end of operation */
430 /* retrieve the I/O timeouts to use for a given handle */
431 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
432 struct io_timeouts *timeouts )
434 NTSTATUS status = STATUS_SUCCESS;
436 timeouts->interval = timeouts->total = -1;
438 switch(type)
440 case FD_TYPE_SERIAL:
442 /* GetCommTimeouts */
443 SERIAL_TIMEOUTS st;
444 IO_STATUS_BLOCK io;
446 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
447 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
448 if (status) break;
450 if (is_read)
452 if (st.ReadIntervalTimeout)
453 timeouts->interval = st.ReadIntervalTimeout;
455 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
457 timeouts->total = st.ReadTotalTimeoutConstant;
458 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
459 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
461 else if (st.ReadIntervalTimeout == MAXDWORD)
462 timeouts->interval = timeouts->total = 0;
464 else /* write */
466 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
468 timeouts->total = st.WriteTotalTimeoutConstant;
469 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
470 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
474 break;
475 case FD_TYPE_MAILSLOT:
476 if (is_read)
478 timeouts->interval = 0; /* return as soon as we got something */
479 SERVER_START_REQ( set_mailslot_info )
481 req->handle = wine_server_obj_handle( handle );
482 req->flags = 0;
483 if (!(status = wine_server_call( req )) &&
484 reply->read_timeout != TIMEOUT_INFINITE)
485 timeouts->total = reply->read_timeout / -10000;
487 SERVER_END_REQ;
489 break;
490 case FD_TYPE_SOCKET:
491 case FD_TYPE_PIPE:
492 case FD_TYPE_CHAR:
493 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
494 break;
495 default:
496 break;
498 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
499 return STATUS_SUCCESS;
503 /* retrieve the timeout for the next wait, in milliseconds */
504 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
506 int ret = -1;
508 if (timeouts->total != -1)
510 ret = timeouts->end_time - NtGetTickCount();
511 if (ret < 0) ret = 0;
513 if (already && timeouts->interval != -1)
515 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
517 return ret;
521 /* retrieve the avail_mode flag for async reads */
522 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
524 NTSTATUS status = STATUS_SUCCESS;
526 switch(type)
528 case FD_TYPE_SERIAL:
530 /* GetCommTimeouts */
531 SERIAL_TIMEOUTS st;
532 IO_STATUS_BLOCK io;
534 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
535 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
536 if (status) break;
537 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
538 !st.ReadTotalTimeoutConstant &&
539 st.ReadIntervalTimeout == MAXDWORD);
541 break;
542 case FD_TYPE_MAILSLOT:
543 case FD_TYPE_SOCKET:
544 case FD_TYPE_PIPE:
545 case FD_TYPE_CHAR:
546 *avail_mode = TRUE;
547 break;
548 default:
549 *avail_mode = FALSE;
550 break;
552 return status;
556 /******************************************************************************
557 * NtReadFile [NTDLL.@]
558 * ZwReadFile [NTDLL.@]
560 * Read from an open file handle.
562 * PARAMS
563 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
564 * Event [I] Event to signal upon completion (or NULL)
565 * ApcRoutine [I] Callback to call upon completion (or NULL)
566 * ApcContext [I] Context for ApcRoutine (or NULL)
567 * IoStatusBlock [O] Receives information about the operation on return
568 * Buffer [O] Destination for the data read
569 * Length [I] Size of Buffer
570 * ByteOffset [O] Destination for the new file pointer position (or NULL)
571 * Key [O] Function unknown (may be NULL)
573 * RETURNS
574 * Success: 0. IoStatusBlock is updated, and the Information member contains
575 * The number of bytes read.
576 * Failure: An NTSTATUS error code describing the error.
578 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
579 PIO_APC_ROUTINE apc, void* apc_user,
580 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
581 PLARGE_INTEGER offset, PULONG key)
583 int result, unix_handle, needs_close, timeout_init_done = 0;
584 unsigned int options;
585 struct io_timeouts timeouts;
586 NTSTATUS status;
587 ULONG total = 0;
588 enum server_fd_type type;
589 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
590 BOOL send_completion = FALSE, async_read;
592 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
593 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
595 if (!io_status) return STATUS_ACCESS_VIOLATION;
597 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
598 &needs_close, &type, &options );
599 if (status) return status;
601 if (!virtual_check_buffer_for_write( buffer, length ))
603 status = STATUS_ACCESS_VIOLATION;
604 goto done;
607 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
609 if (type == FD_TYPE_FILE)
611 if (async_read && (!offset || offset->QuadPart < 0))
613 status = STATUS_INVALID_PARAMETER;
614 goto done;
617 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
619 /* async I/O doesn't make sense on regular files */
620 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
622 if (errno != EINTR)
624 status = FILE_GetNtStatus();
625 goto done;
628 if (!async_read)
629 /* update file pointer position */
630 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
632 total = result;
633 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
634 goto done;
637 else if (type == FD_TYPE_SERIAL)
639 if (async_read && (!offset || offset->QuadPart < 0))
641 status = STATUS_INVALID_PARAMETER;
642 goto done;
646 for (;;)
648 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
650 total += result;
651 if (!result || total == length)
653 if (total)
655 status = STATUS_SUCCESS;
656 goto done;
658 switch (type)
660 case FD_TYPE_FILE:
661 case FD_TYPE_CHAR:
662 status = STATUS_END_OF_FILE;
663 goto done;
664 case FD_TYPE_SERIAL:
665 break;
666 default:
667 status = STATUS_PIPE_BROKEN;
668 goto done;
671 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
673 else if (errno != EAGAIN)
675 if (errno == EINTR) continue;
676 if (!total) status = FILE_GetNtStatus();
677 goto done;
680 if (async_read)
682 async_fileio_read *fileio;
683 BOOL avail_mode;
685 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
686 goto err;
687 if (total && avail_mode)
689 status = STATUS_SUCCESS;
690 goto done;
693 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
695 status = STATUS_NO_MEMORY;
696 goto err;
698 fileio->io.handle = hFile;
699 fileio->io.apc = apc;
700 fileio->io.apc_arg = apc_user;
701 fileio->already = total;
702 fileio->count = length;
703 fileio->buffer = buffer;
704 fileio->avail_mode = avail_mode;
706 SERVER_START_REQ( register_async )
708 req->type = ASYNC_TYPE_READ;
709 req->count = length;
710 req->async.handle = wine_server_obj_handle( hFile );
711 req->async.event = wine_server_obj_handle( hEvent );
712 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
713 req->async.iosb = wine_server_client_ptr( io_status );
714 req->async.arg = wine_server_client_ptr( fileio );
715 req->async.cvalue = cvalue;
716 status = wine_server_call( req );
718 SERVER_END_REQ;
720 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
721 goto err;
723 else /* synchronous read, wait for the fd to become ready */
725 struct pollfd pfd;
726 int ret, timeout;
728 if (!timeout_init_done)
730 timeout_init_done = 1;
731 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
732 goto err;
733 if (hEvent) NtResetEvent( hEvent, NULL );
735 timeout = get_next_io_timeout( &timeouts, total );
737 pfd.fd = unix_handle;
738 pfd.events = POLLIN;
740 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
742 if (total) /* return with what we got so far */
743 status = STATUS_SUCCESS;
744 else
745 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
746 goto done;
748 if (ret == -1 && errno != EINTR)
750 status = FILE_GetNtStatus();
751 goto done;
753 /* will now restart the read */
757 done:
758 send_completion = cvalue != 0;
760 err:
761 if (needs_close) close( unix_handle );
762 if (status == STATUS_SUCCESS)
764 io_status->u.Status = status;
765 io_status->Information = total;
766 TRACE("= SUCCESS (%u)\n", total);
767 if (hEvent) NtSetEvent( hEvent, NULL );
768 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
769 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
771 else
773 TRACE("= 0x%08x\n", status);
774 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
777 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
779 return status;
783 /******************************************************************************
784 * NtReadFileScatter [NTDLL.@]
785 * ZwReadFileScatter [NTDLL.@]
787 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
788 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
789 ULONG length, PLARGE_INTEGER offset, PULONG key )
791 int result, unix_handle, needs_close;
792 unsigned int options;
793 NTSTATUS status;
794 ULONG pos = 0, total = 0;
795 enum server_fd_type type;
796 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
797 BOOL send_completion = FALSE;
799 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
800 file, event, apc, apc_user, io_status, segments, length, offset, key);
802 if (length % page_size) return STATUS_INVALID_PARAMETER;
803 if (!io_status) return STATUS_ACCESS_VIOLATION;
805 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
806 &needs_close, &type, &options );
807 if (status) return status;
809 if ((type != FD_TYPE_FILE) ||
810 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
811 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
813 status = STATUS_INVALID_PARAMETER;
814 goto error;
817 while (length)
819 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
820 result = pread( unix_handle, (char *)segments->Buffer + pos,
821 page_size - pos, offset->QuadPart + total );
822 else
823 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
825 if (result == -1)
827 if (errno == EINTR) continue;
828 status = FILE_GetNtStatus();
829 break;
831 if (!result)
833 status = STATUS_END_OF_FILE;
834 break;
836 total += result;
837 length -= result;
838 if ((pos += result) == page_size)
840 pos = 0;
841 segments++;
845 send_completion = cvalue != 0;
847 error:
848 if (needs_close) close( unix_handle );
849 if (status == STATUS_SUCCESS)
851 io_status->u.Status = status;
852 io_status->Information = total;
853 TRACE("= SUCCESS (%u)\n", total);
854 if (event) NtSetEvent( event, NULL );
855 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
856 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
858 else
860 TRACE("= 0x%08x\n", status);
861 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
864 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
866 return status;
870 /***********************************************************************
871 * FILE_AsyncWriteService (INTERNAL)
873 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status, void **apc)
875 async_fileio_write *fileio = user;
876 int result, fd, needs_close;
877 enum server_fd_type type;
879 switch (status)
881 case STATUS_ALERTED:
882 /* write some data (non-blocking) */
883 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
884 &needs_close, &type, NULL )))
885 break;
887 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
888 result = send( fd, fileio->buffer, 0, 0 );
889 else
890 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
892 if (needs_close) close( fd );
894 if (result < 0)
896 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
897 else status = FILE_GetNtStatus();
899 else
901 fileio->already += result;
902 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
904 break;
906 case STATUS_TIMEOUT:
907 case STATUS_IO_TIMEOUT:
908 if (fileio->already) status = STATUS_SUCCESS;
909 break;
911 if (status != STATUS_PENDING)
913 iosb->u.Status = status;
914 iosb->Information = fileio->already;
915 *apc = fileio_apc;
917 return status;
920 /******************************************************************************
921 * NtWriteFile [NTDLL.@]
922 * ZwWriteFile [NTDLL.@]
924 * Write to an open file handle.
926 * PARAMS
927 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
928 * Event [I] Event to signal upon completion (or NULL)
929 * ApcRoutine [I] Callback to call upon completion (or NULL)
930 * ApcContext [I] Context for ApcRoutine (or NULL)
931 * IoStatusBlock [O] Receives information about the operation on return
932 * Buffer [I] Source for the data to write
933 * Length [I] Size of Buffer
934 * ByteOffset [O] Destination for the new file pointer position (or NULL)
935 * Key [O] Function unknown (may be NULL)
937 * RETURNS
938 * Success: 0. IoStatusBlock is updated, and the Information member contains
939 * The number of bytes written.
940 * Failure: An NTSTATUS error code describing the error.
942 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
943 PIO_APC_ROUTINE apc, void* apc_user,
944 PIO_STATUS_BLOCK io_status,
945 const void* buffer, ULONG length,
946 PLARGE_INTEGER offset, PULONG key)
948 int result, unix_handle, needs_close, timeout_init_done = 0;
949 unsigned int options;
950 struct io_timeouts timeouts;
951 NTSTATUS status;
952 ULONG total = 0;
953 enum server_fd_type type;
954 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
955 BOOL send_completion = FALSE, async_write, append_write = FALSE;
956 LARGE_INTEGER offset_eof;
958 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
959 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
961 if (!io_status) return STATUS_ACCESS_VIOLATION;
963 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
964 &needs_close, &type, &options );
965 if (status == STATUS_ACCESS_DENIED)
967 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
968 &needs_close, &type, &options );
969 append_write = TRUE;
971 if (status) return status;
973 if (!virtual_check_buffer_for_read( buffer, length ))
975 status = STATUS_INVALID_USER_BUFFER;
976 goto done;
979 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
981 if (type == FD_TYPE_FILE)
983 if (async_write && (!offset || offset->QuadPart < 0))
985 status = STATUS_INVALID_PARAMETER;
986 goto done;
989 if (append_write)
991 offset_eof.QuadPart = (LONGLONG)-1; /* FILE_WRITE_TO_END_OF_FILE */
992 offset = &offset_eof;
995 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
997 off_t off = offset->QuadPart;
999 if (offset->QuadPart == (LONGLONG)-1 /* FILE_WRITE_TO_END_OF_FILE */)
1001 struct stat st;
1003 if (fstat( unix_handle, &st ) == -1)
1005 status = FILE_GetNtStatus();
1006 goto done;
1008 off = st.st_size;
1010 else if (offset->QuadPart < 0)
1012 status = STATUS_INVALID_PARAMETER;
1013 goto done;
1016 /* async I/O doesn't make sense on regular files */
1017 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1019 if (errno != EINTR)
1021 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1022 else status = FILE_GetNtStatus();
1023 goto done;
1027 if (!async_write)
1028 /* update file pointer position */
1029 lseek( unix_handle, off + result, SEEK_SET );
1031 total = result;
1032 status = STATUS_SUCCESS;
1033 goto done;
1036 else if (type == FD_TYPE_SERIAL)
1038 if (async_write &&
1039 (!offset || (offset->QuadPart < 0 && offset->QuadPart != (LONGLONG)-1 /* FILE_WRITE_TO_END_OF_FILE */)))
1041 status = STATUS_INVALID_PARAMETER;
1042 goto done;
1046 for (;;)
1048 /* zero-length writes on sockets may not work with plain write(2) */
1049 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1050 result = send( unix_handle, buffer, 0, 0 );
1051 else
1052 result = write( unix_handle, (const char *)buffer + total, length - total );
1054 if (result >= 0)
1056 total += result;
1057 if (total == length)
1059 status = STATUS_SUCCESS;
1060 goto done;
1062 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1064 else if (errno != EAGAIN)
1066 if (errno == EINTR) continue;
1067 if (!total)
1069 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1070 else status = FILE_GetNtStatus();
1072 goto done;
1075 if (async_write)
1077 async_fileio_write *fileio;
1079 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
1081 status = STATUS_NO_MEMORY;
1082 goto err;
1084 fileio->io.handle = hFile;
1085 fileio->io.apc = apc;
1086 fileio->io.apc_arg = apc_user;
1087 fileio->already = total;
1088 fileio->count = length;
1089 fileio->buffer = buffer;
1091 SERVER_START_REQ( register_async )
1093 req->type = ASYNC_TYPE_WRITE;
1094 req->count = length;
1095 req->async.handle = wine_server_obj_handle( hFile );
1096 req->async.event = wine_server_obj_handle( hEvent );
1097 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1098 req->async.iosb = wine_server_client_ptr( io_status );
1099 req->async.arg = wine_server_client_ptr( fileio );
1100 req->async.cvalue = cvalue;
1101 status = wine_server_call( req );
1103 SERVER_END_REQ;
1105 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1106 goto err;
1108 else /* synchronous write, wait for the fd to become ready */
1110 struct pollfd pfd;
1111 int ret, timeout;
1113 if (!timeout_init_done)
1115 timeout_init_done = 1;
1116 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1117 goto err;
1118 if (hEvent) NtResetEvent( hEvent, NULL );
1120 timeout = get_next_io_timeout( &timeouts, total );
1122 pfd.fd = unix_handle;
1123 pfd.events = POLLOUT;
1125 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1127 /* return with what we got so far */
1128 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1129 goto done;
1131 if (ret == -1 && errno != EINTR)
1133 status = FILE_GetNtStatus();
1134 goto done;
1136 /* will now restart the write */
1140 done:
1141 send_completion = cvalue != 0;
1143 err:
1144 if (needs_close) close( unix_handle );
1145 if (status == STATUS_SUCCESS)
1147 io_status->u.Status = status;
1148 io_status->Information = total;
1149 TRACE("= SUCCESS (%u)\n", total);
1150 if (hEvent) NtSetEvent( hEvent, NULL );
1151 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1152 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1154 else
1156 TRACE("= 0x%08x\n", status);
1157 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1160 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1162 return status;
1166 /******************************************************************************
1167 * NtWriteFileGather [NTDLL.@]
1168 * ZwWriteFileGather [NTDLL.@]
1170 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1171 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1172 ULONG length, PLARGE_INTEGER offset, PULONG key )
1174 int result, unix_handle, needs_close;
1175 unsigned int options;
1176 NTSTATUS status;
1177 ULONG pos = 0, total = 0;
1178 enum server_fd_type type;
1179 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1180 BOOL send_completion = FALSE;
1182 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1183 file, event, apc, apc_user, io_status, segments, length, offset, key);
1185 if (length % page_size) return STATUS_INVALID_PARAMETER;
1186 if (!io_status) return STATUS_ACCESS_VIOLATION;
1188 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1189 &needs_close, &type, &options );
1190 if (status) return status;
1192 if ((type != FD_TYPE_FILE) ||
1193 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1194 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1196 status = STATUS_INVALID_PARAMETER;
1197 goto error;
1200 while (length)
1202 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
1203 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1204 page_size - pos, offset->QuadPart + total );
1205 else
1206 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1208 if (result == -1)
1210 if (errno == EINTR) continue;
1211 if (errno == EFAULT)
1213 status = STATUS_INVALID_USER_BUFFER;
1214 goto error;
1216 status = FILE_GetNtStatus();
1217 break;
1219 if (!result)
1221 status = STATUS_DISK_FULL;
1222 break;
1224 total += result;
1225 length -= result;
1226 if ((pos += result) == page_size)
1228 pos = 0;
1229 segments++;
1233 send_completion = cvalue != 0;
1235 error:
1236 if (needs_close) close( unix_handle );
1237 if (status == STATUS_SUCCESS)
1239 io_status->u.Status = status;
1240 io_status->Information = total;
1241 TRACE("= SUCCESS (%u)\n", total);
1242 if (event) NtSetEvent( event, NULL );
1243 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1244 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1246 else
1248 TRACE("= 0x%08x\n", status);
1249 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1252 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1254 return status;
1258 struct async_ioctl
1260 HANDLE handle; /* handle to the device */
1261 HANDLE event; /* async event */
1262 void *buffer; /* buffer for output */
1263 ULONG size; /* size of buffer */
1264 PIO_APC_ROUTINE apc; /* user apc params */
1265 void *apc_arg;
1268 /* callback for ioctl user APC */
1269 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
1271 struct async_ioctl *async = arg;
1272 if (async->apc) async->apc( async->apc_arg, io, reserved );
1273 RtlFreeHeap( GetProcessHeap(), 0, async );
1276 /* callback for ioctl async I/O completion */
1277 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status, void **apc )
1279 struct async_ioctl *async = arg;
1281 if (status == STATUS_ALERTED)
1283 SERVER_START_REQ( get_ioctl_result )
1285 req->handle = wine_server_obj_handle( async->handle );
1286 req->user_arg = wine_server_client_ptr( async );
1287 wine_server_set_reply( req, async->buffer, async->size );
1288 status = wine_server_call( req );
1289 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1291 SERVER_END_REQ;
1293 if (status != STATUS_PENDING)
1295 io->u.Status = status;
1296 if (async->apc || async->event) *apc = ioctl_apc;
1298 return status;
1301 /* do a ioctl call through the server */
1302 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1303 PIO_APC_ROUTINE apc, PVOID apc_context,
1304 IO_STATUS_BLOCK *io, ULONG code,
1305 const void *in_buffer, ULONG in_size,
1306 PVOID out_buffer, ULONG out_size )
1308 struct async_ioctl *async;
1309 NTSTATUS status;
1310 HANDLE wait_handle;
1311 ULONG options;
1312 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1314 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
1315 return STATUS_NO_MEMORY;
1316 async->handle = handle;
1317 async->event = event;
1318 async->buffer = out_buffer;
1319 async->size = out_size;
1320 async->apc = apc;
1321 async->apc_arg = apc_context;
1323 SERVER_START_REQ( ioctl )
1325 req->code = code;
1326 req->blocking = !apc && !event && !cvalue;
1327 req->async.handle = wine_server_obj_handle( handle );
1328 req->async.callback = wine_server_client_ptr( ioctl_completion );
1329 req->async.iosb = wine_server_client_ptr( io );
1330 req->async.arg = wine_server_client_ptr( async );
1331 req->async.event = wine_server_obj_handle( event );
1332 req->async.cvalue = cvalue;
1333 wine_server_add_data( req, in_buffer, in_size );
1334 wine_server_set_reply( req, out_buffer, out_size );
1335 status = wine_server_call( req );
1336 wait_handle = wine_server_ptr_handle( reply->wait );
1337 options = reply->options;
1338 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1340 SERVER_END_REQ;
1342 if (status == STATUS_NOT_SUPPORTED)
1343 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1344 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1346 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1348 if (wait_handle)
1350 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1351 status = io->u.Status;
1352 NtClose( wait_handle );
1353 RtlFreeHeap( GetProcessHeap(), 0, async );
1356 return status;
1359 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1360 * server */
1361 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1362 ULONG in_size)
1364 #ifdef VALGRIND_MAKE_MEM_DEFINED
1365 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1366 do { \
1367 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1368 if ((size) >= FIELD_OFFSET(t, f2)) \
1369 VALGRIND_MAKE_MEM_DEFINED( \
1370 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1371 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1372 } while (0)
1374 switch (code)
1376 case FSCTL_PIPE_WAIT:
1377 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1378 break;
1380 #endif
1384 /**************************************************************************
1385 * NtDeviceIoControlFile [NTDLL.@]
1386 * ZwDeviceIoControlFile [NTDLL.@]
1388 * Perform an I/O control operation on an open file handle.
1390 * PARAMS
1391 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1392 * event [I] Event to signal upon completion (or NULL)
1393 * apc [I] Callback to call upon completion (or NULL)
1394 * apc_context [I] Context for ApcRoutine (or NULL)
1395 * io [O] Receives information about the operation on return
1396 * code [I] Control code for the operation to perform
1397 * in_buffer [I] Source for any input data required (or NULL)
1398 * in_size [I] Size of InputBuffer
1399 * out_buffer [O] Source for any output data returned (or NULL)
1400 * out_size [I] Size of OutputBuffer
1402 * RETURNS
1403 * Success: 0. IoStatusBlock is updated.
1404 * Failure: An NTSTATUS error code describing the error.
1406 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1407 PIO_APC_ROUTINE apc, PVOID apc_context,
1408 PIO_STATUS_BLOCK io, ULONG code,
1409 PVOID in_buffer, ULONG in_size,
1410 PVOID out_buffer, ULONG out_size)
1412 ULONG device = (code >> 16);
1413 NTSTATUS status = STATUS_NOT_SUPPORTED;
1415 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1416 handle, event, apc, apc_context, io, code,
1417 in_buffer, in_size, out_buffer, out_size);
1419 switch(device)
1421 case FILE_DEVICE_DISK:
1422 case FILE_DEVICE_CD_ROM:
1423 case FILE_DEVICE_DVD:
1424 case FILE_DEVICE_CONTROLLER:
1425 case FILE_DEVICE_MASS_STORAGE:
1426 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1427 in_buffer, in_size, out_buffer, out_size);
1428 break;
1429 case FILE_DEVICE_SERIAL_PORT:
1430 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1431 in_buffer, in_size, out_buffer, out_size);
1432 break;
1433 case FILE_DEVICE_TAPE:
1434 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1435 in_buffer, in_size, out_buffer, out_size);
1436 break;
1439 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1440 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1441 in_buffer, in_size, out_buffer, out_size );
1443 if (status != STATUS_PENDING) io->u.Status = status;
1444 return status;
1448 /**************************************************************************
1449 * NtFsControlFile [NTDLL.@]
1450 * ZwFsControlFile [NTDLL.@]
1452 * Perform a file system control operation on an open file handle.
1454 * PARAMS
1455 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1456 * event [I] Event to signal upon completion (or NULL)
1457 * apc [I] Callback to call upon completion (or NULL)
1458 * apc_context [I] Context for ApcRoutine (or NULL)
1459 * io [O] Receives information about the operation on return
1460 * code [I] Control code for the operation to perform
1461 * in_buffer [I] Source for any input data required (or NULL)
1462 * in_size [I] Size of InputBuffer
1463 * out_buffer [O] Source for any output data returned (or NULL)
1464 * out_size [I] Size of OutputBuffer
1466 * RETURNS
1467 * Success: 0. IoStatusBlock is updated.
1468 * Failure: An NTSTATUS error code describing the error.
1470 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1471 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1472 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1474 NTSTATUS status;
1476 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1477 handle, event, apc, apc_context, io, code,
1478 in_buffer, in_size, out_buffer, out_size);
1480 if (!io) return STATUS_INVALID_PARAMETER;
1482 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1484 switch(code)
1486 case FSCTL_DISMOUNT_VOLUME:
1487 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1488 in_buffer, in_size, out_buffer, out_size );
1489 if (!status) status = DIR_unmount_device( handle );
1490 break;
1492 case FSCTL_PIPE_PEEK:
1494 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1495 int avail = 0, fd, needs_close;
1497 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1499 status = STATUS_INFO_LENGTH_MISMATCH;
1500 break;
1503 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1504 break;
1506 #ifdef FIONREAD
1507 if (ioctl( fd, FIONREAD, &avail ) != 0)
1509 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1510 if (needs_close) close( fd );
1511 status = FILE_GetNtStatus();
1512 break;
1514 #endif
1515 if (!avail) /* check for closed pipe */
1517 struct pollfd pollfd;
1518 int ret;
1520 pollfd.fd = fd;
1521 pollfd.events = POLLIN;
1522 pollfd.revents = 0;
1523 ret = poll( &pollfd, 1, 0 );
1524 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1526 if (needs_close) close( fd );
1527 status = STATUS_PIPE_BROKEN;
1528 break;
1531 buffer->NamedPipeState = 0; /* FIXME */
1532 buffer->ReadDataAvailable = avail;
1533 buffer->NumberOfMessages = 0; /* FIXME */
1534 buffer->MessageLength = 0; /* FIXME */
1535 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1536 status = STATUS_SUCCESS;
1537 if (avail)
1539 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1540 if (data_size)
1542 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1543 if (res >= 0) io->Information += res;
1546 if (needs_close) close( fd );
1548 break;
1550 case FSCTL_PIPE_DISCONNECT:
1551 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1552 in_buffer, in_size, out_buffer, out_size );
1553 if (!status)
1555 int fd = server_remove_fd_from_cache( handle );
1556 if (fd != -1) close( fd );
1558 break;
1560 case FSCTL_PIPE_IMPERSONATE:
1561 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1562 status = RtlImpersonateSelf( SecurityImpersonation );
1563 break;
1565 case FSCTL_LOCK_VOLUME:
1566 case FSCTL_UNLOCK_VOLUME:
1567 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1568 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1569 status = STATUS_SUCCESS;
1570 break;
1572 case FSCTL_GET_RETRIEVAL_POINTERS:
1574 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1576 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1578 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1580 buffer->ExtentCount = 1;
1581 buffer->StartingVcn.QuadPart = 1;
1582 buffer->Extents[0].NextVcn.QuadPart = 0;
1583 buffer->Extents[0].Lcn.QuadPart = 0;
1584 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1585 status = STATUS_SUCCESS;
1587 else
1589 io->Information = 0;
1590 status = STATUS_BUFFER_TOO_SMALL;
1592 break;
1594 case FSCTL_PIPE_LISTEN:
1595 case FSCTL_PIPE_WAIT:
1596 default:
1597 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1598 in_buffer, in_size, out_buffer, out_size );
1599 break;
1602 if (status != STATUS_PENDING) io->u.Status = status;
1603 return status;
1606 /******************************************************************************
1607 * NtSetVolumeInformationFile [NTDLL.@]
1608 * ZwSetVolumeInformationFile [NTDLL.@]
1610 * Set volume information for an open file handle.
1612 * PARAMS
1613 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1614 * IoStatusBlock [O] Receives information about the operation on return
1615 * FsInformation [I] Source for volume information
1616 * Length [I] Size of FsInformation
1617 * FsInformationClass [I] Type of volume information to set
1619 * RETURNS
1620 * Success: 0. IoStatusBlock is updated.
1621 * Failure: An NTSTATUS error code describing the error.
1623 NTSTATUS WINAPI NtSetVolumeInformationFile(
1624 IN HANDLE FileHandle,
1625 PIO_STATUS_BLOCK IoStatusBlock,
1626 PVOID FsInformation,
1627 ULONG Length,
1628 FS_INFORMATION_CLASS FsInformationClass)
1630 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1631 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1632 return 0;
1635 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1637 NTSTATUS status = STATUS_SUCCESS;
1639 #ifdef HAVE_FUTIMENS
1640 struct timespec tv[2];
1642 tv[0].tv_sec = tv[1].tv_sec = 0;
1643 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1644 if (atime->QuadPart)
1646 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1647 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1649 if (mtime->QuadPart)
1651 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1652 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1654 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1656 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1657 struct timeval tv[2];
1658 struct stat st;
1660 if (!atime->QuadPart || !mtime->QuadPart)
1663 tv[0].tv_sec = tv[0].tv_usec = 0;
1664 tv[1].tv_sec = tv[1].tv_usec = 0;
1665 if (!fstat( fd, &st ))
1667 tv[0].tv_sec = st.st_atime;
1668 tv[1].tv_sec = st.st_mtime;
1669 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1670 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1671 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1672 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1673 #endif
1674 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1675 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1676 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1677 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1678 #endif
1681 if (atime->QuadPart)
1683 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1684 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1686 if (mtime->QuadPart)
1688 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1689 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1691 #ifdef HAVE_FUTIMES
1692 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1693 #elif defined(HAVE_FUTIMESAT)
1694 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1695 #endif
1697 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1698 FIXME( "setting file times not supported\n" );
1699 status = STATUS_NOT_IMPLEMENTED;
1700 #endif
1701 return status;
1704 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1705 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1707 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1708 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1709 RtlSecondsSince1970ToTime( st->st_atime, atime );
1710 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1711 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1712 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1713 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
1714 #endif
1715 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1716 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
1717 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1718 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
1719 #endif
1720 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1721 atime->QuadPart += st->st_atim.tv_nsec / 100;
1722 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1723 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
1724 #endif
1725 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1726 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
1727 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
1728 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
1729 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1730 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
1731 #endif
1732 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
1733 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
1734 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
1735 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
1736 #endif
1737 #else
1738 *creation = *mtime;
1739 #endif
1742 /* fill in the file information that depends on the stat info */
1743 NTSTATUS fill_stat_info( const struct stat *st, void *ptr, FILE_INFORMATION_CLASS class )
1745 switch (class)
1747 case FileBasicInformation:
1749 FILE_BASIC_INFORMATION *info = ptr;
1751 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1752 &info->LastAccessTime, &info->CreationTime );
1753 if (S_ISDIR(st->st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1754 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1755 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1756 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1758 break;
1759 case FileStandardInformation:
1761 FILE_STANDARD_INFORMATION *info = ptr;
1763 if ((info->Directory = S_ISDIR(st->st_mode)))
1765 info->AllocationSize.QuadPart = 0;
1766 info->EndOfFile.QuadPart = 0;
1767 info->NumberOfLinks = 1;
1769 else
1771 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1772 info->EndOfFile.QuadPart = st->st_size;
1773 info->NumberOfLinks = st->st_nlink;
1776 break;
1777 case FileInternalInformation:
1779 FILE_INTERNAL_INFORMATION *info = ptr;
1780 info->IndexNumber.QuadPart = st->st_ino;
1782 break;
1783 case FileEndOfFileInformation:
1785 FILE_END_OF_FILE_INFORMATION *info = ptr;
1786 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
1788 break;
1789 case FileAllInformation:
1791 FILE_ALL_INFORMATION *info = ptr;
1792 fill_stat_info( st, &info->BasicInformation, FileBasicInformation );
1793 fill_stat_info( st, &info->StandardInformation, FileStandardInformation );
1794 fill_stat_info( st, &info->InternalInformation, FileInternalInformation );
1796 break;
1797 /* all directory structures start with the FileDirectoryInformation layout */
1798 case FileBothDirectoryInformation:
1799 case FileFullDirectoryInformation:
1800 case FileDirectoryInformation:
1802 FILE_DIRECTORY_INFORMATION *info = ptr;
1804 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1805 &info->LastAccessTime, &info->CreationTime );
1806 if (S_ISDIR(st->st_mode))
1808 info->AllocationSize.QuadPart = 0;
1809 info->EndOfFile.QuadPart = 0;
1810 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1812 else
1814 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1815 info->EndOfFile.QuadPart = st->st_size;
1816 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1818 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1819 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1821 break;
1822 case FileIdFullDirectoryInformation:
1824 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
1825 info->FileId.QuadPart = st->st_ino;
1826 fill_stat_info( st, info, FileDirectoryInformation );
1828 break;
1829 case FileIdBothDirectoryInformation:
1831 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
1832 info->FileId.QuadPart = st->st_ino;
1833 fill_stat_info( st, info, FileDirectoryInformation );
1835 break;
1837 default:
1838 return STATUS_INVALID_INFO_CLASS;
1840 return STATUS_SUCCESS;
1843 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
1845 data_size_t size = 1024;
1846 NTSTATUS ret;
1847 char *name;
1849 for (;;)
1851 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
1852 if (!name) return STATUS_NO_MEMORY;
1853 unix_name->MaximumLength = size + 1;
1855 SERVER_START_REQ( get_handle_unix_name )
1857 req->handle = wine_server_obj_handle( handle );
1858 wine_server_set_reply( req, name, size );
1859 ret = wine_server_call( req );
1860 size = reply->name_len;
1862 SERVER_END_REQ;
1864 if (!ret)
1866 name[size] = 0;
1867 unix_name->Buffer = name;
1868 unix_name->Length = size;
1869 break;
1871 RtlFreeHeap( GetProcessHeap(), 0, name );
1872 if (ret != STATUS_BUFFER_OVERFLOW) break;
1874 return ret;
1877 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
1879 UNICODE_STRING nt_name;
1880 NTSTATUS status;
1882 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
1884 const WCHAR *ptr = nt_name.Buffer;
1885 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
1887 /* Skip the volume mount point. */
1888 while (ptr != end && *ptr == '\\') ++ptr;
1889 while (ptr != end && *ptr != '\\') ++ptr;
1890 while (ptr != end && *ptr == '\\') ++ptr;
1891 while (ptr != end && *ptr != '\\') ++ptr;
1893 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
1894 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
1895 else *name_len = info->FileNameLength;
1897 memcpy( info->FileName, ptr, *name_len );
1898 RtlFreeUnicodeString( &nt_name );
1901 return status;
1904 /******************************************************************************
1905 * NtQueryInformationFile [NTDLL.@]
1906 * ZwQueryInformationFile [NTDLL.@]
1908 * Get information about an open file handle.
1910 * PARAMS
1911 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1912 * io [O] Receives information about the operation on return
1913 * ptr [O] Destination for file information
1914 * len [I] Size of FileInformation
1915 * class [I] Type of file information to get
1917 * RETURNS
1918 * Success: 0. IoStatusBlock and FileInformation are updated.
1919 * Failure: An NTSTATUS error code describing the error.
1921 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1922 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1924 static const size_t info_sizes[] =
1927 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1928 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1929 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1930 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1931 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1932 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1933 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1934 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1935 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
1936 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1937 0, /* FileLinkInformation */
1938 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1939 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1940 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1941 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1942 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1943 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1944 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
1945 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1946 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1947 0, /* FileAlternateNameInformation */
1948 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1949 0, /* FilePipeInformation */
1950 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1951 0, /* FilePipeRemoteInformation */
1952 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1953 0, /* FileMailslotSetInformation */
1954 0, /* FileCompressionInformation */
1955 0, /* FileObjectIdInformation */
1956 0, /* FileCompletionInformation */
1957 0, /* FileMoveClusterInformation */
1958 0, /* FileQuotaInformation */
1959 0, /* FileReparsePointInformation */
1960 0, /* FileNetworkOpenInformation */
1961 0, /* FileAttributeTagInformation */
1962 0, /* FileTrackingInformation */
1963 0, /* FileIdBothDirectoryInformation */
1964 0, /* FileIdFullDirectoryInformation */
1965 0, /* FileValidDataLengthInformation */
1966 0, /* FileShortNameInformation */
1970 0, /* FileSfioReserveInformation */
1971 0, /* FileSfioVolumeInformation */
1972 0, /* FileHardLinkInformation */
1974 0, /* FileNormalizedNameInformation */
1976 0, /* FileIdGlobalTxDirectoryInformation */
1980 0 /* FileStandardLinkInformation */
1983 struct stat st;
1984 int fd, needs_close = FALSE;
1986 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1988 io->Information = 0;
1990 if (class <= 0 || class >= FileMaximumInformation)
1991 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1992 if (!info_sizes[class])
1994 FIXME("Unsupported class (%d)\n", class);
1995 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1997 if (len < info_sizes[class])
1998 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2000 if (class != FilePipeLocalInformation)
2002 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2003 return io->u.Status;
2006 switch (class)
2008 case FileBasicInformation:
2009 if (fstat( fd, &st ) == -1)
2010 io->u.Status = FILE_GetNtStatus();
2011 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2012 io->u.Status = STATUS_INVALID_INFO_CLASS;
2013 else
2014 fill_stat_info( &st, ptr, class );
2015 break;
2016 case FileStandardInformation:
2018 FILE_STANDARD_INFORMATION *info = ptr;
2020 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2021 else
2023 fill_stat_info( &st, info, class );
2024 info->DeletePending = FALSE; /* FIXME */
2027 break;
2028 case FilePositionInformation:
2030 FILE_POSITION_INFORMATION *info = ptr;
2031 off_t res = lseek( fd, 0, SEEK_CUR );
2032 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2033 else info->CurrentByteOffset.QuadPart = res;
2035 break;
2036 case FileInternalInformation:
2037 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2038 else fill_stat_info( &st, ptr, class );
2039 break;
2040 case FileEaInformation:
2042 FILE_EA_INFORMATION *info = ptr;
2043 info->EaSize = 0;
2045 break;
2046 case FileEndOfFileInformation:
2047 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2048 else fill_stat_info( &st, ptr, class );
2049 break;
2050 case FileAllInformation:
2052 FILE_ALL_INFORMATION *info = ptr;
2053 ANSI_STRING unix_name;
2055 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2056 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2057 io->u.Status = STATUS_INVALID_INFO_CLASS;
2058 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2060 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2062 fill_stat_info( &st, info, FileAllInformation );
2063 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2064 info->EaInformation.EaSize = 0;
2065 info->AccessInformation.AccessFlags = 0; /* FIXME */
2066 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2067 info->ModeInformation.Mode = 0; /* FIXME */
2068 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2070 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2071 RtlFreeAnsiString( &unix_name );
2072 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2075 break;
2076 case FileMailslotQueryInformation:
2078 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2080 SERVER_START_REQ( set_mailslot_info )
2082 req->handle = wine_server_obj_handle( hFile );
2083 req->flags = 0;
2084 io->u.Status = wine_server_call( req );
2085 if( io->u.Status == STATUS_SUCCESS )
2087 info->MaximumMessageSize = reply->max_msgsize;
2088 info->MailslotQuota = 0;
2089 info->NextMessageSize = 0;
2090 info->MessagesAvailable = 0;
2091 info->ReadTimeout.QuadPart = reply->read_timeout;
2094 SERVER_END_REQ;
2095 if (!io->u.Status)
2097 char *tmpbuf;
2098 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2099 if (size > 0x10000) size = 0x10000;
2100 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2102 int fd, needs_close;
2103 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2105 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2106 info->MessagesAvailable = (res > 0);
2107 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2108 if (needs_close) close( fd );
2110 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2114 break;
2115 case FilePipeLocalInformation:
2117 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2119 SERVER_START_REQ( get_named_pipe_info )
2121 req->handle = wine_server_obj_handle( hFile );
2122 if (!(io->u.Status = wine_server_call( req )))
2124 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2125 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2126 switch (reply->sharing)
2128 case FILE_SHARE_READ:
2129 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2130 break;
2131 case FILE_SHARE_WRITE:
2132 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2133 break;
2134 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2135 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2136 break;
2138 pli->MaximumInstances = reply->maxinstances;
2139 pli->CurrentInstances = reply->instances;
2140 pli->InboundQuota = reply->insize;
2141 pli->ReadDataAvailable = 0; /* FIXME */
2142 pli->OutboundQuota = reply->outsize;
2143 pli->WriteQuotaAvailable = 0; /* FIXME */
2144 pli->NamedPipeState = 0; /* FIXME */
2145 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2146 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2149 SERVER_END_REQ;
2151 break;
2152 case FileNameInformation:
2154 FILE_NAME_INFORMATION *info = ptr;
2155 ANSI_STRING unix_name;
2157 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2159 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2160 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2161 RtlFreeAnsiString( &unix_name );
2162 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2165 break;
2166 default:
2167 FIXME("Unsupported class (%d)\n", class);
2168 io->u.Status = STATUS_NOT_IMPLEMENTED;
2169 break;
2171 if (needs_close) close( fd );
2172 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2173 return io->u.Status;
2176 /******************************************************************************
2177 * NtSetInformationFile [NTDLL.@]
2178 * ZwSetInformationFile [NTDLL.@]
2180 * Set information about an open file handle.
2182 * PARAMS
2183 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2184 * io [O] Receives information about the operation on return
2185 * ptr [I] Source for file information
2186 * len [I] Size of FileInformation
2187 * class [I] Type of file information to set
2189 * RETURNS
2190 * Success: 0. io is updated.
2191 * Failure: An NTSTATUS error code describing the error.
2193 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2194 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2196 int fd, needs_close;
2198 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2200 io->u.Status = STATUS_SUCCESS;
2201 switch (class)
2203 case FileBasicInformation:
2204 if (len >= sizeof(FILE_BASIC_INFORMATION))
2206 struct stat st;
2207 const FILE_BASIC_INFORMATION *info = ptr;
2209 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2210 return io->u.Status;
2212 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2213 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2215 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2217 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2218 else
2220 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2222 if (S_ISDIR( st.st_mode))
2223 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2224 else
2225 st.st_mode &= ~0222; /* clear write permission bits */
2227 else
2229 /* add write permission only where we already have read permission */
2230 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2232 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2236 if (needs_close) close( fd );
2238 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2239 break;
2241 case FilePositionInformation:
2242 if (len >= sizeof(FILE_POSITION_INFORMATION))
2244 const FILE_POSITION_INFORMATION *info = ptr;
2246 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2247 return io->u.Status;
2249 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2250 io->u.Status = FILE_GetNtStatus();
2252 if (needs_close) close( fd );
2254 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2255 break;
2257 case FileEndOfFileInformation:
2258 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2260 struct stat st;
2261 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2263 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2264 return io->u.Status;
2266 /* first try normal truncate */
2267 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2269 /* now check for the need to extend the file */
2270 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2272 static const char zero;
2274 /* extend the file one byte beyond the requested size and then truncate it */
2275 /* this should work around ftruncate implementations that can't extend files */
2276 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2277 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2279 io->u.Status = FILE_GetNtStatus();
2281 if (needs_close) close( fd );
2283 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2284 break;
2286 case FileMailslotSetInformation:
2288 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2290 SERVER_START_REQ( set_mailslot_info )
2292 req->handle = wine_server_obj_handle( handle );
2293 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2294 req->read_timeout = info->ReadTimeout.QuadPart;
2295 io->u.Status = wine_server_call( req );
2297 SERVER_END_REQ;
2299 break;
2301 case FileCompletionInformation:
2302 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2304 FILE_COMPLETION_INFORMATION *info = ptr;
2306 SERVER_START_REQ( set_completion_info )
2308 req->handle = wine_server_obj_handle( handle );
2309 req->chandle = wine_server_obj_handle( info->CompletionPort );
2310 req->ckey = info->CompletionKey;
2311 io->u.Status = wine_server_call( req );
2313 SERVER_END_REQ;
2314 } else
2315 io->u.Status = STATUS_INVALID_PARAMETER_3;
2316 break;
2318 case FileAllInformation:
2319 io->u.Status = STATUS_INVALID_INFO_CLASS;
2320 break;
2322 case FileValidDataLengthInformation:
2323 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2325 struct stat st;
2326 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2328 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2329 return io->u.Status;
2331 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2332 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2333 io->u.Status = STATUS_INVALID_PARAMETER;
2334 else
2336 #ifdef HAVE_FALLOCATE
2337 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2339 NTSTATUS status = FILE_GetNtStatus();
2340 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2341 else io->u.Status = status;
2343 #else
2344 FIXME( "setting valid data length not supported\n" );
2345 #endif
2347 if (needs_close) close( fd );
2349 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2350 break;
2352 default:
2353 FIXME("Unsupported class (%d)\n", class);
2354 io->u.Status = STATUS_NOT_IMPLEMENTED;
2355 break;
2357 io->Information = 0;
2358 return io->u.Status;
2362 /******************************************************************************
2363 * NtQueryFullAttributesFile (NTDLL.@)
2365 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2366 FILE_NETWORK_OPEN_INFORMATION *info )
2368 ANSI_STRING unix_name;
2369 NTSTATUS status;
2371 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2373 struct stat st;
2375 if (stat( unix_name.Buffer, &st ) == -1)
2376 status = FILE_GetNtStatus();
2377 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2378 status = STATUS_INVALID_INFO_CLASS;
2379 else
2381 FILE_BASIC_INFORMATION basic;
2382 FILE_STANDARD_INFORMATION std;
2384 fill_stat_info( &st, &basic, FileBasicInformation );
2385 fill_stat_info( &st, &std, FileStandardInformation );
2387 info->CreationTime = basic.CreationTime;
2388 info->LastAccessTime = basic.LastAccessTime;
2389 info->LastWriteTime = basic.LastWriteTime;
2390 info->ChangeTime = basic.ChangeTime;
2391 info->AllocationSize = std.AllocationSize;
2392 info->EndOfFile = std.EndOfFile;
2393 info->FileAttributes = basic.FileAttributes;
2394 if (DIR_is_hidden_file( attr->ObjectName ))
2395 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2397 RtlFreeAnsiString( &unix_name );
2399 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2400 return status;
2404 /******************************************************************************
2405 * NtQueryAttributesFile (NTDLL.@)
2406 * ZwQueryAttributesFile (NTDLL.@)
2408 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2410 ANSI_STRING unix_name;
2411 NTSTATUS status;
2413 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2415 struct stat st;
2417 if (stat( unix_name.Buffer, &st ) == -1)
2418 status = FILE_GetNtStatus();
2419 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2420 status = STATUS_INVALID_INFO_CLASS;
2421 else
2423 status = fill_stat_info( &st, info, FileBasicInformation );
2424 if (DIR_is_hidden_file( attr->ObjectName ))
2425 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2427 RtlFreeAnsiString( &unix_name );
2429 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2430 return status;
2434 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2435 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2436 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2437 unsigned int flags )
2439 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2441 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2442 /* Don't assume read-only, let the mount options set it below */
2443 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2445 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2446 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2448 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2449 info->Characteristics |= FILE_REMOTE_DEVICE;
2451 else if (!strcmp("procfs", fstypename))
2452 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2453 else
2454 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2456 if (flags & MNT_RDONLY)
2457 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2459 if (!(flags & MNT_LOCAL))
2461 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2462 info->Characteristics |= FILE_REMOTE_DEVICE;
2465 #endif
2467 static inline int is_device_placeholder( int fd )
2469 static const char wine_placeholder[] = "Wine device placeholder";
2470 char buffer[sizeof(wine_placeholder)-1];
2472 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2473 return 0;
2474 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2477 /******************************************************************************
2478 * get_device_info
2480 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2482 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2484 struct stat st;
2486 info->Characteristics = 0;
2487 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2488 if (S_ISCHR( st.st_mode ))
2490 info->DeviceType = FILE_DEVICE_UNKNOWN;
2491 #ifdef linux
2492 switch(major(st.st_rdev))
2494 case MEM_MAJOR:
2495 info->DeviceType = FILE_DEVICE_NULL;
2496 break;
2497 case TTY_MAJOR:
2498 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2499 break;
2500 case LP_MAJOR:
2501 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2502 break;
2503 case SCSI_TAPE_MAJOR:
2504 info->DeviceType = FILE_DEVICE_TAPE;
2505 break;
2507 #endif
2509 else if (S_ISBLK( st.st_mode ))
2511 info->DeviceType = FILE_DEVICE_DISK;
2513 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2515 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2517 else if (is_device_placeholder( fd ))
2519 info->DeviceType = FILE_DEVICE_DISK;
2521 else /* regular file or directory */
2523 #if defined(linux) && defined(HAVE_FSTATFS)
2524 struct statfs stfs;
2526 /* check for floppy disk */
2527 if (major(st.st_dev) == FLOPPY_MAJOR)
2528 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2530 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2531 switch (stfs.f_type)
2533 case 0x9660: /* iso9660 */
2534 case 0x9fa1: /* supermount */
2535 case 0x15013346: /* udf */
2536 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2537 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2538 break;
2539 case 0x6969: /* nfs */
2540 case 0x517B: /* smbfs */
2541 case 0x564c: /* ncpfs */
2542 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2543 info->Characteristics |= FILE_REMOTE_DEVICE;
2544 break;
2545 case 0x01021994: /* tmpfs */
2546 case 0x28cd3d45: /* cramfs */
2547 case 0x1373: /* devfs */
2548 case 0x9fa0: /* procfs */
2549 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2550 break;
2551 default:
2552 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2553 break;
2555 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2556 struct statfs stfs;
2558 if (fstatfs( fd, &stfs ) < 0)
2559 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2560 else
2561 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2562 #elif defined(__NetBSD__)
2563 struct statvfs stfs;
2565 if (fstatvfs( fd, &stfs) < 0)
2566 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2567 else
2568 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2569 #elif defined(sun)
2570 /* Use dkio to work out device types */
2572 # include <sys/dkio.h>
2573 # include <sys/vtoc.h>
2574 struct dk_cinfo dkinf;
2575 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2576 if(retval==-1){
2577 WARN("Unable to get disk device type information - assuming a disk like device\n");
2578 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2580 switch (dkinf.dki_ctype)
2582 case DKC_CDROM:
2583 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2584 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2585 break;
2586 case DKC_NCRFLOPPY:
2587 case DKC_SMSFLOPPY:
2588 case DKC_INTEL82072:
2589 case DKC_INTEL82077:
2590 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2591 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2592 break;
2593 case DKC_MD:
2594 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2595 break;
2596 default:
2597 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2600 #else
2601 static int warned;
2602 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2603 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2604 #endif
2605 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2607 return STATUS_SUCCESS;
2611 /******************************************************************************
2612 * NtQueryVolumeInformationFile [NTDLL.@]
2613 * ZwQueryVolumeInformationFile [NTDLL.@]
2615 * Get volume information for an open file handle.
2617 * PARAMS
2618 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2619 * io [O] Receives information about the operation on return
2620 * buffer [O] Destination for volume information
2621 * length [I] Size of FsInformation
2622 * info_class [I] Type of volume information to set
2624 * RETURNS
2625 * Success: 0. io and buffer are updated.
2626 * Failure: An NTSTATUS error code describing the error.
2628 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2629 PVOID buffer, ULONG length,
2630 FS_INFORMATION_CLASS info_class )
2632 int fd, needs_close;
2633 struct stat st;
2634 static int once;
2636 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2637 return io->u.Status;
2639 io->u.Status = STATUS_NOT_IMPLEMENTED;
2640 io->Information = 0;
2642 switch( info_class )
2644 case FileFsVolumeInformation:
2645 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2646 break;
2647 case FileFsLabelInformation:
2648 FIXME( "%p: label info not supported\n", handle );
2649 break;
2650 case FileFsSizeInformation:
2651 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2652 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2653 else
2655 FILE_FS_SIZE_INFORMATION *info = buffer;
2657 if (fstat( fd, &st ) < 0)
2659 io->u.Status = FILE_GetNtStatus();
2660 break;
2662 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2664 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2666 else
2668 ULONGLONG bsize;
2669 /* Linux's fstatvfs is buggy */
2670 #if !defined(linux) || !defined(HAVE_FSTATFS)
2671 struct statvfs stfs;
2673 if (fstatvfs( fd, &stfs ) < 0)
2675 io->u.Status = FILE_GetNtStatus();
2676 break;
2678 bsize = stfs.f_frsize;
2679 #else
2680 struct statfs stfs;
2681 if (fstatfs( fd, &stfs ) < 0)
2683 io->u.Status = FILE_GetNtStatus();
2684 break;
2686 bsize = stfs.f_bsize;
2687 #endif
2688 if (bsize == 2048) /* assume CD-ROM */
2690 info->BytesPerSector = 2048;
2691 info->SectorsPerAllocationUnit = 1;
2693 else
2695 info->BytesPerSector = 512;
2696 info->SectorsPerAllocationUnit = 8;
2698 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2699 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2700 io->Information = sizeof(*info);
2701 io->u.Status = STATUS_SUCCESS;
2704 break;
2705 case FileFsDeviceInformation:
2706 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2707 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2708 else
2710 FILE_FS_DEVICE_INFORMATION *info = buffer;
2712 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2713 io->Information = sizeof(*info);
2715 break;
2716 case FileFsAttributeInformation:
2717 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
2718 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2719 else
2721 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
2723 FIXME( "%p: faking attribute info\n", handle );
2724 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
2725 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
2726 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
2727 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
2728 info->FileSystemNameLength = sizeof(ntfsW);
2729 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
2731 io->Information = sizeof(*info);
2732 io->u.Status = STATUS_SUCCESS;
2734 break;
2735 case FileFsControlInformation:
2736 FIXME( "%p: control info not supported\n", handle );
2737 break;
2738 case FileFsFullSizeInformation:
2739 FIXME( "%p: full size info not supported\n", handle );
2740 break;
2741 case FileFsObjectIdInformation:
2742 FIXME( "%p: object id info not supported\n", handle );
2743 break;
2744 case FileFsMaximumInformation:
2745 FIXME( "%p: maximum info not supported\n", handle );
2746 break;
2747 default:
2748 io->u.Status = STATUS_INVALID_PARAMETER;
2749 break;
2751 if (needs_close) close( fd );
2752 return io->u.Status;
2756 /******************************************************************
2757 * NtQueryEaFile (NTDLL.@)
2759 * Read extended attributes from NTFS files.
2761 * PARAMS
2762 * hFile [I] File handle, must be opened with FILE_READ_EA access
2763 * iosb [O] Receives information about the operation on return
2764 * buffer [O] Output buffer
2765 * length [I] Length of output buffer
2766 * single_entry [I] Only read and return one entry
2767 * ea_list [I] Optional list with names of EAs to return
2768 * ea_list_len [I] Length of ea_list in bytes
2769 * ea_index [I] Optional pointer to 1-based index of attribute to return
2770 * restart [I] restart EA scan
2772 * RETURNS
2773 * Success: 0. Atrributes read into buffer
2774 * Failure: An NTSTATUS error code describing the error.
2776 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
2777 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
2778 PULONG ea_index, BOOLEAN restart )
2780 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
2781 hFile, iosb, buffer, length, single_entry, ea_list,
2782 ea_list_len, ea_index, restart);
2783 return STATUS_ACCESS_DENIED;
2787 /******************************************************************
2788 * NtSetEaFile (NTDLL.@)
2790 * Update extended attributes for NTFS files.
2792 * PARAMS
2793 * hFile [I] File handle, must be opened with FILE_READ_EA access
2794 * iosb [O] Receives information about the operation on return
2795 * buffer [I] Buffer with EA information
2796 * length [I] Length of buffer
2798 * RETURNS
2799 * Success: 0. Attributes are updated
2800 * Failure: An NTSTATUS error code describing the error.
2802 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
2804 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
2805 return STATUS_ACCESS_DENIED;
2809 /******************************************************************
2810 * NtFlushBuffersFile (NTDLL.@)
2812 * Flush any buffered data on an open file handle.
2814 * PARAMS
2815 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2816 * IoStatusBlock [O] Receives information about the operation on return
2818 * RETURNS
2819 * Success: 0. IoStatusBlock is updated.
2820 * Failure: An NTSTATUS error code describing the error.
2822 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2824 NTSTATUS ret;
2825 HANDLE hEvent = NULL;
2826 enum server_fd_type type;
2827 int fd, needs_close;
2829 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
2831 if (!ret && type == FD_TYPE_SERIAL)
2833 ret = COMM_FlushBuffersFile( fd );
2835 else
2837 SERVER_START_REQ( flush_file )
2839 req->handle = wine_server_obj_handle( hFile );
2840 ret = wine_server_call( req );
2841 hEvent = wine_server_ptr_handle( reply->event );
2843 SERVER_END_REQ;
2844 if (!ret && hEvent)
2846 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2847 NtClose( hEvent );
2851 if (needs_close) close( fd );
2852 return ret;
2855 /******************************************************************
2856 * NtLockFile (NTDLL.@)
2860 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2861 PIO_APC_ROUTINE apc, void* apc_user,
2862 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2863 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2864 BOOLEAN exclusive )
2866 NTSTATUS ret;
2867 HANDLE handle;
2868 BOOLEAN async;
2869 static BOOLEAN warn = TRUE;
2871 if (apc || io_status || key)
2873 FIXME("Unimplemented yet parameter\n");
2874 return STATUS_NOT_IMPLEMENTED;
2877 if (apc_user && warn)
2879 FIXME("I/O completion on lock not implemented yet\n");
2880 warn = FALSE;
2883 for (;;)
2885 SERVER_START_REQ( lock_file )
2887 req->handle = wine_server_obj_handle( hFile );
2888 req->offset = offset->QuadPart;
2889 req->count = count->QuadPart;
2890 req->shared = !exclusive;
2891 req->wait = !dont_wait;
2892 ret = wine_server_call( req );
2893 handle = wine_server_ptr_handle( reply->handle );
2894 async = reply->overlapped;
2896 SERVER_END_REQ;
2897 if (ret != STATUS_PENDING)
2899 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2900 return ret;
2903 if (async)
2905 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2906 if (handle) NtClose( handle );
2907 return STATUS_PENDING;
2909 if (handle)
2911 NtWaitForSingleObject( handle, FALSE, NULL );
2912 NtClose( handle );
2914 else
2916 LARGE_INTEGER time;
2918 /* Unix lock conflict, sleep a bit and retry */
2919 time.QuadPart = 100 * (ULONGLONG)10000;
2920 time.QuadPart = -time.QuadPart;
2921 NtDelayExecution( FALSE, &time );
2927 /******************************************************************
2928 * NtUnlockFile (NTDLL.@)
2932 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2933 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2934 PULONG key )
2936 NTSTATUS status;
2938 TRACE( "%p %x%08x %x%08x\n",
2939 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2941 if (io_status || key)
2943 FIXME("Unimplemented yet parameter\n");
2944 return STATUS_NOT_IMPLEMENTED;
2947 SERVER_START_REQ( unlock_file )
2949 req->handle = wine_server_obj_handle( hFile );
2950 req->offset = offset->QuadPart;
2951 req->count = count->QuadPart;
2952 status = wine_server_call( req );
2954 SERVER_END_REQ;
2955 return status;
2958 /******************************************************************
2959 * NtCreateNamedPipeFile (NTDLL.@)
2963 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2964 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2965 ULONG sharing, ULONG dispo, ULONG options,
2966 ULONG pipe_type, ULONG read_mode,
2967 ULONG completion_mode, ULONG max_inst,
2968 ULONG inbound_quota, ULONG outbound_quota,
2969 PLARGE_INTEGER timeout)
2971 NTSTATUS status;
2973 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2974 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2975 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2976 outbound_quota, timeout);
2978 /* assume we only get relative timeout */
2979 if (timeout->QuadPart > 0)
2980 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2982 SERVER_START_REQ( create_named_pipe )
2984 req->access = access;
2985 req->attributes = attr->Attributes;
2986 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2987 req->options = options;
2988 req->sharing = sharing;
2989 req->flags =
2990 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
2991 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
2992 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
2993 req->maxinstances = max_inst;
2994 req->outsize = outbound_quota;
2995 req->insize = inbound_quota;
2996 req->timeout = timeout->QuadPart;
2997 wine_server_add_data( req, attr->ObjectName->Buffer,
2998 attr->ObjectName->Length );
2999 status = wine_server_call( req );
3000 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3002 SERVER_END_REQ;
3003 return status;
3006 /******************************************************************
3007 * NtDeleteFile (NTDLL.@)
3011 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3013 NTSTATUS status;
3014 HANDLE hFile;
3015 IO_STATUS_BLOCK io;
3017 TRACE("%p\n", ObjectAttributes);
3018 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3019 ObjectAttributes, &io, NULL, 0,
3020 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3021 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3022 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3023 return status;
3026 /******************************************************************
3027 * NtCancelIoFileEx (NTDLL.@)
3031 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3033 LARGE_INTEGER timeout;
3035 TRACE("%p %p %p\n", hFile, iosb, io_status );
3037 SERVER_START_REQ( cancel_async )
3039 req->handle = wine_server_obj_handle( hFile );
3040 req->iosb = wine_server_client_ptr( iosb );
3041 req->only_thread = FALSE;
3042 io_status->u.Status = wine_server_call( req );
3044 SERVER_END_REQ;
3045 if (io_status->u.Status)
3046 return io_status->u.Status;
3048 /* Let some APC be run, so that we can run the remaining APCs on hFile
3049 * either the cancelation of the pending one, but also the execution
3050 * of the queued APC, but not yet run. This is needed to ensure proper
3051 * clean-up of allocated data.
3053 timeout.QuadPart = 0;
3054 NtDelayExecution( TRUE, &timeout );
3055 return io_status->u.Status;
3058 /******************************************************************
3059 * NtCancelIoFile (NTDLL.@)
3063 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3065 LARGE_INTEGER timeout;
3067 TRACE("%p %p\n", hFile, io_status );
3069 SERVER_START_REQ( cancel_async )
3071 req->handle = wine_server_obj_handle( hFile );
3072 req->iosb = 0;
3073 req->only_thread = TRUE;
3074 io_status->u.Status = wine_server_call( req );
3076 SERVER_END_REQ;
3077 if (io_status->u.Status)
3078 return io_status->u.Status;
3080 /* Let some APC be run, so that we can run the remaining APCs on hFile
3081 * either the cancelation of the pending one, but also the execution
3082 * of the queued APC, but not yet run. This is needed to ensure proper
3083 * clean-up of allocated data.
3085 timeout.QuadPart = 0;
3086 NtDelayExecution( TRUE, &timeout );
3087 return io_status->u.Status;
3090 /******************************************************************************
3091 * NtCreateMailslotFile [NTDLL.@]
3092 * ZwCreateMailslotFile [NTDLL.@]
3094 * PARAMS
3095 * pHandle [O] pointer to receive the handle created
3096 * DesiredAccess [I] access mode (read, write, etc)
3097 * ObjectAttributes [I] fully qualified NT path of the mailslot
3098 * IoStatusBlock [O] receives completion status and other info
3099 * CreateOptions [I]
3100 * MailslotQuota [I]
3101 * MaxMessageSize [I]
3102 * TimeOut [I]
3104 * RETURNS
3105 * An NT status code
3107 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3108 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3109 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3110 PLARGE_INTEGER TimeOut)
3112 LARGE_INTEGER timeout;
3113 NTSTATUS ret;
3115 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3116 pHandle, DesiredAccess, attr, IoStatusBlock,
3117 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3119 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3120 if (!attr) return STATUS_INVALID_PARAMETER;
3121 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3124 * For a NULL TimeOut pointer set the default timeout value
3126 if (!TimeOut)
3127 timeout.QuadPart = -1;
3128 else
3129 timeout.QuadPart = TimeOut->QuadPart;
3131 SERVER_START_REQ( create_mailslot )
3133 req->access = DesiredAccess;
3134 req->attributes = attr->Attributes;
3135 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3136 req->max_msgsize = MaxMessageSize;
3137 req->read_timeout = timeout.QuadPart;
3138 wine_server_add_data( req, attr->ObjectName->Buffer,
3139 attr->ObjectName->Length );
3140 ret = wine_server_call( req );
3141 if( ret == STATUS_SUCCESS )
3142 *pHandle = wine_server_ptr_handle( reply->handle );
3144 SERVER_END_REQ;
3146 return ret;