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