push f19c907ab00e303c34c91d18c0dfddd2e9ddf00c
[wine/hacks.git] / dlls / ntdll / file.c
blobd0229af4ab74539d0ae7c8645195fe98bf7f6d95
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 SERVER_START_REQ( create_file )
196 req->access = access;
197 req->attributes = attr->Attributes;
198 req->sharing = sharing;
199 req->create = disposition;
200 req->options = options;
201 req->attrs = attributes;
202 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
203 io->u.Status = wine_server_call( req );
204 *handle = reply->handle;
206 SERVER_END_REQ;
207 RtlFreeAnsiString( &unix_name );
209 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
211 if (io->u.Status == STATUS_SUCCESS)
213 if (created) io->Information = FILE_CREATED;
214 else switch(disposition)
216 case FILE_SUPERSEDE:
217 io->Information = FILE_SUPERSEDED;
218 break;
219 case FILE_CREATE:
220 io->Information = FILE_CREATED;
221 break;
222 case FILE_OPEN:
223 case FILE_OPEN_IF:
224 io->Information = FILE_OPENED;
225 break;
226 case FILE_OVERWRITE:
227 case FILE_OVERWRITE_IF:
228 io->Information = FILE_OVERWRITTEN;
229 break;
233 return io->u.Status;
236 /***********************************************************************
237 * Asynchronous file I/O *
240 typedef struct
242 HANDLE handle;
243 char* buffer;
244 unsigned int already;
245 unsigned int count;
246 BOOL avail_mode;
247 } async_fileio_read;
249 typedef struct
251 HANDLE handle;
252 const char *buffer;
253 unsigned int already;
254 unsigned int count;
255 } async_fileio_write;
258 /***********************************************************************
259 * FILE_GetNtStatus(void)
261 * Retrieve the Nt Status code from errno.
262 * Try to be consistent with FILE_SetDosError().
264 NTSTATUS FILE_GetNtStatus(void)
266 int err = errno;
268 TRACE( "errno = %d\n", errno );
269 switch (err)
271 case EAGAIN: return STATUS_SHARING_VIOLATION;
272 case EBADF: return STATUS_INVALID_HANDLE;
273 case EBUSY: return STATUS_DEVICE_BUSY;
274 case ENOSPC: return STATUS_DISK_FULL;
275 case EPERM:
276 case EROFS:
277 case EACCES: return STATUS_ACCESS_DENIED;
278 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
279 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
280 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
281 case EMFILE:
282 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
283 case EINVAL: return STATUS_INVALID_PARAMETER;
284 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
285 case EPIPE: return STATUS_PIPE_DISCONNECTED;
286 case EIO: return STATUS_DEVICE_NOT_READY;
287 #ifdef ENOMEDIUM
288 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
289 #endif
290 case ENXIO: return STATUS_NO_SUCH_DEVICE;
291 case ENOTTY:
292 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
293 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
294 case EFAULT: return STATUS_ACCESS_VIOLATION;
295 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
296 case ENOEXEC: /* ?? */
297 case EEXIST: /* ?? */
298 default:
299 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
300 return STATUS_UNSUCCESSFUL;
304 /***********************************************************************
305 * FILE_AsyncReadService (INTERNAL)
307 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status)
309 async_fileio_read *fileio = user;
310 int fd, needs_close, result;
312 TRACE("%p %p 0x%x\n", iosb, fileio->buffer, status);
314 switch (status)
316 case STATUS_ALERTED: /* got some new data */
317 /* check to see if the data is ready (non-blocking) */
318 if ((status = server_get_unix_fd( fileio->handle, FILE_READ_DATA, &fd,
319 &needs_close, NULL, NULL )))
320 break;
322 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
323 if (needs_close) close( fd );
325 if (result < 0)
327 if (errno == EAGAIN || errno == EINTR)
329 TRACE("Deferred read %d\n", errno);
330 status = STATUS_PENDING;
332 else /* check to see if the transfer is complete */
333 status = FILE_GetNtStatus();
335 else if (result == 0)
337 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
339 else
341 fileio->already += result;
342 if (fileio->already >= fileio->count || fileio->avail_mode)
343 status = STATUS_SUCCESS;
344 else
346 /* if we only have to read the available data, and none is available,
347 * simply cancel the request. If data was available, it has been read
348 * while in by previous call (NtDelayExecution)
350 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
353 TRACE("read %d more bytes %u/%u so far (%s)\n",
354 result, fileio->already, fileio->count,
355 (status == STATUS_SUCCESS) ? "success" : "pending");
357 break;
359 case STATUS_TIMEOUT:
360 case STATUS_IO_TIMEOUT:
361 if (fileio->already) status = STATUS_SUCCESS;
362 break;
364 if (status != STATUS_PENDING)
366 iosb->u.Status = status;
367 iosb->Information = fileio->already;
368 RtlFreeHeap( GetProcessHeap(), 0, fileio );
370 return status;
373 struct io_timeouts
375 int interval; /* max interval between two bytes */
376 int total; /* total timeout for the whole operation */
377 int end_time; /* absolute time of end of operation */
380 /* retrieve the I/O timeouts to use for a given handle */
381 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
382 struct io_timeouts *timeouts )
384 NTSTATUS status = STATUS_SUCCESS;
386 timeouts->interval = timeouts->total = -1;
388 switch(type)
390 case FD_TYPE_SERIAL:
392 /* GetCommTimeouts */
393 SERIAL_TIMEOUTS st;
394 IO_STATUS_BLOCK io;
396 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
397 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
398 if (status) break;
400 if (is_read)
402 if (st.ReadIntervalTimeout)
403 timeouts->interval = st.ReadIntervalTimeout;
405 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
407 timeouts->total = st.ReadTotalTimeoutConstant;
408 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
409 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
411 else if (st.ReadIntervalTimeout == MAXDWORD)
412 timeouts->interval = 0;
414 else /* write */
416 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
418 timeouts->total = st.WriteTotalTimeoutConstant;
419 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
420 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
424 break;
425 case FD_TYPE_MAILSLOT:
426 if (is_read)
428 timeouts->interval = 0; /* return as soon as we got something */
429 SERVER_START_REQ( set_mailslot_info )
431 req->handle = handle;
432 req->flags = 0;
433 if (!(status = wine_server_call( req )) &&
434 reply->read_timeout != TIMEOUT_INFINITE)
435 timeouts->total = reply->read_timeout / -10000;
437 SERVER_END_REQ;
439 break;
440 case FD_TYPE_SOCKET:
441 case FD_TYPE_PIPE:
442 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
443 break;
444 default:
445 break;
447 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
448 return STATUS_SUCCESS;
452 /* retrieve the timeout for the next wait, in milliseconds */
453 static inline int get_next_io_timeout( struct io_timeouts *timeouts, ULONG already )
455 int ret = -1;
457 if (timeouts->total != -1)
459 ret = timeouts->end_time - NtGetTickCount();
460 if (ret < 0) ret = 0;
462 if (already && timeouts->interval != -1)
464 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
466 return ret;
470 /* retrieve the avail_mode flag for async reads */
471 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
473 NTSTATUS status = STATUS_SUCCESS;
475 switch(type)
477 case FD_TYPE_SERIAL:
479 /* GetCommTimeouts */
480 SERIAL_TIMEOUTS st;
481 IO_STATUS_BLOCK io;
483 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
484 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
485 if (status) break;
486 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
487 !st.ReadTotalTimeoutConstant &&
488 st.ReadIntervalTimeout == MAXDWORD);
490 break;
491 case FD_TYPE_MAILSLOT:
492 case FD_TYPE_SOCKET:
493 case FD_TYPE_PIPE:
494 *avail_mode = TRUE;
495 break;
496 default:
497 *avail_mode = FALSE;
498 break;
500 return status;
504 /******************************************************************************
505 * NtReadFile [NTDLL.@]
506 * ZwReadFile [NTDLL.@]
508 * Read from an open file handle.
510 * PARAMS
511 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
512 * Event [I] Event to signal upon completion (or NULL)
513 * ApcRoutine [I] Callback to call upon completion (or NULL)
514 * ApcContext [I] Context for ApcRoutine (or NULL)
515 * IoStatusBlock [O] Receives information about the operation on return
516 * Buffer [O] Destination for the data read
517 * Length [I] Size of Buffer
518 * ByteOffset [O] Destination for the new file pointer position (or NULL)
519 * Key [O] Function unknown (may be NULL)
521 * RETURNS
522 * Success: 0. IoStatusBlock is updated, and the Information member contains
523 * The number of bytes read.
524 * Failure: An NTSTATUS error code describing the error.
526 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
527 PIO_APC_ROUTINE apc, void* apc_user,
528 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
529 PLARGE_INTEGER offset, PULONG key)
531 int result, unix_handle, needs_close, timeout_init_done = 0;
532 unsigned int options;
533 struct io_timeouts timeouts;
534 NTSTATUS status;
535 ULONG total = 0;
536 enum server_fd_type type;
538 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
539 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
541 if (!io_status) return STATUS_ACCESS_VIOLATION;
543 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
544 &needs_close, &type, &options );
545 if (status) return status;
547 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
549 /* async I/O doesn't make sense on regular files */
550 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
552 if (errno != EINTR)
554 status = FILE_GetNtStatus();
555 goto done;
558 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
559 /* update file pointer position */
560 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
562 total = result;
563 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
564 goto done;
567 for (;;)
569 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
571 total += result;
572 if (!result || total == length)
574 if (total)
575 status = STATUS_SUCCESS;
576 else
577 status = (type == FD_TYPE_FILE) ? STATUS_END_OF_FILE : STATUS_PIPE_BROKEN;
578 goto done;
581 else
583 if (errno == EINTR) continue;
584 if (errno != EAGAIN)
586 status = FILE_GetNtStatus();
587 goto done;
591 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
593 async_fileio_read *fileio;
594 BOOL avail_mode;
596 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
597 goto done;
598 if (total && avail_mode)
600 status = STATUS_SUCCESS;
601 goto done;
604 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
606 status = STATUS_NO_MEMORY;
607 goto done;
609 fileio->handle = hFile;
610 fileio->already = total;
611 fileio->count = length;
612 fileio->buffer = buffer;
613 fileio->avail_mode = avail_mode;
615 SERVER_START_REQ( register_async )
617 req->handle = hFile;
618 req->type = ASYNC_TYPE_READ;
619 req->count = length;
620 req->async.callback = FILE_AsyncReadService;
621 req->async.iosb = io_status;
622 req->async.arg = fileio;
623 req->async.apc = apc;
624 req->async.apc_arg = apc_user;
625 req->async.event = hEvent;
626 status = wine_server_call( req );
628 SERVER_END_REQ;
630 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
631 else NtCurrentTeb()->num_async_io++;
632 goto done;
634 else /* synchronous read, wait for the fd to become ready */
636 struct pollfd pfd;
637 int ret, timeout;
639 if (!timeout_init_done)
641 timeout_init_done = 1;
642 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
643 goto done;
644 if (hEvent) NtResetEvent( hEvent, NULL );
646 timeout = get_next_io_timeout( &timeouts, total );
648 pfd.fd = unix_handle;
649 pfd.events = POLLIN;
651 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
653 if (total) /* return with what we got so far */
654 status = STATUS_SUCCESS;
655 else
656 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
657 goto done;
659 if (ret == -1 && errno != EINTR)
661 status = FILE_GetNtStatus();
662 goto done;
664 /* will now restart the read */
668 done:
669 if (needs_close) close( unix_handle );
670 if (status == STATUS_SUCCESS)
672 io_status->u.Status = status;
673 io_status->Information = total;
674 TRACE("= SUCCESS (%u)\n", total);
675 if (hEvent) NtSetEvent( hEvent, NULL );
676 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
677 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
679 else
681 TRACE("= 0x%08x\n", status);
682 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
684 return status;
687 /***********************************************************************
688 * FILE_AsyncWriteService (INTERNAL)
690 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status)
692 async_fileio_write *fileio = user;
693 int result, fd, needs_close;
694 enum server_fd_type type;
696 TRACE("(%p %p 0x%x)\n",iosb, fileio->buffer, status);
698 switch (status)
700 case STATUS_ALERTED:
701 /* write some data (non-blocking) */
702 if ((status = server_get_unix_fd( fileio->handle, FILE_WRITE_DATA, &fd,
703 &needs_close, &type, NULL )))
704 break;
706 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
707 result = send( fd, fileio->buffer, 0, 0 );
708 else
709 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
711 if (needs_close) close( fd );
713 if (result < 0)
715 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
716 else status = FILE_GetNtStatus();
718 else
720 fileio->already += result;
721 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
722 TRACE("wrote %d more bytes %u/%u so far\n", result, fileio->already, fileio->count);
724 break;
726 case STATUS_TIMEOUT:
727 case STATUS_IO_TIMEOUT:
728 if (fileio->already) status = STATUS_SUCCESS;
729 break;
731 if (status != STATUS_PENDING)
733 iosb->u.Status = status;
734 iosb->Information = fileio->already;
735 RtlFreeHeap( GetProcessHeap(), 0, fileio );
737 return status;
740 /******************************************************************************
741 * NtWriteFile [NTDLL.@]
742 * ZwWriteFile [NTDLL.@]
744 * Write to an open file handle.
746 * PARAMS
747 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
748 * Event [I] Event to signal upon completion (or NULL)
749 * ApcRoutine [I] Callback to call upon completion (or NULL)
750 * ApcContext [I] Context for ApcRoutine (or NULL)
751 * IoStatusBlock [O] Receives information about the operation on return
752 * Buffer [I] Source for the data to write
753 * Length [I] Size of Buffer
754 * ByteOffset [O] Destination for the new file pointer position (or NULL)
755 * Key [O] Function unknown (may be NULL)
757 * RETURNS
758 * Success: 0. IoStatusBlock is updated, and the Information member contains
759 * The number of bytes written.
760 * Failure: An NTSTATUS error code describing the error.
762 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
763 PIO_APC_ROUTINE apc, void* apc_user,
764 PIO_STATUS_BLOCK io_status,
765 const void* buffer, ULONG length,
766 PLARGE_INTEGER offset, PULONG key)
768 int result, unix_handle, needs_close, timeout_init_done = 0;
769 unsigned int options;
770 struct io_timeouts timeouts;
771 NTSTATUS status;
772 ULONG total = 0;
773 enum server_fd_type type;
775 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
776 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
778 if (!io_status) return STATUS_ACCESS_VIOLATION;
780 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
781 &needs_close, &type, &options );
782 if (status) return status;
784 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
786 /* async I/O doesn't make sense on regular files */
787 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
789 if (errno != EINTR)
791 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
792 else status = FILE_GetNtStatus();
793 goto done;
797 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
798 /* update file pointer position */
799 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
801 total = result;
802 status = STATUS_SUCCESS;
803 goto done;
806 for (;;)
808 /* zero-length writes on sockets may not work with plain write(2) */
809 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
810 result = send( unix_handle, buffer, 0, 0 );
811 else
812 result = write( unix_handle, (const char *)buffer + total, length - total );
814 if (result >= 0)
816 total += result;
817 if (total == length)
819 status = STATUS_SUCCESS;
820 goto done;
823 else
825 if (errno == EINTR) continue;
826 if (errno != EAGAIN)
828 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
829 else status = FILE_GetNtStatus();
830 goto done;
834 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
836 async_fileio_write *fileio;
838 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
840 status = STATUS_NO_MEMORY;
841 goto done;
843 fileio->handle = hFile;
844 fileio->already = total;
845 fileio->count = length;
846 fileio->buffer = buffer;
848 SERVER_START_REQ( register_async )
850 req->handle = hFile;
851 req->type = ASYNC_TYPE_WRITE;
852 req->count = length;
853 req->async.callback = FILE_AsyncWriteService;
854 req->async.iosb = io_status;
855 req->async.arg = fileio;
856 req->async.apc = apc;
857 req->async.apc_arg = apc_user;
858 req->async.event = hEvent;
859 status = wine_server_call( req );
861 SERVER_END_REQ;
863 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
864 else NtCurrentTeb()->num_async_io++;
865 goto done;
867 else /* synchronous write, wait for the fd to become ready */
869 struct pollfd pfd;
870 int ret, timeout;
872 if (!timeout_init_done)
874 timeout_init_done = 1;
875 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
876 goto done;
877 if (hEvent) NtResetEvent( hEvent, NULL );
879 timeout = get_next_io_timeout( &timeouts, total );
881 pfd.fd = unix_handle;
882 pfd.events = POLLIN;
884 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
886 /* return with what we got so far */
887 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
888 goto done;
890 if (ret == -1 && errno != EINTR)
892 status = FILE_GetNtStatus();
893 goto done;
895 /* will now restart the write */
899 done:
900 if (needs_close) close( unix_handle );
901 if (status == STATUS_SUCCESS)
903 io_status->u.Status = status;
904 io_status->Information = total;
905 TRACE("= SUCCESS (%u)\n", total);
906 if (hEvent) NtSetEvent( hEvent, NULL );
907 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
908 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
910 else
912 TRACE("= 0x%08x\n", status);
913 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
915 return status;
919 /* callback for ioctl async I/O completion */
920 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status )
922 io->u.Status = status;
923 return status;
926 /* do a ioctl call through the server */
927 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
928 PIO_APC_ROUTINE apc, PVOID apc_context,
929 IO_STATUS_BLOCK *io, ULONG code,
930 PVOID in_buffer, ULONG in_size,
931 PVOID out_buffer, ULONG out_size )
933 NTSTATUS status;
935 SERVER_START_REQ( ioctl )
937 req->handle = handle;
938 req->code = code;
939 req->async.callback = ioctl_completion;
940 req->async.iosb = io;
941 req->async.arg = NULL;
942 req->async.apc = apc;
943 req->async.apc_arg = apc_context;
944 req->async.event = event;
945 wine_server_add_data( req, in_buffer, in_size );
946 wine_server_set_reply( req, out_buffer, out_size );
947 if (!(status = wine_server_call( req )))
948 io->Information = wine_server_reply_size( reply );
950 SERVER_END_REQ;
952 if (status == STATUS_NOT_SUPPORTED)
953 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
954 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
956 return status;
960 /**************************************************************************
961 * NtDeviceIoControlFile [NTDLL.@]
962 * ZwDeviceIoControlFile [NTDLL.@]
964 * Perform an I/O control operation on an open file handle.
966 * PARAMS
967 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
968 * event [I] Event to signal upon completion (or NULL)
969 * apc [I] Callback to call upon completion (or NULL)
970 * apc_context [I] Context for ApcRoutine (or NULL)
971 * io [O] Receives information about the operation on return
972 * code [I] Control code for the operation to perform
973 * in_buffer [I] Source for any input data required (or NULL)
974 * in_size [I] Size of InputBuffer
975 * out_buffer [O] Source for any output data returned (or NULL)
976 * out_size [I] Size of OutputBuffer
978 * RETURNS
979 * Success: 0. IoStatusBlock is updated.
980 * Failure: An NTSTATUS error code describing the error.
982 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
983 PIO_APC_ROUTINE apc, PVOID apc_context,
984 PIO_STATUS_BLOCK io, ULONG code,
985 PVOID in_buffer, ULONG in_size,
986 PVOID out_buffer, ULONG out_size)
988 ULONG device = (code >> 16);
989 NTSTATUS status;
991 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
992 handle, event, apc, apc_context, io, code,
993 in_buffer, in_size, out_buffer, out_size);
995 switch(device)
997 case FILE_DEVICE_DISK:
998 case FILE_DEVICE_CD_ROM:
999 case FILE_DEVICE_DVD:
1000 case FILE_DEVICE_CONTROLLER:
1001 case FILE_DEVICE_MASS_STORAGE:
1002 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1003 in_buffer, in_size, out_buffer, out_size);
1004 break;
1005 case FILE_DEVICE_SERIAL_PORT:
1006 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1007 in_buffer, in_size, out_buffer, out_size);
1008 break;
1009 case FILE_DEVICE_TAPE:
1010 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1011 in_buffer, in_size, out_buffer, out_size);
1012 break;
1013 default:
1014 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1015 in_buffer, in_size, out_buffer, out_size );
1016 break;
1018 if (status != STATUS_PENDING) io->u.Status = status;
1019 return status;
1023 /**************************************************************************
1024 * NtFsControlFile [NTDLL.@]
1025 * ZwFsControlFile [NTDLL.@]
1027 * Perform a file system control operation on an open file handle.
1029 * PARAMS
1030 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1031 * event [I] Event to signal upon completion (or NULL)
1032 * apc [I] Callback to call upon completion (or NULL)
1033 * apc_context [I] Context for ApcRoutine (or NULL)
1034 * io [O] Receives information about the operation on return
1035 * code [I] Control code for the operation to perform
1036 * in_buffer [I] Source for any input data required (or NULL)
1037 * in_size [I] Size of InputBuffer
1038 * out_buffer [O] Source for any output data returned (or NULL)
1039 * out_size [I] Size of OutputBuffer
1041 * RETURNS
1042 * Success: 0. IoStatusBlock is updated.
1043 * Failure: An NTSTATUS error code describing the error.
1045 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1046 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1047 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1049 NTSTATUS status;
1051 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1052 handle, event, apc, apc_context, io, code,
1053 in_buffer, in_size, out_buffer, out_size);
1055 if (!io) return STATUS_INVALID_PARAMETER;
1057 switch(code)
1059 case FSCTL_DISMOUNT_VOLUME:
1060 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1061 in_buffer, in_size, out_buffer, out_size );
1062 if (!status) status = DIR_unmount_device( handle );
1063 break;
1065 case FSCTL_PIPE_LISTEN:
1066 case FSCTL_PIPE_WAIT:
1068 HANDLE internal_event = 0;
1070 if(!event && !apc)
1072 status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
1073 if (status != STATUS_SUCCESS) break;
1074 event = internal_event;
1076 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1077 in_buffer, in_size, out_buffer, out_size );
1079 if (internal_event && status == STATUS_PENDING)
1081 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
1082 status = io->u.Status;
1084 if (internal_event) NtClose(internal_event);
1086 break;
1088 case FSCTL_PIPE_PEEK:
1090 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1091 int avail = 0, fd, needs_close;
1093 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1095 status = STATUS_INFO_LENGTH_MISMATCH;
1096 break;
1099 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1100 break;
1102 #ifdef FIONREAD
1103 if (ioctl( fd, FIONREAD, &avail ) != 0)
1105 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1106 if (needs_close) close( fd );
1107 status = FILE_GetNtStatus();
1108 break;
1110 #endif
1111 if (!avail) /* check for closed pipe */
1113 struct pollfd pollfd;
1114 int ret;
1116 pollfd.fd = fd;
1117 pollfd.events = POLLIN;
1118 pollfd.revents = 0;
1119 ret = poll( &pollfd, 1, 0 );
1120 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1122 if (needs_close) close( fd );
1123 status = STATUS_PIPE_BROKEN;
1124 break;
1127 buffer->NamedPipeState = 0; /* FIXME */
1128 buffer->ReadDataAvailable = avail;
1129 buffer->NumberOfMessages = 0; /* FIXME */
1130 buffer->MessageLength = 0; /* FIXME */
1131 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1132 status = STATUS_SUCCESS;
1133 if (avail)
1135 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1136 if (data_size)
1138 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1139 if (res >= 0) io->Information += res;
1142 if (needs_close) close( fd );
1144 break;
1146 case FSCTL_PIPE_DISCONNECT:
1147 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1148 in_buffer, in_size, out_buffer, out_size );
1149 if (!status)
1151 int fd = server_remove_fd_from_cache( handle );
1152 if (fd != -1) close( fd );
1154 break;
1156 case FSCTL_LOCK_VOLUME:
1157 case FSCTL_UNLOCK_VOLUME:
1158 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1159 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1160 status = STATUS_SUCCESS;
1161 break;
1163 default:
1164 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1165 in_buffer, in_size, out_buffer, out_size );
1166 break;
1169 if (status != STATUS_PENDING) io->u.Status = status;
1170 return status;
1173 /******************************************************************************
1174 * NtSetVolumeInformationFile [NTDLL.@]
1175 * ZwSetVolumeInformationFile [NTDLL.@]
1177 * Set volume information for an open file handle.
1179 * PARAMS
1180 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1181 * IoStatusBlock [O] Receives information about the operation on return
1182 * FsInformation [I] Source for volume information
1183 * Length [I] Size of FsInformation
1184 * FsInformationClass [I] Type of volume information to set
1186 * RETURNS
1187 * Success: 0. IoStatusBlock is updated.
1188 * Failure: An NTSTATUS error code describing the error.
1190 NTSTATUS WINAPI NtSetVolumeInformationFile(
1191 IN HANDLE FileHandle,
1192 PIO_STATUS_BLOCK IoStatusBlock,
1193 PVOID FsInformation,
1194 ULONG Length,
1195 FS_INFORMATION_CLASS FsInformationClass)
1197 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1198 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1199 return 0;
1202 /******************************************************************************
1203 * NtQueryInformationFile [NTDLL.@]
1204 * ZwQueryInformationFile [NTDLL.@]
1206 * Get information about an open file handle.
1208 * PARAMS
1209 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1210 * io [O] Receives information about the operation on return
1211 * ptr [O] Destination for file information
1212 * len [I] Size of FileInformation
1213 * class [I] Type of file information to get
1215 * RETURNS
1216 * Success: 0. IoStatusBlock and FileInformation are updated.
1217 * Failure: An NTSTATUS error code describing the error.
1219 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1220 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1222 static const size_t info_sizes[] =
1225 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1226 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1227 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1228 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1229 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1230 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1231 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1232 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1233 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1234 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1235 0, /* FileLinkInformation */
1236 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1237 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1238 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1239 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1240 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1241 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1242 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1243 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1244 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1245 0, /* FileAlternateNameInformation */
1246 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1247 0, /* FilePipeInformation */
1248 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1249 0, /* FilePipeRemoteInformation */
1250 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1251 0, /* FileMailslotSetInformation */
1252 0, /* FileCompressionInformation */
1253 0, /* FileObjectIdInformation */
1254 0, /* FileCompletionInformation */
1255 0, /* FileMoveClusterInformation */
1256 0, /* FileQuotaInformation */
1257 0, /* FileReparsePointInformation */
1258 0, /* FileNetworkOpenInformation */
1259 0, /* FileAttributeTagInformation */
1260 0 /* FileTrackingInformation */
1263 struct stat st;
1264 int fd, needs_close = FALSE;
1266 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1268 io->Information = 0;
1270 if (class <= 0 || class >= FileMaximumInformation)
1271 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1272 if (!info_sizes[class])
1274 FIXME("Unsupported class (%d)\n", class);
1275 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1277 if (len < info_sizes[class])
1278 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1280 if (class != FilePipeLocalInformation)
1282 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1283 return io->u.Status;
1286 switch (class)
1288 case FileBasicInformation:
1290 FILE_BASIC_INFORMATION *info = ptr;
1292 if (fstat( fd, &st ) == -1)
1293 io->u.Status = FILE_GetNtStatus();
1294 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1295 io->u.Status = STATUS_INVALID_INFO_CLASS;
1296 else
1298 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1299 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1300 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1301 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1302 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1303 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1304 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1305 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1308 break;
1309 case FileStandardInformation:
1311 FILE_STANDARD_INFORMATION *info = ptr;
1313 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1314 else
1316 if ((info->Directory = S_ISDIR(st.st_mode)))
1318 info->AllocationSize.QuadPart = 0;
1319 info->EndOfFile.QuadPart = 0;
1320 info->NumberOfLinks = 1;
1321 info->DeletePending = FALSE;
1323 else
1325 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1326 info->EndOfFile.QuadPart = st.st_size;
1327 info->NumberOfLinks = st.st_nlink;
1328 info->DeletePending = FALSE; /* FIXME */
1332 break;
1333 case FilePositionInformation:
1335 FILE_POSITION_INFORMATION *info = ptr;
1336 off_t res = lseek( fd, 0, SEEK_CUR );
1337 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1338 else info->CurrentByteOffset.QuadPart = res;
1340 break;
1341 case FileInternalInformation:
1343 FILE_INTERNAL_INFORMATION *info = ptr;
1345 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1346 else info->IndexNumber.QuadPart = st.st_ino;
1348 break;
1349 case FileEaInformation:
1351 FILE_EA_INFORMATION *info = ptr;
1352 info->EaSize = 0;
1354 break;
1355 case FileEndOfFileInformation:
1357 FILE_END_OF_FILE_INFORMATION *info = ptr;
1359 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1360 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1362 break;
1363 case FileAllInformation:
1365 FILE_ALL_INFORMATION *info = ptr;
1367 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1368 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1369 io->u.Status = STATUS_INVALID_INFO_CLASS;
1370 else
1372 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1374 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1375 info->StandardInformation.AllocationSize.QuadPart = 0;
1376 info->StandardInformation.EndOfFile.QuadPart = 0;
1377 info->StandardInformation.NumberOfLinks = 1;
1378 info->StandardInformation.DeletePending = FALSE;
1380 else
1382 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1383 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1384 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1385 info->StandardInformation.NumberOfLinks = st.st_nlink;
1386 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1388 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1389 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1390 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1391 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1392 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1393 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1394 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1395 info->EaInformation.EaSize = 0;
1396 info->AccessInformation.AccessFlags = 0; /* FIXME */
1397 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1398 info->ModeInformation.Mode = 0; /* FIXME */
1399 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1400 info->NameInformation.FileNameLength = 0;
1401 io->Information = sizeof(*info) - sizeof(WCHAR);
1404 break;
1405 case FileMailslotQueryInformation:
1407 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1409 SERVER_START_REQ( set_mailslot_info )
1411 req->handle = hFile;
1412 req->flags = 0;
1413 io->u.Status = wine_server_call( req );
1414 if( io->u.Status == STATUS_SUCCESS )
1416 info->MaximumMessageSize = reply->max_msgsize;
1417 info->MailslotQuota = 0;
1418 info->NextMessageSize = 0;
1419 info->MessagesAvailable = 0;
1420 info->ReadTimeout.QuadPart = reply->read_timeout;
1423 SERVER_END_REQ;
1424 if (!io->u.Status)
1426 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1427 char *tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size );
1428 if (tmpbuf)
1430 int fd, needs_close;
1431 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1433 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1434 info->MessagesAvailable = (res > 0);
1435 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1436 if (needs_close) close( fd );
1438 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1442 break;
1443 case FilePipeLocalInformation:
1445 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1447 SERVER_START_REQ( get_named_pipe_info )
1449 req->handle = hFile;
1450 if (!(io->u.Status = wine_server_call( req )))
1452 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1453 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1454 pli->NamedPipeConfiguration = 0; /* FIXME */
1455 pli->MaximumInstances = reply->maxinstances;
1456 pli->CurrentInstances = reply->instances;
1457 pli->InboundQuota = reply->insize;
1458 pli->ReadDataAvailable = 0; /* FIXME */
1459 pli->OutboundQuota = reply->outsize;
1460 pli->WriteQuotaAvailable = 0; /* FIXME */
1461 pli->NamedPipeState = 0; /* FIXME */
1462 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1463 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1466 SERVER_END_REQ;
1468 break;
1469 default:
1470 FIXME("Unsupported class (%d)\n", class);
1471 io->u.Status = STATUS_NOT_IMPLEMENTED;
1472 break;
1474 if (needs_close) close( fd );
1475 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1476 return io->u.Status;
1479 /******************************************************************************
1480 * NtSetInformationFile [NTDLL.@]
1481 * ZwSetInformationFile [NTDLL.@]
1483 * Set information about an open file handle.
1485 * PARAMS
1486 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1487 * io [O] Receives information about the operation on return
1488 * ptr [I] Source for file information
1489 * len [I] Size of FileInformation
1490 * class [I] Type of file information to set
1492 * RETURNS
1493 * Success: 0. io is updated.
1494 * Failure: An NTSTATUS error code describing the error.
1496 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1497 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1499 int fd, needs_close;
1501 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1503 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1504 return io->u.Status;
1506 io->u.Status = STATUS_SUCCESS;
1507 switch (class)
1509 case FileBasicInformation:
1510 if (len >= sizeof(FILE_BASIC_INFORMATION))
1512 struct stat st;
1513 const FILE_BASIC_INFORMATION *info = ptr;
1515 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1517 ULONGLONG sec, nsec;
1518 struct timeval tv[2];
1520 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1523 tv[0].tv_sec = tv[0].tv_usec = 0;
1524 tv[1].tv_sec = tv[1].tv_usec = 0;
1525 if (!fstat( fd, &st ))
1527 tv[0].tv_sec = st.st_atime;
1528 tv[1].tv_sec = st.st_mtime;
1531 if (info->LastAccessTime.QuadPart)
1533 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1534 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1535 tv[0].tv_usec = (UINT)nsec / 10;
1537 if (info->LastWriteTime.QuadPart)
1539 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1540 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1541 tv[1].tv_usec = (UINT)nsec / 10;
1543 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1546 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1548 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1549 else
1551 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1553 if (S_ISDIR( st.st_mode))
1554 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1555 else
1556 st.st_mode &= ~0222; /* clear write permission bits */
1558 else
1560 /* add write permission only where we already have read permission */
1561 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1563 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1567 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1568 break;
1570 case FilePositionInformation:
1571 if (len >= sizeof(FILE_POSITION_INFORMATION))
1573 const FILE_POSITION_INFORMATION *info = ptr;
1575 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1576 io->u.Status = FILE_GetNtStatus();
1578 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1579 break;
1581 case FileEndOfFileInformation:
1582 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1584 struct stat st;
1585 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1587 /* first try normal truncate */
1588 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1590 /* now check for the need to extend the file */
1591 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1593 static const char zero;
1595 /* extend the file one byte beyond the requested size and then truncate it */
1596 /* this should work around ftruncate implementations that can't extend files */
1597 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1598 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1600 io->u.Status = FILE_GetNtStatus();
1602 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1603 break;
1605 case FileMailslotSetInformation:
1607 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1609 SERVER_START_REQ( set_mailslot_info )
1611 req->handle = handle;
1612 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1613 req->read_timeout = info->ReadTimeout.QuadPart;
1614 io->u.Status = wine_server_call( req );
1616 SERVER_END_REQ;
1618 break;
1620 default:
1621 FIXME("Unsupported class (%d)\n", class);
1622 io->u.Status = STATUS_NOT_IMPLEMENTED;
1623 break;
1625 if (needs_close) close( fd );
1626 io->Information = 0;
1627 return io->u.Status;
1631 /******************************************************************************
1632 * NtQueryFullAttributesFile (NTDLL.@)
1634 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1635 FILE_NETWORK_OPEN_INFORMATION *info )
1637 ANSI_STRING unix_name;
1638 NTSTATUS status;
1640 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1641 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1643 struct stat st;
1645 if (stat( unix_name.Buffer, &st ) == -1)
1646 status = FILE_GetNtStatus();
1647 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1648 status = STATUS_INVALID_INFO_CLASS;
1649 else
1651 if (S_ISDIR(st.st_mode))
1653 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1654 info->AllocationSize.QuadPart = 0;
1655 info->EndOfFile.QuadPart = 0;
1657 else
1659 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1660 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1661 info->EndOfFile.QuadPart = st.st_size;
1663 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1664 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1665 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1666 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1667 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1668 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1669 if (DIR_is_hidden_file( attr->ObjectName ))
1670 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1672 RtlFreeAnsiString( &unix_name );
1674 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1675 return status;
1679 /******************************************************************************
1680 * NtQueryAttributesFile (NTDLL.@)
1681 * ZwQueryAttributesFile (NTDLL.@)
1683 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1685 FILE_NETWORK_OPEN_INFORMATION full_info;
1686 NTSTATUS status;
1688 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1690 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1691 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1692 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1693 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1694 info->FileAttributes = full_info.FileAttributes;
1696 return status;
1700 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1701 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1702 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1703 size_t fstypesize, unsigned int flags )
1705 if (!strncmp("cd9660", fstypename, fstypesize) ||
1706 !strncmp("udf", fstypename, fstypesize))
1708 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1709 /* Don't assume read-only, let the mount options set it below */
1710 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1712 else if (!strncmp("nfs", fstypename, fstypesize) ||
1713 !strncmp("nwfs", fstypename, fstypesize) ||
1714 !strncmp("smbfs", fstypename, fstypesize) ||
1715 !strncmp("afpfs", fstypename, fstypesize))
1717 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1718 info->Characteristics |= FILE_REMOTE_DEVICE;
1720 else if (!strncmp("procfs", fstypename, fstypesize))
1721 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1722 else
1723 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1725 if (flags & MNT_RDONLY)
1726 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1728 if (!(flags & MNT_LOCAL))
1730 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1731 info->Characteristics |= FILE_REMOTE_DEVICE;
1734 #endif
1736 /******************************************************************************
1737 * get_device_info
1739 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1741 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1743 struct stat st;
1745 info->Characteristics = 0;
1746 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1747 if (S_ISCHR( st.st_mode ))
1749 info->DeviceType = FILE_DEVICE_UNKNOWN;
1750 #ifdef linux
1751 switch(major(st.st_rdev))
1753 case MEM_MAJOR:
1754 info->DeviceType = FILE_DEVICE_NULL;
1755 break;
1756 case TTY_MAJOR:
1757 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1758 break;
1759 case LP_MAJOR:
1760 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1761 break;
1762 case SCSI_TAPE_MAJOR:
1763 info->DeviceType = FILE_DEVICE_TAPE;
1764 break;
1766 #endif
1768 else if (S_ISBLK( st.st_mode ))
1770 info->DeviceType = FILE_DEVICE_DISK;
1772 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1774 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1776 else /* regular file or directory */
1778 #if defined(linux) && defined(HAVE_FSTATFS)
1779 struct statfs stfs;
1781 /* check for floppy disk */
1782 if (major(st.st_dev) == FLOPPY_MAJOR)
1783 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1785 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1786 switch (stfs.f_type)
1788 case 0x9660: /* iso9660 */
1789 case 0x9fa1: /* supermount */
1790 case 0x15013346: /* udf */
1791 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1792 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1793 break;
1794 case 0x6969: /* nfs */
1795 case 0x517B: /* smbfs */
1796 case 0x564c: /* ncpfs */
1797 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1798 info->Characteristics |= FILE_REMOTE_DEVICE;
1799 break;
1800 case 0x01021994: /* tmpfs */
1801 case 0x28cd3d45: /* cramfs */
1802 case 0x1373: /* devfs */
1803 case 0x9fa0: /* procfs */
1804 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1805 break;
1806 default:
1807 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1808 break;
1810 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1811 struct statfs stfs;
1813 if (fstatfs( fd, &stfs ) < 0)
1814 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1815 else
1816 get_device_info_fstatfs( info, stfs.f_fstypename,
1817 sizeof(stfs.f_fstypename), stfs.f_flags );
1818 #elif defined(__NetBSD__)
1819 struct statvfs stfs;
1821 if (fstatvfs( fd, &stfs) < 0)
1822 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1823 else
1824 get_device_info_fstatfs( info, stfs.f_fstypename,
1825 sizeof(stfs.f_fstypename), stfs.f_flag );
1826 #elif defined(sun)
1827 /* Use dkio to work out device types */
1829 # include <sys/dkio.h>
1830 # include <sys/vtoc.h>
1831 struct dk_cinfo dkinf;
1832 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1833 if(retval==-1){
1834 WARN("Unable to get disk device type information - assuming a disk like device\n");
1835 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1837 switch (dkinf.dki_ctype)
1839 case DKC_CDROM:
1840 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1841 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1842 break;
1843 case DKC_NCRFLOPPY:
1844 case DKC_SMSFLOPPY:
1845 case DKC_INTEL82072:
1846 case DKC_INTEL82077:
1847 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1848 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1849 break;
1850 case DKC_MD:
1851 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1852 break;
1853 default:
1854 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1857 #else
1858 static int warned;
1859 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1860 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1861 #endif
1862 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1864 return STATUS_SUCCESS;
1868 /******************************************************************************
1869 * NtQueryVolumeInformationFile [NTDLL.@]
1870 * ZwQueryVolumeInformationFile [NTDLL.@]
1872 * Get volume information for an open file handle.
1874 * PARAMS
1875 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1876 * io [O] Receives information about the operation on return
1877 * buffer [O] Destination for volume information
1878 * length [I] Size of FsInformation
1879 * info_class [I] Type of volume information to set
1881 * RETURNS
1882 * Success: 0. io and buffer are updated.
1883 * Failure: An NTSTATUS error code describing the error.
1885 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1886 PVOID buffer, ULONG length,
1887 FS_INFORMATION_CLASS info_class )
1889 int fd, needs_close;
1890 struct stat st;
1892 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1893 return io->u.Status;
1895 io->u.Status = STATUS_NOT_IMPLEMENTED;
1896 io->Information = 0;
1898 switch( info_class )
1900 case FileFsVolumeInformation:
1901 FIXME( "%p: volume info not supported\n", handle );
1902 break;
1903 case FileFsLabelInformation:
1904 FIXME( "%p: label info not supported\n", handle );
1905 break;
1906 case FileFsSizeInformation:
1907 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1908 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1909 else
1911 FILE_FS_SIZE_INFORMATION *info = buffer;
1913 if (fstat( fd, &st ) < 0)
1915 io->u.Status = FILE_GetNtStatus();
1916 break;
1918 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1920 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1922 else
1924 /* Linux's fstatvfs is buggy */
1925 #if !defined(linux) || !defined(HAVE_FSTATFS)
1926 struct statvfs stfs;
1928 if (fstatvfs( fd, &stfs ) < 0)
1930 io->u.Status = FILE_GetNtStatus();
1931 break;
1933 info->BytesPerSector = stfs.f_frsize;
1934 #else
1935 struct statfs stfs;
1936 if (fstatfs( fd, &stfs ) < 0)
1938 io->u.Status = FILE_GetNtStatus();
1939 break;
1941 info->BytesPerSector = stfs.f_bsize;
1942 #endif
1943 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1944 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1945 info->SectorsPerAllocationUnit = 1;
1946 io->Information = sizeof(*info);
1947 io->u.Status = STATUS_SUCCESS;
1950 break;
1951 case FileFsDeviceInformation:
1952 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1953 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1954 else
1956 FILE_FS_DEVICE_INFORMATION *info = buffer;
1958 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
1959 io->Information = sizeof(*info);
1961 break;
1962 case FileFsAttributeInformation:
1963 FIXME( "%p: attribute info not supported\n", handle );
1964 break;
1965 case FileFsControlInformation:
1966 FIXME( "%p: control info not supported\n", handle );
1967 break;
1968 case FileFsFullSizeInformation:
1969 FIXME( "%p: full size info not supported\n", handle );
1970 break;
1971 case FileFsObjectIdInformation:
1972 FIXME( "%p: object id info not supported\n", handle );
1973 break;
1974 case FileFsMaximumInformation:
1975 FIXME( "%p: maximum info not supported\n", handle );
1976 break;
1977 default:
1978 io->u.Status = STATUS_INVALID_PARAMETER;
1979 break;
1981 if (needs_close) close( fd );
1982 return io->u.Status;
1986 /******************************************************************
1987 * NtFlushBuffersFile (NTDLL.@)
1989 * Flush any buffered data on an open file handle.
1991 * PARAMS
1992 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1993 * IoStatusBlock [O] Receives information about the operation on return
1995 * RETURNS
1996 * Success: 0. IoStatusBlock is updated.
1997 * Failure: An NTSTATUS error code describing the error.
1999 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2001 NTSTATUS ret;
2002 HANDLE hEvent = NULL;
2004 SERVER_START_REQ( flush_file )
2006 req->handle = hFile;
2007 ret = wine_server_call( req );
2008 hEvent = reply->event;
2010 SERVER_END_REQ;
2011 if (!ret && hEvent)
2013 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2014 NtClose( hEvent );
2016 return ret;
2019 /******************************************************************
2020 * NtLockFile (NTDLL.@)
2024 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2025 PIO_APC_ROUTINE apc, void* apc_user,
2026 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2027 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2028 BOOLEAN exclusive )
2030 NTSTATUS ret;
2031 HANDLE handle;
2032 BOOLEAN async;
2034 if (apc || io_status || key)
2036 FIXME("Unimplemented yet parameter\n");
2037 return STATUS_NOT_IMPLEMENTED;
2040 for (;;)
2042 SERVER_START_REQ( lock_file )
2044 req->handle = hFile;
2045 req->offset_low = offset->u.LowPart;
2046 req->offset_high = offset->u.HighPart;
2047 req->count_low = count->u.LowPart;
2048 req->count_high = count->u.HighPart;
2049 req->shared = !exclusive;
2050 req->wait = !dont_wait;
2051 ret = wine_server_call( req );
2052 handle = reply->handle;
2053 async = reply->overlapped;
2055 SERVER_END_REQ;
2056 if (ret != STATUS_PENDING)
2058 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2059 return ret;
2062 if (async)
2064 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2065 if (handle) NtClose( handle );
2066 return STATUS_PENDING;
2068 if (handle)
2070 NtWaitForSingleObject( handle, FALSE, NULL );
2071 NtClose( handle );
2073 else
2075 LARGE_INTEGER time;
2077 /* Unix lock conflict, sleep a bit and retry */
2078 time.QuadPart = 100 * (ULONGLONG)10000;
2079 time.QuadPart = -time.QuadPart;
2080 NtDelayExecution( FALSE, &time );
2086 /******************************************************************
2087 * NtUnlockFile (NTDLL.@)
2091 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2092 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2093 PULONG key )
2095 NTSTATUS status;
2097 TRACE( "%p %x%08x %x%08x\n",
2098 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2100 if (io_status || key)
2102 FIXME("Unimplemented yet parameter\n");
2103 return STATUS_NOT_IMPLEMENTED;
2106 SERVER_START_REQ( unlock_file )
2108 req->handle = hFile;
2109 req->offset_low = offset->u.LowPart;
2110 req->offset_high = offset->u.HighPart;
2111 req->count_low = count->u.LowPart;
2112 req->count_high = count->u.HighPart;
2113 status = wine_server_call( req );
2115 SERVER_END_REQ;
2116 return status;
2119 /******************************************************************
2120 * NtCreateNamedPipeFile (NTDLL.@)
2124 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2125 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2126 ULONG sharing, ULONG dispo, ULONG options,
2127 ULONG pipe_type, ULONG read_mode,
2128 ULONG completion_mode, ULONG max_inst,
2129 ULONG inbound_quota, ULONG outbound_quota,
2130 PLARGE_INTEGER timeout)
2132 NTSTATUS status;
2134 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2135 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2136 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2137 outbound_quota, timeout);
2139 /* assume we only get relative timeout */
2140 if (timeout->QuadPart > 0)
2141 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2143 SERVER_START_REQ( create_named_pipe )
2145 req->access = access;
2146 req->attributes = attr->Attributes;
2147 req->rootdir = attr->RootDirectory;
2148 req->options = options;
2149 req->flags =
2150 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2151 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2152 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2153 req->maxinstances = max_inst;
2154 req->outsize = outbound_quota;
2155 req->insize = inbound_quota;
2156 req->timeout = timeout->QuadPart;
2157 wine_server_add_data( req, attr->ObjectName->Buffer,
2158 attr->ObjectName->Length );
2159 status = wine_server_call( req );
2160 if (!status) *handle = reply->handle;
2162 SERVER_END_REQ;
2163 return status;
2166 /******************************************************************
2167 * NtDeleteFile (NTDLL.@)
2171 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2173 NTSTATUS status;
2174 HANDLE hFile;
2175 IO_STATUS_BLOCK io;
2177 TRACE("%p\n", ObjectAttributes);
2178 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2179 ObjectAttributes, &io, NULL, 0,
2180 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2181 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2182 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2183 return status;
2186 /******************************************************************
2187 * NtCancelIoFile (NTDLL.@)
2191 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2193 LARGE_INTEGER timeout;
2195 TRACE("%p %p\n", hFile, io_status );
2197 SERVER_START_REQ( cancel_async )
2199 req->handle = hFile;
2200 wine_server_call( req );
2202 SERVER_END_REQ;
2203 /* Let some APC be run, so that we can run the remaining APCs on hFile
2204 * either the cancelation of the pending one, but also the execution
2205 * of the queued APC, but not yet run. This is needed to ensure proper
2206 * clean-up of allocated data.
2208 timeout.u.LowPart = timeout.u.HighPart = 0;
2209 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2212 /******************************************************************************
2213 * NtCreateMailslotFile [NTDLL.@]
2214 * ZwCreateMailslotFile [NTDLL.@]
2216 * PARAMS
2217 * pHandle [O] pointer to receive the handle created
2218 * DesiredAccess [I] access mode (read, write, etc)
2219 * ObjectAttributes [I] fully qualified NT path of the mailslot
2220 * IoStatusBlock [O] receives completion status and other info
2221 * CreateOptions [I]
2222 * MailslotQuota [I]
2223 * MaxMessageSize [I]
2224 * TimeOut [I]
2226 * RETURNS
2227 * An NT status code
2229 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2230 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2231 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2232 PLARGE_INTEGER TimeOut)
2234 LARGE_INTEGER timeout;
2235 NTSTATUS ret;
2237 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2238 pHandle, DesiredAccess, attr, IoStatusBlock,
2239 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2241 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2242 if (!attr) return STATUS_INVALID_PARAMETER;
2243 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2246 * For a NULL TimeOut pointer set the default timeout value
2248 if (!TimeOut)
2249 timeout.QuadPart = -1;
2250 else
2251 timeout.QuadPart = TimeOut->QuadPart;
2253 SERVER_START_REQ( create_mailslot )
2255 req->access = DesiredAccess;
2256 req->attributes = attr->Attributes;
2257 req->rootdir = attr->RootDirectory;
2258 req->max_msgsize = MaxMessageSize;
2259 req->read_timeout = timeout.QuadPart;
2260 wine_server_add_data( req, attr->ObjectName->Buffer,
2261 attr->ObjectName->Length );
2262 ret = wine_server_call( req );
2263 if( ret == STATUS_SUCCESS )
2264 *pHandle = reply->handle;
2266 SERVER_END_REQ;
2268 return ret;