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 DPRINT("DeviceObject %x\n",DeviceObject
);
128 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
129 DPRINT("DeviceObject %x\n",DeviceObject
);
131 if (Status
== STATUS_SUCCESS
)
134 FileObject
->Flags
= FileObject
->Flags
| FO_DIRECT_DEVICE_OPEN
;
135 FileObject
->FileName
.Buffer
= ExAllocatePool(NonPagedPool
,
136 ObjectAttributes
->ObjectName
->Length
);
137 FileObject
->FileName
.Length
= ObjectAttributes
->Length
;
138 RtlCopyUnicodeString(&(FileObject
->FileName
),
139 ObjectAttributes
->ObjectName
);
144 DPRINT("DeviceObject %x\n",DeviceObject
);
145 DPRINT("FileHandle %x\n",FileHandle
);
146 if (DeviceObject
->DeviceType
!= FILE_DEVICE_FILE_SYSTEM
&&
147 DeviceObject
->DeviceType
!= FILE_DEVICE_DISK
)
150 ZwClose(*FileHandle
);
152 return(STATUS_UNSUCCESSFUL
);
154 DPRINT("DeviceObject->Vpb %x\n",DeviceObject
->Vpb
);
155 if (!(DeviceObject
->Vpb
->Flags
& VPB_MOUNTED
))
158 Status
= IoTryToMountStorageDevice(DeviceObject
);
159 if (Status
!=STATUS_SUCCESS
)
162 ZwClose(*FileHandle
);
166 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
168 DPRINT("Remainder %x\n",Remainder
);
169 DPRINT("Remainder %w\n",Remainder
);
170 FileObject
->FileName
.Buffer
= ExAllocatePool(NonPagedPool
,
172 RtlInitUnicodeString(&(FileObject
->FileName
),Remainder
);
173 DPRINT("FileObject->FileName.Buffer %x %w\n",
174 FileObject
->FileName
.Buffer
,FileObject
->FileName
.Buffer
);
178 if (CreateOptions
& FILE_SYNCHRONOUS_IO_ALERT
)
180 FileObject
->Flags
= FileObject
->Flags
| FO_ALERTABLE_IO
;
181 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
183 if (CreateOptions
& FILE_SYNCHRONOUS_IO_NONALERT
)
185 FileObject
->Flags
= FileObject
->Flags
| FO_SYNCHRONOUS_IO
;
188 FileObject
->DeviceObject
=DeviceObject
;
189 FileObject
->Vpb
=DeviceObject
->Vpb
;
191 KeInitializeEvent(&Event
,NotificationEvent
,FALSE
);
193 DPRINT("DevObj StackSize %d\n", DeviceObject
->StackSize
);
194 Irp
= IoAllocateIrp(DeviceObject
->StackSize
, FALSE
);
197 ZwClose(*FileHandle
);
199 return(STATUS_UNSUCCESSFUL
);
202 StackLoc
= IoGetNextIrpStackLocation(Irp
);
203 StackLoc
->MajorFunction
= IRP_MJ_CREATE
;
204 StackLoc
->MinorFunction
= 0;
206 StackLoc
->Control
= 0;
207 StackLoc
->DeviceObject
= DeviceObject
;
208 StackLoc
->FileObject
=FileObject
;
209 Status
= IoCallDriver(DeviceObject
,Irp
);
210 if (Status
==STATUS_PENDING
)
212 KeWaitForSingleObject(&Event
,Executive
,KernelMode
,FALSE
,NULL
);
213 Status
= IoStatusBlock
->Status
;
216 if (Status
!=STATUS_SUCCESS
)
218 ZwClose(*FileHandle
);
222 DPRINT("*FileHandle %x\n",*FileHandle
);
228 NTSTATUS
NtOpenFile(PHANDLE FileHandle
,
229 ACCESS_MASK DesiredAccess
,
230 POBJECT_ATTRIBUTES ObjectAttributes
,
231 PIO_STATUS_BLOCK IoStatusBlock
,
235 return(ZwOpenFile(FileHandle
,
243 NTSTATUS
ZwOpenFile(PHANDLE FileHandle
,
244 ACCESS_MASK DesiredAccess
,
245 POBJECT_ATTRIBUTES ObjectAttributes
,
246 PIO_STATUS_BLOCK IoStatusBlock
,
250 * FUNCTION: Opens a file (simpler than ZwCreateFile)
252 * FileHandle (OUT) = Variable that receives the file handle on return
253 * DesiredAccess = Access desired by the caller to the file
254 * ObjectAttributes = Structue describing the file to be opened
255 * IoStatusBlock (OUT) = Receives details about the result of the
257 * ShareAccess = Type of shared access the caller requires
258 * OpenOptions = Options for the file open
263 return(ZwCreateFile(FileHandle
,