ntdll: Rely on the server to queue the final APC for asynchronous read/write.
[wine/multimedia.git] / dlls / ntdll / file.c
blob65710c56f77554adff73f6b6b45efe5e57f18bfd
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;
672 TRACE("(%p %p 0x%x)\n",iosb, fileio->buffer, status);
674 switch (status)
676 case STATUS_ALERTED:
677 /* write some data (non-blocking) */
678 if ((status = server_get_unix_fd( fileio->handle, FILE_WRITE_DATA, &fd,
679 &needs_close, NULL, NULL )))
681 fileio_terminate(fileio, iosb, status);
682 break;
684 result = write(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
685 if (needs_close) close( fd );
687 if (result < 0)
689 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
690 else status = FILE_GetNtStatus();
692 else
694 fileio->already += result;
695 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
696 TRACE("wrote %d more bytes %u/%u so far\n", result, fileio->already, fileio->count);
698 if (status == STATUS_PENDING)
699 fileio_queue_async(fileio, iosb, FALSE);
700 else
701 fileio_terminate(fileio, iosb, status);
702 break;
703 default:
704 fileio_terminate(fileio, iosb, status);
705 break;
709 /******************************************************************************
710 * NtWriteFile [NTDLL.@]
711 * ZwWriteFile [NTDLL.@]
713 * Write to an open file handle.
715 * PARAMS
716 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
717 * Event [I] Event to signal upon completion (or NULL)
718 * ApcRoutine [I] Callback to call upon completion (or NULL)
719 * ApcContext [I] Context for ApcRoutine (or NULL)
720 * IoStatusBlock [O] Receives information about the operation on return
721 * Buffer [I] Source for the data to write
722 * Length [I] Size of Buffer
723 * ByteOffset [O] Destination for the new file pointer position (or NULL)
724 * Key [O] Function unknown (may be NULL)
726 * RETURNS
727 * Success: 0. IoStatusBlock is updated, and the Information member contains
728 * The number of bytes written.
729 * Failure: An NTSTATUS error code describing the error.
731 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
732 PIO_APC_ROUTINE apc, void* apc_user,
733 PIO_STATUS_BLOCK io_status,
734 const void* buffer, ULONG length,
735 PLARGE_INTEGER offset, PULONG key)
737 int result, unix_handle, needs_close, flags, timeout_init_done = 0;
738 struct io_timeouts timeouts;
739 NTSTATUS status;
740 ULONG total = 0;
741 enum server_fd_type type;
743 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
744 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
746 if (!io_status) return STATUS_ACCESS_VIOLATION;
748 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
749 &needs_close, &type, &flags );
750 if (status) return status;
752 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
754 /* async I/O doesn't make sense on regular files */
755 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
757 if (errno != EINTR)
759 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
760 else status = FILE_GetNtStatus();
761 goto done;
765 if (!(flags & FD_FLAG_OVERLAPPED)) /* update file pointer position */
766 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
768 total = result;
769 status = STATUS_SUCCESS;
770 goto done;
773 for (;;)
775 if ((result = write( unix_handle, (const char *)buffer + total, length - total )) >= 0)
777 total += result;
778 if (total == length)
780 status = STATUS_SUCCESS;
781 goto done;
784 else
786 if (errno == EINTR) continue;
787 if (errno != EAGAIN)
789 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
790 else status = FILE_GetNtStatus();
791 goto done;
795 if (flags & FD_FLAG_OVERLAPPED)
797 async_fileio *fileio;
799 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
801 status = STATUS_NO_MEMORY;
802 goto done;
804 fileio->handle = hFile;
805 fileio->already = total;
806 fileio->count = length;
807 fileio->apc = apc;
808 fileio->apc_user = apc_user;
809 fileio->buffer = (void*)buffer;
810 fileio->event = hEvent;
811 status = fileio_queue_async(fileio, io_status, FALSE);
812 goto done;
814 else /* synchronous write, wait for the fd to become ready */
816 struct pollfd pfd;
817 int ret, timeout;
819 if (!timeout_init_done)
821 timeout_init_done = 1;
822 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
823 goto done;
824 if (hEvent) NtResetEvent( hEvent, NULL );
826 timeout = get_next_io_timeout( &timeouts, total );
828 pfd.fd = unix_handle;
829 pfd.events = POLLIN;
831 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
833 /* return with what we got so far */
834 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
835 goto done;
837 if (ret == -1 && errno != EINTR)
839 status = FILE_GetNtStatus();
840 goto done;
842 /* will now restart the write */
846 done:
847 if (needs_close) close( unix_handle );
848 if (status == STATUS_SUCCESS)
850 io_status->u.Status = status;
851 io_status->Information = total;
852 TRACE("= SUCCESS (%u)\n", total);
853 if (hEvent) NtSetEvent( hEvent, NULL );
854 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
855 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
857 else
859 TRACE("= 0x%08x\n", status);
860 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
862 return status;
865 /**************************************************************************
866 * NtDeviceIoControlFile [NTDLL.@]
867 * ZwDeviceIoControlFile [NTDLL.@]
869 * Perform an I/O control operation on an open file handle.
871 * PARAMS
872 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
873 * event [I] Event to signal upon completion (or NULL)
874 * apc [I] Callback to call upon completion (or NULL)
875 * apc_context [I] Context for ApcRoutine (or NULL)
876 * io [O] Receives information about the operation on return
877 * code [I] Control code for the operation to perform
878 * in_buffer [I] Source for any input data required (or NULL)
879 * in_size [I] Size of InputBuffer
880 * out_buffer [O] Source for any output data returned (or NULL)
881 * out_size [I] Size of OutputBuffer
883 * RETURNS
884 * Success: 0. IoStatusBlock is updated.
885 * Failure: An NTSTATUS error code describing the error.
887 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
888 PIO_APC_ROUTINE apc, PVOID apc_context,
889 PIO_STATUS_BLOCK io, ULONG code,
890 PVOID in_buffer, ULONG in_size,
891 PVOID out_buffer, ULONG out_size)
893 ULONG device = (code >> 16);
895 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
896 handle, event, apc, apc_context, io, code,
897 in_buffer, in_size, out_buffer, out_size);
899 switch(device)
901 case FILE_DEVICE_DISK:
902 case FILE_DEVICE_CD_ROM:
903 case FILE_DEVICE_DVD:
904 case FILE_DEVICE_CONTROLLER:
905 case FILE_DEVICE_MASS_STORAGE:
906 io->u.Status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
907 in_buffer, in_size, out_buffer, out_size);
908 break;
909 case FILE_DEVICE_SERIAL_PORT:
910 io->u.Status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
911 in_buffer, in_size, out_buffer, out_size);
912 break;
913 case FILE_DEVICE_TAPE:
914 io->u.Status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
915 in_buffer, in_size, out_buffer, out_size);
916 break;
917 default:
918 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
919 code, device, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
920 io->u.Status = STATUS_NOT_SUPPORTED;
921 break;
923 return io->u.Status;
926 /***********************************************************************
927 * pipe_completion_wait (Internal)
929 static void CALLBACK pipe_completion_wait(void *arg, PIO_STATUS_BLOCK iosb, ULONG status)
931 TRACE("for %p, status=%08x\n", iosb, status);
932 iosb->u.Status = status;
935 /**************************************************************************
936 * NtFsControlFile [NTDLL.@]
937 * ZwFsControlFile [NTDLL.@]
939 * Perform a file system control operation on an open file handle.
941 * PARAMS
942 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
943 * event [I] Event to signal upon completion (or NULL)
944 * apc [I] Callback to call upon completion (or NULL)
945 * apc_context [I] Context for ApcRoutine (or NULL)
946 * io [O] Receives information about the operation on return
947 * code [I] Control code for the operation to perform
948 * in_buffer [I] Source for any input data required (or NULL)
949 * in_size [I] Size of InputBuffer
950 * out_buffer [O] Source for any output data returned (or NULL)
951 * out_size [I] Size of OutputBuffer
953 * RETURNS
954 * Success: 0. IoStatusBlock is updated.
955 * Failure: An NTSTATUS error code describing the error.
957 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
958 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
959 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
961 NTSTATUS status;
963 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
964 handle, event, apc, apc_context, io, code,
965 in_buffer, in_size, out_buffer, out_size);
967 if (!io) return STATUS_INVALID_PARAMETER;
969 switch(code)
971 case FSCTL_DISMOUNT_VOLUME:
972 status = DIR_unmount_device( handle );
973 break;
975 case FSCTL_PIPE_LISTEN:
977 HANDLE internal_event = 0;
979 if(!event && !apc)
981 status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
982 if (status != STATUS_SUCCESS) break;
984 SERVER_START_REQ(connect_named_pipe)
986 req->handle = handle;
987 req->async.callback = pipe_completion_wait;
988 req->async.iosb = io;
989 req->async.arg = NULL;
990 req->async.apc = apc;
991 req->async.apc_arg = apc_context;
992 req->async.event = event ? event : internal_event;
993 status = wine_server_call(req);
995 SERVER_END_REQ;
997 if (internal_event && status == STATUS_PENDING)
999 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
1000 status = io->u.Status;
1002 if (internal_event) NtClose(internal_event);
1004 break;
1006 case FSCTL_PIPE_WAIT:
1008 HANDLE internal_event = 0;
1009 FILE_PIPE_WAIT_FOR_BUFFER *buff = in_buffer;
1011 if(!event && !apc)
1013 status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
1014 if (status != STATUS_SUCCESS) break;
1016 SERVER_START_REQ(wait_named_pipe)
1018 req->handle = handle;
1019 req->timeout = buff->TimeoutSpecified ? buff->Timeout.QuadPart / -10000L
1020 : NMPWAIT_USE_DEFAULT_WAIT;
1021 req->async.callback = pipe_completion_wait;
1022 req->async.iosb = io;
1023 req->async.arg = NULL;
1024 req->async.apc = apc;
1025 req->async.apc_arg = apc_context;
1026 req->async.event = event ? event : internal_event;
1027 wine_server_add_data( req, buff->Name, buff->NameLength );
1028 status = wine_server_call( req );
1030 SERVER_END_REQ;
1032 if (internal_event && status == STATUS_PENDING)
1034 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
1035 status = io->u.Status;
1037 if (internal_event) NtClose(internal_event);
1039 break;
1041 case FSCTL_PIPE_PEEK:
1043 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1044 int avail = 0, fd, needs_close, flags;
1046 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1048 status = STATUS_INFO_LENGTH_MISMATCH;
1049 break;
1052 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, &flags )))
1053 break;
1055 if (flags & FD_FLAG_RECV_SHUTDOWN)
1057 if (needs_close) close( fd );
1058 status = STATUS_PIPE_DISCONNECTED;
1059 break;
1062 #ifdef FIONREAD
1063 if (ioctl( fd, FIONREAD, &avail ) != 0)
1065 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1066 if (needs_close) close( fd );
1067 status = FILE_GetNtStatus();
1068 break;
1070 #endif
1071 if (!avail) /* check for closed pipe */
1073 struct pollfd pollfd;
1074 int ret;
1076 pollfd.fd = fd;
1077 pollfd.events = POLLIN;
1078 pollfd.revents = 0;
1079 ret = poll( &pollfd, 1, 0 );
1080 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1082 if (needs_close) close( fd );
1083 status = STATUS_PIPE_BROKEN;
1084 break;
1087 buffer->NamedPipeState = 0; /* FIXME */
1088 buffer->ReadDataAvailable = avail;
1089 buffer->NumberOfMessages = 0; /* FIXME */
1090 buffer->MessageLength = 0; /* FIXME */
1091 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1092 status = STATUS_SUCCESS;
1093 if (avail)
1095 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1096 if (data_size)
1098 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1099 if (res >= 0) io->Information += res;
1102 if (needs_close) close( fd );
1104 break;
1106 case FSCTL_PIPE_DISCONNECT:
1107 SERVER_START_REQ(disconnect_named_pipe)
1109 req->handle = handle;
1110 status = wine_server_call(req);
1111 if (!status)
1113 int fd = server_remove_fd_from_cache( handle );
1114 if (fd != -1) close( fd );
1117 SERVER_END_REQ;
1118 break;
1120 case FSCTL_LOCK_VOLUME:
1121 case FSCTL_UNLOCK_VOLUME:
1122 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1123 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1124 status = STATUS_SUCCESS;
1125 break;
1127 default:
1128 FIXME("Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1129 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1130 status = STATUS_NOT_SUPPORTED;
1131 break;
1134 if (status != STATUS_PENDING) io->u.Status = status;
1135 return status;
1138 /******************************************************************************
1139 * NtSetVolumeInformationFile [NTDLL.@]
1140 * ZwSetVolumeInformationFile [NTDLL.@]
1142 * Set volume information for an open file handle.
1144 * PARAMS
1145 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1146 * IoStatusBlock [O] Receives information about the operation on return
1147 * FsInformation [I] Source for volume information
1148 * Length [I] Size of FsInformation
1149 * FsInformationClass [I] Type of volume information to set
1151 * RETURNS
1152 * Success: 0. IoStatusBlock is updated.
1153 * Failure: An NTSTATUS error code describing the error.
1155 NTSTATUS WINAPI NtSetVolumeInformationFile(
1156 IN HANDLE FileHandle,
1157 PIO_STATUS_BLOCK IoStatusBlock,
1158 PVOID FsInformation,
1159 ULONG Length,
1160 FS_INFORMATION_CLASS FsInformationClass)
1162 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1163 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1164 return 0;
1167 /******************************************************************************
1168 * NtQueryInformationFile [NTDLL.@]
1169 * ZwQueryInformationFile [NTDLL.@]
1171 * Get information about an open file handle.
1173 * PARAMS
1174 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1175 * io [O] Receives information about the operation on return
1176 * ptr [O] Destination for file information
1177 * len [I] Size of FileInformation
1178 * class [I] Type of file information to get
1180 * RETURNS
1181 * Success: 0. IoStatusBlock and FileInformation are updated.
1182 * Failure: An NTSTATUS error code describing the error.
1184 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1185 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1187 static const size_t info_sizes[] =
1190 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1191 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1192 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1193 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1194 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1195 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1196 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1197 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1198 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1199 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1200 0, /* FileLinkInformation */
1201 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1202 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1203 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1204 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1205 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1206 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1207 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1208 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1209 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1210 0, /* FileAlternateNameInformation */
1211 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1212 0, /* FilePipeInformation */
1213 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1214 0, /* FilePipeRemoteInformation */
1215 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1216 0, /* FileMailslotSetInformation */
1217 0, /* FileCompressionInformation */
1218 0, /* FileObjectIdInformation */
1219 0, /* FileCompletionInformation */
1220 0, /* FileMoveClusterInformation */
1221 0, /* FileQuotaInformation */
1222 0, /* FileReparsePointInformation */
1223 0, /* FileNetworkOpenInformation */
1224 0, /* FileAttributeTagInformation */
1225 0 /* FileTrackingInformation */
1228 struct stat st;
1229 int fd, needs_close = FALSE;
1231 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1233 io->Information = 0;
1235 if (class <= 0 || class >= FileMaximumInformation)
1236 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1237 if (!info_sizes[class])
1239 FIXME("Unsupported class (%d)\n", class);
1240 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1242 if (len < info_sizes[class])
1243 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1245 if (class != FilePipeLocalInformation)
1247 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1248 return io->u.Status;
1251 switch (class)
1253 case FileBasicInformation:
1255 FILE_BASIC_INFORMATION *info = ptr;
1257 if (fstat( fd, &st ) == -1)
1258 io->u.Status = FILE_GetNtStatus();
1259 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1260 io->u.Status = STATUS_INVALID_INFO_CLASS;
1261 else
1263 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1264 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1265 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1266 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1267 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1268 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1269 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1270 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1273 break;
1274 case FileStandardInformation:
1276 FILE_STANDARD_INFORMATION *info = ptr;
1278 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1279 else
1281 if ((info->Directory = S_ISDIR(st.st_mode)))
1283 info->AllocationSize.QuadPart = 0;
1284 info->EndOfFile.QuadPart = 0;
1285 info->NumberOfLinks = 1;
1286 info->DeletePending = FALSE;
1288 else
1290 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1291 info->EndOfFile.QuadPart = st.st_size;
1292 info->NumberOfLinks = st.st_nlink;
1293 info->DeletePending = FALSE; /* FIXME */
1297 break;
1298 case FilePositionInformation:
1300 FILE_POSITION_INFORMATION *info = ptr;
1301 off_t res = lseek( fd, 0, SEEK_CUR );
1302 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1303 else info->CurrentByteOffset.QuadPart = res;
1305 break;
1306 case FileInternalInformation:
1308 FILE_INTERNAL_INFORMATION *info = ptr;
1310 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1311 else info->IndexNumber.QuadPart = st.st_ino;
1313 break;
1314 case FileEaInformation:
1316 FILE_EA_INFORMATION *info = ptr;
1317 info->EaSize = 0;
1319 break;
1320 case FileEndOfFileInformation:
1322 FILE_END_OF_FILE_INFORMATION *info = ptr;
1324 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1325 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1327 break;
1328 case FileAllInformation:
1330 FILE_ALL_INFORMATION *info = ptr;
1332 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1333 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1334 io->u.Status = STATUS_INVALID_INFO_CLASS;
1335 else
1337 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1339 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1340 info->StandardInformation.AllocationSize.QuadPart = 0;
1341 info->StandardInformation.EndOfFile.QuadPart = 0;
1342 info->StandardInformation.NumberOfLinks = 1;
1343 info->StandardInformation.DeletePending = FALSE;
1345 else
1347 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1348 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1349 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1350 info->StandardInformation.NumberOfLinks = st.st_nlink;
1351 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1353 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1354 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1355 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1356 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1357 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1358 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1359 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1360 info->EaInformation.EaSize = 0;
1361 info->AccessInformation.AccessFlags = 0; /* FIXME */
1362 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1363 info->ModeInformation.Mode = 0; /* FIXME */
1364 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1365 info->NameInformation.FileNameLength = 0;
1366 io->Information = sizeof(*info) - sizeof(WCHAR);
1369 break;
1370 case FileMailslotQueryInformation:
1372 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1374 SERVER_START_REQ( set_mailslot_info )
1376 req->handle = hFile;
1377 req->flags = 0;
1378 io->u.Status = wine_server_call( req );
1379 if( io->u.Status == STATUS_SUCCESS )
1381 info->MaximumMessageSize = reply->max_msgsize;
1382 info->MailslotQuota = 0;
1383 info->NextMessageSize = 0;
1384 info->MessagesAvailable = 0;
1385 info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1388 SERVER_END_REQ;
1389 if (!io->u.Status)
1391 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1392 char *tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size );
1393 if (tmpbuf)
1395 int fd, needs_close;
1396 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1398 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1399 info->MessagesAvailable = (res > 0);
1400 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1401 if (needs_close) close( fd );
1403 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1407 break;
1408 case FilePipeLocalInformation:
1410 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1412 SERVER_START_REQ( get_named_pipe_info )
1414 req->handle = hFile;
1415 if (!(io->u.Status = wine_server_call( req )))
1417 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1418 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1419 pli->NamedPipeConfiguration = 0; /* FIXME */
1420 pli->MaximumInstances = reply->maxinstances;
1421 pli->CurrentInstances = reply->instances;
1422 pli->InboundQuota = reply->insize;
1423 pli->ReadDataAvailable = 0; /* FIXME */
1424 pli->OutboundQuota = reply->outsize;
1425 pli->WriteQuotaAvailable = 0; /* FIXME */
1426 pli->NamedPipeState = 0; /* FIXME */
1427 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1428 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1431 SERVER_END_REQ;
1433 break;
1434 default:
1435 FIXME("Unsupported class (%d)\n", class);
1436 io->u.Status = STATUS_NOT_IMPLEMENTED;
1437 break;
1439 if (needs_close) close( fd );
1440 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1441 return io->u.Status;
1444 /******************************************************************************
1445 * NtSetInformationFile [NTDLL.@]
1446 * ZwSetInformationFile [NTDLL.@]
1448 * Set information about an open file handle.
1450 * PARAMS
1451 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1452 * io [O] Receives information about the operation on return
1453 * ptr [I] Source for file information
1454 * len [I] Size of FileInformation
1455 * class [I] Type of file information to set
1457 * RETURNS
1458 * Success: 0. io is updated.
1459 * Failure: An NTSTATUS error code describing the error.
1461 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1462 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1464 int fd, needs_close;
1466 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1468 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1469 return io->u.Status;
1471 io->u.Status = STATUS_SUCCESS;
1472 switch (class)
1474 case FileBasicInformation:
1475 if (len >= sizeof(FILE_BASIC_INFORMATION))
1477 struct stat st;
1478 const FILE_BASIC_INFORMATION *info = ptr;
1480 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1482 ULONGLONG sec, nsec;
1483 struct timeval tv[2];
1485 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1488 tv[0].tv_sec = tv[0].tv_usec = 0;
1489 tv[1].tv_sec = tv[1].tv_usec = 0;
1490 if (!fstat( fd, &st ))
1492 tv[0].tv_sec = st.st_atime;
1493 tv[1].tv_sec = st.st_mtime;
1496 if (info->LastAccessTime.QuadPart)
1498 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1499 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1500 tv[0].tv_usec = (UINT)nsec / 10;
1502 if (info->LastWriteTime.QuadPart)
1504 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1505 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1506 tv[1].tv_usec = (UINT)nsec / 10;
1508 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1511 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1513 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1514 else
1516 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1518 if (S_ISDIR( st.st_mode))
1519 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1520 else
1521 st.st_mode &= ~0222; /* clear write permission bits */
1523 else
1525 /* add write permission only where we already have read permission */
1526 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1528 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1532 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1533 break;
1535 case FilePositionInformation:
1536 if (len >= sizeof(FILE_POSITION_INFORMATION))
1538 const FILE_POSITION_INFORMATION *info = ptr;
1540 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1541 io->u.Status = FILE_GetNtStatus();
1543 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1544 break;
1546 case FileEndOfFileInformation:
1547 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1549 struct stat st;
1550 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1552 /* first try normal truncate */
1553 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1555 /* now check for the need to extend the file */
1556 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1558 static const char zero;
1560 /* extend the file one byte beyond the requested size and then truncate it */
1561 /* this should work around ftruncate implementations that can't extend files */
1562 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1563 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1565 io->u.Status = FILE_GetNtStatus();
1567 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1568 break;
1570 case FileMailslotSetInformation:
1572 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1574 SERVER_START_REQ( set_mailslot_info )
1576 req->handle = handle;
1577 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1578 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1579 io->u.Status = wine_server_call( req );
1581 SERVER_END_REQ;
1583 break;
1585 default:
1586 FIXME("Unsupported class (%d)\n", class);
1587 io->u.Status = STATUS_NOT_IMPLEMENTED;
1588 break;
1590 if (needs_close) close( fd );
1591 io->Information = 0;
1592 return io->u.Status;
1596 /******************************************************************************
1597 * NtQueryFullAttributesFile (NTDLL.@)
1599 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1600 FILE_NETWORK_OPEN_INFORMATION *info )
1602 ANSI_STRING unix_name;
1603 NTSTATUS status;
1605 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1606 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1608 struct stat st;
1610 if (stat( unix_name.Buffer, &st ) == -1)
1611 status = FILE_GetNtStatus();
1612 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1613 status = STATUS_INVALID_INFO_CLASS;
1614 else
1616 if (S_ISDIR(st.st_mode))
1618 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1619 info->AllocationSize.QuadPart = 0;
1620 info->EndOfFile.QuadPart = 0;
1622 else
1624 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1625 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1626 info->EndOfFile.QuadPart = st.st_size;
1628 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1629 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1630 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1631 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1632 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1633 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1634 if (DIR_is_hidden_file( attr->ObjectName ))
1635 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1637 RtlFreeAnsiString( &unix_name );
1639 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1640 return status;
1644 /******************************************************************************
1645 * NtQueryAttributesFile (NTDLL.@)
1646 * ZwQueryAttributesFile (NTDLL.@)
1648 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1650 FILE_NETWORK_OPEN_INFORMATION full_info;
1651 NTSTATUS status;
1653 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1655 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1656 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1657 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1658 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1659 info->FileAttributes = full_info.FileAttributes;
1661 return status;
1665 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1666 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1667 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1668 size_t fstypesize, unsigned int flags )
1670 if (!strncmp("cd9660", fstypename, fstypesize) ||
1671 !strncmp("udf", fstypename, fstypesize))
1673 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1674 /* Don't assume read-only, let the mount options set it below */
1675 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1677 else if (!strncmp("nfs", fstypename, fstypesize) ||
1678 !strncmp("nwfs", fstypename, fstypesize) ||
1679 !strncmp("smbfs", fstypename, fstypesize) ||
1680 !strncmp("afpfs", fstypename, fstypesize))
1682 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1683 info->Characteristics |= FILE_REMOTE_DEVICE;
1685 else if (!strncmp("procfs", fstypename, fstypesize))
1686 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1687 else
1688 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1690 if (flags & MNT_RDONLY)
1691 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1693 if (!(flags & MNT_LOCAL))
1695 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1696 info->Characteristics |= FILE_REMOTE_DEVICE;
1699 #endif
1701 /******************************************************************************
1702 * get_device_info
1704 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1706 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1708 struct stat st;
1710 info->Characteristics = 0;
1711 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1712 if (S_ISCHR( st.st_mode ))
1714 info->DeviceType = FILE_DEVICE_UNKNOWN;
1715 #ifdef linux
1716 switch(major(st.st_rdev))
1718 case MEM_MAJOR:
1719 info->DeviceType = FILE_DEVICE_NULL;
1720 break;
1721 case TTY_MAJOR:
1722 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1723 break;
1724 case LP_MAJOR:
1725 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1726 break;
1727 case SCSI_TAPE_MAJOR:
1728 info->DeviceType = FILE_DEVICE_TAPE;
1729 break;
1731 #endif
1733 else if (S_ISBLK( st.st_mode ))
1735 info->DeviceType = FILE_DEVICE_DISK;
1737 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1739 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1741 else /* regular file or directory */
1743 #if defined(linux) && defined(HAVE_FSTATFS)
1744 struct statfs stfs;
1746 /* check for floppy disk */
1747 if (major(st.st_dev) == FLOPPY_MAJOR)
1748 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1750 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1751 switch (stfs.f_type)
1753 case 0x9660: /* iso9660 */
1754 case 0x9fa1: /* supermount */
1755 case 0x15013346: /* udf */
1756 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1757 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1758 break;
1759 case 0x6969: /* nfs */
1760 case 0x517B: /* smbfs */
1761 case 0x564c: /* ncpfs */
1762 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1763 info->Characteristics |= FILE_REMOTE_DEVICE;
1764 break;
1765 case 0x01021994: /* tmpfs */
1766 case 0x28cd3d45: /* cramfs */
1767 case 0x1373: /* devfs */
1768 case 0x9fa0: /* procfs */
1769 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1770 break;
1771 default:
1772 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1773 break;
1775 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1776 struct statfs stfs;
1778 if (fstatfs( fd, &stfs ) < 0)
1779 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1780 else
1781 get_device_info_fstatfs( info, stfs.f_fstypename,
1782 sizeof(stfs.f_fstypename), stfs.f_flags );
1783 #elif defined(__NetBSD__)
1784 struct statvfs stfs;
1786 if (fstatvfs( fd, &stfs) < 0)
1787 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1788 else
1789 get_device_info_fstatfs( info, stfs.f_fstypename,
1790 sizeof(stfs.f_fstypename), stfs.f_flag );
1791 #elif defined(sun)
1792 /* Use dkio to work out device types */
1794 # include <sys/dkio.h>
1795 # include <sys/vtoc.h>
1796 struct dk_cinfo dkinf;
1797 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1798 if(retval==-1){
1799 WARN("Unable to get disk device type information - assuming a disk like device\n");
1800 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1802 switch (dkinf.dki_ctype)
1804 case DKC_CDROM:
1805 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1806 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1807 break;
1808 case DKC_NCRFLOPPY:
1809 case DKC_SMSFLOPPY:
1810 case DKC_INTEL82072:
1811 case DKC_INTEL82077:
1812 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1813 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1814 break;
1815 case DKC_MD:
1816 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1817 break;
1818 default:
1819 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1822 #else
1823 static int warned;
1824 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1825 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1826 #endif
1827 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1829 return STATUS_SUCCESS;
1833 /******************************************************************************
1834 * NtQueryVolumeInformationFile [NTDLL.@]
1835 * ZwQueryVolumeInformationFile [NTDLL.@]
1837 * Get volume information for an open file handle.
1839 * PARAMS
1840 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1841 * io [O] Receives information about the operation on return
1842 * buffer [O] Destination for volume information
1843 * length [I] Size of FsInformation
1844 * info_class [I] Type of volume information to set
1846 * RETURNS
1847 * Success: 0. io and buffer are updated.
1848 * Failure: An NTSTATUS error code describing the error.
1850 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1851 PVOID buffer, ULONG length,
1852 FS_INFORMATION_CLASS info_class )
1854 int fd, needs_close;
1855 struct stat st;
1857 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1858 return io->u.Status;
1860 io->u.Status = STATUS_NOT_IMPLEMENTED;
1861 io->Information = 0;
1863 switch( info_class )
1865 case FileFsVolumeInformation:
1866 FIXME( "%p: volume info not supported\n", handle );
1867 break;
1868 case FileFsLabelInformation:
1869 FIXME( "%p: label info not supported\n", handle );
1870 break;
1871 case FileFsSizeInformation:
1872 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1873 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1874 else
1876 FILE_FS_SIZE_INFORMATION *info = buffer;
1878 if (fstat( fd, &st ) < 0)
1880 io->u.Status = FILE_GetNtStatus();
1881 break;
1883 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1885 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1887 else
1889 /* Linux's fstatvfs is buggy */
1890 #if !defined(linux) || !defined(HAVE_FSTATFS)
1891 struct statvfs stfs;
1893 if (fstatvfs( fd, &stfs ) < 0)
1895 io->u.Status = FILE_GetNtStatus();
1896 break;
1898 info->BytesPerSector = stfs.f_frsize;
1899 #else
1900 struct statfs stfs;
1901 if (fstatfs( fd, &stfs ) < 0)
1903 io->u.Status = FILE_GetNtStatus();
1904 break;
1906 info->BytesPerSector = stfs.f_bsize;
1907 #endif
1908 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1909 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1910 info->SectorsPerAllocationUnit = 1;
1911 io->Information = sizeof(*info);
1912 io->u.Status = STATUS_SUCCESS;
1915 break;
1916 case FileFsDeviceInformation:
1917 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1918 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1919 else
1921 FILE_FS_DEVICE_INFORMATION *info = buffer;
1923 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
1924 io->Information = sizeof(*info);
1926 break;
1927 case FileFsAttributeInformation:
1928 FIXME( "%p: attribute info not supported\n", handle );
1929 break;
1930 case FileFsControlInformation:
1931 FIXME( "%p: control info not supported\n", handle );
1932 break;
1933 case FileFsFullSizeInformation:
1934 FIXME( "%p: full size info not supported\n", handle );
1935 break;
1936 case FileFsObjectIdInformation:
1937 FIXME( "%p: object id info not supported\n", handle );
1938 break;
1939 case FileFsMaximumInformation:
1940 FIXME( "%p: maximum info not supported\n", handle );
1941 break;
1942 default:
1943 io->u.Status = STATUS_INVALID_PARAMETER;
1944 break;
1946 if (needs_close) close( fd );
1947 return io->u.Status;
1951 /******************************************************************
1952 * NtFlushBuffersFile (NTDLL.@)
1954 * Flush any buffered data on an open file handle.
1956 * PARAMS
1957 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1958 * IoStatusBlock [O] Receives information about the operation on return
1960 * RETURNS
1961 * Success: 0. IoStatusBlock is updated.
1962 * Failure: An NTSTATUS error code describing the error.
1964 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1966 NTSTATUS ret;
1967 HANDLE hEvent = NULL;
1969 SERVER_START_REQ( flush_file )
1971 req->handle = hFile;
1972 ret = wine_server_call( req );
1973 hEvent = reply->event;
1975 SERVER_END_REQ;
1976 if (!ret && hEvent)
1978 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1979 NtClose( hEvent );
1981 return ret;
1984 /******************************************************************
1985 * NtLockFile (NTDLL.@)
1989 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1990 PIO_APC_ROUTINE apc, void* apc_user,
1991 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1992 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1993 BOOLEAN exclusive )
1995 NTSTATUS ret;
1996 HANDLE handle;
1997 BOOLEAN async;
1999 if (apc || io_status || key)
2001 FIXME("Unimplemented yet parameter\n");
2002 return STATUS_NOT_IMPLEMENTED;
2005 for (;;)
2007 SERVER_START_REQ( lock_file )
2009 req->handle = hFile;
2010 req->offset_low = offset->u.LowPart;
2011 req->offset_high = offset->u.HighPart;
2012 req->count_low = count->u.LowPart;
2013 req->count_high = count->u.HighPart;
2014 req->shared = !exclusive;
2015 req->wait = !dont_wait;
2016 ret = wine_server_call( req );
2017 handle = reply->handle;
2018 async = reply->overlapped;
2020 SERVER_END_REQ;
2021 if (ret != STATUS_PENDING)
2023 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2024 return ret;
2027 if (async)
2029 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2030 if (handle) NtClose( handle );
2031 return STATUS_PENDING;
2033 if (handle)
2035 NtWaitForSingleObject( handle, FALSE, NULL );
2036 NtClose( handle );
2038 else
2040 LARGE_INTEGER time;
2042 /* Unix lock conflict, sleep a bit and retry */
2043 time.QuadPart = 100 * (ULONGLONG)10000;
2044 time.QuadPart = -time.QuadPart;
2045 NtDelayExecution( FALSE, &time );
2051 /******************************************************************
2052 * NtUnlockFile (NTDLL.@)
2056 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2057 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2058 PULONG key )
2060 NTSTATUS status;
2062 TRACE( "%p %x%08x %x%08x\n",
2063 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2065 if (io_status || key)
2067 FIXME("Unimplemented yet parameter\n");
2068 return STATUS_NOT_IMPLEMENTED;
2071 SERVER_START_REQ( unlock_file )
2073 req->handle = hFile;
2074 req->offset_low = offset->u.LowPart;
2075 req->offset_high = offset->u.HighPart;
2076 req->count_low = count->u.LowPart;
2077 req->count_high = count->u.HighPart;
2078 status = wine_server_call( req );
2080 SERVER_END_REQ;
2081 return status;
2084 /******************************************************************
2085 * NtCreateNamedPipeFile (NTDLL.@)
2089 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2090 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2091 ULONG sharing, ULONG dispo, ULONG options,
2092 ULONG pipe_type, ULONG read_mode,
2093 ULONG completion_mode, ULONG max_inst,
2094 ULONG inbound_quota, ULONG outbound_quota,
2095 PLARGE_INTEGER timeout)
2097 NTSTATUS status;
2099 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2100 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2101 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2102 outbound_quota, timeout);
2104 /* assume we only get relative timeout, and storable in a DWORD as ms */
2105 if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
2106 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2108 SERVER_START_REQ( create_named_pipe )
2110 req->access = access;
2111 req->attributes = (attr) ? attr->Attributes : 0;
2112 req->rootdir = attr ? attr->RootDirectory : 0;
2113 req->options = options;
2114 req->flags =
2115 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2116 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2117 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2118 req->maxinstances = max_inst;
2119 req->outsize = outbound_quota;
2120 req->insize = inbound_quota;
2121 req->timeout = timeout->QuadPart / -10000;
2122 wine_server_add_data( req, attr->ObjectName->Buffer,
2123 attr->ObjectName->Length );
2124 status = wine_server_call( req );
2125 if (!status) *handle = reply->handle;
2127 SERVER_END_REQ;
2128 return status;
2131 /******************************************************************
2132 * NtDeleteFile (NTDLL.@)
2136 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2138 NTSTATUS status;
2139 HANDLE hFile;
2140 IO_STATUS_BLOCK io;
2142 TRACE("%p\n", ObjectAttributes);
2143 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2144 ObjectAttributes, &io, NULL, 0,
2145 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2146 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2147 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2148 return status;
2151 /******************************************************************
2152 * NtCancelIoFile (NTDLL.@)
2156 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2158 LARGE_INTEGER timeout;
2160 TRACE("%p %p\n", hFile, io_status );
2162 SERVER_START_REQ( cancel_async )
2164 req->handle = hFile;
2165 wine_server_call( req );
2167 SERVER_END_REQ;
2168 /* Let some APC be run, so that we can run the remaining APCs on hFile
2169 * either the cancelation of the pending one, but also the execution
2170 * of the queued APC, but not yet run. This is needed to ensure proper
2171 * clean-up of allocated data.
2173 timeout.u.LowPart = timeout.u.HighPart = 0;
2174 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2177 /******************************************************************************
2178 * NtCreateMailslotFile [NTDLL.@]
2179 * ZwCreateMailslotFile [NTDLL.@]
2181 * PARAMS
2182 * pHandle [O] pointer to receive the handle created
2183 * DesiredAccess [I] access mode (read, write, etc)
2184 * ObjectAttributes [I] fully qualified NT path of the mailslot
2185 * IoStatusBlock [O] receives completion status and other info
2186 * CreateOptions [I]
2187 * MailslotQuota [I]
2188 * MaxMessageSize [I]
2189 * TimeOut [I]
2191 * RETURNS
2192 * An NT status code
2194 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2195 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2196 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2197 PLARGE_INTEGER TimeOut)
2199 LARGE_INTEGER timeout;
2200 NTSTATUS ret;
2202 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2203 pHandle, DesiredAccess, attr, IoStatusBlock,
2204 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2206 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2207 if (!attr) return STATUS_INVALID_PARAMETER;
2208 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2211 * For a NULL TimeOut pointer set the default timeout value
2213 if (!TimeOut)
2214 timeout.QuadPart = -1;
2215 else
2216 timeout.QuadPart = TimeOut->QuadPart;
2218 SERVER_START_REQ( create_mailslot )
2220 req->access = DesiredAccess;
2221 req->attributes = attr->Attributes;
2222 req->rootdir = attr->RootDirectory;
2223 req->max_msgsize = MaxMessageSize;
2224 req->read_timeout = (timeout.QuadPart <= 0) ? timeout.QuadPart / -10000 : -1;
2225 wine_server_add_data( req, attr->ObjectName->Buffer,
2226 attr->ObjectName->Length );
2227 ret = wine_server_call( req );
2228 if( ret == STATUS_SUCCESS )
2229 *pHandle = reply->handle;
2231 SERVER_END_REQ;
2233 return ret;