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@cwcom.net)
11 /* INCLUDES ***************************************************************/
14 #include <ddk/ntddk.h>
15 #include <internal/ob.h>
16 #include <internal/io.h>
17 #include <internal/id.h>
18 #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 STDCALL
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
IopCreateFile(PVOID ObjectBody
,
63 POBJECT_ATTRIBUTES ObjectAttributes
)
65 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Parent
;
66 PFILE_OBJECT FileObject
= (PFILE_OBJECT
)ObjectBody
;
69 DPRINT("IopCreateFile(ObjectBody %x, Parent %x, RemainingPath %w)\n",
70 ObjectBody
,Parent
,RemainingPath
);
72 if (DeviceObject
== NULL
)
74 return(STATUS_SUCCESS
);
77 Status
= ObReferenceObjectByPointer(DeviceObject
,
78 STANDARD_RIGHTS_REQUIRED
,
81 if (Status
!= STATUS_SUCCESS
)
87 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
89 DPRINT("DeviceObject %x\n",DeviceObject
);
91 if (RemainingPath
== NULL
)
93 FileObject
->Flags
= FileObject
->Flags
| FO_DIRECT_DEVICE_OPEN
;
94 FileObject
->FileName
.Buffer
= ExAllocatePool(NonPagedPool
,
95 (ObjectAttributes
->ObjectName
->Length
+1)*2);
96 FileObject
->FileName
.Length
= ObjectAttributes
->ObjectName
->Length
;
97 FileObject
->FileName
.MaximumLength
=
98 ObjectAttributes
->ObjectName
->MaximumLength
;
99 RtlCopyUnicodeString(&(FileObject
->FileName
),
100 ObjectAttributes
->ObjectName
);
104 if (DeviceObject
->DeviceType
!= FILE_DEVICE_FILE_SYSTEM
&&
105 DeviceObject
->DeviceType
!= FILE_DEVICE_DISK
)
107 return(STATUS_UNSUCCESSFUL
);
109 if (!(DeviceObject
->Vpb
->Flags
& VPB_MOUNTED
))
111 Status
= IoTryToMountStorageDevice(DeviceObject
);
112 if (Status
!=STATUS_SUCCESS
)
116 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
118 RtlInitUnicodeString(&(FileObject
->FileName
),wcsdup(RemainingPath
));
120 DPRINT("FileObject->FileName.Buffer %w\n",FileObject
->FileName
.Buffer
);
121 FileObject
->DeviceObject
= DeviceObject
;
122 FileObject
->Vpb
= DeviceObject
->Vpb
;
123 FileObject
->Type
= ID_FILE_OBJECT
;
125 return(STATUS_SUCCESS
);
128 PFILE_OBJECT
IoCreateStreamFileObject(PFILE_OBJECT FileObject
,
129 PDEVICE_OBJECT DeviceObject
)
132 PFILE_OBJECT CreatedFileObject
;
134 DbgPrint("IoCreateStreamFileObject(FileObject %x, DeviceObject %x)\n",
135 FileObject
, DeviceObject
);
137 assert_irql(PASSIVE_LEVEL
);
139 CreatedFileObject
= ObCreateObject(&FileHandle
,
140 STANDARD_RIGHTS_REQUIRED
,
143 if (CreatedFileObject
== NULL
)
148 if (FileObject
!= NULL
)
150 DeviceObject
= FileObject
->DeviceObject
;
152 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
153 CreatedFileObject
->DeviceObject
= DeviceObject
;
154 CreatedFileObject
->Vpb
= DeviceObject
->Vpb
;
155 CreatedFileObject
->Type
= ID_FILE_OBJECT
;
156 CreatedFileObject
->Flags
= CreatedFileObject
->Flags
| FO_DIRECT_DEVICE_OPEN
;
160 return(CreatedFileObject
);
163 NTSTATUS STDCALL
ZwCreateFile(PHANDLE FileHandle
,
164 ACCESS_MASK DesiredAccess
,
165 POBJECT_ATTRIBUTES ObjectAttributes
,
166 PIO_STATUS_BLOCK IoStatusBlock
,
167 PLARGE_INTEGER AllocateSize
,
168 ULONG FileAttributes
,
170 ULONG CreateDisposition
,
175 * FUNCTION: Either causes a new file or directory to be created, or it opens
176 * an existing file, device, directory or volume, giving the caller a handle
177 * for the file object. This handle can be used by subsequent calls to
178 * manipulate data within the file or the file object's state of attributes.
180 * FileHandle (OUT) = Points to a variable which receives the file
182 * DesiredAccess = Desired access to the file
183 * ObjectAttributes = Structure describing the file
184 * IoStatusBlock (OUT) = Receives information about the operation on
186 * AllocationSize = Initial size of the file in bytes
187 * FileAttributes = Attributes to create the file with
188 * ShareAccess = Type of shared access the caller would like to the file
189 * CreateDisposition = Specifies what to do, depending on whether the
190 * file already exists
191 * CreateOptions = Options for creating a new file
192 * EaBuffer = Undocumented
193 * EaLength = Undocumented
197 PFILE_OBJECT FileObject
;
201 PIO_STACK_LOCATION StackLoc
;
203 DPRINT("ZwCreateFile(FileHandle %x, DesiredAccess %x, "
204 "ObjectAttributes %x ObjectAttributes->ObjectName->Buffer %w)\n",
205 FileHandle
,DesiredAccess
,ObjectAttributes
,
206 ObjectAttributes
->ObjectName
->Buffer
);
208 assert_irql(PASSIVE_LEVEL
);
212 FileObject
= ObCreateObject(FileHandle
,
216 if (FileObject
== NULL
)
218 return(STATUS_UNSUCCESSFUL
);
221 if (CreateOptions
& FILE_SYNCHRONOUS_IO_ALERT
)
223 FileObject
->Flags
= FileObject
->Flags
| FO_ALERTABLE_IO
;
224 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
226 if (CreateOptions
& FILE_SYNCHRONOUS_IO_NONALERT
)
228 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
231 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
233 Irp
= IoAllocateIrp(FileObject
->DeviceObject
->StackSize
, FALSE
);
236 return(STATUS_UNSUCCESSFUL
);
239 StackLoc
= IoGetNextIrpStackLocation(Irp
);
240 StackLoc
->MajorFunction
= IRP_MJ_CREATE
;
241 StackLoc
->MinorFunction
= 0;
243 StackLoc
->Control
= 0;
244 StackLoc
->DeviceObject
= FileObject
->DeviceObject
;
245 StackLoc
->FileObject
= FileObject
;
246 StackLoc
->Parameters
.Create
.Options
= CreateOptions
&FILE_VALID_OPTION_FLAGS
;
247 StackLoc
->Parameters
.Create
.Options
|= CreateDisposition
<<24;
249 Status
= IoCallDriver(FileObject
->DeviceObject
,Irp
);
250 if (Status
== STATUS_PENDING
)
252 KeWaitForSingleObject(&Event
,Executive
,KernelMode
,FALSE
,NULL
);
253 Status
= IoStatusBlock
->Status
;
256 if (!NT_SUCCESS(Status
))
258 DPRINT("Failing create request with status %x\n",Status
);
259 ZwClose(*FileHandle
);
263 assert_irql(PASSIVE_LEVEL
);
264 DPRINT("Finished ZwCreateFile() (*FileHandle) %x\n",(*FileHandle
));
268 NTSTATUS STDCALL
NtOpenFile(PHANDLE FileHandle
,
269 ACCESS_MASK DesiredAccess
,
270 POBJECT_ATTRIBUTES ObjectAttributes
,
271 PIO_STATUS_BLOCK IoStatusBlock
,
275 return(ZwOpenFile(FileHandle
,
283 NTSTATUS STDCALL
ZwOpenFile(PHANDLE FileHandle
,
284 ACCESS_MASK DesiredAccess
,
285 POBJECT_ATTRIBUTES ObjectAttributes
,
286 PIO_STATUS_BLOCK IoStatusBlock
,
290 * FUNCTION: Opens a file (simpler than ZwCreateFile)
292 * FileHandle (OUT) = Variable that receives the file handle on return
293 * DesiredAccess = Access desired by the caller to the file
294 * ObjectAttributes = Structue describing the file to be opened
295 * IoStatusBlock (OUT) = Receives details about the result of the
297 * ShareAccess = Type of shared access the caller requires
298 * OpenOptions = Options for the file open
303 return(ZwCreateFile(FileHandle
,