2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/io/create.c
5 * PURPOSE: Handling file create/open apis
6 * PROGRAMMER: David Welch (welch@mcmail.com)
11 /* INCLUDES ***************************************************************/
14 #include <ddk/ntddk.h>
15 #include <internal/ob.h>
16 #include <internal/io.h>
17 #include <internal/string.h>
21 #include <internal/debug.h>
23 /* FUNCTIONS *************************************************************/
25 NTSTATUS STDCALL
NtDeleteFile(IN POBJECT_ATTRIBUTES ObjectAttributes
)
27 return(ZwDeleteFile(ObjectAttributes
));
30 NTSTATUS STDCALL
ZwDeleteFile(IN POBJECT_ATTRIBUTES ObjectAttributes
)
35 NTSTATUS
NtCreateFile(PHANDLE FileHandle
,
36 ACCESS_MASK DesiredAccess
,
37 POBJECT_ATTRIBUTES ObjectAttributes
,
38 PIO_STATUS_BLOCK IoStatusBlock
,
39 PLARGE_INTEGER AllocateSize
,
42 ULONG CreateDisposition
,
47 return(ZwCreateFile(FileHandle
,
60 NTSTATUS
ZwCreateFile(PHANDLE FileHandle
,
61 ACCESS_MASK DesiredAccess
,
62 POBJECT_ATTRIBUTES ObjectAttributes
,
63 PIO_STATUS_BLOCK IoStatusBlock
,
64 PLARGE_INTEGER AllocateSize
,
67 ULONG CreateDisposition
,
72 * FUNCTION: Either causes a new file or directory to be created, or it opens
73 * an existing file, device, directory or volume, giving the caller a handle
74 * for the file object. This handle can be used by subsequent calls to
75 * manipulate data within the file or the file object's state of attributes.
77 * FileHandle (OUT) = Points to a variable which receives the file
79 * DesiredAccess = Desired access to the file
80 * ObjectAttributes = Structure describing the file
81 * IoStatusBlock (OUT) = Receives information about the operation on
83 * AllocationSize = Initial size of the file in bytes
84 * FileAttributes = Attributes to create the file with
85 * ShareAccess = Type of shared access the caller would like to the file
86 * CreateDisposition = Specifies what to do, depending on whether the
88 * CreateOptions = Options for creating a new file
89 * EaBuffer = Undocumented
90 * EaLength = Undocumented
98 PDEVICE_OBJECT DeviceObject
;
99 PFILE_OBJECT FileObject
;
100 PIO_STACK_LOCATION StackLoc
;
103 DPRINT("ZwCreateFile(FileHandle %x, DesiredAccess %x, "
104 "ObjectAttributes %x ObjectAttributes->ObjectName->Buffer %w)\n",
105 FileHandle
,DesiredAccess
,ObjectAttributes
,
106 ObjectAttributes
->ObjectName
->Buffer
);
108 assert_irql(PASSIVE_LEVEL
);
112 FileObject
= ObGenericCreateObject(FileHandle
,DesiredAccess
,NULL
,IoFileType
);
113 memset(FileObject
,0,sizeof(FILE_OBJECT
));
115 Status
= ObOpenObjectByName(ObjectAttributes
,&Object
,&Remainder
);
117 if (Status
!= STATUS_SUCCESS
&& Status
!= STATUS_FS_QUERY_REQUIRED
)
119 DPRINT("%s() = Failed to find object\n",__FUNCTION__
);
120 ZwClose(*FileHandle
);
122 return(STATUS_UNSUCCESSFUL
);
126 DeviceObject
= (PDEVICE_OBJECT
)Object
;
127 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
128 DPRINT("DeviceObject %x\n",DeviceObject
);
130 if (Status
== STATUS_SUCCESS
)
133 FileObject
->Flags
= FileObject
->Flags
| FO_DIRECT_DEVICE_OPEN
;
134 FileObject
->FileName
.Buffer
= ExAllocatePool(NonPagedPool
,
135 ObjectAttributes
->ObjectName
->Length
);
136 FileObject
->FileName
.Length
= ObjectAttributes
->Length
;
137 RtlCopyUnicodeString(&(FileObject
->FileName
),
138 ObjectAttributes
->ObjectName
);
143 if (DeviceObject
->DeviceType
!= FILE_DEVICE_FILE_SYSTEM
&&
144 DeviceObject
->DeviceType
!= FILE_DEVICE_DISK
)
146 ZwClose(*FileHandle
);
148 return(STATUS_UNSUCCESSFUL
);
150 if (!(DeviceObject
->Vpb
->Flags
& VPB_MOUNTED
))
152 Status
= IoTryToMountStorageDevice(DeviceObject
);
153 if (Status
!=STATUS_SUCCESS
)
155 ZwClose(*FileHandle
);
159 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
161 DPRINT("Remainder %w\n",Remainder
);
162 FileObject
->FileName
.Buffer
= ExAllocatePool(NonPagedPool
,
164 RtlInitUnicodeString(&(FileObject
->FileName
),Remainder
);
165 DPRINT("FileObject->FileName.Buffer %x %w\n",
166 FileObject
->FileName
.Buffer
,FileObject
->FileName
.Buffer
);
170 if (CreateOptions
& FILE_SYNCHRONOUS_IO_ALERT
)
172 FileObject
->Flags
= FileObject
->Flags
| FO_ALERTABLE_IO
;
173 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
175 if (CreateOptions
& FILE_SYNCHRONOUS_IO_NONALERT
)
177 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
180 FileObject
->DeviceObject
=DeviceObject
;
181 FileObject
->Vpb
=DeviceObject
->Vpb
;
183 KeInitializeEvent(&Event
,NotificationEvent
,FALSE
);
185 Irp
= IoAllocateIrp(DeviceObject
->StackSize
, FALSE
);
188 ZwClose(*FileHandle
);
190 return(STATUS_UNSUCCESSFUL
);
193 StackLoc
= IoGetNextIrpStackLocation(Irp
);
194 StackLoc
->MajorFunction
= IRP_MJ_CREATE
;
195 StackLoc
->MinorFunction
= 0;
197 StackLoc
->Control
= 0;
198 StackLoc
->DeviceObject
= DeviceObject
;
199 StackLoc
->FileObject
=FileObject
;
200 Status
= IoCallDriver(DeviceObject
,Irp
);
201 if (Status
==STATUS_PENDING
)
203 KeWaitForSingleObject(&Event
,Executive
,KernelMode
,FALSE
,NULL
);
204 Status
= IoStatusBlock
->Status
;
207 if (Status
!=STATUS_SUCCESS
)
209 ZwClose(*FileHandle
);
213 DPRINT("*FileHandle %x\n",*FileHandle
);
219 NTSTATUS
NtOpenFile(PHANDLE FileHandle
,
220 ACCESS_MASK DesiredAccess
,
221 POBJECT_ATTRIBUTES ObjectAttributes
,
222 PIO_STATUS_BLOCK IoStatusBlock
,
226 return(ZwOpenFile(FileHandle
,
234 NTSTATUS
ZwOpenFile(PHANDLE FileHandle
,
235 ACCESS_MASK DesiredAccess
,
236 POBJECT_ATTRIBUTES ObjectAttributes
,
237 PIO_STATUS_BLOCK IoStatusBlock
,
241 * FUNCTION: Opens a file (simpler than ZwCreateFile)
243 * FileHandle (OUT) = Variable that receives the file handle on return
244 * DesiredAccess = Access desired by the caller to the file
245 * ObjectAttributes = Structue describing the file to be opened
246 * IoStatusBlock (OUT) = Receives details about the result of the
248 * ShareAccess = Type of shared access the caller requires
249 * OpenOptions = Options for the file open
254 return(ZwCreateFile(FileHandle
,