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_LINUX_MAJOR_H
31 # include <linux/major.h>
33 #ifdef HAVE_SYS_STATVFS_H
34 # include <sys/statvfs.h>
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
39 #ifdef HAVE_SYS_SYSCALL_H
40 # include <sys/syscall.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>
75 #ifdef HAVE_VALGRIND_MEMCHECK_H
76 # include <valgrind/memcheck.h>
79 #define NONAMELESSUNION
80 #define NONAMELESSSTRUCT
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"
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)
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
);
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
);
149 if (io
->u
.Status
== STATUS_SUCCESS
) io
->Information
= FILE_OPENED
;
153 if (io
->u
.Status
== STATUS_NO_SUCH_FILE
&&
154 disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
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
);
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
);
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
)
200 io
->Information
= FILE_SUPERSEDED
;
203 io
->Information
= FILE_CREATED
;
207 io
->Information
= FILE_OPENED
;
210 case FILE_OVERWRITE_IF
:
211 io
->Information
= FILE_OVERWRITTEN
;
215 else if (io
->u
.Status
== STATUS_TOO_MANY_OPENED_FILES
)
218 if (!once
++) ERR_(winediag
)( "Too many open files, ulimit -n probably needs to be increased\n" );
224 /**************************************************************************
225 * NtOpenFile [NTDLL.@]
226 * ZwOpenFile [NTDLL.@]
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
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.
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
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 *
296 struct async_fileio io
;
298 unsigned int already
;
305 struct async_fileio io
;
307 unsigned int already
;
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)
330 TRACE( "errno = %d\n", errno
);
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
;
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
;
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
;
350 case ENOMEDIUM
: return STATUS_NO_MEDIA_IN_DEVICE
;
352 case ENXIO
: return STATUS_NO_SUCH_DEVICE
;
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
;
361 case ENOEXEC
: /* ?? */
362 case EEXIST
: /* ?? */
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
;
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
)))
385 result
= read(fd
, &fileio
->buffer
[fileio
->already
], fileio
->count
- fileio
->already
);
386 if (needs_close
) close( fd
);
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
;
401 fileio
->already
+= result
;
402 if (fileio
->already
>= fileio
->count
|| fileio
->avail_mode
)
403 status
= STATUS_SUCCESS
;
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
;
416 case STATUS_IO_TIMEOUT
:
417 if (fileio
->already
) status
= STATUS_SUCCESS
;
420 if (status
!= STATUS_PENDING
)
422 iosb
->u
.Status
= status
;
423 iosb
->Information
= fileio
->already
;
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;
448 /* GetCommTimeouts */
452 status
= NtDeviceIoControlFile( handle
, NULL
, NULL
, NULL
, &io
,
453 IOCTL_SERIAL_GET_TIMEOUTS
, NULL
, 0, &st
, sizeof(st
) );
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;
472 if (st
.WriteTotalTimeoutMultiplier
|| st
.WriteTotalTimeoutConstant
)
474 timeouts
->total
= st
.WriteTotalTimeoutConstant
;
475 if (st
.WriteTotalTimeoutMultiplier
!= MAXDWORD
)
476 timeouts
->total
+= count
* st
.WriteTotalTimeoutMultiplier
;
481 case FD_TYPE_MAILSLOT
:
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
);
489 if (!(status
= wine_server_call( req
)) &&
490 reply
->read_timeout
!= TIMEOUT_INFINITE
)
491 timeouts
->total
= reply
->read_timeout
/ -10000;
499 if (is_read
) timeouts
->interval
= 0; /* return as soon as we got something */
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
)
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
;
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
;
536 /* GetCommTimeouts */
540 status
= NtDeviceIoControlFile( handle
, NULL
, NULL
, NULL
, &io
,
541 IOCTL_SERIAL_GET_TIMEOUTS
, NULL
, 0, &st
, sizeof(st
) );
543 *avail_mode
= (!st
.ReadTotalTimeoutMultiplier
&&
544 !st
.ReadTotalTimeoutConstant
&&
545 st
.ReadIntervalTimeout
== MAXDWORD
);
548 case FD_TYPE_MAILSLOT
:
562 /******************************************************************************
563 * NtReadFile [NTDLL.@]
564 * ZwReadFile [NTDLL.@]
566 * Read from an open file handle.
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)
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
;
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
;
615 if (type
== FD_TYPE_FILE
)
617 if (async_read
&& (!offset
|| offset
->QuadPart
< 0))
619 status
= STATUS_INVALID_PARAMETER
;
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)
630 status
= FILE_GetNtStatus();
635 /* update file pointer position */
636 lseek( unix_handle
, offset
->QuadPart
+ result
, SEEK_SET
);
639 status
= (total
|| !length
) ? STATUS_SUCCESS
: STATUS_END_OF_FILE
;
643 else if (type
== FD_TYPE_SERIAL
)
645 if (async_read
&& (!offset
|| offset
->QuadPart
< 0))
647 status
= STATUS_INVALID_PARAMETER
;
654 if ((result
= read( unix_handle
, (char *)buffer
+ total
, length
- total
)) >= 0)
657 if (!result
|| total
== length
)
661 status
= STATUS_SUCCESS
;
668 status
= length
? STATUS_END_OF_FILE
: STATUS_SUCCESS
;
673 status
= STATUS_PIPE_BROKEN
;
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();
688 async_fileio_read
*fileio
;
691 if ((status
= get_io_avail_mode( hFile
, type
, &avail_mode
)))
693 if (total
&& avail_mode
)
695 status
= STATUS_SUCCESS
;
699 if (!(fileio
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio
))))
701 status
= STATUS_NO_MEMORY
;
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
;
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
);
726 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, fileio
);
729 else /* synchronous read, wait for the fd to become ready */
734 if (!timeout_init_done
)
736 timeout_init_done
= TRUE
;
737 if ((status
= get_io_timeouts( hFile
, type
, length
, TRUE
, &timeouts
)))
739 if (hEvent
) NtResetEvent( hEvent
, NULL
);
741 timeout
= get_next_io_timeout( &timeouts
, total
);
743 pfd
.fd
= unix_handle
;
746 if (!timeout
|| !(ret
= poll( &pfd
, 1, timeout
)))
748 if (total
) /* return with what we got so far */
749 status
= STATUS_SUCCESS
;
751 status
= (type
== FD_TYPE_MAILSLOT
) ? STATUS_IO_TIMEOUT
: STATUS_TIMEOUT
;
754 if (ret
== -1 && errno
!= EINTR
)
756 status
= FILE_GetNtStatus();
759 /* will now restart the read */
764 send_completion
= cvalue
!= 0;
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 );
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
);
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
;
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
;
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
);
829 result
= read( unix_handle
, (char *)segments
->Buffer
+ pos
, page_size
- pos
);
833 if (errno
== EINTR
) continue;
834 status
= FILE_GetNtStatus();
839 status
= STATUS_END_OF_FILE
;
844 if ((pos
+= result
) == page_size
)
851 send_completion
= cvalue
!= 0;
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 );
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
);
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
;
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
)))
893 if (!fileio
->count
&& (type
== FD_TYPE_MAILSLOT
|| type
== FD_TYPE_PIPE
|| type
== FD_TYPE_SOCKET
))
894 result
= send( fd
, fileio
->buffer
, 0, 0 );
896 result
= write( fd
, &fileio
->buffer
[fileio
->already
], fileio
->count
- fileio
->already
);
898 if (needs_close
) close( fd
);
902 if (errno
== EAGAIN
|| errno
== EINTR
) status
= STATUS_PENDING
;
903 else status
= FILE_GetNtStatus();
907 fileio
->already
+= result
;
908 status
= (fileio
->already
< fileio
->count
) ? STATUS_PENDING
: STATUS_SUCCESS
;
913 case STATUS_IO_TIMEOUT
:
914 if (fileio
->already
) status
= STATUS_SUCCESS
;
917 if (status
!= STATUS_PENDING
)
919 iosb
->u
.Status
= status
;
920 iosb
->Information
= fileio
->already
;
926 static NTSTATUS
set_pending_write( HANDLE device
)
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
);
940 /******************************************************************************
941 * NtWriteFile [NTDLL.@]
942 * ZwWriteFile [NTDLL.@]
944 * Write to an open file handle.
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)
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
;
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
);
991 if (status
) return status
;
993 if (!virtual_check_buffer_for_read( buffer
, length
))
995 status
= STATUS_INVALID_USER_BUFFER
;
999 async_write
= !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
1001 if (type
== FD_TYPE_FILE
)
1004 (!offset
|| (offset
->QuadPart
< 0 && offset
->QuadPart
!= FILE_WRITE_TO_END_OF_FILE
)))
1006 status
= STATUS_INVALID_PARAMETER
;
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
)
1024 if (fstat( unix_handle
, &st
) == -1)
1026 status
= FILE_GetNtStatus();
1031 else if (offset
->QuadPart
< 0)
1033 status
= STATUS_INVALID_PARAMETER
;
1037 /* async I/O doesn't make sense on regular files */
1038 while ((result
= pwrite( unix_handle
, buffer
, length
, off
)) == -1)
1042 if (errno
== EFAULT
) status
= STATUS_INVALID_USER_BUFFER
;
1043 else status
= FILE_GetNtStatus();
1049 /* update file pointer position */
1050 lseek( unix_handle
, off
+ result
, SEEK_SET
);
1053 status
= STATUS_SUCCESS
;
1057 else if (type
== FD_TYPE_SERIAL
)
1060 (!offset
|| (offset
->QuadPart
< 0 && offset
->QuadPart
!= FILE_WRITE_TO_END_OF_FILE
)))
1062 status
= STATUS_INVALID_PARAMETER
;
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 );
1073 result
= write( unix_handle
, (const char *)buffer
+ total
, length
- total
);
1078 if (total
== length
)
1080 status
= STATUS_SUCCESS
;
1083 if (type
== FD_TYPE_FILE
) continue; /* no async I/O on regular files */
1085 else if (errno
!= EAGAIN
)
1087 if (errno
== EINTR
) continue;
1090 if (errno
== EFAULT
) status
= STATUS_INVALID_USER_BUFFER
;
1091 else status
= FILE_GetNtStatus();
1098 async_fileio_write
*fileio
;
1100 if (!(fileio
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio
))))
1102 status
= STATUS_NO_MEMORY
;
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
);
1126 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, fileio
);
1129 else /* synchronous write, wait for the fd to become ready */
1134 if (!timeout_init_done
)
1136 timeout_init_done
= TRUE
;
1137 if ((status
= get_io_timeouts( hFile
, type
, length
, FALSE
, &timeouts
)))
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
;
1152 if (ret
== -1 && errno
!= EINTR
)
1154 status
= FILE_GetNtStatus();
1157 /* will now restart the write */
1162 send_completion
= cvalue
!= 0;
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 );
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
);
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
;
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
;
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
);
1231 result
= write( unix_handle
, (char *)segments
->Buffer
+ pos
, page_size
- pos
);
1235 if (errno
== EINTR
) continue;
1236 if (errno
== EFAULT
)
1238 status
= STATUS_INVALID_USER_BUFFER
;
1241 status
= FILE_GetNtStatus();
1246 status
= STATUS_DISK_FULL
;
1251 if ((pos
+= result
) == page_size
)
1258 send_completion
= cvalue
!= 0;
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 );
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
);
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 */
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
);
1318 if (status
!= STATUS_PENDING
)
1320 io
->u
.Status
= status
;
1321 if (async
->apc
|| async
->event
) *apc
= ioctl_apc
;
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
;
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
;
1346 async
->apc_arg
= apc_context
;
1348 SERVER_START_REQ( ioctl
)
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
);
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
);
1375 NtWaitForSingleObject( wait_handle
, (options
& FILE_SYNCHRONOUS_IO_ALERT
), NULL
);
1376 status
= io
->u
.Status
;
1377 NtClose( wait_handle
);
1378 RtlFreeHeap( GetProcessHeap(), 0, async
);
1384 /* Tell Valgrind to ignore any holes in structs we will be passing to the
1386 static void ignore_server_ioctl_struct_holes (ULONG code
, const void *in_buffer
,
1389 #ifdef VALGRIND_MAKE_MEM_DEFINED
1390 # define IGNORE_STRUCT_HOLE(buf, size, t, f1, f2) \
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)); \
1401 case FSCTL_PIPE_WAIT
:
1402 IGNORE_STRUCT_HOLE(in_buffer
, in_size
, FILE_PIPE_WAIT_FOR_BUFFER
, TimeoutSpecified
, Name
);
1409 /**************************************************************************
1410 * NtDeviceIoControlFile [NTDLL.@]
1411 * ZwDeviceIoControlFile [NTDLL.@]
1413 * Perform an I/O control operation on an open file handle.
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
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
);
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
);
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
);
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
);
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
;
1473 /**************************************************************************
1474 * NtFsControlFile [NTDLL.@]
1475 * ZwFsControlFile [NTDLL.@]
1477 * Perform a file system control operation on an open file handle.
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
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
)
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
);
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
);
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
;
1528 if ((status
= server_get_unix_fd( handle
, FILE_READ_DATA
, &fd
, &needs_close
, NULL
, NULL
)))
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();
1540 if (!avail
) /* check for closed pipe */
1542 struct pollfd pollfd
;
1546 pollfd
.events
= POLLIN
;
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
;
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
;
1564 ULONG data_size
= out_size
- FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
);
1567 int res
= recv( fd
, buffer
->Data
, data_size
, MSG_PEEK
);
1568 if (res
>= 0) io
->Information
+= res
;
1571 if (needs_close
) close( fd
);
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
);
1580 int fd
= server_remove_fd_from_cache( handle
);
1581 if (fd
!= -1) close( fd
);
1585 case FSCTL_PIPE_IMPERSONATE
:
1586 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1587 status
= RtlImpersonateSelf( SecurityImpersonation
);
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
;
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
;
1615 io
->Information
= 0;
1616 status
= STATUS_BUFFER_TOO_SMALL
;
1620 case FSCTL_PIPE_LISTEN
:
1621 case FSCTL_PIPE_WAIT
:
1623 status
= server_ioctl_file( handle
, event
, apc
, apc_context
, io
, code
,
1624 in_buffer
, in_size
, out_buffer
, out_size
);
1628 if (status
!= STATUS_PENDING
) io
->u
.Status
= status
;
1632 /******************************************************************************
1633 * NtSetVolumeInformationFile [NTDLL.@]
1634 * ZwSetVolumeInformationFile [NTDLL.@]
1636 * Set volume information for an open file handle.
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
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
,
1654 FS_INFORMATION_CLASS FsInformationClass
)
1656 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1657 FileHandle
,IoStatusBlock
,FsInformation
,Length
,FsInformationClass
);
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];
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;
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;
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;
1727 if (futimes( fd
, tv
) == -1) status
= FILE_GetNtStatus();
1728 #elif defined(HAVE_FUTIMESAT)
1729 if (futimesat( fd
, NULL
, tv
) == -1) status
= FILE_GetNtStatus();
1732 #else /* HAVE_FUTIMES || HAVE_FUTIMESAT */
1733 FIXME( "setting file times not supported\n" );
1734 status
= STATUS_NOT_IMPLEMENTED
;
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;
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;
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;
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;
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;
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 )
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
;
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;
1806 info
->AllocationSize
.QuadPart
= (ULONGLONG
)st
->st_blocks
* 512;
1807 info
->EndOfFile
.QuadPart
= st
->st_size
;
1808 info
->NumberOfLinks
= st
->st_nlink
;
1812 case FileInternalInformation
:
1814 FILE_INTERNAL_INFORMATION
*info
= ptr
;
1815 info
->IndexNumber
.QuadPart
= st
->st_ino
;
1818 case FileEndOfFileInformation
:
1820 FILE_END_OF_FILE_INFORMATION
*info
= ptr
;
1821 info
->EndOfFile
.QuadPart
= S_ISDIR(st
->st_mode
) ? 0 : st
->st_size
;
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
);
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
;
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
;
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
);
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
);
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;
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
;
1902 unix_name
->Buffer
= name
;
1903 unix_name
->Length
= size
;
1906 RtlFreeHeap( GetProcessHeap(), 0, name
);
1907 if (ret
!= STATUS_BUFFER_OVERFLOW
) break;
1912 static NTSTATUS
fill_name_info( const ANSI_STRING
*unix_name
, FILE_NAME_INFORMATION
*info
, LONG
*name_len
)
1914 UNICODE_STRING nt_name
;
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
);
1939 /******************************************************************************
1940 * NtQueryInformationFile [NTDLL.@]
1941 * ZwQueryInformationFile [NTDLL.@]
1943 * Get information about an open file handle.
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
1953 * Success: 0. IoStatusBlock and FileInformation are updated.
1954 * Failure: An NTSTATUS error code describing the error.
1956 NTSTATUS WINAPI
NtQueryInformationFile( HANDLE hFile
, PIO_STATUS_BLOCK io
,
1957 PVOID ptr
, LONG len
, FILE_INFORMATION_CLASS
class )
1959 static const size_t info_sizes
[] =
1962 sizeof(FILE_DIRECTORY_INFORMATION
), /* FileDirectoryInformation */
1963 sizeof(FILE_FULL_DIRECTORY_INFORMATION
), /* FileFullDirectoryInformation */
1964 sizeof(FILE_BOTH_DIRECTORY_INFORMATION
), /* FileBothDirectoryInformation */
1965 sizeof(FILE_BASIC_INFORMATION
), /* FileBasicInformation */
1966 sizeof(FILE_STANDARD_INFORMATION
), /* FileStandardInformation */
1967 sizeof(FILE_INTERNAL_INFORMATION
), /* FileInternalInformation */
1968 sizeof(FILE_EA_INFORMATION
), /* FileEaInformation */
1969 sizeof(FILE_ACCESS_INFORMATION
), /* FileAccessInformation */
1970 sizeof(FILE_NAME_INFORMATION
), /* FileNameInformation */
1971 sizeof(FILE_RENAME_INFORMATION
)-sizeof(WCHAR
), /* FileRenameInformation */
1972 0, /* FileLinkInformation */
1973 sizeof(FILE_NAMES_INFORMATION
)-sizeof(WCHAR
), /* FileNamesInformation */
1974 sizeof(FILE_DISPOSITION_INFORMATION
), /* FileDispositionInformation */
1975 sizeof(FILE_POSITION_INFORMATION
), /* FilePositionInformation */
1976 sizeof(FILE_FULL_EA_INFORMATION
), /* FileFullEaInformation */
1977 sizeof(FILE_MODE_INFORMATION
), /* FileModeInformation */
1978 sizeof(FILE_ALIGNMENT_INFORMATION
), /* FileAlignmentInformation */
1979 sizeof(FILE_ALL_INFORMATION
), /* FileAllInformation */
1980 sizeof(FILE_ALLOCATION_INFORMATION
), /* FileAllocationInformation */
1981 sizeof(FILE_END_OF_FILE_INFORMATION
), /* FileEndOfFileInformation */
1982 0, /* FileAlternateNameInformation */
1983 sizeof(FILE_STREAM_INFORMATION
)-sizeof(WCHAR
), /* FileStreamInformation */
1984 0, /* FilePipeInformation */
1985 sizeof(FILE_PIPE_LOCAL_INFORMATION
), /* FilePipeLocalInformation */
1986 0, /* FilePipeRemoteInformation */
1987 sizeof(FILE_MAILSLOT_QUERY_INFORMATION
), /* FileMailslotQueryInformation */
1988 0, /* FileMailslotSetInformation */
1989 0, /* FileCompressionInformation */
1990 0, /* FileObjectIdInformation */
1991 0, /* FileCompletionInformation */
1992 0, /* FileMoveClusterInformation */
1993 0, /* FileQuotaInformation */
1994 0, /* FileReparsePointInformation */
1995 0, /* FileNetworkOpenInformation */
1996 0, /* FileAttributeTagInformation */
1997 0, /* FileTrackingInformation */
1998 0, /* FileIdBothDirectoryInformation */
1999 0, /* FileIdFullDirectoryInformation */
2000 0, /* FileValidDataLengthInformation */
2001 0, /* FileShortNameInformation */
2005 0, /* FileSfioReserveInformation */
2006 0, /* FileSfioVolumeInformation */
2007 0, /* FileHardLinkInformation */
2009 0, /* FileNormalizedNameInformation */
2011 0, /* FileIdGlobalTxDirectoryInformation */
2015 0 /* FileStandardLinkInformation */
2019 int fd
, needs_close
= FALSE
;
2021 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile
, io
, ptr
, len
, class);
2023 io
->Information
= 0;
2025 if (class <= 0 || class >= FileMaximumInformation
)
2026 return io
->u
.Status
= STATUS_INVALID_INFO_CLASS
;
2027 if (!info_sizes
[class])
2029 FIXME("Unsupported class (%d)\n", class);
2030 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2032 if (len
< info_sizes
[class])
2033 return io
->u
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
2035 if (class != FilePipeLocalInformation
)
2037 if ((io
->u
.Status
= server_get_unix_fd( hFile
, 0, &fd
, &needs_close
, NULL
, NULL
)))
2038 return io
->u
.Status
;
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
;
2049 fill_stat_info( &st
, ptr
, class );
2051 case FileStandardInformation
:
2053 FILE_STANDARD_INFORMATION
*info
= ptr
;
2055 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
2058 fill_stat_info( &st
, info
, class );
2059 info
->DeletePending
= FALSE
; /* FIXME */
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
;
2071 case FileInternalInformation
:
2072 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
2073 else fill_stat_info( &st
, ptr
, class );
2075 case FileEaInformation
:
2077 FILE_EA_INFORMATION
*info
= ptr
;
2081 case FileEndOfFileInformation
:
2082 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
2083 else fill_stat_info( &st
, ptr
, class );
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
;
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
);
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
;
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
);
2149 case FilePipeLocalInformation
:
2151 FILE_PIPE_LOCAL_INFORMATION
* pli
= ptr
;
2153 SERVER_START_REQ( get_named_pipe_info
)
2155 req
->handle
= wine_server_obj_handle( hFile
);
2156 if (!(io
->u
.Status
= wine_server_call( req
)))
2158 pli
->NamedPipeType
= (reply
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) ?
2159 FILE_PIPE_TYPE_MESSAGE
: FILE_PIPE_TYPE_BYTE
;
2160 switch (reply
->sharing
)
2162 case FILE_SHARE_READ
:
2163 pli
->NamedPipeConfiguration
= FILE_PIPE_OUTBOUND
;
2165 case FILE_SHARE_WRITE
:
2166 pli
->NamedPipeConfiguration
= FILE_PIPE_INBOUND
;
2168 case FILE_SHARE_READ
| FILE_SHARE_WRITE
:
2169 pli
->NamedPipeConfiguration
= FILE_PIPE_FULL_DUPLEX
;
2172 pli
->MaximumInstances
= reply
->maxinstances
;
2173 pli
->CurrentInstances
= reply
->instances
;
2174 pli
->InboundQuota
= reply
->insize
;
2175 pli
->ReadDataAvailable
= 0; /* FIXME */
2176 pli
->OutboundQuota
= reply
->outsize
;
2177 pli
->WriteQuotaAvailable
= 0; /* FIXME */
2178 pli
->NamedPipeState
= 0; /* FIXME */
2179 pli
->NamedPipeEnd
= (reply
->flags
& NAMED_PIPE_SERVER_END
) ?
2180 FILE_PIPE_SERVER_END
: FILE_PIPE_CLIENT_END
;
2186 case FileNameInformation
:
2188 FILE_NAME_INFORMATION
*info
= ptr
;
2189 ANSI_STRING unix_name
;
2191 if (!(io
->u
.Status
= server_get_unix_name( hFile
, &unix_name
)))
2193 LONG name_len
= len
- FIELD_OFFSET(FILE_NAME_INFORMATION
, FileName
);
2194 io
->u
.Status
= fill_name_info( &unix_name
, info
, &name_len
);
2195 RtlFreeAnsiString( &unix_name
);
2196 io
->Information
= FIELD_OFFSET(FILE_NAME_INFORMATION
, FileName
) + name_len
;
2201 FIXME("Unsupported class (%d)\n", class);
2202 io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2205 if (needs_close
) close( fd
);
2206 if (io
->u
.Status
== STATUS_SUCCESS
&& !io
->Information
) io
->Information
= info_sizes
[class];
2207 return io
->u
.Status
;
2210 /******************************************************************************
2211 * NtSetInformationFile [NTDLL.@]
2212 * ZwSetInformationFile [NTDLL.@]
2214 * Set information about an open file handle.
2217 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2218 * io [O] Receives information about the operation on return
2219 * ptr [I] Source for file information
2220 * len [I] Size of FileInformation
2221 * class [I] Type of file information to set
2224 * Success: 0. io is updated.
2225 * Failure: An NTSTATUS error code describing the error.
2227 NTSTATUS WINAPI
NtSetInformationFile(HANDLE handle
, PIO_STATUS_BLOCK io
,
2228 PVOID ptr
, ULONG len
, FILE_INFORMATION_CLASS
class)
2230 int fd
, needs_close
;
2232 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle
, io
, ptr
, len
, class);
2234 io
->u
.Status
= STATUS_SUCCESS
;
2237 case FileBasicInformation
:
2238 if (len
>= sizeof(FILE_BASIC_INFORMATION
))
2241 const FILE_BASIC_INFORMATION
*info
= ptr
;
2243 if ((io
->u
.Status
= server_get_unix_fd( handle
, 0, &fd
, &needs_close
, NULL
, NULL
)))
2244 return io
->u
.Status
;
2246 if (info
->LastAccessTime
.QuadPart
|| info
->LastWriteTime
.QuadPart
)
2247 io
->u
.Status
= set_file_times( fd
, &info
->LastWriteTime
, &info
->LastAccessTime
);
2249 if (io
->u
.Status
== STATUS_SUCCESS
&& info
->FileAttributes
)
2251 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
2254 if (info
->FileAttributes
& FILE_ATTRIBUTE_READONLY
)
2256 if (S_ISDIR( st
.st_mode
))
2257 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2259 st
.st_mode
&= ~0222; /* clear write permission bits */
2263 /* add write permission only where we already have read permission */
2264 st
.st_mode
|= (0600 | ((st
.st_mode
& 044) >> 1)) & (~FILE_umask
);
2266 if (fchmod( fd
, st
.st_mode
) == -1) io
->u
.Status
= FILE_GetNtStatus();
2270 if (needs_close
) close( fd
);
2272 else io
->u
.Status
= STATUS_INVALID_PARAMETER_3
;
2275 case FilePositionInformation
:
2276 if (len
>= sizeof(FILE_POSITION_INFORMATION
))
2278 const FILE_POSITION_INFORMATION
*info
= ptr
;
2280 if ((io
->u
.Status
= server_get_unix_fd( handle
, 0, &fd
, &needs_close
, NULL
, NULL
)))
2281 return io
->u
.Status
;
2283 if (lseek( fd
, info
->CurrentByteOffset
.QuadPart
, SEEK_SET
) == (off_t
)-1)
2284 io
->u
.Status
= FILE_GetNtStatus();
2286 if (needs_close
) close( fd
);
2288 else io
->u
.Status
= STATUS_INVALID_PARAMETER_3
;
2291 case FileEndOfFileInformation
:
2292 if (len
>= sizeof(FILE_END_OF_FILE_INFORMATION
))
2295 const FILE_END_OF_FILE_INFORMATION
*info
= ptr
;
2297 if ((io
->u
.Status
= server_get_unix_fd( handle
, 0, &fd
, &needs_close
, NULL
, NULL
)))
2298 return io
->u
.Status
;
2300 /* first try normal truncate */
2301 if (ftruncate( fd
, (off_t
)info
->EndOfFile
.QuadPart
) != -1) break;
2303 /* now check for the need to extend the file */
2304 if (fstat( fd
, &st
) != -1 && (off_t
)info
->EndOfFile
.QuadPart
> st
.st_size
)
2306 static const char zero
;
2308 /* extend the file one byte beyond the requested size and then truncate it */
2309 /* this should work around ftruncate implementations that can't extend files */
2310 if (pwrite( fd
, &zero
, 1, (off_t
)info
->EndOfFile
.QuadPart
) != -1 &&
2311 ftruncate( fd
, (off_t
)info
->EndOfFile
.QuadPart
) != -1) break;
2313 io
->u
.Status
= FILE_GetNtStatus();
2315 if (needs_close
) close( fd
);
2317 else io
->u
.Status
= STATUS_INVALID_PARAMETER_3
;
2320 case FileMailslotSetInformation
:
2322 FILE_MAILSLOT_SET_INFORMATION
*info
= ptr
;
2324 SERVER_START_REQ( set_mailslot_info
)
2326 req
->handle
= wine_server_obj_handle( handle
);
2327 req
->flags
= MAILSLOT_SET_READ_TIMEOUT
;
2328 req
->read_timeout
= info
->ReadTimeout
.QuadPart
;
2329 io
->u
.Status
= wine_server_call( req
);
2335 case FileCompletionInformation
:
2336 if (len
>= sizeof(FILE_COMPLETION_INFORMATION
))
2338 FILE_COMPLETION_INFORMATION
*info
= ptr
;
2340 SERVER_START_REQ( set_completion_info
)
2342 req
->handle
= wine_server_obj_handle( handle
);
2343 req
->chandle
= wine_server_obj_handle( info
->CompletionPort
);
2344 req
->ckey
= info
->CompletionKey
;
2345 io
->u
.Status
= wine_server_call( req
);
2349 io
->u
.Status
= STATUS_INVALID_PARAMETER_3
;
2352 case FileAllInformation
:
2353 io
->u
.Status
= STATUS_INVALID_INFO_CLASS
;
2356 case FileValidDataLengthInformation
:
2357 if (len
>= sizeof(FILE_VALID_DATA_LENGTH_INFORMATION
))
2360 const FILE_VALID_DATA_LENGTH_INFORMATION
*info
= ptr
;
2362 if ((io
->u
.Status
= server_get_unix_fd( handle
, FILE_WRITE_DATA
, &fd
, &needs_close
, NULL
, NULL
)))
2363 return io
->u
.Status
;
2365 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
2366 else if (info
->ValidDataLength
.QuadPart
<= 0 || (off_t
)info
->ValidDataLength
.QuadPart
> st
.st_size
)
2367 io
->u
.Status
= STATUS_INVALID_PARAMETER
;
2370 #ifdef HAVE_FALLOCATE
2371 if (fallocate( fd
, 0, 0, (off_t
)info
->ValidDataLength
.QuadPart
) == -1)
2373 NTSTATUS status
= FILE_GetNtStatus();
2374 if (status
== STATUS_NOT_SUPPORTED
) WARN( "fallocate not supported on this filesystem\n" );
2375 else io
->u
.Status
= status
;
2378 FIXME( "setting valid data length not supported\n" );
2381 if (needs_close
) close( fd
);
2383 else io
->u
.Status
= STATUS_INVALID_PARAMETER_3
;
2387 FIXME("Unsupported class (%d)\n", class);
2388 io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2391 io
->Information
= 0;
2392 return io
->u
.Status
;
2396 /******************************************************************************
2397 * NtQueryFullAttributesFile (NTDLL.@)
2399 NTSTATUS WINAPI
NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES
*attr
,
2400 FILE_NETWORK_OPEN_INFORMATION
*info
)
2402 ANSI_STRING unix_name
;
2405 if (!(status
= nt_to_unix_file_name_attr( attr
, &unix_name
, FILE_OPEN
)))
2409 if (stat( unix_name
.Buffer
, &st
) == -1)
2410 status
= FILE_GetNtStatus();
2411 else if (!S_ISREG(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
2412 status
= STATUS_INVALID_INFO_CLASS
;
2415 FILE_BASIC_INFORMATION basic
;
2416 FILE_STANDARD_INFORMATION std
;
2418 fill_stat_info( &st
, &basic
, FileBasicInformation
);
2419 fill_stat_info( &st
, &std
, FileStandardInformation
);
2421 info
->CreationTime
= basic
.CreationTime
;
2422 info
->LastAccessTime
= basic
.LastAccessTime
;
2423 info
->LastWriteTime
= basic
.LastWriteTime
;
2424 info
->ChangeTime
= basic
.ChangeTime
;
2425 info
->AllocationSize
= std
.AllocationSize
;
2426 info
->EndOfFile
= std
.EndOfFile
;
2427 info
->FileAttributes
= basic
.FileAttributes
;
2428 if (DIR_is_hidden_file( attr
->ObjectName
))
2429 info
->FileAttributes
|= FILE_ATTRIBUTE_HIDDEN
;
2431 RtlFreeAnsiString( &unix_name
);
2433 else WARN("%s not found (%x)\n", debugstr_us(attr
->ObjectName
), status
);
2438 /******************************************************************************
2439 * NtQueryAttributesFile (NTDLL.@)
2440 * ZwQueryAttributesFile (NTDLL.@)
2442 NTSTATUS WINAPI
NtQueryAttributesFile( const OBJECT_ATTRIBUTES
*attr
, FILE_BASIC_INFORMATION
*info
)
2444 ANSI_STRING unix_name
;
2447 if (!(status
= nt_to_unix_file_name_attr( attr
, &unix_name
, FILE_OPEN
)))
2451 if (stat( unix_name
.Buffer
, &st
) == -1)
2452 status
= FILE_GetNtStatus();
2453 else if (!S_ISREG(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
2454 status
= STATUS_INVALID_INFO_CLASS
;
2457 status
= fill_stat_info( &st
, info
, FileBasicInformation
);
2458 if (DIR_is_hidden_file( attr
->ObjectName
))
2459 info
->FileAttributes
|= FILE_ATTRIBUTE_HIDDEN
;
2461 RtlFreeAnsiString( &unix_name
);
2463 else WARN("%s not found (%x)\n", debugstr_us(attr
->ObjectName
), status
);
2468 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2469 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2470 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION
*info
, const char *fstypename
,
2471 unsigned int flags
)
2473 if (!strcmp("cd9660", fstypename
) || !strcmp("udf", fstypename
))
2475 info
->DeviceType
= FILE_DEVICE_CD_ROM_FILE_SYSTEM
;
2476 /* Don't assume read-only, let the mount options set it below */
2477 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
;
2479 else if (!strcmp("nfs", fstypename
) || !strcmp("nwfs", fstypename
) ||
2480 !strcmp("smbfs", fstypename
) || !strcmp("afpfs", fstypename
))
2482 info
->DeviceType
= FILE_DEVICE_NETWORK_FILE_SYSTEM
;
2483 info
->Characteristics
|= FILE_REMOTE_DEVICE
;
2485 else if (!strcmp("procfs", fstypename
))
2486 info
->DeviceType
= FILE_DEVICE_VIRTUAL_DISK
;
2488 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2490 if (flags
& MNT_RDONLY
)
2491 info
->Characteristics
|= FILE_READ_ONLY_DEVICE
;
2493 if (!(flags
& MNT_LOCAL
))
2495 info
->DeviceType
= FILE_DEVICE_NETWORK_FILE_SYSTEM
;
2496 info
->Characteristics
|= FILE_REMOTE_DEVICE
;
2501 static inline BOOL
is_device_placeholder( int fd
)
2503 static const char wine_placeholder
[] = "Wine device placeholder";
2504 char buffer
[sizeof(wine_placeholder
)-1];
2506 if (pread( fd
, buffer
, sizeof(wine_placeholder
) - 1, 0 ) != sizeof(wine_placeholder
) - 1)
2508 return !memcmp( buffer
, wine_placeholder
, sizeof(wine_placeholder
) - 1 );
2511 /******************************************************************************
2514 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2516 static NTSTATUS
get_device_info( int fd
, FILE_FS_DEVICE_INFORMATION
*info
)
2520 info
->Characteristics
= 0;
2521 if (fstat( fd
, &st
) < 0) return FILE_GetNtStatus();
2522 if (S_ISCHR( st
.st_mode
))
2524 info
->DeviceType
= FILE_DEVICE_UNKNOWN
;
2526 switch(major(st
.st_rdev
))
2529 info
->DeviceType
= FILE_DEVICE_NULL
;
2532 info
->DeviceType
= FILE_DEVICE_SERIAL_PORT
;
2535 info
->DeviceType
= FILE_DEVICE_PARALLEL_PORT
;
2537 case SCSI_TAPE_MAJOR
:
2538 info
->DeviceType
= FILE_DEVICE_TAPE
;
2543 else if (S_ISBLK( st
.st_mode
))
2545 info
->DeviceType
= FILE_DEVICE_DISK
;
2547 else if (S_ISFIFO( st
.st_mode
) || S_ISSOCK( st
.st_mode
))
2549 info
->DeviceType
= FILE_DEVICE_NAMED_PIPE
;
2551 else if (is_device_placeholder( fd
))
2553 info
->DeviceType
= FILE_DEVICE_DISK
;
2555 else /* regular file or directory */
2557 #if defined(linux) && defined(HAVE_FSTATFS)
2560 /* check for floppy disk */
2561 if (major(st
.st_dev
) == FLOPPY_MAJOR
)
2562 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
;
2564 if (fstatfs( fd
, &stfs
) < 0) stfs
.f_type
= 0;
2565 switch (stfs
.f_type
)
2567 case 0x9660: /* iso9660 */
2568 case 0x9fa1: /* supermount */
2569 case 0x15013346: /* udf */
2570 info
->DeviceType
= FILE_DEVICE_CD_ROM_FILE_SYSTEM
;
2571 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
|FILE_READ_ONLY_DEVICE
;
2573 case 0x6969: /* nfs */
2574 case 0x517B: /* smbfs */
2575 case 0x564c: /* ncpfs */
2576 info
->DeviceType
= FILE_DEVICE_NETWORK_FILE_SYSTEM
;
2577 info
->Characteristics
|= FILE_REMOTE_DEVICE
;
2579 case 0x01021994: /* tmpfs */
2580 case 0x28cd3d45: /* cramfs */
2581 case 0x1373: /* devfs */
2582 case 0x9fa0: /* procfs */
2583 info
->DeviceType
= FILE_DEVICE_VIRTUAL_DISK
;
2586 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2589 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
2592 if (fstatfs( fd
, &stfs
) < 0)
2593 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2595 get_device_info_fstatfs( info
, stfs
.f_fstypename
, stfs
.f_flags
);
2596 #elif defined(__NetBSD__)
2597 struct statvfs stfs
;
2599 if (fstatvfs( fd
, &stfs
) < 0)
2600 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2602 get_device_info_fstatfs( info
, stfs
.f_fstypename
, stfs
.f_flag
);
2604 /* Use dkio to work out device types */
2606 # include <sys/dkio.h>
2607 # include <sys/vtoc.h>
2608 struct dk_cinfo dkinf
;
2609 int retval
= ioctl(fd
, DKIOCINFO
, &dkinf
);
2611 WARN("Unable to get disk device type information - assuming a disk like device\n");
2612 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2614 switch (dkinf
.dki_ctype
)
2617 info
->DeviceType
= FILE_DEVICE_CD_ROM_FILE_SYSTEM
;
2618 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
|FILE_READ_ONLY_DEVICE
;
2622 case DKC_INTEL82072
:
2623 case DKC_INTEL82077
:
2624 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2625 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
;
2628 info
->DeviceType
= FILE_DEVICE_VIRTUAL_DISK
;
2631 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2636 if (!warned
++) FIXME( "device info not properly supported on this platform\n" );
2637 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2639 info
->Characteristics
|= FILE_DEVICE_IS_MOUNTED
;
2641 return STATUS_SUCCESS
;
2645 /******************************************************************************
2646 * NtQueryVolumeInformationFile [NTDLL.@]
2647 * ZwQueryVolumeInformationFile [NTDLL.@]
2649 * Get volume information for an open file handle.
2652 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2653 * io [O] Receives information about the operation on return
2654 * buffer [O] Destination for volume information
2655 * length [I] Size of FsInformation
2656 * info_class [I] Type of volume information to set
2659 * Success: 0. io and buffer are updated.
2660 * Failure: An NTSTATUS error code describing the error.
2662 NTSTATUS WINAPI
NtQueryVolumeInformationFile( HANDLE handle
, PIO_STATUS_BLOCK io
,
2663 PVOID buffer
, ULONG length
,
2664 FS_INFORMATION_CLASS info_class
)
2666 int fd
, needs_close
;
2670 if ((io
->u
.Status
= server_get_unix_fd( handle
, 0, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
2671 return io
->u
.Status
;
2673 io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2674 io
->Information
= 0;
2676 switch( info_class
)
2678 case FileFsVolumeInformation
:
2679 if (!once
++) FIXME( "%p: volume info not supported\n", handle
);
2681 case FileFsLabelInformation
:
2682 FIXME( "%p: label info not supported\n", handle
);
2684 case FileFsSizeInformation
:
2685 if (length
< sizeof(FILE_FS_SIZE_INFORMATION
))
2686 io
->u
.Status
= STATUS_BUFFER_TOO_SMALL
;
2689 FILE_FS_SIZE_INFORMATION
*info
= buffer
;
2691 if (fstat( fd
, &st
) < 0)
2693 io
->u
.Status
= FILE_GetNtStatus();
2696 if (!S_ISREG(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
2698 io
->u
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
2703 /* Linux's fstatvfs is buggy */
2704 #if !defined(linux) || !defined(HAVE_FSTATFS)
2705 struct statvfs stfs
;
2707 if (fstatvfs( fd
, &stfs
) < 0)
2709 io
->u
.Status
= FILE_GetNtStatus();
2712 bsize
= stfs
.f_frsize
;
2715 if (fstatfs( fd
, &stfs
) < 0)
2717 io
->u
.Status
= FILE_GetNtStatus();
2720 bsize
= stfs
.f_bsize
;
2722 if (bsize
== 2048) /* assume CD-ROM */
2724 info
->BytesPerSector
= 2048;
2725 info
->SectorsPerAllocationUnit
= 1;
2729 info
->BytesPerSector
= 512;
2730 info
->SectorsPerAllocationUnit
= 8;
2732 info
->TotalAllocationUnits
.QuadPart
= bsize
* stfs
.f_blocks
/ (info
->BytesPerSector
* info
->SectorsPerAllocationUnit
);
2733 info
->AvailableAllocationUnits
.QuadPart
= bsize
* stfs
.f_bavail
/ (info
->BytesPerSector
* info
->SectorsPerAllocationUnit
);
2734 io
->Information
= sizeof(*info
);
2735 io
->u
.Status
= STATUS_SUCCESS
;
2739 case FileFsDeviceInformation
:
2740 if (length
< sizeof(FILE_FS_DEVICE_INFORMATION
))
2741 io
->u
.Status
= STATUS_BUFFER_TOO_SMALL
;
2744 FILE_FS_DEVICE_INFORMATION
*info
= buffer
;
2746 if ((io
->u
.Status
= get_device_info( fd
, info
)) == STATUS_SUCCESS
)
2747 io
->Information
= sizeof(*info
);
2750 case FileFsAttributeInformation
:
2751 if (length
< offsetof( FILE_FS_ATTRIBUTE_INFORMATION
, FileSystemName
[sizeof(ntfsW
)/sizeof(WCHAR
)] ))
2752 io
->u
.Status
= STATUS_BUFFER_TOO_SMALL
;
2755 FILE_FS_ATTRIBUTE_INFORMATION
*info
= buffer
;
2757 FIXME( "%p: faking attribute info\n", handle
);
2758 info
->FileSystemAttribute
= FILE_SUPPORTS_ENCRYPTION
| FILE_FILE_COMPRESSION
|
2759 FILE_PERSISTENT_ACLS
| FILE_UNICODE_ON_DISK
|
2760 FILE_CASE_PRESERVED_NAMES
| FILE_CASE_SENSITIVE_SEARCH
;
2761 info
->MaximumComponentNameLength
= MAXIMUM_FILENAME_LENGTH
- 1;
2762 info
->FileSystemNameLength
= sizeof(ntfsW
);
2763 memcpy(info
->FileSystemName
, ntfsW
, sizeof(ntfsW
));
2765 io
->Information
= sizeof(*info
);
2766 io
->u
.Status
= STATUS_SUCCESS
;
2769 case FileFsControlInformation
:
2770 FIXME( "%p: control info not supported\n", handle
);
2772 case FileFsFullSizeInformation
:
2773 FIXME( "%p: full size info not supported\n", handle
);
2775 case FileFsObjectIdInformation
:
2776 FIXME( "%p: object id info not supported\n", handle
);
2778 case FileFsMaximumInformation
:
2779 FIXME( "%p: maximum info not supported\n", handle
);
2782 io
->u
.Status
= STATUS_INVALID_PARAMETER
;
2785 if (needs_close
) close( fd
);
2786 return io
->u
.Status
;
2790 /******************************************************************
2791 * NtQueryEaFile (NTDLL.@)
2793 * Read extended attributes from NTFS files.
2796 * hFile [I] File handle, must be opened with FILE_READ_EA access
2797 * iosb [O] Receives information about the operation on return
2798 * buffer [O] Output buffer
2799 * length [I] Length of output buffer
2800 * single_entry [I] Only read and return one entry
2801 * ea_list [I] Optional list with names of EAs to return
2802 * ea_list_len [I] Length of ea_list in bytes
2803 * ea_index [I] Optional pointer to 1-based index of attribute to return
2804 * restart [I] restart EA scan
2807 * Success: 0. Atrributes read into buffer
2808 * Failure: An NTSTATUS error code describing the error.
2810 NTSTATUS WINAPI
NtQueryEaFile( HANDLE hFile
, PIO_STATUS_BLOCK iosb
, PVOID buffer
, ULONG length
,
2811 BOOLEAN single_entry
, PVOID ea_list
, ULONG ea_list_len
,
2812 PULONG ea_index
, BOOLEAN restart
)
2814 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
2815 hFile
, iosb
, buffer
, length
, single_entry
, ea_list
,
2816 ea_list_len
, ea_index
, restart
);
2817 return STATUS_ACCESS_DENIED
;
2821 /******************************************************************
2822 * NtSetEaFile (NTDLL.@)
2824 * Update extended attributes for NTFS files.
2827 * hFile [I] File handle, must be opened with FILE_READ_EA access
2828 * iosb [O] Receives information about the operation on return
2829 * buffer [I] Buffer with EA information
2830 * length [I] Length of buffer
2833 * Success: 0. Attributes are updated
2834 * Failure: An NTSTATUS error code describing the error.
2836 NTSTATUS WINAPI
NtSetEaFile( HANDLE hFile
, PIO_STATUS_BLOCK iosb
, PVOID buffer
, ULONG length
)
2838 FIXME("(%p,%p,%p,%d) stub\n", hFile
, iosb
, buffer
, length
);
2839 return STATUS_ACCESS_DENIED
;
2843 /******************************************************************
2844 * NtFlushBuffersFile (NTDLL.@)
2846 * Flush any buffered data on an open file handle.
2849 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2850 * IoStatusBlock [O] Receives information about the operation on return
2853 * Success: 0. IoStatusBlock is updated.
2854 * Failure: An NTSTATUS error code describing the error.
2856 NTSTATUS WINAPI
NtFlushBuffersFile( HANDLE hFile
, IO_STATUS_BLOCK
* IoStatusBlock
)
2859 HANDLE hEvent
= NULL
;
2860 enum server_fd_type type
;
2861 int fd
, needs_close
;
2863 ret
= server_get_unix_fd( hFile
, FILE_WRITE_DATA
, &fd
, &needs_close
, &type
, NULL
);
2865 if (!ret
&& type
== FD_TYPE_SERIAL
)
2867 ret
= COMM_FlushBuffersFile( fd
);
2871 SERVER_START_REQ( flush_file
)
2873 req
->handle
= wine_server_obj_handle( hFile
);
2874 ret
= wine_server_call( req
);
2875 hEvent
= wine_server_ptr_handle( reply
->event
);
2880 ret
= NtWaitForSingleObject( hEvent
, FALSE
, NULL
);
2885 if (needs_close
) close( fd
);
2889 /******************************************************************
2890 * NtLockFile (NTDLL.@)
2894 NTSTATUS WINAPI
NtLockFile( HANDLE hFile
, HANDLE lock_granted_event
,
2895 PIO_APC_ROUTINE apc
, void* apc_user
,
2896 PIO_STATUS_BLOCK io_status
, PLARGE_INTEGER offset
,
2897 PLARGE_INTEGER count
, ULONG
* key
, BOOLEAN dont_wait
,
2903 static BOOLEAN warn
= TRUE
;
2905 if (apc
|| io_status
|| key
)
2907 FIXME("Unimplemented yet parameter\n");
2908 return STATUS_NOT_IMPLEMENTED
;
2911 if (apc_user
&& warn
)
2913 FIXME("I/O completion on lock not implemented yet\n");
2919 SERVER_START_REQ( lock_file
)
2921 req
->handle
= wine_server_obj_handle( hFile
);
2922 req
->offset
= offset
->QuadPart
;
2923 req
->count
= count
->QuadPart
;
2924 req
->shared
= !exclusive
;
2925 req
->wait
= !dont_wait
;
2926 ret
= wine_server_call( req
);
2927 handle
= wine_server_ptr_handle( reply
->handle
);
2928 async
= reply
->overlapped
;
2931 if (ret
!= STATUS_PENDING
)
2933 if (!ret
&& lock_granted_event
) NtSetEvent(lock_granted_event
, NULL
);
2939 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2940 if (handle
) NtClose( handle
);
2941 return STATUS_PENDING
;
2945 NtWaitForSingleObject( handle
, FALSE
, NULL
);
2952 /* Unix lock conflict, sleep a bit and retry */
2953 time
.QuadPart
= 100 * (ULONGLONG
)10000;
2954 time
.QuadPart
= -time
.QuadPart
;
2955 NtDelayExecution( FALSE
, &time
);
2961 /******************************************************************
2962 * NtUnlockFile (NTDLL.@)
2966 NTSTATUS WINAPI
NtUnlockFile( HANDLE hFile
, PIO_STATUS_BLOCK io_status
,
2967 PLARGE_INTEGER offset
, PLARGE_INTEGER count
,
2972 TRACE( "%p %x%08x %x%08x\n",
2973 hFile
, offset
->u
.HighPart
, offset
->u
.LowPart
, count
->u
.HighPart
, count
->u
.LowPart
);
2975 if (io_status
|| key
)
2977 FIXME("Unimplemented yet parameter\n");
2978 return STATUS_NOT_IMPLEMENTED
;
2981 SERVER_START_REQ( unlock_file
)
2983 req
->handle
= wine_server_obj_handle( hFile
);
2984 req
->offset
= offset
->QuadPart
;
2985 req
->count
= count
->QuadPart
;
2986 status
= wine_server_call( req
);
2992 /******************************************************************
2993 * NtCreateNamedPipeFile (NTDLL.@)
2997 NTSTATUS WINAPI
NtCreateNamedPipeFile( PHANDLE handle
, ULONG access
,
2998 POBJECT_ATTRIBUTES attr
, PIO_STATUS_BLOCK iosb
,
2999 ULONG sharing
, ULONG dispo
, ULONG options
,
3000 ULONG pipe_type
, ULONG read_mode
,
3001 ULONG completion_mode
, ULONG max_inst
,
3002 ULONG inbound_quota
, ULONG outbound_quota
,
3003 PLARGE_INTEGER timeout
)
3007 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
3008 handle
, access
, debugstr_w(attr
->ObjectName
->Buffer
), iosb
, sharing
, dispo
,
3009 options
, pipe_type
, read_mode
, completion_mode
, max_inst
, inbound_quota
,
3010 outbound_quota
, timeout
);
3012 /* assume we only get relative timeout */
3013 if (timeout
->QuadPart
> 0)
3014 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout
->QuadPart
));
3016 SERVER_START_REQ( create_named_pipe
)
3018 req
->access
= access
;
3019 req
->attributes
= attr
->Attributes
;
3020 req
->rootdir
= wine_server_obj_handle( attr
->RootDirectory
);
3021 req
->options
= options
;
3022 req
->sharing
= sharing
;
3024 (pipe_type
? NAMED_PIPE_MESSAGE_STREAM_WRITE
: 0) |
3025 (read_mode
? NAMED_PIPE_MESSAGE_STREAM_READ
: 0) |
3026 (completion_mode
? NAMED_PIPE_NONBLOCKING_MODE
: 0);
3027 req
->maxinstances
= max_inst
;
3028 req
->outsize
= outbound_quota
;
3029 req
->insize
= inbound_quota
;
3030 req
->timeout
= timeout
->QuadPart
;
3031 wine_server_add_data( req
, attr
->ObjectName
->Buffer
,
3032 attr
->ObjectName
->Length
);
3033 status
= wine_server_call( req
);
3034 if (!status
) *handle
= wine_server_ptr_handle( reply
->handle
);
3040 /******************************************************************
3041 * NtDeleteFile (NTDLL.@)
3045 NTSTATUS WINAPI
NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes
)
3051 TRACE("%p\n", ObjectAttributes
);
3052 status
= NtCreateFile( &hFile
, GENERIC_READ
| GENERIC_WRITE
| DELETE
,
3053 ObjectAttributes
, &io
, NULL
, 0,
3054 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
3055 FILE_OPEN
, FILE_DELETE_ON_CLOSE
, NULL
, 0 );
3056 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
3060 /******************************************************************
3061 * NtCancelIoFileEx (NTDLL.@)
3065 NTSTATUS WINAPI
NtCancelIoFileEx( HANDLE hFile
, PIO_STATUS_BLOCK iosb
, PIO_STATUS_BLOCK io_status
)
3067 LARGE_INTEGER timeout
;
3069 TRACE("%p %p %p\n", hFile
, iosb
, io_status
);
3071 SERVER_START_REQ( cancel_async
)
3073 req
->handle
= wine_server_obj_handle( hFile
);
3074 req
->iosb
= wine_server_client_ptr( iosb
);
3075 req
->only_thread
= FALSE
;
3076 io_status
->u
.Status
= wine_server_call( req
);
3079 if (io_status
->u
.Status
)
3080 return io_status
->u
.Status
;
3082 /* Let some APC be run, so that we can run the remaining APCs on hFile
3083 * either the cancelation of the pending one, but also the execution
3084 * of the queued APC, but not yet run. This is needed to ensure proper
3085 * clean-up of allocated data.
3087 timeout
.QuadPart
= 0;
3088 NtDelayExecution( TRUE
, &timeout
);
3089 return io_status
->u
.Status
;
3092 /******************************************************************
3093 * NtCancelIoFile (NTDLL.@)
3097 NTSTATUS WINAPI
NtCancelIoFile( HANDLE hFile
, PIO_STATUS_BLOCK io_status
)
3099 LARGE_INTEGER timeout
;
3101 TRACE("%p %p\n", hFile
, io_status
);
3103 SERVER_START_REQ( cancel_async
)
3105 req
->handle
= wine_server_obj_handle( hFile
);
3107 req
->only_thread
= TRUE
;
3108 io_status
->u
.Status
= wine_server_call( req
);
3111 if (io_status
->u
.Status
)
3112 return io_status
->u
.Status
;
3114 /* Let some APC be run, so that we can run the remaining APCs on hFile
3115 * either the cancelation of the pending one, but also the execution
3116 * of the queued APC, but not yet run. This is needed to ensure proper
3117 * clean-up of allocated data.
3119 timeout
.QuadPart
= 0;
3120 NtDelayExecution( TRUE
, &timeout
);
3121 return io_status
->u
.Status
;
3124 /******************************************************************************
3125 * NtCreateMailslotFile [NTDLL.@]
3126 * ZwCreateMailslotFile [NTDLL.@]
3129 * pHandle [O] pointer to receive the handle created
3130 * DesiredAccess [I] access mode (read, write, etc)
3131 * ObjectAttributes [I] fully qualified NT path of the mailslot
3132 * IoStatusBlock [O] receives completion status and other info
3135 * MaxMessageSize [I]
3141 NTSTATUS WINAPI
NtCreateMailslotFile(PHANDLE pHandle
, ULONG DesiredAccess
,
3142 POBJECT_ATTRIBUTES attr
, PIO_STATUS_BLOCK IoStatusBlock
,
3143 ULONG CreateOptions
, ULONG MailslotQuota
, ULONG MaxMessageSize
,
3144 PLARGE_INTEGER TimeOut
)
3146 LARGE_INTEGER timeout
;
3149 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
3150 pHandle
, DesiredAccess
, attr
, IoStatusBlock
,
3151 CreateOptions
, MailslotQuota
, MaxMessageSize
, TimeOut
);
3153 if (!pHandle
) return STATUS_ACCESS_VIOLATION
;
3154 if (!attr
) return STATUS_INVALID_PARAMETER
;
3155 if (!attr
->ObjectName
) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
3158 * For a NULL TimeOut pointer set the default timeout value
3161 timeout
.QuadPart
= -1;
3163 timeout
.QuadPart
= TimeOut
->QuadPart
;
3165 SERVER_START_REQ( create_mailslot
)
3167 req
->access
= DesiredAccess
;
3168 req
->attributes
= attr
->Attributes
;
3169 req
->rootdir
= wine_server_obj_handle( attr
->RootDirectory
);
3170 req
->max_msgsize
= MaxMessageSize
;
3171 req
->read_timeout
= timeout
.QuadPart
;
3172 wine_server_add_data( req
, attr
->ObjectName
->Buffer
,
3173 attr
->ObjectName
->Length
);
3174 ret
= wine_server_call( req
);
3175 if( ret
== STATUS_SUCCESS
)
3176 *pHandle
= wine_server_ptr_handle( reply
->handle
);