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 Status
= ObReferenceObjectByPointer(DeviceObject
,
73 STANDARD_RIGHTS_REQUIRED
,
76 if (Status
!= STATUS_SUCCESS
)
82 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
84 DPRINT("DeviceObject %x\n",DeviceObject
);
86 if (RemainingPath
== NULL
)
88 FileObject
->Flags
= FileObject
->Flags
| FO_DIRECT_DEVICE_OPEN
;
89 FileObject
->FileName
.Buffer
= ExAllocatePool(NonPagedPool
,
90 (ObjectAttributes
->ObjectName
->Length
+1)*2);
91 FileObject
->FileName
.Length
= ObjectAttributes
->ObjectName
->Length
;
92 FileObject
->FileName
.MaximumLength
=
93 ObjectAttributes
->ObjectName
->MaximumLength
;
94 RtlCopyUnicodeString(&(FileObject
->FileName
),
95 ObjectAttributes
->ObjectName
);
99 if (DeviceObject
->DeviceType
!= FILE_DEVICE_FILE_SYSTEM
&&
100 DeviceObject
->DeviceType
!= FILE_DEVICE_DISK
)
102 return(STATUS_UNSUCCESSFUL
);
104 if (!(DeviceObject
->Vpb
->Flags
& VPB_MOUNTED
))
106 Status
= IoTryToMountStorageDevice(DeviceObject
);
107 if (Status
!=STATUS_SUCCESS
)
111 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
113 RtlInitUnicodeString(&(FileObject
->FileName
),wcsdup(RemainingPath
));
115 DPRINT("FileObject->FileName.Buffer %w\n",FileObject
->FileName
.Buffer
);
116 FileObject
->DeviceObject
= DeviceObject
;
117 FileObject
->Vpb
= DeviceObject
->Vpb
;
118 FileObject
->Type
= ID_FILE_OBJECT
;
120 return(STATUS_SUCCESS
);
123 NTSTATUS STDCALL
ZwCreateFile(PHANDLE FileHandle
,
124 ACCESS_MASK DesiredAccess
,
125 POBJECT_ATTRIBUTES ObjectAttributes
,
126 PIO_STATUS_BLOCK IoStatusBlock
,
127 PLARGE_INTEGER AllocateSize
,
128 ULONG FileAttributes
,
130 ULONG CreateDisposition
,
135 * FUNCTION: Either causes a new file or directory to be created, or it opens
136 * an existing file, device, directory or volume, giving the caller a handle
137 * for the file object. This handle can be used by subsequent calls to
138 * manipulate data within the file or the file object's state of attributes.
140 * FileHandle (OUT) = Points to a variable which receives the file
142 * DesiredAccess = Desired access to the file
143 * ObjectAttributes = Structure describing the file
144 * IoStatusBlock (OUT) = Receives information about the operation on
146 * AllocationSize = Initial size of the file in bytes
147 * FileAttributes = Attributes to create the file with
148 * ShareAccess = Type of shared access the caller would like to the file
149 * CreateDisposition = Specifies what to do, depending on whether the
150 * file already exists
151 * CreateOptions = Options for creating a new file
152 * EaBuffer = Undocumented
153 * EaLength = Undocumented
157 PFILE_OBJECT FileObject
;
161 PIO_STACK_LOCATION StackLoc
;
163 DPRINT("ZwCreateFile(FileHandle %x, DesiredAccess %x, "
164 "ObjectAttributes %x ObjectAttributes->ObjectName->Buffer %w)\n",
165 FileHandle
,DesiredAccess
,ObjectAttributes
,
166 ObjectAttributes
->ObjectName
->Buffer
);
168 assert_irql(PASSIVE_LEVEL
);
172 FileObject
= ObCreateObject(FileHandle
,
176 if (FileObject
== NULL
)
178 return(STATUS_UNSUCCESSFUL
);
181 if (CreateOptions
& FILE_SYNCHRONOUS_IO_ALERT
)
183 FileObject
->Flags
= FileObject
->Flags
| FO_ALERTABLE_IO
;
184 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
186 if (CreateOptions
& FILE_SYNCHRONOUS_IO_NONALERT
)
188 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
191 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
193 Irp
= IoAllocateIrp(FileObject
->DeviceObject
->StackSize
, FALSE
);
196 return(STATUS_UNSUCCESSFUL
);
199 StackLoc
= IoGetNextIrpStackLocation(Irp
);
200 StackLoc
->MajorFunction
= IRP_MJ_CREATE
;
201 StackLoc
->MinorFunction
= 0;
203 StackLoc
->Control
= 0;
204 StackLoc
->DeviceObject
= FileObject
->DeviceObject
;
205 StackLoc
->FileObject
= FileObject
;
206 StackLoc
->Parameters
.Create
.Options
= CreateOptions
&FILE_VALID_OPTION_FLAGS
;
207 StackLoc
->Parameters
.Create
.Options
|= CreateDisposition
<<24;
209 Status
= IoCallDriver(FileObject
->DeviceObject
,Irp
);
210 if (Status
== STATUS_PENDING
)
212 KeWaitForSingleObject(&Event
,Executive
,KernelMode
,FALSE
,NULL
);
213 Status
= IoStatusBlock
->Status
;
216 if (!NT_SUCCESS(Status
))
218 ZwClose(*FileHandle
);
222 DPRINT("Finished ZwCreateFile() (*FileHandle) %x\n",(*FileHandle
));
226 NTSTATUS STDCALL
NtOpenFile(PHANDLE FileHandle
,
227 ACCESS_MASK DesiredAccess
,
228 POBJECT_ATTRIBUTES ObjectAttributes
,
229 PIO_STATUS_BLOCK IoStatusBlock
,
233 return(ZwOpenFile(FileHandle
,
241 NTSTATUS STDCALL
ZwOpenFile(PHANDLE FileHandle
,
242 ACCESS_MASK DesiredAccess
,
243 POBJECT_ATTRIBUTES ObjectAttributes
,
244 PIO_STATUS_BLOCK IoStatusBlock
,
248 * FUNCTION: Opens a file (simpler than ZwCreateFile)
250 * FileHandle (OUT) = Variable that receives the file handle on return
251 * DesiredAccess = Access desired by the caller to the file
252 * ObjectAttributes = Structue describing the file to be opened
253 * IoStatusBlock (OUT) = Receives details about the result of the
255 * ShareAccess = Type of shared access the caller requires
256 * OpenOptions = Options for the file open
261 return(ZwCreateFile(FileHandle
,