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 (BooleanFlagOn(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 (BooleanFlagOn(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: Find a file
102 PDEVICE_EXTENSION DeviceExt
,
104 PUNICODE_STRING FileToFindU
,
105 PVFAT_DIRENTRY_CONTEXT DirContext
,
108 PWCHAR PathNameBuffer
;
109 USHORT PathNameBufferLength
;
111 PVOID Context
= NULL
;
115 UNICODE_STRING PathNameU
;
116 UNICODE_STRING FileToFindUpcase
;
118 BOOLEAN IsFatX
= vfatVolumeIsFatX(DeviceExt
);
120 DPRINT("FindFile(Parent %p, FileToFind '%wZ', DirIndex: %u)\n",
121 Parent
, FileToFindU
, DirContext
->DirIndex
);
122 DPRINT("FindFile: Path %wZ\n",&Parent
->PathNameU
);
124 PathNameBufferLength
= LONGNAME_MAX_LENGTH
* sizeof(WCHAR
);
125 PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameBufferLength
+ sizeof(WCHAR
), TAG_VFAT
);
128 return STATUS_INSUFFICIENT_RESOURCES
;
131 PathNameU
.Buffer
= PathNameBuffer
;
132 PathNameU
.Length
= 0;
133 PathNameU
.MaximumLength
= PathNameBufferLength
;
135 DirContext
->LongNameU
.Length
= 0;
136 DirContext
->ShortNameU
.Length
= 0;
138 WildCard
= FsRtlDoesNameContainWildCards(FileToFindU
);
140 if (WildCard
== FALSE
)
142 /* if there is no '*?' in the search name, than look first for an existing fcb */
143 RtlCopyUnicodeString(&PathNameU
, &Parent
->PathNameU
);
144 if (!vfatFCBIsRoot(Parent
))
146 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = L
'\\';
147 PathNameU
.Length
+= sizeof(WCHAR
);
149 RtlAppendUnicodeStringToString(&PathNameU
, FileToFindU
);
150 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = 0;
151 rcFcb
= vfatGrabFCBFromTable(DeviceExt
, &PathNameU
);
154 ULONG startIndex
= rcFcb
->startIndex
;
155 if (IsFatX
&& !vfatFCBIsRoot(Parent
))
159 if(startIndex
>= DirContext
->DirIndex
)
161 RtlCopyUnicodeString(&DirContext
->LongNameU
, &rcFcb
->LongNameU
);
162 RtlCopyUnicodeString(&DirContext
->ShortNameU
, &rcFcb
->ShortNameU
);
163 RtlCopyMemory(&DirContext
->DirEntry
, &rcFcb
->entry
, sizeof(DIR_ENTRY
));
164 DirContext
->StartIndex
= rcFcb
->startIndex
;
165 DirContext
->DirIndex
= rcFcb
->dirIndex
;
166 DPRINT("FindFile: new Name %wZ, DirIndex %u (%u)\n",
167 &DirContext
->LongNameU
, DirContext
->DirIndex
, DirContext
->StartIndex
);
168 Status
= STATUS_SUCCESS
;
172 DPRINT("FCB not found for %wZ\n", &PathNameU
);
173 Status
= STATUS_UNSUCCESSFUL
;
175 vfatReleaseFCB(DeviceExt
, rcFcb
);
176 ExFreePool(PathNameBuffer
);
181 /* FsRtlIsNameInExpression need the searched string to be upcase,
182 * even if IgnoreCase is specified */
183 Status
= RtlUpcaseUnicodeString(&FileToFindUpcase
, FileToFindU
, TRUE
);
184 if (!NT_SUCCESS(Status
))
186 ExFreePool(PathNameBuffer
);
192 Status
= VfatGetNextDirEntry(DeviceExt
, &Context
, &Page
, Parent
, DirContext
, First
);
194 if (Status
== STATUS_NO_MORE_ENTRIES
)
198 if (ENTRY_VOLUME(IsFatX
, &DirContext
->DirEntry
))
200 DirContext
->DirIndex
++;
203 if (DirContext
->LongNameU
.Length
== 0 ||
204 DirContext
->ShortNameU
.Length
== 0)
206 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
207 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
209 ASSERT(DirContext
->LongNameU
.Length
!= 0 &&
210 DirContext
->ShortNameU
.Length
!= 0);
212 DirContext
->DirIndex
++;
217 Found
= FsRtlIsNameInExpression(&FileToFindUpcase
, &DirContext
->LongNameU
, TRUE
, NULL
) ||
218 FsRtlIsNameInExpression(&FileToFindUpcase
, &DirContext
->ShortNameU
, TRUE
, NULL
);
222 Found
= FsRtlAreNamesEqual(&DirContext
->LongNameU
, FileToFindU
, TRUE
, NULL
) ||
223 FsRtlAreNamesEqual(&DirContext
->ShortNameU
, FileToFindU
, TRUE
, NULL
);
230 RtlCopyUnicodeString(&PathNameU
, &Parent
->PathNameU
);
231 if (!vfatFCBIsRoot(Parent
))
233 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = L
'\\';
234 PathNameU
.Length
+= sizeof(WCHAR
);
236 RtlAppendUnicodeStringToString(&PathNameU
, &DirContext
->LongNameU
);
237 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = 0;
238 rcFcb
= vfatGrabFCBFromTable(DeviceExt
, &PathNameU
);
241 RtlCopyMemory(&DirContext
->DirEntry
, &rcFcb
->entry
, sizeof(DIR_ENTRY
));
242 vfatReleaseFCB(DeviceExt
, rcFcb
);
245 DPRINT("%u\n", DirContext
->LongNameU
.Length
);
246 DPRINT("FindFile: new Name %wZ, DirIndex %u\n",
247 &DirContext
->LongNameU
, DirContext
->DirIndex
);
251 CcUnpinData(Context
);
253 RtlFreeUnicodeString(&FileToFindUpcase
);
254 ExFreePool(PathNameBuffer
);
255 return STATUS_SUCCESS
;
257 DirContext
->DirIndex
++;
262 CcUnpinData(Context
);
265 RtlFreeUnicodeString(&FileToFindUpcase
);
266 ExFreePool(PathNameBuffer
);
271 * FUNCTION: Opens a file
276 PDEVICE_EXTENSION DeviceExt
,
277 PUNICODE_STRING PathNameU
,
278 PFILE_OBJECT FileObject
,
279 ULONG RequestedDisposition
,
280 ULONG RequestedOptions
,
286 DPRINT("VfatOpenFile(%p, '%wZ', %p, %p)\n", DeviceExt
, PathNameU
, FileObject
, ParentFcb
);
288 if (FileObject
->RelatedFileObject
)
290 DPRINT("'%wZ'\n", &FileObject
->RelatedFileObject
->FileName
);
292 *ParentFcb
= FileObject
->RelatedFileObject
->FsContext
;
299 if (!DeviceExt
->FatInfo
.FixedMedia
)
301 Status
= VfatBlockDeviceIoControl(DeviceExt
->StorageDevice
,
302 IOCTL_DISK_CHECK_VERIFY
,
308 if (!NT_SUCCESS(Status
))
310 DPRINT("Status %lx\n", Status
);
318 vfatGrabFCB(DeviceExt
, *ParentFcb
);
321 /* try first to find an existing FCB in memory */
322 DPRINT("Checking for existing FCB in memory\n");
324 Status
= vfatGetFCBForFile(DeviceExt
, ParentFcb
, &Fcb
, PathNameU
);
325 if (!NT_SUCCESS(Status
))
327 DPRINT ("Could not make a new FCB, status: %x\n", Status
);
331 /* Fail, if we try to overwrite an existing directory */
332 if ((!BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) && vfatFCBIsDirectory(Fcb
)) &&
333 (RequestedDisposition
== FILE_OVERWRITE
||
334 RequestedDisposition
== FILE_OVERWRITE_IF
||
335 RequestedDisposition
== FILE_SUPERSEDE
))
337 vfatReleaseFCB(DeviceExt
, Fcb
);
338 return STATUS_OBJECT_NAME_COLLISION
;
341 if (BooleanFlagOn(Fcb
->Flags
, FCB_DELETE_PENDING
))
343 vfatReleaseFCB(DeviceExt
, Fcb
);
344 return STATUS_DELETE_PENDING
;
347 /* Fail, if we try to overwrite a read-only file */
348 if (vfatFCBIsReadOnly(Fcb
) &&
349 (RequestedDisposition
== FILE_OVERWRITE
||
350 RequestedDisposition
== FILE_OVERWRITE_IF
))
352 vfatReleaseFCB(DeviceExt
, Fcb
);
353 return STATUS_ACCESS_DENIED
;
356 if (vfatFCBIsReadOnly(Fcb
) &&
357 (RequestedOptions
& FILE_DELETE_ON_CLOSE
))
359 vfatReleaseFCB(DeviceExt
, Fcb
);
360 return STATUS_CANNOT_DELETE
;
363 if ((vfatFCBIsRoot(Fcb
) ||
364 (Fcb
->LongNameU
.Length
== sizeof(WCHAR
) && Fcb
->LongNameU
.Buffer
[0] == L
'.') ||
365 (Fcb
->LongNameU
.Length
== 2 * sizeof(WCHAR
) && Fcb
->LongNameU
.Buffer
[0] == L
'.' && Fcb
->LongNameU
.Buffer
[1] == L
'.')) &&
366 BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
368 // we cannot delete a '.', '..' or the root directory
369 vfatReleaseFCB(DeviceExt
, Fcb
);
370 return STATUS_CANNOT_DELETE
;
373 DPRINT("Attaching FCB to fileObject\n");
374 Status
= vfatAttachFCBToFileObject(DeviceExt
, Fcb
, FileObject
);
375 if (!NT_SUCCESS(Status
))
377 vfatReleaseFCB(DeviceExt
, Fcb
);
383 * FUNCTION: Create or open a file
387 PDEVICE_OBJECT DeviceObject
,
390 PIO_STACK_LOCATION Stack
;
391 PFILE_OBJECT FileObject
;
392 NTSTATUS Status
= STATUS_SUCCESS
;
393 PDEVICE_EXTENSION DeviceExt
;
394 ULONG RequestedDisposition
, RequestedOptions
;
395 PVFATFCB pFcb
= NULL
;
396 PVFATFCB ParentFcb
= NULL
;
398 BOOLEAN PagingFileCreate
;
400 BOOLEAN OpenTargetDir
;
401 BOOLEAN TrailingBackslash
;
402 UNICODE_STRING FileNameU
;
403 UNICODE_STRING PathNameU
;
406 /* Unpack the various parameters. */
407 Stack
= IoGetCurrentIrpStackLocation(Irp
);
408 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
409 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
410 PagingFileCreate
= BooleanFlagOn(Stack
->Flags
, SL_OPEN_PAGING_FILE
);
411 OpenTargetDir
= BooleanFlagOn(Stack
->Flags
, SL_OPEN_TARGET_DIRECTORY
);
413 FileObject
= Stack
->FileObject
;
414 DeviceExt
= DeviceObject
->DeviceExtension
;
416 if (BooleanFlagOn(Stack
->Parameters
.Create
.Options
, FILE_OPEN_BY_FILE_ID
))
418 return STATUS_NOT_IMPLEMENTED
;
421 /* Check their validity. */
422 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
423 RequestedDisposition
== FILE_SUPERSEDE
)
425 return STATUS_INVALID_PARAMETER
;
428 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
429 BooleanFlagOn(RequestedOptions
, FILE_NON_DIRECTORY_FILE
))
431 return STATUS_INVALID_PARAMETER
;
434 /* Deny create if the volume is locked */
435 if (BooleanFlagOn(DeviceExt
->Flags
, VCB_VOLUME_LOCKED
))
437 return STATUS_ACCESS_DENIED
;
440 /* This a open operation for the volume itself */
441 if (FileObject
->FileName
.Length
== 0 &&
442 (FileObject
->RelatedFileObject
== NULL
||
443 FileObject
->RelatedFileObject
->FsContext2
!= NULL
||
444 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
))
446 DPRINT("Volume opening\n");
448 if (RequestedDisposition
!= FILE_OPEN
&&
449 RequestedDisposition
!= FILE_OPEN_IF
)
451 return STATUS_ACCESS_DENIED
;
454 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
455 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
== NULL
|| FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
))
457 return STATUS_NOT_A_DIRECTORY
;
462 return STATUS_INVALID_PARAMETER
;
465 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
467 return STATUS_CANNOT_DELETE
;
470 pFcb
= DeviceExt
->VolumeFcb
;
472 if (pFcb
->OpenHandleCount
== 0)
474 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
475 Stack
->Parameters
.Create
.ShareAccess
,
477 &pFcb
->FCBShareAccess
);
481 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
482 Stack
->Parameters
.Create
.ShareAccess
,
484 &pFcb
->FCBShareAccess
,
486 if (!NT_SUCCESS(Status
))
492 vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
493 DeviceExt
->OpenHandleCount
++;
494 pFcb
->OpenHandleCount
++;
496 Irp
->IoStatus
.Information
= FILE_OPENED
;
497 return STATUS_SUCCESS
;
500 if (FileObject
->RelatedFileObject
!= NULL
&&
501 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
)
503 ASSERT(FileObject
->FileName
.Length
!= 0);
504 return STATUS_OBJECT_PATH_NOT_FOUND
;
507 /* Check for illegal characters and illegal dot sequences in the file name */
508 PathNameU
= FileObject
->FileName
;
509 c
= PathNameU
.Buffer
+ PathNameU
.Length
/ sizeof(WCHAR
);
513 while (c
-- > PathNameU
.Buffer
)
515 if (*c
== L
'\\' || c
== PathNameU
.Buffer
)
517 if (Dots
&& last
> c
)
519 return STATUS_OBJECT_NAME_INVALID
;
521 if (*c
== L
'\\' && (c
- 1) > PathNameU
.Buffer
&&
524 return STATUS_OBJECT_NAME_INVALID
;
535 if (*c
!= '\\' && vfatIsLongIllegal(*c
))
537 return STATUS_OBJECT_NAME_INVALID
;
541 /* Check if we try to open target directory of root dir */
542 if (OpenTargetDir
&& FileObject
->RelatedFileObject
== NULL
&& PathNameU
.Length
== sizeof(WCHAR
) &&
543 PathNameU
.Buffer
[0] == L
'\\')
545 return STATUS_INVALID_PARAMETER
;
548 if (FileObject
->RelatedFileObject
&& PathNameU
.Length
>= sizeof(WCHAR
) && PathNameU
.Buffer
[0] == L
'\\')
550 return STATUS_OBJECT_NAME_INVALID
;
553 TrailingBackslash
= FALSE
;
554 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
556 PathNameU
.Length
-= sizeof(WCHAR
);
557 TrailingBackslash
= TRUE
;
560 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
562 return STATUS_OBJECT_NAME_INVALID
;
565 /* Try opening the file. */
568 Status
= VfatOpenFile(DeviceExt
, &PathNameU
, FileObject
, RequestedDisposition
, RequestedOptions
, &ParentFcb
);
573 LONG idx
, FileNameLen
;
575 ParentFcb
= (FileObject
->RelatedFileObject
!= NULL
) ? FileObject
->RelatedFileObject
->FsContext
: NULL
;
578 vfatGrabFCB(DeviceExt
, ParentFcb
);
580 Status
= vfatGetFCBForFile(DeviceExt
, &ParentFcb
, &TargetFcb
, &PathNameU
);
582 if (NT_SUCCESS(Status
))
584 vfatReleaseFCB(DeviceExt
, TargetFcb
);
585 Irp
->IoStatus
.Information
= FILE_EXISTS
;
589 Irp
->IoStatus
.Information
= FILE_DOES_NOT_EXIST
;
592 idx
= FileObject
->FileName
.Length
/ sizeof(WCHAR
) - 1;
594 /* Skip trailing \ - if any */
595 if (PathNameU
.Buffer
[idx
] == L
'\\')
598 PathNameU
.Length
-= sizeof(WCHAR
);
602 while (idx
>= 0 && PathNameU
.Buffer
[idx
] != L
'\\')
607 if (idx
> 0 || PathNameU
.Buffer
[0] == L
'\\')
609 /* We don't want to include / in the name */
610 FileNameLen
= PathNameU
.Length
- ((idx
+ 1) * sizeof(WCHAR
));
612 /* Update FO just to keep file name */
613 /* Skip first slash */
615 FileObject
->FileName
.Length
= FileNameLen
;
616 RtlMoveMemory(&PathNameU
.Buffer
[0], &PathNameU
.Buffer
[idx
], FileObject
->FileName
.Length
);
620 /* This is a relative open and we have only the filename, so open the parent directory
621 * It is in RelatedFileObject
623 ASSERT(FileObject
->RelatedFileObject
!= NULL
);
625 /* No need to modify the FO, it already has the name */
628 /* We're done with opening! */
629 if (ParentFcb
!= NULL
)
631 Status
= vfatAttachFCBToFileObject(DeviceExt
, ParentFcb
, FileObject
);
634 if (NT_SUCCESS(Status
))
636 pFcb
= FileObject
->FsContext
;
637 ASSERT(pFcb
== ParentFcb
);
639 if (pFcb
->OpenHandleCount
== 0)
641 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
642 Stack
->Parameters
.Create
.ShareAccess
,
644 &pFcb
->FCBShareAccess
);
648 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
649 Stack
->Parameters
.Create
.ShareAccess
,
651 &pFcb
->FCBShareAccess
,
653 if (!NT_SUCCESS(Status
))
655 VfatCloseFile(DeviceExt
, FileObject
);
660 pFcb
->OpenHandleCount
++;
661 DeviceExt
->OpenHandleCount
++;
663 else if (ParentFcb
!= NULL
)
665 vfatReleaseFCB(DeviceExt
, ParentFcb
);
672 * If the directory containing the file to open doesn't exist then
675 if (Status
== STATUS_OBJECT_PATH_NOT_FOUND
||
676 Status
== STATUS_INVALID_PARAMETER
||
677 Status
== STATUS_DELETE_PENDING
||
678 Status
== STATUS_ACCESS_DENIED
||
679 Status
== STATUS_OBJECT_NAME_COLLISION
)
683 vfatReleaseFCB(DeviceExt
, ParentFcb
);
688 if (!NT_SUCCESS(Status
) && ParentFcb
== NULL
)
690 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU
, Status
);
694 /* If the file open failed then create the required file */
695 if (!NT_SUCCESS (Status
))
697 if (RequestedDisposition
== FILE_CREATE
||
698 RequestedDisposition
== FILE_OPEN_IF
||
699 RequestedDisposition
== FILE_OVERWRITE_IF
||
700 RequestedDisposition
== FILE_SUPERSEDE
)
702 Attributes
= Stack
->Parameters
.Create
.FileAttributes
& ~FILE_ATTRIBUTE_NORMAL
;
703 if (!BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
))
705 if (TrailingBackslash
)
707 vfatReleaseFCB(DeviceExt
, ParentFcb
);
708 return STATUS_OBJECT_NAME_INVALID
;
710 Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
712 vfatSplitPathName(&PathNameU
, NULL
, &FileNameU
);
713 Status
= VfatAddEntry(DeviceExt
, &FileNameU
, &pFcb
, ParentFcb
, RequestedOptions
,
714 (UCHAR
)FlagOn(Attributes
, FILE_ATTRIBUTE_VALID_FLAGS
), NULL
);
715 vfatReleaseFCB(DeviceExt
, ParentFcb
);
716 if (NT_SUCCESS(Status
))
718 Status
= vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
719 if (!NT_SUCCESS(Status
))
721 vfatReleaseFCB(DeviceExt
, pFcb
);
725 Irp
->IoStatus
.Information
= FILE_CREATED
;
726 VfatSetAllocationSizeInformation(FileObject
,
729 &Irp
->Overlay
.AllocationSize
);
730 VfatSetExtendedAttributes(FileObject
,
731 Irp
->AssociatedIrp
.SystemBuffer
,
732 Stack
->Parameters
.Create
.EaLength
);
734 if (PagingFileCreate
)
736 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
746 vfatReleaseFCB(DeviceExt
, ParentFcb
);
754 vfatReleaseFCB(DeviceExt
, ParentFcb
);
757 pFcb
= FileObject
->FsContext
;
759 /* Otherwise fail if the caller wanted to create a new file */
760 if (RequestedDisposition
== FILE_CREATE
)
762 VfatCloseFile(DeviceExt
, FileObject
);
763 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
765 return STATUS_OBJECT_NAME_INVALID
;
767 Irp
->IoStatus
.Information
= FILE_EXISTS
;
768 return STATUS_OBJECT_NAME_COLLISION
;
771 if (pFcb
->OpenHandleCount
!= 0)
773 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
774 Stack
->Parameters
.Create
.ShareAccess
,
776 &pFcb
->FCBShareAccess
,
778 if (!NT_SUCCESS(Status
))
780 VfatCloseFile(DeviceExt
, FileObject
);
786 * Check the file has the requested attributes
788 if (BooleanFlagOn(RequestedOptions
, FILE_NON_DIRECTORY_FILE
) &&
789 vfatFCBIsDirectory(pFcb
))
791 VfatCloseFile (DeviceExt
, FileObject
);
792 return STATUS_FILE_IS_A_DIRECTORY
;
794 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
795 !vfatFCBIsDirectory(pFcb
))
797 VfatCloseFile (DeviceExt
, FileObject
);
798 return STATUS_NOT_A_DIRECTORY
;
800 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
802 VfatCloseFile (DeviceExt
, FileObject
);
803 return STATUS_OBJECT_NAME_INVALID
;
805 #ifndef USE_ROS_CC_AND_FS
806 if (!vfatFCBIsDirectory(pFcb
))
808 if (BooleanFlagOn(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
, FILE_WRITE_DATA
) ||
809 RequestedDisposition
== FILE_OVERWRITE
||
810 RequestedDisposition
== FILE_OVERWRITE_IF
||
811 (RequestedOptions
& FILE_DELETE_ON_CLOSE
))
813 if (!MmFlushImageSection(&pFcb
->SectionObjectPointers
, MmFlushForWrite
))
815 DPRINT1("%wZ\n", &pFcb
->PathNameU
);
816 DPRINT1("%d %d %d\n", Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
& FILE_WRITE_DATA
,
817 RequestedDisposition
== FILE_OVERWRITE
, RequestedDisposition
== FILE_OVERWRITE_IF
);
818 VfatCloseFile (DeviceExt
, FileObject
);
819 return (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
)) ? STATUS_CANNOT_DELETE
820 : STATUS_SHARING_VIOLATION
;
825 if (PagingFileCreate
)
828 * Do more checking for page files. It is possible,
829 * that the file was opened and closed previously
830 * as a normal cached file. In this case, the cache
831 * manager has referenced the fileobject and the fcb
832 * is held in memory. Try to remove the fileobject
833 * from cache manager and use the fcb.
835 if (pFcb
->RefCount
> 1)
837 if(!BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
839 VfatCloseFile(DeviceExt
, FileObject
);
840 return STATUS_INVALID_PARAMETER
;
845 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
850 if (BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
852 VfatCloseFile(DeviceExt
, FileObject
);
853 return STATUS_INVALID_PARAMETER
;
857 if (RequestedDisposition
== FILE_OVERWRITE
||
858 RequestedDisposition
== FILE_OVERWRITE_IF
||
859 RequestedDisposition
== FILE_SUPERSEDE
)
861 if (!vfatFCBIsDirectory(pFcb
))
863 *pFcb
->Attributes
= Stack
->Parameters
.Create
.FileAttributes
& ~FILE_ATTRIBUTE_NORMAL
;
864 *pFcb
->Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
865 VfatUpdateEntry(pFcb
, vfatVolumeIsFatX(DeviceExt
));
868 ExAcquireResourceExclusiveLite(&(pFcb
->MainResource
), TRUE
);
869 Status
= VfatSetAllocationSizeInformation(FileObject
,
872 &Irp
->Overlay
.AllocationSize
);
873 ExReleaseResourceLite(&(pFcb
->MainResource
));
874 if (!NT_SUCCESS (Status
))
876 VfatCloseFile(DeviceExt
, FileObject
);
881 if (RequestedDisposition
== FILE_SUPERSEDE
)
883 Irp
->IoStatus
.Information
= FILE_SUPERSEDED
;
885 else if (RequestedDisposition
== FILE_OVERWRITE
||
886 RequestedDisposition
== FILE_OVERWRITE_IF
)
888 Irp
->IoStatus
.Information
= FILE_OVERWRITTEN
;
892 Irp
->IoStatus
.Information
= FILE_OPENED
;
896 if (pFcb
->OpenHandleCount
== 0)
898 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
899 Stack
->Parameters
.Create
.ShareAccess
,
901 &pFcb
->FCBShareAccess
);
905 IoUpdateShareAccess(FileObject
,
906 &pFcb
->FCBShareAccess
);
909 if (Irp
->IoStatus
.Information
== FILE_CREATED
)
911 FsRtlNotifyFullReportChange(DeviceExt
->NotifySync
,
912 &(DeviceExt
->NotifyList
),
913 (PSTRING
)&pFcb
->PathNameU
,
914 pFcb
->PathNameU
.Length
- pFcb
->LongNameU
.Length
,
917 (vfatFCBIsDirectory(pFcb
) ?
918 FILE_NOTIFY_CHANGE_DIR_NAME
: FILE_NOTIFY_CHANGE_FILE_NAME
),
923 pFcb
->OpenHandleCount
++;
924 DeviceExt
->OpenHandleCount
++;
926 /* FIXME : test write access if requested */
932 * FUNCTION: Create or open a file
936 PVFAT_IRP_CONTEXT IrpContext
)
942 if (IrpContext
->DeviceObject
== VfatGlobalData
->DeviceObject
)
944 /* DeviceObject represents FileSystem instead of logical volume */
945 DPRINT ("FsdCreate called with file system\n");
946 IrpContext
->Irp
->IoStatus
.Information
= FILE_OPENED
;
947 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;
949 return STATUS_SUCCESS
;
952 if (!BooleanFlagOn(IrpContext
->Flags
, IRPCONTEXT_CANWAIT
))
954 return VfatMarkIrpContextForQueue(IrpContext
);
957 IrpContext
->Irp
->IoStatus
.Information
= 0;
958 ExAcquireResourceExclusiveLite(&IrpContext
->DeviceExt
->DirResource
, TRUE
);
959 Status
= VfatCreateFile(IrpContext
->DeviceObject
, IrpContext
->Irp
);
960 ExReleaseResourceLite(&IrpContext
->DeviceExt
->DirResource
);
962 if (NT_SUCCESS(Status
))
963 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;