1 /*
2 * Copyright 1999, 2000 Juergen Schmied
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
32 #endif
33 #ifdef HAVE_LINUX_MAJOR_H
34 # include <linux/major.h>
35 #endif
36 #ifdef HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
38 #endif
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_POLL_H
49 #include <poll.h>
50 #endif
51 #ifdef HAVE_SYS_POLL_H
52 #include <sys/poll.h>
53 #endif
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
56 #endif
57 #ifdef HAVE_UTIME_H
58 # include <utime.h>
59 #endif
60 #ifdef HAVE_SYS_VFS_H
61 # include <sys/vfs.h>
62 #endif
63 #ifdef HAVE_SYS_MOUNT_H
64 # include <sys/mount.h>
65 #endif
66 #ifdef HAVE_SYS_STATFS_H
67 # include <sys/statfs.h>
68 #endif
69
70 #define NONAMELESSUNION
71 #define NONAMELESSSTRUCT
72 #include "ntstatus.h"
73 #define WIN32_NO_STATUS
74 #include "wine/unicode.h"
75 #include "wine/debug.h"
76 #include "wine/server.h"
77 #include "ntdll_misc.h"
78
79 #include "winternl.h"
80 #include "winioctl.h"
81 #include "ddk/ntddser.h"
82
83 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
84
85 mode_t FILE_umask = 0;
86
87 #define SECSPERDAY 86400
88 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
89
90 /**************************************************************************
91 * NtOpenFile [NTDLL.@]
92 * ZwOpenFile [NTDLL.@]
93 *
94 * Open a file.
95 *
96 * PARAMS
97 * handle [O] Variable that receives the file handle on return
98 * access [I] Access desired by the caller to the file
99 * attr [I] Structure describing the file to be opened
100 * io [O] Receives details about the result of the operation
101 * sharing [I] Type of shared access the caller requires
102 * options [I] Options for the file open
103 *
104 * RETURNS
105 * Success: 0. FileHandle and IoStatusBlock are updated.
106 * Failure: An NTSTATUS error code describing the error.
107 */
108 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
109 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
110 ULONG sharing, ULONG options )
111 {
112 return NtCreateFile( handle, access, attr, io, NULL, 0,
113 sharing, FILE_OPEN, options, NULL, 0 );
114 }
115
116 /**************************************************************************
117 * NtCreateFile [NTDLL.@]
118 * ZwCreateFile [NTDLL.@]
119 *
120 * Either create a new file or directory, or open an existing file, device,
121 * directory or volume.
122 *
123 * PARAMS
124 * handle [O] Points to a variable which receives the file handle on return
125 * access [I] Desired access to the file
126 * attr [I] Structure describing the file
127 * io [O] Receives information about the operation on return
128 * alloc_size [I] Initial size of the file in bytes
129 * attributes [I] Attributes to create the file with
130 * sharing [I] Type of shared access the caller would like to the file
131 * disposition [I] Specifies what to do, depending on whether the file already exists
132 * options [I] Options for creating a new file
133 * ea_buffer [I] Pointer to an extended attributes buffer
134 * ea_length [I] Length of ea_buffer
135 *
136 * RETURNS
137 * Success: 0. handle and io are updated.
138 * Failure: An NTSTATUS error code describing the error.
139 */
140 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
141 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
142 ULONG attributes, ULONG sharing, ULONG disposition,
143 ULONG options, PVOID ea_buffer, ULONG ea_length )
144 {
145 ANSI_STRING unix_name;
146 int created = FALSE;
147
148 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p\n"
149 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
150 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
151 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
152 attributes, sharing, disposition, options, ea_buffer, ea_length );
153
154 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
155
156 if (alloc_size) FIXME( "alloc_size not supported\n" );
157
158 if (attr->RootDirectory)
159 {
160 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
161 return STATUS_OBJECT_NAME_NOT_FOUND;
162 }
163
164 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
165 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
166
167 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
168 {
169 SERVER_START_REQ( open_file_object )
170 {
171 req->access = access;
172 req->attributes = attr->Attributes;
173 req->rootdir = attr->RootDirectory;
174 req->sharing = sharing;
175 req->options = options;
176 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
177 io->u.Status = wine_server_call( req );
178 *handle = reply->handle;
179 }
180 SERVER_END_REQ;
181 if (io->u.Status == STATUS_SUCCESS) io->Information = FILE_OPENED;
182 return io->u.Status;
183 }
184
185 if (io->u.Status == STATUS_NO_SUCH_FILE &&
186 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
187 {
188 created = TRUE;
189 io->u.Status = STATUS_SUCCESS;
190 }
191
192 if (io->u.Status == STATUS_SUCCESS)
193 {
194 struct security_descriptor *sd = NULL;
195 struct object_attributes objattr;
196
197 objattr.rootdir = 0;
198 objattr.sd_len = 0;
199 objattr.name_len = 0;
200 if (attr)
201 {
202 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
203 if (io->u.Status != STATUS_SUCCESS)
204 {
205 RtlFreeAnsiString( &unix_name );
206 return io->u.Status;
207 }
208 }
209
210 SERVER_START_REQ( create_file )
211 {
212 req->access = access;
213 req->attributes = attr->Attributes;
214 req->sharing = sharing;
215 req->create = disposition;
216 req->options = options;
217 req->attrs = attributes;
218 wine_server_add_data( req, &objattr, sizeof(objattr) );
219 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
220 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
221 io->u.Status = wine_server_call( req );
222 *handle = reply->handle;
223 }
224 SERVER_END_REQ;
225 NTDLL_free_struct_sd( sd );
226 RtlFreeAnsiString( &unix_name );
227 }
228 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
229
230 if (io->u.Status == STATUS_SUCCESS)
231 {
232 if (created) io->Information = FILE_CREATED;
233 else switch(disposition)
234 {
235 case FILE_SUPERSEDE:
236 io->Information = FILE_SUPERSEDED;
237 break;
238 case FILE_CREATE:
239 io->Information = FILE_CREATED;
240 break;
241 case FILE_OPEN:
242 case FILE_OPEN_IF:
243 io->Information = FILE_OPENED;
244 break;
245 case FILE_OVERWRITE:
246 case FILE_OVERWRITE_IF:
247 io->Information = FILE_OVERWRITTEN;
248 break;
249 }
250 }
251
252 return io->u.Status;
253 }
254
255 /***********************************************************************
256 * Asynchronous file I/O *
257 */
258
259 struct async_fileio
260 {
261 HANDLE handle;
262 PIO_APC_ROUTINE apc;
263 void *apc_arg;
264 };
265
266 typedef struct
267 {
268 struct async_fileio io;
269 char* buffer;
270 unsigned int already;
271 unsigned int count;
272 BOOL avail_mode;
273 } async_fileio_read;
274
275 typedef struct
276 {
277 struct async_fileio io;
278 const char *buffer;
279 unsigned int already;
280 unsigned int count;
281 } async_fileio_write;
282
283
284 /* callback for file I/O user APC */
285 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
286 {
287 struct async_fileio *async = arg;
288 if (async->apc) async->apc( async->apc_arg, io, reserved );
289 RtlFreeHeap( GetProcessHeap(), 0, async );
290 }
291
292 /***********************************************************************
293 * FILE_GetNtStatus(void)
294 *
295 * Retrieve the Nt Status code from errno.
296 * Try to be consistent with FILE_SetDosError().
297 */
298 NTSTATUS FILE_GetNtStatus(void)
299 {
300 int err = errno;
301
302 TRACE( "errno = %d\n", errno );
303 switch (err)
304 {
305 case EAGAIN: return STATUS_SHARING_VIOLATION;
306 case EBADF: return STATUS_INVALID_HANDLE;
307 case EBUSY: return STATUS_DEVICE_BUSY;
308 case ENOSPC: return STATUS_DISK_FULL;
309 case EPERM:
310 case EROFS:
311 case EACCES: return STATUS_ACCESS_DENIED;
312 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
313 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
314 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
315 case EMFILE:
316 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
317 case EINVAL: return STATUS_INVALID_PARAMETER;
318 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
319 case EPIPE: return STATUS_PIPE_DISCONNECTED;
320 case EIO: return STATUS_DEVICE_NOT_READY;
321 #ifdef ENOMEDIUM
322 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
323 #endif
324 case ENXIO: return STATUS_NO_SUCH_DEVICE;
325 case ENOTTY:
326 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
327 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
328 case EFAULT: return STATUS_ACCESS_VIOLATION;
329 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
330 case ENOEXEC: /* ?? */
331 case EEXIST: /* ?? */
332 default:
333 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
334 return STATUS_UNSUCCESSFUL;
335 }
336 }
337
338 /***********************************************************************
339 * FILE_AsyncReadService (INTERNAL)
340 */
341 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, ULONG_PTR *total)
342 {
343 async_fileio_read *fileio = user;
344 int fd, needs_close, result;
345
346 switch (status)
347 {
348 case STATUS_ALERTED: /* got some new data */
349 /* check to see if the data is ready (non-blocking) */
350 if ((status = server_get_unix_fd( fileio->io.handle, FILE_READ_DATA, &fd,
351 &needs_close, NULL, NULL )))
352 break;
353
354 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
355 if (needs_close) close( fd );
356
357 if (result < 0)
358 {
359 if (errno == EAGAIN || errno == EINTR)
360 status = STATUS_PENDING;
361 else /* check to see if the transfer is complete */
362 status = FILE_GetNtStatus();
363 }
364 else if (result == 0)
365 {
366 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
367 }
368 else
369 {
370 fileio->already += result;
371 if (fileio->already >= fileio->count || fileio->avail_mode)
372 status = STATUS_SUCCESS;
373 else
374 {
375 /* if we only have to read the available data, and none is available,
376 * simply cancel the request. If data was available, it has been read
377 * while in by previous call (NtDelayExecution)
378 */
379 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
380 }
381 }
382 break;
383
384 case STATUS_TIMEOUT:
385 case STATUS_IO_TIMEOUT:
386 if (fileio->already) status = STATUS_SUCCESS;
387 break;
388 }
389 if (status != STATUS_PENDING)
390 {
391 iosb->u.Status = status;
392 iosb->Information = *total = fileio->already;
393 }
394 return status;
395 }
396
397 struct io_timeouts
398 {
399 int interval; /* max interval between two bytes */
400 int total; /* total timeout for the whole operation */
401 int end_time; /* absolute time of end of operation */
402 };
403
404 /* retrieve the I/O timeouts to use for a given handle */
405 static NTSTATUS get_io_timeouts( HANDLE handle, enum server_fd_type type, ULONG count, BOOL is_read,
406 struct io_timeouts *timeouts )
407 {
408 NTSTATUS status = STATUS_SUCCESS;
409
410 timeouts->interval = timeouts->total = -1;
411
412 switch(type)
413 {
414 case FD_TYPE_SERIAL:
415 {
416 /* GetCommTimeouts */
417 SERIAL_TIMEOUTS st;
418 IO_STATUS_BLOCK io;
419
420 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
421 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
422 if (status) break;
423
424 if (is_read)
425 {
426 if (st.ReadIntervalTimeout)
427 timeouts->interval = st.ReadIntervalTimeout;
428
429 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
430 {
431 timeouts->total = st.ReadTotalTimeoutConstant;
432 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
433 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
434 }
435 else if (st.ReadIntervalTimeout == MAXDWORD)
436 timeouts->interval = timeouts->total = 0;
437 }
438 else /* write */
439 {
440 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
441 {
442 timeouts->total = st.WriteTotalTimeoutConstant;
443 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
444 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
445 }
446 }
447 }
448 break;
449 case FD_TYPE_MAILSLOT:
450 if (is_read)
451 {
452 timeouts->interval = 0; /* return as soon as we got something */
453 SERVER_START_REQ( set_mailslot_info )
454 {
455 req->handle = handle;
456 req->flags = 0;
457 if (!(status = wine_server_call( req )) &&
458 reply->read_timeout != TIMEOUT_INFINITE)
459 timeouts->total = reply->read_timeout / -10000;
460 }
461 SERVER_END_REQ;
462 }
463 break;
464 case FD_TYPE_SOCKET:
465 case FD_TYPE_PIPE:
466 case FD_TYPE_CHAR:
467 if (is_read) timeouts->interval = 0; /* return as soon as we got something */
468 break;
469 default:
470 break;
471 }
472 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
473 return STATUS_SUCCESS;
474 }
475
476
477 /* retrieve the timeout for the next wait, in milliseconds */
478 static inline int get_next_io_timeout( const struct io_timeouts *timeouts, ULONG already )
479 {
480 int ret = -1;
481
482 if (timeouts->total != -1)
483 {
484 ret = timeouts->end_time - NtGetTickCount();
485 if (ret < 0) ret = 0;
486 }
487 if (already && timeouts->interval != -1)
488 {
489 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
490 }
491 return ret;
492 }
493
494
495 /* retrieve the avail_mode flag for async reads */
496 static NTSTATUS get_io_avail_mode( HANDLE handle, enum server_fd_type type, BOOL *avail_mode )
497 {
498 NTSTATUS status = STATUS_SUCCESS;
499
500 switch(type)
501 {
502 case FD_TYPE_SERIAL:
503 {
504 /* GetCommTimeouts */
505 SERIAL_TIMEOUTS st;
506 IO_STATUS_BLOCK io;
507
508 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
509 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
510 if (status) break;
511 *avail_mode = (!st.ReadTotalTimeoutMultiplier &&
512 !st.ReadTotalTimeoutConstant &&
513 st.ReadIntervalTimeout == MAXDWORD);
514 }
515 break;
516 case FD_TYPE_MAILSLOT:
517 case FD_TYPE_SOCKET:
518 case FD_TYPE_PIPE:
519 case FD_TYPE_CHAR:
520 *avail_mode = TRUE;
521 break;
522 default:
523 *avail_mode = FALSE;
524 break;
525 }
526 return status;
527 }
528
529
530 /******************************************************************************
531 * NtReadFile [NTDLL.@]
532 * ZwReadFile [NTDLL.@]
533 *
534 * Read from an open file handle.
535 *
536 * PARAMS
537 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
538 * Event [I] Event to signal upon completion (or NULL)
539 * ApcRoutine [I] Callback to call upon completion (or NULL)
540 * ApcContext [I] Context for ApcRoutine (or NULL)
541 * IoStatusBlock [O] Receives information about the operation on return
542 * Buffer [O] Destination for the data read
543 * Length [I] Size of Buffer
544 * ByteOffset [O] Destination for the new file pointer position (or NULL)
545 * Key [O] Function unknown (may be NULL)
546 *
547 * RETURNS
548 * Success: 0. IoStatusBlock is updated, and the Information member contains
549 * The number of bytes read.
550 * Failure: An NTSTATUS error code describing the error.
551 */
552 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
553 PIO_APC_ROUTINE apc, void* apc_user,
554 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
555 PLARGE_INTEGER offset, PULONG key)
556 {
557 int result, unix_handle, needs_close, timeout_init_done = 0;
558 unsigned int options;
559 struct io_timeouts timeouts;
560 NTSTATUS status;
561 ULONG total = 0;
562 enum server_fd_type type;
563 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
564
565 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
566 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
567
568 if (!io_status) return STATUS_ACCESS_VIOLATION;
569
570 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
571 &needs_close, &type, &options );
572 if (status) return status;
573
574 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
575 {
576 /* async I/O doesn't make sense on regular files */
577 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
578 {
579 if (errno != EINTR)
580 {
581 status = FILE_GetNtStatus();
582 goto done;
583 }
584 }
585 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
586 /* update file pointer position */
587 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
588
589 total = result;
590 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
591 goto done;
592 }
593
594 for (;;)
595 {
596 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
597 {
598 total += result;
599 if (!result || total == length)
600 {
601 if (total)
602 {
603 status = STATUS_SUCCESS;
604 goto done;
605 }
606 switch (type)
607 {
608 case FD_TYPE_FILE:
609 case FD_TYPE_CHAR:
610 status = STATUS_END_OF_FILE;
611 goto done;
612 case FD_TYPE_SERIAL:
613 break;
614 default:
615 status = STATUS_PIPE_BROKEN;
616 goto done;
617 }
618 }
619 }
620 else
621 {
622 if (errno == EINTR) continue;
623 if (errno != EAGAIN)
624 {
625 status = FILE_GetNtStatus();
626 goto done;
627 }
628 }
629
630 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
631 {
632 async_fileio_read *fileio;
633 BOOL avail_mode;
634
635 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
636 goto err;
637 if (total && avail_mode)
638 {
639 status = STATUS_SUCCESS;
640 goto done;
641 }
642
643 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
644 {
645 status = STATUS_NO_MEMORY;
646 goto err;
647 }
648 fileio->io.handle = hFile;
649 fileio->io.apc = apc;
650 fileio->io.apc_arg = apc_user;
651 fileio->already = total;
652 fileio->count = length;
653 fileio->buffer = buffer;
654 fileio->avail_mode = avail_mode;
655
656 SERVER_START_REQ( register_async )
657 {
658 req->handle = hFile;
659 req->type = ASYNC_TYPE_READ;
660 req->count = length;
661 req->async.callback = FILE_AsyncReadService;
662 req->async.iosb = io_status;
663 req->async.arg = fileio;
664 req->async.apc = fileio_apc;
665 req->async.event = hEvent;
666 req->async.cvalue = cvalue;
667 status = wine_server_call( req );
668 }
669 SERVER_END_REQ;
670
671 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
672 goto err;
673 }
674 else /* synchronous read, wait for the fd to become ready */
675 {
676 struct pollfd pfd;
677 int ret, timeout;
678
679 if (!timeout_init_done)
680 {
681 timeout_init_done = 1;
682 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
683 goto err;
684 if (hEvent) NtResetEvent( hEvent, NULL );
685 }
686 timeout = get_next_io_timeout( &timeouts, total );
687
688 pfd.fd = unix_handle;
689 pfd.events = POLLIN;
690
691 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
692 {
693 if (total) /* return with what we got so far */
694 status = STATUS_SUCCESS;
695 else
696 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
697 goto done;
698 }
699 if (ret == -1 && errno != EINTR)
700 {
701 status = FILE_GetNtStatus();
702 goto done;
703 }
704 /* will now restart the read */
705 }
706 }
707
708 done:
709 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
710
711 err:
712 if (needs_close) close( unix_handle );
713 if (status == STATUS_SUCCESS)
714 {
715 io_status->u.Status = status;
716 io_status->Information = total;
717 TRACE("= SUCCESS (%u)\n", total);
718 if (hEvent) NtSetEvent( hEvent, NULL );
719 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
720 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
721 }
722 else
723 {
724 TRACE("= 0x%08x\n", status);
725 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
726 }
727 return status;
728 }
729
730
731 /******************************************************************************
732 * NtReadFileScatter [NTDLL.@]
733 * ZwReadFileScatter [NTDLL.@]
734 */
735 NTSTATUS WINAPI NtReadFileScatter( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
736 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
737 ULONG length, PLARGE_INTEGER offset, PULONG key )
738 {
739 size_t page_size = getpagesize();
740 int result, unix_handle, needs_close;
741 unsigned int options;
742 NTSTATUS status;
743 ULONG pos = 0, total = 0;
744 enum server_fd_type type;
745 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
746
747 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
748 file, event, apc, apc_user, io_status, segments, length, offset, key);
749
750 if (length % page_size) return STATUS_INVALID_PARAMETER;
751 if (!io_status) return STATUS_ACCESS_VIOLATION;
752
753 status = server_get_unix_fd( file, FILE_READ_DATA, &unix_handle,
754 &needs_close, &type, &options );
755 if (status) return status;
756
757 if ((type != FD_TYPE_FILE) ||
758 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
759 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
760 {
761 status = STATUS_INVALID_PARAMETER;
762 goto error;
763 }
764
765 while (length)
766 {
767 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
768 result = pread( unix_handle, (char *)segments->Buffer + pos,
769 page_size - pos, offset->QuadPart + total );
770 else
771 result = read( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
772
773 if (result == -1)
774 {
775 if (errno == EINTR) continue;
776 status = FILE_GetNtStatus();
777 break;
778 }
779 if (!result)
780 {
781 status = STATUS_END_OF_FILE;
782 break;
783 }
784 total += result;
785 length -= result;
786 if ((pos += result) == page_size)
787 {
788 pos = 0;
789 segments++;
790 }
791 }
792
793 if (cvalue) NTDLL_AddCompletion( file, cvalue, status, total );
794
795 error:
796 if (needs_close) close( unix_handle );
797 if (status == STATUS_SUCCESS)
798 {
799 io_status->u.Status = status;
800 io_status->Information = total;
801 TRACE("= SUCCESS (%u)\n", total);
802 if (event) NtSetEvent( event, NULL );
803 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
804 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
805 }
806 else
807 {
808 TRACE("= 0x%08x\n", status);
809 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
810 }
811 return status;
812 }
813
814
815 /***********************************************************************
816 * FILE_AsyncWriteService (INTERNAL)
817 */
818 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status, ULONG_PTR *total)
819 {
820 async_fileio_write *fileio = user;
821 int result, fd, needs_close;
822 enum server_fd_type type;
823
824 switch (status)
825 {
826 case STATUS_ALERTED:
827 /* write some data (non-blocking) */
828 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
829 &needs_close, &type, NULL )))
830 break;
831
832 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
833 result = send( fd, fileio->buffer, 0, 0 );
834 else
835 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
836
837 if (needs_close) close( fd );
838
839 if (result < 0)
840 {
841 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
842 else status = FILE_GetNtStatus();
843 }
844 else
845 {
846 fileio->already += result;
847 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
848 }
849 break;
850
851 case STATUS_TIMEOUT:
852 case STATUS_IO_TIMEOUT:
853 if (fileio->already) status = STATUS_SUCCESS;
854 break;
855 }
856 if (status != STATUS_PENDING)
857 {
858 iosb->u.Status = status;
859 iosb->Information = *total = fileio->already;
860 }
861 return status;
862 }
863
864 /******************************************************************************
865 * NtWriteFile [NTDLL.@]
866 * ZwWriteFile [NTDLL.@]
867 *
868 * Write to an open file handle.
869 *
870 * PARAMS
871 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
872 * Event [I] Event to signal upon completion (or NULL)
873 * ApcRoutine [I] Callback to call upon completion (or NULL)
874 * ApcContext [I] Context for ApcRoutine (or NULL)
875 * IoStatusBlock [O] Receives information about the operation on return
876 * Buffer [I] Source for the data to write
877 * Length [I] Size of Buffer
878 * ByteOffset [O] Destination for the new file pointer position (or NULL)
879 * Key [O] Function unknown (may be NULL)
880 *
881 * RETURNS
882 * Success: 0. IoStatusBlock is updated, and the Information member contains
883 * The number of bytes written.
884 * Failure: An NTSTATUS error code describing the error.
885 */
886 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
887 PIO_APC_ROUTINE apc, void* apc_user,
888 PIO_STATUS_BLOCK io_status,
889 const void* buffer, ULONG length,
890 PLARGE_INTEGER offset, PULONG key)
891 {
892 int result, unix_handle, needs_close, timeout_init_done = 0;
893 unsigned int options;
894 struct io_timeouts timeouts;
895 NTSTATUS status;
896 ULONG total = 0;
897 enum server_fd_type type;
898 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
899
900 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
901 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
902
903 if (!io_status) return STATUS_ACCESS_VIOLATION;
904
905 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
906 &needs_close, &type, &options );
907 if (status) return status;
908
909 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
910 {
911 /* async I/O doesn't make sense on regular files */
912 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
913 {
914 if (errno != EINTR)
915 {
916 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
917 else status = FILE_GetNtStatus();
918 goto done;
919 }
920 }
921
922 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
923 /* update file pointer position */
924 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
925
926 total = result;
927 status = STATUS_SUCCESS;
928 goto done;
929 }
930
931 for (;;)
932 {
933 /* zero-length writes on sockets may not work with plain write(2) */
934 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
935 result = send( unix_handle, buffer, 0, 0 );
936 else
937 result = write( unix_handle, (const char *)buffer + total, length - total );
938
939 if (result >= 0)
940 {
941 total += result;
942 if (total == length)
943 {
944 status = STATUS_SUCCESS;
945 goto done;
946 }
947 }
948 else
949 {
950 if (errno == EINTR) continue;
951 if (errno != EAGAIN)
952 {
953 if (errno == EFAULT)
954 {
955 status = STATUS_INVALID_USER_BUFFER;
956 goto err;
957 }
958 status = FILE_GetNtStatus();
959 goto done;
960 }
961 }
962
963 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
964 {
965 async_fileio_write *fileio;
966
967 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
968 {
969 status = STATUS_NO_MEMORY;
970 goto err;
971 }
972 fileio->io.handle = hFile;
973 fileio->io.apc = apc;
974 fileio->io.apc_arg = apc_user;
975 fileio->already = total;
976 fileio->count = length;
977 fileio->buffer = buffer;
978
979 SERVER_START_REQ( register_async )
980 {
981 req->handle = hFile;
982 req->type = ASYNC_TYPE_WRITE;
983 req->count = length;
984 req->async.callback = FILE_AsyncWriteService;
985 req->async.iosb = io_status;
986 req->async.arg = fileio;
987 req->async.apc = fileio_apc;
988 req->async.event = hEvent;
989 req->async.cvalue = cvalue;
990 status = wine_server_call( req );
991 }
992 SERVER_END_REQ;
993
994 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
995 goto err;
996 }
997 else /* synchronous write, wait for the fd to become ready */
998 {
999 struct pollfd pfd;
1000 int ret, timeout;
1001
1002 if (!timeout_init_done)
1003 {
1004 timeout_init_done = 1;
1005 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
1006 goto err;
1007 if (hEvent) NtResetEvent( hEvent, NULL );
1008 }
1009 timeout = get_next_io_timeout( &timeouts, total );
1010
1011 pfd.fd = unix_handle;
1012 pfd.events = POLLOUT;
1013
1014 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
1015 {
1016 /* return with what we got so far */
1017 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
1018 goto done;
1019 }
1020 if (ret == -1 && errno != EINTR)
1021 {
1022 status = FILE_GetNtStatus();
1023 goto done;
1024 }
1025 /* will now restart the write */
1026 }
1027 }
1028
1029 done:
1030 if (cvalue) NTDLL_AddCompletion( hFile, cvalue, status, total );
1031
1032 err:
1033 if (needs_close) close( unix_handle );
1034 if (status == STATUS_SUCCESS)
1035 {
1036 io_status->u.Status = status;
1037 io_status->Information = total;
1038 TRACE("= SUCCESS (%u)\n", total);
1039 if (hEvent) NtSetEvent( hEvent, NULL );
1040 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1041 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1042 }
1043 else
1044 {
1045 TRACE("= 0x%08x\n", status);
1046 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
1047 }
1048 return status;
1049 }
1050
1051
1052 /******************************************************************************
1053 * NtWriteFileGather [NTDLL.@]
1054 * ZwWriteFileGather [NTDLL.@]
1055 */
1056 NTSTATUS WINAPI NtWriteFileGather( HANDLE file, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1057 PIO_STATUS_BLOCK io_status, FILE_SEGMENT_ELEMENT *segments,
1058 ULONG length, PLARGE_INTEGER offset, PULONG key )
1059 {
1060 size_t page_size = getpagesize();
1061 int result, unix_handle, needs_close;
1062 unsigned int options;
1063 NTSTATUS status;
1064 ULONG pos = 0, total = 0;
1065 enum server_fd_type type;
1066 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_user;
1067
1068 TRACE( "(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
1069 file, event, apc, apc_user, io_status, segments, length, offset, key);
1070
1071 if (length % page_size) return STATUS_INVALID_PARAMETER;
1072 if (!io_status) return STATUS_ACCESS_VIOLATION;
1073
1074 status = server_get_unix_fd( file, FILE_WRITE_DATA, &unix_handle,
1075 &needs_close, &type, &options );
1076 if (status) return status;
1077
1078 if ((type != FD_TYPE_FILE) ||
1079 (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) ||
1080 !(options & FILE_NO_INTERMEDIATE_BUFFERING))
1081 {
1082 status = STATUS_INVALID_PARAMETER;
1083 goto error;
1084 }
1085
1086 while (length)
1087 {
1088 if (offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */)
1089 result = pwrite( unix_handle, (char *)segments->Buffer + pos,
1090 page_size - pos, offset->QuadPart + total );
1091 else
1092 result = write( unix_handle, (char *)segments->Buffer + pos, page_size - pos );
1093
1094 if (result == -1)
1095 {
1096 if (errno == EINTR) continue;
1097 if (errno == EFAULT)
1098 {
1099 status = STATUS_INVALID_USER_BUFFER;
1100 goto error;
1101 }
1102 status = FILE_GetNtStatus();
1103 break;
1104 }
1105 if (!result)
1106 {
1107 status = STATUS_DISK_FULL;
1108 break;
1109 }
1110 total += result;
1111 length -= result;
1112 if ((pos += result) == page_size)
1113 {
1114 pos = 0;
1115 segments++;
1116 }
1117 }
1118
1119 if (cvalue) NTDLL_AddCompletion( file, cvalue, status, total );
1120
1121 error:
1122 if (needs_close) close( unix_handle );
1123 if (status == STATUS_SUCCESS)
1124 {
1125 io_status->u.Status = status;
1126 io_status->Information = total;
1127 TRACE("= SUCCESS (%u)\n", total);
1128 if (event) NtSetEvent( event, NULL );
1129 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
1130 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
1131 }
1132 else
1133 {
1134 TRACE("= 0x%08x\n", status);
1135 if (status != STATUS_PENDING && event) NtResetEvent( event, NULL );
1136 }
1137 return status;
1138 }
1139
1140
1141 struct async_ioctl
1142 {
1143 HANDLE handle; /* handle to the device */
1144 void *buffer; /* buffer for output */
1145 ULONG size; /* size of buffer */
1146 PIO_APC_ROUTINE apc; /* user apc params */
1147 void *apc_arg;
1148 };
1149
1150 /* callback for ioctl async I/O completion */
1151 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status )
1152 {
1153 struct async_ioctl *async = arg;
1154
1155 if (status == STATUS_ALERTED)
1156 {
1157 SERVER_START_REQ( get_ioctl_result )
1158 {
1159 req->handle = async->handle;
1160 req->user_arg = async;
1161 wine_server_set_reply( req, async->buffer, async->size );
1162 if (!(status = wine_server_call( req )))
1163 io->Information = wine_server_reply_size( reply );
1164 }
1165 SERVER_END_REQ;
1166 }
1167 if (status != STATUS_PENDING) io->u.Status = status;
1168 return status;
1169 }
1170
1171 /* callback for ioctl user APC */
1172 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
1173 {
1174 struct async_ioctl *async = arg;
1175 if (async->apc) async->apc( async->apc_arg, io, reserved );
1176 RtlFreeHeap( GetProcessHeap(), 0, async );
1177 }
1178
1179 /* do a ioctl call through the server */
1180 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
1181 PIO_APC_ROUTINE apc, PVOID apc_context,
1182 IO_STATUS_BLOCK *io, ULONG code,
1183 const void *in_buffer, ULONG in_size,
1184 PVOID out_buffer, ULONG out_size )
1185 {
1186 struct async_ioctl *async;
1187 NTSTATUS status;
1188 HANDLE wait_handle;
1189 ULONG options;
1190 ULONG_PTR cvalue = apc ? 0 : (ULONG_PTR)apc_context;
1191
1192 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
1193 return STATUS_NO_MEMORY;
1194 async->handle = handle;
1195 async->buffer = out_buffer;
1196 async->size = out_size;
1197 async->apc = apc;
1198 async->apc_arg = apc_context;
1199
1200 SERVER_START_REQ( ioctl )
1201 {
1202 req->handle = handle;
1203 req->code = code;
1204 req->async.callback = ioctl_completion;
1205 req->async.iosb = io;
1206 req->async.arg = async;
1207 req->async.apc = (apc || event) ? ioctl_apc : NULL;
1208 req->async.event = event;
1209 req->async.cvalue = cvalue;
1210 wine_server_add_data( req, in_buffer, in_size );
1211 wine_server_set_reply( req, out_buffer, out_size );
1212 if (!(status = wine_server_call( req )))
1213 io->Information = wine_server_reply_size( reply );
1214 wait_handle = reply->wait;
1215 options = reply->options;
1216 }
1217 SERVER_END_REQ;
1218
1219 if (status == STATUS_NOT_SUPPORTED)
1220 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1221 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1222
1223 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1224
1225 if (wait_handle)
1226 {
1227 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1228 status = io->u.Status;
1229 NtClose( wait_handle );
1230 RtlFreeHeap( GetProcessHeap(), 0, async );
1231 }
1232
1233 return status;
1234 }
1235
1236
1237 /**************************************************************************
1238 * NtDeviceIoControlFile [NTDLL.@]
1239 * ZwDeviceIoControlFile [NTDLL.@]
1240 *
1241 * Perform an I/O control operation on an open file handle.
1242 *
1243 * PARAMS
1244 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1245 * event [I] Event to signal upon completion (or NULL)
1246 * apc [I] Callback to call upon completion (or NULL)
1247 * apc_context [I] Context for ApcRoutine (or NULL)
1248 * io [O] Receives information about the operation on return
1249 * code [I] Control code for the operation to perform
1250 * in_buffer [I] Source for any input data required (or NULL)
1251 * in_size [I] Size of InputBuffer
1252 * out_buffer [O] Source for any output data returned (or NULL)
1253 * out_size [I] Size of OutputBuffer
1254 *
1255 * RETURNS
1256 * Success: 0. IoStatusBlock is updated.
1257 * Failure: An NTSTATUS error code describing the error.
1258 */
1259 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1260 PIO_APC_ROUTINE apc, PVOID apc_context,
1261 PIO_STATUS_BLOCK io, ULONG code,
1262 PVOID in_buffer, ULONG in_size,
1263 PVOID out_buffer, ULONG out_size)
1264 {
1265 ULONG device = (code >> 16);
1266 NTSTATUS status = STATUS_NOT_SUPPORTED;
1267
1268 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1269 handle, event, apc, apc_context, io, code,
1270 in_buffer, in_size, out_buffer, out_size);
1271
1272 switch(device)
1273 {
1274 case FILE_DEVICE_DISK:
1275 case FILE_DEVICE_CD_ROM:
1276 case FILE_DEVICE_DVD:
1277 case FILE_DEVICE_CONTROLLER:
1278 case FILE_DEVICE_MASS_STORAGE:
1279 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1280 in_buffer, in_size, out_buffer, out_size);
1281 break;
1282 case FILE_DEVICE_SERIAL_PORT:
1283 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1284 in_buffer, in_size, out_buffer, out_size);
1285 break;
1286 case FILE_DEVICE_TAPE:
1287 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1288 in_buffer, in_size, out_buffer, out_size);
1289 break;
1290 }
1291
1292 if (status == STATUS_NOT_SUPPORTED || status == STATUS_BAD_DEVICE_TYPE)
1293 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1294 in_buffer, in_size, out_buffer, out_size );
1295
1296 if (status != STATUS_PENDING) io->u.Status = status;
1297 return status;
1298 }
1299
1300
1301 /**************************************************************************
1302 * NtFsControlFile [NTDLL.@]
1303 * ZwFsControlFile [NTDLL.@]
1304 *
1305 * Perform a file system control operation on an open file handle.
1306 *
1307 * PARAMS
1308 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1309 * event [I] Event to signal upon completion (or NULL)
1310 * apc [I] Callback to call upon completion (or NULL)
1311 * apc_context [I] Context for ApcRoutine (or NULL)
1312 * io [O] Receives information about the operation on return
1313 * code [I] Control code for the operation to perform
1314 * in_buffer [I] Source for any input data required (or NULL)
1315 * in_size [I] Size of InputBuffer
1316 * out_buffer [O] Source for any output data returned (or NULL)
1317 * out_size [I] Size of OutputBuffer
1318 *
1319 * RETURNS
1320 * Success: 0. IoStatusBlock is updated.
1321 * Failure: An NTSTATUS error code describing the error.
1322 */
1323 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1324 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1325 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1326 {
1327 NTSTATUS status;
1328
1329 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1330 handle, event, apc, apc_context, io, code,
1331 in_buffer, in_size, out_buffer, out_size);
1332
1333 if (!io) return STATUS_INVALID_PARAMETER;
1334
1335 switch(code)
1336 {
1337 case FSCTL_DISMOUNT_VOLUME:
1338 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1339 in_buffer, in_size, out_buffer, out_size );
1340 if (!status) status = DIR_unmount_device( handle );
1341 break;
1342
1343 case FSCTL_PIPE_PEEK:
1344 {
1345 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1346 int avail = 0, fd, needs_close;
1347
1348 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1349 {
1350 status = STATUS_INFO_LENGTH_MISMATCH;
1351 break;
1352 }
1353
1354 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1355 break;
1356
1357 #ifdef FIONREAD
1358 if (ioctl( fd, FIONREAD, &avail ) != 0)
1359 {
1360 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1361 if (needs_close) close( fd );
1362 status = FILE_GetNtStatus();
1363 break;
1364 }
1365 #endif
1366 if (!avail) /* check for closed pipe */
1367 {
1368 struct pollfd pollfd;
1369 int ret;
1370
1371 pollfd.fd = fd;
1372 pollfd.events = POLLIN;
1373 pollfd.revents = 0;
1374 ret = poll( &pollfd, 1, 0 );
1375 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1376 {
1377 if (needs_close) close( fd );
1378 status = STATUS_PIPE_BROKEN;
1379 break;
1380 }
1381 }
1382 buffer->NamedPipeState = 0; /* FIXME */
1383 buffer->ReadDataAvailable = avail;
1384 buffer->NumberOfMessages = 0; /* FIXME */
1385 buffer->MessageLength = 0; /* FIXME */
1386 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1387 status = STATUS_SUCCESS;
1388 if (avail)
1389 {
1390 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1391 if (data_size)
1392 {
1393 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1394 if (res >= 0) io->Information += res;
1395 }
1396 }
1397 if (needs_close) close( fd );
1398 }
1399 break;
1400
1401 case FSCTL_PIPE_DISCONNECT:
1402 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1403 in_buffer, in_size, out_buffer, out_size );
1404 if (!status)
1405 {
1406 int fd = server_remove_fd_from_cache( handle );
1407 if (fd != -1) close( fd );
1408 }
1409 break;
1410
1411 case FSCTL_PIPE_IMPERSONATE:
1412 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1413 status = RtlImpersonateSelf( SecurityImpersonation );
1414 break;
1415
1416 case FSCTL_LOCK_VOLUME:
1417 case FSCTL_UNLOCK_VOLUME:
1418 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1419 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1420 status = STATUS_SUCCESS;
1421 break;
1422
1423 case FSCTL_PIPE_LISTEN:
1424 case FSCTL_PIPE_WAIT:
1425 default:
1426 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1427 in_buffer, in_size, out_buffer, out_size );
1428 break;
1429 }
1430
1431 if (status != STATUS_PENDING) io->u.Status = status;
1432 return status;
1433 }
1434
1435 /******************************************************************************
1436 * NtSetVolumeInformationFile [NTDLL.@]
1437 * ZwSetVolumeInformationFile [NTDLL.@]
1438 *
1439 * Set volume information for an open file handle.
1440 *
1441 * PARAMS
1442 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1443 * IoStatusBlock [O] Receives information about the operation on return
1444 * FsInformation [I] Source for volume information
1445 * Length [I] Size of FsInformation
1446 * FsInformationClass [I] Type of volume information to set
1447 *
1448 * RETURNS
1449 * Success: 0. IoStatusBlock is updated.
1450 * Failure: An NTSTATUS error code describing the error.
1451 */
1452 NTSTATUS WINAPI NtSetVolumeInformationFile(
1453 IN HANDLE FileHandle,
1454 PIO_STATUS_BLOCK IoStatusBlock,
1455 PVOID FsInformation,
1456 ULONG Length,
1457 FS_INFORMATION_CLASS FsInformationClass)
1458 {
1459 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1460 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1461 return 0;
1462 }
1463
1464 /******************************************************************************
1465 * NtQueryInformationFile [NTDLL.@]
1466 * ZwQueryInformationFile [NTDLL.@]
1467 *
1468 * Get information about an open file handle.
1469 *
1470 * PARAMS
1471 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1472 * io [O] Receives information about the operation on return
1473 * ptr [O] Destination for file information
1474 * len [I] Size of FileInformation
1475 * class [I] Type of file information to get
1476 *
1477 * RETURNS
1478 * Success: 0. IoStatusBlock and FileInformation are updated.
1479 * Failure: An NTSTATUS error code describing the error.
1480 */
1481 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1482 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1483 {
1484 static const size_t info_sizes[] =
1485 {
1486 0,
1487 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1488 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1489 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1490 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1491 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1492 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1493 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1494 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1495 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1496 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1497 0, /* FileLinkInformation */
1498 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1499 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1500 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1501 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1502 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1503 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1504 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1505 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1506 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1507 0, /* FileAlternateNameInformation */
1508 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1509 0, /* FilePipeInformation */
1510 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1511 0, /* FilePipeRemoteInformation */
1512 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1513 0, /* FileMailslotSetInformation */
1514 0, /* FileCompressionInformation */
1515 0, /* FileObjectIdInformation */
1516 0, /* FileCompletionInformation */
1517 0, /* FileMoveClusterInformation */
1518 0, /* FileQuotaInformation */
1519 0, /* FileReparsePointInformation */
1520 0, /* FileNetworkOpenInformation */
1521 0, /* FileAttributeTagInformation */
1522 0 /* FileTrackingInformation */
1523 };
1524
1525 struct stat st;
1526 int fd, needs_close = FALSE;
1527
1528 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1529
1530 io->Information = 0;
1531
1532 if (class <= 0 || class >= FileMaximumInformation)
1533 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1534 if (!info_sizes[class])
1535 {
1536 FIXME("Unsupported class (%d)\n", class);
1537 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1538 }
1539 if (len < info_sizes[class])
1540 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1541
1542 if (class != FilePipeLocalInformation)
1543 {
1544 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1545 return io->u.Status;
1546 }
1547
1548 switch (class)
1549 {
1550 case FileBasicInformation:
1551 {
1552 FILE_BASIC_INFORMATION *info = ptr;
1553
1554 if (fstat( fd, &st ) == -1)
1555 io->u.Status = FILE_GetNtStatus();
1556 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1557 io->u.Status = STATUS_INVALID_INFO_CLASS;
1558 else
1559 {
1560 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1561 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1562 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1563 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1564 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1565 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1566 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1567 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1568 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1569 info->CreationTime.QuadPart += st.st_mtim.tv_nsec / 100;
1570 info->LastWriteTime.QuadPart += st.st_mtim.tv_nsec / 100;
1571 #endif
1572 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1573 info->ChangeTime.QuadPart += st.st_ctim.tv_nsec / 100;
1574 #endif
1575 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1576 info->LastAccessTime.QuadPart += st.st_atim.tv_nsec / 100;
1577 #endif
1578 }
1579 }
1580 break;
1581 case FileStandardInformation:
1582 {
1583 FILE_STANDARD_INFORMATION *info = ptr;
1584
1585 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1586 else
1587 {
1588 if ((info->Directory = S_ISDIR(st.st_mode)))
1589 {
1590 info->AllocationSize.QuadPart = 0;
1591 info->EndOfFile.QuadPart = 0;
1592 info->NumberOfLinks = 1;
1593 info->DeletePending = FALSE;
1594 }
1595 else
1596 {
1597 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1598 info->EndOfFile.QuadPart = st.st_size;
1599 info->NumberOfLinks = st.st_nlink;
1600 info->DeletePending = FALSE; /* FIXME */
1601 }
1602 }
1603 }
1604 break;
1605 case FilePositionInformation:
1606 {
1607 FILE_POSITION_INFORMATION *info = ptr;
1608 off_t res = lseek( fd, 0, SEEK_CUR );
1609 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1610 else info->CurrentByteOffset.QuadPart = res;
1611 }
1612 break;
1613 case FileInternalInformation:
1614 {
1615 FILE_INTERNAL_INFORMATION *info = ptr;
1616
1617 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1618 else info->IndexNumber.QuadPart = st.st_ino;
1619 }
1620 break;
1621 case FileEaInformation:
1622 {
1623 FILE_EA_INFORMATION *info = ptr;
1624 info->EaSize = 0;
1625 }
1626 break;
1627 case FileEndOfFileInformation:
1628 {
1629 FILE_END_OF_FILE_INFORMATION *info = ptr;
1630
1631 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1632 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1633 }
1634 break;
1635 case FileAllInformation:
1636 {
1637 FILE_ALL_INFORMATION *info = ptr;
1638
1639 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1640 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1641 io->u.Status = STATUS_INVALID_INFO_CLASS;
1642 else
1643 {
1644 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1645 {
1646 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1647 info->StandardInformation.AllocationSize.QuadPart = 0;
1648 info->StandardInformation.EndOfFile.QuadPart = 0;
1649 info->StandardInformation.NumberOfLinks = 1;
1650 info->StandardInformation.DeletePending = FALSE;
1651 }
1652 else
1653 {
1654 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1655 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1656 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1657 info->StandardInformation.NumberOfLinks = st.st_nlink;
1658 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1659 }
1660 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1661 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1662 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1663 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1664 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1665 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1666 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1667 info->BasicInformation.CreationTime.QuadPart += st.st_mtim.tv_nsec / 100;
1668 info->BasicInformation.LastWriteTime.QuadPart += st.st_mtim.tv_nsec / 100;
1669 #endif
1670 #ifdef HAVE_STRUCT_STAT_ST_CTIM
1671 info->BasicInformation.ChangeTime.QuadPart += st.st_ctim.tv_nsec / 100;
1672 #endif
1673 #ifdef HAVE_STRUCT_STAT_ST_ATIM
1674 info->BasicInformation.LastAccessTime.QuadPart += st.st_atim.tv_nsec / 100;
1675 #endif
1676 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1677 info->EaInformation.EaSize = 0;
1678 info->AccessInformation.AccessFlags = 0; /* FIXME */
1679 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1680 info->ModeInformation.Mode = 0; /* FIXME */
1681 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1682 info->NameInformation.FileNameLength = 0;
1683 io->Information = sizeof(*info) - sizeof(WCHAR);
1684 }
1685 }
1686 break;
1687 case FileMailslotQueryInformation:
1688 {
1689 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1690
1691 SERVER_START_REQ( set_mailslot_info )
1692 {
1693 req->handle = hFile;
1694 req->flags = 0;
1695 io->u.Status = wine_server_call( req );
1696 if( io->u.Status == STATUS_SUCCESS )
1697 {
1698 info->MaximumMessageSize = reply->max_msgsize;
1699 info->MailslotQuota = 0;
1700 info->NextMessageSize = 0;
1701 info->MessagesAvailable = 0;
1702 info->ReadTimeout.QuadPart = reply->read_timeout;
1703 }
1704 }
1705 SERVER_END_REQ;
1706 if (!io->u.Status)
1707 {
1708 char *tmpbuf;
1709 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1710 if (size > 0x10000) size = 0x10000;
1711 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1712 {
1713 int fd, needs_close;
1714 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1715 {
1716 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1717 info->MessagesAvailable = (res > 0);
1718 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1719 if (needs_close) close( fd );
1720 }
1721 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1722 }
1723 }
1724 }
1725 break;
1726 case FilePipeLocalInformation:
1727 {
1728 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1729
1730 SERVER_START_REQ( get_named_pipe_info )
1731 {
1732 req->handle = hFile;
1733 if (!(io->u.Status = wine_server_call( req )))
1734 {
1735 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1736 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1737 pli->NamedPipeConfiguration = 0; /* FIXME */
1738 pli->MaximumInstances = reply->maxinstances;
1739 pli->CurrentInstances = reply->instances;
1740 pli->InboundQuota = reply->insize;
1741 pli->ReadDataAvailable = 0; /* FIXME */
1742 pli->OutboundQuota = reply->outsize;
1743 pli->WriteQuotaAvailable = 0; /* FIXME */
1744 pli->NamedPipeState = 0; /* FIXME */
1745 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1746 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1747 }
1748 }
1749 SERVER_END_REQ;
1750 }
1751 break;
1752 default:
1753 FIXME("Unsupported class (%d)\n", class);
1754 io->u.Status = STATUS_NOT_IMPLEMENTED;
1755 break;
1756 }
1757 if (needs_close) close( fd );
1758 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1759 return io->u.Status;
1760 }
1761
1762 /******************************************************************************
1763 * NtSetInformationFile [NTDLL.@]
1764 * ZwSetInformationFile [NTDLL.@]
1765 *
1766 * Set information about an open file handle.
1767 *
1768 * PARAMS
1769 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1770 * io [O] Receives information about the operation on return
1771 * ptr [I] Source for file information
1772 * len [I] Size of FileInformation
1773 * class [I] Type of file information to set
1774 *
1775 * RETURNS
1776 * Success: 0. io is updated.
1777 * Failure: An NTSTATUS error code describing the error.
1778 */
1779 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1780 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1781 {
1782 int fd, needs_close;
1783
1784 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1785
1786 io->u.Status = STATUS_SUCCESS;
1787 switch (class)
1788 {
1789 case FileBasicInformation:
1790 if (len >= sizeof(FILE_BASIC_INFORMATION))
1791 {
1792 struct stat st;
1793 const FILE_BASIC_INFORMATION *info = ptr;
1794
1795 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1796 return io->u.Status;
1797
1798 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1799 {
1800 ULONGLONG sec, nsec;
1801 struct timeval tv[2];
1802
1803 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1804 {
1805
1806 tv[0].tv_sec = tv[0].tv_usec = 0;
1807 tv[1].tv_sec = tv[1].tv_usec = 0;
1808 if (!fstat( fd, &st ))
1809 {
1810 tv[0].tv_sec = st.st_atime;
1811 tv[1].tv_sec = st.st_mtime;
1812 }
1813 }
1814 if (info->LastAccessTime.QuadPart)
1815 {
1816 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1817 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1818 tv[0].tv_usec = (UINT)nsec / 10;
1819 }
1820 if (info->LastWriteTime.QuadPart)
1821 {
1822 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1823 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1824 tv[1].tv_usec = (UINT)nsec / 10;
1825 }
1826 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1827 }
1828
1829 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1830 {
1831 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1832 else
1833 {
1834 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1835 {
1836 if (S_ISDIR( st.st_mode))
1837 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1838 else
1839 st.st_mode &= ~0222; /* clear write permission bits */
1840 }
1841 else
1842 {
1843 /* add write permission only where we already have read permission */
1844 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1845 }
1846 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1847 }
1848 }
1849
1850 if (needs_close) close( fd );
1851 }
1852 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1853 break;
1854
1855 case FilePositionInformation:
1856 if (len >= sizeof(FILE_POSITION_INFORMATION))
1857 {
1858 const FILE_POSITION_INFORMATION *info = ptr;
1859
1860 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1861 return io->u.Status;
1862
1863 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1864 io->u.Status = FILE_GetNtStatus();
1865
1866 if (needs_close) close( fd );
1867 }
1868 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1869 break;
1870
1871 case FileEndOfFileInformation:
1872 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1873 {
1874 struct stat st;
1875 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1876
1877 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1878 return io->u.Status;
1879
1880 /* first try normal truncate */
1881 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1882
1883 /* now check for the need to extend the file */
1884 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1885 {
1886 static const char zero;
1887
1888 /* extend the file one byte beyond the requested size and then truncate it */
1889 /* this should work around ftruncate implementations that can't extend files */
1890 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1891 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1892 }
1893 io->u.Status = FILE_GetNtStatus();
1894
1895 if (needs_close) close( fd );
1896 }
1897 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1898 break;
1899
1900 case FileMailslotSetInformation:
1901 {
1902 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1903
1904 SERVER_START_REQ( set_mailslot_info )
1905 {
1906 req->handle = handle;
1907 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1908 req->read_timeout = info->ReadTimeout.QuadPart;
1909 io->u.Status = wine_server_call( req );
1910 }
1911 SERVER_END_REQ;
1912 }
1913 break;
1914
1915 case FileCompletionInformation:
1916 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
1917 {
1918 FILE_COMPLETION_INFORMATION *info = (FILE_COMPLETION_INFORMATION *)ptr;
1919
1920 SERVER_START_REQ( set_completion_info )
1921 {
1922 req->handle = handle;
1923 req->chandle = info->CompletionPort;
1924 req->ckey = info->CompletionKey;
1925 io->u.Status = wine_server_call( req );
1926 }
1927 SERVER_END_REQ;
1928 } else
1929 io->u.Status = STATUS_INVALID_PARAMETER_3;
1930 break;
1931
1932 default:
1933 FIXME("Unsupported class (%d)\n", class);
1934 io->u.Status = STATUS_NOT_IMPLEMENTED;
1935 break;
1936 }
1937 io->Information = 0;
1938 return io->u.Status;
1939 }
1940
1941
1942 /******************************************************************************
1943 * NtQueryFullAttributesFile (NTDLL.@)
1944 */
1945 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1946 FILE_NETWORK_OPEN_INFORMATION *info )
1947 {
1948 ANSI_STRING unix_name;
1949 NTSTATUS status;
1950
1951 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1952 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1953 {
1954 struct stat st;
1955
1956 if (stat( unix_name.Buffer, &st ) == -1)
1957 status = FILE_GetNtStatus();
1958 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1959 status = STATUS_INVALID_INFO_CLASS;
1960 else
1961 {
1962 if (S_ISDIR(st.st_mode))
1963 {
1964 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1965 info->AllocationSize.QuadPart = 0;
1966 info->EndOfFile.QuadPart = 0;
1967 }
1968 else
1969 {
1970 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1971 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1972 info->EndOfFile.QuadPart = st.st_size;
1973 }
1974 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1975 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1976 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1977 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1978 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1979 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1980 if (DIR_is_hidden_file( attr->ObjectName ))
1981 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1982 }
1983 RtlFreeAnsiString( &unix_name );
1984 }
1985 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1986 return status;
1987 }
1988
1989
1990 /******************************************************************************
1991 * NtQueryAttributesFile (NTDLL.@)
1992 * ZwQueryAttributesFile (NTDLL.@)
1993 */
1994 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1995 {
1996 FILE_NETWORK_OPEN_INFORMATION full_info;
1997 NTSTATUS status;
1998
1999 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
2000 {
2001 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
2002 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
2003 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
2004 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
2005 info->FileAttributes = full_info.FileAttributes;
2006 }
2007 return status;
2008 }
2009
2010
2011 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
2012 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
2013 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
2014 unsigned int flags )
2015 {
2016 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
2017 {
2018 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2019 /* Don't assume read-only, let the mount options set it below */
2020 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2021 }
2022 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
2023 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
2024 {
2025 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2026 info->Characteristics |= FILE_REMOTE_DEVICE;
2027 }
2028 else if (!strcmp("procfs", fstypename))
2029 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2030 else
2031 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2032
2033 if (flags & MNT_RDONLY)
2034 info->Characteristics |= FILE_READ_ONLY_DEVICE;
2035
2036 if (!(flags & MNT_LOCAL))
2037 {
2038 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2039 info->Characteristics |= FILE_REMOTE_DEVICE;
2040 }
2041 }
2042 #endif
2043
2044 static inline int is_device_placeholder( int fd )
2045 {
2046 static const char wine_placeholder[] = "Wine device placeholder";
2047 char buffer[sizeof(wine_placeholder)-1];
2048
2049 if (pread( fd, buffer, sizeof(wine_placeholder) - 1, 0 ) != sizeof(wine_placeholder) - 1)
2050 return 0;
2051 return !memcmp( buffer, wine_placeholder, sizeof(wine_placeholder) - 1 );
2052 }
2053
2054 /******************************************************************************
2055 * get_device_info
2056 *
2057 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
2058 */
2059 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
2060 {
2061 struct stat st;
2062
2063 info->Characteristics = 0;
2064 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
2065 if (S_ISCHR( st.st_mode ))
2066 {
2067 info->DeviceType = FILE_DEVICE_UNKNOWN;
2068 #ifdef linux
2069 switch(major(st.st_rdev))
2070 {
2071 case MEM_MAJOR:
2072 info->DeviceType = FILE_DEVICE_NULL;
2073 break;
2074 case TTY_MAJOR:
2075 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
2076 break;
2077 case LP_MAJOR:
2078 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
2079 break;
2080 case SCSI_TAPE_MAJOR:
2081 info->DeviceType = FILE_DEVICE_TAPE;
2082 break;
2083 }
2084 #endif
2085 }
2086 else if (S_ISBLK( st.st_mode ))
2087 {
2088 info->DeviceType = FILE_DEVICE_DISK;
2089 }
2090 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
2091 {
2092 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
2093 }
2094 else if (is_device_placeholder( fd ))
2095 {
2096 info->DeviceType = FILE_DEVICE_DISK;
2097 }
2098 else /* regular file or directory */
2099 {
2100 #if defined(linux) && defined(HAVE_FSTATFS)
2101 struct statfs stfs;
2102
2103 /* check for floppy disk */
2104 if (major(st.st_dev) == FLOPPY_MAJOR)
2105 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2106
2107 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
2108 switch (stfs.f_type)
2109 {
2110 case 0x9660: /* iso9660 */
2111 case 0x9fa1: /* supermount */
2112 case 0x15013346: /* udf */
2113 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2114 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2115 break;
2116 case 0x6969: /* nfs */
2117 case 0x517B: /* smbfs */
2118 case 0x564c: /* ncpfs */
2119 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
2120 info->Characteristics |= FILE_REMOTE_DEVICE;
2121 break;
2122 case 0x01021994: /* tmpfs */
2123 case 0x28cd3d45: /* cramfs */
2124 case 0x1373: /* devfs */
2125 case 0x9fa0: /* procfs */
2126 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2127 break;
2128 default:
2129 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2130 break;
2131 }
2132 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
2133 struct statfs stfs;
2134
2135 if (fstatfs( fd, &stfs ) < 0)
2136 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2137 else
2138 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
2139 #elif defined(__NetBSD__)
2140 struct statvfs stfs;
2141
2142 if (fstatvfs( fd, &stfs) < 0)
2143 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2144 else
2145 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
2146 #elif defined(sun)
2147 /* Use dkio to work out device types */
2148 {
2149 # include <sys/dkio.h>
2150 # include <sys/vtoc.h>
2151 struct dk_cinfo dkinf;
2152 int retval = ioctl(fd, DKIOCINFO, &dkinf);
2153 if(retval==-1){
2154 WARN("Unable to get disk device type information - assuming a disk like device\n");
2155 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2156 }
2157 switch (dkinf.dki_ctype)
2158 {
2159 case DKC_CDROM:
2160 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
2161 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
2162 break;
2163 case DKC_NCRFLOPPY:
2164 case DKC_SMSFLOPPY:
2165 case DKC_INTEL82072:
2166 case DKC_INTEL82077:
2167 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2168 info->Characteristics |= FILE_REMOVABLE_MEDIA;
2169 break;
2170 case DKC_MD:
2171 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
2172 break;
2173 default:
2174 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2175 }
2176 }
2177 #else
2178 static int warned;
2179 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
2180 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
2181 #endif
2182 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
2183 }
2184 return STATUS_SUCCESS;
2185 }
2186
2187
2188 /******************************************************************************
2189 * NtQueryVolumeInformationFile [NTDLL.@]
2190 * ZwQueryVolumeInformationFile [NTDLL.@]
2191 *
2192 * Get volume information for an open file handle.
2193 *
2194 * PARAMS
2195 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2196 * io [O] Receives information about the operation on return
2197 * buffer [O] Destination for volume information
2198 * length [I] Size of FsInformation
2199 * info_class [I] Type of volume information to set
2200 *
2201 * RETURNS
2202 * Success: 0. io and buffer are updated.
2203 * Failure: An NTSTATUS error code describing the error.
2204 */
2205 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
2206 PVOID buffer, ULONG length,
2207 FS_INFORMATION_CLASS info_class )
2208 {
2209 int fd, needs_close;
2210 struct stat st;
2211
2212 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2213 return io->u.Status;
2214
2215 io->u.Status = STATUS_NOT_IMPLEMENTED;
2216 io->Information = 0;
2217
2218 switch( info_class )
2219 {
2220 case FileFsVolumeInformation:
2221 FIXME( "%p: volume info not supported\n", handle );
2222 break;
2223 case FileFsLabelInformation:
2224 FIXME( "%p: label info not supported\n", handle );
2225 break;
2226 case FileFsSizeInformation:
2227 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
2228 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2229 else
2230 {
2231 FILE_FS_SIZE_INFORMATION *info = buffer;
2232
2233 if (fstat( fd, &st ) < 0)
2234 {
2235 io->u.Status = FILE_GetNtStatus();
2236 break;
2237 }
2238 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
2239 {
2240 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
2241 }
2242 else
2243 {
2244 /* Linux's fstatvfs is buggy */
2245 #if !defined(linux) || !defined(HAVE_FSTATFS)
2246 struct statvfs stfs;
2247
2248 if (fstatvfs( fd, &stfs ) < 0)
2249 {
2250 io->u.Status = FILE_GetNtStatus();
2251 break;
2252 }
2253 info->BytesPerSector = stfs.f_frsize;
2254 #else
2255 struct statfs stfs;
2256 if (fstatfs( fd, &stfs ) < 0)
2257 {
2258 io->u.Status = FILE_GetNtStatus();
2259 break;
2260 }
2261 info->BytesPerSector = stfs.f_bsize;
2262 #endif
2263 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
2264 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
2265 info->SectorsPerAllocationUnit = 1;
2266 io->Information = sizeof(*info);
2267 io->u.Status = STATUS_SUCCESS;
2268 }
2269 }
2270 break;
2271 case FileFsDeviceInformation:
2272 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2273 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2274 else
2275 {
2276 FILE_FS_DEVICE_INFORMATION *info = buffer;
2277
2278 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2279 io->Information = sizeof(*info);
2280 }
2281 break;
2282 case FileFsAttributeInformation:
2283 FIXME( "%p: attribute info not supported\n", handle );
2284 break;
2285 case FileFsControlInformation:
2286 FIXME( "%p: control info not supported\n", handle );
2287 break;
2288 case FileFsFullSizeInformation:
2289 FIXME( "%p: full size info not supported\n", handle );
2290 break;
2291 case FileFsObjectIdInformation:
2292 FIXME( "%p: object id info not supported\n", handle );
2293 break;
2294 case FileFsMaximumInformation:
2295 FIXME( "%p: maximum info not supported\n", handle );
2296 break;
2297 default:
2298 io->u.Status = STATUS_INVALID_PARAMETER;
2299 break;
2300 }
2301 if (needs_close) close( fd );
2302 return io->u.Status;
2303 }
2304
2305
2306 /******************************************************************
2307 * NtFlushBuffersFile (NTDLL.@)
2308 *
2309 * Flush any buffered data on an open file handle.
2310 *
2311 * PARAMS
2312 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2313 * IoStatusBlock [O] Receives information about the operation on return
2314 *
2315 * RETURNS
2316 * Success: 0. IoStatusBlock is updated.
2317 * Failure: An NTSTATUS error code describing the error.
2318 */
2319 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2320 {
2321 NTSTATUS ret;
2322 HANDLE hEvent = NULL;
2323
2324 SERVER_START_REQ( flush_file )
2325 {
2326 req->handle = hFile;
2327 ret = wine_server_call( req );
2328 hEvent = reply->event;
2329 }
2330 SERVER_END_REQ;
2331 if (!ret && hEvent)
2332 {
2333 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2334 NtClose( hEvent );
2335 }
2336 return ret;
2337 }
2338
2339 /******************************************************************
2340 * NtLockFile (NTDLL.@)
2341 *
2342 *
2343 */
2344 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2345 PIO_APC_ROUTINE apc, void* apc_user,
2346 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2347 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2348 BOOLEAN exclusive )
2349 {
2350 NTSTATUS ret;
2351 HANDLE handle;
2352 BOOLEAN async;
2353
2354 if (apc || io_status || key)
2355 {
2356 FIXME("Unimplemented yet parameter\n");
2357 return STATUS_NOT_IMPLEMENTED;
2358 }
2359
2360 if (apc_user) FIXME("I/O completion on lock not implemented yet\n");
2361
2362 for (;;)
2363 {
2364 SERVER_START_REQ( lock_file )
2365 {
2366 req->handle = hFile;
2367 req->offset = offset->QuadPart;
2368 req->count = count->QuadPart;
2369 req->shared = !exclusive;
2370 req->wait = !dont_wait;
2371 ret = wine_server_call( req );
2372 handle = reply->handle;
2373 async = reply->overlapped;
2374 }
2375 SERVER_END_REQ;
2376 if (ret != STATUS_PENDING)
2377 {
2378 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2379 return ret;
2380 }
2381
2382 if (async)
2383 {
2384 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2385 if (handle) NtClose( handle );
2386 return STATUS_PENDING;
2387 }
2388 if (handle)
2389 {
2390 NtWaitForSingleObject( handle, FALSE, NULL );
2391 NtClose( handle );
2392 }
2393 else
2394 {
2395 LARGE_INTEGER time;
2396
2397 /* Unix lock conflict, sleep a bit and retry */
2398 time.QuadPart = 100 * (ULONGLONG)10000;
2399 time.QuadPart = -time.QuadPart;
2400 NtDelayExecution( FALSE, &time );
2401 }
2402 }
2403 }
2404
2405
2406 /******************************************************************
2407 * NtUnlockFile (NTDLL.@)
2408 *
2409 *
2410 */
2411 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2412 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2413 PULONG key )
2414 {
2415 NTSTATUS status;
2416
2417 TRACE( "%p %x%08x %x%08x\n",
2418 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2419
2420 if (io_status || key)
2421 {
2422 FIXME("Unimplemented yet parameter\n");
2423 return STATUS_NOT_IMPLEMENTED;
2424 }
2425
2426 SERVER_START_REQ( unlock_file )
2427 {
2428 req->handle = hFile;
2429 req->offset = offset->QuadPart;
2430 req->count = count->QuadPart;
2431 status = wine_server_call( req );
2432 }
2433 SERVER_END_REQ;
2434 return status;
2435 }
2436
2437 /******************************************************************
2438 * NtCreateNamedPipeFile (NTDLL.@)
2439 *
2440 *
2441 */
2442 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2443 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2444 ULONG sharing, ULONG dispo, ULONG options,
2445 ULONG pipe_type, ULONG read_mode,
2446 ULONG completion_mode, ULONG max_inst,
2447 ULONG inbound_quota, ULONG outbound_quota,
2448 PLARGE_INTEGER timeout)
2449 {
2450 NTSTATUS status;
2451
2452 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2453 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2454 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2455 outbound_quota, timeout);
2456
2457 /* assume we only get relative timeout */
2458 if (timeout->QuadPart > 0)
2459 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2460
2461 SERVER_START_REQ( create_named_pipe )
2462 {
2463 req->access = access;
2464 req->attributes = attr->Attributes;
2465 req->rootdir = attr->RootDirectory;
2466 req->options = options;
2467 req->flags =
2468 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2469 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2470 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2471 req->maxinstances = max_inst;
2472 req->outsize = outbound_quota;
2473 req->insize = inbound_quota;
2474 req->timeout = timeout->QuadPart;
2475 wine_server_add_data( req, attr->ObjectName->Buffer,
2476 attr->ObjectName->Length );
2477 status = wine_server_call( req );
2478 if (!status) *handle = reply->handle;
2479 }
2480 SERVER_END_REQ;
2481 return status;
2482 }
2483
2484 /******************************************************************
2485 * NtDeleteFile (NTDLL.@)
2486 *
2487 *
2488 */
2489 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2490 {
2491 NTSTATUS status;
2492 HANDLE hFile;
2493 IO_STATUS_BLOCK io;
2494
2495 TRACE("%p\n", ObjectAttributes);
2496 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2497 ObjectAttributes, &io, NULL, 0,
2498 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2499 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2500 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2501 return status;
2502 }
2503
2504 /******************************************************************
2505 * NtCancelIoFile (NTDLL.@)
2506 *
2507 *
2508 */
2509 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2510 {
2511 LARGE_INTEGER timeout;
2512
2513 TRACE("%p %p\n", hFile, io_status );
2514
2515 SERVER_START_REQ( cancel_async )
2516 {
2517 req->handle = hFile;
2518 wine_server_call( req );
2519 }
2520 SERVER_END_REQ;
2521 /* Let some APC be run, so that we can run the remaining APCs on hFile
2522 * either the cancelation of the pending one, but also the execution
2523 * of the queued APC, but not yet run. This is needed to ensure proper
2524 * clean-up of allocated data.
2525 */
2526 timeout.u.LowPart = timeout.u.HighPart = 0;
2527 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2528 }
2529
2530 /******************************************************************************
2531 * NtCreateMailslotFile [NTDLL.@]
2532 * ZwCreateMailslotFile [NTDLL.@]
2533 *
2534 * PARAMS
2535 * pHandle [O] pointer to receive the handle created
2536 * DesiredAccess [I] access mode (read, write, etc)
2537 * ObjectAttributes [I] fully qualified NT path of the mailslot
2538 * IoStatusBlock [O] receives completion status and other info
2539 * CreateOptions [I]
2540 * MailslotQuota [I]
2541 * MaxMessageSize [I]
2542 * TimeOut [I]
2543 *
2544 * RETURNS
2545 * An NT status code
2546 */
2547 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2548 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2549 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2550 PLARGE_INTEGER TimeOut)
2551 {
2552 LARGE_INTEGER timeout;
2553 NTSTATUS ret;
2554
2555 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2556 pHandle, DesiredAccess, attr, IoStatusBlock,
2557 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2558
2559 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2560 if (!attr) return STATUS_INVALID_PARAMETER;
2561 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2562
2563 /*
2564 * For a NULL TimeOut pointer set the default timeout value
2565 */
2566 if (!TimeOut)
2567 timeout.QuadPart = -1;
2568 else
2569 timeout.QuadPart = TimeOut->QuadPart;
2570
2571 SERVER_START_REQ( create_mailslot )
2572 {
2573 req->access = DesiredAccess;
2574 req->attributes = attr->Attributes;
2575 req->rootdir = attr->RootDirectory;
2576 req->max_msgsize = MaxMessageSize;
2577 req->read_timeout = timeout.QuadPart;
2578 wine_server_add_data( req, attr->ObjectName->Buffer,
2579 attr->ObjectName->Length );
2580 ret = wine_server_call( req );
2581 if( ret == STATUS_SUCCESS )
2582 *pHandle = reply->handle;
2583 }
2584 SERVER_END_REQ;
2585
2586 return ret;
2587 }
2588
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.