user32: Set the display device property on the desktop window as soon as it is created.
[wine.git] / dlls / ntdll / file.c
blobe747e8fce99f202947ad4ee44ad0e7724f2feb8c
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 #define FILE_WRITE_TO_END_OF_FILE ((LONGLONG)-1)
99 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
101 static const WCHAR ntfsW[] = {'N','T','F','S'};
103 /**************************************************************************
104 * FILE_CreateFile (internal)
105 * Open a file.
107 * Parameter set fully identical with NtCreateFile
109 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
110 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
111 ULONG attributes, ULONG sharing, ULONG disposition,
112 ULONG options, PVOID ea_buffer, ULONG ea_length )
114 ANSI_STRING unix_name;
115 int created = FALSE;
117 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
118 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
119 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
120 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
121 attributes, sharing, disposition, options, ea_buffer, ea_length );
123 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
125 if (alloc_size) FIXME( "alloc_size not supported\n" );
127 if (options & FILE_OPEN_BY_FILE_ID)
128 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
129 else
130 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
132 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
134 SERVER_START_REQ( open_file_object )
136 req->access = access;
137 req->attributes = attr->Attributes;
138 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
139 req->sharing = sharing;
140 req->options = options;
141 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
142 io->u.Status = wine_server_call( req );
143 *handle = wine_server_ptr_handle( reply->handle );
145 SERVER_END_REQ;
146 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
147 return io->u.Status;
150 if (io->u.Status == STATUS_NO_SUCH_FILE &&
151 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
153 created = TRUE;
154 io->u.Status = STATUS_SUCCESS;
157 if (io->u.Status == STATUS_SUCCESS)
159 struct security_descriptor *sd;
160 struct object_attributes objattr;
162 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
163 objattr.name_len = 0;
164 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
165 if (io->u.Status != STATUS_SUCCESS)
167 RtlFreeAnsiString( &unix_name );
168 return io->u.Status;
171 SERVER_START_REQ( create_file )
173 req->access = access;
174 req->attributes = attr->Attributes;
175 req->sharing = sharing;
176 req->create = disposition;
177 req->options = options;
178 req->attrs = attributes;
179 wine_server_add_data( req, &objattr, sizeof(objattr) );
180 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
181 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
182 io->u.Status = wine_server_call( req );
183 *handle = wine_server_ptr_handle( reply->handle );
185 SERVER_END_REQ;
186 NTDLL_free_struct_sd( sd );
187 RtlFreeAnsiString( &unix_name );
189 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
191 if (io->u.Status == STATUS_SUCCESS)
193 if (created) io->Information = FILE_CREATED;
194 else switch(disposition)
196 case FILE_SUPERSEDE:
197 io->Information = FILE_SUPERSEDED;
198 break;
199 case FILE_CREATE:
200 io->Information = FILE_CREATED;
201 break;
202 case FILE_OPEN:
203 case FILE_OPEN_IF:
204 io->Information = FILE_OPENED;
205 break;
206 case FILE_OVERWRITE:
207 case FILE_OVERWRITE_IF:
208 io->Information = FILE_OVERWRITTEN;
209 break;
212 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
214 static int once;
215 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
218 return io->u.Status;
221 /**************************************************************************
222 * NtOpenFile [NTDLL.@]
223 * ZwOpenFile [NTDLL.@]
225 * Open a file.
227 * PARAMS
228 * handle [O] Variable that receives the file handle on return
229 * access [I] Access desired by the caller to the file
230 * attr [I] Structure describing the file to be opened
231 * io [O] Receives details about the result of the operation
232 * sharing [I] Type of shared access the caller requires
233 * options [I] Options for the file open
235 * RETURNS
236 * Success: 0. FileHandle and IoStatusBlock are updated.
237 * Failure: An NTSTATUS error code describing the error.
239 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
240 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
241 ULONG sharing, ULONG options )
243 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
244 sharing, FILE_OPEN, options, NULL, 0 );
247 /**************************************************************************
248 * NtCreateFile [NTDLL.@]
249 * ZwCreateFile [NTDLL.@]
251 * Either create a new file or directory, or open an existing file, device,
252 * directory or volume.
254 * PARAMS
255 * handle [O] Points to a variable which receives the file handle on return
256 * access [I] Desired access to the file
257 * attr [I] Structure describing the file
258 * io [O] Receives information about the operation on return
259 * alloc_size [I] Initial size of the file in bytes
260 * attributes [I] Attributes to create the file with
261 * sharing [I] Type of shared access the caller would like to the file
262 * disposition [I] Specifies what to do, depending on whether the file already exists
263 * options [I] Options for creating a new file
264 * ea_buffer [I] Pointer to an extended attributes buffer
265 * ea_length [I] Length of ea_buffer
267 * RETURNS
268 * Success: 0. handle and io are updated.
269 * Failure: An NTSTATUS error code describing the error.
271 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
272 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
273 ULONG attributes, ULONG sharing, ULONG disposition,
274 ULONG options, PVOID ea_buffer, ULONG ea_length )
276 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
277 sharing, disposition, options, ea_buffer, ea_length );
280 /***********************************************************************
281 * Asynchronous file I/O *
284 struct async_fileio
286 HANDLE handle;
287 PIO_APC_ROUTINE apc;
288 void *apc_arg;
291 typedef struct
293 struct async_fileio io;
294 char* buffer;
295 unsigned int already;
296 unsigned int count;
297 BOOL avail_mode;
298 } async_fileio_read;
300 typedef struct
302 struct async_fileio io;
303 const char *buffer;
304 unsigned int already;
305 unsigned int count;
306 } async_fileio_write;
309 /* callback for file I/O user APC */
310 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
312 struct async_fileio *async = arg;
313 if (async->apc) async->apc( async->apc_arg, io, reserved );
314 RtlFreeHeap( GetProcessHeap(), 0, async );
317 /***********************************************************************
318 * FILE_GetNtStatus(void)
320 * Retrieve the Nt Status code from errno.
321 * Try to be consistent with FILE_SetDosError().
323 NTSTATUS FILE_GetNtStatus(void)
325 int err = errno;
327 TRACE( "errno = %d\n", errno );
328 switch (err)
330 case EAGAIN: return STATUS_SHARING_VIOLATION;
331 case EBADF: return STATUS_INVALID_HANDLE;
332 case EBUSY: return STATUS_DEVICE_BUSY;
333 case ENOSPC: return STATUS_DISK_FULL;
334 case EPERM:
335 case EROFS:
336 case EACCES: return STATUS_ACCESS_DENIED;
337 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
338 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
339 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
340 case EMFILE:
341 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
342 case EINVAL: return STATUS_INVALID_PARAMETER;
343 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
344 case EPIPE: return STATUS_PIPE_DISCONNECTED;
345 case EIO: return STATUS_DEVICE_NOT_READY;
346 #ifdef ENOMEDIUM
347 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
348 #endif
349 case ENXIO: return STATUS_NO_SUCH_DEVICE;
350 case ENOTTY:
351 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
352 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
353 case EFAULT: return STATUS_ACCESS_VIOLATION;
354 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
355 #ifdef ETIME /* Missing on FreeBSD */
356 case ETIME: return STATUS_IO_TIMEOUT;
357 #endif
358 case ENOEXEC: /* ?? */
359 case EEXIST: /* ?? */
360 default:
361 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
362 return STATUS_UNSUCCESSFUL;
366 /***********************************************************************
367 * FILE_AsyncReadService (INTERNAL)
369 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc)
371 async_fileio_read *fileio = user;
372 int fd, needs_close, result;
374 switch (status)
376 case STATUS_ALERTED: /* got some new data */
377 /* check to see if the data is ready (non-blocking) */
378 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
379 &needs_close, NULL, NULL )))
380 break;
382 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
383 if (needs_close) close( fd );
385 if (result < 0)
387 if (errno == EAGAIN || errno == EINTR)
388 status = STATUS_PENDING;
389 else /* check to see if the transfer is complete */
390 status = FILE_GetNtStatus();
392 else if (result == 0)
394 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
396 else
398 fileio->already += result;
399 if (fileio->already >= fileio->count || fileio->avail_mode)
400 status = STATUS_SUCCESS;
401 else
403 /* if we only have to read the available data, and none is available,
404 * simply cancel the request. If data was available, it has been read
405 * while in by previous call (NtDelayExecution)
407 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
410 break;
412 case STATUS_TIMEOUT:
413 case STATUS_IO_TIMEOUT:
414 if (fileio->already) status = STATUS_SUCCESS;
415 break;
417 if (status != STATUS_PENDING)
419 iosb->u.Status = status;
420 iosb->Information = fileio->already;
421 *apc = fileio_apc;
423 return status;
426 struct io_timeouts
428 int interval; /* max interval between two bytes */
429 int total; /* total timeout for the whole operation */
430 int end_time; /* absolute time of end of operation */
433 /* retrieve the I/O timeouts to use for a given handle */
434 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
435 struct io_timeouts *timeouts )
437 NTSTATUS status = STATUS_SUCCESS;
439 timeouts->interval = timeouts->total = -1;
441 switch(type)
443 case FD_TYPE_SERIAL:
445 /* GetCommTimeouts */
446 SERIAL_TIMEOUTS st;
447 IO_STATUS_BLOCK io;
449 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
450 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
451 if (status) break;
453 if (is_read)
455 if (st.ReadIntervalTimeout)
456 timeouts->interval = st.ReadIntervalTimeout;
458 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
460 timeouts->total = st.ReadTotalTimeoutConstant;
461 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
462 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
464 else if (st.ReadIntervalTimeout == MAXDWORD)
465 timeouts->interval = timeouts->total = 0;
467 else /* write */
469 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
471 timeouts->total = st.WriteTotalTimeoutConstant;
472 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
473 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
477 break;
478 case FD_TYPE_MAILSLOT:
479 if (is_read)
481 timeouts->interval = 0; /* return as soon as we got something */
482 SERVER_START_REQ( set_mailslot_info )
484 req->handle = wine_server_obj_handle( handle );
485 req->flags = 0;
486 if (!(status = wine_server_call( req )) &&
487 reply->read_timeout != TIMEOUT_INFINITE)
488 timeouts->total = reply->read_timeout / -10000;
490 SERVER_END_REQ;
492 break;
493 case FD_TYPE_SOCKET:
494 case FD_TYPE_PIPE:
495 case FD_TYPE_CHAR:
496 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
497 break;
498 default:
499 break;
501 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
502 return STATUS_SUCCESS;
506 /* retrieve the timeout for the next wait, in milliseconds */
507 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
509 int ret = -1;
511 if (timeouts->total != -1)
513 ret = timeouts->end_time - NtGetTickCount();
514 if (ret < 0) ret = 0;
516 if (already && timeouts->interval != -1)
518 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
520 return ret;
524 /* retrieve the avail_mode flag for async reads */
525 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
527 NTSTATUS status = STATUS_SUCCESS;
529 switch(type)
531 case FD_TYPE_SERIAL:
533 /* GetCommTimeouts */
534 SERIAL_TIMEOUTS st;
535 IO_STATUS_BLOCK io;
537 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
538 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
539 if (status) break;
540 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
541 !st.ReadTotalTimeoutConstant &&
542 st.ReadIntervalTimeout == MAXDWORD);
544 break;
545 case FD_TYPE_MAILSLOT:
546 case FD_TYPE_SOCKET:
547 case FD_TYPE_PIPE:
548 case FD_TYPE_CHAR:
549 *avail_mode = TRUE;
550 break;
551 default:
552 *avail_mode = FALSE;
553 break;
555 return status;
559 /******************************************************************************
560 * NtReadFile [NTDLL.@]
561 * ZwReadFile [NTDLL.@]
563 * Read from an open file handle.
565 * PARAMS
566 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
567 * Event [I] Event to signal upon completion (or NULL)
568 * ApcRoutine [I] Callback to call upon completion (or NULL)
569 * ApcContext [I] Context for ApcRoutine (or NULL)
570 * IoStatusBlock [O] Receives information about the operation on return
571 * Buffer [O] Destination for the data read
572 * Length [I] Size of Buffer
573 * ByteOffset [O] Destination for the new file pointer position (or NULL)
574 * Key [O] Function unknown (may be NULL)
576 * RETURNS
577 * Success: 0. IoStatusBlock is updated, and the Information member contains
578 * The number of bytes read.
579 * Failure: An NTSTATUS error code describing the error.
581 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
582 PIO_APC_ROUTINE apc, void* apc_user,
583 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
584 PLARGE_INTEGER offset, PULONG key)
586 int result, unix_handle, needs_close, timeout_init_done = 0;
587 unsigned int options;
588 struct io_timeouts timeouts;
589 NTSTATUS status;
590 ULONG total = 0;
591 enum server_fd_type type;
592 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
593 BOOL send_completion = FALSE, async_read;
595 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
596 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
598 if (!io_status) return STATUS_ACCESS_VIOLATION;
600 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
601 &needs_close, &type, &options );
602 if (status) return status;
604 if (!virtual_check_buffer_for_write( buffer, length ))
606 status = STATUS_ACCESS_VIOLATION;
607 goto done;
610 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
612 if (type == FD_TYPE_FILE)
614 if (async_read && (!offset || offset->QuadPart < 0))
616 status = STATUS_INVALID_PARAMETER;
617 goto done;
620 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
622 /* async I/O doesn't make sense on regular files */
623 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
625 if (errno != EINTR)
627 status = FILE_GetNtStatus();
628 goto done;
631 if (!async_read)
632 /* update file pointer position */
633 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
635 total = result;
636 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
637 goto done;
640 else if (type == FD_TYPE_SERIAL)
642 if (async_read && (!offset || offset->QuadPart < 0))
644 status = STATUS_INVALID_PARAMETER;
645 goto done;
649 for (;;)
651 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
653 total += result;
654 if (!result || total == length)
656 if (total)
658 status = STATUS_SUCCESS;
659 goto done;
661 switch (type)
663 case FD_TYPE_FILE:
664 case FD_TYPE_CHAR:
665 status = STATUS_END_OF_FILE;
666 goto done;
667 case FD_TYPE_SERIAL:
668 break;
669 default:
670 status = STATUS_PIPE_BROKEN;
671 goto done;
674 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
676 else if (errno != EAGAIN)
678 if (errno == EINTR) continue;
679 if (!total) status = FILE_GetNtStatus();
680 goto done;
683 if (async_read)
685 async_fileio_read *fileio;
686 BOOL avail_mode;
688 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
689 goto err;
690 if (total && avail_mode)
692 status = STATUS_SUCCESS;
693 goto done;
696 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
698 status = STATUS_NO_MEMORY;
699 goto err;
701 fileio->io.handle = hFile;
702 fileio->io.apc = apc;
703 fileio->io.apc_arg = apc_user;
704 fileio->already = total;
705 fileio->count = length;
706 fileio->buffer = buffer;
707 fileio->avail_mode = avail_mode;
709 SERVER_START_REQ( register_async )
711 req->type = ASYNC_TYPE_READ;
712 req->count = length;
713 req->async.handle = wine_server_obj_handle( hFile );
714 req->async.event = wine_server_obj_handle( hEvent );
715 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
716 req->async.iosb = wine_server_client_ptr( io_status );
717 req->async.arg = wine_server_client_ptr( fileio );
718 req->async.cvalue = cvalue;
719 status = wine_server_call( req );
721 SERVER_END_REQ;
723 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
724 goto err;
726 else /* synchronous read, wait for the fd to become ready */
728 struct pollfd pfd;
729 int ret, timeout;
731 if (!timeout_init_done)
733 timeout_init_done = 1;
734 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
735 goto err;
736 if (hEvent) NtResetEvent( hEvent, NULL );
738 timeout = get_next_io_timeout( &timeouts, total );
740 pfd.fd = unix_handle;
741 pfd.events = POLLIN;
743 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
745 if (total) /* return with what we got so far */
746 status = STATUS_SUCCESS;
747 else
748 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
749 goto done;
751 if (ret == -1 && errno != EINTR)
753 status = FILE_GetNtStatus();
754 goto done;
756 /* will now restart the read */
760 done:
761 send_completion = cvalue != 0;
763 err:
764 if (needs_close) close( unix_handle );
765 if (status == STATUS_SUCCESS)
767 io_status->u.Status = status;
768 io_status->Information = total;
769 TRACE("= SUCCESS (%u)\n", total);
770 if (hEvent) NtSetEvent( hEvent, NULL );
771 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
772 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
774 else
776 TRACE("= 0x%08x\n", status);
777 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
780 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
782 return status;
786 /******************************************************************************
787 * NtReadFileScatter [NTDLL.@]
788 * ZwReadFileScatter [NTDLL.@]
790 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
791 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
792 ULONG length, PLARGE_INTEGER offset, PULONG key )
794 int result, unix_handle, needs_close;
795 unsigned int options;
796 NTSTATUS status;
797 ULONG pos = 0, total = 0;
798 enum server_fd_type type;
799 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
800 BOOL send_completion = FALSE;
802 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
803 file, event, apc, apc_user, io_status, segments, length, offset, key);
805 if (length % page_size) return STATUS_INVALID_PARAMETER;
806 if (!io_status) return STATUS_ACCESS_VIOLATION;
808 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
809 &needs_close, &type, &options );
810 if (status) return status;
812 if ((type != FD_TYPE_FILE) ||
813 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
814 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
816 status = STATUS_INVALID_PARAMETER;
817 goto error;
820 while (length)
822 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
823 result = pread( unix_handle, (char *)segments->Buffer + pos,
824 page_size - pos, offset->QuadPart + total );
825 else
826 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
828 if (result == -1)
830 if (errno == EINTR) continue;
831 status = FILE_GetNtStatus();
832 break;
834 if (!result)
836 status = STATUS_END_OF_FILE;
837 break;
839 total += result;
840 length -= result;
841 if ((pos += result) == page_size)
843 pos = 0;
844 segments++;
848 send_completion = cvalue != 0;
850 error:
851 if (needs_close) close( unix_handle );
852 if (status == STATUS_SUCCESS)
854 io_status->u.Status = status;
855 io_status->Information = total;
856 TRACE("= SUCCESS (%u)\n", total);
857 if (event) NtSetEvent( event, NULL );
858 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
859 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
861 else
863 TRACE("= 0x%08x\n", status);
864 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
867 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
869 return status;
873 /***********************************************************************
874 * FILE_AsyncWriteService (INTERNAL)
876 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status, void **apc)
878 async_fileio_write *fileio = user;
879 int result, fd, needs_close;
880 enum server_fd_type type;
882 switch (status)
884 case STATUS_ALERTED:
885 /* write some data (non-blocking) */
886 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
887 &needs_close, &type, NULL )))
888 break;
890 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
891 result = send( fd, fileio->buffer, 0, 0 );
892 else
893 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
895 if (needs_close) close( fd );
897 if (result < 0)
899 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
900 else status = FILE_GetNtStatus();
902 else
904 fileio->already += result;
905 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
907 break;
909 case STATUS_TIMEOUT:
910 case STATUS_IO_TIMEOUT:
911 if (fileio->already) status = STATUS_SUCCESS;
912 break;
914 if (status != STATUS_PENDING)
916 iosb->u.Status = status;
917 iosb->Information = fileio->already;
918 *apc = fileio_apc;
920 return status;
923 /******************************************************************************
924 * NtWriteFile [NTDLL.@]
925 * ZwWriteFile [NTDLL.@]
927 * Write to an open file handle.
929 * PARAMS
930 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
931 * Event [I] Event to signal upon completion (or NULL)
932 * ApcRoutine [I] Callback to call upon completion (or NULL)
933 * ApcContext [I] Context for ApcRoutine (or NULL)
934 * IoStatusBlock [O] Receives information about the operation on return
935 * Buffer [I] Source for the data to write
936 * Length [I] Size of Buffer
937 * ByteOffset [O] Destination for the new file pointer position (or NULL)
938 * Key [O] Function unknown (may be NULL)
940 * RETURNS
941 * Success: 0. IoStatusBlock is updated, and the Information member contains
942 * The number of bytes written.
943 * Failure: An NTSTATUS error code describing the error.
945 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
946 PIO_APC_ROUTINE apc, void* apc_user,
947 PIO_STATUS_BLOCK io_status,
948 const void* buffer, ULONG length,
949 PLARGE_INTEGER offset, PULONG key)
951 int result, unix_handle, needs_close, timeout_init_done = 0;
952 unsigned int options;
953 struct io_timeouts timeouts;
954 NTSTATUS status;
955 ULONG total = 0;
956 enum server_fd_type type;
957 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
958 BOOL send_completion = FALSE, async_write, append_write = FALSE;
959 LARGE_INTEGER offset_eof;
961 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
962 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
964 if (!io_status) return STATUS_ACCESS_VIOLATION;
966 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
967 &needs_close, &type, &options );
968 if (status == STATUS_ACCESS_DENIED)
970 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
971 &needs_close, &type, &options );
972 append_write = TRUE;
974 if (status) return status;
976 if (!virtual_check_buffer_for_read( buffer, length ))
978 status = STATUS_INVALID_USER_BUFFER;
979 goto done;
982 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
984 if (type == FD_TYPE_FILE)
986 if (async_write &&
987 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
989 status = STATUS_INVALID_PARAMETER;
990 goto done;
993 if (append_write)
995 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
996 offset = &offset_eof;
999 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1001 off_t off = offset->QuadPart;
1003 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1005 struct stat st;
1007 if (fstat( unix_handle, &st ) == -1)
1009 status = FILE_GetNtStatus();
1010 goto done;
1012 off = st.st_size;
1014 else if (offset->QuadPart < 0)
1016 status = STATUS_INVALID_PARAMETER;
1017 goto done;
1020 /* async I/O doesn't make sense on regular files */
1021 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1023 if (errno != EINTR)
1025 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1026 else status = FILE_GetNtStatus();
1027 goto done;
1031 if (!async_write)
1032 /* update file pointer position */
1033 lseek( unix_handle, off + result, SEEK_SET );
1035 total = result;
1036 status = STATUS_SUCCESS;
1037 goto done;
1040 else if (type == FD_TYPE_SERIAL)
1042 if (async_write &&
1043 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1045 status = STATUS_INVALID_PARAMETER;
1046 goto done;
1050 for (;;)
1052 /* zero-length writes on sockets may not work with plain write(2) */
1053 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1054 result = send( unix_handle, buffer, 0, 0 );
1055 else
1056 result = write( unix_handle, (const char *)buffer + total, length - total );
1058 if (result >= 0)
1060 total += result;
1061 if (total == length)
1063 status = STATUS_SUCCESS;
1064 goto done;
1066 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1068 else if (errno != EAGAIN)
1070 if (errno == EINTR) continue;
1071 if (!total)
1073 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1074 else status = FILE_GetNtStatus();
1076 goto done;
1079 if (async_write)
1081 async_fileio_write *fileio;
1083 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
1085 status = STATUS_NO_MEMORY;
1086 goto err;
1088 fileio->io.handle = hFile;
1089 fileio->io.apc = apc;
1090 fileio->io.apc_arg = apc_user;
1091 fileio->already = total;
1092 fileio->count = length;
1093 fileio->buffer = buffer;
1095 SERVER_START_REQ( register_async )
1097 req->type = ASYNC_TYPE_WRITE;
1098 req->count = length;
1099 req->async.handle = wine_server_obj_handle( hFile );
1100 req->async.event = wine_server_obj_handle( hEvent );
1101 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1102 req->async.iosb = wine_server_client_ptr( io_status );
1103 req->async.arg = wine_server_client_ptr( fileio );
1104 req->async.cvalue = cvalue;
1105 status = wine_server_call( req );
1107 SERVER_END_REQ;
1109 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1110 goto err;
1112 else /* synchronous write, wait for the fd to become ready */
1114 struct pollfd pfd;
1115 int ret, timeout;
1117 if (!timeout_init_done)
1119 timeout_init_done = 1;
1120 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1121 goto err;
1122 if (hEvent) NtResetEvent( hEvent, NULL );
1124 timeout = get_next_io_timeout( &timeouts, total );
1126 pfd.fd = unix_handle;
1127 pfd.events = POLLOUT;
1129 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1131 /* return with what we got so far */
1132 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1133 goto done;
1135 if (ret == -1 && errno != EINTR)
1137 status = FILE_GetNtStatus();
1138 goto done;
1140 /* will now restart the write */
1144 done:
1145 send_completion = cvalue != 0;
1147 err:
1148 if (needs_close) close( unix_handle );
1149 if (status == STATUS_SUCCESS)
1151 io_status->u.Status = status;
1152 io_status->Information = total;
1153 TRACE("= SUCCESS (%u)\n", total);
1154 if (hEvent) NtSetEvent( hEvent, NULL );
1155 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1156 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1158 else
1160 TRACE("= 0x%08x\n", status);
1161 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1164 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1166 return status;
1170 /******************************************************************************
1171 * NtWriteFileGather [NTDLL.@]
1172 * ZwWriteFileGather [NTDLL.@]
1174 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1175 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1176 ULONG length, PLARGE_INTEGER offset, PULONG key )
1178 int result, unix_handle, needs_close;
1179 unsigned int options;
1180 NTSTATUS status;
1181 ULONG pos = 0, total = 0;
1182 enum server_fd_type type;
1183 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1184 BOOL send_completion = FALSE;
1186 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1187 file, event, apc, apc_user, io_status, segments, length, offset, key);
1189 if (length % page_size) return STATUS_INVALID_PARAMETER;
1190 if (!io_status) return STATUS_ACCESS_VIOLATION;
1192 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1193 &needs_close, &type, &options );
1194 if (status) return status;
1196 if ((type != FD_TYPE_FILE) ||
1197 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1198 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1200 status = STATUS_INVALID_PARAMETER;
1201 goto error;
1204 while (length)
1206 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1207 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1208 page_size - pos, offset->QuadPart + total );
1209 else
1210 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1212 if (result == -1)
1214 if (errno == EINTR) continue;
1215 if (errno == EFAULT)
1217 status = STATUS_INVALID_USER_BUFFER;
1218 goto error;
1220 status = FILE_GetNtStatus();
1221 break;
1223 if (!result)
1225 status = STATUS_DISK_FULL;
1226 break;
1228 total += result;
1229 length -= result;
1230 if ((pos += result) == page_size)
1232 pos = 0;
1233 segments++;
1237 send_completion = cvalue != 0;
1239 error:
1240 if (needs_close) close( unix_handle );
1241 if (status == STATUS_SUCCESS)
1243 io_status->u.Status = status;
1244 io_status->Information = total;
1245 TRACE("= SUCCESS (%u)\n", total);
1246 if (event) NtSetEvent( event, NULL );
1247 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1248 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1250 else
1252 TRACE("= 0x%08x\n", status);
1253 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1256 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1258 return status;
1262 struct async_ioctl
1264 HANDLE handle; /* handle to the device */
1265 HANDLE event; /* async event */
1266 void *buffer; /* buffer for output */
1267 ULONG size; /* size of buffer */
1268 PIO_APC_ROUTINE apc; /* user apc params */
1269 void *apc_arg;
1272 /* callback for ioctl user APC */
1273 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
1275 struct async_ioctl *async = arg;
1276 if (async->apc) async->apc( async->apc_arg, io, reserved );
1277 RtlFreeHeap( GetProcessHeap(), 0, async );
1280 /* callback for ioctl async I/O completion */
1281 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status, void **apc )
1283 struct async_ioctl *async = arg;
1285 if (status == STATUS_ALERTED)
1287 SERVER_START_REQ( get_ioctl_result )
1289 req->handle = wine_server_obj_handle( async->handle );
1290 req->user_arg = wine_server_client_ptr( async );
1291 wine_server_set_reply( req, async->buffer, async->size );
1292 status = wine_server_call( req );
1293 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1295 SERVER_END_REQ;
1297 if (status != STATUS_PENDING)
1299 io->u.Status = status;
1300 if (async->apc || async->event) *apc = ioctl_apc;
1302 return status;
1305 /* do a ioctl call through the server */
1306 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1307 PIO_APC_ROUTINE apc, PVOID apc_context,
1308 IO_STATUS_BLOCK *io, ULONG code,
1309 const void *in_buffer, ULONG in_size,
1310 PVOID out_buffer, ULONG out_size )
1312 struct async_ioctl *async;
1313 NTSTATUS status;
1314 HANDLE wait_handle;
1315 ULONG options;
1316 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1318 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
1319 return STATUS_NO_MEMORY;
1320 async->handle = handle;
1321 async->event = event;
1322 async->buffer = out_buffer;
1323 async->size = out_size;
1324 async->apc = apc;
1325 async->apc_arg = apc_context;
1327 SERVER_START_REQ( ioctl )
1329 req->code = code;
1330 req->blocking = !apc && !event && !cvalue;
1331 req->async.handle = wine_server_obj_handle( handle );
1332 req->async.callback = wine_server_client_ptr( ioctl_completion );
1333 req->async.iosb = wine_server_client_ptr( io );
1334 req->async.arg = wine_server_client_ptr( async );
1335 req->async.event = wine_server_obj_handle( event );
1336 req->async.cvalue = cvalue;
1337 wine_server_add_data( req, in_buffer, in_size );
1338 wine_server_set_reply( req, out_buffer, out_size );
1339 status = wine_server_call( req );
1340 wait_handle = wine_server_ptr_handle( reply->wait );
1341 options = reply->options;
1342 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1344 SERVER_END_REQ;
1346 if (status == STATUS_NOT_SUPPORTED)
1347 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1348 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1350 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1352 if (wait_handle)
1354 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1355 status = io->u.Status;
1356 NtClose( wait_handle );
1357 RtlFreeHeap( GetProcessHeap(), 0, async );
1360 return status;
1363 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1364 * server */
1365 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1366 ULONG in_size)
1368 #ifdef VALGRIND_MAKE_MEM_DEFINED
1369 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1370 do { \
1371 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1372 if ((size) >= FIELD_OFFSET(t, f2)) \
1373 VALGRIND_MAKE_MEM_DEFINED( \
1374 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1375 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1376 } while (0)
1378 switch (code)
1380 case FSCTL_PIPE_WAIT:
1381 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1382 break;
1384 #endif
1388 /**************************************************************************
1389 * NtDeviceIoControlFile [NTDLL.@]
1390 * ZwDeviceIoControlFile [NTDLL.@]
1392 * Perform an I/O control operation on an open file handle.
1394 * PARAMS
1395 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1396 * event [I] Event to signal upon completion (or NULL)
1397 * apc [I] Callback to call upon completion (or NULL)
1398 * apc_context [I] Context for ApcRoutine (or NULL)
1399 * io [O] Receives information about the operation on return
1400 * code [I] Control code for the operation to perform
1401 * in_buffer [I] Source for any input data required (or NULL)
1402 * in_size [I] Size of InputBuffer
1403 * out_buffer [O] Source for any output data returned (or NULL)
1404 * out_size [I] Size of OutputBuffer
1406 * RETURNS
1407 * Success: 0. IoStatusBlock is updated.
1408 * Failure: An NTSTATUS error code describing the error.
1410 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1411 PIO_APC_ROUTINE apc, PVOID apc_context,
1412 PIO_STATUS_BLOCK io, ULONG code,
1413 PVOID in_buffer, ULONG in_size,
1414 PVOID out_buffer, ULONG out_size)
1416 ULONG device = (code >> 16);
1417 NTSTATUS status = STATUS_NOT_SUPPORTED;
1419 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1420 handle, event, apc, apc_context, io, code,
1421 in_buffer, in_size, out_buffer, out_size);
1423 switch(device)
1425 case FILE_DEVICE_DISK:
1426 case FILE_DEVICE_CD_ROM:
1427 case FILE_DEVICE_DVD:
1428 case FILE_DEVICE_CONTROLLER:
1429 case FILE_DEVICE_MASS_STORAGE:
1430 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1431 in_buffer, in_size, out_buffer, out_size);
1432 break;
1433 case FILE_DEVICE_SERIAL_PORT:
1434 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1435 in_buffer, in_size, out_buffer, out_size);
1436 break;
1437 case FILE_DEVICE_TAPE:
1438 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1439 in_buffer, in_size, out_buffer, out_size);
1440 break;
1443 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1444 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1445 in_buffer, in_size, out_buffer, out_size );
1447 if (status != STATUS_PENDING) io->u.Status = status;
1448 return status;
1452 /**************************************************************************
1453 * NtFsControlFile [NTDLL.@]
1454 * ZwFsControlFile [NTDLL.@]
1456 * Perform a file system control operation on an open file handle.
1458 * PARAMS
1459 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1460 * event [I] Event to signal upon completion (or NULL)
1461 * apc [I] Callback to call upon completion (or NULL)
1462 * apc_context [I] Context for ApcRoutine (or NULL)
1463 * io [O] Receives information about the operation on return
1464 * code [I] Control code for the operation to perform
1465 * in_buffer [I] Source for any input data required (or NULL)
1466 * in_size [I] Size of InputBuffer
1467 * out_buffer [O] Source for any output data returned (or NULL)
1468 * out_size [I] Size of OutputBuffer
1470 * RETURNS
1471 * Success: 0. IoStatusBlock is updated.
1472 * Failure: An NTSTATUS error code describing the error.
1474 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1475 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1476 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1478 NTSTATUS status;
1480 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1481 handle, event, apc, apc_context, io, code,
1482 in_buffer, in_size, out_buffer, out_size);
1484 if (!io) return STATUS_INVALID_PARAMETER;
1486 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1488 switch(code)
1490 case FSCTL_DISMOUNT_VOLUME:
1491 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1492 in_buffer, in_size, out_buffer, out_size );
1493 if (!status) status = DIR_unmount_device( handle );
1494 break;
1496 case FSCTL_PIPE_PEEK:
1498 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1499 int avail = 0, fd, needs_close;
1501 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1503 status = STATUS_INFO_LENGTH_MISMATCH;
1504 break;
1507 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1508 break;
1510 #ifdef FIONREAD
1511 if (ioctl( fd, FIONREAD, &avail ) != 0)
1513 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1514 if (needs_close) close( fd );
1515 status = FILE_GetNtStatus();
1516 break;
1518 #endif
1519 if (!avail) /* check for closed pipe */
1521 struct pollfd pollfd;
1522 int ret;
1524 pollfd.fd = fd;
1525 pollfd.events = POLLIN;
1526 pollfd.revents = 0;
1527 ret = poll( &pollfd, 1, 0 );
1528 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1530 if (needs_close) close( fd );
1531 status = STATUS_PIPE_BROKEN;
1532 break;
1535 buffer->NamedPipeState = 0; /* FIXME */
1536 buffer->ReadDataAvailable = avail;
1537 buffer->NumberOfMessages = 0; /* FIXME */
1538 buffer->MessageLength = 0; /* FIXME */
1539 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1540 status = STATUS_SUCCESS;
1541 if (avail)
1543 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1544 if (data_size)
1546 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1547 if (res >= 0) io->Information += res;
1550 if (needs_close) close( fd );
1552 break;
1554 case FSCTL_PIPE_DISCONNECT:
1555 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1556 in_buffer, in_size, out_buffer, out_size );
1557 if (!status)
1559 int fd = server_remove_fd_from_cache( handle );
1560 if (fd != -1) close( fd );
1562 break;
1564 case FSCTL_PIPE_IMPERSONATE:
1565 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1566 status = RtlImpersonateSelf( SecurityImpersonation );
1567 break;
1569 case FSCTL_LOCK_VOLUME:
1570 case FSCTL_UNLOCK_VOLUME:
1571 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1572 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1573 status = STATUS_SUCCESS;
1574 break;
1576 case FSCTL_GET_RETRIEVAL_POINTERS:
1578 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1580 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1582 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1584 buffer->ExtentCount = 1;
1585 buffer->StartingVcn.QuadPart = 1;
1586 buffer->Extents[0].NextVcn.QuadPart = 0;
1587 buffer->Extents[0].Lcn.QuadPart = 0;
1588 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1589 status = STATUS_SUCCESS;
1591 else
1593 io->Information = 0;
1594 status = STATUS_BUFFER_TOO_SMALL;
1596 break;
1598 case FSCTL_PIPE_LISTEN:
1599 case FSCTL_PIPE_WAIT:
1600 default:
1601 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1602 in_buffer, in_size, out_buffer, out_size );
1603 break;
1606 if (status != STATUS_PENDING) io->u.Status = status;
1607 return status;
1610 /******************************************************************************
1611 * NtSetVolumeInformationFile [NTDLL.@]
1612 * ZwSetVolumeInformationFile [NTDLL.@]
1614 * Set volume information for an open file handle.
1616 * PARAMS
1617 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1618 * IoStatusBlock [O] Receives information about the operation on return
1619 * FsInformation [I] Source for volume information
1620 * Length [I] Size of FsInformation
1621 * FsInformationClass [I] Type of volume information to set
1623 * RETURNS
1624 * Success: 0. IoStatusBlock is updated.
1625 * Failure: An NTSTATUS error code describing the error.
1627 NTSTATUS WINAPI NtSetVolumeInformationFile(
1628 IN HANDLE FileHandle,
1629 PIO_STATUS_BLOCK IoStatusBlock,
1630 PVOID FsInformation,
1631 ULONG Length,
1632 FS_INFORMATION_CLASS FsInformationClass)
1634 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1635 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1636 return 0;
1639 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1641 NTSTATUS status = STATUS_SUCCESS;
1643 #ifdef HAVE_FUTIMENS
1644 struct timespec tv[2];
1646 tv[0].tv_sec = tv[1].tv_sec = 0;
1647 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1648 if (atime->QuadPart)
1650 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1651 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1653 if (mtime->QuadPart)
1655 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1656 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1658 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1660 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1661 struct timeval tv[2];
1662 struct stat st;
1664 if (!atime->QuadPart || !mtime->QuadPart)
1667 tv[0].tv_sec = tv[0].tv_usec = 0;
1668 tv[1].tv_sec = tv[1].tv_usec = 0;
1669 if (!fstat( fd, &st ))
1671 tv[0].tv_sec = st.st_atime;
1672 tv[1].tv_sec = st.st_mtime;
1673 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1674 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1675 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1676 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1677 #endif
1678 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1679 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1680 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1681 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1682 #endif
1685 if (atime->QuadPart)
1687 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1688 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1690 if (mtime->QuadPart)
1692 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1693 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1695 #ifdef HAVE_FUTIMES
1696 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1697 #elif defined(HAVE_FUTIMESAT)
1698 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1699 #endif
1701 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1702 FIXME( "setting file times not supported\n" );
1703 status = STATUS_NOT_IMPLEMENTED;
1704 #endif
1705 return status;
1708 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1709 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1711 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1712 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1713 RtlSecondsSince1970ToTime( st->st_atime, atime );
1714 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1715 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1716 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1717 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
1718 #endif
1719 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1720 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
1721 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1722 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
1723 #endif
1724 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1725 atime->QuadPart += st->st_atim.tv_nsec / 100;
1726 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1727 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
1728 #endif
1729 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1730 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
1731 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
1732 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
1733 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1734 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
1735 #endif
1736 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
1737 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
1738 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
1739 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
1740 #endif
1741 #else
1742 *creation = *mtime;
1743 #endif
1746 /* fill in the file information that depends on the stat info */
1747 NTSTATUS fill_stat_info( const struct stat *st, void *ptr, FILE_INFORMATION_CLASS class )
1749 switch (class)
1751 case FileBasicInformation:
1753 FILE_BASIC_INFORMATION *info = ptr;
1755 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1756 &info->LastAccessTime, &info->CreationTime );
1757 if (S_ISDIR(st->st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1758 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1759 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1760 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1762 break;
1763 case FileStandardInformation:
1765 FILE_STANDARD_INFORMATION *info = ptr;
1767 if ((info->Directory = S_ISDIR(st->st_mode)))
1769 info->AllocationSize.QuadPart = 0;
1770 info->EndOfFile.QuadPart = 0;
1771 info->NumberOfLinks = 1;
1773 else
1775 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1776 info->EndOfFile.QuadPart = st->st_size;
1777 info->NumberOfLinks = st->st_nlink;
1780 break;
1781 case FileInternalInformation:
1783 FILE_INTERNAL_INFORMATION *info = ptr;
1784 info->IndexNumber.QuadPart = st->st_ino;
1786 break;
1787 case FileEndOfFileInformation:
1789 FILE_END_OF_FILE_INFORMATION *info = ptr;
1790 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
1792 break;
1793 case FileAllInformation:
1795 FILE_ALL_INFORMATION *info = ptr;
1796 fill_stat_info( st, &info->BasicInformation, FileBasicInformation );
1797 fill_stat_info( st, &info->StandardInformation, FileStandardInformation );
1798 fill_stat_info( st, &info->InternalInformation, FileInternalInformation );
1800 break;
1801 /* all directory structures start with the FileDirectoryInformation layout */
1802 case FileBothDirectoryInformation:
1803 case FileFullDirectoryInformation:
1804 case FileDirectoryInformation:
1806 FILE_DIRECTORY_INFORMATION *info = ptr;
1808 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1809 &info->LastAccessTime, &info->CreationTime );
1810 if (S_ISDIR(st->st_mode))
1812 info->AllocationSize.QuadPart = 0;
1813 info->EndOfFile.QuadPart = 0;
1814 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1816 else
1818 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1819 info->EndOfFile.QuadPart = st->st_size;
1820 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1822 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1823 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1825 break;
1826 case FileIdFullDirectoryInformation:
1828 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
1829 info->FileId.QuadPart = st->st_ino;
1830 fill_stat_info( st, info, FileDirectoryInformation );
1832 break;
1833 case FileIdBothDirectoryInformation:
1835 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
1836 info->FileId.QuadPart = st->st_ino;
1837 fill_stat_info( st, info, FileDirectoryInformation );
1839 break;
1841 default:
1842 return STATUS_INVALID_INFO_CLASS;
1844 return STATUS_SUCCESS;
1847 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
1849 data_size_t size = 1024;
1850 NTSTATUS ret;
1851 char *name;
1853 for (;;)
1855 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
1856 if (!name) return STATUS_NO_MEMORY;
1857 unix_name->MaximumLength = size + 1;
1859 SERVER_START_REQ( get_handle_unix_name )
1861 req->handle = wine_server_obj_handle( handle );
1862 wine_server_set_reply( req, name, size );
1863 ret = wine_server_call( req );
1864 size = reply->name_len;
1866 SERVER_END_REQ;
1868 if (!ret)
1870 name[size] = 0;
1871 unix_name->Buffer = name;
1872 unix_name->Length = size;
1873 break;
1875 RtlFreeHeap( GetProcessHeap(), 0, name );
1876 if (ret != STATUS_BUFFER_OVERFLOW) break;
1878 return ret;
1881 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
1883 UNICODE_STRING nt_name;
1884 NTSTATUS status;
1886 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
1888 const WCHAR *ptr = nt_name.Buffer;
1889 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
1891 /* Skip the volume mount point. */
1892 while (ptr != end && *ptr == '\\') ++ptr;
1893 while (ptr != end && *ptr != '\\') ++ptr;
1894 while (ptr != end && *ptr == '\\') ++ptr;
1895 while (ptr != end && *ptr != '\\') ++ptr;
1897 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
1898 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
1899 else *name_len = info->FileNameLength;
1901 memcpy( info->FileName, ptr, *name_len );
1902 RtlFreeUnicodeString( &nt_name );
1905 return status;
1908 /******************************************************************************
1909 * NtQueryInformationFile [NTDLL.@]
1910 * ZwQueryInformationFile [NTDLL.@]
1912 * Get information about an open file handle.
1914 * PARAMS
1915 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1916 * io [O] Receives information about the operation on return
1917 * ptr [O] Destination for file information
1918 * len [I] Size of FileInformation
1919 * class [I] Type of file information to get
1921 * RETURNS
1922 * Success: 0. IoStatusBlock and FileInformation are updated.
1923 * Failure: An NTSTATUS error code describing the error.
1925 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1926 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1928 static const size_t info_sizes[] =
1931 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1932 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1933 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1934 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1935 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1936 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1937 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1938 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1939 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
1940 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1941 0, /* FileLinkInformation */
1942 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1943 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1944 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1945 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1946 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1947 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1948 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
1949 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1950 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1951 0, /* FileAlternateNameInformation */
1952 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1953 0, /* FilePipeInformation */
1954 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1955 0, /* FilePipeRemoteInformation */
1956 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1957 0, /* FileMailslotSetInformation */
1958 0, /* FileCompressionInformation */
1959 0, /* FileObjectIdInformation */
1960 0, /* FileCompletionInformation */
1961 0, /* FileMoveClusterInformation */
1962 0, /* FileQuotaInformation */
1963 0, /* FileReparsePointInformation */
1964 0, /* FileNetworkOpenInformation */
1965 0, /* FileAttributeTagInformation */
1966 0, /* FileTrackingInformation */
1967 0, /* FileIdBothDirectoryInformation */
1968 0, /* FileIdFullDirectoryInformation */
1969 0, /* FileValidDataLengthInformation */
1970 0, /* FileShortNameInformation */
1974 0, /* FileSfioReserveInformation */
1975 0, /* FileSfioVolumeInformation */
1976 0, /* FileHardLinkInformation */
1978 0, /* FileNormalizedNameInformation */
1980 0, /* FileIdGlobalTxDirectoryInformation */
1984 0 /* FileStandardLinkInformation */
1987 struct stat st;
1988 int fd, needs_close = FALSE;
1990 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1992 io->Information = 0;
1994 if (class <= 0 || class >= FileMaximumInformation)
1995 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1996 if (!info_sizes[class])
1998 FIXME("Unsupported class (%d)\n", class);
1999 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2001 if (len < info_sizes[class])
2002 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2004 if (class != FilePipeLocalInformation)
2006 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2007 return io->u.Status;
2010 switch (class)
2012 case FileBasicInformation:
2013 if (fstat( fd, &st ) == -1)
2014 io->u.Status = FILE_GetNtStatus();
2015 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2016 io->u.Status = STATUS_INVALID_INFO_CLASS;
2017 else
2018 fill_stat_info( &st, ptr, class );
2019 break;
2020 case FileStandardInformation:
2022 FILE_STANDARD_INFORMATION *info = ptr;
2024 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2025 else
2027 fill_stat_info( &st, info, class );
2028 info->DeletePending = FALSE; /* FIXME */
2031 break;
2032 case FilePositionInformation:
2034 FILE_POSITION_INFORMATION *info = ptr;
2035 off_t res = lseek( fd, 0, SEEK_CUR );
2036 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2037 else info->CurrentByteOffset.QuadPart = res;
2039 break;
2040 case FileInternalInformation:
2041 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2042 else fill_stat_info( &st, ptr, class );
2043 break;
2044 case FileEaInformation:
2046 FILE_EA_INFORMATION *info = ptr;
2047 info->EaSize = 0;
2049 break;
2050 case FileEndOfFileInformation:
2051 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2052 else fill_stat_info( &st, ptr, class );
2053 break;
2054 case FileAllInformation:
2056 FILE_ALL_INFORMATION *info = ptr;
2057 ANSI_STRING unix_name;
2059 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2060 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2061 io->u.Status = STATUS_INVALID_INFO_CLASS;
2062 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2064 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2066 fill_stat_info( &st, info, FileAllInformation );
2067 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2068 info->EaInformation.EaSize = 0;
2069 info->AccessInformation.AccessFlags = 0; /* FIXME */
2070 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2071 info->ModeInformation.Mode = 0; /* FIXME */
2072 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2074 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2075 RtlFreeAnsiString( &unix_name );
2076 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2079 break;
2080 case FileMailslotQueryInformation:
2082 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2084 SERVER_START_REQ( set_mailslot_info )
2086 req->handle = wine_server_obj_handle( hFile );
2087 req->flags = 0;
2088 io->u.Status = wine_server_call( req );
2089 if( io->u.Status == STATUS_SUCCESS )
2091 info->MaximumMessageSize = reply->max_msgsize;
2092 info->MailslotQuota = 0;
2093 info->NextMessageSize = 0;
2094 info->MessagesAvailable = 0;
2095 info->ReadTimeout.QuadPart = reply->read_timeout;
2098 SERVER_END_REQ;
2099 if (!io->u.Status)
2101 char *tmpbuf;
2102 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2103 if (size > 0x10000) size = 0x10000;
2104 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2106 int fd, needs_close;
2107 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2109 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2110 info->MessagesAvailable = (res > 0);
2111 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2112 if (needs_close) close( fd );
2114 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2118 break;
2119 case FilePipeLocalInformation:
2121 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2123 SERVER_START_REQ( get_named_pipe_info )
2125 req->handle = wine_server_obj_handle( hFile );
2126 if (!(io->u.Status = wine_server_call( req )))
2128 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2129 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2130 switch (reply->sharing)
2132 case FILE_SHARE_READ:
2133 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2134 break;
2135 case FILE_SHARE_WRITE:
2136 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2137 break;
2138 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2139 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2140 break;
2142 pli->MaximumInstances = reply->maxinstances;
2143 pli->CurrentInstances = reply->instances;
2144 pli->InboundQuota = reply->insize;
2145 pli->ReadDataAvailable = 0; /* FIXME */
2146 pli->OutboundQuota = reply->outsize;
2147 pli->WriteQuotaAvailable = 0; /* FIXME */
2148 pli->NamedPipeState = 0; /* FIXME */
2149 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2150 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2153 SERVER_END_REQ;
2155 break;
2156 case FileNameInformation:
2158 FILE_NAME_INFORMATION *info = ptr;
2159 ANSI_STRING unix_name;
2161 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2163 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2164 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2165 RtlFreeAnsiString( &unix_name );
2166 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2169 break;
2170 default:
2171 FIXME("Unsupported class (%d)\n", class);
2172 io->u.Status = STATUS_NOT_IMPLEMENTED;
2173 break;
2175 if (needs_close) close( fd );
2176 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2177 return io->u.Status;
2180 /******************************************************************************
2181 * NtSetInformationFile [NTDLL.@]
2182 * ZwSetInformationFile [NTDLL.@]
2184 * Set information about an open file handle.
2186 * PARAMS
2187 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2188 * io [O] Receives information about the operation on return
2189 * ptr [I] Source for file information
2190 * len [I] Size of FileInformation
2191 * class [I] Type of file information to set
2193 * RETURNS
2194 * Success: 0. io is updated.
2195 * Failure: An NTSTATUS error code describing the error.
2197 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2198 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2200 int fd, needs_close;
2202 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2204 io->u.Status = STATUS_SUCCESS;
2205 switch (class)
2207 case FileBasicInformation:
2208 if (len >= sizeof(FILE_BASIC_INFORMATION))
2210 struct stat st;
2211 const FILE_BASIC_INFORMATION *info = ptr;
2213 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2214 return io->u.Status;
2216 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2217 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2219 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2221 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2222 else
2224 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2226 if (S_ISDIR( st.st_mode))
2227 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2228 else
2229 st.st_mode &= ~0222; /* clear write permission bits */
2231 else
2233 /* add write permission only where we already have read permission */
2234 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2236 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2240 if (needs_close) close( fd );
2242 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2243 break;
2245 case FilePositionInformation:
2246 if (len >= sizeof(FILE_POSITION_INFORMATION))
2248 const FILE_POSITION_INFORMATION *info = ptr;
2250 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2251 return io->u.Status;
2253 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2254 io->u.Status = FILE_GetNtStatus();
2256 if (needs_close) close( fd );
2258 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2259 break;
2261 case FileEndOfFileInformation:
2262 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2264 struct stat st;
2265 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2267 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2268 return io->u.Status;
2270 /* first try normal truncate */
2271 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2273 /* now check for the need to extend the file */
2274 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2276 static const char zero;
2278 /* extend the file one byte beyond the requested size and then truncate it */
2279 /* this should work around ftruncate implementations that can't extend files */
2280 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2281 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2283 io->u.Status = FILE_GetNtStatus();
2285 if (needs_close) close( fd );
2287 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2288 break;
2290 case FileMailslotSetInformation:
2292 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2294 SERVER_START_REQ( set_mailslot_info )
2296 req->handle = wine_server_obj_handle( handle );
2297 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2298 req->read_timeout = info->ReadTimeout.QuadPart;
2299 io->u.Status = wine_server_call( req );
2301 SERVER_END_REQ;
2303 break;
2305 case FileCompletionInformation:
2306 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2308 FILE_COMPLETION_INFORMATION *info = ptr;
2310 SERVER_START_REQ( set_completion_info )
2312 req->handle = wine_server_obj_handle( handle );
2313 req->chandle = wine_server_obj_handle( info->CompletionPort );
2314 req->ckey = info->CompletionKey;
2315 io->u.Status = wine_server_call( req );
2317 SERVER_END_REQ;
2318 } else
2319 io->u.Status = STATUS_INVALID_PARAMETER_3;
2320 break;
2322 case FileAllInformation:
2323 io->u.Status = STATUS_INVALID_INFO_CLASS;
2324 break;
2326 case FileValidDataLengthInformation:
2327 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2329 struct stat st;
2330 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2332 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2333 return io->u.Status;
2335 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2336 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2337 io->u.Status = STATUS_INVALID_PARAMETER;
2338 else
2340 #ifdef HAVE_FALLOCATE
2341 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2343 NTSTATUS status = FILE_GetNtStatus();
2344 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2345 else io->u.Status = status;
2347 #else
2348 FIXME( "setting valid data length not supported\n" );
2349 #endif
2351 if (needs_close) close( fd );
2353 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2354 break;
2356 default:
2357 FIXME("Unsupported class (%d)\n", class);
2358 io->u.Status = STATUS_NOT_IMPLEMENTED;
2359 break;
2361 io->Information = 0;
2362 return io->u.Status;
2366 /******************************************************************************
2367 * NtQueryFullAttributesFile (NTDLL.@)
2369 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2370 FILE_NETWORK_OPEN_INFORMATION *info )
2372 ANSI_STRING unix_name;
2373 NTSTATUS status;
2375 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2377 struct stat st;
2379 if (stat( unix_name.Buffer, &st ) == -1)
2380 status = FILE_GetNtStatus();
2381 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2382 status = STATUS_INVALID_INFO_CLASS;
2383 else
2385 FILE_BASIC_INFORMATION basic;
2386 FILE_STANDARD_INFORMATION std;
2388 fill_stat_info( &st, &basic, FileBasicInformation );
2389 fill_stat_info( &st, &std, FileStandardInformation );
2391 info->CreationTime = basic.CreationTime;
2392 info->LastAccessTime = basic.LastAccessTime;
2393 info->LastWriteTime = basic.LastWriteTime;
2394 info->ChangeTime = basic.ChangeTime;
2395 info->AllocationSize = std.AllocationSize;
2396 info->EndOfFile = std.EndOfFile;
2397 info->FileAttributes = basic.FileAttributes;
2398 if (DIR_is_hidden_file( attr->ObjectName ))
2399 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2401 RtlFreeAnsiString( &unix_name );
2403 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2404 return status;
2408 /******************************************************************************
2409 * NtQueryAttributesFile (NTDLL.@)
2410 * ZwQueryAttributesFile (NTDLL.@)
2412 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2414 ANSI_STRING unix_name;
2415 NTSTATUS status;
2417 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2419 struct stat st;
2421 if (stat( unix_name.Buffer, &st ) == -1)
2422 status = FILE_GetNtStatus();
2423 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2424 status = STATUS_INVALID_INFO_CLASS;
2425 else
2427 status = fill_stat_info( &st, info, FileBasicInformation );
2428 if (DIR_is_hidden_file( attr->ObjectName ))
2429 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2431 RtlFreeAnsiString( &unix_name );
2433 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2434 return status;
2438 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2439 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2440 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2441 unsigned int flags )
2443 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2445 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2446 /* Don't assume read-only, let the mount options set it below */
2447 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2449 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2450 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2452 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2453 info->Characteristics |= FILE_REMOTE_DEVICE;
2455 else if (!strcmp("procfs", fstypename))
2456 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2457 else
2458 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2460 if (flags & MNT_RDONLY)
2461 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2463 if (!(flags & MNT_LOCAL))
2465 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2466 info->Characteristics |= FILE_REMOTE_DEVICE;
2469 #endif
2471 static inline int is_device_placeholder( int fd )
2473 static const char wine_placeholder[] = "Wine device placeholder";
2474 char buffer[sizeof(wine_placeholder)-1];
2476 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2477 return 0;
2478 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2481 /******************************************************************************
2482 * get_device_info
2484 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2486 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2488 struct stat st;
2490 info->Characteristics = 0;
2491 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2492 if (S_ISCHR( st.st_mode ))
2494 info->DeviceType = FILE_DEVICE_UNKNOWN;
2495 #ifdef linux
2496 switch(major(st.st_rdev))
2498 case MEM_MAJOR:
2499 info->DeviceType = FILE_DEVICE_NULL;
2500 break;
2501 case TTY_MAJOR:
2502 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2503 break;
2504 case LP_MAJOR:
2505 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2506 break;
2507 case SCSI_TAPE_MAJOR:
2508 info->DeviceType = FILE_DEVICE_TAPE;
2509 break;
2511 #endif
2513 else if (S_ISBLK( st.st_mode ))
2515 info->DeviceType = FILE_DEVICE_DISK;
2517 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2519 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2521 else if (is_device_placeholder( fd ))
2523 info->DeviceType = FILE_DEVICE_DISK;
2525 else /* regular file or directory */
2527 #if defined(linux) && defined(HAVE_FSTATFS)
2528 struct statfs stfs;
2530 /* check for floppy disk */
2531 if (major(st.st_dev) == FLOPPY_MAJOR)
2532 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2534 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2535 switch (stfs.f_type)
2537 case 0x9660: /* iso9660 */
2538 case 0x9fa1: /* supermount */
2539 case 0x15013346: /* udf */
2540 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2541 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2542 break;
2543 case 0x6969: /* nfs */
2544 case 0x517B: /* smbfs */
2545 case 0x564c: /* ncpfs */
2546 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2547 info->Characteristics |= FILE_REMOTE_DEVICE;
2548 break;
2549 case 0x01021994: /* tmpfs */
2550 case 0x28cd3d45: /* cramfs */
2551 case 0x1373: /* devfs */
2552 case 0x9fa0: /* procfs */
2553 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2554 break;
2555 default:
2556 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2557 break;
2559 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2560 struct statfs stfs;
2562 if (fstatfs( fd, &stfs ) < 0)
2563 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2564 else
2565 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2566 #elif defined(__NetBSD__)
2567 struct statvfs stfs;
2569 if (fstatvfs( fd, &stfs) < 0)
2570 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2571 else
2572 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2573 #elif defined(sun)
2574 /* Use dkio to work out device types */
2576 # include <sys/dkio.h>
2577 # include <sys/vtoc.h>
2578 struct dk_cinfo dkinf;
2579 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2580 if(retval==-1){
2581 WARN("Unable to get disk device type information - assuming a disk like device\n");
2582 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2584 switch (dkinf.dki_ctype)
2586 case DKC_CDROM:
2587 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2588 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2589 break;
2590 case DKC_NCRFLOPPY:
2591 case DKC_SMSFLOPPY:
2592 case DKC_INTEL82072:
2593 case DKC_INTEL82077:
2594 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2595 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2596 break;
2597 case DKC_MD:
2598 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2599 break;
2600 default:
2601 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2604 #else
2605 static int warned;
2606 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2607 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2608 #endif
2609 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2611 return STATUS_SUCCESS;
2615 /******************************************************************************
2616 * NtQueryVolumeInformationFile [NTDLL.@]
2617 * ZwQueryVolumeInformationFile [NTDLL.@]
2619 * Get volume information for an open file handle.
2621 * PARAMS
2622 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2623 * io [O] Receives information about the operation on return
2624 * buffer [O] Destination for volume information
2625 * length [I] Size of FsInformation
2626 * info_class [I] Type of volume information to set
2628 * RETURNS
2629 * Success: 0. io and buffer are updated.
2630 * Failure: An NTSTATUS error code describing the error.
2632 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2633 PVOID buffer, ULONG length,
2634 FS_INFORMATION_CLASS info_class )
2636 int fd, needs_close;
2637 struct stat st;
2638 static int once;
2640 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2641 return io->u.Status;
2643 io->u.Status = STATUS_NOT_IMPLEMENTED;
2644 io->Information = 0;
2646 switch( info_class )
2648 case FileFsVolumeInformation:
2649 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2650 break;
2651 case FileFsLabelInformation:
2652 FIXME( "%p: label info not supported\n", handle );
2653 break;
2654 case FileFsSizeInformation:
2655 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2656 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2657 else
2659 FILE_FS_SIZE_INFORMATION *info = buffer;
2661 if (fstat( fd, &st ) < 0)
2663 io->u.Status = FILE_GetNtStatus();
2664 break;
2666 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2668 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2670 else
2672 ULONGLONG bsize;
2673 /* Linux's fstatvfs is buggy */
2674 #if !defined(linux) || !defined(HAVE_FSTATFS)
2675 struct statvfs stfs;
2677 if (fstatvfs( fd, &stfs ) < 0)
2679 io->u.Status = FILE_GetNtStatus();
2680 break;
2682 bsize = stfs.f_frsize;
2683 #else
2684 struct statfs stfs;
2685 if (fstatfs( fd, &stfs ) < 0)
2687 io->u.Status = FILE_GetNtStatus();
2688 break;
2690 bsize = stfs.f_bsize;
2691 #endif
2692 if (bsize == 2048) /* assume CD-ROM */
2694 info->BytesPerSector = 2048;
2695 info->SectorsPerAllocationUnit = 1;
2697 else
2699 info->BytesPerSector = 512;
2700 info->SectorsPerAllocationUnit = 8;
2702 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2703 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2704 io->Information = sizeof(*info);
2705 io->u.Status = STATUS_SUCCESS;
2708 break;
2709 case FileFsDeviceInformation:
2710 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2711 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2712 else
2714 FILE_FS_DEVICE_INFORMATION *info = buffer;
2716 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2717 io->Information = sizeof(*info);
2719 break;
2720 case FileFsAttributeInformation:
2721 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
2722 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2723 else
2725 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
2727 FIXME( "%p: faking attribute info\n", handle );
2728 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
2729 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
2730 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
2731 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
2732 info->FileSystemNameLength = sizeof(ntfsW);
2733 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
2735 io->Information = sizeof(*info);
2736 io->u.Status = STATUS_SUCCESS;
2738 break;
2739 case FileFsControlInformation:
2740 FIXME( "%p: control info not supported\n", handle );
2741 break;
2742 case FileFsFullSizeInformation:
2743 FIXME( "%p: full size info not supported\n", handle );
2744 break;
2745 case FileFsObjectIdInformation:
2746 FIXME( "%p: object id info not supported\n", handle );
2747 break;
2748 case FileFsMaximumInformation:
2749 FIXME( "%p: maximum info not supported\n", handle );
2750 break;
2751 default:
2752 io->u.Status = STATUS_INVALID_PARAMETER;
2753 break;
2755 if (needs_close) close( fd );
2756 return io->u.Status;
2760 /******************************************************************
2761 * NtQueryEaFile (NTDLL.@)
2763 * Read extended attributes from NTFS files.
2765 * PARAMS
2766 * hFile [I] File handle, must be opened with FILE_READ_EA access
2767 * iosb [O] Receives information about the operation on return
2768 * buffer [O] Output buffer
2769 * length [I] Length of output buffer
2770 * single_entry [I] Only read and return one entry
2771 * ea_list [I] Optional list with names of EAs to return
2772 * ea_list_len [I] Length of ea_list in bytes
2773 * ea_index [I] Optional pointer to 1-based index of attribute to return
2774 * restart [I] restart EA scan
2776 * RETURNS
2777 * Success: 0. Atrributes read into buffer
2778 * Failure: An NTSTATUS error code describing the error.
2780 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
2781 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
2782 PULONG ea_index, BOOLEAN restart )
2784 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
2785 hFile, iosb, buffer, length, single_entry, ea_list,
2786 ea_list_len, ea_index, restart);
2787 return STATUS_ACCESS_DENIED;
2791 /******************************************************************
2792 * NtSetEaFile (NTDLL.@)
2794 * Update extended attributes for NTFS files.
2796 * PARAMS
2797 * hFile [I] File handle, must be opened with FILE_READ_EA access
2798 * iosb [O] Receives information about the operation on return
2799 * buffer [I] Buffer with EA information
2800 * length [I] Length of buffer
2802 * RETURNS
2803 * Success: 0. Attributes are updated
2804 * Failure: An NTSTATUS error code describing the error.
2806 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
2808 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
2809 return STATUS_ACCESS_DENIED;
2813 /******************************************************************
2814 * NtFlushBuffersFile (NTDLL.@)
2816 * Flush any buffered data on an open file handle.
2818 * PARAMS
2819 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2820 * IoStatusBlock [O] Receives information about the operation on return
2822 * RETURNS
2823 * Success: 0. IoStatusBlock is updated.
2824 * Failure: An NTSTATUS error code describing the error.
2826 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2828 NTSTATUS ret;
2829 HANDLE hEvent = NULL;
2830 enum server_fd_type type;
2831 int fd, needs_close;
2833 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
2835 if (!ret && type == FD_TYPE_SERIAL)
2837 ret = COMM_FlushBuffersFile( fd );
2839 else
2841 SERVER_START_REQ( flush_file )
2843 req->handle = wine_server_obj_handle( hFile );
2844 ret = wine_server_call( req );
2845 hEvent = wine_server_ptr_handle( reply->event );
2847 SERVER_END_REQ;
2848 if (!ret && hEvent)
2850 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2851 NtClose( hEvent );
2855 if (needs_close) close( fd );
2856 return ret;
2859 /******************************************************************
2860 * NtLockFile (NTDLL.@)
2864 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2865 PIO_APC_ROUTINE apc, void* apc_user,
2866 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2867 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2868 BOOLEAN exclusive )
2870 NTSTATUS ret;
2871 HANDLE handle;
2872 BOOLEAN async;
2873 static BOOLEAN warn = TRUE;
2875 if (apc || io_status || key)
2877 FIXME("Unimplemented yet parameter\n");
2878 return STATUS_NOT_IMPLEMENTED;
2881 if (apc_user && warn)
2883 FIXME("I/O completion on lock not implemented yet\n");
2884 warn = FALSE;
2887 for (;;)
2889 SERVER_START_REQ( lock_file )
2891 req->handle = wine_server_obj_handle( hFile );
2892 req->offset = offset->QuadPart;
2893 req->count = count->QuadPart;
2894 req->shared = !exclusive;
2895 req->wait = !dont_wait;
2896 ret = wine_server_call( req );
2897 handle = wine_server_ptr_handle( reply->handle );
2898 async = reply->overlapped;
2900 SERVER_END_REQ;
2901 if (ret != STATUS_PENDING)
2903 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2904 return ret;
2907 if (async)
2909 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2910 if (handle) NtClose( handle );
2911 return STATUS_PENDING;
2913 if (handle)
2915 NtWaitForSingleObject( handle, FALSE, NULL );
2916 NtClose( handle );
2918 else
2920 LARGE_INTEGER time;
2922 /* Unix lock conflict, sleep a bit and retry */
2923 time.QuadPart = 100 * (ULONGLONG)10000;
2924 time.QuadPart = -time.QuadPart;
2925 NtDelayExecution( FALSE, &time );
2931 /******************************************************************
2932 * NtUnlockFile (NTDLL.@)
2936 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2937 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2938 PULONG key )
2940 NTSTATUS status;
2942 TRACE( "%p %x%08x %x%08x\n",
2943 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2945 if (io_status || key)
2947 FIXME("Unimplemented yet parameter\n");
2948 return STATUS_NOT_IMPLEMENTED;
2951 SERVER_START_REQ( unlock_file )
2953 req->handle = wine_server_obj_handle( hFile );
2954 req->offset = offset->QuadPart;
2955 req->count = count->QuadPart;
2956 status = wine_server_call( req );
2958 SERVER_END_REQ;
2959 return status;
2962 /******************************************************************
2963 * NtCreateNamedPipeFile (NTDLL.@)
2967 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2968 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2969 ULONG sharing, ULONG dispo, ULONG options,
2970 ULONG pipe_type, ULONG read_mode,
2971 ULONG completion_mode, ULONG max_inst,
2972 ULONG inbound_quota, ULONG outbound_quota,
2973 PLARGE_INTEGER timeout)
2975 NTSTATUS status;
2977 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2978 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2979 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2980 outbound_quota, timeout);
2982 /* assume we only get relative timeout */
2983 if (timeout->QuadPart > 0)
2984 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2986 SERVER_START_REQ( create_named_pipe )
2988 req->access = access;
2989 req->attributes = attr->Attributes;
2990 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2991 req->options = options;
2992 req->sharing = sharing;
2993 req->flags =
2994 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
2995 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
2996 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
2997 req->maxinstances = max_inst;
2998 req->outsize = outbound_quota;
2999 req->insize = inbound_quota;
3000 req->timeout = timeout->QuadPart;
3001 wine_server_add_data( req, attr->ObjectName->Buffer,
3002 attr->ObjectName->Length );
3003 status = wine_server_call( req );
3004 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3006 SERVER_END_REQ;
3007 return status;
3010 /******************************************************************
3011 * NtDeleteFile (NTDLL.@)
3015 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3017 NTSTATUS status;
3018 HANDLE hFile;
3019 IO_STATUS_BLOCK io;
3021 TRACE("%p\n", ObjectAttributes);
3022 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3023 ObjectAttributes, &io, NULL, 0,
3024 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3025 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3026 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3027 return status;
3030 /******************************************************************
3031 * NtCancelIoFileEx (NTDLL.@)
3035 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3037 LARGE_INTEGER timeout;
3039 TRACE("%p %p %p\n", hFile, iosb, io_status );
3041 SERVER_START_REQ( cancel_async )
3043 req->handle = wine_server_obj_handle( hFile );
3044 req->iosb = wine_server_client_ptr( iosb );
3045 req->only_thread = FALSE;
3046 io_status->u.Status = wine_server_call( req );
3048 SERVER_END_REQ;
3049 if (io_status->u.Status)
3050 return io_status->u.Status;
3052 /* Let some APC be run, so that we can run the remaining APCs on hFile
3053 * either the cancelation of the pending one, but also the execution
3054 * of the queued APC, but not yet run. This is needed to ensure proper
3055 * clean-up of allocated data.
3057 timeout.QuadPart = 0;
3058 NtDelayExecution( TRUE, &timeout );
3059 return io_status->u.Status;
3062 /******************************************************************
3063 * NtCancelIoFile (NTDLL.@)
3067 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3069 LARGE_INTEGER timeout;
3071 TRACE("%p %p\n", hFile, io_status );
3073 SERVER_START_REQ( cancel_async )
3075 req->handle = wine_server_obj_handle( hFile );
3076 req->iosb = 0;
3077 req->only_thread = TRUE;
3078 io_status->u.Status = wine_server_call( req );
3080 SERVER_END_REQ;
3081 if (io_status->u.Status)
3082 return io_status->u.Status;
3084 /* Let some APC be run, so that we can run the remaining APCs on hFile
3085 * either the cancelation of the pending one, but also the execution
3086 * of the queued APC, but not yet run. This is needed to ensure proper
3087 * clean-up of allocated data.
3089 timeout.QuadPart = 0;
3090 NtDelayExecution( TRUE, &timeout );
3091 return io_status->u.Status;
3094 /******************************************************************************
3095 * NtCreateMailslotFile [NTDLL.@]
3096 * ZwCreateMailslotFile [NTDLL.@]
3098 * PARAMS
3099 * pHandle [O] pointer to receive the handle created
3100 * DesiredAccess [I] access mode (read, write, etc)
3101 * ObjectAttributes [I] fully qualified NT path of the mailslot
3102 * IoStatusBlock [O] receives completion status and other info
3103 * CreateOptions [I]
3104 * MailslotQuota [I]
3105 * MaxMessageSize [I]
3106 * TimeOut [I]
3108 * RETURNS
3109 * An NT status code
3111 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3112 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3113 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3114 PLARGE_INTEGER TimeOut)
3116 LARGE_INTEGER timeout;
3117 NTSTATUS ret;
3119 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3120 pHandle, DesiredAccess, attr, IoStatusBlock,
3121 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3123 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3124 if (!attr) return STATUS_INVALID_PARAMETER;
3125 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3128 * For a NULL TimeOut pointer set the default timeout value
3130 if (!TimeOut)
3131 timeout.QuadPart = -1;
3132 else
3133 timeout.QuadPart = TimeOut->QuadPart;
3135 SERVER_START_REQ( create_mailslot )
3137 req->access = DesiredAccess;
3138 req->attributes = attr->Attributes;
3139 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3140 req->max_msgsize = MaxMessageSize;
3141 req->read_timeout = timeout.QuadPart;
3142 wine_server_add_data( req, attr->ObjectName->Buffer,
3143 attr->ObjectName->Length );
3144 ret = wine_server_call( req );
3145 if( ret == STATUS_SUCCESS )
3146 *pHandle = wine_server_ptr_handle( reply->handle );
3148 SERVER_END_REQ;
3150 return ret;