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 io_status->Information = 0;
462 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_READ, &unix_handle, &flags );
463 if (io_status->u.Status) return io_status->u.Status;
465 if (flags & FD_FLAG_RECV_SHUTDOWN)
467 wine_server_release_fd( hFile, unix_handle );
468 return STATUS_PIPE_DISCONNECTED;
471 if (flags & FD_FLAG_TIMEOUT)
475 /* this shouldn't happen, but check it */
476 FIXME("NIY-hEvent\n");
477 wine_server_release_fd( hFile, unix_handle );
478 return STATUS_NOT_IMPLEMENTED;
480 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
481 if (io_status->u.Status)
483 wine_server_release_fd( hFile, unix_handle );
484 return io_status->u.Status;
488 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
490 async_fileio* fileio;
493 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
495 wine_server_release_fd( hFile, unix_handle );
496 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
497 return STATUS_NO_MEMORY;
499 fileio->handle = hFile;
500 fileio->count = length;
501 if ( offset == NULL )
505 fileio->offset = offset->QuadPart;
506 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
507 FIXME("High part of offset is lost\n");
510 fileio->apc_user = apc_user;
511 fileio->buffer = buffer;
512 fileio->queue_apc_on_error = 0;
513 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
514 fileio->fd = unix_handle; /* FIXME */
515 fileio->event = hEvent;
516 NtResetEvent(hEvent, NULL);
518 io_status->u.Status = STATUS_PENDING;
519 ret = fileio_queue_async(fileio, io_status, TRUE);
520 if (ret != STATUS_SUCCESS)
522 wine_server_release_fd( hFile, unix_handle );
523 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
524 RtlFreeHeap(GetProcessHeap(), 0, fileio);
527 if (flags & FD_FLAG_TIMEOUT)
529 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
531 if (ret != STATUS_USER_APC)
532 fileio->queue_apc_on_error = 1;
536 LARGE_INTEGER timeout;
538 /* let some APC be run, this will read some already pending data */
539 timeout.u.LowPart = timeout.u.HighPart = 0;
540 ret = NtDelayExecution( TRUE, &timeout );
541 /* the apc didn't run and therefore the completion routine now
542 * needs to be sent errors.
543 * Note that there is no race between setting this flag and
544 * returning errors because apc's are run only during alertable
546 if (ret != STATUS_USER_APC)
547 fileio->queue_apc_on_error = 1;
549 TRACE("= 0x%08lx\n", io_status->u.Status);
550 return io_status->u.Status;
555 FILE_POSITION_INFORMATION fpi;
557 fpi.CurrentByteOffset = *offset;
558 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
559 FilePositionInformation);
560 if (io_status->u.Status)
562 wine_server_release_fd( hFile, unix_handle );
563 return io_status->u.Status;
566 /* code for synchronous reads */
567 while ((io_status->Information = read( unix_handle, buffer, length )) == -1)
569 if ((errno == EAGAIN) || (errno == EINTR)) continue;
572 io_status->Information = 0;
573 io_status->u.Status = STATUS_ACCESS_VIOLATION;
575 else io_status->u.Status = FILE_GetNtStatus();
578 wine_server_release_fd( hFile, unix_handle );
579 TRACE("= 0x%08lx\n", io_status->u.Status);
580 return io_status->u.Status;
583 /***********************************************************************
584 * FILE_AsyncWriteService (INTERNAL)
586 * This function is called while the client is waiting on the
587 * server, so we can't make any server calls here.
589 static void WINAPI FILE_AsyncWriteService(void *ovp, IO_STATUS_BLOCK *iosb, ULONG status)
591 async_fileio *fileio = (async_fileio *) ovp;
593 int already = iosb->Information;
595 TRACE("(%p %p %lu)\n",iosb, fileio->buffer, status);
600 /* write some data (non-blocking) */
601 if ( fileio->avail_mode )
602 result = write(fileio->fd, &fileio->buffer[already],
603 fileio->count - already);
606 result = pwrite(fileio->fd, &fileio->buffer[already],
607 fileio->count - already, fileio->offset + already);
608 if ((result < 0) && (errno == ESPIPE))
609 result = write(fileio->fd, &fileio->buffer[already],
610 fileio->count - already);
615 if (errno == EAGAIN || errno == EINTR) iosb->u.Status = STATUS_PENDING;
616 else iosb->u.Status = FILE_GetNtStatus();
620 iosb->Information += result;
621 iosb->u.Status = (iosb->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
622 TRACE("wrote %d more bytes %ld/%d so far\n",
623 result, iosb->Information, fileio->count);
625 if (iosb->u.Status == STATUS_PENDING)
626 fileio_queue_async(fileio, iosb, FALSE);
629 iosb->u.Status = status;
630 fileio_terminate(fileio, iosb);
635 /******************************************************************************
636 * NtWriteFile [NTDLL.@]
637 * ZwWriteFile [NTDLL.@]
639 * Write to an open file handle.
642 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
643 * Event [I] Event to signal upon completion (or NULL)
644 * ApcRoutine [I] Callback to call upon completion (or NULL)
645 * ApcContext [I] Context for ApcRoutine (or NULL)
646 * IoStatusBlock [O] Receives information about the operation on return
647 * Buffer [I] Source for the data to write
648 * Length [I] Size of Buffer
649 * ByteOffset [O] Destination for the new file pointer position (or NULL)
650 * Key [O] Function unknown (may be NULL)
653 * Success: 0. IoStatusBlock is updated, and the Information member contains
654 * The number of bytes written.
655 * Failure: An NTSTATUS error code describing the error.
657 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
658 PIO_APC_ROUTINE apc, void* apc_user,
659 PIO_STATUS_BLOCK io_status,
660 const void* buffer, ULONG length,
661 PLARGE_INTEGER offset, PULONG key)
663 int unix_handle, flags;
665 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p)!\n",
666 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
668 io_status->Information = 0;
669 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_WRITE, &unix_handle, &flags );
670 if (io_status->u.Status) return io_status->u.Status;
672 if (flags & FD_FLAG_SEND_SHUTDOWN)
674 wine_server_release_fd( hFile, unix_handle );
675 return STATUS_PIPE_DISCONNECTED;
678 if (flags & FD_FLAG_TIMEOUT)
682 /* this shouldn't happen, but check it */
683 FIXME("NIY-hEvent\n");
684 wine_server_release_fd( hFile, unix_handle );
685 return STATUS_NOT_IMPLEMENTED;
687 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
688 if (io_status->u.Status)
690 wine_server_release_fd( hFile, unix_handle );
691 return io_status->u.Status;
695 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
697 async_fileio* fileio;
700 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
702 wine_server_release_fd( hFile, unix_handle );
703 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
704 return STATUS_NO_MEMORY;
706 fileio->handle = hFile;
707 fileio->count = length;
710 fileio->offset = offset->QuadPart;
711 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
712 FIXME("High part of offset is lost\n");
719 fileio->apc_user = apc_user;
720 fileio->buffer = (void*)buffer;
721 fileio->queue_apc_on_error = 0;
722 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
723 fileio->fd = unix_handle; /* FIXME */
724 fileio->event = hEvent;
725 NtResetEvent(hEvent, NULL);
727 io_status->Information = 0;
728 io_status->u.Status = STATUS_PENDING;
729 ret = fileio_queue_async(fileio, io_status, FALSE);
730 if (ret != STATUS_SUCCESS)
732 wine_server_release_fd( hFile, unix_handle );
733 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
734 RtlFreeHeap(GetProcessHeap(), 0, fileio);
737 if (flags & FD_FLAG_TIMEOUT)
739 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
741 if (ret != STATUS_USER_APC)
742 fileio->queue_apc_on_error = 1;
746 LARGE_INTEGER timeout;
748 /* let some APC be run, this will write as much data as possible */
749 timeout.u.LowPart = timeout.u.HighPart = 0;
750 ret = NtDelayExecution( TRUE, &timeout );
751 /* the apc didn't run and therefore the completion routine now
752 * needs to be sent errors.
753 * Note that there is no race between setting this flag and
754 * returning errors because apc's are run only during alertable
756 if (ret != STATUS_USER_APC)
757 fileio->queue_apc_on_error = 1;
759 return io_status->u.Status;
764 FILE_POSITION_INFORMATION fpi;
766 fpi.CurrentByteOffset = *offset;
767 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
768 FilePositionInformation);
769 if (io_status->u.Status)
771 wine_server_release_fd( hFile, unix_handle );
772 return io_status->u.Status;
776 /* synchronous file write */
777 while ((io_status->Information = write( unix_handle, buffer, length )) == -1)
779 if ((errno == EAGAIN) || (errno == EINTR)) continue;
782 io_status->Information = 0;
783 io_status->u.Status = STATUS_INVALID_USER_BUFFER;
785 else if (errno == ENOSPC) io_status->u.Status = STATUS_DISK_FULL;
786 else io_status->u.Status = FILE_GetNtStatus();
789 wine_server_release_fd( hFile, unix_handle );
790 return io_status->u.Status;
793 /**************************************************************************
794 * NtDeviceIoControlFile [NTDLL.@]
795 * ZwDeviceIoControlFile [NTDLL.@]
797 * Perform an I/O control operation on an open file handle.
800 * DeviceHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
801 * Event [I] Event to signal upon completion (or NULL)
802 * ApcRoutine [I] Callback to call upon completion (or NULL)
803 * ApcContext [I] Context for ApcRoutine (or NULL)
804 * IoStatusBlock [O] Receives information about the operation on return
805 * IoControlCode [I] Control code for the operation to perform
806 * InputBuffer [I] Source for any input data required (or NULL)
807 * InputBufferSize [I] Size of InputBuffer
808 * OutputBuffer [O] Source for any output data returned (or NULL)
809 * OutputBufferSize [I] Size of OutputBuffer
812 * Success: 0. IoStatusBlock is updated.
813 * Failure: An NTSTATUS error code describing the error.
815 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE DeviceHandle, HANDLE hEvent,
816 PIO_APC_ROUTINE UserApcRoutine,
817 PVOID UserApcContext,
818 PIO_STATUS_BLOCK IoStatusBlock,
821 ULONG InputBufferSize,
823 ULONG OutputBufferSize)
825 TRACE("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx)\n",
826 DeviceHandle, hEvent, UserApcRoutine, UserApcContext,
827 IoStatusBlock, IoControlCode,
828 InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize);
830 if (CDROM_DeviceIoControl(DeviceHandle, hEvent,
831 UserApcRoutine, UserApcContext,
832 IoStatusBlock, IoControlCode,
833 InputBuffer, InputBufferSize,
834 OutputBuffer, OutputBufferSize) == STATUS_NO_SUCH_DEVICE)
836 /* it wasn't a CDROM */
837 FIXME("Unimplemented dwIoControlCode=%08lx\n", IoControlCode);
838 IoStatusBlock->u.Status = STATUS_NOT_IMPLEMENTED;
839 IoStatusBlock->Information = 0;
840 if (hEvent) NtSetEvent(hEvent, NULL);
842 return IoStatusBlock->u.Status;
845 /******************************************************************************
846 * NtFsControlFile [NTDLL.@]
847 * ZwFsControlFile [NTDLL.@]
849 NTSTATUS WINAPI NtFsControlFile(
850 IN HANDLE DeviceHandle,
851 IN HANDLE Event OPTIONAL,
852 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
853 IN PVOID ApcContext OPTIONAL,
854 OUT PIO_STATUS_BLOCK IoStatusBlock,
855 IN ULONG IoControlCode,
856 IN PVOID InputBuffer,
857 IN ULONG InputBufferSize,
858 OUT PVOID OutputBuffer,
859 IN ULONG OutputBufferSize)
861 FIXME("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx): stub\n",
862 DeviceHandle,Event,ApcRoutine,ApcContext,IoStatusBlock,IoControlCode,
863 InputBuffer,InputBufferSize,OutputBuffer,OutputBufferSize);
867 /******************************************************************************
868 * NtSetVolumeInformationFile [NTDLL.@]
869 * ZwSetVolumeInformationFile [NTDLL.@]
871 * Set volume information for an open file handle.
874 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
875 * IoStatusBlock [O] Receives information about the operation on return
876 * FsInformation [I] Source for volume information
877 * Length [I] Size of FsInformation
878 * FsInformationClass [I] Type of volume information to set
881 * Success: 0. IoStatusBlock is updated.
882 * Failure: An NTSTATUS error code describing the error.
884 NTSTATUS WINAPI NtSetVolumeInformationFile(
885 IN HANDLE FileHandle,
886 PIO_STATUS_BLOCK IoStatusBlock,
889 FS_INFORMATION_CLASS FsInformationClass)
891 FIXME("(%p,%p,%p,0x%08lx,0x%08x) stub\n",
892 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
896 /******************************************************************************
897 * NtQueryInformationFile [NTDLL.@]
898 * ZwQueryInformationFile [NTDLL.@]
900 * Get information about an open file handle.
903 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
904 * io [O] Receives information about the operation on return
905 * ptr [O] Destination for file information
906 * len [I] Size of FileInformation
907 * class [I] Type of file information to get
910 * Success: 0. IoStatusBlock and FileInformation are updated.
911 * Failure: An NTSTATUS error code describing the error.
913 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
914 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
916 static const size_t info_sizes[] =
919 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
920 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
921 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
922 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
923 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
924 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
925 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
926 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
927 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
928 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
929 0, /* FileLinkInformation */
930 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
931 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
932 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
933 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
934 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
935 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
936 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
937 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
938 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
939 0, /* FileAlternateNameInformation */
940 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
941 0, /* FilePipeInformation */
942 0, /* FilePipeLocalInformation */
943 0, /* FilePipeRemoteInformation */
944 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
945 0, /* FileMailslotSetInformation */
946 0, /* FileCompressionInformation */
947 0, /* FileObjectIdInformation */
948 0, /* FileCompletionInformation */
949 0, /* FileMoveClusterInformation */
950 0, /* FileQuotaInformation */
951 0, /* FileReparsePointInformation */
952 0, /* FileNetworkOpenInformation */
953 0, /* FileAttributeTagInformation */
954 0 /* FileTrackingInformation */
960 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", hFile, io, ptr, len, class);
964 if (class <= 0 || class >= FileMaximumInformation)
965 return io->u.Status = STATUS_INVALID_INFO_CLASS;
966 if (!info_sizes[class])
968 FIXME("Unsupported class (%d)\n", class);
969 return io->u.Status = STATUS_NOT_IMPLEMENTED;
971 if (len < info_sizes[class])
972 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
974 if ((io->u.Status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
979 case FileBasicInformation:
981 FILE_BASIC_INFORMATION *info = ptr;
983 if (fstat( fd, &st ) == -1)
984 io->u.Status = FILE_GetNtStatus();
985 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
986 io->u.Status = STATUS_INVALID_INFO_CLASS;
989 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
990 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
991 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
992 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
993 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
994 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
995 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
996 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1000 case FileStandardInformation:
1002 FILE_STANDARD_INFORMATION *info = ptr;
1004 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1007 if ((info->Directory = S_ISDIR(st.st_mode)))
1009 info->AllocationSize.QuadPart = 0;
1010 info->EndOfFile.QuadPart = 0;
1011 info->NumberOfLinks = 1;
1012 info->DeletePending = FALSE;
1016 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1017 info->EndOfFile.QuadPart = st.st_size;
1018 info->NumberOfLinks = st.st_nlink;
1019 info->DeletePending = FALSE; /* FIXME */
1024 case FilePositionInformation:
1026 FILE_POSITION_INFORMATION *info = ptr;
1027 off_t res = lseek( fd, 0, SEEK_CUR );
1028 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1029 else info->CurrentByteOffset.QuadPart = res;
1032 case FileInternalInformation:
1034 FILE_INTERNAL_INFORMATION *info = ptr;
1036 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1037 else info->IndexNumber.QuadPart = st.st_ino;
1040 case FileEaInformation:
1042 FILE_EA_INFORMATION *info = ptr;
1046 case FileEndOfFileInformation:
1048 FILE_END_OF_FILE_INFORMATION *info = ptr;
1050 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1051 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1054 case FileAllInformation:
1056 FILE_ALL_INFORMATION *info = ptr;
1058 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1059 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1060 io->u.Status = STATUS_INVALID_INFO_CLASS;
1063 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1065 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1066 info->StandardInformation.AllocationSize.QuadPart = 0;
1067 info->StandardInformation.EndOfFile.QuadPart = 0;
1068 info->StandardInformation.NumberOfLinks = 1;
1069 info->StandardInformation.DeletePending = FALSE;
1073 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1074 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1075 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1076 info->StandardInformation.NumberOfLinks = st.st_nlink;
1077 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1079 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1080 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1081 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1082 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1083 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1084 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1085 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1086 info->EaInformation.EaSize = 0;
1087 info->AccessInformation.AccessFlags = 0; /* FIXME */
1088 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1089 info->ModeInformation.Mode = 0; /* FIXME */
1090 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1091 info->NameInformation.FileNameLength = 0;
1092 io->Information = sizeof(*info) - sizeof(WCHAR);
1096 case FileMailslotQueryInformation:
1098 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1100 SERVER_START_REQ( set_mailslot_info )
1102 req->handle = hFile;
1104 io->u.Status = wine_server_call( req );
1105 if( io->u.Status == STATUS_SUCCESS )
1107 info->MaximumMessageSize = reply->max_msgsize;
1108 info->MailslotQuota = 0;
1109 info->NextMessageSize = reply->next_msgsize;
1110 info->MessagesAvailable = reply->msg_count;
1111 info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1118 FIXME("Unsupported class (%d)\n", class);
1119 io->u.Status = STATUS_NOT_IMPLEMENTED;
1122 wine_server_release_fd( hFile, fd );
1123 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1124 return io->u.Status;
1127 /******************************************************************************
1128 * NtSetInformationFile [NTDLL.@]
1129 * ZwSetInformationFile [NTDLL.@]
1131 * Set information about an open file handle.
1134 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1135 * io [O] Receives information about the operation on return
1136 * ptr [I] Source for file information
1137 * len [I] Size of FileInformation
1138 * class [I] Type of file information to set
1141 * Success: 0. io is updated.
1142 * Failure: An NTSTATUS error code describing the error.
1144 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1145 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1149 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", handle, io, ptr, len, class);
1151 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )))
1152 return io->u.Status;
1154 io->u.Status = STATUS_SUCCESS;
1157 case FileBasicInformation:
1158 if (len >= sizeof(FILE_BASIC_INFORMATION))
1161 const FILE_BASIC_INFORMATION *info = ptr;
1163 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1165 ULONGLONG sec, nsec;
1166 struct timeval tv[2];
1168 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1171 tv[0].tv_sec = tv[0].tv_usec = 0;
1172 tv[1].tv_sec = tv[1].tv_usec = 0;
1173 if (!fstat( fd, &st ))
1175 tv[0].tv_sec = st.st_atime;
1176 tv[1].tv_sec = st.st_mtime;
1179 if (info->LastAccessTime.QuadPart)
1181 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1182 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1183 tv[0].tv_usec = (UINT)nsec / 10;
1185 if (info->LastWriteTime.QuadPart)
1187 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1188 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1189 tv[1].tv_usec = (UINT)nsec / 10;
1191 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1194 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1196 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1199 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1201 if (S_ISDIR( st.st_mode))
1202 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1204 st.st_mode &= ~0222; /* clear write permission bits */
1208 /* add write permission only where we already have read permission */
1209 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1211 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1215 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1218 case FilePositionInformation:
1219 if (len >= sizeof(FILE_POSITION_INFORMATION))
1221 const FILE_POSITION_INFORMATION *info = ptr;
1223 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1224 io->u.Status = FILE_GetNtStatus();
1226 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1229 case FileEndOfFileInformation:
1230 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1233 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1235 /* first try normal truncate */
1236 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1238 /* now check for the need to extend the file */
1239 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1241 static const char zero;
1243 /* extend the file one byte beyond the requested size and then truncate it */
1244 /* this should work around ftruncate implementations that can't extend files */
1245 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1246 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1248 io->u.Status = FILE_GetNtStatus();
1250 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1253 case FileMailslotSetInformation:
1255 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1257 SERVER_START_REQ( set_mailslot_info )
1259 req->handle = handle;
1260 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1261 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1262 io->u.Status = wine_server_call( req );
1269 FIXME("Unsupported class (%d)\n", class);
1270 io->u.Status = STATUS_NOT_IMPLEMENTED;
1273 wine_server_release_fd( handle, fd );
1274 io->Information = 0;
1275 return io->u.Status;
1279 /******************************************************************************
1280 * NtQueryFullAttributesFile (NTDLL.@)
1282 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1283 FILE_NETWORK_OPEN_INFORMATION *info )
1285 ANSI_STRING unix_name;
1288 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1289 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1293 if (stat( unix_name.Buffer, &st ) == -1)
1294 status = FILE_GetNtStatus();
1295 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1296 status = STATUS_INVALID_INFO_CLASS;
1299 if (S_ISDIR(st.st_mode))
1301 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1302 info->AllocationSize.QuadPart = 0;
1303 info->EndOfFile.QuadPart = 0;
1307 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1308 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1309 info->EndOfFile.QuadPart = st.st_size;
1311 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1312 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1313 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1314 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1315 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1316 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1317 if (DIR_is_hidden_file( attr->ObjectName ))
1318 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1320 RtlFreeAnsiString( &unix_name );
1322 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), status );
1327 /******************************************************************************
1328 * NtQueryAttributesFile (NTDLL.@)
1329 * ZwQueryAttributesFile (NTDLL.@)
1331 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1333 FILE_NETWORK_OPEN_INFORMATION full_info;
1336 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1338 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1339 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1340 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1341 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1342 info->FileAttributes = full_info.FileAttributes;
1348 /******************************************************************************
1349 * NtQueryVolumeInformationFile [NTDLL.@]
1350 * ZwQueryVolumeInformationFile [NTDLL.@]
1352 * Get volume information for an open file handle.
1355 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1356 * io [O] Receives information about the operation on return
1357 * buffer [O] Destination for volume information
1358 * length [I] Size of FsInformation
1359 * info_class [I] Type of volume information to set
1362 * Success: 0. io and buffer are updated.
1363 * Failure: An NTSTATUS error code describing the error.
1365 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1366 PVOID buffer, ULONG length,
1367 FS_INFORMATION_CLASS info_class )
1372 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )) != STATUS_SUCCESS)
1373 return io->u.Status;
1375 io->u.Status = STATUS_NOT_IMPLEMENTED;
1376 io->Information = 0;
1378 switch( info_class )
1380 case FileFsVolumeInformation:
1381 FIXME( "%p: volume info not supported\n", handle );
1383 case FileFsLabelInformation:
1384 FIXME( "%p: label info not supported\n", handle );
1386 case FileFsSizeInformation:
1387 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1388 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1391 FILE_FS_SIZE_INFORMATION *info = buffer;
1393 if (fstat( fd, &st ) < 0)
1395 io->u.Status = FILE_GetNtStatus();
1398 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1400 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1404 /* Linux's fstatvfs is buggy */
1405 #if !defined(linux) || !defined(HAVE_FSTATFS)
1406 struct statvfs stfs;
1408 if (fstatvfs( fd, &stfs ) < 0)
1410 io->u.Status = FILE_GetNtStatus();
1413 info->BytesPerSector = stfs.f_frsize;
1416 if (fstatfs( fd, &stfs ) < 0)
1418 io->u.Status = FILE_GetNtStatus();
1421 info->BytesPerSector = stfs.f_bsize;
1423 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1424 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1425 info->SectorsPerAllocationUnit = 1;
1426 io->Information = sizeof(*info);
1427 io->u.Status = STATUS_SUCCESS;
1431 case FileFsDeviceInformation:
1432 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1433 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1436 FILE_FS_DEVICE_INFORMATION *info = buffer;
1438 info->Characteristics = 0;
1439 if (fstat( fd, &st ) < 0)
1441 io->u.Status = FILE_GetNtStatus();
1444 if (S_ISCHR( st.st_mode ))
1446 info->DeviceType = FILE_DEVICE_UNKNOWN;
1448 switch(major(st.st_rdev))
1451 info->DeviceType = FILE_DEVICE_NULL;
1454 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1457 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1462 else if (S_ISBLK( st.st_mode ))
1464 info->DeviceType = FILE_DEVICE_DISK;
1466 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1468 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1470 else /* regular file or directory */
1472 #if defined(linux) && defined(HAVE_FSTATFS)
1475 /* check for floppy disk */
1476 if (major(st.st_dev) == FLOPPY_MAJOR)
1477 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1479 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1480 switch (stfs.f_type)
1482 case 0x9660: /* iso9660 */
1483 case 0x15013346: /* udf */
1484 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1485 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1487 case 0x6969: /* nfs */
1488 case 0x517B: /* smbfs */
1489 case 0x564c: /* ncpfs */
1490 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1491 info->Characteristics |= FILE_REMOTE_DEVICE;
1493 case 0x01021994: /* tmpfs */
1494 case 0x28cd3d45: /* cramfs */
1495 case 0x1373: /* devfs */
1496 case 0x9fa0: /* procfs */
1497 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1500 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1503 #elif defined (__APPLE__)
1504 # include <IOKit/IOKitLib.h>
1505 # include <CoreFoundation/CFNumber.h> /* for kCFBooleanTrue, kCFBooleanFalse */
1509 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1511 if (fstatfs( fd, &stfs ) < 0) break;
1513 /* stfs.f_type is reserved (always set to 0) so use IOKit */
1514 kern_return_t kernResult = KERN_FAILURE;
1515 mach_port_t masterPort;
1517 char bsdName[6]; /* disk#\0 */
1519 strncpy(bsdName, stfs.f_mntfromname+strlen(_PATH_DEV) , 5);
1522 kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
1524 if (kernResult == KERN_SUCCESS)
1526 CFMutableDictionaryRef matching = IOBSDNameMatching(masterPort, 0, bsdName);
1530 CFMutableDictionaryRef properties;
1531 io_service_t devService = IOServiceGetMatchingService(masterPort, matching);
1533 if (IORegistryEntryCreateCFProperties(devService,
1535 kCFAllocatorDefault, 0) != KERN_SUCCESS)
1538 CFDictionaryGetValue(properties, CFSTR("Removable")),
1540 ) info->Characteristics |= FILE_REMOVABLE_MEDIA;
1543 CFDictionaryGetValue(properties, CFSTR("Writable")),
1545 ) info->Characteristics |= FILE_READ_ONLY_DEVICE;
1548 NB : mounted disk image (.img/.dmg) don't provide specific type
1551 if ( (type = CFDictionaryGetValue(properties, CFSTR("Type"))) )
1553 if ( CFStringCompare(type, CFSTR("CD-ROM"), 0) == kCFCompareEqualTo
1554 || CFStringCompare(type, CFSTR("DVD-ROM"), 0) == kCFCompareEqualTo
1557 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1562 CFRelease(properties);
1567 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1568 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1570 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1572 io->Information = sizeof(*info);
1573 io->u.Status = STATUS_SUCCESS;
1576 case FileFsAttributeInformation:
1577 FIXME( "%p: attribute info not supported\n", handle );
1579 case FileFsControlInformation:
1580 FIXME( "%p: control info not supported\n", handle );
1582 case FileFsFullSizeInformation:
1583 FIXME( "%p: full size info not supported\n", handle );
1585 case FileFsObjectIdInformation:
1586 FIXME( "%p: object id info not supported\n", handle );
1588 case FileFsMaximumInformation:
1589 FIXME( "%p: maximum info not supported\n", handle );
1592 io->u.Status = STATUS_INVALID_PARAMETER;
1595 wine_server_release_fd( handle, fd );
1596 return io->u.Status;
1600 /******************************************************************
1601 * NtFlushBuffersFile (NTDLL.@)
1603 * Flush any buffered data on an open file handle.
1606 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1607 * IoStatusBlock [O] Receives information about the operation on return
1610 * Success: 0. IoStatusBlock is updated.
1611 * Failure: An NTSTATUS error code describing the error.
1613 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1616 HANDLE hEvent = NULL;
1618 SERVER_START_REQ( flush_file )
1620 req->handle = hFile;
1621 ret = wine_server_call( req );
1622 hEvent = reply->event;
1627 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1633 /******************************************************************
1634 * NtLockFile (NTDLL.@)
1638 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1639 PIO_APC_ROUTINE apc, void* apc_user,
1640 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1641 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1648 if (apc || io_status || key)
1650 FIXME("Unimplemented yet parameter\n");
1651 return STATUS_NOT_IMPLEMENTED;
1656 SERVER_START_REQ( lock_file )
1658 req->handle = hFile;
1659 req->offset_low = offset->u.LowPart;
1660 req->offset_high = offset->u.HighPart;
1661 req->count_low = count->u.LowPart;
1662 req->count_high = count->u.HighPart;
1663 req->shared = !exclusive;
1664 req->wait = !dont_wait;
1665 ret = wine_server_call( req );
1666 handle = reply->handle;
1667 async = reply->overlapped;
1670 if (ret != STATUS_PENDING)
1672 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1678 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1679 if (handle) NtClose( handle );
1680 return STATUS_PENDING;
1684 NtWaitForSingleObject( handle, FALSE, NULL );
1691 /* Unix lock conflict, sleep a bit and retry */
1692 time.QuadPart = 100 * (ULONGLONG)10000;
1693 time.QuadPart = -time.QuadPart;
1694 NtDelayExecution( FALSE, &time );
1700 /******************************************************************
1701 * NtUnlockFile (NTDLL.@)
1705 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
1706 PLARGE_INTEGER offset, PLARGE_INTEGER count,
1711 TRACE( "%p %lx%08lx %lx%08lx\n",
1712 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
1714 if (io_status || key)
1716 FIXME("Unimplemented yet parameter\n");
1717 return STATUS_NOT_IMPLEMENTED;
1720 SERVER_START_REQ( unlock_file )
1722 req->handle = hFile;
1723 req->offset_low = offset->u.LowPart;
1724 req->offset_high = offset->u.HighPart;
1725 req->count_low = count->u.LowPart;
1726 req->count_high = count->u.HighPart;
1727 status = wine_server_call( req );
1733 /******************************************************************
1734 * NtCreateNamedPipeFile (NTDLL.@)
1738 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
1739 POBJECT_ATTRIBUTES oa, PIO_STATUS_BLOCK iosb,
1740 ULONG sharing, ULONG dispo, ULONG options,
1741 ULONG pipe_type, ULONG read_mode,
1742 ULONG completion_mode, ULONG max_inst,
1743 ULONG inbound_quota, ULONG outbound_quota,
1744 PLARGE_INTEGER timeout)
1747 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1749 TRACE("(%p %lx %p %p %lx %ld %lx %ld %ld %ld %ld %ld %ld %p): stub\n",
1750 handle, access, oa, iosb, sharing, dispo, options, pipe_type,
1751 read_mode, completion_mode, max_inst, inbound_quota, outbound_quota,
1754 if (oa->ObjectName->Length < sizeof(leadin) ||
1755 strncmpiW( oa->ObjectName->Buffer,
1756 leadin, sizeof(leadin)/sizeof(leadin[0]) ))
1757 return STATUS_OBJECT_NAME_INVALID;
1758 /* assume we only get relative timeout, and storable in a DWORD as ms */
1759 if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
1760 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
1762 SERVER_START_REQ( create_named_pipe )
1764 req->options = options; /* FIXME not used in server yet !!!! */
1766 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
1767 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
1768 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
1769 req->maxinstances = max_inst;
1770 req->outsize = outbound_quota;
1771 req->insize = inbound_quota;
1772 req->timeout = timeout->QuadPart / -10000;
1773 req->inherit = (oa->Attributes & OBJ_INHERIT) != 0;
1774 wine_server_add_data( req, oa->ObjectName->Buffer + 4,
1775 oa->ObjectName->Length - 4 * sizeof(WCHAR) );
1776 status = wine_server_call( req );
1777 if (!status) *handle = reply->handle;
1783 /******************************************************************
1784 * NtDeleteFile (NTDLL.@)
1788 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
1794 TRACE("%p\n", ObjectAttributes);
1795 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE, ObjectAttributes,
1797 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1798 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
1799 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1803 /******************************************************************
1804 * NtCancelIoFile (NTDLL.@)
1808 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
1810 LARGE_INTEGER timeout;
1812 TRACE("%p %p\n", hFile, io_status );
1814 SERVER_START_REQ( cancel_async )
1816 req->handle = hFile;
1817 wine_server_call( req );
1820 /* Let some APC be run, so that we can run the remaining APCs on hFile
1821 * either the cancelation of the pending one, but also the execution
1822 * of the queued APC, but not yet run. This is needed to ensure proper
1823 * clean-up of allocated data.
1825 timeout.u.LowPart = timeout.u.HighPart = 0;
1826 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
1829 /******************************************************************************
1830 * NtCreateMailslotFile [NTDLL.@]
1831 * ZwCreateMailslotFile [NTDLL.@]
1834 * pHandle [O] pointer to receive the handle created
1835 * DesiredAccess [I] access mode (read, write, etc)
1836 * ObjectAttributes [I] fully qualified NT path of the mailslot
1837 * IoStatusBlock [O] receives completion status and other info
1840 * MaxMessageSize [I]
1846 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
1847 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
1848 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
1849 PLARGE_INTEGER TimeOut)
1851 static const WCHAR leadin[] = {
1852 '\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
1855 TRACE("%p %08lx %p %p %08lx %08lx %08lx %p\n",
1856 pHandle, DesiredAccess, attr, IoStatusBlock,
1857 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
1859 if (attr->ObjectName->Length < sizeof(leadin) ||
1860 strncmpiW( attr->ObjectName->Buffer,
1861 leadin, sizeof(leadin)/sizeof(leadin[0]) ))
1863 return STATUS_OBJECT_NAME_INVALID;
1866 SERVER_START_REQ( create_mailslot )
1868 req->max_msgsize = MaxMessageSize;
1869 req->read_timeout = TimeOut->QuadPart / -10000;
1870 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1871 wine_server_add_data( req, attr->ObjectName->Buffer + 4,
1872 attr->ObjectName->Length - 4*sizeof(WCHAR) );
1873 ret = wine_server_call( req );
1874 if( ret == STATUS_SUCCESS )
1875 *pHandle = reply->handle;