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 vfatAddToStat(DeviceExt
, Fat
.CreateHits
, 1);
473 pFcb
= DeviceExt
->VolumeFcb
;
475 if (pFcb
->OpenHandleCount
== 0)
477 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
478 Stack
->Parameters
.Create
.ShareAccess
,
480 &pFcb
->FCBShareAccess
);
484 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
485 Stack
->Parameters
.Create
.ShareAccess
,
487 &pFcb
->FCBShareAccess
,
489 if (!NT_SUCCESS(Status
))
491 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
496 vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
497 DeviceExt
->OpenHandleCount
++;
498 pFcb
->OpenHandleCount
++;
499 vfatAddToStat(DeviceExt
, Fat
.SuccessfulCreates
, 1);
501 Irp
->IoStatus
.Information
= FILE_OPENED
;
502 return STATUS_SUCCESS
;
505 if (FileObject
->RelatedFileObject
!= NULL
&&
506 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
)
508 ASSERT(FileObject
->FileName
.Length
!= 0);
509 return STATUS_OBJECT_PATH_NOT_FOUND
;
512 /* Check for illegal characters and illegal dot sequences in the file name */
513 PathNameU
= FileObject
->FileName
;
514 c
= PathNameU
.Buffer
+ PathNameU
.Length
/ sizeof(WCHAR
);
518 while (c
-- > PathNameU
.Buffer
)
520 if (*c
== L
'\\' || c
== PathNameU
.Buffer
)
522 if (Dots
&& last
> c
)
524 return STATUS_OBJECT_NAME_INVALID
;
526 if (*c
== L
'\\' && (c
- 1) > PathNameU
.Buffer
&&
529 return STATUS_OBJECT_NAME_INVALID
;
540 if (*c
!= '\\' && vfatIsLongIllegal(*c
))
542 return STATUS_OBJECT_NAME_INVALID
;
546 /* Check if we try to open target directory of root dir */
547 if (OpenTargetDir
&& FileObject
->RelatedFileObject
== NULL
&& PathNameU
.Length
== sizeof(WCHAR
) &&
548 PathNameU
.Buffer
[0] == L
'\\')
550 return STATUS_INVALID_PARAMETER
;
553 if (FileObject
->RelatedFileObject
&& PathNameU
.Length
>= sizeof(WCHAR
) && PathNameU
.Buffer
[0] == L
'\\')
555 return STATUS_OBJECT_NAME_INVALID
;
558 TrailingBackslash
= FALSE
;
559 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
561 PathNameU
.Length
-= sizeof(WCHAR
);
562 TrailingBackslash
= TRUE
;
565 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
567 return STATUS_OBJECT_NAME_INVALID
;
570 /* Try opening the file. */
573 vfatAddToStat(DeviceExt
, Fat
.CreateHits
, 1);
575 Status
= VfatOpenFile(DeviceExt
, &PathNameU
, FileObject
, RequestedDisposition
, RequestedOptions
, &ParentFcb
);
580 LONG idx
, FileNameLen
;
582 vfatAddToStat(DeviceExt
, Fat
.CreateHits
, 1);
584 ParentFcb
= (FileObject
->RelatedFileObject
!= NULL
) ? FileObject
->RelatedFileObject
->FsContext
: NULL
;
587 vfatGrabFCB(DeviceExt
, ParentFcb
);
590 Status
= vfatGetFCBForFile(DeviceExt
, &ParentFcb
, &TargetFcb
, &PathNameU
);
591 if (NT_SUCCESS(Status
))
593 vfatReleaseFCB(DeviceExt
, TargetFcb
);
594 Irp
->IoStatus
.Information
= FILE_EXISTS
;
598 Irp
->IoStatus
.Information
= FILE_DOES_NOT_EXIST
;
601 idx
= FileObject
->FileName
.Length
/ sizeof(WCHAR
) - 1;
603 /* Skip trailing \ - if any */
604 if (PathNameU
.Buffer
[idx
] == L
'\\')
607 PathNameU
.Length
-= sizeof(WCHAR
);
611 while (idx
>= 0 && PathNameU
.Buffer
[idx
] != L
'\\')
616 if (idx
> 0 || PathNameU
.Buffer
[0] == L
'\\')
618 /* We don't want to include / in the name */
619 FileNameLen
= PathNameU
.Length
- ((idx
+ 1) * sizeof(WCHAR
));
621 /* Update FO just to keep file name */
622 /* Skip first slash */
624 FileObject
->FileName
.Length
= FileNameLen
;
625 RtlMoveMemory(&PathNameU
.Buffer
[0], &PathNameU
.Buffer
[idx
], FileObject
->FileName
.Length
);
627 /* Terminate the string at the last backslash */
628 PathNameU
.Buffer
[idx
+ 1] = UNICODE_NULL
;
629 PathNameU
.Length
= (idx
+ 1) * sizeof(WCHAR
);
630 PathNameU
.MaximumLength
= PathNameU
.Length
+ sizeof(WCHAR
);
632 /* Update the file object as well */
633 FileObject
->FileName
.Length
= PathNameU
.Length
;
634 FileObject
->FileName
.MaximumLength
= PathNameU
.MaximumLength
;
639 /* This is a relative open and we have only the filename, so open the parent directory
640 * It is in RelatedFileObject
642 ASSERT(FileObject
->RelatedFileObject
!= NULL
);
644 /* No need to modify the FO, it already has the name */
647 /* We're done with opening! */
648 if (ParentFcb
!= NULL
)
650 Status
= vfatAttachFCBToFileObject(DeviceExt
, ParentFcb
, FileObject
);
653 if (NT_SUCCESS(Status
))
655 pFcb
= FileObject
->FsContext
;
656 ASSERT(pFcb
== ParentFcb
);
658 if (pFcb
->OpenHandleCount
== 0)
660 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
661 Stack
->Parameters
.Create
.ShareAccess
,
663 &pFcb
->FCBShareAccess
);
667 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
668 Stack
->Parameters
.Create
.ShareAccess
,
670 &pFcb
->FCBShareAccess
,
672 if (!NT_SUCCESS(Status
))
674 VfatCloseFile(DeviceExt
, FileObject
);
675 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
680 pCcb
= FileObject
->FsContext2
;
681 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
683 pCcb
->Flags
|= CCB_DELETE_ON_CLOSE
;
686 pFcb
->OpenHandleCount
++;
687 DeviceExt
->OpenHandleCount
++;
689 else if (ParentFcb
!= NULL
)
691 vfatReleaseFCB(DeviceExt
, ParentFcb
);
694 if (NT_SUCCESS(Status
))
696 vfatAddToStat(DeviceExt
, Fat
.SuccessfulCreates
, 1);
700 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
707 * If the directory containing the file to open doesn't exist then
710 if (Status
== STATUS_OBJECT_PATH_NOT_FOUND
||
711 Status
== STATUS_INVALID_PARAMETER
||
712 Status
== STATUS_DELETE_PENDING
||
713 Status
== STATUS_ACCESS_DENIED
||
714 Status
== STATUS_OBJECT_NAME_COLLISION
)
718 vfatReleaseFCB(DeviceExt
, ParentFcb
);
720 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
724 if (!NT_SUCCESS(Status
) && ParentFcb
== NULL
)
726 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU
, Status
);
727 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
731 /* If the file open failed then create the required file */
732 if (!NT_SUCCESS (Status
))
734 if (RequestedDisposition
== FILE_CREATE
||
735 RequestedDisposition
== FILE_OPEN_IF
||
736 RequestedDisposition
== FILE_OVERWRITE_IF
||
737 RequestedDisposition
== FILE_SUPERSEDE
)
739 Attributes
= Stack
->Parameters
.Create
.FileAttributes
& ~FILE_ATTRIBUTE_NORMAL
;
740 if (!BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
))
742 if (TrailingBackslash
)
744 vfatReleaseFCB(DeviceExt
, ParentFcb
);
745 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
746 return STATUS_OBJECT_NAME_INVALID
;
748 Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
750 vfatSplitPathName(&PathNameU
, NULL
, &FileNameU
);
751 Status
= VfatAddEntry(DeviceExt
, &FileNameU
, &pFcb
, ParentFcb
, RequestedOptions
,
752 (UCHAR
)FlagOn(Attributes
, FILE_ATTRIBUTE_VALID_FLAGS
), NULL
);
753 vfatReleaseFCB(DeviceExt
, ParentFcb
);
754 if (NT_SUCCESS(Status
))
756 Status
= vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
757 if (!NT_SUCCESS(Status
))
759 vfatReleaseFCB(DeviceExt
, pFcb
);
760 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
764 Irp
->IoStatus
.Information
= FILE_CREATED
;
765 VfatSetAllocationSizeInformation(FileObject
,
768 &Irp
->Overlay
.AllocationSize
);
769 VfatSetExtendedAttributes(FileObject
,
770 Irp
->AssociatedIrp
.SystemBuffer
,
771 Stack
->Parameters
.Create
.EaLength
);
773 if (PagingFileCreate
)
775 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
780 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
786 vfatReleaseFCB(DeviceExt
, ParentFcb
);
787 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
795 vfatReleaseFCB(DeviceExt
, ParentFcb
);
798 pFcb
= FileObject
->FsContext
;
800 /* Otherwise fail if the caller wanted to create a new file */
801 if (RequestedDisposition
== FILE_CREATE
)
803 VfatCloseFile(DeviceExt
, FileObject
);
804 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
806 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
807 return STATUS_OBJECT_NAME_INVALID
;
809 Irp
->IoStatus
.Information
= FILE_EXISTS
;
810 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
811 return STATUS_OBJECT_NAME_COLLISION
;
814 if (pFcb
->OpenHandleCount
!= 0)
816 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
817 Stack
->Parameters
.Create
.ShareAccess
,
819 &pFcb
->FCBShareAccess
,
821 if (!NT_SUCCESS(Status
))
823 VfatCloseFile(DeviceExt
, FileObject
);
824 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
830 * Check the file has the requested attributes
832 if (BooleanFlagOn(RequestedOptions
, FILE_NON_DIRECTORY_FILE
) &&
833 vfatFCBIsDirectory(pFcb
))
835 VfatCloseFile (DeviceExt
, FileObject
);
836 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
837 return STATUS_FILE_IS_A_DIRECTORY
;
839 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
840 !vfatFCBIsDirectory(pFcb
))
842 VfatCloseFile (DeviceExt
, FileObject
);
843 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
844 return STATUS_NOT_A_DIRECTORY
;
846 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
848 VfatCloseFile (DeviceExt
, FileObject
);
849 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
850 return STATUS_OBJECT_NAME_INVALID
;
852 #ifndef USE_ROS_CC_AND_FS
853 if (!vfatFCBIsDirectory(pFcb
))
855 if (BooleanFlagOn(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
, FILE_WRITE_DATA
) ||
856 RequestedDisposition
== FILE_OVERWRITE
||
857 RequestedDisposition
== FILE_OVERWRITE_IF
||
858 (RequestedOptions
& FILE_DELETE_ON_CLOSE
))
860 if (!MmFlushImageSection(&pFcb
->SectionObjectPointers
, MmFlushForWrite
))
862 DPRINT1("%wZ\n", &pFcb
->PathNameU
);
863 DPRINT1("%d %d %d\n", Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
& FILE_WRITE_DATA
,
864 RequestedDisposition
== FILE_OVERWRITE
, RequestedDisposition
== FILE_OVERWRITE_IF
);
865 VfatCloseFile (DeviceExt
, FileObject
);
866 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
867 return (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
)) ? STATUS_CANNOT_DELETE
868 : STATUS_SHARING_VIOLATION
;
873 if (PagingFileCreate
)
876 * Do more checking for page files. It is possible,
877 * that the file was opened and closed previously
878 * as a normal cached file. In this case, the cache
879 * manager has referenced the fileobject and the fcb
880 * is held in memory. Try to remove the fileobject
881 * from cache manager and use the fcb.
883 if (pFcb
->RefCount
> 1)
885 if(!BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
887 VfatCloseFile(DeviceExt
, FileObject
);
888 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
889 return STATUS_INVALID_PARAMETER
;
894 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
899 if (BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
901 VfatCloseFile(DeviceExt
, FileObject
);
902 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
903 return STATUS_INVALID_PARAMETER
;
907 if (RequestedDisposition
== FILE_OVERWRITE
||
908 RequestedDisposition
== FILE_OVERWRITE_IF
||
909 RequestedDisposition
== FILE_SUPERSEDE
)
911 if (!vfatFCBIsDirectory(pFcb
))
913 *pFcb
->Attributes
= Stack
->Parameters
.Create
.FileAttributes
& ~FILE_ATTRIBUTE_NORMAL
;
914 *pFcb
->Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
915 VfatUpdateEntry(pFcb
, vfatVolumeIsFatX(DeviceExt
));
918 ExAcquireResourceExclusiveLite(&(pFcb
->MainResource
), TRUE
);
919 Status
= VfatSetAllocationSizeInformation(FileObject
,
922 &Irp
->Overlay
.AllocationSize
);
923 ExReleaseResourceLite(&(pFcb
->MainResource
));
924 if (!NT_SUCCESS (Status
))
926 VfatCloseFile(DeviceExt
, FileObject
);
927 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
932 if (RequestedDisposition
== FILE_SUPERSEDE
)
934 Irp
->IoStatus
.Information
= FILE_SUPERSEDED
;
936 else if (RequestedDisposition
== FILE_OVERWRITE
||
937 RequestedDisposition
== FILE_OVERWRITE_IF
)
939 Irp
->IoStatus
.Information
= FILE_OVERWRITTEN
;
943 Irp
->IoStatus
.Information
= FILE_OPENED
;
947 if (pFcb
->OpenHandleCount
== 0)
949 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
950 Stack
->Parameters
.Create
.ShareAccess
,
952 &pFcb
->FCBShareAccess
);
956 IoUpdateShareAccess(FileObject
,
957 &pFcb
->FCBShareAccess
);
960 pCcb
= FileObject
->FsContext2
;
961 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
963 pCcb
->Flags
|= CCB_DELETE_ON_CLOSE
;
966 if (Irp
->IoStatus
.Information
== FILE_CREATED
)
968 FsRtlNotifyFullReportChange(DeviceExt
->NotifySync
,
969 &(DeviceExt
->NotifyList
),
970 (PSTRING
)&pFcb
->PathNameU
,
971 pFcb
->PathNameU
.Length
- pFcb
->LongNameU
.Length
,
974 (vfatFCBIsDirectory(pFcb
) ?
975 FILE_NOTIFY_CHANGE_DIR_NAME
: FILE_NOTIFY_CHANGE_FILE_NAME
),
980 pFcb
->OpenHandleCount
++;
981 DeviceExt
->OpenHandleCount
++;
983 /* FIXME : test write access if requested */
985 /* FIXME: That is broken, we cannot reach this code path with failure */
986 ASSERT(NT_SUCCESS(Status
));
987 if (NT_SUCCESS(Status
))
989 vfatAddToStat(DeviceExt
, Fat
.SuccessfulCreates
, 1);
993 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
1000 * FUNCTION: Create or open a file
1004 PVFAT_IRP_CONTEXT IrpContext
)
1010 if (IrpContext
->DeviceObject
== VfatGlobalData
->DeviceObject
)
1012 /* DeviceObject represents FileSystem instead of logical volume */
1013 DPRINT ("FsdCreate called with file system\n");
1014 IrpContext
->Irp
->IoStatus
.Information
= FILE_OPENED
;
1015 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;
1017 return STATUS_SUCCESS
;
1020 if (!BooleanFlagOn(IrpContext
->Flags
, IRPCONTEXT_CANWAIT
))
1022 return VfatMarkIrpContextForQueue(IrpContext
);
1025 IrpContext
->Irp
->IoStatus
.Information
= 0;
1026 ExAcquireResourceExclusiveLite(&IrpContext
->DeviceExt
->DirResource
, TRUE
);
1027 Status
= VfatCreateFile(IrpContext
->DeviceObject
, IrpContext
->Irp
);
1028 ExReleaseResourceLite(&IrpContext
->DeviceExt
->DirResource
);
1030 if (NT_SUCCESS(Status
))
1031 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;