vbscript: Added IRegExp2_QueryInterface tests.
[wine/multimedia.git] / dlls / ntdll / file.c
blob791570e01ee48b479381ca5b42b3a7c737c0a250
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_VALGRIND_MEMCHECK_H
70 # include <valgrind/memcheck.h>
71 #endif
73 #define NONAMELESSUNION
74 #define NONAMELESSSTRUCT
75 #include "ntstatus.h"
76 #define WIN32_NO_STATUS
77 #include "wine/unicode.h"
78 #include "wine/debug.h"
79 #include "wine/server.h"
80 #include "ntdll_misc.h"
82 #include "winternl.h"
83 #include "winioctl.h"
84 #include "ddk/ntddk.h"
85 #include "ddk/ntddser.h"
87 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
88 WINE_DECLARE_DEBUG_CHANNEL(winediag);
90 mode_t FILE_umask = 0;
92 #define SECSPERDAY 86400
93 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
96 /**************************************************************************
97 * FILE_CreateFile (internal)
98 * Open a file.
100 * Parameter set fully identical with NtCreateFile
102 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
103 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
104 ULONG attributes, ULONG sharing, ULONG disposition,
105 ULONG options, PVOID ea_buffer, ULONG ea_length )
107 ANSI_STRING unix_name;
108 int created = FALSE;
110 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
111 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
112 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
113 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
114 attributes, sharing, disposition, options, ea_buffer, ea_length );
116 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
118 if (alloc_size) FIXME( "alloc_size not supported\n" );
120 if (options & FILE_OPEN_BY_FILE_ID)
121 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
122 else
123 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
125 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
127 SERVER_START_REQ( open_file_object )
129 req->access = access;
130 req->attributes = attr->Attributes;
131 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
132 req->sharing = sharing;
133 req->options = options;
134 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
135 io->u.Status = wine_server_call( req );
136 *handle = wine_server_ptr_handle( reply->handle );
138 SERVER_END_REQ;
139 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
140 return io->u.Status;
143 if (io->u.Status == STATUS_NO_SUCH_FILE &&
144 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
146 created = TRUE;
147 io->u.Status = STATUS_SUCCESS;
150 if (io->u.Status == STATUS_SUCCESS)
152 struct security_descriptor *sd;
153 struct object_attributes objattr;
155 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
156 objattr.name_len = 0;
157 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
158 if (io->u.Status != STATUS_SUCCESS)
160 RtlFreeAnsiString( &unix_name );
161 return io->u.Status;
164 SERVER_START_REQ( create_file )
166 req->access = access;
167 req->attributes = attr->Attributes;
168 req->sharing = sharing;
169 req->create = disposition;
170 req->options = options;
171 req->attrs = attributes;
172 wine_server_add_data( req, &objattr, sizeof(objattr) );
173 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
174 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
175 io->u.Status = wine_server_call( req );
176 *handle = wine_server_ptr_handle( reply->handle );
178 SERVER_END_REQ;
179 NTDLL_free_struct_sd( sd );
180 RtlFreeAnsiString( &unix_name );
182 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
184 if (io->u.Status == STATUS_SUCCESS)
186 if (created) io->Information = FILE_CREATED;
187 else switch(disposition)
189 case FILE_SUPERSEDE:
190 io->Information = FILE_SUPERSEDED;
191 break;
192 case FILE_CREATE:
193 io->Information = FILE_CREATED;
194 break;
195 case FILE_OPEN:
196 case FILE_OPEN_IF:
197 io->Information = FILE_OPENED;
198 break;
199 case FILE_OVERWRITE:
200 case FILE_OVERWRITE_IF:
201 io->Information = FILE_OVERWRITTEN;
202 break;
205 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
207 static int once;
208 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
211 return io->u.Status;
214 /**************************************************************************
215 * NtOpenFile [NTDLL.@]
216 * ZwOpenFile [NTDLL.@]
218 * Open a file.
220 * PARAMS
221 * handle [O] Variable that receives the file handle on return
222 * access [I] Access desired by the caller to the file
223 * attr [I] Structure describing the file to be opened
224 * io [O] Receives details about the result of the operation
225 * sharing [I] Type of shared access the caller requires
226 * options [I] Options for the file open
228 * RETURNS
229 * Success: 0. FileHandle and IoStatusBlock are updated.
230 * Failure: An NTSTATUS error code describing the error.
232 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
233 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
234 ULONG sharing, ULONG options )
236 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
237 sharing, FILE_OPEN, options, NULL, 0 );
240 /**************************************************************************
241 * NtCreateFile [NTDLL.@]
242 * ZwCreateFile [NTDLL.@]
244 * Either create a new file or directory, or open an existing file, device,
245 * directory or volume.
247 * PARAMS
248 * handle [O] Points to a variable which receives the file handle on return
249 * access [I] Desired access to the file
250 * attr [I] Structure describing the file
251 * io [O] Receives information about the operation on return
252 * alloc_size [I] Initial size of the file in bytes
253 * attributes [I] Attributes to create the file with
254 * sharing [I] Type of shared access the caller would like to the file
255 * disposition [I] Specifies what to do, depending on whether the file already exists
256 * options [I] Options for creating a new file
257 * ea_buffer [I] Pointer to an extended attributes buffer
258 * ea_length [I] Length of ea_buffer
260 * RETURNS
261 * Success: 0. handle and io are updated.
262 * Failure: An NTSTATUS error code describing the error.
264 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
265 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
266 ULONG attributes, ULONG sharing, ULONG disposition,
267 ULONG options, PVOID ea_buffer, ULONG ea_length )
269 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
270 sharing, disposition, options, ea_buffer, ea_length );
273 /***********************************************************************
274 * Asynchronous file I/O *
277 struct async_fileio
279 HANDLE handle;
280 PIO_APC_ROUTINE apc;
281 void *apc_arg;
284 typedef struct
286 struct async_fileio io;
287 char* buffer;
288 unsigned int already;
289 unsigned int count;
290 BOOL avail_mode;
291 } async_fileio_read;
293 typedef struct
295 struct async_fileio io;
296 const char *buffer;
297 unsigned int already;
298 unsigned int count;
299 } async_fileio_write;
302 /* callback for file I/O user APC */
303 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
305 struct async_fileio *async = arg;
306 if (async->apc) async->apc( async->apc_arg, io, reserved );
307 RtlFreeHeap( GetProcessHeap(), 0, async );
310 /***********************************************************************
311 * FILE_GetNtStatus(void)
313 * Retrieve the Nt Status code from errno.
314 * Try to be consistent with FILE_SetDosError().
316 NTSTATUS FILE_GetNtStatus(void)
318 int err = errno;
320 TRACE( "errno = %d\n", errno );
321 switch (err)
323 case EAGAIN: return STATUS_SHARING_VIOLATION;
324 case EBADF: return STATUS_INVALID_HANDLE;
325 case EBUSY: return STATUS_DEVICE_BUSY;
326 case ENOSPC: return STATUS_DISK_FULL;
327 case EPERM:
328 case EROFS:
329 case EACCES: return STATUS_ACCESS_DENIED;
330 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
331 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
332 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
333 case EMFILE:
334 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
335 case EINVAL: return STATUS_INVALID_PARAMETER;
336 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
337 case EPIPE: return STATUS_PIPE_DISCONNECTED;
338 case EIO: return STATUS_DEVICE_NOT_READY;
339 #ifdef ENOMEDIUM
340 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
341 #endif
342 case ENXIO: return STATUS_NO_SUCH_DEVICE;
343 case ENOTTY:
344 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
345 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
346 case EFAULT: return STATUS_ACCESS_VIOLATION;
347 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
348 #ifdef ETIME /* Missing on FreeBSD */
349 case ETIME: return STATUS_IO_TIMEOUT;
350 #endif
351 case ENOEXEC: /* ?? */
352 case EEXIST: /* ?? */
353 default:
354 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
355 return STATUS_UNSUCCESSFUL;
359 /***********************************************************************
360 * FILE_AsyncReadService (INTERNAL)
362 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc)
364 async_fileio_read *fileio = user;
365 int fd, needs_close, result;
367 switch (status)
369 case STATUS_ALERTED: /* got some new data */
370 /* check to see if the data is ready (non-blocking) */
371 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
372 &needs_close, NULL, NULL )))
373 break;
375 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
376 if (needs_close) close( fd );
378 if (result < 0)
380 if (errno == EAGAIN || errno == EINTR)
381 status = STATUS_PENDING;
382 else /* check to see if the transfer is complete */
383 status = FILE_GetNtStatus();
385 else if (result == 0)
387 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
389 else
391 fileio->already += result;
392 if (fileio->already >= fileio->count || fileio->avail_mode)
393 status = STATUS_SUCCESS;
394 else
396 /* if we only have to read the available data, and none is available,
397 * simply cancel the request. If data was available, it has been read
398 * while in by previous call (NtDelayExecution)
400 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
403 break;
405 case STATUS_TIMEOUT:
406 case STATUS_IO_TIMEOUT:
407 if (fileio->already) status = STATUS_SUCCESS;
408 break;
410 if (status != STATUS_PENDING)
412 iosb->u.Status = status;
413 iosb->Information = fileio->already;
414 *apc = fileio_apc;
416 return status;
419 struct io_timeouts
421 int interval; /* max interval between two bytes */
422 int total; /* total timeout for the whole operation */
423 int end_time; /* absolute time of end of operation */
426 /* retrieve the I/O timeouts to use for a given handle */
427 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
428 struct io_timeouts *timeouts )
430 NTSTATUS status = STATUS_SUCCESS;
432 timeouts->interval = timeouts->total = -1;
434 switch(type)
436 case FD_TYPE_SERIAL:
438 /* GetCommTimeouts */
439 SERIAL_TIMEOUTS st;
440 IO_STATUS_BLOCK io;
442 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
443 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
444 if (status) break;
446 if (is_read)
448 if (st.ReadIntervalTimeout)
449 timeouts->interval = st.ReadIntervalTimeout;
451 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
453 timeouts->total = st.ReadTotalTimeoutConstant;
454 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
455 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
457 else if (st.ReadIntervalTimeout == MAXDWORD)
458 timeouts->interval = timeouts->total = 0;
460 else /* write */
462 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
464 timeouts->total = st.WriteTotalTimeoutConstant;
465 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
466 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
470 break;
471 case FD_TYPE_MAILSLOT:
472 if (is_read)
474 timeouts->interval = 0; /* return as soon as we got something */
475 SERVER_START_REQ( set_mailslot_info )
477 req->handle = wine_server_obj_handle( handle );
478 req->flags = 0;
479 if (!(status = wine_server_call( req )) &&
480 reply->read_timeout != TIMEOUT_INFINITE)
481 timeouts->total = reply->read_timeout / -10000;
483 SERVER_END_REQ;
485 break;
486 case FD_TYPE_SOCKET:
487 case FD_TYPE_PIPE:
488 case FD_TYPE_CHAR:
489 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
490 break;
491 default:
492 break;
494 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
495 return STATUS_SUCCESS;
499 /* retrieve the timeout for the next wait, in milliseconds */
500 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
502 int ret = -1;
504 if (timeouts->total != -1)
506 ret = timeouts->end_time - NtGetTickCount();
507 if (ret < 0) ret = 0;
509 if (already && timeouts->interval != -1)
511 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
513 return ret;
517 /* retrieve the avail_mode flag for async reads */
518 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
520 NTSTATUS status = STATUS_SUCCESS;
522 switch(type)
524 case FD_TYPE_SERIAL:
526 /* GetCommTimeouts */
527 SERIAL_TIMEOUTS st;
528 IO_STATUS_BLOCK io;
530 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
531 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
532 if (status) break;
533 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
534 !st.ReadTotalTimeoutConstant &&
535 st.ReadIntervalTimeout == MAXDWORD);
537 break;
538 case FD_TYPE_MAILSLOT:
539 case FD_TYPE_SOCKET:
540 case FD_TYPE_PIPE:
541 case FD_TYPE_CHAR:
542 *avail_mode = TRUE;
543 break;
544 default:
545 *avail_mode = FALSE;
546 break;
548 return status;
552 /******************************************************************************
553 * NtReadFile [NTDLL.@]
554 * ZwReadFile [NTDLL.@]
556 * Read from an open file handle.
558 * PARAMS
559 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
560 * Event [I] Event to signal upon completion (or NULL)
561 * ApcRoutine [I] Callback to call upon completion (or NULL)
562 * ApcContext [I] Context for ApcRoutine (or NULL)
563 * IoStatusBlock [O] Receives information about the operation on return
564 * Buffer [O] Destination for the data read
565 * Length [I] Size of Buffer
566 * ByteOffset [O] Destination for the new file pointer position (or NULL)
567 * Key [O] Function unknown (may be NULL)
569 * RETURNS
570 * Success: 0. IoStatusBlock is updated, and the Information member contains
571 * The number of bytes read.
572 * Failure: An NTSTATUS error code describing the error.
574 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
575 PIO_APC_ROUTINE apc, void* apc_user,
576 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
577 PLARGE_INTEGER offset, PULONG key)
579 int result, unix_handle, needs_close, timeout_init_done = 0;
580 unsigned int options;
581 struct io_timeouts timeouts;
582 NTSTATUS status;
583 ULONG total = 0;
584 enum server_fd_type type;
585 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
586 BOOL send_completion = FALSE;
588 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
589 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
591 if (!io_status) return STATUS_ACCESS_VIOLATION;
593 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
594 &needs_close, &type, &options );
595 if (status) return status;
597 if (!virtual_check_buffer_for_write( buffer, length ))
599 status = STATUS_ACCESS_VIOLATION;
600 goto done;
603 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
605 /* async I/O doesn't make sense on regular files */
606 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
608 if (errno != EINTR)
610 status = FILE_GetNtStatus();
611 goto done;
614 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
615 /* update file pointer position */
616 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
618 total = result;
619 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
620 goto done;
623 for (;;)
625 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
627 total += result;
628 if (!result || total == length)
630 if (total)
632 status = STATUS_SUCCESS;
633 goto done;
635 switch (type)
637 case FD_TYPE_FILE:
638 case FD_TYPE_CHAR:
639 status = STATUS_END_OF_FILE;
640 goto done;
641 case FD_TYPE_SERIAL:
642 break;
643 default:
644 status = STATUS_PIPE_BROKEN;
645 goto done;
648 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
650 else if (errno != EAGAIN)
652 if (errno == EINTR) continue;
653 if (!total) status = FILE_GetNtStatus();
654 goto done;
657 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
659 async_fileio_read *fileio;
660 BOOL avail_mode;
662 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
663 goto err;
664 if (total && avail_mode)
666 status = STATUS_SUCCESS;
667 goto done;
670 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
672 status = STATUS_NO_MEMORY;
673 goto err;
675 fileio->io.handle = hFile;
676 fileio->io.apc = apc;
677 fileio->io.apc_arg = apc_user;
678 fileio->already = total;
679 fileio->count = length;
680 fileio->buffer = buffer;
681 fileio->avail_mode = avail_mode;
683 SERVER_START_REQ( register_async )
685 req->type = ASYNC_TYPE_READ;
686 req->count = length;
687 req->async.handle = wine_server_obj_handle( hFile );
688 req->async.event = wine_server_obj_handle( hEvent );
689 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
690 req->async.iosb = wine_server_client_ptr( io_status );
691 req->async.arg = wine_server_client_ptr( fileio );
692 req->async.cvalue = cvalue;
693 status = wine_server_call( req );
695 SERVER_END_REQ;
697 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
698 goto err;
700 else /* synchronous read, wait for the fd to become ready */
702 struct pollfd pfd;
703 int ret, timeout;
705 if (!timeout_init_done)
707 timeout_init_done = 1;
708 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
709 goto err;
710 if (hEvent) NtResetEvent( hEvent, NULL );
712 timeout = get_next_io_timeout( &timeouts, total );
714 pfd.fd = unix_handle;
715 pfd.events = POLLIN;
717 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
719 if (total) /* return with what we got so far */
720 status = STATUS_SUCCESS;
721 else
722 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
723 goto done;
725 if (ret == -1 && errno != EINTR)
727 status = FILE_GetNtStatus();
728 goto done;
730 /* will now restart the read */
734 done:
735 send_completion = cvalue != 0;
737 err:
738 if (needs_close) close( unix_handle );
739 if (status == STATUS_SUCCESS)
741 io_status->u.Status = status;
742 io_status->Information = total;
743 TRACE("= SUCCESS (%u)\n", total);
744 if (hEvent) NtSetEvent( hEvent, NULL );
745 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
746 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
748 else
750 TRACE("= 0x%08x\n", status);
751 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
754 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
756 return status;
760 /******************************************************************************
761 * NtReadFileScatter [NTDLL.@]
762 * ZwReadFileScatter [NTDLL.@]
764 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
765 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
766 ULONG length, PLARGE_INTEGER offset, PULONG key )
768 int result, unix_handle, needs_close;
769 unsigned int options;
770 NTSTATUS status;
771 ULONG pos = 0, total = 0;
772 enum server_fd_type type;
773 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
774 BOOL send_completion = FALSE;
776 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
777 file, event, apc, apc_user, io_status, segments, length, offset, key);
779 if (length % page_size) return STATUS_INVALID_PARAMETER;
780 if (!io_status) return STATUS_ACCESS_VIOLATION;
782 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
783 &needs_close, &type, &options );
784 if (status) return status;
786 if ((type != FD_TYPE_FILE) ||
787 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
788 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
790 status = STATUS_INVALID_PARAMETER;
791 goto error;
794 while (length)
796 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
797 result = pread( unix_handle, (char *)segments->Buffer + pos,
798 page_size - pos, offset->QuadPart + total );
799 else
800 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
802 if (result == -1)
804 if (errno == EINTR) continue;
805 status = FILE_GetNtStatus();
806 break;
808 if (!result)
810 status = STATUS_END_OF_FILE;
811 break;
813 total += result;
814 length -= result;
815 if ((pos += result) == page_size)
817 pos = 0;
818 segments++;
822 send_completion = cvalue != 0;
824 error:
825 if (needs_close) close( unix_handle );
826 if (status == STATUS_SUCCESS)
828 io_status->u.Status = status;
829 io_status->Information = total;
830 TRACE("= SUCCESS (%u)\n", total);
831 if (event) NtSetEvent( event, NULL );
832 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
833 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
835 else
837 TRACE("= 0x%08x\n", status);
838 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
841 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
843 return status;
847 /***********************************************************************
848 * FILE_AsyncWriteService (INTERNAL)
850 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status, void **apc)
852 async_fileio_write *fileio = user;
853 int result, fd, needs_close;
854 enum server_fd_type type;
856 switch (status)
858 case STATUS_ALERTED:
859 /* write some data (non-blocking) */
860 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
861 &needs_close, &type, NULL )))
862 break;
864 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
865 result = send( fd, fileio->buffer, 0, 0 );
866 else
867 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
869 if (needs_close) close( fd );
871 if (result < 0)
873 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
874 else status = FILE_GetNtStatus();
876 else
878 fileio->already += result;
879 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
881 break;
883 case STATUS_TIMEOUT:
884 case STATUS_IO_TIMEOUT:
885 if (fileio->already) status = STATUS_SUCCESS;
886 break;
888 if (status != STATUS_PENDING)
890 iosb->u.Status = status;
891 iosb->Information = fileio->already;
892 *apc = fileio_apc;
894 return status;
897 /******************************************************************************
898 * NtWriteFile [NTDLL.@]
899 * ZwWriteFile [NTDLL.@]
901 * Write to an open file handle.
903 * PARAMS
904 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
905 * Event [I] Event to signal upon completion (or NULL)
906 * ApcRoutine [I] Callback to call upon completion (or NULL)
907 * ApcContext [I] Context for ApcRoutine (or NULL)
908 * IoStatusBlock [O] Receives information about the operation on return
909 * Buffer [I] Source for the data to write
910 * Length [I] Size of Buffer
911 * ByteOffset [O] Destination for the new file pointer position (or NULL)
912 * Key [O] Function unknown (may be NULL)
914 * RETURNS
915 * Success: 0. IoStatusBlock is updated, and the Information member contains
916 * The number of bytes written.
917 * Failure: An NTSTATUS error code describing the error.
919 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
920 PIO_APC_ROUTINE apc, void* apc_user,
921 PIO_STATUS_BLOCK io_status,
922 const void* buffer, ULONG length,
923 PLARGE_INTEGER offset, PULONG key)
925 int result, unix_handle, needs_close, timeout_init_done = 0;
926 unsigned int options;
927 struct io_timeouts timeouts;
928 NTSTATUS status;
929 ULONG total = 0;
930 enum server_fd_type type;
931 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
932 BOOL send_completion = FALSE;
934 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
935 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
937 if (!io_status) return STATUS_ACCESS_VIOLATION;
939 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
940 &needs_close, &type, &options );
941 if (status) return status;
943 if (!virtual_check_buffer_for_read( buffer, length ))
945 status = STATUS_INVALID_USER_BUFFER;
946 goto done;
949 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
951 /* async I/O doesn't make sense on regular files */
952 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
954 if (errno != EINTR)
956 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
957 else status = FILE_GetNtStatus();
958 goto done;
962 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
963 /* update file pointer position */
964 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
966 total = result;
967 status = STATUS_SUCCESS;
968 goto done;
971 for (;;)
973 /* zero-length writes on sockets may not work with plain write(2) */
974 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
975 result = send( unix_handle, buffer, 0, 0 );
976 else
977 result = write( unix_handle, (const char *)buffer + total, length - total );
979 if (result >= 0)
981 total += result;
982 if (total == length)
984 status = STATUS_SUCCESS;
985 goto done;
987 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
989 else if (errno != EAGAIN)
991 if (errno == EINTR) continue;
992 if (!total)
994 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
995 else status = FILE_GetNtStatus();
997 goto done;
1000 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
1002 async_fileio_write *fileio;
1004 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
1006 status = STATUS_NO_MEMORY;
1007 goto err;
1009 fileio->io.handle = hFile;
1010 fileio->io.apc = apc;
1011 fileio->io.apc_arg = apc_user;
1012 fileio->already = total;
1013 fileio->count = length;
1014 fileio->buffer = buffer;
1016 SERVER_START_REQ( register_async )
1018 req->type = ASYNC_TYPE_WRITE;
1019 req->count = length;
1020 req->async.handle = wine_server_obj_handle( hFile );
1021 req->async.event = wine_server_obj_handle( hEvent );
1022 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1023 req->async.iosb = wine_server_client_ptr( io_status );
1024 req->async.arg = wine_server_client_ptr( fileio );
1025 req->async.cvalue = cvalue;
1026 status = wine_server_call( req );
1028 SERVER_END_REQ;
1030 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1031 goto err;
1033 else /* synchronous write, wait for the fd to become ready */
1035 struct pollfd pfd;
1036 int ret, timeout;
1038 if (!timeout_init_done)
1040 timeout_init_done = 1;
1041 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1042 goto err;
1043 if (hEvent) NtResetEvent( hEvent, NULL );
1045 timeout = get_next_io_timeout( &timeouts, total );
1047 pfd.fd = unix_handle;
1048 pfd.events = POLLOUT;
1050 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1052 /* return with what we got so far */
1053 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1054 goto done;
1056 if (ret == -1 && errno != EINTR)
1058 status = FILE_GetNtStatus();
1059 goto done;
1061 /* will now restart the write */
1065 done:
1066 send_completion = cvalue != 0;
1068 err:
1069 if (needs_close) close( unix_handle );
1070 if (status == STATUS_SUCCESS)
1072 io_status->u.Status = status;
1073 io_status->Information = total;
1074 TRACE("= SUCCESS (%u)\n", total);
1075 if (hEvent) NtSetEvent( hEvent, NULL );
1076 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1077 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1079 else
1081 TRACE("= 0x%08x\n", status);
1082 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1085 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1087 return status;
1091 /******************************************************************************
1092 * NtWriteFileGather [NTDLL.@]
1093 * ZwWriteFileGather [NTDLL.@]
1095 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1096 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1097 ULONG length, PLARGE_INTEGER offset, PULONG key )
1099 int result, unix_handle, needs_close;
1100 unsigned int options;
1101 NTSTATUS status;
1102 ULONG pos = 0, total = 0;
1103 enum server_fd_type type;
1104 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1105 BOOL send_completion = FALSE;
1107 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1108 file, event, apc, apc_user, io_status, segments, length, offset, key);
1110 if (length % page_size) return STATUS_INVALID_PARAMETER;
1111 if (!io_status) return STATUS_ACCESS_VIOLATION;
1113 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1114 &needs_close, &type, &options );
1115 if (status) return status;
1117 if ((type != FD_TYPE_FILE) ||
1118 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1119 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1121 status = STATUS_INVALID_PARAMETER;
1122 goto error;
1125 while (length)
1127 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
1128 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1129 page_size - pos, offset->QuadPart + total );
1130 else
1131 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1133 if (result == -1)
1135 if (errno == EINTR) continue;
1136 if (errno == EFAULT)
1138 status = STATUS_INVALID_USER_BUFFER;
1139 goto error;
1141 status = FILE_GetNtStatus();
1142 break;
1144 if (!result)
1146 status = STATUS_DISK_FULL;
1147 break;
1149 total += result;
1150 length -= result;
1151 if ((pos += result) == page_size)
1153 pos = 0;
1154 segments++;
1158 send_completion = cvalue != 0;
1160 error:
1161 if (needs_close) close( unix_handle );
1162 if (status == STATUS_SUCCESS)
1164 io_status->u.Status = status;
1165 io_status->Information = total;
1166 TRACE("= SUCCESS (%u)\n", total);
1167 if (event) NtSetEvent( event, NULL );
1168 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1169 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1171 else
1173 TRACE("= 0x%08x\n", status);
1174 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1177 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1179 return status;
1183 struct async_ioctl
1185 HANDLE handle; /* handle to the device */
1186 HANDLE event; /* async event */
1187 void *buffer; /* buffer for output */
1188 ULONG size; /* size of buffer */
1189 PIO_APC_ROUTINE apc; /* user apc params */
1190 void *apc_arg;
1193 /* callback for ioctl user APC */
1194 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
1196 struct async_ioctl *async = arg;
1197 if (async->apc) async->apc( async->apc_arg, io, reserved );
1198 RtlFreeHeap( GetProcessHeap(), 0, async );
1201 /* callback for ioctl async I/O completion */
1202 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status, void **apc )
1204 struct async_ioctl *async = arg;
1206 if (status == STATUS_ALERTED)
1208 SERVER_START_REQ( get_ioctl_result )
1210 req->handle = wine_server_obj_handle( async->handle );
1211 req->user_arg = wine_server_client_ptr( async );
1212 wine_server_set_reply( req, async->buffer, async->size );
1213 status = wine_server_call( req );
1214 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1216 SERVER_END_REQ;
1218 if (status != STATUS_PENDING)
1220 io->u.Status = status;
1221 if (async->apc || async->event) *apc = ioctl_apc;
1223 return status;
1226 /* do a ioctl call through the server */
1227 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1228 PIO_APC_ROUTINE apc, PVOID apc_context,
1229 IO_STATUS_BLOCK *io, ULONG code,
1230 const void *in_buffer, ULONG in_size,
1231 PVOID out_buffer, ULONG out_size )
1233 struct async_ioctl *async;
1234 NTSTATUS status;
1235 HANDLE wait_handle;
1236 ULONG options;
1237 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1239 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
1240 return STATUS_NO_MEMORY;
1241 async->handle = handle;
1242 async->event = event;
1243 async->buffer = out_buffer;
1244 async->size = out_size;
1245 async->apc = apc;
1246 async->apc_arg = apc_context;
1248 SERVER_START_REQ( ioctl )
1250 req->code = code;
1251 req->blocking = !apc && !event && !cvalue;
1252 req->async.handle = wine_server_obj_handle( handle );
1253 req->async.callback = wine_server_client_ptr( ioctl_completion );
1254 req->async.iosb = wine_server_client_ptr( io );
1255 req->async.arg = wine_server_client_ptr( async );
1256 req->async.event = wine_server_obj_handle( event );
1257 req->async.cvalue = cvalue;
1258 wine_server_add_data( req, in_buffer, in_size );
1259 wine_server_set_reply( req, out_buffer, out_size );
1260 status = wine_server_call( req );
1261 wait_handle = wine_server_ptr_handle( reply->wait );
1262 options = reply->options;
1263 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1265 SERVER_END_REQ;
1267 if (status == STATUS_NOT_SUPPORTED)
1268 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1269 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1271 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1273 if (wait_handle)
1275 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1276 status = io->u.Status;
1277 NtClose( wait_handle );
1278 RtlFreeHeap( GetProcessHeap(), 0, async );
1281 return status;
1284 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1285 * server */
1286 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1287 ULONG in_size)
1289 #ifdef VALGRIND_MAKE_MEM_DEFINED
1290 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1291 do { \
1292 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1293 if ((size) >= FIELD_OFFSET(t, f2)) \
1294 VALGRIND_MAKE_MEM_DEFINED( \
1295 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1296 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1297 } while (0)
1299 switch (code)
1301 case FSCTL_PIPE_WAIT:
1302 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1303 break;
1305 #endif
1309 /**************************************************************************
1310 * NtDeviceIoControlFile [NTDLL.@]
1311 * ZwDeviceIoControlFile [NTDLL.@]
1313 * Perform an I/O control operation on an open file handle.
1315 * PARAMS
1316 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1317 * event [I] Event to signal upon completion (or NULL)
1318 * apc [I] Callback to call upon completion (or NULL)
1319 * apc_context [I] Context for ApcRoutine (or NULL)
1320 * io [O] Receives information about the operation on return
1321 * code [I] Control code for the operation to perform
1322 * in_buffer [I] Source for any input data required (or NULL)
1323 * in_size [I] Size of InputBuffer
1324 * out_buffer [O] Source for any output data returned (or NULL)
1325 * out_size [I] Size of OutputBuffer
1327 * RETURNS
1328 * Success: 0. IoStatusBlock is updated.
1329 * Failure: An NTSTATUS error code describing the error.
1331 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1332 PIO_APC_ROUTINE apc, PVOID apc_context,
1333 PIO_STATUS_BLOCK io, ULONG code,
1334 PVOID in_buffer, ULONG in_size,
1335 PVOID out_buffer, ULONG out_size)
1337 ULONG device = (code >> 16);
1338 NTSTATUS status = STATUS_NOT_SUPPORTED;
1340 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1341 handle, event, apc, apc_context, io, code,
1342 in_buffer, in_size, out_buffer, out_size);
1344 switch(device)
1346 case FILE_DEVICE_DISK:
1347 case FILE_DEVICE_CD_ROM:
1348 case FILE_DEVICE_DVD:
1349 case FILE_DEVICE_CONTROLLER:
1350 case FILE_DEVICE_MASS_STORAGE:
1351 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1352 in_buffer, in_size, out_buffer, out_size);
1353 break;
1354 case FILE_DEVICE_SERIAL_PORT:
1355 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1356 in_buffer, in_size, out_buffer, out_size);
1357 break;
1358 case FILE_DEVICE_TAPE:
1359 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1360 in_buffer, in_size, out_buffer, out_size);
1361 break;
1364 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1365 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1366 in_buffer, in_size, out_buffer, out_size );
1368 if (status != STATUS_PENDING) io->u.Status = status;
1369 return status;
1373 /**************************************************************************
1374 * NtFsControlFile [NTDLL.@]
1375 * ZwFsControlFile [NTDLL.@]
1377 * Perform a file system control operation on an open file handle.
1379 * PARAMS
1380 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1381 * event [I] Event to signal upon completion (or NULL)
1382 * apc [I] Callback to call upon completion (or NULL)
1383 * apc_context [I] Context for ApcRoutine (or NULL)
1384 * io [O] Receives information about the operation on return
1385 * code [I] Control code for the operation to perform
1386 * in_buffer [I] Source for any input data required (or NULL)
1387 * in_size [I] Size of InputBuffer
1388 * out_buffer [O] Source for any output data returned (or NULL)
1389 * out_size [I] Size of OutputBuffer
1391 * RETURNS
1392 * Success: 0. IoStatusBlock is updated.
1393 * Failure: An NTSTATUS error code describing the error.
1395 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1396 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1397 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1399 NTSTATUS status;
1401 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1402 handle, event, apc, apc_context, io, code,
1403 in_buffer, in_size, out_buffer, out_size);
1405 if (!io) return STATUS_INVALID_PARAMETER;
1407 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1409 switch(code)
1411 case FSCTL_DISMOUNT_VOLUME:
1412 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1413 in_buffer, in_size, out_buffer, out_size );
1414 if (!status) status = DIR_unmount_device( handle );
1415 break;
1417 case FSCTL_PIPE_PEEK:
1419 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1420 int avail = 0, fd, needs_close;
1422 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1424 status = STATUS_INFO_LENGTH_MISMATCH;
1425 break;
1428 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1429 break;
1431 #ifdef FIONREAD
1432 if (ioctl( fd, FIONREAD, &avail ) != 0)
1434 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1435 if (needs_close) close( fd );
1436 status = FILE_GetNtStatus();
1437 break;
1439 #endif
1440 if (!avail) /* check for closed pipe */
1442 struct pollfd pollfd;
1443 int ret;
1445 pollfd.fd = fd;
1446 pollfd.events = POLLIN;
1447 pollfd.revents = 0;
1448 ret = poll( &pollfd, 1, 0 );
1449 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1451 if (needs_close) close( fd );
1452 status = STATUS_PIPE_BROKEN;
1453 break;
1456 buffer->NamedPipeState = 0; /* FIXME */
1457 buffer->ReadDataAvailable = avail;
1458 buffer->NumberOfMessages = 0; /* FIXME */
1459 buffer->MessageLength = 0; /* FIXME */
1460 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1461 status = STATUS_SUCCESS;
1462 if (avail)
1464 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1465 if (data_size)
1467 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1468 if (res >= 0) io->Information += res;
1471 if (needs_close) close( fd );
1473 break;
1475 case FSCTL_PIPE_DISCONNECT:
1476 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1477 in_buffer, in_size, out_buffer, out_size );
1478 if (!status)
1480 int fd = server_remove_fd_from_cache( handle );
1481 if (fd != -1) close( fd );
1483 break;
1485 case FSCTL_PIPE_IMPERSONATE:
1486 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1487 status = RtlImpersonateSelf( SecurityImpersonation );
1488 break;
1490 case FSCTL_LOCK_VOLUME:
1491 case FSCTL_UNLOCK_VOLUME:
1492 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1493 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1494 status = STATUS_SUCCESS;
1495 break;
1497 case FSCTL_GET_RETRIEVAL_POINTERS:
1499 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1501 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1503 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1505 buffer->ExtentCount = 1;
1506 buffer->StartingVcn.QuadPart = 1;
1507 buffer->Extents[0].NextVcn.QuadPart = 0;
1508 buffer->Extents[0].Lcn.QuadPart = 0;
1509 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1510 status = STATUS_SUCCESS;
1512 else
1514 io->Information = 0;
1515 status = STATUS_BUFFER_TOO_SMALL;
1517 break;
1519 case FSCTL_PIPE_LISTEN:
1520 case FSCTL_PIPE_WAIT:
1521 default:
1522 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1523 in_buffer, in_size, out_buffer, out_size );
1524 break;
1527 if (status != STATUS_PENDING) io->u.Status = status;
1528 return status;
1531 /******************************************************************************
1532 * NtSetVolumeInformationFile [NTDLL.@]
1533 * ZwSetVolumeInformationFile [NTDLL.@]
1535 * Set volume information for an open file handle.
1537 * PARAMS
1538 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1539 * IoStatusBlock [O] Receives information about the operation on return
1540 * FsInformation [I] Source for volume information
1541 * Length [I] Size of FsInformation
1542 * FsInformationClass [I] Type of volume information to set
1544 * RETURNS
1545 * Success: 0. IoStatusBlock is updated.
1546 * Failure: An NTSTATUS error code describing the error.
1548 NTSTATUS WINAPI NtSetVolumeInformationFile(
1549 IN HANDLE FileHandle,
1550 PIO_STATUS_BLOCK IoStatusBlock,
1551 PVOID FsInformation,
1552 ULONG Length,
1553 FS_INFORMATION_CLASS FsInformationClass)
1555 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1556 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1557 return 0;
1560 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1562 NTSTATUS status = STATUS_SUCCESS;
1564 #ifdef HAVE_FUTIMENS
1565 struct timespec tv[2];
1567 tv[0].tv_sec = tv[1].tv_sec = 0;
1568 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1569 if (atime->QuadPart)
1571 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1572 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1574 if (mtime->QuadPart)
1576 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1577 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1579 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1581 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1582 struct timeval tv[2];
1583 struct stat st;
1585 if (!atime->QuadPart || !mtime->QuadPart)
1588 tv[0].tv_sec = tv[0].tv_usec = 0;
1589 tv[1].tv_sec = tv[1].tv_usec = 0;
1590 if (!fstat( fd, &st ))
1592 tv[0].tv_sec = st.st_atime;
1593 tv[1].tv_sec = st.st_mtime;
1594 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1595 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1596 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1597 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1598 #endif
1599 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1600 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1601 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1602 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1603 #endif
1606 if (atime->QuadPart)
1608 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1609 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1611 if (mtime->QuadPart)
1613 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1614 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1616 #ifdef HAVE_FUTIMES
1617 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1618 #elif defined(HAVE_FUTIMESAT)
1619 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1620 #endif
1622 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1623 FIXME( "setting file times not supported\n" );
1624 status = STATUS_NOT_IMPLEMENTED;
1625 #endif
1626 return status;
1629 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1630 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1632 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1633 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1634 RtlSecondsSince1970ToTime( st->st_atime, atime );
1635 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1636 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1637 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1638 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
1639 #endif
1640 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1641 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
1642 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1643 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
1644 #endif
1645 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1646 atime->QuadPart += st->st_atim.tv_nsec / 100;
1647 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1648 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
1649 #endif
1650 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1651 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
1652 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
1653 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
1654 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1655 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
1656 #endif
1657 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
1658 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
1659 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
1660 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
1661 #endif
1662 #else
1663 *creation = *mtime;
1664 #endif
1667 /* fill in the file information that depends on the stat info */
1668 NTSTATUS fill_stat_info( const struct stat *st, void *ptr, FILE_INFORMATION_CLASS class )
1670 switch (class)
1672 case FileBasicInformation:
1674 FILE_BASIC_INFORMATION *info = ptr;
1676 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1677 &info->LastAccessTime, &info->CreationTime );
1678 if (S_ISDIR(st->st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1679 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1680 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1681 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1683 break;
1684 case FileStandardInformation:
1686 FILE_STANDARD_INFORMATION *info = ptr;
1688 if ((info->Directory = S_ISDIR(st->st_mode)))
1690 info->AllocationSize.QuadPart = 0;
1691 info->EndOfFile.QuadPart = 0;
1692 info->NumberOfLinks = 1;
1694 else
1696 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1697 info->EndOfFile.QuadPart = st->st_size;
1698 info->NumberOfLinks = st->st_nlink;
1701 break;
1702 case FileInternalInformation:
1704 FILE_INTERNAL_INFORMATION *info = ptr;
1705 info->IndexNumber.QuadPart = st->st_ino;
1707 break;
1708 case FileEndOfFileInformation:
1710 FILE_END_OF_FILE_INFORMATION *info = ptr;
1711 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
1713 break;
1714 case FileAllInformation:
1716 FILE_ALL_INFORMATION *info = ptr;
1717 fill_stat_info( st, &info->BasicInformation, FileBasicInformation );
1718 fill_stat_info( st, &info->StandardInformation, FileStandardInformation );
1719 fill_stat_info( st, &info->InternalInformation, FileInternalInformation );
1721 break;
1722 /* all directory structures start with the FileDirectoryInformation layout */
1723 case FileBothDirectoryInformation:
1724 case FileFullDirectoryInformation:
1725 case FileDirectoryInformation:
1727 FILE_DIRECTORY_INFORMATION *info = ptr;
1729 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1730 &info->LastAccessTime, &info->CreationTime );
1731 if (S_ISDIR(st->st_mode))
1733 info->AllocationSize.QuadPart = 0;
1734 info->EndOfFile.QuadPart = 0;
1735 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1737 else
1739 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1740 info->EndOfFile.QuadPart = st->st_size;
1741 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1743 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1744 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1746 break;
1747 case FileIdFullDirectoryInformation:
1749 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
1750 info->FileId.QuadPart = st->st_ino;
1751 fill_stat_info( st, info, FileDirectoryInformation );
1753 break;
1754 case FileIdBothDirectoryInformation:
1756 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
1757 info->FileId.QuadPart = st->st_ino;
1758 fill_stat_info( st, info, FileDirectoryInformation );
1760 break;
1762 default:
1763 return STATUS_INVALID_INFO_CLASS;
1765 return STATUS_SUCCESS;
1768 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
1770 data_size_t size = 1024;
1771 NTSTATUS ret;
1772 char *name;
1774 for (;;)
1776 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
1777 if (!name) return STATUS_NO_MEMORY;
1778 unix_name->MaximumLength = size + 1;
1780 SERVER_START_REQ( get_handle_unix_name )
1782 req->handle = wine_server_obj_handle( handle );
1783 wine_server_set_reply( req, name, size );
1784 ret = wine_server_call( req );
1785 size = reply->name_len;
1787 SERVER_END_REQ;
1789 if (!ret)
1791 name[size] = 0;
1792 unix_name->Buffer = name;
1793 unix_name->Length = size;
1794 break;
1796 RtlFreeHeap( GetProcessHeap(), 0, name );
1797 if (ret != STATUS_BUFFER_OVERFLOW) break;
1799 return ret;
1802 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
1804 UNICODE_STRING nt_name;
1805 NTSTATUS status;
1807 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
1809 const WCHAR *ptr = nt_name.Buffer;
1810 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
1812 /* Skip the volume mount point. */
1813 while (ptr != end && *ptr == '\\') ++ptr;
1814 while (ptr != end && *ptr != '\\') ++ptr;
1815 while (ptr != end && *ptr == '\\') ++ptr;
1816 while (ptr != end && *ptr != '\\') ++ptr;
1818 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
1819 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
1820 else *name_len = info->FileNameLength;
1822 memcpy( info->FileName, ptr, *name_len );
1823 RtlFreeUnicodeString( &nt_name );
1826 return status;
1829 /******************************************************************************
1830 * NtQueryInformationFile [NTDLL.@]
1831 * ZwQueryInformationFile [NTDLL.@]
1833 * Get information about an open file handle.
1835 * PARAMS
1836 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1837 * io [O] Receives information about the operation on return
1838 * ptr [O] Destination for file information
1839 * len [I] Size of FileInformation
1840 * class [I] Type of file information to get
1842 * RETURNS
1843 * Success: 0. IoStatusBlock and FileInformation are updated.
1844 * Failure: An NTSTATUS error code describing the error.
1846 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1847 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1849 static const size_t info_sizes[] =
1852 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1853 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1854 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1855 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1856 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1857 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1858 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1859 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1860 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
1861 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1862 0, /* FileLinkInformation */
1863 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1864 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1865 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1866 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1867 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1868 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1869 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
1870 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1871 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1872 0, /* FileAlternateNameInformation */
1873 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1874 0, /* FilePipeInformation */
1875 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1876 0, /* FilePipeRemoteInformation */
1877 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1878 0, /* FileMailslotSetInformation */
1879 0, /* FileCompressionInformation */
1880 0, /* FileObjectIdInformation */
1881 0, /* FileCompletionInformation */
1882 0, /* FileMoveClusterInformation */
1883 0, /* FileQuotaInformation */
1884 0, /* FileReparsePointInformation */
1885 0, /* FileNetworkOpenInformation */
1886 0, /* FileAttributeTagInformation */
1887 0, /* FileTrackingInformation */
1888 0, /* FileIdBothDirectoryInformation */
1889 0, /* FileIdFullDirectoryInformation */
1890 0, /* FileValidDataLengthInformation */
1891 0, /* FileShortNameInformation */
1895 0, /* FileSfioReserveInformation */
1896 0, /* FileSfioVolumeInformation */
1897 0, /* FileHardLinkInformation */
1899 0, /* FileNormalizedNameInformation */
1901 0, /* FileIdGlobalTxDirectoryInformation */
1905 0 /* FileStandardLinkInformation */
1908 struct stat st;
1909 int fd, needs_close = FALSE;
1911 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1913 io->Information = 0;
1915 if (class <= 0 || class >= FileMaximumInformation)
1916 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1917 if (!info_sizes[class])
1919 FIXME("Unsupported class (%d)\n", class);
1920 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1922 if (len < info_sizes[class])
1923 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1925 if (class != FilePipeLocalInformation)
1927 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1928 return io->u.Status;
1931 switch (class)
1933 case FileBasicInformation:
1934 if (fstat( fd, &st ) == -1)
1935 io->u.Status = FILE_GetNtStatus();
1936 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1937 io->u.Status = STATUS_INVALID_INFO_CLASS;
1938 else
1939 fill_stat_info( &st, ptr, class );
1940 break;
1941 case FileStandardInformation:
1943 FILE_STANDARD_INFORMATION *info = ptr;
1945 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1946 else
1948 fill_stat_info( &st, info, class );
1949 info->DeletePending = FALSE; /* FIXME */
1952 break;
1953 case FilePositionInformation:
1955 FILE_POSITION_INFORMATION *info = ptr;
1956 off_t res = lseek( fd, 0, SEEK_CUR );
1957 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1958 else info->CurrentByteOffset.QuadPart = res;
1960 break;
1961 case FileInternalInformation:
1962 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1963 else fill_stat_info( &st, ptr, class );
1964 break;
1965 case FileEaInformation:
1967 FILE_EA_INFORMATION *info = ptr;
1968 info->EaSize = 0;
1970 break;
1971 case FileEndOfFileInformation:
1972 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1973 else fill_stat_info( &st, ptr, class );
1974 break;
1975 case FileAllInformation:
1977 FILE_ALL_INFORMATION *info = ptr;
1978 ANSI_STRING unix_name;
1980 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1981 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1982 io->u.Status = STATUS_INVALID_INFO_CLASS;
1983 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
1985 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
1987 fill_stat_info( &st, info, FileAllInformation );
1988 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1989 info->EaInformation.EaSize = 0;
1990 info->AccessInformation.AccessFlags = 0; /* FIXME */
1991 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1992 info->ModeInformation.Mode = 0; /* FIXME */
1993 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1995 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
1996 RtlFreeAnsiString( &unix_name );
1997 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2000 break;
2001 case FileMailslotQueryInformation:
2003 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2005 SERVER_START_REQ( set_mailslot_info )
2007 req->handle = wine_server_obj_handle( hFile );
2008 req->flags = 0;
2009 io->u.Status = wine_server_call( req );
2010 if( io->u.Status == STATUS_SUCCESS )
2012 info->MaximumMessageSize = reply->max_msgsize;
2013 info->MailslotQuota = 0;
2014 info->NextMessageSize = 0;
2015 info->MessagesAvailable = 0;
2016 info->ReadTimeout.QuadPart = reply->read_timeout;
2019 SERVER_END_REQ;
2020 if (!io->u.Status)
2022 char *tmpbuf;
2023 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2024 if (size > 0x10000) size = 0x10000;
2025 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2027 int fd, needs_close;
2028 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2030 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2031 info->MessagesAvailable = (res > 0);
2032 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2033 if (needs_close) close( fd );
2035 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2039 break;
2040 case FilePipeLocalInformation:
2042 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2044 SERVER_START_REQ( get_named_pipe_info )
2046 req->handle = wine_server_obj_handle( hFile );
2047 if (!(io->u.Status = wine_server_call( req )))
2049 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2050 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2051 switch (reply->sharing)
2053 case FILE_SHARE_READ:
2054 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2055 break;
2056 case FILE_SHARE_WRITE:
2057 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2058 break;
2059 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2060 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2061 break;
2063 pli->MaximumInstances = reply->maxinstances;
2064 pli->CurrentInstances = reply->instances;
2065 pli->InboundQuota = reply->insize;
2066 pli->ReadDataAvailable = 0; /* FIXME */
2067 pli->OutboundQuota = reply->outsize;
2068 pli->WriteQuotaAvailable = 0; /* FIXME */
2069 pli->NamedPipeState = 0; /* FIXME */
2070 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2071 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2074 SERVER_END_REQ;
2076 break;
2077 case FileNameInformation:
2079 FILE_NAME_INFORMATION *info = ptr;
2080 ANSI_STRING unix_name;
2082 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2084 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2085 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2086 RtlFreeAnsiString( &unix_name );
2087 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2090 break;
2091 default:
2092 FIXME("Unsupported class (%d)\n", class);
2093 io->u.Status = STATUS_NOT_IMPLEMENTED;
2094 break;
2096 if (needs_close) close( fd );
2097 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2098 return io->u.Status;
2101 /******************************************************************************
2102 * NtSetInformationFile [NTDLL.@]
2103 * ZwSetInformationFile [NTDLL.@]
2105 * Set information about an open file handle.
2107 * PARAMS
2108 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2109 * io [O] Receives information about the operation on return
2110 * ptr [I] Source for file information
2111 * len [I] Size of FileInformation
2112 * class [I] Type of file information to set
2114 * RETURNS
2115 * Success: 0. io is updated.
2116 * Failure: An NTSTATUS error code describing the error.
2118 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2119 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2121 int fd, needs_close;
2123 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2125 io->u.Status = STATUS_SUCCESS;
2126 switch (class)
2128 case FileBasicInformation:
2129 if (len >= sizeof(FILE_BASIC_INFORMATION))
2131 struct stat st;
2132 const FILE_BASIC_INFORMATION *info = ptr;
2134 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2135 return io->u.Status;
2137 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2138 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2140 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2142 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2143 else
2145 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2147 if (S_ISDIR( st.st_mode))
2148 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2149 else
2150 st.st_mode &= ~0222; /* clear write permission bits */
2152 else
2154 /* add write permission only where we already have read permission */
2155 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2157 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2161 if (needs_close) close( fd );
2163 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2164 break;
2166 case FilePositionInformation:
2167 if (len >= sizeof(FILE_POSITION_INFORMATION))
2169 const FILE_POSITION_INFORMATION *info = ptr;
2171 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2172 return io->u.Status;
2174 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2175 io->u.Status = FILE_GetNtStatus();
2177 if (needs_close) close( fd );
2179 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2180 break;
2182 case FileEndOfFileInformation:
2183 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2185 struct stat st;
2186 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2188 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2189 return io->u.Status;
2191 /* first try normal truncate */
2192 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2194 /* now check for the need to extend the file */
2195 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2197 static const char zero;
2199 /* extend the file one byte beyond the requested size and then truncate it */
2200 /* this should work around ftruncate implementations that can't extend files */
2201 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2202 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2204 io->u.Status = FILE_GetNtStatus();
2206 if (needs_close) close( fd );
2208 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2209 break;
2211 case FileMailslotSetInformation:
2213 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2215 SERVER_START_REQ( set_mailslot_info )
2217 req->handle = wine_server_obj_handle( handle );
2218 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2219 req->read_timeout = info->ReadTimeout.QuadPart;
2220 io->u.Status = wine_server_call( req );
2222 SERVER_END_REQ;
2224 break;
2226 case FileCompletionInformation:
2227 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2229 FILE_COMPLETION_INFORMATION *info = ptr;
2231 SERVER_START_REQ( set_completion_info )
2233 req->handle = wine_server_obj_handle( handle );
2234 req->chandle = wine_server_obj_handle( info->CompletionPort );
2235 req->ckey = info->CompletionKey;
2236 io->u.Status = wine_server_call( req );
2238 SERVER_END_REQ;
2239 } else
2240 io->u.Status = STATUS_INVALID_PARAMETER_3;
2241 break;
2243 case FileAllInformation:
2244 io->u.Status = STATUS_INVALID_INFO_CLASS;
2245 break;
2247 case FileValidDataLengthInformation:
2248 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2250 struct stat st;
2251 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2253 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2254 return io->u.Status;
2256 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2257 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2258 io->u.Status = STATUS_INVALID_PARAMETER;
2259 else
2261 #ifdef HAVE_FALLOCATE
2262 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2264 NTSTATUS status = FILE_GetNtStatus();
2265 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2266 else io->u.Status = status;
2268 #else
2269 FIXME( "setting valid data length not supported\n" );
2270 #endif
2272 if (needs_close) close( fd );
2274 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2275 break;
2277 default:
2278 FIXME("Unsupported class (%d)\n", class);
2279 io->u.Status = STATUS_NOT_IMPLEMENTED;
2280 break;
2282 io->Information = 0;
2283 return io->u.Status;
2287 /******************************************************************************
2288 * NtQueryFullAttributesFile (NTDLL.@)
2290 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2291 FILE_NETWORK_OPEN_INFORMATION *info )
2293 ANSI_STRING unix_name;
2294 NTSTATUS status;
2296 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2298 struct stat st;
2300 if (stat( unix_name.Buffer, &st ) == -1)
2301 status = FILE_GetNtStatus();
2302 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2303 status = STATUS_INVALID_INFO_CLASS;
2304 else
2306 FILE_BASIC_INFORMATION basic;
2307 FILE_STANDARD_INFORMATION std;
2309 fill_stat_info( &st, &basic, FileBasicInformation );
2310 fill_stat_info( &st, &std, FileStandardInformation );
2312 info->CreationTime = basic.CreationTime;
2313 info->LastAccessTime = basic.LastAccessTime;
2314 info->LastWriteTime = basic.LastWriteTime;
2315 info->ChangeTime = basic.ChangeTime;
2316 info->AllocationSize = std.AllocationSize;
2317 info->EndOfFile = std.EndOfFile;
2318 info->FileAttributes = basic.FileAttributes;
2319 if (DIR_is_hidden_file( attr->ObjectName ))
2320 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2322 RtlFreeAnsiString( &unix_name );
2324 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2325 return status;
2329 /******************************************************************************
2330 * NtQueryAttributesFile (NTDLL.@)
2331 * ZwQueryAttributesFile (NTDLL.@)
2333 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2335 ANSI_STRING unix_name;
2336 NTSTATUS status;
2338 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2340 struct stat st;
2342 if (stat( unix_name.Buffer, &st ) == -1)
2343 status = FILE_GetNtStatus();
2344 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2345 status = STATUS_INVALID_INFO_CLASS;
2346 else
2348 status = fill_stat_info( &st, info, FileBasicInformation );
2349 if (DIR_is_hidden_file( attr->ObjectName ))
2350 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2352 RtlFreeAnsiString( &unix_name );
2354 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2355 return status;
2359 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2360 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2361 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2362 unsigned int flags )
2364 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2366 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2367 /* Don't assume read-only, let the mount options set it below */
2368 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2370 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2371 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2373 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2374 info->Characteristics |= FILE_REMOTE_DEVICE;
2376 else if (!strcmp("procfs", fstypename))
2377 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2378 else
2379 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2381 if (flags & MNT_RDONLY)
2382 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2384 if (!(flags & MNT_LOCAL))
2386 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2387 info->Characteristics |= FILE_REMOTE_DEVICE;
2390 #endif
2392 static inline int is_device_placeholder( int fd )
2394 static const char wine_placeholder[] = "Wine device placeholder";
2395 char buffer[sizeof(wine_placeholder)-1];
2397 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2398 return 0;
2399 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2402 /******************************************************************************
2403 * get_device_info
2405 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2407 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2409 struct stat st;
2411 info->Characteristics = 0;
2412 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2413 if (S_ISCHR( st.st_mode ))
2415 info->DeviceType = FILE_DEVICE_UNKNOWN;
2416 #ifdef linux
2417 switch(major(st.st_rdev))
2419 case MEM_MAJOR:
2420 info->DeviceType = FILE_DEVICE_NULL;
2421 break;
2422 case TTY_MAJOR:
2423 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2424 break;
2425 case LP_MAJOR:
2426 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2427 break;
2428 case SCSI_TAPE_MAJOR:
2429 info->DeviceType = FILE_DEVICE_TAPE;
2430 break;
2432 #endif
2434 else if (S_ISBLK( st.st_mode ))
2436 info->DeviceType = FILE_DEVICE_DISK;
2438 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2440 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2442 else if (is_device_placeholder( fd ))
2444 info->DeviceType = FILE_DEVICE_DISK;
2446 else /* regular file or directory */
2448 #if defined(linux) && defined(HAVE_FSTATFS)
2449 struct statfs stfs;
2451 /* check for floppy disk */
2452 if (major(st.st_dev) == FLOPPY_MAJOR)
2453 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2455 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2456 switch (stfs.f_type)
2458 case 0x9660: /* iso9660 */
2459 case 0x9fa1: /* supermount */
2460 case 0x15013346: /* udf */
2461 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2462 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2463 break;
2464 case 0x6969: /* nfs */
2465 case 0x517B: /* smbfs */
2466 case 0x564c: /* ncpfs */
2467 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2468 info->Characteristics |= FILE_REMOTE_DEVICE;
2469 break;
2470 case 0x01021994: /* tmpfs */
2471 case 0x28cd3d45: /* cramfs */
2472 case 0x1373: /* devfs */
2473 case 0x9fa0: /* procfs */
2474 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2475 break;
2476 default:
2477 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2478 break;
2480 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2481 struct statfs stfs;
2483 if (fstatfs( fd, &stfs ) < 0)
2484 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2485 else
2486 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2487 #elif defined(__NetBSD__)
2488 struct statvfs stfs;
2490 if (fstatvfs( fd, &stfs) < 0)
2491 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2492 else
2493 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2494 #elif defined(sun)
2495 /* Use dkio to work out device types */
2497 # include <sys/dkio.h>
2498 # include <sys/vtoc.h>
2499 struct dk_cinfo dkinf;
2500 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2501 if(retval==-1){
2502 WARN("Unable to get disk device type information - assuming a disk like device\n");
2503 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2505 switch (dkinf.dki_ctype)
2507 case DKC_CDROM:
2508 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2509 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2510 break;
2511 case DKC_NCRFLOPPY:
2512 case DKC_SMSFLOPPY:
2513 case DKC_INTEL82072:
2514 case DKC_INTEL82077:
2515 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2516 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2517 break;
2518 case DKC_MD:
2519 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2520 break;
2521 default:
2522 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2525 #else
2526 static int warned;
2527 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2528 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2529 #endif
2530 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2532 return STATUS_SUCCESS;
2536 /******************************************************************************
2537 * NtQueryVolumeInformationFile [NTDLL.@]
2538 * ZwQueryVolumeInformationFile [NTDLL.@]
2540 * Get volume information for an open file handle.
2542 * PARAMS
2543 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2544 * io [O] Receives information about the operation on return
2545 * buffer [O] Destination for volume information
2546 * length [I] Size of FsInformation
2547 * info_class [I] Type of volume information to set
2549 * RETURNS
2550 * Success: 0. io and buffer are updated.
2551 * Failure: An NTSTATUS error code describing the error.
2553 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2554 PVOID buffer, ULONG length,
2555 FS_INFORMATION_CLASS info_class )
2557 int fd, needs_close;
2558 struct stat st;
2559 static int once;
2561 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2562 return io->u.Status;
2564 io->u.Status = STATUS_NOT_IMPLEMENTED;
2565 io->Information = 0;
2567 switch( info_class )
2569 case FileFsVolumeInformation:
2570 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2571 break;
2572 case FileFsLabelInformation:
2573 FIXME( "%p: label info not supported\n", handle );
2574 break;
2575 case FileFsSizeInformation:
2576 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2577 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2578 else
2580 FILE_FS_SIZE_INFORMATION *info = buffer;
2582 if (fstat( fd, &st ) < 0)
2584 io->u.Status = FILE_GetNtStatus();
2585 break;
2587 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2589 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2591 else
2593 ULONGLONG bsize;
2594 /* Linux's fstatvfs is buggy */
2595 #if !defined(linux) || !defined(HAVE_FSTATFS)
2596 struct statvfs stfs;
2598 if (fstatvfs( fd, &stfs ) < 0)
2600 io->u.Status = FILE_GetNtStatus();
2601 break;
2603 bsize = stfs.f_frsize;
2604 #else
2605 struct statfs stfs;
2606 if (fstatfs( fd, &stfs ) < 0)
2608 io->u.Status = FILE_GetNtStatus();
2609 break;
2611 bsize = stfs.f_bsize;
2612 #endif
2613 if (bsize == 2048) /* assume CD-ROM */
2615 info->BytesPerSector = 2048;
2616 info->SectorsPerAllocationUnit = 1;
2618 else
2620 info->BytesPerSector = 512;
2621 info->SectorsPerAllocationUnit = 8;
2623 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2624 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2625 io->Information = sizeof(*info);
2626 io->u.Status = STATUS_SUCCESS;
2629 break;
2630 case FileFsDeviceInformation:
2631 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2632 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2633 else
2635 FILE_FS_DEVICE_INFORMATION *info = buffer;
2637 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2638 io->Information = sizeof(*info);
2640 break;
2641 case FileFsAttributeInformation:
2642 FIXME( "%p: attribute info not supported\n", handle );
2643 break;
2644 case FileFsControlInformation:
2645 FIXME( "%p: control info not supported\n", handle );
2646 break;
2647 case FileFsFullSizeInformation:
2648 FIXME( "%p: full size info not supported\n", handle );
2649 break;
2650 case FileFsObjectIdInformation:
2651 FIXME( "%p: object id info not supported\n", handle );
2652 break;
2653 case FileFsMaximumInformation:
2654 FIXME( "%p: maximum info not supported\n", handle );
2655 break;
2656 default:
2657 io->u.Status = STATUS_INVALID_PARAMETER;
2658 break;
2660 if (needs_close) close( fd );
2661 return io->u.Status;
2665 /******************************************************************
2666 * NtQueryEaFile (NTDLL.@)
2668 * Read extended attributes from NTFS files.
2670 * PARAMS
2671 * hFile [I] File handle, must be opened with FILE_READ_EA access
2672 * iosb [O] Receives information about the operation on return
2673 * buffer [O] Output buffer
2674 * length [I] Length of output buffer
2675 * single_entry [I] Only read and return one entry
2676 * ea_list [I] Optional list with names of EAs to return
2677 * ea_list_len [I] Length of ea_list in bytes
2678 * ea_index [I] Optional pointer to 1-based index of attribute to return
2679 * restart [I] restart EA scan
2681 * RETURNS
2682 * Success: 0. Atrributes read into buffer
2683 * Failure: An NTSTATUS error code describing the error.
2685 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
2686 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
2687 PULONG ea_index, BOOLEAN restart )
2689 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
2690 hFile, iosb, buffer, length, single_entry, ea_list,
2691 ea_list_len, ea_index, restart);
2692 return STATUS_ACCESS_DENIED;
2696 /******************************************************************
2697 * NtSetEaFile (NTDLL.@)
2699 * Update extended attributes for NTFS files.
2701 * PARAMS
2702 * hFile [I] File handle, must be opened with FILE_READ_EA access
2703 * iosb [O] Receives information about the operation on return
2704 * buffer [I] Buffer with EA information
2705 * length [I] Length of buffer
2707 * RETURNS
2708 * Success: 0. Attributes are updated
2709 * Failure: An NTSTATUS error code describing the error.
2711 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
2713 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
2714 return STATUS_ACCESS_DENIED;
2718 /******************************************************************
2719 * NtFlushBuffersFile (NTDLL.@)
2721 * Flush any buffered data on an open file handle.
2723 * PARAMS
2724 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2725 * IoStatusBlock [O] Receives information about the operation on return
2727 * RETURNS
2728 * Success: 0. IoStatusBlock is updated.
2729 * Failure: An NTSTATUS error code describing the error.
2731 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2733 NTSTATUS ret;
2734 HANDLE hEvent = NULL;
2736 SERVER_START_REQ( flush_file )
2738 req->handle = wine_server_obj_handle( hFile );
2739 ret = wine_server_call( req );
2740 hEvent = wine_server_ptr_handle( reply->event );
2742 SERVER_END_REQ;
2743 if (!ret && hEvent)
2745 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2746 NtClose( hEvent );
2748 return ret;
2751 /******************************************************************
2752 * NtLockFile (NTDLL.@)
2756 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2757 PIO_APC_ROUTINE apc, void* apc_user,
2758 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2759 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2760 BOOLEAN exclusive )
2762 NTSTATUS ret;
2763 HANDLE handle;
2764 BOOLEAN async;
2765 static BOOLEAN warn = TRUE;
2767 if (apc || io_status || key)
2769 FIXME("Unimplemented yet parameter\n");
2770 return STATUS_NOT_IMPLEMENTED;
2773 if (apc_user && warn)
2775 FIXME("I/O completion on lock not implemented yet\n");
2776 warn = FALSE;
2779 for (;;)
2781 SERVER_START_REQ( lock_file )
2783 req->handle = wine_server_obj_handle( hFile );
2784 req->offset = offset->QuadPart;
2785 req->count = count->QuadPart;
2786 req->shared = !exclusive;
2787 req->wait = !dont_wait;
2788 ret = wine_server_call( req );
2789 handle = wine_server_ptr_handle( reply->handle );
2790 async = reply->overlapped;
2792 SERVER_END_REQ;
2793 if (ret != STATUS_PENDING)
2795 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2796 return ret;
2799 if (async)
2801 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2802 if (handle) NtClose( handle );
2803 return STATUS_PENDING;
2805 if (handle)
2807 NtWaitForSingleObject( handle, FALSE, NULL );
2808 NtClose( handle );
2810 else
2812 LARGE_INTEGER time;
2814 /* Unix lock conflict, sleep a bit and retry */
2815 time.QuadPart = 100 * (ULONGLONG)10000;
2816 time.QuadPart = -time.QuadPart;
2817 NtDelayExecution( FALSE, &time );
2823 /******************************************************************
2824 * NtUnlockFile (NTDLL.@)
2828 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2829 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2830 PULONG key )
2832 NTSTATUS status;
2834 TRACE( "%p %x%08x %x%08x\n",
2835 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2837 if (io_status || key)
2839 FIXME("Unimplemented yet parameter\n");
2840 return STATUS_NOT_IMPLEMENTED;
2843 SERVER_START_REQ( unlock_file )
2845 req->handle = wine_server_obj_handle( hFile );
2846 req->offset = offset->QuadPart;
2847 req->count = count->QuadPart;
2848 status = wine_server_call( req );
2850 SERVER_END_REQ;
2851 return status;
2854 /******************************************************************
2855 * NtCreateNamedPipeFile (NTDLL.@)
2859 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2860 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2861 ULONG sharing, ULONG dispo, ULONG options,
2862 ULONG pipe_type, ULONG read_mode,
2863 ULONG completion_mode, ULONG max_inst,
2864 ULONG inbound_quota, ULONG outbound_quota,
2865 PLARGE_INTEGER timeout)
2867 NTSTATUS status;
2869 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2870 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2871 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2872 outbound_quota, timeout);
2874 /* assume we only get relative timeout */
2875 if (timeout->QuadPart > 0)
2876 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2878 SERVER_START_REQ( create_named_pipe )
2880 req->access = access;
2881 req->attributes = attr->Attributes;
2882 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2883 req->options = options;
2884 req->sharing = sharing;
2885 req->flags =
2886 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
2887 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
2888 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
2889 req->maxinstances = max_inst;
2890 req->outsize = outbound_quota;
2891 req->insize = inbound_quota;
2892 req->timeout = timeout->QuadPart;
2893 wine_server_add_data( req, attr->ObjectName->Buffer,
2894 attr->ObjectName->Length );
2895 status = wine_server_call( req );
2896 if (!status) *handle = wine_server_ptr_handle( reply->handle );
2898 SERVER_END_REQ;
2899 return status;
2902 /******************************************************************
2903 * NtDeleteFile (NTDLL.@)
2907 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2909 NTSTATUS status;
2910 HANDLE hFile;
2911 IO_STATUS_BLOCK io;
2913 TRACE("%p\n", ObjectAttributes);
2914 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2915 ObjectAttributes, &io, NULL, 0,
2916 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2917 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2918 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2919 return status;
2922 /******************************************************************
2923 * NtCancelIoFileEx (NTDLL.@)
2927 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
2929 LARGE_INTEGER timeout;
2931 TRACE("%p %p %p\n", hFile, iosb, io_status );
2933 SERVER_START_REQ( cancel_async )
2935 req->handle = wine_server_obj_handle( hFile );
2936 req->iosb = wine_server_client_ptr( iosb );
2937 req->only_thread = FALSE;
2938 io_status->u.Status = wine_server_call( req );
2940 SERVER_END_REQ;
2941 if (io_status->u.Status)
2942 return io_status->u.Status;
2944 /* Let some APC be run, so that we can run the remaining APCs on hFile
2945 * either the cancelation of the pending one, but also the execution
2946 * of the queued APC, but not yet run. This is needed to ensure proper
2947 * clean-up of allocated data.
2949 timeout.QuadPart = 0;
2950 NtDelayExecution( TRUE, &timeout );
2951 return io_status->u.Status;
2954 /******************************************************************
2955 * NtCancelIoFile (NTDLL.@)
2959 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2961 LARGE_INTEGER timeout;
2963 TRACE("%p %p\n", hFile, io_status );
2965 SERVER_START_REQ( cancel_async )
2967 req->handle = wine_server_obj_handle( hFile );
2968 req->iosb = 0;
2969 req->only_thread = TRUE;
2970 io_status->u.Status = wine_server_call( req );
2972 SERVER_END_REQ;
2973 if (io_status->u.Status)
2974 return io_status->u.Status;
2976 /* Let some APC be run, so that we can run the remaining APCs on hFile
2977 * either the cancelation of the pending one, but also the execution
2978 * of the queued APC, but not yet run. This is needed to ensure proper
2979 * clean-up of allocated data.
2981 timeout.QuadPart = 0;
2982 NtDelayExecution( TRUE, &timeout );
2983 return io_status->u.Status;
2986 /******************************************************************************
2987 * NtCreateMailslotFile [NTDLL.@]
2988 * ZwCreateMailslotFile [NTDLL.@]
2990 * PARAMS
2991 * pHandle [O] pointer to receive the handle created
2992 * DesiredAccess [I] access mode (read, write, etc)
2993 * ObjectAttributes [I] fully qualified NT path of the mailslot
2994 * IoStatusBlock [O] receives completion status and other info
2995 * CreateOptions [I]
2996 * MailslotQuota [I]
2997 * MaxMessageSize [I]
2998 * TimeOut [I]
3000 * RETURNS
3001 * An NT status code
3003 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3004 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3005 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3006 PLARGE_INTEGER TimeOut)
3008 LARGE_INTEGER timeout;
3009 NTSTATUS ret;
3011 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3012 pHandle, DesiredAccess, attr, IoStatusBlock,
3013 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3015 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3016 if (!attr) return STATUS_INVALID_PARAMETER;
3017 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3020 * For a NULL TimeOut pointer set the default timeout value
3022 if (!TimeOut)
3023 timeout.QuadPart = -1;
3024 else
3025 timeout.QuadPart = TimeOut->QuadPart;
3027 SERVER_START_REQ( create_mailslot )
3029 req->access = DesiredAccess;
3030 req->attributes = attr->Attributes;
3031 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3032 req->max_msgsize = MaxMessageSize;
3033 req->read_timeout = timeout.QuadPart;
3034 wine_server_add_data( req, attr->ObjectName->Buffer,
3035 attr->ObjectName->Length );
3036 ret = wine_server_call( req );
3037 if( ret == STATUS_SUCCESS )
3038 *pHandle = wine_server_ptr_handle( reply->handle );
3040 SERVER_END_REQ;
3042 return ret;