3 * Copyright (C) 2002, 2003, 2004 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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: create.c,v 1.12 2004/09/14 21:46:39 ekohl Exp $
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: services/fs/cdfs/cdfs.c
24 * PURPOSE: CDROM (ISO 9660) filesystem driver
25 * PROGRAMMER: Art Yerkes
29 /* INCLUDES *****************************************************************/
31 #include <ddk/ntddk.h>
39 /* FUNCTIONS ****************************************************************/
42 CdfsMakeAbsoluteFilename(PFILE_OBJECT FileObject
,
43 PUNICODE_STRING RelativeFileName
,
44 PUNICODE_STRING AbsoluteFileName
)
51 DPRINT("try related for %wZ\n", RelativeFileName
);
52 Fcb
= FileObject
->FsContext
;
55 /* verify related object is a directory and target name
56 don't start with \. */
57 if ((Fcb
->Entry
.FileFlags
& FILE_FLAG_DIRECTORY
) == 0 ||
58 RelativeFileName
->Buffer
[0] == L
'\\')
60 return STATUS_INVALID_PARAMETER
;
63 /* construct absolute path name */
64 Length
= (wcslen(Fcb
->PathName
) * sizeof(WCHAR
)) +
66 RelativeFileName
->Length
+
68 // assert(wcslen (Fcb->PathName) + 1 + wcslen (pRelativeFileName) + 1
70 // rcName = ExAllocatePool(NonPagedPool, MAX_PATH * sizeof(WCHAR));
73 // return(STATUS_INSUFFICIENT_RESOURCES);
75 AbsoluteFileName
->Length
= 0;
76 AbsoluteFileName
->MaximumLength
= Length
;
77 AbsoluteFileName
->Buffer
= ExAllocatePool(NonPagedPool
,
79 if (AbsoluteFileName
->Buffer
== NULL
)
82 return STATUS_INSUFFICIENT_RESOURCES
;
86 // wcscpy(rcName, Fcb->PathName);
87 Status
= RtlAppendUnicodeToString(AbsoluteFileName
,
89 if (!NT_SUCCESS(Status
))
92 RtlFreeUnicodeString(AbsoluteFileName
);
96 if (!CdfsFCBIsRoot(Fcb
))
98 // wcscat (rcName, L"\\");
99 Status
= RtlAppendUnicodeToString(AbsoluteFileName
,
101 if (!NT_SUCCESS(Status
))
104 RtlFreeUnicodeString(AbsoluteFileName
);
109 Status
= RtlAppendUnicodeStringToString(AbsoluteFileName
,
111 if (!NT_SUCCESS(Status
))
114 RtlFreeUnicodeString(AbsoluteFileName
);
118 // wcscat (rcName, pRelativeFileName);
119 // *pAbsoluteFilename = rcName;
121 return STATUS_SUCCESS
;
126 * FUNCTION: Opens a file
129 CdfsOpenFile(PDEVICE_EXTENSION DeviceExt
,
130 PFILE_OBJECT FileObject
,
131 PUNICODE_STRING FileName
)
136 UNICODE_STRING AbsFileName
;
138 DPRINT("CdfsOpenFile(%08lx, %08lx, %wZ)\n", DeviceExt
, FileObject
, FileName
);
140 if (FileObject
->RelatedFileObject
)
142 DPRINT("Converting relative filename to absolute filename\n");
144 Status
= CdfsMakeAbsoluteFilename(FileObject
->RelatedFileObject
,
147 if (!NT_SUCCESS(Status
))
152 FileName
= &AbsFileName
;
154 RtlFreeUnicodeString(&AbsFileName
);
156 return STATUS_UNSUCCESSFUL
;
159 Status
= CdfsDeviceIoControl (DeviceExt
->StorageDevice
,
160 IOCTL_CDROM_CHECK_VERIFY
,
166 DPRINT ("Status %lx\n", Status
);
167 if (Status
== STATUS_VERIFY_REQUIRED
)
169 PDEVICE_OBJECT DeviceToVerify
;
171 DPRINT1 ("Media change detected!\n");
172 DPRINT1 ("Device %p\n", DeviceExt
->VolumeDevice
);
174 DeviceToVerify
= IoGetDeviceToVerify (PsGetCurrentThread ());
175 IoSetDeviceToVerify (PsGetCurrentThread (),
178 Status
= IoVerifyVolume (DeviceToVerify
,
180 if (!NT_SUCCESS(Status
))
182 DPRINT1 ("Status %lx\n", Status
);
186 else if (!NT_SUCCESS(Status
))
188 DPRINT1 ("Status %lx\n", Status
);
192 DPRINT("PathName to open: %wZ\n", FileName
);
194 /* try first to find an existing FCB in memory */
195 DPRINT("Checking for existing FCB in memory\n");
196 Fcb
= CdfsGrabFCBFromTable(DeviceExt
,
200 DPRINT("No existing FCB found, making a new one if file exists.\n");
201 Status
= CdfsGetFCBForFile(DeviceExt
,
205 if (ParentFcb
!= NULL
)
207 CdfsReleaseFCB(DeviceExt
,
211 if (!NT_SUCCESS (Status
))
213 DPRINT("Could not make a new FCB, status: %x\n", Status
);
215 if (FileName
== &AbsFileName
)
216 RtlFreeUnicodeString(&AbsFileName
);
222 DPRINT("Attaching FCB to fileObject\n");
223 Status
= CdfsAttachFCBToFileObject(DeviceExt
,
227 if (FileName
== &AbsFileName
)
228 RtlFreeUnicodeString(&AbsFileName
);
235 * FUNCTION: Opens a file
238 CdfsCreateFile(PDEVICE_OBJECT DeviceObject
,
241 PDEVICE_EXTENSION DeviceExt
;
242 PIO_STACK_LOCATION Stack
;
243 PFILE_OBJECT FileObject
;
244 ULONG RequestedDisposition
;
245 ULONG RequestedOptions
;
249 DPRINT("CdfsCreateFile() called\n");
251 DeviceExt
= DeviceObject
->DeviceExtension
;
253 Stack
= IoGetCurrentIrpStackLocation (Irp
);
256 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
257 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
258 DPRINT("RequestedDisposition %x, RequestedOptions %x\n",
259 RequestedDisposition
, RequestedOptions
);
261 FileObject
= Stack
->FileObject
;
263 if (RequestedDisposition
== FILE_CREATE
||
264 RequestedDisposition
== FILE_OVERWRITE_IF
||
265 RequestedDisposition
== FILE_SUPERSEDE
)
267 return STATUS_ACCESS_DENIED
;
270 Status
= CdfsOpenFile(DeviceExt
,
272 &FileObject
->FileName
);
273 if (NT_SUCCESS(Status
))
275 Fcb
= FileObject
->FsContext
;
277 /* Check whether the file has the requested attributes */
278 if (RequestedOptions
& FILE_NON_DIRECTORY_FILE
&& CdfsFCBIsDirectory(Fcb
))
280 CdfsCloseFile (DeviceExt
, FileObject
);
281 return STATUS_FILE_IS_A_DIRECTORY
;
284 if (RequestedOptions
& FILE_DIRECTORY_FILE
&& !CdfsFCBIsDirectory(Fcb
))
286 CdfsCloseFile (DeviceExt
, FileObject
);
287 return STATUS_NOT_A_DIRECTORY
;
292 * If the directory containing the file to open doesn't exist then
295 Irp
->IoStatus
.Information
= (NT_SUCCESS(Status
)) ? FILE_OPENED
: 0;
296 Irp
->IoStatus
.Status
= Status
;
303 CdfsCreate(PDEVICE_OBJECT DeviceObject
,
306 PDEVICE_EXTENSION DeviceExt
;
309 if (DeviceObject
== CdfsGlobalData
->DeviceObject
)
311 /* DeviceObject represents FileSystem instead of logical volume */
312 DPRINT("Opening file system\n");
313 Irp
->IoStatus
.Information
= FILE_OPENED
;
314 Status
= STATUS_SUCCESS
;
318 DeviceExt
= DeviceObject
->DeviceExtension
;
320 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
,
322 Status
= CdfsCreateFile(DeviceObject
,
324 ExReleaseResourceLite(&DeviceExt
->DirResource
);
327 Irp
->IoStatus
.Status
= Status
;
328 IoCompleteRequest(Irp
,
329 NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
);