Sync up with trunk r61578.
[reactos.git] / drivers / filesystems / ntfs / create.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 *
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
24 */
25
26 /* INCLUDES *****************************************************************/
27
28 #include "ntfs.h"
29
30 #define NDEBUG
31 #include <debug.h>
32
33 /* GLOBALS *****************************************************************/
34
35
36 /* FUNCTIONS ****************************************************************/
37
38 static
39 NTSTATUS
40 NtfsMakeAbsoluteFilename(PFILE_OBJECT pFileObject,
41 PWSTR pRelativeFileName,
42 PWSTR *pAbsoluteFilename)
43 {
44 PWSTR rcName;
45 PNTFS_FCB Fcb;
46
47 DPRINT("try related for %S\n", pRelativeFileName);
48 Fcb = pFileObject->FsContext;
49 ASSERT(Fcb);
50
51 /* verify related object is a directory and target name
52 don't start with \. */
53 if (NtfsFCBIsDirectory(Fcb) == FALSE ||
54 pRelativeFileName[0] == L'\\')
55 {
56 return STATUS_INVALID_PARAMETER;
57 }
58
59 /* construct absolute path name */
60 ASSERT(wcslen (Fcb->PathName) + 1 + wcslen (pRelativeFileName) + 1 <= MAX_PATH);
61 rcName = ExAllocatePoolWithTag(NonPagedPool, MAX_PATH * sizeof(WCHAR), TAG_NTFS);
62 if (!rcName)
63 {
64 return STATUS_INSUFFICIENT_RESOURCES;
65 }
66
67 wcscpy(rcName, Fcb->PathName);
68 if (!NtfsFCBIsRoot(Fcb))
69 wcscat (rcName, L"\\");
70 wcscat (rcName, pRelativeFileName);
71 *pAbsoluteFilename = rcName;
72
73 return STATUS_SUCCESS;
74 }
75
76
77 /*
78 * FUNCTION: Opens a file
79 */
80 static
81 NTSTATUS
82 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt,
83 PFILE_OBJECT FileObject,
84 PWSTR FileName)
85 {
86 PNTFS_FCB ParentFcb;
87 PNTFS_FCB Fcb;
88 NTSTATUS Status;
89 PWSTR AbsFileName = NULL;
90
91 DPRINT("NtfsOpenFile(%p, %p, %S)\n", DeviceExt, FileObject, FileName);
92
93 if (FileObject->RelatedFileObject)
94 {
95 DPRINT("Converting relative filename to absolute filename\n");
96
97 Status = NtfsMakeAbsoluteFilename(FileObject->RelatedFileObject,
98 FileName,
99 &AbsFileName);
100 FileName = AbsFileName;
101 if (!NT_SUCCESS(Status))
102 {
103 return Status;
104 }
105
106 return STATUS_UNSUCCESSFUL;
107 }
108
109 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
110
111 DPRINT("PathName to open: %S\n", FileName);
112
113 /* try first to find an existing FCB in memory */
114 DPRINT("Checking for existing FCB in memory\n");
115 Fcb = NtfsGrabFCBFromTable(DeviceExt,
116 FileName);
117 if (Fcb == NULL)
118 {
119 DPRINT("No existing FCB found, making a new one if file exists.\n");
120 Status = NtfsGetFCBForFile(DeviceExt,
121 &ParentFcb,
122 &Fcb,
123 FileName);
124 if (ParentFcb != NULL)
125 {
126 NtfsReleaseFCB(DeviceExt,
127 ParentFcb);
128 }
129
130 if (!NT_SUCCESS (Status))
131 {
132 DPRINT("Could not make a new FCB, status: %x\n", Status);
133
134 if (AbsFileName)
135 ExFreePool(AbsFileName);
136
137 return Status;
138 }
139 }
140
141 DPRINT("Attaching FCB to fileObject\n");
142 Status = NtfsAttachFCBToFileObject(DeviceExt,
143 Fcb,
144 FileObject);
145
146 if (AbsFileName)
147 ExFreePool(AbsFileName);
148
149 return Status;
150 }
151
152
153 /*
154 * FUNCTION: Opens a file
155 */
156 static
157 NTSTATUS
158 NtfsCreateFile(PDEVICE_OBJECT DeviceObject,
159 PIRP Irp)
160 {
161 PDEVICE_EXTENSION DeviceExt;
162 PIO_STACK_LOCATION Stack;
163 PFILE_OBJECT FileObject;
164 ULONG RequestedDisposition;
165 // ULONG RequestedOptions;
166 // PFCB Fcb;
167 // PWSTR FileName;
168 NTSTATUS Status;
169
170 DPRINT("NtfsCreateFile() called\n");
171
172 DeviceExt = DeviceObject->DeviceExtension;
173 ASSERT(DeviceExt);
174 Stack = IoGetCurrentIrpStackLocation (Irp);
175 ASSERT(Stack);
176
177 RequestedDisposition = ((Stack->Parameters.Create.Options >> 24) & 0xff);
178 // RequestedOptions =
179 // Stack->Parameters.Create.Options & FILE_VALID_OPTION_FLAGS;
180 // PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
181 // if ((RequestedOptions & FILE_DIRECTORY_FILE)
182 // && RequestedDisposition == FILE_SUPERSEDE)
183 // return STATUS_INVALID_PARAMETER;
184
185 FileObject = Stack->FileObject;
186
187 if (RequestedDisposition == FILE_CREATE ||
188 RequestedDisposition == FILE_OVERWRITE_IF ||
189 RequestedDisposition == FILE_SUPERSEDE)
190 {
191 return STATUS_ACCESS_DENIED;
192 }
193
194 Status = NtfsOpenFile(DeviceExt,
195 FileObject,
196 FileObject->FileName.Buffer);
197
198 /*
199 * If the directory containing the file to open doesn't exist then
200 * fail immediately
201 */
202 Irp->IoStatus.Information = (NT_SUCCESS(Status)) ? FILE_OPENED : 0;
203 Irp->IoStatus.Status = Status;
204
205 return Status;
206 }
207
208
209 NTSTATUS
210 NTAPI
211 NtfsFsdCreate(PDEVICE_OBJECT DeviceObject,
212 PIRP Irp)
213 {
214 PDEVICE_EXTENSION DeviceExt;
215 NTSTATUS Status;
216
217 if (DeviceObject == NtfsGlobalData->DeviceObject)
218 {
219 /* DeviceObject represents FileSystem instead of logical volume */
220 DPRINT("Opening file system\n");
221 Irp->IoStatus.Information = FILE_OPENED;
222 Status = STATUS_SUCCESS;
223 goto ByeBye;
224 }
225
226 DeviceExt = DeviceObject->DeviceExtension;
227
228 FsRtlEnterFileSystem();
229 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource,
230 TRUE);
231 Status = NtfsCreateFile(DeviceObject,
232 Irp);
233 ExReleaseResourceLite(&DeviceExt->DirResource);
234 FsRtlExitFileSystem();
235
236 ByeBye:
237 Irp->IoStatus.Status = Status;
238 IoCompleteRequest(Irp,
239 NT_SUCCESS(Status) ? IO_DISK_INCREMENT : IO_NO_INCREMENT);
240
241 return Status;
242 }
243
244 /* EOF */