3 * Copyright (C) 2002 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/create.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMER: Eric Kohl
26 /* INCLUDES *****************************************************************/
33 /* FUNCTIONS ****************************************************************/
37 NtfsMakeAbsoluteFilename(PFILE_OBJECT pFileObject
,
38 PWSTR pRelativeFileName
,
39 PWSTR
*pAbsoluteFilename
)
44 DPRINT("try related for %S\n", pRelativeFileName
);
45 Fcb
= pFileObject
->FsContext
;
48 /* verify related object is a directory and target name
49 don't start with \. */
50 if (NtfsFCBIsDirectory(Fcb
) == FALSE
||
51 pRelativeFileName
[0] == L
'\\')
53 return STATUS_INVALID_PARAMETER
;
56 /* construct absolute path name */
57 ASSERT(wcslen (Fcb
->PathName
) + 1 + wcslen (pRelativeFileName
) + 1 <= MAX_PATH
);
58 rcName
= ExAllocatePoolWithTag(NonPagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_NTFS
);
61 return STATUS_INSUFFICIENT_RESOURCES
;
64 wcscpy(rcName
, Fcb
->PathName
);
65 if (!NtfsFCBIsRoot(Fcb
))
66 wcscat (rcName
, L
"\\");
67 wcscat (rcName
, pRelativeFileName
);
68 *pAbsoluteFilename
= rcName
;
70 return STATUS_SUCCESS
;
75 * FUNCTION: Opens a file
79 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt
,
80 PFILE_OBJECT FileObject
,
87 PWSTR AbsFileName
= NULL
;
89 DPRINT1("NtfsOpenFile(%p, %p, %S, %p)\n", DeviceExt
, FileObject
, FileName
, FoundFCB
);
93 if (FileObject
->RelatedFileObject
)
95 DPRINT("Converting relative filename to absolute filename\n");
97 Status
= NtfsMakeAbsoluteFilename(FileObject
->RelatedFileObject
,
100 FileName
= AbsFileName
;
101 if (!NT_SUCCESS(Status
))
106 return STATUS_UNSUCCESSFUL
;
109 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
111 DPRINT("PathName to open: %S\n", FileName
);
113 /* try first to find an existing FCB in memory */
114 DPRINT("Checking for existing FCB in memory\n");
115 Fcb
= NtfsGrabFCBFromTable(DeviceExt
,
119 DPRINT("No existing FCB found, making a new one if file exists.\n");
120 Status
= NtfsGetFCBForFile(DeviceExt
,
124 if (ParentFcb
!= NULL
)
126 NtfsReleaseFCB(DeviceExt
,
130 if (!NT_SUCCESS (Status
))
132 DPRINT("Could not make a new FCB, status: %x\n", Status
);
135 ExFreePool(AbsFileName
);
141 DPRINT("Attaching FCB to fileObject\n");
142 Status
= NtfsAttachFCBToFileObject(DeviceExt
,
147 ExFreePool(AbsFileName
);
156 * FUNCTION: Opens a file
160 NtfsCreateFile(PDEVICE_OBJECT DeviceObject
,
163 PDEVICE_EXTENSION DeviceExt
;
164 PIO_STACK_LOCATION Stack
;
165 PFILE_OBJECT FileObject
;
166 ULONG RequestedDisposition
;
167 ULONG RequestedOptions
;
172 DPRINT1("NtfsCreateFile(%p, %p) called\n", DeviceObject
, Irp
);
174 DeviceExt
= DeviceObject
->DeviceExtension
;
176 Stack
= IoGetCurrentIrpStackLocation (Irp
);
179 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
180 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
181 // PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
182 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
183 RequestedDisposition
== FILE_SUPERSEDE
)
185 return STATUS_INVALID_PARAMETER
;
188 FileObject
= Stack
->FileObject
;
190 if (RequestedDisposition
== FILE_CREATE
||
191 RequestedDisposition
== FILE_OVERWRITE_IF
||
192 RequestedDisposition
== FILE_SUPERSEDE
)
194 return STATUS_ACCESS_DENIED
;
197 /* This a open operation for the volume itself */
198 if (FileObject
->FileName
.Length
== 0 &&
199 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
!= NULL
))
201 if (RequestedDisposition
!= FILE_OPEN
&&
202 RequestedDisposition
!= FILE_OPEN_IF
)
204 return STATUS_ACCESS_DENIED
;
207 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
209 return STATUS_NOT_A_DIRECTORY
;
212 NtfsAttachFCBToFileObject(DeviceExt
, DeviceExt
->VolumeFcb
, FileObject
);
213 DeviceExt
->VolumeFcb
->RefCount
++;
215 Irp
->IoStatus
.Information
= FILE_OPENED
;
216 return STATUS_SUCCESS
;
219 Status
= NtfsOpenFile(DeviceExt
,
221 FileObject
->FileName
.Buffer
,
224 if (NT_SUCCESS(Status
))
226 if (RequestedDisposition
== FILE_CREATE
)
228 Irp
->IoStatus
.Information
= FILE_EXISTS
;
229 NtfsCloseFile(DeviceExt
, FileObject
);
230 return STATUS_OBJECT_NAME_COLLISION
;
233 if (RequestedOptions
& FILE_NON_DIRECTORY_FILE
&&
234 NtfsFCBIsDirectory(Fcb
))
236 NtfsCloseFile(DeviceExt
, FileObject
);
237 return STATUS_FILE_IS_A_DIRECTORY
;
240 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
241 !NtfsFCBIsDirectory(Fcb
))
243 NtfsCloseFile(DeviceExt
, FileObject
);
244 return STATUS_NOT_A_DIRECTORY
;
247 /* HUGLY HACK: remain RO so far... */
248 if (RequestedDisposition
== FILE_OVERWRITE
||
249 RequestedDisposition
== FILE_OVERWRITE_IF
||
250 RequestedDisposition
== FILE_SUPERSEDE
)
252 DPRINT1("Denying write request on NTFS volume\n");
253 NtfsCloseFile(DeviceExt
, FileObject
);
254 return STATUS_ACCESS_DENIED
;
259 /* HUGLY HACK: remain RO so far... */
260 if (RequestedDisposition
== FILE_CREATE
||
261 RequestedDisposition
== FILE_OPEN_IF
||
262 RequestedDisposition
== FILE_OVERWRITE_IF
||
263 RequestedDisposition
== FILE_SUPERSEDE
)
265 DPRINT1("Denying write request on NTFS volume\n");
266 return STATUS_ACCESS_DENIED
;
271 * If the directory containing the file to open doesn't exist then
274 Irp
->IoStatus
.Information
= (NT_SUCCESS(Status
)) ? FILE_OPENED
: 0;
275 Irp
->IoStatus
.Status
= Status
;
283 NtfsFsdCreate(PDEVICE_OBJECT DeviceObject
,
286 PDEVICE_EXTENSION DeviceExt
;
289 if (DeviceObject
== NtfsGlobalData
->DeviceObject
)
291 /* DeviceObject represents FileSystem instead of logical volume */
292 DPRINT("Opening file system\n");
293 Irp
->IoStatus
.Information
= FILE_OPENED
;
294 Status
= STATUS_SUCCESS
;
298 DeviceExt
= DeviceObject
->DeviceExtension
;
300 FsRtlEnterFileSystem();
301 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
,
303 Status
= NtfsCreateFile(DeviceObject
,
305 ExReleaseResourceLite(&DeviceExt
->DirResource
);
306 FsRtlExitFileSystem();
309 Irp
->IoStatus
.Status
= Status
;
310 IoCompleteRequest(Irp
,
311 NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
);