ole32: Use share modes when creating storage files.
[wine/multimedia.git] / dlls / ntdll / file.c
blobd2efcc19c7142934c2f8865e7d303b15413b3de1
1 /*
2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_LINUX_MAJOR_H
31 # include <linux/major.h>
32 #endif
33 #ifdef HAVE_SYS_STATVFS_H
34 # include <sys/statvfs.h>
35 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYS_SYSCALL_H
40 # include <sys/syscall.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
50 #endif
51 #ifdef HAVE_POLL_H
52 #include <poll.h>
53 #endif
54 #ifdef HAVE_SYS_POLL_H
55 #include <sys/poll.h>
56 #endif
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef HAVE_UTIME_H
61 # include <utime.h>
62 #endif
63 #ifdef HAVE_SYS_VFS_H
64 # include <sys/vfs.h>
65 #endif
66 #ifdef HAVE_SYS_MOUNT_H
67 # include <sys/mount.h>
68 #endif
69 #ifdef HAVE_SYS_STATFS_H
70 # include <sys/statfs.h>
71 #endif
72 #ifdef HAVE_TERMIOS_H
73 #include <termios.h>
74 #endif
75 #ifdef HAVE_VALGRIND_MEMCHECK_H
76 # include <valgrind/memcheck.h>
77 #endif
79 #define NONAMELESSUNION
80 #define NONAMELESSSTRUCT
81 #include "ntstatus.h"
82 #define WIN32_NO_STATUS
83 #include "wine/unicode.h"
84 #include "wine/debug.h"
85 #include "wine/server.h"
86 #include "ntdll_misc.h"
88 #include "winternl.h"
89 #include "winioctl.h"
90 #include "ddk/ntddk.h"
91 #include "ddk/ntddser.h"
93 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
94 WINE_DECLARE_DEBUG_CHANNEL(winediag);
96 mode_t FILE_umask = 0;
98 #define SECSPERDAY 86400
99 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
101 #define FILE_WRITE_TO_END_OF_FILE ((LONGLONG)-1)
102 #define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
104 static const WCHAR ntfsW[] = {'N','T','F','S'};
106 /**************************************************************************
107 * FILE_CreateFile (internal)
108 * Open a file.
110 * Parameter set fully identical with NtCreateFile
112 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
113 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
114 ULONG attributes, ULONG sharing, ULONG disposition,
115 ULONG options, PVOID ea_buffer, ULONG ea_length )
117 ANSI_STRING unix_name;
118 BOOL created = FALSE;
120 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p "
121 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
122 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
123 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
124 attributes, sharing, disposition, options, ea_buffer, ea_length );
126 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
128 if (alloc_size) FIXME( "alloc_size not supported\n" );
130 if (options & FILE_OPEN_BY_FILE_ID)
131 io->u.Status = file_id_to_unix_file_name( attr, &unix_name );
132 else
133 io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition );
135 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
137 SERVER_START_REQ( open_file_object )
139 req->access = access;
140 req->attributes = attr->Attributes;
141 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
142 req->sharing = sharing;
143 req->options = options;
144 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
145 io->u.Status = wine_server_call( req );
146 *handle = wine_server_ptr_handle( reply->handle );
148 SERVER_END_REQ;
149 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
150 return io->u.Status;
153 if (io->u.Status == STATUS_NO_SUCH_FILE &&
154 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
156 created = TRUE;
157 io->u.Status = STATUS_SUCCESS;
160 if (io->u.Status == STATUS_SUCCESS)
162 struct security_descriptor *sd;
163 struct object_attributes objattr;
165 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
166 objattr.name_len = 0;
167 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
168 if (io->u.Status != STATUS_SUCCESS)
170 RtlFreeAnsiString( &unix_name );
171 return io->u.Status;
174 SERVER_START_REQ( create_file )
176 req->access = access;
177 req->attributes = attr->Attributes;
178 req->sharing = sharing;
179 req->create = disposition;
180 req->options = options;
181 req->attrs = attributes;
182 wine_server_add_data( req, &objattr, sizeof(objattr) );
183 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
184 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
185 io->u.Status = wine_server_call( req );
186 *handle = wine_server_ptr_handle( reply->handle );
188 SERVER_END_REQ;
189 NTDLL_free_struct_sd( sd );
190 RtlFreeAnsiString( &unix_name );
192 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
194 if (io->u.Status == STATUS_SUCCESS)
196 if (created) io->Information = FILE_CREATED;
197 else switch(disposition)
199 case FILE_SUPERSEDE:
200 io->Information = FILE_SUPERSEDED;
201 break;
202 case FILE_CREATE:
203 io->Information = FILE_CREATED;
204 break;
205 case FILE_OPEN:
206 case FILE_OPEN_IF:
207 io->Information = FILE_OPENED;
208 break;
209 case FILE_OVERWRITE:
210 case FILE_OVERWRITE_IF:
211 io->Information = FILE_OVERWRITTEN;
212 break;
215 else if (io->u.Status == STATUS_TOO_MANY_OPENED_FILES)
217 static int once;
218 if (!once++) ERR_(winediag)( "Too many open files, ulimit -n probably needs to be increased\n" );
221 return io->u.Status;
224 /**************************************************************************
225 * NtOpenFile [NTDLL.@]
226 * ZwOpenFile [NTDLL.@]
228 * Open a file.
230 * PARAMS
231 * handle [O] Variable that receives the file handle on return
232 * access [I] Access desired by the caller to the file
233 * attr [I] Structure describing the file to be opened
234 * io [O] Receives details about the result of the operation
235 * sharing [I] Type of shared access the caller requires
236 * options [I] Options for the file open
238 * RETURNS
239 * Success: 0. FileHandle and IoStatusBlock are updated.
240 * Failure: An NTSTATUS error code describing the error.
242 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
243 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
244 ULONG sharing, ULONG options )
246 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
247 sharing, FILE_OPEN, options, NULL, 0 );
250 /**************************************************************************
251 * NtCreateFile [NTDLL.@]
252 * ZwCreateFile [NTDLL.@]
254 * Either create a new file or directory, or open an existing file, device,
255 * directory or volume.
257 * PARAMS
258 * handle [O] Points to a variable which receives the file handle on return
259 * access [I] Desired access to the file
260 * attr [I] Structure describing the file
261 * io [O] Receives information about the operation on return
262 * alloc_size [I] Initial size of the file in bytes
263 * attributes [I] Attributes to create the file with
264 * sharing [I] Type of shared access the caller would like to the file
265 * disposition [I] Specifies what to do, depending on whether the file already exists
266 * options [I] Options for creating a new file
267 * ea_buffer [I] Pointer to an extended attributes buffer
268 * ea_length [I] Length of ea_buffer
270 * RETURNS
271 * Success: 0. handle and io are updated.
272 * Failure: An NTSTATUS error code describing the error.
274 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
275 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
276 ULONG attributes, ULONG sharing, ULONG disposition,
277 ULONG options, PVOID ea_buffer, ULONG ea_length )
279 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
280 sharing, disposition, options, ea_buffer, ea_length );
283 /***********************************************************************
284 * Asynchronous file I/O *
287 struct async_fileio
289 HANDLE handle;
290 PIO_APC_ROUTINE apc;
291 void *apc_arg;
294 typedef struct
296 struct async_fileio io;
297 char* buffer;
298 unsigned int already;
299 unsigned int count;
300 BOOL avail_mode;
301 } async_fileio_read;
303 typedef struct
305 struct async_fileio io;
306 const char *buffer;
307 unsigned int already;
308 unsigned int count;
309 } async_fileio_write;
312 /* callback for file I/O user APC */
313 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
315 struct async_fileio *async = arg;
316 if (async->apc) async->apc( async->apc_arg, io, reserved );
317 RtlFreeHeap( GetProcessHeap(), 0, async );
320 /***********************************************************************
321 * FILE_GetNtStatus(void)
323 * Retrieve the Nt Status code from errno.
324 * Try to be consistent with FILE_SetDosError().
326 NTSTATUS FILE_GetNtStatus(void)
328 int err = errno;
330 TRACE( "errno = %d\n", errno );
331 switch (err)
333 case EAGAIN: return STATUS_SHARING_VIOLATION;
334 case EBADF: return STATUS_INVALID_HANDLE;
335 case EBUSY: return STATUS_DEVICE_BUSY;
336 case ENOSPC: return STATUS_DISK_FULL;
337 case EPERM:
338 case EROFS:
339 case EACCES: return STATUS_ACCESS_DENIED;
340 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
341 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
342 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
343 case EMFILE:
344 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
345 case EINVAL: return STATUS_INVALID_PARAMETER;
346 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
347 case EPIPE: return STATUS_PIPE_DISCONNECTED;
348 case EIO: return STATUS_DEVICE_NOT_READY;
349 #ifdef ENOMEDIUM
350 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
351 #endif
352 case ENXIO: return STATUS_NO_SUCH_DEVICE;
353 case ENOTTY:
354 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
355 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
356 case EFAULT: return STATUS_ACCESS_VIOLATION;
357 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
358 #ifdef ETIME /* Missing on FreeBSD */
359 case ETIME: return STATUS_IO_TIMEOUT;
360 #endif
361 case ENOEXEC: /* ?? */
362 case EEXIST: /* ?? */
363 default:
364 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
365 return STATUS_UNSUCCESSFUL;
369 /***********************************************************************
370 * FILE_AsyncReadService (INTERNAL)
372 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc)
374 async_fileio_read *fileio = user;
375 int fd, needs_close, result;
377 switch (status)
379 case STATUS_ALERTED: /* got some new data */
380 /* check to see if the data is ready (non-blocking) */
381 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
382 &needs_close, NULL, NULL )))
383 break;
385 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
386 if (needs_close) close( fd );
388 if (result < 0)
390 if (errno == EAGAIN || errno == EINTR)
391 status = STATUS_PENDING;
392 else /* check to see if the transfer is complete */
393 status = FILE_GetNtStatus();
395 else if (result == 0)
397 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
399 else
401 fileio->already += result;
402 if (fileio->already >= fileio->count || fileio->avail_mode)
403 status = STATUS_SUCCESS;
404 else
406 /* if we only have to read the available data, and none is available,
407 * simply cancel the request. If data was available, it has been read
408 * while in by previous call (NtDelayExecution)
410 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
413 break;
415 case STATUS_TIMEOUT:
416 case STATUS_IO_TIMEOUT:
417 if (fileio->already) status = STATUS_SUCCESS;
418 break;
420 if (status != STATUS_PENDING)
422 iosb->u.Status = status;
423 iosb->Information = fileio->already;
424 *apc = fileio_apc;
426 return status;
429 struct io_timeouts
431 int interval; /* max interval between two bytes */
432 int total; /* total timeout for the whole operation */
433 int end_time; /* absolute time of end of operation */
436 /* retrieve the I/O timeouts to use for a given handle */
437 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
438 struct io_timeouts *timeouts )
440 NTSTATUS status = STATUS_SUCCESS;
442 timeouts->interval = timeouts->total = -1;
444 switch(type)
446 case FD_TYPE_SERIAL:
448 /* GetCommTimeouts */
449 SERIAL_TIMEOUTS st;
450 IO_STATUS_BLOCK io;
452 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
453 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
454 if (status) break;
456 if (is_read)
458 if (st.ReadIntervalTimeout)
459 timeouts->interval = st.ReadIntervalTimeout;
461 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
463 timeouts->total = st.ReadTotalTimeoutConstant;
464 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
465 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
467 else if (st.ReadIntervalTimeout == MAXDWORD)
468 timeouts->interval = timeouts->total = 0;
470 else /* write */
472 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
474 timeouts->total = st.WriteTotalTimeoutConstant;
475 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
476 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
480 break;
481 case FD_TYPE_MAILSLOT:
482 if (is_read)
484 timeouts->interval = 0; /* return as soon as we got something */
485 SERVER_START_REQ( set_mailslot_info )
487 req->handle = wine_server_obj_handle( handle );
488 req->flags = 0;
489 if (!(status = wine_server_call( req )) &&
490 reply->read_timeout != TIMEOUT_INFINITE)
491 timeouts->total = reply->read_timeout / -10000;
493 SERVER_END_REQ;
495 break;
496 case FD_TYPE_SOCKET:
497 case FD_TYPE_PIPE:
498 case FD_TYPE_CHAR:
499 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
500 break;
501 default:
502 break;
504 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
505 return STATUS_SUCCESS;
509 /* retrieve the timeout for the next wait, in milliseconds */
510 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
512 int ret = -1;
514 if (timeouts->total != -1)
516 ret = timeouts->end_time - NtGetTickCount();
517 if (ret < 0) ret = 0;
519 if (already && timeouts->interval != -1)
521 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
523 return ret;
527 /* retrieve the avail_mode flag for async reads */
528 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
530 NTSTATUS status = STATUS_SUCCESS;
532 switch(type)
534 case FD_TYPE_SERIAL:
536 /* GetCommTimeouts */
537 SERIAL_TIMEOUTS st;
538 IO_STATUS_BLOCK io;
540 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
541 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
542 if (status) break;
543 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
544 !st.ReadTotalTimeoutConstant &&
545 st.ReadIntervalTimeout == MAXDWORD);
547 break;
548 case FD_TYPE_MAILSLOT:
549 case FD_TYPE_SOCKET:
550 case FD_TYPE_PIPE:
551 case FD_TYPE_CHAR:
552 *avail_mode = TRUE;
553 break;
554 default:
555 *avail_mode = FALSE;
556 break;
558 return status;
562 /******************************************************************************
563 * NtReadFile [NTDLL.@]
564 * ZwReadFile [NTDLL.@]
566 * Read from an open file handle.
568 * PARAMS
569 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
570 * Event [I] Event to signal upon completion (or NULL)
571 * ApcRoutine [I] Callback to call upon completion (or NULL)
572 * ApcContext [I] Context for ApcRoutine (or NULL)
573 * IoStatusBlock [O] Receives information about the operation on return
574 * Buffer [O] Destination for the data read
575 * Length [I] Size of Buffer
576 * ByteOffset [O] Destination for the new file pointer position (or NULL)
577 * Key [O] Function unknown (may be NULL)
579 * RETURNS
580 * Success: 0. IoStatusBlock is updated, and the Information member contains
581 * The number of bytes read.
582 * Failure: An NTSTATUS error code describing the error.
584 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
585 PIO_APC_ROUTINE apc, void* apc_user,
586 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
587 PLARGE_INTEGER offset, PULONG key)
589 int result, unix_handle, needs_close;
590 unsigned int options;
591 struct io_timeouts timeouts;
592 NTSTATUS status;
593 ULONG total = 0;
594 enum server_fd_type type;
595 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
596 BOOL send_completion = FALSE, async_read, timeout_init_done = FALSE;
598 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
599 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
601 if (!io_status) return STATUS_ACCESS_VIOLATION;
603 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
604 &needs_close, &type, &options );
605 if (status) return status;
607 async_read = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
609 if (!virtual_check_buffer_for_write( buffer, length ))
611 status = STATUS_ACCESS_VIOLATION;
612 goto done;
615 if (type == FD_TYPE_FILE)
617 if (async_read && (!offset || offset->QuadPart < 0))
619 status = STATUS_INVALID_PARAMETER;
620 goto done;
623 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
625 /* async I/O doesn't make sense on regular files */
626 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
628 if (errno != EINTR)
630 status = FILE_GetNtStatus();
631 goto done;
634 if (!async_read)
635 /* update file pointer position */
636 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
638 total = result;
639 status = (total || !length) ? STATUS_SUCCESS : STATUS_END_OF_FILE;
640 goto done;
643 else if (type == FD_TYPE_SERIAL)
645 if (async_read && (!offset || offset->QuadPart < 0))
647 status = STATUS_INVALID_PARAMETER;
648 goto done;
652 for (;;)
654 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
656 total += result;
657 if (!result || total == length)
659 if (total)
661 status = STATUS_SUCCESS;
662 goto done;
664 switch (type)
666 case FD_TYPE_FILE:
667 case FD_TYPE_CHAR:
668 status = length ? STATUS_END_OF_FILE : STATUS_SUCCESS;
669 goto done;
670 case FD_TYPE_SERIAL:
671 break;
672 default:
673 status = STATUS_PIPE_BROKEN;
674 goto done;
677 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
679 else if (errno != EAGAIN)
681 if (errno == EINTR) continue;
682 if (!total) status = FILE_GetNtStatus();
683 goto done;
686 if (async_read)
688 async_fileio_read *fileio;
689 BOOL avail_mode;
691 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
692 goto err;
693 if (total && avail_mode)
695 status = STATUS_SUCCESS;
696 goto done;
699 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
701 status = STATUS_NO_MEMORY;
702 goto err;
704 fileio->io.handle = hFile;
705 fileio->io.apc = apc;
706 fileio->io.apc_arg = apc_user;
707 fileio->already = total;
708 fileio->count = length;
709 fileio->buffer = buffer;
710 fileio->avail_mode = avail_mode;
712 SERVER_START_REQ( register_async )
714 req->type = ASYNC_TYPE_READ;
715 req->count = length;
716 req->async.handle = wine_server_obj_handle( hFile );
717 req->async.event = wine_server_obj_handle( hEvent );
718 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
719 req->async.iosb = wine_server_client_ptr( io_status );
720 req->async.arg = wine_server_client_ptr( fileio );
721 req->async.cvalue = cvalue;
722 status = wine_server_call( req );
724 SERVER_END_REQ;
726 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
727 goto err;
729 else /* synchronous read, wait for the fd to become ready */
731 struct pollfd pfd;
732 int ret, timeout;
734 if (!timeout_init_done)
736 timeout_init_done = TRUE;
737 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
738 goto err;
739 if (hEvent) NtResetEvent( hEvent, NULL );
741 timeout = get_next_io_timeout( &timeouts, total );
743 pfd.fd = unix_handle;
744 pfd.events = POLLIN;
746 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
748 if (total) /* return with what we got so far */
749 status = STATUS_SUCCESS;
750 else
751 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
752 goto done;
754 if (ret == -1 && errno != EINTR)
756 status = FILE_GetNtStatus();
757 goto done;
759 /* will now restart the read */
763 done:
764 send_completion = cvalue != 0;
766 err:
767 if (needs_close) close( unix_handle );
768 if (status == STATUS_SUCCESS || (status == STATUS_END_OF_FILE && !async_read))
770 io_status->u.Status = status;
771 io_status->Information = total;
772 TRACE("= SUCCESS (%u)\n", total);
773 if (hEvent) NtSetEvent( hEvent, NULL );
774 if (apc && !status) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
775 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
777 else
779 TRACE("= 0x%08x\n", status);
780 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
783 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
785 return status;
789 /******************************************************************************
790 * NtReadFileScatter [NTDLL.@]
791 * ZwReadFileScatter [NTDLL.@]
793 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
794 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
795 ULONG length, PLARGE_INTEGER offset, PULONG key )
797 int result, unix_handle, needs_close;
798 unsigned int options;
799 NTSTATUS status;
800 ULONG pos = 0, total = 0;
801 enum server_fd_type type;
802 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
803 BOOL send_completion = FALSE;
805 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
806 file, event, apc, apc_user, io_status, segments, length, offset, key);
808 if (length % page_size) return STATUS_INVALID_PARAMETER;
809 if (!io_status) return STATUS_ACCESS_VIOLATION;
811 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
812 &needs_close, &type, &options );
813 if (status) return status;
815 if ((type != FD_TYPE_FILE) ||
816 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
817 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
819 status = STATUS_INVALID_PARAMETER;
820 goto error;
823 while (length)
825 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
826 result = pread( unix_handle, (char *)segments->Buffer + pos,
827 page_size - pos, offset->QuadPart + total );
828 else
829 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
831 if (result == -1)
833 if (errno == EINTR) continue;
834 status = FILE_GetNtStatus();
835 break;
837 if (!result)
839 status = STATUS_END_OF_FILE;
840 break;
842 total += result;
843 length -= result;
844 if ((pos += result) == page_size)
846 pos = 0;
847 segments++;
851 send_completion = cvalue != 0;
853 error:
854 if (needs_close) close( unix_handle );
855 if (status == STATUS_SUCCESS)
857 io_status->u.Status = status;
858 io_status->Information = total;
859 TRACE("= SUCCESS (%u)\n", total);
860 if (event) NtSetEvent( event, NULL );
861 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
862 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
864 else
866 TRACE("= 0x%08x\n", status);
867 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
870 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
872 return status;
876 /***********************************************************************
877 * FILE_AsyncWriteService (INTERNAL)
879 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status, void **apc)
881 async_fileio_write *fileio = user;
882 int result, fd, needs_close;
883 enum server_fd_type type;
885 switch (status)
887 case STATUS_ALERTED:
888 /* write some data (non-blocking) */
889 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
890 &needs_close, &type, NULL )))
891 break;
893 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
894 result = send( fd, fileio->buffer, 0, 0 );
895 else
896 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
898 if (needs_close) close( fd );
900 if (result < 0)
902 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
903 else status = FILE_GetNtStatus();
905 else
907 fileio->already += result;
908 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
910 break;
912 case STATUS_TIMEOUT:
913 case STATUS_IO_TIMEOUT:
914 if (fileio->already) status = STATUS_SUCCESS;
915 break;
917 if (status != STATUS_PENDING)
919 iosb->u.Status = status;
920 iosb->Information = fileio->already;
921 *apc = fileio_apc;
923 return status;
926 static NTSTATUS set_pending_write( HANDLE device )
928 NTSTATUS status;
930 SERVER_START_REQ( set_serial_info )
932 req->handle = wine_server_obj_handle( device );
933 req->flags = SERIALINFO_PENDING_WRITE;
934 status = wine_server_call( req );
936 SERVER_END_REQ;
937 return status;
940 /******************************************************************************
941 * NtWriteFile [NTDLL.@]
942 * ZwWriteFile [NTDLL.@]
944 * Write to an open file handle.
946 * PARAMS
947 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
948 * Event [I] Event to signal upon completion (or NULL)
949 * ApcRoutine [I] Callback to call upon completion (or NULL)
950 * ApcContext [I] Context for ApcRoutine (or NULL)
951 * IoStatusBlock [O] Receives information about the operation on return
952 * Buffer [I] Source for the data to write
953 * Length [I] Size of Buffer
954 * ByteOffset [O] Destination for the new file pointer position (or NULL)
955 * Key [O] Function unknown (may be NULL)
957 * RETURNS
958 * Success: 0. IoStatusBlock is updated, and the Information member contains
959 * The number of bytes written.
960 * Failure: An NTSTATUS error code describing the error.
962 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
963 PIO_APC_ROUTINE apc, void* apc_user,
964 PIO_STATUS_BLOCK io_status,
965 const void* buffer, ULONG length,
966 PLARGE_INTEGER offset, PULONG key)
968 int result, unix_handle, needs_close;
969 unsigned int options;
970 struct io_timeouts timeouts;
971 NTSTATUS status;
972 ULONG total = 0;
973 enum server_fd_type type;
974 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
975 BOOL send_completion = FALSE, async_write, append_write = FALSE, timeout_init_done = FALSE;
976 LARGE_INTEGER offset_eof;
978 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
979 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
981 if (!io_status) return STATUS_ACCESS_VIOLATION;
983 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
984 &needs_close, &type, &options );
985 if (status == STATUS_ACCESS_DENIED)
987 status = server_get_unix_fd( hFile, FILE_APPEND_DATA, &unix_handle,
988 &needs_close, &type, &options );
989 append_write = TRUE;
991 if (status) return status;
993 if (!virtual_check_buffer_for_read( buffer, length ))
995 status = STATUS_INVALID_USER_BUFFER;
996 goto done;
999 async_write = !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
1001 if (type == FD_TYPE_FILE)
1003 if (async_write &&
1004 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1006 status = STATUS_INVALID_PARAMETER;
1007 goto done;
1010 if (append_write)
1012 offset_eof.QuadPart = FILE_WRITE_TO_END_OF_FILE;
1013 offset = &offset_eof;
1016 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1018 off_t off = offset->QuadPart;
1020 if (offset->QuadPart == FILE_WRITE_TO_END_OF_FILE)
1022 struct stat st;
1024 if (fstat( unix_handle, &st ) == -1)
1026 status = FILE_GetNtStatus();
1027 goto done;
1029 off = st.st_size;
1031 else if (offset->QuadPart < 0)
1033 status = STATUS_INVALID_PARAMETER;
1034 goto done;
1037 /* async I/O doesn't make sense on regular files */
1038 while ((result = pwrite( unix_handle, buffer, length, off )) == -1)
1040 if (errno != EINTR)
1042 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1043 else status = FILE_GetNtStatus();
1044 goto done;
1048 if (!async_write)
1049 /* update file pointer position */
1050 lseek( unix_handle, off + result, SEEK_SET );
1052 total = result;
1053 status = STATUS_SUCCESS;
1054 goto done;
1057 else if (type == FD_TYPE_SERIAL)
1059 if (async_write &&
1060 (!offset || (offset->QuadPart < 0 && offset->QuadPart != FILE_WRITE_TO_END_OF_FILE)))
1062 status = STATUS_INVALID_PARAMETER;
1063 goto done;
1067 for (;;)
1069 /* zero-length writes on sockets may not work with plain write(2) */
1070 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
1071 result = send( unix_handle, buffer, 0, 0 );
1072 else
1073 result = write( unix_handle, (const char *)buffer + total, length - total );
1075 if (result >= 0)
1077 total += result;
1078 if (total == length)
1080 status = STATUS_SUCCESS;
1081 goto done;
1083 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
1085 else if (errno != EAGAIN)
1087 if (errno == EINTR) continue;
1088 if (!total)
1090 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
1091 else status = FILE_GetNtStatus();
1093 goto done;
1096 if (async_write)
1098 async_fileio_write *fileio;
1100 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
1102 status = STATUS_NO_MEMORY;
1103 goto err;
1105 fileio->io.handle = hFile;
1106 fileio->io.apc = apc;
1107 fileio->io.apc_arg = apc_user;
1108 fileio->already = total;
1109 fileio->count = length;
1110 fileio->buffer = buffer;
1112 SERVER_START_REQ( register_async )
1114 req->type = ASYNC_TYPE_WRITE;
1115 req->count = length;
1116 req->async.handle = wine_server_obj_handle( hFile );
1117 req->async.event = wine_server_obj_handle( hEvent );
1118 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1119 req->async.iosb = wine_server_client_ptr( io_status );
1120 req->async.arg = wine_server_client_ptr( fileio );
1121 req->async.cvalue = cvalue;
1122 status = wine_server_call( req );
1124 SERVER_END_REQ;
1126 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1127 goto err;
1129 else /* synchronous write, wait for the fd to become ready */
1131 struct pollfd pfd;
1132 int ret, timeout;
1134 if (!timeout_init_done)
1136 timeout_init_done = TRUE;
1137 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1138 goto err;
1139 if (hEvent) NtResetEvent( hEvent, NULL );
1141 timeout = get_next_io_timeout( &timeouts, total );
1143 pfd.fd = unix_handle;
1144 pfd.events = POLLOUT;
1146 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1148 /* return with what we got so far */
1149 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1150 goto done;
1152 if (ret == -1 && errno != EINTR)
1154 status = FILE_GetNtStatus();
1155 goto done;
1157 /* will now restart the write */
1161 done:
1162 send_completion = cvalue != 0;
1164 err:
1165 if (needs_close) close( unix_handle );
1167 if (type == FD_TYPE_SERIAL && (status == STATUS_SUCCESS || status == STATUS_PENDING))
1168 set_pending_write( hFile );
1170 if (status == STATUS_SUCCESS)
1172 io_status->u.Status = status;
1173 io_status->Information = total;
1174 TRACE("= SUCCESS (%u)\n", total);
1175 if (hEvent) NtSetEvent( hEvent, NULL );
1176 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1177 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1179 else
1181 TRACE("= 0x%08x\n", status);
1182 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1185 if (send_completion) NTDLL_AddCompletion( hFile, cvalue, status, total );
1187 return status;
1191 /******************************************************************************
1192 * NtWriteFileGather [NTDLL.@]
1193 * ZwWriteFileGather [NTDLL.@]
1195 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1196 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1197 ULONG length, PLARGE_INTEGER offset, PULONG key )
1199 int result, unix_handle, needs_close;
1200 unsigned int options;
1201 NTSTATUS status;
1202 ULONG pos = 0, total = 0;
1203 enum server_fd_type type;
1204 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1205 BOOL send_completion = FALSE;
1207 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1208 file, event, apc, apc_user, io_status, segments, length, offset, key);
1210 if (length % page_size) return STATUS_INVALID_PARAMETER;
1211 if (!io_status) return STATUS_ACCESS_VIOLATION;
1213 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1214 &needs_close, &type, &options );
1215 if (status) return status;
1217 if ((type != FD_TYPE_FILE) ||
1218 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1219 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1221 status = STATUS_INVALID_PARAMETER;
1222 goto error;
1225 while (length)
1227 if (offset && offset->QuadPart != FILE_USE_FILE_POINTER_POSITION)
1228 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1229 page_size - pos, offset->QuadPart + total );
1230 else
1231 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1233 if (result == -1)
1235 if (errno == EINTR) continue;
1236 if (errno == EFAULT)
1238 status = STATUS_INVALID_USER_BUFFER;
1239 goto error;
1241 status = FILE_GetNtStatus();
1242 break;
1244 if (!result)
1246 status = STATUS_DISK_FULL;
1247 break;
1249 total += result;
1250 length -= result;
1251 if ((pos += result) == page_size)
1253 pos = 0;
1254 segments++;
1258 send_completion = cvalue != 0;
1260 error:
1261 if (needs_close) close( unix_handle );
1262 if (status == STATUS_SUCCESS)
1264 io_status->u.Status = status;
1265 io_status->Information = total;
1266 TRACE("= SUCCESS (%u)\n", total);
1267 if (event) NtSetEvent( event, NULL );
1268 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1269 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1271 else
1273 TRACE("= 0x%08x\n", status);
1274 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1277 if (send_completion) NTDLL_AddCompletion( file, cvalue, status, total );
1279 return status;
1283 struct async_ioctl
1285 HANDLE handle; /* handle to the device */
1286 HANDLE event; /* async event */
1287 void *buffer; /* buffer for output */
1288 ULONG size; /* size of buffer */
1289 PIO_APC_ROUTINE apc; /* user apc params */
1290 void *apc_arg;
1293 /* callback for ioctl user APC */
1294 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
1296 struct async_ioctl *async = arg;
1297 if (async->apc) async->apc( async->apc_arg, io, reserved );
1298 RtlFreeHeap( GetProcessHeap(), 0, async );
1301 /* callback for ioctl async I/O completion */
1302 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status, void **apc )
1304 struct async_ioctl *async = arg;
1306 if (status == STATUS_ALERTED)
1308 SERVER_START_REQ( get_ioctl_result )
1310 req->handle = wine_server_obj_handle( async->handle );
1311 req->user_arg = wine_server_client_ptr( async );
1312 wine_server_set_reply( req, async->buffer, async->size );
1313 status = wine_server_call( req );
1314 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1316 SERVER_END_REQ;
1318 if (status != STATUS_PENDING)
1320 io->u.Status = status;
1321 if (async->apc || async->event) *apc = ioctl_apc;
1323 return status;
1326 /* do an ioctl call through the server */
1327 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1328 PIO_APC_ROUTINE apc, PVOID apc_context,
1329 IO_STATUS_BLOCK *io, ULONG code,
1330 const void *in_buffer, ULONG in_size,
1331 PVOID out_buffer, ULONG out_size )
1333 struct async_ioctl *async;
1334 NTSTATUS status;
1335 HANDLE wait_handle;
1336 ULONG options;
1337 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1339 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
1340 return STATUS_NO_MEMORY;
1341 async->handle = handle;
1342 async->event = event;
1343 async->buffer = out_buffer;
1344 async->size = out_size;
1345 async->apc = apc;
1346 async->apc_arg = apc_context;
1348 SERVER_START_REQ( ioctl )
1350 req->code = code;
1351 req->blocking = !apc && !event && !cvalue;
1352 req->async.handle = wine_server_obj_handle( handle );
1353 req->async.callback = wine_server_client_ptr( ioctl_completion );
1354 req->async.iosb = wine_server_client_ptr( io );
1355 req->async.arg = wine_server_client_ptr( async );
1356 req->async.event = wine_server_obj_handle( event );
1357 req->async.cvalue = cvalue;
1358 wine_server_add_data( req, in_buffer, in_size );
1359 wine_server_set_reply( req, out_buffer, out_size );
1360 status = wine_server_call( req );
1361 wait_handle = wine_server_ptr_handle( reply->wait );
1362 options = reply->options;
1363 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1365 SERVER_END_REQ;
1367 if (status == STATUS_NOT_SUPPORTED)
1368 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1369 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1371 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1373 if (wait_handle)
1375 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1376 status = io->u.Status;
1377 NtClose( wait_handle );
1378 RtlFreeHeap( GetProcessHeap(), 0, async );
1381 return status;
1384 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1385 * server */
1386 static void ignore_server_ioctl_struct_holes (ULONG code, const void *in_buffer,
1387 ULONG in_size)
1389 #ifdef VALGRIND_MAKE_MEM_DEFINED
1390 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
1391 do { \
1392 if (FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1) < FIELD_OFFSET(t, f2)) \
1393 if ((size) >= FIELD_OFFSET(t, f2)) \
1394 VALGRIND_MAKE_MEM_DEFINED( \
1395 (const char *)(buf) + FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1), \
1396 FIELD_OFFSET(t, f2) - FIELD_OFFSET(t, f1) + sizeof(((t *)0)->f1)); \
1397 } while (0)
1399 switch (code)
1401 case FSCTL_PIPE_WAIT:
1402 IGNORE_STRUCT_HOLE(in_buffer, in_size, FILE_PIPE_WAIT_FOR_BUFFER, TimeoutSpecified, Name);
1403 break;
1405 #endif
1409 /**************************************************************************
1410 * NtDeviceIoControlFile [NTDLL.@]
1411 * ZwDeviceIoControlFile [NTDLL.@]
1413 * Perform an I/O control operation on an open file handle.
1415 * PARAMS
1416 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1417 * event [I] Event to signal upon completion (or NULL)
1418 * apc [I] Callback to call upon completion (or NULL)
1419 * apc_context [I] Context for ApcRoutine (or NULL)
1420 * io [O] Receives information about the operation on return
1421 * code [I] Control code for the operation to perform
1422 * in_buffer [I] Source for any input data required (or NULL)
1423 * in_size [I] Size of InputBuffer
1424 * out_buffer [O] Source for any output data returned (or NULL)
1425 * out_size [I] Size of OutputBuffer
1427 * RETURNS
1428 * Success: 0. IoStatusBlock is updated.
1429 * Failure: An NTSTATUS error code describing the error.
1431 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1432 PIO_APC_ROUTINE apc, PVOID apc_context,
1433 PIO_STATUS_BLOCK io, ULONG code,
1434 PVOID in_buffer, ULONG in_size,
1435 PVOID out_buffer, ULONG out_size)
1437 ULONG device = (code >> 16);
1438 NTSTATUS status = STATUS_NOT_SUPPORTED;
1440 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1441 handle, event, apc, apc_context, io, code,
1442 in_buffer, in_size, out_buffer, out_size);
1444 switch(device)
1446 case FILE_DEVICE_DISK:
1447 case FILE_DEVICE_CD_ROM:
1448 case FILE_DEVICE_DVD:
1449 case FILE_DEVICE_CONTROLLER:
1450 case FILE_DEVICE_MASS_STORAGE:
1451 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1452 in_buffer, in_size, out_buffer, out_size);
1453 break;
1454 case FILE_DEVICE_SERIAL_PORT:
1455 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1456 in_buffer, in_size, out_buffer, out_size);
1457 break;
1458 case FILE_DEVICE_TAPE:
1459 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1460 in_buffer, in_size, out_buffer, out_size);
1461 break;
1464 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1465 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1466 in_buffer, in_size, out_buffer, out_size );
1468 if (status != STATUS_PENDING) io->u.Status = status;
1469 return status;
1473 /**************************************************************************
1474 * NtFsControlFile [NTDLL.@]
1475 * ZwFsControlFile [NTDLL.@]
1477 * Perform a file system control operation on an open file handle.
1479 * PARAMS
1480 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1481 * event [I] Event to signal upon completion (or NULL)
1482 * apc [I] Callback to call upon completion (or NULL)
1483 * apc_context [I] Context for ApcRoutine (or NULL)
1484 * io [O] Receives information about the operation on return
1485 * code [I] Control code for the operation to perform
1486 * in_buffer [I] Source for any input data required (or NULL)
1487 * in_size [I] Size of InputBuffer
1488 * out_buffer [O] Source for any output data returned (or NULL)
1489 * out_size [I] Size of OutputBuffer
1491 * RETURNS
1492 * Success: 0. IoStatusBlock is updated.
1493 * Failure: An NTSTATUS error code describing the error.
1495 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1496 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1497 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1499 NTSTATUS status;
1501 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1502 handle, event, apc, apc_context, io, code,
1503 in_buffer, in_size, out_buffer, out_size);
1505 if (!io) return STATUS_INVALID_PARAMETER;
1507 ignore_server_ioctl_struct_holes( code, in_buffer, in_size );
1509 switch(code)
1511 case FSCTL_DISMOUNT_VOLUME:
1512 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1513 in_buffer, in_size, out_buffer, out_size );
1514 if (!status) status = DIR_unmount_device( handle );
1515 break;
1517 case FSCTL_PIPE_PEEK:
1519 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1520 int avail = 0, fd, needs_close;
1522 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1524 status = STATUS_INFO_LENGTH_MISMATCH;
1525 break;
1528 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1529 break;
1531 #ifdef FIONREAD
1532 if (ioctl( fd, FIONREAD, &avail ) != 0)
1534 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1535 if (needs_close) close( fd );
1536 status = FILE_GetNtStatus();
1537 break;
1539 #endif
1540 if (!avail) /* check for closed pipe */
1542 struct pollfd pollfd;
1543 int ret;
1545 pollfd.fd = fd;
1546 pollfd.events = POLLIN;
1547 pollfd.revents = 0;
1548 ret = poll( &pollfd, 1, 0 );
1549 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1551 if (needs_close) close( fd );
1552 status = STATUS_PIPE_BROKEN;
1553 break;
1556 buffer->NamedPipeState = 0; /* FIXME */
1557 buffer->ReadDataAvailable = avail;
1558 buffer->NumberOfMessages = 0; /* FIXME */
1559 buffer->MessageLength = 0; /* FIXME */
1560 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1561 status = STATUS_SUCCESS;
1562 if (avail)
1564 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1565 if (data_size)
1567 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1568 if (res >= 0) io->Information += res;
1571 if (needs_close) close( fd );
1573 break;
1575 case FSCTL_PIPE_DISCONNECT:
1576 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1577 in_buffer, in_size, out_buffer, out_size );
1578 if (!status)
1580 int fd = server_remove_fd_from_cache( handle );
1581 if (fd != -1) close( fd );
1583 break;
1585 case FSCTL_PIPE_IMPERSONATE:
1586 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1587 status = RtlImpersonateSelf( SecurityImpersonation );
1588 break;
1590 case FSCTL_IS_VOLUME_MOUNTED:
1591 case FSCTL_LOCK_VOLUME:
1592 case FSCTL_UNLOCK_VOLUME:
1593 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1594 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1595 status = STATUS_SUCCESS;
1596 break;
1598 case FSCTL_GET_RETRIEVAL_POINTERS:
1600 RETRIEVAL_POINTERS_BUFFER *buffer = (RETRIEVAL_POINTERS_BUFFER *)out_buffer;
1602 FIXME("stub: FSCTL_GET_RETRIEVAL_POINTERS\n");
1604 if (out_size >= sizeof(RETRIEVAL_POINTERS_BUFFER))
1606 buffer->ExtentCount = 1;
1607 buffer->StartingVcn.QuadPart = 1;
1608 buffer->Extents[0].NextVcn.QuadPart = 0;
1609 buffer->Extents[0].Lcn.QuadPart = 0;
1610 io->Information = sizeof(RETRIEVAL_POINTERS_BUFFER);
1611 status = STATUS_SUCCESS;
1613 else
1615 io->Information = 0;
1616 status = STATUS_BUFFER_TOO_SMALL;
1618 break;
1620 case FSCTL_PIPE_LISTEN:
1621 case FSCTL_PIPE_WAIT:
1622 default:
1623 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1624 in_buffer, in_size, out_buffer, out_size );
1625 break;
1628 if (status != STATUS_PENDING) io->u.Status = status;
1629 return status;
1632 /******************************************************************************
1633 * NtSetVolumeInformationFile [NTDLL.@]
1634 * ZwSetVolumeInformationFile [NTDLL.@]
1636 * Set volume information for an open file handle.
1638 * PARAMS
1639 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1640 * IoStatusBlock [O] Receives information about the operation on return
1641 * FsInformation [I] Source for volume information
1642 * Length [I] Size of FsInformation
1643 * FsInformationClass [I] Type of volume information to set
1645 * RETURNS
1646 * Success: 0. IoStatusBlock is updated.
1647 * Failure: An NTSTATUS error code describing the error.
1649 NTSTATUS WINAPI NtSetVolumeInformationFile(
1650 IN HANDLE FileHandle,
1651 PIO_STATUS_BLOCK IoStatusBlock,
1652 PVOID FsInformation,
1653 ULONG Length,
1654 FS_INFORMATION_CLASS FsInformationClass)
1656 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1657 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1658 return 0;
1661 #if defined(__ANDROID__) && !defined(HAVE_FUTIMENS)
1662 static int futimens( int fd, const struct timespec spec[2] )
1664 return syscall( __NR_utimensat, fd, NULL, spec, 0 );
1666 #define UTIME_OMIT ((1 << 30) - 2)
1667 #define HAVE_FUTIMENS
1668 #endif /* __ANDROID__ */
1670 static NTSTATUS set_file_times( int fd, const LARGE_INTEGER *mtime, const LARGE_INTEGER *atime )
1672 NTSTATUS status = STATUS_SUCCESS;
1674 #ifdef HAVE_FUTIMENS
1675 struct timespec tv[2];
1677 tv[0].tv_sec = tv[1].tv_sec = 0;
1678 tv[0].tv_nsec = tv[1].tv_nsec = UTIME_OMIT;
1679 if (atime->QuadPart)
1681 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1682 tv[0].tv_nsec = (atime->QuadPart % 10000000) * 100;
1684 if (mtime->QuadPart)
1686 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1687 tv[1].tv_nsec = (mtime->QuadPart % 10000000) * 100;
1689 if (futimens( fd, tv ) == -1) status = FILE_GetNtStatus();
1691 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT)
1692 struct timeval tv[2];
1693 struct stat st;
1695 if (!atime->QuadPart || !mtime->QuadPart)
1698 tv[0].tv_sec = tv[0].tv_usec = 0;
1699 tv[1].tv_sec = tv[1].tv_usec = 0;
1700 if (!fstat( fd, &st ))
1702 tv[0].tv_sec = st.st_atime;
1703 tv[1].tv_sec = st.st_mtime;
1704 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1705 tv[0].tv_usec = st.st_atim.tv_nsec / 1000;
1706 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1707 tv[0].tv_usec = st.st_atimespec.tv_nsec / 1000;
1708 #endif
1709 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1710 tv[1].tv_usec = st.st_mtim.tv_nsec / 1000;
1711 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1712 tv[1].tv_usec = st.st_mtimespec.tv_nsec / 1000;
1713 #endif
1716 if (atime->QuadPart)
1718 tv[0].tv_sec = atime->QuadPart / 10000000 - SECS_1601_TO_1970;
1719 tv[0].tv_usec = (atime->QuadPart % 10000000) / 10;
1721 if (mtime->QuadPart)
1723 tv[1].tv_sec = mtime->QuadPart / 10000000 - SECS_1601_TO_1970;
1724 tv[1].tv_usec = (mtime->QuadPart % 10000000) / 10;
1726 #ifdef HAVE_FUTIMES
1727 if (futimes( fd, tv ) == -1) status = FILE_GetNtStatus();
1728 #elif defined(HAVE_FUTIMESAT)
1729 if (futimesat( fd, NULL, tv ) == -1) status = FILE_GetNtStatus();
1730 #endif
1732 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1733 FIXME( "setting file times not supported\n" );
1734 status = STATUS_NOT_IMPLEMENTED;
1735 #endif
1736 return status;
1739 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1740 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1742 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1743 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1744 RtlSecondsSince1970ToTime( st->st_atime, atime );
1745 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1746 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1747 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1748 mtime->QuadPart += st->st_mtimespec.tv_nsec / 100;
1749 #endif
1750 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1751 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
1752 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1753 ctime->QuadPart += st->st_ctimespec.tv_nsec / 100;
1754 #endif
1755 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1756 atime->QuadPart += st->st_atim.tv_nsec / 100;
1757 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1758 atime->QuadPart += st->st_atimespec.tv_nsec / 100;
1759 #endif
1760 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1761 RtlSecondsSince1970ToTime( st->st_birthtime, creation );
1762 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIM
1763 creation->QuadPart += st->st_birthtim.tv_nsec / 100;
1764 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1765 creation->QuadPart += st->st_birthtimespec.tv_nsec / 100;
1766 #endif
1767 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
1768 RtlSecondsSince1970ToTime( st->__st_birthtime, creation );
1769 #ifdef HAVE_STRUCT_STAT___ST_BIRTHTIM
1770 creation->QuadPart += st->__st_birthtim.tv_nsec / 100;
1771 #endif
1772 #else
1773 *creation = *mtime;
1774 #endif
1777 /* fill in the file information that depends on the stat info */
1778 NTSTATUS fill_stat_info( const struct stat *st, void *ptr, FILE_INFORMATION_CLASS class )
1780 switch (class)
1782 case FileBasicInformation:
1784 FILE_BASIC_INFORMATION *info = ptr;
1786 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1787 &info->LastAccessTime, &info->CreationTime );
1788 if (S_ISDIR(st->st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1789 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1790 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1791 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1793 break;
1794 case FileStandardInformation:
1796 FILE_STANDARD_INFORMATION *info = ptr;
1798 if ((info->Directory = S_ISDIR(st->st_mode)))
1800 info->AllocationSize.QuadPart = 0;
1801 info->EndOfFile.QuadPart = 0;
1802 info->NumberOfLinks = 1;
1804 else
1806 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1807 info->EndOfFile.QuadPart = st->st_size;
1808 info->NumberOfLinks = st->st_nlink;
1811 break;
1812 case FileInternalInformation:
1814 FILE_INTERNAL_INFORMATION *info = ptr;
1815 info->IndexNumber.QuadPart = st->st_ino;
1817 break;
1818 case FileEndOfFileInformation:
1820 FILE_END_OF_FILE_INFORMATION *info = ptr;
1821 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
1823 break;
1824 case FileAllInformation:
1826 FILE_ALL_INFORMATION *info = ptr;
1827 fill_stat_info( st, &info->BasicInformation, FileBasicInformation );
1828 fill_stat_info( st, &info->StandardInformation, FileStandardInformation );
1829 fill_stat_info( st, &info->InternalInformation, FileInternalInformation );
1831 break;
1832 /* all directory structures start with the FileDirectoryInformation layout */
1833 case FileBothDirectoryInformation:
1834 case FileFullDirectoryInformation:
1835 case FileDirectoryInformation:
1837 FILE_DIRECTORY_INFORMATION *info = ptr;
1839 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1840 &info->LastAccessTime, &info->CreationTime );
1841 if (S_ISDIR(st->st_mode))
1843 info->AllocationSize.QuadPart = 0;
1844 info->EndOfFile.QuadPart = 0;
1845 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1847 else
1849 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1850 info->EndOfFile.QuadPart = st->st_size;
1851 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1853 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1854 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1856 break;
1857 case FileIdFullDirectoryInformation:
1859 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
1860 info->FileId.QuadPart = st->st_ino;
1861 fill_stat_info( st, info, FileDirectoryInformation );
1863 break;
1864 case FileIdBothDirectoryInformation:
1866 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
1867 info->FileId.QuadPart = st->st_ino;
1868 fill_stat_info( st, info, FileDirectoryInformation );
1870 break;
1872 default:
1873 return STATUS_INVALID_INFO_CLASS;
1875 return STATUS_SUCCESS;
1878 NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
1880 data_size_t size = 1024;
1881 NTSTATUS ret;
1882 char *name;
1884 for (;;)
1886 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
1887 if (!name) return STATUS_NO_MEMORY;
1888 unix_name->MaximumLength = size + 1;
1890 SERVER_START_REQ( get_handle_unix_name )
1892 req->handle = wine_server_obj_handle( handle );
1893 wine_server_set_reply( req, name, size );
1894 ret = wine_server_call( req );
1895 size = reply->name_len;
1897 SERVER_END_REQ;
1899 if (!ret)
1901 name[size] = 0;
1902 unix_name->Buffer = name;
1903 unix_name->Length = size;
1904 break;
1906 RtlFreeHeap( GetProcessHeap(), 0, name );
1907 if (ret != STATUS_BUFFER_OVERFLOW) break;
1909 return ret;
1912 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
1914 UNICODE_STRING nt_name;
1915 NTSTATUS status;
1917 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
1919 const WCHAR *ptr = nt_name.Buffer;
1920 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
1922 /* Skip the volume mount point. */
1923 while (ptr != end && *ptr == '\\') ++ptr;
1924 while (ptr != end && *ptr != '\\') ++ptr;
1925 while (ptr != end && *ptr == '\\') ++ptr;
1926 while (ptr != end && *ptr != '\\') ++ptr;
1928 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
1929 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
1930 else *name_len = info->FileNameLength;
1932 memcpy( info->FileName, ptr, *name_len );
1933 RtlFreeUnicodeString( &nt_name );
1936 return status;
1939 /******************************************************************************
1940 * NtQueryInformationFile [NTDLL.@]
1941 * ZwQueryInformationFile [NTDLL.@]
1943 * Get information about an open file handle.
1945 * PARAMS
1946 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1947 * io [O] Receives information about the operation on return
1948 * ptr [O] Destination for file information
1949 * len [I] Size of FileInformation
1950 * class [I] Type of file information to get
1952 * RETURNS
1953 * Success: 0. IoStatusBlock and FileInformation are updated.
1954 * Failure: An NTSTATUS error code describing the error.
1956 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1957 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1959 static const size_t info_sizes[] =
1962 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1963 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1964 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1965 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1966 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1967 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1968 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1969 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1970 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
1971 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1972 0, /* FileLinkInformation */
1973 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1974 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1975 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1976 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1977 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1978 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1979 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
1980 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1981 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1982 0, /* FileAlternateNameInformation */
1983 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1984 0, /* FilePipeInformation */
1985 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1986 0, /* FilePipeRemoteInformation */
1987 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1988 0, /* FileMailslotSetInformation */
1989 0, /* FileCompressionInformation */
1990 0, /* FileObjectIdInformation */
1991 0, /* FileCompletionInformation */
1992 0, /* FileMoveClusterInformation */
1993 0, /* FileQuotaInformation */
1994 0, /* FileReparsePointInformation */
1995 0, /* FileNetworkOpenInformation */
1996 0, /* FileAttributeTagInformation */
1997 0, /* FileTrackingInformation */
1998 0, /* FileIdBothDirectoryInformation */
1999 0, /* FileIdFullDirectoryInformation */
2000 0, /* FileValidDataLengthInformation */
2001 0, /* FileShortNameInformation */
2005 0, /* FileSfioReserveInformation */
2006 0, /* FileSfioVolumeInformation */
2007 0, /* FileHardLinkInformation */
2009 0, /* FileNormalizedNameInformation */
2011 0, /* FileIdGlobalTxDirectoryInformation */
2015 0 /* FileStandardLinkInformation */
2018 struct stat st;
2019 int fd, needs_close = FALSE;
2021 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
2023 io->Information = 0;
2025 if (class <= 0 || class >= FileMaximumInformation)
2026 return io->u.Status = STATUS_INVALID_INFO_CLASS;
2027 if (!info_sizes[class])
2029 FIXME("Unsupported class (%d)\n", class);
2030 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2032 if (len < info_sizes[class])
2033 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2035 if (class != FilePipeLocalInformation)
2037 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
2038 return io->u.Status;
2041 switch (class)
2043 case FileBasicInformation:
2044 if (fstat( fd, &st ) == -1)
2045 io->u.Status = FILE_GetNtStatus();
2046 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2047 io->u.Status = STATUS_INVALID_INFO_CLASS;
2048 else
2049 fill_stat_info( &st, ptr, class );
2050 break;
2051 case FileStandardInformation:
2053 FILE_STANDARD_INFORMATION *info = ptr;
2055 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2056 else
2058 fill_stat_info( &st, info, class );
2059 info->DeletePending = FALSE; /* FIXME */
2062 break;
2063 case FilePositionInformation:
2065 FILE_POSITION_INFORMATION *info = ptr;
2066 off_t res = lseek( fd, 0, SEEK_CUR );
2067 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
2068 else info->CurrentByteOffset.QuadPart = res;
2070 break;
2071 case FileInternalInformation:
2072 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2073 else fill_stat_info( &st, ptr, class );
2074 break;
2075 case FileEaInformation:
2077 FILE_EA_INFORMATION *info = ptr;
2078 info->EaSize = 0;
2080 break;
2081 case FileEndOfFileInformation:
2082 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2083 else fill_stat_info( &st, ptr, class );
2084 break;
2085 case FileAllInformation:
2087 FILE_ALL_INFORMATION *info = ptr;
2088 ANSI_STRING unix_name;
2090 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2091 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2092 io->u.Status = STATUS_INVALID_INFO_CLASS;
2093 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2095 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
2097 fill_stat_info( &st, info, FileAllInformation );
2098 info->StandardInformation.DeletePending = FALSE; /* FIXME */
2099 info->EaInformation.EaSize = 0;
2100 info->AccessInformation.AccessFlags = 0; /* FIXME */
2101 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
2102 info->ModeInformation.Mode = 0; /* FIXME */
2103 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
2105 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
2106 RtlFreeAnsiString( &unix_name );
2107 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
2110 break;
2111 case FileMailslotQueryInformation:
2113 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
2115 SERVER_START_REQ( set_mailslot_info )
2117 req->handle = wine_server_obj_handle( hFile );
2118 req->flags = 0;
2119 io->u.Status = wine_server_call( req );
2120 if( io->u.Status == STATUS_SUCCESS )
2122 info->MaximumMessageSize = reply->max_msgsize;
2123 info->MailslotQuota = 0;
2124 info->NextMessageSize = 0;
2125 info->MessagesAvailable = 0;
2126 info->ReadTimeout.QuadPart = reply->read_timeout;
2129 SERVER_END_REQ;
2130 if (!io->u.Status)
2132 char *tmpbuf;
2133 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
2134 if (size > 0x10000) size = 0x10000;
2135 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2137 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
2139 int res = recv( fd, tmpbuf, size, MSG_PEEK );
2140 info->MessagesAvailable = (res > 0);
2141 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
2142 if (needs_close) close( fd );
2144 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
2148 break;
2149 case FilePipeLocalInformation:
2151 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2153 SERVER_START_REQ( get_named_pipe_info )
2155 req->handle = wine_server_obj_handle( hFile );
2156 if (!(io->u.Status = wine_server_call( req )))
2158 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2159 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2160 switch (reply->sharing)
2162 case FILE_SHARE_READ:
2163 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2164 break;
2165 case FILE_SHARE_WRITE:
2166 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2167 break;
2168 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2169 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2170 break;
2172 pli->MaximumInstances = reply->maxinstances;
2173 pli->CurrentInstances = reply->instances;
2174 pli->InboundQuota = reply->insize;
2175 pli->ReadDataAvailable = 0; /* FIXME */
2176 pli->OutboundQuota = reply->outsize;
2177 pli->WriteQuotaAvailable = 0; /* FIXME */
2178 pli->NamedPipeState = 0; /* FIXME */
2179 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2180 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2183 SERVER_END_REQ;
2185 break;
2186 case FileNameInformation:
2188 FILE_NAME_INFORMATION *info = ptr;
2189 ANSI_STRING unix_name;
2191 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2193 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2194 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2195 RtlFreeAnsiString( &unix_name );
2196 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2199 break;
2200 default:
2201 FIXME("Unsupported class (%d)\n", class);
2202 io->u.Status = STATUS_NOT_IMPLEMENTED;
2203 break;
2205 if (needs_close) close( fd );
2206 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2207 return io->u.Status;
2210 /******************************************************************************
2211 * NtSetInformationFile [NTDLL.@]
2212 * ZwSetInformationFile [NTDLL.@]
2214 * Set information about an open file handle.
2216 * PARAMS
2217 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2218 * io [O] Receives information about the operation on return
2219 * ptr [I] Source for file information
2220 * len [I] Size of FileInformation
2221 * class [I] Type of file information to set
2223 * RETURNS
2224 * Success: 0. io is updated.
2225 * Failure: An NTSTATUS error code describing the error.
2227 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2228 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2230 int fd, needs_close;
2232 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2234 io->u.Status = STATUS_SUCCESS;
2235 switch (class)
2237 case FileBasicInformation:
2238 if (len >= sizeof(FILE_BASIC_INFORMATION))
2240 struct stat st;
2241 const FILE_BASIC_INFORMATION *info = ptr;
2243 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2244 return io->u.Status;
2246 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2247 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2249 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2251 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2252 else
2254 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2256 if (S_ISDIR( st.st_mode))
2257 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2258 else
2259 st.st_mode &= ~0222; /* clear write permission bits */
2261 else
2263 /* add write permission only where we already have read permission */
2264 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2266 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2270 if (needs_close) close( fd );
2272 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2273 break;
2275 case FilePositionInformation:
2276 if (len >= sizeof(FILE_POSITION_INFORMATION))
2278 const FILE_POSITION_INFORMATION *info = ptr;
2280 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2281 return io->u.Status;
2283 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2284 io->u.Status = FILE_GetNtStatus();
2286 if (needs_close) close( fd );
2288 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2289 break;
2291 case FileEndOfFileInformation:
2292 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2294 struct stat st;
2295 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2297 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2298 return io->u.Status;
2300 /* first try normal truncate */
2301 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2303 /* now check for the need to extend the file */
2304 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2306 static const char zero;
2308 /* extend the file one byte beyond the requested size and then truncate it */
2309 /* this should work around ftruncate implementations that can't extend files */
2310 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2311 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2313 io->u.Status = FILE_GetNtStatus();
2315 if (needs_close) close( fd );
2317 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2318 break;
2320 case FileMailslotSetInformation:
2322 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2324 SERVER_START_REQ( set_mailslot_info )
2326 req->handle = wine_server_obj_handle( handle );
2327 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2328 req->read_timeout = info->ReadTimeout.QuadPart;
2329 io->u.Status = wine_server_call( req );
2331 SERVER_END_REQ;
2333 break;
2335 case FileCompletionInformation:
2336 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2338 FILE_COMPLETION_INFORMATION *info = ptr;
2340 SERVER_START_REQ( set_completion_info )
2342 req->handle = wine_server_obj_handle( handle );
2343 req->chandle = wine_server_obj_handle( info->CompletionPort );
2344 req->ckey = info->CompletionKey;
2345 io->u.Status = wine_server_call( req );
2347 SERVER_END_REQ;
2348 } else
2349 io->u.Status = STATUS_INVALID_PARAMETER_3;
2350 break;
2352 case FileAllInformation:
2353 io->u.Status = STATUS_INVALID_INFO_CLASS;
2354 break;
2356 case FileValidDataLengthInformation:
2357 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2359 struct stat st;
2360 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2362 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2363 return io->u.Status;
2365 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2366 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2367 io->u.Status = STATUS_INVALID_PARAMETER;
2368 else
2370 #ifdef HAVE_FALLOCATE
2371 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2373 NTSTATUS status = FILE_GetNtStatus();
2374 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2375 else io->u.Status = status;
2377 #else
2378 FIXME( "setting valid data length not supported\n" );
2379 #endif
2381 if (needs_close) close( fd );
2383 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2384 break;
2386 default:
2387 FIXME("Unsupported class (%d)\n", class);
2388 io->u.Status = STATUS_NOT_IMPLEMENTED;
2389 break;
2391 io->Information = 0;
2392 return io->u.Status;
2396 /******************************************************************************
2397 * NtQueryFullAttributesFile (NTDLL.@)
2399 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2400 FILE_NETWORK_OPEN_INFORMATION *info )
2402 ANSI_STRING unix_name;
2403 NTSTATUS status;
2405 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2407 struct stat st;
2409 if (stat( unix_name.Buffer, &st ) == -1)
2410 status = FILE_GetNtStatus();
2411 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2412 status = STATUS_INVALID_INFO_CLASS;
2413 else
2415 FILE_BASIC_INFORMATION basic;
2416 FILE_STANDARD_INFORMATION std;
2418 fill_stat_info( &st, &basic, FileBasicInformation );
2419 fill_stat_info( &st, &std, FileStandardInformation );
2421 info->CreationTime = basic.CreationTime;
2422 info->LastAccessTime = basic.LastAccessTime;
2423 info->LastWriteTime = basic.LastWriteTime;
2424 info->ChangeTime = basic.ChangeTime;
2425 info->AllocationSize = std.AllocationSize;
2426 info->EndOfFile = std.EndOfFile;
2427 info->FileAttributes = basic.FileAttributes;
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 /******************************************************************************
2439 * NtQueryAttributesFile (NTDLL.@)
2440 * ZwQueryAttributesFile (NTDLL.@)
2442 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2444 ANSI_STRING unix_name;
2445 NTSTATUS status;
2447 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2449 struct stat st;
2451 if (stat( unix_name.Buffer, &st ) == -1)
2452 status = FILE_GetNtStatus();
2453 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2454 status = STATUS_INVALID_INFO_CLASS;
2455 else
2457 status = fill_stat_info( &st, info, FileBasicInformation );
2458 if (DIR_is_hidden_file( attr->ObjectName ))
2459 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2461 RtlFreeAnsiString( &unix_name );
2463 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2464 return status;
2468 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2469 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2470 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2471 unsigned int flags )
2473 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2475 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2476 /* Don't assume read-only, let the mount options set it below */
2477 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2479 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2480 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2482 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2483 info->Characteristics |= FILE_REMOTE_DEVICE;
2485 else if (!strcmp("procfs", fstypename))
2486 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2487 else
2488 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2490 if (flags & MNT_RDONLY)
2491 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2493 if (!(flags & MNT_LOCAL))
2495 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2496 info->Characteristics |= FILE_REMOTE_DEVICE;
2499 #endif
2501 static inline BOOL is_device_placeholder( int fd )
2503 static const char wine_placeholder[] = "Wine device placeholder";
2504 char buffer[sizeof(wine_placeholder)-1];
2506 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2507 return FALSE;
2508 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2511 /******************************************************************************
2512 * get_device_info
2514 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2516 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2518 struct stat st;
2520 info->Characteristics = 0;
2521 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2522 if (S_ISCHR( st.st_mode ))
2524 info->DeviceType = FILE_DEVICE_UNKNOWN;
2525 #ifdef linux
2526 switch(major(st.st_rdev))
2528 case MEM_MAJOR:
2529 info->DeviceType = FILE_DEVICE_NULL;
2530 break;
2531 case TTY_MAJOR:
2532 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2533 break;
2534 case LP_MAJOR:
2535 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2536 break;
2537 case SCSI_TAPE_MAJOR:
2538 info->DeviceType = FILE_DEVICE_TAPE;
2539 break;
2541 #endif
2543 else if (S_ISBLK( st.st_mode ))
2545 info->DeviceType = FILE_DEVICE_DISK;
2547 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2549 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2551 else if (is_device_placeholder( fd ))
2553 info->DeviceType = FILE_DEVICE_DISK;
2555 else /* regular file or directory */
2557 #if defined(linux) && defined(HAVE_FSTATFS)
2558 struct statfs stfs;
2560 /* check for floppy disk */
2561 if (major(st.st_dev) == FLOPPY_MAJOR)
2562 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2564 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2565 switch (stfs.f_type)
2567 case 0x9660: /* iso9660 */
2568 case 0x9fa1: /* supermount */
2569 case 0x15013346: /* udf */
2570 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2571 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2572 break;
2573 case 0x6969: /* nfs */
2574 case 0x517B: /* smbfs */
2575 case 0x564c: /* ncpfs */
2576 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2577 info->Characteristics |= FILE_REMOTE_DEVICE;
2578 break;
2579 case 0x01021994: /* tmpfs */
2580 case 0x28cd3d45: /* cramfs */
2581 case 0x1373: /* devfs */
2582 case 0x9fa0: /* procfs */
2583 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2584 break;
2585 default:
2586 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2587 break;
2589 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2590 struct statfs stfs;
2592 if (fstatfs( fd, &stfs ) < 0)
2593 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2594 else
2595 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2596 #elif defined(__NetBSD__)
2597 struct statvfs stfs;
2599 if (fstatvfs( fd, &stfs) < 0)
2600 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2601 else
2602 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2603 #elif defined(sun)
2604 /* Use dkio to work out device types */
2606 # include <sys/dkio.h>
2607 # include <sys/vtoc.h>
2608 struct dk_cinfo dkinf;
2609 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2610 if(retval==-1){
2611 WARN("Unable to get disk device type information - assuming a disk like device\n");
2612 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2614 switch (dkinf.dki_ctype)
2616 case DKC_CDROM:
2617 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2618 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2619 break;
2620 case DKC_NCRFLOPPY:
2621 case DKC_SMSFLOPPY:
2622 case DKC_INTEL82072:
2623 case DKC_INTEL82077:
2624 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2625 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2626 break;
2627 case DKC_MD:
2628 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2629 break;
2630 default:
2631 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2634 #else
2635 static int warned;
2636 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2637 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2638 #endif
2639 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2641 return STATUS_SUCCESS;
2645 /******************************************************************************
2646 * NtQueryVolumeInformationFile [NTDLL.@]
2647 * ZwQueryVolumeInformationFile [NTDLL.@]
2649 * Get volume information for an open file handle.
2651 * PARAMS
2652 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2653 * io [O] Receives information about the operation on return
2654 * buffer [O] Destination for volume information
2655 * length [I] Size of FsInformation
2656 * info_class [I] Type of volume information to set
2658 * RETURNS
2659 * Success: 0. io and buffer are updated.
2660 * Failure: An NTSTATUS error code describing the error.
2662 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2663 PVOID buffer, ULONG length,
2664 FS_INFORMATION_CLASS info_class )
2666 int fd, needs_close;
2667 struct stat st;
2668 static int once;
2670 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2671 return io->u.Status;
2673 io->u.Status = STATUS_NOT_IMPLEMENTED;
2674 io->Information = 0;
2676 switch( info_class )
2678 case FileFsVolumeInformation:
2679 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2680 break;
2681 case FileFsLabelInformation:
2682 FIXME( "%p: label info not supported\n", handle );
2683 break;
2684 case FileFsSizeInformation:
2685 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2686 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2687 else
2689 FILE_FS_SIZE_INFORMATION *info = buffer;
2691 if (fstat( fd, &st ) < 0)
2693 io->u.Status = FILE_GetNtStatus();
2694 break;
2696 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2698 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2700 else
2702 ULONGLONG bsize;
2703 /* Linux's fstatvfs is buggy */
2704 #if !defined(linux) || !defined(HAVE_FSTATFS)
2705 struct statvfs stfs;
2707 if (fstatvfs( fd, &stfs ) < 0)
2709 io->u.Status = FILE_GetNtStatus();
2710 break;
2712 bsize = stfs.f_frsize;
2713 #else
2714 struct statfs stfs;
2715 if (fstatfs( fd, &stfs ) < 0)
2717 io->u.Status = FILE_GetNtStatus();
2718 break;
2720 bsize = stfs.f_bsize;
2721 #endif
2722 if (bsize == 2048) /* assume CD-ROM */
2724 info->BytesPerSector = 2048;
2725 info->SectorsPerAllocationUnit = 1;
2727 else
2729 info->BytesPerSector = 512;
2730 info->SectorsPerAllocationUnit = 8;
2732 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2733 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2734 io->Information = sizeof(*info);
2735 io->u.Status = STATUS_SUCCESS;
2738 break;
2739 case FileFsDeviceInformation:
2740 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2741 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2742 else
2744 FILE_FS_DEVICE_INFORMATION *info = buffer;
2746 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2747 io->Information = sizeof(*info);
2749 break;
2750 case FileFsAttributeInformation:
2751 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
2752 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2753 else
2755 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
2757 FIXME( "%p: faking attribute info\n", handle );
2758 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
2759 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
2760 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
2761 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
2762 info->FileSystemNameLength = sizeof(ntfsW);
2763 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
2765 io->Information = sizeof(*info);
2766 io->u.Status = STATUS_SUCCESS;
2768 break;
2769 case FileFsControlInformation:
2770 FIXME( "%p: control info not supported\n", handle );
2771 break;
2772 case FileFsFullSizeInformation:
2773 FIXME( "%p: full size info not supported\n", handle );
2774 break;
2775 case FileFsObjectIdInformation:
2776 FIXME( "%p: object id info not supported\n", handle );
2777 break;
2778 case FileFsMaximumInformation:
2779 FIXME( "%p: maximum info not supported\n", handle );
2780 break;
2781 default:
2782 io->u.Status = STATUS_INVALID_PARAMETER;
2783 break;
2785 if (needs_close) close( fd );
2786 return io->u.Status;
2790 /******************************************************************
2791 * NtQueryEaFile (NTDLL.@)
2793 * Read extended attributes from NTFS files.
2795 * PARAMS
2796 * hFile [I] File handle, must be opened with FILE_READ_EA access
2797 * iosb [O] Receives information about the operation on return
2798 * buffer [O] Output buffer
2799 * length [I] Length of output buffer
2800 * single_entry [I] Only read and return one entry
2801 * ea_list [I] Optional list with names of EAs to return
2802 * ea_list_len [I] Length of ea_list in bytes
2803 * ea_index [I] Optional pointer to 1-based index of attribute to return
2804 * restart [I] restart EA scan
2806 * RETURNS
2807 * Success: 0. Atrributes read into buffer
2808 * Failure: An NTSTATUS error code describing the error.
2810 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
2811 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
2812 PULONG ea_index, BOOLEAN restart )
2814 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
2815 hFile, iosb, buffer, length, single_entry, ea_list,
2816 ea_list_len, ea_index, restart);
2817 return STATUS_ACCESS_DENIED;
2821 /******************************************************************
2822 * NtSetEaFile (NTDLL.@)
2824 * Update extended attributes for NTFS files.
2826 * PARAMS
2827 * hFile [I] File handle, must be opened with FILE_READ_EA access
2828 * iosb [O] Receives information about the operation on return
2829 * buffer [I] Buffer with EA information
2830 * length [I] Length of buffer
2832 * RETURNS
2833 * Success: 0. Attributes are updated
2834 * Failure: An NTSTATUS error code describing the error.
2836 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
2838 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
2839 return STATUS_ACCESS_DENIED;
2843 /******************************************************************
2844 * NtFlushBuffersFile (NTDLL.@)
2846 * Flush any buffered data on an open file handle.
2848 * PARAMS
2849 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2850 * IoStatusBlock [O] Receives information about the operation on return
2852 * RETURNS
2853 * Success: 0. IoStatusBlock is updated.
2854 * Failure: An NTSTATUS error code describing the error.
2856 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2858 NTSTATUS ret;
2859 HANDLE hEvent = NULL;
2860 enum server_fd_type type;
2861 int fd, needs_close;
2863 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
2865 if (!ret && type == FD_TYPE_SERIAL)
2867 ret = COMM_FlushBuffersFile( fd );
2869 else
2871 SERVER_START_REQ( flush_file )
2873 req->handle = wine_server_obj_handle( hFile );
2874 ret = wine_server_call( req );
2875 hEvent = wine_server_ptr_handle( reply->event );
2877 SERVER_END_REQ;
2878 if (!ret && hEvent)
2880 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2881 NtClose( hEvent );
2885 if (needs_close) close( fd );
2886 return ret;
2889 /******************************************************************
2890 * NtLockFile (NTDLL.@)
2894 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2895 PIO_APC_ROUTINE apc, void* apc_user,
2896 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2897 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2898 BOOLEAN exclusive )
2900 NTSTATUS ret;
2901 HANDLE handle;
2902 BOOLEAN async;
2903 static BOOLEAN warn = TRUE;
2905 if (apc || io_status || key)
2907 FIXME("Unimplemented yet parameter\n");
2908 return STATUS_NOT_IMPLEMENTED;
2911 if (apc_user && warn)
2913 FIXME("I/O completion on lock not implemented yet\n");
2914 warn = FALSE;
2917 for (;;)
2919 SERVER_START_REQ( lock_file )
2921 req->handle = wine_server_obj_handle( hFile );
2922 req->offset = offset->QuadPart;
2923 req->count = count->QuadPart;
2924 req->shared = !exclusive;
2925 req->wait = !dont_wait;
2926 ret = wine_server_call( req );
2927 handle = wine_server_ptr_handle( reply->handle );
2928 async = reply->overlapped;
2930 SERVER_END_REQ;
2931 if (ret != STATUS_PENDING)
2933 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2934 return ret;
2937 if (async)
2939 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2940 if (handle) NtClose( handle );
2941 return STATUS_PENDING;
2943 if (handle)
2945 NtWaitForSingleObject( handle, FALSE, NULL );
2946 NtClose( handle );
2948 else
2950 LARGE_INTEGER time;
2952 /* Unix lock conflict, sleep a bit and retry */
2953 time.QuadPart = 100 * (ULONGLONG)10000;
2954 time.QuadPart = -time.QuadPart;
2955 NtDelayExecution( FALSE, &time );
2961 /******************************************************************
2962 * NtUnlockFile (NTDLL.@)
2966 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2967 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2968 PULONG key )
2970 NTSTATUS status;
2972 TRACE( "%p %x%08x %x%08x\n",
2973 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2975 if (io_status || key)
2977 FIXME("Unimplemented yet parameter\n");
2978 return STATUS_NOT_IMPLEMENTED;
2981 SERVER_START_REQ( unlock_file )
2983 req->handle = wine_server_obj_handle( hFile );
2984 req->offset = offset->QuadPart;
2985 req->count = count->QuadPart;
2986 status = wine_server_call( req );
2988 SERVER_END_REQ;
2989 return status;
2992 /******************************************************************
2993 * NtCreateNamedPipeFile (NTDLL.@)
2997 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2998 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2999 ULONG sharing, ULONG dispo, ULONG options,
3000 ULONG pipe_type, ULONG read_mode,
3001 ULONG completion_mode, ULONG max_inst,
3002 ULONG inbound_quota, ULONG outbound_quota,
3003 PLARGE_INTEGER timeout)
3005 NTSTATUS status;
3007 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3008 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3009 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3010 outbound_quota, timeout);
3012 /* assume we only get relative timeout */
3013 if (timeout->QuadPart > 0)
3014 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3016 SERVER_START_REQ( create_named_pipe )
3018 req->access = access;
3019 req->attributes = attr->Attributes;
3020 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3021 req->options = options;
3022 req->sharing = sharing;
3023 req->flags =
3024 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3025 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3026 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3027 req->maxinstances = max_inst;
3028 req->outsize = outbound_quota;
3029 req->insize = inbound_quota;
3030 req->timeout = timeout->QuadPart;
3031 wine_server_add_data( req, attr->ObjectName->Buffer,
3032 attr->ObjectName->Length );
3033 status = wine_server_call( req );
3034 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3036 SERVER_END_REQ;
3037 return status;
3040 /******************************************************************
3041 * NtDeleteFile (NTDLL.@)
3045 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3047 NTSTATUS status;
3048 HANDLE hFile;
3049 IO_STATUS_BLOCK io;
3051 TRACE("%p\n", ObjectAttributes);
3052 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3053 ObjectAttributes, &io, NULL, 0,
3054 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3055 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3056 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3057 return status;
3060 /******************************************************************
3061 * NtCancelIoFileEx (NTDLL.@)
3065 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3067 LARGE_INTEGER timeout;
3069 TRACE("%p %p %p\n", hFile, iosb, io_status );
3071 SERVER_START_REQ( cancel_async )
3073 req->handle = wine_server_obj_handle( hFile );
3074 req->iosb = wine_server_client_ptr( iosb );
3075 req->only_thread = FALSE;
3076 io_status->u.Status = wine_server_call( req );
3078 SERVER_END_REQ;
3079 if (io_status->u.Status)
3080 return io_status->u.Status;
3082 /* Let some APC be run, so that we can run the remaining APCs on hFile
3083 * either the cancelation of the pending one, but also the execution
3084 * of the queued APC, but not yet run. This is needed to ensure proper
3085 * clean-up of allocated data.
3087 timeout.QuadPart = 0;
3088 NtDelayExecution( TRUE, &timeout );
3089 return io_status->u.Status;
3092 /******************************************************************
3093 * NtCancelIoFile (NTDLL.@)
3097 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3099 LARGE_INTEGER timeout;
3101 TRACE("%p %p\n", hFile, io_status );
3103 SERVER_START_REQ( cancel_async )
3105 req->handle = wine_server_obj_handle( hFile );
3106 req->iosb = 0;
3107 req->only_thread = TRUE;
3108 io_status->u.Status = wine_server_call( req );
3110 SERVER_END_REQ;
3111 if (io_status->u.Status)
3112 return io_status->u.Status;
3114 /* Let some APC be run, so that we can run the remaining APCs on hFile
3115 * either the cancelation of the pending one, but also the execution
3116 * of the queued APC, but not yet run. This is needed to ensure proper
3117 * clean-up of allocated data.
3119 timeout.QuadPart = 0;
3120 NtDelayExecution( TRUE, &timeout );
3121 return io_status->u.Status;
3124 /******************************************************************************
3125 * NtCreateMailslotFile [NTDLL.@]
3126 * ZwCreateMailslotFile [NTDLL.@]
3128 * PARAMS
3129 * pHandle [O] pointer to receive the handle created
3130 * DesiredAccess [I] access mode (read, write, etc)
3131 * ObjectAttributes [I] fully qualified NT path of the mailslot
3132 * IoStatusBlock [O] receives completion status and other info
3133 * CreateOptions [I]
3134 * MailslotQuota [I]
3135 * MaxMessageSize [I]
3136 * TimeOut [I]
3138 * RETURNS
3139 * An NT status code
3141 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3142 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3143 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3144 PLARGE_INTEGER TimeOut)
3146 LARGE_INTEGER timeout;
3147 NTSTATUS ret;
3149 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3150 pHandle, DesiredAccess, attr, IoStatusBlock,
3151 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3153 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3154 if (!attr) return STATUS_INVALID_PARAMETER;
3155 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3158 * For a NULL TimeOut pointer set the default timeout value
3160 if (!TimeOut)
3161 timeout.QuadPart = -1;
3162 else
3163 timeout.QuadPart = TimeOut->QuadPart;
3165 SERVER_START_REQ( create_mailslot )
3167 req->access = DesiredAccess;
3168 req->attributes = attr->Attributes;
3169 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3170 req->max_msgsize = MaxMessageSize;
3171 req->read_timeout = timeout.QuadPart;
3172 wine_server_add_data( req, attr->ObjectName->Buffer,
3173 attr->ObjectName->Length );
3174 ret = wine_server_call( req );
3175 if( ret == STATUS_SUCCESS )
3176 *pHandle = wine_server_ptr_handle( reply->handle );
3178 SERVER_END_REQ;
3180 return ret;