- Synchronize up to trunk's revision r57864.
[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 NTSTATUS
39 NtfsMakeAbsoluteFilename(PFILE_OBJECT pFileObject,
40 PWSTR pRelativeFileName,
41 PWSTR *pAbsoluteFilename)
42 {
43 PWSTR rcName;
44 PNTFS_FCB Fcb;
45
46 DPRINT("try related for %S\n", pRelativeFileName);
47 Fcb = pFileObject->FsContext;
48 ASSERT(Fcb);
49
50 /* verify related object is a directory and target name
51 don't start with \. */
52 if (NtfsFCBIsDirectory(Fcb) == FALSE ||
53 pRelativeFileName[0] == L'\\')
54 {
55 return(STATUS_INVALID_PARAMETER);
56 }
57
58 /* construct absolute path name */
59 ASSERT(wcslen (Fcb->PathName) + 1 + wcslen (pRelativeFileName) + 1
60 <= 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 static NTSTATUS
78 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt,
79 PFILE_OBJECT FileObject,
80 PWSTR FileName)
81 /*
82 * FUNCTION: Opens a file
83 */
84 {
85 PNTFS_FCB ParentFcb;
86 PNTFS_FCB Fcb;
87 NTSTATUS Status;
88 PWSTR AbsFileName = NULL;
89
90 DPRINT("NtfsOpenFile(%p, %p, %S)\n", DeviceExt, FileObject, FileName);
91
92 if (FileObject->RelatedFileObject)
93 {
94 DPRINT("Converting relative filename to absolute filename\n");
95
96 Status = NtfsMakeAbsoluteFilename(FileObject->RelatedFileObject,
97 FileName,
98 &AbsFileName);
99 FileName = AbsFileName;
100 if (!NT_SUCCESS(Status))
101 {
102 return(Status);
103 }
104 return(STATUS_UNSUCCESSFUL);
105 }
106
107 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
108
109 DPRINT("PathName to open: %S\n", FileName);
110
111 /* try first to find an existing FCB in memory */
112 DPRINT("Checking for existing FCB in memory\n");
113 Fcb = NtfsGrabFCBFromTable(DeviceExt,
114 FileName);
115 if (Fcb == NULL)
116 {
117 DPRINT("No existing FCB found, making a new one if file exists.\n");
118 Status = NtfsGetFCBForFile(DeviceExt,
119 &ParentFcb,
120 &Fcb,
121 FileName);
122 if (ParentFcb != NULL)
123 {
124 NtfsReleaseFCB(DeviceExt,
125 ParentFcb);
126 }
127
128 if (!NT_SUCCESS (Status))
129 {
130 DPRINT("Could not make a new FCB, status: %x\n", Status);
131
132 if (AbsFileName)
133 ExFreePool(AbsFileName);
134
135 return(Status);
136 }
137 }
138
139 DPRINT("Attaching FCB to fileObject\n");
140 Status = NtfsAttachFCBToFileObject(DeviceExt,
141 Fcb,
142 FileObject);
143
144 if (AbsFileName)
145 ExFreePool (AbsFileName);
146
147 return(Status);
148 }
149
150
151 static NTSTATUS
152 NtfsCreateFile(PDEVICE_OBJECT DeviceObject,
153 PIRP Irp)
154 /*
155 * FUNCTION: Opens a file
156 */
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 NTAPI
207 NtfsFsdCreate(PDEVICE_OBJECT DeviceObject,
208 PIRP Irp)
209 {
210 PDEVICE_EXTENSION DeviceExt;
211 NTSTATUS Status;
212
213 if (DeviceObject == NtfsGlobalData->DeviceObject)
214 {
215 /* DeviceObject represents FileSystem instead of logical volume */
216 DPRINT("Opening file system\n");
217 Irp->IoStatus.Information = FILE_OPENED;
218 Status = STATUS_SUCCESS;
219 goto ByeBye;
220 }
221
222 DeviceExt = DeviceObject->DeviceExtension;
223
224 FsRtlEnterFileSystem();
225 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource,
226 TRUE);
227 Status = NtfsCreateFile(DeviceObject,
228 Irp);
229 ExReleaseResourceLite(&DeviceExt->DirResource);
230 FsRtlExitFileSystem();
231
232 ByeBye:
233 Irp->IoStatus.Status = Status;
234 IoCompleteRequest(Irp,
235 NT_SUCCESS(Status) ? IO_DISK_INCREMENT : IO_NO_INCREMENT);
236
237 return(Status);
238 }
239
240 /* EOF */