Stub for DrawDibProfileDisplay.
[wine/multimedia.git] / dlls / ntdll / file.c
blob424ba4ecf2f8ba78599fb9e98f33fb7b1866ca30
1 /*
2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
32 #endif
33 #ifdef HAVE_LINUX_MAJOR_H
34 # include <linux/major.h>
35 #endif
36 #ifdef HAVE_SYS_STATVFS_H
37 # include <sys/statvfs.h>
38 #endif
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 #endif
45 #ifdef HAVE_UTIME_H
46 # include <utime.h>
47 #endif
48 #ifdef STATFS_DEFINED_BY_SYS_VFS
49 # include <sys/vfs.h>
50 #else
51 # ifdef STATFS_DEFINED_BY_SYS_MOUNT
52 # include <sys/mount.h>
53 # else
54 # ifdef STATFS_DEFINED_BY_SYS_STATFS
55 # include <sys/statfs.h>
56 # endif
57 # endif
58 #endif
60 #define NONAMELESSUNION
61 #define NONAMELESSSTRUCT
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
64 #include "wine/server.h"
65 #include "async.h"
66 #include "ntdll_misc.h"
68 #include "winternl.h"
69 #include "winioctl.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.@]
82 * Open a file.
84 * PARAMS
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] Structue 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
92 * RETURNS
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.
111 * PARAMS
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] Undocumented
122 * ea_length [I] Undocumented
124 * RETURNS
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 ANSI_STRING unix_name;
135 int created = FALSE;
137 TRACE("handle=%p access=%08lx name=%s objattr=%08lx root=%p sec=%p io=%p alloc_size=%p\n"
138 "attr=%08lx sharing=%08lx disp=%ld options=%08lx ea=%p.0x%08lx\n",
139 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
140 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
141 attributes, sharing, disposition, options, ea_buffer, ea_length );
143 if (attr->RootDirectory)
145 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
146 return STATUS_OBJECT_NAME_NOT_FOUND;
148 if (alloc_size) FIXME( "alloc_size not supported\n" );
150 /* check for named pipe */
152 if (attr->ObjectName->Length > sizeof(pipeW) &&
153 !memicmpW( attr->ObjectName->Buffer, pipeW, sizeof(pipeW)/sizeof(WCHAR) ))
155 SERVER_START_REQ( open_named_pipe )
157 req->access = access;
158 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
159 wine_server_add_data( req, attr->ObjectName->Buffer + 4,
160 attr->ObjectName->Length - 4*sizeof(WCHAR) );
161 io->u.Status = wine_server_call( req );
162 *handle = reply->handle;
164 SERVER_END_REQ;
165 return io->u.Status;
168 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
169 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
171 if (io->u.Status == STATUS_NO_SUCH_FILE &&
172 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
174 created = TRUE;
175 io->u.Status = STATUS_SUCCESS;
178 if (io->u.Status == STATUS_SUCCESS)
180 SERVER_START_REQ( create_file )
182 req->access = access;
183 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
184 req->sharing = sharing;
185 req->create = disposition;
186 req->options = options;
187 req->attrs = attributes;
188 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
189 io->u.Status = wine_server_call( req );
190 *handle = reply->handle;
192 SERVER_END_REQ;
193 RtlFreeAnsiString( &unix_name );
195 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), io->u.Status );
197 if (io->u.Status == STATUS_SUCCESS)
199 if (created) io->Information = FILE_CREATED;
200 else switch(disposition)
202 case FILE_SUPERSEDE:
203 io->Information = FILE_SUPERSEDED;
204 break;
205 case FILE_CREATE:
206 io->Information = FILE_CREATED;
207 break;
208 case FILE_OPEN:
209 case FILE_OPEN_IF:
210 io->Information = FILE_OPENED;
211 break;
212 case FILE_OVERWRITE:
213 case FILE_OVERWRITE_IF:
214 io->Information = FILE_OVERWRITTEN;
215 break;
219 return io->u.Status;
222 /***********************************************************************
223 * Asynchronous file I/O *
225 static DWORD fileio_get_async_count(const async_private *ovp);
226 static void CALLBACK fileio_call_completion_func(ULONG_PTR data);
227 static void fileio_async_cleanup(async_private *ovp);
229 static async_ops fileio_async_ops =
231 fileio_get_async_count, /* get_count */
232 fileio_call_completion_func, /* call_completion */
233 fileio_async_cleanup /* cleanup */
236 static async_ops fileio_nocomp_async_ops =
238 fileio_get_async_count, /* get_count */
239 NULL, /* call_completion */
240 fileio_async_cleanup /* cleanup */
243 typedef struct async_fileio
245 struct async_private async;
246 PIO_APC_ROUTINE apc;
247 void* apc_user;
248 char *buffer;
249 unsigned int count;
250 unsigned long offset;
251 enum fd_type fd_type;
252 } async_fileio;
254 static DWORD fileio_get_async_count(const struct async_private *ovp)
256 async_fileio *fileio = (async_fileio*) ovp;
258 if (fileio->count < fileio->async.iosb->Information)
259 return 0;
260 return fileio->count - fileio->async.iosb->Information;
263 static void CALLBACK fileio_call_completion_func(ULONG_PTR data)
265 async_fileio *ovp = (async_fileio*) data;
266 TRACE("data: %p\n", ovp);
268 ovp->apc( ovp->apc_user, ovp->async.iosb, ovp->async.iosb->Information );
270 fileio_async_cleanup( &ovp->async );
273 static void fileio_async_cleanup( struct async_private *ovp )
275 RtlFreeHeap( GetProcessHeap(), 0, ovp );
278 /***********************************************************************
279 * FILE_GetNtStatus(void)
281 * Retrieve the Nt Status code from errno.
282 * Try to be consistent with FILE_SetDosError().
284 NTSTATUS FILE_GetNtStatus(void)
286 int err = errno;
288 TRACE( "errno = %d\n", errno );
289 switch (err)
291 case EAGAIN: return STATUS_SHARING_VIOLATION;
292 case EBADF: return STATUS_INVALID_HANDLE;
293 case ENOSPC: return STATUS_DISK_FULL;
294 case EPERM:
295 case EROFS:
296 case EACCES: return STATUS_ACCESS_DENIED;
297 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
298 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
299 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
300 case EMFILE:
301 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
302 case EINVAL: return STATUS_INVALID_PARAMETER;
303 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
304 case EPIPE: return STATUS_PIPE_BROKEN;
305 case EIO: return STATUS_DEVICE_NOT_READY;
306 #ifdef ENOMEDIUM
307 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
308 #endif
309 case ENOTTY:
310 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
311 case ENOEXEC: /* ?? */
312 case ESPIPE: /* ?? */
313 case EEXIST: /* ?? */
314 default:
315 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
316 return STATUS_UNSUCCESSFUL;
320 /***********************************************************************
321 * FILE_AsyncReadService (INTERNAL)
323 * This function is called while the client is waiting on the
324 * server, so we can't make any server calls here.
326 static void FILE_AsyncReadService(async_private *ovp)
328 async_fileio *fileio = (async_fileio*) ovp;
329 IO_STATUS_BLOCK* io_status = fileio->async.iosb;
330 int result;
331 int already = io_status->Information;
333 TRACE("%p %p\n", io_status, fileio->buffer );
335 /* check to see if the data is ready (non-blocking) */
337 if ( fileio->fd_type == FD_TYPE_SOCKET )
338 result = read(ovp->fd, &fileio->buffer[already], fileio->count - already);
339 else
341 result = pread(ovp->fd, &fileio->buffer[already], fileio->count - already,
342 fileio->offset + already);
343 if ((result < 0) && (errno == ESPIPE))
344 result = read(ovp->fd, &fileio->buffer[already], fileio->count - already);
347 if ((result < 0) && ((errno == EAGAIN) || (errno == EINTR)))
349 TRACE("Deferred read %d\n",errno);
350 io_status->u.Status = STATUS_PENDING;
351 return;
354 /* check to see if the transfer is complete */
355 if (result < 0)
357 io_status->u.Status = FILE_GetNtStatus();
358 return;
360 else if (result == 0)
362 io_status->u.Status = io_status->Information ? STATUS_SUCCESS : STATUS_END_OF_FILE;
363 return;
366 io_status->Information += result;
367 if (io_status->Information >= fileio->count || fileio->fd_type == FD_TYPE_SOCKET )
368 io_status->u.Status = STATUS_SUCCESS;
369 else
370 io_status->u.Status = STATUS_PENDING;
372 TRACE("read %d more bytes %ld/%d so far\n",
373 result, io_status->Information, fileio->count);
377 /******************************************************************************
378 * NtReadFile [NTDLL.@]
379 * ZwReadFile [NTDLL.@]
381 * Read from an open file handle.
383 * PARAMS
384 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
385 * Event [I] Event to signal upon completion (or NULL)
386 * ApcRoutine [I] Callback to call upon completion (or NULL)
387 * ApcContext [I] Context for ApcRoutine (or NULL)
388 * IoStatusBlock [O] Receives information about the operation on return
389 * Buffer [O] Destination for the data read
390 * Length [I] Size of Buffer
391 * ByteOffset [O] Destination for the new file pointer position (or NULL)
392 * Key [O] Function unknown (may be NULL)
394 * RETURNS
395 * Success: 0. IoStatusBlock is updated, and the Information member contains
396 * The number of bytes read.
397 * Failure: An NTSTATUS error code describing the error.
399 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
400 PIO_APC_ROUTINE apc, void* apc_user,
401 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
402 PLARGE_INTEGER offset, PULONG key)
404 int unix_handle, flags;
405 enum fd_type type;
407 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
408 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
410 io_status->Information = 0;
411 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_READ, &unix_handle, &type, &flags );
412 if (io_status->u.Status) return io_status->u.Status;
414 if (flags & FD_FLAG_RECV_SHUTDOWN)
416 wine_server_release_fd( hFile, unix_handle );
417 return STATUS_PIPE_DISCONNECTED;
420 if (flags & FD_FLAG_TIMEOUT)
422 if (hEvent)
424 /* this shouldn't happen, but check it */
425 FIXME("NIY-hEvent\n");
426 wine_server_release_fd( hFile, unix_handle );
427 return STATUS_NOT_IMPLEMENTED;
429 io_status->u.Status = NtCreateEvent(&hEvent, SYNCHRONIZE, NULL, 0, 0);
430 if (io_status->u.Status)
432 wine_server_release_fd( hFile, unix_handle );
433 return io_status->u.Status;
437 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
439 async_fileio* ovp;
440 NTSTATUS ret;
442 if (!(ovp = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
444 wine_server_release_fd( hFile, unix_handle );
445 return STATUS_NO_MEMORY;
447 ovp->async.ops = (apc ? &fileio_async_ops : &fileio_nocomp_async_ops );
448 ovp->async.handle = hFile;
449 ovp->async.fd = unix_handle; /* FIXME */
450 ovp->async.type = ASYNC_TYPE_READ;
451 ovp->async.func = FILE_AsyncReadService;
452 ovp->async.event = hEvent;
453 ovp->async.iosb = io_status;
454 ovp->count = length;
455 if ( offset == NULL )
456 ovp->offset = 0;
457 else
459 ovp->offset = offset->u.LowPart;
460 if (offset->u.HighPart) FIXME("NIY-high part\n");
462 ovp->apc = apc;
463 ovp->apc_user = apc_user;
464 ovp->buffer = buffer;
465 ovp->fd_type = type;
467 io_status->Information = 0;
468 ret = register_new_async(&ovp->async);
469 if (ret != STATUS_SUCCESS)
470 return ret;
471 if (flags & FD_FLAG_TIMEOUT)
473 NtWaitForSingleObject(hEvent, TRUE, NULL);
474 NtClose(hEvent);
476 else
478 LARGE_INTEGER timeout;
480 /* let some APC be run, this will read some already pending data */
481 timeout.u.LowPart = timeout.u.HighPart = 0;
482 NtDelayExecution( TRUE, &timeout );
484 return io_status->u.Status;
487 if (offset)
489 FILE_POSITION_INFORMATION fpi;
491 fpi.CurrentByteOffset = *offset;
492 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
493 FilePositionInformation);
494 if (io_status->u.Status)
496 wine_server_release_fd( hFile, unix_handle );
497 return io_status->u.Status;
500 /* code for synchronous reads */
501 while ((io_status->Information = read( unix_handle, buffer, length )) == -1)
503 if ((errno == EAGAIN) || (errno == EINTR)) continue;
504 if (errno == EFAULT) FIXME( "EFAULT handling broken for now\n" );
505 io_status->u.Status = FILE_GetNtStatus();
506 break;
508 wine_server_release_fd( hFile, unix_handle );
509 return io_status->u.Status;
512 /***********************************************************************
513 * FILE_AsyncWriteService (INTERNAL)
515 * This function is called while the client is waiting on the
516 * server, so we can't make any server calls here.
518 static void FILE_AsyncWriteService(struct async_private *ovp)
520 async_fileio *fileio = (async_fileio *) ovp;
521 PIO_STATUS_BLOCK io_status = fileio->async.iosb;
522 int result;
523 int already = io_status->Information;
525 TRACE("(%p %p)\n",io_status,fileio->buffer);
527 /* write some data (non-blocking) */
529 if ( fileio->fd_type == FD_TYPE_SOCKET )
530 result = write(ovp->fd, &fileio->buffer[already], fileio->count - already);
531 else
533 result = pwrite(ovp->fd, &fileio->buffer[already], fileio->count - already,
534 fileio->offset + already);
535 if ((result < 0) && (errno == ESPIPE))
536 result = write(ovp->fd, &fileio->buffer[already], fileio->count - already);
539 if ((result < 0) && ((errno == EAGAIN) || (errno == EINTR)))
541 io_status->u.Status = STATUS_PENDING;
542 return;
545 /* check to see if the transfer is complete */
546 if (result < 0)
548 io_status->u.Status = FILE_GetNtStatus();
549 return;
552 io_status->Information += result;
553 io_status->u.Status = (io_status->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
554 TRACE("wrote %d more bytes %ld/%d so far\n",result,io_status->Information,fileio->count);
557 /******************************************************************************
558 * NtWriteFile [NTDLL.@]
559 * ZwWriteFile [NTDLL.@]
561 * Write to an open file handle.
563 * PARAMS
564 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
565 * Event [I] Event to signal upon completion (or NULL)
566 * ApcRoutine [I] Callback to call upon completion (or NULL)
567 * ApcContext [I] Context for ApcRoutine (or NULL)
568 * IoStatusBlock [O] Receives information about the operation on return
569 * Buffer [I] Source for the data to write
570 * Length [I] Size of Buffer
571 * ByteOffset [O] Destination for the new file pointer position (or NULL)
572 * Key [O] Function unknown (may be NULL)
574 * RETURNS
575 * Success: 0. IoStatusBlock is updated, and the Information member contains
576 * The number of bytes written.
577 * Failure: An NTSTATUS error code describing the error.
579 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
580 PIO_APC_ROUTINE apc, void* apc_user,
581 PIO_STATUS_BLOCK io_status,
582 const void* buffer, ULONG length,
583 PLARGE_INTEGER offset, PULONG key)
585 int unix_handle, flags;
586 enum fd_type type;
588 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p)!\n",
589 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
591 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
592 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
594 io_status->Information = 0;
595 io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_WRITE, &unix_handle, &type, &flags );
596 if (io_status->u.Status) return io_status->u.Status;
598 if (flags & FD_FLAG_SEND_SHUTDOWN)
600 wine_server_release_fd( hFile, unix_handle );
601 return STATUS_PIPE_DISCONNECTED;
604 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
606 async_fileio* ovp;
607 NTSTATUS ret;
609 if (!(ovp = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
611 wine_server_release_fd( hFile, unix_handle );
612 return STATUS_NO_MEMORY;
614 ovp->async.ops = (apc ? &fileio_async_ops : &fileio_nocomp_async_ops );
615 ovp->async.handle = hFile;
616 ovp->async.fd = unix_handle; /* FIXME */
617 ovp->async.type = ASYNC_TYPE_WRITE;
618 ovp->async.func = FILE_AsyncWriteService;
619 ovp->async.event = hEvent;
620 ovp->async.iosb = io_status;
621 ovp->count = length;
622 if (offset) {
623 ovp->offset = offset->u.LowPart;
624 if (offset->u.HighPart) FIXME("NIY-high part\n");
625 } else {
626 ovp->offset = 0;
628 ovp->apc = apc;
629 ovp->apc_user = apc_user;
630 ovp->buffer = (void*)buffer;
631 ovp->fd_type = type;
633 io_status->Information = 0;
634 ret = register_new_async(&ovp->async);
635 if (ret != STATUS_SUCCESS)
636 return ret;
637 if (flags & FD_FLAG_TIMEOUT)
639 NtWaitForSingleObject(hEvent, TRUE, NULL);
640 NtClose(hEvent);
642 else
644 LARGE_INTEGER timeout;
646 /* let some APC be run, this will write as much data as possible */
647 timeout.u.LowPart = timeout.u.HighPart = 0;
648 NtDelayExecution( TRUE, &timeout );
650 return io_status->u.Status;
653 if (offset)
655 FILE_POSITION_INFORMATION fpi;
657 fpi.CurrentByteOffset = *offset;
658 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
659 FilePositionInformation);
660 if (io_status->u.Status)
662 wine_server_release_fd( hFile, unix_handle );
663 return io_status->u.Status;
667 /* synchronous file write */
668 while ((io_status->Information = write( unix_handle, buffer, length )) == -1)
670 if ((errno == EAGAIN) || (errno == EINTR)) continue;
671 if (errno == EFAULT) FIXME( "EFAULT handling broken for now\n" );
672 if (errno == ENOSPC) io_status->u.Status = STATUS_DISK_FULL;
673 else io_status->u.Status = FILE_GetNtStatus();
674 break;
676 wine_server_release_fd( hFile, unix_handle );
677 return io_status->u.Status;
680 /**************************************************************************
681 * NtDeviceIoControlFile [NTDLL.@]
682 * ZwDeviceIoControlFile [NTDLL.@]
684 * Perform an I/O control operation on an open file handle.
686 * PARAMS
687 * DeviceHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
688 * Event [I] Event to signal upon completion (or NULL)
689 * ApcRoutine [I] Callback to call upon completion (or NULL)
690 * ApcContext [I] Context for ApcRoutine (or NULL)
691 * IoStatusBlock [O] Receives information about the operation on return
692 * IoControlCode [I] Control code for the operation to perform
693 * InputBuffer [I] Source for any input data required (or NULL)
694 * InputBufferSize [I] Size of InputBuffer
695 * OutputBuffer [O] Source for any output data returned (or NULL)
696 * OutputBufferSize [I] Size of OutputBuffer
698 * RETURNS
699 * Success: 0. IoStatusBlock is updated.
700 * Failure: An NTSTATUS error code describing the error.
702 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE DeviceHandle, HANDLE hEvent,
703 PIO_APC_ROUTINE UserApcRoutine,
704 PVOID UserApcContext,
705 PIO_STATUS_BLOCK IoStatusBlock,
706 ULONG IoControlCode,
707 PVOID InputBuffer,
708 ULONG InputBufferSize,
709 PVOID OutputBuffer,
710 ULONG OutputBufferSize)
712 TRACE("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx)\n",
713 DeviceHandle, hEvent, UserApcRoutine, UserApcContext,
714 IoStatusBlock, IoControlCode,
715 InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize);
717 if (CDROM_DeviceIoControl(DeviceHandle, hEvent,
718 UserApcRoutine, UserApcContext,
719 IoStatusBlock, IoControlCode,
720 InputBuffer, InputBufferSize,
721 OutputBuffer, OutputBufferSize) == STATUS_NO_SUCH_DEVICE)
723 /* it wasn't a CDROM */
724 FIXME("Unimplemented dwIoControlCode=%08lx\n", IoControlCode);
725 IoStatusBlock->u.Status = STATUS_NOT_IMPLEMENTED;
726 IoStatusBlock->Information = 0;
727 if (hEvent) NtSetEvent(hEvent, NULL);
729 return IoStatusBlock->u.Status;
732 /******************************************************************************
733 * NtFsControlFile [NTDLL.@]
734 * ZwFsControlFile [NTDLL.@]
736 NTSTATUS WINAPI NtFsControlFile(
737 IN HANDLE DeviceHandle,
738 IN HANDLE Event OPTIONAL,
739 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
740 IN PVOID ApcContext OPTIONAL,
741 OUT PIO_STATUS_BLOCK IoStatusBlock,
742 IN ULONG IoControlCode,
743 IN PVOID InputBuffer,
744 IN ULONG InputBufferSize,
745 OUT PVOID OutputBuffer,
746 IN ULONG OutputBufferSize)
748 FIXME("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx): stub\n",
749 DeviceHandle,Event,ApcRoutine,ApcContext,IoStatusBlock,IoControlCode,
750 InputBuffer,InputBufferSize,OutputBuffer,OutputBufferSize);
751 return 0;
754 /******************************************************************************
755 * NtSetVolumeInformationFile [NTDLL.@]
756 * ZwSetVolumeInformationFile [NTDLL.@]
758 * Set volume information for an open file handle.
760 * PARAMS
761 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
762 * IoStatusBlock [O] Receives information about the operation on return
763 * FsInformation [I] Source for volume information
764 * Length [I] Size of FsInformation
765 * FsInformationClass [I] Type of volume information to set
767 * RETURNS
768 * Success: 0. IoStatusBlock is updated.
769 * Failure: An NTSTATUS error code describing the error.
771 NTSTATUS WINAPI NtSetVolumeInformationFile(
772 IN HANDLE FileHandle,
773 PIO_STATUS_BLOCK IoStatusBlock,
774 PVOID FsInformation,
775 ULONG Length,
776 FS_INFORMATION_CLASS FsInformationClass)
778 FIXME("(%p,%p,%p,0x%08lx,0x%08x) stub\n",
779 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
780 return 0;
783 /******************************************************************************
784 * NtQueryInformationFile [NTDLL.@]
785 * ZwQueryInformationFile [NTDLL.@]
787 * Get information about an open file handle.
789 * PARAMS
790 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
791 * io [O] Receives information about the operation on return
792 * ptr [O] Destination for file information
793 * len [I] Size of FileInformation
794 * class [I] Type of file information to get
796 * RETURNS
797 * Success: 0. IoStatusBlock and FileInformation are updated.
798 * Failure: An NTSTATUS error code describing the error.
800 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
801 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
803 static const size_t info_sizes[] =
806 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
807 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
808 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
809 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
810 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
811 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
812 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
813 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
814 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
815 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
816 0, /* FileLinkInformation */
817 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
818 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
819 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
820 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
821 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
822 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
823 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
824 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
825 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
826 0, /* FileAlternateNameInformation */
827 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
828 0, /* FilePipeInformation */
829 0, /* FilePipeLocalInformation */
830 0, /* FilePipeRemoteInformation */
831 0, /* FileMailslotQueryInformation */
832 0, /* FileMailslotSetInformation */
833 0, /* FileCompressionInformation */
834 0, /* FileObjectIdInformation */
835 0, /* FileCompletionInformation */
836 0, /* FileMoveClusterInformation */
837 0, /* FileQuotaInformation */
838 0, /* FileReparsePointInformation */
839 0, /* FileNetworkOpenInformation */
840 0, /* FileAttributeTagInformation */
841 0 /* FileTrackingInformation */
844 struct stat st;
845 int fd;
847 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", hFile, io, ptr, len, class);
849 io->Information = 0;
851 if (class <= 0 || class >= FileMaximumInformation)
852 return io->u.Status = STATUS_INVALID_INFO_CLASS;
853 if (!info_sizes[class])
855 FIXME("Unsupported class (%d)\n", class);
856 return io->u.Status = STATUS_NOT_IMPLEMENTED;
858 if (len < info_sizes[class])
859 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
861 if ((io->u.Status = wine_server_handle_to_fd( hFile, 0, &fd, NULL, NULL )))
862 return io->u.Status;
864 switch (class)
866 case FileBasicInformation:
868 FILE_BASIC_INFORMATION *info = ptr;
870 if (fstat( fd, &st ) == -1)
871 io->u.Status = FILE_GetNtStatus();
872 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
873 io->u.Status = STATUS_INVALID_INFO_CLASS;
874 else
876 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
877 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
878 if (!(st.st_mode & S_IWUSR)) info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
879 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
880 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
881 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
882 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
885 break;
886 case FileStandardInformation:
888 FILE_STANDARD_INFORMATION *info = ptr;
890 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
891 else
893 if ((info->Directory = S_ISDIR(st.st_mode)))
895 info->AllocationSize.QuadPart = 0;
896 info->EndOfFile.QuadPart = 0;
897 info->NumberOfLinks = 1;
898 info->DeletePending = FALSE;
900 else
902 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
903 info->EndOfFile.QuadPart = st.st_size;
904 info->NumberOfLinks = st.st_nlink;
905 info->DeletePending = FALSE; /* FIXME */
909 break;
910 case FilePositionInformation:
912 FILE_POSITION_INFORMATION *info = ptr;
913 off_t res = lseek( fd, 0, SEEK_CUR );
914 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
915 else info->CurrentByteOffset.QuadPart = res;
917 break;
918 case FileInternalInformation:
920 FILE_INTERNAL_INFORMATION *info = ptr;
922 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
923 else info->IndexNumber.QuadPart = st.st_ino;
925 break;
926 case FileEaInformation:
928 FILE_EA_INFORMATION *info = ptr;
929 info->EaSize = 0;
931 break;
932 case FileEndOfFileInformation:
934 FILE_END_OF_FILE_INFORMATION *info = ptr;
936 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
937 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
939 break;
940 case FileAllInformation:
942 FILE_ALL_INFORMATION *info = ptr;
944 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
945 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
946 io->u.Status = STATUS_INVALID_INFO_CLASS;
947 else
949 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
951 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
952 info->StandardInformation.AllocationSize.QuadPart = 0;
953 info->StandardInformation.EndOfFile.QuadPart = 0;
954 info->StandardInformation.NumberOfLinks = 1;
955 info->StandardInformation.DeletePending = FALSE;
957 else
959 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
960 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
961 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
962 info->StandardInformation.NumberOfLinks = st.st_nlink;
963 info->StandardInformation.DeletePending = FALSE; /* FIXME */
965 if (!(st.st_mode & S_IWUSR))
966 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
967 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
968 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
969 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
970 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
971 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
972 info->EaInformation.EaSize = 0;
973 info->AccessInformation.AccessFlags = 0; /* FIXME */
974 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
975 info->ModeInformation.Mode = 0; /* FIXME */
976 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
977 info->NameInformation.FileNameLength = 0;
978 io->Information = sizeof(*info) - sizeof(WCHAR);
981 break;
982 default:
983 FIXME("Unsupported class (%d)\n", class);
984 io->u.Status = STATUS_NOT_IMPLEMENTED;
985 break;
987 wine_server_release_fd( hFile, fd );
988 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
989 return io->u.Status;
992 /******************************************************************************
993 * NtSetInformationFile [NTDLL.@]
994 * ZwSetInformationFile [NTDLL.@]
996 * Set information about an open file handle.
998 * PARAMS
999 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1000 * io [O] Receives information about the operation on return
1001 * ptr [I] Source for file information
1002 * len [I] Size of FileInformation
1003 * class [I] Type of file information to set
1005 * RETURNS
1006 * Success: 0. io is updated.
1007 * Failure: An NTSTATUS error code describing the error.
1009 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1010 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1012 int fd;
1014 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", handle, io, ptr, len, class);
1016 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL )))
1017 return io->u.Status;
1019 io->u.Status = STATUS_SUCCESS;
1020 switch (class)
1022 case FileBasicInformation:
1023 if (len >= sizeof(FILE_BASIC_INFORMATION))
1025 struct stat st;
1026 const FILE_BASIC_INFORMATION *info = ptr;
1028 #ifdef HAVE_FUTIMES
1029 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1031 ULONGLONG sec, nsec;
1032 struct timeval tv[2];
1034 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1037 tv[0].tv_sec = tv[0].tv_usec = 0;
1038 tv[1].tv_sec = tv[1].tv_usec = 0;
1039 if (!fstat( fd, &st ))
1041 tv[0].tv_sec = st.st_atime;
1042 tv[1].tv_sec = st.st_mtime;
1045 if (info->LastAccessTime.QuadPart)
1047 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1048 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1049 tv[0].tv_usec = (UINT)nsec / 10;
1051 if (info->LastWriteTime.QuadPart)
1053 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1054 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1055 tv[1].tv_usec = (UINT)nsec / 10;
1057 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1059 #endif /* HAVE_FUTIMES */
1060 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1062 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1063 else
1065 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1067 st.st_mode &= ~0222; /* clear write permission bits */
1069 else
1071 /* add write permission only where we already have read permission */
1072 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1074 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1078 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1079 break;
1081 case FilePositionInformation:
1082 if (len >= sizeof(FILE_POSITION_INFORMATION))
1084 const FILE_POSITION_INFORMATION *info = ptr;
1086 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1087 io->u.Status = FILE_GetNtStatus();
1089 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1090 break;
1092 case FileEndOfFileInformation:
1093 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1095 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1097 if (info->EndOfFile.QuadPart > lseek( fd, 0, SEEK_END ))
1099 static const char zero;
1101 /* extend the file one byte beyond the requested size and then truncate it */
1102 /* this should work around ftruncate implementations that can't extend files */
1103 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) == -1)
1105 io->u.Status = FILE_GetNtStatus();
1106 break;
1109 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) == -1)
1110 io->u.Status = FILE_GetNtStatus();
1112 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1113 break;
1115 default:
1116 FIXME("Unsupported class (%d)\n", class);
1117 io->u.Status = STATUS_NOT_IMPLEMENTED;
1118 break;
1120 wine_server_release_fd( handle, fd );
1121 io->Information = 0;
1122 return io->u.Status;
1126 /******************************************************************************
1127 * NtQueryFullAttributesFile (NTDLL.@)
1129 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1130 FILE_NETWORK_OPEN_INFORMATION *info )
1132 ANSI_STRING unix_name;
1133 NTSTATUS status;
1135 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1136 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1138 struct stat st;
1140 if (stat( unix_name.Buffer, &st ) == -1)
1141 status = FILE_GetNtStatus();
1142 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1143 status = STATUS_INVALID_INFO_CLASS;
1144 else
1146 if (S_ISDIR(st.st_mode))
1148 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1149 info->AllocationSize.QuadPart = 0;
1150 info->EndOfFile.QuadPart = 0;
1152 else
1154 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1155 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1156 info->EndOfFile.QuadPart = st.st_size;
1158 if (!(st.st_mode & S_IWUSR)) info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1159 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1160 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1161 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1162 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1163 if (DIR_is_hidden_file( attr->ObjectName ))
1164 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1166 RtlFreeAnsiString( &unix_name );
1168 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), status );
1169 return status;
1173 /******************************************************************************
1174 * NtQueryAttributesFile (NTDLL.@)
1175 * ZwQueryAttributesFile (NTDLL.@)
1177 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1179 FILE_NETWORK_OPEN_INFORMATION full_info;
1180 NTSTATUS status;
1182 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1184 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1185 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1186 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1187 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1188 info->FileAttributes = full_info.FileAttributes;
1190 return status;
1194 /******************************************************************************
1195 * NtQueryVolumeInformationFile [NTDLL.@]
1196 * ZwQueryVolumeInformationFile [NTDLL.@]
1198 * Get volume information for an open file handle.
1200 * PARAMS
1201 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1202 * io [O] Receives information about the operation on return
1203 * buffer [O] Destination for volume information
1204 * length [I] Size of FsInformation
1205 * info_class [I] Type of volume information to set
1207 * RETURNS
1208 * Success: 0. io and buffer are updated.
1209 * Failure: An NTSTATUS error code describing the error.
1211 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1212 PVOID buffer, ULONG length,
1213 FS_INFORMATION_CLASS info_class )
1215 int fd;
1216 struct stat st;
1218 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL )) != STATUS_SUCCESS)
1219 return io->u.Status;
1221 io->u.Status = STATUS_NOT_IMPLEMENTED;
1222 io->Information = 0;
1224 switch( info_class )
1226 case FileFsVolumeInformation:
1227 FIXME( "%p: volume info not supported\n", handle );
1228 break;
1229 case FileFsLabelInformation:
1230 FIXME( "%p: label info not supported\n", handle );
1231 break;
1232 case FileFsSizeInformation:
1233 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1234 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1235 else
1237 FILE_FS_SIZE_INFORMATION *info = buffer;
1238 struct statvfs stvfs;
1240 if (fstat( fd, &st ) < 0)
1242 io->u.Status = FILE_GetNtStatus();
1243 break;
1245 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1247 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1248 break;
1250 if (fstatvfs( fd, &stvfs ) < 0) io->u.Status = FILE_GetNtStatus();
1251 else
1253 info->TotalAllocationUnits.QuadPart = stvfs.f_blocks;
1254 info->AvailableAllocationUnits.QuadPart = stvfs.f_bavail;
1255 info->SectorsPerAllocationUnit = 1;
1256 info->BytesPerSector = stvfs.f_frsize;
1257 io->Information = sizeof(*info);
1258 io->u.Status = STATUS_SUCCESS;
1261 break;
1262 case FileFsDeviceInformation:
1263 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1264 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1265 else
1267 FILE_FS_DEVICE_INFORMATION *info = buffer;
1269 info->Characteristics = 0;
1270 if (fstat( fd, &st ) < 0)
1272 io->u.Status = FILE_GetNtStatus();
1273 break;
1275 if (S_ISCHR( st.st_mode ))
1277 info->DeviceType = FILE_DEVICE_UNKNOWN;
1278 #ifdef linux
1279 switch(major(st.st_rdev))
1281 case MEM_MAJOR:
1282 info->DeviceType = FILE_DEVICE_NULL;
1283 break;
1284 case TTY_MAJOR:
1285 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1286 break;
1287 case LP_MAJOR:
1288 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1289 break;
1291 #endif
1293 else if (S_ISBLK( st.st_mode ))
1295 info->DeviceType = FILE_DEVICE_DISK;
1297 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1299 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1301 else /* regular file or directory */
1303 #if defined(linux) && defined(HAVE_FSTATFS)
1304 struct statfs stfs;
1306 /* check for floppy disk */
1307 if (major(st.st_dev) == FLOPPY_MAJOR)
1308 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1310 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1311 switch (stfs.f_type)
1313 case 0x9660: /* iso9660 */
1314 case 0x15013346: /* udf */
1315 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1316 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1317 break;
1318 case 0x6969: /* nfs */
1319 case 0x517B: /* smbfs */
1320 case 0x564c: /* ncpfs */
1321 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1322 info->Characteristics |= FILE_REMOTE_DEVICE;
1323 break;
1324 case 0x01021994: /* tmpfs */
1325 case 0x28cd3d45: /* cramfs */
1326 case 0x1373: /* devfs */
1327 case 0x9fa0: /* procfs */
1328 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1329 break;
1330 default:
1331 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1332 break;
1334 #else
1335 static int warned;
1336 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1337 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1338 #endif
1339 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1341 io->Information = sizeof(*info);
1342 io->u.Status = STATUS_SUCCESS;
1344 break;
1345 case FileFsAttributeInformation:
1346 FIXME( "%p: attribute info not supported\n", handle );
1347 break;
1348 case FileFsControlInformation:
1349 FIXME( "%p: control info not supported\n", handle );
1350 break;
1351 case FileFsFullSizeInformation:
1352 FIXME( "%p: full size info not supported\n", handle );
1353 break;
1354 case FileFsObjectIdInformation:
1355 FIXME( "%p: object id info not supported\n", handle );
1356 break;
1357 case FileFsMaximumInformation:
1358 FIXME( "%p: maximum info not supported\n", handle );
1359 break;
1360 default:
1361 io->u.Status = STATUS_INVALID_PARAMETER;
1362 break;
1364 wine_server_release_fd( handle, fd );
1365 return io->u.Status;
1369 /******************************************************************
1370 * NtFlushBuffersFile (NTDLL.@)
1372 * Flush any buffered data on an open file handle.
1374 * PARAMS
1375 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1376 * IoStatusBlock [O] Receives information about the operation on return
1378 * RETURNS
1379 * Success: 0. IoStatusBlock is updated.
1380 * Failure: An NTSTATUS error code describing the error.
1382 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1384 NTSTATUS ret;
1385 HANDLE hEvent = NULL;
1387 SERVER_START_REQ( flush_file )
1389 req->handle = hFile;
1390 ret = wine_server_call( req );
1391 hEvent = reply->event;
1393 SERVER_END_REQ;
1394 if (!ret && hEvent)
1396 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1397 NtClose( hEvent );
1399 return ret;
1402 /******************************************************************
1403 * NtLockFile (NTDLL.@)
1407 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1408 PIO_APC_ROUTINE apc, void* apc_user,
1409 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1410 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1411 BOOLEAN exclusive )
1413 NTSTATUS ret;
1414 HANDLE handle;
1415 BOOLEAN async;
1417 if (apc || io_status || key)
1419 FIXME("Unimplemented yet parameter\n");
1420 return STATUS_NOT_IMPLEMENTED;
1423 for (;;)
1425 SERVER_START_REQ( lock_file )
1427 req->handle = hFile;
1428 req->offset_low = offset->u.LowPart;
1429 req->offset_high = offset->u.HighPart;
1430 req->count_low = count->u.LowPart;
1431 req->count_high = count->u.HighPart;
1432 req->shared = !exclusive;
1433 req->wait = !dont_wait;
1434 ret = wine_server_call( req );
1435 handle = reply->handle;
1436 async = reply->overlapped;
1438 SERVER_END_REQ;
1439 if (ret != STATUS_PENDING)
1441 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1442 return ret;
1445 if (async)
1447 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1448 if (handle) NtClose( handle );
1449 return STATUS_PENDING;
1451 if (handle)
1453 NtWaitForSingleObject( handle, FALSE, NULL );
1454 NtClose( handle );
1456 else
1458 LARGE_INTEGER time;
1460 /* Unix lock conflict, sleep a bit and retry */
1461 time.QuadPart = 100 * (ULONGLONG)10000;
1462 time.QuadPart = -time.QuadPart;
1463 NtDelayExecution( FALSE, &time );
1469 /******************************************************************
1470 * NtUnlockFile (NTDLL.@)
1474 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
1475 PLARGE_INTEGER offset, PLARGE_INTEGER count,
1476 PULONG key )
1478 NTSTATUS status;
1480 TRACE( "%p %lx%08lx %lx%08lx\n",
1481 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
1483 if (io_status || key)
1485 FIXME("Unimplemented yet parameter\n");
1486 return STATUS_NOT_IMPLEMENTED;
1489 SERVER_START_REQ( unlock_file )
1491 req->handle = hFile;
1492 req->offset_low = offset->u.LowPart;
1493 req->offset_high = offset->u.HighPart;
1494 req->count_low = count->u.LowPart;
1495 req->count_high = count->u.HighPart;
1496 status = wine_server_call( req );
1498 SERVER_END_REQ;
1499 return status;