d2d1: Implement d2d_d3d_render_target_CreateBitmap().
[wine/multimedia.git] / dlls / ntdll / file.c
blob92d9829ecece7e326d45f06f923c589653002ef8
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 sizeof(FILE_PIPE_INFORMATION), /* 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 != FilePipeInformation && 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 FilePipeInformation:
2151 FILE_PIPE_INFORMATION* pi = 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 pi->ReadMode = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ) ?
2159 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
2160 pi->CompletionMode = (reply->flags & NAMED_PIPE_NONBLOCKING_MODE) ?
2161 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
2164 SERVER_END_REQ;
2166 break;
2167 case FilePipeLocalInformation:
2169 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
2171 SERVER_START_REQ( get_named_pipe_info )
2173 req->handle = wine_server_obj_handle( hFile );
2174 if (!(io->u.Status = wine_server_call( req )))
2176 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
2177 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
2178 switch (reply->sharing)
2180 case FILE_SHARE_READ:
2181 pli->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
2182 break;
2183 case FILE_SHARE_WRITE:
2184 pli->NamedPipeConfiguration = FILE_PIPE_INBOUND;
2185 break;
2186 case FILE_SHARE_READ | FILE_SHARE_WRITE:
2187 pli->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
2188 break;
2190 pli->MaximumInstances = reply->maxinstances;
2191 pli->CurrentInstances = reply->instances;
2192 pli->InboundQuota = reply->insize;
2193 pli->ReadDataAvailable = 0; /* FIXME */
2194 pli->OutboundQuota = reply->outsize;
2195 pli->WriteQuotaAvailable = 0; /* FIXME */
2196 pli->NamedPipeState = 0; /* FIXME */
2197 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
2198 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
2201 SERVER_END_REQ;
2203 break;
2204 case FileNameInformation:
2206 FILE_NAME_INFORMATION *info = ptr;
2207 ANSI_STRING unix_name;
2209 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
2211 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2212 io->u.Status = fill_name_info( &unix_name, info, &name_len );
2213 RtlFreeAnsiString( &unix_name );
2214 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
2217 break;
2218 default:
2219 FIXME("Unsupported class (%d)\n", class);
2220 io->u.Status = STATUS_NOT_IMPLEMENTED;
2221 break;
2223 if (needs_close) close( fd );
2224 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
2225 return io->u.Status;
2228 /******************************************************************************
2229 * NtSetInformationFile [NTDLL.@]
2230 * ZwSetInformationFile [NTDLL.@]
2232 * Set information about an open file handle.
2234 * PARAMS
2235 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2236 * io [O] Receives information about the operation on return
2237 * ptr [I] Source for file information
2238 * len [I] Size of FileInformation
2239 * class [I] Type of file information to set
2241 * RETURNS
2242 * Success: 0. io is updated.
2243 * Failure: An NTSTATUS error code describing the error.
2245 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
2246 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
2248 int fd, needs_close;
2250 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
2252 io->u.Status = STATUS_SUCCESS;
2253 switch (class)
2255 case FileBasicInformation:
2256 if (len >= sizeof(FILE_BASIC_INFORMATION))
2258 struct stat st;
2259 const FILE_BASIC_INFORMATION *info = ptr;
2261 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2262 return io->u.Status;
2264 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
2265 io->u.Status = set_file_times( fd, &info->LastWriteTime, &info->LastAccessTime );
2267 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
2269 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2270 else
2272 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2274 if (S_ISDIR( st.st_mode))
2275 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2276 else
2277 st.st_mode &= ~0222; /* clear write permission bits */
2279 else
2281 /* add write permission only where we already have read permission */
2282 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2284 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2288 if (needs_close) close( fd );
2290 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2291 break;
2293 case FilePositionInformation:
2294 if (len >= sizeof(FILE_POSITION_INFORMATION))
2296 const FILE_POSITION_INFORMATION *info = ptr;
2298 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2299 return io->u.Status;
2301 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2302 io->u.Status = FILE_GetNtStatus();
2304 if (needs_close) close( fd );
2306 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2307 break;
2309 case FileEndOfFileInformation:
2310 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2312 struct stat st;
2313 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2315 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2316 return io->u.Status;
2318 /* first try normal truncate */
2319 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2321 /* now check for the need to extend the file */
2322 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2324 static const char zero;
2326 /* extend the file one byte beyond the requested size and then truncate it */
2327 /* this should work around ftruncate implementations that can't extend files */
2328 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2329 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2331 io->u.Status = FILE_GetNtStatus();
2333 if (needs_close) close( fd );
2335 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2336 break;
2338 case FilePipeInformation:
2339 if (len >= sizeof(FILE_PIPE_INFORMATION))
2341 FILE_PIPE_INFORMATION *info = ptr;
2343 if ((info->CompletionMode | info->ReadMode) & ~1)
2345 io->u.Status = STATUS_INVALID_PARAMETER;
2346 break;
2349 SERVER_START_REQ( set_named_pipe_info )
2351 req->handle = wine_server_obj_handle( handle );
2352 req->flags = (info->CompletionMode ? NAMED_PIPE_NONBLOCKING_MODE : 0) |
2353 (info->ReadMode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0);
2354 io->u.Status = wine_server_call( req );
2356 SERVER_END_REQ;
2358 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2359 break;
2361 case FileMailslotSetInformation:
2363 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2365 SERVER_START_REQ( set_mailslot_info )
2367 req->handle = wine_server_obj_handle( handle );
2368 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2369 req->read_timeout = info->ReadTimeout.QuadPart;
2370 io->u.Status = wine_server_call( req );
2372 SERVER_END_REQ;
2374 break;
2376 case FileCompletionInformation:
2377 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2379 FILE_COMPLETION_INFORMATION *info = ptr;
2381 SERVER_START_REQ( set_completion_info )
2383 req->handle = wine_server_obj_handle( handle );
2384 req->chandle = wine_server_obj_handle( info->CompletionPort );
2385 req->ckey = info->CompletionKey;
2386 io->u.Status = wine_server_call( req );
2388 SERVER_END_REQ;
2389 } else
2390 io->u.Status = STATUS_INVALID_PARAMETER_3;
2391 break;
2393 case FileAllInformation:
2394 io->u.Status = STATUS_INVALID_INFO_CLASS;
2395 break;
2397 case FileValidDataLengthInformation:
2398 if (len >= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION))
2400 struct stat st;
2401 const FILE_VALID_DATA_LENGTH_INFORMATION *info = ptr;
2403 if ((io->u.Status = server_get_unix_fd( handle, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL )))
2404 return io->u.Status;
2406 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
2407 else if (info->ValidDataLength.QuadPart <= 0 || (off_t)info->ValidDataLength.QuadPart > st.st_size)
2408 io->u.Status = STATUS_INVALID_PARAMETER;
2409 else
2411 #ifdef HAVE_FALLOCATE
2412 if (fallocate( fd, 0, 0, (off_t)info->ValidDataLength.QuadPart ) == -1)
2414 NTSTATUS status = FILE_GetNtStatus();
2415 if (status == STATUS_NOT_SUPPORTED) WARN( "fallocate not supported on this filesystem\n" );
2416 else io->u.Status = status;
2418 #else
2419 FIXME( "setting valid data length not supported\n" );
2420 #endif
2422 if (needs_close) close( fd );
2424 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2425 break;
2427 default:
2428 FIXME("Unsupported class (%d)\n", class);
2429 io->u.Status = STATUS_NOT_IMPLEMENTED;
2430 break;
2432 io->Information = 0;
2433 return io->u.Status;
2437 /******************************************************************************
2438 * NtQueryFullAttributesFile (NTDLL.@)
2440 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2441 FILE_NETWORK_OPEN_INFORMATION *info )
2443 ANSI_STRING unix_name;
2444 NTSTATUS status;
2446 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2448 struct stat st;
2450 if (stat( unix_name.Buffer, &st ) == -1)
2451 status = FILE_GetNtStatus();
2452 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2453 status = STATUS_INVALID_INFO_CLASS;
2454 else
2456 FILE_BASIC_INFORMATION basic;
2457 FILE_STANDARD_INFORMATION std;
2459 fill_stat_info( &st, &basic, FileBasicInformation );
2460 fill_stat_info( &st, &std, FileStandardInformation );
2462 info->CreationTime = basic.CreationTime;
2463 info->LastAccessTime = basic.LastAccessTime;
2464 info->LastWriteTime = basic.LastWriteTime;
2465 info->ChangeTime = basic.ChangeTime;
2466 info->AllocationSize = std.AllocationSize;
2467 info->EndOfFile = std.EndOfFile;
2468 info->FileAttributes = basic.FileAttributes;
2469 if (DIR_is_hidden_file( attr->ObjectName ))
2470 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2472 RtlFreeAnsiString( &unix_name );
2474 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2475 return status;
2479 /******************************************************************************
2480 * NtQueryAttributesFile (NTDLL.@)
2481 * ZwQueryAttributesFile (NTDLL.@)
2483 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2485 ANSI_STRING unix_name;
2486 NTSTATUS status;
2488 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2490 struct stat st;
2492 if (stat( unix_name.Buffer, &st ) == -1)
2493 status = FILE_GetNtStatus();
2494 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2495 status = STATUS_INVALID_INFO_CLASS;
2496 else
2498 status = fill_stat_info( &st, info, FileBasicInformation );
2499 if (DIR_is_hidden_file( attr->ObjectName ))
2500 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2502 RtlFreeAnsiString( &unix_name );
2504 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2505 return status;
2509 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2510 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2511 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2512 unsigned int flags )
2514 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2516 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2517 /* Don't assume read-only, let the mount options set it below */
2518 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2520 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2521 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2523 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2524 info->Characteristics |= FILE_REMOTE_DEVICE;
2526 else if (!strcmp("procfs", fstypename))
2527 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2528 else
2529 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2531 if (flags & MNT_RDONLY)
2532 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2534 if (!(flags & MNT_LOCAL))
2536 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2537 info->Characteristics |= FILE_REMOTE_DEVICE;
2540 #endif
2542 static inline BOOL is_device_placeholder( int fd )
2544 static const char wine_placeholder[] = "Wine device placeholder";
2545 char buffer[sizeof(wine_placeholder)-1];
2547 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2548 return FALSE;
2549 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2552 /******************************************************************************
2553 * get_device_info
2555 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2557 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2559 struct stat st;
2561 info->Characteristics = 0;
2562 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2563 if (S_ISCHR( st.st_mode ))
2565 info->DeviceType = FILE_DEVICE_UNKNOWN;
2566 #ifdef linux
2567 switch(major(st.st_rdev))
2569 case MEM_MAJOR:
2570 info->DeviceType = FILE_DEVICE_NULL;
2571 break;
2572 case TTY_MAJOR:
2573 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2574 break;
2575 case LP_MAJOR:
2576 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2577 break;
2578 case SCSI_TAPE_MAJOR:
2579 info->DeviceType = FILE_DEVICE_TAPE;
2580 break;
2582 #endif
2584 else if (S_ISBLK( st.st_mode ))
2586 info->DeviceType = FILE_DEVICE_DISK;
2588 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2590 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2592 else if (is_device_placeholder( fd ))
2594 info->DeviceType = FILE_DEVICE_DISK;
2596 else /* regular file or directory */
2598 #if defined(linux) && defined(HAVE_FSTATFS)
2599 struct statfs stfs;
2601 /* check for floppy disk */
2602 if (major(st.st_dev) == FLOPPY_MAJOR)
2603 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2605 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2606 switch (stfs.f_type)
2608 case 0x9660: /* iso9660 */
2609 case 0x9fa1: /* supermount */
2610 case 0x15013346: /* udf */
2611 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2612 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2613 break;
2614 case 0x6969: /* nfs */
2615 case 0x517B: /* smbfs */
2616 case 0x564c: /* ncpfs */
2617 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2618 info->Characteristics |= FILE_REMOTE_DEVICE;
2619 break;
2620 case 0x01021994: /* tmpfs */
2621 case 0x28cd3d45: /* cramfs */
2622 case 0x1373: /* devfs */
2623 case 0x9fa0: /* procfs */
2624 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2625 break;
2626 default:
2627 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2628 break;
2630 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2631 struct statfs stfs;
2633 if (fstatfs( fd, &stfs ) < 0)
2634 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2635 else
2636 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2637 #elif defined(__NetBSD__)
2638 struct statvfs stfs;
2640 if (fstatvfs( fd, &stfs) < 0)
2641 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2642 else
2643 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2644 #elif defined(sun)
2645 /* Use dkio to work out device types */
2647 # include <sys/dkio.h>
2648 # include <sys/vtoc.h>
2649 struct dk_cinfo dkinf;
2650 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2651 if(retval==-1){
2652 WARN("Unable to get disk device type information - assuming a disk like device\n");
2653 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2655 switch (dkinf.dki_ctype)
2657 case DKC_CDROM:
2658 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2659 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2660 break;
2661 case DKC_NCRFLOPPY:
2662 case DKC_SMSFLOPPY:
2663 case DKC_INTEL82072:
2664 case DKC_INTEL82077:
2665 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2666 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2667 break;
2668 case DKC_MD:
2669 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2670 break;
2671 default:
2672 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2675 #else
2676 static int warned;
2677 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2678 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2679 #endif
2680 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2682 return STATUS_SUCCESS;
2686 /******************************************************************************
2687 * NtQueryVolumeInformationFile [NTDLL.@]
2688 * ZwQueryVolumeInformationFile [NTDLL.@]
2690 * Get volume information for an open file handle.
2692 * PARAMS
2693 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2694 * io [O] Receives information about the operation on return
2695 * buffer [O] Destination for volume information
2696 * length [I] Size of FsInformation
2697 * info_class [I] Type of volume information to set
2699 * RETURNS
2700 * Success: 0. io and buffer are updated.
2701 * Failure: An NTSTATUS error code describing the error.
2703 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2704 PVOID buffer, ULONG length,
2705 FS_INFORMATION_CLASS info_class )
2707 int fd, needs_close;
2708 struct stat st;
2709 static int once;
2711 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2712 return io->u.Status;
2714 io->u.Status = STATUS_NOT_IMPLEMENTED;
2715 io->Information = 0;
2717 switch( info_class )
2719 case FileFsVolumeInformation:
2720 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2721 break;
2722 case FileFsLabelInformation:
2723 FIXME( "%p: label info not supported\n", handle );
2724 break;
2725 case FileFsSizeInformation:
2726 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2727 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2728 else
2730 FILE_FS_SIZE_INFORMATION *info = buffer;
2732 if (fstat( fd, &st ) < 0)
2734 io->u.Status = FILE_GetNtStatus();
2735 break;
2737 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2739 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2741 else
2743 ULONGLONG bsize;
2744 /* Linux's fstatvfs is buggy */
2745 #if !defined(linux) || !defined(HAVE_FSTATFS)
2746 struct statvfs stfs;
2748 if (fstatvfs( fd, &stfs ) < 0)
2750 io->u.Status = FILE_GetNtStatus();
2751 break;
2753 bsize = stfs.f_frsize;
2754 #else
2755 struct statfs stfs;
2756 if (fstatfs( fd, &stfs ) < 0)
2758 io->u.Status = FILE_GetNtStatus();
2759 break;
2761 bsize = stfs.f_bsize;
2762 #endif
2763 if (bsize == 2048) /* assume CD-ROM */
2765 info->BytesPerSector = 2048;
2766 info->SectorsPerAllocationUnit = 1;
2768 else
2770 info->BytesPerSector = 512;
2771 info->SectorsPerAllocationUnit = 8;
2773 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2774 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2775 io->Information = sizeof(*info);
2776 io->u.Status = STATUS_SUCCESS;
2779 break;
2780 case FileFsDeviceInformation:
2781 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2782 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2783 else
2785 FILE_FS_DEVICE_INFORMATION *info = buffer;
2787 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2788 io->Information = sizeof(*info);
2790 break;
2791 case FileFsAttributeInformation:
2792 if (length < offsetof( FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName[sizeof(ntfsW)/sizeof(WCHAR)] ))
2793 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2794 else
2796 FILE_FS_ATTRIBUTE_INFORMATION *info = buffer;
2798 FIXME( "%p: faking attribute info\n", handle );
2799 info->FileSystemAttribute = FILE_SUPPORTS_ENCRYPTION | FILE_FILE_COMPRESSION |
2800 FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
2801 FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH;
2802 info->MaximumComponentNameLength = MAXIMUM_FILENAME_LENGTH - 1;
2803 info->FileSystemNameLength = sizeof(ntfsW);
2804 memcpy(info->FileSystemName, ntfsW, sizeof(ntfsW));
2806 io->Information = sizeof(*info);
2807 io->u.Status = STATUS_SUCCESS;
2809 break;
2810 case FileFsControlInformation:
2811 FIXME( "%p: control info not supported\n", handle );
2812 break;
2813 case FileFsFullSizeInformation:
2814 FIXME( "%p: full size info not supported\n", handle );
2815 break;
2816 case FileFsObjectIdInformation:
2817 FIXME( "%p: object id info not supported\n", handle );
2818 break;
2819 case FileFsMaximumInformation:
2820 FIXME( "%p: maximum info not supported\n", handle );
2821 break;
2822 default:
2823 io->u.Status = STATUS_INVALID_PARAMETER;
2824 break;
2826 if (needs_close) close( fd );
2827 return io->u.Status;
2831 /******************************************************************
2832 * NtQueryEaFile (NTDLL.@)
2834 * Read extended attributes from NTFS files.
2836 * PARAMS
2837 * hFile [I] File handle, must be opened with FILE_READ_EA access
2838 * iosb [O] Receives information about the operation on return
2839 * buffer [O] Output buffer
2840 * length [I] Length of output buffer
2841 * single_entry [I] Only read and return one entry
2842 * ea_list [I] Optional list with names of EAs to return
2843 * ea_list_len [I] Length of ea_list in bytes
2844 * ea_index [I] Optional pointer to 1-based index of attribute to return
2845 * restart [I] restart EA scan
2847 * RETURNS
2848 * Success: 0. Atrributes read into buffer
2849 * Failure: An NTSTATUS error code describing the error.
2851 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
2852 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
2853 PULONG ea_index, BOOLEAN restart )
2855 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
2856 hFile, iosb, buffer, length, single_entry, ea_list,
2857 ea_list_len, ea_index, restart);
2858 return STATUS_ACCESS_DENIED;
2862 /******************************************************************
2863 * NtSetEaFile (NTDLL.@)
2865 * Update extended attributes for NTFS files.
2867 * PARAMS
2868 * hFile [I] File handle, must be opened with FILE_READ_EA access
2869 * iosb [O] Receives information about the operation on return
2870 * buffer [I] Buffer with EA information
2871 * length [I] Length of buffer
2873 * RETURNS
2874 * Success: 0. Attributes are updated
2875 * Failure: An NTSTATUS error code describing the error.
2877 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
2879 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
2880 return STATUS_ACCESS_DENIED;
2884 /******************************************************************
2885 * NtFlushBuffersFile (NTDLL.@)
2887 * Flush any buffered data on an open file handle.
2889 * PARAMS
2890 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2891 * IoStatusBlock [O] Receives information about the operation on return
2893 * RETURNS
2894 * Success: 0. IoStatusBlock is updated.
2895 * Failure: An NTSTATUS error code describing the error.
2897 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2899 NTSTATUS ret;
2900 HANDLE hEvent = NULL;
2901 enum server_fd_type type;
2902 int fd, needs_close;
2904 ret = server_get_unix_fd( hFile, FILE_WRITE_DATA, &fd, &needs_close, &type, NULL );
2906 if (!ret && type == FD_TYPE_SERIAL)
2908 ret = COMM_FlushBuffersFile( fd );
2910 else
2912 SERVER_START_REQ( flush_file )
2914 req->handle = wine_server_obj_handle( hFile );
2915 ret = wine_server_call( req );
2916 hEvent = wine_server_ptr_handle( reply->event );
2918 SERVER_END_REQ;
2919 if (!ret && hEvent)
2921 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2922 NtClose( hEvent );
2926 if (needs_close) close( fd );
2927 return ret;
2930 /******************************************************************
2931 * NtLockFile (NTDLL.@)
2935 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2936 PIO_APC_ROUTINE apc, void* apc_user,
2937 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2938 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2939 BOOLEAN exclusive )
2941 NTSTATUS ret;
2942 HANDLE handle;
2943 BOOLEAN async;
2944 static BOOLEAN warn = TRUE;
2946 if (apc || io_status || key)
2948 FIXME("Unimplemented yet parameter\n");
2949 return STATUS_NOT_IMPLEMENTED;
2952 if (apc_user && warn)
2954 FIXME("I/O completion on lock not implemented yet\n");
2955 warn = FALSE;
2958 for (;;)
2960 SERVER_START_REQ( lock_file )
2962 req->handle = wine_server_obj_handle( hFile );
2963 req->offset = offset->QuadPart;
2964 req->count = count->QuadPart;
2965 req->shared = !exclusive;
2966 req->wait = !dont_wait;
2967 ret = wine_server_call( req );
2968 handle = wine_server_ptr_handle( reply->handle );
2969 async = reply->overlapped;
2971 SERVER_END_REQ;
2972 if (ret != STATUS_PENDING)
2974 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2975 return ret;
2978 if (async)
2980 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2981 if (handle) NtClose( handle );
2982 return STATUS_PENDING;
2984 if (handle)
2986 NtWaitForSingleObject( handle, FALSE, NULL );
2987 NtClose( handle );
2989 else
2991 LARGE_INTEGER time;
2993 /* Unix lock conflict, sleep a bit and retry */
2994 time.QuadPart = 100 * (ULONGLONG)10000;
2995 time.QuadPart = -time.QuadPart;
2996 NtDelayExecution( FALSE, &time );
3002 /******************************************************************
3003 * NtUnlockFile (NTDLL.@)
3007 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
3008 PLARGE_INTEGER offset, PLARGE_INTEGER count,
3009 PULONG key )
3011 NTSTATUS status;
3013 TRACE( "%p %x%08x %x%08x\n",
3014 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
3016 if (io_status || key)
3018 FIXME("Unimplemented yet parameter\n");
3019 return STATUS_NOT_IMPLEMENTED;
3022 SERVER_START_REQ( unlock_file )
3024 req->handle = wine_server_obj_handle( hFile );
3025 req->offset = offset->QuadPart;
3026 req->count = count->QuadPart;
3027 status = wine_server_call( req );
3029 SERVER_END_REQ;
3030 return status;
3033 /******************************************************************
3034 * NtCreateNamedPipeFile (NTDLL.@)
3038 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
3039 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
3040 ULONG sharing, ULONG dispo, ULONG options,
3041 ULONG pipe_type, ULONG read_mode,
3042 ULONG completion_mode, ULONG max_inst,
3043 ULONG inbound_quota, ULONG outbound_quota,
3044 PLARGE_INTEGER timeout)
3046 NTSTATUS status;
3048 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3049 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
3050 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
3051 outbound_quota, timeout);
3053 /* assume we only get relative timeout */
3054 if (timeout->QuadPart > 0)
3055 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
3057 SERVER_START_REQ( create_named_pipe )
3059 req->access = access;
3060 req->attributes = attr->Attributes;
3061 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3062 req->options = options;
3063 req->sharing = sharing;
3064 req->flags =
3065 (pipe_type ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0) |
3066 (read_mode ? NAMED_PIPE_MESSAGE_STREAM_READ : 0) |
3067 (completion_mode ? NAMED_PIPE_NONBLOCKING_MODE : 0);
3068 req->maxinstances = max_inst;
3069 req->outsize = outbound_quota;
3070 req->insize = inbound_quota;
3071 req->timeout = timeout->QuadPart;
3072 wine_server_add_data( req, attr->ObjectName->Buffer,
3073 attr->ObjectName->Length );
3074 status = wine_server_call( req );
3075 if (!status) *handle = wine_server_ptr_handle( reply->handle );
3077 SERVER_END_REQ;
3078 return status;
3081 /******************************************************************
3082 * NtDeleteFile (NTDLL.@)
3086 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
3088 NTSTATUS status;
3089 HANDLE hFile;
3090 IO_STATUS_BLOCK io;
3092 TRACE("%p\n", ObjectAttributes);
3093 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
3094 ObjectAttributes, &io, NULL, 0,
3095 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3096 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
3097 if (status == STATUS_SUCCESS) status = NtClose(hFile);
3098 return status;
3101 /******************************************************************
3102 * NtCancelIoFileEx (NTDLL.@)
3106 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
3108 LARGE_INTEGER timeout;
3110 TRACE("%p %p %p\n", hFile, iosb, io_status );
3112 SERVER_START_REQ( cancel_async )
3114 req->handle = wine_server_obj_handle( hFile );
3115 req->iosb = wine_server_client_ptr( iosb );
3116 req->only_thread = FALSE;
3117 io_status->u.Status = wine_server_call( req );
3119 SERVER_END_REQ;
3120 if (io_status->u.Status)
3121 return io_status->u.Status;
3123 /* Let some APC be run, so that we can run the remaining APCs on hFile
3124 * either the cancelation of the pending one, but also the execution
3125 * of the queued APC, but not yet run. This is needed to ensure proper
3126 * clean-up of allocated data.
3128 timeout.QuadPart = 0;
3129 NtDelayExecution( TRUE, &timeout );
3130 return io_status->u.Status;
3133 /******************************************************************
3134 * NtCancelIoFile (NTDLL.@)
3138 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
3140 LARGE_INTEGER timeout;
3142 TRACE("%p %p\n", hFile, io_status );
3144 SERVER_START_REQ( cancel_async )
3146 req->handle = wine_server_obj_handle( hFile );
3147 req->iosb = 0;
3148 req->only_thread = TRUE;
3149 io_status->u.Status = wine_server_call( req );
3151 SERVER_END_REQ;
3152 if (io_status->u.Status)
3153 return io_status->u.Status;
3155 /* Let some APC be run, so that we can run the remaining APCs on hFile
3156 * either the cancelation of the pending one, but also the execution
3157 * of the queued APC, but not yet run. This is needed to ensure proper
3158 * clean-up of allocated data.
3160 timeout.QuadPart = 0;
3161 NtDelayExecution( TRUE, &timeout );
3162 return io_status->u.Status;
3165 /******************************************************************************
3166 * NtCreateMailslotFile [NTDLL.@]
3167 * ZwCreateMailslotFile [NTDLL.@]
3169 * PARAMS
3170 * pHandle [O] pointer to receive the handle created
3171 * DesiredAccess [I] access mode (read, write, etc)
3172 * ObjectAttributes [I] fully qualified NT path of the mailslot
3173 * IoStatusBlock [O] receives completion status and other info
3174 * CreateOptions [I]
3175 * MailslotQuota [I]
3176 * MaxMessageSize [I]
3177 * TimeOut [I]
3179 * RETURNS
3180 * An NT status code
3182 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
3183 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
3184 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
3185 PLARGE_INTEGER TimeOut)
3187 LARGE_INTEGER timeout;
3188 NTSTATUS ret;
3190 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3191 pHandle, DesiredAccess, attr, IoStatusBlock,
3192 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
3194 if (!pHandle) return STATUS_ACCESS_VIOLATION;
3195 if (!attr) return STATUS_INVALID_PARAMETER;
3196 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3199 * For a NULL TimeOut pointer set the default timeout value
3201 if (!TimeOut)
3202 timeout.QuadPart = -1;
3203 else
3204 timeout.QuadPart = TimeOut->QuadPart;
3206 SERVER_START_REQ( create_mailslot )
3208 req->access = DesiredAccess;
3209 req->attributes = attr->Attributes;
3210 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3211 req->max_msgsize = MaxMessageSize;
3212 req->read_timeout = timeout.QuadPart;
3213 wine_server_add_data( req, attr->ObjectName->Buffer,
3214 attr->ObjectName->Length );
3215 ret = wine_server_call( req );
3216 if( ret == STATUS_SUCCESS )
3217 *pHandle = wine_server_ptr_handle( reply->handle );
3219 SERVER_END_REQ;
3221 return ret;