ntdll: Remove an unnecessary NULL pointer check.
[wine/multimedia.git] / dlls / ntdll / file.c
blob56c26c21ffc1d9ba88a58f2ee7dd9141119bd65a
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_SYS_FILIO_H
49 # include <sys/filio.h>
50 #endif
51 #ifdef HAVE_POLL_H
52 #include <poll.h>
53 #endif
54 #ifdef HAVE_SYS_POLL_H
55 #include <sys/poll.h>
56 #endif
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef HAVE_UTIME_H
61 # include <utime.h>
62 #endif
63 #ifdef HAVE_SYS_VFS_H
64 # include <sys/vfs.h>
65 #endif
66 #ifdef HAVE_SYS_MOUNT_H
67 # include <sys/mount.h>
68 #endif
69 #ifdef HAVE_SYS_STATFS_H
70 # include <sys/statfs.h>
71 #endif
73 #define NONAMELESSUNION
74 #define NONAMELESSSTRUCT
75 #include "ntstatus.h"
76 #define WIN32_NO_STATUS
77 #include "wine/unicode.h"
78 #include "wine/debug.h"
79 #include "wine/server.h"
80 #include "ntdll_misc.h"
82 #include "winternl.h"
83 #include "winioctl.h"
84 #include "ddk/ntddser.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
88 mode_t FILE_umask = 0;
90 #define SECSPERDAY 86400
91 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
94 /**************************************************************************
95 * FILE_CreateFile (internal)
96 * Open a file.
98 * Parameter set fully identical with NtCreateFile
100 static NTSTATUS FILE_CreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
101 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
102 ULONG attributes, ULONG sharing, ULONG disposition,
103 ULONG options, PVOID ea_buffer, ULONG ea_length )
105 ANSI_STRING unix_name;
106 int created = FALSE;
108 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p\n"
109 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
110 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
111 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
112 attributes, sharing, disposition, options, ea_buffer, ea_length );
114 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
116 if (alloc_size) FIXME( "alloc_size not supported\n" );
118 if ((io->u.Status = nt_to_unix_file_name_attr( attr, &unix_name, disposition )) == STATUS_BAD_DEVICE_TYPE)
120 SERVER_START_REQ( open_file_object )
122 req->access = access;
123 req->attributes = attr->Attributes;
124 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
125 req->sharing = sharing;
126 req->options = options;
127 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
128 io->u.Status = wine_server_call( req );
129 *handle = wine_server_ptr_handle( reply->handle );
131 SERVER_END_REQ;
132 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
133 return io->u.Status;
136 if (io->u.Status == STATUS_NO_SUCH_FILE &&
137 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
139 created = TRUE;
140 io->u.Status = STATUS_SUCCESS;
143 if (io->u.Status == STATUS_SUCCESS)
145 struct security_descriptor *sd;
146 struct object_attributes objattr;
148 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
149 objattr.name_len = 0;
150 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
151 if (io->u.Status != STATUS_SUCCESS)
153 RtlFreeAnsiString( &unix_name );
154 return io->u.Status;
157 SERVER_START_REQ( create_file )
159 req->access = access;
160 req->attributes = attr->Attributes;
161 req->sharing = sharing;
162 req->create = disposition;
163 req->options = options;
164 req->attrs = attributes;
165 wine_server_add_data( req, &objattr, sizeof(objattr) );
166 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
167 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
168 io->u.Status = wine_server_call( req );
169 *handle = wine_server_ptr_handle( reply->handle );
171 SERVER_END_REQ;
172 NTDLL_free_struct_sd( sd );
173 RtlFreeAnsiString( &unix_name );
175 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
177 if (io->u.Status == STATUS_SUCCESS)
179 if (created) io->Information = FILE_CREATED;
180 else switch(disposition)
182 case FILE_SUPERSEDE:
183 io->Information = FILE_SUPERSEDED;
184 break;
185 case FILE_CREATE:
186 io->Information = FILE_CREATED;
187 break;
188 case FILE_OPEN:
189 case FILE_OPEN_IF:
190 io->Information = FILE_OPENED;
191 break;
192 case FILE_OVERWRITE:
193 case FILE_OVERWRITE_IF:
194 io->Information = FILE_OVERWRITTEN;
195 break;
199 return io->u.Status;
202 /**************************************************************************
203 * NtOpenFile [NTDLL.@]
204 * ZwOpenFile [NTDLL.@]
206 * Open a file.
208 * PARAMS
209 * handle [O] Variable that receives the file handle on return
210 * access [I] Access desired by the caller to the file
211 * attr [I] Structure describing the file to be opened
212 * io [O] Receives details about the result of the operation
213 * sharing [I] Type of shared access the caller requires
214 * options [I] Options for the file open
216 * RETURNS
217 * Success: 0. FileHandle and IoStatusBlock are updated.
218 * Failure: An NTSTATUS error code describing the error.
220 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
221 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
222 ULONG sharing, ULONG options )
224 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
225 sharing, FILE_OPEN, options, NULL, 0 );
228 /**************************************************************************
229 * NtCreateFile [NTDLL.@]
230 * ZwCreateFile [NTDLL.@]
232 * Either create a new file or directory, or open an existing file, device,
233 * directory or volume.
235 * PARAMS
236 * handle [O] Points to a variable which receives the file handle on return
237 * access [I] Desired access to the file
238 * attr [I] Structure describing the file
239 * io [O] Receives information about the operation on return
240 * alloc_size [I] Initial size of the file in bytes
241 * attributes [I] Attributes to create the file with
242 * sharing [I] Type of shared access the caller would like to the file
243 * disposition [I] Specifies what to do, depending on whether the file already exists
244 * options [I] Options for creating a new file
245 * ea_buffer [I] Pointer to an extended attributes buffer
246 * ea_length [I] Length of ea_buffer
248 * RETURNS
249 * Success: 0. handle and io are updated.
250 * Failure: An NTSTATUS error code describing the error.
252 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
253 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
254 ULONG attributes, ULONG sharing, ULONG disposition,
255 ULONG options, PVOID ea_buffer, ULONG ea_length )
257 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
258 sharing, disposition, options, ea_buffer, ea_length );
261 /***********************************************************************
262 * Asynchronous file I/O *
265 struct async_fileio
267 HANDLE handle;
268 PIO_APC_ROUTINE apc;
269 void *apc_arg;
272 typedef struct
274 struct async_fileio io;
275 char* buffer;
276 unsigned int already;
277 unsigned int count;
278 BOOL avail_mode;
279 } async_fileio_read;
281 typedef struct
283 struct async_fileio io;
284 const char *buffer;
285 unsigned int already;
286 unsigned int count;
287 } async_fileio_write;
290 /* callback for file I/O user APC */
291 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
293 struct async_fileio *async = arg;
294 if (async->apc) async->apc( async->apc_arg, io, reserved );
295 RtlFreeHeap( GetProcessHeap(), 0, async );
298 /***********************************************************************
299 * FILE_GetNtStatus(void)
301 * Retrieve the Nt Status code from errno.
302 * Try to be consistent with FILE_SetDosError().
304 NTSTATUS FILE_GetNtStatus(void)
306 int err = errno;
308 TRACE( "errno = %d\n", errno );
309 switch (err)
311 case EAGAIN: return STATUS_SHARING_VIOLATION;
312 case EBADF: return STATUS_INVALID_HANDLE;
313 case EBUSY: return STATUS_DEVICE_BUSY;
314 case ENOSPC: return STATUS_DISK_FULL;
315 case EPERM:
316 case EROFS:
317 case EACCES: return STATUS_ACCESS_DENIED;
318 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
319 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
320 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
321 case EMFILE:
322 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
323 case EINVAL: return STATUS_INVALID_PARAMETER;
324 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
325 case EPIPE: return STATUS_PIPE_DISCONNECTED;
326 case EIO: return STATUS_DEVICE_NOT_READY;
327 #ifdef ENOMEDIUM
328 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
329 #endif
330 case ENXIO: return STATUS_NO_SUCH_DEVICE;
331 case ENOTTY:
332 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
333 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
334 case EFAULT: return STATUS_ACCESS_VIOLATION;
335 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
336 case ENOEXEC: /* ?? */
337 case EEXIST: /* ?? */
338 default:
339 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
340 return STATUS_UNSUCCESSFUL;
344 /***********************************************************************
345 * FILE_AsyncReadService (INTERNAL)
347 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc)
349 async_fileio_read *fileio = user;
350 int fd, needs_close, result;
352 switch (status)
354 case STATUS_ALERTED: /* got some new data */
355 /* check to see if the data is ready (non-blocking) */
356 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
357 &needs_close, NULL, NULL )))
358 break;
360 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
361 if (needs_close) close( fd );
363 if (result < 0)
365 if (errno == EAGAIN || errno == EINTR)
366 status = STATUS_PENDING;
367 else /* check to see if the transfer is complete */
368 status = FILE_GetNtStatus();
370 else if (result == 0)
372 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
374 else
376 fileio->already += result;
377 if (fileio->already >= fileio->count || fileio->avail_mode)
378 status = STATUS_SUCCESS;
379 else
381 /* if we only have to read the available data, and none is available,
382 * simply cancel the request. If data was available, it has been read
383 * while in by previous call (NtDelayExecution)
385 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
388 break;
390 case STATUS_TIMEOUT:
391 case STATUS_IO_TIMEOUT:
392 if (fileio->already) status = STATUS_SUCCESS;
393 break;
395 if (status != STATUS_PENDING)
397 iosb->u.Status = status;
398 iosb->Information = fileio->already;
399 *apc = fileio_apc;
401 return status;
404 struct io_timeouts
406 int interval; /* max interval between two bytes */
407 int total; /* total timeout for the whole operation */
408 int end_time; /* absolute time of end of operation */
411 /* retrieve the I/O timeouts to use for a given handle */
412 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
413 struct io_timeouts *timeouts )
415 NTSTATUS status = STATUS_SUCCESS;
417 timeouts->interval = timeouts->total = -1;
419 switch(type)
421 case FD_TYPE_SERIAL:
423 /* GetCommTimeouts */
424 SERIAL_TIMEOUTS st;
425 IO_STATUS_BLOCK io;
427 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
428 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
429 if (status) break;
431 if (is_read)
433 if (st.ReadIntervalTimeout)
434 timeouts->interval = st.ReadIntervalTimeout;
436 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
438 timeouts->total = st.ReadTotalTimeoutConstant;
439 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
440 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
442 else if (st.ReadIntervalTimeout == MAXDWORD)
443 timeouts->interval = timeouts->total = 0;
445 else /* write */
447 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
449 timeouts->total = st.WriteTotalTimeoutConstant;
450 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
451 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
455 break;
456 case FD_TYPE_MAILSLOT:
457 if (is_read)
459 timeouts->interval = 0; /* return as soon as we got something */
460 SERVER_START_REQ( set_mailslot_info )
462 req->handle = wine_server_obj_handle( handle );
463 req->flags = 0;
464 if (!(status = wine_server_call( req )) &&
465 reply->read_timeout != TIMEOUT_INFINITE)
466 timeouts->total = reply->read_timeout / -10000;
468 SERVER_END_REQ;
470 break;
471 case FD_TYPE_SOCKET:
472 case FD_TYPE_PIPE:
473 case FD_TYPE_CHAR:
474 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
475 break;
476 default:
477 break;
479 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
480 return STATUS_SUCCESS;
484 /* retrieve the timeout for the next wait, in milliseconds */
485 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
487 int ret = -1;
489 if (timeouts->total != -1)
491 ret = timeouts->end_time - NtGetTickCount();
492 if (ret < 0) ret = 0;
494 if (already && timeouts->interval != -1)
496 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
498 return ret;
502 /* retrieve the avail_mode flag for async reads */
503 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
505 NTSTATUS status = STATUS_SUCCESS;
507 switch(type)
509 case FD_TYPE_SERIAL:
511 /* GetCommTimeouts */
512 SERIAL_TIMEOUTS st;
513 IO_STATUS_BLOCK io;
515 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
516 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
517 if (status) break;
518 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
519 !st.ReadTotalTimeoutConstant &&
520 st.ReadIntervalTimeout == MAXDWORD);
522 break;
523 case FD_TYPE_MAILSLOT:
524 case FD_TYPE_SOCKET:
525 case FD_TYPE_PIPE:
526 case FD_TYPE_CHAR:
527 *avail_mode = TRUE;
528 break;
529 default:
530 *avail_mode = FALSE;
531 break;
533 return status;
537 /******************************************************************************
538 * NtReadFile [NTDLL.@]
539 * ZwReadFile [NTDLL.@]
541 * Read from an open file handle.
543 * PARAMS
544 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
545 * Event [I] Event to signal upon completion (or NULL)
546 * ApcRoutine [I] Callback to call upon completion (or NULL)
547 * ApcContext [I] Context for ApcRoutine (or NULL)
548 * IoStatusBlock [O] Receives information about the operation on return
549 * Buffer [O] Destination for the data read
550 * Length [I] Size of Buffer
551 * ByteOffset [O] Destination for the new file pointer position (or NULL)
552 * Key [O] Function unknown (may be NULL)
554 * RETURNS
555 * Success: 0. IoStatusBlock is updated, and the Information member contains
556 * The number of bytes read.
557 * Failure: An NTSTATUS error code describing the error.
559 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
560 PIO_APC_ROUTINE apc, void* apc_user,
561 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
562 PLARGE_INTEGER offset, PULONG key)
564 int result, unix_handle, needs_close, timeout_init_done = 0;
565 unsigned int options;
566 struct io_timeouts timeouts;
567 NTSTATUS status;
568 ULONG total = 0;
569 enum server_fd_type type;
570 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
572 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
573 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
575 if (!io_status) return STATUS_ACCESS_VIOLATION;
577 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
578 &needs_close, &type, &options );
579 if (status) return status;
581 if (!virtual_check_buffer_for_write( buffer, length ))
583 status = STATUS_ACCESS_VIOLATION;
584 goto done;
587 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
589 /* async I/O doesn't make sense on regular files */
590 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
592 if (errno != EINTR)
594 status = FILE_GetNtStatus();
595 goto done;
598 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
599 /* update file pointer position */
600 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
602 total = result;
603 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
604 goto done;
607 for (;;)
609 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
611 total += result;
612 if (!result || total == length)
614 if (total)
616 status = STATUS_SUCCESS;
617 goto done;
619 switch (type)
621 case FD_TYPE_FILE:
622 case FD_TYPE_CHAR:
623 status = STATUS_END_OF_FILE;
624 goto done;
625 case FD_TYPE_SERIAL:
626 break;
627 default:
628 status = STATUS_PIPE_BROKEN;
629 goto done;
632 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
634 else if (errno != EAGAIN)
636 if (errno == EINTR) continue;
637 if (!total) status = FILE_GetNtStatus();
638 goto done;
641 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
643 async_fileio_read *fileio;
644 BOOL avail_mode;
646 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
647 goto err;
648 if (total && avail_mode)
650 status = STATUS_SUCCESS;
651 goto done;
654 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
656 status = STATUS_NO_MEMORY;
657 goto err;
659 fileio->io.handle = hFile;
660 fileio->io.apc = apc;
661 fileio->io.apc_arg = apc_user;
662 fileio->already = total;
663 fileio->count = length;
664 fileio->buffer = buffer;
665 fileio->avail_mode = avail_mode;
667 SERVER_START_REQ( register_async )
669 req->type = ASYNC_TYPE_READ;
670 req->count = length;
671 req->async.handle = wine_server_obj_handle( hFile );
672 req->async.event = wine_server_obj_handle( hEvent );
673 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
674 req->async.iosb = wine_server_client_ptr( io_status );
675 req->async.arg = wine_server_client_ptr( fileio );
676 req->async.cvalue = cvalue;
677 status = wine_server_call( req );
679 SERVER_END_REQ;
681 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
682 goto err;
684 else /* synchronous read, wait for the fd to become ready */
686 struct pollfd pfd;
687 int ret, timeout;
689 if (!timeout_init_done)
691 timeout_init_done = 1;
692 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
693 goto err;
694 if (hEvent) NtResetEvent( hEvent, NULL );
696 timeout = get_next_io_timeout( &timeouts, total );
698 pfd.fd = unix_handle;
699 pfd.events = POLLIN;
701 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
703 if (total) /* return with what we got so far */
704 status = STATUS_SUCCESS;
705 else
706 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
707 goto done;
709 if (ret == -1 && errno != EINTR)
711 status = FILE_GetNtStatus();
712 goto done;
714 /* will now restart the read */
718 done:
719 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
721 err:
722 if (needs_close) close( unix_handle );
723 if (status == STATUS_SUCCESS)
725 io_status->u.Status = status;
726 io_status->Information = total;
727 TRACE("= SUCCESS (%u)\n", total);
728 if (hEvent) NtSetEvent( hEvent, NULL );
729 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
730 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
732 else
734 TRACE("= 0x%08x\n", status);
735 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
737 return status;
741 /******************************************************************************
742 * NtReadFileScatter [NTDLL.@]
743 * ZwReadFileScatter [NTDLL.@]
745 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
746 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
747 ULONG length, PLARGE_INTEGER offset, PULONG key )
749 size_t page_size = getpagesize();
750 int result, unix_handle, needs_close;
751 unsigned int options;
752 NTSTATUS status;
753 ULONG pos = 0, total = 0;
754 enum server_fd_type type;
755 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
757 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
758 file, event, apc, apc_user, io_status, segments, length, offset, key);
760 if (length % page_size) return STATUS_INVALID_PARAMETER;
761 if (!io_status) return STATUS_ACCESS_VIOLATION;
763 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
764 &needs_close, &type, &options );
765 if (status) return status;
767 if ((type != FD_TYPE_FILE) ||
768 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
769 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
771 status = STATUS_INVALID_PARAMETER;
772 goto error;
775 while (length)
777 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
778 result = pread( unix_handle, (char *)segments->Buffer + pos,
779 page_size - pos, offset->QuadPart + total );
780 else
781 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
783 if (result == -1)
785 if (errno == EINTR) continue;
786 status = FILE_GetNtStatus();
787 break;
789 if (!result)
791 status = STATUS_END_OF_FILE;
792 break;
794 total += result;
795 length -= result;
796 if ((pos += result) == page_size)
798 pos = 0;
799 segments++;
803 if (cvalue) NTDLL_AddCompletion( file, cvalue, status, total );
805 error:
806 if (needs_close) close( unix_handle );
807 if (status == STATUS_SUCCESS)
809 io_status->u.Status = status;
810 io_status->Information = total;
811 TRACE("= SUCCESS (%u)\n", total);
812 if (event) NtSetEvent( event, NULL );
813 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
814 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
816 else
818 TRACE("= 0x%08x\n", status);
819 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
821 return status;
825 /***********************************************************************
826 * FILE_AsyncWriteService (INTERNAL)
828 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status, void **apc)
830 async_fileio_write *fileio = user;
831 int result, fd, needs_close;
832 enum server_fd_type type;
834 switch (status)
836 case STATUS_ALERTED:
837 /* write some data (non-blocking) */
838 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
839 &needs_close, &type, NULL )))
840 break;
842 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
843 result = send( fd, fileio->buffer, 0, 0 );
844 else
845 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
847 if (needs_close) close( fd );
849 if (result < 0)
851 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
852 else status = FILE_GetNtStatus();
854 else
856 fileio->already += result;
857 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
859 break;
861 case STATUS_TIMEOUT:
862 case STATUS_IO_TIMEOUT:
863 if (fileio->already) status = STATUS_SUCCESS;
864 break;
866 if (status != STATUS_PENDING)
868 iosb->u.Status = status;
869 iosb->Information = fileio->already;
870 *apc = fileio_apc;
872 return status;
875 /******************************************************************************
876 * NtWriteFile [NTDLL.@]
877 * ZwWriteFile [NTDLL.@]
879 * Write to an open file handle.
881 * PARAMS
882 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
883 * Event [I] Event to signal upon completion (or NULL)
884 * ApcRoutine [I] Callback to call upon completion (or NULL)
885 * ApcContext [I] Context for ApcRoutine (or NULL)
886 * IoStatusBlock [O] Receives information about the operation on return
887 * Buffer [I] Source for the data to write
888 * Length [I] Size of Buffer
889 * ByteOffset [O] Destination for the new file pointer position (or NULL)
890 * Key [O] Function unknown (may be NULL)
892 * RETURNS
893 * Success: 0. IoStatusBlock is updated, and the Information member contains
894 * The number of bytes written.
895 * Failure: An NTSTATUS error code describing the error.
897 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
898 PIO_APC_ROUTINE apc, void* apc_user,
899 PIO_STATUS_BLOCK io_status,
900 const void* buffer, ULONG length,
901 PLARGE_INTEGER offset, PULONG key)
903 int result, unix_handle, needs_close, timeout_init_done = 0;
904 unsigned int options;
905 struct io_timeouts timeouts;
906 NTSTATUS status;
907 ULONG total = 0;
908 enum server_fd_type type;
909 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
911 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
912 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
914 if (!io_status) return STATUS_ACCESS_VIOLATION;
916 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
917 &needs_close, &type, &options );
918 if (status) return status;
920 if (!virtual_check_buffer_for_read( buffer, length ))
922 status = STATUS_INVALID_USER_BUFFER;
923 goto done;
926 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
928 /* async I/O doesn't make sense on regular files */
929 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
931 if (errno != EINTR)
933 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
934 else status = FILE_GetNtStatus();
935 goto done;
939 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
940 /* update file pointer position */
941 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
943 total = result;
944 status = STATUS_SUCCESS;
945 goto done;
948 for (;;)
950 /* zero-length writes on sockets may not work with plain write(2) */
951 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
952 result = send( unix_handle, buffer, 0, 0 );
953 else
954 result = write( unix_handle, (const char *)buffer + total, length - total );
956 if (result >= 0)
958 total += result;
959 if (total == length)
961 status = STATUS_SUCCESS;
962 goto done;
964 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
966 else if (errno != EAGAIN)
968 if (errno == EINTR) continue;
969 if (!total)
971 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
972 else status = FILE_GetNtStatus();
974 goto done;
977 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
979 async_fileio_write *fileio;
981 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
983 status = STATUS_NO_MEMORY;
984 goto err;
986 fileio->io.handle = hFile;
987 fileio->io.apc = apc;
988 fileio->io.apc_arg = apc_user;
989 fileio->already = total;
990 fileio->count = length;
991 fileio->buffer = buffer;
993 SERVER_START_REQ( register_async )
995 req->type = ASYNC_TYPE_WRITE;
996 req->count = length;
997 req->async.handle = wine_server_obj_handle( hFile );
998 req->async.event = wine_server_obj_handle( hEvent );
999 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1000 req->async.iosb = wine_server_client_ptr( io_status );
1001 req->async.arg = wine_server_client_ptr( fileio );
1002 req->async.cvalue = cvalue;
1003 status = wine_server_call( req );
1005 SERVER_END_REQ;
1007 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1008 goto err;
1010 else /* synchronous write, wait for the fd to become ready */
1012 struct pollfd pfd;
1013 int ret, timeout;
1015 if (!timeout_init_done)
1017 timeout_init_done = 1;
1018 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1019 goto err;
1020 if (hEvent) NtResetEvent( hEvent, NULL );
1022 timeout = get_next_io_timeout( &timeouts, total );
1024 pfd.fd = unix_handle;
1025 pfd.events = POLLOUT;
1027 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1029 /* return with what we got so far */
1030 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1031 goto done;
1033 if (ret == -1 && errno != EINTR)
1035 status = FILE_GetNtStatus();
1036 goto done;
1038 /* will now restart the write */
1042 done:
1043 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
1045 err:
1046 if (needs_close) close( unix_handle );
1047 if (status == STATUS_SUCCESS)
1049 io_status->u.Status = status;
1050 io_status->Information = total;
1051 TRACE("= SUCCESS (%u)\n", total);
1052 if (hEvent) NtSetEvent( hEvent, NULL );
1053 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1054 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1056 else
1058 TRACE("= 0x%08x\n", status);
1059 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1061 return status;
1065 /******************************************************************************
1066 * NtWriteFileGather [NTDLL.@]
1067 * ZwWriteFileGather [NTDLL.@]
1069 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1070 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1071 ULONG length, PLARGE_INTEGER offset, PULONG key )
1073 size_t page_size = getpagesize();
1074 int result, unix_handle, needs_close;
1075 unsigned int options;
1076 NTSTATUS status;
1077 ULONG pos = 0, total = 0;
1078 enum server_fd_type type;
1079 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1081 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1082 file, event, apc, apc_user, io_status, segments, length, offset, key);
1084 if (length % page_size) return STATUS_INVALID_PARAMETER;
1085 if (!io_status) return STATUS_ACCESS_VIOLATION;
1087 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1088 &needs_close, &type, &options );
1089 if (status) return status;
1091 if ((type != FD_TYPE_FILE) ||
1092 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1093 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1095 status = STATUS_INVALID_PARAMETER;
1096 goto error;
1099 while (length)
1101 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
1102 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1103 page_size - pos, offset->QuadPart + total );
1104 else
1105 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1107 if (result == -1)
1109 if (errno == EINTR) continue;
1110 if (errno == EFAULT)
1112 status = STATUS_INVALID_USER_BUFFER;
1113 goto error;
1115 status = FILE_GetNtStatus();
1116 break;
1118 if (!result)
1120 status = STATUS_DISK_FULL;
1121 break;
1123 total += result;
1124 length -= result;
1125 if ((pos += result) == page_size)
1127 pos = 0;
1128 segments++;
1132 if (cvalue) NTDLL_AddCompletion( file, cvalue, status, total );
1134 error:
1135 if (needs_close) close( unix_handle );
1136 if (status == STATUS_SUCCESS)
1138 io_status->u.Status = status;
1139 io_status->Information = total;
1140 TRACE("= SUCCESS (%u)\n", total);
1141 if (event) NtSetEvent( event, NULL );
1142 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1143 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1145 else
1147 TRACE("= 0x%08x\n", status);
1148 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1150 return status;
1154 struct async_ioctl
1156 HANDLE handle; /* handle to the device */
1157 HANDLE event; /* async event */
1158 void *buffer; /* buffer for output */
1159 ULONG size; /* size of buffer */
1160 PIO_APC_ROUTINE apc; /* user apc params */
1161 void *apc_arg;
1164 /* callback for ioctl user APC */
1165 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
1167 struct async_ioctl *async = arg;
1168 if (async->apc) async->apc( async->apc_arg, io, reserved );
1169 RtlFreeHeap( GetProcessHeap(), 0, async );
1172 /* callback for ioctl async I/O completion */
1173 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status, void **apc )
1175 struct async_ioctl *async = arg;
1177 if (status == STATUS_ALERTED)
1179 SERVER_START_REQ( get_ioctl_result )
1181 req->handle = wine_server_obj_handle( async->handle );
1182 req->user_arg = wine_server_client_ptr( async );
1183 wine_server_set_reply( req, async->buffer, async->size );
1184 status = wine_server_call( req );
1185 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1187 SERVER_END_REQ;
1189 if (status != STATUS_PENDING)
1191 io->u.Status = status;
1192 if (async->apc || async->event) *apc = ioctl_apc;
1194 return status;
1197 /* do a ioctl call through the server */
1198 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1199 PIO_APC_ROUTINE apc, PVOID apc_context,
1200 IO_STATUS_BLOCK *io, ULONG code,
1201 const void *in_buffer, ULONG in_size,
1202 PVOID out_buffer, ULONG out_size )
1204 struct async_ioctl *async;
1205 NTSTATUS status;
1206 HANDLE wait_handle;
1207 ULONG options;
1208 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1210 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
1211 return STATUS_NO_MEMORY;
1212 async->handle = handle;
1213 async->event = event;
1214 async->buffer = out_buffer;
1215 async->size = out_size;
1216 async->apc = apc;
1217 async->apc_arg = apc_context;
1219 SERVER_START_REQ( ioctl )
1221 req->code = code;
1222 req->blocking = !apc && !event;
1223 req->async.handle = wine_server_obj_handle( handle );
1224 req->async.callback = wine_server_client_ptr( ioctl_completion );
1225 req->async.iosb = wine_server_client_ptr( io );
1226 req->async.arg = wine_server_client_ptr( async );
1227 req->async.event = wine_server_obj_handle( event );
1228 req->async.cvalue = cvalue;
1229 wine_server_add_data( req, in_buffer, in_size );
1230 wine_server_set_reply( req, out_buffer, out_size );
1231 status = wine_server_call( req );
1232 wait_handle = wine_server_ptr_handle( reply->wait );
1233 options = reply->options;
1234 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1236 SERVER_END_REQ;
1238 if (status == STATUS_NOT_SUPPORTED)
1239 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1240 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1242 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1244 if (wait_handle)
1246 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1247 status = io->u.Status;
1248 NtClose( wait_handle );
1249 RtlFreeHeap( GetProcessHeap(), 0, async );
1252 return status;
1256 /**************************************************************************
1257 * NtDeviceIoControlFile [NTDLL.@]
1258 * ZwDeviceIoControlFile [NTDLL.@]
1260 * Perform an I/O control operation on an open file handle.
1262 * PARAMS
1263 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1264 * event [I] Event to signal upon completion (or NULL)
1265 * apc [I] Callback to call upon completion (or NULL)
1266 * apc_context [I] Context for ApcRoutine (or NULL)
1267 * io [O] Receives information about the operation on return
1268 * code [I] Control code for the operation to perform
1269 * in_buffer [I] Source for any input data required (or NULL)
1270 * in_size [I] Size of InputBuffer
1271 * out_buffer [O] Source for any output data returned (or NULL)
1272 * out_size [I] Size of OutputBuffer
1274 * RETURNS
1275 * Success: 0. IoStatusBlock is updated.
1276 * Failure: An NTSTATUS error code describing the error.
1278 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1279 PIO_APC_ROUTINE apc, PVOID apc_context,
1280 PIO_STATUS_BLOCK io, ULONG code,
1281 PVOID in_buffer, ULONG in_size,
1282 PVOID out_buffer, ULONG out_size)
1284 ULONG device = (code >> 16);
1285 NTSTATUS status = STATUS_NOT_SUPPORTED;
1287 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1288 handle, event, apc, apc_context, io, code,
1289 in_buffer, in_size, out_buffer, out_size);
1291 switch(device)
1293 case FILE_DEVICE_DISK:
1294 case FILE_DEVICE_CD_ROM:
1295 case FILE_DEVICE_DVD:
1296 case FILE_DEVICE_CONTROLLER:
1297 case FILE_DEVICE_MASS_STORAGE:
1298 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1299 in_buffer, in_size, out_buffer, out_size);
1300 break;
1301 case FILE_DEVICE_SERIAL_PORT:
1302 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1303 in_buffer, in_size, out_buffer, out_size);
1304 break;
1305 case FILE_DEVICE_TAPE:
1306 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1307 in_buffer, in_size, out_buffer, out_size);
1308 break;
1311 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1312 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1313 in_buffer, in_size, out_buffer, out_size );
1315 if (status != STATUS_PENDING) io->u.Status = status;
1316 return status;
1320 /**************************************************************************
1321 * NtFsControlFile [NTDLL.@]
1322 * ZwFsControlFile [NTDLL.@]
1324 * Perform a file system control operation on an open file handle.
1326 * PARAMS
1327 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1328 * event [I] Event to signal upon completion (or NULL)
1329 * apc [I] Callback to call upon completion (or NULL)
1330 * apc_context [I] Context for ApcRoutine (or NULL)
1331 * io [O] Receives information about the operation on return
1332 * code [I] Control code for the operation to perform
1333 * in_buffer [I] Source for any input data required (or NULL)
1334 * in_size [I] Size of InputBuffer
1335 * out_buffer [O] Source for any output data returned (or NULL)
1336 * out_size [I] Size of OutputBuffer
1338 * RETURNS
1339 * Success: 0. IoStatusBlock is updated.
1340 * Failure: An NTSTATUS error code describing the error.
1342 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1343 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1344 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1346 NTSTATUS status;
1348 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1349 handle, event, apc, apc_context, io, code,
1350 in_buffer, in_size, out_buffer, out_size);
1352 if (!io) return STATUS_INVALID_PARAMETER;
1354 switch(code)
1356 case FSCTL_DISMOUNT_VOLUME:
1357 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1358 in_buffer, in_size, out_buffer, out_size );
1359 if (!status) status = DIR_unmount_device( handle );
1360 break;
1362 case FSCTL_PIPE_PEEK:
1364 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1365 int avail = 0, fd, needs_close;
1367 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1369 status = STATUS_INFO_LENGTH_MISMATCH;
1370 break;
1373 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1374 break;
1376 #ifdef FIONREAD
1377 if (ioctl( fd, FIONREAD, &avail ) != 0)
1379 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1380 if (needs_close) close( fd );
1381 status = FILE_GetNtStatus();
1382 break;
1384 #endif
1385 if (!avail) /* check for closed pipe */
1387 struct pollfd pollfd;
1388 int ret;
1390 pollfd.fd = fd;
1391 pollfd.events = POLLIN;
1392 pollfd.revents = 0;
1393 ret = poll( &pollfd, 1, 0 );
1394 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1396 if (needs_close) close( fd );
1397 status = STATUS_PIPE_BROKEN;
1398 break;
1401 buffer->NamedPipeState = 0; /* FIXME */
1402 buffer->ReadDataAvailable = avail;
1403 buffer->NumberOfMessages = 0; /* FIXME */
1404 buffer->MessageLength = 0; /* FIXME */
1405 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1406 status = STATUS_SUCCESS;
1407 if (avail)
1409 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1410 if (data_size)
1412 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1413 if (res >= 0) io->Information += res;
1416 if (needs_close) close( fd );
1418 break;
1420 case FSCTL_PIPE_DISCONNECT:
1421 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1422 in_buffer, in_size, out_buffer, out_size );
1423 if (!status)
1425 int fd = server_remove_fd_from_cache( handle );
1426 if (fd != -1) close( fd );
1428 break;
1430 case FSCTL_PIPE_IMPERSONATE:
1431 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1432 status = RtlImpersonateSelf( SecurityImpersonation );
1433 break;
1435 case FSCTL_LOCK_VOLUME:
1436 case FSCTL_UNLOCK_VOLUME:
1437 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1438 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1439 status = STATUS_SUCCESS;
1440 break;
1442 case FSCTL_PIPE_LISTEN:
1443 case FSCTL_PIPE_WAIT:
1444 default:
1445 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1446 in_buffer, in_size, out_buffer, out_size );
1447 break;
1450 if (status != STATUS_PENDING) io->u.Status = status;
1451 return status;
1454 /******************************************************************************
1455 * NtSetVolumeInformationFile [NTDLL.@]
1456 * ZwSetVolumeInformationFile [NTDLL.@]
1458 * Set volume information for an open file handle.
1460 * PARAMS
1461 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1462 * IoStatusBlock [O] Receives information about the operation on return
1463 * FsInformation [I] Source for volume information
1464 * Length [I] Size of FsInformation
1465 * FsInformationClass [I] Type of volume information to set
1467 * RETURNS
1468 * Success: 0. IoStatusBlock is updated.
1469 * Failure: An NTSTATUS error code describing the error.
1471 NTSTATUS WINAPI NtSetVolumeInformationFile(
1472 IN HANDLE FileHandle,
1473 PIO_STATUS_BLOCK IoStatusBlock,
1474 PVOID FsInformation,
1475 ULONG Length,
1476 FS_INFORMATION_CLASS FsInformationClass)
1478 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1479 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1480 return 0;
1483 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1484 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1486 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1487 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1488 RtlSecondsSince1970ToTime( st->st_atime, atime );
1489 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1490 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1491 #endif
1492 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1493 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
1494 #endif
1495 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1496 atime->QuadPart += st->st_atim.tv_nsec / 100;
1497 #endif
1498 *creation = *mtime;
1501 /* fill in the file information that depends on the stat info */
1502 NTSTATUS fill_stat_info( const struct stat *st, void *ptr, FILE_INFORMATION_CLASS class )
1504 switch (class)
1506 case FileBasicInformation:
1508 FILE_BASIC_INFORMATION *info = ptr;
1510 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1511 &info->LastAccessTime, &info->CreationTime );
1512 if (S_ISDIR(st->st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1513 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1514 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1515 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1517 break;
1518 case FileStandardInformation:
1520 FILE_STANDARD_INFORMATION *info = ptr;
1522 if ((info->Directory = S_ISDIR(st->st_mode)))
1524 info->AllocationSize.QuadPart = 0;
1525 info->EndOfFile.QuadPart = 0;
1526 info->NumberOfLinks = 1;
1528 else
1530 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1531 info->EndOfFile.QuadPart = st->st_size;
1532 info->NumberOfLinks = st->st_nlink;
1535 break;
1536 case FileInternalInformation:
1538 FILE_INTERNAL_INFORMATION *info = ptr;
1539 info->IndexNumber.QuadPart = st->st_ino;
1541 break;
1542 case FileEndOfFileInformation:
1544 FILE_END_OF_FILE_INFORMATION *info = ptr;
1545 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
1547 break;
1548 case FileAllInformation:
1550 FILE_ALL_INFORMATION *info = ptr;
1551 fill_stat_info( st, &info->BasicInformation, FileBasicInformation );
1552 fill_stat_info( st, &info->StandardInformation, FileStandardInformation );
1553 fill_stat_info( st, &info->InternalInformation, FileInternalInformation );
1555 break;
1556 /* all directory structures start with the FileDirectoryInformation layout */
1557 case FileBothDirectoryInformation:
1558 case FileFullDirectoryInformation:
1559 case FileDirectoryInformation:
1561 FILE_DIRECTORY_INFORMATION *info = ptr;
1563 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1564 &info->LastAccessTime, &info->CreationTime );
1565 if (S_ISDIR(st->st_mode))
1567 info->AllocationSize.QuadPart = 0;
1568 info->EndOfFile.QuadPart = 0;
1569 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1571 else
1573 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1574 info->EndOfFile.QuadPart = st->st_size;
1575 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1577 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1578 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1580 break;
1581 case FileIdFullDirectoryInformation:
1583 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
1584 info->FileId.QuadPart = st->st_ino;
1585 fill_stat_info( st, info, FileDirectoryInformation );
1587 break;
1588 case FileIdBothDirectoryInformation:
1590 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
1591 info->FileId.QuadPart = st->st_ino;
1592 fill_stat_info( st, info, FileDirectoryInformation );
1594 break;
1596 default:
1597 return STATUS_INVALID_INFO_CLASS;
1599 return STATUS_SUCCESS;
1602 static NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
1604 data_size_t size = 1024;
1605 NTSTATUS ret;
1606 char *name;
1608 for (;;)
1610 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
1611 if (!name) return STATUS_NO_MEMORY;
1612 unix_name->MaximumLength = size + 1;
1614 SERVER_START_REQ( get_handle_unix_name )
1616 req->handle = wine_server_obj_handle( handle );
1617 wine_server_set_reply( req, name, size );
1618 ret = wine_server_call( req );
1619 size = reply->name_len;
1621 SERVER_END_REQ;
1623 if (!ret)
1625 name[size] = 0;
1626 unix_name->Buffer = name;
1627 unix_name->Length = size;
1628 break;
1630 RtlFreeHeap( GetProcessHeap(), 0, name );
1631 if (ret != STATUS_BUFFER_OVERFLOW) break;
1633 return ret;
1636 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
1638 UNICODE_STRING nt_name;
1639 NTSTATUS status;
1641 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
1643 const WCHAR *ptr = nt_name.Buffer;
1644 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
1646 /* Skip the volume mount point. */
1647 while (ptr != end && *ptr == '\\') ++ptr;
1648 while (ptr != end && *ptr != '\\') ++ptr;
1649 while (ptr != end && *ptr == '\\') ++ptr;
1650 while (ptr != end && *ptr != '\\') ++ptr;
1652 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
1653 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
1654 else *name_len = info->FileNameLength;
1656 memcpy( info->FileName, ptr, *name_len );
1657 RtlFreeUnicodeString( &nt_name );
1660 return status;
1663 /******************************************************************************
1664 * NtQueryInformationFile [NTDLL.@]
1665 * ZwQueryInformationFile [NTDLL.@]
1667 * Get information about an open file handle.
1669 * PARAMS
1670 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1671 * io [O] Receives information about the operation on return
1672 * ptr [O] Destination for file information
1673 * len [I] Size of FileInformation
1674 * class [I] Type of file information to get
1676 * RETURNS
1677 * Success: 0. IoStatusBlock and FileInformation are updated.
1678 * Failure: An NTSTATUS error code describing the error.
1680 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1681 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1683 static const size_t info_sizes[] =
1686 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1687 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1688 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1689 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1690 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1691 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1692 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1693 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1694 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
1695 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1696 0, /* FileLinkInformation */
1697 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1698 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1699 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1700 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1701 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1702 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1703 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
1704 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1705 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1706 0, /* FileAlternateNameInformation */
1707 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1708 0, /* FilePipeInformation */
1709 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1710 0, /* FilePipeRemoteInformation */
1711 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1712 0, /* FileMailslotSetInformation */
1713 0, /* FileCompressionInformation */
1714 0, /* FileObjectIdInformation */
1715 0, /* FileCompletionInformation */
1716 0, /* FileMoveClusterInformation */
1717 0, /* FileQuotaInformation */
1718 0, /* FileReparsePointInformation */
1719 0, /* FileNetworkOpenInformation */
1720 0, /* FileAttributeTagInformation */
1721 0, /* FileTrackingInformation */
1722 0, /* FileIdBothDirectoryInformation */
1723 0, /* FileIdFullDirectoryInformation */
1724 0, /* FileValidDataLengthInformation */
1725 0, /* FileShortNameInformation */
1729 0, /* FileSfioReserveInformation */
1730 0, /* FileSfioVolumeInformation */
1731 0, /* FileHardLinkInformation */
1733 0, /* FileNormalizedNameInformation */
1735 0, /* FileIdGlobalTxDirectoryInformation */
1739 0 /* FileStandardLinkInformation */
1742 struct stat st;
1743 int fd, needs_close = FALSE;
1745 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1747 io->Information = 0;
1749 if (class <= 0 || class >= FileMaximumInformation)
1750 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1751 if (!info_sizes[class])
1753 FIXME("Unsupported class (%d)\n", class);
1754 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1756 if (len < info_sizes[class])
1757 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1759 if (class != FilePipeLocalInformation)
1761 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1762 return io->u.Status;
1765 switch (class)
1767 case FileBasicInformation:
1768 if (fstat( fd, &st ) == -1)
1769 io->u.Status = FILE_GetNtStatus();
1770 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1771 io->u.Status = STATUS_INVALID_INFO_CLASS;
1772 else
1773 fill_stat_info( &st, ptr, class );
1774 break;
1775 case FileStandardInformation:
1777 FILE_STANDARD_INFORMATION *info = ptr;
1779 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1780 else
1782 fill_stat_info( &st, info, class );
1783 info->DeletePending = FALSE; /* FIXME */
1786 break;
1787 case FilePositionInformation:
1789 FILE_POSITION_INFORMATION *info = ptr;
1790 off_t res = lseek( fd, 0, SEEK_CUR );
1791 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1792 else info->CurrentByteOffset.QuadPart = res;
1794 break;
1795 case FileInternalInformation:
1796 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1797 else fill_stat_info( &st, ptr, class );
1798 break;
1799 case FileEaInformation:
1801 FILE_EA_INFORMATION *info = ptr;
1802 info->EaSize = 0;
1804 break;
1805 case FileEndOfFileInformation:
1806 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1807 else fill_stat_info( &st, ptr, class );
1808 break;
1809 case FileAllInformation:
1811 FILE_ALL_INFORMATION *info = ptr;
1812 ANSI_STRING unix_name;
1814 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1815 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1816 io->u.Status = STATUS_INVALID_INFO_CLASS;
1817 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
1819 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
1821 fill_stat_info( &st, info, FileAllInformation );
1822 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1823 info->EaInformation.EaSize = 0;
1824 info->AccessInformation.AccessFlags = 0; /* FIXME */
1825 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1826 info->ModeInformation.Mode = 0; /* FIXME */
1827 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1829 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
1830 RtlFreeAnsiString( &unix_name );
1831 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
1834 break;
1835 case FileMailslotQueryInformation:
1837 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1839 SERVER_START_REQ( set_mailslot_info )
1841 req->handle = wine_server_obj_handle( hFile );
1842 req->flags = 0;
1843 io->u.Status = wine_server_call( req );
1844 if( io->u.Status == STATUS_SUCCESS )
1846 info->MaximumMessageSize = reply->max_msgsize;
1847 info->MailslotQuota = 0;
1848 info->NextMessageSize = 0;
1849 info->MessagesAvailable = 0;
1850 info->ReadTimeout.QuadPart = reply->read_timeout;
1853 SERVER_END_REQ;
1854 if (!io->u.Status)
1856 char *tmpbuf;
1857 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1858 if (size > 0x10000) size = 0x10000;
1859 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1861 int fd, needs_close;
1862 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1864 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1865 info->MessagesAvailable = (res > 0);
1866 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1867 if (needs_close) close( fd );
1869 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1873 break;
1874 case FilePipeLocalInformation:
1876 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1878 SERVER_START_REQ( get_named_pipe_info )
1880 req->handle = wine_server_obj_handle( hFile );
1881 if (!(io->u.Status = wine_server_call( req )))
1883 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1884 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1885 pli->NamedPipeConfiguration = 0; /* FIXME */
1886 pli->MaximumInstances = reply->maxinstances;
1887 pli->CurrentInstances = reply->instances;
1888 pli->InboundQuota = reply->insize;
1889 pli->ReadDataAvailable = 0; /* FIXME */
1890 pli->OutboundQuota = reply->outsize;
1891 pli->WriteQuotaAvailable = 0; /* FIXME */
1892 pli->NamedPipeState = 0; /* FIXME */
1893 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1894 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1897 SERVER_END_REQ;
1899 break;
1900 case FileNameInformation:
1902 FILE_NAME_INFORMATION *info = ptr;
1903 ANSI_STRING unix_name;
1905 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
1907 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
1908 io->u.Status = fill_name_info( &unix_name, info, &name_len );
1909 RtlFreeAnsiString( &unix_name );
1910 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
1913 break;
1914 default:
1915 FIXME("Unsupported class (%d)\n", class);
1916 io->u.Status = STATUS_NOT_IMPLEMENTED;
1917 break;
1919 if (needs_close) close( fd );
1920 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1921 return io->u.Status;
1924 /******************************************************************************
1925 * NtSetInformationFile [NTDLL.@]
1926 * ZwSetInformationFile [NTDLL.@]
1928 * Set information about an open file handle.
1930 * PARAMS
1931 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1932 * io [O] Receives information about the operation on return
1933 * ptr [I] Source for file information
1934 * len [I] Size of FileInformation
1935 * class [I] Type of file information to set
1937 * RETURNS
1938 * Success: 0. io is updated.
1939 * Failure: An NTSTATUS error code describing the error.
1941 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1942 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1944 int fd, needs_close;
1946 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1948 io->u.Status = STATUS_SUCCESS;
1949 switch (class)
1951 case FileBasicInformation:
1952 if (len >= sizeof(FILE_BASIC_INFORMATION))
1954 struct stat st;
1955 const FILE_BASIC_INFORMATION *info = ptr;
1957 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1958 return io->u.Status;
1960 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1962 struct timeval tv[2];
1964 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1967 tv[0].tv_sec = tv[0].tv_usec = 0;
1968 tv[1].tv_sec = tv[1].tv_usec = 0;
1969 if (!fstat( fd, &st ))
1971 tv[0].tv_sec = st.st_atime;
1972 tv[1].tv_sec = st.st_mtime;
1975 if (info->LastAccessTime.QuadPart)
1977 ULONGLONG sec = info->LastAccessTime.QuadPart / 10000000;
1978 UINT nsec = info->LastAccessTime.QuadPart % 10000000;
1979 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1980 tv[0].tv_usec = nsec / 10;
1982 if (info->LastWriteTime.QuadPart)
1984 ULONGLONG sec = info->LastWriteTime.QuadPart / 10000000;
1985 UINT nsec = info->LastWriteTime.QuadPart % 10000000;
1986 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1987 tv[1].tv_usec = nsec / 10;
1989 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1992 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1994 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1995 else
1997 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1999 if (S_ISDIR( st.st_mode))
2000 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2001 else
2002 st.st_mode &= ~0222; /* clear write permission bits */
2004 else
2006 /* add write permission only where we already have read permission */
2007 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2009 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2013 if (needs_close) close( fd );
2015 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2016 break;
2018 case FilePositionInformation:
2019 if (len >= sizeof(FILE_POSITION_INFORMATION))
2021 const FILE_POSITION_INFORMATION *info = ptr;
2023 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2024 return io->u.Status;
2026 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2027 io->u.Status = FILE_GetNtStatus();
2029 if (needs_close) close( fd );
2031 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2032 break;
2034 case FileEndOfFileInformation:
2035 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2037 struct stat st;
2038 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2040 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2041 return io->u.Status;
2043 /* first try normal truncate */
2044 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2046 /* now check for the need to extend the file */
2047 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2049 static const char zero;
2051 /* extend the file one byte beyond the requested size and then truncate it */
2052 /* this should work around ftruncate implementations that can't extend files */
2053 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2054 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2056 io->u.Status = FILE_GetNtStatus();
2058 if (needs_close) close( fd );
2060 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2061 break;
2063 case FileMailslotSetInformation:
2065 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2067 SERVER_START_REQ( set_mailslot_info )
2069 req->handle = wine_server_obj_handle( handle );
2070 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2071 req->read_timeout = info->ReadTimeout.QuadPart;
2072 io->u.Status = wine_server_call( req );
2074 SERVER_END_REQ;
2076 break;
2078 case FileCompletionInformation:
2079 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2081 FILE_COMPLETION_INFORMATION *info = ptr;
2083 SERVER_START_REQ( set_completion_info )
2085 req->handle = wine_server_obj_handle( handle );
2086 req->chandle = wine_server_obj_handle( info->CompletionPort );
2087 req->ckey = info->CompletionKey;
2088 io->u.Status = wine_server_call( req );
2090 SERVER_END_REQ;
2091 } else
2092 io->u.Status = STATUS_INVALID_PARAMETER_3;
2093 break;
2095 default:
2096 FIXME("Unsupported class (%d)\n", class);
2097 io->u.Status = STATUS_NOT_IMPLEMENTED;
2098 break;
2100 io->Information = 0;
2101 return io->u.Status;
2105 /******************************************************************************
2106 * NtQueryFullAttributesFile (NTDLL.@)
2108 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2109 FILE_NETWORK_OPEN_INFORMATION *info )
2111 ANSI_STRING unix_name;
2112 NTSTATUS status;
2114 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2116 struct stat st;
2118 if (stat( unix_name.Buffer, &st ) == -1)
2119 status = FILE_GetNtStatus();
2120 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2121 status = STATUS_INVALID_INFO_CLASS;
2122 else
2124 FILE_BASIC_INFORMATION basic;
2125 FILE_STANDARD_INFORMATION std;
2127 fill_stat_info( &st, &basic, FileBasicInformation );
2128 fill_stat_info( &st, &std, FileStandardInformation );
2130 info->CreationTime = basic.CreationTime;
2131 info->LastAccessTime = basic.LastAccessTime;
2132 info->LastWriteTime = basic.LastWriteTime;
2133 info->ChangeTime = basic.ChangeTime;
2134 info->AllocationSize = std.AllocationSize;
2135 info->EndOfFile = std.EndOfFile;
2136 info->FileAttributes = basic.FileAttributes;
2137 if (DIR_is_hidden_file( attr->ObjectName ))
2138 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2140 RtlFreeAnsiString( &unix_name );
2142 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2143 return status;
2147 /******************************************************************************
2148 * NtQueryAttributesFile (NTDLL.@)
2149 * ZwQueryAttributesFile (NTDLL.@)
2151 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2153 ANSI_STRING unix_name;
2154 NTSTATUS status;
2156 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2158 struct stat st;
2160 if (stat( unix_name.Buffer, &st ) == -1)
2161 status = FILE_GetNtStatus();
2162 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2163 status = STATUS_INVALID_INFO_CLASS;
2164 else
2166 status = fill_stat_info( &st, info, FileBasicInformation );
2167 if (DIR_is_hidden_file( attr->ObjectName ))
2168 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2170 RtlFreeAnsiString( &unix_name );
2172 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2173 return status;
2177 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
2178 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2179 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2180 unsigned int flags )
2182 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2184 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2185 /* Don't assume read-only, let the mount options set it below */
2186 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2188 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2189 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2191 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2192 info->Characteristics |= FILE_REMOTE_DEVICE;
2194 else if (!strcmp("procfs", fstypename))
2195 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2196 else
2197 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2199 if (flags & MNT_RDONLY)
2200 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2202 if (!(flags & MNT_LOCAL))
2204 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2205 info->Characteristics |= FILE_REMOTE_DEVICE;
2208 #endif
2210 static inline int is_device_placeholder( int fd )
2212 static const char wine_placeholder[] = "Wine device placeholder";
2213 char buffer[sizeof(wine_placeholder)-1];
2215 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2216 return 0;
2217 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2220 /******************************************************************************
2221 * get_device_info
2223 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2225 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2227 struct stat st;
2229 info->Characteristics = 0;
2230 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2231 if (S_ISCHR( st.st_mode ))
2233 info->DeviceType = FILE_DEVICE_UNKNOWN;
2234 #ifdef linux
2235 switch(major(st.st_rdev))
2237 case MEM_MAJOR:
2238 info->DeviceType = FILE_DEVICE_NULL;
2239 break;
2240 case TTY_MAJOR:
2241 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2242 break;
2243 case LP_MAJOR:
2244 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2245 break;
2246 case SCSI_TAPE_MAJOR:
2247 info->DeviceType = FILE_DEVICE_TAPE;
2248 break;
2250 #endif
2252 else if (S_ISBLK( st.st_mode ))
2254 info->DeviceType = FILE_DEVICE_DISK;
2256 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2258 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2260 else if (is_device_placeholder( fd ))
2262 info->DeviceType = FILE_DEVICE_DISK;
2264 else /* regular file or directory */
2266 #if defined(linux) && defined(HAVE_FSTATFS)
2267 struct statfs stfs;
2269 /* check for floppy disk */
2270 if (major(st.st_dev) == FLOPPY_MAJOR)
2271 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2273 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2274 switch (stfs.f_type)
2276 case 0x9660: /* iso9660 */
2277 case 0x9fa1: /* supermount */
2278 case 0x15013346: /* udf */
2279 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2280 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2281 break;
2282 case 0x6969: /* nfs */
2283 case 0x517B: /* smbfs */
2284 case 0x564c: /* ncpfs */
2285 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2286 info->Characteristics |= FILE_REMOTE_DEVICE;
2287 break;
2288 case 0x01021994: /* tmpfs */
2289 case 0x28cd3d45: /* cramfs */
2290 case 0x1373: /* devfs */
2291 case 0x9fa0: /* procfs */
2292 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2293 break;
2294 default:
2295 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2296 break;
2298 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__APPLE__)
2299 struct statfs stfs;
2301 if (fstatfs( fd, &stfs ) < 0)
2302 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2303 else
2304 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2305 #elif defined(__NetBSD__)
2306 struct statvfs stfs;
2308 if (fstatvfs( fd, &stfs) < 0)
2309 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2310 else
2311 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2312 #elif defined(sun)
2313 /* Use dkio to work out device types */
2315 # include <sys/dkio.h>
2316 # include <sys/vtoc.h>
2317 struct dk_cinfo dkinf;
2318 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2319 if(retval==-1){
2320 WARN("Unable to get disk device type information - assuming a disk like device\n");
2321 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2323 switch (dkinf.dki_ctype)
2325 case DKC_CDROM:
2326 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2327 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2328 break;
2329 case DKC_NCRFLOPPY:
2330 case DKC_SMSFLOPPY:
2331 case DKC_INTEL82072:
2332 case DKC_INTEL82077:
2333 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2334 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2335 break;
2336 case DKC_MD:
2337 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2338 break;
2339 default:
2340 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2343 #else
2344 static int warned;
2345 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2346 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2347 #endif
2348 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2350 return STATUS_SUCCESS;
2354 /******************************************************************************
2355 * NtQueryVolumeInformationFile [NTDLL.@]
2356 * ZwQueryVolumeInformationFile [NTDLL.@]
2358 * Get volume information for an open file handle.
2360 * PARAMS
2361 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2362 * io [O] Receives information about the operation on return
2363 * buffer [O] Destination for volume information
2364 * length [I] Size of FsInformation
2365 * info_class [I] Type of volume information to set
2367 * RETURNS
2368 * Success: 0. io and buffer are updated.
2369 * Failure: An NTSTATUS error code describing the error.
2371 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2372 PVOID buffer, ULONG length,
2373 FS_INFORMATION_CLASS info_class )
2375 int fd, needs_close;
2376 struct stat st;
2377 static int once;
2379 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2380 return io->u.Status;
2382 io->u.Status = STATUS_NOT_IMPLEMENTED;
2383 io->Information = 0;
2385 switch( info_class )
2387 case FileFsVolumeInformation:
2388 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2389 break;
2390 case FileFsLabelInformation:
2391 FIXME( "%p: label info not supported\n", handle );
2392 break;
2393 case FileFsSizeInformation:
2394 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2395 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2396 else
2398 FILE_FS_SIZE_INFORMATION *info = buffer;
2400 if (fstat( fd, &st ) < 0)
2402 io->u.Status = FILE_GetNtStatus();
2403 break;
2405 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2407 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2409 else
2411 ULONGLONG bsize;
2412 /* Linux's fstatvfs is buggy */
2413 #if !defined(linux) || !defined(HAVE_FSTATFS)
2414 struct statvfs stfs;
2416 if (fstatvfs( fd, &stfs ) < 0)
2418 io->u.Status = FILE_GetNtStatus();
2419 break;
2421 bsize = stfs.f_frsize;
2422 #else
2423 struct statfs stfs;
2424 if (fstatfs( fd, &stfs ) < 0)
2426 io->u.Status = FILE_GetNtStatus();
2427 break;
2429 bsize = stfs.f_bsize;
2430 #endif
2431 if (bsize == 2048) /* assume CD-ROM */
2433 info->BytesPerSector = 2048;
2434 info->SectorsPerAllocationUnit = 1;
2436 else
2438 info->BytesPerSector = 512;
2439 info->SectorsPerAllocationUnit = 8;
2441 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2442 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2443 io->Information = sizeof(*info);
2444 io->u.Status = STATUS_SUCCESS;
2447 break;
2448 case FileFsDeviceInformation:
2449 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2450 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2451 else
2453 FILE_FS_DEVICE_INFORMATION *info = buffer;
2455 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2456 io->Information = sizeof(*info);
2458 break;
2459 case FileFsAttributeInformation:
2460 FIXME( "%p: attribute info not supported\n", handle );
2461 break;
2462 case FileFsControlInformation:
2463 FIXME( "%p: control info not supported\n", handle );
2464 break;
2465 case FileFsFullSizeInformation:
2466 FIXME( "%p: full size info not supported\n", handle );
2467 break;
2468 case FileFsObjectIdInformation:
2469 FIXME( "%p: object id info not supported\n", handle );
2470 break;
2471 case FileFsMaximumInformation:
2472 FIXME( "%p: maximum info not supported\n", handle );
2473 break;
2474 default:
2475 io->u.Status = STATUS_INVALID_PARAMETER;
2476 break;
2478 if (needs_close) close( fd );
2479 return io->u.Status;
2483 /******************************************************************
2484 * NtQueryEaFile (NTDLL.@)
2486 * Read extended attributes from NTFS files.
2488 * PARAMS
2489 * hFile [I] File handle, must be opened with FILE_READ_EA access
2490 * iosb [O] Receives information about the operation on return
2491 * buffer [O] Output buffer
2492 * length [I] Length of output buffer
2493 * single_entry [I] Only read and return one entry
2494 * ea_list [I] Optional list with names of EAs to return
2495 * ea_list_len [I] Length of ea_list in bytes
2496 * ea_index [I] Optional pointer to 1-based index of attribute to return
2497 * restart [I] restart EA scan
2499 * RETURNS
2500 * Success: 0. Atrributes read into buffer
2501 * Failure: An NTSTATUS error code describing the error.
2503 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
2504 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
2505 PULONG ea_index, BOOLEAN restart )
2507 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
2508 hFile, iosb, buffer, length, single_entry, ea_list,
2509 ea_list_len, ea_index, restart);
2510 return STATUS_ACCESS_DENIED;
2514 /******************************************************************
2515 * NtSetEaFile (NTDLL.@)
2517 * Update extended attributes for NTFS files.
2519 * PARAMS
2520 * hFile [I] File handle, must be opened with FILE_READ_EA access
2521 * iosb [O] Receives information about the operation on return
2522 * buffer [I] Buffer with EA information
2523 * length [I] Length of buffer
2525 * RETURNS
2526 * Success: 0. Attributes are updated
2527 * Failure: An NTSTATUS error code describing the error.
2529 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
2531 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
2532 return STATUS_ACCESS_DENIED;
2536 /******************************************************************
2537 * NtFlushBuffersFile (NTDLL.@)
2539 * Flush any buffered data on an open file handle.
2541 * PARAMS
2542 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2543 * IoStatusBlock [O] Receives information about the operation on return
2545 * RETURNS
2546 * Success: 0. IoStatusBlock is updated.
2547 * Failure: An NTSTATUS error code describing the error.
2549 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2551 NTSTATUS ret;
2552 HANDLE hEvent = NULL;
2554 SERVER_START_REQ( flush_file )
2556 req->handle = wine_server_obj_handle( hFile );
2557 ret = wine_server_call( req );
2558 hEvent = wine_server_ptr_handle( reply->event );
2560 SERVER_END_REQ;
2561 if (!ret && hEvent)
2563 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2564 NtClose( hEvent );
2566 return ret;
2569 /******************************************************************
2570 * NtLockFile (NTDLL.@)
2574 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2575 PIO_APC_ROUTINE apc, void* apc_user,
2576 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2577 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2578 BOOLEAN exclusive )
2580 NTSTATUS ret;
2581 HANDLE handle;
2582 BOOLEAN async;
2583 static BOOLEAN warn = TRUE;
2585 if (apc || io_status || key)
2587 FIXME("Unimplemented yet parameter\n");
2588 return STATUS_NOT_IMPLEMENTED;
2591 if (apc_user && warn)
2593 FIXME("I/O completion on lock not implemented yet\n");
2594 warn = FALSE;
2597 for (;;)
2599 SERVER_START_REQ( lock_file )
2601 req->handle = wine_server_obj_handle( hFile );
2602 req->offset = offset->QuadPart;
2603 req->count = count->QuadPart;
2604 req->shared = !exclusive;
2605 req->wait = !dont_wait;
2606 ret = wine_server_call( req );
2607 handle = wine_server_ptr_handle( reply->handle );
2608 async = reply->overlapped;
2610 SERVER_END_REQ;
2611 if (ret != STATUS_PENDING)
2613 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2614 return ret;
2617 if (async)
2619 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2620 if (handle) NtClose( handle );
2621 return STATUS_PENDING;
2623 if (handle)
2625 NtWaitForSingleObject( handle, FALSE, NULL );
2626 NtClose( handle );
2628 else
2630 LARGE_INTEGER time;
2632 /* Unix lock conflict, sleep a bit and retry */
2633 time.QuadPart = 100 * (ULONGLONG)10000;
2634 time.QuadPart = -time.QuadPart;
2635 NtDelayExecution( FALSE, &time );
2641 /******************************************************************
2642 * NtUnlockFile (NTDLL.@)
2646 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2647 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2648 PULONG key )
2650 NTSTATUS status;
2652 TRACE( "%p %x%08x %x%08x\n",
2653 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2655 if (io_status || key)
2657 FIXME("Unimplemented yet parameter\n");
2658 return STATUS_NOT_IMPLEMENTED;
2661 SERVER_START_REQ( unlock_file )
2663 req->handle = wine_server_obj_handle( hFile );
2664 req->offset = offset->QuadPart;
2665 req->count = count->QuadPart;
2666 status = wine_server_call( req );
2668 SERVER_END_REQ;
2669 return status;
2672 /******************************************************************
2673 * NtCreateNamedPipeFile (NTDLL.@)
2677 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2678 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2679 ULONG sharing, ULONG dispo, ULONG options,
2680 ULONG pipe_type, ULONG read_mode,
2681 ULONG completion_mode, ULONG max_inst,
2682 ULONG inbound_quota, ULONG outbound_quota,
2683 PLARGE_INTEGER timeout)
2685 NTSTATUS status;
2687 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2688 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2689 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2690 outbound_quota, timeout);
2692 /* assume we only get relative timeout */
2693 if (timeout->QuadPart > 0)
2694 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2696 SERVER_START_REQ( create_named_pipe )
2698 req->access = access;
2699 req->attributes = attr->Attributes;
2700 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2701 req->options = options;
2702 req->flags =
2703 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2704 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2705 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2706 req->maxinstances = max_inst;
2707 req->outsize = outbound_quota;
2708 req->insize = inbound_quota;
2709 req->timeout = timeout->QuadPart;
2710 wine_server_add_data( req, attr->ObjectName->Buffer,
2711 attr->ObjectName->Length );
2712 status = wine_server_call( req );
2713 if (!status) *handle = wine_server_ptr_handle( reply->handle );
2715 SERVER_END_REQ;
2716 return status;
2719 /******************************************************************
2720 * NtDeleteFile (NTDLL.@)
2724 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2726 NTSTATUS status;
2727 HANDLE hFile;
2728 IO_STATUS_BLOCK io;
2730 TRACE("%p\n", ObjectAttributes);
2731 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2732 ObjectAttributes, &io, NULL, 0,
2733 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2734 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2735 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2736 return status;
2739 /******************************************************************
2740 * NtCancelIoFileEx (NTDLL.@)
2744 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
2746 LARGE_INTEGER timeout;
2748 TRACE("%p %p %p\n", hFile, iosb, io_status );
2750 SERVER_START_REQ( cancel_async )
2752 req->handle = wine_server_obj_handle( hFile );
2753 req->iosb = wine_server_client_ptr( iosb );
2754 req->only_thread = FALSE;
2755 io_status->u.Status = wine_server_call( req );
2757 SERVER_END_REQ;
2758 if (io_status->u.Status)
2759 return io_status->u.Status;
2761 /* Let some APC be run, so that we can run the remaining APCs on hFile
2762 * either the cancelation of the pending one, but also the execution
2763 * of the queued APC, but not yet run. This is needed to ensure proper
2764 * clean-up of allocated data.
2766 timeout.u.LowPart = timeout.u.HighPart = 0;
2767 NtDelayExecution( TRUE, &timeout );
2768 return io_status->u.Status;
2771 /******************************************************************
2772 * NtCancelIoFile (NTDLL.@)
2776 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2778 LARGE_INTEGER timeout;
2780 TRACE("%p %p\n", hFile, io_status );
2782 SERVER_START_REQ( cancel_async )
2784 req->handle = wine_server_obj_handle( hFile );
2785 req->iosb = 0;
2786 req->only_thread = TRUE;
2787 io_status->u.Status = wine_server_call( req );
2789 SERVER_END_REQ;
2790 if (io_status->u.Status)
2791 return io_status->u.Status;
2793 /* Let some APC be run, so that we can run the remaining APCs on hFile
2794 * either the cancelation of the pending one, but also the execution
2795 * of the queued APC, but not yet run. This is needed to ensure proper
2796 * clean-up of allocated data.
2798 timeout.u.LowPart = timeout.u.HighPart = 0;
2799 NtDelayExecution( TRUE, &timeout );
2800 return io_status->u.Status;
2803 /******************************************************************************
2804 * NtCreateMailslotFile [NTDLL.@]
2805 * ZwCreateMailslotFile [NTDLL.@]
2807 * PARAMS
2808 * pHandle [O] pointer to receive the handle created
2809 * DesiredAccess [I] access mode (read, write, etc)
2810 * ObjectAttributes [I] fully qualified NT path of the mailslot
2811 * IoStatusBlock [O] receives completion status and other info
2812 * CreateOptions [I]
2813 * MailslotQuota [I]
2814 * MaxMessageSize [I]
2815 * TimeOut [I]
2817 * RETURNS
2818 * An NT status code
2820 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2821 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2822 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2823 PLARGE_INTEGER TimeOut)
2825 LARGE_INTEGER timeout;
2826 NTSTATUS ret;
2828 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2829 pHandle, DesiredAccess, attr, IoStatusBlock,
2830 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2832 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2833 if (!attr) return STATUS_INVALID_PARAMETER;
2834 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2837 * For a NULL TimeOut pointer set the default timeout value
2839 if (!TimeOut)
2840 timeout.QuadPart = -1;
2841 else
2842 timeout.QuadPart = TimeOut->QuadPart;
2844 SERVER_START_REQ( create_mailslot )
2846 req->access = DesiredAccess;
2847 req->attributes = attr->Attributes;
2848 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2849 req->max_msgsize = MaxMessageSize;
2850 req->read_timeout = timeout.QuadPart;
2851 wine_server_add_data( req, attr->ObjectName->Buffer,
2852 attr->ObjectName->Length );
2853 ret = wine_server_call( req );
2854 if( ret == STATUS_SUCCESS )
2855 *pHandle = wine_server_ptr_handle( reply->handle );
2857 SERVER_END_REQ;
2859 return ret;