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"
65 #include "wine/server.h"
66 #include "ntdll_misc.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
73 mode_t FILE_umask = 0;
75 #define SECSPERDAY 86400
76 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
78 /**************************************************************************
79 * NtOpenFile [NTDLL.@]
80 * ZwOpenFile [NTDLL.@]
85 * handle [O] Variable that receives the file handle on return
86 * access [I] Access desired by the caller to the file
87 * attr [I] Structure describing the file to be opened
88 * io [O] Receives details about the result of the operation
89 * sharing [I] Type of shared access the caller requires
90 * options [I] Options for the file open
93 * Success: 0. FileHandle and IoStatusBlock are updated.
94 * Failure: An NTSTATUS error code describing the error.
96 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
97 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
98 ULONG sharing, ULONG options )
100 return NtCreateFile( handle, access, attr, io, NULL, 0,
101 sharing, FILE_OPEN, options, NULL, 0 );
104 /**************************************************************************
105 * NtCreateFile [NTDLL.@]
106 * ZwCreateFile [NTDLL.@]
108 * Either create a new file or directory, or open an existing file, device,
109 * directory or volume.
112 * handle [O] Points to a variable which receives the file handle on return
113 * access [I] Desired access to the file
114 * attr [I] Structure describing the file
115 * io [O] Receives information about the operation on return
116 * alloc_size [I] Initial size of the file in bytes
117 * attributes [I] Attributes to create the file with
118 * sharing [I] Type of shared access the caller would like to the file
119 * disposition [I] Specifies what to do, depending on whether the file already exists
120 * options [I] Options for creating a new file
121 * ea_buffer [I] Pointer to an extended attributes buffer
122 * ea_length [I] Length of ea_buffer
125 * Success: 0. handle and io are updated.
126 * Failure: An NTSTATUS error code describing the error.
128 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
129 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
130 ULONG attributes, ULONG sharing, ULONG disposition,
131 ULONG options, PVOID ea_buffer, ULONG ea_length )
133 static const WCHAR pipeW[] = {'\\','?','?','\\','p','i','p','e','\\'};
134 static const WCHAR mailslotW[] = {'\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
135 ANSI_STRING unix_name;
138 TRACE("handle=%p access=%08lx name=%s objattr=%08lx root=%p sec=%p io=%p alloc_size=%p\n"
139 "attr=%08lx sharing=%08lx disp=%ld options=%08lx ea=%p.0x%08lx\n",
140 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
141 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
142 attributes, sharing, disposition, options, ea_buffer, ea_length );
144 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
146 if (attr->RootDirectory)
148 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
149 return STATUS_OBJECT_NAME_NOT_FOUND;
151 if (alloc_size) FIXME( "alloc_size not supported\n" );
153 /* check for named pipe */
155 if (attr->ObjectName->Length > sizeof(pipeW) &&
156 !memicmpW( attr->ObjectName->Buffer, pipeW, sizeof(pipeW)/sizeof(WCHAR) ))
158 SERVER_START_REQ( open_named_pipe )
160 req->access = access;
161 req->flags = options;
162 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
163 wine_server_add_data( req, attr->ObjectName->Buffer + 4,
164 attr->ObjectName->Length - 4*sizeof(WCHAR) );
165 io->u.Status = wine_server_call( req );
166 *handle = reply->handle;
172 /* check for mailslot */
174 if (attr->ObjectName->Length > sizeof(mailslotW) &&
175 !memicmpW( attr->ObjectName->Buffer, mailslotW, sizeof(mailslotW)/sizeof(WCHAR) ))
177 SERVER_START_REQ( open_mailslot )
179 req->access = access & GENERIC_WRITE;
180 req->sharing = sharing;
181 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
182 wine_server_add_data( req, attr->ObjectName->Buffer + 4,
183 attr->ObjectName->Length - 4*sizeof(WCHAR) );
184 io->u.Status = wine_server_call( req );
185 *handle = reply->handle;
191 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
192 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
194 if (io->u.Status == STATUS_NO_SUCH_FILE &&
195 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
198 io->u.Status = STATUS_SUCCESS;
201 if (io->u.Status == STATUS_SUCCESS)
203 SERVER_START_REQ( create_file )
205 req->access = access;
206 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
207 req->sharing = sharing;
208 req->create = disposition;
209 req->options = options;
210 req->attrs = attributes;
211 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
212 io->u.Status = wine_server_call( req );
213 *handle = reply->handle;
216 RtlFreeAnsiString( &unix_name );
218 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), io->u.Status );
220 if (io->u.Status == STATUS_SUCCESS)
222 if (created) io->Information = FILE_CREATED;
223 else switch(disposition)
226 io->Information = FILE_SUPERSEDED;
229 io->Information = FILE_CREATED;
233 io->Information = FILE_OPENED;
236 case FILE_OVERWRITE_IF:
237 io->Information = FILE_OVERWRITTEN;
245 /***********************************************************************
246 * Asynchronous file I/O *
248 static void WINAPI FILE_AsyncReadService(void*, PIO_STATUS_BLOCK, ULONG);
249 static void WINAPI FILE_AsyncWriteService(void*, PIO_STATUS_BLOCK, ULONG);
251 typedef struct async_fileio
259 int queue_apc_on_error;
265 static void fileio_terminate(async_fileio *fileio, IO_STATUS_BLOCK* iosb)
267 TRACE("data: %p\n", fileio);
269 wine_server_release_fd( fileio->handle, fileio->fd );
270 if ( fileio->event != INVALID_HANDLE_VALUE )
271 NtSetEvent( fileio->event, NULL );
274 (iosb->u.Status == STATUS_SUCCESS || fileio->queue_apc_on_error))
275 fileio->apc( fileio->apc_user, iosb, iosb->Information );
277 RtlFreeHeap( GetProcessHeap(), 0, fileio );
281 static ULONG fileio_queue_async(async_fileio* fileio, IO_STATUS_BLOCK* iosb,
284 PIO_APC_ROUTINE apc = do_read ? FILE_AsyncReadService : FILE_AsyncWriteService;
287 SERVER_START_REQ( register_async )
289 req->handle = fileio->handle;
292 req->io_user = fileio;
293 req->type = do_read ? ASYNC_TYPE_READ : ASYNC_TYPE_WRITE;
294 req->count = (fileio->count < iosb->Information) ?
295 0 : fileio->count - iosb->Information;
296 status = wine_server_call( req );
300 if ( status ) iosb->u.Status = status;
301 if ( iosb->u.Status != STATUS_PENDING )
303 (apc)( fileio, iosb, iosb->u.Status );
304 return iosb->u.Status;
306 NtCurrentTeb()->num_async_io++;
307 return STATUS_SUCCESS;
310 /***********************************************************************
311 * FILE_GetNtStatus(void)
313 * Retrieve the Nt Status code from errno.
314 * Try to be consistent with FILE_SetDosError().
316 NTSTATUS FILE_GetNtStatus(void)
320 TRACE( "errno = %d\n", errno );
323 case EAGAIN: return STATUS_SHARING_VIOLATION;
324 case EBADF: return STATUS_INVALID_HANDLE;
325 case ENOSPC: return STATUS_DISK_FULL;
328 case EACCES: return STATUS_ACCESS_DENIED;
329 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
330 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
331 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
333 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
334 case EINVAL: return STATUS_INVALID_PARAMETER;
335 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
336 case EPIPE: return STATUS_PIPE_BROKEN;
337 case EIO: return STATUS_DEVICE_NOT_READY;
339 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
342 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
343 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
344 case ENOEXEC: /* ?? */
345 case ESPIPE: /* ?? */
346 case EEXIST: /* ?? */
348 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
349 return STATUS_UNSUCCESSFUL;
353 /***********************************************************************
354 * FILE_AsyncReadService (INTERNAL)
356 * This function is called while the client is waiting on the
357 * server, so we can't make any server calls here.
359 static void WINAPI FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
361 async_fileio *fileio = (async_fileio*)user;
363 int already = iosb->Information;
365 TRACE("%p %p %lu\n", iosb, fileio->buffer, status);
369 case STATUS_ALERTED: /* got some new data */
370 if (iosb->u.Status != STATUS_PENDING) FIXME("unexpected status %08lx\n", iosb->u.Status);
371 /* check to see if the data is ready (non-blocking) */
372 if ( fileio->avail_mode )
373 result = read(fileio->fd, &fileio->buffer[already],
374 fileio->count - already);
377 result = pread(fileio->fd, &fileio->buffer[already],
378 fileio->count - already,
379 fileio->offset + already);
380 if ((result < 0) && (errno == ESPIPE))
381 result = read(fileio->fd, &fileio->buffer[already],
382 fileio->count - already);
387 if (errno == EAGAIN || errno == EINTR)
389 TRACE("Deferred read %d\n", errno);
390 iosb->u.Status = STATUS_PENDING;
392 else /* check to see if the transfer is complete */
393 iosb->u.Status = FILE_GetNtStatus();
395 else if (result == 0)
397 iosb->u.Status = iosb->Information ? STATUS_SUCCESS : STATUS_END_OF_FILE;
401 iosb->Information += result;
402 if (iosb->Information >= fileio->count || fileio->avail_mode)
403 iosb->u.Status = STATUS_SUCCESS;
406 /* if we only have to read the available data, and none is available,
407 * simply cancel the request. If data was available, it has been read
408 * while in by previous call (NtDelayExecution)
410 iosb->u.Status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
413 TRACE("read %d more bytes %ld/%d so far (%s)\n",
414 result, iosb->Information, fileio->count,
415 (iosb->u.Status == STATUS_SUCCESS) ? "success" : "pending");
417 /* queue another async operation ? */
418 if (iosb->u.Status == STATUS_PENDING)
419 fileio_queue_async(fileio, iosb, TRUE);
421 fileio_terminate(fileio, iosb);
424 iosb->u.Status = status;
425 fileio_terminate(fileio, iosb);
431 /******************************************************************************
432 * NtReadFile [NTDLL.@]
433 * ZwReadFile [NTDLL.@]
435 * Read from an open file handle.
438 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
439 * Event [I] Event to signal upon completion (or NULL)
440 * ApcRoutine [I] Callback to call upon completion (or NULL)
441 * ApcContext [I] Context for ApcRoutine (or NULL)
442 * IoStatusBlock [O] Receives information about the operation on return
443 * Buffer [O] Destination for the data read
444 * Length [I] Size of Buffer
445 * ByteOffset [O] Destination for the new file pointer position (or NULL)
446 * Key [O] Function unknown (may be NULL)
449 * Success: 0. IoStatusBlock is updated, and the Information member contains
450 * The number of bytes read.
451 * Failure: An NTSTATUS error code describing the error.
453 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
454 PIO_APC_ROUTINE apc, void* apc_user,
455 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
456 PLARGE_INTEGER offset, PULONG key)
458 int unix_handle, flags;
460 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
461 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
463 if (!io_status) return STATUS_ACCESS_VIOLATION;
465 io_status->Information = 0;
466 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_READ, &unix_handle, &flags );
467 if (io_status->u.Status) return io_status->u.Status;
469 if (flags & FD_FLAG_RECV_SHUTDOWN)
471 wine_server_release_fd( hFile, unix_handle );
472 return STATUS_PIPE_DISCONNECTED;
475 if (flags & FD_FLAG_TIMEOUT)
479 /* this shouldn't happen, but check it */
480 FIXME("NIY-hEvent\n");
481 wine_server_release_fd( hFile, unix_handle );
482 return STATUS_NOT_IMPLEMENTED;
484 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
485 if (io_status->u.Status)
487 wine_server_release_fd( hFile, unix_handle );
488 return io_status->u.Status;
492 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
494 async_fileio* fileio;
497 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
499 wine_server_release_fd( hFile, unix_handle );
500 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
501 return STATUS_NO_MEMORY;
503 fileio->handle = hFile;
504 fileio->count = length;
505 if ( offset == NULL )
509 fileio->offset = offset->QuadPart;
510 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
511 FIXME("High part of offset is lost\n");
514 fileio->apc_user = apc_user;
515 fileio->buffer = buffer;
516 fileio->queue_apc_on_error = 0;
517 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
518 fileio->fd = unix_handle; /* FIXME */
519 fileio->event = hEvent;
520 NtResetEvent(hEvent, NULL);
522 io_status->u.Status = STATUS_PENDING;
523 ret = fileio_queue_async(fileio, io_status, TRUE);
524 if (ret != STATUS_SUCCESS)
526 wine_server_release_fd( hFile, unix_handle );
527 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
528 RtlFreeHeap(GetProcessHeap(), 0, fileio);
531 if (flags & FD_FLAG_TIMEOUT)
533 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
535 if (ret != STATUS_USER_APC)
536 fileio->queue_apc_on_error = 1;
540 LARGE_INTEGER timeout;
542 /* let some APC be run, this will read some already pending data */
543 timeout.u.LowPart = timeout.u.HighPart = 0;
544 ret = NtDelayExecution( TRUE, &timeout );
545 /* the apc didn't run and therefore the completion routine now
546 * needs to be sent errors.
547 * Note that there is no race between setting this flag and
548 * returning errors because apc's are run only during alertable
550 if (ret != STATUS_USER_APC)
551 fileio->queue_apc_on_error = 1;
553 TRACE("= 0x%08lx\n", io_status->u.Status);
554 return io_status->u.Status;
559 FILE_POSITION_INFORMATION fpi;
561 fpi.CurrentByteOffset = *offset;
562 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
563 FilePositionInformation);
564 if (io_status->u.Status)
566 wine_server_release_fd( hFile, unix_handle );
567 return io_status->u.Status;
570 /* code for synchronous reads */
571 while ((io_status->Information = read( unix_handle, buffer, length )) == -1)
573 if ((errno == EAGAIN) || (errno == EINTR)) continue;
576 io_status->Information = 0;
577 io_status->u.Status = STATUS_ACCESS_VIOLATION;
579 else io_status->u.Status = FILE_GetNtStatus();
582 wine_server_release_fd( hFile, unix_handle );
583 TRACE("= 0x%08lx\n", io_status->u.Status);
584 return io_status->u.Status;
587 /***********************************************************************
588 * FILE_AsyncWriteService (INTERNAL)
590 * This function is called while the client is waiting on the
591 * server, so we can't make any server calls here.
593 static void WINAPI FILE_AsyncWriteService(void *ovp, IO_STATUS_BLOCK *iosb, ULONG status)
595 async_fileio *fileio = (async_fileio *) ovp;
597 int already = iosb->Information;
599 TRACE("(%p %p %lu)\n",iosb, fileio->buffer, status);
604 /* write some data (non-blocking) */
605 if ( fileio->avail_mode )
606 result = write(fileio->fd, &fileio->buffer[already],
607 fileio->count - already);
610 result = pwrite(fileio->fd, &fileio->buffer[already],
611 fileio->count - already, fileio->offset + already);
612 if ((result < 0) && (errno == ESPIPE))
613 result = write(fileio->fd, &fileio->buffer[already],
614 fileio->count - already);
619 if (errno == EAGAIN || errno == EINTR) iosb->u.Status = STATUS_PENDING;
620 else iosb->u.Status = FILE_GetNtStatus();
624 iosb->Information += result;
625 iosb->u.Status = (iosb->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
626 TRACE("wrote %d more bytes %ld/%d so far\n",
627 result, iosb->Information, fileio->count);
629 if (iosb->u.Status == STATUS_PENDING)
630 fileio_queue_async(fileio, iosb, FALSE);
632 fileio_terminate(fileio, iosb);
635 iosb->u.Status = status;
636 fileio_terminate(fileio, iosb);
641 /******************************************************************************
642 * NtWriteFile [NTDLL.@]
643 * ZwWriteFile [NTDLL.@]
645 * Write to an open file handle.
648 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
649 * Event [I] Event to signal upon completion (or NULL)
650 * ApcRoutine [I] Callback to call upon completion (or NULL)
651 * ApcContext [I] Context for ApcRoutine (or NULL)
652 * IoStatusBlock [O] Receives information about the operation on return
653 * Buffer [I] Source for the data to write
654 * Length [I] Size of Buffer
655 * ByteOffset [O] Destination for the new file pointer position (or NULL)
656 * Key [O] Function unknown (may be NULL)
659 * Success: 0. IoStatusBlock is updated, and the Information member contains
660 * The number of bytes written.
661 * Failure: An NTSTATUS error code describing the error.
663 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
664 PIO_APC_ROUTINE apc, void* apc_user,
665 PIO_STATUS_BLOCK io_status,
666 const void* buffer, ULONG length,
667 PLARGE_INTEGER offset, PULONG key)
669 int unix_handle, flags;
671 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p)!\n",
672 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
674 if (!io_status) return STATUS_ACCESS_VIOLATION;
676 io_status->Information = 0;
677 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_WRITE, &unix_handle, &flags );
678 if (io_status->u.Status) return io_status->u.Status;
680 if (flags & FD_FLAG_SEND_SHUTDOWN)
682 wine_server_release_fd( hFile, unix_handle );
683 return STATUS_PIPE_DISCONNECTED;
686 if (flags & FD_FLAG_TIMEOUT)
690 /* this shouldn't happen, but check it */
691 FIXME("NIY-hEvent\n");
692 wine_server_release_fd( hFile, unix_handle );
693 return STATUS_NOT_IMPLEMENTED;
695 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
696 if (io_status->u.Status)
698 wine_server_release_fd( hFile, unix_handle );
699 return io_status->u.Status;
703 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
705 async_fileio* fileio;
708 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
710 wine_server_release_fd( hFile, unix_handle );
711 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
712 return STATUS_NO_MEMORY;
714 fileio->handle = hFile;
715 fileio->count = length;
718 fileio->offset = offset->QuadPart;
719 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
720 FIXME("High part of offset is lost\n");
727 fileio->apc_user = apc_user;
728 fileio->buffer = (void*)buffer;
729 fileio->queue_apc_on_error = 0;
730 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
731 fileio->fd = unix_handle; /* FIXME */
732 fileio->event = hEvent;
733 NtResetEvent(hEvent, NULL);
735 io_status->Information = 0;
736 io_status->u.Status = STATUS_PENDING;
737 ret = fileio_queue_async(fileio, io_status, FALSE);
738 if (ret != STATUS_SUCCESS)
740 wine_server_release_fd( hFile, unix_handle );
741 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
742 RtlFreeHeap(GetProcessHeap(), 0, fileio);
745 if (flags & FD_FLAG_TIMEOUT)
747 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
749 if (ret != STATUS_USER_APC)
750 fileio->queue_apc_on_error = 1;
754 LARGE_INTEGER timeout;
756 /* let some APC be run, this will write as much data as possible */
757 timeout.u.LowPart = timeout.u.HighPart = 0;
758 ret = NtDelayExecution( TRUE, &timeout );
759 /* the apc didn't run and therefore the completion routine now
760 * needs to be sent errors.
761 * Note that there is no race between setting this flag and
762 * returning errors because apc's are run only during alertable
764 if (ret != STATUS_USER_APC)
765 fileio->queue_apc_on_error = 1;
767 return io_status->u.Status;
772 FILE_POSITION_INFORMATION fpi;
774 fpi.CurrentByteOffset = *offset;
775 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
776 FilePositionInformation);
777 if (io_status->u.Status)
779 wine_server_release_fd( hFile, unix_handle );
780 return io_status->u.Status;
784 /* synchronous file write */
785 while ((io_status->Information = write( unix_handle, buffer, length )) == -1)
787 if ((errno == EAGAIN) || (errno == EINTR)) continue;
790 io_status->Information = 0;
791 io_status->u.Status = STATUS_INVALID_USER_BUFFER;
793 else if (errno == ENOSPC) io_status->u.Status = STATUS_DISK_FULL;
794 else io_status->u.Status = FILE_GetNtStatus();
797 wine_server_release_fd( hFile, unix_handle );
798 return io_status->u.Status;
801 /**************************************************************************
802 * NtDeviceIoControlFile [NTDLL.@]
803 * ZwDeviceIoControlFile [NTDLL.@]
805 * Perform an I/O control operation on an open file handle.
808 * DeviceHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
809 * Event [I] Event to signal upon completion (or NULL)
810 * ApcRoutine [I] Callback to call upon completion (or NULL)
811 * ApcContext [I] Context for ApcRoutine (or NULL)
812 * IoStatusBlock [O] Receives information about the operation on return
813 * IoControlCode [I] Control code for the operation to perform
814 * InputBuffer [I] Source for any input data required (or NULL)
815 * InputBufferSize [I] Size of InputBuffer
816 * OutputBuffer [O] Source for any output data returned (or NULL)
817 * OutputBufferSize [I] Size of OutputBuffer
820 * Success: 0. IoStatusBlock is updated.
821 * Failure: An NTSTATUS error code describing the error.
823 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE DeviceHandle, HANDLE hEvent,
824 PIO_APC_ROUTINE UserApcRoutine,
825 PVOID UserApcContext,
826 PIO_STATUS_BLOCK IoStatusBlock,
829 ULONG InputBufferSize,
831 ULONG OutputBufferSize)
833 TRACE("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx)\n",
834 DeviceHandle, hEvent, UserApcRoutine, UserApcContext,
835 IoStatusBlock, IoControlCode,
836 InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize);
838 if (CDROM_DeviceIoControl(DeviceHandle, hEvent,
839 UserApcRoutine, UserApcContext,
840 IoStatusBlock, IoControlCode,
841 InputBuffer, InputBufferSize,
842 OutputBuffer, OutputBufferSize) == STATUS_NO_SUCH_DEVICE)
844 /* it wasn't a CDROM */
845 FIXME("Unimplemented dwIoControlCode=%08lx\n", IoControlCode);
846 IoStatusBlock->u.Status = STATUS_NOT_IMPLEMENTED;
847 IoStatusBlock->Information = 0;
848 if (hEvent) NtSetEvent(hEvent, NULL);
850 return IoStatusBlock->u.Status;
853 /******************************************************************************
854 * NtFsControlFile [NTDLL.@]
855 * ZwFsControlFile [NTDLL.@]
857 NTSTATUS WINAPI NtFsControlFile(
858 IN HANDLE DeviceHandle,
859 IN HANDLE Event OPTIONAL,
860 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
861 IN PVOID ApcContext OPTIONAL,
862 OUT PIO_STATUS_BLOCK IoStatusBlock,
863 IN ULONG IoControlCode,
864 IN PVOID InputBuffer,
865 IN ULONG InputBufferSize,
866 OUT PVOID OutputBuffer,
867 IN ULONG OutputBufferSize)
869 FIXME("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx): stub\n",
870 DeviceHandle,Event,ApcRoutine,ApcContext,IoStatusBlock,IoControlCode,
871 InputBuffer,InputBufferSize,OutputBuffer,OutputBufferSize);
875 /******************************************************************************
876 * NtSetVolumeInformationFile [NTDLL.@]
877 * ZwSetVolumeInformationFile [NTDLL.@]
879 * Set volume information for an open file handle.
882 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
883 * IoStatusBlock [O] Receives information about the operation on return
884 * FsInformation [I] Source for volume information
885 * Length [I] Size of FsInformation
886 * FsInformationClass [I] Type of volume information to set
889 * Success: 0. IoStatusBlock is updated.
890 * Failure: An NTSTATUS error code describing the error.
892 NTSTATUS WINAPI NtSetVolumeInformationFile(
893 IN HANDLE FileHandle,
894 PIO_STATUS_BLOCK IoStatusBlock,
897 FS_INFORMATION_CLASS FsInformationClass)
899 FIXME("(%p,%p,%p,0x%08lx,0x%08x) stub\n",
900 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
904 /******************************************************************************
905 * NtQueryInformationFile [NTDLL.@]
906 * ZwQueryInformationFile [NTDLL.@]
908 * Get information about an open file handle.
911 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
912 * io [O] Receives information about the operation on return
913 * ptr [O] Destination for file information
914 * len [I] Size of FileInformation
915 * class [I] Type of file information to get
918 * Success: 0. IoStatusBlock and FileInformation are updated.
919 * Failure: An NTSTATUS error code describing the error.
921 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
922 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
924 static const size_t info_sizes[] =
927 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
928 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
929 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
930 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
931 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
932 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
933 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
934 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
935 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
936 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
937 0, /* FileLinkInformation */
938 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
939 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
940 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
941 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
942 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
943 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
944 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
945 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
946 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
947 0, /* FileAlternateNameInformation */
948 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
949 0, /* FilePipeInformation */
950 0, /* FilePipeLocalInformation */
951 0, /* FilePipeRemoteInformation */
952 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
953 0, /* FileMailslotSetInformation */
954 0, /* FileCompressionInformation */
955 0, /* FileObjectIdInformation */
956 0, /* FileCompletionInformation */
957 0, /* FileMoveClusterInformation */
958 0, /* FileQuotaInformation */
959 0, /* FileReparsePointInformation */
960 0, /* FileNetworkOpenInformation */
961 0, /* FileAttributeTagInformation */
962 0 /* FileTrackingInformation */
968 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", hFile, io, ptr, len, class);
972 if (class <= 0 || class >= FileMaximumInformation)
973 return io->u.Status = STATUS_INVALID_INFO_CLASS;
974 if (!info_sizes[class])
976 FIXME("Unsupported class (%d)\n", class);
977 return io->u.Status = STATUS_NOT_IMPLEMENTED;
979 if (len < info_sizes[class])
980 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
982 if ((io->u.Status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
987 case FileBasicInformation:
989 FILE_BASIC_INFORMATION *info = ptr;
991 if (fstat( fd, &st ) == -1)
992 io->u.Status = FILE_GetNtStatus();
993 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
994 io->u.Status = STATUS_INVALID_INFO_CLASS;
997 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
998 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
999 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1000 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1001 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1002 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1003 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1004 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1008 case FileStandardInformation:
1010 FILE_STANDARD_INFORMATION *info = ptr;
1012 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1015 if ((info->Directory = S_ISDIR(st.st_mode)))
1017 info->AllocationSize.QuadPart = 0;
1018 info->EndOfFile.QuadPart = 0;
1019 info->NumberOfLinks = 1;
1020 info->DeletePending = FALSE;
1024 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1025 info->EndOfFile.QuadPart = st.st_size;
1026 info->NumberOfLinks = st.st_nlink;
1027 info->DeletePending = FALSE; /* FIXME */
1032 case FilePositionInformation:
1034 FILE_POSITION_INFORMATION *info = ptr;
1035 off_t res = lseek( fd, 0, SEEK_CUR );
1036 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1037 else info->CurrentByteOffset.QuadPart = res;
1040 case FileInternalInformation:
1042 FILE_INTERNAL_INFORMATION *info = ptr;
1044 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1045 else info->IndexNumber.QuadPart = st.st_ino;
1048 case FileEaInformation:
1050 FILE_EA_INFORMATION *info = ptr;
1054 case FileEndOfFileInformation:
1056 FILE_END_OF_FILE_INFORMATION *info = ptr;
1058 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1059 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1062 case FileAllInformation:
1064 FILE_ALL_INFORMATION *info = ptr;
1066 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1067 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1068 io->u.Status = STATUS_INVALID_INFO_CLASS;
1071 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1073 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1074 info->StandardInformation.AllocationSize.QuadPart = 0;
1075 info->StandardInformation.EndOfFile.QuadPart = 0;
1076 info->StandardInformation.NumberOfLinks = 1;
1077 info->StandardInformation.DeletePending = FALSE;
1081 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1082 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1083 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1084 info->StandardInformation.NumberOfLinks = st.st_nlink;
1085 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1087 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1088 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1089 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1090 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1091 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1092 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1093 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1094 info->EaInformation.EaSize = 0;
1095 info->AccessInformation.AccessFlags = 0; /* FIXME */
1096 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1097 info->ModeInformation.Mode = 0; /* FIXME */
1098 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1099 info->NameInformation.FileNameLength = 0;
1100 io->Information = sizeof(*info) - sizeof(WCHAR);
1104 case FileMailslotQueryInformation:
1106 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1108 SERVER_START_REQ( set_mailslot_info )
1110 req->handle = hFile;
1112 io->u.Status = wine_server_call( req );
1113 if( io->u.Status == STATUS_SUCCESS )
1115 info->MaximumMessageSize = reply->max_msgsize;
1116 info->MailslotQuota = 0;
1117 info->NextMessageSize = reply->next_msgsize;
1118 info->MessagesAvailable = reply->msg_count;
1119 info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1126 FIXME("Unsupported class (%d)\n", class);
1127 io->u.Status = STATUS_NOT_IMPLEMENTED;
1130 wine_server_release_fd( hFile, fd );
1131 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1132 return io->u.Status;
1135 /******************************************************************************
1136 * NtSetInformationFile [NTDLL.@]
1137 * ZwSetInformationFile [NTDLL.@]
1139 * Set information about an open file handle.
1142 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1143 * io [O] Receives information about the operation on return
1144 * ptr [I] Source for file information
1145 * len [I] Size of FileInformation
1146 * class [I] Type of file information to set
1149 * Success: 0. io is updated.
1150 * Failure: An NTSTATUS error code describing the error.
1152 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1153 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1157 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", handle, io, ptr, len, class);
1159 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )))
1160 return io->u.Status;
1162 io->u.Status = STATUS_SUCCESS;
1165 case FileBasicInformation:
1166 if (len >= sizeof(FILE_BASIC_INFORMATION))
1169 const FILE_BASIC_INFORMATION *info = ptr;
1171 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1173 ULONGLONG sec, nsec;
1174 struct timeval tv[2];
1176 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1179 tv[0].tv_sec = tv[0].tv_usec = 0;
1180 tv[1].tv_sec = tv[1].tv_usec = 0;
1181 if (!fstat( fd, &st ))
1183 tv[0].tv_sec = st.st_atime;
1184 tv[1].tv_sec = st.st_mtime;
1187 if (info->LastAccessTime.QuadPart)
1189 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1190 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1191 tv[0].tv_usec = (UINT)nsec / 10;
1193 if (info->LastWriteTime.QuadPart)
1195 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1196 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1197 tv[1].tv_usec = (UINT)nsec / 10;
1199 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1202 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1204 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1207 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1209 if (S_ISDIR( st.st_mode))
1210 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1212 st.st_mode &= ~0222; /* clear write permission bits */
1216 /* add write permission only where we already have read permission */
1217 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1219 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1223 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1226 case FilePositionInformation:
1227 if (len >= sizeof(FILE_POSITION_INFORMATION))
1229 const FILE_POSITION_INFORMATION *info = ptr;
1231 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1232 io->u.Status = FILE_GetNtStatus();
1234 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1237 case FileEndOfFileInformation:
1238 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1241 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1243 /* first try normal truncate */
1244 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1246 /* now check for the need to extend the file */
1247 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1249 static const char zero;
1251 /* extend the file one byte beyond the requested size and then truncate it */
1252 /* this should work around ftruncate implementations that can't extend files */
1253 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1254 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1256 io->u.Status = FILE_GetNtStatus();
1258 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1261 case FileMailslotSetInformation:
1263 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1265 SERVER_START_REQ( set_mailslot_info )
1267 req->handle = handle;
1268 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1269 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1270 io->u.Status = wine_server_call( req );
1277 FIXME("Unsupported class (%d)\n", class);
1278 io->u.Status = STATUS_NOT_IMPLEMENTED;
1281 wine_server_release_fd( handle, fd );
1282 io->Information = 0;
1283 return io->u.Status;
1287 /******************************************************************************
1288 * NtQueryFullAttributesFile (NTDLL.@)
1290 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1291 FILE_NETWORK_OPEN_INFORMATION *info )
1293 ANSI_STRING unix_name;
1296 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1297 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1301 if (stat( unix_name.Buffer, &st ) == -1)
1302 status = FILE_GetNtStatus();
1303 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1304 status = STATUS_INVALID_INFO_CLASS;
1307 if (S_ISDIR(st.st_mode))
1309 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1310 info->AllocationSize.QuadPart = 0;
1311 info->EndOfFile.QuadPart = 0;
1315 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1316 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1317 info->EndOfFile.QuadPart = st.st_size;
1319 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1320 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1321 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1322 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1323 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1324 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1325 if (DIR_is_hidden_file( attr->ObjectName ))
1326 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1328 RtlFreeAnsiString( &unix_name );
1330 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), status );
1335 /******************************************************************************
1336 * NtQueryAttributesFile (NTDLL.@)
1337 * ZwQueryAttributesFile (NTDLL.@)
1339 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1341 FILE_NETWORK_OPEN_INFORMATION full_info;
1344 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1346 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1347 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1348 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1349 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1350 info->FileAttributes = full_info.FileAttributes;
1356 /******************************************************************************
1357 * NtQueryVolumeInformationFile [NTDLL.@]
1358 * ZwQueryVolumeInformationFile [NTDLL.@]
1360 * Get volume information for an open file handle.
1363 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1364 * io [O] Receives information about the operation on return
1365 * buffer [O] Destination for volume information
1366 * length [I] Size of FsInformation
1367 * info_class [I] Type of volume information to set
1370 * Success: 0. io and buffer are updated.
1371 * Failure: An NTSTATUS error code describing the error.
1373 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1374 PVOID buffer, ULONG length,
1375 FS_INFORMATION_CLASS info_class )
1380 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )) != STATUS_SUCCESS)
1381 return io->u.Status;
1383 io->u.Status = STATUS_NOT_IMPLEMENTED;
1384 io->Information = 0;
1386 switch( info_class )
1388 case FileFsVolumeInformation:
1389 FIXME( "%p: volume info not supported\n", handle );
1391 case FileFsLabelInformation:
1392 FIXME( "%p: label info not supported\n", handle );
1394 case FileFsSizeInformation:
1395 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1396 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1399 FILE_FS_SIZE_INFORMATION *info = buffer;
1401 if (fstat( fd, &st ) < 0)
1403 io->u.Status = FILE_GetNtStatus();
1406 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1408 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1412 /* Linux's fstatvfs is buggy */
1413 #if !defined(linux) || !defined(HAVE_FSTATFS)
1414 struct statvfs stfs;
1416 if (fstatvfs( fd, &stfs ) < 0)
1418 io->u.Status = FILE_GetNtStatus();
1421 info->BytesPerSector = stfs.f_frsize;
1424 if (fstatfs( fd, &stfs ) < 0)
1426 io->u.Status = FILE_GetNtStatus();
1429 info->BytesPerSector = stfs.f_bsize;
1431 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1432 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1433 info->SectorsPerAllocationUnit = 1;
1434 io->Information = sizeof(*info);
1435 io->u.Status = STATUS_SUCCESS;
1439 case FileFsDeviceInformation:
1440 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1441 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1444 FILE_FS_DEVICE_INFORMATION *info = buffer;
1446 info->Characteristics = 0;
1447 if (fstat( fd, &st ) < 0)
1449 io->u.Status = FILE_GetNtStatus();
1452 if (S_ISCHR( st.st_mode ))
1454 info->DeviceType = FILE_DEVICE_UNKNOWN;
1456 switch(major(st.st_rdev))
1459 info->DeviceType = FILE_DEVICE_NULL;
1462 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1465 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1470 else if (S_ISBLK( st.st_mode ))
1472 info->DeviceType = FILE_DEVICE_DISK;
1474 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1476 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1478 else /* regular file or directory */
1480 #if defined(linux) && defined(HAVE_FSTATFS)
1483 /* check for floppy disk */
1484 if (major(st.st_dev) == FLOPPY_MAJOR)
1485 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1487 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1488 switch (stfs.f_type)
1490 case 0x9660: /* iso9660 */
1491 case 0x15013346: /* udf */
1492 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1493 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1495 case 0x6969: /* nfs */
1496 case 0x517B: /* smbfs */
1497 case 0x564c: /* ncpfs */
1498 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1499 info->Characteristics |= FILE_REMOTE_DEVICE;
1501 case 0x01021994: /* tmpfs */
1502 case 0x28cd3d45: /* cramfs */
1503 case 0x1373: /* devfs */
1504 case 0x9fa0: /* procfs */
1505 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1508 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1511 #elif defined(__FreeBSD__)
1514 /* The proper way to do this in FreeBSD seems to be with the
1515 * name rather than the type, since their linux-compatible
1516 * fstatfs call converts the name to one of the Linux types.
1518 if (fstatfs( fd, &stfs ) < 0)
1519 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1520 else if (!strncmp("cd9660", stfs.f_fstypename,
1521 sizeof(stfs.f_fstypename)))
1523 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1524 /* Don't assume read-only, let the mount options set it
1527 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1529 else if (!strncmp("nfs", stfs.f_fstypename,
1530 sizeof(stfs.f_fstypename)))
1532 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1533 info->Characteristics |= FILE_REMOTE_DEVICE;
1535 else if (!strncmp("nwfs", stfs.f_fstypename,
1536 sizeof(stfs.f_fstypename)))
1538 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1539 info->Characteristics |= FILE_REMOTE_DEVICE;
1541 else if (!strncmp("procfs", stfs.f_fstypename,
1542 sizeof(stfs.f_fstypename)))
1543 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1545 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1546 if (stfs.f_flags & MNT_RDONLY)
1547 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1548 if (!(stfs.f_flags & MNT_LOCAL))
1550 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1551 info->Characteristics |= FILE_REMOTE_DEVICE;
1553 #elif defined (__APPLE__)
1554 # include <IOKit/IOKitLib.h>
1555 # include <CoreFoundation/CFNumber.h> /* for kCFBooleanTrue, kCFBooleanFalse */
1559 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1561 if (fstatfs( fd, &stfs ) < 0) break;
1563 /* stfs.f_type is reserved (always set to 0) so use IOKit */
1564 kern_return_t kernResult = KERN_FAILURE;
1565 mach_port_t masterPort;
1567 char bsdName[6]; /* disk#\0 */
1568 const char *name = stfs.f_mntfromname + strlen(_PATH_DEV);
1569 memcpy( bsdName, name, min(strlen(name)+1,sizeof(bsdName)) );
1570 bsdName[sizeof(bsdName)-1] = 0;
1572 kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
1574 if (kernResult == KERN_SUCCESS)
1576 CFMutableDictionaryRef matching = IOBSDNameMatching(masterPort, 0, bsdName);
1580 CFMutableDictionaryRef properties;
1581 io_service_t devService = IOServiceGetMatchingService(masterPort, matching);
1583 if (IORegistryEntryCreateCFProperties(devService,
1585 kCFAllocatorDefault, 0) != KERN_SUCCESS)
1588 CFDictionaryGetValue(properties, CFSTR("Removable")),
1590 ) info->Characteristics |= FILE_REMOVABLE_MEDIA;
1593 CFDictionaryGetValue(properties, CFSTR("Writable")),
1595 ) info->Characteristics |= FILE_READ_ONLY_DEVICE;
1598 NB : mounted disk image (.img/.dmg) don't provide specific type
1601 if ( (type = CFDictionaryGetValue(properties, CFSTR("Type"))) )
1603 if ( CFStringCompare(type, CFSTR("CD-ROM"), 0) == kCFCompareEqualTo
1604 || CFStringCompare(type, CFSTR("DVD-ROM"), 0) == kCFCompareEqualTo
1607 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1612 CFRelease(properties);
1616 /* Use dkio to work out device types */
1618 # include <sys/dkio.h>
1619 # include <sys/vtoc.h>
1620 struct dk_cinfo dkinf;
1621 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1623 WARN("Unable to get disk device type information - assuming a disk like device\n");
1624 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1626 switch (dkinf.dki_ctype)
1629 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1630 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1634 case DKC_INTEL82072:
1635 case DKC_INTEL82077:
1636 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1637 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1640 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1643 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1648 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1649 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1651 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1653 io->Information = sizeof(*info);
1654 io->u.Status = STATUS_SUCCESS;
1657 case FileFsAttributeInformation:
1658 FIXME( "%p: attribute info not supported\n", handle );
1660 case FileFsControlInformation:
1661 FIXME( "%p: control info not supported\n", handle );
1663 case FileFsFullSizeInformation:
1664 FIXME( "%p: full size info not supported\n", handle );
1666 case FileFsObjectIdInformation:
1667 FIXME( "%p: object id info not supported\n", handle );
1669 case FileFsMaximumInformation:
1670 FIXME( "%p: maximum info not supported\n", handle );
1673 io->u.Status = STATUS_INVALID_PARAMETER;
1676 wine_server_release_fd( handle, fd );
1677 return io->u.Status;
1681 /******************************************************************
1682 * NtFlushBuffersFile (NTDLL.@)
1684 * Flush any buffered data on an open file handle.
1687 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1688 * IoStatusBlock [O] Receives information about the operation on return
1691 * Success: 0. IoStatusBlock is updated.
1692 * Failure: An NTSTATUS error code describing the error.
1694 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1697 HANDLE hEvent = NULL;
1699 SERVER_START_REQ( flush_file )
1701 req->handle = hFile;
1702 ret = wine_server_call( req );
1703 hEvent = reply->event;
1708 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1714 /******************************************************************
1715 * NtLockFile (NTDLL.@)
1719 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1720 PIO_APC_ROUTINE apc, void* apc_user,
1721 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1722 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1729 if (apc || io_status || key)
1731 FIXME("Unimplemented yet parameter\n");
1732 return STATUS_NOT_IMPLEMENTED;
1737 SERVER_START_REQ( lock_file )
1739 req->handle = hFile;
1740 req->offset_low = offset->u.LowPart;
1741 req->offset_high = offset->u.HighPart;
1742 req->count_low = count->u.LowPart;
1743 req->count_high = count->u.HighPart;
1744 req->shared = !exclusive;
1745 req->wait = !dont_wait;
1746 ret = wine_server_call( req );
1747 handle = reply->handle;
1748 async = reply->overlapped;
1751 if (ret != STATUS_PENDING)
1753 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1759 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1760 if (handle) NtClose( handle );
1761 return STATUS_PENDING;
1765 NtWaitForSingleObject( handle, FALSE, NULL );
1772 /* Unix lock conflict, sleep a bit and retry */
1773 time.QuadPart = 100 * (ULONGLONG)10000;
1774 time.QuadPart = -time.QuadPart;
1775 NtDelayExecution( FALSE, &time );
1781 /******************************************************************
1782 * NtUnlockFile (NTDLL.@)
1786 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
1787 PLARGE_INTEGER offset, PLARGE_INTEGER count,
1792 TRACE( "%p %lx%08lx %lx%08lx\n",
1793 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
1795 if (io_status || key)
1797 FIXME("Unimplemented yet parameter\n");
1798 return STATUS_NOT_IMPLEMENTED;
1801 SERVER_START_REQ( unlock_file )
1803 req->handle = hFile;
1804 req->offset_low = offset->u.LowPart;
1805 req->offset_high = offset->u.HighPart;
1806 req->count_low = count->u.LowPart;
1807 req->count_high = count->u.HighPart;
1808 status = wine_server_call( req );
1814 /******************************************************************
1815 * NtCreateNamedPipeFile (NTDLL.@)
1819 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
1820 POBJECT_ATTRIBUTES oa, PIO_STATUS_BLOCK iosb,
1821 ULONG sharing, ULONG dispo, ULONG options,
1822 ULONG pipe_type, ULONG read_mode,
1823 ULONG completion_mode, ULONG max_inst,
1824 ULONG inbound_quota, ULONG outbound_quota,
1825 PLARGE_INTEGER timeout)
1828 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1830 TRACE("(%p %lx %p %p %lx %ld %lx %ld %ld %ld %ld %ld %ld %p): stub\n",
1831 handle, access, oa, iosb, sharing, dispo, options, pipe_type,
1832 read_mode, completion_mode, max_inst, inbound_quota, outbound_quota,
1835 if (oa->ObjectName->Length < sizeof(leadin) ||
1836 strncmpiW( oa->ObjectName->Buffer,
1837 leadin, sizeof(leadin)/sizeof(leadin[0]) ))
1838 return STATUS_OBJECT_NAME_INVALID;
1839 /* assume we only get relative timeout, and storable in a DWORD as ms */
1840 if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
1841 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
1843 SERVER_START_REQ( create_named_pipe )
1845 req->options = options; /* FIXME not used in server yet !!!! */
1847 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
1848 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
1849 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
1850 req->maxinstances = max_inst;
1851 req->outsize = outbound_quota;
1852 req->insize = inbound_quota;
1853 req->timeout = timeout->QuadPart / -10000;
1854 req->inherit = (oa->Attributes & OBJ_INHERIT) != 0;
1855 wine_server_add_data( req, oa->ObjectName->Buffer + 4,
1856 oa->ObjectName->Length - 4 * sizeof(WCHAR) );
1857 status = wine_server_call( req );
1858 if (!status) *handle = reply->handle;
1864 /******************************************************************
1865 * NtDeleteFile (NTDLL.@)
1869 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
1875 TRACE("%p\n", ObjectAttributes);
1876 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE, ObjectAttributes,
1878 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1879 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
1880 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1884 /******************************************************************
1885 * NtCancelIoFile (NTDLL.@)
1889 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
1891 LARGE_INTEGER timeout;
1893 TRACE("%p %p\n", hFile, io_status );
1895 SERVER_START_REQ( cancel_async )
1897 req->handle = hFile;
1898 wine_server_call( req );
1901 /* Let some APC be run, so that we can run the remaining APCs on hFile
1902 * either the cancelation of the pending one, but also the execution
1903 * of the queued APC, but not yet run. This is needed to ensure proper
1904 * clean-up of allocated data.
1906 timeout.u.LowPart = timeout.u.HighPart = 0;
1907 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
1910 /******************************************************************************
1911 * NtCreateMailslotFile [NTDLL.@]
1912 * ZwCreateMailslotFile [NTDLL.@]
1915 * pHandle [O] pointer to receive the handle created
1916 * DesiredAccess [I] access mode (read, write, etc)
1917 * ObjectAttributes [I] fully qualified NT path of the mailslot
1918 * IoStatusBlock [O] receives completion status and other info
1921 * MaxMessageSize [I]
1927 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
1928 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
1929 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
1930 PLARGE_INTEGER TimeOut)
1932 static const WCHAR leadin[] = {
1933 '\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
1936 TRACE("%p %08lx %p %p %08lx %08lx %08lx %p\n",
1937 pHandle, DesiredAccess, attr, IoStatusBlock,
1938 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
1940 if (attr->ObjectName->Length < sizeof(leadin) ||
1941 strncmpiW( attr->ObjectName->Buffer,
1942 leadin, sizeof(leadin)/sizeof(leadin[0]) ))
1944 return STATUS_OBJECT_NAME_INVALID;
1947 SERVER_START_REQ( create_mailslot )
1949 req->max_msgsize = MaxMessageSize;
1950 req->read_timeout = TimeOut->QuadPart / -10000;
1951 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1952 wine_server_add_data( req, attr->ObjectName->Buffer + 4,
1953 attr->ObjectName->Length - 4*sizeof(WCHAR) );
1954 ret = wine_server_call( req );
1955 if( ret == STATUS_SUCCESS )
1956 *pHandle = reply->handle;