server: Always return STATUS_PENDING when an async I/O operation has been queued.
[wine/dibdrv.git] / dlls / ntdll / file.c
blob6873e0e77aec1f4ff30e44d36d9e577f431a0e57
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"
83 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
85 mode_t FILE_umask = 0;
87 #define SECSPERDAY 86400
88 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
90 /**************************************************************************
91 * NtOpenFile [NTDLL.@]
92 * ZwOpenFile [NTDLL.@]
94 * Open a file.
96 * PARAMS
97 * handle [O] Variable that receives the file handle on return
98 * access [I] Access desired by the caller to the file
99 * attr [I] Structure describing the file to be opened
100 * io [O] Receives details about the result of the operation
101 * sharing [I] Type of shared access the caller requires
102 * options [I] Options for the file open
104 * RETURNS
105 * Success: 0. FileHandle and IoStatusBlock are updated.
106 * Failure: An NTSTATUS error code describing the error.
108 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
109 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
110 ULONG sharing, ULONG options )
112 return NtCreateFile( handle, access, attr, io, NULL, 0,
113 sharing, FILE_OPEN, options, NULL, 0 );
116 /**************************************************************************
117 * NtCreateFile [NTDLL.@]
118 * ZwCreateFile [NTDLL.@]
120 * Either create a new file or directory, or open an existing file, device,
121 * directory or volume.
123 * PARAMS
124 * handle [O] Points to a variable which receives the file handle on return
125 * access [I] Desired access to the file
126 * attr [I] Structure describing the file
127 * io [O] Receives information about the operation on return
128 * alloc_size [I] Initial size of the file in bytes
129 * attributes [I] Attributes to create the file with
130 * sharing [I] Type of shared access the caller would like to the file
131 * disposition [I] Specifies what to do, depending on whether the file already exists
132 * options [I] Options for creating a new file
133 * ea_buffer [I] Pointer to an extended attributes buffer
134 * ea_length [I] Length of ea_buffer
136 * RETURNS
137 * Success: 0. handle and io are updated.
138 * Failure: An NTSTATUS error code describing the error.
140 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
141 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
142 ULONG attributes, ULONG sharing, ULONG disposition,
143 ULONG options, PVOID ea_buffer, ULONG ea_length )
145 ANSI_STRING unix_name;
146 int created = FALSE;
148 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p\n"
149 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
150 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
151 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
152 attributes, sharing, disposition, options, ea_buffer, ea_length );
154 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
156 if (alloc_size) FIXME( "alloc_size not supported\n" );
158 if (attr->RootDirectory)
160 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
161 return STATUS_OBJECT_NAME_NOT_FOUND;
164 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
165 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
167 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
169 SERVER_START_REQ( open_file_object )
171 req->access = access;
172 req->attributes = attr->Attributes;
173 req->rootdir = attr->RootDirectory;
174 req->sharing = sharing;
175 req->options = options;
176 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
177 io->u.Status = wine_server_call( req );
178 *handle = reply->handle;
180 SERVER_END_REQ;
181 return io->u.Status;
184 if (io->u.Status == STATUS_NO_SUCH_FILE &&
185 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
187 created = TRUE;
188 io->u.Status = STATUS_SUCCESS;
191 if (io->u.Status == STATUS_SUCCESS)
193 SERVER_START_REQ( create_file )
195 req->access = access;
196 req->attributes = attr->Attributes;
197 req->sharing = sharing;
198 req->create = disposition;
199 req->options = options;
200 req->attrs = attributes;
201 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
202 io->u.Status = wine_server_call( req );
203 *handle = reply->handle;
205 SERVER_END_REQ;
206 RtlFreeAnsiString( &unix_name );
208 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
210 if (io->u.Status == STATUS_SUCCESS)
212 if (created) io->Information = FILE_CREATED;
213 else switch(disposition)
215 case FILE_SUPERSEDE:
216 io->Information = FILE_SUPERSEDED;
217 break;
218 case FILE_CREATE:
219 io->Information = FILE_CREATED;
220 break;
221 case FILE_OPEN:
222 case FILE_OPEN_IF:
223 io->Information = FILE_OPENED;
224 break;
225 case FILE_OVERWRITE:
226 case FILE_OVERWRITE_IF:
227 io->Information = FILE_OVERWRITTEN;
228 break;
232 return io->u.Status;
235 /***********************************************************************
236 * Asynchronous file I/O *
238 static void WINAPI FILE_AsyncReadService(void*, PIO_STATUS_BLOCK, ULONG);
239 static void WINAPI FILE_AsyncWriteService(void*, PIO_STATUS_BLOCK, ULONG);
241 typedef struct async_fileio
243 HANDLE handle;
244 PIO_APC_ROUTINE apc;
245 void* apc_user;
246 char* buffer;
247 unsigned int count;
248 int queue_apc_on_error;
249 BOOL avail_mode;
250 HANDLE event;
251 } async_fileio;
253 static void fileio_terminate(async_fileio *fileio, IO_STATUS_BLOCK* iosb)
255 TRACE("data: %p\n", fileio);
257 if (fileio->apc &&
258 (iosb->u.Status == STATUS_SUCCESS || fileio->queue_apc_on_error))
259 fileio->apc( fileio->apc_user, iosb, iosb->Information );
261 RtlFreeHeap( GetProcessHeap(), 0, fileio );
265 static ULONG fileio_queue_async(async_fileio* fileio, IO_STATUS_BLOCK* iosb,
266 BOOL do_read)
268 PIO_APC_ROUTINE apc = do_read ? FILE_AsyncReadService : FILE_AsyncWriteService;
269 NTSTATUS status;
271 SERVER_START_REQ( register_async )
273 req->handle = fileio->handle;
274 req->async.callback = apc;
275 req->async.iosb = iosb;
276 req->async.arg = fileio;
277 req->async.event = fileio->event;
278 req->type = do_read ? ASYNC_TYPE_READ : ASYNC_TYPE_WRITE;
279 req->count = (fileio->count < iosb->Information) ?
280 0 : fileio->count - iosb->Information;
281 status = wine_server_call( req );
283 SERVER_END_REQ;
285 if (status != STATUS_PENDING)
287 iosb->u.Status = status;
288 (apc)( fileio, iosb, iosb->u.Status );
290 else NtCurrentTeb()->num_async_io++;
291 return status;
294 /***********************************************************************
295 * FILE_GetNtStatus(void)
297 * Retrieve the Nt Status code from errno.
298 * Try to be consistent with FILE_SetDosError().
300 NTSTATUS FILE_GetNtStatus(void)
302 int err = errno;
304 TRACE( "errno = %d\n", errno );
305 switch (err)
307 case EAGAIN: return STATUS_SHARING_VIOLATION;
308 case EBADF: return STATUS_INVALID_HANDLE;
309 case EBUSY: return STATUS_DEVICE_BUSY;
310 case ENOSPC: return STATUS_DISK_FULL;
311 case EPERM:
312 case EROFS:
313 case EACCES: return STATUS_ACCESS_DENIED;
314 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
315 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
316 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
317 case EMFILE:
318 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
319 case EINVAL: return STATUS_INVALID_PARAMETER;
320 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
321 case EPIPE: return STATUS_PIPE_BROKEN;
322 case EIO: return STATUS_DEVICE_NOT_READY;
323 #ifdef ENOMEDIUM
324 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
325 #endif
326 case ENXIO: return STATUS_NO_SUCH_DEVICE;
327 case ENOTTY:
328 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
329 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
330 case EFAULT: return STATUS_ACCESS_VIOLATION;
331 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
332 case ENOEXEC: /* ?? */
333 case EEXIST: /* ?? */
334 default:
335 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
336 return STATUS_UNSUCCESSFUL;
340 /***********************************************************************
341 * FILE_AsyncReadService (INTERNAL)
343 static void WINAPI FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
345 async_fileio *fileio = (async_fileio*)user;
346 int fd, needs_close, result;
347 int already = iosb->Information;
349 TRACE("%p %p 0x%x\n", iosb, fileio->buffer, status);
351 switch (status)
353 case STATUS_ALERTED: /* got some new data */
354 if (iosb->u.Status != STATUS_PENDING) FIXME("unexpected status %08x\n", iosb->u.Status);
355 /* check to see if the data is ready (non-blocking) */
356 if ((iosb->u.Status = server_get_unix_fd( fileio->handle, FILE_READ_DATA, &fd,
357 &needs_close, NULL, NULL )))
359 fileio_terminate(fileio, iosb);
360 break;
362 result = read(fd, &fileio->buffer[already], fileio->count - already);
363 if (needs_close) close( fd );
365 if (result < 0)
367 if (errno == EAGAIN || errno == EINTR)
369 TRACE("Deferred read %d\n", errno);
370 iosb->u.Status = STATUS_PENDING;
372 else /* check to see if the transfer is complete */
373 iosb->u.Status = FILE_GetNtStatus();
375 else if (result == 0)
377 iosb->u.Status = iosb->Information ? STATUS_SUCCESS : STATUS_END_OF_FILE;
379 else
381 iosb->Information += result;
382 if (iosb->Information >= fileio->count || fileio->avail_mode)
383 iosb->u.Status = STATUS_SUCCESS;
384 else
386 /* if we only have to read the available data, and none is available,
387 * simply cancel the request. If data was available, it has been read
388 * while in by previous call (NtDelayExecution)
390 iosb->u.Status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
393 TRACE("read %d more bytes %ld/%d so far (%s)\n",
394 result, iosb->Information, fileio->count,
395 (iosb->u.Status == STATUS_SUCCESS) ? "success" : "pending");
397 /* queue another async operation ? */
398 if (iosb->u.Status == STATUS_PENDING)
399 fileio_queue_async(fileio, iosb, TRUE);
400 else
401 fileio_terminate(fileio, iosb);
402 break;
403 default:
404 iosb->u.Status = status;
405 fileio_terminate(fileio, iosb);
406 break;
411 /******************************************************************************
412 * NtReadFile [NTDLL.@]
413 * ZwReadFile [NTDLL.@]
415 * Read from an open file handle.
417 * PARAMS
418 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
419 * Event [I] Event to signal upon completion (or NULL)
420 * ApcRoutine [I] Callback to call upon completion (or NULL)
421 * ApcContext [I] Context for ApcRoutine (or NULL)
422 * IoStatusBlock [O] Receives information about the operation on return
423 * Buffer [O] Destination for the data read
424 * Length [I] Size of Buffer
425 * ByteOffset [O] Destination for the new file pointer position (or NULL)
426 * Key [O] Function unknown (may be NULL)
428 * RETURNS
429 * Success: 0. IoStatusBlock is updated, and the Information member contains
430 * The number of bytes read.
431 * Failure: An NTSTATUS error code describing the error.
433 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
434 PIO_APC_ROUTINE apc, void* apc_user,
435 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
436 PLARGE_INTEGER offset, PULONG key)
438 int result, unix_handle, needs_close, flags;
439 enum server_fd_type type;
441 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
442 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
444 if (!io_status) return STATUS_ACCESS_VIOLATION;
446 io_status->Information = 0;
447 io_status->u.Status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
448 &needs_close, &type, &flags );
449 if (io_status->u.Status) return io_status->u.Status;
451 if (type == FD_TYPE_FILE && offset)
453 /* async I/O doesn't make sense on regular files */
454 if (flags & FD_FLAG_OVERLAPPED)
456 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
458 if (errno == EINTR) continue;
459 if (errno == EAGAIN || errno == ESPIPE)
461 io_status->u.Status = STATUS_PENDING;
462 break;
464 result = 0;
465 io_status->u.Status = FILE_GetNtStatus();
466 goto done;
468 if (result >= 0)
470 io_status->Information = result;
471 io_status->u.Status = result ? STATUS_SUCCESS : STATUS_END_OF_FILE;
472 if (hEvent) NtSetEvent( hEvent, NULL );
473 if (apc && !io_status->u.Status) apc( apc_user, io_status, io_status->Information );
474 goto done;
477 else
479 if (lseek( unix_handle, offset->QuadPart, SEEK_SET ) == (off_t)-1)
481 io_status->u.Status = FILE_GetNtStatus();
482 goto done;
487 if (flags & FD_FLAG_RECV_SHUTDOWN)
489 if (needs_close) close( unix_handle );
490 return STATUS_PIPE_DISCONNECTED;
493 if (flags & FD_FLAG_TIMEOUT)
495 if (hEvent)
497 /* this shouldn't happen, but check it */
498 FIXME("NIY-hEvent\n");
499 if (needs_close) close( unix_handle );
500 return STATUS_NOT_IMPLEMENTED;
502 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
503 if (io_status->u.Status)
505 if (needs_close) close( unix_handle );
506 return io_status->u.Status;
510 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
512 async_fileio* fileio;
513 NTSTATUS ret;
515 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
517 if (needs_close) close( unix_handle );
518 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
519 return STATUS_NO_MEMORY;
521 fileio->handle = hFile;
522 fileio->count = length;
523 fileio->apc = apc;
524 fileio->apc_user = apc_user;
525 fileio->buffer = buffer;
526 fileio->queue_apc_on_error = 0;
527 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
528 fileio->event = hEvent;
529 if (needs_close) close( unix_handle );
531 io_status->u.Status = STATUS_PENDING;
532 ret = fileio_queue_async(fileio, io_status, TRUE);
533 if (ret != STATUS_PENDING)
535 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
536 return ret;
538 if (flags & FD_FLAG_TIMEOUT)
542 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
544 while (ret == STATUS_USER_APC && io_status->u.Status == STATUS_PENDING);
545 NtClose(hEvent);
546 if (ret != STATUS_USER_APC)
547 fileio->queue_apc_on_error = 1;
549 else
551 LARGE_INTEGER timeout;
553 /* let some APC be run, this will read some already pending data */
554 timeout.u.LowPart = timeout.u.HighPart = 0;
555 ret = NtDelayExecution( TRUE, &timeout );
556 /* the apc didn't run and therefore the completion routine now
557 * needs to be sent errors.
558 * Note that there is no race between setting this flag and
559 * returning errors because apc's are run only during alertable
560 * waits */
561 if (ret != STATUS_USER_APC)
562 fileio->queue_apc_on_error = 1;
564 TRACE("= 0x%08x\n", io_status->u.Status);
565 return io_status->u.Status;
568 /* code for synchronous reads */
569 while ((result = read( unix_handle, buffer, length )) == -1)
571 if ((errno == EAGAIN) || (errno == EINTR)) continue;
572 io_status->u.Status = FILE_GetNtStatus();
573 result = 0;
574 break;
576 io_status->Information = result;
577 if (io_status->u.Status == STATUS_SUCCESS && io_status->Information == 0)
579 struct stat st;
580 if (fstat( unix_handle, &st ) != -1 && S_ISSOCK( st.st_mode ))
581 io_status->u.Status = STATUS_PIPE_BROKEN;
582 else
583 io_status->u.Status = STATUS_END_OF_FILE;
585 done:
586 if (needs_close) close( unix_handle );
587 TRACE("= 0x%08x (%lu)\n", io_status->u.Status, io_status->Information);
588 return io_status->u.Status;
591 /***********************************************************************
592 * FILE_AsyncWriteService (INTERNAL)
594 static void WINAPI FILE_AsyncWriteService(void *ovp, IO_STATUS_BLOCK *iosb, ULONG status)
596 async_fileio *fileio = (async_fileio *) ovp;
597 int result, fd, needs_close;
598 int already = iosb->Information;
600 TRACE("(%p %p 0x%x)\n",iosb, fileio->buffer, status);
602 switch (status)
604 case STATUS_ALERTED:
605 /* write some data (non-blocking) */
606 if ((iosb->u.Status = server_get_unix_fd( fileio->handle, FILE_WRITE_DATA, &fd,
607 &needs_close, NULL, NULL )))
609 fileio_terminate(fileio, iosb);
610 break;
612 result = write(fd, &fileio->buffer[already], fileio->count - already);
613 if (needs_close) close( fd );
615 if (result < 0)
617 if (errno == EAGAIN || errno == EINTR) iosb->u.Status = STATUS_PENDING;
618 else iosb->u.Status = FILE_GetNtStatus();
620 else
622 iosb->Information += result;
623 iosb->u.Status = (iosb->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
624 TRACE("wrote %d more bytes %ld/%d so far\n",
625 result, iosb->Information, fileio->count);
627 if (iosb->u.Status == STATUS_PENDING)
628 fileio_queue_async(fileio, iosb, FALSE);
629 else
630 fileio_terminate(fileio, iosb);
631 break;
632 default:
633 iosb->u.Status = status;
634 fileio_terminate(fileio, iosb);
635 break;
639 /******************************************************************************
640 * NtWriteFile [NTDLL.@]
641 * ZwWriteFile [NTDLL.@]
643 * Write to an open file handle.
645 * PARAMS
646 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
647 * Event [I] Event to signal upon completion (or NULL)
648 * ApcRoutine [I] Callback to call upon completion (or NULL)
649 * ApcContext [I] Context for ApcRoutine (or NULL)
650 * IoStatusBlock [O] Receives information about the operation on return
651 * Buffer [I] Source for the data to write
652 * Length [I] Size of Buffer
653 * ByteOffset [O] Destination for the new file pointer position (or NULL)
654 * Key [O] Function unknown (may be NULL)
656 * RETURNS
657 * Success: 0. IoStatusBlock is updated, and the Information member contains
658 * The number of bytes written.
659 * Failure: An NTSTATUS error code describing the error.
661 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
662 PIO_APC_ROUTINE apc, void* apc_user,
663 PIO_STATUS_BLOCK io_status,
664 const void* buffer, ULONG length,
665 PLARGE_INTEGER offset, PULONG key)
667 int result, unix_handle, needs_close, flags;
668 enum server_fd_type type;
670 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
671 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
673 if (!io_status) return STATUS_ACCESS_VIOLATION;
675 io_status->Information = 0;
676 io_status->u.Status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
677 &needs_close, &type, &flags );
678 if (io_status->u.Status) return io_status->u.Status;
680 if (type == FD_TYPE_FILE && offset)
682 if (flags & FD_FLAG_OVERLAPPED)
684 /* async I/O doesn't make sense on regular files */
685 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
687 if (errno == EINTR) continue;
688 if (errno == EAGAIN || errno == ESPIPE)
690 io_status->u.Status = STATUS_PENDING;
691 break;
693 result = 0;
694 if (errno == EFAULT) io_status->u.Status = STATUS_INVALID_USER_BUFFER;
695 else io_status->u.Status = FILE_GetNtStatus();
696 goto done;
698 if (result >= 0)
700 io_status->Information = result;
701 io_status->u.Status = STATUS_SUCCESS;
702 if (hEvent) NtSetEvent( hEvent, NULL );
703 if (apc) apc( apc_user, io_status, io_status->Information );
704 goto done;
707 else
709 if (lseek( unix_handle, offset->QuadPart, SEEK_SET ) == (off_t)-1)
711 io_status->u.Status = FILE_GetNtStatus();
712 goto done;
717 if (flags & FD_FLAG_SEND_SHUTDOWN)
719 if (needs_close) close( unix_handle );
720 return STATUS_PIPE_DISCONNECTED;
723 if (flags & FD_FLAG_TIMEOUT)
725 if (hEvent)
727 /* this shouldn't happen, but check it */
728 FIXME("NIY-hEvent\n");
729 if (needs_close) close( unix_handle );
730 return STATUS_NOT_IMPLEMENTED;
732 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
733 if (io_status->u.Status)
735 if (needs_close) close( unix_handle );
736 return io_status->u.Status;
740 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
742 async_fileio* fileio;
743 NTSTATUS ret;
745 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
747 if (needs_close) close( unix_handle );
748 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
749 return STATUS_NO_MEMORY;
751 fileio->handle = hFile;
752 fileio->count = length;
753 fileio->apc = apc;
754 fileio->apc_user = apc_user;
755 fileio->buffer = (void*)buffer;
756 fileio->queue_apc_on_error = 0;
757 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
758 fileio->event = hEvent;
759 if (needs_close) close( unix_handle );
761 io_status->Information = 0;
762 io_status->u.Status = STATUS_PENDING;
763 ret = fileio_queue_async(fileio, io_status, FALSE);
764 if (ret != STATUS_PENDING)
766 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
767 return ret;
769 if (flags & FD_FLAG_TIMEOUT)
773 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
775 while (ret == STATUS_USER_APC && io_status->u.Status == STATUS_PENDING);
776 NtClose(hEvent);
777 if (ret != STATUS_USER_APC)
778 fileio->queue_apc_on_error = 1;
780 else
782 LARGE_INTEGER timeout;
784 /* let some APC be run, this will write as much data as possible */
785 timeout.u.LowPart = timeout.u.HighPart = 0;
786 ret = NtDelayExecution( TRUE, &timeout );
787 /* the apc didn't run and therefore the completion routine now
788 * needs to be sent errors.
789 * Note that there is no race between setting this flag and
790 * returning errors because apc's are run only during alertable
791 * waits */
792 if (ret != STATUS_USER_APC)
793 fileio->queue_apc_on_error = 1;
795 return io_status->u.Status;
798 /* synchronous file write */
799 while ((result = write( unix_handle, buffer, length )) == -1)
801 if ((errno == EAGAIN) || (errno == EINTR)) continue;
802 result = 0;
803 if (errno == EFAULT) io_status->u.Status = STATUS_INVALID_USER_BUFFER;
804 else io_status->u.Status = FILE_GetNtStatus();
805 break;
807 io_status->Information = result;
808 done:
809 if (needs_close) close( unix_handle );
810 return io_status->u.Status;
813 /**************************************************************************
814 * NtDeviceIoControlFile [NTDLL.@]
815 * ZwDeviceIoControlFile [NTDLL.@]
817 * Perform an I/O control operation on an open file handle.
819 * PARAMS
820 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
821 * event [I] Event to signal upon completion (or NULL)
822 * apc [I] Callback to call upon completion (or NULL)
823 * apc_context [I] Context for ApcRoutine (or NULL)
824 * io [O] Receives information about the operation on return
825 * code [I] Control code for the operation to perform
826 * in_buffer [I] Source for any input data required (or NULL)
827 * in_size [I] Size of InputBuffer
828 * out_buffer [O] Source for any output data returned (or NULL)
829 * out_size [I] Size of OutputBuffer
831 * RETURNS
832 * Success: 0. IoStatusBlock is updated.
833 * Failure: An NTSTATUS error code describing the error.
835 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
836 PIO_APC_ROUTINE apc, PVOID apc_context,
837 PIO_STATUS_BLOCK io, ULONG code,
838 PVOID in_buffer, ULONG in_size,
839 PVOID out_buffer, ULONG out_size)
841 ULONG device = (code >> 16);
843 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
844 handle, event, apc, apc_context, io, code,
845 in_buffer, in_size, out_buffer, out_size);
847 switch(device)
849 case FILE_DEVICE_DISK:
850 case FILE_DEVICE_CD_ROM:
851 case FILE_DEVICE_DVD:
852 case FILE_DEVICE_CONTROLLER:
853 case FILE_DEVICE_MASS_STORAGE:
854 io->u.Status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
855 in_buffer, in_size, out_buffer, out_size);
856 break;
857 case FILE_DEVICE_SERIAL_PORT:
858 io->u.Status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
859 in_buffer, in_size, out_buffer, out_size);
860 break;
861 case FILE_DEVICE_TAPE:
862 io->u.Status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
863 in_buffer, in_size, out_buffer, out_size);
864 break;
865 default:
866 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
867 code, device, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
868 io->u.Status = STATUS_NOT_SUPPORTED;
869 break;
871 return io->u.Status;
874 /***********************************************************************
875 * pipe_completion_wait (Internal)
877 static void CALLBACK pipe_completion_wait(void *arg, PIO_STATUS_BLOCK iosb, ULONG status)
879 TRACE("for %p, status=%08x\n", iosb, status);
880 iosb->u.Status = status;
883 /**************************************************************************
884 * NtFsControlFile [NTDLL.@]
885 * ZwFsControlFile [NTDLL.@]
887 * Perform a file system control operation on an open file handle.
889 * PARAMS
890 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
891 * event [I] Event to signal upon completion (or NULL)
892 * apc [I] Callback to call upon completion (or NULL)
893 * apc_context [I] Context for ApcRoutine (or NULL)
894 * io [O] Receives information about the operation on return
895 * code [I] Control code for the operation to perform
896 * in_buffer [I] Source for any input data required (or NULL)
897 * in_size [I] Size of InputBuffer
898 * out_buffer [O] Source for any output data returned (or NULL)
899 * out_size [I] Size of OutputBuffer
901 * RETURNS
902 * Success: 0. IoStatusBlock is updated.
903 * Failure: An NTSTATUS error code describing the error.
905 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
906 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
907 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
909 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
910 handle, event, apc, apc_context, io, code,
911 in_buffer, in_size, out_buffer, out_size);
913 if (!io) return STATUS_INVALID_PARAMETER;
915 switch(code)
917 case FSCTL_DISMOUNT_VOLUME:
918 io->u.Status = DIR_unmount_device( handle );
919 break;
921 case FSCTL_PIPE_LISTEN:
923 HANDLE internal_event = 0;
925 if(!event)
927 io->u.Status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
928 if (io->u.Status != STATUS_SUCCESS) return io->u.Status;
930 SERVER_START_REQ(connect_named_pipe)
932 req->handle = handle;
933 req->async.callback = pipe_completion_wait;
934 req->async.iosb = io;
935 req->async.arg = NULL;
936 req->async.event = event ? event : internal_event;
937 io->u.Status = wine_server_call(req);
939 SERVER_END_REQ;
941 if (!event && io->u.Status == STATUS_PENDING)
943 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
945 if (internal_event) NtClose(internal_event);
947 break;
949 case FSCTL_PIPE_WAIT:
951 HANDLE internal_event = 0;
952 FILE_PIPE_WAIT_FOR_BUFFER *buff = in_buffer;
954 if(!event)
956 io->u.Status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
957 if (io->u.Status != STATUS_SUCCESS) return io->u.Status;
959 SERVER_START_REQ(wait_named_pipe)
961 req->handle = handle;
962 req->timeout = buff->TimeoutSpecified ? buff->Timeout.QuadPart / -10000L
963 : NMPWAIT_USE_DEFAULT_WAIT;
964 req->async.callback = pipe_completion_wait;
965 req->async.iosb = io;
966 req->async.arg = NULL;
967 req->async.event = event ? event : internal_event;
968 wine_server_add_data( req, buff->Name, buff->NameLength );
969 io->u.Status = wine_server_call( req );
971 SERVER_END_REQ;
973 if (!event && io->u.Status == STATUS_PENDING)
975 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
977 if (internal_event) NtClose(internal_event);
979 break;
981 case FSCTL_PIPE_PEEK:
983 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
984 int avail = 0, fd, needs_close, flags;
986 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
988 io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
989 break;
992 if ((io->u.Status = server_get_unix_fd( handle, FILE_READ_DATA, &fd,
993 &needs_close, NULL, &flags )))
994 break;
996 if (flags & FD_FLAG_RECV_SHUTDOWN)
998 if (needs_close) close( fd );
999 io->u.Status = STATUS_PIPE_DISCONNECTED;
1000 break;
1003 #ifdef FIONREAD
1004 if (ioctl( fd, FIONREAD, &avail ) != 0)
1006 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1007 if (needs_close) close( fd );
1008 io->u.Status = FILE_GetNtStatus();
1009 break;
1011 #endif
1012 if (!avail) /* check for closed pipe */
1014 struct pollfd pollfd;
1015 int ret;
1017 pollfd.fd = fd;
1018 pollfd.events = POLLIN;
1019 pollfd.revents = 0;
1020 ret = poll( &pollfd, 1, 0 );
1021 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1023 if (needs_close) close( fd );
1024 io->u.Status = STATUS_PIPE_BROKEN;
1025 break;
1028 buffer->NamedPipeState = 0; /* FIXME */
1029 buffer->ReadDataAvailable = avail;
1030 buffer->NumberOfMessages = 0; /* FIXME */
1031 buffer->MessageLength = 0; /* FIXME */
1032 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1033 io->u.Status = STATUS_SUCCESS;
1034 if (avail)
1036 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1037 if (data_size)
1039 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1040 if (res >= 0) io->Information += res;
1043 if (needs_close) close( fd );
1045 break;
1047 case FSCTL_PIPE_DISCONNECT:
1048 SERVER_START_REQ(disconnect_named_pipe)
1050 req->handle = handle;
1051 io->u.Status = wine_server_call(req);
1052 if (!io->u.Status)
1054 int fd = server_remove_fd_from_cache( handle );
1055 if (fd != -1) close( fd );
1058 SERVER_END_REQ;
1059 break;
1061 case FSCTL_LOCK_VOLUME:
1062 case FSCTL_UNLOCK_VOLUME:
1063 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1064 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1065 io->u.Status = STATUS_SUCCESS;
1066 break;
1068 default:
1069 FIXME("Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1070 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1071 io->u.Status = STATUS_NOT_SUPPORTED;
1072 break;
1074 return io->u.Status;
1077 /******************************************************************************
1078 * NtSetVolumeInformationFile [NTDLL.@]
1079 * ZwSetVolumeInformationFile [NTDLL.@]
1081 * Set volume information for an open file handle.
1083 * PARAMS
1084 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1085 * IoStatusBlock [O] Receives information about the operation on return
1086 * FsInformation [I] Source for volume information
1087 * Length [I] Size of FsInformation
1088 * FsInformationClass [I] Type of volume information to set
1090 * RETURNS
1091 * Success: 0. IoStatusBlock is updated.
1092 * Failure: An NTSTATUS error code describing the error.
1094 NTSTATUS WINAPI NtSetVolumeInformationFile(
1095 IN HANDLE FileHandle,
1096 PIO_STATUS_BLOCK IoStatusBlock,
1097 PVOID FsInformation,
1098 ULONG Length,
1099 FS_INFORMATION_CLASS FsInformationClass)
1101 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1102 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1103 return 0;
1106 /******************************************************************************
1107 * NtQueryInformationFile [NTDLL.@]
1108 * ZwQueryInformationFile [NTDLL.@]
1110 * Get information about an open file handle.
1112 * PARAMS
1113 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1114 * io [O] Receives information about the operation on return
1115 * ptr [O] Destination for file information
1116 * len [I] Size of FileInformation
1117 * class [I] Type of file information to get
1119 * RETURNS
1120 * Success: 0. IoStatusBlock and FileInformation are updated.
1121 * Failure: An NTSTATUS error code describing the error.
1123 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1124 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1126 static const size_t info_sizes[] =
1129 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1130 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1131 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1132 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1133 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1134 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1135 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1136 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1137 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1138 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1139 0, /* FileLinkInformation */
1140 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1141 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1142 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1143 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1144 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1145 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1146 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1147 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1148 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1149 0, /* FileAlternateNameInformation */
1150 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1151 0, /* FilePipeInformation */
1152 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1153 0, /* FilePipeRemoteInformation */
1154 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1155 0, /* FileMailslotSetInformation */
1156 0, /* FileCompressionInformation */
1157 0, /* FileObjectIdInformation */
1158 0, /* FileCompletionInformation */
1159 0, /* FileMoveClusterInformation */
1160 0, /* FileQuotaInformation */
1161 0, /* FileReparsePointInformation */
1162 0, /* FileNetworkOpenInformation */
1163 0, /* FileAttributeTagInformation */
1164 0 /* FileTrackingInformation */
1167 struct stat st;
1168 int fd, needs_close = FALSE;
1170 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1172 io->Information = 0;
1174 if (class <= 0 || class >= FileMaximumInformation)
1175 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1176 if (!info_sizes[class])
1178 FIXME("Unsupported class (%d)\n", class);
1179 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1181 if (len < info_sizes[class])
1182 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1184 if (class != FilePipeLocalInformation)
1186 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1187 return io->u.Status;
1190 switch (class)
1192 case FileBasicInformation:
1194 FILE_BASIC_INFORMATION *info = ptr;
1196 if (fstat( fd, &st ) == -1)
1197 io->u.Status = FILE_GetNtStatus();
1198 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1199 io->u.Status = STATUS_INVALID_INFO_CLASS;
1200 else
1202 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1203 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1204 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1205 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1206 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1207 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1208 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1209 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1212 break;
1213 case FileStandardInformation:
1215 FILE_STANDARD_INFORMATION *info = ptr;
1217 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1218 else
1220 if ((info->Directory = S_ISDIR(st.st_mode)))
1222 info->AllocationSize.QuadPart = 0;
1223 info->EndOfFile.QuadPart = 0;
1224 info->NumberOfLinks = 1;
1225 info->DeletePending = FALSE;
1227 else
1229 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1230 info->EndOfFile.QuadPart = st.st_size;
1231 info->NumberOfLinks = st.st_nlink;
1232 info->DeletePending = FALSE; /* FIXME */
1236 break;
1237 case FilePositionInformation:
1239 FILE_POSITION_INFORMATION *info = ptr;
1240 off_t res = lseek( fd, 0, SEEK_CUR );
1241 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1242 else info->CurrentByteOffset.QuadPart = res;
1244 break;
1245 case FileInternalInformation:
1247 FILE_INTERNAL_INFORMATION *info = ptr;
1249 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1250 else info->IndexNumber.QuadPart = st.st_ino;
1252 break;
1253 case FileEaInformation:
1255 FILE_EA_INFORMATION *info = ptr;
1256 info->EaSize = 0;
1258 break;
1259 case FileEndOfFileInformation:
1261 FILE_END_OF_FILE_INFORMATION *info = ptr;
1263 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1264 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1266 break;
1267 case FileAllInformation:
1269 FILE_ALL_INFORMATION *info = ptr;
1271 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1272 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1273 io->u.Status = STATUS_INVALID_INFO_CLASS;
1274 else
1276 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1278 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1279 info->StandardInformation.AllocationSize.QuadPart = 0;
1280 info->StandardInformation.EndOfFile.QuadPart = 0;
1281 info->StandardInformation.NumberOfLinks = 1;
1282 info->StandardInformation.DeletePending = FALSE;
1284 else
1286 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1287 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1288 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1289 info->StandardInformation.NumberOfLinks = st.st_nlink;
1290 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1292 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1293 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1294 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1295 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1296 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1297 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1298 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1299 info->EaInformation.EaSize = 0;
1300 info->AccessInformation.AccessFlags = 0; /* FIXME */
1301 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1302 info->ModeInformation.Mode = 0; /* FIXME */
1303 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1304 info->NameInformation.FileNameLength = 0;
1305 io->Information = sizeof(*info) - sizeof(WCHAR);
1308 break;
1309 case FileMailslotQueryInformation:
1311 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1313 SERVER_START_REQ( set_mailslot_info )
1315 req->handle = hFile;
1316 req->flags = 0;
1317 io->u.Status = wine_server_call( req );
1318 if( io->u.Status == STATUS_SUCCESS )
1320 info->MaximumMessageSize = reply->max_msgsize;
1321 info->MailslotQuota = 0;
1322 info->NextMessageSize = 0;
1323 info->MessagesAvailable = 0;
1324 info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1327 SERVER_END_REQ;
1328 if (!io->u.Status)
1330 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1331 char *tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size );
1332 if (tmpbuf)
1334 int fd, needs_close;
1335 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1337 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1338 info->MessagesAvailable = (res > 0);
1339 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1340 if (needs_close) close( fd );
1342 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1346 break;
1347 case FilePipeLocalInformation:
1349 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1351 SERVER_START_REQ( get_named_pipe_info )
1353 req->handle = hFile;
1354 if (!(io->u.Status = wine_server_call( req )))
1356 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1357 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1358 pli->NamedPipeConfiguration = 0; /* FIXME */
1359 pli->MaximumInstances = reply->maxinstances;
1360 pli->CurrentInstances = reply->instances;
1361 pli->InboundQuota = reply->insize;
1362 pli->ReadDataAvailable = 0; /* FIXME */
1363 pli->OutboundQuota = reply->outsize;
1364 pli->WriteQuotaAvailable = 0; /* FIXME */
1365 pli->NamedPipeState = 0; /* FIXME */
1366 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1367 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1370 SERVER_END_REQ;
1372 break;
1373 default:
1374 FIXME("Unsupported class (%d)\n", class);
1375 io->u.Status = STATUS_NOT_IMPLEMENTED;
1376 break;
1378 if (needs_close) close( fd );
1379 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1380 return io->u.Status;
1383 /******************************************************************************
1384 * NtSetInformationFile [NTDLL.@]
1385 * ZwSetInformationFile [NTDLL.@]
1387 * Set information about an open file handle.
1389 * PARAMS
1390 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1391 * io [O] Receives information about the operation on return
1392 * ptr [I] Source for file information
1393 * len [I] Size of FileInformation
1394 * class [I] Type of file information to set
1396 * RETURNS
1397 * Success: 0. io is updated.
1398 * Failure: An NTSTATUS error code describing the error.
1400 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1401 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1403 int fd, needs_close;
1405 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1407 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1408 return io->u.Status;
1410 io->u.Status = STATUS_SUCCESS;
1411 switch (class)
1413 case FileBasicInformation:
1414 if (len >= sizeof(FILE_BASIC_INFORMATION))
1416 struct stat st;
1417 const FILE_BASIC_INFORMATION *info = ptr;
1419 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1421 ULONGLONG sec, nsec;
1422 struct timeval tv[2];
1424 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1427 tv[0].tv_sec = tv[0].tv_usec = 0;
1428 tv[1].tv_sec = tv[1].tv_usec = 0;
1429 if (!fstat( fd, &st ))
1431 tv[0].tv_sec = st.st_atime;
1432 tv[1].tv_sec = st.st_mtime;
1435 if (info->LastAccessTime.QuadPart)
1437 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1438 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1439 tv[0].tv_usec = (UINT)nsec / 10;
1441 if (info->LastWriteTime.QuadPart)
1443 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1444 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1445 tv[1].tv_usec = (UINT)nsec / 10;
1447 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1450 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1452 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1453 else
1455 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1457 if (S_ISDIR( st.st_mode))
1458 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1459 else
1460 st.st_mode &= ~0222; /* clear write permission bits */
1462 else
1464 /* add write permission only where we already have read permission */
1465 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1467 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1471 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1472 break;
1474 case FilePositionInformation:
1475 if (len >= sizeof(FILE_POSITION_INFORMATION))
1477 const FILE_POSITION_INFORMATION *info = ptr;
1479 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1480 io->u.Status = FILE_GetNtStatus();
1482 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1483 break;
1485 case FileEndOfFileInformation:
1486 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1488 struct stat st;
1489 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1491 /* first try normal truncate */
1492 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1494 /* now check for the need to extend the file */
1495 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1497 static const char zero;
1499 /* extend the file one byte beyond the requested size and then truncate it */
1500 /* this should work around ftruncate implementations that can't extend files */
1501 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1502 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1504 io->u.Status = FILE_GetNtStatus();
1506 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1507 break;
1509 case FileMailslotSetInformation:
1511 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1513 SERVER_START_REQ( set_mailslot_info )
1515 req->handle = handle;
1516 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1517 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1518 io->u.Status = wine_server_call( req );
1520 SERVER_END_REQ;
1522 break;
1524 default:
1525 FIXME("Unsupported class (%d)\n", class);
1526 io->u.Status = STATUS_NOT_IMPLEMENTED;
1527 break;
1529 if (needs_close) close( fd );
1530 io->Information = 0;
1531 return io->u.Status;
1535 /******************************************************************************
1536 * NtQueryFullAttributesFile (NTDLL.@)
1538 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1539 FILE_NETWORK_OPEN_INFORMATION *info )
1541 ANSI_STRING unix_name;
1542 NTSTATUS status;
1544 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1545 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1547 struct stat st;
1549 if (stat( unix_name.Buffer, &st ) == -1)
1550 status = FILE_GetNtStatus();
1551 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1552 status = STATUS_INVALID_INFO_CLASS;
1553 else
1555 if (S_ISDIR(st.st_mode))
1557 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1558 info->AllocationSize.QuadPart = 0;
1559 info->EndOfFile.QuadPart = 0;
1561 else
1563 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1564 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1565 info->EndOfFile.QuadPart = st.st_size;
1567 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1568 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1569 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1570 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1571 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1572 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1573 if (DIR_is_hidden_file( attr->ObjectName ))
1574 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1576 RtlFreeAnsiString( &unix_name );
1578 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1579 return status;
1583 /******************************************************************************
1584 * NtQueryAttributesFile (NTDLL.@)
1585 * ZwQueryAttributesFile (NTDLL.@)
1587 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1589 FILE_NETWORK_OPEN_INFORMATION full_info;
1590 NTSTATUS status;
1592 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1594 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1595 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1596 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1597 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1598 info->FileAttributes = full_info.FileAttributes;
1600 return status;
1604 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1605 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1606 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1607 size_t fstypesize, unsigned int flags )
1609 if (!strncmp("cd9660", fstypename, fstypesize) ||
1610 !strncmp("udf", fstypename, fstypesize))
1612 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1613 /* Don't assume read-only, let the mount options set it below */
1614 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1616 else if (!strncmp("nfs", fstypename, fstypesize) ||
1617 !strncmp("nwfs", fstypename, fstypesize) ||
1618 !strncmp("smbfs", fstypename, fstypesize) ||
1619 !strncmp("afpfs", fstypename, fstypesize))
1621 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1622 info->Characteristics |= FILE_REMOTE_DEVICE;
1624 else if (!strncmp("procfs", fstypename, fstypesize))
1625 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1626 else
1627 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1629 if (flags & MNT_RDONLY)
1630 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1632 if (!(flags & MNT_LOCAL))
1634 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1635 info->Characteristics |= FILE_REMOTE_DEVICE;
1638 #endif
1640 /******************************************************************************
1641 * get_device_info
1643 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1645 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1647 struct stat st;
1649 info->Characteristics = 0;
1650 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1651 if (S_ISCHR( st.st_mode ))
1653 info->DeviceType = FILE_DEVICE_UNKNOWN;
1654 #ifdef linux
1655 switch(major(st.st_rdev))
1657 case MEM_MAJOR:
1658 info->DeviceType = FILE_DEVICE_NULL;
1659 break;
1660 case TTY_MAJOR:
1661 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1662 break;
1663 case LP_MAJOR:
1664 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1665 break;
1666 case SCSI_TAPE_MAJOR:
1667 info->DeviceType = FILE_DEVICE_TAPE;
1668 break;
1670 #endif
1672 else if (S_ISBLK( st.st_mode ))
1674 info->DeviceType = FILE_DEVICE_DISK;
1676 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1678 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1680 else /* regular file or directory */
1682 #if defined(linux) && defined(HAVE_FSTATFS)
1683 struct statfs stfs;
1685 /* check for floppy disk */
1686 if (major(st.st_dev) == FLOPPY_MAJOR)
1687 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1689 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1690 switch (stfs.f_type)
1692 case 0x9660: /* iso9660 */
1693 case 0x9fa1: /* supermount */
1694 case 0x15013346: /* udf */
1695 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1696 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1697 break;
1698 case 0x6969: /* nfs */
1699 case 0x517B: /* smbfs */
1700 case 0x564c: /* ncpfs */
1701 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1702 info->Characteristics |= FILE_REMOTE_DEVICE;
1703 break;
1704 case 0x01021994: /* tmpfs */
1705 case 0x28cd3d45: /* cramfs */
1706 case 0x1373: /* devfs */
1707 case 0x9fa0: /* procfs */
1708 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1709 break;
1710 default:
1711 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1712 break;
1714 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1715 struct statfs stfs;
1717 if (fstatfs( fd, &stfs ) < 0)
1718 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1719 else
1720 get_device_info_fstatfs( info, stfs.f_fstypename,
1721 sizeof(stfs.f_fstypename), stfs.f_flags );
1722 #elif defined(__NetBSD__)
1723 struct statvfs stfs;
1725 if (fstatvfs( fd, &stfs) < 0)
1726 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1727 else
1728 get_device_info_fstatfs( info, stfs.f_fstypename,
1729 sizeof(stfs.f_fstypename), stfs.f_flag );
1730 #elif defined(sun)
1731 /* Use dkio to work out device types */
1733 # include <sys/dkio.h>
1734 # include <sys/vtoc.h>
1735 struct dk_cinfo dkinf;
1736 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1737 if(retval==-1){
1738 WARN("Unable to get disk device type information - assuming a disk like device\n");
1739 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1741 switch (dkinf.dki_ctype)
1743 case DKC_CDROM:
1744 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1745 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1746 break;
1747 case DKC_NCRFLOPPY:
1748 case DKC_SMSFLOPPY:
1749 case DKC_INTEL82072:
1750 case DKC_INTEL82077:
1751 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1752 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1753 break;
1754 case DKC_MD:
1755 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1756 break;
1757 default:
1758 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1761 #else
1762 static int warned;
1763 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1764 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1765 #endif
1766 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1768 return STATUS_SUCCESS;
1772 /******************************************************************************
1773 * NtQueryVolumeInformationFile [NTDLL.@]
1774 * ZwQueryVolumeInformationFile [NTDLL.@]
1776 * Get volume information for an open file handle.
1778 * PARAMS
1779 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1780 * io [O] Receives information about the operation on return
1781 * buffer [O] Destination for volume information
1782 * length [I] Size of FsInformation
1783 * info_class [I] Type of volume information to set
1785 * RETURNS
1786 * Success: 0. io and buffer are updated.
1787 * Failure: An NTSTATUS error code describing the error.
1789 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1790 PVOID buffer, ULONG length,
1791 FS_INFORMATION_CLASS info_class )
1793 int fd, needs_close;
1794 struct stat st;
1796 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1797 return io->u.Status;
1799 io->u.Status = STATUS_NOT_IMPLEMENTED;
1800 io->Information = 0;
1802 switch( info_class )
1804 case FileFsVolumeInformation:
1805 FIXME( "%p: volume info not supported\n", handle );
1806 break;
1807 case FileFsLabelInformation:
1808 FIXME( "%p: label info not supported\n", handle );
1809 break;
1810 case FileFsSizeInformation:
1811 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1812 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1813 else
1815 FILE_FS_SIZE_INFORMATION *info = buffer;
1817 if (fstat( fd, &st ) < 0)
1819 io->u.Status = FILE_GetNtStatus();
1820 break;
1822 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1824 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1826 else
1828 /* Linux's fstatvfs is buggy */
1829 #if !defined(linux) || !defined(HAVE_FSTATFS)
1830 struct statvfs stfs;
1832 if (fstatvfs( fd, &stfs ) < 0)
1834 io->u.Status = FILE_GetNtStatus();
1835 break;
1837 info->BytesPerSector = stfs.f_frsize;
1838 #else
1839 struct statfs stfs;
1840 if (fstatfs( fd, &stfs ) < 0)
1842 io->u.Status = FILE_GetNtStatus();
1843 break;
1845 info->BytesPerSector = stfs.f_bsize;
1846 #endif
1847 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1848 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1849 info->SectorsPerAllocationUnit = 1;
1850 io->Information = sizeof(*info);
1851 io->u.Status = STATUS_SUCCESS;
1854 break;
1855 case FileFsDeviceInformation:
1856 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1857 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1858 else
1860 FILE_FS_DEVICE_INFORMATION *info = buffer;
1862 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
1863 io->Information = sizeof(*info);
1865 break;
1866 case FileFsAttributeInformation:
1867 FIXME( "%p: attribute info not supported\n", handle );
1868 break;
1869 case FileFsControlInformation:
1870 FIXME( "%p: control info not supported\n", handle );
1871 break;
1872 case FileFsFullSizeInformation:
1873 FIXME( "%p: full size info not supported\n", handle );
1874 break;
1875 case FileFsObjectIdInformation:
1876 FIXME( "%p: object id info not supported\n", handle );
1877 break;
1878 case FileFsMaximumInformation:
1879 FIXME( "%p: maximum info not supported\n", handle );
1880 break;
1881 default:
1882 io->u.Status = STATUS_INVALID_PARAMETER;
1883 break;
1885 if (needs_close) close( fd );
1886 return io->u.Status;
1890 /******************************************************************
1891 * NtFlushBuffersFile (NTDLL.@)
1893 * Flush any buffered data on an open file handle.
1895 * PARAMS
1896 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1897 * IoStatusBlock [O] Receives information about the operation on return
1899 * RETURNS
1900 * Success: 0. IoStatusBlock is updated.
1901 * Failure: An NTSTATUS error code describing the error.
1903 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1905 NTSTATUS ret;
1906 HANDLE hEvent = NULL;
1908 SERVER_START_REQ( flush_file )
1910 req->handle = hFile;
1911 ret = wine_server_call( req );
1912 hEvent = reply->event;
1914 SERVER_END_REQ;
1915 if (!ret && hEvent)
1917 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1918 NtClose( hEvent );
1920 return ret;
1923 /******************************************************************
1924 * NtLockFile (NTDLL.@)
1928 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1929 PIO_APC_ROUTINE apc, void* apc_user,
1930 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1931 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1932 BOOLEAN exclusive )
1934 NTSTATUS ret;
1935 HANDLE handle;
1936 BOOLEAN async;
1938 if (apc || io_status || key)
1940 FIXME("Unimplemented yet parameter\n");
1941 return STATUS_NOT_IMPLEMENTED;
1944 for (;;)
1946 SERVER_START_REQ( lock_file )
1948 req->handle = hFile;
1949 req->offset_low = offset->u.LowPart;
1950 req->offset_high = offset->u.HighPart;
1951 req->count_low = count->u.LowPart;
1952 req->count_high = count->u.HighPart;
1953 req->shared = !exclusive;
1954 req->wait = !dont_wait;
1955 ret = wine_server_call( req );
1956 handle = reply->handle;
1957 async = reply->overlapped;
1959 SERVER_END_REQ;
1960 if (ret != STATUS_PENDING)
1962 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1963 return ret;
1966 if (async)
1968 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1969 if (handle) NtClose( handle );
1970 return STATUS_PENDING;
1972 if (handle)
1974 NtWaitForSingleObject( handle, FALSE, NULL );
1975 NtClose( handle );
1977 else
1979 LARGE_INTEGER time;
1981 /* Unix lock conflict, sleep a bit and retry */
1982 time.QuadPart = 100 * (ULONGLONG)10000;
1983 time.QuadPart = -time.QuadPart;
1984 NtDelayExecution( FALSE, &time );
1990 /******************************************************************
1991 * NtUnlockFile (NTDLL.@)
1995 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
1996 PLARGE_INTEGER offset, PLARGE_INTEGER count,
1997 PULONG key )
1999 NTSTATUS status;
2001 TRACE( "%p %x%08x %x%08x\n",
2002 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2004 if (io_status || key)
2006 FIXME("Unimplemented yet parameter\n");
2007 return STATUS_NOT_IMPLEMENTED;
2010 SERVER_START_REQ( unlock_file )
2012 req->handle = hFile;
2013 req->offset_low = offset->u.LowPart;
2014 req->offset_high = offset->u.HighPart;
2015 req->count_low = count->u.LowPart;
2016 req->count_high = count->u.HighPart;
2017 status = wine_server_call( req );
2019 SERVER_END_REQ;
2020 return status;
2023 /******************************************************************
2024 * NtCreateNamedPipeFile (NTDLL.@)
2028 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2029 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2030 ULONG sharing, ULONG dispo, ULONG options,
2031 ULONG pipe_type, ULONG read_mode,
2032 ULONG completion_mode, ULONG max_inst,
2033 ULONG inbound_quota, ULONG outbound_quota,
2034 PLARGE_INTEGER timeout)
2036 NTSTATUS status;
2038 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2039 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2040 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2041 outbound_quota, timeout);
2043 /* assume we only get relative timeout, and storable in a DWORD as ms */
2044 if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
2045 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2047 SERVER_START_REQ( create_named_pipe )
2049 req->access = access;
2050 req->attributes = (attr) ? attr->Attributes : 0;
2051 req->rootdir = attr ? attr->RootDirectory : 0;
2052 req->options = options;
2053 req->flags =
2054 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2055 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2056 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2057 req->maxinstances = max_inst;
2058 req->outsize = outbound_quota;
2059 req->insize = inbound_quota;
2060 req->timeout = timeout->QuadPart / -10000;
2061 wine_server_add_data( req, attr->ObjectName->Buffer,
2062 attr->ObjectName->Length );
2063 status = wine_server_call( req );
2064 if (!status) *handle = reply->handle;
2066 SERVER_END_REQ;
2067 return status;
2070 /******************************************************************
2071 * NtDeleteFile (NTDLL.@)
2075 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2077 NTSTATUS status;
2078 HANDLE hFile;
2079 IO_STATUS_BLOCK io;
2081 TRACE("%p\n", ObjectAttributes);
2082 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2083 ObjectAttributes, &io, NULL, 0,
2084 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2085 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2086 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2087 return status;
2090 /******************************************************************
2091 * NtCancelIoFile (NTDLL.@)
2095 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2097 LARGE_INTEGER timeout;
2099 TRACE("%p %p\n", hFile, io_status );
2101 SERVER_START_REQ( cancel_async )
2103 req->handle = hFile;
2104 wine_server_call( req );
2106 SERVER_END_REQ;
2107 /* Let some APC be run, so that we can run the remaining APCs on hFile
2108 * either the cancelation of the pending one, but also the execution
2109 * of the queued APC, but not yet run. This is needed to ensure proper
2110 * clean-up of allocated data.
2112 timeout.u.LowPart = timeout.u.HighPart = 0;
2113 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2116 /******************************************************************************
2117 * NtCreateMailslotFile [NTDLL.@]
2118 * ZwCreateMailslotFile [NTDLL.@]
2120 * PARAMS
2121 * pHandle [O] pointer to receive the handle created
2122 * DesiredAccess [I] access mode (read, write, etc)
2123 * ObjectAttributes [I] fully qualified NT path of the mailslot
2124 * IoStatusBlock [O] receives completion status and other info
2125 * CreateOptions [I]
2126 * MailslotQuota [I]
2127 * MaxMessageSize [I]
2128 * TimeOut [I]
2130 * RETURNS
2131 * An NT status code
2133 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2134 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2135 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2136 PLARGE_INTEGER TimeOut)
2138 LARGE_INTEGER timeout;
2139 NTSTATUS ret;
2141 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2142 pHandle, DesiredAccess, attr, IoStatusBlock,
2143 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2145 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2146 if (!attr) return STATUS_INVALID_PARAMETER;
2147 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2150 * For a NULL TimeOut pointer set the default timeout value
2152 if (!TimeOut)
2153 timeout.QuadPart = -1;
2154 else
2155 timeout.QuadPart = TimeOut->QuadPart;
2157 SERVER_START_REQ( create_mailslot )
2159 req->access = DesiredAccess;
2160 req->attributes = attr->Attributes;
2161 req->rootdir = attr->RootDirectory;
2162 req->max_msgsize = MaxMessageSize;
2163 req->read_timeout = (timeout.QuadPart <= 0) ? timeout.QuadPart / -10000 : -1;
2164 wine_server_add_data( req, attr->ObjectName->Buffer,
2165 attr->ObjectName->Length );
2166 ret = wine_server_call( req );
2167 if( ret == STATUS_SUCCESS )
2168 *pHandle = reply->handle;
2170 SERVER_END_REQ;
2172 return ret;