server: Remove the extra apc_arg parameter now that user APCs all require the same...
[wine/gsoc_dplay.git] / dlls / ntdll / file.c
blobb48cc9d389ea39734254c1916f0dbcf74c08c5ce
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 struct async_fileio
242 HANDLE handle;
243 PIO_APC_ROUTINE apc;
244 void *apc_arg;
247 typedef struct
249 struct async_fileio io;
250 char* buffer;
251 unsigned int already;
252 unsigned int count;
253 BOOL avail_mode;
254 } async_fileio_read;
256 typedef struct
258 struct async_fileio io;
259 const char *buffer;
260 unsigned int already;
261 unsigned int count;
262 } async_fileio_write;
265 /* callback for file I/O user APC */
266 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
268 struct async_fileio *async = arg;
269 if (async->apc) async->apc( async->apc_arg, io, reserved );
270 RtlFreeHeap( GetProcessHeap(), 0, async );
273 /***********************************************************************
274 * FILE_GetNtStatus(void)
276 * Retrieve the Nt Status code from errno.
277 * Try to be consistent with FILE_SetDosError().
279 NTSTATUS FILE_GetNtStatus(void)
281 int err = errno;
283 TRACE( "errno = %d\n", errno );
284 switch (err)
286 case EAGAIN: return STATUS_SHARING_VIOLATION;
287 case EBADF: return STATUS_INVALID_HANDLE;
288 case EBUSY: return STATUS_DEVICE_BUSY;
289 case ENOSPC: return STATUS_DISK_FULL;
290 case EPERM:
291 case EROFS:
292 case EACCES: return STATUS_ACCESS_DENIED;
293 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
294 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
295 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
296 case EMFILE:
297 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
298 case EINVAL: return STATUS_INVALID_PARAMETER;
299 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
300 case EPIPE: return STATUS_PIPE_DISCONNECTED;
301 case EIO: return STATUS_DEVICE_NOT_READY;
302 #ifdef ENOMEDIUM
303 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
304 #endif
305 case ENXIO: return STATUS_NO_SUCH_DEVICE;
306 case ENOTTY:
307 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
308 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
309 case EFAULT: return STATUS_ACCESS_VIOLATION;
310 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
311 case ENOEXEC: /* ?? */
312 case EEXIST: /* ?? */
313 default:
314 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
315 return STATUS_UNSUCCESSFUL;
319 /***********************************************************************
320 * FILE_AsyncReadService (INTERNAL)
322 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status)
324 async_fileio_read *fileio = user;
325 int fd, needs_close, result;
327 TRACE("%p %p 0x%x\n", iosb, fileio->buffer, status);
329 switch (status)
331 case STATUS_ALERTED: /* got some new data */
332 /* check to see if the data is ready (non-blocking) */
333 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
334 &needs_close, NULL, NULL )))
335 break;
337 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
338 if (needs_close) close( fd );
340 if (result < 0)
342 if (errno == EAGAIN || errno == EINTR)
344 TRACE("Deferred read %d\n", errno);
345 status = STATUS_PENDING;
347 else /* check to see if the transfer is complete */
348 status = FILE_GetNtStatus();
350 else if (result == 0)
352 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
354 else
356 fileio->already += result;
357 if (fileio->already >= fileio->count || fileio->avail_mode)
358 status = STATUS_SUCCESS;
359 else
361 /* if we only have to read the available data, and none is available,
362 * simply cancel the request. If data was available, it has been read
363 * while in by previous call (NtDelayExecution)
365 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
368 TRACE("read %d more bytes %u/%u so far (%s)\n",
369 result, fileio->already, fileio->count,
370 (status == STATUS_SUCCESS) ? "success" : "pending");
372 break;
374 case STATUS_TIMEOUT:
375 case STATUS_IO_TIMEOUT:
376 if (fileio->already) status = STATUS_SUCCESS;
377 break;
379 if (status != STATUS_PENDING)
381 iosb->u.Status = status;
382 iosb->Information = fileio->already;
384 return status;
387 struct io_timeouts
389 int interval; /* max interval between two bytes */
390 int total; /* total timeout for the whole operation */
391 int end_time; /* absolute time of end of operation */
394 /* retrieve the I/O timeouts to use for a given handle */
395 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
396 struct io_timeouts *timeouts )
398 NTSTATUS status = STATUS_SUCCESS;
400 timeouts->interval = timeouts->total = -1;
402 switch(type)
404 case FD_TYPE_SERIAL:
406 /* GetCommTimeouts */
407 SERIAL_TIMEOUTS st;
408 IO_STATUS_BLOCK io;
410 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
411 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
412 if (status) break;
414 if (is_read)
416 if (st.ReadIntervalTimeout)
417 timeouts->interval = st.ReadIntervalTimeout;
419 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
421 timeouts->total = st.ReadTotalTimeoutConstant;
422 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
423 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
425 else if (st.ReadIntervalTimeout == MAXDWORD)
426 timeouts->interval = 0;
428 else /* write */
430 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
432 timeouts->total = st.WriteTotalTimeoutConstant;
433 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
434 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
438 break;
439 case FD_TYPE_MAILSLOT:
440 if (is_read)
442 timeouts->interval = 0; /* return as soon as we got something */
443 SERVER_START_REQ( set_mailslot_info )
445 req->handle = handle;
446 req->flags = 0;
447 if (!(status = wine_server_call( req )) &&
448 reply->read_timeout != TIMEOUT_INFINITE)
449 timeouts->total = reply->read_timeout / -10000;
451 SERVER_END_REQ;
453 break;
454 case FD_TYPE_SOCKET:
455 case FD_TYPE_PIPE:
456 case FD_TYPE_CHAR:
457 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
458 break;
459 default:
460 break;
462 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
463 return STATUS_SUCCESS;
467 /* retrieve the timeout for the next wait, in milliseconds */
468 static inline int get_next_io_timeout( struct io_timeouts *timeouts, ULONG already )
470 int ret = -1;
472 if (timeouts->total != -1)
474 ret = timeouts->end_time - NtGetTickCount();
475 if (ret < 0) ret = 0;
477 if (already && timeouts->interval != -1)
479 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
481 return ret;
485 /* retrieve the avail_mode flag for async reads */
486 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
488 NTSTATUS status = STATUS_SUCCESS;
490 switch(type)
492 case FD_TYPE_SERIAL:
494 /* GetCommTimeouts */
495 SERIAL_TIMEOUTS st;
496 IO_STATUS_BLOCK io;
498 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
499 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
500 if (status) break;
501 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
502 !st.ReadTotalTimeoutConstant &&
503 st.ReadIntervalTimeout == MAXDWORD);
505 break;
506 case FD_TYPE_MAILSLOT:
507 case FD_TYPE_SOCKET:
508 case FD_TYPE_PIPE:
509 case FD_TYPE_CHAR:
510 *avail_mode = TRUE;
511 break;
512 default:
513 *avail_mode = FALSE;
514 break;
516 return status;
520 /******************************************************************************
521 * NtReadFile [NTDLL.@]
522 * ZwReadFile [NTDLL.@]
524 * Read from an open file handle.
526 * PARAMS
527 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
528 * Event [I] Event to signal upon completion (or NULL)
529 * ApcRoutine [I] Callback to call upon completion (or NULL)
530 * ApcContext [I] Context for ApcRoutine (or NULL)
531 * IoStatusBlock [O] Receives information about the operation on return
532 * Buffer [O] Destination for the data read
533 * Length [I] Size of Buffer
534 * ByteOffset [O] Destination for the new file pointer position (or NULL)
535 * Key [O] Function unknown (may be NULL)
537 * RETURNS
538 * Success: 0. IoStatusBlock is updated, and the Information member contains
539 * The number of bytes read.
540 * Failure: An NTSTATUS error code describing the error.
542 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
543 PIO_APC_ROUTINE apc, void* apc_user,
544 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
545 PLARGE_INTEGER offset, PULONG key)
547 int result, unix_handle, needs_close, timeout_init_done = 0;
548 unsigned int options;
549 struct io_timeouts timeouts;
550 NTSTATUS status;
551 ULONG total = 0;
552 enum server_fd_type type;
554 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
555 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
557 if (!io_status) return STATUS_ACCESS_VIOLATION;
559 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
560 &needs_close, &type, &options );
561 if (status) return status;
563 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
565 /* async I/O doesn't make sense on regular files */
566 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
568 if (errno != EINTR)
570 status = FILE_GetNtStatus();
571 goto done;
574 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
575 /* update file pointer position */
576 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
578 total = result;
579 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
580 goto done;
583 for (;;)
585 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
587 total += result;
588 if (!result || total == length)
590 if (total)
591 status = STATUS_SUCCESS;
592 else
593 status = (type == FD_TYPE_FILE || type == FD_TYPE_CHAR) ? STATUS_END_OF_FILE : STATUS_PIPE_BROKEN;
594 goto done;
597 else
599 if (errno == EINTR) continue;
600 if (errno != EAGAIN)
602 status = FILE_GetNtStatus();
603 goto done;
607 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
609 async_fileio_read *fileio;
610 BOOL avail_mode;
612 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
613 goto done;
614 if (total && avail_mode)
616 status = STATUS_SUCCESS;
617 goto done;
620 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
622 status = STATUS_NO_MEMORY;
623 goto done;
625 fileio->io.handle = hFile;
626 fileio->io.apc = apc;
627 fileio->io.apc_arg = apc_user;
628 fileio->already = total;
629 fileio->count = length;
630 fileio->buffer = buffer;
631 fileio->avail_mode = avail_mode;
633 SERVER_START_REQ( register_async )
635 req->handle = hFile;
636 req->type = ASYNC_TYPE_READ;
637 req->count = length;
638 req->async.callback = FILE_AsyncReadService;
639 req->async.iosb = io_status;
640 req->async.arg = fileio;
641 req->async.apc = fileio_apc;
642 req->async.event = hEvent;
643 status = wine_server_call( req );
645 SERVER_END_REQ;
647 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
648 else NtCurrentTeb()->num_async_io++;
649 goto done;
651 else /* synchronous read, wait for the fd to become ready */
653 struct pollfd pfd;
654 int ret, timeout;
656 if (!timeout_init_done)
658 timeout_init_done = 1;
659 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
660 goto done;
661 if (hEvent) NtResetEvent( hEvent, NULL );
663 timeout = get_next_io_timeout( &timeouts, total );
665 pfd.fd = unix_handle;
666 pfd.events = POLLIN;
668 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
670 if (total) /* return with what we got so far */
671 status = STATUS_SUCCESS;
672 else
673 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
674 goto done;
676 if (ret == -1 && errno != EINTR)
678 status = FILE_GetNtStatus();
679 goto done;
681 /* will now restart the read */
685 done:
686 if (needs_close) close( unix_handle );
687 if (status == STATUS_SUCCESS)
689 io_status->u.Status = status;
690 io_status->Information = total;
691 TRACE("= SUCCESS (%u)\n", total);
692 if (hEvent) NtSetEvent( hEvent, NULL );
693 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
694 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
696 else
698 TRACE("= 0x%08x\n", status);
699 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
701 return status;
704 /***********************************************************************
705 * FILE_AsyncWriteService (INTERNAL)
707 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status)
709 async_fileio_write *fileio = user;
710 int result, fd, needs_close;
711 enum server_fd_type type;
713 TRACE("(%p %p 0x%x)\n",iosb, fileio->buffer, status);
715 switch (status)
717 case STATUS_ALERTED:
718 /* write some data (non-blocking) */
719 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
720 &needs_close, &type, NULL )))
721 break;
723 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
724 result = send( fd, fileio->buffer, 0, 0 );
725 else
726 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
728 if (needs_close) close( fd );
730 if (result < 0)
732 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
733 else status = FILE_GetNtStatus();
735 else
737 fileio->already += result;
738 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
739 TRACE("wrote %d more bytes %u/%u so far\n", result, fileio->already, fileio->count);
741 break;
743 case STATUS_TIMEOUT:
744 case STATUS_IO_TIMEOUT:
745 if (fileio->already) status = STATUS_SUCCESS;
746 break;
748 if (status != STATUS_PENDING)
750 iosb->u.Status = status;
751 iosb->Information = fileio->already;
753 return status;
756 /******************************************************************************
757 * NtWriteFile [NTDLL.@]
758 * ZwWriteFile [NTDLL.@]
760 * Write to an open file handle.
762 * PARAMS
763 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
764 * Event [I] Event to signal upon completion (or NULL)
765 * ApcRoutine [I] Callback to call upon completion (or NULL)
766 * ApcContext [I] Context for ApcRoutine (or NULL)
767 * IoStatusBlock [O] Receives information about the operation on return
768 * Buffer [I] Source for the data to write
769 * Length [I] Size of Buffer
770 * ByteOffset [O] Destination for the new file pointer position (or NULL)
771 * Key [O] Function unknown (may be NULL)
773 * RETURNS
774 * Success: 0. IoStatusBlock is updated, and the Information member contains
775 * The number of bytes written.
776 * Failure: An NTSTATUS error code describing the error.
778 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
779 PIO_APC_ROUTINE apc, void* apc_user,
780 PIO_STATUS_BLOCK io_status,
781 const void* buffer, ULONG length,
782 PLARGE_INTEGER offset, PULONG key)
784 int result, unix_handle, needs_close, timeout_init_done = 0;
785 unsigned int options;
786 struct io_timeouts timeouts;
787 NTSTATUS status;
788 ULONG total = 0;
789 enum server_fd_type type;
791 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
792 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
794 if (!io_status) return STATUS_ACCESS_VIOLATION;
796 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
797 &needs_close, &type, &options );
798 if (status) return status;
800 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
802 /* async I/O doesn't make sense on regular files */
803 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
805 if (errno != EINTR)
807 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
808 else status = FILE_GetNtStatus();
809 goto done;
813 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
814 /* update file pointer position */
815 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
817 total = result;
818 status = STATUS_SUCCESS;
819 goto done;
822 for (;;)
824 /* zero-length writes on sockets may not work with plain write(2) */
825 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
826 result = send( unix_handle, buffer, 0, 0 );
827 else
828 result = write( unix_handle, (const char *)buffer + total, length - total );
830 if (result >= 0)
832 total += result;
833 if (total == length)
835 status = STATUS_SUCCESS;
836 goto done;
839 else
841 if (errno == EINTR) continue;
842 if (errno != EAGAIN)
844 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
845 else status = FILE_GetNtStatus();
846 goto done;
850 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
852 async_fileio_write *fileio;
854 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
856 status = STATUS_NO_MEMORY;
857 goto done;
859 fileio->io.handle = hFile;
860 fileio->io.apc = apc;
861 fileio->io.apc_arg = apc_user;
862 fileio->already = total;
863 fileio->count = length;
864 fileio->buffer = buffer;
866 SERVER_START_REQ( register_async )
868 req->handle = hFile;
869 req->type = ASYNC_TYPE_WRITE;
870 req->count = length;
871 req->async.callback = FILE_AsyncWriteService;
872 req->async.iosb = io_status;
873 req->async.arg = fileio;
874 req->async.apc = fileio_apc;
875 req->async.event = hEvent;
876 status = wine_server_call( req );
878 SERVER_END_REQ;
880 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
881 else NtCurrentTeb()->num_async_io++;
882 goto done;
884 else /* synchronous write, wait for the fd to become ready */
886 struct pollfd pfd;
887 int ret, timeout;
889 if (!timeout_init_done)
891 timeout_init_done = 1;
892 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
893 goto done;
894 if (hEvent) NtResetEvent( hEvent, NULL );
896 timeout = get_next_io_timeout( &timeouts, total );
898 pfd.fd = unix_handle;
899 pfd.events = POLLOUT;
901 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
903 /* return with what we got so far */
904 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
905 goto done;
907 if (ret == -1 && errno != EINTR)
909 status = FILE_GetNtStatus();
910 goto done;
912 /* will now restart the write */
916 done:
917 if (needs_close) close( unix_handle );
918 if (status == STATUS_SUCCESS)
920 io_status->u.Status = status;
921 io_status->Information = total;
922 TRACE("= SUCCESS (%u)\n", total);
923 if (hEvent) NtSetEvent( hEvent, NULL );
924 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
925 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
927 else
929 TRACE("= 0x%08x\n", status);
930 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
932 return status;
936 struct async_ioctl
938 HANDLE handle; /* handle to the device */
939 void *buffer; /* buffer for output */
940 ULONG size; /* size of buffer */
941 PIO_APC_ROUTINE apc; /* user apc params */
942 void *apc_arg;
945 /* callback for ioctl async I/O completion */
946 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status )
948 struct async_ioctl *async = arg;
950 if (status == STATUS_ALERTED)
952 SERVER_START_REQ( get_ioctl_result )
954 req->handle = async->handle;
955 req->user_arg = async;
956 wine_server_set_reply( req, async->buffer, async->size );
957 if (!(status = wine_server_call( req )))
958 io->Information = wine_server_reply_size( reply );
960 SERVER_END_REQ;
962 if (status != STATUS_PENDING) io->u.Status = status;
963 return status;
966 /* callback for ioctl user APC */
967 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
969 struct async_ioctl *async = arg;
970 if (async->apc) async->apc( async->apc_arg, io, reserved );
971 RtlFreeHeap( GetProcessHeap(), 0, async );
974 /* do a ioctl call through the server */
975 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
976 PIO_APC_ROUTINE apc, PVOID apc_context,
977 IO_STATUS_BLOCK *io, ULONG code,
978 PVOID in_buffer, ULONG in_size,
979 PVOID out_buffer, ULONG out_size )
981 struct async_ioctl *async;
982 NTSTATUS status;
983 HANDLE wait_handle;
984 ULONG options;
986 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
987 return STATUS_NO_MEMORY;
988 async->handle = handle;
989 async->buffer = out_buffer;
990 async->size = out_size;
991 async->apc = apc;
992 async->apc_arg = apc_context;
994 SERVER_START_REQ( ioctl )
996 req->handle = handle;
997 req->code = code;
998 req->async.callback = ioctl_completion;
999 req->async.iosb = io;
1000 req->async.arg = async;
1001 req->async.apc = (apc || event) ? ioctl_apc : NULL;
1002 req->async.event = event;
1003 wine_server_add_data( req, in_buffer, in_size );
1004 wine_server_set_reply( req, out_buffer, out_size );
1005 if (!(status = wine_server_call( req )))
1006 io->Information = wine_server_reply_size( reply );
1007 wait_handle = reply->wait;
1008 options = reply->options;
1010 SERVER_END_REQ;
1012 if (status == STATUS_NOT_SUPPORTED)
1013 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1014 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1016 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1018 if (wait_handle)
1020 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1021 status = io->u.Status;
1022 NtClose( wait_handle );
1023 RtlFreeHeap( GetProcessHeap(), 0, async );
1026 return status;
1030 /**************************************************************************
1031 * NtDeviceIoControlFile [NTDLL.@]
1032 * ZwDeviceIoControlFile [NTDLL.@]
1034 * Perform an I/O control operation on an open file handle.
1036 * PARAMS
1037 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1038 * event [I] Event to signal upon completion (or NULL)
1039 * apc [I] Callback to call upon completion (or NULL)
1040 * apc_context [I] Context for ApcRoutine (or NULL)
1041 * io [O] Receives information about the operation on return
1042 * code [I] Control code for the operation to perform
1043 * in_buffer [I] Source for any input data required (or NULL)
1044 * in_size [I] Size of InputBuffer
1045 * out_buffer [O] Source for any output data returned (or NULL)
1046 * out_size [I] Size of OutputBuffer
1048 * RETURNS
1049 * Success: 0. IoStatusBlock is updated.
1050 * Failure: An NTSTATUS error code describing the error.
1052 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1053 PIO_APC_ROUTINE apc, PVOID apc_context,
1054 PIO_STATUS_BLOCK io, ULONG code,
1055 PVOID in_buffer, ULONG in_size,
1056 PVOID out_buffer, ULONG out_size)
1058 ULONG device = (code >> 16);
1059 NTSTATUS status;
1061 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1062 handle, event, apc, apc_context, io, code,
1063 in_buffer, in_size, out_buffer, out_size);
1065 switch(device)
1067 case FILE_DEVICE_DISK:
1068 case FILE_DEVICE_CD_ROM:
1069 case FILE_DEVICE_DVD:
1070 case FILE_DEVICE_CONTROLLER:
1071 case FILE_DEVICE_MASS_STORAGE:
1072 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1073 in_buffer, in_size, out_buffer, out_size);
1074 break;
1075 case FILE_DEVICE_SERIAL_PORT:
1076 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1077 in_buffer, in_size, out_buffer, out_size);
1078 break;
1079 case FILE_DEVICE_TAPE:
1080 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1081 in_buffer, in_size, out_buffer, out_size);
1082 break;
1083 default:
1084 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1085 in_buffer, in_size, out_buffer, out_size );
1086 break;
1088 if (status != STATUS_PENDING) io->u.Status = status;
1089 return status;
1093 /**************************************************************************
1094 * NtFsControlFile [NTDLL.@]
1095 * ZwFsControlFile [NTDLL.@]
1097 * Perform a file system control operation on an open file handle.
1099 * PARAMS
1100 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1101 * event [I] Event to signal upon completion (or NULL)
1102 * apc [I] Callback to call upon completion (or NULL)
1103 * apc_context [I] Context for ApcRoutine (or NULL)
1104 * io [O] Receives information about the operation on return
1105 * code [I] Control code for the operation to perform
1106 * in_buffer [I] Source for any input data required (or NULL)
1107 * in_size [I] Size of InputBuffer
1108 * out_buffer [O] Source for any output data returned (or NULL)
1109 * out_size [I] Size of OutputBuffer
1111 * RETURNS
1112 * Success: 0. IoStatusBlock is updated.
1113 * Failure: An NTSTATUS error code describing the error.
1115 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1116 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1117 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1119 NTSTATUS status;
1121 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1122 handle, event, apc, apc_context, io, code,
1123 in_buffer, in_size, out_buffer, out_size);
1125 if (!io) return STATUS_INVALID_PARAMETER;
1127 switch(code)
1129 case FSCTL_DISMOUNT_VOLUME:
1130 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1131 in_buffer, in_size, out_buffer, out_size );
1132 if (!status) status = DIR_unmount_device( handle );
1133 break;
1135 case FSCTL_PIPE_PEEK:
1137 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1138 int avail = 0, fd, needs_close;
1140 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1142 status = STATUS_INFO_LENGTH_MISMATCH;
1143 break;
1146 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1147 break;
1149 #ifdef FIONREAD
1150 if (ioctl( fd, FIONREAD, &avail ) != 0)
1152 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1153 if (needs_close) close( fd );
1154 status = FILE_GetNtStatus();
1155 break;
1157 #endif
1158 if (!avail) /* check for closed pipe */
1160 struct pollfd pollfd;
1161 int ret;
1163 pollfd.fd = fd;
1164 pollfd.events = POLLIN;
1165 pollfd.revents = 0;
1166 ret = poll( &pollfd, 1, 0 );
1167 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1169 if (needs_close) close( fd );
1170 status = STATUS_PIPE_BROKEN;
1171 break;
1174 buffer->NamedPipeState = 0; /* FIXME */
1175 buffer->ReadDataAvailable = avail;
1176 buffer->NumberOfMessages = 0; /* FIXME */
1177 buffer->MessageLength = 0; /* FIXME */
1178 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1179 status = STATUS_SUCCESS;
1180 if (avail)
1182 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1183 if (data_size)
1185 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1186 if (res >= 0) io->Information += res;
1189 if (needs_close) close( fd );
1191 break;
1193 case FSCTL_PIPE_DISCONNECT:
1194 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1195 in_buffer, in_size, out_buffer, out_size );
1196 if (!status)
1198 int fd = server_remove_fd_from_cache( handle );
1199 if (fd != -1) close( fd );
1201 break;
1203 case FSCTL_LOCK_VOLUME:
1204 case FSCTL_UNLOCK_VOLUME:
1205 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1206 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1207 status = STATUS_SUCCESS;
1208 break;
1210 case FSCTL_PIPE_LISTEN:
1211 case FSCTL_PIPE_WAIT:
1212 default:
1213 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1214 in_buffer, in_size, out_buffer, out_size );
1215 break;
1218 if (status != STATUS_PENDING) io->u.Status = status;
1219 return status;
1222 /******************************************************************************
1223 * NtSetVolumeInformationFile [NTDLL.@]
1224 * ZwSetVolumeInformationFile [NTDLL.@]
1226 * Set volume information for an open file handle.
1228 * PARAMS
1229 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1230 * IoStatusBlock [O] Receives information about the operation on return
1231 * FsInformation [I] Source for volume information
1232 * Length [I] Size of FsInformation
1233 * FsInformationClass [I] Type of volume information to set
1235 * RETURNS
1236 * Success: 0. IoStatusBlock is updated.
1237 * Failure: An NTSTATUS error code describing the error.
1239 NTSTATUS WINAPI NtSetVolumeInformationFile(
1240 IN HANDLE FileHandle,
1241 PIO_STATUS_BLOCK IoStatusBlock,
1242 PVOID FsInformation,
1243 ULONG Length,
1244 FS_INFORMATION_CLASS FsInformationClass)
1246 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1247 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1248 return 0;
1251 /******************************************************************************
1252 * NtQueryInformationFile [NTDLL.@]
1253 * ZwQueryInformationFile [NTDLL.@]
1255 * Get information about an open file handle.
1257 * PARAMS
1258 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1259 * io [O] Receives information about the operation on return
1260 * ptr [O] Destination for file information
1261 * len [I] Size of FileInformation
1262 * class [I] Type of file information to get
1264 * RETURNS
1265 * Success: 0. IoStatusBlock and FileInformation are updated.
1266 * Failure: An NTSTATUS error code describing the error.
1268 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1269 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1271 static const size_t info_sizes[] =
1274 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1275 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1276 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1277 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1278 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1279 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1280 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1281 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1282 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1283 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1284 0, /* FileLinkInformation */
1285 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1286 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1287 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1288 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1289 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1290 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1291 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1292 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1293 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1294 0, /* FileAlternateNameInformation */
1295 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1296 0, /* FilePipeInformation */
1297 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1298 0, /* FilePipeRemoteInformation */
1299 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1300 0, /* FileMailslotSetInformation */
1301 0, /* FileCompressionInformation */
1302 0, /* FileObjectIdInformation */
1303 0, /* FileCompletionInformation */
1304 0, /* FileMoveClusterInformation */
1305 0, /* FileQuotaInformation */
1306 0, /* FileReparsePointInformation */
1307 0, /* FileNetworkOpenInformation */
1308 0, /* FileAttributeTagInformation */
1309 0 /* FileTrackingInformation */
1312 struct stat st;
1313 int fd, needs_close = FALSE;
1315 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1317 io->Information = 0;
1319 if (class <= 0 || class >= FileMaximumInformation)
1320 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1321 if (!info_sizes[class])
1323 FIXME("Unsupported class (%d)\n", class);
1324 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1326 if (len < info_sizes[class])
1327 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1329 if (class != FilePipeLocalInformation)
1331 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1332 return io->u.Status;
1335 switch (class)
1337 case FileBasicInformation:
1339 FILE_BASIC_INFORMATION *info = ptr;
1341 if (fstat( fd, &st ) == -1)
1342 io->u.Status = FILE_GetNtStatus();
1343 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1344 io->u.Status = STATUS_INVALID_INFO_CLASS;
1345 else
1347 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1348 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1349 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1350 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1351 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1352 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1353 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1354 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1357 break;
1358 case FileStandardInformation:
1360 FILE_STANDARD_INFORMATION *info = ptr;
1362 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1363 else
1365 if ((info->Directory = S_ISDIR(st.st_mode)))
1367 info->AllocationSize.QuadPart = 0;
1368 info->EndOfFile.QuadPart = 0;
1369 info->NumberOfLinks = 1;
1370 info->DeletePending = FALSE;
1372 else
1374 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1375 info->EndOfFile.QuadPart = st.st_size;
1376 info->NumberOfLinks = st.st_nlink;
1377 info->DeletePending = FALSE; /* FIXME */
1381 break;
1382 case FilePositionInformation:
1384 FILE_POSITION_INFORMATION *info = ptr;
1385 off_t res = lseek( fd, 0, SEEK_CUR );
1386 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1387 else info->CurrentByteOffset.QuadPart = res;
1389 break;
1390 case FileInternalInformation:
1392 FILE_INTERNAL_INFORMATION *info = ptr;
1394 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1395 else info->IndexNumber.QuadPart = st.st_ino;
1397 break;
1398 case FileEaInformation:
1400 FILE_EA_INFORMATION *info = ptr;
1401 info->EaSize = 0;
1403 break;
1404 case FileEndOfFileInformation:
1406 FILE_END_OF_FILE_INFORMATION *info = ptr;
1408 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1409 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1411 break;
1412 case FileAllInformation:
1414 FILE_ALL_INFORMATION *info = ptr;
1416 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1417 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1418 io->u.Status = STATUS_INVALID_INFO_CLASS;
1419 else
1421 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1423 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1424 info->StandardInformation.AllocationSize.QuadPart = 0;
1425 info->StandardInformation.EndOfFile.QuadPart = 0;
1426 info->StandardInformation.NumberOfLinks = 1;
1427 info->StandardInformation.DeletePending = FALSE;
1429 else
1431 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1432 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1433 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1434 info->StandardInformation.NumberOfLinks = st.st_nlink;
1435 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1437 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1438 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1439 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1440 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1441 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1442 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1443 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1444 info->EaInformation.EaSize = 0;
1445 info->AccessInformation.AccessFlags = 0; /* FIXME */
1446 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1447 info->ModeInformation.Mode = 0; /* FIXME */
1448 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1449 info->NameInformation.FileNameLength = 0;
1450 io->Information = sizeof(*info) - sizeof(WCHAR);
1453 break;
1454 case FileMailslotQueryInformation:
1456 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1458 SERVER_START_REQ( set_mailslot_info )
1460 req->handle = hFile;
1461 req->flags = 0;
1462 io->u.Status = wine_server_call( req );
1463 if( io->u.Status == STATUS_SUCCESS )
1465 info->MaximumMessageSize = reply->max_msgsize;
1466 info->MailslotQuota = 0;
1467 info->NextMessageSize = 0;
1468 info->MessagesAvailable = 0;
1469 info->ReadTimeout.QuadPart = reply->read_timeout;
1472 SERVER_END_REQ;
1473 if (!io->u.Status)
1475 char *tmpbuf;
1476 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1477 if (size > 0x10000) size = 0x10000;
1478 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1480 int fd, needs_close;
1481 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1483 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1484 info->MessagesAvailable = (res > 0);
1485 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1486 if (needs_close) close( fd );
1488 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1492 break;
1493 case FilePipeLocalInformation:
1495 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1497 SERVER_START_REQ( get_named_pipe_info )
1499 req->handle = hFile;
1500 if (!(io->u.Status = wine_server_call( req )))
1502 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1503 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1504 pli->NamedPipeConfiguration = 0; /* FIXME */
1505 pli->MaximumInstances = reply->maxinstances;
1506 pli->CurrentInstances = reply->instances;
1507 pli->InboundQuota = reply->insize;
1508 pli->ReadDataAvailable = 0; /* FIXME */
1509 pli->OutboundQuota = reply->outsize;
1510 pli->WriteQuotaAvailable = 0; /* FIXME */
1511 pli->NamedPipeState = 0; /* FIXME */
1512 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1513 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1516 SERVER_END_REQ;
1518 break;
1519 default:
1520 FIXME("Unsupported class (%d)\n", class);
1521 io->u.Status = STATUS_NOT_IMPLEMENTED;
1522 break;
1524 if (needs_close) close( fd );
1525 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1526 return io->u.Status;
1529 /******************************************************************************
1530 * NtSetInformationFile [NTDLL.@]
1531 * ZwSetInformationFile [NTDLL.@]
1533 * Set information about an open file handle.
1535 * PARAMS
1536 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1537 * io [O] Receives information about the operation on return
1538 * ptr [I] Source for file information
1539 * len [I] Size of FileInformation
1540 * class [I] Type of file information to set
1542 * RETURNS
1543 * Success: 0. io is updated.
1544 * Failure: An NTSTATUS error code describing the error.
1546 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1547 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1549 int fd, needs_close;
1551 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1553 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1554 return io->u.Status;
1556 io->u.Status = STATUS_SUCCESS;
1557 switch (class)
1559 case FileBasicInformation:
1560 if (len >= sizeof(FILE_BASIC_INFORMATION))
1562 struct stat st;
1563 const FILE_BASIC_INFORMATION *info = ptr;
1565 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1567 ULONGLONG sec, nsec;
1568 struct timeval tv[2];
1570 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1573 tv[0].tv_sec = tv[0].tv_usec = 0;
1574 tv[1].tv_sec = tv[1].tv_usec = 0;
1575 if (!fstat( fd, &st ))
1577 tv[0].tv_sec = st.st_atime;
1578 tv[1].tv_sec = st.st_mtime;
1581 if (info->LastAccessTime.QuadPart)
1583 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1584 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1585 tv[0].tv_usec = (UINT)nsec / 10;
1587 if (info->LastWriteTime.QuadPart)
1589 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1590 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1591 tv[1].tv_usec = (UINT)nsec / 10;
1593 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1596 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1598 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1599 else
1601 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1603 if (S_ISDIR( st.st_mode))
1604 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1605 else
1606 st.st_mode &= ~0222; /* clear write permission bits */
1608 else
1610 /* add write permission only where we already have read permission */
1611 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1613 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1617 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1618 break;
1620 case FilePositionInformation:
1621 if (len >= sizeof(FILE_POSITION_INFORMATION))
1623 const FILE_POSITION_INFORMATION *info = ptr;
1625 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1626 io->u.Status = FILE_GetNtStatus();
1628 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1629 break;
1631 case FileEndOfFileInformation:
1632 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1634 struct stat st;
1635 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1637 /* first try normal truncate */
1638 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1640 /* now check for the need to extend the file */
1641 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1643 static const char zero;
1645 /* extend the file one byte beyond the requested size and then truncate it */
1646 /* this should work around ftruncate implementations that can't extend files */
1647 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1648 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1650 io->u.Status = FILE_GetNtStatus();
1652 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1653 break;
1655 case FileMailslotSetInformation:
1657 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1659 SERVER_START_REQ( set_mailslot_info )
1661 req->handle = handle;
1662 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1663 req->read_timeout = info->ReadTimeout.QuadPart;
1664 io->u.Status = wine_server_call( req );
1666 SERVER_END_REQ;
1668 break;
1670 default:
1671 FIXME("Unsupported class (%d)\n", class);
1672 io->u.Status = STATUS_NOT_IMPLEMENTED;
1673 break;
1675 if (needs_close) close( fd );
1676 io->Information = 0;
1677 return io->u.Status;
1681 /******************************************************************************
1682 * NtQueryFullAttributesFile (NTDLL.@)
1684 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1685 FILE_NETWORK_OPEN_INFORMATION *info )
1687 ANSI_STRING unix_name;
1688 NTSTATUS status;
1690 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1691 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1693 struct stat st;
1695 if (stat( unix_name.Buffer, &st ) == -1)
1696 status = FILE_GetNtStatus();
1697 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1698 status = STATUS_INVALID_INFO_CLASS;
1699 else
1701 if (S_ISDIR(st.st_mode))
1703 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1704 info->AllocationSize.QuadPart = 0;
1705 info->EndOfFile.QuadPart = 0;
1707 else
1709 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1710 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1711 info->EndOfFile.QuadPart = st.st_size;
1713 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1714 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1715 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1716 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1717 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1718 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1719 if (DIR_is_hidden_file( attr->ObjectName ))
1720 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1722 RtlFreeAnsiString( &unix_name );
1724 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1725 return status;
1729 /******************************************************************************
1730 * NtQueryAttributesFile (NTDLL.@)
1731 * ZwQueryAttributesFile (NTDLL.@)
1733 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1735 FILE_NETWORK_OPEN_INFORMATION full_info;
1736 NTSTATUS status;
1738 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1740 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1741 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1742 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1743 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1744 info->FileAttributes = full_info.FileAttributes;
1746 return status;
1750 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1751 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1752 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1753 size_t fstypesize, unsigned int flags )
1755 if (!strncmp("cd9660", fstypename, fstypesize) ||
1756 !strncmp("udf", fstypename, fstypesize))
1758 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1759 /* Don't assume read-only, let the mount options set it below */
1760 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1762 else if (!strncmp("nfs", fstypename, fstypesize) ||
1763 !strncmp("nwfs", fstypename, fstypesize) ||
1764 !strncmp("smbfs", fstypename, fstypesize) ||
1765 !strncmp("afpfs", fstypename, fstypesize))
1767 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1768 info->Characteristics |= FILE_REMOTE_DEVICE;
1770 else if (!strncmp("procfs", fstypename, fstypesize))
1771 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1772 else
1773 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1775 if (flags & MNT_RDONLY)
1776 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1778 if (!(flags & MNT_LOCAL))
1780 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1781 info->Characteristics |= FILE_REMOTE_DEVICE;
1784 #endif
1786 /******************************************************************************
1787 * get_device_info
1789 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1791 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1793 struct stat st;
1795 info->Characteristics = 0;
1796 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1797 if (S_ISCHR( st.st_mode ))
1799 info->DeviceType = FILE_DEVICE_UNKNOWN;
1800 #ifdef linux
1801 switch(major(st.st_rdev))
1803 case MEM_MAJOR:
1804 info->DeviceType = FILE_DEVICE_NULL;
1805 break;
1806 case TTY_MAJOR:
1807 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1808 break;
1809 case LP_MAJOR:
1810 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1811 break;
1812 case SCSI_TAPE_MAJOR:
1813 info->DeviceType = FILE_DEVICE_TAPE;
1814 break;
1816 #endif
1818 else if (S_ISBLK( st.st_mode ))
1820 info->DeviceType = FILE_DEVICE_DISK;
1822 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1824 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1826 else /* regular file or directory */
1828 #if defined(linux) && defined(HAVE_FSTATFS)
1829 struct statfs stfs;
1831 /* check for floppy disk */
1832 if (major(st.st_dev) == FLOPPY_MAJOR)
1833 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1835 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1836 switch (stfs.f_type)
1838 case 0x9660: /* iso9660 */
1839 case 0x9fa1: /* supermount */
1840 case 0x15013346: /* udf */
1841 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1842 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1843 break;
1844 case 0x6969: /* nfs */
1845 case 0x517B: /* smbfs */
1846 case 0x564c: /* ncpfs */
1847 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1848 info->Characteristics |= FILE_REMOTE_DEVICE;
1849 break;
1850 case 0x01021994: /* tmpfs */
1851 case 0x28cd3d45: /* cramfs */
1852 case 0x1373: /* devfs */
1853 case 0x9fa0: /* procfs */
1854 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1855 break;
1856 default:
1857 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1858 break;
1860 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1861 struct statfs stfs;
1863 if (fstatfs( fd, &stfs ) < 0)
1864 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1865 else
1866 get_device_info_fstatfs( info, stfs.f_fstypename,
1867 sizeof(stfs.f_fstypename), stfs.f_flags );
1868 #elif defined(__NetBSD__)
1869 struct statvfs stfs;
1871 if (fstatvfs( fd, &stfs) < 0)
1872 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1873 else
1874 get_device_info_fstatfs( info, stfs.f_fstypename,
1875 sizeof(stfs.f_fstypename), stfs.f_flag );
1876 #elif defined(sun)
1877 /* Use dkio to work out device types */
1879 # include <sys/dkio.h>
1880 # include <sys/vtoc.h>
1881 struct dk_cinfo dkinf;
1882 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1883 if(retval==-1){
1884 WARN("Unable to get disk device type information - assuming a disk like device\n");
1885 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1887 switch (dkinf.dki_ctype)
1889 case DKC_CDROM:
1890 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1891 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1892 break;
1893 case DKC_NCRFLOPPY:
1894 case DKC_SMSFLOPPY:
1895 case DKC_INTEL82072:
1896 case DKC_INTEL82077:
1897 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1898 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1899 break;
1900 case DKC_MD:
1901 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1902 break;
1903 default:
1904 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1907 #else
1908 static int warned;
1909 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1910 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1911 #endif
1912 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1914 return STATUS_SUCCESS;
1918 /******************************************************************************
1919 * NtQueryVolumeInformationFile [NTDLL.@]
1920 * ZwQueryVolumeInformationFile [NTDLL.@]
1922 * Get volume information for an open file handle.
1924 * PARAMS
1925 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1926 * io [O] Receives information about the operation on return
1927 * buffer [O] Destination for volume information
1928 * length [I] Size of FsInformation
1929 * info_class [I] Type of volume information to set
1931 * RETURNS
1932 * Success: 0. io and buffer are updated.
1933 * Failure: An NTSTATUS error code describing the error.
1935 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1936 PVOID buffer, ULONG length,
1937 FS_INFORMATION_CLASS info_class )
1939 int fd, needs_close;
1940 struct stat st;
1942 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1943 return io->u.Status;
1945 io->u.Status = STATUS_NOT_IMPLEMENTED;
1946 io->Information = 0;
1948 switch( info_class )
1950 case FileFsVolumeInformation:
1951 FIXME( "%p: volume info not supported\n", handle );
1952 break;
1953 case FileFsLabelInformation:
1954 FIXME( "%p: label info not supported\n", handle );
1955 break;
1956 case FileFsSizeInformation:
1957 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1958 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1959 else
1961 FILE_FS_SIZE_INFORMATION *info = buffer;
1963 if (fstat( fd, &st ) < 0)
1965 io->u.Status = FILE_GetNtStatus();
1966 break;
1968 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1970 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1972 else
1974 /* Linux's fstatvfs is buggy */
1975 #if !defined(linux) || !defined(HAVE_FSTATFS)
1976 struct statvfs stfs;
1978 if (fstatvfs( fd, &stfs ) < 0)
1980 io->u.Status = FILE_GetNtStatus();
1981 break;
1983 info->BytesPerSector = stfs.f_frsize;
1984 #else
1985 struct statfs stfs;
1986 if (fstatfs( fd, &stfs ) < 0)
1988 io->u.Status = FILE_GetNtStatus();
1989 break;
1991 info->BytesPerSector = stfs.f_bsize;
1992 #endif
1993 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1994 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1995 info->SectorsPerAllocationUnit = 1;
1996 io->Information = sizeof(*info);
1997 io->u.Status = STATUS_SUCCESS;
2000 break;
2001 case FileFsDeviceInformation:
2002 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2003 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2004 else
2006 FILE_FS_DEVICE_INFORMATION *info = buffer;
2008 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2009 io->Information = sizeof(*info);
2011 break;
2012 case FileFsAttributeInformation:
2013 FIXME( "%p: attribute info not supported\n", handle );
2014 break;
2015 case FileFsControlInformation:
2016 FIXME( "%p: control info not supported\n", handle );
2017 break;
2018 case FileFsFullSizeInformation:
2019 FIXME( "%p: full size info not supported\n", handle );
2020 break;
2021 case FileFsObjectIdInformation:
2022 FIXME( "%p: object id info not supported\n", handle );
2023 break;
2024 case FileFsMaximumInformation:
2025 FIXME( "%p: maximum info not supported\n", handle );
2026 break;
2027 default:
2028 io->u.Status = STATUS_INVALID_PARAMETER;
2029 break;
2031 if (needs_close) close( fd );
2032 return io->u.Status;
2036 /******************************************************************
2037 * NtFlushBuffersFile (NTDLL.@)
2039 * Flush any buffered data on an open file handle.
2041 * PARAMS
2042 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2043 * IoStatusBlock [O] Receives information about the operation on return
2045 * RETURNS
2046 * Success: 0. IoStatusBlock is updated.
2047 * Failure: An NTSTATUS error code describing the error.
2049 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2051 NTSTATUS ret;
2052 HANDLE hEvent = NULL;
2054 SERVER_START_REQ( flush_file )
2056 req->handle = hFile;
2057 ret = wine_server_call( req );
2058 hEvent = reply->event;
2060 SERVER_END_REQ;
2061 if (!ret && hEvent)
2063 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2064 NtClose( hEvent );
2066 return ret;
2069 /******************************************************************
2070 * NtLockFile (NTDLL.@)
2074 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2075 PIO_APC_ROUTINE apc, void* apc_user,
2076 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2077 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2078 BOOLEAN exclusive )
2080 NTSTATUS ret;
2081 HANDLE handle;
2082 BOOLEAN async;
2084 if (apc || io_status || key)
2086 FIXME("Unimplemented yet parameter\n");
2087 return STATUS_NOT_IMPLEMENTED;
2090 for (;;)
2092 SERVER_START_REQ( lock_file )
2094 req->handle = hFile;
2095 req->offset_low = offset->u.LowPart;
2096 req->offset_high = offset->u.HighPart;
2097 req->count_low = count->u.LowPart;
2098 req->count_high = count->u.HighPart;
2099 req->shared = !exclusive;
2100 req->wait = !dont_wait;
2101 ret = wine_server_call( req );
2102 handle = reply->handle;
2103 async = reply->overlapped;
2105 SERVER_END_REQ;
2106 if (ret != STATUS_PENDING)
2108 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2109 return ret;
2112 if (async)
2114 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2115 if (handle) NtClose( handle );
2116 return STATUS_PENDING;
2118 if (handle)
2120 NtWaitForSingleObject( handle, FALSE, NULL );
2121 NtClose( handle );
2123 else
2125 LARGE_INTEGER time;
2127 /* Unix lock conflict, sleep a bit and retry */
2128 time.QuadPart = 100 * (ULONGLONG)10000;
2129 time.QuadPart = -time.QuadPart;
2130 NtDelayExecution( FALSE, &time );
2136 /******************************************************************
2137 * NtUnlockFile (NTDLL.@)
2141 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2142 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2143 PULONG key )
2145 NTSTATUS status;
2147 TRACE( "%p %x%08x %x%08x\n",
2148 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2150 if (io_status || key)
2152 FIXME("Unimplemented yet parameter\n");
2153 return STATUS_NOT_IMPLEMENTED;
2156 SERVER_START_REQ( unlock_file )
2158 req->handle = hFile;
2159 req->offset_low = offset->u.LowPart;
2160 req->offset_high = offset->u.HighPart;
2161 req->count_low = count->u.LowPart;
2162 req->count_high = count->u.HighPart;
2163 status = wine_server_call( req );
2165 SERVER_END_REQ;
2166 return status;
2169 /******************************************************************
2170 * NtCreateNamedPipeFile (NTDLL.@)
2174 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2175 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2176 ULONG sharing, ULONG dispo, ULONG options,
2177 ULONG pipe_type, ULONG read_mode,
2178 ULONG completion_mode, ULONG max_inst,
2179 ULONG inbound_quota, ULONG outbound_quota,
2180 PLARGE_INTEGER timeout)
2182 NTSTATUS status;
2184 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2185 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2186 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2187 outbound_quota, timeout);
2189 /* assume we only get relative timeout */
2190 if (timeout->QuadPart > 0)
2191 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2193 SERVER_START_REQ( create_named_pipe )
2195 req->access = access;
2196 req->attributes = attr->Attributes;
2197 req->rootdir = attr->RootDirectory;
2198 req->options = options;
2199 req->flags =
2200 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2201 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2202 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2203 req->maxinstances = max_inst;
2204 req->outsize = outbound_quota;
2205 req->insize = inbound_quota;
2206 req->timeout = timeout->QuadPart;
2207 wine_server_add_data( req, attr->ObjectName->Buffer,
2208 attr->ObjectName->Length );
2209 status = wine_server_call( req );
2210 if (!status) *handle = reply->handle;
2212 SERVER_END_REQ;
2213 return status;
2216 /******************************************************************
2217 * NtDeleteFile (NTDLL.@)
2221 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2223 NTSTATUS status;
2224 HANDLE hFile;
2225 IO_STATUS_BLOCK io;
2227 TRACE("%p\n", ObjectAttributes);
2228 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2229 ObjectAttributes, &io, NULL, 0,
2230 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2231 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2232 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2233 return status;
2236 /******************************************************************
2237 * NtCancelIoFile (NTDLL.@)
2241 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2243 LARGE_INTEGER timeout;
2245 TRACE("%p %p\n", hFile, io_status );
2247 SERVER_START_REQ( cancel_async )
2249 req->handle = hFile;
2250 wine_server_call( req );
2252 SERVER_END_REQ;
2253 /* Let some APC be run, so that we can run the remaining APCs on hFile
2254 * either the cancelation of the pending one, but also the execution
2255 * of the queued APC, but not yet run. This is needed to ensure proper
2256 * clean-up of allocated data.
2258 timeout.u.LowPart = timeout.u.HighPart = 0;
2259 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2262 /******************************************************************************
2263 * NtCreateMailslotFile [NTDLL.@]
2264 * ZwCreateMailslotFile [NTDLL.@]
2266 * PARAMS
2267 * pHandle [O] pointer to receive the handle created
2268 * DesiredAccess [I] access mode (read, write, etc)
2269 * ObjectAttributes [I] fully qualified NT path of the mailslot
2270 * IoStatusBlock [O] receives completion status and other info
2271 * CreateOptions [I]
2272 * MailslotQuota [I]
2273 * MaxMessageSize [I]
2274 * TimeOut [I]
2276 * RETURNS
2277 * An NT status code
2279 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2280 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2281 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2282 PLARGE_INTEGER TimeOut)
2284 LARGE_INTEGER timeout;
2285 NTSTATUS ret;
2287 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2288 pHandle, DesiredAccess, attr, IoStatusBlock,
2289 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2291 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2292 if (!attr) return STATUS_INVALID_PARAMETER;
2293 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2296 * For a NULL TimeOut pointer set the default timeout value
2298 if (!TimeOut)
2299 timeout.QuadPart = -1;
2300 else
2301 timeout.QuadPart = TimeOut->QuadPart;
2303 SERVER_START_REQ( create_mailslot )
2305 req->access = DesiredAccess;
2306 req->attributes = attr->Attributes;
2307 req->rootdir = attr->RootDirectory;
2308 req->max_msgsize = MaxMessageSize;
2309 req->read_timeout = timeout.QuadPart;
2310 wine_server_add_data( req, attr->ObjectName->Buffer,
2311 attr->ObjectName->Length );
2312 ret = wine_server_call( req );
2313 if( ret == STATUS_SUCCESS )
2314 *pHandle = reply->handle;
2316 SERVER_END_REQ;
2318 return ret;