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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "wine/port.h"
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
33 #ifdef HAVE_LINUX_MAJOR_H
34 # include <linux/major.h>
36 #ifdef HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
48 #ifdef STATFS_DEFINED_BY_SYS_VFS
51 # ifdef STATFS_DEFINED_BY_SYS_MOUNT
52 # include <sys/mount.h>
54 # ifdef STATFS_DEFINED_BY_SYS_STATFS
55 # include <sys/statfs.h>
60 #define NONAMELESSUNION
61 #define NONAMELESSSTRUCT
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
64 #include "wine/server.h"
65 #include "ntdll_misc.h"
70 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
72 mode_t FILE_umask = 0;
74 #define SECSPERDAY 86400
75 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
77 /**************************************************************************
78 * NtOpenFile [NTDLL.@]
79 * ZwOpenFile [NTDLL.@]
84 * handle [O] Variable that receives the file handle on return
85 * access [I] Access desired by the caller to the file
86 * attr [I] Structure describing the file to be opened
87 * io [O] Receives details about the result of the operation
88 * sharing [I] Type of shared access the caller requires
89 * options [I] Options for the file open
92 * Success: 0. FileHandle and IoStatusBlock are updated.
93 * Failure: An NTSTATUS error code describing the error.
95 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
96 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
97 ULONG sharing, ULONG options )
99 return NtCreateFile( handle, access, attr, io, NULL, 0,
100 sharing, FILE_OPEN, options, NULL, 0 );
103 /**************************************************************************
104 * NtCreateFile [NTDLL.@]
105 * ZwCreateFile [NTDLL.@]
107 * Either create a new file or directory, or open an existing file, device,
108 * directory or volume.
111 * handle [O] Points to a variable which receives the file handle on return
112 * access [I] Desired access to the file
113 * attr [I] Structure describing the file
114 * io [O] Receives information about the operation on return
115 * alloc_size [I] Initial size of the file in bytes
116 * attributes [I] Attributes to create the file with
117 * sharing [I] Type of shared access the caller would like to the file
118 * disposition [I] Specifies what to do, depending on whether the file already exists
119 * options [I] Options for creating a new file
120 * ea_buffer [I] Pointer to an extended attributes buffer
121 * ea_length [I] Length of ea_buffer
124 * Success: 0. handle and io are updated.
125 * Failure: An NTSTATUS error code describing the error.
127 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
128 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
129 ULONG attributes, ULONG sharing, ULONG disposition,
130 ULONG options, PVOID ea_buffer, ULONG ea_length )
132 static const WCHAR pipeW[] = {'\\','?','?','\\','p','i','p','e','\\'};
133 static const WCHAR mailslotW[] = {'\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
134 ANSI_STRING unix_name;
137 TRACE("handle=%p access=%08lx name=%s objattr=%08lx root=%p sec=%p io=%p alloc_size=%p\n"
138 "attr=%08lx sharing=%08lx disp=%ld options=%08lx ea=%p.0x%08lx\n",
139 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
140 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
141 attributes, sharing, disposition, options, ea_buffer, ea_length );
143 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
145 if (attr->RootDirectory)
147 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
148 return STATUS_OBJECT_NAME_NOT_FOUND;
150 if (alloc_size) FIXME( "alloc_size not supported\n" );
152 /* check for named pipe */
154 if (attr->ObjectName->Length > sizeof(pipeW) &&
155 !memicmpW( attr->ObjectName->Buffer, pipeW, sizeof(pipeW)/sizeof(WCHAR) ))
157 SERVER_START_REQ( open_named_pipe )
159 req->access = access;
160 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
161 wine_server_add_data( req, attr->ObjectName->Buffer + 4,
162 attr->ObjectName->Length - 4*sizeof(WCHAR) );
163 io->u.Status = wine_server_call( req );
164 *handle = reply->handle;
170 /* check for mailslot */
172 if (attr->ObjectName->Length > sizeof(mailslotW) &&
173 !memicmpW( attr->ObjectName->Buffer, mailslotW, sizeof(mailslotW)/sizeof(WCHAR) ))
175 SERVER_START_REQ( open_mailslot )
177 req->access = access & GENERIC_WRITE;
178 req->sharing = sharing;
179 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
180 wine_server_add_data( req, attr->ObjectName->Buffer + 4,
181 attr->ObjectName->Length - 4*sizeof(WCHAR) );
182 io->u.Status = wine_server_call( req );
183 *handle = reply->handle;
189 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
190 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
192 if (io->u.Status == STATUS_NO_SUCH_FILE &&
193 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
196 io->u.Status = STATUS_SUCCESS;
199 if (io->u.Status == STATUS_SUCCESS)
201 SERVER_START_REQ( create_file )
203 req->access = access;
204 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
205 req->sharing = sharing;
206 req->create = disposition;
207 req->options = options;
208 req->attrs = attributes;
209 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
210 io->u.Status = wine_server_call( req );
211 *handle = reply->handle;
214 RtlFreeAnsiString( &unix_name );
216 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), io->u.Status );
218 if (io->u.Status == STATUS_SUCCESS)
220 if (created) io->Information = FILE_CREATED;
221 else switch(disposition)
224 io->Information = FILE_SUPERSEDED;
227 io->Information = FILE_CREATED;
231 io->Information = FILE_OPENED;
234 case FILE_OVERWRITE_IF:
235 io->Information = FILE_OVERWRITTEN;
243 /***********************************************************************
244 * Asynchronous file I/O *
246 static void WINAPI FILE_AsyncReadService(void*, PIO_STATUS_BLOCK, ULONG);
247 static void WINAPI FILE_AsyncWriteService(void*, PIO_STATUS_BLOCK, ULONG);
249 typedef struct async_fileio
257 int queue_apc_on_error;
263 static void fileio_terminate(async_fileio *fileio, IO_STATUS_BLOCK* iosb)
265 TRACE("data: %p\n", fileio);
267 wine_server_release_fd( fileio->handle, fileio->fd );
268 if ( fileio->event != INVALID_HANDLE_VALUE )
269 NtSetEvent( fileio->event, NULL );
272 (iosb->u.Status == STATUS_SUCCESS || fileio->queue_apc_on_error))
273 fileio->apc( fileio->apc_user, iosb, iosb->Information );
275 RtlFreeHeap( GetProcessHeap(), 0, fileio );
279 static ULONG fileio_queue_async(async_fileio* fileio, IO_STATUS_BLOCK* iosb,
282 PIO_APC_ROUTINE apc = do_read ? FILE_AsyncReadService : FILE_AsyncWriteService;
285 SERVER_START_REQ( register_async )
287 req->handle = fileio->handle;
290 req->io_user = fileio;
291 req->type = do_read ? ASYNC_TYPE_READ : ASYNC_TYPE_WRITE;
292 req->count = (fileio->count < iosb->Information) ?
293 0 : fileio->count - iosb->Information;
294 status = wine_server_call( req );
298 if ( status ) iosb->u.Status = status;
299 if ( iosb->u.Status != STATUS_PENDING )
301 (apc)( fileio, iosb, iosb->u.Status );
302 return iosb->u.Status;
304 NtCurrentTeb()->num_async_io++;
305 return STATUS_SUCCESS;
308 /***********************************************************************
309 * FILE_GetNtStatus(void)
311 * Retrieve the Nt Status code from errno.
312 * Try to be consistent with FILE_SetDosError().
314 NTSTATUS FILE_GetNtStatus(void)
318 TRACE( "errno = %d\n", errno );
321 case EAGAIN: return STATUS_SHARING_VIOLATION;
322 case EBADF: return STATUS_INVALID_HANDLE;
323 case ENOSPC: return STATUS_DISK_FULL;
326 case EACCES: return STATUS_ACCESS_DENIED;
327 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
328 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
329 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
331 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
332 case EINVAL: return STATUS_INVALID_PARAMETER;
333 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
334 case EPIPE: return STATUS_PIPE_BROKEN;
335 case EIO: return STATUS_DEVICE_NOT_READY;
337 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
340 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
341 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
342 case ENOEXEC: /* ?? */
343 case ESPIPE: /* ?? */
344 case EEXIST: /* ?? */
346 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
347 return STATUS_UNSUCCESSFUL;
351 /***********************************************************************
352 * FILE_AsyncReadService (INTERNAL)
354 * This function is called while the client is waiting on the
355 * server, so we can't make any server calls here.
357 static void WINAPI FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
359 async_fileio *fileio = (async_fileio*)user;
361 int already = iosb->Information;
363 TRACE("%p %p %lu\n", iosb, fileio->buffer, status);
367 case STATUS_ALERTED: /* got some new data */
368 if (iosb->u.Status != STATUS_PENDING) FIXME("unexpected status %08lx\n", iosb->u.Status);
369 /* check to see if the data is ready (non-blocking) */
370 if ( fileio->avail_mode )
371 result = read(fileio->fd, &fileio->buffer[already],
372 fileio->count - already);
375 result = pread(fileio->fd, &fileio->buffer[already],
376 fileio->count - already,
377 fileio->offset + already);
378 if ((result < 0) && (errno == ESPIPE))
379 result = read(fileio->fd, &fileio->buffer[already],
380 fileio->count - already);
385 if (errno == EAGAIN || errno == EINTR)
387 TRACE("Deferred read %d\n", errno);
388 iosb->u.Status = STATUS_PENDING;
390 else /* check to see if the transfer is complete */
391 iosb->u.Status = FILE_GetNtStatus();
393 else if (result == 0)
395 iosb->u.Status = iosb->Information ? STATUS_SUCCESS : STATUS_END_OF_FILE;
399 iosb->Information += result;
400 if (iosb->Information >= fileio->count || fileio->avail_mode)
401 iosb->u.Status = STATUS_SUCCESS;
404 /* if we only have to read the available data, and none is available,
405 * simply cancel the request. If data was available, it has been read
406 * while in by previous call (NtDelayExecution)
408 iosb->u.Status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
411 TRACE("read %d more bytes %ld/%d so far (%s)\n",
412 result, iosb->Information, fileio->count,
413 (iosb->u.Status == STATUS_SUCCESS) ? "success" : "pending");
415 /* queue another async operation ? */
416 if (iosb->u.Status == STATUS_PENDING)
417 fileio_queue_async(fileio, iosb, TRUE);
419 fileio_terminate(fileio, iosb);
422 iosb->u.Status = status;
423 fileio_terminate(fileio, iosb);
429 /******************************************************************************
430 * NtReadFile [NTDLL.@]
431 * ZwReadFile [NTDLL.@]
433 * Read from an open file handle.
436 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
437 * Event [I] Event to signal upon completion (or NULL)
438 * ApcRoutine [I] Callback to call upon completion (or NULL)
439 * ApcContext [I] Context for ApcRoutine (or NULL)
440 * IoStatusBlock [O] Receives information about the operation on return
441 * Buffer [O] Destination for the data read
442 * Length [I] Size of Buffer
443 * ByteOffset [O] Destination for the new file pointer position (or NULL)
444 * Key [O] Function unknown (may be NULL)
447 * Success: 0. IoStatusBlock is updated, and the Information member contains
448 * The number of bytes read.
449 * Failure: An NTSTATUS error code describing the error.
451 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
452 PIO_APC_ROUTINE apc, void* apc_user,
453 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
454 PLARGE_INTEGER offset, PULONG key)
456 int unix_handle, flags;
458 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
459 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
461 if (!io_status) return STATUS_ACCESS_VIOLATION;
463 io_status->Information = 0;
464 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_READ, &unix_handle, &flags );
465 if (io_status->u.Status) return io_status->u.Status;
467 if (flags & FD_FLAG_RECV_SHUTDOWN)
469 wine_server_release_fd( hFile, unix_handle );
470 return STATUS_PIPE_DISCONNECTED;
473 if (flags & FD_FLAG_TIMEOUT)
477 /* this shouldn't happen, but check it */
478 FIXME("NIY-hEvent\n");
479 wine_server_release_fd( hFile, unix_handle );
480 return STATUS_NOT_IMPLEMENTED;
482 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
483 if (io_status->u.Status)
485 wine_server_release_fd( hFile, unix_handle );
486 return io_status->u.Status;
490 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
492 async_fileio* fileio;
495 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
497 wine_server_release_fd( hFile, unix_handle );
498 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
499 return STATUS_NO_MEMORY;
501 fileio->handle = hFile;
502 fileio->count = length;
503 if ( offset == NULL )
507 fileio->offset = offset->QuadPart;
508 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
509 FIXME("High part of offset is lost\n");
512 fileio->apc_user = apc_user;
513 fileio->buffer = buffer;
514 fileio->queue_apc_on_error = 0;
515 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
516 fileio->fd = unix_handle; /* FIXME */
517 fileio->event = hEvent;
518 NtResetEvent(hEvent, NULL);
520 io_status->u.Status = STATUS_PENDING;
521 ret = fileio_queue_async(fileio, io_status, TRUE);
522 if (ret != STATUS_SUCCESS)
524 wine_server_release_fd( hFile, unix_handle );
525 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
526 RtlFreeHeap(GetProcessHeap(), 0, fileio);
529 if (flags & FD_FLAG_TIMEOUT)
531 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
533 if (ret != STATUS_USER_APC)
534 fileio->queue_apc_on_error = 1;
538 LARGE_INTEGER timeout;
540 /* let some APC be run, this will read some already pending data */
541 timeout.u.LowPart = timeout.u.HighPart = 0;
542 ret = NtDelayExecution( TRUE, &timeout );
543 /* the apc didn't run and therefore the completion routine now
544 * needs to be sent errors.
545 * Note that there is no race between setting this flag and
546 * returning errors because apc's are run only during alertable
548 if (ret != STATUS_USER_APC)
549 fileio->queue_apc_on_error = 1;
551 TRACE("= 0x%08lx\n", io_status->u.Status);
552 return io_status->u.Status;
557 FILE_POSITION_INFORMATION fpi;
559 fpi.CurrentByteOffset = *offset;
560 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
561 FilePositionInformation);
562 if (io_status->u.Status)
564 wine_server_release_fd( hFile, unix_handle );
565 return io_status->u.Status;
568 /* code for synchronous reads */
569 while ((io_status->Information = read( unix_handle, buffer, length )) == -1)
571 if ((errno == EAGAIN) || (errno == EINTR)) continue;
574 io_status->Information = 0;
575 io_status->u.Status = STATUS_ACCESS_VIOLATION;
577 else io_status->u.Status = FILE_GetNtStatus();
580 wine_server_release_fd( hFile, unix_handle );
581 TRACE("= 0x%08lx\n", io_status->u.Status);
582 return io_status->u.Status;
585 /***********************************************************************
586 * FILE_AsyncWriteService (INTERNAL)
588 * This function is called while the client is waiting on the
589 * server, so we can't make any server calls here.
591 static void WINAPI FILE_AsyncWriteService(void *ovp, IO_STATUS_BLOCK *iosb, ULONG status)
593 async_fileio *fileio = (async_fileio *) ovp;
595 int already = iosb->Information;
597 TRACE("(%p %p %lu)\n",iosb, fileio->buffer, status);
602 /* write some data (non-blocking) */
603 if ( fileio->avail_mode )
604 result = write(fileio->fd, &fileio->buffer[already],
605 fileio->count - already);
608 result = pwrite(fileio->fd, &fileio->buffer[already],
609 fileio->count - already, fileio->offset + already);
610 if ((result < 0) && (errno == ESPIPE))
611 result = write(fileio->fd, &fileio->buffer[already],
612 fileio->count - already);
617 if (errno == EAGAIN || errno == EINTR) iosb->u.Status = STATUS_PENDING;
618 else iosb->u.Status = FILE_GetNtStatus();
622 iosb->Information += result;
623 iosb->u.Status = (iosb->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
624 TRACE("wrote %d more bytes %ld/%d so far\n",
625 result, iosb->Information, fileio->count);
627 if (iosb->u.Status == STATUS_PENDING)
628 fileio_queue_async(fileio, iosb, FALSE);
631 iosb->u.Status = status;
632 fileio_terminate(fileio, iosb);
637 /******************************************************************************
638 * NtWriteFile [NTDLL.@]
639 * ZwWriteFile [NTDLL.@]
641 * Write to an open file handle.
644 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
645 * Event [I] Event to signal upon completion (or NULL)
646 * ApcRoutine [I] Callback to call upon completion (or NULL)
647 * ApcContext [I] Context for ApcRoutine (or NULL)
648 * IoStatusBlock [O] Receives information about the operation on return
649 * Buffer [I] Source for the data to write
650 * Length [I] Size of Buffer
651 * ByteOffset [O] Destination for the new file pointer position (or NULL)
652 * Key [O] Function unknown (may be NULL)
655 * Success: 0. IoStatusBlock is updated, and the Information member contains
656 * The number of bytes written.
657 * Failure: An NTSTATUS error code describing the error.
659 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
660 PIO_APC_ROUTINE apc, void* apc_user,
661 PIO_STATUS_BLOCK io_status,
662 const void* buffer, ULONG length,
663 PLARGE_INTEGER offset, PULONG key)
665 int unix_handle, flags;
667 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p)!\n",
668 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
670 if (!io_status) return STATUS_ACCESS_VIOLATION;
672 io_status->Information = 0;
673 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_WRITE, &unix_handle, &flags );
674 if (io_status->u.Status) return io_status->u.Status;
676 if (flags & FD_FLAG_SEND_SHUTDOWN)
678 wine_server_release_fd( hFile, unix_handle );
679 return STATUS_PIPE_DISCONNECTED;
682 if (flags & FD_FLAG_TIMEOUT)
686 /* this shouldn't happen, but check it */
687 FIXME("NIY-hEvent\n");
688 wine_server_release_fd( hFile, unix_handle );
689 return STATUS_NOT_IMPLEMENTED;
691 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
692 if (io_status->u.Status)
694 wine_server_release_fd( hFile, unix_handle );
695 return io_status->u.Status;
699 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
701 async_fileio* fileio;
704 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
706 wine_server_release_fd( hFile, unix_handle );
707 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
708 return STATUS_NO_MEMORY;
710 fileio->handle = hFile;
711 fileio->count = length;
714 fileio->offset = offset->QuadPart;
715 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
716 FIXME("High part of offset is lost\n");
723 fileio->apc_user = apc_user;
724 fileio->buffer = (void*)buffer;
725 fileio->queue_apc_on_error = 0;
726 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
727 fileio->fd = unix_handle; /* FIXME */
728 fileio->event = hEvent;
729 NtResetEvent(hEvent, NULL);
731 io_status->Information = 0;
732 io_status->u.Status = STATUS_PENDING;
733 ret = fileio_queue_async(fileio, io_status, FALSE);
734 if (ret != STATUS_SUCCESS)
736 wine_server_release_fd( hFile, unix_handle );
737 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
738 RtlFreeHeap(GetProcessHeap(), 0, fileio);
741 if (flags & FD_FLAG_TIMEOUT)
743 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
745 if (ret != STATUS_USER_APC)
746 fileio->queue_apc_on_error = 1;
750 LARGE_INTEGER timeout;
752 /* let some APC be run, this will write as much data as possible */
753 timeout.u.LowPart = timeout.u.HighPart = 0;
754 ret = NtDelayExecution( TRUE, &timeout );
755 /* the apc didn't run and therefore the completion routine now
756 * needs to be sent errors.
757 * Note that there is no race between setting this flag and
758 * returning errors because apc's are run only during alertable
760 if (ret != STATUS_USER_APC)
761 fileio->queue_apc_on_error = 1;
763 return io_status->u.Status;
768 FILE_POSITION_INFORMATION fpi;
770 fpi.CurrentByteOffset = *offset;
771 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
772 FilePositionInformation);
773 if (io_status->u.Status)
775 wine_server_release_fd( hFile, unix_handle );
776 return io_status->u.Status;
780 /* synchronous file write */
781 while ((io_status->Information = write( unix_handle, buffer, length )) == -1)
783 if ((errno == EAGAIN) || (errno == EINTR)) continue;
786 io_status->Information = 0;
787 io_status->u.Status = STATUS_INVALID_USER_BUFFER;
789 else if (errno == ENOSPC) io_status->u.Status = STATUS_DISK_FULL;
790 else io_status->u.Status = FILE_GetNtStatus();
793 wine_server_release_fd( hFile, unix_handle );
794 return io_status->u.Status;
797 /**************************************************************************
798 * NtDeviceIoControlFile [NTDLL.@]
799 * ZwDeviceIoControlFile [NTDLL.@]
801 * Perform an I/O control operation on an open file handle.
804 * DeviceHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
805 * Event [I] Event to signal upon completion (or NULL)
806 * ApcRoutine [I] Callback to call upon completion (or NULL)
807 * ApcContext [I] Context for ApcRoutine (or NULL)
808 * IoStatusBlock [O] Receives information about the operation on return
809 * IoControlCode [I] Control code for the operation to perform
810 * InputBuffer [I] Source for any input data required (or NULL)
811 * InputBufferSize [I] Size of InputBuffer
812 * OutputBuffer [O] Source for any output data returned (or NULL)
813 * OutputBufferSize [I] Size of OutputBuffer
816 * Success: 0. IoStatusBlock is updated.
817 * Failure: An NTSTATUS error code describing the error.
819 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE DeviceHandle, HANDLE hEvent,
820 PIO_APC_ROUTINE UserApcRoutine,
821 PVOID UserApcContext,
822 PIO_STATUS_BLOCK IoStatusBlock,
825 ULONG InputBufferSize,
827 ULONG OutputBufferSize)
829 TRACE("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx)\n",
830 DeviceHandle, hEvent, UserApcRoutine, UserApcContext,
831 IoStatusBlock, IoControlCode,
832 InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize);
834 if (CDROM_DeviceIoControl(DeviceHandle, hEvent,
835 UserApcRoutine, UserApcContext,
836 IoStatusBlock, IoControlCode,
837 InputBuffer, InputBufferSize,
838 OutputBuffer, OutputBufferSize) == STATUS_NO_SUCH_DEVICE)
840 /* it wasn't a CDROM */
841 FIXME("Unimplemented dwIoControlCode=%08lx\n", IoControlCode);
842 IoStatusBlock->u.Status = STATUS_NOT_IMPLEMENTED;
843 IoStatusBlock->Information = 0;
844 if (hEvent) NtSetEvent(hEvent, NULL);
846 return IoStatusBlock->u.Status;
849 /******************************************************************************
850 * NtFsControlFile [NTDLL.@]
851 * ZwFsControlFile [NTDLL.@]
853 NTSTATUS WINAPI NtFsControlFile(
854 IN HANDLE DeviceHandle,
855 IN HANDLE Event OPTIONAL,
856 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
857 IN PVOID ApcContext OPTIONAL,
858 OUT PIO_STATUS_BLOCK IoStatusBlock,
859 IN ULONG IoControlCode,
860 IN PVOID InputBuffer,
861 IN ULONG InputBufferSize,
862 OUT PVOID OutputBuffer,
863 IN ULONG OutputBufferSize)
865 FIXME("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx): stub\n",
866 DeviceHandle,Event,ApcRoutine,ApcContext,IoStatusBlock,IoControlCode,
867 InputBuffer,InputBufferSize,OutputBuffer,OutputBufferSize);
871 /******************************************************************************
872 * NtSetVolumeInformationFile [NTDLL.@]
873 * ZwSetVolumeInformationFile [NTDLL.@]
875 * Set volume information for an open file handle.
878 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
879 * IoStatusBlock [O] Receives information about the operation on return
880 * FsInformation [I] Source for volume information
881 * Length [I] Size of FsInformation
882 * FsInformationClass [I] Type of volume information to set
885 * Success: 0. IoStatusBlock is updated.
886 * Failure: An NTSTATUS error code describing the error.
888 NTSTATUS WINAPI NtSetVolumeInformationFile(
889 IN HANDLE FileHandle,
890 PIO_STATUS_BLOCK IoStatusBlock,
893 FS_INFORMATION_CLASS FsInformationClass)
895 FIXME("(%p,%p,%p,0x%08lx,0x%08x) stub\n",
896 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
900 /******************************************************************************
901 * NtQueryInformationFile [NTDLL.@]
902 * ZwQueryInformationFile [NTDLL.@]
904 * Get information about an open file handle.
907 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
908 * io [O] Receives information about the operation on return
909 * ptr [O] Destination for file information
910 * len [I] Size of FileInformation
911 * class [I] Type of file information to get
914 * Success: 0. IoStatusBlock and FileInformation are updated.
915 * Failure: An NTSTATUS error code describing the error.
917 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
918 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
920 static const size_t info_sizes[] =
923 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
924 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
925 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
926 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
927 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
928 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
929 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
930 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
931 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
932 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
933 0, /* FileLinkInformation */
934 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
935 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
936 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
937 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
938 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
939 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
940 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
941 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
942 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
943 0, /* FileAlternateNameInformation */
944 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
945 0, /* FilePipeInformation */
946 0, /* FilePipeLocalInformation */
947 0, /* FilePipeRemoteInformation */
948 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
949 0, /* FileMailslotSetInformation */
950 0, /* FileCompressionInformation */
951 0, /* FileObjectIdInformation */
952 0, /* FileCompletionInformation */
953 0, /* FileMoveClusterInformation */
954 0, /* FileQuotaInformation */
955 0, /* FileReparsePointInformation */
956 0, /* FileNetworkOpenInformation */
957 0, /* FileAttributeTagInformation */
958 0 /* FileTrackingInformation */
964 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", hFile, io, ptr, len, class);
968 if (class <= 0 || class >= FileMaximumInformation)
969 return io->u.Status = STATUS_INVALID_INFO_CLASS;
970 if (!info_sizes[class])
972 FIXME("Unsupported class (%d)\n", class);
973 return io->u.Status = STATUS_NOT_IMPLEMENTED;
975 if (len < info_sizes[class])
976 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
978 if ((io->u.Status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
983 case FileBasicInformation:
985 FILE_BASIC_INFORMATION *info = ptr;
987 if (fstat( fd, &st ) == -1)
988 io->u.Status = FILE_GetNtStatus();
989 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
990 io->u.Status = STATUS_INVALID_INFO_CLASS;
993 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
994 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
995 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
996 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
997 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
998 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
999 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1000 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1004 case FileStandardInformation:
1006 FILE_STANDARD_INFORMATION *info = ptr;
1008 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1011 if ((info->Directory = S_ISDIR(st.st_mode)))
1013 info->AllocationSize.QuadPart = 0;
1014 info->EndOfFile.QuadPart = 0;
1015 info->NumberOfLinks = 1;
1016 info->DeletePending = FALSE;
1020 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1021 info->EndOfFile.QuadPart = st.st_size;
1022 info->NumberOfLinks = st.st_nlink;
1023 info->DeletePending = FALSE; /* FIXME */
1028 case FilePositionInformation:
1030 FILE_POSITION_INFORMATION *info = ptr;
1031 off_t res = lseek( fd, 0, SEEK_CUR );
1032 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1033 else info->CurrentByteOffset.QuadPart = res;
1036 case FileInternalInformation:
1038 FILE_INTERNAL_INFORMATION *info = ptr;
1040 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1041 else info->IndexNumber.QuadPart = st.st_ino;
1044 case FileEaInformation:
1046 FILE_EA_INFORMATION *info = ptr;
1050 case FileEndOfFileInformation:
1052 FILE_END_OF_FILE_INFORMATION *info = ptr;
1054 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1055 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1058 case FileAllInformation:
1060 FILE_ALL_INFORMATION *info = ptr;
1062 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1063 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1064 io->u.Status = STATUS_INVALID_INFO_CLASS;
1067 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1069 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1070 info->StandardInformation.AllocationSize.QuadPart = 0;
1071 info->StandardInformation.EndOfFile.QuadPart = 0;
1072 info->StandardInformation.NumberOfLinks = 1;
1073 info->StandardInformation.DeletePending = FALSE;
1077 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1078 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1079 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1080 info->StandardInformation.NumberOfLinks = st.st_nlink;
1081 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1083 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1084 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1085 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1086 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1087 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1088 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1089 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1090 info->EaInformation.EaSize = 0;
1091 info->AccessInformation.AccessFlags = 0; /* FIXME */
1092 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1093 info->ModeInformation.Mode = 0; /* FIXME */
1094 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1095 info->NameInformation.FileNameLength = 0;
1096 io->Information = sizeof(*info) - sizeof(WCHAR);
1100 case FileMailslotQueryInformation:
1102 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1104 SERVER_START_REQ( set_mailslot_info )
1106 req->handle = hFile;
1108 io->u.Status = wine_server_call( req );
1109 if( io->u.Status == STATUS_SUCCESS )
1111 info->MaximumMessageSize = reply->max_msgsize;
1112 info->MailslotQuota = 0;
1113 info->NextMessageSize = reply->next_msgsize;
1114 info->MessagesAvailable = reply->msg_count;
1115 info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1122 FIXME("Unsupported class (%d)\n", class);
1123 io->u.Status = STATUS_NOT_IMPLEMENTED;
1126 wine_server_release_fd( hFile, fd );
1127 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1128 return io->u.Status;
1131 /******************************************************************************
1132 * NtSetInformationFile [NTDLL.@]
1133 * ZwSetInformationFile [NTDLL.@]
1135 * Set information about an open file handle.
1138 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1139 * io [O] Receives information about the operation on return
1140 * ptr [I] Source for file information
1141 * len [I] Size of FileInformation
1142 * class [I] Type of file information to set
1145 * Success: 0. io is updated.
1146 * Failure: An NTSTATUS error code describing the error.
1148 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1149 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1153 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", handle, io, ptr, len, class);
1155 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )))
1156 return io->u.Status;
1158 io->u.Status = STATUS_SUCCESS;
1161 case FileBasicInformation:
1162 if (len >= sizeof(FILE_BASIC_INFORMATION))
1165 const FILE_BASIC_INFORMATION *info = ptr;
1167 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1169 ULONGLONG sec, nsec;
1170 struct timeval tv[2];
1172 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1175 tv[0].tv_sec = tv[0].tv_usec = 0;
1176 tv[1].tv_sec = tv[1].tv_usec = 0;
1177 if (!fstat( fd, &st ))
1179 tv[0].tv_sec = st.st_atime;
1180 tv[1].tv_sec = st.st_mtime;
1183 if (info->LastAccessTime.QuadPart)
1185 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1186 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1187 tv[0].tv_usec = (UINT)nsec / 10;
1189 if (info->LastWriteTime.QuadPart)
1191 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1192 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1193 tv[1].tv_usec = (UINT)nsec / 10;
1195 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1198 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1200 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1203 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1205 if (S_ISDIR( st.st_mode))
1206 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1208 st.st_mode &= ~0222; /* clear write permission bits */
1212 /* add write permission only where we already have read permission */
1213 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1215 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1219 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1222 case FilePositionInformation:
1223 if (len >= sizeof(FILE_POSITION_INFORMATION))
1225 const FILE_POSITION_INFORMATION *info = ptr;
1227 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1228 io->u.Status = FILE_GetNtStatus();
1230 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1233 case FileEndOfFileInformation:
1234 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1237 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1239 /* first try normal truncate */
1240 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1242 /* now check for the need to extend the file */
1243 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1245 static const char zero;
1247 /* extend the file one byte beyond the requested size and then truncate it */
1248 /* this should work around ftruncate implementations that can't extend files */
1249 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1250 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1252 io->u.Status = FILE_GetNtStatus();
1254 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1257 case FileMailslotSetInformation:
1259 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1261 SERVER_START_REQ( set_mailslot_info )
1263 req->handle = handle;
1264 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1265 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1266 io->u.Status = wine_server_call( req );
1273 FIXME("Unsupported class (%d)\n", class);
1274 io->u.Status = STATUS_NOT_IMPLEMENTED;
1277 wine_server_release_fd( handle, fd );
1278 io->Information = 0;
1279 return io->u.Status;
1283 /******************************************************************************
1284 * NtQueryFullAttributesFile (NTDLL.@)
1286 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1287 FILE_NETWORK_OPEN_INFORMATION *info )
1289 ANSI_STRING unix_name;
1292 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1293 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1297 if (stat( unix_name.Buffer, &st ) == -1)
1298 status = FILE_GetNtStatus();
1299 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1300 status = STATUS_INVALID_INFO_CLASS;
1303 if (S_ISDIR(st.st_mode))
1305 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1306 info->AllocationSize.QuadPart = 0;
1307 info->EndOfFile.QuadPart = 0;
1311 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1312 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1313 info->EndOfFile.QuadPart = st.st_size;
1315 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1316 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1317 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1318 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1319 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1320 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1321 if (DIR_is_hidden_file( attr->ObjectName ))
1322 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1324 RtlFreeAnsiString( &unix_name );
1326 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), status );
1331 /******************************************************************************
1332 * NtQueryAttributesFile (NTDLL.@)
1333 * ZwQueryAttributesFile (NTDLL.@)
1335 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1337 FILE_NETWORK_OPEN_INFORMATION full_info;
1340 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1342 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1343 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1344 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1345 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1346 info->FileAttributes = full_info.FileAttributes;
1352 /******************************************************************************
1353 * NtQueryVolumeInformationFile [NTDLL.@]
1354 * ZwQueryVolumeInformationFile [NTDLL.@]
1356 * Get volume information for an open file handle.
1359 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1360 * io [O] Receives information about the operation on return
1361 * buffer [O] Destination for volume information
1362 * length [I] Size of FsInformation
1363 * info_class [I] Type of volume information to set
1366 * Success: 0. io and buffer are updated.
1367 * Failure: An NTSTATUS error code describing the error.
1369 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1370 PVOID buffer, ULONG length,
1371 FS_INFORMATION_CLASS info_class )
1376 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )) != STATUS_SUCCESS)
1377 return io->u.Status;
1379 io->u.Status = STATUS_NOT_IMPLEMENTED;
1380 io->Information = 0;
1382 switch( info_class )
1384 case FileFsVolumeInformation:
1385 FIXME( "%p: volume info not supported\n", handle );
1387 case FileFsLabelInformation:
1388 FIXME( "%p: label info not supported\n", handle );
1390 case FileFsSizeInformation:
1391 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1392 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1395 FILE_FS_SIZE_INFORMATION *info = buffer;
1397 if (fstat( fd, &st ) < 0)
1399 io->u.Status = FILE_GetNtStatus();
1402 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1404 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1408 /* Linux's fstatvfs is buggy */
1409 #if !defined(linux) || !defined(HAVE_FSTATFS)
1410 struct statvfs stfs;
1412 if (fstatvfs( fd, &stfs ) < 0)
1414 io->u.Status = FILE_GetNtStatus();
1417 info->BytesPerSector = stfs.f_frsize;
1420 if (fstatfs( fd, &stfs ) < 0)
1422 io->u.Status = FILE_GetNtStatus();
1425 info->BytesPerSector = stfs.f_bsize;
1427 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1428 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1429 info->SectorsPerAllocationUnit = 1;
1430 io->Information = sizeof(*info);
1431 io->u.Status = STATUS_SUCCESS;
1435 case FileFsDeviceInformation:
1436 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1437 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1440 FILE_FS_DEVICE_INFORMATION *info = buffer;
1442 info->Characteristics = 0;
1443 if (fstat( fd, &st ) < 0)
1445 io->u.Status = FILE_GetNtStatus();
1448 if (S_ISCHR( st.st_mode ))
1450 info->DeviceType = FILE_DEVICE_UNKNOWN;
1452 switch(major(st.st_rdev))
1455 info->DeviceType = FILE_DEVICE_NULL;
1458 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1461 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1466 else if (S_ISBLK( st.st_mode ))
1468 info->DeviceType = FILE_DEVICE_DISK;
1470 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1472 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1474 else /* regular file or directory */
1476 #if defined(linux) && defined(HAVE_FSTATFS)
1479 /* check for floppy disk */
1480 if (major(st.st_dev) == FLOPPY_MAJOR)
1481 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1483 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1484 switch (stfs.f_type)
1486 case 0x9660: /* iso9660 */
1487 case 0x15013346: /* udf */
1488 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1489 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1491 case 0x6969: /* nfs */
1492 case 0x517B: /* smbfs */
1493 case 0x564c: /* ncpfs */
1494 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1495 info->Characteristics |= FILE_REMOTE_DEVICE;
1497 case 0x01021994: /* tmpfs */
1498 case 0x28cd3d45: /* cramfs */
1499 case 0x1373: /* devfs */
1500 case 0x9fa0: /* procfs */
1501 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1504 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1507 #elif defined (__APPLE__)
1508 # include <IOKit/IOKitLib.h>
1509 # include <CoreFoundation/CFNumber.h> /* for kCFBooleanTrue, kCFBooleanFalse */
1513 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1515 if (fstatfs( fd, &stfs ) < 0) break;
1517 /* stfs.f_type is reserved (always set to 0) so use IOKit */
1518 kern_return_t kernResult = KERN_FAILURE;
1519 mach_port_t masterPort;
1521 char bsdName[6]; /* disk#\0 */
1522 const char *name = stfs.f_mntfromname + strlen(_PATH_DEV);
1523 memcpy( bsdName, name, min(strlen(name)+1,sizeof(bsdName)) );
1524 bsdName[sizeof(bsdName)-1] = 0;
1526 kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
1528 if (kernResult == KERN_SUCCESS)
1530 CFMutableDictionaryRef matching = IOBSDNameMatching(masterPort, 0, bsdName);
1534 CFMutableDictionaryRef properties;
1535 io_service_t devService = IOServiceGetMatchingService(masterPort, matching);
1537 if (IORegistryEntryCreateCFProperties(devService,
1539 kCFAllocatorDefault, 0) != KERN_SUCCESS)
1542 CFDictionaryGetValue(properties, CFSTR("Removable")),
1544 ) info->Characteristics |= FILE_REMOVABLE_MEDIA;
1547 CFDictionaryGetValue(properties, CFSTR("Writable")),
1549 ) info->Characteristics |= FILE_READ_ONLY_DEVICE;
1552 NB : mounted disk image (.img/.dmg) don't provide specific type
1555 if ( (type = CFDictionaryGetValue(properties, CFSTR("Type"))) )
1557 if ( CFStringCompare(type, CFSTR("CD-ROM"), 0) == kCFCompareEqualTo
1558 || CFStringCompare(type, CFSTR("DVD-ROM"), 0) == kCFCompareEqualTo
1561 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1566 CFRelease(properties);
1570 /* Use dkio to work out device types */
1572 # include <sys/dkio.h>
1573 # include <sys/vtoc.h>
1574 struct dk_cinfo dkinf;
1575 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1577 WARN("Unable to get disk device type information - assuming a disk like device\n");
1578 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1580 switch (dkinf.dki_ctype)
1583 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1584 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1588 case DKC_INTEL82072:
1589 case DKC_INTEL82077:
1590 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1591 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1594 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1597 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1602 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1603 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1605 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1607 io->Information = sizeof(*info);
1608 io->u.Status = STATUS_SUCCESS;
1611 case FileFsAttributeInformation:
1612 FIXME( "%p: attribute info not supported\n", handle );
1614 case FileFsControlInformation:
1615 FIXME( "%p: control info not supported\n", handle );
1617 case FileFsFullSizeInformation:
1618 FIXME( "%p: full size info not supported\n", handle );
1620 case FileFsObjectIdInformation:
1621 FIXME( "%p: object id info not supported\n", handle );
1623 case FileFsMaximumInformation:
1624 FIXME( "%p: maximum info not supported\n", handle );
1627 io->u.Status = STATUS_INVALID_PARAMETER;
1630 wine_server_release_fd( handle, fd );
1631 return io->u.Status;
1635 /******************************************************************
1636 * NtFlushBuffersFile (NTDLL.@)
1638 * Flush any buffered data on an open file handle.
1641 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1642 * IoStatusBlock [O] Receives information about the operation on return
1645 * Success: 0. IoStatusBlock is updated.
1646 * Failure: An NTSTATUS error code describing the error.
1648 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1651 HANDLE hEvent = NULL;
1653 SERVER_START_REQ( flush_file )
1655 req->handle = hFile;
1656 ret = wine_server_call( req );
1657 hEvent = reply->event;
1662 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1668 /******************************************************************
1669 * NtLockFile (NTDLL.@)
1673 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1674 PIO_APC_ROUTINE apc, void* apc_user,
1675 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1676 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1683 if (apc || io_status || key)
1685 FIXME("Unimplemented yet parameter\n");
1686 return STATUS_NOT_IMPLEMENTED;
1691 SERVER_START_REQ( lock_file )
1693 req->handle = hFile;
1694 req->offset_low = offset->u.LowPart;
1695 req->offset_high = offset->u.HighPart;
1696 req->count_low = count->u.LowPart;
1697 req->count_high = count->u.HighPart;
1698 req->shared = !exclusive;
1699 req->wait = !dont_wait;
1700 ret = wine_server_call( req );
1701 handle = reply->handle;
1702 async = reply->overlapped;
1705 if (ret != STATUS_PENDING)
1707 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1713 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1714 if (handle) NtClose( handle );
1715 return STATUS_PENDING;
1719 NtWaitForSingleObject( handle, FALSE, NULL );
1726 /* Unix lock conflict, sleep a bit and retry */
1727 time.QuadPart = 100 * (ULONGLONG)10000;
1728 time.QuadPart = -time.QuadPart;
1729 NtDelayExecution( FALSE, &time );
1735 /******************************************************************
1736 * NtUnlockFile (NTDLL.@)
1740 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
1741 PLARGE_INTEGER offset, PLARGE_INTEGER count,
1746 TRACE( "%p %lx%08lx %lx%08lx\n",
1747 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
1749 if (io_status || key)
1751 FIXME("Unimplemented yet parameter\n");
1752 return STATUS_NOT_IMPLEMENTED;
1755 SERVER_START_REQ( unlock_file )
1757 req->handle = hFile;
1758 req->offset_low = offset->u.LowPart;
1759 req->offset_high = offset->u.HighPart;
1760 req->count_low = count->u.LowPart;
1761 req->count_high = count->u.HighPart;
1762 status = wine_server_call( req );
1768 /******************************************************************
1769 * NtCreateNamedPipeFile (NTDLL.@)
1773 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
1774 POBJECT_ATTRIBUTES oa, PIO_STATUS_BLOCK iosb,
1775 ULONG sharing, ULONG dispo, ULONG options,
1776 ULONG pipe_type, ULONG read_mode,
1777 ULONG completion_mode, ULONG max_inst,
1778 ULONG inbound_quota, ULONG outbound_quota,
1779 PLARGE_INTEGER timeout)
1782 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1784 TRACE("(%p %lx %p %p %lx %ld %lx %ld %ld %ld %ld %ld %ld %p): stub\n",
1785 handle, access, oa, iosb, sharing, dispo, options, pipe_type,
1786 read_mode, completion_mode, max_inst, inbound_quota, outbound_quota,
1789 if (oa->ObjectName->Length < sizeof(leadin) ||
1790 strncmpiW( oa->ObjectName->Buffer,
1791 leadin, sizeof(leadin)/sizeof(leadin[0]) ))
1792 return STATUS_OBJECT_NAME_INVALID;
1793 /* assume we only get relative timeout, and storable in a DWORD as ms */
1794 if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
1795 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
1797 SERVER_START_REQ( create_named_pipe )
1799 req->options = options; /* FIXME not used in server yet !!!! */
1801 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
1802 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
1803 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
1804 req->maxinstances = max_inst;
1805 req->outsize = outbound_quota;
1806 req->insize = inbound_quota;
1807 req->timeout = timeout->QuadPart / -10000;
1808 req->inherit = (oa->Attributes & OBJ_INHERIT) != 0;
1809 wine_server_add_data( req, oa->ObjectName->Buffer + 4,
1810 oa->ObjectName->Length - 4 * sizeof(WCHAR) );
1811 status = wine_server_call( req );
1812 if (!status) *handle = reply->handle;
1818 /******************************************************************
1819 * NtDeleteFile (NTDLL.@)
1823 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
1829 TRACE("%p\n", ObjectAttributes);
1830 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE, ObjectAttributes,
1832 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1833 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
1834 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1838 /******************************************************************
1839 * NtCancelIoFile (NTDLL.@)
1843 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
1845 LARGE_INTEGER timeout;
1847 TRACE("%p %p\n", hFile, io_status );
1849 SERVER_START_REQ( cancel_async )
1851 req->handle = hFile;
1852 wine_server_call( req );
1855 /* Let some APC be run, so that we can run the remaining APCs on hFile
1856 * either the cancelation of the pending one, but also the execution
1857 * of the queued APC, but not yet run. This is needed to ensure proper
1858 * clean-up of allocated data.
1860 timeout.u.LowPart = timeout.u.HighPart = 0;
1861 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
1864 /******************************************************************************
1865 * NtCreateMailslotFile [NTDLL.@]
1866 * ZwCreateMailslotFile [NTDLL.@]
1869 * pHandle [O] pointer to receive the handle created
1870 * DesiredAccess [I] access mode (read, write, etc)
1871 * ObjectAttributes [I] fully qualified NT path of the mailslot
1872 * IoStatusBlock [O] receives completion status and other info
1875 * MaxMessageSize [I]
1881 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
1882 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
1883 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
1884 PLARGE_INTEGER TimeOut)
1886 static const WCHAR leadin[] = {
1887 '\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
1890 TRACE("%p %08lx %p %p %08lx %08lx %08lx %p\n",
1891 pHandle, DesiredAccess, attr, IoStatusBlock,
1892 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
1894 if (attr->ObjectName->Length < sizeof(leadin) ||
1895 strncmpiW( attr->ObjectName->Buffer,
1896 leadin, sizeof(leadin)/sizeof(leadin[0]) ))
1898 return STATUS_OBJECT_NAME_INVALID;
1901 SERVER_START_REQ( create_mailslot )
1903 req->max_msgsize = MaxMessageSize;
1904 req->read_timeout = TimeOut->QuadPart / -10000;
1905 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1906 wine_server_add_data( req, attr->ObjectName->Buffer + 4,
1907 attr->ObjectName->Length - 4*sizeof(WCHAR) );
1908 ret = wine_server_call( req );
1909 if( ret == STATUS_SUCCESS )
1910 *pHandle = reply->handle;