branch update for HEAD-2003050101
[reactos.git] / drivers / fs / 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., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /* $Id$
20  *
21  * COPYRIGHT:        See COPYING in the top level directory
22  * PROJECT:          ReactOS kernel
23  * FILE:             services/fs/ntfs/create.c
24  * PURPOSE:          NTFS filesystem driver
25  * PROGRAMMER:       Eric Kohl
26  */
27
28 /* INCLUDES *****************************************************************/
29
30 #include <ddk/ntddk.h>
31
32 //#define NDEBUG
33 #include <debug.h>
34
35 #include "ntfs.h"
36
37
38 /* FUNCTIONS ****************************************************************/
39
40 static NTSTATUS
41 NtfsMakeAbsoluteFilename(PFILE_OBJECT pFileObject,
42                          PWSTR pRelativeFileName,
43                          PWSTR *pAbsoluteFilename)
44 {
45   PWSTR rcName;
46   PFCB Fcb;
47
48   DPRINT("try related for %S\n", pRelativeFileName);
49   Fcb = pFileObject->FsContext;
50   assert(Fcb);
51
52   /* verify related object is a directory and target name
53      don't start with \. */
54   if (NtfsFCBIsDirectory(Fcb) == FALSE ||
55       pRelativeFileName[0] == L'\\')
56     {
57       return(STATUS_INVALID_PARAMETER);
58     }
59
60   /* construct absolute path name */
61   assert(wcslen (Fcb->PathName) + 1 + wcslen (pRelativeFileName) + 1
62           <= MAX_PATH);
63   rcName = ExAllocatePool(NonPagedPool, MAX_PATH * sizeof(WCHAR));
64   if (!rcName)
65     {
66       return(STATUS_INSUFFICIENT_RESOURCES);
67     }
68
69   wcscpy(rcName, Fcb->PathName);
70   if (!NtfsFCBIsRoot(Fcb))
71     wcscat (rcName, L"\\");
72   wcscat (rcName, pRelativeFileName);
73   *pAbsoluteFilename = rcName;
74
75   return(STATUS_SUCCESS);
76 }
77
78
79 static NTSTATUS
80 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt,
81              PFILE_OBJECT FileObject,
82              PWSTR FileName)
83 /*
84  * FUNCTION: Opens a file
85  */
86 {
87   PFCB ParentFcb;
88   PFCB Fcb;
89   NTSTATUS Status;
90   PWSTR AbsFileName = NULL;
91
92   DPRINT("NtfsOpenFile(%08lx, %08lx, %S)\n", DeviceExt, FileObject, FileName);
93
94   if (FileObject->RelatedFileObject)
95     {
96       DPRINT("Converting relative filename to absolute filename\n");
97
98       Status = NtfsMakeAbsoluteFilename(FileObject->RelatedFileObject,
99                                         FileName,
100                                         &AbsFileName);
101       FileName = AbsFileName;
102       if (!NT_SUCCESS(Status))
103         {
104           return(Status);
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 static NTSTATUS
154 NtfsCreateFile(PDEVICE_OBJECT DeviceObject,
155                PIRP Irp)
156 /*
157  * FUNCTION: Opens a file
158  */
159 {
160   PDEVICE_EXTENSION DeviceExt;
161   PIO_STACK_LOCATION Stack;
162   PFILE_OBJECT FileObject;
163   ULONG RequestedDisposition;
164   ULONG RequestedOptions;
165   PFCB Fcb;
166 //  PWSTR FileName;
167   NTSTATUS Status;
168
169   DPRINT("NtfsCreateFile() called\n");
170
171   DeviceExt = DeviceObject->DeviceExtension;
172   assert (DeviceExt);
173   Stack = IoGetCurrentIrpStackLocation (Irp);
174   assert (Stack);
175
176   RequestedDisposition = ((Stack->Parameters.Create.Options >> 24) & 0xff);
177 //  RequestedOptions =
178 //    Stack->Parameters.Create.Options & FILE_VALID_OPTION_FLAGS;
179 //  PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
180 //  if ((RequestedOptions & FILE_DIRECTORY_FILE)
181 //      && RequestedDisposition == FILE_SUPERSEDE)
182 //    return STATUS_INVALID_PARAMETER;
183
184   FileObject = Stack->FileObject;
185
186   if (RequestedDisposition == FILE_CREATE ||
187       RequestedDisposition == FILE_OVERWRITE_IF ||
188       RequestedDisposition == FILE_SUPERSEDE)
189     {
190       return(STATUS_ACCESS_DENIED);
191     }
192
193   Status = NtfsOpenFile(DeviceExt,
194                         FileObject,
195                         FileObject->FileName.Buffer);
196
197   /*
198    * If the directory containing the file to open doesn't exist then
199    * fail immediately
200    */
201   Irp->IoStatus.Information = (NT_SUCCESS(Status)) ? FILE_OPENED : 0;
202   Irp->IoStatus.Status = Status;
203
204   return(Status);
205 }
206
207
208 NTSTATUS STDCALL
209 NtfsCreate(PDEVICE_OBJECT DeviceObject,
210            PIRP Irp)
211 {
212   PDEVICE_EXTENSION DeviceExt;
213   NTSTATUS Status;
214
215   if (DeviceObject == NtfsGlobalData->DeviceObject)
216     {
217       /* DeviceObject represents FileSystem instead of logical volume */
218       DPRINT("Opening file system\n");
219       Irp->IoStatus.Information = FILE_OPENED;
220       Status = STATUS_SUCCESS;
221       goto ByeBye;
222     }
223
224   DeviceExt = DeviceObject->DeviceExtension;
225
226   ExAcquireResourceExclusiveLite(&DeviceExt->DirResource,
227                                  TRUE);
228   Status = NtfsCreateFile(DeviceObject,
229                           Irp);
230   ExReleaseResourceLite(&DeviceExt->DirResource);
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 */