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 ***************************************************************/
13 #include <ddk/ntddk.h>
14 #include <internal/ob.h>
15 #include <internal/io.h>
16 #include <internal/id.h>
17 #include <internal/string.h>
20 #include <internal/debug.h>
22 /* FUNCTIONS *************************************************************/
27 IN POBJECT_ATTRIBUTES ObjectAttributes
34 NTSTATUS
IopCreateFile(PVOID ObjectBody
,
37 POBJECT_ATTRIBUTES ObjectAttributes
)
39 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Parent
;
40 PFILE_OBJECT FileObject
= (PFILE_OBJECT
)ObjectBody
;
43 DPRINT("IopCreateFile(ObjectBody %x, Parent %x, RemainingPath %w)\n",
44 ObjectBody
,Parent
,RemainingPath
);
46 if (DeviceObject
== NULL
)
48 return(STATUS_SUCCESS
);
51 Status
= ObReferenceObjectByPointer(DeviceObject
,
52 STANDARD_RIGHTS_REQUIRED
,
55 if (Status
!= STATUS_SUCCESS
)
61 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
63 DPRINT("DeviceObject %x\n",DeviceObject
);
65 if (RemainingPath
== NULL
)
67 FileObject
->Flags
= FileObject
->Flags
| FO_DIRECT_DEVICE_OPEN
;
68 FileObject
->FileName
.Buffer
= ExAllocatePool(NonPagedPool
,
69 (ObjectAttributes
->ObjectName
->Length
+1)*2);
70 FileObject
->FileName
.Length
= ObjectAttributes
->ObjectName
->Length
;
71 FileObject
->FileName
.MaximumLength
=
72 ObjectAttributes
->ObjectName
->MaximumLength
;
73 RtlCopyUnicodeString(&(FileObject
->FileName
),
74 ObjectAttributes
->ObjectName
);
78 if (DeviceObject
->DeviceType
!= FILE_DEVICE_FILE_SYSTEM
&&
79 DeviceObject
->DeviceType
!= FILE_DEVICE_DISK
)
81 return(STATUS_UNSUCCESSFUL
);
83 if (!(DeviceObject
->Vpb
->Flags
& VPB_MOUNTED
))
85 Status
= IoTryToMountStorageDevice(DeviceObject
);
86 if (Status
!=STATUS_SUCCESS
)
90 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
92 RtlInitUnicodeString(&(FileObject
->FileName
),wcsdup(RemainingPath
));
94 DPRINT("FileObject->FileName.Buffer %w\n",FileObject
->FileName
.Buffer
);
95 FileObject
->DeviceObject
= DeviceObject
;
96 FileObject
->Vpb
= DeviceObject
->Vpb
;
97 FileObject
->Type
= ID_FILE_OBJECT
;
99 return(STATUS_SUCCESS
);
102 PFILE_OBJECT
IoCreateStreamFileObject(PFILE_OBJECT FileObject
,
103 PDEVICE_OBJECT DeviceObject
)
106 PFILE_OBJECT CreatedFileObject
;
108 DbgPrint("IoCreateStreamFileObject(FileObject %x, DeviceObject %x)\n",
109 FileObject
, DeviceObject
);
111 assert_irql(PASSIVE_LEVEL
);
113 CreatedFileObject
= ObCreateObject(&FileHandle
,
114 STANDARD_RIGHTS_REQUIRED
,
117 if (CreatedFileObject
== NULL
)
122 if (FileObject
!= NULL
)
124 DeviceObject
= FileObject
->DeviceObject
;
126 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
127 CreatedFileObject
->DeviceObject
= DeviceObject
;
128 CreatedFileObject
->Vpb
= DeviceObject
->Vpb
;
129 CreatedFileObject
->Type
= ID_FILE_OBJECT
;
130 CreatedFileObject
->Flags
= CreatedFileObject
->Flags
| FO_DIRECT_DEVICE_OPEN
;
134 return(CreatedFileObject
);
141 ACCESS_MASK DesiredAccess
,
142 POBJECT_ATTRIBUTES ObjectAttributes
,
143 PIO_STATUS_BLOCK IoStatusBlock
,
144 PLARGE_INTEGER AllocateSize
,
145 ULONG FileAttributes
,
147 ULONG CreateDisposition
,
153 * FUNCTION: Either causes a new file or directory to be created, or it opens
154 * an existing file, device, directory or volume, giving the caller a handle
155 * for the file object. This handle can be used by subsequent calls to
156 * manipulate data within the file or the file object's state of attributes.
158 * FileHandle (OUT) = Points to a variable which receives the file
160 * DesiredAccess = Desired access to the file
161 * ObjectAttributes = Structure describing the file
162 * IoStatusBlock (OUT) = Receives information about the operation on
164 * AllocationSize = Initial size of the file in bytes
165 * FileAttributes = Attributes to create the file with
166 * ShareAccess = Type of shared access the caller would like to the file
167 * CreateDisposition = Specifies what to do, depending on whether the
168 * file already exists
169 * CreateOptions = Options for creating a new file
170 * EaBuffer = Undocumented
171 * EaLength = Undocumented
175 PFILE_OBJECT FileObject
;
179 PIO_STACK_LOCATION StackLoc
;
181 DPRINT("NtCreateFile(FileHandle %x, DesiredAccess %x, "
182 "ObjectAttributes %x ObjectAttributes->ObjectName->Buffer %w)\n",
183 FileHandle
,DesiredAccess
,ObjectAttributes
,
184 ObjectAttributes
->ObjectName
->Buffer
);
186 assert_irql(PASSIVE_LEVEL
);
190 FileObject
= ObCreateObject(FileHandle
,
194 if (FileObject
== NULL
)
196 return(STATUS_UNSUCCESSFUL
);
199 if (CreateOptions
& FILE_SYNCHRONOUS_IO_ALERT
)
201 FileObject
->Flags
= FileObject
->Flags
| FO_ALERTABLE_IO
;
202 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
204 if (CreateOptions
& FILE_SYNCHRONOUS_IO_NONALERT
)
206 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
209 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
211 Irp
= IoAllocateIrp(FileObject
->DeviceObject
->StackSize
, FALSE
);
214 return(STATUS_UNSUCCESSFUL
);
217 StackLoc
= IoGetNextIrpStackLocation(Irp
);
218 StackLoc
->MajorFunction
= IRP_MJ_CREATE
;
219 StackLoc
->MinorFunction
= 0;
221 StackLoc
->Control
= 0;
222 StackLoc
->DeviceObject
= FileObject
->DeviceObject
;
223 StackLoc
->FileObject
= FileObject
;
224 StackLoc
->Parameters
.Create
.Options
= CreateOptions
&FILE_VALID_OPTION_FLAGS
;
225 StackLoc
->Parameters
.Create
.Options
|= CreateDisposition
<<24;
227 Status
= IoCallDriver(FileObject
->DeviceObject
,Irp
);
228 if (Status
== STATUS_PENDING
)
230 KeWaitForSingleObject(&Event
,Executive
,KernelMode
,FALSE
,NULL
);
231 Status
= IoStatusBlock
->Status
;
234 if (!NT_SUCCESS(Status
))
236 DPRINT("Failing create request with status %x\n",Status
);
237 ZwClose(*FileHandle
);
241 assert_irql(PASSIVE_LEVEL
);
242 DPRINT("Finished NtCreateFile() (*FileHandle) %x\n",(*FileHandle
));
251 ACCESS_MASK DesiredAccess
,
252 POBJECT_ATTRIBUTES ObjectAttributes
,
253 PIO_STATUS_BLOCK IoStatusBlock
,
258 * FUNCTION: Opens a file (simpler than ZwCreateFile)
260 * FileHandle (OUT) = Variable that receives the file handle on return
261 * DesiredAccess = Access desired by the caller to the file
262 * ObjectAttributes = Structue describing the file to be opened
263 * IoStatusBlock (OUT) = Receives details about the result of the
265 * ShareAccess = Type of shared access the caller requires
266 * OpenOptions = Options for the file open