imm32: Change the default IME window to better reflect applications request.
[wine/dibdrv.git] / dlls / ntdll / file.c
blob89de3b332a7a9262e23b4fd38a08f1324b2a330f
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 && !apc)
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.apc = apc;
937 req->async.apc_arg = apc_context;
938 req->async.event = event ? event : internal_event;
939 io->u.Status = wine_server_call(req);
941 SERVER_END_REQ;
943 if (internal_event && io->u.Status == STATUS_PENDING)
945 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
947 if (internal_event) NtClose(internal_event);
949 break;
951 case FSCTL_PIPE_WAIT:
953 HANDLE internal_event = 0;
954 FILE_PIPE_WAIT_FOR_BUFFER *buff = in_buffer;
956 if(!event && !apc)
958 io->u.Status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
959 if (io->u.Status != STATUS_SUCCESS) return io->u.Status;
961 SERVER_START_REQ(wait_named_pipe)
963 req->handle = handle;
964 req->timeout = buff->TimeoutSpecified ? buff->Timeout.QuadPart / -10000L
965 : NMPWAIT_USE_DEFAULT_WAIT;
966 req->async.callback = pipe_completion_wait;
967 req->async.iosb = io;
968 req->async.arg = NULL;
969 req->async.apc = apc;
970 req->async.apc_arg = apc_context;
971 req->async.event = event ? event : internal_event;
972 wine_server_add_data( req, buff->Name, buff->NameLength );
973 io->u.Status = wine_server_call( req );
975 SERVER_END_REQ;
977 if (internal_event && io->u.Status == STATUS_PENDING)
979 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
981 if (internal_event) NtClose(internal_event);
983 break;
985 case FSCTL_PIPE_PEEK:
987 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
988 int avail = 0, fd, needs_close, flags;
990 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
992 io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
993 break;
996 if ((io->u.Status = server_get_unix_fd( handle, FILE_READ_DATA, &fd,
997 &needs_close, NULL, &flags )))
998 break;
1000 if (flags & FD_FLAG_RECV_SHUTDOWN)
1002 if (needs_close) close( fd );
1003 io->u.Status = STATUS_PIPE_DISCONNECTED;
1004 break;
1007 #ifdef FIONREAD
1008 if (ioctl( fd, FIONREAD, &avail ) != 0)
1010 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1011 if (needs_close) close( fd );
1012 io->u.Status = FILE_GetNtStatus();
1013 break;
1015 #endif
1016 if (!avail) /* check for closed pipe */
1018 struct pollfd pollfd;
1019 int ret;
1021 pollfd.fd = fd;
1022 pollfd.events = POLLIN;
1023 pollfd.revents = 0;
1024 ret = poll( &pollfd, 1, 0 );
1025 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1027 if (needs_close) close( fd );
1028 io->u.Status = STATUS_PIPE_BROKEN;
1029 break;
1032 buffer->NamedPipeState = 0; /* FIXME */
1033 buffer->ReadDataAvailable = avail;
1034 buffer->NumberOfMessages = 0; /* FIXME */
1035 buffer->MessageLength = 0; /* FIXME */
1036 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1037 io->u.Status = STATUS_SUCCESS;
1038 if (avail)
1040 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1041 if (data_size)
1043 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1044 if (res >= 0) io->Information += res;
1047 if (needs_close) close( fd );
1049 break;
1051 case FSCTL_PIPE_DISCONNECT:
1052 SERVER_START_REQ(disconnect_named_pipe)
1054 req->handle = handle;
1055 io->u.Status = wine_server_call(req);
1056 if (!io->u.Status)
1058 int fd = server_remove_fd_from_cache( handle );
1059 if (fd != -1) close( fd );
1062 SERVER_END_REQ;
1063 break;
1065 case FSCTL_LOCK_VOLUME:
1066 case FSCTL_UNLOCK_VOLUME:
1067 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1068 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1069 io->u.Status = STATUS_SUCCESS;
1070 break;
1072 default:
1073 FIXME("Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1074 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1075 io->u.Status = STATUS_NOT_SUPPORTED;
1076 break;
1078 return io->u.Status;
1081 /******************************************************************************
1082 * NtSetVolumeInformationFile [NTDLL.@]
1083 * ZwSetVolumeInformationFile [NTDLL.@]
1085 * Set volume information for an open file handle.
1087 * PARAMS
1088 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1089 * IoStatusBlock [O] Receives information about the operation on return
1090 * FsInformation [I] Source for volume information
1091 * Length [I] Size of FsInformation
1092 * FsInformationClass [I] Type of volume information to set
1094 * RETURNS
1095 * Success: 0. IoStatusBlock is updated.
1096 * Failure: An NTSTATUS error code describing the error.
1098 NTSTATUS WINAPI NtSetVolumeInformationFile(
1099 IN HANDLE FileHandle,
1100 PIO_STATUS_BLOCK IoStatusBlock,
1101 PVOID FsInformation,
1102 ULONG Length,
1103 FS_INFORMATION_CLASS FsInformationClass)
1105 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1106 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1107 return 0;
1110 /******************************************************************************
1111 * NtQueryInformationFile [NTDLL.@]
1112 * ZwQueryInformationFile [NTDLL.@]
1114 * Get information about an open file handle.
1116 * PARAMS
1117 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1118 * io [O] Receives information about the operation on return
1119 * ptr [O] Destination for file information
1120 * len [I] Size of FileInformation
1121 * class [I] Type of file information to get
1123 * RETURNS
1124 * Success: 0. IoStatusBlock and FileInformation are updated.
1125 * Failure: An NTSTATUS error code describing the error.
1127 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1128 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1130 static const size_t info_sizes[] =
1133 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1134 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1135 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1136 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1137 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1138 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1139 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1140 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1141 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1142 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1143 0, /* FileLinkInformation */
1144 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1145 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1146 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1147 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1148 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1149 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1150 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1151 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1152 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1153 0, /* FileAlternateNameInformation */
1154 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1155 0, /* FilePipeInformation */
1156 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1157 0, /* FilePipeRemoteInformation */
1158 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1159 0, /* FileMailslotSetInformation */
1160 0, /* FileCompressionInformation */
1161 0, /* FileObjectIdInformation */
1162 0, /* FileCompletionInformation */
1163 0, /* FileMoveClusterInformation */
1164 0, /* FileQuotaInformation */
1165 0, /* FileReparsePointInformation */
1166 0, /* FileNetworkOpenInformation */
1167 0, /* FileAttributeTagInformation */
1168 0 /* FileTrackingInformation */
1171 struct stat st;
1172 int fd, needs_close = FALSE;
1174 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1176 io->Information = 0;
1178 if (class <= 0 || class >= FileMaximumInformation)
1179 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1180 if (!info_sizes[class])
1182 FIXME("Unsupported class (%d)\n", class);
1183 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1185 if (len < info_sizes[class])
1186 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1188 if (class != FilePipeLocalInformation)
1190 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1191 return io->u.Status;
1194 switch (class)
1196 case FileBasicInformation:
1198 FILE_BASIC_INFORMATION *info = ptr;
1200 if (fstat( fd, &st ) == -1)
1201 io->u.Status = FILE_GetNtStatus();
1202 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1203 io->u.Status = STATUS_INVALID_INFO_CLASS;
1204 else
1206 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1207 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1208 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1209 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1210 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1211 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1212 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1213 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1216 break;
1217 case FileStandardInformation:
1219 FILE_STANDARD_INFORMATION *info = ptr;
1221 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1222 else
1224 if ((info->Directory = S_ISDIR(st.st_mode)))
1226 info->AllocationSize.QuadPart = 0;
1227 info->EndOfFile.QuadPart = 0;
1228 info->NumberOfLinks = 1;
1229 info->DeletePending = FALSE;
1231 else
1233 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1234 info->EndOfFile.QuadPart = st.st_size;
1235 info->NumberOfLinks = st.st_nlink;
1236 info->DeletePending = FALSE; /* FIXME */
1240 break;
1241 case FilePositionInformation:
1243 FILE_POSITION_INFORMATION *info = ptr;
1244 off_t res = lseek( fd, 0, SEEK_CUR );
1245 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1246 else info->CurrentByteOffset.QuadPart = res;
1248 break;
1249 case FileInternalInformation:
1251 FILE_INTERNAL_INFORMATION *info = ptr;
1253 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1254 else info->IndexNumber.QuadPart = st.st_ino;
1256 break;
1257 case FileEaInformation:
1259 FILE_EA_INFORMATION *info = ptr;
1260 info->EaSize = 0;
1262 break;
1263 case FileEndOfFileInformation:
1265 FILE_END_OF_FILE_INFORMATION *info = ptr;
1267 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1268 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1270 break;
1271 case FileAllInformation:
1273 FILE_ALL_INFORMATION *info = ptr;
1275 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1276 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1277 io->u.Status = STATUS_INVALID_INFO_CLASS;
1278 else
1280 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1282 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1283 info->StandardInformation.AllocationSize.QuadPart = 0;
1284 info->StandardInformation.EndOfFile.QuadPart = 0;
1285 info->StandardInformation.NumberOfLinks = 1;
1286 info->StandardInformation.DeletePending = FALSE;
1288 else
1290 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1291 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1292 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1293 info->StandardInformation.NumberOfLinks = st.st_nlink;
1294 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1296 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1297 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1298 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1299 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1300 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1301 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1302 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1303 info->EaInformation.EaSize = 0;
1304 info->AccessInformation.AccessFlags = 0; /* FIXME */
1305 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1306 info->ModeInformation.Mode = 0; /* FIXME */
1307 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1308 info->NameInformation.FileNameLength = 0;
1309 io->Information = sizeof(*info) - sizeof(WCHAR);
1312 break;
1313 case FileMailslotQueryInformation:
1315 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1317 SERVER_START_REQ( set_mailslot_info )
1319 req->handle = hFile;
1320 req->flags = 0;
1321 io->u.Status = wine_server_call( req );
1322 if( io->u.Status == STATUS_SUCCESS )
1324 info->MaximumMessageSize = reply->max_msgsize;
1325 info->MailslotQuota = 0;
1326 info->NextMessageSize = 0;
1327 info->MessagesAvailable = 0;
1328 info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1331 SERVER_END_REQ;
1332 if (!io->u.Status)
1334 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1335 char *tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size );
1336 if (tmpbuf)
1338 int fd, needs_close;
1339 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1341 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1342 info->MessagesAvailable = (res > 0);
1343 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1344 if (needs_close) close( fd );
1346 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1350 break;
1351 case FilePipeLocalInformation:
1353 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1355 SERVER_START_REQ( get_named_pipe_info )
1357 req->handle = hFile;
1358 if (!(io->u.Status = wine_server_call( req )))
1360 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1361 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1362 pli->NamedPipeConfiguration = 0; /* FIXME */
1363 pli->MaximumInstances = reply->maxinstances;
1364 pli->CurrentInstances = reply->instances;
1365 pli->InboundQuota = reply->insize;
1366 pli->ReadDataAvailable = 0; /* FIXME */
1367 pli->OutboundQuota = reply->outsize;
1368 pli->WriteQuotaAvailable = 0; /* FIXME */
1369 pli->NamedPipeState = 0; /* FIXME */
1370 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1371 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1374 SERVER_END_REQ;
1376 break;
1377 default:
1378 FIXME("Unsupported class (%d)\n", class);
1379 io->u.Status = STATUS_NOT_IMPLEMENTED;
1380 break;
1382 if (needs_close) close( fd );
1383 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1384 return io->u.Status;
1387 /******************************************************************************
1388 * NtSetInformationFile [NTDLL.@]
1389 * ZwSetInformationFile [NTDLL.@]
1391 * Set information about an open file handle.
1393 * PARAMS
1394 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1395 * io [O] Receives information about the operation on return
1396 * ptr [I] Source for file information
1397 * len [I] Size of FileInformation
1398 * class [I] Type of file information to set
1400 * RETURNS
1401 * Success: 0. io is updated.
1402 * Failure: An NTSTATUS error code describing the error.
1404 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1405 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1407 int fd, needs_close;
1409 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1411 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1412 return io->u.Status;
1414 io->u.Status = STATUS_SUCCESS;
1415 switch (class)
1417 case FileBasicInformation:
1418 if (len >= sizeof(FILE_BASIC_INFORMATION))
1420 struct stat st;
1421 const FILE_BASIC_INFORMATION *info = ptr;
1423 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1425 ULONGLONG sec, nsec;
1426 struct timeval tv[2];
1428 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1431 tv[0].tv_sec = tv[0].tv_usec = 0;
1432 tv[1].tv_sec = tv[1].tv_usec = 0;
1433 if (!fstat( fd, &st ))
1435 tv[0].tv_sec = st.st_atime;
1436 tv[1].tv_sec = st.st_mtime;
1439 if (info->LastAccessTime.QuadPart)
1441 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1442 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1443 tv[0].tv_usec = (UINT)nsec / 10;
1445 if (info->LastWriteTime.QuadPart)
1447 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1448 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1449 tv[1].tv_usec = (UINT)nsec / 10;
1451 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1454 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1456 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1457 else
1459 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1461 if (S_ISDIR( st.st_mode))
1462 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1463 else
1464 st.st_mode &= ~0222; /* clear write permission bits */
1466 else
1468 /* add write permission only where we already have read permission */
1469 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1471 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1475 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1476 break;
1478 case FilePositionInformation:
1479 if (len >= sizeof(FILE_POSITION_INFORMATION))
1481 const FILE_POSITION_INFORMATION *info = ptr;
1483 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1484 io->u.Status = FILE_GetNtStatus();
1486 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1487 break;
1489 case FileEndOfFileInformation:
1490 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1492 struct stat st;
1493 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1495 /* first try normal truncate */
1496 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1498 /* now check for the need to extend the file */
1499 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1501 static const char zero;
1503 /* extend the file one byte beyond the requested size and then truncate it */
1504 /* this should work around ftruncate implementations that can't extend files */
1505 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1506 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1508 io->u.Status = FILE_GetNtStatus();
1510 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1511 break;
1513 case FileMailslotSetInformation:
1515 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1517 SERVER_START_REQ( set_mailslot_info )
1519 req->handle = handle;
1520 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1521 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1522 io->u.Status = wine_server_call( req );
1524 SERVER_END_REQ;
1526 break;
1528 default:
1529 FIXME("Unsupported class (%d)\n", class);
1530 io->u.Status = STATUS_NOT_IMPLEMENTED;
1531 break;
1533 if (needs_close) close( fd );
1534 io->Information = 0;
1535 return io->u.Status;
1539 /******************************************************************************
1540 * NtQueryFullAttributesFile (NTDLL.@)
1542 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1543 FILE_NETWORK_OPEN_INFORMATION *info )
1545 ANSI_STRING unix_name;
1546 NTSTATUS status;
1548 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1549 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1551 struct stat st;
1553 if (stat( unix_name.Buffer, &st ) == -1)
1554 status = FILE_GetNtStatus();
1555 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1556 status = STATUS_INVALID_INFO_CLASS;
1557 else
1559 if (S_ISDIR(st.st_mode))
1561 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1562 info->AllocationSize.QuadPart = 0;
1563 info->EndOfFile.QuadPart = 0;
1565 else
1567 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1568 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1569 info->EndOfFile.QuadPart = st.st_size;
1571 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1572 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1573 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1574 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1575 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1576 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1577 if (DIR_is_hidden_file( attr->ObjectName ))
1578 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1580 RtlFreeAnsiString( &unix_name );
1582 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1583 return status;
1587 /******************************************************************************
1588 * NtQueryAttributesFile (NTDLL.@)
1589 * ZwQueryAttributesFile (NTDLL.@)
1591 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1593 FILE_NETWORK_OPEN_INFORMATION full_info;
1594 NTSTATUS status;
1596 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1598 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1599 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1600 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1601 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1602 info->FileAttributes = full_info.FileAttributes;
1604 return status;
1608 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1609 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1610 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1611 size_t fstypesize, unsigned int flags )
1613 if (!strncmp("cd9660", fstypename, fstypesize) ||
1614 !strncmp("udf", fstypename, fstypesize))
1616 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1617 /* Don't assume read-only, let the mount options set it below */
1618 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1620 else if (!strncmp("nfs", fstypename, fstypesize) ||
1621 !strncmp("nwfs", fstypename, fstypesize) ||
1622 !strncmp("smbfs", fstypename, fstypesize) ||
1623 !strncmp("afpfs", fstypename, fstypesize))
1625 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1626 info->Characteristics |= FILE_REMOTE_DEVICE;
1628 else if (!strncmp("procfs", fstypename, fstypesize))
1629 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1630 else
1631 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1633 if (flags & MNT_RDONLY)
1634 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1636 if (!(flags & MNT_LOCAL))
1638 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1639 info->Characteristics |= FILE_REMOTE_DEVICE;
1642 #endif
1644 /******************************************************************************
1645 * get_device_info
1647 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1649 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1651 struct stat st;
1653 info->Characteristics = 0;
1654 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1655 if (S_ISCHR( st.st_mode ))
1657 info->DeviceType = FILE_DEVICE_UNKNOWN;
1658 #ifdef linux
1659 switch(major(st.st_rdev))
1661 case MEM_MAJOR:
1662 info->DeviceType = FILE_DEVICE_NULL;
1663 break;
1664 case TTY_MAJOR:
1665 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1666 break;
1667 case LP_MAJOR:
1668 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1669 break;
1670 case SCSI_TAPE_MAJOR:
1671 info->DeviceType = FILE_DEVICE_TAPE;
1672 break;
1674 #endif
1676 else if (S_ISBLK( st.st_mode ))
1678 info->DeviceType = FILE_DEVICE_DISK;
1680 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1682 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1684 else /* regular file or directory */
1686 #if defined(linux) && defined(HAVE_FSTATFS)
1687 struct statfs stfs;
1689 /* check for floppy disk */
1690 if (major(st.st_dev) == FLOPPY_MAJOR)
1691 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1693 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1694 switch (stfs.f_type)
1696 case 0x9660: /* iso9660 */
1697 case 0x9fa1: /* supermount */
1698 case 0x15013346: /* udf */
1699 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1700 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1701 break;
1702 case 0x6969: /* nfs */
1703 case 0x517B: /* smbfs */
1704 case 0x564c: /* ncpfs */
1705 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1706 info->Characteristics |= FILE_REMOTE_DEVICE;
1707 break;
1708 case 0x01021994: /* tmpfs */
1709 case 0x28cd3d45: /* cramfs */
1710 case 0x1373: /* devfs */
1711 case 0x9fa0: /* procfs */
1712 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1713 break;
1714 default:
1715 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1716 break;
1718 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1719 struct statfs stfs;
1721 if (fstatfs( fd, &stfs ) < 0)
1722 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1723 else
1724 get_device_info_fstatfs( info, stfs.f_fstypename,
1725 sizeof(stfs.f_fstypename), stfs.f_flags );
1726 #elif defined(__NetBSD__)
1727 struct statvfs stfs;
1729 if (fstatvfs( fd, &stfs) < 0)
1730 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1731 else
1732 get_device_info_fstatfs( info, stfs.f_fstypename,
1733 sizeof(stfs.f_fstypename), stfs.f_flag );
1734 #elif defined(sun)
1735 /* Use dkio to work out device types */
1737 # include <sys/dkio.h>
1738 # include <sys/vtoc.h>
1739 struct dk_cinfo dkinf;
1740 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1741 if(retval==-1){
1742 WARN("Unable to get disk device type information - assuming a disk like device\n");
1743 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1745 switch (dkinf.dki_ctype)
1747 case DKC_CDROM:
1748 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1749 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1750 break;
1751 case DKC_NCRFLOPPY:
1752 case DKC_SMSFLOPPY:
1753 case DKC_INTEL82072:
1754 case DKC_INTEL82077:
1755 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1756 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1757 break;
1758 case DKC_MD:
1759 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1760 break;
1761 default:
1762 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1765 #else
1766 static int warned;
1767 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1768 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1769 #endif
1770 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1772 return STATUS_SUCCESS;
1776 /******************************************************************************
1777 * NtQueryVolumeInformationFile [NTDLL.@]
1778 * ZwQueryVolumeInformationFile [NTDLL.@]
1780 * Get volume information for an open file handle.
1782 * PARAMS
1783 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1784 * io [O] Receives information about the operation on return
1785 * buffer [O] Destination for volume information
1786 * length [I] Size of FsInformation
1787 * info_class [I] Type of volume information to set
1789 * RETURNS
1790 * Success: 0. io and buffer are updated.
1791 * Failure: An NTSTATUS error code describing the error.
1793 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1794 PVOID buffer, ULONG length,
1795 FS_INFORMATION_CLASS info_class )
1797 int fd, needs_close;
1798 struct stat st;
1800 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1801 return io->u.Status;
1803 io->u.Status = STATUS_NOT_IMPLEMENTED;
1804 io->Information = 0;
1806 switch( info_class )
1808 case FileFsVolumeInformation:
1809 FIXME( "%p: volume info not supported\n", handle );
1810 break;
1811 case FileFsLabelInformation:
1812 FIXME( "%p: label info not supported\n", handle );
1813 break;
1814 case FileFsSizeInformation:
1815 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1816 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1817 else
1819 FILE_FS_SIZE_INFORMATION *info = buffer;
1821 if (fstat( fd, &st ) < 0)
1823 io->u.Status = FILE_GetNtStatus();
1824 break;
1826 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1828 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1830 else
1832 /* Linux's fstatvfs is buggy */
1833 #if !defined(linux) || !defined(HAVE_FSTATFS)
1834 struct statvfs stfs;
1836 if (fstatvfs( fd, &stfs ) < 0)
1838 io->u.Status = FILE_GetNtStatus();
1839 break;
1841 info->BytesPerSector = stfs.f_frsize;
1842 #else
1843 struct statfs stfs;
1844 if (fstatfs( fd, &stfs ) < 0)
1846 io->u.Status = FILE_GetNtStatus();
1847 break;
1849 info->BytesPerSector = stfs.f_bsize;
1850 #endif
1851 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1852 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1853 info->SectorsPerAllocationUnit = 1;
1854 io->Information = sizeof(*info);
1855 io->u.Status = STATUS_SUCCESS;
1858 break;
1859 case FileFsDeviceInformation:
1860 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1861 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1862 else
1864 FILE_FS_DEVICE_INFORMATION *info = buffer;
1866 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
1867 io->Information = sizeof(*info);
1869 break;
1870 case FileFsAttributeInformation:
1871 FIXME( "%p: attribute info not supported\n", handle );
1872 break;
1873 case FileFsControlInformation:
1874 FIXME( "%p: control info not supported\n", handle );
1875 break;
1876 case FileFsFullSizeInformation:
1877 FIXME( "%p: full size info not supported\n", handle );
1878 break;
1879 case FileFsObjectIdInformation:
1880 FIXME( "%p: object id info not supported\n", handle );
1881 break;
1882 case FileFsMaximumInformation:
1883 FIXME( "%p: maximum info not supported\n", handle );
1884 break;
1885 default:
1886 io->u.Status = STATUS_INVALID_PARAMETER;
1887 break;
1889 if (needs_close) close( fd );
1890 return io->u.Status;
1894 /******************************************************************
1895 * NtFlushBuffersFile (NTDLL.@)
1897 * Flush any buffered data on an open file handle.
1899 * PARAMS
1900 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1901 * IoStatusBlock [O] Receives information about the operation on return
1903 * RETURNS
1904 * Success: 0. IoStatusBlock is updated.
1905 * Failure: An NTSTATUS error code describing the error.
1907 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1909 NTSTATUS ret;
1910 HANDLE hEvent = NULL;
1912 SERVER_START_REQ( flush_file )
1914 req->handle = hFile;
1915 ret = wine_server_call( req );
1916 hEvent = reply->event;
1918 SERVER_END_REQ;
1919 if (!ret && hEvent)
1921 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1922 NtClose( hEvent );
1924 return ret;
1927 /******************************************************************
1928 * NtLockFile (NTDLL.@)
1932 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1933 PIO_APC_ROUTINE apc, void* apc_user,
1934 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1935 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1936 BOOLEAN exclusive )
1938 NTSTATUS ret;
1939 HANDLE handle;
1940 BOOLEAN async;
1942 if (apc || io_status || key)
1944 FIXME("Unimplemented yet parameter\n");
1945 return STATUS_NOT_IMPLEMENTED;
1948 for (;;)
1950 SERVER_START_REQ( lock_file )
1952 req->handle = hFile;
1953 req->offset_low = offset->u.LowPart;
1954 req->offset_high = offset->u.HighPart;
1955 req->count_low = count->u.LowPart;
1956 req->count_high = count->u.HighPart;
1957 req->shared = !exclusive;
1958 req->wait = !dont_wait;
1959 ret = wine_server_call( req );
1960 handle = reply->handle;
1961 async = reply->overlapped;
1963 SERVER_END_REQ;
1964 if (ret != STATUS_PENDING)
1966 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1967 return ret;
1970 if (async)
1972 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1973 if (handle) NtClose( handle );
1974 return STATUS_PENDING;
1976 if (handle)
1978 NtWaitForSingleObject( handle, FALSE, NULL );
1979 NtClose( handle );
1981 else
1983 LARGE_INTEGER time;
1985 /* Unix lock conflict, sleep a bit and retry */
1986 time.QuadPart = 100 * (ULONGLONG)10000;
1987 time.QuadPart = -time.QuadPart;
1988 NtDelayExecution( FALSE, &time );
1994 /******************************************************************
1995 * NtUnlockFile (NTDLL.@)
1999 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2000 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2001 PULONG key )
2003 NTSTATUS status;
2005 TRACE( "%p %x%08x %x%08x\n",
2006 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2008 if (io_status || key)
2010 FIXME("Unimplemented yet parameter\n");
2011 return STATUS_NOT_IMPLEMENTED;
2014 SERVER_START_REQ( unlock_file )
2016 req->handle = hFile;
2017 req->offset_low = offset->u.LowPart;
2018 req->offset_high = offset->u.HighPart;
2019 req->count_low = count->u.LowPart;
2020 req->count_high = count->u.HighPart;
2021 status = wine_server_call( req );
2023 SERVER_END_REQ;
2024 return status;
2027 /******************************************************************
2028 * NtCreateNamedPipeFile (NTDLL.@)
2032 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2033 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2034 ULONG sharing, ULONG dispo, ULONG options,
2035 ULONG pipe_type, ULONG read_mode,
2036 ULONG completion_mode, ULONG max_inst,
2037 ULONG inbound_quota, ULONG outbound_quota,
2038 PLARGE_INTEGER timeout)
2040 NTSTATUS status;
2042 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2043 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2044 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2045 outbound_quota, timeout);
2047 /* assume we only get relative timeout, and storable in a DWORD as ms */
2048 if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
2049 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2051 SERVER_START_REQ( create_named_pipe )
2053 req->access = access;
2054 req->attributes = (attr) ? attr->Attributes : 0;
2055 req->rootdir = attr ? attr->RootDirectory : 0;
2056 req->options = options;
2057 req->flags =
2058 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2059 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2060 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2061 req->maxinstances = max_inst;
2062 req->outsize = outbound_quota;
2063 req->insize = inbound_quota;
2064 req->timeout = timeout->QuadPart / -10000;
2065 wine_server_add_data( req, attr->ObjectName->Buffer,
2066 attr->ObjectName->Length );
2067 status = wine_server_call( req );
2068 if (!status) *handle = reply->handle;
2070 SERVER_END_REQ;
2071 return status;
2074 /******************************************************************
2075 * NtDeleteFile (NTDLL.@)
2079 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2081 NTSTATUS status;
2082 HANDLE hFile;
2083 IO_STATUS_BLOCK io;
2085 TRACE("%p\n", ObjectAttributes);
2086 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2087 ObjectAttributes, &io, NULL, 0,
2088 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2089 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2090 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2091 return status;
2094 /******************************************************************
2095 * NtCancelIoFile (NTDLL.@)
2099 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2101 LARGE_INTEGER timeout;
2103 TRACE("%p %p\n", hFile, io_status );
2105 SERVER_START_REQ( cancel_async )
2107 req->handle = hFile;
2108 wine_server_call( req );
2110 SERVER_END_REQ;
2111 /* Let some APC be run, so that we can run the remaining APCs on hFile
2112 * either the cancelation of the pending one, but also the execution
2113 * of the queued APC, but not yet run. This is needed to ensure proper
2114 * clean-up of allocated data.
2116 timeout.u.LowPart = timeout.u.HighPart = 0;
2117 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2120 /******************************************************************************
2121 * NtCreateMailslotFile [NTDLL.@]
2122 * ZwCreateMailslotFile [NTDLL.@]
2124 * PARAMS
2125 * pHandle [O] pointer to receive the handle created
2126 * DesiredAccess [I] access mode (read, write, etc)
2127 * ObjectAttributes [I] fully qualified NT path of the mailslot
2128 * IoStatusBlock [O] receives completion status and other info
2129 * CreateOptions [I]
2130 * MailslotQuota [I]
2131 * MaxMessageSize [I]
2132 * TimeOut [I]
2134 * RETURNS
2135 * An NT status code
2137 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2138 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2139 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2140 PLARGE_INTEGER TimeOut)
2142 LARGE_INTEGER timeout;
2143 NTSTATUS ret;
2145 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2146 pHandle, DesiredAccess, attr, IoStatusBlock,
2147 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2149 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2150 if (!attr) return STATUS_INVALID_PARAMETER;
2151 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2154 * For a NULL TimeOut pointer set the default timeout value
2156 if (!TimeOut)
2157 timeout.QuadPart = -1;
2158 else
2159 timeout.QuadPart = TimeOut->QuadPart;
2161 SERVER_START_REQ( create_mailslot )
2163 req->access = DesiredAccess;
2164 req->attributes = attr->Attributes;
2165 req->rootdir = attr->RootDirectory;
2166 req->max_msgsize = MaxMessageSize;
2167 req->read_timeout = (timeout.QuadPart <= 0) ? timeout.QuadPart / -10000 : -1;
2168 wine_server_add_data( req, attr->ObjectName->Buffer,
2169 attr->ObjectName->Length );
2170 ret = wine_server_call( req );
2171 if( ret == STATUS_SUCCESS )
2172 *pHandle = reply->handle;
2174 SERVER_END_REQ;
2176 return ret;