ntdll: Add support for converting file names to Unix when a root directory is specified.
[wine/multimedia.git] / dlls / ntdll / file.c
blob213d083b88604c6fc944360bcaacc7fde81c657b
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 = NULL;
146 struct object_attributes objattr;
148 objattr.rootdir = wine_server_obj_handle( attr->RootDirectory );
149 objattr.sd_len = 0;
150 objattr.name_len = 0;
151 if (attr)
153 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
154 if (io->u.Status != STATUS_SUCCESS)
156 RtlFreeAnsiString( &unix_name );
157 return io->u.Status;
161 SERVER_START_REQ( create_file )
163 req->access = access;
164 req->attributes = attr->Attributes;
165 req->sharing = sharing;
166 req->create = disposition;
167 req->options = options;
168 req->attrs = attributes;
169 wine_server_add_data( req, &objattr, sizeof(objattr) );
170 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
171 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
172 io->u.Status = wine_server_call( req );
173 *handle = wine_server_ptr_handle( reply->handle );
175 SERVER_END_REQ;
176 NTDLL_free_struct_sd( sd );
177 RtlFreeAnsiString( &unix_name );
179 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
181 if (io->u.Status == STATUS_SUCCESS)
183 if (created) io->Information = FILE_CREATED;
184 else switch(disposition)
186 case FILE_SUPERSEDE:
187 io->Information = FILE_SUPERSEDED;
188 break;
189 case FILE_CREATE:
190 io->Information = FILE_CREATED;
191 break;
192 case FILE_OPEN:
193 case FILE_OPEN_IF:
194 io->Information = FILE_OPENED;
195 break;
196 case FILE_OVERWRITE:
197 case FILE_OVERWRITE_IF:
198 io->Information = FILE_OVERWRITTEN;
199 break;
203 return io->u.Status;
206 /**************************************************************************
207 * NtOpenFile [NTDLL.@]
208 * ZwOpenFile [NTDLL.@]
210 * Open a file.
212 * PARAMS
213 * handle [O] Variable that receives the file handle on return
214 * access [I] Access desired by the caller to the file
215 * attr [I] Structure describing the file to be opened
216 * io [O] Receives details about the result of the operation
217 * sharing [I] Type of shared access the caller requires
218 * options [I] Options for the file open
220 * RETURNS
221 * Success: 0. FileHandle and IoStatusBlock are updated.
222 * Failure: An NTSTATUS error code describing the error.
224 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
225 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
226 ULONG sharing, ULONG options )
228 return FILE_CreateFile( handle, access, attr, io, NULL, 0,
229 sharing, FILE_OPEN, options, NULL, 0 );
232 /**************************************************************************
233 * NtCreateFile [NTDLL.@]
234 * ZwCreateFile [NTDLL.@]
236 * Either create a new file or directory, or open an existing file, device,
237 * directory or volume.
239 * PARAMS
240 * handle [O] Points to a variable which receives the file handle on return
241 * access [I] Desired access to the file
242 * attr [I] Structure describing the file
243 * io [O] Receives information about the operation on return
244 * alloc_size [I] Initial size of the file in bytes
245 * attributes [I] Attributes to create the file with
246 * sharing [I] Type of shared access the caller would like to the file
247 * disposition [I] Specifies what to do, depending on whether the file already exists
248 * options [I] Options for creating a new file
249 * ea_buffer [I] Pointer to an extended attributes buffer
250 * ea_length [I] Length of ea_buffer
252 * RETURNS
253 * Success: 0. handle and io are updated.
254 * Failure: An NTSTATUS error code describing the error.
256 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
257 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
258 ULONG attributes, ULONG sharing, ULONG disposition,
259 ULONG options, PVOID ea_buffer, ULONG ea_length )
261 return FILE_CreateFile( handle, access, attr, io, alloc_size, attributes,
262 sharing, disposition, options, ea_buffer, ea_length );
265 /***********************************************************************
266 * Asynchronous file I/O *
269 struct async_fileio
271 HANDLE handle;
272 PIO_APC_ROUTINE apc;
273 void *apc_arg;
276 typedef struct
278 struct async_fileio io;
279 char* buffer;
280 unsigned int already;
281 unsigned int count;
282 BOOL avail_mode;
283 } async_fileio_read;
285 typedef struct
287 struct async_fileio io;
288 const char *buffer;
289 unsigned int already;
290 unsigned int count;
291 } async_fileio_write;
294 /* callback for file I/O user APC */
295 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
297 struct async_fileio *async = arg;
298 if (async->apc) async->apc( async->apc_arg, io, reserved );
299 RtlFreeHeap( GetProcessHeap(), 0, async );
302 /***********************************************************************
303 * FILE_GetNtStatus(void)
305 * Retrieve the Nt Status code from errno.
306 * Try to be consistent with FILE_SetDosError().
308 NTSTATUS FILE_GetNtStatus(void)
310 int err = errno;
312 TRACE( "errno = %d\n", errno );
313 switch (err)
315 case EAGAIN: return STATUS_SHARING_VIOLATION;
316 case EBADF: return STATUS_INVALID_HANDLE;
317 case EBUSY: return STATUS_DEVICE_BUSY;
318 case ENOSPC: return STATUS_DISK_FULL;
319 case EPERM:
320 case EROFS:
321 case EACCES: return STATUS_ACCESS_DENIED;
322 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
323 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
324 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
325 case EMFILE:
326 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
327 case EINVAL: return STATUS_INVALID_PARAMETER;
328 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
329 case EPIPE: return STATUS_PIPE_DISCONNECTED;
330 case EIO: return STATUS_DEVICE_NOT_READY;
331 #ifdef ENOMEDIUM
332 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
333 #endif
334 case ENXIO: return STATUS_NO_SUCH_DEVICE;
335 case ENOTTY:
336 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
337 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
338 case EFAULT: return STATUS_ACCESS_VIOLATION;
339 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
340 case ENOEXEC: /* ?? */
341 case EEXIST: /* ?? */
342 default:
343 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
344 return STATUS_UNSUCCESSFUL;
348 /***********************************************************************
349 * FILE_AsyncReadService (INTERNAL)
351 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc)
353 async_fileio_read *fileio = user;
354 int fd, needs_close, result;
356 switch (status)
358 case STATUS_ALERTED: /* got some new data */
359 /* check to see if the data is ready (non-blocking) */
360 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
361 &needs_close, NULL, NULL )))
362 break;
364 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
365 if (needs_close) close( fd );
367 if (result < 0)
369 if (errno == EAGAIN || errno == EINTR)
370 status = STATUS_PENDING;
371 else /* check to see if the transfer is complete */
372 status = FILE_GetNtStatus();
374 else if (result == 0)
376 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
378 else
380 fileio->already += result;
381 if (fileio->already >= fileio->count || fileio->avail_mode)
382 status = STATUS_SUCCESS;
383 else
385 /* if we only have to read the available data, and none is available,
386 * simply cancel the request. If data was available, it has been read
387 * while in by previous call (NtDelayExecution)
389 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
392 break;
394 case STATUS_TIMEOUT:
395 case STATUS_IO_TIMEOUT:
396 if (fileio->already) status = STATUS_SUCCESS;
397 break;
399 if (status != STATUS_PENDING)
401 iosb->u.Status = status;
402 iosb->Information = fileio->already;
403 *apc = fileio_apc;
405 return status;
408 struct io_timeouts
410 int interval; /* max interval between two bytes */
411 int total; /* total timeout for the whole operation */
412 int end_time; /* absolute time of end of operation */
415 /* retrieve the I/O timeouts to use for a given handle */
416 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
417 struct io_timeouts *timeouts )
419 NTSTATUS status = STATUS_SUCCESS;
421 timeouts->interval = timeouts->total = -1;
423 switch(type)
425 case FD_TYPE_SERIAL:
427 /* GetCommTimeouts */
428 SERIAL_TIMEOUTS st;
429 IO_STATUS_BLOCK io;
431 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
432 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
433 if (status) break;
435 if (is_read)
437 if (st.ReadIntervalTimeout)
438 timeouts->interval = st.ReadIntervalTimeout;
440 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
442 timeouts->total = st.ReadTotalTimeoutConstant;
443 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
444 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
446 else if (st.ReadIntervalTimeout == MAXDWORD)
447 timeouts->interval = timeouts->total = 0;
449 else /* write */
451 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
453 timeouts->total = st.WriteTotalTimeoutConstant;
454 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
455 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
459 break;
460 case FD_TYPE_MAILSLOT:
461 if (is_read)
463 timeouts->interval = 0; /* return as soon as we got something */
464 SERVER_START_REQ( set_mailslot_info )
466 req->handle = wine_server_obj_handle( handle );
467 req->flags = 0;
468 if (!(status = wine_server_call( req )) &&
469 reply->read_timeout != TIMEOUT_INFINITE)
470 timeouts->total = reply->read_timeout / -10000;
472 SERVER_END_REQ;
474 break;
475 case FD_TYPE_SOCKET:
476 case FD_TYPE_PIPE:
477 case FD_TYPE_CHAR:
478 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
479 break;
480 default:
481 break;
483 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
484 return STATUS_SUCCESS;
488 /* retrieve the timeout for the next wait, in milliseconds */
489 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
491 int ret = -1;
493 if (timeouts->total != -1)
495 ret = timeouts->end_time - NtGetTickCount();
496 if (ret < 0) ret = 0;
498 if (already && timeouts->interval != -1)
500 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
502 return ret;
506 /* retrieve the avail_mode flag for async reads */
507 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
509 NTSTATUS status = STATUS_SUCCESS;
511 switch(type)
513 case FD_TYPE_SERIAL:
515 /* GetCommTimeouts */
516 SERIAL_TIMEOUTS st;
517 IO_STATUS_BLOCK io;
519 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
520 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
521 if (status) break;
522 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
523 !st.ReadTotalTimeoutConstant &&
524 st.ReadIntervalTimeout == MAXDWORD);
526 break;
527 case FD_TYPE_MAILSLOT:
528 case FD_TYPE_SOCKET:
529 case FD_TYPE_PIPE:
530 case FD_TYPE_CHAR:
531 *avail_mode = TRUE;
532 break;
533 default:
534 *avail_mode = FALSE;
535 break;
537 return status;
541 /******************************************************************************
542 * NtReadFile [NTDLL.@]
543 * ZwReadFile [NTDLL.@]
545 * Read from an open file handle.
547 * PARAMS
548 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
549 * Event [I] Event to signal upon completion (or NULL)
550 * ApcRoutine [I] Callback to call upon completion (or NULL)
551 * ApcContext [I] Context for ApcRoutine (or NULL)
552 * IoStatusBlock [O] Receives information about the operation on return
553 * Buffer [O] Destination for the data read
554 * Length [I] Size of Buffer
555 * ByteOffset [O] Destination for the new file pointer position (or NULL)
556 * Key [O] Function unknown (may be NULL)
558 * RETURNS
559 * Success: 0. IoStatusBlock is updated, and the Information member contains
560 * The number of bytes read.
561 * Failure: An NTSTATUS error code describing the error.
563 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
564 PIO_APC_ROUTINE apc, void* apc_user,
565 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
566 PLARGE_INTEGER offset, PULONG key)
568 int result, unix_handle, needs_close, timeout_init_done = 0;
569 unsigned int options;
570 struct io_timeouts timeouts;
571 NTSTATUS status;
572 ULONG total = 0;
573 enum server_fd_type type;
574 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
576 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
577 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
579 if (!io_status) return STATUS_ACCESS_VIOLATION;
581 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
582 &needs_close, &type, &options );
583 if (status) return status;
585 if (!virtual_check_buffer_for_write( buffer, length ))
587 status = STATUS_ACCESS_VIOLATION;
588 goto done;
591 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
593 /* async I/O doesn't make sense on regular files */
594 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
596 if (errno != EINTR)
598 status = FILE_GetNtStatus();
599 goto done;
602 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
603 /* update file pointer position */
604 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
606 total = result;
607 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
608 goto done;
611 for (;;)
613 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
615 total += result;
616 if (!result || total == length)
618 if (total)
620 status = STATUS_SUCCESS;
621 goto done;
623 switch (type)
625 case FD_TYPE_FILE:
626 case FD_TYPE_CHAR:
627 status = STATUS_END_OF_FILE;
628 goto done;
629 case FD_TYPE_SERIAL:
630 break;
631 default:
632 status = STATUS_PIPE_BROKEN;
633 goto done;
636 else if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
638 else if (errno != EAGAIN)
640 if (errno == EINTR) continue;
641 if (!total) status = FILE_GetNtStatus();
642 goto done;
645 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
647 async_fileio_read *fileio;
648 BOOL avail_mode;
650 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
651 goto err;
652 if (total && avail_mode)
654 status = STATUS_SUCCESS;
655 goto done;
658 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
660 status = STATUS_NO_MEMORY;
661 goto err;
663 fileio->io.handle = hFile;
664 fileio->io.apc = apc;
665 fileio->io.apc_arg = apc_user;
666 fileio->already = total;
667 fileio->count = length;
668 fileio->buffer = buffer;
669 fileio->avail_mode = avail_mode;
671 SERVER_START_REQ( register_async )
673 req->type = ASYNC_TYPE_READ;
674 req->count = length;
675 req->async.handle = wine_server_obj_handle( hFile );
676 req->async.event = wine_server_obj_handle( hEvent );
677 req->async.callback = wine_server_client_ptr( FILE_AsyncReadService );
678 req->async.iosb = wine_server_client_ptr( io_status );
679 req->async.arg = wine_server_client_ptr( fileio );
680 req->async.cvalue = cvalue;
681 status = wine_server_call( req );
683 SERVER_END_REQ;
685 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
686 goto err;
688 else /* synchronous read, wait for the fd to become ready */
690 struct pollfd pfd;
691 int ret, timeout;
693 if (!timeout_init_done)
695 timeout_init_done = 1;
696 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
697 goto err;
698 if (hEvent) NtResetEvent( hEvent, NULL );
700 timeout = get_next_io_timeout( &timeouts, total );
702 pfd.fd = unix_handle;
703 pfd.events = POLLIN;
705 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
707 if (total) /* return with what we got so far */
708 status = STATUS_SUCCESS;
709 else
710 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
711 goto done;
713 if (ret == -1 && errno != EINTR)
715 status = FILE_GetNtStatus();
716 goto done;
718 /* will now restart the read */
722 done:
723 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
725 err:
726 if (needs_close) close( unix_handle );
727 if (status == STATUS_SUCCESS)
729 io_status->u.Status = status;
730 io_status->Information = total;
731 TRACE("= SUCCESS (%u)\n", total);
732 if (hEvent) NtSetEvent( hEvent, NULL );
733 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
734 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
736 else
738 TRACE("= 0x%08x\n", status);
739 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
741 return status;
745 /******************************************************************************
746 * NtReadFileScatter [NTDLL.@]
747 * ZwReadFileScatter [NTDLL.@]
749 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
750 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
751 ULONG length, PLARGE_INTEGER offset, PULONG key )
753 size_t page_size = getpagesize();
754 int result, unix_handle, needs_close;
755 unsigned int options;
756 NTSTATUS status;
757 ULONG pos = 0, total = 0;
758 enum server_fd_type type;
759 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
761 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
762 file, event, apc, apc_user, io_status, segments, length, offset, key);
764 if (length % page_size) return STATUS_INVALID_PARAMETER;
765 if (!io_status) return STATUS_ACCESS_VIOLATION;
767 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
768 &needs_close, &type, &options );
769 if (status) return status;
771 if ((type != FD_TYPE_FILE) ||
772 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
773 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
775 status = STATUS_INVALID_PARAMETER;
776 goto error;
779 while (length)
781 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
782 result = pread( unix_handle, (char *)segments->Buffer + pos,
783 page_size - pos, offset->QuadPart + total );
784 else
785 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
787 if (result == -1)
789 if (errno == EINTR) continue;
790 status = FILE_GetNtStatus();
791 break;
793 if (!result)
795 status = STATUS_END_OF_FILE;
796 break;
798 total += result;
799 length -= result;
800 if ((pos += result) == page_size)
802 pos = 0;
803 segments++;
807 if (cvalue) NTDLL_AddCompletion( file, cvalue, status, total );
809 error:
810 if (needs_close) close( unix_handle );
811 if (status == STATUS_SUCCESS)
813 io_status->u.Status = status;
814 io_status->Information = total;
815 TRACE("= SUCCESS (%u)\n", total);
816 if (event) NtSetEvent( event, NULL );
817 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
818 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
820 else
822 TRACE("= 0x%08x\n", status);
823 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
825 return status;
829 /***********************************************************************
830 * FILE_AsyncWriteService (INTERNAL)
832 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status, void **apc)
834 async_fileio_write *fileio = user;
835 int result, fd, needs_close;
836 enum server_fd_type type;
838 switch (status)
840 case STATUS_ALERTED:
841 /* write some data (non-blocking) */
842 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
843 &needs_close, &type, NULL )))
844 break;
846 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
847 result = send( fd, fileio->buffer, 0, 0 );
848 else
849 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
851 if (needs_close) close( fd );
853 if (result < 0)
855 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
856 else status = FILE_GetNtStatus();
858 else
860 fileio->already += result;
861 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
863 break;
865 case STATUS_TIMEOUT:
866 case STATUS_IO_TIMEOUT:
867 if (fileio->already) status = STATUS_SUCCESS;
868 break;
870 if (status != STATUS_PENDING)
872 iosb->u.Status = status;
873 iosb->Information = fileio->already;
874 *apc = fileio_apc;
876 return status;
879 /******************************************************************************
880 * NtWriteFile [NTDLL.@]
881 * ZwWriteFile [NTDLL.@]
883 * Write to an open file handle.
885 * PARAMS
886 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
887 * Event [I] Event to signal upon completion (or NULL)
888 * ApcRoutine [I] Callback to call upon completion (or NULL)
889 * ApcContext [I] Context for ApcRoutine (or NULL)
890 * IoStatusBlock [O] Receives information about the operation on return
891 * Buffer [I] Source for the data to write
892 * Length [I] Size of Buffer
893 * ByteOffset [O] Destination for the new file pointer position (or NULL)
894 * Key [O] Function unknown (may be NULL)
896 * RETURNS
897 * Success: 0. IoStatusBlock is updated, and the Information member contains
898 * The number of bytes written.
899 * Failure: An NTSTATUS error code describing the error.
901 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
902 PIO_APC_ROUTINE apc, void* apc_user,
903 PIO_STATUS_BLOCK io_status,
904 const void* buffer, ULONG length,
905 PLARGE_INTEGER offset, PULONG key)
907 int result, unix_handle, needs_close, timeout_init_done = 0;
908 unsigned int options;
909 struct io_timeouts timeouts;
910 NTSTATUS status;
911 ULONG total = 0;
912 enum server_fd_type type;
913 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
915 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
916 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
918 if (!io_status) return STATUS_ACCESS_VIOLATION;
920 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
921 &needs_close, &type, &options );
922 if (status) return status;
924 if (!virtual_check_buffer_for_read( buffer, length ))
926 status = STATUS_INVALID_USER_BUFFER;
927 goto done;
930 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
932 /* async I/O doesn't make sense on regular files */
933 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
935 if (errno != EINTR)
937 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
938 else status = FILE_GetNtStatus();
939 goto done;
943 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
944 /* update file pointer position */
945 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
947 total = result;
948 status = STATUS_SUCCESS;
949 goto done;
952 for (;;)
954 /* zero-length writes on sockets may not work with plain write(2) */
955 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
956 result = send( unix_handle, buffer, 0, 0 );
957 else
958 result = write( unix_handle, (const char *)buffer + total, length - total );
960 if (result >= 0)
962 total += result;
963 if (total == length)
965 status = STATUS_SUCCESS;
966 goto done;
968 if (type == FD_TYPE_FILE) continue; /* no async I/O on regular files */
970 else if (errno != EAGAIN)
972 if (errno == EINTR) continue;
973 if (!total)
975 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
976 else status = FILE_GetNtStatus();
978 goto done;
981 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
983 async_fileio_write *fileio;
985 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
987 status = STATUS_NO_MEMORY;
988 goto err;
990 fileio->io.handle = hFile;
991 fileio->io.apc = apc;
992 fileio->io.apc_arg = apc_user;
993 fileio->already = total;
994 fileio->count = length;
995 fileio->buffer = buffer;
997 SERVER_START_REQ( register_async )
999 req->type = ASYNC_TYPE_WRITE;
1000 req->count = length;
1001 req->async.handle = wine_server_obj_handle( hFile );
1002 req->async.event = wine_server_obj_handle( hEvent );
1003 req->async.callback = wine_server_client_ptr( FILE_AsyncWriteService );
1004 req->async.iosb = wine_server_client_ptr( io_status );
1005 req->async.arg = wine_server_client_ptr( fileio );
1006 req->async.cvalue = cvalue;
1007 status = wine_server_call( req );
1009 SERVER_END_REQ;
1011 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
1012 goto err;
1014 else /* synchronous write, wait for the fd to become ready */
1016 struct pollfd pfd;
1017 int ret, timeout;
1019 if (!timeout_init_done)
1021 timeout_init_done = 1;
1022 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1023 goto err;
1024 if (hEvent) NtResetEvent( hEvent, NULL );
1026 timeout = get_next_io_timeout( &timeouts, total );
1028 pfd.fd = unix_handle;
1029 pfd.events = POLLOUT;
1031 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1033 /* return with what we got so far */
1034 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1035 goto done;
1037 if (ret == -1 && errno != EINTR)
1039 status = FILE_GetNtStatus();
1040 goto done;
1042 /* will now restart the write */
1046 done:
1047 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
1049 err:
1050 if (needs_close) close( unix_handle );
1051 if (status == STATUS_SUCCESS)
1053 io_status->u.Status = status;
1054 io_status->Information = total;
1055 TRACE("= SUCCESS (%u)\n", total);
1056 if (hEvent) NtSetEvent( hEvent, NULL );
1057 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1058 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1060 else
1062 TRACE("= 0x%08x\n", status);
1063 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1065 return status;
1069 /******************************************************************************
1070 * NtWriteFileGather [NTDLL.@]
1071 * ZwWriteFileGather [NTDLL.@]
1073 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1074 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1075 ULONG length, PLARGE_INTEGER offset, PULONG key )
1077 size_t page_size = getpagesize();
1078 int result, unix_handle, needs_close;
1079 unsigned int options;
1080 NTSTATUS status;
1081 ULONG pos = 0, total = 0;
1082 enum server_fd_type type;
1083 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1085 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1086 file, event, apc, apc_user, io_status, segments, length, offset, key);
1088 if (length % page_size) return STATUS_INVALID_PARAMETER;
1089 if (!io_status) return STATUS_ACCESS_VIOLATION;
1091 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1092 &needs_close, &type, &options );
1093 if (status) return status;
1095 if ((type != FD_TYPE_FILE) ||
1096 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1097 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1099 status = STATUS_INVALID_PARAMETER;
1100 goto error;
1103 while (length)
1105 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
1106 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1107 page_size - pos, offset->QuadPart + total );
1108 else
1109 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1111 if (result == -1)
1113 if (errno == EINTR) continue;
1114 if (errno == EFAULT)
1116 status = STATUS_INVALID_USER_BUFFER;
1117 goto error;
1119 status = FILE_GetNtStatus();
1120 break;
1122 if (!result)
1124 status = STATUS_DISK_FULL;
1125 break;
1127 total += result;
1128 length -= result;
1129 if ((pos += result) == page_size)
1131 pos = 0;
1132 segments++;
1136 if (cvalue) NTDLL_AddCompletion( file, cvalue, status, total );
1138 error:
1139 if (needs_close) close( unix_handle );
1140 if (status == STATUS_SUCCESS)
1142 io_status->u.Status = status;
1143 io_status->Information = total;
1144 TRACE("= SUCCESS (%u)\n", total);
1145 if (event) NtSetEvent( event, NULL );
1146 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1147 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1149 else
1151 TRACE("= 0x%08x\n", status);
1152 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1154 return status;
1158 struct async_ioctl
1160 HANDLE handle; /* handle to the device */
1161 HANDLE event; /* async event */
1162 void *buffer; /* buffer for output */
1163 ULONG size; /* size of buffer */
1164 PIO_APC_ROUTINE apc; /* user apc params */
1165 void *apc_arg;
1168 /* callback for ioctl user APC */
1169 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
1171 struct async_ioctl *async = arg;
1172 if (async->apc) async->apc( async->apc_arg, io, reserved );
1173 RtlFreeHeap( GetProcessHeap(), 0, async );
1176 /* callback for ioctl async I/O completion */
1177 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status, void **apc )
1179 struct async_ioctl *async = arg;
1181 if (status == STATUS_ALERTED)
1183 SERVER_START_REQ( get_ioctl_result )
1185 req->handle = wine_server_obj_handle( async->handle );
1186 req->user_arg = wine_server_client_ptr( async );
1187 wine_server_set_reply( req, async->buffer, async->size );
1188 status = wine_server_call( req );
1189 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1191 SERVER_END_REQ;
1193 if (status != STATUS_PENDING)
1195 io->u.Status = status;
1196 if (async->apc || async->event) *apc = ioctl_apc;
1198 return status;
1201 /* do a ioctl call through the server */
1202 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1203 PIO_APC_ROUTINE apc, PVOID apc_context,
1204 IO_STATUS_BLOCK *io, ULONG code,
1205 const void *in_buffer, ULONG in_size,
1206 PVOID out_buffer, ULONG out_size )
1208 struct async_ioctl *async;
1209 NTSTATUS status;
1210 HANDLE wait_handle;
1211 ULONG options;
1212 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1214 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
1215 return STATUS_NO_MEMORY;
1216 async->handle = handle;
1217 async->event = event;
1218 async->buffer = out_buffer;
1219 async->size = out_size;
1220 async->apc = apc;
1221 async->apc_arg = apc_context;
1223 SERVER_START_REQ( ioctl )
1225 req->code = code;
1226 req->blocking = !apc && !event;
1227 req->async.handle = wine_server_obj_handle( handle );
1228 req->async.callback = wine_server_client_ptr( ioctl_completion );
1229 req->async.iosb = wine_server_client_ptr( io );
1230 req->async.arg = wine_server_client_ptr( async );
1231 req->async.event = wine_server_obj_handle( event );
1232 req->async.cvalue = cvalue;
1233 wine_server_add_data( req, in_buffer, in_size );
1234 wine_server_set_reply( req, out_buffer, out_size );
1235 status = wine_server_call( req );
1236 wait_handle = wine_server_ptr_handle( reply->wait );
1237 options = reply->options;
1238 if (status != STATUS_PENDING) io->Information = wine_server_reply_size( reply );
1240 SERVER_END_REQ;
1242 if (status == STATUS_NOT_SUPPORTED)
1243 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1244 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1246 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1248 if (wait_handle)
1250 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1251 status = io->u.Status;
1252 NtClose( wait_handle );
1253 RtlFreeHeap( GetProcessHeap(), 0, async );
1256 return status;
1260 /**************************************************************************
1261 * NtDeviceIoControlFile [NTDLL.@]
1262 * ZwDeviceIoControlFile [NTDLL.@]
1264 * Perform an I/O control operation on an open file handle.
1266 * PARAMS
1267 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1268 * event [I] Event to signal upon completion (or NULL)
1269 * apc [I] Callback to call upon completion (or NULL)
1270 * apc_context [I] Context for ApcRoutine (or NULL)
1271 * io [O] Receives information about the operation on return
1272 * code [I] Control code for the operation to perform
1273 * in_buffer [I] Source for any input data required (or NULL)
1274 * in_size [I] Size of InputBuffer
1275 * out_buffer [O] Source for any output data returned (or NULL)
1276 * out_size [I] Size of OutputBuffer
1278 * RETURNS
1279 * Success: 0. IoStatusBlock is updated.
1280 * Failure: An NTSTATUS error code describing the error.
1282 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1283 PIO_APC_ROUTINE apc, PVOID apc_context,
1284 PIO_STATUS_BLOCK io, ULONG code,
1285 PVOID in_buffer, ULONG in_size,
1286 PVOID out_buffer, ULONG out_size)
1288 ULONG device = (code >> 16);
1289 NTSTATUS status = STATUS_NOT_SUPPORTED;
1291 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1292 handle, event, apc, apc_context, io, code,
1293 in_buffer, in_size, out_buffer, out_size);
1295 switch(device)
1297 case FILE_DEVICE_DISK:
1298 case FILE_DEVICE_CD_ROM:
1299 case FILE_DEVICE_DVD:
1300 case FILE_DEVICE_CONTROLLER:
1301 case FILE_DEVICE_MASS_STORAGE:
1302 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1303 in_buffer, in_size, out_buffer, out_size);
1304 break;
1305 case FILE_DEVICE_SERIAL_PORT:
1306 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1307 in_buffer, in_size, out_buffer, out_size);
1308 break;
1309 case FILE_DEVICE_TAPE:
1310 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1311 in_buffer, in_size, out_buffer, out_size);
1312 break;
1315 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1316 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1317 in_buffer, in_size, out_buffer, out_size );
1319 if (status != STATUS_PENDING) io->u.Status = status;
1320 return status;
1324 /**************************************************************************
1325 * NtFsControlFile [NTDLL.@]
1326 * ZwFsControlFile [NTDLL.@]
1328 * Perform a file system control operation on an open file handle.
1330 * PARAMS
1331 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1332 * event [I] Event to signal upon completion (or NULL)
1333 * apc [I] Callback to call upon completion (or NULL)
1334 * apc_context [I] Context for ApcRoutine (or NULL)
1335 * io [O] Receives information about the operation on return
1336 * code [I] Control code for the operation to perform
1337 * in_buffer [I] Source for any input data required (or NULL)
1338 * in_size [I] Size of InputBuffer
1339 * out_buffer [O] Source for any output data returned (or NULL)
1340 * out_size [I] Size of OutputBuffer
1342 * RETURNS
1343 * Success: 0. IoStatusBlock is updated.
1344 * Failure: An NTSTATUS error code describing the error.
1346 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1347 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1348 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1350 NTSTATUS status;
1352 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1353 handle, event, apc, apc_context, io, code,
1354 in_buffer, in_size, out_buffer, out_size);
1356 if (!io) return STATUS_INVALID_PARAMETER;
1358 switch(code)
1360 case FSCTL_DISMOUNT_VOLUME:
1361 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1362 in_buffer, in_size, out_buffer, out_size );
1363 if (!status) status = DIR_unmount_device( handle );
1364 break;
1366 case FSCTL_PIPE_PEEK:
1368 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1369 int avail = 0, fd, needs_close;
1371 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1373 status = STATUS_INFO_LENGTH_MISMATCH;
1374 break;
1377 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1378 break;
1380 #ifdef FIONREAD
1381 if (ioctl( fd, FIONREAD, &avail ) != 0)
1383 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1384 if (needs_close) close( fd );
1385 status = FILE_GetNtStatus();
1386 break;
1388 #endif
1389 if (!avail) /* check for closed pipe */
1391 struct pollfd pollfd;
1392 int ret;
1394 pollfd.fd = fd;
1395 pollfd.events = POLLIN;
1396 pollfd.revents = 0;
1397 ret = poll( &pollfd, 1, 0 );
1398 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1400 if (needs_close) close( fd );
1401 status = STATUS_PIPE_BROKEN;
1402 break;
1405 buffer->NamedPipeState = 0; /* FIXME */
1406 buffer->ReadDataAvailable = avail;
1407 buffer->NumberOfMessages = 0; /* FIXME */
1408 buffer->MessageLength = 0; /* FIXME */
1409 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1410 status = STATUS_SUCCESS;
1411 if (avail)
1413 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1414 if (data_size)
1416 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1417 if (res >= 0) io->Information += res;
1420 if (needs_close) close( fd );
1422 break;
1424 case FSCTL_PIPE_DISCONNECT:
1425 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1426 in_buffer, in_size, out_buffer, out_size );
1427 if (!status)
1429 int fd = server_remove_fd_from_cache( handle );
1430 if (fd != -1) close( fd );
1432 break;
1434 case FSCTL_PIPE_IMPERSONATE:
1435 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1436 status = RtlImpersonateSelf( SecurityImpersonation );
1437 break;
1439 case FSCTL_LOCK_VOLUME:
1440 case FSCTL_UNLOCK_VOLUME:
1441 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1442 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1443 status = STATUS_SUCCESS;
1444 break;
1446 case FSCTL_PIPE_LISTEN:
1447 case FSCTL_PIPE_WAIT:
1448 default:
1449 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1450 in_buffer, in_size, out_buffer, out_size );
1451 break;
1454 if (status != STATUS_PENDING) io->u.Status = status;
1455 return status;
1458 /******************************************************************************
1459 * NtSetVolumeInformationFile [NTDLL.@]
1460 * ZwSetVolumeInformationFile [NTDLL.@]
1462 * Set volume information for an open file handle.
1464 * PARAMS
1465 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1466 * IoStatusBlock [O] Receives information about the operation on return
1467 * FsInformation [I] Source for volume information
1468 * Length [I] Size of FsInformation
1469 * FsInformationClass [I] Type of volume information to set
1471 * RETURNS
1472 * Success: 0. IoStatusBlock is updated.
1473 * Failure: An NTSTATUS error code describing the error.
1475 NTSTATUS WINAPI NtSetVolumeInformationFile(
1476 IN HANDLE FileHandle,
1477 PIO_STATUS_BLOCK IoStatusBlock,
1478 PVOID FsInformation,
1479 ULONG Length,
1480 FS_INFORMATION_CLASS FsInformationClass)
1482 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1483 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1484 return 0;
1487 static inline void get_file_times( const struct stat *st, LARGE_INTEGER *mtime, LARGE_INTEGER *ctime,
1488 LARGE_INTEGER *atime, LARGE_INTEGER *creation )
1490 RtlSecondsSince1970ToTime( st->st_mtime, mtime );
1491 RtlSecondsSince1970ToTime( st->st_ctime, ctime );
1492 RtlSecondsSince1970ToTime( st->st_atime, atime );
1493 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1494 mtime->QuadPart += st->st_mtim.tv_nsec / 100;
1495 #endif
1496 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1497 ctime->QuadPart += st->st_ctim.tv_nsec / 100;
1498 #endif
1499 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1500 atime->QuadPart += st->st_atim.tv_nsec / 100;
1501 #endif
1502 *creation = *mtime;
1505 /* fill in the file information that depends on the stat info */
1506 NTSTATUS fill_stat_info( const struct stat *st, void *ptr, FILE_INFORMATION_CLASS class )
1508 switch (class)
1510 case FileBasicInformation:
1512 FILE_BASIC_INFORMATION *info = ptr;
1514 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1515 &info->LastAccessTime, &info->CreationTime );
1516 if (S_ISDIR(st->st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1517 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1518 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1519 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1521 break;
1522 case FileStandardInformation:
1524 FILE_STANDARD_INFORMATION *info = ptr;
1526 if ((info->Directory = S_ISDIR(st->st_mode)))
1528 info->AllocationSize.QuadPart = 0;
1529 info->EndOfFile.QuadPart = 0;
1530 info->NumberOfLinks = 1;
1532 else
1534 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1535 info->EndOfFile.QuadPart = st->st_size;
1536 info->NumberOfLinks = st->st_nlink;
1539 break;
1540 case FileInternalInformation:
1542 FILE_INTERNAL_INFORMATION *info = ptr;
1543 info->IndexNumber.QuadPart = st->st_ino;
1545 break;
1546 case FileEndOfFileInformation:
1548 FILE_END_OF_FILE_INFORMATION *info = ptr;
1549 info->EndOfFile.QuadPart = S_ISDIR(st->st_mode) ? 0 : st->st_size;
1551 break;
1552 case FileAllInformation:
1554 FILE_ALL_INFORMATION *info = ptr;
1555 fill_stat_info( st, &info->BasicInformation, FileBasicInformation );
1556 fill_stat_info( st, &info->StandardInformation, FileStandardInformation );
1557 fill_stat_info( st, &info->InternalInformation, FileInternalInformation );
1559 break;
1560 /* all directory structures start with the FileDirectoryInformation layout */
1561 case FileBothDirectoryInformation:
1562 case FileFullDirectoryInformation:
1563 case FileDirectoryInformation:
1565 FILE_DIRECTORY_INFORMATION *info = ptr;
1567 get_file_times( st, &info->LastWriteTime, &info->ChangeTime,
1568 &info->LastAccessTime, &info->CreationTime );
1569 if (S_ISDIR(st->st_mode))
1571 info->AllocationSize.QuadPart = 0;
1572 info->EndOfFile.QuadPart = 0;
1573 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1575 else
1577 info->AllocationSize.QuadPart = (ULONGLONG)st->st_blocks * 512;
1578 info->EndOfFile.QuadPart = st->st_size;
1579 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1581 if (!(st->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1582 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1584 break;
1585 case FileIdFullDirectoryInformation:
1587 FILE_ID_FULL_DIRECTORY_INFORMATION *info = ptr;
1588 info->FileId.QuadPart = st->st_ino;
1589 fill_stat_info( st, info, FileDirectoryInformation );
1591 break;
1592 case FileIdBothDirectoryInformation:
1594 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = ptr;
1595 info->FileId.QuadPart = st->st_ino;
1596 fill_stat_info( st, info, FileDirectoryInformation );
1598 break;
1600 default:
1601 return STATUS_INVALID_INFO_CLASS;
1603 return STATUS_SUCCESS;
1606 static NTSTATUS server_get_unix_name( HANDLE handle, ANSI_STRING *unix_name )
1608 data_size_t size = 1024;
1609 NTSTATUS ret;
1610 char *name;
1612 for (;;)
1614 name = RtlAllocateHeap( GetProcessHeap(), 0, size + 1 );
1615 if (!name) return STATUS_NO_MEMORY;
1616 unix_name->MaximumLength = size + 1;
1618 SERVER_START_REQ( get_handle_unix_name )
1620 req->handle = wine_server_obj_handle( handle );
1621 wine_server_set_reply( req, name, size );
1622 ret = wine_server_call( req );
1623 size = reply->name_len;
1625 SERVER_END_REQ;
1627 if (!ret)
1629 name[size] = 0;
1630 unix_name->Buffer = name;
1631 unix_name->Length = size;
1632 break;
1634 RtlFreeHeap( GetProcessHeap(), 0, name );
1635 if (ret != STATUS_BUFFER_OVERFLOW) break;
1637 return ret;
1640 static NTSTATUS fill_name_info( const ANSI_STRING *unix_name, FILE_NAME_INFORMATION *info, LONG *name_len )
1642 UNICODE_STRING nt_name;
1643 NTSTATUS status;
1645 if (!(status = wine_unix_to_nt_file_name( unix_name, &nt_name )))
1647 const WCHAR *ptr = nt_name.Buffer;
1648 const WCHAR *end = ptr + (nt_name.Length / sizeof(WCHAR));
1650 /* Skip the volume mount point. */
1651 while (ptr != end && *ptr == '\\') ++ptr;
1652 while (ptr != end && *ptr != '\\') ++ptr;
1653 while (ptr != end && *ptr == '\\') ++ptr;
1654 while (ptr != end && *ptr != '\\') ++ptr;
1656 info->FileNameLength = (end - ptr) * sizeof(WCHAR);
1657 if (*name_len < info->FileNameLength) status = STATUS_BUFFER_OVERFLOW;
1658 else *name_len = info->FileNameLength;
1660 memcpy( info->FileName, ptr, *name_len );
1661 RtlFreeUnicodeString( &nt_name );
1664 return status;
1667 /******************************************************************************
1668 * NtQueryInformationFile [NTDLL.@]
1669 * ZwQueryInformationFile [NTDLL.@]
1671 * Get information about an open file handle.
1673 * PARAMS
1674 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1675 * io [O] Receives information about the operation on return
1676 * ptr [O] Destination for file information
1677 * len [I] Size of FileInformation
1678 * class [I] Type of file information to get
1680 * RETURNS
1681 * Success: 0. IoStatusBlock and FileInformation are updated.
1682 * Failure: An NTSTATUS error code describing the error.
1684 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1685 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1687 static const size_t info_sizes[] =
1690 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1691 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1692 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1693 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1694 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1695 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1696 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1697 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1698 sizeof(FILE_NAME_INFORMATION), /* FileNameInformation */
1699 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1700 0, /* FileLinkInformation */
1701 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1702 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1703 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1704 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1705 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1706 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1707 sizeof(FILE_ALL_INFORMATION), /* FileAllInformation */
1708 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1709 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1710 0, /* FileAlternateNameInformation */
1711 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1712 0, /* FilePipeInformation */
1713 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1714 0, /* FilePipeRemoteInformation */
1715 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1716 0, /* FileMailslotSetInformation */
1717 0, /* FileCompressionInformation */
1718 0, /* FileObjectIdInformation */
1719 0, /* FileCompletionInformation */
1720 0, /* FileMoveClusterInformation */
1721 0, /* FileQuotaInformation */
1722 0, /* FileReparsePointInformation */
1723 0, /* FileNetworkOpenInformation */
1724 0, /* FileAttributeTagInformation */
1725 0, /* FileTrackingInformation */
1726 0, /* FileIdBothDirectoryInformation */
1727 0, /* FileIdFullDirectoryInformation */
1728 0, /* FileValidDataLengthInformation */
1729 0, /* FileShortNameInformation */
1733 0, /* FileSfioReserveInformation */
1734 0, /* FileSfioVolumeInformation */
1735 0, /* FileHardLinkInformation */
1737 0, /* FileNormalizedNameInformation */
1739 0, /* FileIdGlobalTxDirectoryInformation */
1743 0 /* FileStandardLinkInformation */
1746 struct stat st;
1747 int fd, needs_close = FALSE;
1749 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1751 io->Information = 0;
1753 if (class <= 0 || class >= FileMaximumInformation)
1754 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1755 if (!info_sizes[class])
1757 FIXME("Unsupported class (%d)\n", class);
1758 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1760 if (len < info_sizes[class])
1761 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1763 if (class != FilePipeLocalInformation)
1765 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1766 return io->u.Status;
1769 switch (class)
1771 case FileBasicInformation:
1772 if (fstat( fd, &st ) == -1)
1773 io->u.Status = FILE_GetNtStatus();
1774 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1775 io->u.Status = STATUS_INVALID_INFO_CLASS;
1776 else
1777 fill_stat_info( &st, ptr, class );
1778 break;
1779 case FileStandardInformation:
1781 FILE_STANDARD_INFORMATION *info = ptr;
1783 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1784 else
1786 fill_stat_info( &st, info, class );
1787 info->DeletePending = FALSE; /* FIXME */
1790 break;
1791 case FilePositionInformation:
1793 FILE_POSITION_INFORMATION *info = ptr;
1794 off_t res = lseek( fd, 0, SEEK_CUR );
1795 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1796 else info->CurrentByteOffset.QuadPart = res;
1798 break;
1799 case FileInternalInformation:
1800 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1801 else fill_stat_info( &st, ptr, class );
1802 break;
1803 case FileEaInformation:
1805 FILE_EA_INFORMATION *info = ptr;
1806 info->EaSize = 0;
1808 break;
1809 case FileEndOfFileInformation:
1810 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1811 else fill_stat_info( &st, ptr, class );
1812 break;
1813 case FileAllInformation:
1815 FILE_ALL_INFORMATION *info = ptr;
1816 ANSI_STRING unix_name;
1818 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1819 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1820 io->u.Status = STATUS_INVALID_INFO_CLASS;
1821 else if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
1823 LONG name_len = len - FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName);
1825 fill_stat_info( &st, info, FileAllInformation );
1826 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1827 info->EaInformation.EaSize = 0;
1828 info->AccessInformation.AccessFlags = 0; /* FIXME */
1829 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1830 info->ModeInformation.Mode = 0; /* FIXME */
1831 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1833 io->u.Status = fill_name_info( &unix_name, &info->NameInformation, &name_len );
1834 RtlFreeAnsiString( &unix_name );
1835 io->Information = FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName) + name_len;
1838 break;
1839 case FileMailslotQueryInformation:
1841 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1843 SERVER_START_REQ( set_mailslot_info )
1845 req->handle = wine_server_obj_handle( hFile );
1846 req->flags = 0;
1847 io->u.Status = wine_server_call( req );
1848 if( io->u.Status == STATUS_SUCCESS )
1850 info->MaximumMessageSize = reply->max_msgsize;
1851 info->MailslotQuota = 0;
1852 info->NextMessageSize = 0;
1853 info->MessagesAvailable = 0;
1854 info->ReadTimeout.QuadPart = reply->read_timeout;
1857 SERVER_END_REQ;
1858 if (!io->u.Status)
1860 char *tmpbuf;
1861 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1862 if (size > 0x10000) size = 0x10000;
1863 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1865 int fd, needs_close;
1866 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1868 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1869 info->MessagesAvailable = (res > 0);
1870 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1871 if (needs_close) close( fd );
1873 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1877 break;
1878 case FilePipeLocalInformation:
1880 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1882 SERVER_START_REQ( get_named_pipe_info )
1884 req->handle = wine_server_obj_handle( hFile );
1885 if (!(io->u.Status = wine_server_call( req )))
1887 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1888 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1889 pli->NamedPipeConfiguration = 0; /* FIXME */
1890 pli->MaximumInstances = reply->maxinstances;
1891 pli->CurrentInstances = reply->instances;
1892 pli->InboundQuota = reply->insize;
1893 pli->ReadDataAvailable = 0; /* FIXME */
1894 pli->OutboundQuota = reply->outsize;
1895 pli->WriteQuotaAvailable = 0; /* FIXME */
1896 pli->NamedPipeState = 0; /* FIXME */
1897 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1898 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1901 SERVER_END_REQ;
1903 break;
1904 case FileNameInformation:
1906 FILE_NAME_INFORMATION *info = ptr;
1907 ANSI_STRING unix_name;
1909 if (!(io->u.Status = server_get_unix_name( hFile, &unix_name )))
1911 LONG name_len = len - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
1912 io->u.Status = fill_name_info( &unix_name, info, &name_len );
1913 RtlFreeAnsiString( &unix_name );
1914 io->Information = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + name_len;
1917 break;
1918 default:
1919 FIXME("Unsupported class (%d)\n", class);
1920 io->u.Status = STATUS_NOT_IMPLEMENTED;
1921 break;
1923 if (needs_close) close( fd );
1924 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1925 return io->u.Status;
1928 /******************************************************************************
1929 * NtSetInformationFile [NTDLL.@]
1930 * ZwSetInformationFile [NTDLL.@]
1932 * Set information about an open file handle.
1934 * PARAMS
1935 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1936 * io [O] Receives information about the operation on return
1937 * ptr [I] Source for file information
1938 * len [I] Size of FileInformation
1939 * class [I] Type of file information to set
1941 * RETURNS
1942 * Success: 0. io is updated.
1943 * Failure: An NTSTATUS error code describing the error.
1945 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1946 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1948 int fd, needs_close;
1950 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1952 io->u.Status = STATUS_SUCCESS;
1953 switch (class)
1955 case FileBasicInformation:
1956 if (len >= sizeof(FILE_BASIC_INFORMATION))
1958 struct stat st;
1959 const FILE_BASIC_INFORMATION *info = ptr;
1961 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1962 return io->u.Status;
1964 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1966 struct timeval tv[2];
1968 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1971 tv[0].tv_sec = tv[0].tv_usec = 0;
1972 tv[1].tv_sec = tv[1].tv_usec = 0;
1973 if (!fstat( fd, &st ))
1975 tv[0].tv_sec = st.st_atime;
1976 tv[1].tv_sec = st.st_mtime;
1979 if (info->LastAccessTime.QuadPart)
1981 ULONGLONG sec = info->LastAccessTime.QuadPart / 10000000;
1982 UINT nsec = info->LastAccessTime.QuadPart % 10000000;
1983 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1984 tv[0].tv_usec = nsec / 10;
1986 if (info->LastWriteTime.QuadPart)
1988 ULONGLONG sec = info->LastWriteTime.QuadPart / 10000000;
1989 UINT nsec = info->LastWriteTime.QuadPart % 10000000;
1990 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1991 tv[1].tv_usec = nsec / 10;
1993 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1996 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1998 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1999 else
2001 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
2003 if (S_ISDIR( st.st_mode))
2004 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
2005 else
2006 st.st_mode &= ~0222; /* clear write permission bits */
2008 else
2010 /* add write permission only where we already have read permission */
2011 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
2013 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
2017 if (needs_close) close( fd );
2019 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2020 break;
2022 case FilePositionInformation:
2023 if (len >= sizeof(FILE_POSITION_INFORMATION))
2025 const FILE_POSITION_INFORMATION *info = ptr;
2027 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2028 return io->u.Status;
2030 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
2031 io->u.Status = FILE_GetNtStatus();
2033 if (needs_close) close( fd );
2035 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2036 break;
2038 case FileEndOfFileInformation:
2039 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
2041 struct stat st;
2042 const FILE_END_OF_FILE_INFORMATION *info = ptr;
2044 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
2045 return io->u.Status;
2047 /* first try normal truncate */
2048 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2050 /* now check for the need to extend the file */
2051 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
2053 static const char zero;
2055 /* extend the file one byte beyond the requested size and then truncate it */
2056 /* this should work around ftruncate implementations that can't extend files */
2057 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
2058 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
2060 io->u.Status = FILE_GetNtStatus();
2062 if (needs_close) close( fd );
2064 else io->u.Status = STATUS_INVALID_PARAMETER_3;
2065 break;
2067 case FileMailslotSetInformation:
2069 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
2071 SERVER_START_REQ( set_mailslot_info )
2073 req->handle = wine_server_obj_handle( handle );
2074 req->flags = MAILSLOT_SET_READ_TIMEOUT;
2075 req->read_timeout = info->ReadTimeout.QuadPart;
2076 io->u.Status = wine_server_call( req );
2078 SERVER_END_REQ;
2080 break;
2082 case FileCompletionInformation:
2083 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
2085 FILE_COMPLETION_INFORMATION *info = ptr;
2087 SERVER_START_REQ( set_completion_info )
2089 req->handle = wine_server_obj_handle( handle );
2090 req->chandle = wine_server_obj_handle( info->CompletionPort );
2091 req->ckey = info->CompletionKey;
2092 io->u.Status = wine_server_call( req );
2094 SERVER_END_REQ;
2095 } else
2096 io->u.Status = STATUS_INVALID_PARAMETER_3;
2097 break;
2099 default:
2100 FIXME("Unsupported class (%d)\n", class);
2101 io->u.Status = STATUS_NOT_IMPLEMENTED;
2102 break;
2104 io->Information = 0;
2105 return io->u.Status;
2109 /******************************************************************************
2110 * NtQueryFullAttributesFile (NTDLL.@)
2112 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
2113 FILE_NETWORK_OPEN_INFORMATION *info )
2115 ANSI_STRING unix_name;
2116 NTSTATUS status;
2118 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2120 struct stat st;
2122 if (stat( unix_name.Buffer, &st ) == -1)
2123 status = FILE_GetNtStatus();
2124 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2125 status = STATUS_INVALID_INFO_CLASS;
2126 else
2128 FILE_BASIC_INFORMATION basic;
2129 FILE_STANDARD_INFORMATION std;
2131 fill_stat_info( &st, &basic, FileBasicInformation );
2132 fill_stat_info( &st, &std, FileStandardInformation );
2134 info->CreationTime = basic.CreationTime;
2135 info->LastAccessTime = basic.LastAccessTime;
2136 info->LastWriteTime = basic.LastWriteTime;
2137 info->ChangeTime = basic.ChangeTime;
2138 info->AllocationSize = std.AllocationSize;
2139 info->EndOfFile = std.EndOfFile;
2140 info->FileAttributes = basic.FileAttributes;
2141 if (DIR_is_hidden_file( attr->ObjectName ))
2142 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2144 RtlFreeAnsiString( &unix_name );
2146 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2147 return status;
2151 /******************************************************************************
2152 * NtQueryAttributesFile (NTDLL.@)
2153 * ZwQueryAttributesFile (NTDLL.@)
2155 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
2157 ANSI_STRING unix_name;
2158 NTSTATUS status;
2160 if (!(status = nt_to_unix_file_name_attr( attr, &unix_name, FILE_OPEN )))
2162 struct stat st;
2164 if (stat( unix_name.Buffer, &st ) == -1)
2165 status = FILE_GetNtStatus();
2166 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2167 status = STATUS_INVALID_INFO_CLASS;
2168 else
2170 status = fill_stat_info( &st, info, FileBasicInformation );
2171 if (DIR_is_hidden_file( attr->ObjectName ))
2172 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
2174 RtlFreeAnsiString( &unix_name );
2176 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
2177 return status;
2181 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
2182 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2183 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2184 unsigned int flags )
2186 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2188 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2189 /* Don't assume read-only, let the mount options set it below */
2190 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2192 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2193 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2195 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2196 info->Characteristics |= FILE_REMOTE_DEVICE;
2198 else if (!strcmp("procfs", fstypename))
2199 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2200 else
2201 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2203 if (flags & MNT_RDONLY)
2204 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2206 if (!(flags & MNT_LOCAL))
2208 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2209 info->Characteristics |= FILE_REMOTE_DEVICE;
2212 #endif
2214 static inline int is_device_placeholder( int fd )
2216 static const char wine_placeholder[] = "Wine device placeholder";
2217 char buffer[sizeof(wine_placeholder)-1];
2219 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2220 return 0;
2221 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2224 /******************************************************************************
2225 * get_device_info
2227 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2229 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2231 struct stat st;
2233 info->Characteristics = 0;
2234 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2235 if (S_ISCHR( st.st_mode ))
2237 info->DeviceType = FILE_DEVICE_UNKNOWN;
2238 #ifdef linux
2239 switch(major(st.st_rdev))
2241 case MEM_MAJOR:
2242 info->DeviceType = FILE_DEVICE_NULL;
2243 break;
2244 case TTY_MAJOR:
2245 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2246 break;
2247 case LP_MAJOR:
2248 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2249 break;
2250 case SCSI_TAPE_MAJOR:
2251 info->DeviceType = FILE_DEVICE_TAPE;
2252 break;
2254 #endif
2256 else if (S_ISBLK( st.st_mode ))
2258 info->DeviceType = FILE_DEVICE_DISK;
2260 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2262 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2264 else if (is_device_placeholder( fd ))
2266 info->DeviceType = FILE_DEVICE_DISK;
2268 else /* regular file or directory */
2270 #if defined(linux) && defined(HAVE_FSTATFS)
2271 struct statfs stfs;
2273 /* check for floppy disk */
2274 if (major(st.st_dev) == FLOPPY_MAJOR)
2275 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2277 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2278 switch (stfs.f_type)
2280 case 0x9660: /* iso9660 */
2281 case 0x9fa1: /* supermount */
2282 case 0x15013346: /* udf */
2283 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2284 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2285 break;
2286 case 0x6969: /* nfs */
2287 case 0x517B: /* smbfs */
2288 case 0x564c: /* ncpfs */
2289 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2290 info->Characteristics |= FILE_REMOTE_DEVICE;
2291 break;
2292 case 0x01021994: /* tmpfs */
2293 case 0x28cd3d45: /* cramfs */
2294 case 0x1373: /* devfs */
2295 case 0x9fa0: /* procfs */
2296 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2297 break;
2298 default:
2299 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2300 break;
2302 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__APPLE__)
2303 struct statfs stfs;
2305 if (fstatfs( fd, &stfs ) < 0)
2306 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2307 else
2308 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2309 #elif defined(__NetBSD__)
2310 struct statvfs stfs;
2312 if (fstatvfs( fd, &stfs) < 0)
2313 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2314 else
2315 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2316 #elif defined(sun)
2317 /* Use dkio to work out device types */
2319 # include <sys/dkio.h>
2320 # include <sys/vtoc.h>
2321 struct dk_cinfo dkinf;
2322 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2323 if(retval==-1){
2324 WARN("Unable to get disk device type information - assuming a disk like device\n");
2325 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2327 switch (dkinf.dki_ctype)
2329 case DKC_CDROM:
2330 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2331 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2332 break;
2333 case DKC_NCRFLOPPY:
2334 case DKC_SMSFLOPPY:
2335 case DKC_INTEL82072:
2336 case DKC_INTEL82077:
2337 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2338 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2339 break;
2340 case DKC_MD:
2341 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2342 break;
2343 default:
2344 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2347 #else
2348 static int warned;
2349 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2350 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2351 #endif
2352 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2354 return STATUS_SUCCESS;
2358 /******************************************************************************
2359 * NtQueryVolumeInformationFile [NTDLL.@]
2360 * ZwQueryVolumeInformationFile [NTDLL.@]
2362 * Get volume information for an open file handle.
2364 * PARAMS
2365 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2366 * io [O] Receives information about the operation on return
2367 * buffer [O] Destination for volume information
2368 * length [I] Size of FsInformation
2369 * info_class [I] Type of volume information to set
2371 * RETURNS
2372 * Success: 0. io and buffer are updated.
2373 * Failure: An NTSTATUS error code describing the error.
2375 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2376 PVOID buffer, ULONG length,
2377 FS_INFORMATION_CLASS info_class )
2379 int fd, needs_close;
2380 struct stat st;
2381 static int once;
2383 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2384 return io->u.Status;
2386 io->u.Status = STATUS_NOT_IMPLEMENTED;
2387 io->Information = 0;
2389 switch( info_class )
2391 case FileFsVolumeInformation:
2392 if (!once++) FIXME( "%p: volume info not supported\n", handle );
2393 break;
2394 case FileFsLabelInformation:
2395 FIXME( "%p: label info not supported\n", handle );
2396 break;
2397 case FileFsSizeInformation:
2398 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2399 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2400 else
2402 FILE_FS_SIZE_INFORMATION *info = buffer;
2404 if (fstat( fd, &st ) < 0)
2406 io->u.Status = FILE_GetNtStatus();
2407 break;
2409 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2411 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2413 else
2415 ULONGLONG bsize;
2416 /* Linux's fstatvfs is buggy */
2417 #if !defined(linux) || !defined(HAVE_FSTATFS)
2418 struct statvfs stfs;
2420 if (fstatvfs( fd, &stfs ) < 0)
2422 io->u.Status = FILE_GetNtStatus();
2423 break;
2425 bsize = stfs.f_frsize;
2426 #else
2427 struct statfs stfs;
2428 if (fstatfs( fd, &stfs ) < 0)
2430 io->u.Status = FILE_GetNtStatus();
2431 break;
2433 bsize = stfs.f_bsize;
2434 #endif
2435 if (bsize == 2048) /* assume CD-ROM */
2437 info->BytesPerSector = 2048;
2438 info->SectorsPerAllocationUnit = 1;
2440 else
2442 info->BytesPerSector = 512;
2443 info->SectorsPerAllocationUnit = 8;
2445 info->TotalAllocationUnits.QuadPart = bsize * stfs.f_blocks / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2446 info->AvailableAllocationUnits.QuadPart = bsize * stfs.f_bavail / (info->BytesPerSector * info->SectorsPerAllocationUnit);
2447 io->Information = sizeof(*info);
2448 io->u.Status = STATUS_SUCCESS;
2451 break;
2452 case FileFsDeviceInformation:
2453 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2454 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2455 else
2457 FILE_FS_DEVICE_INFORMATION *info = buffer;
2459 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2460 io->Information = sizeof(*info);
2462 break;
2463 case FileFsAttributeInformation:
2464 FIXME( "%p: attribute info not supported\n", handle );
2465 break;
2466 case FileFsControlInformation:
2467 FIXME( "%p: control info not supported\n", handle );
2468 break;
2469 case FileFsFullSizeInformation:
2470 FIXME( "%p: full size info not supported\n", handle );
2471 break;
2472 case FileFsObjectIdInformation:
2473 FIXME( "%p: object id info not supported\n", handle );
2474 break;
2475 case FileFsMaximumInformation:
2476 FIXME( "%p: maximum info not supported\n", handle );
2477 break;
2478 default:
2479 io->u.Status = STATUS_INVALID_PARAMETER;
2480 break;
2482 if (needs_close) close( fd );
2483 return io->u.Status;
2487 /******************************************************************
2488 * NtQueryEaFile (NTDLL.@)
2490 * Read extended attributes from NTFS files.
2492 * PARAMS
2493 * hFile [I] File handle, must be opened with FILE_READ_EA access
2494 * iosb [O] Receives information about the operation on return
2495 * buffer [O] Output buffer
2496 * length [I] Length of output buffer
2497 * single_entry [I] Only read and return one entry
2498 * ea_list [I] Optional list with names of EAs to return
2499 * ea_list_len [I] Length of ea_list in bytes
2500 * ea_index [I] Optional pointer to 1-based index of attribute to return
2501 * restart [I] restart EA scan
2503 * RETURNS
2504 * Success: 0. Atrributes read into buffer
2505 * Failure: An NTSTATUS error code describing the error.
2507 NTSTATUS WINAPI NtQueryEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length,
2508 BOOLEAN single_entry, PVOID ea_list, ULONG ea_list_len,
2509 PULONG ea_index, BOOLEAN restart )
2511 FIXME("(%p,%p,%p,%d,%d,%p,%d,%p,%d) stub\n",
2512 hFile, iosb, buffer, length, single_entry, ea_list,
2513 ea_list_len, ea_index, restart);
2514 return STATUS_ACCESS_DENIED;
2518 /******************************************************************
2519 * NtSetEaFile (NTDLL.@)
2521 * Update extended attributes for NTFS files.
2523 * PARAMS
2524 * hFile [I] File handle, must be opened with FILE_READ_EA access
2525 * iosb [O] Receives information about the operation on return
2526 * buffer [I] Buffer with EA information
2527 * length [I] Length of buffer
2529 * RETURNS
2530 * Success: 0. Attributes are updated
2531 * Failure: An NTSTATUS error code describing the error.
2533 NTSTATUS WINAPI NtSetEaFile( HANDLE hFile, PIO_STATUS_BLOCK iosb, PVOID buffer, ULONG length )
2535 FIXME("(%p,%p,%p,%d) stub\n", hFile, iosb, buffer, length);
2536 return STATUS_ACCESS_DENIED;
2540 /******************************************************************
2541 * NtFlushBuffersFile (NTDLL.@)
2543 * Flush any buffered data on an open file handle.
2545 * PARAMS
2546 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2547 * IoStatusBlock [O] Receives information about the operation on return
2549 * RETURNS
2550 * Success: 0. IoStatusBlock is updated.
2551 * Failure: An NTSTATUS error code describing the error.
2553 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2555 NTSTATUS ret;
2556 HANDLE hEvent = NULL;
2558 SERVER_START_REQ( flush_file )
2560 req->handle = wine_server_obj_handle( hFile );
2561 ret = wine_server_call( req );
2562 hEvent = wine_server_ptr_handle( reply->event );
2564 SERVER_END_REQ;
2565 if (!ret && hEvent)
2567 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2568 NtClose( hEvent );
2570 return ret;
2573 /******************************************************************
2574 * NtLockFile (NTDLL.@)
2578 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2579 PIO_APC_ROUTINE apc, void* apc_user,
2580 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2581 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2582 BOOLEAN exclusive )
2584 NTSTATUS ret;
2585 HANDLE handle;
2586 BOOLEAN async;
2587 static BOOLEAN warn = TRUE;
2589 if (apc || io_status || key)
2591 FIXME("Unimplemented yet parameter\n");
2592 return STATUS_NOT_IMPLEMENTED;
2595 if (apc_user && warn)
2597 FIXME("I/O completion on lock not implemented yet\n");
2598 warn = FALSE;
2601 for (;;)
2603 SERVER_START_REQ( lock_file )
2605 req->handle = wine_server_obj_handle( hFile );
2606 req->offset = offset->QuadPart;
2607 req->count = count->QuadPart;
2608 req->shared = !exclusive;
2609 req->wait = !dont_wait;
2610 ret = wine_server_call( req );
2611 handle = wine_server_ptr_handle( reply->handle );
2612 async = reply->overlapped;
2614 SERVER_END_REQ;
2615 if (ret != STATUS_PENDING)
2617 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2618 return ret;
2621 if (async)
2623 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2624 if (handle) NtClose( handle );
2625 return STATUS_PENDING;
2627 if (handle)
2629 NtWaitForSingleObject( handle, FALSE, NULL );
2630 NtClose( handle );
2632 else
2634 LARGE_INTEGER time;
2636 /* Unix lock conflict, sleep a bit and retry */
2637 time.QuadPart = 100 * (ULONGLONG)10000;
2638 time.QuadPart = -time.QuadPart;
2639 NtDelayExecution( FALSE, &time );
2645 /******************************************************************
2646 * NtUnlockFile (NTDLL.@)
2650 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2651 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2652 PULONG key )
2654 NTSTATUS status;
2656 TRACE( "%p %x%08x %x%08x\n",
2657 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2659 if (io_status || key)
2661 FIXME("Unimplemented yet parameter\n");
2662 return STATUS_NOT_IMPLEMENTED;
2665 SERVER_START_REQ( unlock_file )
2667 req->handle = wine_server_obj_handle( hFile );
2668 req->offset = offset->QuadPart;
2669 req->count = count->QuadPart;
2670 status = wine_server_call( req );
2672 SERVER_END_REQ;
2673 return status;
2676 /******************************************************************
2677 * NtCreateNamedPipeFile (NTDLL.@)
2681 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2682 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2683 ULONG sharing, ULONG dispo, ULONG options,
2684 ULONG pipe_type, ULONG read_mode,
2685 ULONG completion_mode, ULONG max_inst,
2686 ULONG inbound_quota, ULONG outbound_quota,
2687 PLARGE_INTEGER timeout)
2689 NTSTATUS status;
2691 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2692 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2693 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2694 outbound_quota, timeout);
2696 /* assume we only get relative timeout */
2697 if (timeout->QuadPart > 0)
2698 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2700 SERVER_START_REQ( create_named_pipe )
2702 req->access = access;
2703 req->attributes = attr->Attributes;
2704 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2705 req->options = options;
2706 req->flags =
2707 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2708 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2709 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2710 req->maxinstances = max_inst;
2711 req->outsize = outbound_quota;
2712 req->insize = inbound_quota;
2713 req->timeout = timeout->QuadPart;
2714 wine_server_add_data( req, attr->ObjectName->Buffer,
2715 attr->ObjectName->Length );
2716 status = wine_server_call( req );
2717 if (!status) *handle = wine_server_ptr_handle( reply->handle );
2719 SERVER_END_REQ;
2720 return status;
2723 /******************************************************************
2724 * NtDeleteFile (NTDLL.@)
2728 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2730 NTSTATUS status;
2731 HANDLE hFile;
2732 IO_STATUS_BLOCK io;
2734 TRACE("%p\n", ObjectAttributes);
2735 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2736 ObjectAttributes, &io, NULL, 0,
2737 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2738 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2739 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2740 return status;
2743 /******************************************************************
2744 * NtCancelIoFileEx (NTDLL.@)
2748 NTSTATUS WINAPI NtCancelIoFileEx( HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status )
2750 LARGE_INTEGER timeout;
2752 TRACE("%p %p %p\n", hFile, iosb, io_status );
2754 SERVER_START_REQ( cancel_async )
2756 req->handle = wine_server_obj_handle( hFile );
2757 req->iosb = wine_server_client_ptr( iosb );
2758 req->only_thread = FALSE;
2759 io_status->u.Status = wine_server_call( req );
2761 SERVER_END_REQ;
2762 if (io_status->u.Status)
2763 return io_status->u.Status;
2765 /* Let some APC be run, so that we can run the remaining APCs on hFile
2766 * either the cancelation of the pending one, but also the execution
2767 * of the queued APC, but not yet run. This is needed to ensure proper
2768 * clean-up of allocated data.
2770 timeout.u.LowPart = timeout.u.HighPart = 0;
2771 NtDelayExecution( TRUE, &timeout );
2772 return io_status->u.Status;
2775 /******************************************************************
2776 * NtCancelIoFile (NTDLL.@)
2780 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2782 LARGE_INTEGER timeout;
2784 TRACE("%p %p\n", hFile, io_status );
2786 SERVER_START_REQ( cancel_async )
2788 req->handle = wine_server_obj_handle( hFile );
2789 req->iosb = 0;
2790 req->only_thread = TRUE;
2791 io_status->u.Status = wine_server_call( req );
2793 SERVER_END_REQ;
2794 if (io_status->u.Status)
2795 return io_status->u.Status;
2797 /* Let some APC be run, so that we can run the remaining APCs on hFile
2798 * either the cancelation of the pending one, but also the execution
2799 * of the queued APC, but not yet run. This is needed to ensure proper
2800 * clean-up of allocated data.
2802 timeout.u.LowPart = timeout.u.HighPart = 0;
2803 NtDelayExecution( TRUE, &timeout );
2804 return io_status->u.Status;
2807 /******************************************************************************
2808 * NtCreateMailslotFile [NTDLL.@]
2809 * ZwCreateMailslotFile [NTDLL.@]
2811 * PARAMS
2812 * pHandle [O] pointer to receive the handle created
2813 * DesiredAccess [I] access mode (read, write, etc)
2814 * ObjectAttributes [I] fully qualified NT path of the mailslot
2815 * IoStatusBlock [O] receives completion status and other info
2816 * CreateOptions [I]
2817 * MailslotQuota [I]
2818 * MaxMessageSize [I]
2819 * TimeOut [I]
2821 * RETURNS
2822 * An NT status code
2824 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2825 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2826 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2827 PLARGE_INTEGER TimeOut)
2829 LARGE_INTEGER timeout;
2830 NTSTATUS ret;
2832 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2833 pHandle, DesiredAccess, attr, IoStatusBlock,
2834 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2836 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2837 if (!attr) return STATUS_INVALID_PARAMETER;
2838 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2841 * For a NULL TimeOut pointer set the default timeout value
2843 if (!TimeOut)
2844 timeout.QuadPart = -1;
2845 else
2846 timeout.QuadPart = TimeOut->QuadPart;
2848 SERVER_START_REQ( create_mailslot )
2850 req->access = DesiredAccess;
2851 req->attributes = attr->Attributes;
2852 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2853 req->max_msgsize = MaxMessageSize;
2854 req->read_timeout = timeout.QuadPart;
2855 wine_server_add_data( req, attr->ObjectName->Buffer,
2856 attr->ObjectName->Length );
2857 ret = wine_server_call( req );
2858 if( ret == STATUS_SUCCESS )
2859 *pHandle = wine_server_ptr_handle( reply->handle );
2861 SERVER_END_REQ;
2863 return ret;