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