server: Make the FILE_SHARE_DELETE sharing checks depend on DELETE
[wine/multimedia.git] / dlls / ntdll / file.c
blobb817283c230fa55357567e771583d9ade7a81247
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 #ifdef HAVE_IOKIT_IOKITLIB_H
61 # include <IOKit/IOKitLib.h>
62 # include <CoreFoundation/CFNumber.h> /* for kCFBooleanTrue, kCFBooleanFalse */
63 # include <paths.h>
64 #endif
66 #define NONAMELESSUNION
67 #define NONAMELESSSTRUCT
68 #include "ntstatus.h"
69 #define WIN32_NO_STATUS
70 #include "wine/unicode.h"
71 #include "wine/debug.h"
72 #include "thread.h"
73 #include "wine/server.h"
74 #include "ntdll_misc.h"
76 #include "winternl.h"
77 #include "winioctl.h"
79 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
81 mode_t FILE_umask = 0;
83 #define SECSPERDAY 86400
84 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
86 /**************************************************************************
87 * NtOpenFile [NTDLL.@]
88 * ZwOpenFile [NTDLL.@]
90 * Open a file.
92 * PARAMS
93 * handle [O] Variable that receives the file handle on return
94 * access [I] Access desired by the caller to the file
95 * attr [I] Structure describing the file to be opened
96 * io [O] Receives details about the result of the operation
97 * sharing [I] Type of shared access the caller requires
98 * options [I] Options for the file open
100 * RETURNS
101 * Success: 0. FileHandle and IoStatusBlock are updated.
102 * Failure: An NTSTATUS error code describing the error.
104 NTSTATUS WINAPI NtOpenFile( PHANDLE handle, ACCESS_MASK access,
105 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK io,
106 ULONG sharing, ULONG options )
108 return NtCreateFile( handle, access, attr, io, NULL, 0,
109 sharing, FILE_OPEN, options, NULL, 0 );
112 /**************************************************************************
113 * NtCreateFile [NTDLL.@]
114 * ZwCreateFile [NTDLL.@]
116 * Either create a new file or directory, or open an existing file, device,
117 * directory or volume.
119 * PARAMS
120 * handle [O] Points to a variable which receives the file handle on return
121 * access [I] Desired access to the file
122 * attr [I] Structure describing the file
123 * io [O] Receives information about the operation on return
124 * alloc_size [I] Initial size of the file in bytes
125 * attributes [I] Attributes to create the file with
126 * sharing [I] Type of shared access the caller would like to the file
127 * disposition [I] Specifies what to do, depending on whether the file already exists
128 * options [I] Options for creating a new file
129 * ea_buffer [I] Pointer to an extended attributes buffer
130 * ea_length [I] Length of ea_buffer
132 * RETURNS
133 * Success: 0. handle and io are updated.
134 * Failure: An NTSTATUS error code describing the error.
136 NTSTATUS WINAPI NtCreateFile( PHANDLE handle, ACCESS_MASK access, POBJECT_ATTRIBUTES attr,
137 PIO_STATUS_BLOCK io, PLARGE_INTEGER alloc_size,
138 ULONG attributes, ULONG sharing, ULONG disposition,
139 ULONG options, PVOID ea_buffer, ULONG ea_length )
141 static const WCHAR pipeW[] = {'\\','?','?','\\','p','i','p','e','\\'};
142 static const WCHAR mailslotW[] = {'\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
143 ANSI_STRING unix_name;
144 int created = FALSE;
146 TRACE("handle=%p access=%08lx name=%s objattr=%08lx root=%p sec=%p io=%p alloc_size=%p\n"
147 "attr=%08lx sharing=%08lx disp=%ld options=%08lx ea=%p.0x%08lx\n",
148 handle, access, debugstr_us(attr->ObjectName), attr->Attributes,
149 attr->RootDirectory, attr->SecurityDescriptor, io, alloc_size,
150 attributes, sharing, disposition, options, ea_buffer, ea_length );
152 if (!attr || !attr->ObjectName) return STATUS_INVALID_PARAMETER;
154 if (alloc_size) FIXME( "alloc_size not supported\n" );
156 /* check for named pipe */
158 if (attr->ObjectName->Length > sizeof(pipeW) &&
159 !memicmpW( attr->ObjectName->Buffer, pipeW, sizeof(pipeW)/sizeof(WCHAR) ))
161 SERVER_START_REQ( open_named_pipe )
163 req->access = access;
164 req->attributes = (attr) ? attr->Attributes : 0;
165 req->rootdir = attr ? attr->RootDirectory : 0;
166 req->flags = options;
167 wine_server_add_data( req, attr->ObjectName->Buffer,
168 attr->ObjectName->Length );
169 io->u.Status = wine_server_call( req );
170 *handle = reply->handle;
172 SERVER_END_REQ;
173 return io->u.Status;
176 /* check for mailslot */
178 if (attr->ObjectName->Length > sizeof(mailslotW) &&
179 !memicmpW( attr->ObjectName->Buffer, mailslotW, sizeof(mailslotW)/sizeof(WCHAR) ))
181 SERVER_START_REQ( open_mailslot )
183 req->access = access & GENERIC_WRITE;
184 req->attributes = (attr) ? attr->Attributes : 0;
185 req->rootdir = attr ? attr->RootDirectory : 0;
186 req->sharing = sharing;
187 wine_server_add_data( req, attr->ObjectName->Buffer,
188 attr->ObjectName->Length );
189 io->u.Status = wine_server_call( req );
190 *handle = reply->handle;
192 SERVER_END_REQ;
193 return io->u.Status;
196 if (attr->RootDirectory)
198 FIXME( "RootDirectory %p not supported\n", attr->RootDirectory );
199 return STATUS_OBJECT_NAME_NOT_FOUND;
202 io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
203 !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
205 if (io->u.Status == STATUS_BAD_DEVICE_TYPE)
207 SERVER_START_REQ( open_file_object )
209 req->access = access;
210 req->attributes = attr->Attributes;
211 req->rootdir = attr->RootDirectory;
212 req->sharing = sharing;
213 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
214 io->u.Status = wine_server_call( req );
215 *handle = reply->handle;
217 SERVER_END_REQ;
218 return io->u.Status;
221 if (io->u.Status == STATUS_NO_SUCH_FILE &&
222 disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
224 created = TRUE;
225 io->u.Status = STATUS_SUCCESS;
228 if (io->u.Status == STATUS_SUCCESS)
230 SERVER_START_REQ( create_file )
232 req->access = access;
233 req->attributes = attr->Attributes;
234 req->sharing = sharing;
235 req->create = disposition;
236 req->options = options;
237 req->attrs = attributes;
238 wine_server_add_data( req, unix_name.Buffer, unix_name.Length );
239 io->u.Status = wine_server_call( req );
240 *handle = reply->handle;
242 SERVER_END_REQ;
243 RtlFreeAnsiString( &unix_name );
245 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), io->u.Status );
247 if (io->u.Status == STATUS_SUCCESS)
249 if (created) io->Information = FILE_CREATED;
250 else switch(disposition)
252 case FILE_SUPERSEDE:
253 io->Information = FILE_SUPERSEDED;
254 break;
255 case FILE_CREATE:
256 io->Information = FILE_CREATED;
257 break;
258 case FILE_OPEN:
259 case FILE_OPEN_IF:
260 io->Information = FILE_OPENED;
261 break;
262 case FILE_OVERWRITE:
263 case FILE_OVERWRITE_IF:
264 io->Information = FILE_OVERWRITTEN;
265 break;
269 return io->u.Status;
272 /***********************************************************************
273 * Asynchronous file I/O *
275 static void WINAPI FILE_AsyncReadService(void*, PIO_STATUS_BLOCK, ULONG);
276 static void WINAPI FILE_AsyncWriteService(void*, PIO_STATUS_BLOCK, ULONG);
278 typedef struct async_fileio
280 HANDLE handle;
281 PIO_APC_ROUTINE apc;
282 void* apc_user;
283 char* buffer;
284 unsigned int count;
285 off_t offset;
286 int queue_apc_on_error;
287 BOOL avail_mode;
288 int fd;
289 HANDLE event;
290 } async_fileio;
292 static void fileio_terminate(async_fileio *fileio, IO_STATUS_BLOCK* iosb)
294 TRACE("data: %p\n", fileio);
296 wine_server_release_fd( fileio->handle, fileio->fd );
297 if ( fileio->event != INVALID_HANDLE_VALUE )
298 NtSetEvent( fileio->event, NULL );
300 if (fileio->apc &&
301 (iosb->u.Status == STATUS_SUCCESS || fileio->queue_apc_on_error))
302 fileio->apc( fileio->apc_user, iosb, iosb->Information );
304 RtlFreeHeap( GetProcessHeap(), 0, fileio );
308 static ULONG fileio_queue_async(async_fileio* fileio, IO_STATUS_BLOCK* iosb,
309 BOOL do_read)
311 PIO_APC_ROUTINE apc = do_read ? FILE_AsyncReadService : FILE_AsyncWriteService;
312 NTSTATUS status;
314 SERVER_START_REQ( register_async )
316 req->handle = fileio->handle;
317 req->io_apc = apc;
318 req->io_sb = iosb;
319 req->io_user = fileio;
320 req->type = do_read ? ASYNC_TYPE_READ : ASYNC_TYPE_WRITE;
321 req->count = (fileio->count < iosb->Information) ?
322 0 : fileio->count - iosb->Information;
323 status = wine_server_call( req );
325 SERVER_END_REQ;
327 if ( status ) iosb->u.Status = status;
328 if ( iosb->u.Status != STATUS_PENDING )
330 (apc)( fileio, iosb, iosb->u.Status );
331 return iosb->u.Status;
333 NtCurrentTeb()->num_async_io++;
334 return STATUS_SUCCESS;
337 /***********************************************************************
338 * FILE_GetNtStatus(void)
340 * Retrieve the Nt Status code from errno.
341 * Try to be consistent with FILE_SetDosError().
343 NTSTATUS FILE_GetNtStatus(void)
345 int err = errno;
347 TRACE( "errno = %d\n", errno );
348 switch (err)
350 case EAGAIN: return STATUS_SHARING_VIOLATION;
351 case EBADF: return STATUS_INVALID_HANDLE;
352 case EBUSY: return STATUS_DEVICE_BUSY;
353 case ENOSPC: return STATUS_DISK_FULL;
354 case EPERM:
355 case EROFS:
356 case EACCES: return STATUS_ACCESS_DENIED;
357 case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
358 case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
359 case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
360 case EMFILE:
361 case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
362 case EINVAL: return STATUS_INVALID_PARAMETER;
363 case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
364 case EPIPE: return STATUS_PIPE_BROKEN;
365 case EIO: return STATUS_DEVICE_NOT_READY;
366 #ifdef ENOMEDIUM
367 case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
368 #endif
369 case ENOTTY:
370 case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
371 case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
372 case ENOEXEC: /* ?? */
373 case ESPIPE: /* ?? */
374 case EEXIST: /* ?? */
375 default:
376 FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
377 return STATUS_UNSUCCESSFUL;
381 /***********************************************************************
382 * FILE_AsyncReadService (INTERNAL)
384 * This function is called while the client is waiting on the
385 * server, so we can't make any server calls here.
387 static void WINAPI FILE_AsyncReadService(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
389 async_fileio *fileio = (async_fileio*)user;
390 int result;
391 int already = iosb->Information;
393 TRACE("%p %p 0x%lx\n", iosb, fileio->buffer, status);
395 switch (status)
397 case STATUS_ALERTED: /* got some new data */
398 if (iosb->u.Status != STATUS_PENDING) FIXME("unexpected status %08lx\n", iosb->u.Status);
399 /* check to see if the data is ready (non-blocking) */
400 if ( fileio->avail_mode )
401 result = read(fileio->fd, &fileio->buffer[already],
402 fileio->count - already);
403 else
405 result = pread(fileio->fd, &fileio->buffer[already],
406 fileio->count - already,
407 fileio->offset + already);
408 if ((result < 0) && (errno == ESPIPE))
409 result = read(fileio->fd, &fileio->buffer[already],
410 fileio->count - already);
413 if (result < 0)
415 if (errno == EAGAIN || errno == EINTR)
417 TRACE("Deferred read %d\n", errno);
418 iosb->u.Status = STATUS_PENDING;
420 else /* check to see if the transfer is complete */
421 iosb->u.Status = FILE_GetNtStatus();
423 else if (result == 0)
425 iosb->u.Status = iosb->Information ? STATUS_SUCCESS : STATUS_END_OF_FILE;
427 else
429 iosb->Information += result;
430 if (iosb->Information >= fileio->count || fileio->avail_mode)
431 iosb->u.Status = STATUS_SUCCESS;
432 else
434 /* if we only have to read the available data, and none is available,
435 * simply cancel the request. If data was available, it has been read
436 * while in by previous call (NtDelayExecution)
438 iosb->u.Status = (fileio->avail_mode) ? STATUS_SUCCESS : STATUS_PENDING;
441 TRACE("read %d more bytes %ld/%d so far (%s)\n",
442 result, iosb->Information, fileio->count,
443 (iosb->u.Status == STATUS_SUCCESS) ? "success" : "pending");
445 /* queue another async operation ? */
446 if (iosb->u.Status == STATUS_PENDING)
447 fileio_queue_async(fileio, iosb, TRUE);
448 else
449 fileio_terminate(fileio, iosb);
450 break;
451 default:
452 iosb->u.Status = status;
453 fileio_terminate(fileio, iosb);
454 break;
459 /******************************************************************************
460 * NtReadFile [NTDLL.@]
461 * ZwReadFile [NTDLL.@]
463 * Read from an open file handle.
465 * PARAMS
466 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
467 * Event [I] Event to signal upon completion (or NULL)
468 * ApcRoutine [I] Callback to call upon completion (or NULL)
469 * ApcContext [I] Context for ApcRoutine (or NULL)
470 * IoStatusBlock [O] Receives information about the operation on return
471 * Buffer [O] Destination for the data read
472 * Length [I] Size of Buffer
473 * ByteOffset [O] Destination for the new file pointer position (or NULL)
474 * Key [O] Function unknown (may be NULL)
476 * RETURNS
477 * Success: 0. IoStatusBlock is updated, and the Information member contains
478 * The number of bytes read.
479 * Failure: An NTSTATUS error code describing the error.
481 NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
482 PIO_APC_ROUTINE apc, void* apc_user,
483 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
484 PLARGE_INTEGER offset, PULONG key)
486 int unix_handle, flags;
488 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
489 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
491 if (!io_status) return STATUS_ACCESS_VIOLATION;
493 io_status->Information = 0;
494 io_status->u.Status = wine_server_handle_to_fd( hFile, FILE_READ_DATA, &unix_handle, &flags );
495 if (io_status->u.Status) return io_status->u.Status;
497 if (flags & FD_FLAG_RECV_SHUTDOWN)
499 wine_server_release_fd( hFile, unix_handle );
500 return STATUS_PIPE_DISCONNECTED;
503 if (flags & FD_FLAG_TIMEOUT)
505 if (hEvent)
507 /* this shouldn't happen, but check it */
508 FIXME("NIY-hEvent\n");
509 wine_server_release_fd( hFile, unix_handle );
510 return STATUS_NOT_IMPLEMENTED;
512 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
513 if (io_status->u.Status)
515 wine_server_release_fd( hFile, unix_handle );
516 return io_status->u.Status;
520 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
522 async_fileio* fileio;
523 NTSTATUS ret;
525 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
527 wine_server_release_fd( hFile, unix_handle );
528 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
529 return STATUS_NO_MEMORY;
531 fileio->handle = hFile;
532 fileio->count = length;
533 if ( offset == NULL )
534 fileio->offset = 0;
535 else
537 fileio->offset = offset->QuadPart;
538 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
539 FIXME("High part of offset is lost\n");
541 fileio->apc = apc;
542 fileio->apc_user = apc_user;
543 fileio->buffer = buffer;
544 fileio->queue_apc_on_error = 0;
545 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
546 fileio->fd = unix_handle; /* FIXME */
547 fileio->event = hEvent;
548 NtResetEvent(hEvent, NULL);
550 io_status->u.Status = STATUS_PENDING;
551 ret = fileio_queue_async(fileio, io_status, TRUE);
552 if (ret != STATUS_SUCCESS)
554 wine_server_release_fd( hFile, unix_handle );
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%08lx\n", io_status->u.Status);
585 return io_status->u.Status;
588 if (offset)
590 FILE_POSITION_INFORMATION fpi;
592 fpi.CurrentByteOffset = *offset;
593 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
594 FilePositionInformation);
595 if (io_status->u.Status)
597 wine_server_release_fd( hFile, unix_handle );
598 return io_status->u.Status;
601 /* code for synchronous reads */
602 while ((io_status->Information = read( unix_handle, buffer, length )) == -1)
604 if ((errno == EAGAIN) || (errno == EINTR)) continue;
605 if (errno == EFAULT)
607 io_status->Information = 0;
608 io_status->u.Status = STATUS_ACCESS_VIOLATION;
610 else io_status->u.Status = FILE_GetNtStatus();
611 break;
613 if (io_status->u.Status == STATUS_SUCCESS && io_status->Information == 0)
615 struct stat st;
616 if (fstat( unix_handle, &st ) != -1 && S_ISSOCK( st.st_mode ))
617 io_status->u.Status = STATUS_PIPE_BROKEN;
618 else
619 io_status->u.Status = STATUS_END_OF_FILE;
621 wine_server_release_fd( hFile, unix_handle );
622 TRACE("= 0x%08lx (%lu)\n", io_status->u.Status, io_status->Information);
623 return io_status->u.Status;
626 /***********************************************************************
627 * FILE_AsyncWriteService (INTERNAL)
629 * This function is called while the client is waiting on the
630 * server, so we can't make any server calls here.
632 static void WINAPI FILE_AsyncWriteService(void *ovp, IO_STATUS_BLOCK *iosb, ULONG status)
634 async_fileio *fileio = (async_fileio *) ovp;
635 int result;
636 int already = iosb->Information;
638 TRACE("(%p %p 0x%lx)\n",iosb, fileio->buffer, status);
640 switch (status)
642 case STATUS_ALERTED:
643 /* write some data (non-blocking) */
644 if ( fileio->avail_mode )
645 result = write(fileio->fd, &fileio->buffer[already],
646 fileio->count - already);
647 else
649 result = pwrite(fileio->fd, &fileio->buffer[already],
650 fileio->count - already, fileio->offset + already);
651 if ((result < 0) && (errno == ESPIPE))
652 result = write(fileio->fd, &fileio->buffer[already],
653 fileio->count - already);
656 if (result < 0)
658 if (errno == EAGAIN || errno == EINTR) iosb->u.Status = STATUS_PENDING;
659 else iosb->u.Status = FILE_GetNtStatus();
661 else
663 iosb->Information += result;
664 iosb->u.Status = (iosb->Information < fileio->count) ? STATUS_PENDING : STATUS_SUCCESS;
665 TRACE("wrote %d more bytes %ld/%d so far\n",
666 result, iosb->Information, fileio->count);
668 if (iosb->u.Status == STATUS_PENDING)
669 fileio_queue_async(fileio, iosb, FALSE);
670 else
671 fileio_terminate(fileio, iosb);
672 break;
673 default:
674 iosb->u.Status = status;
675 fileio_terminate(fileio, iosb);
676 break;
680 /******************************************************************************
681 * NtWriteFile [NTDLL.@]
682 * ZwWriteFile [NTDLL.@]
684 * Write to an open file handle.
686 * PARAMS
687 * FileHandle [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 * Buffer [I] Source for the data to write
693 * Length [I] Size of Buffer
694 * ByteOffset [O] Destination for the new file pointer position (or NULL)
695 * Key [O] Function unknown (may be NULL)
697 * RETURNS
698 * Success: 0. IoStatusBlock is updated, and the Information member contains
699 * The number of bytes written.
700 * Failure: An NTSTATUS error code describing the error.
702 NTSTATUS WINAPI NtWriteFile(HANDLE hFile, HANDLE hEvent,
703 PIO_APC_ROUTINE apc, void* apc_user,
704 PIO_STATUS_BLOCK io_status,
705 const void* buffer, ULONG length,
706 PLARGE_INTEGER offset, PULONG key)
708 int unix_handle, flags;
710 TRACE("(%p,%p,%p,%p,%p,%p,0x%08lx,%p,%p)!\n",
711 hFile,hEvent,apc,apc_user,io_status,buffer,length,offset,key);
713 if (!io_status) return STATUS_ACCESS_VIOLATION;
715 io_status->Information = 0;
716 io_status->u.Status = wine_server_handle_to_fd( hFile, FILE_WRITE_DATA, &unix_handle, &flags );
717 if (io_status->u.Status) return io_status->u.Status;
719 if (flags & FD_FLAG_SEND_SHUTDOWN)
721 wine_server_release_fd( hFile, unix_handle );
722 return STATUS_PIPE_DISCONNECTED;
725 if (flags & FD_FLAG_TIMEOUT)
727 if (hEvent)
729 /* this shouldn't happen, but check it */
730 FIXME("NIY-hEvent\n");
731 wine_server_release_fd( hFile, unix_handle );
732 return STATUS_NOT_IMPLEMENTED;
734 io_status->u.Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, NULL, 0, 0);
735 if (io_status->u.Status)
737 wine_server_release_fd( hFile, unix_handle );
738 return io_status->u.Status;
742 if (flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))
744 async_fileio* fileio;
745 NTSTATUS ret;
747 if (!(fileio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(async_fileio))))
749 wine_server_release_fd( hFile, unix_handle );
750 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
751 return STATUS_NO_MEMORY;
753 fileio->handle = hFile;
754 fileio->count = length;
755 if (offset)
757 fileio->offset = offset->QuadPart;
758 if (offset->u.HighPart && fileio->offset == offset->u.LowPart)
759 FIXME("High part of offset is lost\n");
761 else
763 fileio->offset = 0;
765 fileio->apc = apc;
766 fileio->apc_user = apc_user;
767 fileio->buffer = (void*)buffer;
768 fileio->queue_apc_on_error = 0;
769 fileio->avail_mode = (flags & FD_FLAG_AVAILABLE);
770 fileio->fd = unix_handle; /* FIXME */
771 fileio->event = hEvent;
772 NtResetEvent(hEvent, NULL);
774 io_status->Information = 0;
775 io_status->u.Status = STATUS_PENDING;
776 ret = fileio_queue_async(fileio, io_status, FALSE);
777 if (ret != STATUS_SUCCESS)
779 wine_server_release_fd( hFile, unix_handle );
780 if (flags & FD_FLAG_TIMEOUT) NtClose(hEvent);
781 return ret;
783 if (flags & FD_FLAG_TIMEOUT)
787 ret = NtWaitForSingleObject(hEvent, TRUE, NULL);
789 while (ret == STATUS_USER_APC && io_status->u.Status == STATUS_PENDING);
790 NtClose(hEvent);
791 if (ret != STATUS_USER_APC)
792 fileio->queue_apc_on_error = 1;
794 else
796 LARGE_INTEGER timeout;
798 /* let some APC be run, this will write as much data as possible */
799 timeout.u.LowPart = timeout.u.HighPart = 0;
800 ret = NtDelayExecution( TRUE, &timeout );
801 /* the apc didn't run and therefore the completion routine now
802 * needs to be sent errors.
803 * Note that there is no race between setting this flag and
804 * returning errors because apc's are run only during alertable
805 * waits */
806 if (ret != STATUS_USER_APC)
807 fileio->queue_apc_on_error = 1;
809 return io_status->u.Status;
812 if (offset)
814 FILE_POSITION_INFORMATION fpi;
816 fpi.CurrentByteOffset = *offset;
817 io_status->u.Status = NtSetInformationFile(hFile, io_status, &fpi, sizeof(fpi),
818 FilePositionInformation);
819 if (io_status->u.Status)
821 wine_server_release_fd( hFile, unix_handle );
822 return io_status->u.Status;
826 /* synchronous file write */
827 while ((io_status->Information = write( unix_handle, buffer, length )) == -1)
829 if ((errno == EAGAIN) || (errno == EINTR)) continue;
830 if (errno == EFAULT)
832 io_status->Information = 0;
833 io_status->u.Status = STATUS_INVALID_USER_BUFFER;
835 else if (errno == ENOSPC) io_status->u.Status = STATUS_DISK_FULL;
836 else io_status->u.Status = FILE_GetNtStatus();
837 break;
839 wine_server_release_fd( hFile, unix_handle );
840 return io_status->u.Status;
843 /**************************************************************************
844 * NtDeviceIoControlFile [NTDLL.@]
845 * ZwDeviceIoControlFile [NTDLL.@]
847 * Perform an I/O control operation on an open file handle.
849 * PARAMS
850 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
851 * event [I] Event to signal upon completion (or NULL)
852 * apc [I] Callback to call upon completion (or NULL)
853 * apc_context [I] Context for ApcRoutine (or NULL)
854 * io [O] Receives information about the operation on return
855 * code [I] Control code for the operation to perform
856 * in_buffer [I] Source for any input data required (or NULL)
857 * in_size [I] Size of InputBuffer
858 * out_buffer [O] Source for any output data returned (or NULL)
859 * out_size [I] Size of OutputBuffer
861 * RETURNS
862 * Success: 0. IoStatusBlock is updated.
863 * Failure: An NTSTATUS error code describing the error.
865 NTSTATUS WINAPI NtDeviceIoControlFile(HANDLE handle, HANDLE event,
866 PIO_APC_ROUTINE apc, PVOID apc_context,
867 PIO_STATUS_BLOCK io, ULONG code,
868 PVOID in_buffer, ULONG in_size,
869 PVOID out_buffer, ULONG out_size)
871 ULONG device = (code >> 16);
873 TRACE("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx)\n",
874 handle, event, apc, apc_context, io, code,
875 in_buffer, in_size, out_buffer, out_size);
877 switch(device)
879 case FILE_DEVICE_DISK:
880 case FILE_DEVICE_CD_ROM:
881 case FILE_DEVICE_DVD:
882 case FILE_DEVICE_CONTROLLER:
883 case FILE_DEVICE_MASS_STORAGE:
884 io->u.Status = CDROM_DeviceIoControl(handle, event, apc, apc_context, io, code,
885 in_buffer, in_size, out_buffer, out_size);
886 break;
887 case FILE_DEVICE_SERIAL_PORT:
888 io->u.Status = COMM_DeviceIoControl(handle, event, apc, apc_context, io, code,
889 in_buffer, in_size, out_buffer, out_size);
890 break;
891 case FILE_DEVICE_TAPE:
892 io->u.Status = TAPE_DeviceIoControl(handle, event, apc, apc_context, io, code,
893 in_buffer, in_size, out_buffer, out_size);
894 break;
895 default:
896 FIXME("Unsupported ioctl %lx (device=%lx access=%lx func=%lx method=%lx)\n",
897 code, device, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
898 io->u.Status = STATUS_NOT_SUPPORTED;
899 break;
901 return io->u.Status;
904 /***********************************************************************
905 * pipe_completion_wait (Internal)
907 static void CALLBACK pipe_completion_wait(HANDLE event, PIO_STATUS_BLOCK iosb, ULONG status)
909 TRACE("for %p/%p, status=%08lx\n", event, iosb, status);
911 if (iosb)
912 iosb->u.Status = status;
913 NtSetEvent(event, NULL);
914 TRACE("done\n");
917 /**************************************************************************
918 * NtFsControlFile [NTDLL.@]
919 * ZwFsControlFile [NTDLL.@]
921 * Perform a file system control operation on an open file handle.
923 * PARAMS
924 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
925 * event [I] Event to signal upon completion (or NULL)
926 * apc [I] Callback to call upon completion (or NULL)
927 * apc_context [I] Context for ApcRoutine (or NULL)
928 * io [O] Receives information about the operation on return
929 * code [I] Control code for the operation to perform
930 * in_buffer [I] Source for any input data required (or NULL)
931 * in_size [I] Size of InputBuffer
932 * out_buffer [O] Source for any output data returned (or NULL)
933 * out_size [I] Size of OutputBuffer
935 * RETURNS
936 * Success: 0. IoStatusBlock is updated.
937 * Failure: An NTSTATUS error code describing the error.
939 NTSTATUS WINAPI NtFsControlFile(HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc,
940 PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code,
941 PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size)
943 TRACE("(%p,%p,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx)\n",
944 handle, event, apc, apc_context, io, code,
945 in_buffer, in_size, out_buffer, out_size);
947 if (!io) return STATUS_INVALID_PARAMETER;
949 switch(code)
951 case FSCTL_DISMOUNT_VOLUME:
952 io->u.Status = DIR_unmount_device( handle );
953 break;
955 case FSCTL_PIPE_LISTEN:
957 HANDLE internal_event = 0;
959 if(!event)
961 io->u.Status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
962 if (io->u.Status != STATUS_SUCCESS) return io->u.Status;
964 SERVER_START_REQ(connect_named_pipe)
966 req->handle = handle;
967 req->event = event ? event : internal_event;
968 req->func = pipe_completion_wait;
969 io->u.Status = wine_server_call(req);
971 SERVER_END_REQ;
973 if(io->u.Status == STATUS_SUCCESS)
975 if(event) io->u.Status = STATUS_PENDING;
976 else
979 io->u.Status = NtWaitForSingleObject(internal_event, TRUE, NULL);
980 while(io->u.Status == STATUS_USER_APC);
983 if (internal_event) NtClose(internal_event);
985 break;
987 case FSCTL_PIPE_WAIT:
989 HANDLE internal_event = 0;
990 FILE_PIPE_WAIT_FOR_BUFFER *buff = in_buffer;
992 if(!event)
994 io->u.Status = NtCreateEvent(&internal_event, EVENT_ALL_ACCESS, NULL, FALSE, FALSE);
995 if (io->u.Status != STATUS_SUCCESS) return io->u.Status;
997 SERVER_START_REQ(wait_named_pipe)
999 req->handle = handle;
1000 req->timeout = buff->TimeoutSpecified ? buff->Timeout.QuadPart / -10000L
1001 : NMPWAIT_USE_DEFAULT_WAIT;
1002 req->event = event ? event : internal_event;
1003 req->func = pipe_completion_wait;
1004 wine_server_add_data( req, buff->Name, buff->NameLength );
1005 io->u.Status = wine_server_call( req );
1007 SERVER_END_REQ;
1009 if(io->u.Status == STATUS_SUCCESS)
1011 if(event)
1012 io->u.Status = STATUS_PENDING;
1013 else
1016 io->u.Status = NtWaitForSingleObject(internal_event, TRUE, NULL);
1017 while(io->u.Status == STATUS_USER_APC);
1020 if (internal_event) NtClose(internal_event);
1022 break;
1024 case FSCTL_PIPE_DISCONNECT:
1025 SERVER_START_REQ(disconnect_named_pipe)
1027 req->handle = handle;
1028 io->u.Status = wine_server_call(req);
1029 if (!io->u.Status && reply->fd != -1) close(reply->fd);
1031 SERVER_END_REQ;
1032 break;
1034 default:
1035 FIXME("Unsupported fsctl %lx (device=%lx access=%lx func=%lx method=%lx)\n",
1036 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
1037 io->u.Status = STATUS_NOT_SUPPORTED;
1038 break;
1040 return io->u.Status;
1043 /******************************************************************************
1044 * NtSetVolumeInformationFile [NTDLL.@]
1045 * ZwSetVolumeInformationFile [NTDLL.@]
1047 * Set volume information for an open file handle.
1049 * PARAMS
1050 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1051 * IoStatusBlock [O] Receives information about the operation on return
1052 * FsInformation [I] Source for volume information
1053 * Length [I] Size of FsInformation
1054 * FsInformationClass [I] Type of volume information to set
1056 * RETURNS
1057 * Success: 0. IoStatusBlock is updated.
1058 * Failure: An NTSTATUS error code describing the error.
1060 NTSTATUS WINAPI NtSetVolumeInformationFile(
1061 IN HANDLE FileHandle,
1062 PIO_STATUS_BLOCK IoStatusBlock,
1063 PVOID FsInformation,
1064 ULONG Length,
1065 FS_INFORMATION_CLASS FsInformationClass)
1067 FIXME("(%p,%p,%p,0x%08lx,0x%08x) stub\n",
1068 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
1069 return 0;
1072 /******************************************************************************
1073 * NtQueryInformationFile [NTDLL.@]
1074 * ZwQueryInformationFile [NTDLL.@]
1076 * Get information about an open file handle.
1078 * PARAMS
1079 * hFile [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1080 * io [O] Receives information about the operation on return
1081 * ptr [O] Destination for file information
1082 * len [I] Size of FileInformation
1083 * class [I] Type of file information to get
1085 * RETURNS
1086 * Success: 0. IoStatusBlock and FileInformation are updated.
1087 * Failure: An NTSTATUS error code describing the error.
1089 NTSTATUS WINAPI NtQueryInformationFile( HANDLE hFile, PIO_STATUS_BLOCK io,
1090 PVOID ptr, LONG len, FILE_INFORMATION_CLASS class )
1092 static const size_t info_sizes[] =
1095 sizeof(FILE_DIRECTORY_INFORMATION), /* FileDirectoryInformation */
1096 sizeof(FILE_FULL_DIRECTORY_INFORMATION), /* FileFullDirectoryInformation */
1097 sizeof(FILE_BOTH_DIRECTORY_INFORMATION), /* FileBothDirectoryInformation */
1098 sizeof(FILE_BASIC_INFORMATION), /* FileBasicInformation */
1099 sizeof(FILE_STANDARD_INFORMATION), /* FileStandardInformation */
1100 sizeof(FILE_INTERNAL_INFORMATION), /* FileInternalInformation */
1101 sizeof(FILE_EA_INFORMATION), /* FileEaInformation */
1102 sizeof(FILE_ACCESS_INFORMATION), /* FileAccessInformation */
1103 sizeof(FILE_NAME_INFORMATION)-sizeof(WCHAR), /* FileNameInformation */
1104 sizeof(FILE_RENAME_INFORMATION)-sizeof(WCHAR), /* FileRenameInformation */
1105 0, /* FileLinkInformation */
1106 sizeof(FILE_NAMES_INFORMATION)-sizeof(WCHAR), /* FileNamesInformation */
1107 sizeof(FILE_DISPOSITION_INFORMATION), /* FileDispositionInformation */
1108 sizeof(FILE_POSITION_INFORMATION), /* FilePositionInformation */
1109 sizeof(FILE_FULL_EA_INFORMATION), /* FileFullEaInformation */
1110 sizeof(FILE_MODE_INFORMATION), /* FileModeInformation */
1111 sizeof(FILE_ALIGNMENT_INFORMATION), /* FileAlignmentInformation */
1112 sizeof(FILE_ALL_INFORMATION)-sizeof(WCHAR), /* FileAllInformation */
1113 sizeof(FILE_ALLOCATION_INFORMATION), /* FileAllocationInformation */
1114 sizeof(FILE_END_OF_FILE_INFORMATION), /* FileEndOfFileInformation */
1115 0, /* FileAlternateNameInformation */
1116 sizeof(FILE_STREAM_INFORMATION)-sizeof(WCHAR), /* FileStreamInformation */
1117 0, /* FilePipeInformation */
1118 0, /* FilePipeLocalInformation */
1119 0, /* FilePipeRemoteInformation */
1120 sizeof(FILE_MAILSLOT_QUERY_INFORMATION), /* FileMailslotQueryInformation */
1121 0, /* FileMailslotSetInformation */
1122 0, /* FileCompressionInformation */
1123 0, /* FileObjectIdInformation */
1124 0, /* FileCompletionInformation */
1125 0, /* FileMoveClusterInformation */
1126 0, /* FileQuotaInformation */
1127 0, /* FileReparsePointInformation */
1128 0, /* FileNetworkOpenInformation */
1129 0, /* FileAttributeTagInformation */
1130 0 /* FileTrackingInformation */
1133 struct stat st;
1134 int fd;
1136 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", hFile, io, ptr, len, class);
1138 io->Information = 0;
1140 if (class <= 0 || class >= FileMaximumInformation)
1141 return io->u.Status = STATUS_INVALID_INFO_CLASS;
1142 if (!info_sizes[class])
1144 FIXME("Unsupported class (%d)\n", class);
1145 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1147 if (len < info_sizes[class])
1148 return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
1150 if ((io->u.Status = wine_server_handle_to_fd( hFile, 0, &fd, NULL )))
1151 return io->u.Status;
1153 switch (class)
1155 case FileBasicInformation:
1157 FILE_BASIC_INFORMATION *info = ptr;
1159 if (fstat( fd, &st ) == -1)
1160 io->u.Status = FILE_GetNtStatus();
1161 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1162 io->u.Status = STATUS_INVALID_INFO_CLASS;
1163 else
1165 if (S_ISDIR(st.st_mode)) info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1166 else info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1167 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1168 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1169 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime);
1170 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime);
1171 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime);
1172 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime);
1175 break;
1176 case FileStandardInformation:
1178 FILE_STANDARD_INFORMATION *info = ptr;
1180 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1181 else
1183 if ((info->Directory = S_ISDIR(st.st_mode)))
1185 info->AllocationSize.QuadPart = 0;
1186 info->EndOfFile.QuadPart = 0;
1187 info->NumberOfLinks = 1;
1188 info->DeletePending = FALSE;
1190 else
1192 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1193 info->EndOfFile.QuadPart = st.st_size;
1194 info->NumberOfLinks = st.st_nlink;
1195 info->DeletePending = FALSE; /* FIXME */
1199 break;
1200 case FilePositionInformation:
1202 FILE_POSITION_INFORMATION *info = ptr;
1203 off_t res = lseek( fd, 0, SEEK_CUR );
1204 if (res == (off_t)-1) io->u.Status = FILE_GetNtStatus();
1205 else info->CurrentByteOffset.QuadPart = res;
1207 break;
1208 case FileInternalInformation:
1210 FILE_INTERNAL_INFORMATION *info = ptr;
1212 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1213 else info->IndexNumber.QuadPart = st.st_ino;
1215 break;
1216 case FileEaInformation:
1218 FILE_EA_INFORMATION *info = ptr;
1219 info->EaSize = 0;
1221 break;
1222 case FileEndOfFileInformation:
1224 FILE_END_OF_FILE_INFORMATION *info = ptr;
1226 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1227 else info->EndOfFile.QuadPart = S_ISDIR(st.st_mode) ? 0 : st.st_size;
1229 break;
1230 case FileAllInformation:
1232 FILE_ALL_INFORMATION *info = ptr;
1234 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1235 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1236 io->u.Status = STATUS_INVALID_INFO_CLASS;
1237 else
1239 if ((info->StandardInformation.Directory = S_ISDIR(st.st_mode)))
1241 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1242 info->StandardInformation.AllocationSize.QuadPart = 0;
1243 info->StandardInformation.EndOfFile.QuadPart = 0;
1244 info->StandardInformation.NumberOfLinks = 1;
1245 info->StandardInformation.DeletePending = FALSE;
1247 else
1249 info->BasicInformation.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1250 info->StandardInformation.AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1251 info->StandardInformation.EndOfFile.QuadPart = st.st_size;
1252 info->StandardInformation.NumberOfLinks = st.st_nlink;
1253 info->StandardInformation.DeletePending = FALSE; /* FIXME */
1255 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1256 info->BasicInformation.FileAttributes |= FILE_ATTRIBUTE_READONLY;
1257 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.CreationTime);
1258 RtlSecondsSince1970ToTime( st.st_mtime, &info->BasicInformation.LastWriteTime);
1259 RtlSecondsSince1970ToTime( st.st_ctime, &info->BasicInformation.ChangeTime);
1260 RtlSecondsSince1970ToTime( st.st_atime, &info->BasicInformation.LastAccessTime);
1261 info->InternalInformation.IndexNumber.QuadPart = st.st_ino;
1262 info->EaInformation.EaSize = 0;
1263 info->AccessInformation.AccessFlags = 0; /* FIXME */
1264 info->PositionInformation.CurrentByteOffset.QuadPart = lseek( fd, 0, SEEK_CUR );
1265 info->ModeInformation.Mode = 0; /* FIXME */
1266 info->AlignmentInformation.AlignmentRequirement = 1; /* FIXME */
1267 info->NameInformation.FileNameLength = 0;
1268 io->Information = sizeof(*info) - sizeof(WCHAR);
1271 break;
1272 case FileMailslotQueryInformation:
1274 FILE_MAILSLOT_QUERY_INFORMATION *info = ptr;
1276 SERVER_START_REQ( set_mailslot_info )
1278 req->handle = hFile;
1279 req->flags = 0;
1280 io->u.Status = wine_server_call( req );
1281 if( io->u.Status == STATUS_SUCCESS )
1283 info->MaximumMessageSize = reply->max_msgsize;
1284 info->MailslotQuota = 0;
1285 info->NextMessageSize = reply->next_msgsize;
1286 info->MessagesAvailable = reply->msg_count;
1287 info->ReadTimeout.QuadPart = reply->read_timeout * -10000;
1290 SERVER_END_REQ;
1292 break;
1293 default:
1294 FIXME("Unsupported class (%d)\n", class);
1295 io->u.Status = STATUS_NOT_IMPLEMENTED;
1296 break;
1298 wine_server_release_fd( hFile, fd );
1299 if (io->u.Status == STATUS_SUCCESS && !io->Information) io->Information = info_sizes[class];
1300 return io->u.Status;
1303 /******************************************************************************
1304 * NtSetInformationFile [NTDLL.@]
1305 * ZwSetInformationFile [NTDLL.@]
1307 * Set information about an open file handle.
1309 * PARAMS
1310 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1311 * io [O] Receives information about the operation on return
1312 * ptr [I] Source for file information
1313 * len [I] Size of FileInformation
1314 * class [I] Type of file information to set
1316 * RETURNS
1317 * Success: 0. io is updated.
1318 * Failure: An NTSTATUS error code describing the error.
1320 NTSTATUS WINAPI NtSetInformationFile(HANDLE handle, PIO_STATUS_BLOCK io,
1321 PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class)
1323 int fd;
1325 TRACE("(%p,%p,%p,0x%08lx,0x%08x)\n", handle, io, ptr, len, class);
1327 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )))
1328 return io->u.Status;
1330 io->u.Status = STATUS_SUCCESS;
1331 switch (class)
1333 case FileBasicInformation:
1334 if (len >= sizeof(FILE_BASIC_INFORMATION))
1336 struct stat st;
1337 const FILE_BASIC_INFORMATION *info = ptr;
1339 if (info->LastAccessTime.QuadPart || info->LastWriteTime.QuadPart)
1341 ULONGLONG sec, nsec;
1342 struct timeval tv[2];
1344 if (!info->LastAccessTime.QuadPart || !info->LastWriteTime.QuadPart)
1347 tv[0].tv_sec = tv[0].tv_usec = 0;
1348 tv[1].tv_sec = tv[1].tv_usec = 0;
1349 if (!fstat( fd, &st ))
1351 tv[0].tv_sec = st.st_atime;
1352 tv[1].tv_sec = st.st_mtime;
1355 if (info->LastAccessTime.QuadPart)
1357 sec = RtlLargeIntegerDivide( info->LastAccessTime.QuadPart, 10000000, &nsec );
1358 tv[0].tv_sec = sec - SECS_1601_TO_1970;
1359 tv[0].tv_usec = (UINT)nsec / 10;
1361 if (info->LastWriteTime.QuadPart)
1363 sec = RtlLargeIntegerDivide( info->LastWriteTime.QuadPart, 10000000, &nsec );
1364 tv[1].tv_sec = sec - SECS_1601_TO_1970;
1365 tv[1].tv_usec = (UINT)nsec / 10;
1367 if (futimes( fd, tv ) == -1) io->u.Status = FILE_GetNtStatus();
1370 if (io->u.Status == STATUS_SUCCESS && info->FileAttributes)
1372 if (fstat( fd, &st ) == -1) io->u.Status = FILE_GetNtStatus();
1373 else
1375 if (info->FileAttributes & FILE_ATTRIBUTE_READONLY)
1377 if (S_ISDIR( st.st_mode))
1378 WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
1379 else
1380 st.st_mode &= ~0222; /* clear write permission bits */
1382 else
1384 /* add write permission only where we already have read permission */
1385 st.st_mode |= (0600 | ((st.st_mode & 044) >> 1)) & (~FILE_umask);
1387 if (fchmod( fd, st.st_mode ) == -1) io->u.Status = FILE_GetNtStatus();
1391 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1392 break;
1394 case FilePositionInformation:
1395 if (len >= sizeof(FILE_POSITION_INFORMATION))
1397 const FILE_POSITION_INFORMATION *info = ptr;
1399 if (lseek( fd, info->CurrentByteOffset.QuadPart, SEEK_SET ) == (off_t)-1)
1400 io->u.Status = FILE_GetNtStatus();
1402 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1403 break;
1405 case FileEndOfFileInformation:
1406 if (len >= sizeof(FILE_END_OF_FILE_INFORMATION))
1408 struct stat st;
1409 const FILE_END_OF_FILE_INFORMATION *info = ptr;
1411 /* first try normal truncate */
1412 if (ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1414 /* now check for the need to extend the file */
1415 if (fstat( fd, &st ) != -1 && (off_t)info->EndOfFile.QuadPart > st.st_size)
1417 static const char zero;
1419 /* extend the file one byte beyond the requested size and then truncate it */
1420 /* this should work around ftruncate implementations that can't extend files */
1421 if (pwrite( fd, &zero, 1, (off_t)info->EndOfFile.QuadPart ) != -1 &&
1422 ftruncate( fd, (off_t)info->EndOfFile.QuadPart ) != -1) break;
1424 io->u.Status = FILE_GetNtStatus();
1426 else io->u.Status = STATUS_INVALID_PARAMETER_3;
1427 break;
1429 case FileMailslotSetInformation:
1431 FILE_MAILSLOT_SET_INFORMATION *info = ptr;
1433 SERVER_START_REQ( set_mailslot_info )
1435 req->handle = handle;
1436 req->flags = MAILSLOT_SET_READ_TIMEOUT;
1437 req->read_timeout = info->ReadTimeout.QuadPart / -10000;
1438 io->u.Status = wine_server_call( req );
1440 SERVER_END_REQ;
1442 break;
1444 default:
1445 FIXME("Unsupported class (%d)\n", class);
1446 io->u.Status = STATUS_NOT_IMPLEMENTED;
1447 break;
1449 wine_server_release_fd( handle, fd );
1450 io->Information = 0;
1451 return io->u.Status;
1455 /******************************************************************************
1456 * NtQueryFullAttributesFile (NTDLL.@)
1458 NTSTATUS WINAPI NtQueryFullAttributesFile( const OBJECT_ATTRIBUTES *attr,
1459 FILE_NETWORK_OPEN_INFORMATION *info )
1461 ANSI_STRING unix_name;
1462 NTSTATUS status;
1464 if (!(status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, FILE_OPEN,
1465 !(attr->Attributes & OBJ_CASE_INSENSITIVE) )))
1467 struct stat st;
1469 if (stat( unix_name.Buffer, &st ) == -1)
1470 status = FILE_GetNtStatus();
1471 else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1472 status = STATUS_INVALID_INFO_CLASS;
1473 else
1475 if (S_ISDIR(st.st_mode))
1477 info->FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1478 info->AllocationSize.QuadPart = 0;
1479 info->EndOfFile.QuadPart = 0;
1481 else
1483 info->FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1484 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
1485 info->EndOfFile.QuadPart = st.st_size;
1487 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1488 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1489 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
1490 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
1491 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
1492 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
1493 if (DIR_is_hidden_file( attr->ObjectName ))
1494 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1496 RtlFreeAnsiString( &unix_name );
1498 else WARN("%s not found (%lx)\n", debugstr_us(attr->ObjectName), status );
1499 return status;
1503 /******************************************************************************
1504 * NtQueryAttributesFile (NTDLL.@)
1505 * ZwQueryAttributesFile (NTDLL.@)
1507 NTSTATUS WINAPI NtQueryAttributesFile( const OBJECT_ATTRIBUTES *attr, FILE_BASIC_INFORMATION *info )
1509 FILE_NETWORK_OPEN_INFORMATION full_info;
1510 NTSTATUS status;
1512 if (!(status = NtQueryFullAttributesFile( attr, &full_info )))
1514 info->CreationTime.QuadPart = full_info.CreationTime.QuadPart;
1515 info->LastAccessTime.QuadPart = full_info.LastAccessTime.QuadPart;
1516 info->LastWriteTime.QuadPart = full_info.LastWriteTime.QuadPart;
1517 info->ChangeTime.QuadPart = full_info.ChangeTime.QuadPart;
1518 info->FileAttributes = full_info.FileAttributes;
1520 return status;
1524 /******************************************************************************
1525 * FILE_GetDeviceInfo
1527 * Implementation of the FileFsDeviceInformation query for NtQueryVolumeInformationFile.
1529 NTSTATUS FILE_GetDeviceInfo( int fd, FILE_FS_DEVICE_INFORMATION *info )
1531 struct stat st;
1533 info->Characteristics = 0;
1534 if (fstat( fd, &st ) < 0) return FILE_GetNtStatus();
1535 if (S_ISCHR( st.st_mode ))
1537 info->DeviceType = FILE_DEVICE_UNKNOWN;
1538 #ifdef linux
1539 switch(major(st.st_rdev))
1541 case MEM_MAJOR:
1542 info->DeviceType = FILE_DEVICE_NULL;
1543 break;
1544 case TTY_MAJOR:
1545 info->DeviceType = FILE_DEVICE_SERIAL_PORT;
1546 break;
1547 case LP_MAJOR:
1548 info->DeviceType = FILE_DEVICE_PARALLEL_PORT;
1549 break;
1550 case SCSI_TAPE_MAJOR:
1551 info->DeviceType = FILE_DEVICE_TAPE;
1552 break;
1554 #endif
1556 else if (S_ISBLK( st.st_mode ))
1558 info->DeviceType = FILE_DEVICE_DISK;
1560 else if (S_ISFIFO( st.st_mode ) || S_ISSOCK( st.st_mode ))
1562 info->DeviceType = FILE_DEVICE_NAMED_PIPE;
1564 else /* regular file or directory */
1566 #if defined(linux) && defined(HAVE_FSTATFS)
1567 struct statfs stfs;
1569 /* check for floppy disk */
1570 if (major(st.st_dev) == FLOPPY_MAJOR)
1571 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1573 if (fstatfs( fd, &stfs ) < 0) stfs.f_type = 0;
1574 switch (stfs.f_type)
1576 case 0x9660: /* iso9660 */
1577 case 0x15013346: /* udf */
1578 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1579 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1580 break;
1581 case 0x6969: /* nfs */
1582 case 0x517B: /* smbfs */
1583 case 0x564c: /* ncpfs */
1584 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1585 info->Characteristics |= FILE_REMOTE_DEVICE;
1586 break;
1587 case 0x01021994: /* tmpfs */
1588 case 0x28cd3d45: /* cramfs */
1589 case 0x1373: /* devfs */
1590 case 0x9fa0: /* procfs */
1591 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1592 break;
1593 default:
1594 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1595 break;
1597 #elif defined(__FreeBSD__)
1598 struct statfs stfs;
1600 /* The proper way to do this in FreeBSD seems to be with the
1601 * name rather than the type, since their linux-compatible
1602 * fstatfs call converts the name to one of the Linux types.
1604 if (fstatfs( fd, &stfs ) < 0)
1605 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1606 else if (!strncmp("cd9660", stfs.f_fstypename,
1607 sizeof(stfs.f_fstypename)))
1609 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1610 /* Don't assume read-only, let the mount options set it
1611 * below
1613 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1615 else if (!strncmp("nfs", stfs.f_fstypename,
1616 sizeof(stfs.f_fstypename)))
1618 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1619 info->Characteristics |= FILE_REMOTE_DEVICE;
1621 else if (!strncmp("nwfs", stfs.f_fstypename,
1622 sizeof(stfs.f_fstypename)))
1624 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1625 info->Characteristics |= FILE_REMOTE_DEVICE;
1627 else if (!strncmp("procfs", stfs.f_fstypename,
1628 sizeof(stfs.f_fstypename)))
1629 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1630 else
1631 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1632 if (stfs.f_flags & MNT_RDONLY)
1633 info->Characteristics |= FILE_READ_ONLY_DEVICE;
1634 if (!(stfs.f_flags & MNT_LOCAL))
1636 info->DeviceType = FILE_DEVICE_NETWORK_FILE_SYSTEM;
1637 info->Characteristics |= FILE_REMOTE_DEVICE;
1639 #elif defined (__APPLE__)
1640 struct statfs stfs;
1641 kern_return_t kernResult = KERN_FAILURE;
1642 mach_port_t masterPort;
1643 char bsdName[6]; /* disk#\0 */
1644 const char *name;
1646 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1648 if (fstatfs( fd, &stfs ) < 0) return FILE_GetNtStatus();
1650 /* stfs.f_type is reserved (always set to 0) so use IOKit */
1651 name = stfs.f_mntfromname + strlen(_PATH_DEV);
1652 memcpy( bsdName, name, min(strlen(name)+1,sizeof(bsdName)) );
1653 bsdName[sizeof(bsdName)-1] = 0;
1655 kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
1657 if (kernResult == KERN_SUCCESS)
1659 CFMutableDictionaryRef matching = IOBSDNameMatching(masterPort, 0, bsdName);
1661 if (matching)
1663 CFStringRef type;
1664 CFMutableDictionaryRef properties;
1665 io_service_t devService = IOServiceGetMatchingService(masterPort, matching);
1667 if (IORegistryEntryCreateCFProperties(devService,
1668 &properties,
1669 kCFAllocatorDefault, 0) != KERN_SUCCESS)
1670 return FILE_GetNtStatus(); /* FIXME */
1671 if ( CFEqual(
1672 CFDictionaryGetValue(properties, CFSTR("Removable")),
1673 kCFBooleanTrue)
1674 ) info->Characteristics |= FILE_REMOVABLE_MEDIA;
1676 if ( CFEqual(
1677 CFDictionaryGetValue(properties, CFSTR("Writable")),
1678 kCFBooleanFalse)
1679 ) info->Characteristics |= FILE_READ_ONLY_DEVICE;
1682 NB : mounted disk image (.img/.dmg) don't provide specific type
1684 if ( (type = CFDictionaryGetValue(properties, CFSTR("Type"))) )
1686 if ( CFStringCompare(type, CFSTR("CD-ROM"), 0) == kCFCompareEqualTo
1687 || CFStringCompare(type, CFSTR("DVD-ROM"), 0) == kCFCompareEqualTo
1690 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1694 if (properties)
1695 CFRelease(properties);
1698 #elif defined(sun)
1699 /* Use dkio to work out device types */
1701 # include <sys/dkio.h>
1702 # include <sys/vtoc.h>
1703 struct dk_cinfo dkinf;
1704 int retval = ioctl(fd, DKIOCINFO, &dkinf);
1705 if(retval==-1){
1706 WARN("Unable to get disk device type information - assuming a disk like device\n");
1707 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1709 switch (dkinf.dki_ctype)
1711 case DKC_CDROM:
1712 info->DeviceType = FILE_DEVICE_CD_ROM_FILE_SYSTEM;
1713 info->Characteristics |= FILE_REMOVABLE_MEDIA|FILE_READ_ONLY_DEVICE;
1714 break;
1715 case DKC_NCRFLOPPY:
1716 case DKC_SMSFLOPPY:
1717 case DKC_INTEL82072:
1718 case DKC_INTEL82077:
1719 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1720 info->Characteristics |= FILE_REMOVABLE_MEDIA;
1721 break;
1722 case DKC_MD:
1723 info->DeviceType = FILE_DEVICE_VIRTUAL_DISK;
1724 break;
1725 default:
1726 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1729 #else
1730 static int warned;
1731 if (!warned++) FIXME( "device info not properly supported on this platform\n" );
1732 info->DeviceType = FILE_DEVICE_DISK_FILE_SYSTEM;
1733 #endif
1734 info->Characteristics |= FILE_DEVICE_IS_MOUNTED;
1736 return STATUS_SUCCESS;
1740 /******************************************************************************
1741 * NtQueryVolumeInformationFile [NTDLL.@]
1742 * ZwQueryVolumeInformationFile [NTDLL.@]
1744 * Get volume information for an open file handle.
1746 * PARAMS
1747 * handle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1748 * io [O] Receives information about the operation on return
1749 * buffer [O] Destination for volume information
1750 * length [I] Size of FsInformation
1751 * info_class [I] Type of volume information to set
1753 * RETURNS
1754 * Success: 0. io and buffer are updated.
1755 * Failure: An NTSTATUS error code describing the error.
1757 NTSTATUS WINAPI NtQueryVolumeInformationFile( HANDLE handle, PIO_STATUS_BLOCK io,
1758 PVOID buffer, ULONG length,
1759 FS_INFORMATION_CLASS info_class )
1761 int fd;
1762 struct stat st;
1764 if ((io->u.Status = wine_server_handle_to_fd( handle, 0, &fd, NULL )) != STATUS_SUCCESS)
1765 return io->u.Status;
1767 io->u.Status = STATUS_NOT_IMPLEMENTED;
1768 io->Information = 0;
1770 switch( info_class )
1772 case FileFsVolumeInformation:
1773 FIXME( "%p: volume info not supported\n", handle );
1774 break;
1775 case FileFsLabelInformation:
1776 FIXME( "%p: label info not supported\n", handle );
1777 break;
1778 case FileFsSizeInformation:
1779 if (length < sizeof(FILE_FS_SIZE_INFORMATION))
1780 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1781 else
1783 FILE_FS_SIZE_INFORMATION *info = buffer;
1785 if (fstat( fd, &st ) < 0)
1787 io->u.Status = FILE_GetNtStatus();
1788 break;
1790 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
1792 io->u.Status = STATUS_INVALID_DEVICE_REQUEST;
1794 else
1796 /* Linux's fstatvfs is buggy */
1797 #if !defined(linux) || !defined(HAVE_FSTATFS)
1798 struct statvfs stfs;
1800 if (fstatvfs( fd, &stfs ) < 0)
1802 io->u.Status = FILE_GetNtStatus();
1803 break;
1805 info->BytesPerSector = stfs.f_frsize;
1806 #else
1807 struct statfs stfs;
1808 if (fstatfs( fd, &stfs ) < 0)
1810 io->u.Status = FILE_GetNtStatus();
1811 break;
1813 info->BytesPerSector = stfs.f_bsize;
1814 #endif
1815 info->TotalAllocationUnits.QuadPart = stfs.f_blocks;
1816 info->AvailableAllocationUnits.QuadPart = stfs.f_bavail;
1817 info->SectorsPerAllocationUnit = 1;
1818 io->Information = sizeof(*info);
1819 io->u.Status = STATUS_SUCCESS;
1822 break;
1823 case FileFsDeviceInformation:
1824 if (length < sizeof(FILE_FS_DEVICE_INFORMATION))
1825 io->u.Status = STATUS_BUFFER_TOO_SMALL;
1826 else
1828 FILE_FS_DEVICE_INFORMATION *info = buffer;
1830 if ((io->u.Status = FILE_GetDeviceInfo( fd, info )) == STATUS_SUCCESS)
1831 io->Information = sizeof(*info);
1833 break;
1834 case FileFsAttributeInformation:
1835 FIXME( "%p: attribute info not supported\n", handle );
1836 break;
1837 case FileFsControlInformation:
1838 FIXME( "%p: control info not supported\n", handle );
1839 break;
1840 case FileFsFullSizeInformation:
1841 FIXME( "%p: full size info not supported\n", handle );
1842 break;
1843 case FileFsObjectIdInformation:
1844 FIXME( "%p: object id info not supported\n", handle );
1845 break;
1846 case FileFsMaximumInformation:
1847 FIXME( "%p: maximum info not supported\n", handle );
1848 break;
1849 default:
1850 io->u.Status = STATUS_INVALID_PARAMETER;
1851 break;
1853 wine_server_release_fd( handle, fd );
1854 return io->u.Status;
1858 /******************************************************************
1859 * NtFlushBuffersFile (NTDLL.@)
1861 * Flush any buffered data on an open file handle.
1863 * PARAMS
1864 * FileHandle [I] Handle returned from ZwOpenFile() or ZwCreateFile()
1865 * IoStatusBlock [O] Receives information about the operation on return
1867 * RETURNS
1868 * Success: 0. IoStatusBlock is updated.
1869 * Failure: An NTSTATUS error code describing the error.
1871 NTSTATUS WINAPI NtFlushBuffersFile( HANDLE hFile, IO_STATUS_BLOCK* IoStatusBlock )
1873 NTSTATUS ret;
1874 HANDLE hEvent = NULL;
1876 SERVER_START_REQ( flush_file )
1878 req->handle = hFile;
1879 ret = wine_server_call( req );
1880 hEvent = reply->event;
1882 SERVER_END_REQ;
1883 if (!ret && hEvent)
1885 ret = NtWaitForSingleObject( hEvent, FALSE, NULL );
1886 NtClose( hEvent );
1888 return ret;
1891 /******************************************************************
1892 * NtLockFile (NTDLL.@)
1896 NTSTATUS WINAPI NtLockFile( HANDLE hFile, HANDLE lock_granted_event,
1897 PIO_APC_ROUTINE apc, void* apc_user,
1898 PIO_STATUS_BLOCK io_status, PLARGE_INTEGER offset,
1899 PLARGE_INTEGER count, ULONG* key, BOOLEAN dont_wait,
1900 BOOLEAN exclusive )
1902 NTSTATUS ret;
1903 HANDLE handle;
1904 BOOLEAN async;
1906 if (apc || io_status || key)
1908 FIXME("Unimplemented yet parameter\n");
1909 return STATUS_NOT_IMPLEMENTED;
1912 for (;;)
1914 SERVER_START_REQ( lock_file )
1916 req->handle = hFile;
1917 req->offset_low = offset->u.LowPart;
1918 req->offset_high = offset->u.HighPart;
1919 req->count_low = count->u.LowPart;
1920 req->count_high = count->u.HighPart;
1921 req->shared = !exclusive;
1922 req->wait = !dont_wait;
1923 ret = wine_server_call( req );
1924 handle = reply->handle;
1925 async = reply->overlapped;
1927 SERVER_END_REQ;
1928 if (ret != STATUS_PENDING)
1930 if (!ret && lock_granted_event) NtSetEvent(lock_granted_event, NULL);
1931 return ret;
1934 if (async)
1936 FIXME( "Async I/O lock wait not implemented, might deadlock\n" );
1937 if (handle) NtClose( handle );
1938 return STATUS_PENDING;
1940 if (handle)
1942 NtWaitForSingleObject( handle, FALSE, NULL );
1943 NtClose( handle );
1945 else
1947 LARGE_INTEGER time;
1949 /* Unix lock conflict, sleep a bit and retry */
1950 time.QuadPart = 100 * (ULONGLONG)10000;
1951 time.QuadPart = -time.QuadPart;
1952 NtDelayExecution( FALSE, &time );
1958 /******************************************************************
1959 * NtUnlockFile (NTDLL.@)
1963 NTSTATUS WINAPI NtUnlockFile( HANDLE hFile, PIO_STATUS_BLOCK io_status,
1964 PLARGE_INTEGER offset, PLARGE_INTEGER count,
1965 PULONG key )
1967 NTSTATUS status;
1969 TRACE( "%p %lx%08lx %lx%08lx\n",
1970 hFile, offset->u.HighPart, offset->u.LowPart, count->u.HighPart, count->u.LowPart );
1972 if (io_status || key)
1974 FIXME("Unimplemented yet parameter\n");
1975 return STATUS_NOT_IMPLEMENTED;
1978 SERVER_START_REQ( unlock_file )
1980 req->handle = hFile;
1981 req->offset_low = offset->u.LowPart;
1982 req->offset_high = offset->u.HighPart;
1983 req->count_low = count->u.LowPart;
1984 req->count_high = count->u.HighPart;
1985 status = wine_server_call( req );
1987 SERVER_END_REQ;
1988 return status;
1991 /******************************************************************
1992 * NtCreateNamedPipeFile (NTDLL.@)
1996 NTSTATUS WINAPI NtCreateNamedPipeFile( PHANDLE handle, ULONG access,
1997 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
1998 ULONG sharing, ULONG dispo, ULONG options,
1999 ULONG pipe_type, ULONG read_mode,
2000 ULONG completion_mode, ULONG max_inst,
2001 ULONG inbound_quota, ULONG outbound_quota,
2002 PLARGE_INTEGER timeout)
2004 NTSTATUS status;
2005 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
2007 TRACE("(%p %lx %s %p %lx %ld %lx %ld %ld %ld %ld %ld %ld %p)\n",
2008 handle, access, debugstr_w(attr->ObjectName->Buffer), iosb, sharing, dispo,
2009 options, pipe_type, read_mode, completion_mode, max_inst, inbound_quota,
2010 outbound_quota, timeout);
2012 if (attr->ObjectName->Length < sizeof(leadin) ||
2013 strncmpiW( attr->ObjectName->Buffer,
2014 leadin, sizeof(leadin)/sizeof(leadin[0]) ))
2015 return STATUS_OBJECT_NAME_INVALID;
2016 /* assume we only get relative timeout, and storable in a DWORD as ms */
2017 if (timeout->QuadPart > 0 || (timeout->QuadPart / -10000) >> 32)
2018 FIXME("Wrong time %s\n", wine_dbgstr_longlong(timeout->QuadPart));
2020 SERVER_START_REQ( create_named_pipe )
2022 req->access = access;
2023 req->attributes = (attr) ? attr->Attributes : 0;
2024 req->rootdir = attr ? attr->RootDirectory : 0;
2025 req->options = options;
2026 req->flags =
2027 (pipe_type) ? NAMED_PIPE_MESSAGE_STREAM_WRITE : 0 |
2028 (read_mode) ? NAMED_PIPE_MESSAGE_STREAM_READ : 0 |
2029 (completion_mode) ? NAMED_PIPE_NONBLOCKING_MODE : 0;
2030 req->maxinstances = max_inst;
2031 req->outsize = outbound_quota;
2032 req->insize = inbound_quota;
2033 req->timeout = timeout->QuadPart / -10000;
2034 wine_server_add_data( req, attr->ObjectName->Buffer,
2035 attr->ObjectName->Length );
2036 status = wine_server_call( req );
2037 if (!status) *handle = reply->handle;
2039 SERVER_END_REQ;
2040 return status;
2043 /******************************************************************
2044 * NtDeleteFile (NTDLL.@)
2048 NTSTATUS WINAPI NtDeleteFile( POBJECT_ATTRIBUTES ObjectAttributes )
2050 NTSTATUS status;
2051 HANDLE hFile;
2052 IO_STATUS_BLOCK io;
2054 TRACE("%p\n", ObjectAttributes);
2055 status = NtCreateFile( &hFile, GENERIC_READ | GENERIC_WRITE | DELETE,
2056 ObjectAttributes, &io, NULL, 0,
2057 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2058 FILE_OPEN, FILE_DELETE_ON_CLOSE, NULL, 0 );
2059 if (status == STATUS_SUCCESS) status = NtClose(hFile);
2060 return status;
2063 /******************************************************************
2064 * NtCancelIoFile (NTDLL.@)
2068 NTSTATUS WINAPI NtCancelIoFile( HANDLE hFile, PIO_STATUS_BLOCK io_status )
2070 LARGE_INTEGER timeout;
2072 TRACE("%p %p\n", hFile, io_status );
2074 SERVER_START_REQ( cancel_async )
2076 req->handle = hFile;
2077 wine_server_call( req );
2079 SERVER_END_REQ;
2080 /* Let some APC be run, so that we can run the remaining APCs on hFile
2081 * either the cancelation of the pending one, but also the execution
2082 * of the queued APC, but not yet run. This is needed to ensure proper
2083 * clean-up of allocated data.
2085 timeout.u.LowPart = timeout.u.HighPart = 0;
2086 return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
2089 /******************************************************************************
2090 * NtCreateMailslotFile [NTDLL.@]
2091 * ZwCreateMailslotFile [NTDLL.@]
2093 * PARAMS
2094 * pHandle [O] pointer to receive the handle created
2095 * DesiredAccess [I] access mode (read, write, etc)
2096 * ObjectAttributes [I] fully qualified NT path of the mailslot
2097 * IoStatusBlock [O] receives completion status and other info
2098 * CreateOptions [I]
2099 * MailslotQuota [I]
2100 * MaxMessageSize [I]
2101 * TimeOut [I]
2103 * RETURNS
2104 * An NT status code
2106 NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
2107 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
2108 ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
2109 PLARGE_INTEGER TimeOut)
2111 static const WCHAR leadin[] = {
2112 '\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
2113 NTSTATUS ret;
2115 TRACE("%p %08lx %p %p %08lx %08lx %08lx %p\n",
2116 pHandle, DesiredAccess, attr, IoStatusBlock,
2117 CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
2119 if (attr->ObjectName->Length < sizeof(leadin) ||
2120 strncmpiW( attr->ObjectName->Buffer,
2121 leadin, sizeof(leadin)/sizeof(leadin[0]) ))
2123 return STATUS_OBJECT_NAME_INVALID;
2126 SERVER_START_REQ( create_mailslot )
2128 req->access = DesiredAccess;
2129 req->attributes = (attr) ? attr->Attributes : 0;
2130 req->rootdir = attr ? attr->RootDirectory : 0;
2131 req->max_msgsize = MaxMessageSize;
2132 req->read_timeout = (TimeOut->QuadPart <= 0) ? TimeOut->QuadPart / -10000 : -1;
2133 wine_server_add_data( req, attr->ObjectName->Buffer,
2134 attr->ObjectName->Length );
2135 ret = wine_server_call( req );
2136 if( ret == STATUS_SUCCESS )
2137 *pHandle = reply->handle;
2139 SERVER_END_REQ;
2141 return ret;