3 * Copyright (C) 1998, 1999, 2000, 2001 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystems/fastfat/create.c
22 * PURPOSE: VFAT Filesystem
23 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
24 * Pierre Schweitzer (pierre@reactos.org)
27 /* INCLUDES *****************************************************************/
34 /* FUNCTIONS *****************************************************************/
38 PFAT_DIR_ENTRY pEntry
,
39 PUNICODE_STRING NameU
)
45 RtlCopyMemory(cString
, pEntry
->ShortName
, 11);
47 if (cString
[0] == 0x05)
52 StringA
.Buffer
= cString
;
53 for (StringA
.Length
= 0;
54 StringA
.Length
< 8 && StringA
.Buffer
[StringA
.Length
] != ' ';
56 StringA
.MaximumLength
= StringA
.Length
;
58 RtlOemStringToUnicodeString(NameU
, &StringA
, FALSE
);
60 if (pEntry
->lCase
& VFAT_CASE_LOWER_BASE
)
62 RtlDowncaseUnicodeString(NameU
, NameU
, FALSE
);
65 if (cString
[8] != ' ')
67 Length
= NameU
->Length
;
68 NameU
->Buffer
+= Length
/ sizeof(WCHAR
);
69 if (!FAT_ENTRY_VOLUME(pEntry
))
71 Length
+= sizeof(WCHAR
);
72 NameU
->Buffer
[0] = L
'.';
76 NameU
->MaximumLength
-= Length
;
78 StringA
.Buffer
= &cString
[8];
79 for (StringA
.Length
= 0;
80 StringA
.Length
< 3 && StringA
.Buffer
[StringA
.Length
] != ' ';
82 StringA
.MaximumLength
= StringA
.Length
;
83 RtlOemStringToUnicodeString(NameU
, &StringA
, FALSE
);
84 if (pEntry
->lCase
& VFAT_CASE_LOWER_EXT
)
86 RtlDowncaseUnicodeString(NameU
, NameU
, FALSE
);
88 NameU
->Buffer
-= Length
/ sizeof(WCHAR
);
89 NameU
->Length
+= Length
;
90 NameU
->MaximumLength
+= Length
;
93 NameU
->Buffer
[NameU
->Length
/ sizeof(WCHAR
)] = 0;
94 DPRINT("'%wZ'\n", NameU
);
98 * FUNCTION: Read the volume label
102 PDEVICE_EXTENSION DeviceExt
,
105 PVOID Context
= NULL
;
109 LARGE_INTEGER FileOffset
;
110 UNICODE_STRING NameU
;
112 ULONG EntriesPerPage
;
114 NTSTATUS Status
= STATUS_SUCCESS
;
116 NameU
.Buffer
= Vpb
->VolumeLabel
;
118 NameU
.MaximumLength
= sizeof(Vpb
->VolumeLabel
);
119 *(Vpb
->VolumeLabel
) = 0;
120 Vpb
->VolumeLabelLength
= 0;
122 if (DeviceExt
->Flags
& VCB_IS_FATX
)
124 SizeDirEntry
= sizeof(FATX_DIR_ENTRY
);
125 EntriesPerPage
= FATX_ENTRIES_PER_PAGE
;
129 SizeDirEntry
= sizeof(FAT_DIR_ENTRY
);
130 EntriesPerPage
= FAT_ENTRIES_PER_PAGE
;
133 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
, TRUE
);
134 pFcb
= vfatOpenRootFCB(DeviceExt
);
135 ExReleaseResourceLite(&DeviceExt
->DirResource
);
137 FileOffset
.QuadPart
= 0;
140 CcMapData(pFcb
->FileObject
, &FileOffset
, SizeDirEntry
, MAP_WAIT
, &Context
, (PVOID
*)&Entry
);
142 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
144 Status
= _SEH2_GetExceptionCode();
147 if (NT_SUCCESS(Status
))
151 if (ENTRY_VOLUME(DeviceExt
, Entry
))
153 /* copy volume label */
154 if (DeviceExt
->Flags
& VCB_IS_FATX
)
156 StringO
.Buffer
= (PCHAR
)Entry
->FatX
.Filename
;
157 StringO
.MaximumLength
= StringO
.Length
= Entry
->FatX
.FilenameLength
;
158 RtlOemStringToUnicodeString(&NameU
, &StringO
, FALSE
);
162 vfat8Dot3ToString(&Entry
->Fat
, &NameU
);
164 Vpb
->VolumeLabelLength
= NameU
.Length
;
167 if (ENTRY_END(DeviceExt
, Entry
))
172 Entry
= (PDIR_ENTRY
)((ULONG_PTR
)Entry
+ SizeDirEntry
);
173 if ((DirIndex
% EntriesPerPage
) == 0)
175 CcUnpinData(Context
);
176 FileOffset
.u
.LowPart
+= PAGE_SIZE
;
179 CcMapData(pFcb
->FileObject
, &FileOffset
, SizeDirEntry
, MAP_WAIT
, &Context
, (PVOID
*)&Entry
);
181 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
183 Status
= _SEH2_GetExceptionCode();
186 if (!NT_SUCCESS(Status
))
195 CcUnpinData(Context
);
198 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
, TRUE
);
199 vfatReleaseFCB(DeviceExt
, pFcb
);
200 ExReleaseResourceLite(&DeviceExt
->DirResource
);
202 return STATUS_SUCCESS
;
206 * FUNCTION: Find a file
210 PDEVICE_EXTENSION DeviceExt
,
212 PUNICODE_STRING FileToFindU
,
213 PVFAT_DIRENTRY_CONTEXT DirContext
,
216 PWCHAR PathNameBuffer
;
217 USHORT PathNameBufferLength
;
219 PVOID Context
= NULL
;
223 UNICODE_STRING PathNameU
;
224 UNICODE_STRING FileToFindUpcase
;
227 DPRINT("FindFile(Parent %p, FileToFind '%wZ', DirIndex: %u)\n",
228 Parent
, FileToFindU
, DirContext
->DirIndex
);
229 DPRINT("FindFile: Path %wZ\n",&Parent
->PathNameU
);
231 PathNameBufferLength
= LONGNAME_MAX_LENGTH
* sizeof(WCHAR
);
232 PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameBufferLength
+ sizeof(WCHAR
), TAG_VFAT
);
235 return STATUS_INSUFFICIENT_RESOURCES
;
238 PathNameU
.Buffer
= PathNameBuffer
;
239 PathNameU
.Length
= 0;
240 PathNameU
.MaximumLength
= PathNameBufferLength
;
242 DirContext
->LongNameU
.Length
= 0;
243 DirContext
->ShortNameU
.Length
= 0;
245 WildCard
= FsRtlDoesNameContainWildCards(FileToFindU
);
247 if (WildCard
== FALSE
)
249 /* if there is no '*?' in the search name, than look first for an existing fcb */
250 RtlCopyUnicodeString(&PathNameU
, &Parent
->PathNameU
);
251 if (!vfatFCBIsRoot(Parent
))
253 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = L
'\\';
254 PathNameU
.Length
+= sizeof(WCHAR
);
256 RtlAppendUnicodeStringToString(&PathNameU
, FileToFindU
);
257 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = 0;
258 rcFcb
= vfatGrabFCBFromTable(DeviceExt
, &PathNameU
);
261 ULONG startIndex
= rcFcb
->startIndex
;
262 if ((rcFcb
->Flags
& FCB_IS_FATX_ENTRY
) && !vfatFCBIsRoot(Parent
))
266 if(startIndex
>= DirContext
->DirIndex
)
268 RtlCopyUnicodeString(&DirContext
->LongNameU
, &rcFcb
->LongNameU
);
269 RtlCopyUnicodeString(&DirContext
->ShortNameU
, &rcFcb
->ShortNameU
);
270 RtlCopyMemory(&DirContext
->DirEntry
, &rcFcb
->entry
, sizeof(DIR_ENTRY
));
271 DirContext
->StartIndex
= rcFcb
->startIndex
;
272 DirContext
->DirIndex
= rcFcb
->dirIndex
;
273 DPRINT("FindFile: new Name %wZ, DirIndex %u (%u)\n",
274 &DirContext
->LongNameU
, DirContext
->DirIndex
, DirContext
->StartIndex
);
275 Status
= STATUS_SUCCESS
;
279 DPRINT("FCB not found for %wZ\n", &PathNameU
);
280 Status
= STATUS_UNSUCCESSFUL
;
282 vfatReleaseFCB(DeviceExt
, rcFcb
);
283 ExFreePool(PathNameBuffer
);
288 /* FsRtlIsNameInExpression need the searched string to be upcase,
289 * even if IgnoreCase is specified */
290 Status
= RtlUpcaseUnicodeString(&FileToFindUpcase
, FileToFindU
, TRUE
);
291 if (!NT_SUCCESS(Status
))
293 ExFreePool(PathNameBuffer
);
299 Status
= DeviceExt
->GetNextDirEntry(&Context
, &Page
, Parent
, DirContext
, First
);
301 if (Status
== STATUS_NO_MORE_ENTRIES
)
305 if (ENTRY_VOLUME(DeviceExt
, &DirContext
->DirEntry
))
307 DirContext
->DirIndex
++;
310 if (DirContext
->LongNameU
.Length
== 0 ||
311 DirContext
->ShortNameU
.Length
== 0)
313 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
314 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
316 ASSERT(DirContext
->LongNameU
.Length
!= 0 &&
317 DirContext
->ShortNameU
.Length
!= 0);
319 DirContext
->DirIndex
++;
324 Found
= FsRtlIsNameInExpression(&FileToFindUpcase
, &DirContext
->LongNameU
, TRUE
, NULL
) ||
325 FsRtlIsNameInExpression(&FileToFindUpcase
, &DirContext
->ShortNameU
, TRUE
, NULL
);
329 Found
= FsRtlAreNamesEqual(&DirContext
->LongNameU
, FileToFindU
, TRUE
, NULL
) ||
330 FsRtlAreNamesEqual(&DirContext
->ShortNameU
, FileToFindU
, TRUE
, NULL
);
337 RtlCopyUnicodeString(&PathNameU
, &Parent
->PathNameU
);
338 if (!vfatFCBIsRoot(Parent
))
340 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = L
'\\';
341 PathNameU
.Length
+= sizeof(WCHAR
);
343 RtlAppendUnicodeStringToString(&PathNameU
, &DirContext
->LongNameU
);
344 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = 0;
345 rcFcb
= vfatGrabFCBFromTable(DeviceExt
, &PathNameU
);
348 RtlCopyMemory(&DirContext
->DirEntry
, &rcFcb
->entry
, sizeof(DIR_ENTRY
));
349 vfatReleaseFCB(DeviceExt
, rcFcb
);
352 DPRINT("%u\n", DirContext
->LongNameU
.Length
);
353 DPRINT("FindFile: new Name %wZ, DirIndex %u\n",
354 &DirContext
->LongNameU
, DirContext
->DirIndex
);
358 CcUnpinData(Context
);
360 RtlFreeUnicodeString(&FileToFindUpcase
);
361 ExFreePool(PathNameBuffer
);
362 return STATUS_SUCCESS
;
364 DirContext
->DirIndex
++;
369 CcUnpinData(Context
);
372 RtlFreeUnicodeString(&FileToFindUpcase
);
373 ExFreePool(PathNameBuffer
);
378 * FUNCTION: Opens a file
383 PDEVICE_EXTENSION DeviceExt
,
384 PUNICODE_STRING PathNameU
,
385 PFILE_OBJECT FileObject
,
386 ULONG RequestedDisposition
,
387 ULONG RequestedOptions
,
393 DPRINT("VfatOpenFile(%p, '%wZ', %p, %p)\n", DeviceExt
, PathNameU
, FileObject
, ParentFcb
);
395 if (FileObject
->RelatedFileObject
)
397 DPRINT("'%wZ'\n", &FileObject
->RelatedFileObject
->FileName
);
399 *ParentFcb
= FileObject
->RelatedFileObject
->FsContext
;
406 if (!DeviceExt
->FatInfo
.FixedMedia
)
408 Status
= VfatBlockDeviceIoControl(DeviceExt
->StorageDevice
,
409 IOCTL_DISK_CHECK_VERIFY
,
415 if (!NT_SUCCESS(Status
))
417 DPRINT("Status %lx\n", Status
);
425 vfatGrabFCB(DeviceExt
, *ParentFcb
);
428 /* try first to find an existing FCB in memory */
429 DPRINT("Checking for existing FCB in memory\n");
431 Status
= vfatGetFCBForFile(DeviceExt
, ParentFcb
, &Fcb
, PathNameU
);
432 if (!NT_SUCCESS(Status
))
434 DPRINT ("Could not make a new FCB, status: %x\n", Status
);
438 /* Fail, if we try to overwrite an existing directory */
439 if ((!(RequestedOptions
& FILE_DIRECTORY_FILE
) && (*Fcb
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
)) &&
440 (RequestedDisposition
== FILE_OVERWRITE
||
441 RequestedDisposition
== FILE_OVERWRITE_IF
||
442 RequestedDisposition
== FILE_SUPERSEDE
))
444 vfatReleaseFCB(DeviceExt
, Fcb
);
445 return STATUS_OBJECT_NAME_COLLISION
;
448 if (Fcb
->Flags
& FCB_DELETE_PENDING
)
450 vfatReleaseFCB(DeviceExt
, Fcb
);
451 return STATUS_DELETE_PENDING
;
454 /* Fail, if we try to overwrite a read-only file */
455 if ((*Fcb
->Attributes
& FILE_ATTRIBUTE_READONLY
) &&
456 (RequestedDisposition
== FILE_OVERWRITE
||
457 RequestedDisposition
== FILE_OVERWRITE_IF
))
459 vfatReleaseFCB(DeviceExt
, Fcb
);
460 return STATUS_ACCESS_DENIED
;
463 DPRINT("Attaching FCB to fileObject\n");
464 Status
= vfatAttachFCBToFileObject(DeviceExt
, Fcb
, FileObject
);
465 if (!NT_SUCCESS(Status
))
467 vfatReleaseFCB(DeviceExt
, Fcb
);
473 * FUNCTION: Create or open a file
477 PDEVICE_OBJECT DeviceObject
,
480 PIO_STACK_LOCATION Stack
;
481 PFILE_OBJECT FileObject
;
482 NTSTATUS Status
= STATUS_SUCCESS
;
483 PDEVICE_EXTENSION DeviceExt
;
484 ULONG RequestedDisposition
, RequestedOptions
;
485 PVFATFCB pFcb
= NULL
;
486 PVFATFCB ParentFcb
= NULL
;
488 BOOLEAN PagingFileCreate
;
490 BOOLEAN OpenTargetDir
;
491 UNICODE_STRING FileNameU
;
492 UNICODE_STRING PathNameU
;
495 /* Unpack the various parameters. */
496 Stack
= IoGetCurrentIrpStackLocation(Irp
);
497 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
498 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
499 PagingFileCreate
= (Stack
->Flags
& SL_OPEN_PAGING_FILE
) ? TRUE
: FALSE
;
500 OpenTargetDir
= (Stack
->Flags
& SL_OPEN_TARGET_DIRECTORY
) ? TRUE
: FALSE
;
502 FileObject
= Stack
->FileObject
;
503 DeviceExt
= DeviceObject
->DeviceExtension
;
505 /* Check their validity. */
506 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
507 RequestedDisposition
== FILE_SUPERSEDE
)
509 return STATUS_INVALID_PARAMETER
;
512 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
513 RequestedOptions
& FILE_NON_DIRECTORY_FILE
)
515 return STATUS_INVALID_PARAMETER
;
518 /* Deny create if the volume is locked */
519 if (DeviceExt
->Flags
& VCB_VOLUME_LOCKED
)
521 return STATUS_ACCESS_DENIED
;
524 /* This a open operation for the volume itself */
525 if (FileObject
->FileName
.Length
== 0 &&
526 (FileObject
->RelatedFileObject
== NULL
||
527 FileObject
->RelatedFileObject
->FsContext2
!= NULL
||
528 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
))
530 DPRINT("Volume opening\n");
532 if (RequestedDisposition
!= FILE_OPEN
&&
533 RequestedDisposition
!= FILE_OPEN_IF
)
535 return STATUS_ACCESS_DENIED
;
538 /* In spite of what is shown in WDK, it seems that Windows FAT driver doesn't perform that test */
539 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
541 return STATUS_NOT_A_DIRECTORY
;
547 return STATUS_INVALID_PARAMETER
;
550 pFcb
= DeviceExt
->VolumeFcb
;
552 if (pFcb
->OpenHandleCount
== 0)
554 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
555 Stack
->Parameters
.Create
.ShareAccess
,
557 &pFcb
->FCBShareAccess
);
561 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
562 Stack
->Parameters
.Create
.ShareAccess
,
564 &pFcb
->FCBShareAccess
,
566 if (!NT_SUCCESS(Status
))
572 vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
573 DeviceExt
->OpenHandleCount
++;
574 pFcb
->OpenHandleCount
++;
576 Irp
->IoStatus
.Information
= FILE_OPENED
;
577 return STATUS_SUCCESS
;
580 if (FileObject
->RelatedFileObject
!= NULL
&&
581 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
)
583 ASSERT(FileObject
->FileName
.Length
!= 0);
584 return STATUS_OBJECT_PATH_NOT_FOUND
;
587 /* Check for illegal characters and illegale dot sequences in the file name */
588 PathNameU
= FileObject
->FileName
;
589 c
= PathNameU
.Buffer
+ PathNameU
.Length
/ sizeof(WCHAR
);
592 while (c
-- > PathNameU
.Buffer
)
594 if (*c
== L
'\\' || c
== PathNameU
.Buffer
)
596 if (Dots
&& last
> c
)
598 return STATUS_OBJECT_NAME_INVALID
;
608 if (*c
!= '\\' && vfatIsLongIllegal(*c
))
610 return STATUS_OBJECT_NAME_INVALID
;
614 /* Check if we try to open target directory of root dir */
615 if (OpenTargetDir
&& FileObject
->RelatedFileObject
== NULL
&& PathNameU
.Length
== sizeof(WCHAR
) &&
616 PathNameU
.Buffer
[0] == L
'\\')
618 return STATUS_INVALID_PARAMETER
;
621 if (FileObject
->RelatedFileObject
&& PathNameU
.Length
>= sizeof(WCHAR
) && PathNameU
.Buffer
[0] == L
'\\')
623 return STATUS_OBJECT_NAME_INVALID
;
626 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
628 PathNameU
.Length
-= sizeof(WCHAR
);
631 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
633 return STATUS_OBJECT_NAME_INVALID
;
636 /* Try opening the file. */
639 Status
= VfatOpenFile(DeviceExt
, &PathNameU
, FileObject
, RequestedDisposition
, RequestedOptions
, &ParentFcb
);
644 LONG idx
, FileNameLen
;
646 ParentFcb
= (FileObject
->RelatedFileObject
!= NULL
) ? FileObject
->RelatedFileObject
->FsContext
: NULL
;
649 vfatGrabFCB(DeviceExt
, ParentFcb
);
651 Status
= vfatGetFCBForFile(DeviceExt
, &ParentFcb
, &TargetFcb
, &PathNameU
);
653 if (NT_SUCCESS(Status
))
655 vfatReleaseFCB(DeviceExt
, TargetFcb
);
656 Irp
->IoStatus
.Information
= FILE_EXISTS
;
660 Irp
->IoStatus
.Information
= FILE_DOES_NOT_EXIST
;
663 idx
= FileObject
->FileName
.Length
/ sizeof(WCHAR
) - 1;
665 /* Skip trailing \ - if any */
666 if (PathNameU
.Buffer
[idx
] == L
'\\')
669 PathNameU
.Length
-= sizeof(WCHAR
);
673 while (idx
>= 0 && PathNameU
.Buffer
[idx
] != L
'\\')
678 if (idx
> 0 || PathNameU
.Buffer
[0] == L
'\\')
680 /* We don't want to include / in the name */
681 FileNameLen
= PathNameU
.Length
- ((idx
+ 1) * sizeof(WCHAR
));
683 /* Update FO just to keep file name */
684 /* Skip first slash */
686 FileObject
->FileName
.Length
= FileNameLen
;
687 RtlMoveMemory(&PathNameU
.Buffer
[0], &PathNameU
.Buffer
[idx
], FileObject
->FileName
.Length
);
691 /* This is a relative open and we have only the filename, so open the parent directory
692 * It is in RelatedFileObject
694 ASSERT(FileObject
->RelatedFileObject
!= NULL
);
696 /* No need to modify the FO, it already has the name */
699 /* We're done with opening! */
700 if (ParentFcb
!= NULL
)
702 Status
= vfatAttachFCBToFileObject(DeviceExt
, ParentFcb
, FileObject
);
705 if (NT_SUCCESS(Status
))
707 pFcb
= FileObject
->FsContext
;
708 ASSERT(pFcb
== ParentFcb
);
710 if (pFcb
->OpenHandleCount
== 0)
712 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
713 Stack
->Parameters
.Create
.ShareAccess
,
715 &pFcb
->FCBShareAccess
);
719 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
720 Stack
->Parameters
.Create
.ShareAccess
,
722 &pFcb
->FCBShareAccess
,
724 if (!NT_SUCCESS(Status
))
726 VfatCloseFile(DeviceExt
, FileObject
);
731 pFcb
->OpenHandleCount
++;
732 DeviceExt
->OpenHandleCount
++;
734 else if (ParentFcb
!= NULL
)
736 vfatReleaseFCB(DeviceExt
, ParentFcb
);
743 * If the directory containing the file to open doesn't exist then
746 if (Status
== STATUS_OBJECT_PATH_NOT_FOUND
||
747 Status
== STATUS_INVALID_PARAMETER
||
748 Status
== STATUS_DELETE_PENDING
||
749 Status
== STATUS_ACCESS_DENIED
||
750 Status
== STATUS_OBJECT_NAME_COLLISION
)
754 vfatReleaseFCB(DeviceExt
, ParentFcb
);
759 if (!NT_SUCCESS(Status
) && ParentFcb
== NULL
)
761 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU
, Status
);
765 /* If the file open failed then create the required file */
766 if (!NT_SUCCESS (Status
))
768 if (RequestedDisposition
== FILE_CREATE
||
769 RequestedDisposition
== FILE_OPEN_IF
||
770 RequestedDisposition
== FILE_OVERWRITE_IF
||
771 RequestedDisposition
== FILE_SUPERSEDE
)
773 Attributes
= Stack
->Parameters
.Create
.FileAttributes
& ~FILE_ATTRIBUTE_NORMAL
;
774 if (!(RequestedOptions
& FILE_DIRECTORY_FILE
))
775 Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
776 vfatSplitPathName(&PathNameU
, NULL
, &FileNameU
);
777 Status
= VfatAddEntry(DeviceExt
, &FileNameU
, &pFcb
, ParentFcb
, RequestedOptions
,
778 (UCHAR
)(Attributes
& FILE_ATTRIBUTE_VALID_FLAGS
), NULL
);
779 vfatReleaseFCB(DeviceExt
, ParentFcb
);
780 if (NT_SUCCESS(Status
))
782 Status
= vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
783 if (!NT_SUCCESS(Status
))
785 vfatReleaseFCB(DeviceExt
, pFcb
);
789 Irp
->IoStatus
.Information
= FILE_CREATED
;
790 VfatSetAllocationSizeInformation(FileObject
,
793 &Irp
->Overlay
.AllocationSize
);
794 VfatSetExtendedAttributes(FileObject
,
795 Irp
->AssociatedIrp
.SystemBuffer
,
796 Stack
->Parameters
.Create
.EaLength
);
798 if (PagingFileCreate
)
800 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
812 vfatReleaseFCB(DeviceExt
, ParentFcb
);
821 vfatReleaseFCB(DeviceExt
, ParentFcb
);
824 /* Otherwise fail if the caller wanted to create a new file */
825 if (RequestedDisposition
== FILE_CREATE
)
827 Irp
->IoStatus
.Information
= FILE_EXISTS
;
828 VfatCloseFile(DeviceExt
, FileObject
);
829 return STATUS_OBJECT_NAME_COLLISION
;
832 pFcb
= FileObject
->FsContext
;
834 if (pFcb
->OpenHandleCount
!= 0)
836 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
837 Stack
->Parameters
.Create
.ShareAccess
,
839 &pFcb
->FCBShareAccess
,
841 if (!NT_SUCCESS(Status
))
843 VfatCloseFile(DeviceExt
, FileObject
);
849 * Check the file has the requested attributes
851 if (RequestedOptions
& FILE_NON_DIRECTORY_FILE
&&
852 *pFcb
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
)
854 VfatCloseFile (DeviceExt
, FileObject
);
855 return STATUS_FILE_IS_A_DIRECTORY
;
857 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
858 !(*pFcb
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
))
860 VfatCloseFile (DeviceExt
, FileObject
);
861 return STATUS_NOT_A_DIRECTORY
;
863 #ifndef USE_ROS_CC_AND_FS
864 if (!(*pFcb
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
))
866 if (Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
& FILE_WRITE_DATA
||
867 RequestedDisposition
== FILE_OVERWRITE
||
868 RequestedDisposition
== FILE_OVERWRITE_IF
)
870 if (!MmFlushImageSection(&pFcb
->SectionObjectPointers
, MmFlushForWrite
))
872 DPRINT1("%wZ\n", &pFcb
->PathNameU
);
873 DPRINT1("%d %d %d\n", Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
& FILE_WRITE_DATA
,
874 RequestedDisposition
== FILE_OVERWRITE
, RequestedDisposition
== FILE_OVERWRITE_IF
);
875 VfatCloseFile (DeviceExt
, FileObject
);
876 return STATUS_SHARING_VIOLATION
;
881 if (PagingFileCreate
)
884 * Do more checking for page files. It is possible,
885 * that the file was opened and closed previously
886 * as a normal cached file. In this case, the cache
887 * manager has referenced the fileobject and the fcb
888 * is held in memory. Try to remove the fileobject
889 * from cache manager and use the fcb.
891 if (pFcb
->RefCount
> 1)
893 if(!(pFcb
->Flags
& FCB_IS_PAGE_FILE
))
895 VfatCloseFile(DeviceExt
, FileObject
);
896 return STATUS_INVALID_PARAMETER
;
901 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
906 if (pFcb
->Flags
& FCB_IS_PAGE_FILE
)
908 VfatCloseFile(DeviceExt
, FileObject
);
909 return STATUS_INVALID_PARAMETER
;
913 if (RequestedDisposition
== FILE_OVERWRITE
||
914 RequestedDisposition
== FILE_OVERWRITE_IF
||
915 RequestedDisposition
== FILE_SUPERSEDE
)
917 if (!(*pFcb
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
))
919 *pFcb
->Attributes
= Stack
->Parameters
.Create
.FileAttributes
& ~FILE_ATTRIBUTE_NORMAL
;
920 *pFcb
->Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
921 VfatUpdateEntry(pFcb
);
924 ExAcquireResourceExclusiveLite(&(pFcb
->MainResource
), TRUE
);
925 Status
= VfatSetAllocationSizeInformation(FileObject
,
928 &Irp
->Overlay
.AllocationSize
);
929 ExReleaseResourceLite(&(pFcb
->MainResource
));
930 if (!NT_SUCCESS (Status
))
932 VfatCloseFile(DeviceExt
, FileObject
);
937 if (RequestedDisposition
== FILE_SUPERSEDE
)
939 Irp
->IoStatus
.Information
= FILE_SUPERSEDED
;
941 else if (RequestedDisposition
== FILE_OVERWRITE
||
942 RequestedDisposition
== FILE_OVERWRITE_IF
)
944 Irp
->IoStatus
.Information
= FILE_OVERWRITTEN
;
948 Irp
->IoStatus
.Information
= FILE_OPENED
;
952 if (pFcb
->OpenHandleCount
== 0)
954 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
955 Stack
->Parameters
.Create
.ShareAccess
,
957 &pFcb
->FCBShareAccess
);
961 IoUpdateShareAccess(FileObject
,
962 &pFcb
->FCBShareAccess
);
965 if (Irp
->IoStatus
.Information
== FILE_CREATED
)
967 FsRtlNotifyFullReportChange(DeviceExt
->NotifySync
,
968 &(DeviceExt
->NotifyList
),
969 (PSTRING
)&pFcb
->PathNameU
,
970 pFcb
->PathNameU
.Length
- pFcb
->LongNameU
.Length
,
973 ((*pFcb
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
) ?
974 FILE_NOTIFY_CHANGE_DIR_NAME
: FILE_NOTIFY_CHANGE_FILE_NAME
),
979 pFcb
->OpenHandleCount
++;
980 DeviceExt
->OpenHandleCount
++;
982 /* FIXME : test write access if requested */
988 * FUNCTION: Create or open a file
992 PVFAT_IRP_CONTEXT IrpContext
)
998 if (IrpContext
->DeviceObject
== VfatGlobalData
->DeviceObject
)
1000 /* DeviceObject represents FileSystem instead of logical volume */
1001 DPRINT ("FsdCreate called with file system\n");
1002 IrpContext
->Irp
->IoStatus
.Information
= FILE_OPENED
;
1003 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;
1005 return STATUS_SUCCESS
;
1008 if (!(IrpContext
->Flags
& IRPCONTEXT_CANWAIT
))
1010 return VfatMarkIrpContextForQueue(IrpContext
);
1013 IrpContext
->Irp
->IoStatus
.Information
= 0;
1014 ExAcquireResourceExclusiveLite(&IrpContext
->DeviceExt
->DirResource
, TRUE
);
1015 Status
= VfatCreateFile(IrpContext
->DeviceObject
, IrpContext
->Irp
);
1016 ExReleaseResourceLite(&IrpContext
->DeviceExt
->DirResource
);
1018 if (NT_SUCCESS(Status
))
1019 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;