2 * FILE: drivers/filesystems/fastfat/fcb.c
3 * PURPOSE: Routines to manipulate FCBs.
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS kernel
6 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
7 * Rex Jolliff (rex@lvcablemodem.com)
8 * Herve Poussineau (reactos@poussine.freesurf.fr)
9 * Pierre Schweitzer (pierre@reactos.org)
12 /* ------------------------------------------------------- INCLUDES */
20 #include <wctype.h> /* towlower prototype */
23 /* -------------------------------------------------------- DEFINES */
25 #define TAG_FCB 'BCFV'
27 /* -------------------------------------------------------- PUBLICS */
33 PUNICODE_STRING NameU
)
39 // LFN could start from "."
40 //ASSERT(NameU->Buffer[0] != L'.');
42 last
= NameU
->Buffer
+ NameU
->Length
/ sizeof(WCHAR
);
46 c
= towlower(*curr
++);
47 hash
= (hash
+ (c
<< 4) + (c
>> 4)) * 11;
54 PUNICODE_STRING PathNameU
,
55 PUNICODE_STRING DirNameU
,
56 PUNICODE_STRING FileNameU
)
60 pName
= PathNameU
->Buffer
+ PathNameU
->Length
/ sizeof(WCHAR
) - 1;
61 while (*pName
!= L
'\\' && pName
>= PathNameU
->Buffer
)
66 ASSERT(*pName
== L
'\\' || pName
< PathNameU
->Buffer
);
69 FileNameU
->Buffer
= pName
+ 1;
70 FileNameU
->Length
= FileNameU
->MaximumLength
= Length
* sizeof(WCHAR
);
74 DirNameU
->Buffer
= PathNameU
->Buffer
;
75 DirNameU
->Length
= (pName
+ 1 - PathNameU
->Buffer
) * sizeof(WCHAR
);
76 DirNameU
->MaximumLength
= DirNameU
->Length
;
84 PUNICODE_STRING NameU
)
86 USHORT PathNameBufferLength
;
89 PathNameBufferLength
= NameU
->Length
+ sizeof(WCHAR
);
91 PathNameBufferLength
= 0;
93 Fcb
->PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameBufferLength
, TAG_FCB
);
94 if (!Fcb
->PathNameBuffer
)
96 /* FIXME: what to do if no more memory? */
97 DPRINT1("Unable to initialize FCB for filename '%wZ'\n", NameU
);
98 KeBugCheckEx(FAT_FILE_SYSTEM
, (ULONG_PTR
)Fcb
, (ULONG_PTR
)NameU
, 0, 0);
101 Fcb
->PathNameU
.Length
= 0;
102 Fcb
->PathNameU
.Buffer
= Fcb
->PathNameBuffer
;
103 Fcb
->PathNameU
.MaximumLength
= PathNameBufferLength
;
104 Fcb
->ShortNameU
.Length
= 0;
105 Fcb
->ShortNameU
.Buffer
= Fcb
->ShortNameBuffer
;
106 Fcb
->ShortNameU
.MaximumLength
= sizeof(Fcb
->ShortNameBuffer
);
107 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
108 if (NameU
&& NameU
->Length
)
110 RtlCopyUnicodeString(&Fcb
->PathNameU
, NameU
);
111 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
115 Fcb
->DirNameU
.Buffer
= Fcb
->LongNameU
.Buffer
= NULL
;
116 Fcb
->DirNameU
.MaximumLength
= Fcb
->DirNameU
.Length
= 0;
117 Fcb
->LongNameU
.MaximumLength
= Fcb
->LongNameU
.Length
= 0;
119 RtlZeroMemory(&Fcb
->FCBShareAccess
, sizeof(SHARE_ACCESS
));
120 Fcb
->OpenHandleCount
= 0;
125 PDEVICE_EXTENSION pVCB
,
126 PUNICODE_STRING pFileNameU
)
130 DPRINT("'%wZ'\n", pFileNameU
);
132 rcFCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->FcbLookasideList
);
137 RtlZeroMemory(rcFCB
, sizeof(VFATFCB
));
138 vfatInitFcb(rcFCB
, pFileNameU
);
139 if (vfatVolumeIsFatX(pVCB
))
140 rcFCB
->Attributes
= &rcFCB
->entry
.FatX
.Attrib
;
142 rcFCB
->Attributes
= &rcFCB
->entry
.Fat
.Attrib
;
143 rcFCB
->Hash
.Hash
= vfatNameHash(0, &rcFCB
->PathNameU
);
144 rcFCB
->Hash
.self
= rcFCB
;
145 rcFCB
->ShortHash
.self
= rcFCB
;
146 ExInitializeResourceLite(&rcFCB
->PagingIoResource
);
147 ExInitializeResourceLite(&rcFCB
->MainResource
);
148 FsRtlInitializeFileLock(&rcFCB
->FileLock
, NULL
, NULL
);
149 ExInitializeFastMutex(&rcFCB
->LastMutex
);
150 rcFCB
->RFCB
.PagingIoResource
= &rcFCB
->PagingIoResource
;
151 rcFCB
->RFCB
.Resource
= &rcFCB
->MainResource
;
152 rcFCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
153 InitializeListHead(&rcFCB
->ParentListHead
);
161 PDEVICE_EXTENSION pVCB
,
168 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
169 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
171 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
173 entry
= pVCB
->FcbHashTable
[ShortIndex
];
174 if (entry
->self
== pFCB
)
176 pVCB
->FcbHashTable
[ShortIndex
] = entry
->next
;
180 while (entry
->next
->self
!= pFCB
)
184 entry
->next
= pFCB
->ShortHash
.next
;
187 entry
= pVCB
->FcbHashTable
[Index
];
188 if (entry
->self
== pFCB
)
190 pVCB
->FcbHashTable
[Index
] = entry
->next
;
194 while (entry
->next
->self
!= pFCB
)
198 entry
->next
= pFCB
->Hash
.next
;
201 RemoveEntryList(&pFCB
->FcbListEntry
);
207 PVFATFCB directoryFCB
,
208 PUNICODE_STRING LongNameU
,
209 PUNICODE_STRING ShortNameU
,
210 PUNICODE_STRING NameU
)
212 PWCHAR PathNameBuffer
;
213 USHORT PathNameLength
;
215 PathNameLength
= directoryFCB
->PathNameU
.Length
+ max(LongNameU
->Length
, ShortNameU
->Length
);
216 if (!vfatFCBIsRoot(directoryFCB
))
218 PathNameLength
+= sizeof(WCHAR
);
221 if (PathNameLength
> LONGNAME_MAX_LENGTH
* sizeof(WCHAR
))
223 return STATUS_OBJECT_NAME_INVALID
;
225 PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameLength
+ sizeof(WCHAR
), TAG_FCB
);
228 return STATUS_INSUFFICIENT_RESOURCES
;
230 NameU
->Buffer
= PathNameBuffer
;
232 NameU
->MaximumLength
= PathNameLength
;
234 RtlCopyUnicodeString(NameU
, &directoryFCB
->PathNameU
);
235 if (!vfatFCBIsRoot(directoryFCB
))
237 RtlAppendUnicodeToString(NameU
, L
"\\");
239 if (LongNameU
->Length
> 0)
241 RtlAppendUnicodeStringToString(NameU
, LongNameU
);
245 RtlAppendUnicodeStringToString(NameU
, ShortNameU
);
247 NameU
->Buffer
[NameU
->Length
/ sizeof(WCHAR
)] = 0;
249 return STATUS_SUCCESS
;
256 if (pCcb
->SearchPattern
.Buffer
)
258 ExFreePoolWithTag(pCcb
->SearchPattern
.Buffer
, TAG_VFAT
);
260 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, pCcb
);
267 FsRtlUninitializeFileLock(&pFCB
->FileLock
);
268 if (!vfatFCBIsRoot(pFCB
) &&
269 !BooleanFlagOn(pFCB
->Flags
, FCB_IS_FAT
) && !BooleanFlagOn(pFCB
->Flags
, FCB_IS_VOLUME
))
271 RemoveEntryList(&pFCB
->ParentListEntry
);
273 ExFreePool(pFCB
->PathNameBuffer
);
274 ExDeleteResourceLite(&pFCB
->PagingIoResource
);
275 ExDeleteResourceLite(&pFCB
->MainResource
);
276 ExFreeToNPagedLookasideList(&VfatGlobalData
->FcbLookasideList
, pFCB
);
277 ASSERT(IsListEmpty(&pFCB
->ParentListHead
));
284 return FCB
->PathNameU
.Length
== sizeof(WCHAR
) && FCB
->PathNameU
.Buffer
[0] == L
'\\' ? TRUE
: FALSE
;
289 PDEVICE_EXTENSION pVCB
,
292 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
294 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
295 ASSERT(pFCB
->RefCount
> 0);
301 PDEVICE_EXTENSION pVCB
,
306 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
307 pFCB
, &pFCB
->PathNameU
, pFCB
->RefCount
);
309 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
313 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
314 ASSERT(pFCB
->RefCount
> 0);
316 if (pFCB
->RefCount
== 0)
318 ASSERT(pFCB
->OpenHandleCount
== 0);
319 tmpFcb
= pFCB
->parentFcb
;
320 vfatDelFCBFromTable(pVCB
, pFCB
);
321 vfatDestroyFCB(pFCB
);
334 PDEVICE_EXTENSION pVCB
,
340 ASSERT(pFCB
->Hash
.Hash
== vfatNameHash(0, &pFCB
->PathNameU
));
341 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
342 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
344 InsertTailList(&pVCB
->FcbListHead
, &pFCB
->FcbListEntry
);
346 pFCB
->Hash
.next
= pVCB
->FcbHashTable
[Index
];
347 pVCB
->FcbHashTable
[Index
] = &pFCB
->Hash
;
348 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
350 pFCB
->ShortHash
.next
= pVCB
->FcbHashTable
[ShortIndex
];
351 pVCB
->FcbHashTable
[ShortIndex
] = &pFCB
->ShortHash
;
355 vfatGrabFCB(pVCB
, pFCB
->parentFcb
);
361 vfatInitFCBFromDirEntry(
362 PDEVICE_EXTENSION Vcb
,
364 PVFAT_DIRENTRY_CONTEXT DirContext
)
368 RtlCopyMemory(&Fcb
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
369 RtlCopyUnicodeString(&Fcb
->ShortNameU
, &DirContext
->ShortNameU
);
370 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
371 if (vfatVolumeIsFatX(Vcb
))
373 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
377 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
378 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
381 if (vfatFCBIsDirectory(Fcb
))
383 ULONG FirstCluster
, CurrentCluster
;
384 NTSTATUS Status
= STATUS_SUCCESS
;
386 FirstCluster
= vfatDirEntryGetFirstCluster(Vcb
, &Fcb
->entry
);
387 if (FirstCluster
== 1)
389 Size
= Vcb
->FatInfo
.rootDirectorySectors
* Vcb
->FatInfo
.BytesPerSector
;
391 else if (FirstCluster
!= 0)
393 CurrentCluster
= FirstCluster
;
394 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
396 Size
+= Vcb
->FatInfo
.BytesPerCluster
;
397 Status
= NextCluster(Vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
401 else if (vfatVolumeIsFatX(Vcb
))
403 Size
= Fcb
->entry
.FatX
.FileSize
;
407 Size
= Fcb
->entry
.Fat
.FileSize
;
409 Fcb
->dirIndex
= DirContext
->DirIndex
;
410 Fcb
->startIndex
= DirContext
->StartIndex
;
411 if (vfatVolumeIsFatX(Vcb
) && !vfatFCBIsRoot(Fcb
))
413 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
414 Fcb
->dirIndex
= DirContext
->DirIndex
-2;
415 Fcb
->startIndex
= DirContext
->StartIndex
-2;
417 Fcb
->RFCB
.FileSize
.QuadPart
= Size
;
418 Fcb
->RFCB
.ValidDataLength
.QuadPart
= Size
;
419 Fcb
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP_64(Size
, Vcb
->FatInfo
.BytesPerCluster
);
423 vfatSetFCBNewDirName(
424 PDEVICE_EXTENSION pVCB
,
429 UNICODE_STRING NewNameU
;
431 /* Get full path name */
432 Status
= vfatMakeFullName(ParentFcb
, &Fcb
->LongNameU
, &Fcb
->ShortNameU
, &NewNameU
);
433 if (!NT_SUCCESS(Status
))
438 /* Delete old name */
439 if (Fcb
->PathNameBuffer
)
441 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
443 Fcb
->PathNameU
= NewNameU
;
445 /* Delete from table */
446 vfatDelFCBFromTable(pVCB
, Fcb
);
448 /* Split it properly */
449 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
450 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
451 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
452 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
453 if (vfatVolumeIsFatX(pVCB
))
455 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
459 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
460 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
463 vfatAddFCBToTable(pVCB
, Fcb
);
464 vfatReleaseFCB(pVCB
, ParentFcb
);
466 return STATUS_SUCCESS
;
471 PDEVICE_EXTENSION pVCB
,
473 PVFAT_DIRENTRY_CONTEXT DirContext
,
479 DPRINT("vfatUpdateFCB(%p, %p, %p, %p)\n", pVCB
, Fcb
, DirContext
, ParentFcb
);
481 /* Get full path name */
482 Status
= vfatMakeFullName(ParentFcb
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &Fcb
->PathNameU
);
483 if (!NT_SUCCESS(Status
))
488 /* Delete old name */
489 if (Fcb
->PathNameBuffer
)
491 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
494 /* Delete from table */
495 vfatDelFCBFromTable(pVCB
, Fcb
);
497 /* Split it properly */
498 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
499 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
500 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
502 /* Save old parent */
503 OldParent
= Fcb
->parentFcb
;
504 RemoveEntryList(&Fcb
->ParentListEntry
);
507 vfatInitFCBFromDirEntry(pVCB
, Fcb
, DirContext
);
509 if (vfatFCBIsDirectory(Fcb
))
511 CcFlushCache(&Fcb
->SectionObjectPointers
, NULL
, 0, NULL
);
513 Fcb
->parentFcb
= ParentFcb
;
514 InsertTailList(&ParentFcb
->ParentListHead
, &Fcb
->ParentListEntry
);
515 vfatAddFCBToTable(pVCB
, Fcb
);
517 /* If we moved across directories, dereference our old parent
518 * We also dereference in case we're just renaming since AddFCBToTable references it
520 vfatReleaseFCB(pVCB
, OldParent
);
522 return STATUS_SUCCESS
;
526 vfatGrabFCBFromTable(
527 PDEVICE_EXTENSION pVCB
,
528 PUNICODE_STRING PathNameU
)
532 UNICODE_STRING DirNameU
;
533 UNICODE_STRING FileNameU
;
534 PUNICODE_STRING FcbNameU
;
538 DPRINT("'%wZ'\n", PathNameU
);
540 ASSERT(PathNameU
->Length
>= sizeof(WCHAR
) && PathNameU
->Buffer
[0] == L
'\\');
541 Hash
= vfatNameHash(0, PathNameU
);
543 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
546 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
551 if (entry
->Hash
== Hash
)
554 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
555 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
557 if (rcFCB
->Hash
.Hash
== Hash
)
559 FcbNameU
= &rcFCB
->LongNameU
;
563 FcbNameU
= &rcFCB
->ShortNameU
;
565 /* compare the file name */
566 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
567 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
569 vfatGrabFCB(pVCB
, rcFCB
);
581 vfatFCBInitializeCacheFromVolume(
585 PFILE_OBJECT fileObject
;
589 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
591 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
594 ObDereferenceObject(fileObject
);
595 return STATUS_INSUFFICIENT_RESOURCES
;
597 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
599 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
600 fileObject
->FsContext
= fcb
;
601 fileObject
->FsContext2
= newCCB
;
602 fcb
->FileObject
= fileObject
;
606 CcInitializeCacheMap(fileObject
,
607 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
609 &VfatGlobalData
->CacheMgrCallbacks
,
612 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
614 status
= _SEH2_GetExceptionCode();
615 fcb
->FileObject
= NULL
;
616 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
617 ObDereferenceObject(fileObject
);
622 vfatGrabFCB(vcb
, fcb
);
623 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
624 return STATUS_SUCCESS
;
629 PDEVICE_EXTENSION pVCB
)
632 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
633 NTSTATUS Status
= STATUS_SUCCESS
;
634 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
636 FCB
= vfatNewFCB(pVCB
, &NameU
);
637 if (vfatVolumeIsFatX(pVCB
))
639 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
640 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
641 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
642 FCB
->entry
.FatX
.FirstCluster
= 1;
643 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
647 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
648 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
649 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
650 if (pVCB
->FatInfo
.FatType
== FAT32
)
652 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
653 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
654 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
656 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
658 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
659 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
664 FCB
->entry
.Fat
.FirstCluster
= 1;
665 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
668 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
671 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
672 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
673 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
674 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
676 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
677 vfatAddFCBToTable(pVCB
, FCB
);
684 PDEVICE_EXTENSION pVCB
)
687 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
689 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
692 FCB
= vfatMakeRootFCB(pVCB
);
699 vfatMakeFCBFromDirEntry(
701 PVFATFCB directoryFCB
,
702 PVFAT_DIRENTRY_CONTEXT DirContext
,
706 UNICODE_STRING NameU
;
709 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
710 if (!NT_SUCCESS(Status
))
715 rcFCB
= vfatNewFCB(vcb
, &NameU
);
716 vfatInitFCBFromDirEntry(vcb
, rcFCB
, DirContext
);
719 if (vfatFCBIsDirectory(rcFCB
))
721 Status
= vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
722 if (!NT_SUCCESS(Status
))
724 vfatReleaseFCB(vcb
, rcFCB
);
725 ExFreePoolWithTag(NameU
.Buffer
, TAG_FCB
);
729 rcFCB
->parentFcb
= directoryFCB
;
730 InsertTailList(&directoryFCB
->ParentListHead
, &rcFCB
->ParentListEntry
);
731 vfatAddFCBToTable(vcb
, rcFCB
);
734 ExFreePoolWithTag(NameU
.Buffer
, TAG_FCB
);
735 return STATUS_SUCCESS
;
739 vfatAttachFCBToFileObject(
740 PDEVICE_EXTENSION vcb
,
742 PFILE_OBJECT fileObject
)
746 UNREFERENCED_PARAMETER(vcb
);
748 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
751 return STATUS_INSUFFICIENT_RESOURCES
;
753 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
755 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
756 fileObject
->FsContext
= fcb
;
757 fileObject
->FsContext2
= newCCB
;
758 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
760 return STATUS_SUCCESS
;
765 PDEVICE_EXTENSION pDeviceExt
,
766 PVFATFCB pDirectoryFCB
,
767 PUNICODE_STRING FileToFindU
,
771 PVOID Context
= NULL
;
773 BOOLEAN First
= TRUE
;
774 VFAT_DIRENTRY_CONTEXT DirContext
;
775 /* This buffer must have a size of 260 characters, because
776 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
777 WCHAR LongNameBuffer
[260];
778 WCHAR ShortNameBuffer
[13];
779 BOOLEAN FoundLong
= FALSE
;
780 BOOLEAN FoundShort
= FALSE
;
783 ASSERT(pDirectoryFCB
);
786 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
787 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
788 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
790 DirContext
.DirIndex
= 0;
791 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
792 DirContext
.LongNameU
.Length
= 0;
793 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
794 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
795 DirContext
.ShortNameU
.Length
= 0;
796 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
800 status
= pDeviceExt
->GetNextDirEntry(&Context
,
806 if (status
== STATUS_NO_MORE_ENTRIES
)
808 return STATUS_OBJECT_NAME_NOT_FOUND
;
810 if (!NT_SUCCESS(status
))
815 DPRINT(" Index:%u longName:%wZ\n",
816 DirContext
.DirIndex
, &DirContext
.LongNameU
);
818 if (!ENTRY_VOLUME(pDeviceExt
, &DirContext
.DirEntry
))
820 if (DirContext
.LongNameU
.Length
== 0 ||
821 DirContext
.ShortNameU
.Length
== 0)
823 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
824 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
826 ASSERT(DirContext
.LongNameU
.Length
!= 0 &&
827 DirContext
.ShortNameU
.Length
!= 0);
829 DirContext
.DirIndex
++;
832 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
833 if (FoundLong
== FALSE
)
835 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
837 if (FoundLong
|| FoundShort
)
839 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
843 CcUnpinData(Context
);
847 DirContext
.DirIndex
++;
850 return STATUS_OBJECT_NAME_NOT_FOUND
;
855 PDEVICE_EXTENSION pVCB
,
856 PVFATFCB
*pParentFCB
,
858 PUNICODE_STRING pFileNameU
)
863 UNICODE_STRING NameU
;
864 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
865 UNICODE_STRING FileNameU
;
866 WCHAR NameBuffer
[260];
867 PWCHAR curr
, prev
, last
;
870 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
871 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
873 RtlInitEmptyUnicodeString(&FileNameU
, NameBuffer
, sizeof(NameBuffer
));
875 parentFCB
= *pParentFCB
;
877 if (parentFCB
== NULL
)
879 /* Passed-in name is the full name */
880 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
882 // Trivial case, open of the root directory on volume
883 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
885 DPRINT("returning root FCB\n");
887 FCB
= vfatOpenRootFCB(pVCB
);
891 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
894 /* Check for an existing FCB */
895 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
899 *pParentFCB
= FCB
->parentFcb
;
900 vfatGrabFCB(pVCB
, *pParentFCB
);
901 return STATUS_SUCCESS
;
904 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
905 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
910 if (curr
> FileNameU
.Buffer
)
912 NameU
.Buffer
= FileNameU
.Buffer
;
913 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
914 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
917 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
918 if (Length
!= FCB
->PathNameU
.Length
)
920 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
922 vfatReleaseFCB(pVCB
, FCB
);
923 return STATUS_OBJECT_NAME_INVALID
;
925 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
926 curr
, FileNameU
.Length
- Length
);
927 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
928 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
929 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
931 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
941 FCB
= vfatOpenRootFCB(pVCB
);
942 curr
= FileNameU
.Buffer
;
950 /* Make absolute path */
951 RtlCopyUnicodeString(&FileNameU
, &parentFCB
->PathNameU
);
952 curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
955 RtlAppendUnicodeToString(&FileNameU
, L
"\\");
958 ASSERT(*curr
== L
'\\');
959 RtlAppendUnicodeStringToString(&FileNameU
, pFileNameU
);
964 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
971 vfatReleaseFCB(pVCB
, parentFCB
);
974 // fail if element in FCB is not a directory
975 if (!vfatFCBIsDirectory(FCB
))
977 DPRINT ("Element in requested path is not a directory\n");
979 vfatReleaseFCB(pVCB
, FCB
);
984 return STATUS_OBJECT_PATH_NOT_FOUND
;
989 Length
= (curr
- prev
) * sizeof(WCHAR
);
990 if (Length
!= parentFCB
->LongNameU
.Length
)
992 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
994 vfatReleaseFCB(pVCB
, parentFCB
);
997 return STATUS_OBJECT_NAME_INVALID
;
999 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
1000 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
1001 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
1002 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
1003 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
1005 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
1009 while (*curr
!= L
'\\' && curr
<= last
)
1013 NameU
.Buffer
= FileNameU
.Buffer
;
1014 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
1015 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
1016 DPRINT("%wZ\n", &NameU
);
1017 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
1020 NameU
.Buffer
= prev
;
1021 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
1022 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
1023 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1028 *pParentFCB
= parentFCB
;
1029 return STATUS_OBJECT_NAME_NOT_FOUND
;
1033 vfatReleaseFCB(pVCB
, parentFCB
);
1035 return STATUS_OBJECT_PATH_NOT_FOUND
;
1038 else if (!NT_SUCCESS(status
))
1040 vfatReleaseFCB(pVCB
, parentFCB
);
1049 *pParentFCB
= parentFCB
;
1052 return STATUS_SUCCESS
;