Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / ntdll / file.c
blobdbd53b0322d9c1df4001b38b925537113b4660c4
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_POLL_H
49 #include <poll.h>
50 #endif
51 #ifdef HAVE_SYS_POLL_H
52 #include <sys/poll.h>
53 #endif
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
56 #endif
57 #ifdef HAVE_UTIME_H
58 # include <utime.h>
59 #endif
60 #ifdef HAVE_SYS_VFS_H
61 # include <sys/vfs.h>
62 #endif
63 #ifdef HAVE_SYS_MOUNT_H
64 # include <sys/mount.h>
65 #endif
66 #ifdef HAVE_SYS_STATFS_H
67 # include <sys/statfs.h>
68 #endif
70 #define NONAMELESSUNION
71 #define NONAMELESSSTRUCT
72 #include "ntstatus.h"
73 #define WIN32_NO_STATUS
74 #include "wine/unicode.h"
75 #include "wine/debug.h"
76 #include "thread.h"
77 #include "wine/server.h"
78 #include "ntdll_misc.h"
80 #include "winternl.h"
81 #include "winioctl.h"
83 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
85 mode_t FILE_umask = 0;
87 #define SECSPERDAY 86400
88 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
90 /**************************************************************************
91 * NtOpenFile [NTDLL.@]
92 * ZwOpenFile [NTDLL.@]
94 * Open a file.
96 * PARAMS
97 * handle [O] Variable that receives the file handle on return
98 * access [I] Access desired by the caller to the file
99 * attr [I] Structure describing the file to be opened
100 * io [O] Receives details about the result of the operation
101 * sharing [I] Type of shared access the caller requires
102 * options [I] Options for the file open
104 * RETURNS
105 * Success: 0. FileHandle and IoStatusBlock are updated.
106 * Failure: An NTSTATUS error code describing the error.
108 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
109 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
110 ULONG sharing, ULONG options )
112 return NtCreateFile( handle, access, attr, io, NULL, 0,
113 sharing, FILE_OPEN, options, NULL, 0 );
116 /**************************************************************************
117 * NtCreateFile [NTDLL.@]
118 * ZwCreateFile [NTDLL.@]
120 * Either create a new file or directory, or open an existing file, device,
121 * directory or volume.
123 * PARAMS
124 * handle [O] Points to a variable which receives the file handle on return
125 * access [I] Desired access to the file
126 * attr [I] Structure describing the file
127 * io [O] Receives information about the operation on return
128 * alloc_size [I] Initial size of the file in bytes
129 * attributes [I] Attributes to create the file with
130 * sharing [I] Type of shared access the caller would like to the file
131 * disposition [I] Specifies what to do, depending on whether the file already exists
132 * options [I] Options for creating a new file
133 * ea_buffer [I] Pointer to an extended attributes buffer
134 * ea_length [I] Length of ea_buffer
136 * RETURNS
137 * Success: 0. handle and io are updated.
138 * Failure: An NTSTATUS error code describing the error.
140 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
141 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
142 ULONG attributes, ULONG sharing, ULONG disposition,
143 ULONG options, PVOID ea_buffer, ULONG ea_length )
145 ANSI_STRING unix_name;
146 int created = FALSE;
148 TRACE("handle=%p access=%08x name=%s objattr=%08x root=%p sec=%p io=%p alloc_size=%p\n"
149 "attr=%08x sharing=%08x disp=%d options=%08x ea=%p.0x%08x\n",
150 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
151 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
152 attributes, sharing, disposition, options, ea_buffer, ea_length );
154 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
156 if (alloc_size) FIXME( "alloc_size not supported\n" );
158 if (attr->RootDirectory)
160 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
161 return STATUS_OBJECT_NAME_NOT_FOUND;
164 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
165 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
167 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
169 SERVER_START_REQ( open_file_object )
171 req->access = access;
172 req->attributes = attr->Attributes;
173 req->rootdir = attr->RootDirectory;
174 req->sharing = sharing;
175 req->options = options;
176 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
177 io->u.Status = wine_server_call( req );
178 *handle = reply->handle;
180 SERVER_END_REQ;
181 return io->u.Status;
184 if (io->u.Status == STATUS_NO_SUCH_FILE &&
185 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
187 created = TRUE;
188 io->u.Status = STATUS_SUCCESS;
191 if (io->u.Status == STATUS_SUCCESS)
193 SERVER_START_REQ( create_file )
195 req->access = access;
196 req->attributes = attr->Attributes;
197 req->sharing = sharing;
198 req->create = disposition;
199 req->options = options;
200 req->attrs = attributes;
201 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
202 io->u.Status = wine_server_call( req );
203 *handle = reply->handle;
205 SERVER_END_REQ;
207 /* BEGIN CODEWEAVERS HACK */
208 if (created)
210 static const char wininit[] = "WinInit.Ini", sc[] = { ';',';' };
212 if (unix_name.Length >= sizeof(wininit)-1 &&
213 !strcmp( unix_name.Buffer + unix_name.Length - (sizeof(wininit)-1), wininit ))
215 int fd;
216 if (wine_server_handle_to_fd( *handle, FILE_WRITE_DATA, &fd, NULL ) == STATUS_SUCCESS)
218 struct stat st;
219 if (fstat( fd, &st ) != -1 && st.st_size == 0) pwrite( fd, sc, sizeof(sc), 0 );
220 wine_server_release_fd( *handle, fd );
224 /* END CODEWEAVERS HACK */
226 RtlFreeAnsiString( &unix_name );
228 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), io->u.Status );
230 if (io->u.Status == STATUS_SUCCESS)
232 if (created) io->Information = FILE_CREATED;
233 else switch(disposition)
235 case FILE_SUPERSEDE:
236 io->Information = FILE_SUPERSEDED;
237 break;
238 case FILE_CREATE:
239 io->Information = FILE_CREATED;
240 break;
241 case FILE_OPEN:
242 case FILE_OPEN_IF:
243 io->Information = FILE_OPENED;
244 break;
245 case FILE_OVERWRITE:
246 case FILE_OVERWRITE_IF:
247 io->Information = FILE_OVERWRITTEN;
248 break;
252 return io->u.Status;
255 /***********************************************************************
256 * Asynchronous file I/O *
258 static void WINAPI FILE_AsyncReadService(void*, PIO_STATUS_BLOCK, ULONG);
259 static void WINAPI FILE_AsyncWriteService(void*, PIO_STATUS_BLOCK, ULONG);
261 typedef struct async_fileio
263 HANDLE handle;
264 PIO_APC_ROUTINE apc;
265 void* apc_user;
266 char* buffer;
267 unsigned int count;
268 int queue_apc_on_error;
269 BOOL avail_mode;
270 HANDLE event;
271 } async_fileio;
273 static void fileio_terminate(async_fileio *fileio, IO_STATUS_BLOCK* iosb)
275 TRACE("data: %p\n", fileio);
277 if (fileio->apc &&
278 (iosb->u.Status == STATUS_SUCCESS || fileio->queue_apc_on_error))
279 fileio->apc( fileio->apc_user, iosb, iosb->Information );
281 RtlFreeHeap( GetProcessHeap(), 0, fileio );
285 static ULONG fileio_queue_async(async_fileio* fileio, IO_STATUS_BLOCK* iosb,
286 BOOL do_read)
288 PIO_APC_ROUTINE apc = do_read ? FILE_AsyncReadService : FILE_AsyncWriteService;
289 NTSTATUS status;
291 SERVER_START_REQ( register_async )
293 req->handle = fileio->handle;
294 req->async.callback = apc;
295 req->async.iosb = iosb;
296 req->async.arg = fileio;
297 req->async.event = fileio->event;
298 req->type = do_read ? ASYNC_TYPE_READ : ASYNC_TYPE_WRITE;
299 req->count = (fileio->count < iosb->Information) ?
300 0 : fileio->count - iosb->Information;
301 status = wine_server_call( req );
303 SERVER_END_REQ;
305 if (status != STATUS_PENDING)
307 iosb->u.Status = status;
308 (apc)( fileio, iosb, iosb->u.Status );
310 else NtCurrentTeb()->num_async_io++;
311 return status;
314 /***********************************************************************
315 * FILE_GetNtStatus(void)
317 * Retrieve the Nt Status code from errno.
318 * Try to be consistent with FILE_SetDosError().
320 NTSTATUS FILE_GetNtStatus(void)
322 int err = errno;
324 TRACE( "errno = %d\n", errno );
325 switch (err)
327 case EAGAIN: return STATUS_SHARING_VIOLATION;
328 case EBADF: return STATUS_INVALID_HANDLE;
329 case EBUSY: return STATUS_DEVICE_BUSY;
330 case ENOSPC: return STATUS_DISK_FULL;
331 case EPERM:
332 case EROFS:
333 case EACCES: return STATUS_ACCESS_DENIED;
334 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
335 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
336 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
337 case EMFILE:
338 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
339 case EINVAL: return STATUS_INVALID_PARAMETER;
340 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
341 case EPIPE: return STATUS_PIPE_BROKEN;
342 case EIO: return STATUS_DEVICE_NOT_READY;
343 #ifdef ENOMEDIUM
344 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
345 #endif
346 case ENXIO: return STATUS_NO_SUCH_DEVICE;
347 case ENOTTY:
348 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
349 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
350 case EFAULT: return STATUS_ACCESS_VIOLATION;
351 case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
352 case ENOEXEC: /* ?? */
353 case EEXIST: /* ?? */
354 default:
355 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
356 return STATUS_UNSUCCESSFUL;
360 /***********************************************************************
361 * FILE_AsyncReadService (INTERNAL)
363 static void WINAPI FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
365 async_fileio *fileio = (async_fileio*)user;
366 int fd, needs_close, result;
367 int already = iosb->Information;
369 TRACE("%p %p 0x%x\n", iosb, fileio->buffer, status);
371 switch (status)
373 case STATUS_ALERTED: /* got some new data */
374 if (iosb->u.Status != STATUS_PENDING) FIXME("unexpected status %08x\n", iosb->u.Status);
375 /* check to see if the data is ready (non-blocking) */
376 if ((iosb->u.Status = server_get_unix_fd( fileio->handle, FILE_READ_DATA, &fd,
377 &needs_close, NULL, NULL )))
379 fileio_terminate(fileio, iosb);
380 break;
382 result = read(fd, &fileio->buffer[already], fileio->count - already);
383 if (needs_close) close( fd );
385 if (result < 0)
387 if (errno == EAGAIN || errno == EINTR)
389 TRACE("Deferred read %d\n", errno);
390 iosb->u.Status = STATUS_PENDING;
392 else /* check to see if the transfer is complete */
393 iosb->u.Status = FILE_GetNtStatus();
395 else if (result == 0)
397 iosb->u.Status = iosb->Information ? STATUS_SUCCESS : STATUS_END_OF_FILE;
399 else
401 iosb->Information += result;
402 if (iosb->Information >= fileio->count || fileio->avail_mode)
403 iosb->u.Status = STATUS_SUCCESS;
404 else
406 /* if we only have to read the available data, and none is available,
407 * simply cancel the request. If data was available, it has been read
408 * while in by previous call (NtDelayExecution)
410 iosb->u.Status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
413 TRACE("read %d more bytes %ld/%d so far (%s)\n",
414 result, iosb->Information, fileio->count,
415 (iosb->u.Status == STATUS_SUCCESS) ? "success" : "pending");
417 /* queue another async operation ? */
418 if (iosb->u.Status == STATUS_PENDING)
419 fileio_queue_async(fileio, iosb, TRUE);
420 else
421 fileio_terminate(fileio, iosb);
422 break;
423 default:
424 iosb->u.Status = status;
425 fileio_terminate(fileio, iosb);
426 break;
431 /******************************************************************************
432 * NtReadFile [NTDLL.@]
433 * ZwReadFile [NTDLL.@]
435 * Read from an open file handle.
437 * PARAMS
438 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
439 * Event [I] Event to signal upon completion (or NULL)
440 * ApcRoutine [I] Callback to call upon completion (or NULL)
441 * ApcContext [I] Context for ApcRoutine (or NULL)
442 * IoStatusBlock [O] Receives information about the operation on return
443 * Buffer [O] Destination for the data read
444 * Length [I] Size of Buffer
445 * ByteOffset [O] Destination for the new file pointer position (or NULL)
446 * Key [O] Function unknown (may be NULL)
448 * RETURNS
449 * Success: 0. IoStatusBlock is updated, and the Information member contains
450 * The number of bytes read.
451 * Failure: An NTSTATUS error code describing the error.
453 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
454 PIO_APC_ROUTINE apc, void* apc_user,
455 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
456 PLARGE_INTEGER offset, PULONG key)
458 int result, unix_handle, needs_close, flags;
459 enum server_fd_type type;
461 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p),partial stub!\n",
462 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
464 if (!io_status) return STATUS_ACCESS_VIOLATION;
466 io_status->Information = 0;
467 io_status->u.Status = server_get_unix_fd( hFile, FILE_READ_DATA, &unix_handle,
468 &needs_close, &type, &flags );
469 if (io_status->u.Status) return io_status->u.Status;
471 if (type == FD_TYPE_FILE && offset)
473 /* async I/O doesn't make sense on regular files */
474 if (flags & FD_FLAG_OVERLAPPED)
476 while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
478 if (errno == EINTR) continue;
479 if (errno == EAGAIN || errno == ESPIPE)
481 io_status->u.Status = STATUS_PENDING;
482 break;
484 result = 0;
485 io_status->u.Status = FILE_GetNtStatus();
486 goto done;
488 if (result >= 0)
490 io_status->Information = result;
491 io_status->u.Status = result ? STATUS_SUCCESS : STATUS_END_OF_FILE;
492 if (hEvent) NtSetEvent( hEvent, NULL );
493 if (apc && !io_status->u.Status) apc( apc_user, io_status, io_status->Information );
494 goto done;
497 else
499 if (lseek( unix_handle, offset->QuadPart, SEEK_SET ) == (off_t)-1)
501 io_status->u.Status = FILE_GetNtStatus();
502 goto done;
507 if (flags & FD_FLAG_RECV_SHUTDOWN)
509 if (needs_close) close( unix_handle );
510 return STATUS_PIPE_DISCONNECTED;
513 if (flags & FD_FLAG_TIMEOUT)
515 if (hEvent)
517 /* this shouldn't happen, but check it */
518 FIXME("NIY-hEvent\n");
519 if (needs_close) close( unix_handle );
520 return STATUS_NOT_IMPLEMENTED;
522 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
523 if (io_status->u.Status)
525 if (needs_close) close( unix_handle );
526 return io_status->u.Status;
530 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
532 async_fileio* fileio;
533 NTSTATUS ret;
535 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
537 if (needs_close) close( unix_handle );
538 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
539 return STATUS_NO_MEMORY;
541 fileio->handle = hFile;
542 fileio->count = length;
543 fileio->apc = apc;
544 fileio->apc_user = apc_user;
545 fileio->buffer = buffer;
546 fileio->queue_apc_on_error = 0;
547 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
548 fileio->event = hEvent;
549 if (needs_close) close( unix_handle );
551 io_status->u.Status = STATUS_PENDING;
552 ret = fileio_queue_async(fileio, io_status, TRUE);
553 if (ret != STATUS_PENDING)
555 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
556 return ret;
558 if (flags & FD_FLAG_TIMEOUT)
562 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
564 while (ret == STATUS_USER_APC && io_status->u.Status == STATUS_PENDING);
565 NtClose(hEvent);
566 if (ret != STATUS_USER_APC)
567 fileio->queue_apc_on_error = 1;
569 else
571 LARGE_INTEGER timeout;
573 /* let some APC be run, this will read some already pending data */
574 timeout.u.LowPart = timeout.u.HighPart = 0;
575 ret = NtDelayExecution( TRUE, &timeout );
576 /* the apc didn't run and therefore the completion routine now
577 * needs to be sent errors.
578 * Note that there is no race between setting this flag and
579 * returning errors because apc's are run only during alertable
580 * waits */
581 if (ret != STATUS_USER_APC)
582 fileio->queue_apc_on_error = 1;
584 TRACE("= 0x%08x\n", io_status->u.Status);
585 return io_status->u.Status;
588 /* code for synchronous reads */
589 while ((result = read( unix_handle, buffer, length )) == -1)
591 if ((errno == EAGAIN) || (errno == EINTR)) continue;
592 io_status->u.Status = FILE_GetNtStatus();
593 result = 0;
594 break;
596 io_status->Information = result;
597 if (io_status->u.Status == STATUS_SUCCESS && io_status->Information == 0)
599 struct stat st;
600 if (fstat( unix_handle, &st ) != -1 && S_ISSOCK( st.st_mode ))
601 io_status->u.Status = STATUS_PIPE_BROKEN;
602 else
603 io_status->u.Status = STATUS_END_OF_FILE;
605 done:
606 if (needs_close) close( unix_handle );
607 TRACE("= 0x%08x (%lu)\n", io_status->u.Status, io_status->Information);
608 return io_status->u.Status;
611 /***********************************************************************
612 * FILE_AsyncWriteService (INTERNAL)
614 static void WINAPI FILE_AsyncWriteService(void *ovp, IO_STATUS_BLOCK *iosb, ULONG status)
616 async_fileio *fileio = (async_fileio *) ovp;
617 int result, fd, needs_close;
618 int already = iosb->Information;
620 TRACE("(%p %p 0x%x)\n",iosb, fileio->buffer, status);
622 switch (status)
624 case STATUS_ALERTED:
625 /* write some data (non-blocking) */
626 if ((iosb->u.Status = server_get_unix_fd( fileio->handle, FILE_WRITE_DATA, &fd,
627 &needs_close, NULL, NULL )))
629 fileio_terminate(fileio, iosb);
630 break;
632 result = write(fd, &fileio->buffer[already], fileio->count - already);
633 if (needs_close) close( fd );
635 if (result < 0)
637 if (errno == EAGAIN || errno == EINTR) iosb->u.Status = STATUS_PENDING;
638 else iosb->u.Status = FILE_GetNtStatus();
640 else
642 iosb->Information += result;
643 iosb->u.Status = (iosb->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
644 TRACE("wrote %d more bytes %ld/%d so far\n",
645 result, iosb->Information, fileio->count);
647 if (iosb->u.Status == STATUS_PENDING)
648 fileio_queue_async(fileio, iosb, FALSE);
649 else
650 fileio_terminate(fileio, iosb);
651 break;
652 default:
653 iosb->u.Status = status;
654 fileio_terminate(fileio, iosb);
655 break;
659 /******************************************************************************
660 * NtWriteFile [NTDLL.@]
661 * ZwWriteFile [NTDLL.@]
663 * Write to an open file handle.
665 * PARAMS
666 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
667 * Event [I] Event to signal upon completion (or NULL)
668 * ApcRoutine [I] Callback to call upon completion (or NULL)
669 * ApcContext [I] Context for ApcRoutine (or NULL)
670 * IoStatusBlock [O] Receives information about the operation on return
671 * Buffer [I] Source for the data to write
672 * Length [I] Size of Buffer
673 * ByteOffset [O] Destination for the new file pointer position (or NULL)
674 * Key [O] Function unknown (may be NULL)
676 * RETURNS
677 * Success: 0. IoStatusBlock is updated, and the Information member contains
678 * The number of bytes written.
679 * Failure: An NTSTATUS error code describing the error.
681 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
682 PIO_APC_ROUTINE apc, void* apc_user,
683 PIO_STATUS_BLOCK io_status,
684 const void* buffer, ULONG length,
685 PLARGE_INTEGER offset, PULONG key)
687 int result, unix_handle, needs_close, flags;
688 enum server_fd_type type;
690 TRACE("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%p)!\n",
691 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
693 if (!io_status) return STATUS_ACCESS_VIOLATION;
695 io_status->Information = 0;
696 io_status->u.Status = server_get_unix_fd( hFile, FILE_WRITE_DATA, &unix_handle,
697 &needs_close, &type, &flags );
698 if (io_status->u.Status) return io_status->u.Status;
700 if (type == FD_TYPE_FILE && offset)
702 if (flags & FD_FLAG_OVERLAPPED)
704 /* async I/O doesn't make sense on regular files */
705 while ((result = pwrite( unix_handle, buffer, length, offset->QuadPart )) == -1)
707 if (errno == EINTR) continue;
708 if (errno == EAGAIN || errno == ESPIPE)
710 io_status->u.Status = STATUS_PENDING;
711 break;
713 result = 0;
714 if (errno == EFAULT) io_status->u.Status = STATUS_INVALID_USER_BUFFER;
715 else io_status->u.Status = FILE_GetNtStatus();
716 goto done;
718 if (result >= 0)
720 io_status->Information = result;
721 io_status->u.Status = STATUS_SUCCESS;
722 if (hEvent) NtSetEvent( hEvent, NULL );
723 if (apc) apc( apc_user, io_status, io_status->Information );
724 goto done;
727 else
729 if (lseek( unix_handle, offset->QuadPart, SEEK_SET ) == (off_t)-1)
731 io_status->u.Status = FILE_GetNtStatus();
732 goto done;
737 if (flags & FD_FLAG_SEND_SHUTDOWN)
739 if (needs_close) close( unix_handle );
740 return STATUS_PIPE_DISCONNECTED;
743 if (flags & FD_FLAG_TIMEOUT)
745 if (hEvent)
747 /* this shouldn't happen, but check it */
748 FIXME("NIY-hEvent\n");
749 if (needs_close) close( unix_handle );
750 return STATUS_NOT_IMPLEMENTED;
752 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
753 if (io_status->u.Status)
755 if (needs_close) close( unix_handle );
756 return io_status->u.Status;
760 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
762 async_fileio* fileio;
763 NTSTATUS ret;
765 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
767 if (needs_close) close( unix_handle );
768 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
769 return STATUS_NO_MEMORY;
771 fileio->handle = hFile;
772 fileio->count = length;
773 fileio->apc = apc;
774 fileio->apc_user = apc_user;
775 fileio->buffer = (void*)buffer;
776 fileio->queue_apc_on_error = 0;
777 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
778 fileio->event = hEvent;
779 if (needs_close) close( unix_handle );
781 io_status->Information = 0;
782 io_status->u.Status = STATUS_PENDING;
783 ret = fileio_queue_async(fileio, io_status, FALSE);
784 if (ret != STATUS_PENDING)
786 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
787 return ret;
789 if (flags & FD_FLAG_TIMEOUT)
793 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
795 while (ret == STATUS_USER_APC && io_status->u.Status == STATUS_PENDING);
796 NtClose(hEvent);
797 if (ret != STATUS_USER_APC)
798 fileio->queue_apc_on_error = 1;
800 else
802 LARGE_INTEGER timeout;
804 /* let some APC be run, this will write as much data as possible */
805 timeout.u.LowPart = timeout.u.HighPart = 0;
806 ret = NtDelayExecution( TRUE, &timeout );
807 /* the apc didn't run and therefore the completion routine now
808 * needs to be sent errors.
809 * Note that there is no race between setting this flag and
810 * returning errors because apc's are run only during alertable
811 * waits */
812 if (ret != STATUS_USER_APC)
813 fileio->queue_apc_on_error = 1;
815 return io_status->u.Status;
818 /* synchronous file write */
819 while ((result = write( unix_handle, buffer, length )) == -1)
821 if ((errno == EAGAIN) || (errno == EINTR)) continue;
822 result = 0;
823 if (errno == EFAULT) io_status->u.Status = STATUS_INVALID_USER_BUFFER;
824 else io_status->u.Status = FILE_GetNtStatus();
825 break;
827 io_status->Information = result;
828 done:
829 if (needs_close) close( unix_handle );
830 return io_status->u.Status;
833 /**************************************************************************
834 * NtDeviceIoControlFile [NTDLL.@]
835 * ZwDeviceIoControlFile [NTDLL.@]
837 * Perform an I/O control operation on an open file handle.
839 * PARAMS
840 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
841 * event [I] Event to signal upon completion (or NULL)
842 * apc [I] Callback to call upon completion (or NULL)
843 * apc_context [I] Context for ApcRoutine (or NULL)
844 * io [O] Receives information about the operation on return
845 * code [I] Control code for the operation to perform
846 * in_buffer [I] Source for any input data required (or NULL)
847 * in_size [I] Size of InputBuffer
848 * out_buffer [O] Source for any output data returned (or NULL)
849 * out_size [I] Size of OutputBuffer
851 * RETURNS
852 * Success: 0. IoStatusBlock is updated.
853 * Failure: An NTSTATUS error code describing the error.
855 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
856 PIO_APC_ROUTINE apc, PVOID apc_context,
857 PIO_STATUS_BLOCK io, ULONG code,
858 PVOID in_buffer, ULONG in_size,
859 PVOID out_buffer, ULONG out_size)
861 ULONG device = (code >> 16);
863 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
864 handle, event, apc, apc_context, io, code,
865 in_buffer, in_size, out_buffer, out_size);
867 switch(device)
869 case FILE_DEVICE_DISK:
870 case FILE_DEVICE_CD_ROM:
871 case FILE_DEVICE_DVD:
872 case FILE_DEVICE_CONTROLLER:
873 case FILE_DEVICE_MASS_STORAGE:
874 io->u.Status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
875 in_buffer, in_size, out_buffer, out_size);
876 break;
877 case FILE_DEVICE_SERIAL_PORT:
878 io->u.Status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
879 in_buffer, in_size, out_buffer, out_size);
880 break;
881 case FILE_DEVICE_TAPE:
882 io->u.Status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
883 in_buffer, in_size, out_buffer, out_size);
884 break;
885 default:
886 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
887 code, device, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
888 io->u.Status = STATUS_NOT_SUPPORTED;
889 break;
891 return io->u.Status;
894 /***********************************************************************
895 * pipe_completion_wait (Internal)
897 static void CALLBACK pipe_completion_wait(void *arg, PIO_STATUS_BLOCK iosb, ULONG status)
899 TRACE("for %p, status=%08x\n", iosb, status);
900 iosb->u.Status = status;
903 /**************************************************************************
904 * NtFsControlFile [NTDLL.@]
905 * ZwFsControlFile [NTDLL.@]
907 * Perform a file system control operation on an open file handle.
909 * PARAMS
910 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
911 * event [I] Event to signal upon completion (or NULL)
912 * apc [I] Callback to call upon completion (or NULL)
913 * apc_context [I] Context for ApcRoutine (or NULL)
914 * io [O] Receives information about the operation on return
915 * code [I] Control code for the operation to perform
916 * in_buffer [I] Source for any input data required (or NULL)
917 * in_size [I] Size of InputBuffer
918 * out_buffer [O] Source for any output data returned (or NULL)
919 * out_size [I] Size of OutputBuffer
921 * RETURNS
922 * Success: 0. IoStatusBlock is updated.
923 * Failure: An NTSTATUS error code describing the error.
925 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
926 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
927 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
929 TRACE("(%p,%p,%p,%p,%p,0x%08x,%p,0x%08x,%p,0x%08x)\n",
930 handle, event, apc, apc_context, io, code,
931 in_buffer, in_size, out_buffer, out_size);
933 if (!io) return STATUS_INVALID_PARAMETER;
935 switch(code)
937 case FSCTL_DISMOUNT_VOLUME:
938 io->u.Status = DIR_unmount_device( handle );
939 break;
941 case FSCTL_PIPE_LISTEN:
943 HANDLE internal_event = 0;
945 if(!event && !apc)
947 io->u.Status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
948 if (io->u.Status != STATUS_SUCCESS) return io->u.Status;
950 SERVER_START_REQ(connect_named_pipe)
952 req->handle = handle;
953 req->async.callback = pipe_completion_wait;
954 req->async.iosb = io;
955 req->async.arg = NULL;
956 req->async.apc = apc;
957 req->async.apc_arg = apc_context;
958 req->async.event = event ? event : internal_event;
959 io->u.Status = wine_server_call(req);
961 SERVER_END_REQ;
963 if (internal_event && io->u.Status == STATUS_PENDING)
965 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
967 if (internal_event) NtClose(internal_event);
969 break;
971 case FSCTL_PIPE_WAIT:
973 HANDLE internal_event = 0;
974 FILE_PIPE_WAIT_FOR_BUFFER *buff = in_buffer;
976 if(!event && !apc)
978 io->u.Status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
979 if (io->u.Status != STATUS_SUCCESS) return io->u.Status;
981 SERVER_START_REQ(wait_named_pipe)
983 req->handle = handle;
984 req->timeout = buff->TimeoutSpecified ? buff->Timeout.QuadPart / -10000L
985 : NMPWAIT_USE_DEFAULT_WAIT;
986 req->async.callback = pipe_completion_wait;
987 req->async.iosb = io;
988 req->async.arg = NULL;
989 req->async.apc = apc;
990 req->async.apc_arg = apc_context;
991 req->async.event = event ? event : internal_event;
992 wine_server_add_data( req, buff->Name, buff->NameLength );
993 io->u.Status = wine_server_call( req );
995 SERVER_END_REQ;
997 if (internal_event && io->u.Status == STATUS_PENDING)
999 while (NtWaitForSingleObject(internal_event, TRUE, NULL) == STATUS_USER_APC) /*nothing*/ ;
1001 if (internal_event) NtClose(internal_event);
1003 break;
1005 case FSCTL_PIPE_PEEK:
1007 FILE_PIPE_PEEK_BUFFER *buffer = out_buffer;
1008 int avail = 0, fd, needs_close, flags;
1010 if (out_size < FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ))
1012 io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1013 break;
1016 if ((io->u.Status = server_get_unix_fd( handle, FILE_READ_DATA, &fd,
1017 &needs_close, NULL, &flags )))
1018 break;
1020 if (flags & FD_FLAG_RECV_SHUTDOWN)
1022 if (needs_close) close( fd );
1023 io->u.Status = STATUS_PIPE_DISCONNECTED;
1024 break;
1027 #ifdef FIONREAD
1028 if (ioctl( fd, FIONREAD, &avail ) != 0)
1030 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1031 if (needs_close) close( fd );
1032 io->u.Status = FILE_GetNtStatus();
1033 break;
1035 #endif
1036 if (!avail) /* check for closed pipe */
1038 struct pollfd pollfd;
1039 int ret;
1041 pollfd.fd = fd;
1042 pollfd.events = POLLIN;
1043 pollfd.revents = 0;
1044 ret = poll( &pollfd, 1, 0 );
1045 if (ret == -1 || (ret == 1 && (pollfd.revents & (POLLHUP|POLLERR))))
1047 if (needs_close) close( fd );
1048 io->u.Status = STATUS_PIPE_BROKEN;
1049 break;
1052 buffer->NamedPipeState = 0; /* FIXME */
1053 buffer->ReadDataAvailable = avail;
1054 buffer->NumberOfMessages = 0; /* FIXME */
1055 buffer->MessageLength = 0; /* FIXME */
1056 io->Information = FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1057 io->u.Status = STATUS_SUCCESS;
1058 if (avail)
1060 ULONG data_size = out_size - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1061 if (data_size)
1063 int res = recv( fd, buffer->Data, data_size, MSG_PEEK );
1064 if (res >= 0) io->Information += res;
1067 if (needs_close) close( fd );
1069 break;
1071 case FSCTL_PIPE_DISCONNECT:
1072 SERVER_START_REQ(disconnect_named_pipe)
1074 req->handle = handle;
1075 io->u.Status = wine_server_call(req);
1076 if (!io->u.Status)
1078 int fd = server_remove_fd_from_cache( handle );
1079 if (fd != -1) close( fd );
1082 SERVER_END_REQ;
1083 break;
1085 case FSCTL_LOCK_VOLUME:
1086 case FSCTL_UNLOCK_VOLUME:
1087 FIXME("stub! return success - Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1088 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1089 io->u.Status = STATUS_SUCCESS;
1090 break;
1092 default:
1093 FIXME("Unsupported fsctl %x (device=%x access=%x func=%x method=%x)\n",
1094 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1095 io->u.Status = STATUS_NOT_SUPPORTED;
1096 break;
1098 return io->u.Status;
1101 /******************************************************************************
1102 * NtSetVolumeInformationFile [NTDLL.@]
1103 * ZwSetVolumeInformationFile [NTDLL.@]
1105 * Set volume information for an open file handle.
1107 * PARAMS
1108 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1109 * IoStatusBlock [O] Receives information about the operation on return
1110 * FsInformation [I] Source for volume information
1111 * Length [I] Size of FsInformation
1112 * FsInformationClass [I] Type of volume information to set
1114 * RETURNS
1115 * Success: 0. IoStatusBlock is updated.
1116 * Failure: An NTSTATUS error code describing the error.
1118 NTSTATUS WINAPI NtSetVolumeInformationFile(
1119 IN HANDLE FileHandle,
1120 PIO_STATUS_BLOCK IoStatusBlock,
1121 PVOID FsInformation,
1122 ULONG Length,
1123 FS_INFORMATION_CLASS FsInformationClass)
1125 FIXME("(%p,%p,%p,0x%08x,0x%08x) stub\n",
1126 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1127 return 0;
1130 /******************************************************************************
1131 * NtQueryInformationFile [NTDLL.@]
1132 * ZwQueryInformationFile [NTDLL.@]
1134 * Get information about an open file handle.
1136 * PARAMS
1137 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1138 * io [O] Receives information about the operation on return
1139 * ptr [O] Destination for file information
1140 * len [I] Size of FileInformation
1141 * class [I] Type of file information to get
1143 * RETURNS
1144 * Success: 0. IoStatusBlock and FileInformation are updated.
1145 * Failure: An NTSTATUS error code describing the error.
1147 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1148 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1150 static const size_t info_sizes[] =
1153 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1154 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1155 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1156 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1157 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1158 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1159 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1160 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1161 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1162 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1163 0, /* FileLinkInformation */
1164 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1165 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1166 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1167 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1168 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1169 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1170 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1171 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1172 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1173 0, /* FileAlternateNameInformation */
1174 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1175 0, /* FilePipeInformation */
1176 sizeof(FILE_PIPE_LOCAL_INFORMATION), /* FilePipeLocalInformation */
1177 0, /* FilePipeRemoteInformation */
1178 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1179 0, /* FileMailslotSetInformation */
1180 0, /* FileCompressionInformation */
1181 0, /* FileObjectIdInformation */
1182 0, /* FileCompletionInformation */
1183 0, /* FileMoveClusterInformation */
1184 0, /* FileQuotaInformation */
1185 0, /* FileReparsePointInformation */
1186 0, /* FileNetworkOpenInformation */
1187 0, /* FileAttributeTagInformation */
1188 0 /* FileTrackingInformation */
1191 struct stat st;
1192 int fd, needs_close = FALSE;
1194 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", hFile, io, ptr, len, class);
1196 io->Information = 0;
1198 if (class <= 0 || class >= FileMaximumInformation)
1199 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1200 if (!info_sizes[class])
1202 FIXME("Unsupported class (%d)\n", class);
1203 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1205 if (len < info_sizes[class])
1206 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1208 if (class != FilePipeLocalInformation)
1210 if ((io->u.Status = server_get_unix_fd( hFile, 0, &fd, &needs_close, NULL, NULL )))
1211 return io->u.Status;
1214 switch (class)
1216 case FileBasicInformation:
1218 FILE_BASIC_INFORMATION *info = ptr;
1220 if (fstat( fd, &st ) == -1)
1221 io->u.Status = FILE_GetNtStatus();
1222 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1223 io->u.Status = STATUS_INVALID_INFO_CLASS;
1224 else
1226 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1227 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1228 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1229 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1230 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1231 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1232 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1233 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1236 break;
1237 case FileStandardInformation:
1239 FILE_STANDARD_INFORMATION *info = ptr;
1241 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1242 else
1244 if ((info->Directory = S_ISDIR(st.st_mode)))
1246 info->AllocationSize.QuadPart = 0;
1247 info->EndOfFile.QuadPart = 0;
1248 info->NumberOfLinks = 1;
1249 info->DeletePending = FALSE;
1251 else
1253 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1254 info->EndOfFile.QuadPart = st.st_size;
1255 info->NumberOfLinks = st.st_nlink;
1256 info->DeletePending = FALSE; /* FIXME */
1260 break;
1261 case FilePositionInformation:
1263 FILE_POSITION_INFORMATION *info = ptr;
1264 off_t res = lseek( fd, 0, SEEK_CUR );
1265 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1266 else info->CurrentByteOffset.QuadPart = res;
1268 break;
1269 case FileInternalInformation:
1271 FILE_INTERNAL_INFORMATION *info = ptr;
1273 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1274 else info->IndexNumber.QuadPart = st.st_ino;
1276 break;
1277 case FileEaInformation:
1279 FILE_EA_INFORMATION *info = ptr;
1280 info->EaSize = 0;
1282 break;
1283 case FileEndOfFileInformation:
1285 FILE_END_OF_FILE_INFORMATION *info = ptr;
1287 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1288 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1290 break;
1291 case FileAllInformation:
1293 FILE_ALL_INFORMATION *info = ptr;
1295 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1296 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1297 io->u.Status = STATUS_INVALID_INFO_CLASS;
1298 else
1300 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1302 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1303 info->StandardInformation.AllocationSize.QuadPart = 0;
1304 info->StandardInformation.EndOfFile.QuadPart = 0;
1305 info->StandardInformation.NumberOfLinks = 1;
1306 info->StandardInformation.DeletePending = FALSE;
1308 else
1310 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1311 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1312 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1313 info->StandardInformation.NumberOfLinks = st.st_nlink;
1314 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1316 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1317 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1318 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1319 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1320 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1321 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1322 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1323 info->EaInformation.EaSize = 0;
1324 info->AccessInformation.AccessFlags = 0; /* FIXME */
1325 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1326 info->ModeInformation.Mode = 0; /* FIXME */
1327 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1328 info->NameInformation.FileNameLength = 0;
1329 io->Information = sizeof(*info) - sizeof(WCHAR);
1332 break;
1333 case FileMailslotQueryInformation:
1335 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1337 SERVER_START_REQ( set_mailslot_info )
1339 req->handle = hFile;
1340 req->flags = 0;
1341 io->u.Status = wine_server_call( req );
1342 if( io->u.Status == STATUS_SUCCESS )
1344 info->MaximumMessageSize = reply->max_msgsize;
1345 info->MailslotQuota = 0;
1346 info->NextMessageSize = 0;
1347 info->MessagesAvailable = 0;
1348 info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1351 SERVER_END_REQ;
1352 if (!io->u.Status)
1354 ULONG size = info->MaximumMessageSize ? info->MaximumMessageSize : 0x10000;
1355 char *tmpbuf = RtlAllocateHeap( GetProcessHeap(), 0, size );
1356 if (tmpbuf)
1358 int fd, needs_close;
1359 if (!server_get_unix_fd( hFile, FILE_READ_DATA, &fd, &needs_close, NULL, NULL ))
1361 int res = recv( fd, tmpbuf, size, MSG_PEEK );
1362 info->MessagesAvailable = (res > 0);
1363 info->NextMessageSize = (res >= 0) ? res : MAILSLOT_NO_MESSAGE;
1364 if (needs_close) close( fd );
1366 RtlFreeHeap( GetProcessHeap(), 0, tmpbuf );
1370 break;
1371 case FilePipeLocalInformation:
1373 FILE_PIPE_LOCAL_INFORMATION* pli = ptr;
1375 SERVER_START_REQ( get_named_pipe_info )
1377 req->handle = hFile;
1378 if (!(io->u.Status = wine_server_call( req )))
1380 pli->NamedPipeType = (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) ?
1381 FILE_PIPE_TYPE_MESSAGE : FILE_PIPE_TYPE_BYTE;
1382 pli->NamedPipeConfiguration = 0; /* FIXME */
1383 pli->MaximumInstances = reply->maxinstances;
1384 pli->CurrentInstances = reply->instances;
1385 pli->InboundQuota = reply->insize;
1386 pli->ReadDataAvailable = 0; /* FIXME */
1387 pli->OutboundQuota = reply->outsize;
1388 pli->WriteQuotaAvailable = 0; /* FIXME */
1389 pli->NamedPipeState = 0; /* FIXME */
1390 pli->NamedPipeEnd = (reply->flags & NAMED_PIPE_SERVER_END) ?
1391 FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
1394 SERVER_END_REQ;
1396 break;
1397 default:
1398 FIXME("Unsupported class (%d)\n", class);
1399 io->u.Status = STATUS_NOT_IMPLEMENTED;
1400 break;
1402 if (needs_close) close( fd );
1403 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1404 return io->u.Status;
1407 /******************************************************************************
1408 * NtSetInformationFile [NTDLL.@]
1409 * ZwSetInformationFile [NTDLL.@]
1411 * Set information about an open file handle.
1413 * PARAMS
1414 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1415 * io [O] Receives information about the operation on return
1416 * ptr [I] Source for file information
1417 * len [I] Size of FileInformation
1418 * class [I] Type of file information to set
1420 * RETURNS
1421 * Success: 0. io is updated.
1422 * Failure: An NTSTATUS error code describing the error.
1424 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1425 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1427 int fd, needs_close;
1429 TRACE("(%p,%p,%p,0x%08x,0x%08x)\n", handle, io, ptr, len, class);
1431 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )))
1432 return io->u.Status;
1434 io->u.Status = STATUS_SUCCESS;
1435 switch (class)
1437 case FileBasicInformation:
1438 if (len >= sizeof(FILE_BASIC_INFORMATION))
1440 struct stat st;
1441 const FILE_BASIC_INFORMATION *info = ptr;
1443 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1445 ULONGLONG sec, nsec;
1446 struct timeval tv[2];
1448 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1451 tv[0].tv_sec = tv[0].tv_usec = 0;
1452 tv[1].tv_sec = tv[1].tv_usec = 0;
1453 if (!fstat( fd, &st ))
1455 tv[0].tv_sec = st.st_atime;
1456 tv[1].tv_sec = st.st_mtime;
1459 if (info->LastAccessTime.QuadPart)
1461 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1462 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1463 tv[0].tv_usec = (UINT)nsec / 10;
1465 if (info->LastWriteTime.QuadPart)
1467 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1468 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1469 tv[1].tv_usec = (UINT)nsec / 10;
1471 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1474 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1476 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1477 else
1479 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1481 if (S_ISDIR( st.st_mode))
1482 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1483 else
1484 st.st_mode &= ~0222; /* clear write permission bits */
1486 else
1488 /* add write permission only where we already have read permission */
1489 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1491 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1495 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1496 break;
1498 case FilePositionInformation:
1499 if (len >= sizeof(FILE_POSITION_INFORMATION))
1501 const FILE_POSITION_INFORMATION *info = ptr;
1503 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1504 io->u.Status = FILE_GetNtStatus();
1506 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1507 break;
1509 case FileEndOfFileInformation:
1510 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1512 struct stat st;
1513 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1515 /* first try normal truncate */
1516 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1518 /* now check for the need to extend the file */
1519 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1521 static const char zero;
1523 /* extend the file one byte beyond the requested size and then truncate it */
1524 /* this should work around ftruncate implementations that can't extend files */
1525 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1526 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1528 io->u.Status = FILE_GetNtStatus();
1530 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1531 break;
1533 case FileMailslotSetInformation:
1535 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1537 SERVER_START_REQ( set_mailslot_info )
1539 req->handle = handle;
1540 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1541 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1542 io->u.Status = wine_server_call( req );
1544 SERVER_END_REQ;
1546 break;
1548 default:
1549 FIXME("Unsupported class (%d)\n", class);
1550 io->u.Status = STATUS_NOT_IMPLEMENTED;
1551 break;
1553 if (needs_close) close( fd );
1554 io->Information = 0;
1555 return io->u.Status;
1559 /******************************************************************************
1560 * NtQueryFullAttributesFile (NTDLL.@)
1562 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1563 FILE_NETWORK_OPEN_INFORMATION *info )
1565 ANSI_STRING unix_name;
1566 NTSTATUS status;
1568 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1569 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1571 struct stat st;
1573 if (stat( unix_name.Buffer, &st ) == -1)
1574 status = FILE_GetNtStatus();
1575 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1576 status = STATUS_INVALID_INFO_CLASS;
1577 else
1579 if (S_ISDIR(st.st_mode))
1581 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1582 info->AllocationSize.QuadPart = 0;
1583 info->EndOfFile.QuadPart = 0;
1585 else
1587 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1588 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1589 info->EndOfFile.QuadPart = st.st_size;
1591 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1592 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1593 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1594 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1595 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1596 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1597 if (DIR_is_hidden_file( attr->ObjectName ))
1598 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1600 RtlFreeAnsiString( &unix_name );
1602 else WARN("%s not found (%x)\n", debugstr_us(attr->ObjectName), status );
1603 return status;
1607 /******************************************************************************
1608 * NtQueryAttributesFile (NTDLL.@)
1609 * ZwQueryAttributesFile (NTDLL.@)
1611 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1613 FILE_NETWORK_OPEN_INFORMATION full_info;
1614 NTSTATUS status;
1616 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1618 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1619 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1620 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1621 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1622 info->FileAttributes = full_info.FileAttributes;
1624 return status;
1628 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
1629 /* helper for FILE_GetDeviceInfo to hide some platform differences in fstatfs */
1630 static inline void get_device_info_fstatfs( FILE_FS_DEVICE_INFORMATION *info, const char *fstypename,
1631 size_t fstypesize, unsigned int flags )
1633 if (!strncmp("cd9660", fstypename, fstypesize) ||
1634 !strncmp("udf", fstypename, fstypesize))
1636 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1637 /* Don't assume read-only, let the mount options set it below */
1638 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1640 else if (!strncmp("nfs", fstypename, fstypesize) ||
1641 !strncmp("nwfs", fstypename, fstypesize) ||
1642 !strncmp("smbfs", fstypename, fstypesize) ||
1643 !strncmp("afpfs", fstypename, fstypesize))
1645 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1646 info->Characteristics |= FILE_REMOTE_DEVICE;
1648 else if (!strncmp("procfs", fstypename, fstypesize))
1649 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1650 else
1651 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1653 if (flags & MNT_RDONLY)
1654 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1656 if (!(flags & MNT_LOCAL))
1658 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1659 info->Characteristics |= FILE_REMOTE_DEVICE;
1662 #endif
1664 /******************************************************************************
1665 * get_device_info
1667 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1669 static NTSTATUS get_device_info( int fd, FILE_FS_DEVICE_INFORMATION *info )
1671 struct stat st;
1673 info->Characteristics = 0;
1674 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1675 if (S_ISCHR( st.st_mode ))
1677 info->DeviceType = FILE_DEVICE_UNKNOWN;
1678 #ifdef linux
1679 switch(major(st.st_rdev))
1681 case MEM_MAJOR:
1682 info->DeviceType = FILE_DEVICE_NULL;
1683 break;
1684 case TTY_MAJOR:
1685 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1686 break;
1687 case LP_MAJOR:
1688 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1689 break;
1690 case SCSI_TAPE_MAJOR:
1691 info->DeviceType = FILE_DEVICE_TAPE;
1692 break;
1694 #endif
1696 else if (S_ISBLK( st.st_mode ))
1698 info->DeviceType = FILE_DEVICE_DISK;
1700 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1702 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1704 else /* regular file or directory */
1706 #if defined(linux) && defined(HAVE_FSTATFS)
1707 struct statfs stfs;
1709 /* check for floppy disk */
1710 if (major(st.st_dev) == FLOPPY_MAJOR)
1711 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1713 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1714 switch (stfs.f_type)
1716 case 0x9660: /* iso9660 */
1717 case 0x9fa1: /* supermount */
1718 case 0x15013346: /* udf */
1719 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1720 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1721 break;
1722 case 0x6969: /* nfs */
1723 case 0x517B: /* smbfs */
1724 case 0x564c: /* ncpfs */
1725 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1726 info->Characteristics |= FILE_REMOTE_DEVICE;
1727 break;
1728 case 0x01021994: /* tmpfs */
1729 case 0x28cd3d45: /* cramfs */
1730 case 0x1373: /* devfs */
1731 case 0x9fa0: /* procfs */
1732 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1733 break;
1734 default:
1735 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1736 break;
1738 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
1739 struct statfs stfs;
1741 if (fstatfs( fd, &stfs ) < 0)
1742 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1743 else
1744 get_device_info_fstatfs( info, stfs.f_fstypename,
1745 sizeof(stfs.f_fstypename), stfs.f_flags );
1746 #elif defined(__NetBSD__)
1747 struct statvfs stfs;
1749 if (fstatvfs( fd, &stfs) < 0)
1750 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1751 else
1752 get_device_info_fstatfs( info, stfs.f_fstypename,
1753 sizeof(stfs.f_fstypename), stfs.f_flag );
1754 #elif defined(sun)
1755 /* Use dkio to work out device types */
1757 # include <sys/dkio.h>
1758 # include <sys/vtoc.h>
1759 struct dk_cinfo dkinf;
1760 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1761 if(retval==-1){
1762 WARN("Unable to get disk device type information - assuming a disk like device\n");
1763 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1765 switch (dkinf.dki_ctype)
1767 case DKC_CDROM:
1768 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1769 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1770 break;
1771 case DKC_NCRFLOPPY:
1772 case DKC_SMSFLOPPY:
1773 case DKC_INTEL82072:
1774 case DKC_INTEL82077:
1775 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1776 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1777 break;
1778 case DKC_MD:
1779 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1780 break;
1781 default:
1782 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1785 #else
1786 static int warned;
1787 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1788 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1789 #endif
1790 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1792 return STATUS_SUCCESS;
1796 /******************************************************************************
1797 * NtQueryVolumeInformationFile [NTDLL.@]
1798 * ZwQueryVolumeInformationFile [NTDLL.@]
1800 * Get volume information for an open file handle.
1802 * PARAMS
1803 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1804 * io [O] Receives information about the operation on return
1805 * buffer [O] Destination for volume information
1806 * length [I] Size of FsInformation
1807 * info_class [I] Type of volume information to set
1809 * RETURNS
1810 * Success: 0. io and buffer are updated.
1811 * Failure: An NTSTATUS error code describing the error.
1813 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1814 PVOID buffer, ULONG length,
1815 FS_INFORMATION_CLASS info_class )
1817 int fd, needs_close;
1818 struct stat st;
1820 if ((io->u.Status = server_get_unix_fd( handle, 0, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1821 return io->u.Status;
1823 io->u.Status = STATUS_NOT_IMPLEMENTED;
1824 io->Information = 0;
1826 switch( info_class )
1828 case FileFsVolumeInformation:
1829 FIXME( "%p: volume info not supported\n", handle );
1830 break;
1831 case FileFsLabelInformation:
1832 FIXME( "%p: label info not supported\n", handle );
1833 break;
1834 case FileFsSizeInformation:
1835 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1836 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1837 else
1839 FILE_FS_SIZE_INFORMATION *info = buffer;
1841 if (fstat( fd, &st ) < 0)
1843 io->u.Status = FILE_GetNtStatus();
1844 break;
1846 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1848 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1850 else
1852 /* Linux's fstatvfs is buggy */
1853 #if !defined(linux) || !defined(HAVE_FSTATFS)
1854 struct statvfs stfs;
1856 if (fstatvfs( fd, &stfs ) < 0)
1858 io->u.Status = FILE_GetNtStatus();
1859 break;
1861 info->BytesPerSector = stfs.f_frsize;
1862 #else
1863 struct statfs stfs;
1864 if (fstatfs( fd, &stfs ) < 0)
1866 io->u.Status = FILE_GetNtStatus();
1867 break;
1869 info->BytesPerSector = stfs.f_bsize;
1870 #endif
1871 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1872 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1873 info->SectorsPerAllocationUnit = 1;
1874 io->Information = sizeof(*info);
1875 io->u.Status = STATUS_SUCCESS;
1878 break;
1879 case FileFsDeviceInformation:
1880 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1881 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1882 else
1884 FILE_FS_DEVICE_INFORMATION *info = buffer;
1886 if ((io->u.Status = get_device_info( fd, info )) == STATUS_SUCCESS)
1887 io->Information = sizeof(*info);
1889 break;
1890 case FileFsAttributeInformation:
1891 FIXME( "%p: attribute info not supported\n", handle );
1892 break;
1893 case FileFsControlInformation:
1894 FIXME( "%p: control info not supported\n", handle );
1895 break;
1896 case FileFsFullSizeInformation:
1897 FIXME( "%p: full size info not supported\n", handle );
1898 break;
1899 case FileFsObjectIdInformation:
1900 FIXME( "%p: object id info not supported\n", handle );
1901 break;
1902 case FileFsMaximumInformation:
1903 FIXME( "%p: maximum info not supported\n", handle );
1904 break;
1905 default:
1906 io->u.Status = STATUS_INVALID_PARAMETER;
1907 break;
1909 if (needs_close) close( fd );
1910 return io->u.Status;
1914 /******************************************************************
1915 * NtFlushBuffersFile (NTDLL.@)
1917 * Flush any buffered data on an open file handle.
1919 * PARAMS
1920 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1921 * IoStatusBlock [O] Receives information about the operation on return
1923 * RETURNS
1924 * Success: 0. IoStatusBlock is updated.
1925 * Failure: An NTSTATUS error code describing the error.
1927 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1929 NTSTATUS ret;
1930 HANDLE hEvent = NULL;
1932 SERVER_START_REQ( flush_file )
1934 req->handle = hFile;
1935 ret = wine_server_call( req );
1936 hEvent = reply->event;
1938 SERVER_END_REQ;
1939 if (!ret && hEvent)
1941 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1942 NtClose( hEvent );
1944 return ret;
1947 /******************************************************************
1948 * NtLockFile (NTDLL.@)
1952 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1953 PIO_APC_ROUTINE apc, void* apc_user,
1954 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1955 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1956 BOOLEAN exclusive )
1958 NTSTATUS ret;
1959 HANDLE handle;
1960 BOOLEAN async;
1962 if (apc || io_status || key)
1964 FIXME("Unimplemented yet parameter\n");
1965 return STATUS_NOT_IMPLEMENTED;
1968 for (;;)
1970 SERVER_START_REQ( lock_file )
1972 req->handle = hFile;
1973 req->offset_low = offset->u.LowPart;
1974 req->offset_high = offset->u.HighPart;
1975 req->count_low = count->u.LowPart;
1976 req->count_high = count->u.HighPart;
1977 req->shared = !exclusive;
1978 req->wait = !dont_wait;
1979 ret = wine_server_call( req );
1980 handle = reply->handle;
1981 async = reply->overlapped;
1983 SERVER_END_REQ;
1984 if (ret != STATUS_PENDING)
1986 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1987 return ret;
1990 if (async)
1992 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1993 if (handle) NtClose( handle );
1994 return STATUS_PENDING;
1996 if (handle)
1998 NtWaitForSingleObject( handle, FALSE, NULL );
1999 NtClose( handle );
2001 else
2003 LARGE_INTEGER time;
2005 /* Unix lock conflict, sleep a bit and retry */
2006 time.QuadPart = 100 * (ULONGLONG)10000;
2007 time.QuadPart = -time.QuadPart;
2008 NtDelayExecution( FALSE, &time );
2014 /******************************************************************
2015 * NtUnlockFile (NTDLL.@)
2019 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
2020 PLARGE_INTEGER offset, PLARGE_INTEGER count,
2021 PULONG key )
2023 NTSTATUS status;
2025 TRACE( "%p %x%08x %x%08x\n",
2026 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
2028 if (io_status || key)
2030 FIXME("Unimplemented yet parameter\n");
2031 return STATUS_NOT_IMPLEMENTED;
2034 SERVER_START_REQ( unlock_file )
2036 req->handle = hFile;
2037 req->offset_low = offset->u.LowPart;
2038 req->offset_high = offset->u.HighPart;
2039 req->count_low = count->u.LowPart;
2040 req->count_high = count->u.HighPart;
2041 status = wine_server_call( req );
2043 SERVER_END_REQ;
2044 return status;
2047 /******************************************************************
2048 * NtCreateNamedPipeFile (NTDLL.@)
2052 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
2053 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
2054 ULONG sharing, ULONG dispo, ULONG options,
2055 ULONG pipe_type, ULONG read_mode,
2056 ULONG completion_mode, ULONG max_inst,
2057 ULONG inbound_quota, ULONG outbound_quota,
2058 PLARGE_INTEGER timeout)
2060 NTSTATUS status;
2062 TRACE("(%p %x %s %p %x %d %x %d %d %d %d %d %d %p)\n",
2063 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2064 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2065 outbound_quota, timeout);
2067 /* assume we only get relative timeout, and storable in a DWORD as ms */
2068 if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
2069 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2071 SERVER_START_REQ( create_named_pipe )
2073 req->access = access;
2074 req->attributes = (attr) ? attr->Attributes : 0;
2075 req->rootdir = attr ? attr->RootDirectory : 0;
2076 req->options = options;
2077 req->flags =
2078 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2079 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2080 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2081 req->maxinstances = max_inst;
2082 req->outsize = outbound_quota;
2083 req->insize = inbound_quota;
2084 req->timeout = timeout->QuadPart / -10000;
2085 wine_server_add_data( req, attr->ObjectName->Buffer,
2086 attr->ObjectName->Length );
2087 status = wine_server_call( req );
2088 if (!status) *handle = reply->handle;
2090 SERVER_END_REQ;
2091 return status;
2094 /******************************************************************
2095 * NtDeleteFile (NTDLL.@)
2099 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2101 NTSTATUS status;
2102 HANDLE hFile;
2103 IO_STATUS_BLOCK io;
2105 TRACE("%p\n", ObjectAttributes);
2106 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2107 ObjectAttributes, &io, NULL, 0,
2108 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2109 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2110 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2111 return status;
2114 /******************************************************************
2115 * NtCancelIoFile (NTDLL.@)
2119 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2121 LARGE_INTEGER timeout;
2123 TRACE("%p %p\n", hFile, io_status );
2125 SERVER_START_REQ( cancel_async )
2127 req->handle = hFile;
2128 wine_server_call( req );
2130 SERVER_END_REQ;
2131 /* Let some APC be run, so that we can run the remaining APCs on hFile
2132 * either the cancelation of the pending one, but also the execution
2133 * of the queued APC, but not yet run. This is needed to ensure proper
2134 * clean-up of allocated data.
2136 timeout.u.LowPart = timeout.u.HighPart = 0;
2137 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2140 /******************************************************************************
2141 * NtCreateMailslotFile [NTDLL.@]
2142 * ZwCreateMailslotFile [NTDLL.@]
2144 * PARAMS
2145 * pHandle [O] pointer to receive the handle created
2146 * DesiredAccess [I] access mode (read, write, etc)
2147 * ObjectAttributes [I] fully qualified NT path of the mailslot
2148 * IoStatusBlock [O] receives completion status and other info
2149 * CreateOptions [I]
2150 * MailslotQuota [I]
2151 * MaxMessageSize [I]
2152 * TimeOut [I]
2154 * RETURNS
2155 * An NT status code
2157 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2158 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2159 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2160 PLARGE_INTEGER TimeOut)
2162 LARGE_INTEGER timeout;
2163 NTSTATUS ret;
2165 TRACE("%p %08x %p %p %08x %08x %08x %p\n",
2166 pHandle, DesiredAccess, attr, IoStatusBlock,
2167 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2169 if (!pHandle) return STATUS_ACCESS_VIOLATION;
2170 if (!attr) return STATUS_INVALID_PARAMETER;
2171 if (!attr->ObjectName) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2174 * For a NULL TimeOut pointer set the default timeout value
2176 if (!TimeOut)
2177 timeout.QuadPart = -1;
2178 else
2179 timeout.QuadPart = TimeOut->QuadPart;
2181 SERVER_START_REQ( create_mailslot )
2183 req->access = DesiredAccess;
2184 req->attributes = attr->Attributes;
2185 req->rootdir = attr->RootDirectory;
2186 req->max_msgsize = MaxMessageSize;
2187 req->read_timeout = (timeout.QuadPart <= 0) ? timeout.QuadPart / -10000 : -1;
2188 wine_server_add_data( req, attr->ObjectName->Buffer,
2189 attr->ObjectName->Length );
2190 ret = wine_server_call( req );
2191 if( ret == STATUS_SUCCESS )
2192 *pHandle = reply->handle;
2194 SERVER_END_REQ;
2196 return ret;