2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
33 #ifdef HAVE_LINUX_MAJOR_H
34 # include <linux/major.h>
36 #ifdef HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
54 #ifdef HAVE_SYS_POLL_H
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
66 #ifdef HAVE_SYS_MOUNT_H
67 # include <sys/mount.h>
69 #ifdef HAVE_SYS_STATFS_H
70 # include <sys/statfs.h>
73 #define NONAMELESSUNION
74 #define NONAMELESSSTRUCT
76 #define WIN32_NO_STATUS
77 #include "wine/unicode.h"
78 #include "wine/debug.h"
79 #include "wine/server.h"
80 #include "ntdll_misc.h"
84 #include "ddk/ntddser.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
88 mode_t FILE_umask
= 0;
90 #define SECSPERDAY 86400
91 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
93 /**************************************************************************
94 * NtOpenFile [NTDLL.@]
95 * ZwOpenFile [NTDLL.@]
100 * handle [O] Variable that receives the file handle on return
101 * access [I] Access desired by the caller to the file
102 * attr [I] Structure describing the file to be opened
103 * io [O] Receives details about the result of the operation
104 * sharing [I] Type of shared access the caller requires
105 * options [I] Options for the file open
108 * Success: 0. FileHandle and IoStatusBlock are updated.
109 * Failure: An NTSTATUS error code describing the error.
111 NTSTATUS WINAPI
NtOpenFile( PHANDLE handle
, ACCESS_MASK access
,
112 POBJECT_ATTRIBUTES attr
, PIO_STATUS_BLOCK io
,
113 ULONG sharing
, ULONG options
)
115 return NtCreateFile( handle
, access
, attr
, io
, NULL
, 0,
116 sharing
, FILE_OPEN
, options
, NULL
, 0 );
119 /**************************************************************************
120 * NtCreateFile [NTDLL.@]
121 * ZwCreateFile [NTDLL.@]
123 * Either create a new file or directory, or open an existing file, device,
124 * directory or volume.
127 * handle [O] Points to a variable which receives the file handle on return
128 * access [I] Desired access to the file
129 * attr [I] Structure describing the file
130 * io [O] Receives information about the operation on return
131 * alloc_size [I] Initial size of the file in bytes
132 * attributes [I] Attributes to create the file with
133 * sharing [I] Type of shared access the caller would like to the file
134 * disposition [I] Specifies what to do, depending on whether the file already exists
135 * options [I] Options for creating a new file
136 * ea_buffer [I] Pointer to an extended attributes buffer
137 * ea_length [I] Length of ea_buffer
140 * Success: 0. handle and io are updated.
141 * Failure: An NTSTATUS error code describing the error.
143 NTSTATUS WINAPI
NtCreateFile( PHANDLE handle
, ACCESS_MASK access
, POBJECT_ATTRIBUTES attr
,
144 PIO_STATUS_BLOCK io
, PLARGE_INTEGER alloc_size
,
145 ULONG attributes
, ULONG sharing
, ULONG disposition
,
146 ULONG options
, PVOID ea_buffer
, ULONG ea_length
)
148 ANSI_STRING unix_name
;
151 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p\n"
152 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
153 handle
, access
, debugstr_us(attr
->ObjectName
), attr
->Attributes
,
154 attr
->RootDirectory
, attr
->SecurityDescriptor
, io
, alloc_size
,
155 attributes
, sharing
, disposition
, options
, ea_buffer
, ea_length
);
157 if (!attr
|| !attr
->ObjectName
) return STATUS_INVALID_PARAMETER
;
159 if (alloc_size
) FIXME( "alloc_size not supported\n" );
161 if (attr
->RootDirectory
)
163 FIXME( "RootDirectory %p not supported\n", attr
->RootDirectory
);
164 return STATUS_OBJECT_NAME_NOT_FOUND
;
167 io
->u
.Status
= wine_nt_to_unix_file_name( attr
->ObjectName
, &unix_name
, disposition
,
168 !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
) );
170 if (io
->u
.Status
== STATUS_BAD_DEVICE_TYPE
)
172 SERVER_START_REQ( open_file_object
)
174 req
->access
= access
;
175 req
->attributes
= attr
->Attributes
;
176 req
->rootdir
= attr
->RootDirectory
;
177 req
->sharing
= sharing
;
178 req
->options
= options
;
179 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, attr
->ObjectName
->Length
);
180 io
->u
.Status
= wine_server_call( req
);
181 *handle
= reply
->handle
;
184 if (io
->u
.Status
== STATUS_SUCCESS
) io
->Information
= FILE_OPENED
;
188 if (io
->u
.Status
== STATUS_NO_SUCH_FILE
&&
189 disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
192 io
->u
.Status
= STATUS_SUCCESS
;
195 if (io
->u
.Status
== STATUS_SUCCESS
)
197 struct security_descriptor
*sd
= NULL
;
198 struct object_attributes objattr
;
202 objattr
.name_len
= 0;
205 io
->u
.Status
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
206 if (io
->u
.Status
!= STATUS_SUCCESS
)
208 RtlFreeAnsiString( &unix_name
);
213 SERVER_START_REQ( create_file
)
215 req
->access
= access
;
216 req
->attributes
= attr
->Attributes
;
217 req
->sharing
= sharing
;
218 req
->create
= disposition
;
219 req
->options
= options
;
220 req
->attrs
= attributes
;
221 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
222 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
223 wine_server_add_data( req
, unix_name
.Buffer
, unix_name
.Length
);
224 io
->u
.Status
= wine_server_call( req
);
225 *handle
= reply
->handle
;
228 NTDLL_free_struct_sd( sd
);
229 RtlFreeAnsiString( &unix_name
);
231 else WARN("%s not found (%x)\n", debugstr_us(attr
->ObjectName
), io
->u
.Status
);
233 if (io
->u
.Status
== STATUS_SUCCESS
)
235 if (created
) io
->Information
= FILE_CREATED
;
236 else switch(disposition
)
239 io
->Information
= FILE_SUPERSEDED
;
242 io
->Information
= FILE_CREATED
;
246 io
->Information
= FILE_OPENED
;
249 case FILE_OVERWRITE_IF
:
250 io
->Information
= FILE_OVERWRITTEN
;
258 /***********************************************************************
259 * Asynchronous file I/O *
271 struct async_fileio io
;
273 unsigned int already
;
280 struct async_fileio io
;
282 unsigned int already
;
284 } async_fileio_write
;
287 /* callback for file I/O user APC */
288 static void WINAPI
fileio_apc( void *arg
, IO_STATUS_BLOCK
*io
, ULONG reserved
)
290 struct async_fileio
*async
= arg
;
291 if (async
->apc
) async
->apc( async
->apc_arg
, io
, reserved
);
292 RtlFreeHeap( GetProcessHeap(), 0, async
);
295 /***********************************************************************
296 * FILE_GetNtStatus(void)
298 * Retrieve the Nt Status code from errno.
299 * Try to be consistent with FILE_SetDosError().
301 NTSTATUS
FILE_GetNtStatus(void)
305 TRACE( "errno = %d\n", errno
);
308 case EAGAIN
: return STATUS_SHARING_VIOLATION
;
309 case EBADF
: return STATUS_INVALID_HANDLE
;
310 case EBUSY
: return STATUS_DEVICE_BUSY
;
311 case ENOSPC
: return STATUS_DISK_FULL
;
314 case EACCES
: return STATUS_ACCESS_DENIED
;
315 case ENOTDIR
: return STATUS_OBJECT_PATH_NOT_FOUND
;
316 case ENOENT
: return STATUS_OBJECT_NAME_NOT_FOUND
;
317 case EISDIR
: return STATUS_FILE_IS_A_DIRECTORY
;
319 case ENFILE
: return STATUS_TOO_MANY_OPENED_FILES
;
320 case EINVAL
: return STATUS_INVALID_PARAMETER
;
321 case ENOTEMPTY
: return STATUS_DIRECTORY_NOT_EMPTY
;
322 case EPIPE
: return STATUS_PIPE_DISCONNECTED
;
323 case EIO
: return STATUS_DEVICE_NOT_READY
;
325 case ENOMEDIUM
: return STATUS_NO_MEDIA_IN_DEVICE
;
327 case ENXIO
: return STATUS_NO_SUCH_DEVICE
;
329 case EOPNOTSUPP
:return STATUS_NOT_SUPPORTED
;
330 case ECONNRESET
:return STATUS_PIPE_DISCONNECTED
;
331 case EFAULT
: return STATUS_ACCESS_VIOLATION
;
332 case ESPIPE
: return STATUS_ILLEGAL_FUNCTION
;
333 case ENOEXEC
: /* ?? */
334 case EEXIST
: /* ?? */
336 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err
);
337 return STATUS_UNSUCCESSFUL
;
341 /***********************************************************************
342 * FILE_AsyncReadService (INTERNAL)
344 static NTSTATUS
FILE_AsyncReadService(void *user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, ULONG_PTR
*total
)
346 async_fileio_read
*fileio
= user
;
347 int fd
, needs_close
, result
;
351 case STATUS_ALERTED
: /* got some new data */
352 /* check to see if the data is ready (non-blocking) */
353 if ((status
= server_get_unix_fd( fileio
->io
.handle
, FILE_READ_DATA
, &fd
,
354 &needs_close
, NULL
, NULL
)))
357 result
= read(fd
, &fileio
->buffer
[fileio
->already
], fileio
->count
- fileio
->already
);
358 if (needs_close
) close( fd
);
362 if (errno
== EAGAIN
|| errno
== EINTR
)
363 status
= STATUS_PENDING
;
364 else /* check to see if the transfer is complete */
365 status
= FILE_GetNtStatus();
367 else if (result
== 0)
369 status
= fileio
->already
? STATUS_SUCCESS
: STATUS_PIPE_BROKEN
;
373 fileio
->already
+= result
;
374 if (fileio
->already
>= fileio
->count
|| fileio
->avail_mode
)
375 status
= STATUS_SUCCESS
;
378 /* if we only have to read the available data, and none is available,
379 * simply cancel the request. If data was available, it has been read
380 * while in by previous call (NtDelayExecution)
382 status
= (fileio
->avail_mode
) ? STATUS_SUCCESS
: STATUS_PENDING
;
388 case STATUS_IO_TIMEOUT
:
389 if (fileio
->already
) status
= STATUS_SUCCESS
;
392 if (status
!= STATUS_PENDING
)
394 iosb
->u
.Status
= status
;
395 iosb
->Information
= *total
= fileio
->already
;
402 int interval
; /* max interval between two bytes */
403 int total
; /* total timeout for the whole operation */
404 int end_time
; /* absolute time of end of operation */
407 /* retrieve the I/O timeouts to use for a given handle */
408 static NTSTATUS
get_io_timeouts( HANDLE handle
, enum server_fd_type type
, ULONG count
, BOOL is_read
,
409 struct io_timeouts
*timeouts
)
411 NTSTATUS status
= STATUS_SUCCESS
;
413 timeouts
->interval
= timeouts
->total
= -1;
419 /* GetCommTimeouts */
423 status
= NtDeviceIoControlFile( handle
, NULL
, NULL
, NULL
, &io
,
424 IOCTL_SERIAL_GET_TIMEOUTS
, NULL
, 0, &st
, sizeof(st
) );
429 if (st
.ReadIntervalTimeout
)
430 timeouts
->interval
= st
.ReadIntervalTimeout
;
432 if (st
.ReadTotalTimeoutMultiplier
|| st
.ReadTotalTimeoutConstant
)
434 timeouts
->total
= st
.ReadTotalTimeoutConstant
;
435 if (st
.ReadTotalTimeoutMultiplier
!= MAXDWORD
)
436 timeouts
->total
+= count
* st
.ReadTotalTimeoutMultiplier
;
438 else if (st
.ReadIntervalTimeout
== MAXDWORD
)
439 timeouts
->interval
= timeouts
->total
= 0;
443 if (st
.WriteTotalTimeoutMultiplier
|| st
.WriteTotalTimeoutConstant
)
445 timeouts
->total
= st
.WriteTotalTimeoutConstant
;
446 if (st
.WriteTotalTimeoutMultiplier
!= MAXDWORD
)
447 timeouts
->total
+= count
* st
.WriteTotalTimeoutMultiplier
;
452 case FD_TYPE_MAILSLOT
:
455 timeouts
->interval
= 0; /* return as soon as we got something */
456 SERVER_START_REQ( set_mailslot_info
)
458 req
->handle
= handle
;
460 if (!(status
= wine_server_call( req
)) &&
461 reply
->read_timeout
!= TIMEOUT_INFINITE
)
462 timeouts
->total
= reply
->read_timeout
/ -10000;
470 if (is_read
) timeouts
->interval
= 0; /* return as soon as we got something */
475 if (timeouts
->total
!= -1) timeouts
->end_time
= NtGetTickCount() + timeouts
->total
;
476 return STATUS_SUCCESS
;
480 /* retrieve the timeout for the next wait, in milliseconds */
481 static inline int get_next_io_timeout( const struct io_timeouts
*timeouts
, ULONG already
)
485 if (timeouts
->total
!= -1)
487 ret
= timeouts
->end_time
- NtGetTickCount();
488 if (ret
< 0) ret
= 0;
490 if (already
&& timeouts
->interval
!= -1)
492 if (ret
== -1 || ret
> timeouts
->interval
) ret
= timeouts
->interval
;
498 /* retrieve the avail_mode flag for async reads */
499 static NTSTATUS
get_io_avail_mode( HANDLE handle
, enum server_fd_type type
, BOOL
*avail_mode
)
501 NTSTATUS status
= STATUS_SUCCESS
;
507 /* GetCommTimeouts */
511 status
= NtDeviceIoControlFile( handle
, NULL
, NULL
, NULL
, &io
,
512 IOCTL_SERIAL_GET_TIMEOUTS
, NULL
, 0, &st
, sizeof(st
) );
514 *avail_mode
= (!st
.ReadTotalTimeoutMultiplier
&&
515 !st
.ReadTotalTimeoutConstant
&&
516 st
.ReadIntervalTimeout
== MAXDWORD
);
519 case FD_TYPE_MAILSLOT
:
533 /******************************************************************************
534 * NtReadFile [NTDLL.@]
535 * ZwReadFile [NTDLL.@]
537 * Read from an open file handle.
540 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
541 * Event [I] Event to signal upon completion (or NULL)
542 * ApcRoutine [I] Callback to call upon completion (or NULL)
543 * ApcContext [I] Context for ApcRoutine (or NULL)
544 * IoStatusBlock [O] Receives information about the operation on return
545 * Buffer [O] Destination for the data read
546 * Length [I] Size of Buffer
547 * ByteOffset [O] Destination for the new file pointer position (or NULL)
548 * Key [O] Function unknown (may be NULL)
551 * Success: 0. IoStatusBlock is updated, and the Information member contains
552 * The number of bytes read.
553 * Failure: An NTSTATUS error code describing the error.
555 NTSTATUS WINAPI
NtReadFile(HANDLE hFile
, HANDLE hEvent
,
556 PIO_APC_ROUTINE apc
, void* apc_user
,
557 PIO_STATUS_BLOCK io_status
, void* buffer
, ULONG length
,
558 PLARGE_INTEGER offset
, PULONG key
)
560 int result
, unix_handle
, needs_close
, timeout_init_done
= 0;
561 unsigned int options
;
562 struct io_timeouts timeouts
;
565 enum server_fd_type type
;
566 ULONG_PTR cvalue
= apc
? 0 : (ULONG_PTR
)apc_user
;
568 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
569 hFile
,hEvent
,apc
,apc_user
,io_status
,buffer
,length
,offset
,key
);
571 if (!io_status
) return STATUS_ACCESS_VIOLATION
;
573 status
= server_get_unix_fd( hFile
, FILE_READ_DATA
, &unix_handle
,
574 &needs_close
, &type
, &options
);
575 if (status
) return status
;
577 if (type
== FD_TYPE_FILE
&& offset
&& offset
->QuadPart
!= (LONGLONG
)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
579 /* async I/O doesn't make sense on regular files */
580 while ((result
= pread( unix_handle
, buffer
, length
, offset
->QuadPart
)) == -1)
584 status
= FILE_GetNtStatus();
588 if (options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
))
589 /* update file pointer position */
590 lseek( unix_handle
, offset
->QuadPart
+ result
, SEEK_SET
);
593 status
= total
? STATUS_SUCCESS
: STATUS_END_OF_FILE
;
599 if ((result
= read( unix_handle
, (char *)buffer
+ total
, length
- total
)) >= 0)
602 if (!result
|| total
== length
)
606 status
= STATUS_SUCCESS
;
613 status
= STATUS_END_OF_FILE
;
618 status
= STATUS_PIPE_BROKEN
;
625 if (errno
== EINTR
) continue;
628 status
= FILE_GetNtStatus();
633 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
635 async_fileio_read
*fileio
;
638 if ((status
= get_io_avail_mode( hFile
, type
, &avail_mode
)))
640 if (total
&& avail_mode
)
642 status
= STATUS_SUCCESS
;
646 if (!(fileio
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio
))))
648 status
= STATUS_NO_MEMORY
;
651 fileio
->io
.handle
= hFile
;
652 fileio
->io
.apc
= apc
;
653 fileio
->io
.apc_arg
= apc_user
;
654 fileio
->already
= total
;
655 fileio
->count
= length
;
656 fileio
->buffer
= buffer
;
657 fileio
->avail_mode
= avail_mode
;
659 SERVER_START_REQ( register_async
)
662 req
->type
= ASYNC_TYPE_READ
;
664 req
->async
.callback
= FILE_AsyncReadService
;
665 req
->async
.iosb
= io_status
;
666 req
->async
.arg
= fileio
;
667 req
->async
.apc
= fileio_apc
;
668 req
->async
.event
= hEvent
;
669 req
->async
.cvalue
= cvalue
;
670 status
= wine_server_call( req
);
674 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, fileio
);
677 else /* synchronous read, wait for the fd to become ready */
682 if (!timeout_init_done
)
684 timeout_init_done
= 1;
685 if ((status
= get_io_timeouts( hFile
, type
, length
, TRUE
, &timeouts
)))
687 if (hEvent
) NtResetEvent( hEvent
, NULL
);
689 timeout
= get_next_io_timeout( &timeouts
, total
);
691 pfd
.fd
= unix_handle
;
694 if (!timeout
|| !(ret
= poll( &pfd
, 1, timeout
)))
696 if (total
) /* return with what we got so far */
697 status
= STATUS_SUCCESS
;
699 status
= (type
== FD_TYPE_MAILSLOT
) ? STATUS_IO_TIMEOUT
: STATUS_TIMEOUT
;
702 if (ret
== -1 && errno
!= EINTR
)
704 status
= FILE_GetNtStatus();
707 /* will now restart the read */
712 if (cvalue
) NTDLL_AddCompletion( hFile
, cvalue
, status
, total
);
715 if (needs_close
) close( unix_handle
);
716 if (status
== STATUS_SUCCESS
)
718 io_status
->u
.Status
= status
;
719 io_status
->Information
= total
;
720 TRACE("= SUCCESS (%u)\n", total
);
721 if (hEvent
) NtSetEvent( hEvent
, NULL
);
722 if (apc
) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)apc
,
723 (ULONG_PTR
)apc_user
, (ULONG_PTR
)io_status
, 0 );
727 TRACE("= 0x%08x\n", status
);
728 if (status
!= STATUS_PENDING
&& hEvent
) NtResetEvent( hEvent
, NULL
);
734 /******************************************************************************
735 * NtReadFileScatter [NTDLL.@]
736 * ZwReadFileScatter [NTDLL.@]
738 NTSTATUS WINAPI
NtReadFileScatter( HANDLE file
, HANDLE event
, PIO_APC_ROUTINE apc
, void *apc_user
,
739 PIO_STATUS_BLOCK io_status
, FILE_SEGMENT_ELEMENT
*segments
,
740 ULONG length
, PLARGE_INTEGER offset
, PULONG key
)
742 size_t page_size
= getpagesize();
743 int result
, unix_handle
, needs_close
;
744 unsigned int options
;
746 ULONG pos
= 0, total
= 0;
747 enum server_fd_type type
;
748 ULONG_PTR cvalue
= apc
? 0 : (ULONG_PTR
)apc_user
;
750 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
751 file
, event
, apc
, apc_user
, io_status
, segments
, length
, offset
, key
);
753 if (length
% page_size
) return STATUS_INVALID_PARAMETER
;
754 if (!io_status
) return STATUS_ACCESS_VIOLATION
;
756 status
= server_get_unix_fd( file
, FILE_READ_DATA
, &unix_handle
,
757 &needs_close
, &type
, &options
);
758 if (status
) return status
;
760 if ((type
!= FD_TYPE_FILE
) ||
761 (options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)) ||
762 !(options
& FILE_NO_INTERMEDIATE_BUFFERING
))
764 status
= STATUS_INVALID_PARAMETER
;
770 if (offset
&& offset
->QuadPart
!= (LONGLONG
)-2 /* FILE_USE_FILE_POINTER_POSITION */)
771 result
= pread( unix_handle
, (char *)segments
->Buffer
+ pos
,
772 page_size
- pos
, offset
->QuadPart
+ total
);
774 result
= read( unix_handle
, (char *)segments
->Buffer
+ pos
, page_size
- pos
);
778 if (errno
== EINTR
) continue;
779 status
= FILE_GetNtStatus();
784 status
= STATUS_END_OF_FILE
;
789 if ((pos
+= result
) == page_size
)
796 if (cvalue
) NTDLL_AddCompletion( file
, cvalue
, status
, total
);
799 if (needs_close
) close( unix_handle
);
800 if (status
== STATUS_SUCCESS
)
802 io_status
->u
.Status
= status
;
803 io_status
->Information
= total
;
804 TRACE("= SUCCESS (%u)\n", total
);
805 if (event
) NtSetEvent( event
, NULL
);
806 if (apc
) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)apc
,
807 (ULONG_PTR
)apc_user
, (ULONG_PTR
)io_status
, 0 );
811 TRACE("= 0x%08x\n", status
);
812 if (status
!= STATUS_PENDING
&& event
) NtResetEvent( event
, NULL
);
818 /***********************************************************************
819 * FILE_AsyncWriteService (INTERNAL)
821 static NTSTATUS
FILE_AsyncWriteService(void *user
, IO_STATUS_BLOCK
*iosb
, NTSTATUS status
, ULONG_PTR
*total
)
823 async_fileio_write
*fileio
= user
;
824 int result
, fd
, needs_close
;
825 enum server_fd_type type
;
830 /* write some data (non-blocking) */
831 if ((status
= server_get_unix_fd( fileio
->io
.handle
, FILE_WRITE_DATA
, &fd
,
832 &needs_close
, &type
, NULL
)))
835 if (!fileio
->count
&& (type
== FD_TYPE_MAILSLOT
|| type
== FD_TYPE_PIPE
|| type
== FD_TYPE_SOCKET
))
836 result
= send( fd
, fileio
->buffer
, 0, 0 );
838 result
= write( fd
, &fileio
->buffer
[fileio
->already
], fileio
->count
- fileio
->already
);
840 if (needs_close
) close( fd
);
844 if (errno
== EAGAIN
|| errno
== EINTR
) status
= STATUS_PENDING
;
845 else status
= FILE_GetNtStatus();
849 fileio
->already
+= result
;
850 status
= (fileio
->already
< fileio
->count
) ? STATUS_PENDING
: STATUS_SUCCESS
;
855 case STATUS_IO_TIMEOUT
:
856 if (fileio
->already
) status
= STATUS_SUCCESS
;
859 if (status
!= STATUS_PENDING
)
861 iosb
->u
.Status
= status
;
862 iosb
->Information
= *total
= fileio
->already
;
867 /******************************************************************************
868 * NtWriteFile [NTDLL.@]
869 * ZwWriteFile [NTDLL.@]
871 * Write to an open file handle.
874 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
875 * Event [I] Event to signal upon completion (or NULL)
876 * ApcRoutine [I] Callback to call upon completion (or NULL)
877 * ApcContext [I] Context for ApcRoutine (or NULL)
878 * IoStatusBlock [O] Receives information about the operation on return
879 * Buffer [I] Source for the data to write
880 * Length [I] Size of Buffer
881 * ByteOffset [O] Destination for the new file pointer position (or NULL)
882 * Key [O] Function unknown (may be NULL)
885 * Success: 0. IoStatusBlock is updated, and the Information member contains
886 * The number of bytes written.
887 * Failure: An NTSTATUS error code describing the error.
889 NTSTATUS WINAPI
NtWriteFile(HANDLE hFile
, HANDLE hEvent
,
890 PIO_APC_ROUTINE apc
, void* apc_user
,
891 PIO_STATUS_BLOCK io_status
,
892 const void* buffer
, ULONG length
,
893 PLARGE_INTEGER offset
, PULONG key
)
895 int result
, unix_handle
, needs_close
, timeout_init_done
= 0;
896 unsigned int options
;
897 struct io_timeouts timeouts
;
900 enum server_fd_type type
;
901 ULONG_PTR cvalue
= apc
? 0 : (ULONG_PTR
)apc_user
;
903 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
904 hFile
,hEvent
,apc
,apc_user
,io_status
,buffer
,length
,offset
,key
);
906 if (!io_status
) return STATUS_ACCESS_VIOLATION
;
908 status
= server_get_unix_fd( hFile
, FILE_WRITE_DATA
, &unix_handle
,
909 &needs_close
, &type
, &options
);
910 if (status
) return status
;
912 if (type
== FD_TYPE_FILE
&& offset
&& offset
->QuadPart
!= (LONGLONG
)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
914 /* async I/O doesn't make sense on regular files */
915 while ((result
= pwrite( unix_handle
, buffer
, length
, offset
->QuadPart
)) == -1)
919 if (errno
== EFAULT
) status
= STATUS_INVALID_USER_BUFFER
;
920 else status
= FILE_GetNtStatus();
925 if (options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
))
926 /* update file pointer position */
927 lseek( unix_handle
, offset
->QuadPart
+ result
, SEEK_SET
);
930 status
= STATUS_SUCCESS
;
936 /* zero-length writes on sockets may not work with plain write(2) */
937 if (!length
&& (type
== FD_TYPE_MAILSLOT
|| type
== FD_TYPE_PIPE
|| type
== FD_TYPE_SOCKET
))
938 result
= send( unix_handle
, buffer
, 0, 0 );
940 result
= write( unix_handle
, (const char *)buffer
+ total
, length
- total
);
947 status
= STATUS_SUCCESS
;
953 if (errno
== EINTR
) continue;
958 status
= STATUS_INVALID_USER_BUFFER
;
961 status
= FILE_GetNtStatus();
966 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
968 async_fileio_write
*fileio
;
970 if (!(fileio
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio
))))
972 status
= STATUS_NO_MEMORY
;
975 fileio
->io
.handle
= hFile
;
976 fileio
->io
.apc
= apc
;
977 fileio
->io
.apc_arg
= apc_user
;
978 fileio
->already
= total
;
979 fileio
->count
= length
;
980 fileio
->buffer
= buffer
;
982 SERVER_START_REQ( register_async
)
985 req
->type
= ASYNC_TYPE_WRITE
;
987 req
->async
.callback
= FILE_AsyncWriteService
;
988 req
->async
.iosb
= io_status
;
989 req
->async
.arg
= fileio
;
990 req
->async
.apc
= fileio_apc
;
991 req
->async
.event
= hEvent
;
992 req
->async
.cvalue
= cvalue
;
993 status
= wine_server_call( req
);
997 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, fileio
);
1000 else /* synchronous write, wait for the fd to become ready */
1005 if (!timeout_init_done
)
1007 timeout_init_done
= 1;
1008 if ((status
= get_io_timeouts( hFile
, type
, length
, FALSE
, &timeouts
)))
1010 if (hEvent
) NtResetEvent( hEvent
, NULL
);
1012 timeout
= get_next_io_timeout( &timeouts
, total
);
1014 pfd
.fd
= unix_handle
;
1015 pfd
.events
= POLLOUT
;
1017 if (!timeout
|| !(ret
= poll( &pfd
, 1, timeout
)))
1019 /* return with what we got so far */
1020 status
= total
? STATUS_SUCCESS
: STATUS_TIMEOUT
;
1023 if (ret
== -1 && errno
!= EINTR
)
1025 status
= FILE_GetNtStatus();
1028 /* will now restart the write */
1033 if (cvalue
) NTDLL_AddCompletion( hFile
, cvalue
, status
, total
);
1036 if (needs_close
) close( unix_handle
);
1037 if (status
== STATUS_SUCCESS
)
1039 io_status
->u
.Status
= status
;
1040 io_status
->Information
= total
;
1041 TRACE("= SUCCESS (%u)\n", total
);
1042 if (hEvent
) NtSetEvent( hEvent
, NULL
);
1043 if (apc
) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)apc
,
1044 (ULONG_PTR
)apc_user
, (ULONG_PTR
)io_status
, 0 );
1048 TRACE("= 0x%08x\n", status
);
1049 if (status
!= STATUS_PENDING
&& hEvent
) NtResetEvent( hEvent
, NULL
);
1055 /******************************************************************************
1056 * NtWriteFileGather [NTDLL.@]
1057 * ZwWriteFileGather [NTDLL.@]
1059 NTSTATUS WINAPI
NtWriteFileGather( HANDLE file
, HANDLE event
, PIO_APC_ROUTINE apc
, void *apc_user
,
1060 PIO_STATUS_BLOCK io_status
, FILE_SEGMENT_ELEMENT
*segments
,
1061 ULONG length
, PLARGE_INTEGER offset
, PULONG key
)
1063 size_t page_size
= getpagesize();
1064 int result
, unix_handle
, needs_close
;
1065 unsigned int options
;
1067 ULONG pos
= 0, total
= 0;
1068 enum server_fd_type type
;
1069 ULONG_PTR cvalue
= apc
? 0 : (ULONG_PTR
)apc_user
;
1071 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1072 file
, event
, apc
, apc_user
, io_status
, segments
, length
, offset
, key
);
1074 if (length
% page_size
) return STATUS_INVALID_PARAMETER
;
1075 if (!io_status
) return STATUS_ACCESS_VIOLATION
;
1077 status
= server_get_unix_fd( file
, FILE_WRITE_DATA
, &unix_handle
,
1078 &needs_close
, &type
, &options
);
1079 if (status
) return status
;
1081 if ((type
!= FD_TYPE_FILE
) ||
1082 (options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)) ||
1083 !(options
& FILE_NO_INTERMEDIATE_BUFFERING
))
1085 status
= STATUS_INVALID_PARAMETER
;
1091 if (offset
&& offset
->QuadPart
!= (LONGLONG
)-2 /* FILE_USE_FILE_POINTER_POSITION */)
1092 result
= pwrite( unix_handle
, (char *)segments
->Buffer
+ pos
,
1093 page_size
- pos
, offset
->QuadPart
+ total
);
1095 result
= write( unix_handle
, (char *)segments
->Buffer
+ pos
, page_size
- pos
);
1099 if (errno
== EINTR
) continue;
1100 if (errno
== EFAULT
)
1102 status
= STATUS_INVALID_USER_BUFFER
;
1105 status
= FILE_GetNtStatus();
1110 status
= STATUS_DISK_FULL
;
1115 if ((pos
+= result
) == page_size
)
1122 if (cvalue
) NTDLL_AddCompletion( file
, cvalue
, status
, total
);
1125 if (needs_close
) close( unix_handle
);
1126 if (status
== STATUS_SUCCESS
)
1128 io_status
->u
.Status
= status
;
1129 io_status
->Information
= total
;
1130 TRACE("= SUCCESS (%u)\n", total
);
1131 if (event
) NtSetEvent( event
, NULL
);
1132 if (apc
) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)apc
,
1133 (ULONG_PTR
)apc_user
, (ULONG_PTR
)io_status
, 0 );
1137 TRACE("= 0x%08x\n", status
);
1138 if (status
!= STATUS_PENDING
&& event
) NtResetEvent( event
, NULL
);
1146 HANDLE handle
; /* handle to the device */
1147 void *buffer
; /* buffer for output */
1148 ULONG size
; /* size of buffer */
1149 PIO_APC_ROUTINE apc
; /* user apc params */
1153 /* callback for ioctl async I/O completion */
1154 static NTSTATUS
ioctl_completion( void *arg
, IO_STATUS_BLOCK
*io
, NTSTATUS status
)
1156 struct async_ioctl
*async
= arg
;
1158 if (status
== STATUS_ALERTED
)
1160 SERVER_START_REQ( get_ioctl_result
)
1162 req
->handle
= async
->handle
;
1163 req
->user_arg
= async
;
1164 wine_server_set_reply( req
, async
->buffer
, async
->size
);
1165 if (!(status
= wine_server_call( req
)))
1166 io
->Information
= wine_server_reply_size( reply
);
1170 if (status
!= STATUS_PENDING
) io
->u
.Status
= status
;
1174 /* callback for ioctl user APC */
1175 static void WINAPI
ioctl_apc( void *arg
, IO_STATUS_BLOCK
*io
, ULONG reserved
)
1177 struct async_ioctl
*async
= arg
;
1178 if (async
->apc
) async
->apc( async
->apc_arg
, io
, reserved
);
1179 RtlFreeHeap( GetProcessHeap(), 0, async
);
1182 /* do a ioctl call through the server */
1183 static NTSTATUS
server_ioctl_file( HANDLE handle
, HANDLE event
,
1184 PIO_APC_ROUTINE apc
, PVOID apc_context
,
1185 IO_STATUS_BLOCK
*io
, ULONG code
,
1186 const void *in_buffer
, ULONG in_size
,
1187 PVOID out_buffer
, ULONG out_size
)
1189 struct async_ioctl
*async
;
1193 ULONG_PTR cvalue
= apc
? 0 : (ULONG_PTR
)apc_context
;
1195 if (!(async
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async
) )))
1196 return STATUS_NO_MEMORY
;
1197 async
->handle
= handle
;
1198 async
->buffer
= out_buffer
;
1199 async
->size
= out_size
;
1201 async
->apc_arg
= apc_context
;
1203 SERVER_START_REQ( ioctl
)
1205 req
->handle
= handle
;
1207 req
->async
.callback
= ioctl_completion
;
1208 req
->async
.iosb
= io
;
1209 req
->async
.arg
= async
;
1210 req
->async
.apc
= (apc
|| event
) ? ioctl_apc
: NULL
;
1211 req
->async
.event
= event
;
1212 req
->async
.cvalue
= cvalue
;
1213 wine_server_add_data( req
, in_buffer
, in_size
);
1214 wine_server_set_reply( req
, out_buffer
, out_size
);
1215 if (!(status
= wine_server_call( req
)))
1216 io
->Information
= wine_server_reply_size( reply
);
1217 wait_handle
= reply
->wait
;
1218 options
= reply
->options
;
1222 if (status
== STATUS_NOT_SUPPORTED
)
1223 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1224 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
1226 if (status
!= STATUS_PENDING
) RtlFreeHeap( GetProcessHeap(), 0, async
);
1230 NtWaitForSingleObject( wait_handle
, (options
& FILE_SYNCHRONOUS_IO_ALERT
), NULL
);
1231 status
= io
->u
.Status
;
1232 NtClose( wait_handle
);
1233 RtlFreeHeap( GetProcessHeap(), 0, async
);
1240 /**************************************************************************
1241 * NtDeviceIoControlFile [NTDLL.@]
1242 * ZwDeviceIoControlFile [NTDLL.@]
1244 * Perform an I/O control operation on an open file handle.
1247 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1248 * event [I] Event to signal upon completion (or NULL)
1249 * apc [I] Callback to call upon completion (or NULL)
1250 * apc_context [I] Context for ApcRoutine (or NULL)
1251 * io [O] Receives information about the operation on return
1252 * code [I] Control code for the operation to perform
1253 * in_buffer [I] Source for any input data required (or NULL)
1254 * in_size [I] Size of InputBuffer
1255 * out_buffer [O] Source for any output data returned (or NULL)
1256 * out_size [I] Size of OutputBuffer
1259 * Success: 0. IoStatusBlock is updated.
1260 * Failure: An NTSTATUS error code describing the error.
1262 NTSTATUS WINAPI
NtDeviceIoControlFile(HANDLE handle
, HANDLE event
,
1263 PIO_APC_ROUTINE apc
, PVOID apc_context
,
1264 PIO_STATUS_BLOCK io
, ULONG code
,
1265 PVOID in_buffer
, ULONG in_size
,
1266 PVOID out_buffer
, ULONG out_size
)
1268 ULONG device
= (code
>> 16);
1269 NTSTATUS status
= STATUS_NOT_SUPPORTED
;
1271 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1272 handle
, event
, apc
, apc_context
, io
, code
,
1273 in_buffer
, in_size
, out_buffer
, out_size
);
1277 case FILE_DEVICE_DISK
:
1278 case FILE_DEVICE_CD_ROM
:
1279 case FILE_DEVICE_DVD
:
1280 case FILE_DEVICE_CONTROLLER
:
1281 case FILE_DEVICE_MASS_STORAGE
:
1282 status
= CDROM_DeviceIoControl(handle
, event
, apc
, apc_context
, io
, code
,
1283 in_buffer
, in_size
, out_buffer
, out_size
);
1285 case FILE_DEVICE_SERIAL_PORT
:
1286 status
= COMM_DeviceIoControl(handle
, event
, apc
, apc_context
, io
, code
,
1287 in_buffer
, in_size
, out_buffer
, out_size
);
1289 case FILE_DEVICE_TAPE
:
1290 status
= TAPE_DeviceIoControl(handle
, event
, apc
, apc_context
, io
, code
,
1291 in_buffer
, in_size
, out_buffer
, out_size
);
1295 if (status
== STATUS_NOT_SUPPORTED
|| status
== STATUS_BAD_DEVICE_TYPE
)
1296 status
= server_ioctl_file( handle
, event
, apc
, apc_context
, io
, code
,
1297 in_buffer
, in_size
, out_buffer
, out_size
);
1299 if (status
!= STATUS_PENDING
) io
->u
.Status
= status
;
1304 /**************************************************************************
1305 * NtFsControlFile [NTDLL.@]
1306 * ZwFsControlFile [NTDLL.@]
1308 * Perform a file system control operation on an open file handle.
1311 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1312 * event [I] Event to signal upon completion (or NULL)
1313 * apc [I] Callback to call upon completion (or NULL)
1314 * apc_context [I] Context for ApcRoutine (or NULL)
1315 * io [O] Receives information about the operation on return
1316 * code [I] Control code for the operation to perform
1317 * in_buffer [I] Source for any input data required (or NULL)
1318 * in_size [I] Size of InputBuffer
1319 * out_buffer [O] Source for any output data returned (or NULL)
1320 * out_size [I] Size of OutputBuffer
1323 * Success: 0. IoStatusBlock is updated.
1324 * Failure: An NTSTATUS error code describing the error.
1326 NTSTATUS WINAPI
NtFsControlFile(HANDLE handle
, HANDLE event
, PIO_APC_ROUTINE apc
,
1327 PVOID apc_context
, PIO_STATUS_BLOCK io
, ULONG code
,
1328 PVOID in_buffer
, ULONG in_size
, PVOID out_buffer
, ULONG out_size
)
1332 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1333 handle
, event
, apc
, apc_context
, io
, code
,
1334 in_buffer
, in_size
, out_buffer
, out_size
);
1336 if (!io
) return STATUS_INVALID_PARAMETER
;
1340 case FSCTL_DISMOUNT_VOLUME
:
1341 status
= server_ioctl_file( handle
, event
, apc
, apc_context
, io
, code
,
1342 in_buffer
, in_size
, out_buffer
, out_size
);
1343 if (!status
) status
= DIR_unmount_device( handle
);
1346 case FSCTL_PIPE_PEEK
:
1348 FILE_PIPE_PEEK_BUFFER
*buffer
= out_buffer
;
1349 int avail
= 0, fd
, needs_close
;
1351 if (out_size
< FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
))
1353 status
= STATUS_INFO_LENGTH_MISMATCH
;
1357 if ((status
= server_get_unix_fd( handle
, FILE_READ_DATA
, &fd
, &needs_close
, NULL
, NULL
)))
1361 if (ioctl( fd
, FIONREAD
, &avail
) != 0)
1363 TRACE("FIONREAD failed reason: %s\n",strerror(errno
));
1364 if (needs_close
) close( fd
);
1365 status
= FILE_GetNtStatus();
1369 if (!avail
) /* check for closed pipe */
1371 struct pollfd pollfd
;
1375 pollfd
.events
= POLLIN
;
1377 ret
= poll( &pollfd
, 1, 0 );
1378 if (ret
== -1 || (ret
== 1 && (pollfd
.revents
& (POLLHUP
|POLLERR
))))
1380 if (needs_close
) close( fd
);
1381 status
= STATUS_PIPE_BROKEN
;
1385 buffer
->NamedPipeState
= 0; /* FIXME */
1386 buffer
->ReadDataAvailable
= avail
;
1387 buffer
->NumberOfMessages
= 0; /* FIXME */
1388 buffer
->MessageLength
= 0; /* FIXME */
1389 io
->Information
= FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
);
1390 status
= STATUS_SUCCESS
;
1393 ULONG data_size
= out_size
- FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
);
1396 int res
= recv( fd
, buffer
->Data
, data_size
, MSG_PEEK
);
1397 if (res
>= 0) io
->Information
+= res
;
1400 if (needs_close
) close( fd
);
1404 case FSCTL_PIPE_DISCONNECT
:
1405 status
= server_ioctl_file( handle
, event
, apc
, apc_context
, io
, code
,
1406 in_buffer
, in_size
, out_buffer
, out_size
);
1409 int fd
= server_remove_fd_from_cache( handle
);
1410 if (fd
!= -1) close( fd
);
1414 case FSCTL_PIPE_IMPERSONATE
:
1415 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1416 status
= RtlImpersonateSelf( SecurityImpersonation
);
1419 case FSCTL_LOCK_VOLUME
:
1420 case FSCTL_UNLOCK_VOLUME
:
1421 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1422 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
1423 status
= STATUS_SUCCESS
;
1426 case FSCTL_PIPE_LISTEN
:
1427 case FSCTL_PIPE_WAIT
:
1429 status
= server_ioctl_file( handle
, event
, apc
, apc_context
, io
, code
,
1430 in_buffer
, in_size
, out_buffer
, out_size
);
1434 if (status
!= STATUS_PENDING
) io
->u
.Status
= status
;
1438 /******************************************************************************
1439 * NtSetVolumeInformationFile [NTDLL.@]
1440 * ZwSetVolumeInformationFile [NTDLL.@]
1442 * Set volume information for an open file handle.
1445 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1446 * IoStatusBlock [O] Receives information about the operation on return
1447 * FsInformation [I] Source for volume information
1448 * Length [I] Size of FsInformation
1449 * FsInformationClass [I] Type of volume information to set
1452 * Success: 0. IoStatusBlock is updated.
1453 * Failure: An NTSTATUS error code describing the error.
1455 NTSTATUS WINAPI
NtSetVolumeInformationFile(
1456 IN HANDLE FileHandle
,
1457 PIO_STATUS_BLOCK IoStatusBlock
,
1458 PVOID FsInformation
,
1460 FS_INFORMATION_CLASS FsInformationClass
)
1462 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1463 FileHandle
,IoStatusBlock
,FsInformation
,Length
,FsInformationClass
);
1467 /******************************************************************************
1468 * NtQueryInformationFile [NTDLL.@]
1469 * ZwQueryInformationFile [NTDLL.@]
1471 * Get information about an open file handle.
1474 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1475 * io [O] Receives information about the operation on return
1476 * ptr [O] Destination for file information
1477 * len [I] Size of FileInformation
1478 * class [I] Type of file information to get
1481 * Success: 0. IoStatusBlock and FileInformation are updated.
1482 * Failure: An NTSTATUS error code describing the error.
1484 NTSTATUS WINAPI
NtQueryInformationFile( HANDLE hFile
, PIO_STATUS_BLOCK io
,
1485 PVOID ptr
, LONG len
, FILE_INFORMATION_CLASS
class )
1487 static const size_t info_sizes
[] =
1490 sizeof(FILE_DIRECTORY_INFORMATION
), /* FileDirectoryInformation */
1491 sizeof(FILE_FULL_DIRECTORY_INFORMATION
), /* FileFullDirectoryInformation */
1492 sizeof(FILE_BOTH_DIRECTORY_INFORMATION
), /* FileBothDirectoryInformation */
1493 sizeof(FILE_BASIC_INFORMATION
), /* FileBasicInformation */
1494 sizeof(FILE_STANDARD_INFORMATION
), /* FileStandardInformation */
1495 sizeof(FILE_INTERNAL_INFORMATION
), /* FileInternalInformation */
1496 sizeof(FILE_EA_INFORMATION
), /* FileEaInformation */
1497 sizeof(FILE_ACCESS_INFORMATION
), /* FileAccessInformation */
1498 sizeof(FILE_NAME_INFORMATION
)-sizeof(WCHAR
), /* FileNameInformation */
1499 sizeof(FILE_RENAME_INFORMATION
)-sizeof(WCHAR
), /* FileRenameInformation */
1500 0, /* FileLinkInformation */
1501 sizeof(FILE_NAMES_INFORMATION
)-sizeof(WCHAR
), /* FileNamesInformation */
1502 sizeof(FILE_DISPOSITION_INFORMATION
), /* FileDispositionInformation */
1503 sizeof(FILE_POSITION_INFORMATION
), /* FilePositionInformation */
1504 sizeof(FILE_FULL_EA_INFORMATION
), /* FileFullEaInformation */
1505 sizeof(FILE_MODE_INFORMATION
), /* FileModeInformation */
1506 sizeof(FILE_ALIGNMENT_INFORMATION
), /* FileAlignmentInformation */
1507 sizeof(FILE_ALL_INFORMATION
)-sizeof(WCHAR
), /* FileAllInformation */
1508 sizeof(FILE_ALLOCATION_INFORMATION
), /* FileAllocationInformation */
1509 sizeof(FILE_END_OF_FILE_INFORMATION
), /* FileEndOfFileInformation */
1510 0, /* FileAlternateNameInformation */
1511 sizeof(FILE_STREAM_INFORMATION
)-sizeof(WCHAR
), /* FileStreamInformation */
1512 0, /* FilePipeInformation */
1513 sizeof(FILE_PIPE_LOCAL_INFORMATION
), /* FilePipeLocalInformation */
1514 0, /* FilePipeRemoteInformation */
1515 sizeof(FILE_MAILSLOT_QUERY_INFORMATION
), /* FileMailslotQueryInformation */
1516 0, /* FileMailslotSetInformation */
1517 0, /* FileCompressionInformation */
1518 0, /* FileObjectIdInformation */
1519 0, /* FileCompletionInformation */
1520 0, /* FileMoveClusterInformation */
1521 0, /* FileQuotaInformation */
1522 0, /* FileReparsePointInformation */
1523 0, /* FileNetworkOpenInformation */
1524 0, /* FileAttributeTagInformation */
1525 0 /* FileTrackingInformation */
1529 int fd
, needs_close
= FALSE
;
1531 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile
, io
, ptr
, len
, class);
1533 io
->Information
= 0;
1535 if (class <= 0 || class >= FileMaximumInformation
)
1536 return io
->u
.Status
= STATUS_INVALID_INFO_CLASS
;
1537 if (!info_sizes
[class])
1539 FIXME("Unsupported class (%d)\n", class);
1540 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
1542 if (len
< info_sizes
[class])
1543 return io
->u
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
1545 if (class != FilePipeLocalInformation
)
1547 if ((io
->u
.Status
= server_get_unix_fd( hFile
, 0, &fd
, &needs_close
, NULL
, NULL
)))
1548 return io
->u
.Status
;
1553 case FileBasicInformation
:
1555 FILE_BASIC_INFORMATION
*info
= ptr
;
1557 if (fstat( fd
, &st
) == -1)
1558 io
->u
.Status
= FILE_GetNtStatus();
1559 else if (!S_ISREG(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
1560 io
->u
.Status
= STATUS_INVALID_INFO_CLASS
;
1563 if (S_ISDIR(st
.st_mode
)) info
->FileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
1564 else info
->FileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1565 if (!(st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)))
1566 info
->FileAttributes
|= FILE_ATTRIBUTE_READONLY
;
1567 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->CreationTime
);
1568 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->LastWriteTime
);
1569 RtlSecondsSince1970ToTime( st
.st_ctime
, &info
->ChangeTime
);
1570 RtlSecondsSince1970ToTime( st
.st_atime
, &info
->LastAccessTime
);
1571 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1572 info
->CreationTime
.QuadPart
+= st
.st_mtim
.tv_nsec
/ 100;
1573 info
->LastWriteTime
.QuadPart
+= st
.st_mtim
.tv_nsec
/ 100;
1575 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1576 info
->ChangeTime
.QuadPart
+= st
.st_ctim
.tv_nsec
/ 100;
1578 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1579 info
->LastAccessTime
.QuadPart
+= st
.st_atim
.tv_nsec
/ 100;
1584 case FileStandardInformation
:
1586 FILE_STANDARD_INFORMATION
*info
= ptr
;
1588 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
1591 if ((info
->Directory
= S_ISDIR(st
.st_mode
)))
1593 info
->AllocationSize
.QuadPart
= 0;
1594 info
->EndOfFile
.QuadPart
= 0;
1595 info
->NumberOfLinks
= 1;
1596 info
->DeletePending
= FALSE
;
1600 info
->AllocationSize
.QuadPart
= (ULONGLONG
)st
.st_blocks
* 512;
1601 info
->EndOfFile
.QuadPart
= st
.st_size
;
1602 info
->NumberOfLinks
= st
.st_nlink
;
1603 info
->DeletePending
= FALSE
; /* FIXME */
1608 case FilePositionInformation
:
1610 FILE_POSITION_INFORMATION
*info
= ptr
;
1611 off_t res
= lseek( fd
, 0, SEEK_CUR
);
1612 if (res
== (off_t
)-1) io
->u
.Status
= FILE_GetNtStatus();
1613 else info
->CurrentByteOffset
.QuadPart
= res
;
1616 case FileInternalInformation
:
1618 FILE_INTERNAL_INFORMATION
*info
= ptr
;
1620 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
1621 else info
->IndexNumber
.QuadPart
= st
.st_ino
;
1624 case FileEaInformation
:
1626 FILE_EA_INFORMATION
*info
= ptr
;
1630 case FileEndOfFileInformation
:
1632 FILE_END_OF_FILE_INFORMATION
*info
= ptr
;
1634 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
1635 else info
->EndOfFile
.QuadPart
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
1638 case FileAllInformation
:
1640 FILE_ALL_INFORMATION
*info
= ptr
;
1642 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
1643 else if (!S_ISREG(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
1644 io
->u
.Status
= STATUS_INVALID_INFO_CLASS
;
1647 if ((info
->StandardInformation
.Directory
= S_ISDIR(st
.st_mode
)))
1649 info
->BasicInformation
.FileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
1650 info
->StandardInformation
.AllocationSize
.QuadPart
= 0;
1651 info
->StandardInformation
.EndOfFile
.QuadPart
= 0;
1652 info
->StandardInformation
.NumberOfLinks
= 1;
1653 info
->StandardInformation
.DeletePending
= FALSE
;
1657 info
->BasicInformation
.FileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1658 info
->StandardInformation
.AllocationSize
.QuadPart
= (ULONGLONG
)st
.st_blocks
* 512;
1659 info
->StandardInformation
.EndOfFile
.QuadPart
= st
.st_size
;
1660 info
->StandardInformation
.NumberOfLinks
= st
.st_nlink
;
1661 info
->StandardInformation
.DeletePending
= FALSE
; /* FIXME */
1663 if (!(st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)))
1664 info
->BasicInformation
.FileAttributes
|= FILE_ATTRIBUTE_READONLY
;
1665 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->BasicInformation
.CreationTime
);
1666 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->BasicInformation
.LastWriteTime
);
1667 RtlSecondsSince1970ToTime( st
.st_ctime
, &info
->BasicInformation
.ChangeTime
);
1668 RtlSecondsSince1970ToTime( st
.st_atime
, &info
->BasicInformation
.LastAccessTime
);
1669 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1670 info
->BasicInformation
.CreationTime
.QuadPart
+= st
.st_mtim
.tv_nsec
/ 100;
1671 info
->BasicInformation
.LastWriteTime
.QuadPart
+= st
.st_mtim
.tv_nsec
/ 100;
1673 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1674 info
->BasicInformation
.ChangeTime
.QuadPart
+= st
.st_ctim
.tv_nsec
/ 100;
1676 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1677 info
->BasicInformation
.LastAccessTime
.QuadPart
+= st
.st_atim
.tv_nsec
/ 100;
1679 info
->InternalInformation
.IndexNumber
.QuadPart
= st
.st_ino
;
1680 info
->EaInformation
.EaSize
= 0;
1681 info
->AccessInformation
.AccessFlags
= 0; /* FIXME */
1682 info
->PositionInformation
.CurrentByteOffset
.QuadPart
= lseek( fd
, 0, SEEK_CUR
);
1683 info
->ModeInformation
.Mode
= 0; /* FIXME */
1684 info
->AlignmentInformation
.AlignmentRequirement
= 1; /* FIXME */
1685 info
->NameInformation
.FileNameLength
= 0;
1686 io
->Information
= sizeof(*info
) - sizeof(WCHAR
);
1690 case FileMailslotQueryInformation
:
1692 FILE_MAILSLOT_QUERY_INFORMATION
*info
= ptr
;
1694 SERVER_START_REQ( set_mailslot_info
)
1696 req
->handle
= hFile
;
1698 io
->u
.Status
= wine_server_call( req
);
1699 if( io
->u
.Status
== STATUS_SUCCESS
)
1701 info
->MaximumMessageSize
= reply
->max_msgsize
;
1702 info
->MailslotQuota
= 0;
1703 info
->NextMessageSize
= 0;
1704 info
->MessagesAvailable
= 0;
1705 info
->ReadTimeout
.QuadPart
= reply
->read_timeout
;
1712 ULONG size
= info
->MaximumMessageSize
? info
->MaximumMessageSize
: 0x10000;
1713 if (size
> 0x10000) size
= 0x10000;
1714 if ((tmpbuf
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1716 int fd
, needs_close
;
1717 if (!server_get_unix_fd( hFile
, FILE_READ_DATA
, &fd
, &needs_close
, NULL
, NULL
))
1719 int res
= recv( fd
, tmpbuf
, size
, MSG_PEEK
);
1720 info
->MessagesAvailable
= (res
> 0);
1721 info
->NextMessageSize
= (res
>= 0) ? res
: MAILSLOT_NO_MESSAGE
;
1722 if (needs_close
) close( fd
);
1724 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf
);
1729 case FilePipeLocalInformation
:
1731 FILE_PIPE_LOCAL_INFORMATION
* pli
= ptr
;
1733 SERVER_START_REQ( get_named_pipe_info
)
1735 req
->handle
= hFile
;
1736 if (!(io
->u
.Status
= wine_server_call( req
)))
1738 pli
->NamedPipeType
= (reply
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) ?
1739 FILE_PIPE_TYPE_MESSAGE
: FILE_PIPE_TYPE_BYTE
;
1740 pli
->NamedPipeConfiguration
= 0; /* FIXME */
1741 pli
->MaximumInstances
= reply
->maxinstances
;
1742 pli
->CurrentInstances
= reply
->instances
;
1743 pli
->InboundQuota
= reply
->insize
;
1744 pli
->ReadDataAvailable
= 0; /* FIXME */
1745 pli
->OutboundQuota
= reply
->outsize
;
1746 pli
->WriteQuotaAvailable
= 0; /* FIXME */
1747 pli
->NamedPipeState
= 0; /* FIXME */
1748 pli
->NamedPipeEnd
= (reply
->flags
& NAMED_PIPE_SERVER_END
) ?
1749 FILE_PIPE_SERVER_END
: FILE_PIPE_CLIENT_END
;
1756 FIXME("Unsupported class (%d)\n", class);
1757 io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
1760 if (needs_close
) close( fd
);
1761 if (io
->u
.Status
== STATUS_SUCCESS
&& !io
->Information
) io
->Information
= info_sizes
[class];
1762 return io
->u
.Status
;
1765 /******************************************************************************
1766 * NtSetInformationFile [NTDLL.@]
1767 * ZwSetInformationFile [NTDLL.@]
1769 * Set information about an open file handle.
1772 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1773 * io [O] Receives information about the operation on return
1774 * ptr [I] Source for file information
1775 * len [I] Size of FileInformation
1776 * class [I] Type of file information to set
1779 * Success: 0. io is updated.
1780 * Failure: An NTSTATUS error code describing the error.
1782 NTSTATUS WINAPI
NtSetInformationFile(HANDLE handle
, PIO_STATUS_BLOCK io
,
1783 PVOID ptr
, ULONG len
, FILE_INFORMATION_CLASS
class)
1785 int fd
, needs_close
;
1787 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle
, io
, ptr
, len
, class);
1789 io
->u
.Status
= STATUS_SUCCESS
;
1792 case FileBasicInformation
:
1793 if (len
>= sizeof(FILE_BASIC_INFORMATION
))
1796 const FILE_BASIC_INFORMATION
*info
= ptr
;
1798 if ((io
->u
.Status
= server_get_unix_fd( handle
, 0, &fd
, &needs_close
, NULL
, NULL
)))
1799 return io
->u
.Status
;
1801 if (info
->LastAccessTime
.QuadPart
|| info
->LastWriteTime
.QuadPart
)
1803 ULONGLONG sec
, nsec
;
1804 struct timeval tv
[2];
1806 if (!info
->LastAccessTime
.QuadPart
|| !info
->LastWriteTime
.QuadPart
)
1809 tv
[0].tv_sec
= tv
[0].tv_usec
= 0;
1810 tv
[1].tv_sec
= tv
[1].tv_usec
= 0;
1811 if (!fstat( fd
, &st
))
1813 tv
[0].tv_sec
= st
.st_atime
;
1814 tv
[1].tv_sec
= st
.st_mtime
;
1817 if (info
->LastAccessTime
.QuadPart
)
1819 sec
= RtlLargeIntegerDivide( info
->LastAccessTime
.QuadPart
, 10000000, &nsec
);
1820 tv
[0].tv_sec
= sec
- SECS_1601_TO_1970
;
1821 tv
[0].tv_usec
= (UINT
)nsec
/ 10;
1823 if (info
->LastWriteTime
.QuadPart
)
1825 sec
= RtlLargeIntegerDivide( info
->LastWriteTime
.QuadPart
, 10000000, &nsec
);
1826 tv
[1].tv_sec
= sec
- SECS_1601_TO_1970
;
1827 tv
[1].tv_usec
= (UINT
)nsec
/ 10;
1829 if (futimes( fd
, tv
) == -1) io
->u
.Status
= FILE_GetNtStatus();
1832 if (io
->u
.Status
== STATUS_SUCCESS
&& info
->FileAttributes
)
1834 if (fstat( fd
, &st
) == -1) io
->u
.Status
= FILE_GetNtStatus();
1837 if (info
->FileAttributes
& FILE_ATTRIBUTE_READONLY
)
1839 if (S_ISDIR( st
.st_mode
))
1840 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1842 st
.st_mode
&= ~0222; /* clear write permission bits */
1846 /* add write permission only where we already have read permission */
1847 st
.st_mode
|= (0600 | ((st
.st_mode
& 044) >> 1)) & (~FILE_umask
);
1849 if (fchmod( fd
, st
.st_mode
) == -1) io
->u
.Status
= FILE_GetNtStatus();
1853 if (needs_close
) close( fd
);
1855 else io
->u
.Status
= STATUS_INVALID_PARAMETER_3
;
1858 case FilePositionInformation
:
1859 if (len
>= sizeof(FILE_POSITION_INFORMATION
))
1861 const FILE_POSITION_INFORMATION
*info
= ptr
;
1863 if ((io
->u
.Status
= server_get_unix_fd( handle
, 0, &fd
, &needs_close
, NULL
, NULL
)))
1864 return io
->u
.Status
;
1866 if (lseek( fd
, info
->CurrentByteOffset
.QuadPart
, SEEK_SET
) == (off_t
)-1)
1867 io
->u
.Status
= FILE_GetNtStatus();
1869 if (needs_close
) close( fd
);
1871 else io
->u
.Status
= STATUS_INVALID_PARAMETER_3
;
1874 case FileEndOfFileInformation
:
1875 if (len
>= sizeof(FILE_END_OF_FILE_INFORMATION
))
1878 const FILE_END_OF_FILE_INFORMATION
*info
= ptr
;
1880 if ((io
->u
.Status
= server_get_unix_fd( handle
, 0, &fd
, &needs_close
, NULL
, NULL
)))
1881 return io
->u
.Status
;
1883 /* first try normal truncate */
1884 if (ftruncate( fd
, (off_t
)info
->EndOfFile
.QuadPart
) != -1) break;
1886 /* now check for the need to extend the file */
1887 if (fstat( fd
, &st
) != -1 && (off_t
)info
->EndOfFile
.QuadPart
> st
.st_size
)
1889 static const char zero
;
1891 /* extend the file one byte beyond the requested size and then truncate it */
1892 /* this should work around ftruncate implementations that can't extend files */
1893 if (pwrite( fd
, &zero
, 1, (off_t
)info
->EndOfFile
.QuadPart
) != -1 &&
1894 ftruncate( fd
, (off_t
)info
->EndOfFile
.QuadPart
) != -1) break;
1896 io
->u
.Status
= FILE_GetNtStatus();
1898 if (needs_close
) close( fd
);
1900 else io
->u
.Status
= STATUS_INVALID_PARAMETER_3
;
1903 case FileMailslotSetInformation
:
1905 FILE_MAILSLOT_SET_INFORMATION
*info
= ptr
;
1907 SERVER_START_REQ( set_mailslot_info
)
1909 req
->handle
= handle
;
1910 req
->flags
= MAILSLOT_SET_READ_TIMEOUT
;
1911 req
->read_timeout
= info
->ReadTimeout
.QuadPart
;
1912 io
->u
.Status
= wine_server_call( req
);
1918 case FileCompletionInformation
:
1919 if (len
>= sizeof(FILE_COMPLETION_INFORMATION
))
1921 FILE_COMPLETION_INFORMATION
*info
= (FILE_COMPLETION_INFORMATION
*)ptr
;
1923 SERVER_START_REQ( set_completion_info
)
1925 req
->handle
= handle
;
1926 req
->chandle
= info
->CompletionPort
;
1927 req
->ckey
= info
->CompletionKey
;
1928 io
->u
.Status
= wine_server_call( req
);
1932 io
->u
.Status
= STATUS_INVALID_PARAMETER_3
;
1936 FIXME("Unsupported class (%d)\n", class);
1937 io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
1940 io
->Information
= 0;
1941 return io
->u
.Status
;
1945 /******************************************************************************
1946 * NtQueryFullAttributesFile (NTDLL.@)
1948 NTSTATUS WINAPI
NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES
*attr
,
1949 FILE_NETWORK_OPEN_INFORMATION
*info
)
1951 ANSI_STRING unix_name
;
1954 if (!(status
= wine_nt_to_unix_file_name( attr
->ObjectName
, &unix_name
, FILE_OPEN
,
1955 !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
) )))
1959 if (stat( unix_name
.Buffer
, &st
) == -1)
1960 status
= FILE_GetNtStatus();
1961 else if (!S_ISREG(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
1962 status
= STATUS_INVALID_INFO_CLASS
;
1965 if (S_ISDIR(st
.st_mode
))
1967 info
->FileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
1968 info
->AllocationSize
.QuadPart
= 0;
1969 info
->EndOfFile
.QuadPart
= 0;
1973 info
->FileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1974 info
->AllocationSize
.QuadPart
= (ULONGLONG
)st
.st_blocks
* 512;
1975 info
->EndOfFile
.QuadPart
= st
.st_size
;
1977 if (!(st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)))
1978 info
->FileAttributes
|= FILE_ATTRIBUTE_READONLY
;
1979 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->CreationTime
);
1980 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->LastWriteTime
);
1981 RtlSecondsSince1970ToTime( st
.st_ctime
, &info
->ChangeTime
);
1982 RtlSecondsSince1970ToTime( st
.st_atime
, &info
->LastAccessTime
);
1983 if (DIR_is_hidden_file( attr
->ObjectName
))
1984 info
->FileAttributes
|= FILE_ATTRIBUTE_HIDDEN
;
1986 RtlFreeAnsiString( &unix_name
);
1988 else WARN("%s not found (%x)\n", debugstr_us(attr
->ObjectName
), status
);
1993 /******************************************************************************
1994 * NtQueryAttributesFile (NTDLL.@)
1995 * ZwQueryAttributesFile (NTDLL.@)
1997 NTSTATUS WINAPI
NtQueryAttributesFile( const OBJECT_ATTRIBUTES
*attr
, FILE_BASIC_INFORMATION
*info
)
1999 FILE_NETWORK_OPEN_INFORMATION full_info
;
2002 if (!(status
= NtQueryFullAttributesFile( attr
, &full_info
)))
2004 info
->CreationTime
.QuadPart
= full_info
.CreationTime
.QuadPart
;
2005 info
->LastAccessTime
.QuadPart
= full_info
.LastAccessTime
.QuadPart
;
2006 info
->LastWriteTime
.QuadPart
= full_info
.LastWriteTime
.QuadPart
;
2007 info
->ChangeTime
.QuadPart
= full_info
.ChangeTime
.QuadPart
;
2008 info
->FileAttributes
= full_info
.FileAttributes
;
2014 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
2015 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2016 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION
*info
, const char *fstypename
,
2017 unsigned int flags
)
2019 if (!strcmp("cd9660", fstypename
) || !strcmp("udf", fstypename
))
2021 info
->DeviceType
= FILE_DEVICE_CD_ROM_FILE_SYSTEM
;
2022 /* Don't assume read-only, let the mount options set it below */
2023 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
;
2025 else if (!strcmp("nfs", fstypename
) || !strcmp("nwfs", fstypename
) ||
2026 !strcmp("smbfs", fstypename
) || !strcmp("afpfs", fstypename
))
2028 info
->DeviceType
= FILE_DEVICE_NETWORK_FILE_SYSTEM
;
2029 info
->Characteristics
|= FILE_REMOTE_DEVICE
;
2031 else if (!strcmp("procfs", fstypename
))
2032 info
->DeviceType
= FILE_DEVICE_VIRTUAL_DISK
;
2034 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2036 if (flags
& MNT_RDONLY
)
2037 info
->Characteristics
|= FILE_READ_ONLY_DEVICE
;
2039 if (!(flags
& MNT_LOCAL
))
2041 info
->DeviceType
= FILE_DEVICE_NETWORK_FILE_SYSTEM
;
2042 info
->Characteristics
|= FILE_REMOTE_DEVICE
;
2047 static inline int is_device_placeholder( int fd
)
2049 static const char wine_placeholder
[] = "Wine device placeholder";
2050 char buffer
[sizeof(wine_placeholder
)-1];
2052 if (pread( fd
, buffer
, sizeof(wine_placeholder
) - 1, 0 ) != sizeof(wine_placeholder
) - 1)
2054 return !memcmp( buffer
, wine_placeholder
, sizeof(wine_placeholder
) - 1 );
2057 /******************************************************************************
2060 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2062 static NTSTATUS
get_device_info( int fd
, FILE_FS_DEVICE_INFORMATION
*info
)
2066 info
->Characteristics
= 0;
2067 if (fstat( fd
, &st
) < 0) return FILE_GetNtStatus();
2068 if (S_ISCHR( st
.st_mode
))
2070 info
->DeviceType
= FILE_DEVICE_UNKNOWN
;
2072 switch(major(st
.st_rdev
))
2075 info
->DeviceType
= FILE_DEVICE_NULL
;
2078 info
->DeviceType
= FILE_DEVICE_SERIAL_PORT
;
2081 info
->DeviceType
= FILE_DEVICE_PARALLEL_PORT
;
2083 case SCSI_TAPE_MAJOR
:
2084 info
->DeviceType
= FILE_DEVICE_TAPE
;
2089 else if (S_ISBLK( st
.st_mode
))
2091 info
->DeviceType
= FILE_DEVICE_DISK
;
2093 else if (S_ISFIFO( st
.st_mode
) || S_ISSOCK( st
.st_mode
))
2095 info
->DeviceType
= FILE_DEVICE_NAMED_PIPE
;
2097 else if (is_device_placeholder( fd
))
2099 info
->DeviceType
= FILE_DEVICE_DISK
;
2101 else /* regular file or directory */
2103 #if defined(linux) && defined(HAVE_FSTATFS)
2106 /* check for floppy disk */
2107 if (major(st
.st_dev
) == FLOPPY_MAJOR
)
2108 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
;
2110 if (fstatfs( fd
, &stfs
) < 0) stfs
.f_type
= 0;
2111 switch (stfs
.f_type
)
2113 case 0x9660: /* iso9660 */
2114 case 0x9fa1: /* supermount */
2115 case 0x15013346: /* udf */
2116 info
->DeviceType
= FILE_DEVICE_CD_ROM_FILE_SYSTEM
;
2117 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
|FILE_READ_ONLY_DEVICE
;
2119 case 0x6969: /* nfs */
2120 case 0x517B: /* smbfs */
2121 case 0x564c: /* ncpfs */
2122 info
->DeviceType
= FILE_DEVICE_NETWORK_FILE_SYSTEM
;
2123 info
->Characteristics
|= FILE_REMOTE_DEVICE
;
2125 case 0x01021994: /* tmpfs */
2126 case 0x28cd3d45: /* cramfs */
2127 case 0x1373: /* devfs */
2128 case 0x9fa0: /* procfs */
2129 info
->DeviceType
= FILE_DEVICE_VIRTUAL_DISK
;
2132 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2135 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
2138 if (fstatfs( fd
, &stfs
) < 0)
2139 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2141 get_device_info_fstatfs( info
, stfs
.f_fstypename
, stfs
.f_flags
);
2142 #elif defined(__NetBSD__)
2143 struct statvfs stfs
;
2145 if (fstatvfs( fd
, &stfs
) < 0)
2146 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2148 get_device_info_fstatfs( info
, stfs
.f_fstypename
, stfs
.f_flag
);
2150 /* Use dkio to work out device types */
2152 # include <sys/dkio.h>
2153 # include <sys/vtoc.h>
2154 struct dk_cinfo dkinf
;
2155 int retval
= ioctl(fd
, DKIOCINFO
, &dkinf
);
2157 WARN("Unable to get disk device type information - assuming a disk like device\n");
2158 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2160 switch (dkinf
.dki_ctype
)
2163 info
->DeviceType
= FILE_DEVICE_CD_ROM_FILE_SYSTEM
;
2164 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
|FILE_READ_ONLY_DEVICE
;
2168 case DKC_INTEL82072
:
2169 case DKC_INTEL82077
:
2170 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2171 info
->Characteristics
|= FILE_REMOVABLE_MEDIA
;
2174 info
->DeviceType
= FILE_DEVICE_VIRTUAL_DISK
;
2177 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2182 if (!warned
++) FIXME( "device info not properly supported on this platform\n" );
2183 info
->DeviceType
= FILE_DEVICE_DISK_FILE_SYSTEM
;
2185 info
->Characteristics
|= FILE_DEVICE_IS_MOUNTED
;
2187 return STATUS_SUCCESS
;
2191 /******************************************************************************
2192 * NtQueryVolumeInformationFile [NTDLL.@]
2193 * ZwQueryVolumeInformationFile [NTDLL.@]
2195 * Get volume information for an open file handle.
2198 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2199 * io [O] Receives information about the operation on return
2200 * buffer [O] Destination for volume information
2201 * length [I] Size of FsInformation
2202 * info_class [I] Type of volume information to set
2205 * Success: 0. io and buffer are updated.
2206 * Failure: An NTSTATUS error code describing the error.
2208 NTSTATUS WINAPI
NtQueryVolumeInformationFile( HANDLE handle
, PIO_STATUS_BLOCK io
,
2209 PVOID buffer
, ULONG length
,
2210 FS_INFORMATION_CLASS info_class
)
2212 int fd
, needs_close
;
2215 if ((io
->u
.Status
= server_get_unix_fd( handle
, 0, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
2216 return io
->u
.Status
;
2218 io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2219 io
->Information
= 0;
2221 switch( info_class
)
2223 case FileFsVolumeInformation
:
2224 FIXME( "%p: volume info not supported\n", handle
);
2226 case FileFsLabelInformation
:
2227 FIXME( "%p: label info not supported\n", handle
);
2229 case FileFsSizeInformation
:
2230 if (length
< sizeof(FILE_FS_SIZE_INFORMATION
))
2231 io
->u
.Status
= STATUS_BUFFER_TOO_SMALL
;
2234 FILE_FS_SIZE_INFORMATION
*info
= buffer
;
2236 if (fstat( fd
, &st
) < 0)
2238 io
->u
.Status
= FILE_GetNtStatus();
2241 if (!S_ISREG(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
2243 io
->u
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
2247 /* Linux's fstatvfs is buggy */
2248 #if !defined(linux) || !defined(HAVE_FSTATFS)
2249 struct statvfs stfs
;
2251 if (fstatvfs( fd
, &stfs
) < 0)
2253 io
->u
.Status
= FILE_GetNtStatus();
2256 info
->BytesPerSector
= stfs
.f_frsize
;
2259 if (fstatfs( fd
, &stfs
) < 0)
2261 io
->u
.Status
= FILE_GetNtStatus();
2264 info
->BytesPerSector
= stfs
.f_bsize
;
2266 info
->TotalAllocationUnits
.QuadPart
= stfs
.f_blocks
;
2267 info
->AvailableAllocationUnits
.QuadPart
= stfs
.f_bavail
;
2268 info
->SectorsPerAllocationUnit
= 1;
2269 io
->Information
= sizeof(*info
);
2270 io
->u
.Status
= STATUS_SUCCESS
;
2274 case FileFsDeviceInformation
:
2275 if (length
< sizeof(FILE_FS_DEVICE_INFORMATION
))
2276 io
->u
.Status
= STATUS_BUFFER_TOO_SMALL
;
2279 FILE_FS_DEVICE_INFORMATION
*info
= buffer
;
2281 if ((io
->u
.Status
= get_device_info( fd
, info
)) == STATUS_SUCCESS
)
2282 io
->Information
= sizeof(*info
);
2285 case FileFsAttributeInformation
:
2286 FIXME( "%p: attribute info not supported\n", handle
);
2288 case FileFsControlInformation
:
2289 FIXME( "%p: control info not supported\n", handle
);
2291 case FileFsFullSizeInformation
:
2292 FIXME( "%p: full size info not supported\n", handle
);
2294 case FileFsObjectIdInformation
:
2295 FIXME( "%p: object id info not supported\n", handle
);
2297 case FileFsMaximumInformation
:
2298 FIXME( "%p: maximum info not supported\n", handle
);
2301 io
->u
.Status
= STATUS_INVALID_PARAMETER
;
2304 if (needs_close
) close( fd
);
2305 return io
->u
.Status
;
2309 /******************************************************************
2310 * NtFlushBuffersFile (NTDLL.@)
2312 * Flush any buffered data on an open file handle.
2315 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2316 * IoStatusBlock [O] Receives information about the operation on return
2319 * Success: 0. IoStatusBlock is updated.
2320 * Failure: An NTSTATUS error code describing the error.
2322 NTSTATUS WINAPI
NtFlushBuffersFile( HANDLE hFile
, IO_STATUS_BLOCK
* IoStatusBlock
)
2325 HANDLE hEvent
= NULL
;
2327 SERVER_START_REQ( flush_file
)
2329 req
->handle
= hFile
;
2330 ret
= wine_server_call( req
);
2331 hEvent
= reply
->event
;
2336 ret
= NtWaitForSingleObject( hEvent
, FALSE
, NULL
);
2342 /******************************************************************
2343 * NtLockFile (NTDLL.@)
2347 NTSTATUS WINAPI
NtLockFile( HANDLE hFile
, HANDLE lock_granted_event
,
2348 PIO_APC_ROUTINE apc
, void* apc_user
,
2349 PIO_STATUS_BLOCK io_status
, PLARGE_INTEGER offset
,
2350 PLARGE_INTEGER count
, ULONG
* key
, BOOLEAN dont_wait
,
2357 if (apc
|| io_status
|| key
)
2359 FIXME("Unimplemented yet parameter\n");
2360 return STATUS_NOT_IMPLEMENTED
;
2363 if (apc_user
) FIXME("I/O completion on lock not implemented yet\n");
2367 SERVER_START_REQ( lock_file
)
2369 req
->handle
= hFile
;
2370 req
->offset
= offset
->QuadPart
;
2371 req
->count
= count
->QuadPart
;
2372 req
->shared
= !exclusive
;
2373 req
->wait
= !dont_wait
;
2374 ret
= wine_server_call( req
);
2375 handle
= reply
->handle
;
2376 async
= reply
->overlapped
;
2379 if (ret
!= STATUS_PENDING
)
2381 if (!ret
&& lock_granted_event
) NtSetEvent(lock_granted_event
, NULL
);
2387 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2388 if (handle
) NtClose( handle
);
2389 return STATUS_PENDING
;
2393 NtWaitForSingleObject( handle
, FALSE
, NULL
);
2400 /* Unix lock conflict, sleep a bit and retry */
2401 time
.QuadPart
= 100 * (ULONGLONG
)10000;
2402 time
.QuadPart
= -time
.QuadPart
;
2403 NtDelayExecution( FALSE
, &time
);
2409 /******************************************************************
2410 * NtUnlockFile (NTDLL.@)
2414 NTSTATUS WINAPI
NtUnlockFile( HANDLE hFile
, PIO_STATUS_BLOCK io_status
,
2415 PLARGE_INTEGER offset
, PLARGE_INTEGER count
,
2420 TRACE( "%p %x%08x %x%08x\n",
2421 hFile
, offset
->u
.HighPart
, offset
->u
.LowPart
, count
->u
.HighPart
, count
->u
.LowPart
);
2423 if (io_status
|| key
)
2425 FIXME("Unimplemented yet parameter\n");
2426 return STATUS_NOT_IMPLEMENTED
;
2429 SERVER_START_REQ( unlock_file
)
2431 req
->handle
= hFile
;
2432 req
->offset
= offset
->QuadPart
;
2433 req
->count
= count
->QuadPart
;
2434 status
= wine_server_call( req
);
2440 /******************************************************************
2441 * NtCreateNamedPipeFile (NTDLL.@)
2445 NTSTATUS WINAPI
NtCreateNamedPipeFile( PHANDLE handle
, ULONG access
,
2446 POBJECT_ATTRIBUTES attr
, PIO_STATUS_BLOCK iosb
,
2447 ULONG sharing
, ULONG dispo
, ULONG options
,
2448 ULONG pipe_type
, ULONG read_mode
,
2449 ULONG completion_mode
, ULONG max_inst
,
2450 ULONG inbound_quota
, ULONG outbound_quota
,
2451 PLARGE_INTEGER timeout
)
2455 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2456 handle
, access
, debugstr_w(attr
->ObjectName
->Buffer
), iosb
, sharing
, dispo
,
2457 options
, pipe_type
, read_mode
, completion_mode
, max_inst
, inbound_quota
,
2458 outbound_quota
, timeout
);
2460 /* assume we only get relative timeout */
2461 if (timeout
->QuadPart
> 0)
2462 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout
->QuadPart
));
2464 SERVER_START_REQ( create_named_pipe
)
2466 req
->access
= access
;
2467 req
->attributes
= attr
->Attributes
;
2468 req
->rootdir
= attr
->RootDirectory
;
2469 req
->options
= options
;
2471 (pipe_type
) ? NAMED_PIPE_MESSAGE_STREAM_WRITE
: 0 |
2472 (read_mode
) ? NAMED_PIPE_MESSAGE_STREAM_READ
: 0 |
2473 (completion_mode
) ? NAMED_PIPE_NONBLOCKING_MODE
: 0;
2474 req
->maxinstances
= max_inst
;
2475 req
->outsize
= outbound_quota
;
2476 req
->insize
= inbound_quota
;
2477 req
->timeout
= timeout
->QuadPart
;
2478 wine_server_add_data( req
, attr
->ObjectName
->Buffer
,
2479 attr
->ObjectName
->Length
);
2480 status
= wine_server_call( req
);
2481 if (!status
) *handle
= reply
->handle
;
2487 /******************************************************************
2488 * NtDeleteFile (NTDLL.@)
2492 NTSTATUS WINAPI
NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes
)
2498 TRACE("%p\n", ObjectAttributes
);
2499 status
= NtCreateFile( &hFile
, GENERIC_READ
| GENERIC_WRITE
| DELETE
,
2500 ObjectAttributes
, &io
, NULL
, 0,
2501 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2502 FILE_OPEN
, FILE_DELETE_ON_CLOSE
, NULL
, 0 );
2503 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
2507 /******************************************************************
2508 * NtCancelIoFile (NTDLL.@)
2512 NTSTATUS WINAPI
NtCancelIoFile( HANDLE hFile
, PIO_STATUS_BLOCK io_status
)
2514 LARGE_INTEGER timeout
;
2516 TRACE("%p %p\n", hFile
, io_status
);
2518 SERVER_START_REQ( cancel_async
)
2520 req
->handle
= hFile
;
2521 wine_server_call( req
);
2524 /* Let some APC be run, so that we can run the remaining APCs on hFile
2525 * either the cancelation of the pending one, but also the execution
2526 * of the queued APC, but not yet run. This is needed to ensure proper
2527 * clean-up of allocated data.
2529 timeout
.u
.LowPart
= timeout
.u
.HighPart
= 0;
2530 return io_status
->u
.Status
= NtDelayExecution( TRUE
, &timeout
);
2533 /******************************************************************************
2534 * NtCreateMailslotFile [NTDLL.@]
2535 * ZwCreateMailslotFile [NTDLL.@]
2538 * pHandle [O] pointer to receive the handle created
2539 * DesiredAccess [I] access mode (read, write, etc)
2540 * ObjectAttributes [I] fully qualified NT path of the mailslot
2541 * IoStatusBlock [O] receives completion status and other info
2544 * MaxMessageSize [I]
2550 NTSTATUS WINAPI
NtCreateMailslotFile(PHANDLE pHandle
, ULONG DesiredAccess
,
2551 POBJECT_ATTRIBUTES attr
, PIO_STATUS_BLOCK IoStatusBlock
,
2552 ULONG CreateOptions
, ULONG MailslotQuota
, ULONG MaxMessageSize
,
2553 PLARGE_INTEGER TimeOut
)
2555 LARGE_INTEGER timeout
;
2558 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2559 pHandle
, DesiredAccess
, attr
, IoStatusBlock
,
2560 CreateOptions
, MailslotQuota
, MaxMessageSize
, TimeOut
);
2562 if (!pHandle
) return STATUS_ACCESS_VIOLATION
;
2563 if (!attr
) return STATUS_INVALID_PARAMETER
;
2564 if (!attr
->ObjectName
) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2567 * For a NULL TimeOut pointer set the default timeout value
2570 timeout
.QuadPart
= -1;
2572 timeout
.QuadPart
= TimeOut
->QuadPart
;
2574 SERVER_START_REQ( create_mailslot
)
2576 req
->access
= DesiredAccess
;
2577 req
->attributes
= attr
->Attributes
;
2578 req
->rootdir
= attr
->RootDirectory
;
2579 req
->max_msgsize
= MaxMessageSize
;
2580 req
->read_timeout
= timeout
.QuadPart
;
2581 wine_server_add_data( req
, attr
->ObjectName
->Buffer
,
2582 attr
->ObjectName
->Length
);
2583 ret
= wine_server_call( req
);
2584 if( ret
== STATUS_SUCCESS
)
2585 *pHandle
= reply
->handle
;