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
;
397 PVFATCCB pCcb
= NULL
;
399 BOOLEAN PagingFileCreate
;
401 BOOLEAN OpenTargetDir
;
402 BOOLEAN TrailingBackslash
;
403 UNICODE_STRING FileNameU
;
404 UNICODE_STRING PathNameU
;
407 /* Unpack the various parameters. */
408 Stack
= IoGetCurrentIrpStackLocation(Irp
);
409 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
410 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
411 PagingFileCreate
= BooleanFlagOn(Stack
->Flags
, SL_OPEN_PAGING_FILE
);
412 OpenTargetDir
= BooleanFlagOn(Stack
->Flags
, SL_OPEN_TARGET_DIRECTORY
);
414 FileObject
= Stack
->FileObject
;
415 DeviceExt
= DeviceObject
->DeviceExtension
;
417 if (BooleanFlagOn(Stack
->Parameters
.Create
.Options
, FILE_OPEN_BY_FILE_ID
))
419 return STATUS_NOT_IMPLEMENTED
;
422 /* Check their validity. */
423 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
424 RequestedDisposition
== FILE_SUPERSEDE
)
426 return STATUS_INVALID_PARAMETER
;
429 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
430 BooleanFlagOn(RequestedOptions
, FILE_NON_DIRECTORY_FILE
))
432 return STATUS_INVALID_PARAMETER
;
435 /* Deny create if the volume is locked */
436 if (BooleanFlagOn(DeviceExt
->Flags
, VCB_VOLUME_LOCKED
))
438 return STATUS_ACCESS_DENIED
;
441 /* This a open operation for the volume itself */
442 if (FileObject
->FileName
.Length
== 0 &&
443 (FileObject
->RelatedFileObject
== NULL
||
444 FileObject
->RelatedFileObject
->FsContext2
!= NULL
||
445 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
))
447 DPRINT("Volume opening\n");
449 if (RequestedDisposition
!= FILE_OPEN
&&
450 RequestedDisposition
!= FILE_OPEN_IF
)
452 return STATUS_ACCESS_DENIED
;
455 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
456 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
== NULL
|| FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
))
458 return STATUS_NOT_A_DIRECTORY
;
463 return STATUS_INVALID_PARAMETER
;
466 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
468 return STATUS_CANNOT_DELETE
;
471 pFcb
= DeviceExt
->VolumeFcb
;
473 if (pFcb
->OpenHandleCount
== 0)
475 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
476 Stack
->Parameters
.Create
.ShareAccess
,
478 &pFcb
->FCBShareAccess
);
482 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
483 Stack
->Parameters
.Create
.ShareAccess
,
485 &pFcb
->FCBShareAccess
,
487 if (!NT_SUCCESS(Status
))
493 vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
494 DeviceExt
->OpenHandleCount
++;
495 pFcb
->OpenHandleCount
++;
497 Irp
->IoStatus
.Information
= FILE_OPENED
;
498 return STATUS_SUCCESS
;
501 if (FileObject
->RelatedFileObject
!= NULL
&&
502 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
)
504 ASSERT(FileObject
->FileName
.Length
!= 0);
505 return STATUS_OBJECT_PATH_NOT_FOUND
;
508 /* Check for illegal characters and illegal dot sequences in the file name */
509 PathNameU
= FileObject
->FileName
;
510 c
= PathNameU
.Buffer
+ PathNameU
.Length
/ sizeof(WCHAR
);
514 while (c
-- > PathNameU
.Buffer
)
516 if (*c
== L
'\\' || c
== PathNameU
.Buffer
)
518 if (Dots
&& last
> c
)
520 return STATUS_OBJECT_NAME_INVALID
;
522 if (*c
== L
'\\' && (c
- 1) > PathNameU
.Buffer
&&
525 return STATUS_OBJECT_NAME_INVALID
;
536 if (*c
!= '\\' && vfatIsLongIllegal(*c
))
538 return STATUS_OBJECT_NAME_INVALID
;
542 /* Check if we try to open target directory of root dir */
543 if (OpenTargetDir
&& FileObject
->RelatedFileObject
== NULL
&& PathNameU
.Length
== sizeof(WCHAR
) &&
544 PathNameU
.Buffer
[0] == L
'\\')
546 return STATUS_INVALID_PARAMETER
;
549 if (FileObject
->RelatedFileObject
&& PathNameU
.Length
>= sizeof(WCHAR
) && PathNameU
.Buffer
[0] == L
'\\')
551 return STATUS_OBJECT_NAME_INVALID
;
554 TrailingBackslash
= FALSE
;
555 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
557 PathNameU
.Length
-= sizeof(WCHAR
);
558 TrailingBackslash
= TRUE
;
561 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
563 return STATUS_OBJECT_NAME_INVALID
;
566 /* Try opening the file. */
569 Status
= VfatOpenFile(DeviceExt
, &PathNameU
, FileObject
, RequestedDisposition
, RequestedOptions
, &ParentFcb
);
574 LONG idx
, FileNameLen
;
576 ParentFcb
= (FileObject
->RelatedFileObject
!= NULL
) ? FileObject
->RelatedFileObject
->FsContext
: NULL
;
579 vfatGrabFCB(DeviceExt
, ParentFcb
);
581 Status
= vfatGetFCBForFile(DeviceExt
, &ParentFcb
, &TargetFcb
, &PathNameU
);
583 if (NT_SUCCESS(Status
))
585 vfatReleaseFCB(DeviceExt
, TargetFcb
);
586 Irp
->IoStatus
.Information
= FILE_EXISTS
;
590 Irp
->IoStatus
.Information
= FILE_DOES_NOT_EXIST
;
593 idx
= FileObject
->FileName
.Length
/ sizeof(WCHAR
) - 1;
595 /* Skip trailing \ - if any */
596 if (PathNameU
.Buffer
[idx
] == L
'\\')
599 PathNameU
.Length
-= sizeof(WCHAR
);
603 while (idx
>= 0 && PathNameU
.Buffer
[idx
] != L
'\\')
608 if (idx
> 0 || PathNameU
.Buffer
[0] == L
'\\')
610 /* We don't want to include / in the name */
611 FileNameLen
= PathNameU
.Length
- ((idx
+ 1) * sizeof(WCHAR
));
613 /* Update FO just to keep file name */
614 /* Skip first slash */
616 FileObject
->FileName
.Length
= FileNameLen
;
617 RtlMoveMemory(&PathNameU
.Buffer
[0], &PathNameU
.Buffer
[idx
], FileObject
->FileName
.Length
);
621 /* This is a relative open and we have only the filename, so open the parent directory
622 * It is in RelatedFileObject
624 ASSERT(FileObject
->RelatedFileObject
!= NULL
);
626 /* No need to modify the FO, it already has the name */
629 /* We're done with opening! */
630 if (ParentFcb
!= NULL
)
632 Status
= vfatAttachFCBToFileObject(DeviceExt
, ParentFcb
, FileObject
);
635 if (NT_SUCCESS(Status
))
637 pFcb
= FileObject
->FsContext
;
638 ASSERT(pFcb
== ParentFcb
);
640 if (pFcb
->OpenHandleCount
== 0)
642 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
643 Stack
->Parameters
.Create
.ShareAccess
,
645 &pFcb
->FCBShareAccess
);
649 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
650 Stack
->Parameters
.Create
.ShareAccess
,
652 &pFcb
->FCBShareAccess
,
654 if (!NT_SUCCESS(Status
))
656 VfatCloseFile(DeviceExt
, FileObject
);
661 pCcb
= FileObject
->FsContext2
;
662 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
664 pCcb
->Flags
|= CCB_DELETE_ON_CLOSE
;
667 pFcb
->OpenHandleCount
++;
668 DeviceExt
->OpenHandleCount
++;
670 else if (ParentFcb
!= NULL
)
672 vfatReleaseFCB(DeviceExt
, ParentFcb
);
679 * If the directory containing the file to open doesn't exist then
682 if (Status
== STATUS_OBJECT_PATH_NOT_FOUND
||
683 Status
== STATUS_INVALID_PARAMETER
||
684 Status
== STATUS_DELETE_PENDING
||
685 Status
== STATUS_ACCESS_DENIED
||
686 Status
== STATUS_OBJECT_NAME_COLLISION
)
690 vfatReleaseFCB(DeviceExt
, ParentFcb
);
695 if (!NT_SUCCESS(Status
) && ParentFcb
== NULL
)
697 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU
, Status
);
701 /* If the file open failed then create the required file */
702 if (!NT_SUCCESS (Status
))
704 if (RequestedDisposition
== FILE_CREATE
||
705 RequestedDisposition
== FILE_OPEN_IF
||
706 RequestedDisposition
== FILE_OVERWRITE_IF
||
707 RequestedDisposition
== FILE_SUPERSEDE
)
709 Attributes
= Stack
->Parameters
.Create
.FileAttributes
& ~FILE_ATTRIBUTE_NORMAL
;
710 if (!BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
))
712 if (TrailingBackslash
)
714 vfatReleaseFCB(DeviceExt
, ParentFcb
);
715 return STATUS_OBJECT_NAME_INVALID
;
717 Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
719 vfatSplitPathName(&PathNameU
, NULL
, &FileNameU
);
720 Status
= VfatAddEntry(DeviceExt
, &FileNameU
, &pFcb
, ParentFcb
, RequestedOptions
,
721 (UCHAR
)FlagOn(Attributes
, FILE_ATTRIBUTE_VALID_FLAGS
), NULL
);
722 vfatReleaseFCB(DeviceExt
, ParentFcb
);
723 if (NT_SUCCESS(Status
))
725 Status
= vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
726 if (!NT_SUCCESS(Status
))
728 vfatReleaseFCB(DeviceExt
, pFcb
);
732 Irp
->IoStatus
.Information
= FILE_CREATED
;
733 VfatSetAllocationSizeInformation(FileObject
,
736 &Irp
->Overlay
.AllocationSize
);
737 VfatSetExtendedAttributes(FileObject
,
738 Irp
->AssociatedIrp
.SystemBuffer
,
739 Stack
->Parameters
.Create
.EaLength
);
741 if (PagingFileCreate
)
743 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
753 vfatReleaseFCB(DeviceExt
, ParentFcb
);
761 vfatReleaseFCB(DeviceExt
, ParentFcb
);
764 pFcb
= FileObject
->FsContext
;
766 /* Otherwise fail if the caller wanted to create a new file */
767 if (RequestedDisposition
== FILE_CREATE
)
769 VfatCloseFile(DeviceExt
, FileObject
);
770 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
772 return STATUS_OBJECT_NAME_INVALID
;
774 Irp
->IoStatus
.Information
= FILE_EXISTS
;
775 return STATUS_OBJECT_NAME_COLLISION
;
778 if (pFcb
->OpenHandleCount
!= 0)
780 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
781 Stack
->Parameters
.Create
.ShareAccess
,
783 &pFcb
->FCBShareAccess
,
785 if (!NT_SUCCESS(Status
))
787 VfatCloseFile(DeviceExt
, FileObject
);
793 * Check the file has the requested attributes
795 if (BooleanFlagOn(RequestedOptions
, FILE_NON_DIRECTORY_FILE
) &&
796 vfatFCBIsDirectory(pFcb
))
798 VfatCloseFile (DeviceExt
, FileObject
);
799 return STATUS_FILE_IS_A_DIRECTORY
;
801 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
802 !vfatFCBIsDirectory(pFcb
))
804 VfatCloseFile (DeviceExt
, FileObject
);
805 return STATUS_NOT_A_DIRECTORY
;
807 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
809 VfatCloseFile (DeviceExt
, FileObject
);
810 return STATUS_OBJECT_NAME_INVALID
;
812 #ifndef USE_ROS_CC_AND_FS
813 if (!vfatFCBIsDirectory(pFcb
))
815 if (BooleanFlagOn(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
, FILE_WRITE_DATA
) ||
816 RequestedDisposition
== FILE_OVERWRITE
||
817 RequestedDisposition
== FILE_OVERWRITE_IF
||
818 (RequestedOptions
& FILE_DELETE_ON_CLOSE
))
820 if (!MmFlushImageSection(&pFcb
->SectionObjectPointers
, MmFlushForWrite
))
822 DPRINT1("%wZ\n", &pFcb
->PathNameU
);
823 DPRINT1("%d %d %d\n", Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
& FILE_WRITE_DATA
,
824 RequestedDisposition
== FILE_OVERWRITE
, RequestedDisposition
== FILE_OVERWRITE_IF
);
825 VfatCloseFile (DeviceExt
, FileObject
);
826 return (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
)) ? STATUS_CANNOT_DELETE
827 : STATUS_SHARING_VIOLATION
;
832 if (PagingFileCreate
)
835 * Do more checking for page files. It is possible,
836 * that the file was opened and closed previously
837 * as a normal cached file. In this case, the cache
838 * manager has referenced the fileobject and the fcb
839 * is held in memory. Try to remove the fileobject
840 * from cache manager and use the fcb.
842 if (pFcb
->RefCount
> 1)
844 if(!BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
846 VfatCloseFile(DeviceExt
, FileObject
);
847 return STATUS_INVALID_PARAMETER
;
852 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
857 if (BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
859 VfatCloseFile(DeviceExt
, FileObject
);
860 return STATUS_INVALID_PARAMETER
;
864 if (RequestedDisposition
== FILE_OVERWRITE
||
865 RequestedDisposition
== FILE_OVERWRITE_IF
||
866 RequestedDisposition
== FILE_SUPERSEDE
)
868 if (!vfatFCBIsDirectory(pFcb
))
870 *pFcb
->Attributes
= Stack
->Parameters
.Create
.FileAttributes
& ~FILE_ATTRIBUTE_NORMAL
;
871 *pFcb
->Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
872 VfatUpdateEntry(pFcb
, vfatVolumeIsFatX(DeviceExt
));
875 ExAcquireResourceExclusiveLite(&(pFcb
->MainResource
), TRUE
);
876 Status
= VfatSetAllocationSizeInformation(FileObject
,
879 &Irp
->Overlay
.AllocationSize
);
880 ExReleaseResourceLite(&(pFcb
->MainResource
));
881 if (!NT_SUCCESS (Status
))
883 VfatCloseFile(DeviceExt
, FileObject
);
888 if (RequestedDisposition
== FILE_SUPERSEDE
)
890 Irp
->IoStatus
.Information
= FILE_SUPERSEDED
;
892 else if (RequestedDisposition
== FILE_OVERWRITE
||
893 RequestedDisposition
== FILE_OVERWRITE_IF
)
895 Irp
->IoStatus
.Information
= FILE_OVERWRITTEN
;
899 Irp
->IoStatus
.Information
= FILE_OPENED
;
903 if (pFcb
->OpenHandleCount
== 0)
905 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
906 Stack
->Parameters
.Create
.ShareAccess
,
908 &pFcb
->FCBShareAccess
);
912 IoUpdateShareAccess(FileObject
,
913 &pFcb
->FCBShareAccess
);
916 pCcb
= FileObject
->FsContext2
;
917 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
919 pCcb
->Flags
|= CCB_DELETE_ON_CLOSE
;
922 if (Irp
->IoStatus
.Information
== FILE_CREATED
)
924 FsRtlNotifyFullReportChange(DeviceExt
->NotifySync
,
925 &(DeviceExt
->NotifyList
),
926 (PSTRING
)&pFcb
->PathNameU
,
927 pFcb
->PathNameU
.Length
- pFcb
->LongNameU
.Length
,
930 (vfatFCBIsDirectory(pFcb
) ?
931 FILE_NOTIFY_CHANGE_DIR_NAME
: FILE_NOTIFY_CHANGE_FILE_NAME
),
936 pFcb
->OpenHandleCount
++;
937 DeviceExt
->OpenHandleCount
++;
939 /* FIXME : test write access if requested */
945 * FUNCTION: Create or open a file
949 PVFAT_IRP_CONTEXT IrpContext
)
955 if (IrpContext
->DeviceObject
== VfatGlobalData
->DeviceObject
)
957 /* DeviceObject represents FileSystem instead of logical volume */
958 DPRINT ("FsdCreate called with file system\n");
959 IrpContext
->Irp
->IoStatus
.Information
= FILE_OPENED
;
960 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;
962 return STATUS_SUCCESS
;
965 if (!BooleanFlagOn(IrpContext
->Flags
, IRPCONTEXT_CANWAIT
))
967 return VfatMarkIrpContextForQueue(IrpContext
);
970 IrpContext
->Irp
->IoStatus
.Information
= 0;
971 ExAcquireResourceExclusiveLite(&IrpContext
->DeviceExt
->DirResource
, TRUE
);
972 Status
= VfatCreateFile(IrpContext
->DeviceObject
, IrpContext
->Irp
);
973 ExReleaseResourceLite(&IrpContext
->DeviceExt
->DirResource
);
975 if (NT_SUCCESS(Status
))
976 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;