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
20 #include "wine/port.h"
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
33 #ifdef HAVE_LINUX_MAJOR_H
34 # include <linux/major.h>
36 #ifdef HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
54 #ifdef HAVE_SYS_POLL_H
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
66 #ifdef HAVE_SYS_MOUNT_H
67 # include <sys/mount.h>
69 #ifdef HAVE_SYS_STATFS_H
70 # include <sys/statfs.h>
73 #define NONAMELESSUNION
74 #define NONAMELESSSTRUCT
76 #define WIN32_NO_STATUS
77 #include "wine/unicode.h"
78 #include "wine/debug.h"
79 #include "wine/server.h"
80 #include "ntdll_misc.h"
84 #include "ddk/ntddser.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
88 mode_t FILE_umask = 0;
90 #define SECSPERDAY 86400
91 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
94 /**************************************************************************
95 * FILE_CreateFile (internal)
98 * Parameter set fully identical with NtCreateFile
100 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
101 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
102 ULONG attributes, ULONG sharing, ULONG disposition,
103 ULONG options, PVOID ea_buffer, ULONG ea_length )
105 ANSI_STRING unix_name;
108 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p\n"
109 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
110 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
111 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
112 attributes, sharing, disposition, options, ea_buffer, ea_length );
114 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
116 if (alloc_size) FIXME( "alloc_size not supported\n" );
118 if (attr->RootDirectory ||
119 (io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
120 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )) == STATUS_BAD_DEVICE_TYPE)
122 SERVER_START_REQ( open_file_object )
124 req->access = access;
125 req->attributes = attr->Attributes;
126 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
127 req->sharing = sharing;
128 req->options = options;
129 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
130 io->u.Status = wine_server_call( req );
131 *handle = wine_server_ptr_handle( reply->handle );
134 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
138 if (io->u.Status == STATUS_NO_SUCH_FILE &&
139 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
142 io->u.Status = STATUS_SUCCESS;
145 if (io->u.Status == STATUS_SUCCESS)
147 struct security_descriptor *sd = NULL;
148 struct object_attributes objattr;
152 objattr.name_len = 0;
155 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
156 if (io->u.Status != STATUS_SUCCESS)
158 RtlFreeAnsiString( &unix_name );
163 SERVER_START_REQ( create_file )
165 req->access = access;
166 req->attributes = attr->Attributes;
167 req->sharing = sharing;
168 req->create = disposition;
169 req->options = options;
170 req->attrs = attributes;
171 wine_server_add_data( req, &objattr, sizeof(objattr) );
172 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
173 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
174 io->u.Status = wine_server_call( req );
175 *handle = wine_server_ptr_handle( reply->handle );
178 NTDLL_free_struct_sd( sd );
179 RtlFreeAnsiString( &unix_name );
181 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
183 if (io->u.Status == STATUS_SUCCESS)
185 if (created) io->Information = FILE_CREATED;
186 else switch(disposition)
189 io->Information = FILE_SUPERSEDED;
192 io->Information = FILE_CREATED;
196 io->Information = FILE_OPENED;
199 case FILE_OVERWRITE_IF:
200 io->Information = FILE_OVERWRITTEN;
208 /**************************************************************************
209 * NtOpenFile [NTDLL.@]
210 * ZwOpenFile [NTDLL.@]
215 * handle [O] Variable that receives the file handle on return
216 * access [I] Access desired by the caller to the file
217 * attr [I] Structure describing the file to be opened
218 * io [O] Receives details about the result of the operation
219 * sharing [I] Type of shared access the caller requires
220 * options [I] Options for the file open
223 * Success: 0. FileHandle and IoStatusBlock are updated.
224 * Failure: An NTSTATUS error code describing the error.
226 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
227 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
228 ULONG sharing, ULONG options )
230 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
231 sharing, FILE_OPEN, options, NULL, 0 );
234 /**************************************************************************
235 * NtCreateFile [NTDLL.@]
236 * ZwCreateFile [NTDLL.@]
238 * Either create a new file or directory, or open an existing file, device,
239 * directory or volume.
242 * handle [O] Points to a variable which receives the file handle on return
243 * access [I] Desired access to the file
244 * attr [I] Structure describing the file
245 * io [O] Receives information about the operation on return
246 * alloc_size [I] Initial size of the file in bytes
247 * attributes [I] Attributes to create the file with
248 * sharing [I] Type of shared access the caller would like to the file
249 * disposition [I] Specifies what to do, depending on whether the file already exists
250 * options [I] Options for creating a new file
251 * ea_buffer [I] Pointer to an extended attributes buffer
252 * ea_length [I] Length of ea_buffer
255 * Success: 0. handle and io are updated.
256 * Failure: An NTSTATUS error code describing the error.
258 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
259 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
260 ULONG attributes, ULONG sharing, ULONG disposition,
261 ULONG options, PVOID ea_buffer, ULONG ea_length )
263 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
264 sharing, disposition, options, ea_buffer, ea_length );
267 /***********************************************************************
268 * Asynchronous file I/O *
280 struct async_fileio io;
282 unsigned int already;
289 struct async_fileio io;
291 unsigned int already;
293 } async_fileio_write;
296 /* callback for file I/O user APC */
297 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
299 struct async_fileio *async = arg;
300 if (async->apc) async->apc( async->apc_arg, io, reserved );
301 RtlFreeHeap( GetProcessHeap(), 0, async );
304 /***********************************************************************
305 * FILE_GetNtStatus(void)
307 * Retrieve the Nt Status code from errno.
308 * Try to be consistent with FILE_SetDosError().
310 NTSTATUS FILE_GetNtStatus(void)
314 TRACE( "errno = %d\n", errno );
317 case EAGAIN: return STATUS_SHARING_VIOLATION;
318 case EBADF: return STATUS_INVALID_HANDLE;
319 case EBUSY: return STATUS_DEVICE_BUSY;
320 case ENOSPC: return STATUS_DISK_FULL;
323 case EACCES: return STATUS_ACCESS_DENIED;
324 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
325 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
326 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
328 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
329 case EINVAL: return STATUS_INVALID_PARAMETER;
330 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
331 case EPIPE: return STATUS_PIPE_DISCONNECTED;
332 case EIO: return STATUS_DEVICE_NOT_READY;
334 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
336 case ENXIO: return STATUS_NO_SUCH_DEVICE;
338 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
339 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
340 case EFAULT: return STATUS_ACCESS_VIOLATION;
341 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
342 case ENOEXEC: /* ?? */
343 case EEXIST: /* ?? */
345 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
346 return STATUS_UNSUCCESSFUL;
350 /***********************************************************************
351 * FILE_AsyncReadService (INTERNAL)
353 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc)
355 async_fileio_read *fileio = user;
356 int fd, needs_close, result;
360 case STATUS_ALERTED: /* got some new data */
361 /* check to see if the data is ready (non-blocking) */
362 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
363 &needs_close, NULL, NULL )))
366 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
367 if (needs_close) close( fd );
371 if (errno == EAGAIN || errno == EINTR)
372 status = STATUS_PENDING;
373 else /* check to see if the transfer is complete */
374 status = FILE_GetNtStatus();
376 else if (result == 0)
378 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
382 fileio->already += result;
383 if (fileio->already >= fileio->count || fileio->avail_mode)
384 status = STATUS_SUCCESS;
387 /* if we only have to read the available data, and none is available,
388 * simply cancel the request. If data was available, it has been read
389 * while in by previous call (NtDelayExecution)
391 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
397 case STATUS_IO_TIMEOUT:
398 if (fileio->already) status = STATUS_SUCCESS;
401 if (status != STATUS_PENDING)
403 iosb->u.Status = status;
404 iosb->Information = fileio->already;
412 int interval; /* max interval between two bytes */
413 int total; /* total timeout for the whole operation */
414 int end_time; /* absolute time of end of operation */
417 /* retrieve the I/O timeouts to use for a given handle */
418 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
419 struct io_timeouts *timeouts )
421 NTSTATUS status = STATUS_SUCCESS;
423 timeouts->interval = timeouts->total = -1;
429 /* GetCommTimeouts */
433 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
434 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
439 if (st.ReadIntervalTimeout)
440 timeouts->interval = st.ReadIntervalTimeout;
442 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
444 timeouts->total = st.ReadTotalTimeoutConstant;
445 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
446 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
448 else if (st.ReadIntervalTimeout == MAXDWORD)
449 timeouts->interval = timeouts->total = 0;
453 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
455 timeouts->total = st.WriteTotalTimeoutConstant;
456 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
457 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
462 case FD_TYPE_MAILSLOT:
465 timeouts->interval = 0; /* return as soon as we got something */
466 SERVER_START_REQ( set_mailslot_info )
468 req->handle = wine_server_obj_handle( handle );
470 if (!(status = wine_server_call( req )) &&
471 reply->read_timeout != TIMEOUT_INFINITE)
472 timeouts->total = reply->read_timeout / -10000;
480 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
485 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
486 return STATUS_SUCCESS;
490 /* retrieve the timeout for the next wait, in milliseconds */
491 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
495 if (timeouts->total != -1)
497 ret = timeouts->end_time - NtGetTickCount();
498 if (ret < 0) ret = 0;
500 if (already && timeouts->interval != -1)
502 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
508 /* retrieve the avail_mode flag for async reads */
509 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
511 NTSTATUS status = STATUS_SUCCESS;
517 /* GetCommTimeouts */
521 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
522 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
524 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
525 !st.ReadTotalTimeoutConstant &&
526 st.ReadIntervalTimeout == MAXDWORD);
529 case FD_TYPE_MAILSLOT:
543 /******************************************************************************
544 * NtReadFile [NTDLL.@]
545 * ZwReadFile [NTDLL.@]
547 * Read from an open file handle.
550 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
551 * Event [I] Event to signal upon completion (or NULL)
552 * ApcRoutine [I] Callback to call upon completion (or NULL)
553 * ApcContext [I] Context for ApcRoutine (or NULL)
554 * IoStatusBlock [O] Receives information about the operation on return
555 * Buffer [O] Destination for the data read
556 * Length [I] Size of Buffer
557 * ByteOffset [O] Destination for the new file pointer position (or NULL)
558 * Key [O] Function unknown (may be NULL)
561 * Success: 0. IoStatusBlock is updated, and the Information member contains
562 * The number of bytes read.
563 * Failure: An NTSTATUS error code describing the error.
565 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
566 PIO_APC_ROUTINE apc, void* apc_user,
567 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
568 PLARGE_INTEGER offset, PULONG key)
570 int result, unix_handle, needs_close, timeout_init_done = 0;
571 unsigned int options;
572 struct io_timeouts timeouts;
575 enum server_fd_type type;
576 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
578 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
579 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
581 if (!io_status) return STATUS_ACCESS_VIOLATION;
583 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
584 &needs_close, &type, &options );
585 if (status) return status;
587 if (!virtual_check_buffer_for_write( buffer, length ))
589 status = STATUS_ACCESS_VIOLATION;
593 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
595 /* async I/O doesn't make sense on regular files */
596 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
600 status = FILE_GetNtStatus();
604 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
605 /* update file pointer position */
606 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
609 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
615 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
618 if (!result || total == length)
622 status = STATUS_SUCCESS;
629 status = STATUS_END_OF_FILE;
634 status = STATUS_PIPE_BROKEN;
638 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
640 else if (errno != EAGAIN)
642 if (errno == EINTR) continue;
643 if (!total) status = FILE_GetNtStatus();
647 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
649 async_fileio_read *fileio;
652 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
654 if (total && avail_mode)
656 status = STATUS_SUCCESS;
660 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
662 status = STATUS_NO_MEMORY;
665 fileio->io.handle = hFile;
666 fileio->io.apc = apc;
667 fileio->io.apc_arg = apc_user;
668 fileio->already = total;
669 fileio->count = length;
670 fileio->buffer = buffer;
671 fileio->avail_mode = avail_mode;
673 SERVER_START_REQ( register_async )
675 req->type = ASYNC_TYPE_READ;
677 req->async.handle = wine_server_obj_handle( hFile );
678 req->async.event = wine_server_obj_handle( hEvent );
679 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
680 req->async.iosb = wine_server_client_ptr( io_status );
681 req->async.arg = wine_server_client_ptr( fileio );
682 req->async.cvalue = cvalue;
683 status = wine_server_call( req );
687 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
690 else /* synchronous read, wait for the fd to become ready */
695 if (!timeout_init_done)
697 timeout_init_done = 1;
698 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
700 if (hEvent) NtResetEvent( hEvent, NULL );
702 timeout = get_next_io_timeout( &timeouts, total );
704 pfd.fd = unix_handle;
707 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
709 if (total) /* return with what we got so far */
710 status = STATUS_SUCCESS;
712 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
715 if (ret == -1 && errno != EINTR)
717 status = FILE_GetNtStatus();
720 /* will now restart the read */
725 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
728 if (needs_close) close( unix_handle );
729 if (status == STATUS_SUCCESS)
731 io_status->u.Status = status;
732 io_status->Information = total;
733 TRACE("= SUCCESS (%u)\n", total);
734 if (hEvent) NtSetEvent( hEvent, NULL );
735 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
736 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
740 TRACE("= 0x%08x\n", status);
741 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
747 /******************************************************************************
748 * NtReadFileScatter [NTDLL.@]
749 * ZwReadFileScatter [NTDLL.@]
751 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
752 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
753 ULONG length, PLARGE_INTEGER offset, PULONG key )
755 size_t page_size = getpagesize();
756 int result, unix_handle, needs_close;
757 unsigned int options;
759 ULONG pos = 0, total = 0;
760 enum server_fd_type type;
761 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
763 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
764 file, event, apc, apc_user, io_status, segments, length, offset, key);
766 if (length % page_size) return STATUS_INVALID_PARAMETER;
767 if (!io_status) return STATUS_ACCESS_VIOLATION;
769 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
770 &needs_close, &type, &options );
771 if (status) return status;
773 if ((type != FD_TYPE_FILE) ||
774 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
775 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
777 status = STATUS_INVALID_PARAMETER;
783 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
784 result = pread( unix_handle, (char *)segments->Buffer + pos,
785 page_size - pos, offset->QuadPart + total );
787 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
791 if (errno == EINTR) continue;
792 status = FILE_GetNtStatus();
797 status = STATUS_END_OF_FILE;
802 if ((pos += result) == page_size)
809 if (cvalue) NTDLL_AddCompletion( file, cvalue, status, total );
812 if (needs_close) close( unix_handle );
813 if (status == STATUS_SUCCESS)
815 io_status->u.Status = status;
816 io_status->Information = total;
817 TRACE("= SUCCESS (%u)\n", total);
818 if (event) NtSetEvent( event, NULL );
819 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
820 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
824 TRACE("= 0x%08x\n", status);
825 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
831 /***********************************************************************
832 * FILE_AsyncWriteService (INTERNAL)
834 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status, void **apc)
836 async_fileio_write *fileio = user;
837 int result, fd, needs_close;
838 enum server_fd_type type;
843 /* write some data (non-blocking) */
844 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
845 &needs_close, &type, NULL )))
848 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
849 result = send( fd, fileio->buffer, 0, 0 );
851 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
853 if (needs_close) close( fd );
857 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
858 else status = FILE_GetNtStatus();
862 fileio->already += result;
863 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
868 case STATUS_IO_TIMEOUT:
869 if (fileio->already) status = STATUS_SUCCESS;
872 if (status != STATUS_PENDING)
874 iosb->u.Status = status;
875 iosb->Information = fileio->already;
881 /******************************************************************************
882 * NtWriteFile [NTDLL.@]
883 * ZwWriteFile [NTDLL.@]
885 * Write to an open file handle.
888 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
889 * Event [I] Event to signal upon completion (or NULL)
890 * ApcRoutine [I] Callback to call upon completion (or NULL)
891 * ApcContext [I] Context for ApcRoutine (or NULL)
892 * IoStatusBlock [O] Receives information about the operation on return
893 * Buffer [I] Source for the data to write
894 * Length [I] Size of Buffer
895 * ByteOffset [O] Destination for the new file pointer position (or NULL)
896 * Key [O] Function unknown (may be NULL)
899 * Success: 0. IoStatusBlock is updated, and the Information member contains
900 * The number of bytes written.
901 * Failure: An NTSTATUS error code describing the error.
903 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
904 PIO_APC_ROUTINE apc, void* apc_user,
905 PIO_STATUS_BLOCK io_status,
906 const void* buffer, ULONG length,
907 PLARGE_INTEGER offset, PULONG key)
909 int result, unix_handle, needs_close, timeout_init_done = 0;
910 unsigned int options;
911 struct io_timeouts timeouts;
914 enum server_fd_type type;
915 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
917 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
918 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
920 if (!io_status) return STATUS_ACCESS_VIOLATION;
922 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
923 &needs_close, &type, &options );
924 if (status) return status;
926 if (!virtual_check_buffer_for_read( buffer, length ))
928 status = STATUS_INVALID_USER_BUFFER;
932 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
934 /* async I/O doesn't make sense on regular files */
935 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
939 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
940 else status = FILE_GetNtStatus();
945 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
946 /* update file pointer position */
947 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
950 status = STATUS_SUCCESS;
956 /* zero-length writes on sockets may not work with plain write(2) */
957 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
958 result = send( unix_handle, buffer, 0, 0 );
960 result = write( unix_handle, (const char *)buffer + total, length - total );
967 status = STATUS_SUCCESS;
970 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
972 else if (errno != EAGAIN)
974 if (errno == EINTR) continue;
977 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
978 else status = FILE_GetNtStatus();
983 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
985 async_fileio_write *fileio;
987 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
989 status = STATUS_NO_MEMORY;
992 fileio->io.handle = hFile;
993 fileio->io.apc = apc;
994 fileio->io.apc_arg = apc_user;
995 fileio->already = total;
996 fileio->count = length;
997 fileio->buffer = buffer;
999 SERVER_START_REQ( register_async )
1001 req->type = ASYNC_TYPE_WRITE;
1002 req->count = length;
1003 req->async.handle = wine_server_obj_handle( hFile );
1004 req->async.event = wine_server_obj_handle( hEvent );
1005 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1006 req->async.iosb = wine_server_client_ptr( io_status );
1007 req->async.arg = wine_server_client_ptr( fileio );
1008 req->async.cvalue = cvalue;
1009 status = wine_server_call( req );
1013 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1016 else /* synchronous write, wait for the fd to become ready */
1021 if (!timeout_init_done)
1023 timeout_init_done = 1;
1024 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1026 if (hEvent) NtResetEvent( hEvent, NULL );
1028 timeout = get_next_io_timeout( &timeouts, total );
1030 pfd.fd = unix_handle;
1031 pfd.events = POLLOUT;
1033 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1035 /* return with what we got so far */
1036 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1039 if (ret == -1 && errno != EINTR)
1041 status = FILE_GetNtStatus();
1044 /* will now restart the write */
1049 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
1052 if (needs_close) close( unix_handle );
1053 if (status == STATUS_SUCCESS)
1055 io_status->u.Status = status;
1056 io_status->Information = total;
1057 TRACE("= SUCCESS (%u)\n", total);
1058 if (hEvent) NtSetEvent( hEvent, NULL );
1059 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1060 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1064 TRACE("= 0x%08x\n", status);
1065 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1071 /******************************************************************************
1072 * NtWriteFileGather [NTDLL.@]
1073 * ZwWriteFileGather [NTDLL.@]
1075 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1076 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1077 ULONG length, PLARGE_INTEGER offset, PULONG key )
1079 size_t page_size = getpagesize();
1080 int result, unix_handle, needs_close;
1081 unsigned int options;
1083 ULONG pos = 0, total = 0;
1084 enum server_fd_type type;
1085 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1087 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1088 file, event, apc, apc_user, io_status, segments, length, offset, key);
1090 if (length % page_size) return STATUS_INVALID_PARAMETER;
1091 if (!io_status) return STATUS_ACCESS_VIOLATION;
1093 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1094 &needs_close, &type, &options );
1095 if (status) return status;
1097 if ((type != FD_TYPE_FILE) ||
1098 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1099 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1101 status = STATUS_INVALID_PARAMETER;
1107 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
1108 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1109 page_size - pos, offset->QuadPart + total );
1111 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1115 if (errno == EINTR) continue;
1116 if (errno == EFAULT)
1118 status = STATUS_INVALID_USER_BUFFER;
1121 status = FILE_GetNtStatus();
1126 status = STATUS_DISK_FULL;
1131 if ((pos += result) == page_size)
1138 if (cvalue) NTDLL_AddCompletion( file, cvalue, status, total );
1141 if (needs_close) close( unix_handle );
1142 if (status == STATUS_SUCCESS)
1144 io_status->u.Status = status;
1145 io_status->Information = total;
1146 TRACE("= SUCCESS (%u)\n", total);
1147 if (event) NtSetEvent( event, NULL );
1148 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1149 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1153 TRACE("= 0x%08x\n", status);
1154 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1162 HANDLE handle; /* handle to the device */
1163 HANDLE event; /* async event */
1164 void *buffer; /* buffer for output */
1165 ULONG size; /* size of buffer */
1166 PIO_APC_ROUTINE apc; /* user apc params */
1170 /* callback for ioctl user APC */
1171 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
1173 struct async_ioctl *async = arg;
1174 if (async->apc) async->apc( async->apc_arg, io, reserved );
1175 RtlFreeHeap( GetProcessHeap(), 0, async );
1178 /* callback for ioctl async I/O completion */
1179 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status, void **apc )
1181 struct async_ioctl *async = arg;
1183 if (status == STATUS_ALERTED)
1185 SERVER_START_REQ( get_ioctl_result )
1187 req->handle = wine_server_obj_handle( async->handle );
1188 req->user_arg = wine_server_client_ptr( async );
1189 wine_server_set_reply( req, async->buffer, async->size );
1190 status = wine_server_call( req );
1191 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1195 if (status != STATUS_PENDING)
1197 io->u.Status = status;
1198 if (async->apc || async->event) *apc = ioctl_apc;
1203 /* do a ioctl call through the server */
1204 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1205 PIO_APC_ROUTINE apc, PVOID apc_context,
1206 IO_STATUS_BLOCK *io, ULONG code,
1207 const void *in_buffer, ULONG in_size,
1208 PVOID out_buffer, ULONG out_size )
1210 struct async_ioctl *async;
1214 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1216 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
1217 return STATUS_NO_MEMORY;
1218 async->handle = handle;
1219 async->event = event;
1220 async->buffer = out_buffer;
1221 async->size = out_size;
1223 async->apc_arg = apc_context;
1225 SERVER_START_REQ( ioctl )
1228 req->blocking = !apc && !event;
1229 req->async.handle = wine_server_obj_handle( handle );
1230 req->async.callback = wine_server_client_ptr( ioctl_completion );
1231 req->async.iosb = wine_server_client_ptr( io );
1232 req->async.arg = wine_server_client_ptr( async );
1233 req->async.event = wine_server_obj_handle( event );
1234 req->async.cvalue = cvalue;
1235 wine_server_add_data( req, in_buffer, in_size );
1236 wine_server_set_reply( req, out_buffer, out_size );
1237 status = wine_server_call( req );
1238 wait_handle = wine_server_ptr_handle( reply->wait );
1239 options = reply->options;
1240 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1244 if (status == STATUS_NOT_SUPPORTED)
1245 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1246 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1248 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1252 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1253 status = io->u.Status;
1254 NtClose( wait_handle );
1255 RtlFreeHeap( GetProcessHeap(), 0, async );
1262 /**************************************************************************
1263 * NtDeviceIoControlFile [NTDLL.@]
1264 * ZwDeviceIoControlFile [NTDLL.@]
1266 * Perform an I/O control operation on an open file handle.
1269 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1270 * event [I] Event to signal upon completion (or NULL)
1271 * apc [I] Callback to call upon completion (or NULL)
1272 * apc_context [I] Context for ApcRoutine (or NULL)
1273 * io [O] Receives information about the operation on return
1274 * code [I] Control code for the operation to perform
1275 * in_buffer [I] Source for any input data required (or NULL)
1276 * in_size [I] Size of InputBuffer
1277 * out_buffer [O] Source for any output data returned (or NULL)
1278 * out_size [I] Size of OutputBuffer
1281 * Success: 0. IoStatusBlock is updated.
1282 * Failure: An NTSTATUS error code describing the error.
1284 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1285 PIO_APC_ROUTINE apc, PVOID apc_context,
1286 PIO_STATUS_BLOCK io, ULONG code,
1287 PVOID in_buffer, ULONG in_size,
1288 PVOID out_buffer, ULONG out_size)
1290 ULONG device = (code >> 16);
1291 NTSTATUS status = STATUS_NOT_SUPPORTED;
1293 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1294 handle, event, apc, apc_context, io, code,
1295 in_buffer, in_size, out_buffer, out_size);
1299 case FILE_DEVICE_DISK:
1300 case FILE_DEVICE_CD_ROM:
1301 case FILE_DEVICE_DVD:
1302 case FILE_DEVICE_CONTROLLER:
1303 case FILE_DEVICE_MASS_STORAGE:
1304 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1305 in_buffer, in_size, out_buffer, out_size);
1307 case FILE_DEVICE_SERIAL_PORT:
1308 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1309 in_buffer, in_size, out_buffer, out_size);
1311 case FILE_DEVICE_TAPE:
1312 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1313 in_buffer, in_size, out_buffer, out_size);
1317 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1318 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1319 in_buffer, in_size, out_buffer, out_size );
1321 if (status != STATUS_PENDING) io->u.Status = status;
1326 /**************************************************************************
1327 * NtFsControlFile [NTDLL.@]
1328 * ZwFsControlFile [NTDLL.@]
1330 * Perform a file system control operation on an open file handle.
1333 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1334 * event [I] Event to signal upon completion (or NULL)
1335 * apc [I] Callback to call upon completion (or NULL)
1336 * apc_context [I] Context for ApcRoutine (or NULL)
1337 * io [O] Receives information about the operation on return
1338 * code [I] Control code for the operation to perform
1339 * in_buffer [I] Source for any input data required (or NULL)
1340 * in_size [I] Size of InputBuffer
1341 * out_buffer [O] Source for any output data returned (or NULL)
1342 * out_size [I] Size of OutputBuffer
1345 * Success: 0. IoStatusBlock is updated.
1346 * Failure: An NTSTATUS error code describing the error.
1348 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1349 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1350 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1354 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1355 handle, event, apc, apc_context, io, code,
1356 in_buffer, in_size, out_buffer, out_size);
1358 if (!io) return STATUS_INVALID_PARAMETER;
1362 case FSCTL_DISMOUNT_VOLUME:
1363 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1364 in_buffer, in_size, out_buffer, out_size );
1365 if (!status) status = DIR_unmount_device( handle );
1368 case FSCTL_PIPE_PEEK:
1370 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1371 int avail = 0, fd, needs_close;
1373 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1375 status = STATUS_INFO_LENGTH_MISMATCH;
1379 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1383 if (ioctl( fd, FIONREAD, &avail ) != 0)
1385 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1386 if (needs_close) close( fd );
1387 status = FILE_GetNtStatus();
1391 if (!avail) /* check for closed pipe */
1393 struct pollfd pollfd;
1397 pollfd.events = POLLIN;
1399 ret = poll( &pollfd, 1, 0 );
1400 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1402 if (needs_close) close( fd );
1403 status = STATUS_PIPE_BROKEN;
1407 buffer->NamedPipeState = 0; /* FIXME */
1408 buffer->ReadDataAvailable = avail;
1409 buffer->NumberOfMessages = 0; /* FIXME */
1410 buffer->MessageLength = 0; /* FIXME */
1411 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1412 status = STATUS_SUCCESS;
1415 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1418 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1419 if (res >= 0) io->Information += res;
1422 if (needs_close) close( fd );
1426 case FSCTL_PIPE_DISCONNECT:
1427 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1428 in_buffer, in_size, out_buffer, out_size );
1431 int fd = server_remove_fd_from_cache( handle );
1432 if (fd != -1) close( fd );
1436 case FSCTL_PIPE_IMPERSONATE:
1437 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1438 status = RtlImpersonateSelf( SecurityImpersonation );
1441 case FSCTL_LOCK_VOLUME:
1442 case FSCTL_UNLOCK_VOLUME:
1443 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1444 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1445 status = STATUS_SUCCESS;
1448 case FSCTL_PIPE_LISTEN:
1449 case FSCTL_PIPE_WAIT:
1451 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1452 in_buffer, in_size, out_buffer, out_size );
1456 if (status != STATUS_PENDING) io->u.Status = status;
1460 /******************************************************************************
1461 * NtSetVolumeInformationFile [NTDLL.@]
1462 * ZwSetVolumeInformationFile [NTDLL.@]
1464 * Set volume information for an open file handle.
1467 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1468 * IoStatusBlock [O] Receives information about the operation on return
1469 * FsInformation [I] Source for volume information
1470 * Length [I] Size of FsInformation
1471 * FsInformationClass [I] Type of volume information to set
1474 * Success: 0. IoStatusBlock is updated.
1475 * Failure: An NTSTATUS error code describing the error.
1477 NTSTATUS WINAPI NtSetVolumeInformationFile(
1478 IN HANDLE FileHandle,
1479 PIO_STATUS_BLOCK IoStatusBlock,
1480 PVOID FsInformation,
1482 FS_INFORMATION_CLASS FsInformationClass)
1484 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1485 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1489 static NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
1491 data_size_t size = 1024;
1497 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
1498 if (!name) return STATUS_NO_MEMORY;
1499 unix_name->MaximumLength = size + 1;
1501 SERVER_START_REQ( get_handle_unix_name )
1503 req->handle = wine_server_obj_handle( handle );
1504 wine_server_set_reply( req, name, size );
1505 ret = wine_server_call( req );
1506 size = reply->name_len;
1513 unix_name->Buffer = name;
1514 unix_name->Length = size;
1517 RtlFreeHeap( GetProcessHeap(), 0, name );
1518 if (ret != STATUS_BUFFER_OVERFLOW) break;
1523 /******************************************************************************
1524 * NtQueryInformationFile [NTDLL.@]
1525 * ZwQueryInformationFile [NTDLL.@]
1527 * Get information about an open file handle.
1530 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1531 * io [O] Receives information about the operation on return
1532 * ptr [O] Destination for file information
1533 * len [I] Size of FileInformation
1534 * class [I] Type of file information to get
1537 * Success: 0. IoStatusBlock and FileInformation are updated.
1538 * Failure: An NTSTATUS error code describing the error.
1540 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1541 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1543 static const size_t info_sizes[] =
1546 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1547 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1548 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1549 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1550 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1551 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1552 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1553 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1554 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
1555 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1556 0, /* FileLinkInformation */
1557 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1558 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1559 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1560 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1561 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1562 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1563 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1564 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1565 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1566 0, /* FileAlternateNameInformation */
1567 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1568 0, /* FilePipeInformation */
1569 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1570 0, /* FilePipeRemoteInformation */
1571 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1572 0, /* FileMailslotSetInformation */
1573 0, /* FileCompressionInformation */
1574 0, /* FileObjectIdInformation */
1575 0, /* FileCompletionInformation */
1576 0, /* FileMoveClusterInformation */
1577 0, /* FileQuotaInformation */
1578 0, /* FileReparsePointInformation */
1579 0, /* FileNetworkOpenInformation */
1580 0, /* FileAttributeTagInformation */
1581 0 /* FileTrackingInformation */
1585 int fd, needs_close = FALSE;
1587 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1589 io->Information = 0;
1591 if (class <= 0 || class >= FileMaximumInformation)
1592 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1593 if (!info_sizes[class])
1595 FIXME("Unsupported class (%d)\n", class);
1596 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1598 if (len < info_sizes[class])
1599 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1601 if (class != FilePipeLocalInformation)
1603 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1604 return io->u.Status;
1609 case FileBasicInformation:
1611 FILE_BASIC_INFORMATION *info = ptr;
1613 if (fstat( fd, &st ) == -1)
1614 io->u.Status = FILE_GetNtStatus();
1615 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1616 io->u.Status = STATUS_INVALID_INFO_CLASS;
1619 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1620 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1621 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1622 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1623 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1624 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1625 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1626 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1627 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1628 info->CreationTime.QuadPart += st.st_mtim.tv_nsec / 100;
1629 info->LastWriteTime.QuadPart += st.st_mtim.tv_nsec / 100;
1631 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1632 info->ChangeTime.QuadPart += st.st_ctim.tv_nsec / 100;
1634 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1635 info->LastAccessTime.QuadPart += st.st_atim.tv_nsec / 100;
1640 case FileStandardInformation:
1642 FILE_STANDARD_INFORMATION *info = ptr;
1644 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1647 if ((info->Directory = S_ISDIR(st.st_mode)))
1649 info->AllocationSize.QuadPart = 0;
1650 info->EndOfFile.QuadPart = 0;
1651 info->NumberOfLinks = 1;
1652 info->DeletePending = FALSE;
1656 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1657 info->EndOfFile.QuadPart = st.st_size;
1658 info->NumberOfLinks = st.st_nlink;
1659 info->DeletePending = FALSE; /* FIXME */
1664 case FilePositionInformation:
1666 FILE_POSITION_INFORMATION *info = ptr;
1667 off_t res = lseek( fd, 0, SEEK_CUR );
1668 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1669 else info->CurrentByteOffset.QuadPart = res;
1672 case FileInternalInformation:
1674 FILE_INTERNAL_INFORMATION *info = ptr;
1676 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1677 else info->IndexNumber.QuadPart = st.st_ino;
1680 case FileEaInformation:
1682 FILE_EA_INFORMATION *info = ptr;
1686 case FileEndOfFileInformation:
1688 FILE_END_OF_FILE_INFORMATION *info = ptr;
1690 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1691 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1694 case FileAllInformation:
1696 FILE_ALL_INFORMATION *info = ptr;
1698 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1699 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1700 io->u.Status = STATUS_INVALID_INFO_CLASS;
1703 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1705 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1706 info->StandardInformation.AllocationSize.QuadPart = 0;
1707 info->StandardInformation.EndOfFile.QuadPart = 0;
1708 info->StandardInformation.NumberOfLinks = 1;
1709 info->StandardInformation.DeletePending = FALSE;
1713 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1714 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1715 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1716 info->StandardInformation.NumberOfLinks = st.st_nlink;
1717 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1719 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1720 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1721 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1722 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1723 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1724 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1725 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1726 info->BasicInformation.CreationTime.QuadPart += st.st_mtim.tv_nsec / 100;
1727 info->BasicInformation.LastWriteTime.QuadPart += st.st_mtim.tv_nsec / 100;
1729 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1730 info->BasicInformation.ChangeTime.QuadPart += st.st_ctim.tv_nsec / 100;
1732 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1733 info->BasicInformation.LastAccessTime.QuadPart += st.st_atim.tv_nsec / 100;
1735 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1736 info->EaInformation.EaSize = 0;
1737 info->AccessInformation.AccessFlags = 0; /* FIXME */
1738 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1739 info->ModeInformation.Mode = 0; /* FIXME */
1740 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1741 info->NameInformation.FileNameLength = 0;
1742 io->Information = sizeof(*info) - sizeof(WCHAR);
1746 case FileMailslotQueryInformation:
1748 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1750 SERVER_START_REQ( set_mailslot_info )
1752 req->handle = wine_server_obj_handle( hFile );
1754 io->u.Status = wine_server_call( req );
1755 if( io->u.Status == STATUS_SUCCESS )
1757 info->MaximumMessageSize = reply->max_msgsize;
1758 info->MailslotQuota = 0;
1759 info->NextMessageSize = 0;
1760 info->MessagesAvailable = 0;
1761 info->ReadTimeout.QuadPart = reply->read_timeout;
1768 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1769 if (size > 0x10000) size = 0x10000;
1770 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1772 int fd, needs_close;
1773 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1775 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1776 info->MessagesAvailable = (res > 0);
1777 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1778 if (needs_close) close( fd );
1780 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1785 case FilePipeLocalInformation:
1787 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1789 SERVER_START_REQ( get_named_pipe_info )
1791 req->handle = wine_server_obj_handle( hFile );
1792 if (!(io->u.Status = wine_server_call( req )))
1794 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1795 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1796 pli->NamedPipeConfiguration = 0; /* FIXME */
1797 pli->MaximumInstances = reply->maxinstances;
1798 pli->CurrentInstances = reply->instances;
1799 pli->InboundQuota = reply->insize;
1800 pli->ReadDataAvailable = 0; /* FIXME */
1801 pli->OutboundQuota = reply->outsize;
1802 pli->WriteQuotaAvailable = 0; /* FIXME */
1803 pli->NamedPipeState = 0; /* FIXME */
1804 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1805 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1811 case FileNameInformation:
1813 FILE_NAME_INFORMATION *info = ptr;
1814 ANSI_STRING unix_name;
1816 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
1818 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
1819 UNICODE_STRING nt_name;
1821 io->u.Status = wine_unix_to_nt_file_name( &unix_name, &nt_name );
1822 RtlFreeAnsiString( &unix_name );
1826 const WCHAR *ptr = nt_name.Buffer;
1827 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
1829 /* Skip the volume mount point. */
1830 while (ptr != end && *ptr == '\\') ++ptr;
1831 while (ptr != end && *ptr != '\\') ++ptr;
1832 while (ptr != end && *ptr == '\\') ++ptr;
1833 while (ptr != end && *ptr != '\\') ++ptr;
1835 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
1836 if (name_len < info->FileNameLength) io->u.Status = STATUS_BUFFER_OVERFLOW;
1837 else name_len = info->FileNameLength;
1839 memcpy( info->FileName, ptr, name_len );
1840 RtlFreeUnicodeString( &nt_name );
1842 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
1848 FIXME("Unsupported class (%d)\n", class);
1849 io->u.Status = STATUS_NOT_IMPLEMENTED;
1852 if (needs_close) close( fd );
1853 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1854 return io->u.Status;
1857 /******************************************************************************
1858 * NtSetInformationFile [NTDLL.@]
1859 * ZwSetInformationFile [NTDLL.@]
1861 * Set information about an open file handle.
1864 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1865 * io [O] Receives information about the operation on return
1866 * ptr [I] Source for file information
1867 * len [I] Size of FileInformation
1868 * class [I] Type of file information to set
1871 * Success: 0. io is updated.
1872 * Failure: An NTSTATUS error code describing the error.
1874 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1875 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1877 int fd, needs_close;
1879 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1881 io->u.Status = STATUS_SUCCESS;
1884 case FileBasicInformation:
1885 if (len >= sizeof(FILE_BASIC_INFORMATION))
1888 const FILE_BASIC_INFORMATION *info = ptr;
1890 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1891 return io->u.Status;
1893 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1895 struct timeval tv[2];
1897 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1900 tv[0].tv_sec = tv[0].tv_usec = 0;
1901 tv[1].tv_sec = tv[1].tv_usec = 0;
1902 if (!fstat( fd, &st ))
1904 tv[0].tv_sec = st.st_atime;
1905 tv[1].tv_sec = st.st_mtime;
1908 if (info->LastAccessTime.QuadPart)
1910 ULONGLONG sec = info->LastAccessTime.QuadPart / 10000000;
1911 UINT nsec = info->LastAccessTime.QuadPart % 10000000;
1912 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1913 tv[0].tv_usec = nsec / 10;
1915 if (info->LastWriteTime.QuadPart)
1917 ULONGLONG sec = info->LastWriteTime.QuadPart / 10000000;
1918 UINT nsec = info->LastWriteTime.QuadPart % 10000000;
1919 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1920 tv[1].tv_usec = nsec / 10;
1922 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1925 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1927 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1930 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1932 if (S_ISDIR( st.st_mode))
1933 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1935 st.st_mode &= ~0222; /* clear write permission bits */
1939 /* add write permission only where we already have read permission */
1940 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1942 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1946 if (needs_close) close( fd );
1948 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1951 case FilePositionInformation:
1952 if (len >= sizeof(FILE_POSITION_INFORMATION))
1954 const FILE_POSITION_INFORMATION *info = ptr;
1956 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1957 return io->u.Status;
1959 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1960 io->u.Status = FILE_GetNtStatus();
1962 if (needs_close) close( fd );
1964 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1967 case FileEndOfFileInformation:
1968 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1971 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1973 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1974 return io->u.Status;
1976 /* first try normal truncate */
1977 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1979 /* now check for the need to extend the file */
1980 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1982 static const char zero;
1984 /* extend the file one byte beyond the requested size and then truncate it */
1985 /* this should work around ftruncate implementations that can't extend files */
1986 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1987 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1989 io->u.Status = FILE_GetNtStatus();
1991 if (needs_close) close( fd );
1993 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1996 case FileMailslotSetInformation:
1998 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2000 SERVER_START_REQ( set_mailslot_info )
2002 req->handle = wine_server_obj_handle( handle );
2003 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2004 req->read_timeout = info->ReadTimeout.QuadPart;
2005 io->u.Status = wine_server_call( req );
2011 case FileCompletionInformation:
2012 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2014 FILE_COMPLETION_INFORMATION *info = ptr;
2016 SERVER_START_REQ( set_completion_info )
2018 req->handle = wine_server_obj_handle( handle );
2019 req->chandle = wine_server_obj_handle( info->CompletionPort );
2020 req->ckey = info->CompletionKey;
2021 io->u.Status = wine_server_call( req );
2025 io->u.Status = STATUS_INVALID_PARAMETER_3;
2029 FIXME("Unsupported class (%d)\n", class);
2030 io->u.Status = STATUS_NOT_IMPLEMENTED;
2033 io->Information = 0;
2034 return io->u.Status;
2038 /******************************************************************************
2039 * FILE_QueryFullAttributesFile (internal)
2041 static NTSTATUS FILE_QueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2042 FILE_NETWORK_OPEN_INFORMATION *info )
2044 ANSI_STRING unix_name;
2047 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
2048 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
2052 if (stat( unix_name.Buffer, &st ) == -1)
2053 status = FILE_GetNtStatus();
2054 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2055 status = STATUS_INVALID_INFO_CLASS;
2058 if (S_ISDIR(st.st_mode))
2060 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
2061 info->AllocationSize.QuadPart = 0;
2062 info->EndOfFile.QuadPart = 0;
2066 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
2067 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
2068 info->EndOfFile.QuadPart = st.st_size;
2070 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
2071 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
2072 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
2073 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
2074 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
2075 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
2076 if (DIR_is_hidden_file( attr->ObjectName ))
2077 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2079 RtlFreeAnsiString( &unix_name );
2081 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2085 /******************************************************************************
2086 * NtQueryFullAttributesFile (NTDLL.@)
2088 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2089 FILE_NETWORK_OPEN_INFORMATION *info )
2091 return FILE_QueryFullAttributesFile( attr, info );
2095 /******************************************************************************
2096 * NtQueryAttributesFile (NTDLL.@)
2097 * ZwQueryAttributesFile (NTDLL.@)
2099 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2101 FILE_NETWORK_OPEN_INFORMATION full_info;
2104 if (!(status = FILE_QueryFullAttributesFile( attr, &full_info )))
2106 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
2107 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
2108 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
2109 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
2110 info->FileAttributes = full_info.FileAttributes;
2116 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
2117 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2118 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2119 unsigned int flags )
2121 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2123 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2124 /* Don't assume read-only, let the mount options set it below */
2125 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2127 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2128 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2130 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2131 info->Characteristics |= FILE_REMOTE_DEVICE;
2133 else if (!strcmp("procfs", fstypename))
2134 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2136 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2138 if (flags & MNT_RDONLY)
2139 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2141 if (!(flags & MNT_LOCAL))
2143 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2144 info->Characteristics |= FILE_REMOTE_DEVICE;
2149 static inline int is_device_placeholder( int fd )
2151 static const char wine_placeholder[] = "Wine device placeholder";
2152 char buffer[sizeof(wine_placeholder)-1];
2154 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2156 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2159 /******************************************************************************
2162 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2164 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2168 info->Characteristics = 0;
2169 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2170 if (S_ISCHR( st.st_mode ))
2172 info->DeviceType = FILE_DEVICE_UNKNOWN;
2174 switch(major(st.st_rdev))
2177 info->DeviceType = FILE_DEVICE_NULL;
2180 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2183 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2185 case SCSI_TAPE_MAJOR:
2186 info->DeviceType = FILE_DEVICE_TAPE;
2191 else if (S_ISBLK( st.st_mode ))
2193 info->DeviceType = FILE_DEVICE_DISK;
2195 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2197 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2199 else if (is_device_placeholder( fd ))
2201 info->DeviceType = FILE_DEVICE_DISK;
2203 else /* regular file or directory */
2205 #if defined(linux) && defined(HAVE_FSTATFS)
2208 /* check for floppy disk */
2209 if (major(st.st_dev) == FLOPPY_MAJOR)
2210 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2212 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2213 switch (stfs.f_type)
2215 case 0x9660: /* iso9660 */
2216 case 0x9fa1: /* supermount */
2217 case 0x15013346: /* udf */
2218 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2219 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2221 case 0x6969: /* nfs */
2222 case 0x517B: /* smbfs */
2223 case 0x564c: /* ncpfs */
2224 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2225 info->Characteristics |= FILE_REMOTE_DEVICE;
2227 case 0x01021994: /* tmpfs */
2228 case 0x28cd3d45: /* cramfs */
2229 case 0x1373: /* devfs */
2230 case 0x9fa0: /* procfs */
2231 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2234 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2237 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__APPLE__)
2240 if (fstatfs( fd, &stfs ) < 0)
2241 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2243 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2244 #elif defined(__NetBSD__)
2245 struct statvfs stfs;
2247 if (fstatvfs( fd, &stfs) < 0)
2248 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2250 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2252 /* Use dkio to work out device types */
2254 # include <sys/dkio.h>
2255 # include <sys/vtoc.h>
2256 struct dk_cinfo dkinf;
2257 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2259 WARN("Unable to get disk device type information - assuming a disk like device\n");
2260 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2262 switch (dkinf.dki_ctype)
2265 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2266 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2270 case DKC_INTEL82072:
2271 case DKC_INTEL82077:
2272 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2273 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2276 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2279 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2284 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2285 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2287 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2289 return STATUS_SUCCESS;
2293 /******************************************************************************
2294 * NtQueryVolumeInformationFile [NTDLL.@]
2295 * ZwQueryVolumeInformationFile [NTDLL.@]
2297 * Get volume information for an open file handle.
2300 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2301 * io [O] Receives information about the operation on return
2302 * buffer [O] Destination for volume information
2303 * length [I] Size of FsInformation
2304 * info_class [I] Type of volume information to set
2307 * Success: 0. io and buffer are updated.
2308 * Failure: An NTSTATUS error code describing the error.
2310 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2311 PVOID buffer, ULONG length,
2312 FS_INFORMATION_CLASS info_class )
2314 int fd, needs_close;
2318 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2319 return io->u.Status;
2321 io->u.Status = STATUS_NOT_IMPLEMENTED;
2322 io->Information = 0;
2324 switch( info_class )
2326 case FileFsVolumeInformation:
2327 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2329 case FileFsLabelInformation:
2330 FIXME( "%p: label info not supported\n", handle );
2332 case FileFsSizeInformation:
2333 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2334 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2337 FILE_FS_SIZE_INFORMATION *info = buffer;
2339 if (fstat( fd, &st ) < 0)
2341 io->u.Status = FILE_GetNtStatus();
2344 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2346 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2351 /* Linux's fstatvfs is buggy */
2352 #if !defined(linux) || !defined(HAVE_FSTATFS)
2353 struct statvfs stfs;
2355 if (fstatvfs( fd, &stfs ) < 0)
2357 io->u.Status = FILE_GetNtStatus();
2360 bsize = stfs.f_frsize;
2363 if (fstatfs( fd, &stfs ) < 0)
2365 io->u.Status = FILE_GetNtStatus();
2368 bsize = stfs.f_bsize;
2370 if (bsize == 2048) /* assume CD-ROM */
2372 info->BytesPerSector = 2048;
2373 info->SectorsPerAllocationUnit = 1;
2377 info->BytesPerSector = 512;
2378 info->SectorsPerAllocationUnit = 8;
2380 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2381 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2382 io->Information = sizeof(*info);
2383 io->u.Status = STATUS_SUCCESS;
2387 case FileFsDeviceInformation:
2388 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2389 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2392 FILE_FS_DEVICE_INFORMATION *info = buffer;
2394 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2395 io->Information = sizeof(*info);
2398 case FileFsAttributeInformation:
2399 FIXME( "%p: attribute info not supported\n", handle );
2401 case FileFsControlInformation:
2402 FIXME( "%p: control info not supported\n", handle );
2404 case FileFsFullSizeInformation:
2405 FIXME( "%p: full size info not supported\n", handle );
2407 case FileFsObjectIdInformation:
2408 FIXME( "%p: object id info not supported\n", handle );
2410 case FileFsMaximumInformation:
2411 FIXME( "%p: maximum info not supported\n", handle );
2414 io->u.Status = STATUS_INVALID_PARAMETER;
2417 if (needs_close) close( fd );
2418 return io->u.Status;
2422 /******************************************************************
2423 * NtQueryEaFile (NTDLL.@)
2425 * Read extended attributes from NTFS files.
2428 * hFile [I] File handle, must be opened with FILE_READ_EA access
2429 * iosb [O] Receives information about the operation on return
2430 * buffer [O] Output buffer
2431 * length [I] Length of output buffer
2432 * single_entry [I] Only read and return one entry
2433 * ea_list [I] Optional list with names of EAs to return
2434 * ea_list_len [I] Length of ea_list in bytes
2435 * ea_index [I] Optional pointer to 1-based index of attribute to return
2436 * restart [I] restart EA scan
2439 * Success: 0. Atrributes read into buffer
2440 * Failure: An NTSTATUS error code describing the error.
2442 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
2443 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
2444 PULONG ea_index, BOOLEAN restart )
2446 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
2447 hFile, iosb, buffer, length, single_entry, ea_list,
2448 ea_list_len, ea_index, restart);
2449 return STATUS_ACCESS_DENIED;
2453 /******************************************************************
2454 * NtSetEaFile (NTDLL.@)
2456 * Update extended attributes for NTFS files.
2459 * hFile [I] File handle, must be opened with FILE_READ_EA access
2460 * iosb [O] Receives information about the operation on return
2461 * buffer [I] Buffer with EA information
2462 * length [I] Length of buffer
2465 * Success: 0. Attributes are updated
2466 * Failure: An NTSTATUS error code describing the error.
2468 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
2470 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
2471 return STATUS_ACCESS_DENIED;
2475 /******************************************************************
2476 * NtFlushBuffersFile (NTDLL.@)
2478 * Flush any buffered data on an open file handle.
2481 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2482 * IoStatusBlock [O] Receives information about the operation on return
2485 * Success: 0. IoStatusBlock is updated.
2486 * Failure: An NTSTATUS error code describing the error.
2488 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2491 HANDLE hEvent = NULL;
2493 SERVER_START_REQ( flush_file )
2495 req->handle = wine_server_obj_handle( hFile );
2496 ret = wine_server_call( req );
2497 hEvent = wine_server_ptr_handle( reply->event );
2502 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2508 /******************************************************************
2509 * NtLockFile (NTDLL.@)
2513 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2514 PIO_APC_ROUTINE apc, void* apc_user,
2515 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2516 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2522 static BOOLEAN warn = TRUE;
2524 if (apc || io_status || key)
2526 FIXME("Unimplemented yet parameter\n");
2527 return STATUS_NOT_IMPLEMENTED;
2530 if (apc_user && warn)
2532 FIXME("I/O completion on lock not implemented yet\n");
2538 SERVER_START_REQ( lock_file )
2540 req->handle = wine_server_obj_handle( hFile );
2541 req->offset = offset->QuadPart;
2542 req->count = count->QuadPart;
2543 req->shared = !exclusive;
2544 req->wait = !dont_wait;
2545 ret = wine_server_call( req );
2546 handle = wine_server_ptr_handle( reply->handle );
2547 async = reply->overlapped;
2550 if (ret != STATUS_PENDING)
2552 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2558 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2559 if (handle) NtClose( handle );
2560 return STATUS_PENDING;
2564 NtWaitForSingleObject( handle, FALSE, NULL );
2571 /* Unix lock conflict, sleep a bit and retry */
2572 time.QuadPart = 100 * (ULONGLONG)10000;
2573 time.QuadPart = -time.QuadPart;
2574 NtDelayExecution( FALSE, &time );
2580 /******************************************************************
2581 * NtUnlockFile (NTDLL.@)
2585 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2586 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2591 TRACE( "%p %x%08x %x%08x\n",
2592 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2594 if (io_status || key)
2596 FIXME("Unimplemented yet parameter\n");
2597 return STATUS_NOT_IMPLEMENTED;
2600 SERVER_START_REQ( unlock_file )
2602 req->handle = wine_server_obj_handle( hFile );
2603 req->offset = offset->QuadPart;
2604 req->count = count->QuadPart;
2605 status = wine_server_call( req );
2611 /******************************************************************
2612 * NtCreateNamedPipeFile (NTDLL.@)
2616 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2617 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2618 ULONG sharing, ULONG dispo, ULONG options,
2619 ULONG pipe_type, ULONG read_mode,
2620 ULONG completion_mode, ULONG max_inst,
2621 ULONG inbound_quota, ULONG outbound_quota,
2622 PLARGE_INTEGER timeout)
2626 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2627 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2628 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2629 outbound_quota, timeout);
2631 /* assume we only get relative timeout */
2632 if (timeout->QuadPart > 0)
2633 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2635 SERVER_START_REQ( create_named_pipe )
2637 req->access = access;
2638 req->attributes = attr->Attributes;
2639 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2640 req->options = options;
2642 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2643 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2644 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2645 req->maxinstances = max_inst;
2646 req->outsize = outbound_quota;
2647 req->insize = inbound_quota;
2648 req->timeout = timeout->QuadPart;
2649 wine_server_add_data( req, attr->ObjectName->Buffer,
2650 attr->ObjectName->Length );
2651 status = wine_server_call( req );
2652 if (!status) *handle = wine_server_ptr_handle( reply->handle );
2658 /******************************************************************
2659 * NtDeleteFile (NTDLL.@)
2663 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2669 TRACE("%p\n", ObjectAttributes);
2670 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2671 ObjectAttributes, &io, NULL, 0,
2672 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2673 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2674 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2678 /******************************************************************
2679 * NtCancelIoFileEx (NTDLL.@)
2683 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
2685 LARGE_INTEGER timeout;
2687 TRACE("%p %p %p\n", hFile, iosb, io_status );
2689 SERVER_START_REQ( cancel_async )
2691 req->handle = wine_server_obj_handle( hFile );
2692 req->iosb = wine_server_client_ptr( iosb );
2693 req->only_thread = FALSE;
2694 io_status->u.Status = wine_server_call( req );
2697 if (io_status->u.Status)
2698 return io_status->u.Status;
2700 /* Let some APC be run, so that we can run the remaining APCs on hFile
2701 * either the cancelation of the pending one, but also the execution
2702 * of the queued APC, but not yet run. This is needed to ensure proper
2703 * clean-up of allocated data.
2705 timeout.u.LowPart = timeout.u.HighPart = 0;
2706 NtDelayExecution( TRUE, &timeout );
2707 return io_status->u.Status;
2710 /******************************************************************
2711 * NtCancelIoFile (NTDLL.@)
2715 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2717 LARGE_INTEGER timeout;
2719 TRACE("%p %p\n", hFile, io_status );
2721 SERVER_START_REQ( cancel_async )
2723 req->handle = wine_server_obj_handle( hFile );
2725 req->only_thread = TRUE;
2726 io_status->u.Status = wine_server_call( req );
2729 if (io_status->u.Status)
2730 return io_status->u.Status;
2732 /* Let some APC be run, so that we can run the remaining APCs on hFile
2733 * either the cancelation of the pending one, but also the execution
2734 * of the queued APC, but not yet run. This is needed to ensure proper
2735 * clean-up of allocated data.
2737 timeout.u.LowPart = timeout.u.HighPart = 0;
2738 NtDelayExecution( TRUE, &timeout );
2739 return io_status->u.Status;
2742 /******************************************************************************
2743 * NtCreateMailslotFile [NTDLL.@]
2744 * ZwCreateMailslotFile [NTDLL.@]
2747 * pHandle [O] pointer to receive the handle created
2748 * DesiredAccess [I] access mode (read, write, etc)
2749 * ObjectAttributes [I] fully qualified NT path of the mailslot
2750 * IoStatusBlock [O] receives completion status and other info
2753 * MaxMessageSize [I]
2759 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2760 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2761 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2762 PLARGE_INTEGER TimeOut)
2764 LARGE_INTEGER timeout;
2767 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2768 pHandle, DesiredAccess, attr, IoStatusBlock,
2769 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2771 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2772 if (!attr) return STATUS_INVALID_PARAMETER;
2773 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2776 * For a NULL TimeOut pointer set the default timeout value
2779 timeout.QuadPart = -1;
2781 timeout.QuadPart = TimeOut->QuadPart;
2783 SERVER_START_REQ( create_mailslot )
2785 req->access = DesiredAccess;
2786 req->attributes = attr->Attributes;
2787 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2788 req->max_msgsize = MaxMessageSize;
2789 req->read_timeout = timeout.QuadPart;
2790 wine_server_add_data( req, attr->ObjectName->Buffer,
2791 attr->ObjectName->Length );
2792 ret = wine_server_call( req );
2793 if( ret == STATUS_SUCCESS )
2794 *pHandle = wine_server_ptr_handle( reply->handle );