2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "wine/port.h"
27 #ifdef HAVE_SYS_ERRNO_H
28 #include <sys/errno.h>
30 #include "wine/debug.h"
31 #include "wine/server.h"
32 #include "ntdll_misc.h"
33 #include "file.h" /* FIXME */
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 /**************************************************************************
42 * NtOpenFile [NTDLL.@]
43 * ZwOpenFile [NTDLL.@]
44 * FUNCTION: Opens a file
46 * FileHandle Variable that receives the file handle on return
47 * DesiredAccess Access desired by the caller to the file
48 * ObjectAttributes Structue describing the file to be opened
49 * IoStatusBlock Receives details about the result of the operation
50 * ShareAccess Type of shared access the caller requires
51 * OpenOptions Options for the file open
53 NTSTATUS WINAPI NtOpenFile(
54 OUT PHANDLE FileHandle,
55 ACCESS_MASK DesiredAccess,
56 POBJECT_ATTRIBUTES ObjectAttributes,
57 OUT PIO_STATUS_BLOCK IoStatusBlock,
63 CHAR szDosDevices[] = "\\DosDevices\\";
64 DOS_FULL_NAME full_name;
67 FIXME("(%p,0x%08lx,%p,%p,0x%08lx,0x%08lx) partial stub\n",
68 FileHandle, DesiredAccess, ObjectAttributes,
69 IoStatusBlock, ShareAccess, OpenOptions);
71 dump_ObjectAttributes (ObjectAttributes);
73 if(ObjectAttributes->RootDirectory)
75 FIXME("Object root directory unknown %x\n",
76 ObjectAttributes->RootDirectory);
77 return STATUS_OBJECT_NAME_NOT_FOUND;
80 /* create an ascii string from the unicode filename */
81 RtlUnicodeToMultiByteSize( &len, ObjectAttributes->ObjectName->Buffer,
82 ObjectAttributes->ObjectName->Length );
83 filename = RtlAllocateHeap( GetProcessHeap(), 0, len + 1);
84 RtlUnicodeToMultiByteN(filename, len, NULL, ObjectAttributes->ObjectName->Buffer,
85 ObjectAttributes->ObjectName->Length );
88 /* FIXME: DOSFS stuff should call here, not vice-versa */
89 if(strncmp(filename, szDosDevices, strlen(szDosDevices)))
90 return STATUS_OBJECT_NAME_NOT_FOUND;
92 /* FIXME: this calls SetLastError() -> bad */
93 if(!DOSFS_GetFullName(&filename[strlen(szDosDevices)], TRUE,
95 return STATUS_OBJECT_NAME_NOT_FOUND;
97 /* FIXME: modify server protocol so
98 create file takes an OBJECT_ATTRIBUTES structure */
99 SERVER_START_REQ( create_file )
101 req->access = DesiredAccess;
103 req->sharing = ShareAccess;
104 req->create = OPEN_EXISTING;
106 req->drive_type = GetDriveTypeA( full_name.short_name );
107 wine_server_add_data( req, full_name.long_name, strlen(full_name.long_name) );
109 r = wine_server_call( req );
110 *FileHandle = reply->handle;
117 /**************************************************************************
118 * NtCreateFile [NTDLL.@]
119 * ZwCreateFile [NTDLL.@]
120 * FUNCTION: Either causes a new file or directory to be created, or it opens
121 * an existing file, device, directory or volume, giving the caller a handle
122 * for the file object. This handle can be used by subsequent calls to
123 * manipulate data within the file or the file object's state of attributes.
125 * FileHandle Points to a variable which receives the file handle on return
126 * DesiredAccess Desired access to the file
127 * ObjectAttributes Structure describing the file
128 * IoStatusBlock Receives information about the operation on return
129 * AllocationSize Initial size of the file in bytes
130 * FileAttributes Attributes to create the file with
131 * ShareAccess Type of shared access the caller would like to the file
132 * CreateDisposition Specifies what to do, depending on whether the file already exists
133 * CreateOptions Options for creating a new file
134 * EaBuffer Undocumented
135 * EaLength Undocumented
137 NTSTATUS WINAPI NtCreateFile(
138 OUT PHANDLE FileHandle,
139 ACCESS_MASK DesiredAccess,
140 POBJECT_ATTRIBUTES ObjectAttributes,
141 OUT PIO_STATUS_BLOCK IoStatusBlock,
142 PLARGE_INTEGER AllocateSize,
143 ULONG FileAttributes,
145 ULONG CreateDisposition,
150 FIXME("(%p,0x%08lx,%p,%p,%p,0x%08lx,0x%08lx,0x%08lx,0x%08lx,%p,0x%08lx) stub\n",
151 FileHandle,DesiredAccess,ObjectAttributes,
152 IoStatusBlock,AllocateSize,FileAttributes,
153 ShareAccess,CreateDisposition,CreateOptions,EaBuffer,EaLength);
154 dump_ObjectAttributes (ObjectAttributes);
158 /* set the last error depending on errno */
159 NTSTATUS NTFILE_errno_to_status(int val)
163 case EAGAIN: return ( STATUS_SHARING_VIOLATION );
165 case EBADF: return ( STATUS_INVALID_HANDLE );
166 case ENOSPC: return ( STATUS_DISK_FULL );
169 case EPERM: return ( STATUS_ACCESS_DENIED );
170 case EROFS: return ( STATUS_MEDIA_WRITE_PROTECTED );
171 case EBUSY: return ( STATUS_FILE_LOCK_CONFLICT );
172 case ENOENT: return ( STATUS_NO_SUCH_FILE );
173 case EISDIR: return ( STATUS_FILE_IS_A_DIRECTORY );
175 case EMFILE: return ( STATUS_NO_MORE_FILES );
176 case EEXIST: return ( STATUS_OBJECT_NAME_COLLISION );
177 case EINVAL: return ( STATUS_INVALID_PARAMETER );
178 case ENOTEMPTY: return ( STATUS_DIRECTORY_NOT_EMPTY );
179 case EIO: return ( STATUS_ACCESS_VIOLATION );
181 perror("file_set_error");
182 return ( STATUS_INVALID_PARAMETER );
186 /******************************************************************************
187 * NtReadFile [NTDLL.@]
188 * ZwReadFile [NTDLL.@]
191 * HANDLE32 FileHandle
192 * HANDLE32 Event OPTIONAL
193 * PIO_APC_ROUTINE ApcRoutine OPTIONAL
194 * PVOID ApcContext OPTIONAL
195 * PIO_STATUS_BLOCK IoStatusBlock
198 * PLARGE_INTEGER ByteOffset OPTIONAL
199 * PULONG Key OPTIONAL
201 * IoStatusBlock->Information contains the number of bytes read on return.
203 NTSTATUS WINAPI NtReadFile (
206 PIO_APC_ROUTINE ApcRoutine,
208 PIO_STATUS_BLOCK IoStatusBlock,
211 PLARGE_INTEGER ByteOffset,
214 int fd, result, flags, ret;
217 FIXME("(0x%08x,0x%08x,%p,%p,%p,%p,0x%08lx,%p,%p),partial stub!\n",
218 FileHandle,EventHandle,ApcRoutine,ApcContext,IoStatusBlock,Buffer,Length,ByteOffset,Key);
220 if (IsBadWritePtr( Buffer, Length ) ||
221 IsBadWritePtr( IoStatusBlock, sizeof *IoStatusBlock) ||
222 IsBadWritePtr( ByteOffset, sizeof *ByteOffset) )
223 return STATUS_ACCESS_VIOLATION;
225 IoStatusBlock->Information = 0;
227 ret = wine_server_handle_to_fd( FileHandle, GENERIC_READ, &fd, &type, &flags );
231 /* FIXME: this code only does synchronous reads so far */
233 /* FIXME: depending on how libc implements this, between two processes
234 there could be a race condition between the seek and read here */
237 result = pread( fd, Buffer, Length, ByteOffset->QuadPart);
239 while ( (result == -1) && ((errno == EAGAIN) || (errno == EINTR)) );
245 return IoStatusBlock->u.Status = NTFILE_errno_to_status(errno);
248 IoStatusBlock->Information = result;
249 IoStatusBlock->u.Status = 0;
251 return STATUS_SUCCESS;
254 /******************************************************************************
255 * NtWriteFile [NTDLL.@]
256 * ZwWriteFile [NTDLL.@]
259 * HANDLE32 FileHandle
260 * HANDLE32 Event OPTIONAL
261 * PIO_APC_ROUTINE ApcRoutine OPTIONAL
262 * PVOID ApcContext OPTIONAL
263 * PIO_STATUS_BLOCK IoStatusBlock
266 * PLARGE_INTEGER ByteOffset OPTIONAL
267 * PULONG Key OPTIONAL
269 NTSTATUS WINAPI NtWriteFile (
272 PIO_APC_ROUTINE ApcRoutine,
274 PIO_STATUS_BLOCK IoStatusBlock,
277 PLARGE_INTEGER ByteOffset,
280 FIXME("(0x%08x,0x%08x,%p,%p,%p,%p,0x%08lx,%p,%p),stub!\n",
281 FileHandle,EventHandle,ApcRoutine,ApcContext,IoStatusBlock,Buffer,Length,ByteOffset,Key);
285 /**************************************************************************
286 * NtDeviceIoControlFile [NTDLL.@]
287 * ZwDeviceIoControlFile [NTDLL.@]
289 NTSTATUS WINAPI NtDeviceIoControlFile(
290 IN HANDLE DeviceHandle,
291 IN HANDLE Event OPTIONAL,
292 IN PIO_APC_ROUTINE UserApcRoutine OPTIONAL,
293 IN PVOID UserApcContext OPTIONAL,
294 OUT PIO_STATUS_BLOCK IoStatusBlock,
295 IN ULONG IoControlCode,
296 IN PVOID InputBuffer,
297 IN ULONG InputBufferSize,
298 OUT PVOID OutputBuffer,
299 IN ULONG OutputBufferSize)
301 FIXME("(0x%08x,0x%08x,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx): empty stub\n",
302 DeviceHandle, Event, UserApcRoutine, UserApcContext,
303 IoStatusBlock, IoControlCode, InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize);
307 /******************************************************************************
308 * NtFsControlFile [NTDLL.@]
309 * ZwFsControlFile [NTDLL.@]
311 NTSTATUS WINAPI NtFsControlFile(
312 IN HANDLE DeviceHandle,
313 IN HANDLE Event OPTIONAL,
314 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
315 IN PVOID ApcContext OPTIONAL,
316 OUT PIO_STATUS_BLOCK IoStatusBlock,
317 IN ULONG IoControlCode,
318 IN PVOID InputBuffer,
319 IN ULONG InputBufferSize,
320 OUT PVOID OutputBuffer,
321 IN ULONG OutputBufferSize)
323 FIXME("(0x%08x,0x%08x,%p,%p,%p,0x%08lx,%p,0x%08lx,%p,0x%08lx): stub\n",
324 DeviceHandle,Event,ApcRoutine,ApcContext,IoStatusBlock,IoControlCode,
325 InputBuffer,InputBufferSize,OutputBuffer,OutputBufferSize);
329 /******************************************************************************
330 * NtSetVolumeInformationFile [NTDLL.@]
331 * ZwSetVolumeInformationFile [NTDLL.@]
333 NTSTATUS WINAPI NtSetVolumeInformationFile(
334 IN HANDLE FileHandle,
335 PIO_STATUS_BLOCK IoStatusBlock,
338 FS_INFORMATION_CLASS FsInformationClass)
340 FIXME("(0x%08x,%p,%p,0x%08lx,0x%08x) stub\n",
341 FileHandle,IoStatusBlock,FsInformation,Length,FsInformationClass);
345 /******************************************************************************
346 * NtQueryInformationFile [NTDLL.@]
347 * ZwQueryInformationFile [NTDLL.@]
349 NTSTATUS WINAPI NtQueryInformationFile(
351 PIO_STATUS_BLOCK IoStatusBlock,
352 PVOID FileInformation,
354 FILE_INFORMATION_CLASS FileInformationClass)
356 FIXME("(0x%08x,%p,%p,0x%08lx,0x%08x),stub!\n",
357 FileHandle,IoStatusBlock,FileInformation,Length,FileInformationClass);
361 /******************************************************************************
362 * NtSetInformationFile [NTDLL.@]
363 * ZwSetInformationFile [NTDLL.@]
365 NTSTATUS WINAPI NtSetInformationFile(
367 PIO_STATUS_BLOCK IoStatusBlock,
368 PVOID FileInformation,
370 FILE_INFORMATION_CLASS FileInformationClass)
372 FIXME("(0x%08x,%p,%p,0x%08lx,0x%08x)\n",
373 FileHandle,IoStatusBlock,FileInformation,Length,FileInformationClass);
377 /******************************************************************************
378 * NtQueryDirectoryFile [NTDLL.@]
379 * ZwQueryDirectoryFile [NTDLL.@]
380 * ZwQueryDirectoryFile
382 NTSTATUS WINAPI NtQueryDirectoryFile(
383 IN HANDLE FileHandle,
384 IN HANDLE Event OPTIONAL,
385 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
386 IN PVOID ApcContext OPTIONAL,
387 OUT PIO_STATUS_BLOCK IoStatusBlock,
388 OUT PVOID FileInformation,
390 IN FILE_INFORMATION_CLASS FileInformationClass,
391 IN BOOLEAN ReturnSingleEntry,
392 IN PUNICODE_STRING FileName OPTIONAL,
393 IN BOOLEAN RestartScan)
395 FIXME("(0x%08x 0x%08x %p %p %p %p 0x%08lx 0x%08x 0x%08x %p 0x%08x\n",
396 FileHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, FileInformation,
397 Length, FileInformationClass, ReturnSingleEntry,
398 debugstr_us(FileName),RestartScan);
402 /******************************************************************************
403 * NtQueryVolumeInformationFile [NTDLL.@]
404 * ZwQueryVolumeInformationFile [NTDLL.@]
406 NTSTATUS WINAPI NtQueryVolumeInformationFile (
407 IN HANDLE FileHandle,
408 OUT PIO_STATUS_BLOCK IoStatusBlock,
409 OUT PVOID FSInformation,
411 IN FS_INFORMATION_CLASS FSInformationClass)
415 FIXME("(0x%08x %p %p 0x%08lx 0x%08x) stub!\n",
416 FileHandle, IoStatusBlock, FSInformation, Length, FSInformationClass);
418 switch ( FSInformationClass )
420 case FileFsVolumeInformation:
421 len = sizeof( FILE_FS_VOLUME_INFORMATION );
423 case FileFsLabelInformation:
427 case FileFsSizeInformation:
428 len = sizeof( FILE_FS_SIZE_INFORMATION );
431 case FileFsDeviceInformation:
432 len = sizeof( FILE_FS_DEVICE_INFORMATION );
434 case FileFsAttributeInformation:
435 len = sizeof( FILE_FS_ATTRIBUTE_INFORMATION );
438 case FileFsControlInformation:
442 case FileFsFullSizeInformation:
446 case FileFsObjectIdInformation:
450 case FileFsMaximumInformation:
456 return STATUS_BUFFER_TOO_SMALL;
458 switch ( FSInformationClass )
460 case FileFsVolumeInformation:
462 case FileFsLabelInformation:
465 case FileFsSizeInformation:
468 case FileFsDeviceInformation:
471 FILE_FS_DEVICE_INFORMATION * DeviceInfo = FSInformation;
472 DeviceInfo->DeviceType = FILE_DEVICE_DISK;
473 DeviceInfo->Characteristics = 0;
476 case FileFsAttributeInformation:
479 case FileFsControlInformation:
482 case FileFsFullSizeInformation:
485 case FileFsObjectIdInformation:
488 case FileFsMaximumInformation:
491 IoStatusBlock->DUMMYUNIONNAME.Status = STATUS_SUCCESS;
492 IoStatusBlock->Information = len;
493 return STATUS_SUCCESS;