server: Allow async i/o operations to send completion messages.
[wine/wine64.git] / dlls / ntdll / file.c
bloba3a4af16be0920b588a86c98eafb8e792b89cabd
1 /*
2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
32 #endif
33 #ifdef HAVE_LINUX_MAJOR_H
34 # include <linux/major.h>
35 #endif
36 #ifdef HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
38 #endif
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_POLL_H
49 #include <poll.h>
50 #endif
51 #ifdef HAVE_SYS_POLL_H
52 #include <sys/poll.h>
53 #endif
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
56 #endif
57 #ifdef HAVE_UTIME_H
58 # include <utime.h>
59 #endif
60 #ifdef HAVE_SYS_VFS_H
61 # include <sys/vfs.h>
62 #endif
63 #ifdef HAVE_SYS_MOUNT_H
64 # include <sys/mount.h>
65 #endif
66 #ifdef HAVE_SYS_STATFS_H
67 # include <sys/statfs.h>
68 #endif
70 #define NONAMELESSUNION
71 #define NONAMELESSSTRUCT
72 #include "ntstatus.h"
73 #define WIN32_NO_STATUS
74 #include "wine/unicode.h"
75 #include "wine/debug.h"
76 #include "thread.h"
77 #include "wine/server.h"
78 #include "ntdll_misc.h"
80 #include "winternl.h"
81 #include "winioctl.h"
82 #include "ddk/ntddser.h"
84 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
86 mode_t FILE_umask = 0;
88 #define SECSPERDAY 86400
89 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
91 /**************************************************************************
92 * NtOpenFile [NTDLL.@]
93 * ZwOpenFile [NTDLL.@]
95 * Open a file.
97 * PARAMS
98 * handle [O] Variable that receives the file handle on return
99 * access [I] Access desired by the caller to the file
100 * attr [I] Structure describing the file to be opened
101 * io [O] Receives details about the result of the operation
102 * sharing [I] Type of shared access the caller requires
103 * options [I] Options for the file open
105 * RETURNS
106 * Success: 0. FileHandle and IoStatusBlock are updated.
107 * Failure: An NTSTATUS error code describing the error.
109 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
110 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
111 ULONG sharing, ULONG options )
113 return NtCreateFile( handle, access, attr, io, NULL, 0,
114 sharing, FILE_OPEN, options, NULL, 0 );
117 /**************************************************************************
118 * NtCreateFile [NTDLL.@]
119 * ZwCreateFile [NTDLL.@]
121 * Either create a new file or directory, or open an existing file, device,
122 * directory or volume.
124 * PARAMS
125 * handle [O] Points to a variable which receives the file handle on return
126 * access [I] Desired access to the file
127 * attr [I] Structure describing the file
128 * io [O] Receives information about the operation on return
129 * alloc_size [I] Initial size of the file in bytes
130 * attributes [I] Attributes to create the file with
131 * sharing [I] Type of shared access the caller would like to the file
132 * disposition [I] Specifies what to do, depending on whether the file already exists
133 * options [I] Options for creating a new file
134 * ea_buffer [I] Pointer to an extended attributes buffer
135 * ea_length [I] Length of ea_buffer
137 * RETURNS
138 * Success: 0. handle and io are updated.
139 * Failure: An NTSTATUS error code describing the error.
141 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
142 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
143 ULONG attributes, ULONG sharing, ULONG disposition,
144 ULONG options, PVOID ea_buffer, ULONG ea_length )
146 ANSI_STRING unix_name;
147 int created = FALSE;
149 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p\n"
150 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
151 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
152 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
153 attributes, sharing, disposition, options, ea_buffer, ea_length );
155 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
157 if (alloc_size) FIXME( "alloc_size not supported\n" );
159 if (attr->RootDirectory)
161 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
162 return STATUS_OBJECT_NAME_NOT_FOUND;
165 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
166 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
168 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
170 SERVER_START_REQ( open_file_object )
172 req->access = access;
173 req->attributes = attr->Attributes;
174 req->rootdir = attr->RootDirectory;
175 req->sharing = sharing;
176 req->options = options;
177 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
178 io->u.Status = wine_server_call( req );
179 *handle = reply->handle;
181 SERVER_END_REQ;
182 return io->u.Status;
185 if (io->u.Status == STATUS_NO_SUCH_FILE &&
186 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
188 created = TRUE;
189 io->u.Status = STATUS_SUCCESS;
192 if (io->u.Status == STATUS_SUCCESS)
194 struct security_descriptor *sd = NULL;
195 struct object_attributes objattr;
197 objattr.rootdir = 0;
198 objattr.sd_len = 0;
199 objattr.name_len = 0;
200 if (attr)
202 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
203 if (io->u.Status != STATUS_SUCCESS)
205 RtlFreeAnsiString( &unix_name );
206 return io->u.Status;
210 SERVER_START_REQ( create_file )
212 req->access = access;
213 req->attributes = attr->Attributes;
214 req->sharing = sharing;
215 req->create = disposition;
216 req->options = options;
217 req->attrs = attributes;
218 wine_server_add_data( req, &objattr, sizeof(objattr) );
219 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
220 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
221 io->u.Status = wine_server_call( req );
222 *handle = reply->handle;
224 SERVER_END_REQ;
225 NTDLL_free_struct_sd( sd );
226 RtlFreeAnsiString( &unix_name );
228 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
230 if (io->u.Status == STATUS_SUCCESS)
232 if (created) io->Information = FILE_CREATED;
233 else switch(disposition)
235 case FILE_SUPERSEDE:
236 io->Information = FILE_SUPERSEDED;
237 break;
238 case FILE_CREATE:
239 io->Information = FILE_CREATED;
240 break;
241 case FILE_OPEN:
242 case FILE_OPEN_IF:
243 io->Information = FILE_OPENED;
244 break;
245 case FILE_OVERWRITE:
246 case FILE_OVERWRITE_IF:
247 io->Information = FILE_OVERWRITTEN;
248 break;
252 return io->u.Status;
255 /***********************************************************************
256 * Asynchronous file I/O *
259 struct async_fileio
261 HANDLE handle;
262 PIO_APC_ROUTINE apc;
263 void *apc_arg;
266 typedef struct
268 struct async_fileio io;
269 char* buffer;
270 unsigned int already;
271 unsigned int count;
272 BOOL avail_mode;
273 } async_fileio_read;
275 typedef struct
277 struct async_fileio io;
278 const char *buffer;
279 unsigned int already;
280 unsigned int count;
281 } async_fileio_write;
284 /* callback for file I/O user APC */
285 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
287 struct async_fileio *async = arg;
288 if (async->apc) async->apc( async->apc_arg, io, reserved );
289 RtlFreeHeap( GetProcessHeap(), 0, async );
292 /***********************************************************************
293 * FILE_GetNtStatus(void)
295 * Retrieve the Nt Status code from errno.
296 * Try to be consistent with FILE_SetDosError().
298 NTSTATUS FILE_GetNtStatus(void)
300 int err = errno;
302 TRACE( "errno = %d\n", errno );
303 switch (err)
305 case EAGAIN: return STATUS_SHARING_VIOLATION;
306 case EBADF: return STATUS_INVALID_HANDLE;
307 case EBUSY: return STATUS_DEVICE_BUSY;
308 case ENOSPC: return STATUS_DISK_FULL;
309 case EPERM:
310 case EROFS:
311 case EACCES: return STATUS_ACCESS_DENIED;
312 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
313 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
314 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
315 case EMFILE:
316 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
317 case EINVAL: return STATUS_INVALID_PARAMETER;
318 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
319 case EPIPE: return STATUS_PIPE_DISCONNECTED;
320 case EIO: return STATUS_DEVICE_NOT_READY;
321 #ifdef ENOMEDIUM
322 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
323 #endif
324 case ENXIO: return STATUS_NO_SUCH_DEVICE;
325 case ENOTTY:
326 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
327 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
328 case EFAULT: return STATUS_ACCESS_VIOLATION;
329 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
330 case ENOEXEC: /* ?? */
331 case EEXIST: /* ?? */
332 default:
333 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
334 return STATUS_UNSUCCESSFUL;
338 /***********************************************************************
339 * FILE_AsyncReadService (INTERNAL)
341 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status)
343 async_fileio_read *fileio = user;
344 int fd, needs_close, result;
346 switch (status)
348 case STATUS_ALERTED: /* got some new data */
349 /* check to see if the data is ready (non-blocking) */
350 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
351 &needs_close, NULL, NULL )))
352 break;
354 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
355 if (needs_close) close( fd );
357 if (result < 0)
359 if (errno == EAGAIN || errno == EINTR)
360 status = STATUS_PENDING;
361 else /* check to see if the transfer is complete */
362 status = FILE_GetNtStatus();
364 else if (result == 0)
366 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
368 else
370 fileio->already += result;
371 if (fileio->already >= fileio->count || fileio->avail_mode)
372 status = STATUS_SUCCESS;
373 else
375 /* if we only have to read the available data, and none is available,
376 * simply cancel the request. If data was available, it has been read
377 * while in by previous call (NtDelayExecution)
379 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
382 break;
384 case STATUS_TIMEOUT:
385 case STATUS_IO_TIMEOUT:
386 if (fileio->already) status = STATUS_SUCCESS;
387 break;
389 if (status != STATUS_PENDING)
391 iosb->u.Status = status;
392 iosb->Information = fileio->already;
394 return status;
397 struct io_timeouts
399 int interval; /* max interval between two bytes */
400 int total; /* total timeout for the whole operation */
401 int end_time; /* absolute time of end of operation */
404 /* retrieve the I/O timeouts to use for a given handle */
405 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
406 struct io_timeouts *timeouts )
408 NTSTATUS status = STATUS_SUCCESS;
410 timeouts->interval = timeouts->total = -1;
412 switch(type)
414 case FD_TYPE_SERIAL:
416 /* GetCommTimeouts */
417 SERIAL_TIMEOUTS st;
418 IO_STATUS_BLOCK io;
420 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
421 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
422 if (status) break;
424 if (is_read)
426 if (st.ReadIntervalTimeout)
427 timeouts->interval = st.ReadIntervalTimeout;
429 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
431 timeouts->total = st.ReadTotalTimeoutConstant;
432 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
433 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
435 else if (st.ReadIntervalTimeout == MAXDWORD)
436 timeouts->interval = 0;
438 else /* write */
440 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
442 timeouts->total = st.WriteTotalTimeoutConstant;
443 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
444 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
448 break;
449 case FD_TYPE_MAILSLOT:
450 if (is_read)
452 timeouts->interval = 0; /* return as soon as we got something */
453 SERVER_START_REQ( set_mailslot_info )
455 req->handle = handle;
456 req->flags = 0;
457 if (!(status = wine_server_call( req )) &&
458 reply->read_timeout != TIMEOUT_INFINITE)
459 timeouts->total = reply->read_timeout / -10000;
461 SERVER_END_REQ;
463 break;
464 case FD_TYPE_SOCKET:
465 case FD_TYPE_PIPE:
466 case FD_TYPE_CHAR:
467 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
468 break;
469 default:
470 break;
472 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
473 return STATUS_SUCCESS;
477 /* retrieve the timeout for the next wait, in milliseconds */
478 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
480 int ret = -1;
482 if (timeouts->total != -1)
484 ret = timeouts->end_time - NtGetTickCount();
485 if (ret < 0) ret = 0;
487 if (already && timeouts->interval != -1)
489 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
491 return ret;
495 /* retrieve the avail_mode flag for async reads */
496 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
498 NTSTATUS status = STATUS_SUCCESS;
500 switch(type)
502 case FD_TYPE_SERIAL:
504 /* GetCommTimeouts */
505 SERIAL_TIMEOUTS st;
506 IO_STATUS_BLOCK io;
508 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
509 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
510 if (status) break;
511 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
512 !st.ReadTotalTimeoutConstant &&
513 st.ReadIntervalTimeout == MAXDWORD);
515 break;
516 case FD_TYPE_MAILSLOT:
517 case FD_TYPE_SOCKET:
518 case FD_TYPE_PIPE:
519 case FD_TYPE_CHAR:
520 *avail_mode = TRUE;
521 break;
522 default:
523 *avail_mode = FALSE;
524 break;
526 return status;
530 /******************************************************************************
531 * NtReadFile [NTDLL.@]
532 * ZwReadFile [NTDLL.@]
534 * Read from an open file handle.
536 * PARAMS
537 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
538 * Event [I] Event to signal upon completion (or NULL)
539 * ApcRoutine [I] Callback to call upon completion (or NULL)
540 * ApcContext [I] Context for ApcRoutine (or NULL)
541 * IoStatusBlock [O] Receives information about the operation on return
542 * Buffer [O] Destination for the data read
543 * Length [I] Size of Buffer
544 * ByteOffset [O] Destination for the new file pointer position (or NULL)
545 * Key [O] Function unknown (may be NULL)
547 * RETURNS
548 * Success: 0. IoStatusBlock is updated, and the Information member contains
549 * The number of bytes read.
550 * Failure: An NTSTATUS error code describing the error.
552 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
553 PIO_APC_ROUTINE apc, void* apc_user,
554 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
555 PLARGE_INTEGER offset, PULONG key)
557 int result, unix_handle, needs_close, timeout_init_done = 0;
558 unsigned int options;
559 struct io_timeouts timeouts;
560 NTSTATUS status;
561 ULONG total = 0;
562 enum server_fd_type type;
564 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
565 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
567 if (!io_status) return STATUS_ACCESS_VIOLATION;
569 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
570 &needs_close, &type, &options );
571 if (status) return status;
573 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
575 /* async I/O doesn't make sense on regular files */
576 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
578 if (errno != EINTR)
580 status = FILE_GetNtStatus();
581 goto done;
584 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
585 /* update file pointer position */
586 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
588 total = result;
589 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
590 goto done;
593 for (;;)
595 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
597 total += result;
598 if (!result || total == length)
600 if (total)
601 status = STATUS_SUCCESS;
602 else
603 status = (type == FD_TYPE_FILE || type == FD_TYPE_CHAR) ? STATUS_END_OF_FILE : STATUS_PIPE_BROKEN;
604 goto done;
607 else
609 if (errno == EINTR) continue;
610 if (errno != EAGAIN)
612 status = FILE_GetNtStatus();
613 goto done;
617 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
619 async_fileio_read *fileio;
620 BOOL avail_mode;
622 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
623 goto done;
624 if (total && avail_mode)
626 status = STATUS_SUCCESS;
627 goto done;
630 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
632 status = STATUS_NO_MEMORY;
633 goto done;
635 fileio->io.handle = hFile;
636 fileio->io.apc = apc;
637 fileio->io.apc_arg = apc_user;
638 fileio->already = total;
639 fileio->count = length;
640 fileio->buffer = buffer;
641 fileio->avail_mode = avail_mode;
643 SERVER_START_REQ( register_async )
645 req->handle = hFile;
646 req->type = ASYNC_TYPE_READ;
647 req->count = length;
648 req->async.callback = FILE_AsyncReadService;
649 req->async.iosb = io_status;
650 req->async.arg = fileio;
651 req->async.apc = fileio_apc;
652 req->async.event = hEvent;
653 req->async.cvalue = 0;
654 status = wine_server_call( req );
656 SERVER_END_REQ;
658 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
659 goto done;
661 else /* synchronous read, wait for the fd to become ready */
663 struct pollfd pfd;
664 int ret, timeout;
666 if (!timeout_init_done)
668 timeout_init_done = 1;
669 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
670 goto done;
671 if (hEvent) NtResetEvent( hEvent, NULL );
673 timeout = get_next_io_timeout( &timeouts, total );
675 pfd.fd = unix_handle;
676 pfd.events = POLLIN;
678 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
680 if (total) /* return with what we got so far */
681 status = STATUS_SUCCESS;
682 else
683 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
684 goto done;
686 if (ret == -1 && errno != EINTR)
688 status = FILE_GetNtStatus();
689 goto done;
691 /* will now restart the read */
695 done:
696 if (needs_close) close( unix_handle );
697 if (status == STATUS_SUCCESS)
699 io_status->u.Status = status;
700 io_status->Information = total;
701 TRACE("= SUCCESS (%u)\n", total);
702 if (hEvent) NtSetEvent( hEvent, NULL );
703 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
704 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
706 else
708 TRACE("= 0x%08x\n", status);
709 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
711 return status;
714 /***********************************************************************
715 * FILE_AsyncWriteService (INTERNAL)
717 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status)
719 async_fileio_write *fileio = user;
720 int result, fd, needs_close;
721 enum server_fd_type type;
723 switch (status)
725 case STATUS_ALERTED:
726 /* write some data (non-blocking) */
727 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
728 &needs_close, &type, NULL )))
729 break;
731 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
732 result = send( fd, fileio->buffer, 0, 0 );
733 else
734 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
736 if (needs_close) close( fd );
738 if (result < 0)
740 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
741 else status = FILE_GetNtStatus();
743 else
745 fileio->already += result;
746 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
748 break;
750 case STATUS_TIMEOUT:
751 case STATUS_IO_TIMEOUT:
752 if (fileio->already) status = STATUS_SUCCESS;
753 break;
755 if (status != STATUS_PENDING)
757 iosb->u.Status = status;
758 iosb->Information = fileio->already;
760 return status;
763 /******************************************************************************
764 * NtWriteFile [NTDLL.@]
765 * ZwWriteFile [NTDLL.@]
767 * Write to an open file handle.
769 * PARAMS
770 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
771 * Event [I] Event to signal upon completion (or NULL)
772 * ApcRoutine [I] Callback to call upon completion (or NULL)
773 * ApcContext [I] Context for ApcRoutine (or NULL)
774 * IoStatusBlock [O] Receives information about the operation on return
775 * Buffer [I] Source for the data to write
776 * Length [I] Size of Buffer
777 * ByteOffset [O] Destination for the new file pointer position (or NULL)
778 * Key [O] Function unknown (may be NULL)
780 * RETURNS
781 * Success: 0. IoStatusBlock is updated, and the Information member contains
782 * The number of bytes written.
783 * Failure: An NTSTATUS error code describing the error.
785 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
786 PIO_APC_ROUTINE apc, void* apc_user,
787 PIO_STATUS_BLOCK io_status,
788 const void* buffer, ULONG length,
789 PLARGE_INTEGER offset, PULONG key)
791 int result, unix_handle, needs_close, timeout_init_done = 0;
792 unsigned int options;
793 struct io_timeouts timeouts;
794 NTSTATUS status;
795 ULONG total = 0;
796 enum server_fd_type type;
798 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
799 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
801 if (!io_status) return STATUS_ACCESS_VIOLATION;
803 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
804 &needs_close, &type, &options );
805 if (status) return status;
807 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
809 /* async I/O doesn't make sense on regular files */
810 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
812 if (errno != EINTR)
814 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
815 else status = FILE_GetNtStatus();
816 goto done;
820 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
821 /* update file pointer position */
822 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
824 total = result;
825 status = STATUS_SUCCESS;
826 goto done;
829 for (;;)
831 /* zero-length writes on sockets may not work with plain write(2) */
832 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
833 result = send( unix_handle, buffer, 0, 0 );
834 else
835 result = write( unix_handle, (const char *)buffer + total, length - total );
837 if (result >= 0)
839 total += result;
840 if (total == length)
842 status = STATUS_SUCCESS;
843 goto done;
846 else
848 if (errno == EINTR) continue;
849 if (errno != EAGAIN)
851 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
852 else status = FILE_GetNtStatus();
853 goto done;
857 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
859 async_fileio_write *fileio;
861 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
863 status = STATUS_NO_MEMORY;
864 goto done;
866 fileio->io.handle = hFile;
867 fileio->io.apc = apc;
868 fileio->io.apc_arg = apc_user;
869 fileio->already = total;
870 fileio->count = length;
871 fileio->buffer = buffer;
873 SERVER_START_REQ( register_async )
875 req->handle = hFile;
876 req->type = ASYNC_TYPE_WRITE;
877 req->count = length;
878 req->async.callback = FILE_AsyncWriteService;
879 req->async.iosb = io_status;
880 req->async.arg = fileio;
881 req->async.apc = fileio_apc;
882 req->async.event = hEvent;
883 req->async.cvalue = 0;
884 status = wine_server_call( req );
886 SERVER_END_REQ;
888 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
889 goto done;
891 else /* synchronous write, wait for the fd to become ready */
893 struct pollfd pfd;
894 int ret, timeout;
896 if (!timeout_init_done)
898 timeout_init_done = 1;
899 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
900 goto done;
901 if (hEvent) NtResetEvent( hEvent, NULL );
903 timeout = get_next_io_timeout( &timeouts, total );
905 pfd.fd = unix_handle;
906 pfd.events = POLLOUT;
908 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
910 /* return with what we got so far */
911 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
912 goto done;
914 if (ret == -1 && errno != EINTR)
916 status = FILE_GetNtStatus();
917 goto done;
919 /* will now restart the write */
923 done:
924 if (needs_close) close( unix_handle );
925 if (status == STATUS_SUCCESS)
927 io_status->u.Status = status;
928 io_status->Information = total;
929 TRACE("= SUCCESS (%u)\n", total);
930 if (hEvent) NtSetEvent( hEvent, NULL );
931 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
932 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
934 else
936 TRACE("= 0x%08x\n", status);
937 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
939 return status;
943 struct async_ioctl
945 HANDLE handle; /* handle to the device */
946 void *buffer; /* buffer for output */
947 ULONG size; /* size of buffer */
948 PIO_APC_ROUTINE apc; /* user apc params */
949 void *apc_arg;
952 /* callback for ioctl async I/O completion */
953 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status )
955 struct async_ioctl *async = arg;
957 if (status == STATUS_ALERTED)
959 SERVER_START_REQ( get_ioctl_result )
961 req->handle = async->handle;
962 req->user_arg = async;
963 wine_server_set_reply( req, async->buffer, async->size );
964 if (!(status = wine_server_call( req )))
965 io->Information = wine_server_reply_size( reply );
967 SERVER_END_REQ;
969 if (status != STATUS_PENDING) io->u.Status = status;
970 return status;
973 /* callback for ioctl user APC */
974 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
976 struct async_ioctl *async = arg;
977 if (async->apc) async->apc( async->apc_arg, io, reserved );
978 RtlFreeHeap( GetProcessHeap(), 0, async );
981 /* do a ioctl call through the server */
982 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
983 PIO_APC_ROUTINE apc, PVOID apc_context,
984 IO_STATUS_BLOCK *io, ULONG code,
985 const void *in_buffer, ULONG in_size,
986 PVOID out_buffer, ULONG out_size )
988 struct async_ioctl *async;
989 NTSTATUS status;
990 HANDLE wait_handle;
991 ULONG options;
993 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
994 return STATUS_NO_MEMORY;
995 async->handle = handle;
996 async->buffer = out_buffer;
997 async->size = out_size;
998 async->apc = apc;
999 async->apc_arg = apc_context;
1001 SERVER_START_REQ( ioctl )
1003 req->handle = handle;
1004 req->code = code;
1005 req->async.callback = ioctl_completion;
1006 req->async.iosb = io;
1007 req->async.arg = async;
1008 req->async.apc = (apc || event) ? ioctl_apc : NULL;
1009 req->async.event = event;
1010 req->async.cvalue = 0;
1011 wine_server_add_data( req, in_buffer, in_size );
1012 wine_server_set_reply( req, out_buffer, out_size );
1013 if (!(status = wine_server_call( req )))
1014 io->Information = wine_server_reply_size( reply );
1015 wait_handle = reply->wait;
1016 options = reply->options;
1018 SERVER_END_REQ;
1020 if (status == STATUS_NOT_SUPPORTED)
1021 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1022 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1024 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1026 if (wait_handle)
1028 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1029 status = io->u.Status;
1030 NtClose( wait_handle );
1031 RtlFreeHeap( GetProcessHeap(), 0, async );
1034 return status;
1038 /**************************************************************************
1039 * NtDeviceIoControlFile [NTDLL.@]
1040 * ZwDeviceIoControlFile [NTDLL.@]
1042 * Perform an I/O control operation on an open file handle.
1044 * PARAMS
1045 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1046 * event [I] Event to signal upon completion (or NULL)
1047 * apc [I] Callback to call upon completion (or NULL)
1048 * apc_context [I] Context for ApcRoutine (or NULL)
1049 * io [O] Receives information about the operation on return
1050 * code [I] Control code for the operation to perform
1051 * in_buffer [I] Source for any input data required (or NULL)
1052 * in_size [I] Size of InputBuffer
1053 * out_buffer [O] Source for any output data returned (or NULL)
1054 * out_size [I] Size of OutputBuffer
1056 * RETURNS
1057 * Success: 0. IoStatusBlock is updated.
1058 * Failure: An NTSTATUS error code describing the error.
1060 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1061 PIO_APC_ROUTINE apc, PVOID apc_context,
1062 PIO_STATUS_BLOCK io, ULONG code,
1063 PVOID in_buffer, ULONG in_size,
1064 PVOID out_buffer, ULONG out_size)
1066 ULONG device = (code >> 16);
1067 NTSTATUS status = STATUS_NOT_SUPPORTED;
1069 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1070 handle, event, apc, apc_context, io, code,
1071 in_buffer, in_size, out_buffer, out_size);
1073 switch(device)
1075 case FILE_DEVICE_DISK:
1076 case FILE_DEVICE_CD_ROM:
1077 case FILE_DEVICE_DVD:
1078 case FILE_DEVICE_CONTROLLER:
1079 case FILE_DEVICE_MASS_STORAGE:
1080 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1081 in_buffer, in_size, out_buffer, out_size);
1082 break;
1083 case FILE_DEVICE_SERIAL_PORT:
1084 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1085 in_buffer, in_size, out_buffer, out_size);
1086 break;
1087 case FILE_DEVICE_TAPE:
1088 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1089 in_buffer, in_size, out_buffer, out_size);
1090 break;
1093 if (status == STATUS_NOT_SUPPORTED)
1094 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1095 in_buffer, in_size, out_buffer, out_size );
1097 if (status != STATUS_PENDING) io->u.Status = status;
1098 return status;
1102 /**************************************************************************
1103 * NtFsControlFile [NTDLL.@]
1104 * ZwFsControlFile [NTDLL.@]
1106 * Perform a file system control operation on an open file handle.
1108 * PARAMS
1109 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1110 * event [I] Event to signal upon completion (or NULL)
1111 * apc [I] Callback to call upon completion (or NULL)
1112 * apc_context [I] Context for ApcRoutine (or NULL)
1113 * io [O] Receives information about the operation on return
1114 * code [I] Control code for the operation to perform
1115 * in_buffer [I] Source for any input data required (or NULL)
1116 * in_size [I] Size of InputBuffer
1117 * out_buffer [O] Source for any output data returned (or NULL)
1118 * out_size [I] Size of OutputBuffer
1120 * RETURNS
1121 * Success: 0. IoStatusBlock is updated.
1122 * Failure: An NTSTATUS error code describing the error.
1124 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1125 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1126 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1128 NTSTATUS status;
1130 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1131 handle, event, apc, apc_context, io, code,
1132 in_buffer, in_size, out_buffer, out_size);
1134 if (!io) return STATUS_INVALID_PARAMETER;
1136 switch(code)
1138 case FSCTL_DISMOUNT_VOLUME:
1139 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1140 in_buffer, in_size, out_buffer, out_size );
1141 if (!status) status = DIR_unmount_device( handle );
1142 break;
1144 case FSCTL_PIPE_PEEK:
1146 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1147 int avail = 0, fd, needs_close;
1149 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1151 status = STATUS_INFO_LENGTH_MISMATCH;
1152 break;
1155 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1156 break;
1158 #ifdef FIONREAD
1159 if (ioctl( fd, FIONREAD, &avail ) != 0)
1161 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1162 if (needs_close) close( fd );
1163 status = FILE_GetNtStatus();
1164 break;
1166 #endif
1167 if (!avail) /* check for closed pipe */
1169 struct pollfd pollfd;
1170 int ret;
1172 pollfd.fd = fd;
1173 pollfd.events = POLLIN;
1174 pollfd.revents = 0;
1175 ret = poll( &pollfd, 1, 0 );
1176 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1178 if (needs_close) close( fd );
1179 status = STATUS_PIPE_BROKEN;
1180 break;
1183 buffer->NamedPipeState = 0; /* FIXME */
1184 buffer->ReadDataAvailable = avail;
1185 buffer->NumberOfMessages = 0; /* FIXME */
1186 buffer->MessageLength = 0; /* FIXME */
1187 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1188 status = STATUS_SUCCESS;
1189 if (avail)
1191 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1192 if (data_size)
1194 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1195 if (res >= 0) io->Information += res;
1198 if (needs_close) close( fd );
1200 break;
1202 case FSCTL_PIPE_DISCONNECT:
1203 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1204 in_buffer, in_size, out_buffer, out_size );
1205 if (!status)
1207 int fd = server_remove_fd_from_cache( handle );
1208 if (fd != -1) close( fd );
1210 break;
1212 case FSCTL_PIPE_IMPERSONATE:
1213 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1214 status = RtlImpersonateSelf( SecurityImpersonation );
1215 break;
1217 case FSCTL_LOCK_VOLUME:
1218 case FSCTL_UNLOCK_VOLUME:
1219 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1220 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1221 status = STATUS_SUCCESS;
1222 break;
1224 case FSCTL_PIPE_LISTEN:
1225 case FSCTL_PIPE_WAIT:
1226 default:
1227 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1228 in_buffer, in_size, out_buffer, out_size );
1229 break;
1232 if (status != STATUS_PENDING) io->u.Status = status;
1233 return status;
1236 /******************************************************************************
1237 * NtSetVolumeInformationFile [NTDLL.@]
1238 * ZwSetVolumeInformationFile [NTDLL.@]
1240 * Set volume information for an open file handle.
1242 * PARAMS
1243 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1244 * IoStatusBlock [O] Receives information about the operation on return
1245 * FsInformation [I] Source for volume information
1246 * Length [I] Size of FsInformation
1247 * FsInformationClass [I] Type of volume information to set
1249 * RETURNS
1250 * Success: 0. IoStatusBlock is updated.
1251 * Failure: An NTSTATUS error code describing the error.
1253 NTSTATUS WINAPI NtSetVolumeInformationFile(
1254 IN HANDLE FileHandle,
1255 PIO_STATUS_BLOCK IoStatusBlock,
1256 PVOID FsInformation,
1257 ULONG Length,
1258 FS_INFORMATION_CLASS FsInformationClass)
1260 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1261 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1262 return 0;
1265 /******************************************************************************
1266 * NtQueryInformationFile [NTDLL.@]
1267 * ZwQueryInformationFile [NTDLL.@]
1269 * Get information about an open file handle.
1271 * PARAMS
1272 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1273 * io [O] Receives information about the operation on return
1274 * ptr [O] Destination for file information
1275 * len [I] Size of FileInformation
1276 * class [I] Type of file information to get
1278 * RETURNS
1279 * Success: 0. IoStatusBlock and FileInformation are updated.
1280 * Failure: An NTSTATUS error code describing the error.
1282 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1283 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1285 static const size_t info_sizes[] =
1288 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1289 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1290 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1291 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1292 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1293 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1294 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1295 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1296 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1297 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1298 0, /* FileLinkInformation */
1299 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1300 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1301 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1302 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1303 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1304 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1305 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1306 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1307 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1308 0, /* FileAlternateNameInformation */
1309 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1310 0, /* FilePipeInformation */
1311 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1312 0, /* FilePipeRemoteInformation */
1313 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1314 0, /* FileMailslotSetInformation */
1315 0, /* FileCompressionInformation */
1316 0, /* FileObjectIdInformation */
1317 0, /* FileCompletionInformation */
1318 0, /* FileMoveClusterInformation */
1319 0, /* FileQuotaInformation */
1320 0, /* FileReparsePointInformation */
1321 0, /* FileNetworkOpenInformation */
1322 0, /* FileAttributeTagInformation */
1323 0 /* FileTrackingInformation */
1326 struct stat st;
1327 int fd, needs_close = FALSE;
1329 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1331 io->Information = 0;
1333 if (class <= 0 || class >= FileMaximumInformation)
1334 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1335 if (!info_sizes[class])
1337 FIXME("Unsupported class (%d)\n", class);
1338 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1340 if (len < info_sizes[class])
1341 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1343 if (class != FilePipeLocalInformation)
1345 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1346 return io->u.Status;
1349 switch (class)
1351 case FileBasicInformation:
1353 FILE_BASIC_INFORMATION *info = ptr;
1355 if (fstat( fd, &st ) == -1)
1356 io->u.Status = FILE_GetNtStatus();
1357 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1358 io->u.Status = STATUS_INVALID_INFO_CLASS;
1359 else
1361 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1362 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1363 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1364 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1365 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1366 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1367 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1368 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1371 break;
1372 case FileStandardInformation:
1374 FILE_STANDARD_INFORMATION *info = ptr;
1376 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1377 else
1379 if ((info->Directory = S_ISDIR(st.st_mode)))
1381 info->AllocationSize.QuadPart = 0;
1382 info->EndOfFile.QuadPart = 0;
1383 info->NumberOfLinks = 1;
1384 info->DeletePending = FALSE;
1386 else
1388 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1389 info->EndOfFile.QuadPart = st.st_size;
1390 info->NumberOfLinks = st.st_nlink;
1391 info->DeletePending = FALSE; /* FIXME */
1395 break;
1396 case FilePositionInformation:
1398 FILE_POSITION_INFORMATION *info = ptr;
1399 off_t res = lseek( fd, 0, SEEK_CUR );
1400 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1401 else info->CurrentByteOffset.QuadPart = res;
1403 break;
1404 case FileInternalInformation:
1406 FILE_INTERNAL_INFORMATION *info = ptr;
1408 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1409 else info->IndexNumber.QuadPart = st.st_ino;
1411 break;
1412 case FileEaInformation:
1414 FILE_EA_INFORMATION *info = ptr;
1415 info->EaSize = 0;
1417 break;
1418 case FileEndOfFileInformation:
1420 FILE_END_OF_FILE_INFORMATION *info = ptr;
1422 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1423 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1425 break;
1426 case FileAllInformation:
1428 FILE_ALL_INFORMATION *info = ptr;
1430 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1431 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1432 io->u.Status = STATUS_INVALID_INFO_CLASS;
1433 else
1435 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1437 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1438 info->StandardInformation.AllocationSize.QuadPart = 0;
1439 info->StandardInformation.EndOfFile.QuadPart = 0;
1440 info->StandardInformation.NumberOfLinks = 1;
1441 info->StandardInformation.DeletePending = FALSE;
1443 else
1445 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1446 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1447 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1448 info->StandardInformation.NumberOfLinks = st.st_nlink;
1449 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1451 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1452 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1453 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1454 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1455 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1456 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1457 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1458 info->EaInformation.EaSize = 0;
1459 info->AccessInformation.AccessFlags = 0; /* FIXME */
1460 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1461 info->ModeInformation.Mode = 0; /* FIXME */
1462 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1463 info->NameInformation.FileNameLength = 0;
1464 io->Information = sizeof(*info) - sizeof(WCHAR);
1467 break;
1468 case FileMailslotQueryInformation:
1470 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1472 SERVER_START_REQ( set_mailslot_info )
1474 req->handle = hFile;
1475 req->flags = 0;
1476 io->u.Status = wine_server_call( req );
1477 if( io->u.Status == STATUS_SUCCESS )
1479 info->MaximumMessageSize = reply->max_msgsize;
1480 info->MailslotQuota = 0;
1481 info->NextMessageSize = 0;
1482 info->MessagesAvailable = 0;
1483 info->ReadTimeout.QuadPart = reply->read_timeout;
1486 SERVER_END_REQ;
1487 if (!io->u.Status)
1489 char *tmpbuf;
1490 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1491 if (size > 0x10000) size = 0x10000;
1492 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1494 int fd, needs_close;
1495 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1497 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1498 info->MessagesAvailable = (res > 0);
1499 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1500 if (needs_close) close( fd );
1502 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1506 break;
1507 case FilePipeLocalInformation:
1509 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1511 SERVER_START_REQ( get_named_pipe_info )
1513 req->handle = hFile;
1514 if (!(io->u.Status = wine_server_call( req )))
1516 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1517 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1518 pli->NamedPipeConfiguration = 0; /* FIXME */
1519 pli->MaximumInstances = reply->maxinstances;
1520 pli->CurrentInstances = reply->instances;
1521 pli->InboundQuota = reply->insize;
1522 pli->ReadDataAvailable = 0; /* FIXME */
1523 pli->OutboundQuota = reply->outsize;
1524 pli->WriteQuotaAvailable = 0; /* FIXME */
1525 pli->NamedPipeState = 0; /* FIXME */
1526 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1527 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1530 SERVER_END_REQ;
1532 break;
1533 default:
1534 FIXME("Unsupported class (%d)\n", class);
1535 io->u.Status = STATUS_NOT_IMPLEMENTED;
1536 break;
1538 if (needs_close) close( fd );
1539 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1540 return io->u.Status;
1543 /******************************************************************************
1544 * NtSetInformationFile [NTDLL.@]
1545 * ZwSetInformationFile [NTDLL.@]
1547 * Set information about an open file handle.
1549 * PARAMS
1550 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1551 * io [O] Receives information about the operation on return
1552 * ptr [I] Source for file information
1553 * len [I] Size of FileInformation
1554 * class [I] Type of file information to set
1556 * RETURNS
1557 * Success: 0. io is updated.
1558 * Failure: An NTSTATUS error code describing the error.
1560 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1561 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1563 int fd, needs_close;
1565 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1567 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1568 return io->u.Status;
1570 io->u.Status = STATUS_SUCCESS;
1571 switch (class)
1573 case FileBasicInformation:
1574 if (len >= sizeof(FILE_BASIC_INFORMATION))
1576 struct stat st;
1577 const FILE_BASIC_INFORMATION *info = ptr;
1579 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1581 ULONGLONG sec, nsec;
1582 struct timeval tv[2];
1584 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1587 tv[0].tv_sec = tv[0].tv_usec = 0;
1588 tv[1].tv_sec = tv[1].tv_usec = 0;
1589 if (!fstat( fd, &st ))
1591 tv[0].tv_sec = st.st_atime;
1592 tv[1].tv_sec = st.st_mtime;
1595 if (info->LastAccessTime.QuadPart)
1597 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1598 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1599 tv[0].tv_usec = (UINT)nsec / 10;
1601 if (info->LastWriteTime.QuadPart)
1603 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1604 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1605 tv[1].tv_usec = (UINT)nsec / 10;
1607 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1610 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1612 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1613 else
1615 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1617 if (S_ISDIR( st.st_mode))
1618 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1619 else
1620 st.st_mode &= ~0222; /* clear write permission bits */
1622 else
1624 /* add write permission only where we already have read permission */
1625 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1627 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1631 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1632 break;
1634 case FilePositionInformation:
1635 if (len >= sizeof(FILE_POSITION_INFORMATION))
1637 const FILE_POSITION_INFORMATION *info = ptr;
1639 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1640 io->u.Status = FILE_GetNtStatus();
1642 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1643 break;
1645 case FileEndOfFileInformation:
1646 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1648 struct stat st;
1649 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1651 /* first try normal truncate */
1652 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1654 /* now check for the need to extend the file */
1655 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1657 static const char zero;
1659 /* extend the file one byte beyond the requested size and then truncate it */
1660 /* this should work around ftruncate implementations that can't extend files */
1661 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1662 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1664 io->u.Status = FILE_GetNtStatus();
1666 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1667 break;
1669 case FileMailslotSetInformation:
1671 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1673 SERVER_START_REQ( set_mailslot_info )
1675 req->handle = handle;
1676 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1677 req->read_timeout = info->ReadTimeout.QuadPart;
1678 io->u.Status = wine_server_call( req );
1680 SERVER_END_REQ;
1682 break;
1684 case FileCompletionInformation:
1685 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
1687 FILE_COMPLETION_INFORMATION *info = (FILE_COMPLETION_INFORMATION *)ptr;
1689 SERVER_START_REQ( set_completion_info )
1691 req->handle = handle;
1692 req->chandle = info->CompletionPort;
1693 req->ckey = info->CompletionKey;
1694 io->u.Status = wine_server_call( req );
1696 SERVER_END_REQ;
1697 } else
1698 io->u.Status = STATUS_INVALID_PARAMETER_3;
1699 break;
1701 default:
1702 FIXME("Unsupported class (%d)\n", class);
1703 io->u.Status = STATUS_NOT_IMPLEMENTED;
1704 break;
1706 if (needs_close) close( fd );
1707 io->Information = 0;
1708 return io->u.Status;
1712 /******************************************************************************
1713 * NtQueryFullAttributesFile (NTDLL.@)
1715 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1716 FILE_NETWORK_OPEN_INFORMATION *info )
1718 ANSI_STRING unix_name;
1719 NTSTATUS status;
1721 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1722 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1724 struct stat st;
1726 if (stat( unix_name.Buffer, &st ) == -1)
1727 status = FILE_GetNtStatus();
1728 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1729 status = STATUS_INVALID_INFO_CLASS;
1730 else
1732 if (S_ISDIR(st.st_mode))
1734 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1735 info->AllocationSize.QuadPart = 0;
1736 info->EndOfFile.QuadPart = 0;
1738 else
1740 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1741 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1742 info->EndOfFile.QuadPart = st.st_size;
1744 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1745 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1746 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1747 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1748 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1749 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1750 if (DIR_is_hidden_file( attr->ObjectName ))
1751 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1753 RtlFreeAnsiString( &unix_name );
1755 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1756 return status;
1760 /******************************************************************************
1761 * NtQueryAttributesFile (NTDLL.@)
1762 * ZwQueryAttributesFile (NTDLL.@)
1764 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1766 FILE_NETWORK_OPEN_INFORMATION full_info;
1767 NTSTATUS status;
1769 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1771 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1772 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1773 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1774 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1775 info->FileAttributes = full_info.FileAttributes;
1777 return status;
1781 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1782 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1783 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1784 unsigned int flags )
1786 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
1788 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1789 /* Don't assume read-only, let the mount options set it below */
1790 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1792 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
1793 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
1795 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1796 info->Characteristics |= FILE_REMOTE_DEVICE;
1798 else if (!strcmp("procfs", fstypename))
1799 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1800 else
1801 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1803 if (flags & MNT_RDONLY)
1804 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1806 if (!(flags & MNT_LOCAL))
1808 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1809 info->Characteristics |= FILE_REMOTE_DEVICE;
1812 #endif
1814 static inline int is_device_placeholder( int fd )
1816 static const char wine_placeholder[] = "Wine device placeholder";
1817 char buffer[sizeof(wine_placeholder)-1];
1819 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
1820 return 0;
1821 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
1824 /******************************************************************************
1825 * get_device_info
1827 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1829 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1831 struct stat st;
1833 info->Characteristics = 0;
1834 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1835 if (S_ISCHR( st.st_mode ))
1837 info->DeviceType = FILE_DEVICE_UNKNOWN;
1838 #ifdef linux
1839 switch(major(st.st_rdev))
1841 case MEM_MAJOR:
1842 info->DeviceType = FILE_DEVICE_NULL;
1843 break;
1844 case TTY_MAJOR:
1845 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1846 break;
1847 case LP_MAJOR:
1848 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1849 break;
1850 case SCSI_TAPE_MAJOR:
1851 info->DeviceType = FILE_DEVICE_TAPE;
1852 break;
1854 #endif
1856 else if (S_ISBLK( st.st_mode ))
1858 info->DeviceType = FILE_DEVICE_DISK;
1860 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1862 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1864 else if (is_device_placeholder( fd ))
1866 info->DeviceType = FILE_DEVICE_DISK;
1868 else /* regular file or directory */
1870 #if defined(linux) && defined(HAVE_FSTATFS)
1871 struct statfs stfs;
1873 /* check for floppy disk */
1874 if (major(st.st_dev) == FLOPPY_MAJOR)
1875 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1877 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1878 switch (stfs.f_type)
1880 case 0x9660: /* iso9660 */
1881 case 0x9fa1: /* supermount */
1882 case 0x15013346: /* udf */
1883 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1884 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1885 break;
1886 case 0x6969: /* nfs */
1887 case 0x517B: /* smbfs */
1888 case 0x564c: /* ncpfs */
1889 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1890 info->Characteristics |= FILE_REMOTE_DEVICE;
1891 break;
1892 case 0x01021994: /* tmpfs */
1893 case 0x28cd3d45: /* cramfs */
1894 case 0x1373: /* devfs */
1895 case 0x9fa0: /* procfs */
1896 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1897 break;
1898 default:
1899 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1900 break;
1902 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1903 struct statfs stfs;
1905 if (fstatfs( fd, &stfs ) < 0)
1906 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1907 else
1908 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
1909 #elif defined(__NetBSD__)
1910 struct statvfs stfs;
1912 if (fstatvfs( fd, &stfs) < 0)
1913 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1914 else
1915 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
1916 #elif defined(sun)
1917 /* Use dkio to work out device types */
1919 # include <sys/dkio.h>
1920 # include <sys/vtoc.h>
1921 struct dk_cinfo dkinf;
1922 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1923 if(retval==-1){
1924 WARN("Unable to get disk device type information - assuming a disk like device\n");
1925 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1927 switch (dkinf.dki_ctype)
1929 case DKC_CDROM:
1930 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1931 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1932 break;
1933 case DKC_NCRFLOPPY:
1934 case DKC_SMSFLOPPY:
1935 case DKC_INTEL82072:
1936 case DKC_INTEL82077:
1937 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1938 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1939 break;
1940 case DKC_MD:
1941 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1942 break;
1943 default:
1944 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1947 #else
1948 static int warned;
1949 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1950 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1951 #endif
1952 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1954 return STATUS_SUCCESS;
1958 /******************************************************************************
1959 * NtQueryVolumeInformationFile [NTDLL.@]
1960 * ZwQueryVolumeInformationFile [NTDLL.@]
1962 * Get volume information for an open file handle.
1964 * PARAMS
1965 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1966 * io [O] Receives information about the operation on return
1967 * buffer [O] Destination for volume information
1968 * length [I] Size of FsInformation
1969 * info_class [I] Type of volume information to set
1971 * RETURNS
1972 * Success: 0. io and buffer are updated.
1973 * Failure: An NTSTATUS error code describing the error.
1975 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1976 PVOID buffer, ULONG length,
1977 FS_INFORMATION_CLASS info_class )
1979 int fd, needs_close;
1980 struct stat st;
1982 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1983 return io->u.Status;
1985 io->u.Status = STATUS_NOT_IMPLEMENTED;
1986 io->Information = 0;
1988 switch( info_class )
1990 case FileFsVolumeInformation:
1991 FIXME( "%p: volume info not supported\n", handle );
1992 break;
1993 case FileFsLabelInformation:
1994 FIXME( "%p: label info not supported\n", handle );
1995 break;
1996 case FileFsSizeInformation:
1997 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1998 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1999 else
2001 FILE_FS_SIZE_INFORMATION *info = buffer;
2003 if (fstat( fd, &st ) < 0)
2005 io->u.Status = FILE_GetNtStatus();
2006 break;
2008 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2010 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2012 else
2014 /* Linux's fstatvfs is buggy */
2015 #if !defined(linux) || !defined(HAVE_FSTATFS)
2016 struct statvfs stfs;
2018 if (fstatvfs( fd, &stfs ) < 0)
2020 io->u.Status = FILE_GetNtStatus();
2021 break;
2023 info->BytesPerSector = stfs.f_frsize;
2024 #else
2025 struct statfs stfs;
2026 if (fstatfs( fd, &stfs ) < 0)
2028 io->u.Status = FILE_GetNtStatus();
2029 break;
2031 info->BytesPerSector = stfs.f_bsize;
2032 #endif
2033 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
2034 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
2035 info->SectorsPerAllocationUnit = 1;
2036 io->Information = sizeof(*info);
2037 io->u.Status = STATUS_SUCCESS;
2040 break;
2041 case FileFsDeviceInformation:
2042 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2043 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2044 else
2046 FILE_FS_DEVICE_INFORMATION *info = buffer;
2048 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2049 io->Information = sizeof(*info);
2051 break;
2052 case FileFsAttributeInformation:
2053 FIXME( "%p: attribute info not supported\n", handle );
2054 break;
2055 case FileFsControlInformation:
2056 FIXME( "%p: control info not supported\n", handle );
2057 break;
2058 case FileFsFullSizeInformation:
2059 FIXME( "%p: full size info not supported\n", handle );
2060 break;
2061 case FileFsObjectIdInformation:
2062 FIXME( "%p: object id info not supported\n", handle );
2063 break;
2064 case FileFsMaximumInformation:
2065 FIXME( "%p: maximum info not supported\n", handle );
2066 break;
2067 default:
2068 io->u.Status = STATUS_INVALID_PARAMETER;
2069 break;
2071 if (needs_close) close( fd );
2072 return io->u.Status;
2076 /******************************************************************
2077 * NtFlushBuffersFile (NTDLL.@)
2079 * Flush any buffered data on an open file handle.
2081 * PARAMS
2082 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2083 * IoStatusBlock [O] Receives information about the operation on return
2085 * RETURNS
2086 * Success: 0. IoStatusBlock is updated.
2087 * Failure: An NTSTATUS error code describing the error.
2089 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2091 NTSTATUS ret;
2092 HANDLE hEvent = NULL;
2094 SERVER_START_REQ( flush_file )
2096 req->handle = hFile;
2097 ret = wine_server_call( req );
2098 hEvent = reply->event;
2100 SERVER_END_REQ;
2101 if (!ret && hEvent)
2103 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2104 NtClose( hEvent );
2106 return ret;
2109 /******************************************************************
2110 * NtLockFile (NTDLL.@)
2114 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2115 PIO_APC_ROUTINE apc, void* apc_user,
2116 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2117 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2118 BOOLEAN exclusive )
2120 NTSTATUS ret;
2121 HANDLE handle;
2122 BOOLEAN async;
2124 if (apc || io_status || key)
2126 FIXME("Unimplemented yet parameter\n");
2127 return STATUS_NOT_IMPLEMENTED;
2130 for (;;)
2132 SERVER_START_REQ( lock_file )
2134 req->handle = hFile;
2135 req->offset = offset->QuadPart;
2136 req->count = count->QuadPart;
2137 req->shared = !exclusive;
2138 req->wait = !dont_wait;
2139 ret = wine_server_call( req );
2140 handle = reply->handle;
2141 async = reply->overlapped;
2143 SERVER_END_REQ;
2144 if (ret != STATUS_PENDING)
2146 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2147 return ret;
2150 if (async)
2152 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2153 if (handle) NtClose( handle );
2154 return STATUS_PENDING;
2156 if (handle)
2158 NtWaitForSingleObject( handle, FALSE, NULL );
2159 NtClose( handle );
2161 else
2163 LARGE_INTEGER time;
2165 /* Unix lock conflict, sleep a bit and retry */
2166 time.QuadPart = 100 * (ULONGLONG)10000;
2167 time.QuadPart = -time.QuadPart;
2168 NtDelayExecution( FALSE, &time );
2174 /******************************************************************
2175 * NtUnlockFile (NTDLL.@)
2179 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2180 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2181 PULONG key )
2183 NTSTATUS status;
2185 TRACE( "%p %x%08x %x%08x\n",
2186 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2188 if (io_status || key)
2190 FIXME("Unimplemented yet parameter\n");
2191 return STATUS_NOT_IMPLEMENTED;
2194 SERVER_START_REQ( unlock_file )
2196 req->handle = hFile;
2197 req->offset = offset->QuadPart;
2198 req->count = count->QuadPart;
2199 status = wine_server_call( req );
2201 SERVER_END_REQ;
2202 return status;
2205 /******************************************************************
2206 * NtCreateNamedPipeFile (NTDLL.@)
2210 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2211 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2212 ULONG sharing, ULONG dispo, ULONG options,
2213 ULONG pipe_type, ULONG read_mode,
2214 ULONG completion_mode, ULONG max_inst,
2215 ULONG inbound_quota, ULONG outbound_quota,
2216 PLARGE_INTEGER timeout)
2218 NTSTATUS status;
2220 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2221 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2222 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2223 outbound_quota, timeout);
2225 /* assume we only get relative timeout */
2226 if (timeout->QuadPart > 0)
2227 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2229 SERVER_START_REQ( create_named_pipe )
2231 req->access = access;
2232 req->attributes = attr->Attributes;
2233 req->rootdir = attr->RootDirectory;
2234 req->options = options;
2235 req->flags =
2236 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2237 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2238 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2239 req->maxinstances = max_inst;
2240 req->outsize = outbound_quota;
2241 req->insize = inbound_quota;
2242 req->timeout = timeout->QuadPart;
2243 wine_server_add_data( req, attr->ObjectName->Buffer,
2244 attr->ObjectName->Length );
2245 status = wine_server_call( req );
2246 if (!status) *handle = reply->handle;
2248 SERVER_END_REQ;
2249 return status;
2252 /******************************************************************
2253 * NtDeleteFile (NTDLL.@)
2257 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2259 NTSTATUS status;
2260 HANDLE hFile;
2261 IO_STATUS_BLOCK io;
2263 TRACE("%p\n", ObjectAttributes);
2264 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2265 ObjectAttributes, &io, NULL, 0,
2266 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2267 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2268 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2269 return status;
2272 /******************************************************************
2273 * NtCancelIoFile (NTDLL.@)
2277 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2279 LARGE_INTEGER timeout;
2281 TRACE("%p %p\n", hFile, io_status );
2283 SERVER_START_REQ( cancel_async )
2285 req->handle = hFile;
2286 wine_server_call( req );
2288 SERVER_END_REQ;
2289 /* Let some APC be run, so that we can run the remaining APCs on hFile
2290 * either the cancelation of the pending one, but also the execution
2291 * of the queued APC, but not yet run. This is needed to ensure proper
2292 * clean-up of allocated data.
2294 timeout.u.LowPart = timeout.u.HighPart = 0;
2295 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2298 /******************************************************************************
2299 * NtCreateMailslotFile [NTDLL.@]
2300 * ZwCreateMailslotFile [NTDLL.@]
2302 * PARAMS
2303 * pHandle [O] pointer to receive the handle created
2304 * DesiredAccess [I] access mode (read, write, etc)
2305 * ObjectAttributes [I] fully qualified NT path of the mailslot
2306 * IoStatusBlock [O] receives completion status and other info
2307 * CreateOptions [I]
2308 * MailslotQuota [I]
2309 * MaxMessageSize [I]
2310 * TimeOut [I]
2312 * RETURNS
2313 * An NT status code
2315 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2316 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2317 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2318 PLARGE_INTEGER TimeOut)
2320 LARGE_INTEGER timeout;
2321 NTSTATUS ret;
2323 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2324 pHandle, DesiredAccess, attr, IoStatusBlock,
2325 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2327 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2328 if (!attr) return STATUS_INVALID_PARAMETER;
2329 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2332 * For a NULL TimeOut pointer set the default timeout value
2334 if (!TimeOut)
2335 timeout.QuadPart = -1;
2336 else
2337 timeout.QuadPart = TimeOut->QuadPart;
2339 SERVER_START_REQ( create_mailslot )
2341 req->access = DesiredAccess;
2342 req->attributes = attr->Attributes;
2343 req->rootdir = attr->RootDirectory;
2344 req->max_msgsize = MaxMessageSize;
2345 req->read_timeout = timeout.QuadPart;
2346 wine_server_add_data( req, attr->ObjectName->Buffer,
2347 attr->ObjectName->Length );
2348 ret = wine_server_call( req );
2349 if( ret == STATUS_SUCCESS )
2350 *pHandle = reply->handle;
2352 SERVER_END_REQ;
2354 return ret;