user32: Properly handle integer atoms specified as strings in class names.
[wine/multimedia.git] / dlls / ntdll / file.c
blob7b64b6784840d334192fa589361d15b97cbdb96f
1 /*
2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
32 #endif
33 #ifdef HAVE_LINUX_MAJOR_H
34 # include <linux/major.h>
35 #endif
36 #ifdef HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
38 #endif
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_POLL_H
49 #include <poll.h>
50 #endif
51 #ifdef HAVE_SYS_POLL_H
52 #include <sys/poll.h>
53 #endif
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
56 #endif
57 #ifdef HAVE_UTIME_H
58 # include <utime.h>
59 #endif
60 #ifdef HAVE_SYS_VFS_H
61 # include <sys/vfs.h>
62 #endif
63 #ifdef HAVE_SYS_MOUNT_H
64 # include <sys/mount.h>
65 #endif
66 #ifdef HAVE_SYS_STATFS_H
67 # include <sys/statfs.h>
68 #endif
70 #define NONAMELESSUNION
71 #define NONAMELESSSTRUCT
72 #include "ntstatus.h"
73 #define WIN32_NO_STATUS
74 #include "wine/unicode.h"
75 #include "wine/debug.h"
76 #include "thread.h"
77 #include "wine/server.h"
78 #include "ntdll_misc.h"
80 #include "winternl.h"
81 #include "winioctl.h"
82 #include "ddk/ntddser.h"
84 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
86 mode_t FILE_umask = 0;
88 #define SECSPERDAY 86400
89 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
91 /**************************************************************************
92 * NtOpenFile [NTDLL.@]
93 * ZwOpenFile [NTDLL.@]
95 * Open a file.
97 * PARAMS
98 * handle [O] Variable that receives the file handle on return
99 * access [I] Access desired by the caller to the file
100 * attr [I] Structure describing the file to be opened
101 * io [O] Receives details about the result of the operation
102 * sharing [I] Type of shared access the caller requires
103 * options [I] Options for the file open
105 * RETURNS
106 * Success: 0. FileHandle and IoStatusBlock are updated.
107 * Failure: An NTSTATUS error code describing the error.
109 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
110 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
111 ULONG sharing, ULONG options )
113 return NtCreateFile( handle, access, attr, io, NULL, 0,
114 sharing, FILE_OPEN, options, NULL, 0 );
117 /**************************************************************************
118 * NtCreateFile [NTDLL.@]
119 * ZwCreateFile [NTDLL.@]
121 * Either create a new file or directory, or open an existing file, device,
122 * directory or volume.
124 * PARAMS
125 * handle [O] Points to a variable which receives the file handle on return
126 * access [I] Desired access to the file
127 * attr [I] Structure describing the file
128 * io [O] Receives information about the operation on return
129 * alloc_size [I] Initial size of the file in bytes
130 * attributes [I] Attributes to create the file with
131 * sharing [I] Type of shared access the caller would like to the file
132 * disposition [I] Specifies what to do, depending on whether the file already exists
133 * options [I] Options for creating a new file
134 * ea_buffer [I] Pointer to an extended attributes buffer
135 * ea_length [I] Length of ea_buffer
137 * RETURNS
138 * Success: 0. handle and io are updated.
139 * Failure: An NTSTATUS error code describing the error.
141 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
142 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
143 ULONG attributes, ULONG sharing, ULONG disposition,
144 ULONG options, PVOID ea_buffer, ULONG ea_length )
146 ANSI_STRING unix_name;
147 int created = FALSE;
149 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p\n"
150 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
151 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
152 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
153 attributes, sharing, disposition, options, ea_buffer, ea_length );
155 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
157 if (alloc_size) FIXME( "alloc_size not supported\n" );
159 if (attr->RootDirectory)
161 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
162 return STATUS_OBJECT_NAME_NOT_FOUND;
165 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
166 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
168 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
170 SERVER_START_REQ( open_file_object )
172 req->access = access;
173 req->attributes = attr->Attributes;
174 req->rootdir = attr->RootDirectory;
175 req->sharing = sharing;
176 req->options = options;
177 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
178 io->u.Status = wine_server_call( req );
179 *handle = reply->handle;
181 SERVER_END_REQ;
182 return io->u.Status;
185 if (io->u.Status == STATUS_NO_SUCH_FILE &&
186 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
188 created = TRUE;
189 io->u.Status = STATUS_SUCCESS;
192 if (io->u.Status == STATUS_SUCCESS)
194 struct security_descriptor *sd = NULL;
195 struct object_attributes objattr;
197 objattr.rootdir = 0;
198 objattr.sd_len = 0;
199 objattr.name_len = 0;
200 if (attr)
202 io->u.Status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
203 if (io->u.Status != STATUS_SUCCESS)
205 RtlFreeAnsiString( &unix_name );
206 return io->u.Status;
210 SERVER_START_REQ( create_file )
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;
224 SERVER_END_REQ;
225 NTDLL_free_struct_sd( sd );
226 RtlFreeAnsiString( &unix_name );
228 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
230 if (io->u.Status == STATUS_SUCCESS)
232 if (created) io->Information = FILE_CREATED;
233 else switch(disposition)
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;
252 return io->u.Status;
255 /***********************************************************************
256 * Asynchronous file I/O *
259 struct async_fileio
261 HANDLE handle;
262 PIO_APC_ROUTINE apc;
263 void *apc_arg;
266 typedef struct
268 struct async_fileio io;
269 char* buffer;
270 unsigned int already;
271 unsigned int count;
272 BOOL avail_mode;
273 } async_fileio_read;
275 typedef struct
277 struct async_fileio io;
278 const char *buffer;
279 unsigned int already;
280 unsigned int count;
281 } async_fileio_write;
284 /* callback for file I/O user APC */
285 static void WINAPI fileio_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
287 struct async_fileio *async = arg;
288 if (async->apc) async->apc( async->apc_arg, io, reserved );
289 RtlFreeHeap( GetProcessHeap(), 0, async );
292 /***********************************************************************
293 * FILE_GetNtStatus(void)
295 * Retrieve the Nt Status code from errno.
296 * Try to be consistent with FILE_SetDosError().
298 NTSTATUS FILE_GetNtStatus(void)
300 int err = errno;
302 TRACE( "errno = %d\n", errno );
303 switch (err)
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;
338 /***********************************************************************
339 * FILE_AsyncReadService (INTERNAL)
341 static NTSTATUS FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status)
343 async_fileio_read *fileio = user;
344 int fd, needs_close, result;
346 switch (status)
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;
354 result = read(fd, &fileio->buffer[fileio->already], fileio->count - fileio->already);
355 if (needs_close) close( fd );
357 if (result < 0)
359 if (errno == EAGAIN || errno == EINTR)
360 status = STATUS_PENDING;
361 else /* check to see if the transfer is complete */
362 status = FILE_GetNtStatus();
364 else if (result == 0)
366 status = fileio->already ? STATUS_SUCCESS : STATUS_PIPE_BROKEN;
368 else
370 fileio->already += result;
371 if (fileio->already >= fileio->count || fileio->avail_mode)
372 status = STATUS_SUCCESS;
373 else
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)
379 status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
382 break;
384 case STATUS_TIMEOUT:
385 case STATUS_IO_TIMEOUT:
386 if (fileio->already) status = STATUS_SUCCESS;
387 break;
389 if (status != STATUS_PENDING)
391 iosb->u.Status = status;
392 iosb->Information = fileio->already;
394 return status;
397 struct io_timeouts
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 */
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 )
408 NTSTATUS status = STATUS_SUCCESS;
410 timeouts->interval = timeouts->total = -1;
412 switch(type)
414 case FD_TYPE_SERIAL:
416 /* GetCommTimeouts */
417 SERIAL_TIMEOUTS st;
418 IO_STATUS_BLOCK io;
420 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io,
421 IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, &st, sizeof(st) );
422 if (status) break;
424 if (is_read)
426 if (st.ReadIntervalTimeout)
427 timeouts->interval = st.ReadIntervalTimeout;
429 if (st.ReadTotalTimeoutMultiplier || st.ReadTotalTimeoutConstant)
431 timeouts->total = st.ReadTotalTimeoutConstant;
432 if (st.ReadTotalTimeoutMultiplier != MAXDWORD)
433 timeouts->total += count * st.ReadTotalTimeoutMultiplier;
435 else if (st.ReadIntervalTimeout == MAXDWORD)
436 timeouts->interval = 0;
438 else /* write */
440 if (st.WriteTotalTimeoutMultiplier || st.WriteTotalTimeoutConstant)
442 timeouts->total = st.WriteTotalTimeoutConstant;
443 if (st.WriteTotalTimeoutMultiplier != MAXDWORD)
444 timeouts->total += count * st.WriteTotalTimeoutMultiplier;
448 break;
449 case FD_TYPE_MAILSLOT:
450 if (is_read)
452 timeouts->interval = 0; /* return as soon as we got something */
453 SERVER_START_REQ( set_mailslot_info )
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;
461 SERVER_END_REQ;
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;
472 if (timeouts->total != -1) timeouts->end_time = NtGetTickCount() + timeouts->total;
473 return STATUS_SUCCESS;
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 )
480 int ret = -1;
482 if (timeouts->total != -1)
484 ret = timeouts->end_time - NtGetTickCount();
485 if (ret < 0) ret = 0;
487 if (already && timeouts->interval != -1)
489 if (ret == -1 || ret > timeouts->interval) ret = timeouts->interval;
491 return ret;
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 )
498 NTSTATUS status = STATUS_SUCCESS;
500 switch(type)
502 case FD_TYPE_SERIAL:
504 /* GetCommTimeouts */
505 SERIAL_TIMEOUTS st;
506 IO_STATUS_BLOCK io;
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);
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;
526 return status;
530 /******************************************************************************
531 * NtReadFile [NTDLL.@]
532 * ZwReadFile [NTDLL.@]
534 * Read from an open file handle.
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)
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.
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)
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;
564 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
565 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
567 if (!io_status) return STATUS_ACCESS_VIOLATION;
569 status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
570 &needs_close, &type, &options );
571 if (status) return status;
573 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
575 /* async I/O doesn't make sense on regular files */
576 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
578 if (errno != EINTR)
580 status = FILE_GetNtStatus();
581 goto done;
584 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
585 /* update file pointer position */
586 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
588 total = result;
589 status = total ? STATUS_SUCCESS : STATUS_END_OF_FILE;
590 goto done;
593 for (;;)
595 if ((result = read( unix_handle, (char *)buffer + total, length - total )) >= 0)
597 total += result;
598 if (!result || total == length)
600 if (total)
601 status = STATUS_SUCCESS;
602 else
603 status = (type == FD_TYPE_FILE || type == FD_TYPE_CHAR) ? STATUS_END_OF_FILE : STATUS_PIPE_BROKEN;
604 goto done;
607 else
609 if (errno == EINTR) continue;
610 if (errno != EAGAIN)
612 status = FILE_GetNtStatus();
613 goto done;
617 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
619 async_fileio_read *fileio;
620 BOOL avail_mode;
622 if ((status = get_io_avail_mode( hFile, type, &avail_mode )))
623 goto done;
624 if (total && avail_mode)
626 status = STATUS_SUCCESS;
627 goto done;
630 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
632 status = STATUS_NO_MEMORY;
633 goto done;
635 fileio->io.handle = hFile;
636 fileio->io.apc = apc;
637 fileio->io.apc_arg = apc_user;
638 fileio->already = total;
639 fileio->count = length;
640 fileio->buffer = buffer;
641 fileio->avail_mode = avail_mode;
643 SERVER_START_REQ( register_async )
645 req->handle = hFile;
646 req->type = ASYNC_TYPE_READ;
647 req->count = length;
648 req->async.callback = FILE_AsyncReadService;
649 req->async.iosb = io_status;
650 req->async.arg = fileio;
651 req->async.apc = fileio_apc;
652 req->async.event = hEvent;
653 status = wine_server_call( req );
655 SERVER_END_REQ;
657 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
658 goto done;
660 else /* synchronous read, wait for the fd to become ready */
662 struct pollfd pfd;
663 int ret, timeout;
665 if (!timeout_init_done)
667 timeout_init_done = 1;
668 if ((status = get_io_timeouts( hFile, type, length, TRUE, &timeouts )))
669 goto done;
670 if (hEvent) NtResetEvent( hEvent, NULL );
672 timeout = get_next_io_timeout( &timeouts, total );
674 pfd.fd = unix_handle;
675 pfd.events = POLLIN;
677 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
679 if (total) /* return with what we got so far */
680 status = STATUS_SUCCESS;
681 else
682 status = (type == FD_TYPE_MAILSLOT) ? STATUS_IO_TIMEOUT : STATUS_TIMEOUT;
683 goto done;
685 if (ret == -1 && errno != EINTR)
687 status = FILE_GetNtStatus();
688 goto done;
690 /* will now restart the read */
694 done:
695 if (needs_close) close( unix_handle );
696 if (status == STATUS_SUCCESS)
698 io_status->u.Status = status;
699 io_status->Information = total;
700 TRACE("= SUCCESS (%u)\n", total);
701 if (hEvent) NtSetEvent( hEvent, NULL );
702 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
703 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
705 else
707 TRACE("= 0x%08x\n", status);
708 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
710 return status;
713 /***********************************************************************
714 * FILE_AsyncWriteService (INTERNAL)
716 static NTSTATUS FILE_AsyncWriteService(void *user, IO_STATUS_BLOCK *iosb, NTSTATUS status)
718 async_fileio_write *fileio = user;
719 int result, fd, needs_close;
720 enum server_fd_type type;
722 switch (status)
724 case STATUS_ALERTED:
725 /* write some data (non-blocking) */
726 if ((status = server_get_unix_fd( fileio->io.handle, FILE_WRITE_DATA, &fd,
727 &needs_close, &type, NULL )))
728 break;
730 if (!fileio->count && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
731 result = send( fd, fileio->buffer, 0, 0 );
732 else
733 result = write( fd, &fileio->buffer[fileio->already], fileio->count - fileio->already );
735 if (needs_close) close( fd );
737 if (result < 0)
739 if (errno == EAGAIN || errno == EINTR) status = STATUS_PENDING;
740 else status = FILE_GetNtStatus();
742 else
744 fileio->already += result;
745 status = (fileio->already < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
747 break;
749 case STATUS_TIMEOUT:
750 case STATUS_IO_TIMEOUT:
751 if (fileio->already) status = STATUS_SUCCESS;
752 break;
754 if (status != STATUS_PENDING)
756 iosb->u.Status = status;
757 iosb->Information = fileio->already;
759 return status;
762 /******************************************************************************
763 * NtWriteFile [NTDLL.@]
764 * ZwWriteFile [NTDLL.@]
766 * Write to an open file handle.
768 * PARAMS
769 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
770 * Event [I] Event to signal upon completion (or NULL)
771 * ApcRoutine [I] Callback to call upon completion (or NULL)
772 * ApcContext [I] Context for ApcRoutine (or NULL)
773 * IoStatusBlock [O] Receives information about the operation on return
774 * Buffer [I] Source for the data to write
775 * Length [I] Size of Buffer
776 * ByteOffset [O] Destination for the new file pointer position (or NULL)
777 * Key [O] Function unknown (may be NULL)
779 * RETURNS
780 * Success: 0. IoStatusBlock is updated, and the Information member contains
781 * The number of bytes written.
782 * Failure: An NTSTATUS error code describing the error.
784 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
785 PIO_APC_ROUTINE apc, void* apc_user,
786 PIO_STATUS_BLOCK io_status,
787 const void* buffer, ULONG length,
788 PLARGE_INTEGER offset, PULONG key)
790 int result, unix_handle, needs_close, timeout_init_done = 0;
791 unsigned int options;
792 struct io_timeouts timeouts;
793 NTSTATUS status;
794 ULONG total = 0;
795 enum server_fd_type type;
797 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
798 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
800 if (!io_status) return STATUS_ACCESS_VIOLATION;
802 status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
803 &needs_close, &type, &options );
804 if (status) return status;
806 if (type == FD_TYPE_FILE && offset && offset->QuadPart != (LONGLONG)-2 /* FILE_USE_FILE_POINTER_POSITION */ )
808 /* async I/O doesn't make sense on regular files */
809 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
811 if (errno != EINTR)
813 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
814 else status = FILE_GetNtStatus();
815 goto done;
819 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
820 /* update file pointer position */
821 lseek( unix_handle, offset->QuadPart + result, SEEK_SET );
823 total = result;
824 status = STATUS_SUCCESS;
825 goto done;
828 for (;;)
830 /* zero-length writes on sockets may not work with plain write(2) */
831 if (!length && (type == FD_TYPE_MAILSLOT || type == FD_TYPE_PIPE || type == FD_TYPE_SOCKET))
832 result = send( unix_handle, buffer, 0, 0 );
833 else
834 result = write( unix_handle, (const char *)buffer + total, length - total );
836 if (result >= 0)
838 total += result;
839 if (total == length)
841 status = STATUS_SUCCESS;
842 goto done;
845 else
847 if (errno == EINTR) continue;
848 if (errno != EAGAIN)
850 if (errno == EFAULT) status = STATUS_INVALID_USER_BUFFER;
851 else status = FILE_GetNtStatus();
852 goto done;
856 if (!(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
858 async_fileio_write *fileio;
860 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*fileio))))
862 status = STATUS_NO_MEMORY;
863 goto done;
865 fileio->io.handle = hFile;
866 fileio->io.apc = apc;
867 fileio->io.apc_arg = apc_user;
868 fileio->already = total;
869 fileio->count = length;
870 fileio->buffer = buffer;
872 SERVER_START_REQ( register_async )
874 req->handle = hFile;
875 req->type = ASYNC_TYPE_WRITE;
876 req->count = length;
877 req->async.callback = FILE_AsyncWriteService;
878 req->async.iosb = io_status;
879 req->async.arg = fileio;
880 req->async.apc = fileio_apc;
881 req->async.event = hEvent;
882 status = wine_server_call( req );
884 SERVER_END_REQ;
886 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, fileio );
887 goto done;
889 else /* synchronous write, wait for the fd to become ready */
891 struct pollfd pfd;
892 int ret, timeout;
894 if (!timeout_init_done)
896 timeout_init_done = 1;
897 if ((status = get_io_timeouts( hFile, type, length, FALSE, &timeouts )))
898 goto done;
899 if (hEvent) NtResetEvent( hEvent, NULL );
901 timeout = get_next_io_timeout( &timeouts, total );
903 pfd.fd = unix_handle;
904 pfd.events = POLLOUT;
906 if (!timeout || !(ret = poll( &pfd, 1, timeout )))
908 /* return with what we got so far */
909 status = total ? STATUS_SUCCESS : STATUS_TIMEOUT;
910 goto done;
912 if (ret == -1 && errno != EINTR)
914 status = FILE_GetNtStatus();
915 goto done;
917 /* will now restart the write */
921 done:
922 if (needs_close) close( unix_handle );
923 if (status == STATUS_SUCCESS)
925 io_status->u.Status = status;
926 io_status->Information = total;
927 TRACE("= SUCCESS (%u)\n", total);
928 if (hEvent) NtSetEvent( hEvent, NULL );
929 if (apc) NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC)apc,
930 (ULONG_PTR)apc_user, (ULONG_PTR)io_status, 0 );
932 else
934 TRACE("= 0x%08x\n", status);
935 if (status != STATUS_PENDING && hEvent) NtResetEvent( hEvent, NULL );
937 return status;
941 struct async_ioctl
943 HANDLE handle; /* handle to the device */
944 void *buffer; /* buffer for output */
945 ULONG size; /* size of buffer */
946 PIO_APC_ROUTINE apc; /* user apc params */
947 void *apc_arg;
950 /* callback for ioctl async I/O completion */
951 static NTSTATUS ioctl_completion( void *arg, IO_STATUS_BLOCK *io, NTSTATUS status )
953 struct async_ioctl *async = arg;
955 if (status == STATUS_ALERTED)
957 SERVER_START_REQ( get_ioctl_result )
959 req->handle = async->handle;
960 req->user_arg = async;
961 wine_server_set_reply( req, async->buffer, async->size );
962 if (!(status = wine_server_call( req )))
963 io->Information = wine_server_reply_size( reply );
965 SERVER_END_REQ;
967 if (status != STATUS_PENDING) io->u.Status = status;
968 return status;
971 /* callback for ioctl user APC */
972 static void WINAPI ioctl_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
974 struct async_ioctl *async = arg;
975 if (async->apc) async->apc( async->apc_arg, io, reserved );
976 RtlFreeHeap( GetProcessHeap(), 0, async );
979 /* do a ioctl call through the server */
980 static NTSTATUS server_ioctl_file( HANDLE handle, HANDLE event,
981 PIO_APC_ROUTINE apc, PVOID apc_context,
982 IO_STATUS_BLOCK *io, ULONG code,
983 const void *in_buffer, ULONG in_size,
984 PVOID out_buffer, ULONG out_size )
986 struct async_ioctl *async;
987 NTSTATUS status;
988 HANDLE wait_handle;
989 ULONG options;
991 if (!(async = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*async) )))
992 return STATUS_NO_MEMORY;
993 async->handle = handle;
994 async->buffer = out_buffer;
995 async->size = out_size;
996 async->apc = apc;
997 async->apc_arg = apc_context;
999 SERVER_START_REQ( ioctl )
1001 req->handle = handle;
1002 req->code = code;
1003 req->async.callback = ioctl_completion;
1004 req->async.iosb = io;
1005 req->async.arg = async;
1006 req->async.apc = (apc || event) ? ioctl_apc : NULL;
1007 req->async.event = event;
1008 wine_server_add_data( req, in_buffer, in_size );
1009 wine_server_set_reply( req, out_buffer, out_size );
1010 if (!(status = wine_server_call( req )))
1011 io->Information = wine_server_reply_size( reply );
1012 wait_handle = reply->wait;
1013 options = reply->options;
1015 SERVER_END_REQ;
1017 if (status == STATUS_NOT_SUPPORTED)
1018 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
1019 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1021 if (status != STATUS_PENDING) RtlFreeHeap( GetProcessHeap(), 0, async );
1023 if (wait_handle)
1025 NtWaitForSingleObject( wait_handle, (options & FILE_SYNCHRONOUS_IO_ALERT), NULL );
1026 status = io->u.Status;
1027 NtClose( wait_handle );
1028 RtlFreeHeap( GetProcessHeap(), 0, async );
1031 return status;
1035 /**************************************************************************
1036 * NtDeviceIoControlFile [NTDLL.@]
1037 * ZwDeviceIoControlFile [NTDLL.@]
1039 * Perform an I/O control operation on an open file handle.
1041 * PARAMS
1042 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1043 * event [I] Event to signal upon completion (or NULL)
1044 * apc [I] Callback to call upon completion (or NULL)
1045 * apc_context [I] Context for ApcRoutine (or NULL)
1046 * io [O] Receives information about the operation on return
1047 * code [I] Control code for the operation to perform
1048 * in_buffer [I] Source for any input data required (or NULL)
1049 * in_size [I] Size of InputBuffer
1050 * out_buffer [O] Source for any output data returned (or NULL)
1051 * out_size [I] Size of OutputBuffer
1053 * RETURNS
1054 * Success: 0. IoStatusBlock is updated.
1055 * Failure: An NTSTATUS error code describing the error.
1057 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
1058 PIO_APC_ROUTINE apc, PVOID apc_context,
1059 PIO_STATUS_BLOCK io, ULONG code,
1060 PVOID in_buffer, ULONG in_size,
1061 PVOID out_buffer, ULONG out_size)
1063 ULONG device = (code >> 16);
1064 NTSTATUS status;
1066 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1067 handle, event, apc, apc_context, io, code,
1068 in_buffer, in_size, out_buffer, out_size);
1070 switch(device)
1072 case FILE_DEVICE_DISK:
1073 case FILE_DEVICE_CD_ROM:
1074 case FILE_DEVICE_DVD:
1075 case FILE_DEVICE_CONTROLLER:
1076 case FILE_DEVICE_MASS_STORAGE:
1077 status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1078 in_buffer, in_size, out_buffer, out_size);
1079 break;
1080 case FILE_DEVICE_SERIAL_PORT:
1081 status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
1082 in_buffer, in_size, out_buffer, out_size);
1083 break;
1084 case FILE_DEVICE_TAPE:
1085 status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
1086 in_buffer, in_size, out_buffer, out_size);
1087 break;
1088 default:
1089 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1090 in_buffer, in_size, out_buffer, out_size );
1091 break;
1093 if (status != STATUS_PENDING) io->u.Status = status;
1094 return status;
1098 /**************************************************************************
1099 * NtFsControlFile [NTDLL.@]
1100 * ZwFsControlFile [NTDLL.@]
1102 * Perform a file system control operation on an open file handle.
1104 * PARAMS
1105 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1106 * event [I] Event to signal upon completion (or NULL)
1107 * apc [I] Callback to call upon completion (or NULL)
1108 * apc_context [I] Context for ApcRoutine (or NULL)
1109 * io [O] Receives information about the operation on return
1110 * code [I] Control code for the operation to perform
1111 * in_buffer [I] Source for any input data required (or NULL)
1112 * in_size [I] Size of InputBuffer
1113 * out_buffer [O] Source for any output data returned (or NULL)
1114 * out_size [I] Size of OutputBuffer
1116 * RETURNS
1117 * Success: 0. IoStatusBlock is updated.
1118 * Failure: An NTSTATUS error code describing the error.
1120 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
1121 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
1122 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
1124 NTSTATUS status;
1126 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
1127 handle, event, apc, apc_context, io, code,
1128 in_buffer, in_size, out_buffer, out_size);
1130 if (!io) return STATUS_INVALID_PARAMETER;
1132 switch(code)
1134 case FSCTL_DISMOUNT_VOLUME:
1135 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1136 in_buffer, in_size, out_buffer, out_size );
1137 if (!status) status = DIR_unmount_device( handle );
1138 break;
1140 case FSCTL_PIPE_PEEK:
1142 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1143 int avail = 0, fd, needs_close;
1145 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1147 status = STATUS_INFO_LENGTH_MISMATCH;
1148 break;
1151 if ((status = server_get_unix_fd( handle, FILE_READ_DATA, &fd, &needs_close, NULL, NULL )))
1152 break;
1154 #ifdef FIONREAD
1155 if (ioctl( fd, FIONREAD, &avail ) != 0)
1157 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1158 if (needs_close) close( fd );
1159 status = FILE_GetNtStatus();
1160 break;
1162 #endif
1163 if (!avail) /* check for closed pipe */
1165 struct pollfd pollfd;
1166 int ret;
1168 pollfd.fd = fd;
1169 pollfd.events = POLLIN;
1170 pollfd.revents = 0;
1171 ret = poll( &pollfd, 1, 0 );
1172 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1174 if (needs_close) close( fd );
1175 status = STATUS_PIPE_BROKEN;
1176 break;
1179 buffer->NamedPipeState = 0; /* FIXME */
1180 buffer->ReadDataAvailable = avail;
1181 buffer->NumberOfMessages = 0; /* FIXME */
1182 buffer->MessageLength = 0; /* FIXME */
1183 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1184 status = STATUS_SUCCESS;
1185 if (avail)
1187 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1188 if (data_size)
1190 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1191 if (res >= 0) io->Information += res;
1194 if (needs_close) close( fd );
1196 break;
1198 case FSCTL_PIPE_DISCONNECT:
1199 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1200 in_buffer, in_size, out_buffer, out_size );
1201 if (!status)
1203 int fd = server_remove_fd_from_cache( handle );
1204 if (fd != -1) close( fd );
1206 break;
1208 case FSCTL_PIPE_IMPERSONATE:
1209 FIXME("FSCTL_PIPE_IMPERSONATE: impersonating self\n");
1210 status = RtlImpersonateSelf( SecurityImpersonation );
1211 break;
1213 case FSCTL_LOCK_VOLUME:
1214 case FSCTL_UNLOCK_VOLUME:
1215 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1216 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1217 status = STATUS_SUCCESS;
1218 break;
1220 case FSCTL_PIPE_LISTEN:
1221 case FSCTL_PIPE_WAIT:
1222 default:
1223 status = server_ioctl_file( handle, event, apc, apc_context, io, code,
1224 in_buffer, in_size, out_buffer, out_size );
1225 break;
1228 if (status != STATUS_PENDING) io->u.Status = status;
1229 return status;
1232 /******************************************************************************
1233 * NtSetVolumeInformationFile [NTDLL.@]
1234 * ZwSetVolumeInformationFile [NTDLL.@]
1236 * Set volume information for an open file handle.
1238 * PARAMS
1239 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1240 * IoStatusBlock [O] Receives information about the operation on return
1241 * FsInformation [I] Source for volume information
1242 * Length [I] Size of FsInformation
1243 * FsInformationClass [I] Type of volume information to set
1245 * RETURNS
1246 * Success: 0. IoStatusBlock is updated.
1247 * Failure: An NTSTATUS error code describing the error.
1249 NTSTATUS WINAPI NtSetVolumeInformationFile(
1250 IN HANDLE FileHandle,
1251 PIO_STATUS_BLOCK IoStatusBlock,
1252 PVOID FsInformation,
1253 ULONG Length,
1254 FS_INFORMATION_CLASS FsInformationClass)
1256 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1257 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1258 return 0;
1261 /******************************************************************************
1262 * NtQueryInformationFile [NTDLL.@]
1263 * ZwQueryInformationFile [NTDLL.@]
1265 * Get information about an open file handle.
1267 * PARAMS
1268 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1269 * io [O] Receives information about the operation on return
1270 * ptr [O] Destination for file information
1271 * len [I] Size of FileInformation
1272 * class [I] Type of file information to get
1274 * RETURNS
1275 * Success: 0. IoStatusBlock and FileInformation are updated.
1276 * Failure: An NTSTATUS error code describing the error.
1278 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1279 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1281 static const size_t info_sizes[] =
1284 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1285 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1286 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1287 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1288 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1289 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1290 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1291 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1292 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1293 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1294 0, /* FileLinkInformation */
1295 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1296 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1297 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1298 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1299 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1300 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1301 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1302 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1303 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1304 0, /* FileAlternateNameInformation */
1305 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1306 0, /* FilePipeInformation */
1307 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1308 0, /* FilePipeRemoteInformation */
1309 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1310 0, /* FileMailslotSetInformation */
1311 0, /* FileCompressionInformation */
1312 0, /* FileObjectIdInformation */
1313 0, /* FileCompletionInformation */
1314 0, /* FileMoveClusterInformation */
1315 0, /* FileQuotaInformation */
1316 0, /* FileReparsePointInformation */
1317 0, /* FileNetworkOpenInformation */
1318 0, /* FileAttributeTagInformation */
1319 0 /* FileTrackingInformation */
1322 struct stat st;
1323 int fd, needs_close = FALSE;
1325 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1327 io->Information = 0;
1329 if (class <= 0 || class >= FileMaximumInformation)
1330 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1331 if (!info_sizes[class])
1333 FIXME("Unsupported class (%d)\n", class);
1334 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1336 if (len < info_sizes[class])
1337 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1339 if (class != FilePipeLocalInformation)
1341 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1342 return io->u.Status;
1345 switch (class)
1347 case FileBasicInformation:
1349 FILE_BASIC_INFORMATION *info = ptr;
1351 if (fstat( fd, &st ) == -1)
1352 io->u.Status = FILE_GetNtStatus();
1353 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1354 io->u.Status = STATUS_INVALID_INFO_CLASS;
1355 else
1357 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1358 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1359 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1360 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1361 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1362 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1363 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1364 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1367 break;
1368 case FileStandardInformation:
1370 FILE_STANDARD_INFORMATION *info = ptr;
1372 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1373 else
1375 if ((info->Directory = S_ISDIR(st.st_mode)))
1377 info->AllocationSize.QuadPart = 0;
1378 info->EndOfFile.QuadPart = 0;
1379 info->NumberOfLinks = 1;
1380 info->DeletePending = FALSE;
1382 else
1384 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1385 info->EndOfFile.QuadPart = st.st_size;
1386 info->NumberOfLinks = st.st_nlink;
1387 info->DeletePending = FALSE; /* FIXME */
1391 break;
1392 case FilePositionInformation:
1394 FILE_POSITION_INFORMATION *info = ptr;
1395 off_t res = lseek( fd, 0, SEEK_CUR );
1396 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1397 else info->CurrentByteOffset.QuadPart = res;
1399 break;
1400 case FileInternalInformation:
1402 FILE_INTERNAL_INFORMATION *info = ptr;
1404 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1405 else info->IndexNumber.QuadPart = st.st_ino;
1407 break;
1408 case FileEaInformation:
1410 FILE_EA_INFORMATION *info = ptr;
1411 info->EaSize = 0;
1413 break;
1414 case FileEndOfFileInformation:
1416 FILE_END_OF_FILE_INFORMATION *info = ptr;
1418 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1419 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1421 break;
1422 case FileAllInformation:
1424 FILE_ALL_INFORMATION *info = ptr;
1426 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1427 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1428 io->u.Status = STATUS_INVALID_INFO_CLASS;
1429 else
1431 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1433 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1434 info->StandardInformation.AllocationSize.QuadPart = 0;
1435 info->StandardInformation.EndOfFile.QuadPart = 0;
1436 info->StandardInformation.NumberOfLinks = 1;
1437 info->StandardInformation.DeletePending = FALSE;
1439 else
1441 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1442 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1443 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1444 info->StandardInformation.NumberOfLinks = st.st_nlink;
1445 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1447 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1448 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1449 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1450 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1451 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1452 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1453 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1454 info->EaInformation.EaSize = 0;
1455 info->AccessInformation.AccessFlags = 0; /* FIXME */
1456 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1457 info->ModeInformation.Mode = 0; /* FIXME */
1458 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1459 info->NameInformation.FileNameLength = 0;
1460 io->Information = sizeof(*info) - sizeof(WCHAR);
1463 break;
1464 case FileMailslotQueryInformation:
1466 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1468 SERVER_START_REQ( set_mailslot_info )
1470 req->handle = hFile;
1471 req->flags = 0;
1472 io->u.Status = wine_server_call( req );
1473 if( io->u.Status == STATUS_SUCCESS )
1475 info->MaximumMessageSize = reply->max_msgsize;
1476 info->MailslotQuota = 0;
1477 info->NextMessageSize = 0;
1478 info->MessagesAvailable = 0;
1479 info->ReadTimeout.QuadPart = reply->read_timeout;
1482 SERVER_END_REQ;
1483 if (!io->u.Status)
1485 char *tmpbuf;
1486 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1487 if (size > 0x10000) size = 0x10000;
1488 if ((tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1490 int fd, needs_close;
1491 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1493 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1494 info->MessagesAvailable = (res > 0);
1495 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1496 if (needs_close) close( fd );
1498 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1502 break;
1503 case FilePipeLocalInformation:
1505 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1507 SERVER_START_REQ( get_named_pipe_info )
1509 req->handle = hFile;
1510 if (!(io->u.Status = wine_server_call( req )))
1512 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1513 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1514 pli->NamedPipeConfiguration = 0; /* FIXME */
1515 pli->MaximumInstances = reply->maxinstances;
1516 pli->CurrentInstances = reply->instances;
1517 pli->InboundQuota = reply->insize;
1518 pli->ReadDataAvailable = 0; /* FIXME */
1519 pli->OutboundQuota = reply->outsize;
1520 pli->WriteQuotaAvailable = 0; /* FIXME */
1521 pli->NamedPipeState = 0; /* FIXME */
1522 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1523 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1526 SERVER_END_REQ;
1528 break;
1529 default:
1530 FIXME("Unsupported class (%d)\n", class);
1531 io->u.Status = STATUS_NOT_IMPLEMENTED;
1532 break;
1534 if (needs_close) close( fd );
1535 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1536 return io->u.Status;
1539 /******************************************************************************
1540 * NtSetInformationFile [NTDLL.@]
1541 * ZwSetInformationFile [NTDLL.@]
1543 * Set information about an open file handle.
1545 * PARAMS
1546 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1547 * io [O] Receives information about the operation on return
1548 * ptr [I] Source for file information
1549 * len [I] Size of FileInformation
1550 * class [I] Type of file information to set
1552 * RETURNS
1553 * Success: 0. io is updated.
1554 * Failure: An NTSTATUS error code describing the error.
1556 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1557 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1559 int fd, needs_close;
1561 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1563 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1564 return io->u.Status;
1566 io->u.Status = STATUS_SUCCESS;
1567 switch (class)
1569 case FileBasicInformation:
1570 if (len >= sizeof(FILE_BASIC_INFORMATION))
1572 struct stat st;
1573 const FILE_BASIC_INFORMATION *info = ptr;
1575 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1577 ULONGLONG sec, nsec;
1578 struct timeval tv[2];
1580 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1583 tv[0].tv_sec = tv[0].tv_usec = 0;
1584 tv[1].tv_sec = tv[1].tv_usec = 0;
1585 if (!fstat( fd, &st ))
1587 tv[0].tv_sec = st.st_atime;
1588 tv[1].tv_sec = st.st_mtime;
1591 if (info->LastAccessTime.QuadPart)
1593 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1594 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1595 tv[0].tv_usec = (UINT)nsec / 10;
1597 if (info->LastWriteTime.QuadPart)
1599 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1600 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1601 tv[1].tv_usec = (UINT)nsec / 10;
1603 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1606 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1608 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1609 else
1611 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1613 if (S_ISDIR( st.st_mode))
1614 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1615 else
1616 st.st_mode &= ~0222; /* clear write permission bits */
1618 else
1620 /* add write permission only where we already have read permission */
1621 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1623 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1627 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1628 break;
1630 case FilePositionInformation:
1631 if (len >= sizeof(FILE_POSITION_INFORMATION))
1633 const FILE_POSITION_INFORMATION *info = ptr;
1635 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1636 io->u.Status = FILE_GetNtStatus();
1638 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1639 break;
1641 case FileEndOfFileInformation:
1642 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1644 struct stat st;
1645 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1647 /* first try normal truncate */
1648 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1650 /* now check for the need to extend the file */
1651 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1653 static const char zero;
1655 /* extend the file one byte beyond the requested size and then truncate it */
1656 /* this should work around ftruncate implementations that can't extend files */
1657 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1658 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1660 io->u.Status = FILE_GetNtStatus();
1662 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1663 break;
1665 case FileMailslotSetInformation:
1667 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1669 SERVER_START_REQ( set_mailslot_info )
1671 req->handle = handle;
1672 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1673 req->read_timeout = info->ReadTimeout.QuadPart;
1674 io->u.Status = wine_server_call( req );
1676 SERVER_END_REQ;
1678 break;
1680 case FileCompletionInformation:
1681 if (len >= sizeof(FILE_COMPLETION_INFORMATION))
1683 FILE_COMPLETION_INFORMATION *info = (FILE_COMPLETION_INFORMATION *)ptr;
1685 SERVER_START_REQ( set_completion_info )
1687 req->handle = handle;
1688 req->chandle = info->CompletionPort;
1689 req->ckey = info->CompletionKey;
1690 io->u.Status = wine_server_call( req );
1692 SERVER_END_REQ;
1693 } else
1694 io->u.Status = STATUS_INVALID_PARAMETER_3;
1695 break;
1697 default:
1698 FIXME("Unsupported class (%d)\n", class);
1699 io->u.Status = STATUS_NOT_IMPLEMENTED;
1700 break;
1702 if (needs_close) close( fd );
1703 io->Information = 0;
1704 return io->u.Status;
1708 /******************************************************************************
1709 * NtQueryFullAttributesFile (NTDLL.@)
1711 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1712 FILE_NETWORK_OPEN_INFORMATION *info )
1714 ANSI_STRING unix_name;
1715 NTSTATUS status;
1717 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1718 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1720 struct stat st;
1722 if (stat( unix_name.Buffer, &st ) == -1)
1723 status = FILE_GetNtStatus();
1724 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1725 status = STATUS_INVALID_INFO_CLASS;
1726 else
1728 if (S_ISDIR(st.st_mode))
1730 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1731 info->AllocationSize.QuadPart = 0;
1732 info->EndOfFile.QuadPart = 0;
1734 else
1736 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1737 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1738 info->EndOfFile.QuadPart = st.st_size;
1740 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1741 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1742 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1743 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1744 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1745 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1746 if (DIR_is_hidden_file( attr->ObjectName ))
1747 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1749 RtlFreeAnsiString( &unix_name );
1751 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1752 return status;
1756 /******************************************************************************
1757 * NtQueryAttributesFile (NTDLL.@)
1758 * ZwQueryAttributesFile (NTDLL.@)
1760 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1762 FILE_NETWORK_OPEN_INFORMATION full_info;
1763 NTSTATUS status;
1765 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1767 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1768 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1769 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1770 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1771 info->FileAttributes = full_info.FileAttributes;
1773 return status;
1777 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1778 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1779 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1780 unsigned int flags )
1782 if (!strcmp("cd9660", fstypename) || !strcmp("udf", fstypename))
1784 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1785 /* Don't assume read-only, let the mount options set it below */
1786 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1788 else if (!strcmp("nfs", fstypename) || !strcmp("nwfs", fstypename) ||
1789 !strcmp("smbfs", fstypename) || !strcmp("afpfs", fstypename))
1791 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1792 info->Characteristics |= FILE_REMOTE_DEVICE;
1794 else if (!strcmp("procfs", fstypename))
1795 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1796 else
1797 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1799 if (flags & MNT_RDONLY)
1800 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1802 if (!(flags & MNT_LOCAL))
1804 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1805 info->Characteristics |= FILE_REMOTE_DEVICE;
1808 #endif
1810 /******************************************************************************
1811 * get_device_info
1813 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1815 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1817 struct stat st;
1819 info->Characteristics = 0;
1820 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1821 if (S_ISCHR( st.st_mode ))
1823 info->DeviceType = FILE_DEVICE_UNKNOWN;
1824 #ifdef linux
1825 switch(major(st.st_rdev))
1827 case MEM_MAJOR:
1828 info->DeviceType = FILE_DEVICE_NULL;
1829 break;
1830 case TTY_MAJOR:
1831 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1832 break;
1833 case LP_MAJOR:
1834 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1835 break;
1836 case SCSI_TAPE_MAJOR:
1837 info->DeviceType = FILE_DEVICE_TAPE;
1838 break;
1840 #endif
1842 else if (S_ISBLK( st.st_mode ))
1844 info->DeviceType = FILE_DEVICE_DISK;
1846 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1848 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1850 else /* regular file or directory */
1852 #if defined(linux) && defined(HAVE_FSTATFS)
1853 struct statfs stfs;
1855 /* check for floppy disk */
1856 if (major(st.st_dev) == FLOPPY_MAJOR)
1857 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1859 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1860 switch (stfs.f_type)
1862 case 0x9660: /* iso9660 */
1863 case 0x9fa1: /* supermount */
1864 case 0x15013346: /* udf */
1865 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1866 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1867 break;
1868 case 0x6969: /* nfs */
1869 case 0x517B: /* smbfs */
1870 case 0x564c: /* ncpfs */
1871 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1872 info->Characteristics |= FILE_REMOTE_DEVICE;
1873 break;
1874 case 0x01021994: /* tmpfs */
1875 case 0x28cd3d45: /* cramfs */
1876 case 0x1373: /* devfs */
1877 case 0x9fa0: /* procfs */
1878 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1879 break;
1880 default:
1881 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1882 break;
1884 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1885 struct statfs stfs;
1887 if (fstatfs( fd, &stfs ) < 0)
1888 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1889 else
1890 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flags );
1891 #elif defined(__NetBSD__)
1892 struct statvfs stfs;
1894 if (fstatvfs( fd, &stfs) < 0)
1895 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1896 else
1897 get_device_info_fstatfs( info, stfs.f_fstypename, stfs.f_flag );
1898 #elif defined(sun)
1899 /* Use dkio to work out device types */
1901 # include <sys/dkio.h>
1902 # include <sys/vtoc.h>
1903 struct dk_cinfo dkinf;
1904 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1905 if(retval==-1){
1906 WARN("Unable to get disk device type information - assuming a disk like device\n");
1907 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1909 switch (dkinf.dki_ctype)
1911 case DKC_CDROM:
1912 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1913 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1914 break;
1915 case DKC_NCRFLOPPY:
1916 case DKC_SMSFLOPPY:
1917 case DKC_INTEL82072:
1918 case DKC_INTEL82077:
1919 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1920 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1921 break;
1922 case DKC_MD:
1923 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1924 break;
1925 default:
1926 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1929 #else
1930 static int warned;
1931 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1932 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1933 #endif
1934 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1936 return STATUS_SUCCESS;
1940 /******************************************************************************
1941 * NtQueryVolumeInformationFile [NTDLL.@]
1942 * ZwQueryVolumeInformationFile [NTDLL.@]
1944 * Get volume information for an open file handle.
1946 * PARAMS
1947 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1948 * io [O] Receives information about the operation on return
1949 * buffer [O] Destination for volume information
1950 * length [I] Size of FsInformation
1951 * info_class [I] Type of volume information to set
1953 * RETURNS
1954 * Success: 0. io and buffer are updated.
1955 * Failure: An NTSTATUS error code describing the error.
1957 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1958 PVOID buffer, ULONG length,
1959 FS_INFORMATION_CLASS info_class )
1961 int fd, needs_close;
1962 struct stat st;
1964 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1965 return io->u.Status;
1967 io->u.Status = STATUS_NOT_IMPLEMENTED;
1968 io->Information = 0;
1970 switch( info_class )
1972 case FileFsVolumeInformation:
1973 FIXME( "%p: volume info not supported\n", handle );
1974 break;
1975 case FileFsLabelInformation:
1976 FIXME( "%p: label info not supported\n", handle );
1977 break;
1978 case FileFsSizeInformation:
1979 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1980 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1981 else
1983 FILE_FS_SIZE_INFORMATION *info = buffer;
1985 if (fstat( fd, &st ) < 0)
1987 io->u.Status = FILE_GetNtStatus();
1988 break;
1990 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1992 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1994 else
1996 /* Linux's fstatvfs is buggy */
1997 #if !defined(linux) || !defined(HAVE_FSTATFS)
1998 struct statvfs stfs;
2000 if (fstatvfs( fd, &stfs ) < 0)
2002 io->u.Status = FILE_GetNtStatus();
2003 break;
2005 info->BytesPerSector = stfs.f_frsize;
2006 #else
2007 struct statfs stfs;
2008 if (fstatfs( fd, &stfs ) < 0)
2010 io->u.Status = FILE_GetNtStatus();
2011 break;
2013 info->BytesPerSector = stfs.f_bsize;
2014 #endif
2015 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
2016 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
2017 info->SectorsPerAllocationUnit = 1;
2018 io->Information = sizeof(*info);
2019 io->u.Status = STATUS_SUCCESS;
2022 break;
2023 case FileFsDeviceInformation:
2024 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
2025 io->u.Status = STATUS_BUFFER_TOO_SMALL;
2026 else
2028 FILE_FS_DEVICE_INFORMATION *info = buffer;
2030 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
2031 io->Information = sizeof(*info);
2033 break;
2034 case FileFsAttributeInformation:
2035 FIXME( "%p: attribute info not supported\n", handle );
2036 break;
2037 case FileFsControlInformation:
2038 FIXME( "%p: control info not supported\n", handle );
2039 break;
2040 case FileFsFullSizeInformation:
2041 FIXME( "%p: full size info not supported\n", handle );
2042 break;
2043 case FileFsObjectIdInformation:
2044 FIXME( "%p: object id info not supported\n", handle );
2045 break;
2046 case FileFsMaximumInformation:
2047 FIXME( "%p: maximum info not supported\n", handle );
2048 break;
2049 default:
2050 io->u.Status = STATUS_INVALID_PARAMETER;
2051 break;
2053 if (needs_close) close( fd );
2054 return io->u.Status;
2058 /******************************************************************
2059 * NtFlushBuffersFile (NTDLL.@)
2061 * Flush any buffered data on an open file handle.
2063 * PARAMS
2064 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
2065 * IoStatusBlock [O] Receives information about the operation on return
2067 * RETURNS
2068 * Success: 0. IoStatusBlock is updated.
2069 * Failure: An NTSTATUS error code describing the error.
2071 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
2073 NTSTATUS ret;
2074 HANDLE hEvent = NULL;
2076 SERVER_START_REQ( flush_file )
2078 req->handle = hFile;
2079 ret = wine_server_call( req );
2080 hEvent = reply->event;
2082 SERVER_END_REQ;
2083 if (!ret && hEvent)
2085 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
2086 NtClose( hEvent );
2088 return ret;
2091 /******************************************************************
2092 * NtLockFile (NTDLL.@)
2096 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
2097 PIO_APC_ROUTINE apc, void* apc_user,
2098 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
2099 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
2100 BOOLEAN exclusive )
2102 NTSTATUS ret;
2103 HANDLE handle;
2104 BOOLEAN async;
2106 if (apc || io_status || key)
2108 FIXME("Unimplemented yet parameter\n");
2109 return STATUS_NOT_IMPLEMENTED;
2112 for (;;)
2114 SERVER_START_REQ( lock_file )
2116 req->handle = hFile;
2117 req->offset = offset->QuadPart;
2118 req->count = count->QuadPart;
2119 req->shared = !exclusive;
2120 req->wait = !dont_wait;
2121 ret = wine_server_call( req );
2122 handle = reply->handle;
2123 async = reply->overlapped;
2125 SERVER_END_REQ;
2126 if (ret != STATUS_PENDING)
2128 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
2129 return ret;
2132 if (async)
2134 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
2135 if (handle) NtClose( handle );
2136 return STATUS_PENDING;
2138 if (handle)
2140 NtWaitForSingleObject( handle, FALSE, NULL );
2141 NtClose( handle );
2143 else
2145 LARGE_INTEGER time;
2147 /* Unix lock conflict, sleep a bit and retry */
2148 time.QuadPart = 100 * (ULONGLONG)10000;
2149 time.QuadPart = -time.QuadPart;
2150 NtDelayExecution( FALSE, &time );
2156 /******************************************************************
2157 * NtUnlockFile (NTDLL.@)
2161 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2162 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2163 PULONG key )
2165 NTSTATUS status;
2167 TRACE( "%p %x%08x %x%08x\n",
2168 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2170 if (io_status || key)
2172 FIXME("Unimplemented yet parameter\n");
2173 return STATUS_NOT_IMPLEMENTED;
2176 SERVER_START_REQ( unlock_file )
2178 req->handle = hFile;
2179 req->offset = offset->QuadPart;
2180 req->count = count->QuadPart;
2181 status = wine_server_call( req );
2183 SERVER_END_REQ;
2184 return status;
2187 /******************************************************************
2188 * NtCreateNamedPipeFile (NTDLL.@)
2192 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2193 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2194 ULONG sharing, ULONG dispo, ULONG options,
2195 ULONG pipe_type, ULONG read_mode,
2196 ULONG completion_mode, ULONG max_inst,
2197 ULONG inbound_quota, ULONG outbound_quota,
2198 PLARGE_INTEGER timeout)
2200 NTSTATUS status;
2202 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2203 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2204 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2205 outbound_quota, timeout);
2207 /* assume we only get relative timeout */
2208 if (timeout->QuadPart > 0)
2209 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2211 SERVER_START_REQ( create_named_pipe )
2213 req->access = access;
2214 req->attributes = attr->Attributes;
2215 req->rootdir = attr->RootDirectory;
2216 req->options = options;
2217 req->flags =
2218 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2219 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2220 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2221 req->maxinstances = max_inst;
2222 req->outsize = outbound_quota;
2223 req->insize = inbound_quota;
2224 req->timeout = timeout->QuadPart;
2225 wine_server_add_data( req, attr->ObjectName->Buffer,
2226 attr->ObjectName->Length );
2227 status = wine_server_call( req );
2228 if (!status) *handle = reply->handle;
2230 SERVER_END_REQ;
2231 return status;
2234 /******************************************************************
2235 * NtDeleteFile (NTDLL.@)
2239 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2241 NTSTATUS status;
2242 HANDLE hFile;
2243 IO_STATUS_BLOCK io;
2245 TRACE("%p\n", ObjectAttributes);
2246 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2247 ObjectAttributes, &io, NULL, 0,
2248 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2249 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2250 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2251 return status;
2254 /******************************************************************
2255 * NtCancelIoFile (NTDLL.@)
2259 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2261 LARGE_INTEGER timeout;
2263 TRACE("%p %p\n", hFile, io_status );
2265 SERVER_START_REQ( cancel_async )
2267 req->handle = hFile;
2268 wine_server_call( req );
2270 SERVER_END_REQ;
2271 /* Let some APC be run, so that we can run the remaining APCs on hFile
2272 * either the cancelation of the pending one, but also the execution
2273 * of the queued APC, but not yet run. This is needed to ensure proper
2274 * clean-up of allocated data.
2276 timeout.u.LowPart = timeout.u.HighPart = 0;
2277 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2280 /******************************************************************************
2281 * NtCreateMailslotFile [NTDLL.@]
2282 * ZwCreateMailslotFile [NTDLL.@]
2284 * PARAMS
2285 * pHandle [O] pointer to receive the handle created
2286 * DesiredAccess [I] access mode (read, write, etc)
2287 * ObjectAttributes [I] fully qualified NT path of the mailslot
2288 * IoStatusBlock [O] receives completion status and other info
2289 * CreateOptions [I]
2290 * MailslotQuota [I]
2291 * MaxMessageSize [I]
2292 * TimeOut [I]
2294 * RETURNS
2295 * An NT status code
2297 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2298 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2299 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2300 PLARGE_INTEGER TimeOut)
2302 LARGE_INTEGER timeout;
2303 NTSTATUS ret;
2305 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2306 pHandle, DesiredAccess, attr, IoStatusBlock,
2307 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2309 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2310 if (!attr) return STATUS_INVALID_PARAMETER;
2311 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2314 * For a NULL TimeOut pointer set the default timeout value
2316 if (!TimeOut)
2317 timeout.QuadPart = -1;
2318 else
2319 timeout.QuadPart = TimeOut->QuadPart;
2321 SERVER_START_REQ( create_mailslot )
2323 req->access = DesiredAccess;
2324 req->attributes = attr->Attributes;
2325 req->rootdir = attr->RootDirectory;
2326 req->max_msgsize = MaxMessageSize;
2327 req->read_timeout = timeout.QuadPart;
2328 wine_server_add_data( req, attr->ObjectName->Buffer,
2329 attr->ObjectName->Length );
2330 ret = wine_server_call( req );
2331 if( ret == STATUS_SUCCESS )
2332 *pHandle = reply->handle;
2334 SERVER_END_REQ;
2336 return ret;