push 856d50732ba67807092b0a409f2aa3863489b114
[wine/hacks.git] / dlls / ntdll / file.c
blob6584a92db6453beb651da30bab8057c1ed1392c6
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 *
239 static void WINAPI FILE_AsyncReadService(void*, PIO_STATUS_BLOCK, ULONG);
240 static void WINAPI FILE_AsyncWriteService(void*, PIO_STATUS_BLOCK, ULONG);
242 typedef struct async_fileio
244 HANDLE handle;
245 PIO_APC_ROUTINE apc;
246 void* apc_user;
247 char* buffer;
248 unsigned int already;
249 unsigned int count;
250 BOOL avail_mode;
251 HANDLE event;
252 } async_fileio;
254 static void fileio_terminate(async_fileio *fileio, IO_STATUS_BLOCK* iosb, NTSTATUS status)
256 TRACE("data: %p\n", fileio);
258 iosb->u.Status = status;
259 iosb->Information = fileio->already;
260 RtlFreeHeap( GetProcessHeap(), 0, fileio );
264 static ULONG fileio_queue_async(async_fileio* fileio, IO_STATUS_BLOCK* iosb,
265 BOOL do_read)
267 PIO_APC_ROUTINE apc = do_read ? FILE_AsyncReadService : FILE_AsyncWriteService;
268 NTSTATUS status;
270 SERVER_START_REQ( register_async )
272 req->handle = fileio->handle;
273 req->async.callback = apc;
274 req->async.iosb = iosb;
275 req->async.arg = fileio;
276 req->async.apc = fileio->apc;
277 req->async.apc_arg = fileio->apc_user;
278 req->async.event = fileio->event;
279 req->type = do_read ? ASYNC_TYPE_READ : ASYNC_TYPE_WRITE;
280 req->count = (fileio->count < fileio->already) ? 0 : fileio->count - fileio->already;
281 status = wine_server_call( req );
283 SERVER_END_REQ;
285 if (status != STATUS_PENDING)
286 fileio_terminate( fileio, iosb, status );
287 else
288 NtCurrentTeb()->num_async_io++;
289 return status;
292 /***********************************************************************
293 * FILE_GetNtStatus(void)
295 * Retrieve the Nt Status code from errno.
296 * Try to be consistent with FILE_SetDosError().
298 NTSTATUS FILE_GetNtStatus(void)
300 int err = errno;
302 TRACE( "errno = %d\n", errno );
303 switch (err)
305 case EAGAIN: return STATUS_SHARING_VIOLATION;
306 case EBADF: return STATUS_INVALID_HANDLE;
307 case EBUSY: return STATUS_DEVICE_BUSY;
308 case ENOSPC: return STATUS_DISK_FULL;
309 case EPERM:
310 case EROFS:
311 case EACCES: return STATUS_ACCESS_DENIED;
312 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
313 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
314 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
315 case EMFILE:
316 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
317 case EINVAL: return STATUS_INVALID_PARAMETER;
318 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
319 case EPIPE: return STATUS_PIPE_BROKEN;
320 case EIO: return STATUS_DEVICE_NOT_READY;
321 #ifdef ENOMEDIUM
322 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
323 #endif
324 case ENXIO: return STATUS_NO_SUCH_DEVICE;
325 case ENOTTY:
326 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
327 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
328 case EFAULT: return STATUS_ACCESS_VIOLATION;
329 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
330 case ENOEXEC: /* ?? */
331 case EEXIST: /* ?? */
332 default:
333 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
334 return STATUS_UNSUCCESSFUL;
338 /***********************************************************************
339 * FILE_AsyncReadService (INTERNAL)
341 static void WINAPI FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
343 async_fileio *fileio = (async_fileio*)user;
344 int fd, needs_close, result;
346 TRACE("%p %p 0x%x\n", iosb, fileio->buffer, status);
348 switch (status)
350 case STATUS_ALERTED: /* got some new data */
351 /* check to see if the data is ready (non-blocking) */
352 if ((status = server_get_unix_fd( fileio->handle, FILE_READ_DATA, &fd,
353 &needs_close, NULL, NULL )))
355 fileio_terminate(fileio, iosb, status);
356 break;
358 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
359 if (needs_close) close( fd );
361 if (result < 0)
363 if (errno == EAGAIN || errno == EINTR)
365 TRACE("Deferred read %d\n", errno);
366 status = STATUS_PENDING;
368 else /* check to see if the transfer is complete */
369 status = FILE_GetNtStatus();
371 else if (result == 0)
373 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
375 else
377 fileio->already += result;
378 if (fileio->already >= fileio->count || fileio->avail_mode)
379 status = STATUS_SUCCESS;
380 else
382 /* if we only have to read the available data, and none is available,
383 * simply cancel the request. If data was available, it has been read
384 * while in by previous call (NtDelayExecution)
386 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
389 TRACE("read %d more bytes %u/%u so far (%s)\n",
390 result, fileio->already, fileio->count,
391 (status == STATUS_SUCCESS) ? "success" : "pending");
393 /* queue another async operation ? */
394 if (status == STATUS_PENDING)
395 fileio_queue_async(fileio, iosb, TRUE);
396 else
397 fileio_terminate(fileio, iosb, status);
398 break;
399 default:
400 fileio_terminate(fileio, iosb, status);
401 break;
405 struct io_timeouts
407 int interval; /* max interval between two bytes */
408 int total; /* total timeout for the whole operation */
409 int end_time; /* absolute time of end of operation */
412 /* retrieve the I/O timeouts to use for a given handle */
413 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
414 struct io_timeouts *timeouts )
416 NTSTATUS status = STATUS_SUCCESS;
418 timeouts->interval = timeouts->total = -1;
420 switch(type)
422 case FD_TYPE_SERIAL:
424 /* GetCommTimeouts */
425 SERIAL_TIMEOUTS st;
426 IO_STATUS_BLOCK io;
428 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
429 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
430 if (status) break;
432 if (is_read)
434 if (st.ReadIntervalTimeout)
435 timeouts->interval = st.ReadIntervalTimeout;
437 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
439 timeouts->total = st.ReadTotalTimeoutConstant;
440 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
441 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
443 else if (st.ReadIntervalTimeout == MAXDWORD)
444 timeouts->interval = 0;
446 else /* write */
448 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
450 timeouts->total = st.WriteTotalTimeoutConstant;
451 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
452 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
456 break;
457 case FD_TYPE_MAILSLOT:
458 if (is_read)
460 timeouts->interval = 0; /* return as soon as we got something */
461 SERVER_START_REQ( set_mailslot_info )
463 req->handle = handle;
464 req->flags = 0;
465 if (!(status = wine_server_call( req ))) timeouts->total = reply->read_timeout;
467 SERVER_END_REQ;
469 break;
470 case FD_TYPE_SOCKET:
471 case FD_TYPE_PIPE:
472 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
473 break;
474 default:
475 break;
477 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
478 return STATUS_SUCCESS;
482 /* retrieve the timeout for the next wait, in milliseconds */
483 static inline int get_next_io_timeout( struct io_timeouts *timeouts, ULONG already )
485 int ret = -1;
487 if (timeouts->total != -1)
489 ret = timeouts->end_time - NtGetTickCount();
490 if (ret < 0) ret = 0;
492 if (already && timeouts->interval != -1)
494 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
496 return ret;
500 /******************************************************************************
501 * NtReadFile [NTDLL.@]
502 * ZwReadFile [NTDLL.@]
504 * Read from an open file handle.
506 * PARAMS
507 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
508 * Event [I] Event to signal upon completion (or NULL)
509 * ApcRoutine [I] Callback to call upon completion (or NULL)
510 * ApcContext [I] Context for ApcRoutine (or NULL)
511 * IoStatusBlock [O] Receives information about the operation on return
512 * Buffer [O] Destination for the data read
513 * Length [I] Size of Buffer
514 * ByteOffset [O] Destination for the new file pointer position (or NULL)
515 * Key [O] Function unknown (may be NULL)
517 * RETURNS
518 * Success: 0. IoStatusBlock is updated, and the Information member contains
519 * The number of bytes read.
520 * Failure: An NTSTATUS error code describing the error.
522 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
523 PIO_APC_ROUTINE apc, void* apc_user,
524 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
525 PLARGE_INTEGER offset, PULONG key)
527 int result, unix_handle, needs_close, flags, timeout_init_done = 0;
528 struct io_timeouts timeouts;
529 NTSTATUS status;
530 ULONG total = 0;
531 enum server_fd_type type;
533 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
534 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
536 if (!io_status) return STATUS_ACCESS_VIOLATION;
538 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
539 &needs_close, &type, &flags );
540 if (status) return status;
542 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
544 /* async I/O doesn't make sense on regular files */
545 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
547 if (errno != EINTR)
549 status = FILE_GetNtStatus();
550 goto done;
553 if (!(flags & FD_FLAG_OVERLAPPED)) /* update file pointer position */
554 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
556 total = result;
557 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
558 goto done;
561 for (;;)
563 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
565 total += result;
566 if (!result || total == length)
568 if (total)
569 status = STATUS_SUCCESS;
570 else
571 status = (type == FD_TYPE_FILE) ? STATUS_END_OF_FILE : STATUS_PIPE_BROKEN;
572 goto done;
575 else
577 if (errno == EINTR) continue;
578 if (errno != EAGAIN)
580 status = FILE_GetNtStatus();
581 goto done;
585 if (flags & FD_FLAG_OVERLAPPED)
587 async_fileio *fileio;
589 if (total && (flags & FD_FLAG_AVAILABLE))
591 status = STATUS_SUCCESS;
592 goto done;
595 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
597 status = STATUS_NO_MEMORY;
598 goto done;
600 fileio->handle = hFile;
601 fileio->already = total;
602 fileio->count = length;
603 fileio->apc = apc;
604 fileio->apc_user = apc_user;
605 fileio->buffer = buffer;
606 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
607 fileio->event = hEvent;
608 status = fileio_queue_async(fileio, io_status, TRUE);
609 goto done;
611 else /* synchronous read, wait for the fd to become ready */
613 struct pollfd pfd;
614 int ret, timeout;
616 if (!timeout_init_done)
618 timeout_init_done = 1;
619 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
620 goto done;
621 if (hEvent) NtResetEvent( hEvent, NULL );
623 timeout = get_next_io_timeout( &timeouts, total );
625 pfd.fd = unix_handle;
626 pfd.events = POLLIN;
628 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
630 if (total) /* return with what we got so far */
631 status = STATUS_SUCCESS;
632 else
633 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
634 goto done;
636 if (ret == -1 && errno != EINTR)
638 status = FILE_GetNtStatus();
639 goto done;
641 /* will now restart the read */
645 done:
646 if (needs_close) close( unix_handle );
647 if (status == STATUS_SUCCESS)
649 io_status->u.Status = status;
650 io_status->Information = total;
651 TRACE("= SUCCESS (%u)\n", total);
652 if (hEvent) NtSetEvent( hEvent, NULL );
653 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
654 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
656 else
658 TRACE("= 0x%08x\n", status);
659 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
661 return status;
664 /***********************************************************************
665 * FILE_AsyncWriteService (INTERNAL)
667 static void WINAPI FILE_AsyncWriteService(void *ovp, IO_STATUS_BLOCK *iosb, ULONG status)
669 async_fileio *fileio = (async_fileio *) ovp;
670 int result, fd, needs_close;
671 enum server_fd_type type;
673 TRACE("(%p %p 0x%x)\n",iosb, fileio->buffer, status);
675 switch (status)
677 case STATUS_ALERTED:
678 /* write some data (non-blocking) */
679 if ((status = server_get_unix_fd( fileio->handle, FILE_WRITE_DATA, &fd,
680 &needs_close, &type, NULL )))
682 fileio_terminate(fileio, iosb, status);
683 break;
685 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
686 result = send( fd, fileio->buffer, 0, 0 );
687 else
688 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
690 if (needs_close) close( fd );
692 if (result < 0)
694 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
695 else status = FILE_GetNtStatus();
697 else
699 fileio->already += result;
700 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
701 TRACE("wrote %d more bytes %u/%u so far\n", result, fileio->already, fileio->count);
703 if (status == STATUS_PENDING)
704 fileio_queue_async(fileio, iosb, FALSE);
705 else
706 fileio_terminate(fileio, iosb, status);
707 break;
708 default:
709 fileio_terminate(fileio, iosb, status);
710 break;
714 /******************************************************************************
715 * NtWriteFile [NTDLL.@]
716 * ZwWriteFile [NTDLL.@]
718 * Write to an open file handle.
720 * PARAMS
721 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
722 * Event [I] Event to signal upon completion (or NULL)
723 * ApcRoutine [I] Callback to call upon completion (or NULL)
724 * ApcContext [I] Context for ApcRoutine (or NULL)
725 * IoStatusBlock [O] Receives information about the operation on return
726 * Buffer [I] Source for the data to write
727 * Length [I] Size of Buffer
728 * ByteOffset [O] Destination for the new file pointer position (or NULL)
729 * Key [O] Function unknown (may be NULL)
731 * RETURNS
732 * Success: 0. IoStatusBlock is updated, and the Information member contains
733 * The number of bytes written.
734 * Failure: An NTSTATUS error code describing the error.
736 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
737 PIO_APC_ROUTINE apc, void* apc_user,
738 PIO_STATUS_BLOCK io_status,
739 const void* buffer, ULONG length,
740 PLARGE_INTEGER offset, PULONG key)
742 int result, unix_handle, needs_close, flags, timeout_init_done = 0;
743 struct io_timeouts timeouts;
744 NTSTATUS status;
745 ULONG total = 0;
746 enum server_fd_type type;
748 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
749 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
751 if (!io_status) return STATUS_ACCESS_VIOLATION;
753 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
754 &needs_close, &type, &flags );
755 if (status) return status;
757 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
759 /* async I/O doesn't make sense on regular files */
760 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
762 if (errno != EINTR)
764 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
765 else status = FILE_GetNtStatus();
766 goto done;
770 if (!(flags & FD_FLAG_OVERLAPPED)) /* update file pointer position */
771 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
773 total = result;
774 status = STATUS_SUCCESS;
775 goto done;
778 for (;;)
780 /* zero-length writes on sockets may not work with plain write(2) */
781 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
782 result = send( unix_handle, buffer, 0, 0 );
783 else
784 result = write( unix_handle, (const char *)buffer + total, length - total );
786 if (result >= 0)
788 total += result;
789 if (total == length)
791 status = STATUS_SUCCESS;
792 goto done;
795 else
797 if (errno == EINTR) continue;
798 if (errno != EAGAIN)
800 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
801 else status = FILE_GetNtStatus();
802 goto done;
806 if (flags & FD_FLAG_OVERLAPPED)
808 async_fileio *fileio;
810 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
812 status = STATUS_NO_MEMORY;
813 goto done;
815 fileio->handle = hFile;
816 fileio->already = total;
817 fileio->count = length;
818 fileio->apc = apc;
819 fileio->apc_user = apc_user;
820 fileio->buffer = (void*)buffer;
821 fileio->event = hEvent;
822 status = fileio_queue_async(fileio, io_status, FALSE);
823 goto done;
825 else /* synchronous write, wait for the fd to become ready */
827 struct pollfd pfd;
828 int ret, timeout;
830 if (!timeout_init_done)
832 timeout_init_done = 1;
833 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
834 goto done;
835 if (hEvent) NtResetEvent( hEvent, NULL );
837 timeout = get_next_io_timeout( &timeouts, total );
839 pfd.fd = unix_handle;
840 pfd.events = POLLIN;
842 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
844 /* return with what we got so far */
845 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
846 goto done;
848 if (ret == -1 && errno != EINTR)
850 status = FILE_GetNtStatus();
851 goto done;
853 /* will now restart the write */
857 done:
858 if (needs_close) close( unix_handle );
859 if (status == STATUS_SUCCESS)
861 io_status->u.Status = status;
862 io_status->Information = total;
863 TRACE("= SUCCESS (%u)\n", total);
864 if (hEvent) NtSetEvent( hEvent, NULL );
865 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
866 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
868 else
870 TRACE("= 0x%08x\n", status);
871 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
873 return status;
876 /**************************************************************************
877 * NtDeviceIoControlFile [NTDLL.@]
878 * ZwDeviceIoControlFile [NTDLL.@]
880 * Perform an I/O control operation on an open file handle.
882 * PARAMS
883 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
884 * event [I] Event to signal upon completion (or NULL)
885 * apc [I] Callback to call upon completion (or NULL)
886 * apc_context [I] Context for ApcRoutine (or NULL)
887 * io [O] Receives information about the operation on return
888 * code [I] Control code for the operation to perform
889 * in_buffer [I] Source for any input data required (or NULL)
890 * in_size [I] Size of InputBuffer
891 * out_buffer [O] Source for any output data returned (or NULL)
892 * out_size [I] Size of OutputBuffer
894 * RETURNS
895 * Success: 0. IoStatusBlock is updated.
896 * Failure: An NTSTATUS error code describing the error.
898 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
899 PIO_APC_ROUTINE apc, PVOID apc_context,
900 PIO_STATUS_BLOCK io, ULONG code,
901 PVOID in_buffer, ULONG in_size,
902 PVOID out_buffer, ULONG out_size)
904 ULONG device = (code >> 16);
906 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
907 handle, event, apc, apc_context, io, code,
908 in_buffer, in_size, out_buffer, out_size);
910 switch(device)
912 case FILE_DEVICE_DISK:
913 case FILE_DEVICE_CD_ROM:
914 case FILE_DEVICE_DVD:
915 case FILE_DEVICE_CONTROLLER:
916 case FILE_DEVICE_MASS_STORAGE:
917 io->u.Status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
918 in_buffer, in_size, out_buffer, out_size);
919 break;
920 case FILE_DEVICE_SERIAL_PORT:
921 io->u.Status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
922 in_buffer, in_size, out_buffer, out_size);
923 break;
924 case FILE_DEVICE_TAPE:
925 io->u.Status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
926 in_buffer, in_size, out_buffer, out_size);
927 break;
928 default:
929 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
930 code, device, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
931 io->u.Status = STATUS_NOT_SUPPORTED;
932 break;
934 return io->u.Status;
937 /***********************************************************************
938 * pipe_completion_wait (Internal)
940 static void CALLBACK pipe_completion_wait(void *arg, PIO_STATUS_BLOCK iosb, ULONG status)
942 TRACE("for %p, status=%08x\n", iosb, status);
943 iosb->u.Status = status;
946 /**************************************************************************
947 * NtFsControlFile [NTDLL.@]
948 * ZwFsControlFile [NTDLL.@]
950 * Perform a file system control operation on an open file handle.
952 * PARAMS
953 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
954 * event [I] Event to signal upon completion (or NULL)
955 * apc [I] Callback to call upon completion (or NULL)
956 * apc_context [I] Context for ApcRoutine (or NULL)
957 * io [O] Receives information about the operation on return
958 * code [I] Control code for the operation to perform
959 * in_buffer [I] Source for any input data required (or NULL)
960 * in_size [I] Size of InputBuffer
961 * out_buffer [O] Source for any output data returned (or NULL)
962 * out_size [I] Size of OutputBuffer
964 * RETURNS
965 * Success: 0. IoStatusBlock is updated.
966 * Failure: An NTSTATUS error code describing the error.
968 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
969 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
970 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
972 NTSTATUS status;
974 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
975 handle, event, apc, apc_context, io, code,
976 in_buffer, in_size, out_buffer, out_size);
978 if (!io) return STATUS_INVALID_PARAMETER;
980 switch(code)
982 case FSCTL_DISMOUNT_VOLUME:
983 status = DIR_unmount_device( handle );
984 break;
986 case FSCTL_PIPE_LISTEN:
988 HANDLE internal_event = 0;
990 if(!event && !apc)
992 status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
993 if (status != STATUS_SUCCESS) break;
995 SERVER_START_REQ(connect_named_pipe)
997 req->handle = handle;
998 req->async.callback = pipe_completion_wait;
999 req->async.iosb = io;
1000 req->async.arg = NULL;
1001 req->async.apc = apc;
1002 req->async.apc_arg = apc_context;
1003 req->async.event = event ? event : internal_event;
1004 status = wine_server_call(req);
1006 SERVER_END_REQ;
1008 if (internal_event && status == STATUS_PENDING)
1010 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
1011 status = io->u.Status;
1013 if (internal_event) NtClose(internal_event);
1015 break;
1017 case FSCTL_PIPE_WAIT:
1019 HANDLE internal_event = 0;
1020 FILE_PIPE_WAIT_FOR_BUFFER *buff = in_buffer;
1022 if(!event && !apc)
1024 status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
1025 if (status != STATUS_SUCCESS) break;
1027 SERVER_START_REQ(wait_named_pipe)
1029 req->handle = handle;
1030 req->timeout = buff->TimeoutSpecified ? buff->Timeout.QuadPart / -10000L
1031 : NMPWAIT_USE_DEFAULT_WAIT;
1032 req->async.callback = pipe_completion_wait;
1033 req->async.iosb = io;
1034 req->async.arg = NULL;
1035 req->async.apc = apc;
1036 req->async.apc_arg = apc_context;
1037 req->async.event = event ? event : internal_event;
1038 wine_server_add_data( req, buff->Name, buff->NameLength );
1039 status = wine_server_call( req );
1041 SERVER_END_REQ;
1043 if (internal_event && status == STATUS_PENDING)
1045 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
1046 status = io->u.Status;
1048 if (internal_event) NtClose(internal_event);
1050 break;
1052 case FSCTL_PIPE_PEEK:
1054 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1055 int avail = 0, fd, needs_close, flags;
1057 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1059 status = STATUS_INFO_LENGTH_MISMATCH;
1060 break;
1063 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, &flags )))
1064 break;
1066 if (flags & FD_FLAG_RECV_SHUTDOWN)
1068 if (needs_close) close( fd );
1069 status = STATUS_PIPE_DISCONNECTED;
1070 break;
1073 #ifdef FIONREAD
1074 if (ioctl( fd, FIONREAD, &avail ) != 0)
1076 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1077 if (needs_close) close( fd );
1078 status = FILE_GetNtStatus();
1079 break;
1081 #endif
1082 if (!avail) /* check for closed pipe */
1084 struct pollfd pollfd;
1085 int ret;
1087 pollfd.fd = fd;
1088 pollfd.events = POLLIN;
1089 pollfd.revents = 0;
1090 ret = poll( &pollfd, 1, 0 );
1091 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1093 if (needs_close) close( fd );
1094 status = STATUS_PIPE_BROKEN;
1095 break;
1098 buffer->NamedPipeState = 0; /* FIXME */
1099 buffer->ReadDataAvailable = avail;
1100 buffer->NumberOfMessages = 0; /* FIXME */
1101 buffer->MessageLength = 0; /* FIXME */
1102 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1103 status = STATUS_SUCCESS;
1104 if (avail)
1106 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1107 if (data_size)
1109 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1110 if (res >= 0) io->Information += res;
1113 if (needs_close) close( fd );
1115 break;
1117 case FSCTL_PIPE_DISCONNECT:
1118 SERVER_START_REQ(disconnect_named_pipe)
1120 req->handle = handle;
1121 status = wine_server_call(req);
1122 if (!status)
1124 int fd = server_remove_fd_from_cache( handle );
1125 if (fd != -1) close( fd );
1128 SERVER_END_REQ;
1129 break;
1131 case FSCTL_LOCK_VOLUME:
1132 case FSCTL_UNLOCK_VOLUME:
1133 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1134 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1135 status = STATUS_SUCCESS;
1136 break;
1138 default:
1139 FIXME("Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1140 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1141 status = STATUS_NOT_SUPPORTED;
1142 break;
1145 if (status != STATUS_PENDING) io->u.Status = status;
1146 return status;
1149 /******************************************************************************
1150 * NtSetVolumeInformationFile [NTDLL.@]
1151 * ZwSetVolumeInformationFile [NTDLL.@]
1153 * Set volume information for an open file handle.
1155 * PARAMS
1156 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1157 * IoStatusBlock [O] Receives information about the operation on return
1158 * FsInformation [I] Source for volume information
1159 * Length [I] Size of FsInformation
1160 * FsInformationClass [I] Type of volume information to set
1162 * RETURNS
1163 * Success: 0. IoStatusBlock is updated.
1164 * Failure: An NTSTATUS error code describing the error.
1166 NTSTATUS WINAPI NtSetVolumeInformationFile(
1167 IN HANDLE FileHandle,
1168 PIO_STATUS_BLOCK IoStatusBlock,
1169 PVOID FsInformation,
1170 ULONG Length,
1171 FS_INFORMATION_CLASS FsInformationClass)
1173 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1174 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1175 return 0;
1178 /******************************************************************************
1179 * NtQueryInformationFile [NTDLL.@]
1180 * ZwQueryInformationFile [NTDLL.@]
1182 * Get information about an open file handle.
1184 * PARAMS
1185 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1186 * io [O] Receives information about the operation on return
1187 * ptr [O] Destination for file information
1188 * len [I] Size of FileInformation
1189 * class [I] Type of file information to get
1191 * RETURNS
1192 * Success: 0. IoStatusBlock and FileInformation are updated.
1193 * Failure: An NTSTATUS error code describing the error.
1195 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1196 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1198 static const size_t info_sizes[] =
1201 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1202 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1203 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1204 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1205 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1206 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1207 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1208 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1209 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1210 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1211 0, /* FileLinkInformation */
1212 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1213 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1214 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1215 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1216 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1217 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1218 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1219 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1220 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1221 0, /* FileAlternateNameInformation */
1222 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1223 0, /* FilePipeInformation */
1224 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1225 0, /* FilePipeRemoteInformation */
1226 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1227 0, /* FileMailslotSetInformation */
1228 0, /* FileCompressionInformation */
1229 0, /* FileObjectIdInformation */
1230 0, /* FileCompletionInformation */
1231 0, /* FileMoveClusterInformation */
1232 0, /* FileQuotaInformation */
1233 0, /* FileReparsePointInformation */
1234 0, /* FileNetworkOpenInformation */
1235 0, /* FileAttributeTagInformation */
1236 0 /* FileTrackingInformation */
1239 struct stat st;
1240 int fd, needs_close = FALSE;
1242 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1244 io->Information = 0;
1246 if (class <= 0 || class >= FileMaximumInformation)
1247 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1248 if (!info_sizes[class])
1250 FIXME("Unsupported class (%d)\n", class);
1251 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1253 if (len < info_sizes[class])
1254 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1256 if (class != FilePipeLocalInformation)
1258 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1259 return io->u.Status;
1262 switch (class)
1264 case FileBasicInformation:
1266 FILE_BASIC_INFORMATION *info = ptr;
1268 if (fstat( fd, &st ) == -1)
1269 io->u.Status = FILE_GetNtStatus();
1270 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1271 io->u.Status = STATUS_INVALID_INFO_CLASS;
1272 else
1274 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1275 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1276 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1277 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1278 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1279 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1280 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1281 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1284 break;
1285 case FileStandardInformation:
1287 FILE_STANDARD_INFORMATION *info = ptr;
1289 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1290 else
1292 if ((info->Directory = S_ISDIR(st.st_mode)))
1294 info->AllocationSize.QuadPart = 0;
1295 info->EndOfFile.QuadPart = 0;
1296 info->NumberOfLinks = 1;
1297 info->DeletePending = FALSE;
1299 else
1301 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1302 info->EndOfFile.QuadPart = st.st_size;
1303 info->NumberOfLinks = st.st_nlink;
1304 info->DeletePending = FALSE; /* FIXME */
1308 break;
1309 case FilePositionInformation:
1311 FILE_POSITION_INFORMATION *info = ptr;
1312 off_t res = lseek( fd, 0, SEEK_CUR );
1313 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1314 else info->CurrentByteOffset.QuadPart = res;
1316 break;
1317 case FileInternalInformation:
1319 FILE_INTERNAL_INFORMATION *info = ptr;
1321 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1322 else info->IndexNumber.QuadPart = st.st_ino;
1324 break;
1325 case FileEaInformation:
1327 FILE_EA_INFORMATION *info = ptr;
1328 info->EaSize = 0;
1330 break;
1331 case FileEndOfFileInformation:
1333 FILE_END_OF_FILE_INFORMATION *info = ptr;
1335 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1336 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1338 break;
1339 case FileAllInformation:
1341 FILE_ALL_INFORMATION *info = ptr;
1343 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1344 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1345 io->u.Status = STATUS_INVALID_INFO_CLASS;
1346 else
1348 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1350 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1351 info->StandardInformation.AllocationSize.QuadPart = 0;
1352 info->StandardInformation.EndOfFile.QuadPart = 0;
1353 info->StandardInformation.NumberOfLinks = 1;
1354 info->StandardInformation.DeletePending = FALSE;
1356 else
1358 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1359 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1360 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1361 info->StandardInformation.NumberOfLinks = st.st_nlink;
1362 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1364 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1365 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1366 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1367 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1368 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1369 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1370 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1371 info->EaInformation.EaSize = 0;
1372 info->AccessInformation.AccessFlags = 0; /* FIXME */
1373 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1374 info->ModeInformation.Mode = 0; /* FIXME */
1375 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1376 info->NameInformation.FileNameLength = 0;
1377 io->Information = sizeof(*info) - sizeof(WCHAR);
1380 break;
1381 case FileMailslotQueryInformation:
1383 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1385 SERVER_START_REQ( set_mailslot_info )
1387 req->handle = hFile;
1388 req->flags = 0;
1389 io->u.Status = wine_server_call( req );
1390 if( io->u.Status == STATUS_SUCCESS )
1392 info->MaximumMessageSize = reply->max_msgsize;
1393 info->MailslotQuota = 0;
1394 info->NextMessageSize = 0;
1395 info->MessagesAvailable = 0;
1396 info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1399 SERVER_END_REQ;
1400 if (!io->u.Status)
1402 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1403 char *tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size );
1404 if (tmpbuf)
1406 int fd, needs_close;
1407 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1409 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1410 info->MessagesAvailable = (res > 0);
1411 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1412 if (needs_close) close( fd );
1414 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1418 break;
1419 case FilePipeLocalInformation:
1421 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1423 SERVER_START_REQ( get_named_pipe_info )
1425 req->handle = hFile;
1426 if (!(io->u.Status = wine_server_call( req )))
1428 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1429 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1430 pli->NamedPipeConfiguration = 0; /* FIXME */
1431 pli->MaximumInstances = reply->maxinstances;
1432 pli->CurrentInstances = reply->instances;
1433 pli->InboundQuota = reply->insize;
1434 pli->ReadDataAvailable = 0; /* FIXME */
1435 pli->OutboundQuota = reply->outsize;
1436 pli->WriteQuotaAvailable = 0; /* FIXME */
1437 pli->NamedPipeState = 0; /* FIXME */
1438 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1439 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1442 SERVER_END_REQ;
1444 break;
1445 default:
1446 FIXME("Unsupported class (%d)\n", class);
1447 io->u.Status = STATUS_NOT_IMPLEMENTED;
1448 break;
1450 if (needs_close) close( fd );
1451 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1452 return io->u.Status;
1455 /******************************************************************************
1456 * NtSetInformationFile [NTDLL.@]
1457 * ZwSetInformationFile [NTDLL.@]
1459 * Set information about an open file handle.
1461 * PARAMS
1462 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1463 * io [O] Receives information about the operation on return
1464 * ptr [I] Source for file information
1465 * len [I] Size of FileInformation
1466 * class [I] Type of file information to set
1468 * RETURNS
1469 * Success: 0. io is updated.
1470 * Failure: An NTSTATUS error code describing the error.
1472 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1473 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1475 int fd, needs_close;
1477 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1479 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1480 return io->u.Status;
1482 io->u.Status = STATUS_SUCCESS;
1483 switch (class)
1485 case FileBasicInformation:
1486 if (len >= sizeof(FILE_BASIC_INFORMATION))
1488 struct stat st;
1489 const FILE_BASIC_INFORMATION *info = ptr;
1491 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1493 ULONGLONG sec, nsec;
1494 struct timeval tv[2];
1496 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1499 tv[0].tv_sec = tv[0].tv_usec = 0;
1500 tv[1].tv_sec = tv[1].tv_usec = 0;
1501 if (!fstat( fd, &st ))
1503 tv[0].tv_sec = st.st_atime;
1504 tv[1].tv_sec = st.st_mtime;
1507 if (info->LastAccessTime.QuadPart)
1509 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1510 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1511 tv[0].tv_usec = (UINT)nsec / 10;
1513 if (info->LastWriteTime.QuadPart)
1515 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1516 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1517 tv[1].tv_usec = (UINT)nsec / 10;
1519 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1522 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1524 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1525 else
1527 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1529 if (S_ISDIR( st.st_mode))
1530 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1531 else
1532 st.st_mode &= ~0222; /* clear write permission bits */
1534 else
1536 /* add write permission only where we already have read permission */
1537 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1539 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1543 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1544 break;
1546 case FilePositionInformation:
1547 if (len >= sizeof(FILE_POSITION_INFORMATION))
1549 const FILE_POSITION_INFORMATION *info = ptr;
1551 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1552 io->u.Status = FILE_GetNtStatus();
1554 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1555 break;
1557 case FileEndOfFileInformation:
1558 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1560 struct stat st;
1561 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1563 /* first try normal truncate */
1564 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1566 /* now check for the need to extend the file */
1567 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1569 static const char zero;
1571 /* extend the file one byte beyond the requested size and then truncate it */
1572 /* this should work around ftruncate implementations that can't extend files */
1573 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1574 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1576 io->u.Status = FILE_GetNtStatus();
1578 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1579 break;
1581 case FileMailslotSetInformation:
1583 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1585 SERVER_START_REQ( set_mailslot_info )
1587 req->handle = handle;
1588 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1589 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1590 io->u.Status = wine_server_call( req );
1592 SERVER_END_REQ;
1594 break;
1596 default:
1597 FIXME("Unsupported class (%d)\n", class);
1598 io->u.Status = STATUS_NOT_IMPLEMENTED;
1599 break;
1601 if (needs_close) close( fd );
1602 io->Information = 0;
1603 return io->u.Status;
1607 /******************************************************************************
1608 * NtQueryFullAttributesFile (NTDLL.@)
1610 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1611 FILE_NETWORK_OPEN_INFORMATION *info )
1613 ANSI_STRING unix_name;
1614 NTSTATUS status;
1616 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1617 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1619 struct stat st;
1621 if (stat( unix_name.Buffer, &st ) == -1)
1622 status = FILE_GetNtStatus();
1623 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1624 status = STATUS_INVALID_INFO_CLASS;
1625 else
1627 if (S_ISDIR(st.st_mode))
1629 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1630 info->AllocationSize.QuadPart = 0;
1631 info->EndOfFile.QuadPart = 0;
1633 else
1635 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1636 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1637 info->EndOfFile.QuadPart = st.st_size;
1639 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1640 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1641 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1642 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1643 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1644 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1645 if (DIR_is_hidden_file( attr->ObjectName ))
1646 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1648 RtlFreeAnsiString( &unix_name );
1650 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1651 return status;
1655 /******************************************************************************
1656 * NtQueryAttributesFile (NTDLL.@)
1657 * ZwQueryAttributesFile (NTDLL.@)
1659 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1661 FILE_NETWORK_OPEN_INFORMATION full_info;
1662 NTSTATUS status;
1664 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1666 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1667 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1668 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1669 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1670 info->FileAttributes = full_info.FileAttributes;
1672 return status;
1676 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1677 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1678 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1679 size_t fstypesize, unsigned int flags )
1681 if (!strncmp("cd9660", fstypename, fstypesize) ||
1682 !strncmp("udf", fstypename, fstypesize))
1684 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1685 /* Don't assume read-only, let the mount options set it below */
1686 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1688 else if (!strncmp("nfs", fstypename, fstypesize) ||
1689 !strncmp("nwfs", fstypename, fstypesize) ||
1690 !strncmp("smbfs", fstypename, fstypesize) ||
1691 !strncmp("afpfs", fstypename, fstypesize))
1693 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1694 info->Characteristics |= FILE_REMOTE_DEVICE;
1696 else if (!strncmp("procfs", fstypename, fstypesize))
1697 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1698 else
1699 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1701 if (flags & MNT_RDONLY)
1702 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1704 if (!(flags & MNT_LOCAL))
1706 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1707 info->Characteristics |= FILE_REMOTE_DEVICE;
1710 #endif
1712 /******************************************************************************
1713 * get_device_info
1715 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1717 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1719 struct stat st;
1721 info->Characteristics = 0;
1722 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1723 if (S_ISCHR( st.st_mode ))
1725 info->DeviceType = FILE_DEVICE_UNKNOWN;
1726 #ifdef linux
1727 switch(major(st.st_rdev))
1729 case MEM_MAJOR:
1730 info->DeviceType = FILE_DEVICE_NULL;
1731 break;
1732 case TTY_MAJOR:
1733 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1734 break;
1735 case LP_MAJOR:
1736 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1737 break;
1738 case SCSI_TAPE_MAJOR:
1739 info->DeviceType = FILE_DEVICE_TAPE;
1740 break;
1742 #endif
1744 else if (S_ISBLK( st.st_mode ))
1746 info->DeviceType = FILE_DEVICE_DISK;
1748 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1750 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1752 else /* regular file or directory */
1754 #if defined(linux) && defined(HAVE_FSTATFS)
1755 struct statfs stfs;
1757 /* check for floppy disk */
1758 if (major(st.st_dev) == FLOPPY_MAJOR)
1759 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1761 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1762 switch (stfs.f_type)
1764 case 0x9660: /* iso9660 */
1765 case 0x9fa1: /* supermount */
1766 case 0x15013346: /* udf */
1767 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1768 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1769 break;
1770 case 0x6969: /* nfs */
1771 case 0x517B: /* smbfs */
1772 case 0x564c: /* ncpfs */
1773 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1774 info->Characteristics |= FILE_REMOTE_DEVICE;
1775 break;
1776 case 0x01021994: /* tmpfs */
1777 case 0x28cd3d45: /* cramfs */
1778 case 0x1373: /* devfs */
1779 case 0x9fa0: /* procfs */
1780 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1781 break;
1782 default:
1783 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1784 break;
1786 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1787 struct statfs stfs;
1789 if (fstatfs( fd, &stfs ) < 0)
1790 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1791 else
1792 get_device_info_fstatfs( info, stfs.f_fstypename,
1793 sizeof(stfs.f_fstypename), stfs.f_flags );
1794 #elif defined(__NetBSD__)
1795 struct statvfs stfs;
1797 if (fstatvfs( fd, &stfs) < 0)
1798 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1799 else
1800 get_device_info_fstatfs( info, stfs.f_fstypename,
1801 sizeof(stfs.f_fstypename), stfs.f_flag );
1802 #elif defined(sun)
1803 /* Use dkio to work out device types */
1805 # include <sys/dkio.h>
1806 # include <sys/vtoc.h>
1807 struct dk_cinfo dkinf;
1808 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1809 if(retval==-1){
1810 WARN("Unable to get disk device type information - assuming a disk like device\n");
1811 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1813 switch (dkinf.dki_ctype)
1815 case DKC_CDROM:
1816 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1817 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1818 break;
1819 case DKC_NCRFLOPPY:
1820 case DKC_SMSFLOPPY:
1821 case DKC_INTEL82072:
1822 case DKC_INTEL82077:
1823 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1824 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1825 break;
1826 case DKC_MD:
1827 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1828 break;
1829 default:
1830 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1833 #else
1834 static int warned;
1835 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1836 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1837 #endif
1838 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1840 return STATUS_SUCCESS;
1844 /******************************************************************************
1845 * NtQueryVolumeInformationFile [NTDLL.@]
1846 * ZwQueryVolumeInformationFile [NTDLL.@]
1848 * Get volume information for an open file handle.
1850 * PARAMS
1851 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1852 * io [O] Receives information about the operation on return
1853 * buffer [O] Destination for volume information
1854 * length [I] Size of FsInformation
1855 * info_class [I] Type of volume information to set
1857 * RETURNS
1858 * Success: 0. io and buffer are updated.
1859 * Failure: An NTSTATUS error code describing the error.
1861 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1862 PVOID buffer, ULONG length,
1863 FS_INFORMATION_CLASS info_class )
1865 int fd, needs_close;
1866 struct stat st;
1868 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1869 return io->u.Status;
1871 io->u.Status = STATUS_NOT_IMPLEMENTED;
1872 io->Information = 0;
1874 switch( info_class )
1876 case FileFsVolumeInformation:
1877 FIXME( "%p: volume info not supported\n", handle );
1878 break;
1879 case FileFsLabelInformation:
1880 FIXME( "%p: label info not supported\n", handle );
1881 break;
1882 case FileFsSizeInformation:
1883 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1884 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1885 else
1887 FILE_FS_SIZE_INFORMATION *info = buffer;
1889 if (fstat( fd, &st ) < 0)
1891 io->u.Status = FILE_GetNtStatus();
1892 break;
1894 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1896 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1898 else
1900 /* Linux's fstatvfs is buggy */
1901 #if !defined(linux) || !defined(HAVE_FSTATFS)
1902 struct statvfs stfs;
1904 if (fstatvfs( fd, &stfs ) < 0)
1906 io->u.Status = FILE_GetNtStatus();
1907 break;
1909 info->BytesPerSector = stfs.f_frsize;
1910 #else
1911 struct statfs stfs;
1912 if (fstatfs( fd, &stfs ) < 0)
1914 io->u.Status = FILE_GetNtStatus();
1915 break;
1917 info->BytesPerSector = stfs.f_bsize;
1918 #endif
1919 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1920 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1921 info->SectorsPerAllocationUnit = 1;
1922 io->Information = sizeof(*info);
1923 io->u.Status = STATUS_SUCCESS;
1926 break;
1927 case FileFsDeviceInformation:
1928 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1929 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1930 else
1932 FILE_FS_DEVICE_INFORMATION *info = buffer;
1934 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
1935 io->Information = sizeof(*info);
1937 break;
1938 case FileFsAttributeInformation:
1939 FIXME( "%p: attribute info not supported\n", handle );
1940 break;
1941 case FileFsControlInformation:
1942 FIXME( "%p: control info not supported\n", handle );
1943 break;
1944 case FileFsFullSizeInformation:
1945 FIXME( "%p: full size info not supported\n", handle );
1946 break;
1947 case FileFsObjectIdInformation:
1948 FIXME( "%p: object id info not supported\n", handle );
1949 break;
1950 case FileFsMaximumInformation:
1951 FIXME( "%p: maximum info not supported\n", handle );
1952 break;
1953 default:
1954 io->u.Status = STATUS_INVALID_PARAMETER;
1955 break;
1957 if (needs_close) close( fd );
1958 return io->u.Status;
1962 /******************************************************************
1963 * NtFlushBuffersFile (NTDLL.@)
1965 * Flush any buffered data on an open file handle.
1967 * PARAMS
1968 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1969 * IoStatusBlock [O] Receives information about the operation on return
1971 * RETURNS
1972 * Success: 0. IoStatusBlock is updated.
1973 * Failure: An NTSTATUS error code describing the error.
1975 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1977 NTSTATUS ret;
1978 HANDLE hEvent = NULL;
1980 SERVER_START_REQ( flush_file )
1982 req->handle = hFile;
1983 ret = wine_server_call( req );
1984 hEvent = reply->event;
1986 SERVER_END_REQ;
1987 if (!ret && hEvent)
1989 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1990 NtClose( hEvent );
1992 return ret;
1995 /******************************************************************
1996 * NtLockFile (NTDLL.@)
2000 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2001 PIO_APC_ROUTINE apc, void* apc_user,
2002 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2003 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2004 BOOLEAN exclusive )
2006 NTSTATUS ret;
2007 HANDLE handle;
2008 BOOLEAN async;
2010 if (apc || io_status || key)
2012 FIXME("Unimplemented yet parameter\n");
2013 return STATUS_NOT_IMPLEMENTED;
2016 for (;;)
2018 SERVER_START_REQ( lock_file )
2020 req->handle = hFile;
2021 req->offset_low = offset->u.LowPart;
2022 req->offset_high = offset->u.HighPart;
2023 req->count_low = count->u.LowPart;
2024 req->count_high = count->u.HighPart;
2025 req->shared = !exclusive;
2026 req->wait = !dont_wait;
2027 ret = wine_server_call( req );
2028 handle = reply->handle;
2029 async = reply->overlapped;
2031 SERVER_END_REQ;
2032 if (ret != STATUS_PENDING)
2034 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2035 return ret;
2038 if (async)
2040 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2041 if (handle) NtClose( handle );
2042 return STATUS_PENDING;
2044 if (handle)
2046 NtWaitForSingleObject( handle, FALSE, NULL );
2047 NtClose( handle );
2049 else
2051 LARGE_INTEGER time;
2053 /* Unix lock conflict, sleep a bit and retry */
2054 time.QuadPart = 100 * (ULONGLONG)10000;
2055 time.QuadPart = -time.QuadPart;
2056 NtDelayExecution( FALSE, &time );
2062 /******************************************************************
2063 * NtUnlockFile (NTDLL.@)
2067 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2068 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2069 PULONG key )
2071 NTSTATUS status;
2073 TRACE( "%p %x%08x %x%08x\n",
2074 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2076 if (io_status || key)
2078 FIXME("Unimplemented yet parameter\n");
2079 return STATUS_NOT_IMPLEMENTED;
2082 SERVER_START_REQ( unlock_file )
2084 req->handle = hFile;
2085 req->offset_low = offset->u.LowPart;
2086 req->offset_high = offset->u.HighPart;
2087 req->count_low = count->u.LowPart;
2088 req->count_high = count->u.HighPart;
2089 status = wine_server_call( req );
2091 SERVER_END_REQ;
2092 return status;
2095 /******************************************************************
2096 * NtCreateNamedPipeFile (NTDLL.@)
2100 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2101 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2102 ULONG sharing, ULONG dispo, ULONG options,
2103 ULONG pipe_type, ULONG read_mode,
2104 ULONG completion_mode, ULONG max_inst,
2105 ULONG inbound_quota, ULONG outbound_quota,
2106 PLARGE_INTEGER timeout)
2108 NTSTATUS status;
2110 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2111 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2112 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2113 outbound_quota, timeout);
2115 /* assume we only get relative timeout, and storable in a DWORD as ms */
2116 if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
2117 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2119 SERVER_START_REQ( create_named_pipe )
2121 req->access = access;
2122 req->attributes = attr->Attributes;
2123 req->rootdir = attr->RootDirectory;
2124 req->options = options;
2125 req->flags =
2126 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2127 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2128 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2129 req->maxinstances = max_inst;
2130 req->outsize = outbound_quota;
2131 req->insize = inbound_quota;
2132 req->timeout = timeout->QuadPart / -10000;
2133 wine_server_add_data( req, attr->ObjectName->Buffer,
2134 attr->ObjectName->Length );
2135 status = wine_server_call( req );
2136 if (!status) *handle = reply->handle;
2138 SERVER_END_REQ;
2139 return status;
2142 /******************************************************************
2143 * NtDeleteFile (NTDLL.@)
2147 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2149 NTSTATUS status;
2150 HANDLE hFile;
2151 IO_STATUS_BLOCK io;
2153 TRACE("%p\n", ObjectAttributes);
2154 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2155 ObjectAttributes, &io, NULL, 0,
2156 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2157 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2158 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2159 return status;
2162 /******************************************************************
2163 * NtCancelIoFile (NTDLL.@)
2167 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2169 LARGE_INTEGER timeout;
2171 TRACE("%p %p\n", hFile, io_status );
2173 SERVER_START_REQ( cancel_async )
2175 req->handle = hFile;
2176 wine_server_call( req );
2178 SERVER_END_REQ;
2179 /* Let some APC be run, so that we can run the remaining APCs on hFile
2180 * either the cancelation of the pending one, but also the execution
2181 * of the queued APC, but not yet run. This is needed to ensure proper
2182 * clean-up of allocated data.
2184 timeout.u.LowPart = timeout.u.HighPart = 0;
2185 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2188 /******************************************************************************
2189 * NtCreateMailslotFile [NTDLL.@]
2190 * ZwCreateMailslotFile [NTDLL.@]
2192 * PARAMS
2193 * pHandle [O] pointer to receive the handle created
2194 * DesiredAccess [I] access mode (read, write, etc)
2195 * ObjectAttributes [I] fully qualified NT path of the mailslot
2196 * IoStatusBlock [O] receives completion status and other info
2197 * CreateOptions [I]
2198 * MailslotQuota [I]
2199 * MaxMessageSize [I]
2200 * TimeOut [I]
2202 * RETURNS
2203 * An NT status code
2205 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2206 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2207 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2208 PLARGE_INTEGER TimeOut)
2210 LARGE_INTEGER timeout;
2211 NTSTATUS ret;
2213 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2214 pHandle, DesiredAccess, attr, IoStatusBlock,
2215 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2217 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2218 if (!attr) return STATUS_INVALID_PARAMETER;
2219 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2222 * For a NULL TimeOut pointer set the default timeout value
2224 if (!TimeOut)
2225 timeout.QuadPart = -1;
2226 else
2227 timeout.QuadPart = TimeOut->QuadPart;
2229 SERVER_START_REQ( create_mailslot )
2231 req->access = DesiredAccess;
2232 req->attributes = attr->Attributes;
2233 req->rootdir = attr->RootDirectory;
2234 req->max_msgsize = MaxMessageSize;
2235 req->read_timeout = (timeout.QuadPart <= 0) ? timeout.QuadPart / -10000 : -1;
2236 wine_server_add_data( req, attr->ObjectName->Buffer,
2237 attr->ObjectName->Length );
2238 ret = wine_server_call( req );
2239 if( ret == STATUS_SUCCESS )
2240 *pHandle = reply->handle;
2242 SERVER_END_REQ;
2244 return ret;