msxml3: Implement IXMLDOMCDATASection_substringData.
[wine/wine64.git] / dlls / ntdll / file.c
blobc7b944b6a9b0ea1ad8741e72f0095e371b50e737
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 "wine/server.h"
77 #include "ntdll_misc.h"
79 #include "winternl.h"
80 #include "winioctl.h"
81 #include "ddk/ntddser.h"
83 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
85 mode_t FILE_umask = 0;
87 #define SECSPERDAY 86400
88 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
90 /**************************************************************************
91 * NtOpenFile [NTDLL.@]
92 * ZwOpenFile [NTDLL.@]
94 * Open a file.
96 * PARAMS
97 * handle [O] Variable that receives the file handle on return
98 * access [I] Access desired by the caller to the file
99 * attr [I] Structure describing the file to be opened
100 * io [O] Receives details about the result of the operation
101 * sharing [I] Type of shared access the caller requires
102 * options [I] Options for the file open
104 * RETURNS
105 * Success: 0. FileHandle and IoStatusBlock are updated.
106 * Failure: An NTSTATUS error code describing the error.
108 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
109 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
110 ULONG sharing, ULONG options )
112 return NtCreateFile( handle, access, attr, io, NULL, 0,
113 sharing, FILE_OPEN, options, NULL, 0 );
116 /**************************************************************************
117 * NtCreateFile [NTDLL.@]
118 * ZwCreateFile [NTDLL.@]
120 * Either create a new file or directory, or open an existing file, device,
121 * directory or volume.
123 * PARAMS
124 * handle [O] Points to a variable which receives the file handle on return
125 * access [I] Desired access to the file
126 * attr [I] Structure describing the file
127 * io [O] Receives information about the operation on return
128 * alloc_size [I] Initial size of the file in bytes
129 * attributes [I] Attributes to create the file with
130 * sharing [I] Type of shared access the caller would like to the file
131 * disposition [I] Specifies what to do, depending on whether the file already exists
132 * options [I] Options for creating a new file
133 * ea_buffer [I] Pointer to an extended attributes buffer
134 * ea_length [I] Length of ea_buffer
136 * RETURNS
137 * Success: 0. handle and io are updated.
138 * Failure: An NTSTATUS error code describing the error.
140 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
141 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
142 ULONG attributes, ULONG sharing, ULONG disposition,
143 ULONG options, PVOID ea_buffer, ULONG ea_length )
145 ANSI_STRING unix_name;
146 int created = FALSE;
148 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p\n"
149 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
150 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
151 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
152 attributes, sharing, disposition, options, ea_buffer, ea_length );
154 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
156 if (alloc_size) FIXME( "alloc_size not supported\n" );
158 if (attr->RootDirectory)
160 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
161 return STATUS_OBJECT_NAME_NOT_FOUND;
164 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
165 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
167 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
169 SERVER_START_REQ( open_file_object )
171 req->access = access;
172 req->attributes = attr->Attributes;
173 req->rootdir = attr->RootDirectory;
174 req->sharing = sharing;
175 req->options = options;
176 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
177 io->u.Status = wine_server_call( req );
178 *handle = reply->handle;
180 SERVER_END_REQ;
181 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
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, ULONG_PTR *total)
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 = *total = 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;
563 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
565 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
566 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
568 if (!io_status) return STATUS_ACCESS_VIOLATION;
570 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
571 &needs_close, &type, &options );
572 if (status) return status;
574 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
576 /* async I/O doesn't make sense on regular files */
577 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
579 if (errno != EINTR)
581 status = FILE_GetNtStatus();
582 goto done;
585 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
586 /* update file pointer position */
587 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
589 total = result;
590 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
591 goto done;
594 for (;;)
596 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
598 total += result;
599 if (!result || total == length)
601 if (total)
602 status = STATUS_SUCCESS;
603 else
604 status = (type == FD_TYPE_FILE || type == FD_TYPE_CHAR) ? STATUS_END_OF_FILE : STATUS_PIPE_BROKEN;
605 goto done;
608 else
610 if (errno == EINTR) continue;
611 if (errno != EAGAIN)
613 status = FILE_GetNtStatus();
614 goto done;
618 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
620 async_fileio_read *fileio;
621 BOOL avail_mode;
623 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
624 goto err;
625 if (total && avail_mode)
627 status = STATUS_SUCCESS;
628 goto done;
631 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
633 status = STATUS_NO_MEMORY;
634 goto err;
636 fileio->io.handle = hFile;
637 fileio->io.apc = apc;
638 fileio->io.apc_arg = apc_user;
639 fileio->already = total;
640 fileio->count = length;
641 fileio->buffer = buffer;
642 fileio->avail_mode = avail_mode;
644 SERVER_START_REQ( register_async )
646 req->handle = hFile;
647 req->type = ASYNC_TYPE_READ;
648 req->count = length;
649 req->async.callback = FILE_AsyncReadService;
650 req->async.iosb = io_status;
651 req->async.arg = fileio;
652 req->async.apc = fileio_apc;
653 req->async.event = hEvent;
654 req->async.cvalue = cvalue;
655 status = wine_server_call( req );
657 SERVER_END_REQ;
659 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
660 goto err;
662 else /* synchronous read, wait for the fd to become ready */
664 struct pollfd pfd;
665 int ret, timeout;
667 if (!timeout_init_done)
669 timeout_init_done = 1;
670 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
671 goto err;
672 if (hEvent) NtResetEvent( hEvent, NULL );
674 timeout = get_next_io_timeout( &timeouts, total );
676 pfd.fd = unix_handle;
677 pfd.events = POLLIN;
679 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
681 if (total) /* return with what we got so far */
682 status = STATUS_SUCCESS;
683 else
684 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
685 goto done;
687 if (ret == -1 && errno != EINTR)
689 status = FILE_GetNtStatus();
690 goto done;
692 /* will now restart the read */
696 done:
697 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
699 err:
700 if (needs_close) close( unix_handle );
701 if (status == STATUS_SUCCESS)
703 io_status->u.Status = status;
704 io_status->Information = total;
705 TRACE("= SUCCESS (%u)\n", total);
706 if (hEvent) NtSetEvent( hEvent, NULL );
707 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
708 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
710 else
712 TRACE("= 0x%08x\n", status);
713 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
715 return status;
718 /***********************************************************************
719 * FILE_AsyncWriteService (INTERNAL)
721 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status, ULONG_PTR *total)
723 async_fileio_write *fileio = user;
724 int result, fd, needs_close;
725 enum server_fd_type type;
727 switch (status)
729 case STATUS_ALERTED:
730 /* write some data (non-blocking) */
731 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
732 &needs_close, &type, NULL )))
733 break;
735 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
736 result = send( fd, fileio->buffer, 0, 0 );
737 else
738 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
740 if (needs_close) close( fd );
742 if (result < 0)
744 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
745 else status = FILE_GetNtStatus();
747 else
749 fileio->already += result;
750 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
752 break;
754 case STATUS_TIMEOUT:
755 case STATUS_IO_TIMEOUT:
756 if (fileio->already) status = STATUS_SUCCESS;
757 break;
759 if (status != STATUS_PENDING)
761 iosb->u.Status = status;
762 iosb->Information = *total = fileio->already;
764 return status;
767 /******************************************************************************
768 * NtWriteFile [NTDLL.@]
769 * ZwWriteFile [NTDLL.@]
771 * Write to an open file handle.
773 * PARAMS
774 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
775 * Event [I] Event to signal upon completion (or NULL)
776 * ApcRoutine [I] Callback to call upon completion (or NULL)
777 * ApcContext [I] Context for ApcRoutine (or NULL)
778 * IoStatusBlock [O] Receives information about the operation on return
779 * Buffer [I] Source for the data to write
780 * Length [I] Size of Buffer
781 * ByteOffset [O] Destination for the new file pointer position (or NULL)
782 * Key [O] Function unknown (may be NULL)
784 * RETURNS
785 * Success: 0. IoStatusBlock is updated, and the Information member contains
786 * The number of bytes written.
787 * Failure: An NTSTATUS error code describing the error.
789 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
790 PIO_APC_ROUTINE apc, void* apc_user,
791 PIO_STATUS_BLOCK io_status,
792 const void* buffer, ULONG length,
793 PLARGE_INTEGER offset, PULONG key)
795 int result, unix_handle, needs_close, timeout_init_done = 0;
796 unsigned int options;
797 struct io_timeouts timeouts;
798 NTSTATUS status;
799 ULONG total = 0;
800 enum server_fd_type type;
801 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
803 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
804 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
806 if (!io_status) return STATUS_ACCESS_VIOLATION;
808 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
809 &needs_close, &type, &options );
810 if (status) return status;
812 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
814 /* async I/O doesn't make sense on regular files */
815 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
817 if (errno != EINTR)
819 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
820 else status = FILE_GetNtStatus();
821 goto done;
825 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
826 /* update file pointer position */
827 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
829 total = result;
830 status = STATUS_SUCCESS;
831 goto done;
834 for (;;)
836 /* zero-length writes on sockets may not work with plain write(2) */
837 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
838 result = send( unix_handle, buffer, 0, 0 );
839 else
840 result = write( unix_handle, (const char *)buffer + total, length - total );
842 if (result >= 0)
844 total += result;
845 if (total == length)
847 status = STATUS_SUCCESS;
848 goto done;
851 else
853 if (errno == EINTR) continue;
854 if (errno != EAGAIN)
856 if (errno == EFAULT)
858 status = STATUS_INVALID_USER_BUFFER;
859 goto err;
861 status = FILE_GetNtStatus();
862 goto done;
866 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
868 async_fileio_write *fileio;
870 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
872 status = STATUS_NO_MEMORY;
873 goto err;
875 fileio->io.handle = hFile;
876 fileio->io.apc = apc;
877 fileio->io.apc_arg = apc_user;
878 fileio->already = total;
879 fileio->count = length;
880 fileio->buffer = buffer;
882 SERVER_START_REQ( register_async )
884 req->handle = hFile;
885 req->type = ASYNC_TYPE_WRITE;
886 req->count = length;
887 req->async.callback = FILE_AsyncWriteService;
888 req->async.iosb = io_status;
889 req->async.arg = fileio;
890 req->async.apc = fileio_apc;
891 req->async.event = hEvent;
892 req->async.cvalue = cvalue;
893 status = wine_server_call( req );
895 SERVER_END_REQ;
897 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
898 goto err;
900 else /* synchronous write, wait for the fd to become ready */
902 struct pollfd pfd;
903 int ret, timeout;
905 if (!timeout_init_done)
907 timeout_init_done = 1;
908 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
909 goto err;
910 if (hEvent) NtResetEvent( hEvent, NULL );
912 timeout = get_next_io_timeout( &timeouts, total );
914 pfd.fd = unix_handle;
915 pfd.events = POLLOUT;
917 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
919 /* return with what we got so far */
920 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
921 goto done;
923 if (ret == -1 && errno != EINTR)
925 status = FILE_GetNtStatus();
926 goto done;
928 /* will now restart the write */
932 done:
933 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
935 err:
936 if (needs_close) close( unix_handle );
937 if (status == STATUS_SUCCESS)
939 io_status->u.Status = status;
940 io_status->Information = total;
941 TRACE("= SUCCESS (%u)\n", total);
942 if (hEvent) NtSetEvent( hEvent, NULL );
943 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
944 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
946 else
948 TRACE("= 0x%08x\n", status);
949 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
951 return status;
955 struct async_ioctl
957 HANDLE handle; /* handle to the device */
958 void *buffer; /* buffer for output */
959 ULONG size; /* size of buffer */
960 PIO_APC_ROUTINE apc; /* user apc params */
961 void *apc_arg;
964 /* callback for ioctl async I/O completion */
965 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status )
967 struct async_ioctl *async = arg;
969 if (status == STATUS_ALERTED)
971 SERVER_START_REQ( get_ioctl_result )
973 req->handle = async->handle;
974 req->user_arg = async;
975 wine_server_set_reply( req, async->buffer, async->size );
976 if (!(status = wine_server_call( req )))
977 io->Information = wine_server_reply_size( reply );
979 SERVER_END_REQ;
981 if (status != STATUS_PENDING) io->u.Status = status;
982 return status;
985 /* callback for ioctl user APC */
986 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
988 struct async_ioctl *async = arg;
989 if (async->apc) async->apc( async->apc_arg, io, reserved );
990 RtlFreeHeap( GetProcessHeap(), 0, async );
993 /* do a ioctl call through the server */
994 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
995 PIO_APC_ROUTINE apc, PVOID apc_context,
996 IO_STATUS_BLOCK *io, ULONG code,
997 const void *in_buffer, ULONG in_size,
998 PVOID out_buffer, ULONG out_size )
1000 struct async_ioctl *async;
1001 NTSTATUS status;
1002 HANDLE wait_handle;
1003 ULONG options;
1004 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1006 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
1007 return STATUS_NO_MEMORY;
1008 async->handle = handle;
1009 async->buffer = out_buffer;
1010 async->size = out_size;
1011 async->apc = apc;
1012 async->apc_arg = apc_context;
1014 SERVER_START_REQ( ioctl )
1016 req->handle = handle;
1017 req->code = code;
1018 req->async.callback = ioctl_completion;
1019 req->async.iosb = io;
1020 req->async.arg = async;
1021 req->async.apc = (apc || event) ? ioctl_apc : NULL;
1022 req->async.event = event;
1023 req->async.cvalue = cvalue;
1024 wine_server_add_data( req, in_buffer, in_size );
1025 wine_server_set_reply( req, out_buffer, out_size );
1026 if (!(status = wine_server_call( req )))
1027 io->Information = wine_server_reply_size( reply );
1028 wait_handle = reply->wait;
1029 options = reply->options;
1031 SERVER_END_REQ;
1033 if (status == STATUS_NOT_SUPPORTED)
1034 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1035 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1037 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1039 if (wait_handle)
1041 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1042 status = io->u.Status;
1043 NtClose( wait_handle );
1044 RtlFreeHeap( GetProcessHeap(), 0, async );
1047 return status;
1051 /**************************************************************************
1052 * NtDeviceIoControlFile [NTDLL.@]
1053 * ZwDeviceIoControlFile [NTDLL.@]
1055 * Perform an I/O control operation on an open file handle.
1057 * PARAMS
1058 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1059 * event [I] Event to signal upon completion (or NULL)
1060 * apc [I] Callback to call upon completion (or NULL)
1061 * apc_context [I] Context for ApcRoutine (or NULL)
1062 * io [O] Receives information about the operation on return
1063 * code [I] Control code for the operation to perform
1064 * in_buffer [I] Source for any input data required (or NULL)
1065 * in_size [I] Size of InputBuffer
1066 * out_buffer [O] Source for any output data returned (or NULL)
1067 * out_size [I] Size of OutputBuffer
1069 * RETURNS
1070 * Success: 0. IoStatusBlock is updated.
1071 * Failure: An NTSTATUS error code describing the error.
1073 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1074 PIO_APC_ROUTINE apc, PVOID apc_context,
1075 PIO_STATUS_BLOCK io, ULONG code,
1076 PVOID in_buffer, ULONG in_size,
1077 PVOID out_buffer, ULONG out_size)
1079 ULONG device = (code >> 16);
1080 NTSTATUS status = STATUS_NOT_SUPPORTED;
1082 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1083 handle, event, apc, apc_context, io, code,
1084 in_buffer, in_size, out_buffer, out_size);
1086 switch(device)
1088 case FILE_DEVICE_DISK:
1089 case FILE_DEVICE_CD_ROM:
1090 case FILE_DEVICE_DVD:
1091 case FILE_DEVICE_CONTROLLER:
1092 case FILE_DEVICE_MASS_STORAGE:
1093 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1094 in_buffer, in_size, out_buffer, out_size);
1095 break;
1096 case FILE_DEVICE_SERIAL_PORT:
1097 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1098 in_buffer, in_size, out_buffer, out_size);
1099 break;
1100 case FILE_DEVICE_TAPE:
1101 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1102 in_buffer, in_size, out_buffer, out_size);
1103 break;
1106 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1107 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1108 in_buffer, in_size, out_buffer, out_size );
1110 if (status != STATUS_PENDING) io->u.Status = status;
1111 return status;
1115 /**************************************************************************
1116 * NtFsControlFile [NTDLL.@]
1117 * ZwFsControlFile [NTDLL.@]
1119 * Perform a file system control operation on an open file handle.
1121 * PARAMS
1122 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1123 * event [I] Event to signal upon completion (or NULL)
1124 * apc [I] Callback to call upon completion (or NULL)
1125 * apc_context [I] Context for ApcRoutine (or NULL)
1126 * io [O] Receives information about the operation on return
1127 * code [I] Control code for the operation to perform
1128 * in_buffer [I] Source for any input data required (or NULL)
1129 * in_size [I] Size of InputBuffer
1130 * out_buffer [O] Source for any output data returned (or NULL)
1131 * out_size [I] Size of OutputBuffer
1133 * RETURNS
1134 * Success: 0. IoStatusBlock is updated.
1135 * Failure: An NTSTATUS error code describing the error.
1137 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1138 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1139 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1141 NTSTATUS status;
1143 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1144 handle, event, apc, apc_context, io, code,
1145 in_buffer, in_size, out_buffer, out_size);
1147 if (!io) return STATUS_INVALID_PARAMETER;
1149 switch(code)
1151 case FSCTL_DISMOUNT_VOLUME:
1152 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1153 in_buffer, in_size, out_buffer, out_size );
1154 if (!status) status = DIR_unmount_device( handle );
1155 break;
1157 case FSCTL_PIPE_PEEK:
1159 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1160 int avail = 0, fd, needs_close;
1162 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1164 status = STATUS_INFO_LENGTH_MISMATCH;
1165 break;
1168 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1169 break;
1171 #ifdef FIONREAD
1172 if (ioctl( fd, FIONREAD, &avail ) != 0)
1174 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1175 if (needs_close) close( fd );
1176 status = FILE_GetNtStatus();
1177 break;
1179 #endif
1180 if (!avail) /* check for closed pipe */
1182 struct pollfd pollfd;
1183 int ret;
1185 pollfd.fd = fd;
1186 pollfd.events = POLLIN;
1187 pollfd.revents = 0;
1188 ret = poll( &pollfd, 1, 0 );
1189 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1191 if (needs_close) close( fd );
1192 status = STATUS_PIPE_BROKEN;
1193 break;
1196 buffer->NamedPipeState = 0; /* FIXME */
1197 buffer->ReadDataAvailable = avail;
1198 buffer->NumberOfMessages = 0; /* FIXME */
1199 buffer->MessageLength = 0; /* FIXME */
1200 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1201 status = STATUS_SUCCESS;
1202 if (avail)
1204 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1205 if (data_size)
1207 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1208 if (res >= 0) io->Information += res;
1211 if (needs_close) close( fd );
1213 break;
1215 case FSCTL_PIPE_DISCONNECT:
1216 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1217 in_buffer, in_size, out_buffer, out_size );
1218 if (!status)
1220 int fd = server_remove_fd_from_cache( handle );
1221 if (fd != -1) close( fd );
1223 break;
1225 case FSCTL_PIPE_IMPERSONATE:
1226 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1227 status = RtlImpersonateSelf( SecurityImpersonation );
1228 break;
1230 case FSCTL_LOCK_VOLUME:
1231 case FSCTL_UNLOCK_VOLUME:
1232 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1233 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1234 status = STATUS_SUCCESS;
1235 break;
1237 case FSCTL_PIPE_LISTEN:
1238 case FSCTL_PIPE_WAIT:
1239 default:
1240 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1241 in_buffer, in_size, out_buffer, out_size );
1242 break;
1245 if (status != STATUS_PENDING) io->u.Status = status;
1246 return status;
1249 /******************************************************************************
1250 * NtSetVolumeInformationFile [NTDLL.@]
1251 * ZwSetVolumeInformationFile [NTDLL.@]
1253 * Set volume information for an open file handle.
1255 * PARAMS
1256 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1257 * IoStatusBlock [O] Receives information about the operation on return
1258 * FsInformation [I] Source for volume information
1259 * Length [I] Size of FsInformation
1260 * FsInformationClass [I] Type of volume information to set
1262 * RETURNS
1263 * Success: 0. IoStatusBlock is updated.
1264 * Failure: An NTSTATUS error code describing the error.
1266 NTSTATUS WINAPI NtSetVolumeInformationFile(
1267 IN HANDLE FileHandle,
1268 PIO_STATUS_BLOCK IoStatusBlock,
1269 PVOID FsInformation,
1270 ULONG Length,
1271 FS_INFORMATION_CLASS FsInformationClass)
1273 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1274 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1275 return 0;
1278 /******************************************************************************
1279 * NtQueryInformationFile [NTDLL.@]
1280 * ZwQueryInformationFile [NTDLL.@]
1282 * Get information about an open file handle.
1284 * PARAMS
1285 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1286 * io [O] Receives information about the operation on return
1287 * ptr [O] Destination for file information
1288 * len [I] Size of FileInformation
1289 * class [I] Type of file information to get
1291 * RETURNS
1292 * Success: 0. IoStatusBlock and FileInformation are updated.
1293 * Failure: An NTSTATUS error code describing the error.
1295 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1296 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1298 static const size_t info_sizes[] =
1301 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1302 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1303 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1304 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1305 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1306 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1307 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1308 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1309 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1310 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1311 0, /* FileLinkInformation */
1312 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1313 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1314 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1315 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1316 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1317 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1318 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1319 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1320 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1321 0, /* FileAlternateNameInformation */
1322 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1323 0, /* FilePipeInformation */
1324 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1325 0, /* FilePipeRemoteInformation */
1326 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1327 0, /* FileMailslotSetInformation */
1328 0, /* FileCompressionInformation */
1329 0, /* FileObjectIdInformation */
1330 0, /* FileCompletionInformation */
1331 0, /* FileMoveClusterInformation */
1332 0, /* FileQuotaInformation */
1333 0, /* FileReparsePointInformation */
1334 0, /* FileNetworkOpenInformation */
1335 0, /* FileAttributeTagInformation */
1336 0 /* FileTrackingInformation */
1339 struct stat st;
1340 int fd, needs_close = FALSE;
1342 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1344 io->Information = 0;
1346 if (class <= 0 || class >= FileMaximumInformation)
1347 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1348 if (!info_sizes[class])
1350 FIXME("Unsupported class (%d)\n", class);
1351 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1353 if (len < info_sizes[class])
1354 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1356 if (class != FilePipeLocalInformation)
1358 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1359 return io->u.Status;
1362 switch (class)
1364 case FileBasicInformation:
1366 FILE_BASIC_INFORMATION *info = ptr;
1368 if (fstat( fd, &st ) == -1)
1369 io->u.Status = FILE_GetNtStatus();
1370 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1371 io->u.Status = STATUS_INVALID_INFO_CLASS;
1372 else
1374 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1375 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1376 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1377 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1378 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1379 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1380 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1381 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1384 break;
1385 case FileStandardInformation:
1387 FILE_STANDARD_INFORMATION *info = ptr;
1389 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1390 else
1392 if ((info->Directory = S_ISDIR(st.st_mode)))
1394 info->AllocationSize.QuadPart = 0;
1395 info->EndOfFile.QuadPart = 0;
1396 info->NumberOfLinks = 1;
1397 info->DeletePending = FALSE;
1399 else
1401 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1402 info->EndOfFile.QuadPart = st.st_size;
1403 info->NumberOfLinks = st.st_nlink;
1404 info->DeletePending = FALSE; /* FIXME */
1408 break;
1409 case FilePositionInformation:
1411 FILE_POSITION_INFORMATION *info = ptr;
1412 off_t res = lseek( fd, 0, SEEK_CUR );
1413 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1414 else info->CurrentByteOffset.QuadPart = res;
1416 break;
1417 case FileInternalInformation:
1419 FILE_INTERNAL_INFORMATION *info = ptr;
1421 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1422 else info->IndexNumber.QuadPart = st.st_ino;
1424 break;
1425 case FileEaInformation:
1427 FILE_EA_INFORMATION *info = ptr;
1428 info->EaSize = 0;
1430 break;
1431 case FileEndOfFileInformation:
1433 FILE_END_OF_FILE_INFORMATION *info = ptr;
1435 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1436 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1438 break;
1439 case FileAllInformation:
1441 FILE_ALL_INFORMATION *info = ptr;
1443 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1444 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1445 io->u.Status = STATUS_INVALID_INFO_CLASS;
1446 else
1448 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1450 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1451 info->StandardInformation.AllocationSize.QuadPart = 0;
1452 info->StandardInformation.EndOfFile.QuadPart = 0;
1453 info->StandardInformation.NumberOfLinks = 1;
1454 info->StandardInformation.DeletePending = FALSE;
1456 else
1458 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1459 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1460 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1461 info->StandardInformation.NumberOfLinks = st.st_nlink;
1462 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1464 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1465 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1466 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1467 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1468 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1469 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1470 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1471 info->EaInformation.EaSize = 0;
1472 info->AccessInformation.AccessFlags = 0; /* FIXME */
1473 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1474 info->ModeInformation.Mode = 0; /* FIXME */
1475 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1476 info->NameInformation.FileNameLength = 0;
1477 io->Information = sizeof(*info) - sizeof(WCHAR);
1480 break;
1481 case FileMailslotQueryInformation:
1483 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1485 SERVER_START_REQ( set_mailslot_info )
1487 req->handle = hFile;
1488 req->flags = 0;
1489 io->u.Status = wine_server_call( req );
1490 if( io->u.Status == STATUS_SUCCESS )
1492 info->MaximumMessageSize = reply->max_msgsize;
1493 info->MailslotQuota = 0;
1494 info->NextMessageSize = 0;
1495 info->MessagesAvailable = 0;
1496 info->ReadTimeout.QuadPart = reply->read_timeout;
1499 SERVER_END_REQ;
1500 if (!io->u.Status)
1502 char *tmpbuf;
1503 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1504 if (size > 0x10000) size = 0x10000;
1505 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1507 int fd, needs_close;
1508 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1510 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1511 info->MessagesAvailable = (res > 0);
1512 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1513 if (needs_close) close( fd );
1515 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1519 break;
1520 case FilePipeLocalInformation:
1522 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1524 SERVER_START_REQ( get_named_pipe_info )
1526 req->handle = hFile;
1527 if (!(io->u.Status = wine_server_call( req )))
1529 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1530 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1531 pli->NamedPipeConfiguration = 0; /* FIXME */
1532 pli->MaximumInstances = reply->maxinstances;
1533 pli->CurrentInstances = reply->instances;
1534 pli->InboundQuota = reply->insize;
1535 pli->ReadDataAvailable = 0; /* FIXME */
1536 pli->OutboundQuota = reply->outsize;
1537 pli->WriteQuotaAvailable = 0; /* FIXME */
1538 pli->NamedPipeState = 0; /* FIXME */
1539 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1540 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1543 SERVER_END_REQ;
1545 break;
1546 default:
1547 FIXME("Unsupported class (%d)\n", class);
1548 io->u.Status = STATUS_NOT_IMPLEMENTED;
1549 break;
1551 if (needs_close) close( fd );
1552 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1553 return io->u.Status;
1556 /******************************************************************************
1557 * NtSetInformationFile [NTDLL.@]
1558 * ZwSetInformationFile [NTDLL.@]
1560 * Set information about an open file handle.
1562 * PARAMS
1563 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1564 * io [O] Receives information about the operation on return
1565 * ptr [I] Source for file information
1566 * len [I] Size of FileInformation
1567 * class [I] Type of file information to set
1569 * RETURNS
1570 * Success: 0. io is updated.
1571 * Failure: An NTSTATUS error code describing the error.
1573 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1574 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1576 int fd, needs_close;
1578 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1580 io->u.Status = STATUS_SUCCESS;
1581 switch (class)
1583 case FileBasicInformation:
1584 if (len >= sizeof(FILE_BASIC_INFORMATION))
1586 struct stat st;
1587 const FILE_BASIC_INFORMATION *info = ptr;
1589 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1590 return io->u.Status;
1592 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1594 ULONGLONG sec, nsec;
1595 struct timeval tv[2];
1597 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1600 tv[0].tv_sec = tv[0].tv_usec = 0;
1601 tv[1].tv_sec = tv[1].tv_usec = 0;
1602 if (!fstat( fd, &st ))
1604 tv[0].tv_sec = st.st_atime;
1605 tv[1].tv_sec = st.st_mtime;
1608 if (info->LastAccessTime.QuadPart)
1610 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1611 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1612 tv[0].tv_usec = (UINT)nsec / 10;
1614 if (info->LastWriteTime.QuadPart)
1616 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1617 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1618 tv[1].tv_usec = (UINT)nsec / 10;
1620 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1623 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1625 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1626 else
1628 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1630 if (S_ISDIR( st.st_mode))
1631 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1632 else
1633 st.st_mode &= ~0222; /* clear write permission bits */
1635 else
1637 /* add write permission only where we already have read permission */
1638 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1640 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1644 if (needs_close) close( fd );
1646 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1647 break;
1649 case FilePositionInformation:
1650 if (len >= sizeof(FILE_POSITION_INFORMATION))
1652 const FILE_POSITION_INFORMATION *info = ptr;
1654 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1655 return io->u.Status;
1657 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1658 io->u.Status = FILE_GetNtStatus();
1660 if (needs_close) close( fd );
1662 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1663 break;
1665 case FileEndOfFileInformation:
1666 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1668 struct stat st;
1669 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1671 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1672 return io->u.Status;
1674 /* first try normal truncate */
1675 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1677 /* now check for the need to extend the file */
1678 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1680 static const char zero;
1682 /* extend the file one byte beyond the requested size and then truncate it */
1683 /* this should work around ftruncate implementations that can't extend files */
1684 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1685 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1687 io->u.Status = FILE_GetNtStatus();
1689 if (needs_close) close( fd );
1691 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1692 break;
1694 case FileMailslotSetInformation:
1696 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1698 SERVER_START_REQ( set_mailslot_info )
1700 req->handle = handle;
1701 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1702 req->read_timeout = info->ReadTimeout.QuadPart;
1703 io->u.Status = wine_server_call( req );
1705 SERVER_END_REQ;
1707 break;
1709 case FileCompletionInformation:
1710 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
1712 FILE_COMPLETION_INFORMATION *info = (FILE_COMPLETION_INFORMATION *)ptr;
1714 SERVER_START_REQ( set_completion_info )
1716 req->handle = handle;
1717 req->chandle = info->CompletionPort;
1718 req->ckey = info->CompletionKey;
1719 io->u.Status = wine_server_call( req );
1721 SERVER_END_REQ;
1722 } else
1723 io->u.Status = STATUS_INVALID_PARAMETER_3;
1724 break;
1726 default:
1727 FIXME("Unsupported class (%d)\n", class);
1728 io->u.Status = STATUS_NOT_IMPLEMENTED;
1729 break;
1731 io->Information = 0;
1732 return io->u.Status;
1736 /******************************************************************************
1737 * NtQueryFullAttributesFile (NTDLL.@)
1739 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1740 FILE_NETWORK_OPEN_INFORMATION *info )
1742 ANSI_STRING unix_name;
1743 NTSTATUS status;
1745 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1746 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1748 struct stat st;
1750 if (stat( unix_name.Buffer, &st ) == -1)
1751 status = FILE_GetNtStatus();
1752 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1753 status = STATUS_INVALID_INFO_CLASS;
1754 else
1756 if (S_ISDIR(st.st_mode))
1758 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1759 info->AllocationSize.QuadPart = 0;
1760 info->EndOfFile.QuadPart = 0;
1762 else
1764 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1765 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1766 info->EndOfFile.QuadPart = st.st_size;
1768 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1769 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1770 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1771 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1772 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1773 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1774 if (DIR_is_hidden_file( attr->ObjectName ))
1775 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1777 RtlFreeAnsiString( &unix_name );
1779 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1780 return status;
1784 /******************************************************************************
1785 * NtQueryAttributesFile (NTDLL.@)
1786 * ZwQueryAttributesFile (NTDLL.@)
1788 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1790 FILE_NETWORK_OPEN_INFORMATION full_info;
1791 NTSTATUS status;
1793 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1795 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1796 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1797 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1798 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1799 info->FileAttributes = full_info.FileAttributes;
1801 return status;
1805 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1806 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1807 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1808 unsigned int flags )
1810 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
1812 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1813 /* Don't assume read-only, let the mount options set it below */
1814 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1816 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
1817 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
1819 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1820 info->Characteristics |= FILE_REMOTE_DEVICE;
1822 else if (!strcmp("procfs", fstypename))
1823 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1824 else
1825 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1827 if (flags & MNT_RDONLY)
1828 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1830 if (!(flags & MNT_LOCAL))
1832 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1833 info->Characteristics |= FILE_REMOTE_DEVICE;
1836 #endif
1838 static inline int is_device_placeholder( int fd )
1840 static const char wine_placeholder[] = "Wine device placeholder";
1841 char buffer[sizeof(wine_placeholder)-1];
1843 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
1844 return 0;
1845 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
1848 /******************************************************************************
1849 * get_device_info
1851 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1853 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1855 struct stat st;
1857 info->Characteristics = 0;
1858 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1859 if (S_ISCHR( st.st_mode ))
1861 info->DeviceType = FILE_DEVICE_UNKNOWN;
1862 #ifdef linux
1863 switch(major(st.st_rdev))
1865 case MEM_MAJOR:
1866 info->DeviceType = FILE_DEVICE_NULL;
1867 break;
1868 case TTY_MAJOR:
1869 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1870 break;
1871 case LP_MAJOR:
1872 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1873 break;
1874 case SCSI_TAPE_MAJOR:
1875 info->DeviceType = FILE_DEVICE_TAPE;
1876 break;
1878 #endif
1880 else if (S_ISBLK( st.st_mode ))
1882 info->DeviceType = FILE_DEVICE_DISK;
1884 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1886 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1888 else if (is_device_placeholder( fd ))
1890 info->DeviceType = FILE_DEVICE_DISK;
1892 else /* regular file or directory */
1894 #if defined(linux) && defined(HAVE_FSTATFS)
1895 struct statfs stfs;
1897 /* check for floppy disk */
1898 if (major(st.st_dev) == FLOPPY_MAJOR)
1899 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1901 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1902 switch (stfs.f_type)
1904 case 0x9660: /* iso9660 */
1905 case 0x9fa1: /* supermount */
1906 case 0x15013346: /* udf */
1907 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1908 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1909 break;
1910 case 0x6969: /* nfs */
1911 case 0x517B: /* smbfs */
1912 case 0x564c: /* ncpfs */
1913 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1914 info->Characteristics |= FILE_REMOTE_DEVICE;
1915 break;
1916 case 0x01021994: /* tmpfs */
1917 case 0x28cd3d45: /* cramfs */
1918 case 0x1373: /* devfs */
1919 case 0x9fa0: /* procfs */
1920 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1921 break;
1922 default:
1923 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1924 break;
1926 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1927 struct statfs stfs;
1929 if (fstatfs( fd, &stfs ) < 0)
1930 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1931 else
1932 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
1933 #elif defined(__NetBSD__)
1934 struct statvfs stfs;
1936 if (fstatvfs( fd, &stfs) < 0)
1937 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1938 else
1939 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
1940 #elif defined(sun)
1941 /* Use dkio to work out device types */
1943 # include <sys/dkio.h>
1944 # include <sys/vtoc.h>
1945 struct dk_cinfo dkinf;
1946 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1947 if(retval==-1){
1948 WARN("Unable to get disk device type information - assuming a disk like device\n");
1949 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1951 switch (dkinf.dki_ctype)
1953 case DKC_CDROM:
1954 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1955 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1956 break;
1957 case DKC_NCRFLOPPY:
1958 case DKC_SMSFLOPPY:
1959 case DKC_INTEL82072:
1960 case DKC_INTEL82077:
1961 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1962 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1963 break;
1964 case DKC_MD:
1965 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1966 break;
1967 default:
1968 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1971 #else
1972 static int warned;
1973 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1974 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1975 #endif
1976 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1978 return STATUS_SUCCESS;
1982 /******************************************************************************
1983 * NtQueryVolumeInformationFile [NTDLL.@]
1984 * ZwQueryVolumeInformationFile [NTDLL.@]
1986 * Get volume information for an open file handle.
1988 * PARAMS
1989 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1990 * io [O] Receives information about the operation on return
1991 * buffer [O] Destination for volume information
1992 * length [I] Size of FsInformation
1993 * info_class [I] Type of volume information to set
1995 * RETURNS
1996 * Success: 0. io and buffer are updated.
1997 * Failure: An NTSTATUS error code describing the error.
1999 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2000 PVOID buffer, ULONG length,
2001 FS_INFORMATION_CLASS info_class )
2003 int fd, needs_close;
2004 struct stat st;
2006 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2007 return io->u.Status;
2009 io->u.Status = STATUS_NOT_IMPLEMENTED;
2010 io->Information = 0;
2012 switch( info_class )
2014 case FileFsVolumeInformation:
2015 FIXME( "%p: volume info not supported\n", handle );
2016 break;
2017 case FileFsLabelInformation:
2018 FIXME( "%p: label info not supported\n", handle );
2019 break;
2020 case FileFsSizeInformation:
2021 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2022 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2023 else
2025 FILE_FS_SIZE_INFORMATION *info = buffer;
2027 if (fstat( fd, &st ) < 0)
2029 io->u.Status = FILE_GetNtStatus();
2030 break;
2032 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2034 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2036 else
2038 /* Linux's fstatvfs is buggy */
2039 #if !defined(linux) || !defined(HAVE_FSTATFS)
2040 struct statvfs stfs;
2042 if (fstatvfs( fd, &stfs ) < 0)
2044 io->u.Status = FILE_GetNtStatus();
2045 break;
2047 info->BytesPerSector = stfs.f_frsize;
2048 #else
2049 struct statfs stfs;
2050 if (fstatfs( fd, &stfs ) < 0)
2052 io->u.Status = FILE_GetNtStatus();
2053 break;
2055 info->BytesPerSector = stfs.f_bsize;
2056 #endif
2057 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
2058 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
2059 info->SectorsPerAllocationUnit = 1;
2060 io->Information = sizeof(*info);
2061 io->u.Status = STATUS_SUCCESS;
2064 break;
2065 case FileFsDeviceInformation:
2066 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2067 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2068 else
2070 FILE_FS_DEVICE_INFORMATION *info = buffer;
2072 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2073 io->Information = sizeof(*info);
2075 break;
2076 case FileFsAttributeInformation:
2077 FIXME( "%p: attribute info not supported\n", handle );
2078 break;
2079 case FileFsControlInformation:
2080 FIXME( "%p: control info not supported\n", handle );
2081 break;
2082 case FileFsFullSizeInformation:
2083 FIXME( "%p: full size info not supported\n", handle );
2084 break;
2085 case FileFsObjectIdInformation:
2086 FIXME( "%p: object id info not supported\n", handle );
2087 break;
2088 case FileFsMaximumInformation:
2089 FIXME( "%p: maximum info not supported\n", handle );
2090 break;
2091 default:
2092 io->u.Status = STATUS_INVALID_PARAMETER;
2093 break;
2095 if (needs_close) close( fd );
2096 return io->u.Status;
2100 /******************************************************************
2101 * NtFlushBuffersFile (NTDLL.@)
2103 * Flush any buffered data on an open file handle.
2105 * PARAMS
2106 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2107 * IoStatusBlock [O] Receives information about the operation on return
2109 * RETURNS
2110 * Success: 0. IoStatusBlock is updated.
2111 * Failure: An NTSTATUS error code describing the error.
2113 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2115 NTSTATUS ret;
2116 HANDLE hEvent = NULL;
2118 SERVER_START_REQ( flush_file )
2120 req->handle = hFile;
2121 ret = wine_server_call( req );
2122 hEvent = reply->event;
2124 SERVER_END_REQ;
2125 if (!ret && hEvent)
2127 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2128 NtClose( hEvent );
2130 return ret;
2133 /******************************************************************
2134 * NtLockFile (NTDLL.@)
2138 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2139 PIO_APC_ROUTINE apc, void* apc_user,
2140 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2141 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2142 BOOLEAN exclusive )
2144 NTSTATUS ret;
2145 HANDLE handle;
2146 BOOLEAN async;
2148 if (apc || io_status || key)
2150 FIXME("Unimplemented yet parameter\n");
2151 return STATUS_NOT_IMPLEMENTED;
2154 if (apc_user) FIXME("I/O completion on lock not implemented yet\n");
2156 for (;;)
2158 SERVER_START_REQ( lock_file )
2160 req->handle = hFile;
2161 req->offset = offset->QuadPart;
2162 req->count = count->QuadPart;
2163 req->shared = !exclusive;
2164 req->wait = !dont_wait;
2165 ret = wine_server_call( req );
2166 handle = reply->handle;
2167 async = reply->overlapped;
2169 SERVER_END_REQ;
2170 if (ret != STATUS_PENDING)
2172 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2173 return ret;
2176 if (async)
2178 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2179 if (handle) NtClose( handle );
2180 return STATUS_PENDING;
2182 if (handle)
2184 NtWaitForSingleObject( handle, FALSE, NULL );
2185 NtClose( handle );
2187 else
2189 LARGE_INTEGER time;
2191 /* Unix lock conflict, sleep a bit and retry */
2192 time.QuadPart = 100 * (ULONGLONG)10000;
2193 time.QuadPart = -time.QuadPart;
2194 NtDelayExecution( FALSE, &time );
2200 /******************************************************************
2201 * NtUnlockFile (NTDLL.@)
2205 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2206 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2207 PULONG key )
2209 NTSTATUS status;
2211 TRACE( "%p %x%08x %x%08x\n",
2212 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2214 if (io_status || key)
2216 FIXME("Unimplemented yet parameter\n");
2217 return STATUS_NOT_IMPLEMENTED;
2220 SERVER_START_REQ( unlock_file )
2222 req->handle = hFile;
2223 req->offset = offset->QuadPart;
2224 req->count = count->QuadPart;
2225 status = wine_server_call( req );
2227 SERVER_END_REQ;
2228 return status;
2231 /******************************************************************
2232 * NtCreateNamedPipeFile (NTDLL.@)
2236 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2237 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2238 ULONG sharing, ULONG dispo, ULONG options,
2239 ULONG pipe_type, ULONG read_mode,
2240 ULONG completion_mode, ULONG max_inst,
2241 ULONG inbound_quota, ULONG outbound_quota,
2242 PLARGE_INTEGER timeout)
2244 NTSTATUS status;
2246 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2247 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2248 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2249 outbound_quota, timeout);
2251 /* assume we only get relative timeout */
2252 if (timeout->QuadPart > 0)
2253 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2255 SERVER_START_REQ( create_named_pipe )
2257 req->access = access;
2258 req->attributes = attr->Attributes;
2259 req->rootdir = attr->RootDirectory;
2260 req->options = options;
2261 req->flags =
2262 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2263 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2264 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2265 req->maxinstances = max_inst;
2266 req->outsize = outbound_quota;
2267 req->insize = inbound_quota;
2268 req->timeout = timeout->QuadPart;
2269 wine_server_add_data( req, attr->ObjectName->Buffer,
2270 attr->ObjectName->Length );
2271 status = wine_server_call( req );
2272 if (!status) *handle = reply->handle;
2274 SERVER_END_REQ;
2275 return status;
2278 /******************************************************************
2279 * NtDeleteFile (NTDLL.@)
2283 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2285 NTSTATUS status;
2286 HANDLE hFile;
2287 IO_STATUS_BLOCK io;
2289 TRACE("%p\n", ObjectAttributes);
2290 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2291 ObjectAttributes, &io, NULL, 0,
2292 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2293 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2294 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2295 return status;
2298 /******************************************************************
2299 * NtCancelIoFile (NTDLL.@)
2303 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2305 LARGE_INTEGER timeout;
2307 TRACE("%p %p\n", hFile, io_status );
2309 SERVER_START_REQ( cancel_async )
2311 req->handle = hFile;
2312 wine_server_call( req );
2314 SERVER_END_REQ;
2315 /* Let some APC be run, so that we can run the remaining APCs on hFile
2316 * either the cancelation of the pending one, but also the execution
2317 * of the queued APC, but not yet run. This is needed to ensure proper
2318 * clean-up of allocated data.
2320 timeout.u.LowPart = timeout.u.HighPart = 0;
2321 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2324 /******************************************************************************
2325 * NtCreateMailslotFile [NTDLL.@]
2326 * ZwCreateMailslotFile [NTDLL.@]
2328 * PARAMS
2329 * pHandle [O] pointer to receive the handle created
2330 * DesiredAccess [I] access mode (read, write, etc)
2331 * ObjectAttributes [I] fully qualified NT path of the mailslot
2332 * IoStatusBlock [O] receives completion status and other info
2333 * CreateOptions [I]
2334 * MailslotQuota [I]
2335 * MaxMessageSize [I]
2336 * TimeOut [I]
2338 * RETURNS
2339 * An NT status code
2341 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2342 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2343 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2344 PLARGE_INTEGER TimeOut)
2346 LARGE_INTEGER timeout;
2347 NTSTATUS ret;
2349 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2350 pHandle, DesiredAccess, attr, IoStatusBlock,
2351 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2353 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2354 if (!attr) return STATUS_INVALID_PARAMETER;
2355 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2358 * For a NULL TimeOut pointer set the default timeout value
2360 if (!TimeOut)
2361 timeout.QuadPart = -1;
2362 else
2363 timeout.QuadPart = TimeOut->QuadPart;
2365 SERVER_START_REQ( create_mailslot )
2367 req->access = DesiredAccess;
2368 req->attributes = attr->Attributes;
2369 req->rootdir = attr->RootDirectory;
2370 req->max_msgsize = MaxMessageSize;
2371 req->read_timeout = timeout.QuadPart;
2372 wine_server_add_data( req, attr->ObjectName->Buffer,
2373 attr->ObjectName->Length );
2374 ret = wine_server_call( req );
2375 if( ret == STATUS_SUCCESS )
2376 *pHandle = reply->handle;
2378 SERVER_END_REQ;
2380 return ret;