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 a 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 ANSI_STRING unix_name;
136 TRACE("handle=%p access=%08lx name=%s objattr=%08lx root=%p sec=%p io=%p alloc_size=%p\n"
137 "attr=%08lx sharing=%08lx disp=%ld options=%08lx ea=%p.0x%08lx\n",
138 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
139 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
140 attributes, sharing, disposition, options, ea_buffer, ea_length );
142 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
144 if (attr->RootDirectory)
146 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
147 return STATUS_OBJECT_NAME_NOT_FOUND;
149 if (alloc_size) FIXME( "alloc_size not supported\n" );
151 /* check for named pipe */
153 if (attr->ObjectName->Length > sizeof(pipeW) &&
154 !memicmpW( attr->ObjectName->Buffer, pipeW, sizeof(pipeW)/sizeof(WCHAR) ))
156 SERVER_START_REQ( open_named_pipe )
158 req->access = access;
159 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
160 wine_server_add_data( req, attr->ObjectName->Buffer + 4,
161 attr->ObjectName->Length - 4*sizeof(WCHAR) );
162 io->u.Status = wine_server_call( req );
163 *handle = reply->handle;
169 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
170 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
172 if (io->u.Status == STATUS_NO_SUCH_FILE &&
173 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
176 io->u.Status = STATUS_SUCCESS;
179 if (io->u.Status == STATUS_SUCCESS)
181 SERVER_START_REQ( create_file )
183 req->access = access;
184 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
185 req->sharing = sharing;
186 req->create = disposition;
187 req->options = options;
188 req->attrs = attributes;
189 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
190 io->u.Status = wine_server_call( req );
191 *handle = reply->handle;
194 RtlFreeAnsiString( &unix_name );
196 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), io->u.Status );
198 if (io->u.Status == STATUS_SUCCESS)
200 if (created) io->Information = FILE_CREATED;
201 else switch(disposition)
204 io->Information = FILE_SUPERSEDED;
207 io->Information = FILE_CREATED;
211 io->Information = FILE_OPENED;
214 case FILE_OVERWRITE_IF:
215 io->Information = FILE_OVERWRITTEN;
223 /***********************************************************************
224 * Asynchronous file I/O *
226 static void WINAPI FILE_AsyncReadService(void*, PIO_STATUS_BLOCK, ULONG);
227 static void WINAPI FILE_AsyncWriteService(void*, PIO_STATUS_BLOCK, ULONG);
229 typedef struct async_fileio
237 int queue_apc_on_error;
243 static void fileio_terminate(async_fileio *fileio, IO_STATUS_BLOCK* iosb)
245 TRACE("data: %p\n", fileio);
247 wine_server_release_fd( fileio->handle, fileio->fd );
248 if ( fileio->event != INVALID_HANDLE_VALUE )
249 NtSetEvent( fileio->event, NULL );
252 (iosb->u.Status == STATUS_SUCCESS || fileio->queue_apc_on_error))
253 fileio->apc( fileio->apc_user, iosb, iosb->Information );
255 RtlFreeHeap( GetProcessHeap(), 0, fileio );
259 static ULONG fileio_queue_async(async_fileio* fileio, IO_STATUS_BLOCK* iosb,
262 PIO_APC_ROUTINE apc = do_read ? FILE_AsyncReadService : FILE_AsyncWriteService;
265 SERVER_START_REQ( register_async )
267 req->handle = fileio->handle;
270 req->io_user = fileio;
271 req->type = do_read ? ASYNC_TYPE_READ : ASYNC_TYPE_WRITE;
272 req->count = (fileio->count < iosb->Information) ?
273 0 : fileio->count - iosb->Information;
274 status = wine_server_call( req );
278 if ( status ) iosb->u.Status = status;
279 if ( iosb->u.Status != STATUS_PENDING )
281 (apc)( fileio, iosb, iosb->u.Status );
282 return iosb->u.Status;
284 NtCurrentTeb()->num_async_io++;
285 return STATUS_SUCCESS;
288 /***********************************************************************
289 * FILE_GetNtStatus(void)
291 * Retrieve the Nt Status code from errno.
292 * Try to be consistent with FILE_SetDosError().
294 NTSTATUS FILE_GetNtStatus(void)
298 TRACE( "errno = %d\n", errno );
301 case EAGAIN: return STATUS_SHARING_VIOLATION;
302 case EBADF: return STATUS_INVALID_HANDLE;
303 case ENOSPC: return STATUS_DISK_FULL;
306 case EACCES: return STATUS_ACCESS_DENIED;
307 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
308 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
309 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
311 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
312 case EINVAL: return STATUS_INVALID_PARAMETER;
313 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
314 case EPIPE: return STATUS_PIPE_BROKEN;
315 case EIO: return STATUS_DEVICE_NOT_READY;
317 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
320 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
321 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
322 case ENOEXEC: /* ?? */
323 case ESPIPE: /* ?? */
324 case EEXIST: /* ?? */
326 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
327 return STATUS_UNSUCCESSFUL;
331 /***********************************************************************
332 * FILE_AsyncReadService (INTERNAL)
334 * This function is called while the client is waiting on the
335 * server, so we can't make any server calls here.
337 static void WINAPI FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
339 async_fileio *fileio = (async_fileio*)user;
341 int already = iosb->Information;
343 TRACE("%p %p %lu\n", iosb, fileio->buffer, status);
347 case STATUS_ALERTED: /* got some new data */
348 if (iosb->u.Status != STATUS_PENDING) FIXME("unexpected status %08lx\n", iosb->u.Status);
349 /* check to see if the data is ready (non-blocking) */
350 if ( fileio->avail_mode )
351 result = read(fileio->fd, &fileio->buffer[already],
352 fileio->count - already);
355 result = pread(fileio->fd, &fileio->buffer[already],
356 fileio->count - already,
357 fileio->offset + already);
358 if ((result < 0) && (errno == ESPIPE))
359 result = read(fileio->fd, &fileio->buffer[already],
360 fileio->count - already);
365 if (errno == EAGAIN || errno == EINTR)
367 TRACE("Deferred read %d\n", errno);
368 iosb->u.Status = STATUS_PENDING;
370 else /* check to see if the transfer is complete */
371 iosb->u.Status = FILE_GetNtStatus();
373 else if (result == 0)
375 iosb->u.Status = iosb->Information ? STATUS_SUCCESS : STATUS_END_OF_FILE;
379 iosb->Information += result;
380 if (iosb->Information >= fileio->count || fileio->avail_mode)
381 iosb->u.Status = STATUS_SUCCESS;
384 /* if we only have to read the available data, and none is available,
385 * simply cancel the request. If data was available, it has been read
386 * while in by previous call (NtDelayExecution)
388 iosb->u.Status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
391 TRACE("read %d more bytes %ld/%d so far (%s)\n",
392 result, iosb->Information, fileio->count,
393 (iosb->u.Status == STATUS_SUCCESS) ? "success" : "pending");
395 /* queue another async operation ? */
396 if (iosb->u.Status == STATUS_PENDING)
397 fileio_queue_async(fileio, iosb, TRUE);
399 fileio_terminate(fileio, iosb);
402 iosb->u.Status = status;
403 fileio_terminate(fileio, iosb);
409 /******************************************************************************
410 * NtReadFile [NTDLL.@]
411 * ZwReadFile [NTDLL.@]
413 * Read from an open file handle.
416 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
417 * Event [I] Event to signal upon completion (or NULL)
418 * ApcRoutine [I] Callback to call upon completion (or NULL)
419 * ApcContext [I] Context for ApcRoutine (or NULL)
420 * IoStatusBlock [O] Receives information about the operation on return
421 * Buffer [O] Destination for the data read
422 * Length [I] Size of Buffer
423 * ByteOffset [O] Destination for the new file pointer position (or NULL)
424 * Key [O] Function unknown (may be NULL)
427 * Success: 0. IoStatusBlock is updated, and the Information member contains
428 * The number of bytes read.
429 * Failure: An NTSTATUS error code describing the error.
431 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
432 PIO_APC_ROUTINE apc, void* apc_user,
433 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
434 PLARGE_INTEGER offset, PULONG key)
436 int unix_handle, flags;
438 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
439 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
441 io_status->Information = 0;
442 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_READ, &unix_handle, &flags );
443 if (io_status->u.Status) return io_status->u.Status;
445 if (flags & FD_FLAG_RECV_SHUTDOWN)
447 wine_server_release_fd( hFile, unix_handle );
448 return STATUS_PIPE_DISCONNECTED;
451 if (flags & FD_FLAG_TIMEOUT)
455 /* this shouldn't happen, but check it */
456 FIXME("NIY-hEvent\n");
457 wine_server_release_fd( hFile, unix_handle );
458 return STATUS_NOT_IMPLEMENTED;
460 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
461 if (io_status->u.Status)
463 wine_server_release_fd( hFile, unix_handle );
464 return io_status->u.Status;
468 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
470 async_fileio* fileio;
473 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
475 wine_server_release_fd( hFile, unix_handle );
476 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
477 return STATUS_NO_MEMORY;
479 fileio->handle = hFile;
480 fileio->count = length;
481 if ( offset == NULL )
485 fileio->offset = offset->QuadPart;
486 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
487 FIXME("High part of offset is lost\n");
490 fileio->apc_user = apc_user;
491 fileio->buffer = buffer;
492 fileio->queue_apc_on_error = 0;
493 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
494 fileio->fd = unix_handle; /* FIXME */
495 fileio->event = hEvent;
496 NtResetEvent(hEvent, NULL);
498 io_status->u.Status = STATUS_PENDING;
499 ret = fileio_queue_async(fileio, io_status, TRUE);
500 if (ret != STATUS_SUCCESS)
502 wine_server_release_fd( hFile, unix_handle );
503 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
504 RtlFreeHeap(GetProcessHeap(), 0, fileio);
507 if (flags & FD_FLAG_TIMEOUT)
509 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
511 if (ret != STATUS_USER_APC)
512 fileio->queue_apc_on_error = 1;
516 LARGE_INTEGER timeout;
518 /* let some APC be run, this will read some already pending data */
519 timeout.u.LowPart = timeout.u.HighPart = 0;
520 ret = NtDelayExecution( TRUE, &timeout );
521 /* the apc didn't run and therefore the completion routine now
522 * needs to be sent errors.
523 * Note that there is no race between setting this flag and
524 * returning errors because apc's are run only during alertable
526 if (ret != STATUS_USER_APC)
527 fileio->queue_apc_on_error = 1;
529 TRACE("= 0x%08lx\n", io_status->u.Status);
530 return io_status->u.Status;
535 FILE_POSITION_INFORMATION fpi;
537 fpi.CurrentByteOffset = *offset;
538 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
539 FilePositionInformation);
540 if (io_status->u.Status)
542 wine_server_release_fd( hFile, unix_handle );
543 return io_status->u.Status;
546 /* code for synchronous reads */
547 while ((io_status->Information = read( unix_handle, buffer, length )) == -1)
549 if ((errno == EAGAIN) || (errno == EINTR)) continue;
552 io_status->Information = 0;
553 io_status->u.Status = STATUS_ACCESS_VIOLATION;
555 else io_status->u.Status = FILE_GetNtStatus();
558 wine_server_release_fd( hFile, unix_handle );
559 TRACE("= 0x%08lx\n", io_status->u.Status);
560 return io_status->u.Status;
563 /***********************************************************************
564 * FILE_AsyncWriteService (INTERNAL)
566 * This function is called while the client is waiting on the
567 * server, so we can't make any server calls here.
569 static void WINAPI FILE_AsyncWriteService(void *ovp, IO_STATUS_BLOCK *iosb, ULONG status)
571 async_fileio *fileio = (async_fileio *) ovp;
573 int already = iosb->Information;
575 TRACE("(%p %p %lu)\n",iosb, fileio->buffer, status);
580 /* write some data (non-blocking) */
581 if ( fileio->avail_mode )
582 result = write(fileio->fd, &fileio->buffer[already],
583 fileio->count - already);
586 result = pwrite(fileio->fd, &fileio->buffer[already],
587 fileio->count - already, fileio->offset + already);
588 if ((result < 0) && (errno == ESPIPE))
589 result = write(fileio->fd, &fileio->buffer[already],
590 fileio->count - already);
595 if (errno == EAGAIN || errno == EINTR) iosb->u.Status = STATUS_PENDING;
596 else iosb->u.Status = FILE_GetNtStatus();
600 iosb->Information += result;
601 iosb->u.Status = (iosb->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
602 TRACE("wrote %d more bytes %ld/%d so far\n",
603 result, iosb->Information, fileio->count);
605 if (iosb->u.Status == STATUS_PENDING)
606 fileio_queue_async(fileio, iosb, FALSE);
609 iosb->u.Status = status;
610 fileio_terminate(fileio, iosb);
615 /******************************************************************************
616 * NtWriteFile [NTDLL.@]
617 * ZwWriteFile [NTDLL.@]
619 * Write to an open file handle.
622 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
623 * Event [I] Event to signal upon completion (or NULL)
624 * ApcRoutine [I] Callback to call upon completion (or NULL)
625 * ApcContext [I] Context for ApcRoutine (or NULL)
626 * IoStatusBlock [O] Receives information about the operation on return
627 * Buffer [I] Source for the data to write
628 * Length [I] Size of Buffer
629 * ByteOffset [O] Destination for the new file pointer position (or NULL)
630 * Key [O] Function unknown (may be NULL)
633 * Success: 0. IoStatusBlock is updated, and the Information member contains
634 * The number of bytes written.
635 * Failure: An NTSTATUS error code describing the error.
637 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
638 PIO_APC_ROUTINE apc, void* apc_user,
639 PIO_STATUS_BLOCK io_status,
640 const void* buffer, ULONG length,
641 PLARGE_INTEGER offset, PULONG key)
643 int unix_handle, flags;
645 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p)!\n",
646 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
648 io_status->Information = 0;
649 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_WRITE, &unix_handle, &flags );
650 if (io_status->u.Status) return io_status->u.Status;
652 if (flags & FD_FLAG_SEND_SHUTDOWN)
654 wine_server_release_fd( hFile, unix_handle );
655 return STATUS_PIPE_DISCONNECTED;
658 if (flags & FD_FLAG_TIMEOUT)
662 /* this shouldn't happen, but check it */
663 FIXME("NIY-hEvent\n");
664 wine_server_release_fd( hFile, unix_handle );
665 return STATUS_NOT_IMPLEMENTED;
667 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
668 if (io_status->u.Status)
670 wine_server_release_fd( hFile, unix_handle );
671 return io_status->u.Status;
675 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
677 async_fileio* fileio;
680 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
682 wine_server_release_fd( hFile, unix_handle );
683 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
684 return STATUS_NO_MEMORY;
686 fileio->handle = hFile;
687 fileio->count = length;
690 fileio->offset = offset->QuadPart;
691 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
692 FIXME("High part of offset is lost\n");
699 fileio->apc_user = apc_user;
700 fileio->buffer = (void*)buffer;
701 fileio->queue_apc_on_error = 0;
702 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
703 fileio->fd = unix_handle; /* FIXME */
704 fileio->event = hEvent;
705 NtResetEvent(hEvent, NULL);
707 io_status->Information = 0;
708 io_status->u.Status = STATUS_PENDING;
709 ret = fileio_queue_async(fileio, io_status, FALSE);
710 if (ret != STATUS_SUCCESS)
712 wine_server_release_fd( hFile, unix_handle );
713 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
714 RtlFreeHeap(GetProcessHeap(), 0, fileio);
717 if (flags & FD_FLAG_TIMEOUT)
719 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
721 if (ret != STATUS_USER_APC)
722 fileio->queue_apc_on_error = 1;
726 LARGE_INTEGER timeout;
728 /* let some APC be run, this will write as much data as possible */
729 timeout.u.LowPart = timeout.u.HighPart = 0;
730 ret = NtDelayExecution( TRUE, &timeout );
731 /* the apc didn't run and therefore the completion routine now
732 * needs to be sent errors.
733 * Note that there is no race between setting this flag and
734 * returning errors because apc's are run only during alertable
736 if (ret != STATUS_USER_APC)
737 fileio->queue_apc_on_error = 1;
739 return io_status->u.Status;
744 FILE_POSITION_INFORMATION fpi;
746 fpi.CurrentByteOffset = *offset;
747 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
748 FilePositionInformation);
749 if (io_status->u.Status)
751 wine_server_release_fd( hFile, unix_handle );
752 return io_status->u.Status;
756 /* synchronous file write */
757 while ((io_status->Information = write( unix_handle, buffer, length )) == -1)
759 if ((errno == EAGAIN) || (errno == EINTR)) continue;
762 io_status->Information = 0;
763 io_status->u.Status = STATUS_INVALID_USER_BUFFER;
765 else if (errno == ENOSPC) io_status->u.Status = STATUS_DISK_FULL;
766 else io_status->u.Status = FILE_GetNtStatus();
769 wine_server_release_fd( hFile, unix_handle );
770 return io_status->u.Status;
773 /**************************************************************************
774 * NtDeviceIoControlFile [NTDLL.@]
775 * ZwDeviceIoControlFile [NTDLL.@]
777 * Perform an I/O control operation on an open file handle.
780 * DeviceHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
781 * Event [I] Event to signal upon completion (or NULL)
782 * ApcRoutine [I] Callback to call upon completion (or NULL)
783 * ApcContext [I] Context for ApcRoutine (or NULL)
784 * IoStatusBlock [O] Receives information about the operation on return
785 * IoControlCode [I] Control code for the operation to perform
786 * InputBuffer [I] Source for any input data required (or NULL)
787 * InputBufferSize [I] Size of InputBuffer
788 * OutputBuffer [O] Source for any output data returned (or NULL)
789 * OutputBufferSize [I] Size of OutputBuffer
792 * Success: 0. IoStatusBlock is updated.
793 * Failure: An NTSTATUS error code describing the error.
795 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE DeviceHandle, HANDLE hEvent,
796 PIO_APC_ROUTINE UserApcRoutine,
797 PVOID UserApcContext,
798 PIO_STATUS_BLOCK IoStatusBlock,
801 ULONG InputBufferSize,
803 ULONG OutputBufferSize)
805 TRACE("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx)\n",
806 DeviceHandle, hEvent, UserApcRoutine, UserApcContext,
807 IoStatusBlock, IoControlCode,
808 InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize);
810 if (CDROM_DeviceIoControl(DeviceHandle, hEvent,
811 UserApcRoutine, UserApcContext,
812 IoStatusBlock, IoControlCode,
813 InputBuffer, InputBufferSize,
814 OutputBuffer, OutputBufferSize) == STATUS_NO_SUCH_DEVICE)
816 /* it wasn't a CDROM */
817 FIXME("Unimplemented dwIoControlCode=%08lx\n", IoControlCode);
818 IoStatusBlock->u.Status = STATUS_NOT_IMPLEMENTED;
819 IoStatusBlock->Information = 0;
820 if (hEvent) NtSetEvent(hEvent, NULL);
822 return IoStatusBlock->u.Status;
825 /******************************************************************************
826 * NtFsControlFile [NTDLL.@]
827 * ZwFsControlFile [NTDLL.@]
829 NTSTATUS WINAPI NtFsControlFile(
830 IN HANDLE DeviceHandle,
831 IN HANDLE Event OPTIONAL,
832 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
833 IN PVOID ApcContext OPTIONAL,
834 OUT PIO_STATUS_BLOCK IoStatusBlock,
835 IN ULONG IoControlCode,
836 IN PVOID InputBuffer,
837 IN ULONG InputBufferSize,
838 OUT PVOID OutputBuffer,
839 IN ULONG OutputBufferSize)
841 FIXME("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx): stub\n",
842 DeviceHandle,Event,ApcRoutine,ApcContext,IoStatusBlock,IoControlCode,
843 InputBuffer,InputBufferSize,OutputBuffer,OutputBufferSize);
847 /******************************************************************************
848 * NtSetVolumeInformationFile [NTDLL.@]
849 * ZwSetVolumeInformationFile [NTDLL.@]
851 * Set volume information for an open file handle.
854 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
855 * IoStatusBlock [O] Receives information about the operation on return
856 * FsInformation [I] Source for volume information
857 * Length [I] Size of FsInformation
858 * FsInformationClass [I] Type of volume information to set
861 * Success: 0. IoStatusBlock is updated.
862 * Failure: An NTSTATUS error code describing the error.
864 NTSTATUS WINAPI NtSetVolumeInformationFile(
865 IN HANDLE FileHandle,
866 PIO_STATUS_BLOCK IoStatusBlock,
869 FS_INFORMATION_CLASS FsInformationClass)
871 FIXME("(%p,%p,%p,0x%08lx,0x%08x) stub\n",
872 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
876 /******************************************************************************
877 * NtQueryInformationFile [NTDLL.@]
878 * ZwQueryInformationFile [NTDLL.@]
880 * Get information about an open file handle.
883 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
884 * io [O] Receives information about the operation on return
885 * ptr [O] Destination for file information
886 * len [I] Size of FileInformation
887 * class [I] Type of file information to get
890 * Success: 0. IoStatusBlock and FileInformation are updated.
891 * Failure: An NTSTATUS error code describing the error.
893 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
894 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
896 static const size_t info_sizes[] =
899 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
900 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
901 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
902 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
903 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
904 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
905 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
906 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
907 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
908 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
909 0, /* FileLinkInformation */
910 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
911 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
912 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
913 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
914 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
915 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
916 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
917 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
918 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
919 0, /* FileAlternateNameInformation */
920 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
921 0, /* FilePipeInformation */
922 0, /* FilePipeLocalInformation */
923 0, /* FilePipeRemoteInformation */
924 0, /* FileMailslotQueryInformation */
925 0, /* FileMailslotSetInformation */
926 0, /* FileCompressionInformation */
927 0, /* FileObjectIdInformation */
928 0, /* FileCompletionInformation */
929 0, /* FileMoveClusterInformation */
930 0, /* FileQuotaInformation */
931 0, /* FileReparsePointInformation */
932 0, /* FileNetworkOpenInformation */
933 0, /* FileAttributeTagInformation */
934 0 /* FileTrackingInformation */
940 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", hFile, io, ptr, len, class);
944 if (class <= 0 || class >= FileMaximumInformation)
945 return io->u.Status = STATUS_INVALID_INFO_CLASS;
946 if (!info_sizes[class])
948 FIXME("Unsupported class (%d)\n", class);
949 return io->u.Status = STATUS_NOT_IMPLEMENTED;
951 if (len < info_sizes[class])
952 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
954 if ((io->u.Status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
959 case FileBasicInformation:
961 FILE_BASIC_INFORMATION *info = ptr;
963 if (fstat( fd, &st ) == -1)
964 io->u.Status = FILE_GetNtStatus();
965 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
966 io->u.Status = STATUS_INVALID_INFO_CLASS;
969 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
970 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
971 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
972 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
973 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
974 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
975 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
976 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
980 case FileStandardInformation:
982 FILE_STANDARD_INFORMATION *info = ptr;
984 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
987 if ((info->Directory = S_ISDIR(st.st_mode)))
989 info->AllocationSize.QuadPart = 0;
990 info->EndOfFile.QuadPart = 0;
991 info->NumberOfLinks = 1;
992 info->DeletePending = FALSE;
996 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
997 info->EndOfFile.QuadPart = st.st_size;
998 info->NumberOfLinks = st.st_nlink;
999 info->DeletePending = FALSE; /* FIXME */
1004 case FilePositionInformation:
1006 FILE_POSITION_INFORMATION *info = ptr;
1007 off_t res = lseek( fd, 0, SEEK_CUR );
1008 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1009 else info->CurrentByteOffset.QuadPart = res;
1012 case FileInternalInformation:
1014 FILE_INTERNAL_INFORMATION *info = ptr;
1016 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1017 else info->IndexNumber.QuadPart = st.st_ino;
1020 case FileEaInformation:
1022 FILE_EA_INFORMATION *info = ptr;
1026 case FileEndOfFileInformation:
1028 FILE_END_OF_FILE_INFORMATION *info = ptr;
1030 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1031 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1034 case FileAllInformation:
1036 FILE_ALL_INFORMATION *info = ptr;
1038 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1039 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1040 io->u.Status = STATUS_INVALID_INFO_CLASS;
1043 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1045 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1046 info->StandardInformation.AllocationSize.QuadPart = 0;
1047 info->StandardInformation.EndOfFile.QuadPart = 0;
1048 info->StandardInformation.NumberOfLinks = 1;
1049 info->StandardInformation.DeletePending = FALSE;
1053 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1054 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1055 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1056 info->StandardInformation.NumberOfLinks = st.st_nlink;
1057 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1059 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1060 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1061 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1062 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1063 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1064 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1065 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1066 info->EaInformation.EaSize = 0;
1067 info->AccessInformation.AccessFlags = 0; /* FIXME */
1068 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1069 info->ModeInformation.Mode = 0; /* FIXME */
1070 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1071 info->NameInformation.FileNameLength = 0;
1072 io->Information = sizeof(*info) - sizeof(WCHAR);
1077 FIXME("Unsupported class (%d)\n", class);
1078 io->u.Status = STATUS_NOT_IMPLEMENTED;
1081 wine_server_release_fd( hFile, fd );
1082 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1083 return io->u.Status;
1086 /******************************************************************************
1087 * NtSetInformationFile [NTDLL.@]
1088 * ZwSetInformationFile [NTDLL.@]
1090 * Set information about an open file handle.
1093 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1094 * io [O] Receives information about the operation on return
1095 * ptr [I] Source for file information
1096 * len [I] Size of FileInformation
1097 * class [I] Type of file information to set
1100 * Success: 0. io is updated.
1101 * Failure: An NTSTATUS error code describing the error.
1103 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1104 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1108 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", handle, io, ptr, len, class);
1110 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )))
1111 return io->u.Status;
1113 io->u.Status = STATUS_SUCCESS;
1116 case FileBasicInformation:
1117 if (len >= sizeof(FILE_BASIC_INFORMATION))
1120 const FILE_BASIC_INFORMATION *info = ptr;
1122 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1124 ULONGLONG sec, nsec;
1125 struct timeval tv[2];
1127 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1130 tv[0].tv_sec = tv[0].tv_usec = 0;
1131 tv[1].tv_sec = tv[1].tv_usec = 0;
1132 if (!fstat( fd, &st ))
1134 tv[0].tv_sec = st.st_atime;
1135 tv[1].tv_sec = st.st_mtime;
1138 if (info->LastAccessTime.QuadPart)
1140 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1141 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1142 tv[0].tv_usec = (UINT)nsec / 10;
1144 if (info->LastWriteTime.QuadPart)
1146 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1147 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1148 tv[1].tv_usec = (UINT)nsec / 10;
1150 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1153 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1155 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1158 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1160 if (S_ISDIR( st.st_mode))
1161 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1163 st.st_mode &= ~0222; /* clear write permission bits */
1167 /* add write permission only where we already have read permission */
1168 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1170 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1174 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1177 case FilePositionInformation:
1178 if (len >= sizeof(FILE_POSITION_INFORMATION))
1180 const FILE_POSITION_INFORMATION *info = ptr;
1182 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1183 io->u.Status = FILE_GetNtStatus();
1185 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1188 case FileEndOfFileInformation:
1189 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1192 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1194 /* first try normal truncate */
1195 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1197 /* now check for the need to extend the file */
1198 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1200 static const char zero;
1202 /* extend the file one byte beyond the requested size and then truncate it */
1203 /* this should work around ftruncate implementations that can't extend files */
1204 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1205 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1207 io->u.Status = FILE_GetNtStatus();
1209 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1213 FIXME("Unsupported class (%d)\n", class);
1214 io->u.Status = STATUS_NOT_IMPLEMENTED;
1217 wine_server_release_fd( handle, fd );
1218 io->Information = 0;
1219 return io->u.Status;
1223 /******************************************************************************
1224 * NtQueryFullAttributesFile (NTDLL.@)
1226 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1227 FILE_NETWORK_OPEN_INFORMATION *info )
1229 ANSI_STRING unix_name;
1232 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1233 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1237 if (stat( unix_name.Buffer, &st ) == -1)
1238 status = FILE_GetNtStatus();
1239 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1240 status = STATUS_INVALID_INFO_CLASS;
1243 if (S_ISDIR(st.st_mode))
1245 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1246 info->AllocationSize.QuadPart = 0;
1247 info->EndOfFile.QuadPart = 0;
1251 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1252 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1253 info->EndOfFile.QuadPart = st.st_size;
1255 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1256 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1257 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1258 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1259 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1260 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1261 if (DIR_is_hidden_file( attr->ObjectName ))
1262 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1264 RtlFreeAnsiString( &unix_name );
1266 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), status );
1271 /******************************************************************************
1272 * NtQueryAttributesFile (NTDLL.@)
1273 * ZwQueryAttributesFile (NTDLL.@)
1275 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1277 FILE_NETWORK_OPEN_INFORMATION full_info;
1280 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1282 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1283 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1284 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1285 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1286 info->FileAttributes = full_info.FileAttributes;
1292 /******************************************************************************
1293 * NtQueryVolumeInformationFile [NTDLL.@]
1294 * ZwQueryVolumeInformationFile [NTDLL.@]
1296 * Get volume information for an open file handle.
1299 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1300 * io [O] Receives information about the operation on return
1301 * buffer [O] Destination for volume information
1302 * length [I] Size of FsInformation
1303 * info_class [I] Type of volume information to set
1306 * Success: 0. io and buffer are updated.
1307 * Failure: An NTSTATUS error code describing the error.
1309 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1310 PVOID buffer, ULONG length,
1311 FS_INFORMATION_CLASS info_class )
1316 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )) != STATUS_SUCCESS)
1317 return io->u.Status;
1319 io->u.Status = STATUS_NOT_IMPLEMENTED;
1320 io->Information = 0;
1322 switch( info_class )
1324 case FileFsVolumeInformation:
1325 FIXME( "%p: volume info not supported\n", handle );
1327 case FileFsLabelInformation:
1328 FIXME( "%p: label info not supported\n", handle );
1330 case FileFsSizeInformation:
1331 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1332 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1335 FILE_FS_SIZE_INFORMATION *info = buffer;
1337 if (fstat( fd, &st ) < 0)
1339 io->u.Status = FILE_GetNtStatus();
1342 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1344 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1348 /* Linux's fstatvfs is buggy */
1349 #if !defined(linux) || !defined(HAVE_FSTATFS)
1350 struct statvfs stfs;
1352 if (fstatvfs( fd, &stfs ) < 0)
1354 io->u.Status = FILE_GetNtStatus();
1357 info->BytesPerSector = stfs.f_frsize;
1360 if (fstatfs( fd, &stfs ) < 0)
1362 io->u.Status = FILE_GetNtStatus();
1365 info->BytesPerSector = stfs.f_bsize;
1367 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1368 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1369 info->SectorsPerAllocationUnit = 1;
1370 io->Information = sizeof(*info);
1371 io->u.Status = STATUS_SUCCESS;
1375 case FileFsDeviceInformation:
1376 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1377 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1380 FILE_FS_DEVICE_INFORMATION *info = buffer;
1382 info->Characteristics = 0;
1383 if (fstat( fd, &st ) < 0)
1385 io->u.Status = FILE_GetNtStatus();
1388 if (S_ISCHR( st.st_mode ))
1390 info->DeviceType = FILE_DEVICE_UNKNOWN;
1392 switch(major(st.st_rdev))
1395 info->DeviceType = FILE_DEVICE_NULL;
1398 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1401 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1406 else if (S_ISBLK( st.st_mode ))
1408 info->DeviceType = FILE_DEVICE_DISK;
1410 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1412 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1414 else /* regular file or directory */
1416 #if defined(linux) && defined(HAVE_FSTATFS)
1419 /* check for floppy disk */
1420 if (major(st.st_dev) == FLOPPY_MAJOR)
1421 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1423 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1424 switch (stfs.f_type)
1426 case 0x9660: /* iso9660 */
1427 case 0x15013346: /* udf */
1428 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1429 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1431 case 0x6969: /* nfs */
1432 case 0x517B: /* smbfs */
1433 case 0x564c: /* ncpfs */
1434 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1435 info->Characteristics |= FILE_REMOTE_DEVICE;
1437 case 0x01021994: /* tmpfs */
1438 case 0x28cd3d45: /* cramfs */
1439 case 0x1373: /* devfs */
1440 case 0x9fa0: /* procfs */
1441 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1444 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1447 #elif defined (__APPLE__)
1448 # include <IOKit/IOKitLib.h>
1449 # include <CoreFoundation/CFNumber.h> /* for kCFBooleanTrue, kCFBooleanFalse */
1453 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1455 if (fstatfs( fd, &stfs ) < 0) break;
1457 /* stfs.f_type is reserved (always set to 0) so use IOKit */
1458 kern_return_t kernResult = KERN_FAILURE;
1459 mach_port_t masterPort;
1461 char bsdName[6]; /* disk#\0 */
1463 strncpy(bsdName, stfs.f_mntfromname+strlen(_PATH_DEV) , 5);
1466 kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
1468 if (kernResult == KERN_SUCCESS)
1470 CFMutableDictionaryRef matching = IOBSDNameMatching(masterPort, 0, bsdName);
1474 CFMutableDictionaryRef properties;
1475 io_service_t devService = IOServiceGetMatchingService(masterPort, matching);
1477 if (IORegistryEntryCreateCFProperties(devService,
1479 kCFAllocatorDefault, 0) != KERN_SUCCESS)
1482 CFDictionaryGetValue(properties, CFSTR("Removable")),
1484 ) info->Characteristics |= FILE_REMOVABLE_MEDIA;
1487 CFDictionaryGetValue(properties, CFSTR("Writable")),
1489 ) info->Characteristics |= FILE_READ_ONLY_DEVICE;
1492 NB : mounted disk image (.img/.dmg) don't provide specific type
1495 if ( (type = CFDictionaryGetValue(properties, CFSTR("Type"))) )
1497 if ( CFStringCompare(type, CFSTR("CD-ROM"), 0) == kCFCompareEqualTo
1498 || CFStringCompare(type, CFSTR("DVD-ROM"), 0) == kCFCompareEqualTo
1501 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1506 CFRelease(properties);
1511 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1512 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1514 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1516 io->Information = sizeof(*info);
1517 io->u.Status = STATUS_SUCCESS;
1520 case FileFsAttributeInformation:
1521 FIXME( "%p: attribute info not supported\n", handle );
1523 case FileFsControlInformation:
1524 FIXME( "%p: control info not supported\n", handle );
1526 case FileFsFullSizeInformation:
1527 FIXME( "%p: full size info not supported\n", handle );
1529 case FileFsObjectIdInformation:
1530 FIXME( "%p: object id info not supported\n", handle );
1532 case FileFsMaximumInformation:
1533 FIXME( "%p: maximum info not supported\n", handle );
1536 io->u.Status = STATUS_INVALID_PARAMETER;
1539 wine_server_release_fd( handle, fd );
1540 return io->u.Status;
1544 /******************************************************************
1545 * NtFlushBuffersFile (NTDLL.@)
1547 * Flush any buffered data on an open file handle.
1550 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1551 * IoStatusBlock [O] Receives information about the operation on return
1554 * Success: 0. IoStatusBlock is updated.
1555 * Failure: An NTSTATUS error code describing the error.
1557 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1560 HANDLE hEvent = NULL;
1562 SERVER_START_REQ( flush_file )
1564 req->handle = hFile;
1565 ret = wine_server_call( req );
1566 hEvent = reply->event;
1571 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1577 /******************************************************************
1578 * NtLockFile (NTDLL.@)
1582 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1583 PIO_APC_ROUTINE apc, void* apc_user,
1584 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1585 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1592 if (apc || io_status || key)
1594 FIXME("Unimplemented yet parameter\n");
1595 return STATUS_NOT_IMPLEMENTED;
1600 SERVER_START_REQ( lock_file )
1602 req->handle = hFile;
1603 req->offset_low = offset->u.LowPart;
1604 req->offset_high = offset->u.HighPart;
1605 req->count_low = count->u.LowPart;
1606 req->count_high = count->u.HighPart;
1607 req->shared = !exclusive;
1608 req->wait = !dont_wait;
1609 ret = wine_server_call( req );
1610 handle = reply->handle;
1611 async = reply->overlapped;
1614 if (ret != STATUS_PENDING)
1616 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1622 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1623 if (handle) NtClose( handle );
1624 return STATUS_PENDING;
1628 NtWaitForSingleObject( handle, FALSE, NULL );
1635 /* Unix lock conflict, sleep a bit and retry */
1636 time.QuadPart = 100 * (ULONGLONG)10000;
1637 time.QuadPart = -time.QuadPart;
1638 NtDelayExecution( FALSE, &time );
1644 /******************************************************************
1645 * NtUnlockFile (NTDLL.@)
1649 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
1650 PLARGE_INTEGER offset, PLARGE_INTEGER count,
1655 TRACE( "%p %lx%08lx %lx%08lx\n",
1656 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
1658 if (io_status || key)
1660 FIXME("Unimplemented yet parameter\n");
1661 return STATUS_NOT_IMPLEMENTED;
1664 SERVER_START_REQ( unlock_file )
1666 req->handle = hFile;
1667 req->offset_low = offset->u.LowPart;
1668 req->offset_high = offset->u.HighPart;
1669 req->count_low = count->u.LowPart;
1670 req->count_high = count->u.HighPart;
1671 status = wine_server_call( req );
1677 /******************************************************************
1678 * NtCreateNamedPipeFile (NTDLL.@)
1682 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE FileHandle, ULONG DesiredAccess,
1683 POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock,
1684 ULONG ShareAccess, ULONG CreateDisposition, ULONG CreateOptions,
1685 ULONG NamedPipeType, ULONG ReadMode, ULONG CompletionMode,
1686 ULONG MaximumInstances, ULONG InboundQuota, ULONG OutboundQuota,
1687 PLARGE_INTEGER DefaultTimeout)
1689 FIXME("(%p %lx %p %p %lx %ld %lx %ld %ld %ld %ld %ld %ld %p): stub\n",
1690 FileHandle, DesiredAccess, ObjectAttributes, IoStatusBlock,
1691 ShareAccess, CreateDisposition, CreateOptions, NamedPipeType,
1692 ReadMode, CompletionMode, MaximumInstances, InboundQuota,
1693 OutboundQuota, DefaultTimeout);
1694 return STATUS_NOT_IMPLEMENTED;
1697 /******************************************************************
1698 * NtDeleteFile (NTDLL.@)
1702 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
1708 TRACE("%p\n", ObjectAttributes);
1709 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE, ObjectAttributes,
1711 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1712 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
1713 if (status == STATUS_SUCCESS) status = NtClose(hFile);
1717 /******************************************************************
1718 * NtCancelIoFile (NTDLL.@)
1722 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
1724 LARGE_INTEGER timeout;
1726 TRACE("%p %p\n", hFile, io_status );
1728 SERVER_START_REQ( cancel_async )
1730 req->handle = hFile;
1731 wine_server_call( req );
1734 /* Let some APC be run, so that we can run the remaining APCs on hFile
1735 * either the cancelation of the pending one, but also the execution
1736 * of the queued APC, but not yet run. This is needed to ensure proper
1737 * clean-up of allocated data.
1739 timeout.u.LowPart = timeout.u.HighPart = 0;
1740 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );