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 (pVCB
->Flags
& VCB_IS_FATX
)
141 rcFCB
->Flags
|= FCB_IS_FATX_ENTRY
;
142 rcFCB
->Attributes
= &rcFCB
->entry
.FatX
.Attrib
;
145 rcFCB
->Attributes
= &rcFCB
->entry
.Fat
.Attrib
;
146 rcFCB
->Hash
.Hash
= vfatNameHash(0, &rcFCB
->PathNameU
);
147 rcFCB
->Hash
.self
= rcFCB
;
148 rcFCB
->ShortHash
.self
= rcFCB
;
149 ExInitializeResourceLite(&rcFCB
->PagingIoResource
);
150 ExInitializeResourceLite(&rcFCB
->MainResource
);
151 FsRtlInitializeFileLock(&rcFCB
->FileLock
, NULL
, NULL
);
152 ExInitializeFastMutex(&rcFCB
->LastMutex
);
153 rcFCB
->RFCB
.PagingIoResource
= &rcFCB
->PagingIoResource
;
154 rcFCB
->RFCB
.Resource
= &rcFCB
->MainResource
;
155 rcFCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
156 InitializeListHead(&rcFCB
->ParentListHead
);
164 PDEVICE_EXTENSION pVCB
,
171 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
172 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
174 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
176 entry
= pVCB
->FcbHashTable
[ShortIndex
];
177 if (entry
->self
== pFCB
)
179 pVCB
->FcbHashTable
[ShortIndex
] = entry
->next
;
183 while (entry
->next
->self
!= pFCB
)
187 entry
->next
= pFCB
->ShortHash
.next
;
190 entry
= pVCB
->FcbHashTable
[Index
];
191 if (entry
->self
== pFCB
)
193 pVCB
->FcbHashTable
[Index
] = entry
->next
;
197 while (entry
->next
->self
!= pFCB
)
201 entry
->next
= pFCB
->Hash
.next
;
204 RemoveEntryList(&pFCB
->FcbListEntry
);
210 PVFATFCB directoryFCB
,
211 PUNICODE_STRING LongNameU
,
212 PUNICODE_STRING ShortNameU
,
213 PUNICODE_STRING NameU
)
215 PWCHAR PathNameBuffer
;
216 USHORT PathNameLength
;
218 PathNameLength
= directoryFCB
->PathNameU
.Length
+ max(LongNameU
->Length
, ShortNameU
->Length
);
219 if (!vfatFCBIsRoot(directoryFCB
))
221 PathNameLength
+= sizeof(WCHAR
);
224 if (PathNameLength
> LONGNAME_MAX_LENGTH
* sizeof(WCHAR
))
226 return STATUS_OBJECT_NAME_INVALID
;
228 PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameLength
+ sizeof(WCHAR
), TAG_FCB
);
231 return STATUS_INSUFFICIENT_RESOURCES
;
233 NameU
->Buffer
= PathNameBuffer
;
235 NameU
->MaximumLength
= PathNameLength
;
237 RtlCopyUnicodeString(NameU
, &directoryFCB
->PathNameU
);
238 if (!vfatFCBIsRoot(directoryFCB
))
240 RtlAppendUnicodeToString(NameU
, L
"\\");
242 if (LongNameU
->Length
> 0)
244 RtlAppendUnicodeStringToString(NameU
, LongNameU
);
248 RtlAppendUnicodeStringToString(NameU
, ShortNameU
);
250 NameU
->Buffer
[NameU
->Length
/ sizeof(WCHAR
)] = 0;
252 return STATUS_SUCCESS
;
259 if (pCcb
->SearchPattern
.Buffer
)
261 ExFreePoolWithTag(pCcb
->SearchPattern
.Buffer
, TAG_VFAT
);
263 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, pCcb
);
270 FsRtlUninitializeFileLock(&pFCB
->FileLock
);
271 if (!vfatFCBIsRoot(pFCB
) &&
272 !BooleanFlagOn(pFCB
->Flags
, FCB_IS_FAT
) && !BooleanFlagOn(pFCB
->Flags
, FCB_IS_VOLUME
))
274 RemoveEntryList(&pFCB
->ParentListEntry
);
276 ExFreePool(pFCB
->PathNameBuffer
);
277 ExDeleteResourceLite(&pFCB
->PagingIoResource
);
278 ExDeleteResourceLite(&pFCB
->MainResource
);
279 ExFreeToNPagedLookasideList(&VfatGlobalData
->FcbLookasideList
, pFCB
);
280 ASSERT(IsListEmpty(&pFCB
->ParentListHead
));
287 return ((*FCB
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
294 return FCB
->PathNameU
.Length
== sizeof(WCHAR
) && FCB
->PathNameU
.Buffer
[0] == L
'\\' ? TRUE
: FALSE
;
299 PDEVICE_EXTENSION pVCB
,
302 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
304 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
305 ASSERT(pFCB
->RefCount
> 0);
311 PDEVICE_EXTENSION pVCB
,
316 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
317 pFCB
, &pFCB
->PathNameU
, pFCB
->RefCount
);
319 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
323 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
324 ASSERT(pFCB
->RefCount
> 0);
326 if (pFCB
->RefCount
== 0)
328 ASSERT(pFCB
->OpenHandleCount
== 0);
329 tmpFcb
= pFCB
->parentFcb
;
330 vfatDelFCBFromTable(pVCB
, pFCB
);
331 vfatDestroyFCB(pFCB
);
344 PDEVICE_EXTENSION pVCB
,
350 ASSERT(pFCB
->Hash
.Hash
== vfatNameHash(0, &pFCB
->PathNameU
));
351 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
352 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
354 InsertTailList(&pVCB
->FcbListHead
, &pFCB
->FcbListEntry
);
356 pFCB
->Hash
.next
= pVCB
->FcbHashTable
[Index
];
357 pVCB
->FcbHashTable
[Index
] = &pFCB
->Hash
;
358 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
360 pFCB
->ShortHash
.next
= pVCB
->FcbHashTable
[ShortIndex
];
361 pVCB
->FcbHashTable
[ShortIndex
] = &pFCB
->ShortHash
;
365 vfatGrabFCB(pVCB
, pFCB
->parentFcb
);
371 vfatInitFCBFromDirEntry(
372 PDEVICE_EXTENSION Vcb
,
374 PVFAT_DIRENTRY_CONTEXT DirContext
)
378 RtlCopyMemory(&Fcb
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
379 RtlCopyUnicodeString(&Fcb
->ShortNameU
, &DirContext
->ShortNameU
);
380 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
381 if (Vcb
->Flags
& VCB_IS_FATX
)
383 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
387 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
388 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
391 if (vfatFCBIsDirectory(Fcb
))
393 ULONG FirstCluster
, CurrentCluster
;
394 NTSTATUS Status
= STATUS_SUCCESS
;
396 FirstCluster
= vfatDirEntryGetFirstCluster(Vcb
, &Fcb
->entry
);
397 if (FirstCluster
== 1)
399 Size
= Vcb
->FatInfo
.rootDirectorySectors
* Vcb
->FatInfo
.BytesPerSector
;
401 else if (FirstCluster
!= 0)
403 CurrentCluster
= FirstCluster
;
404 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
406 Size
+= Vcb
->FatInfo
.BytesPerCluster
;
407 Status
= NextCluster(Vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
411 else if (Fcb
->Flags
& FCB_IS_FATX_ENTRY
)
413 Size
= Fcb
->entry
.FatX
.FileSize
;
417 Size
= Fcb
->entry
.Fat
.FileSize
;
419 Fcb
->dirIndex
= DirContext
->DirIndex
;
420 Fcb
->startIndex
= DirContext
->StartIndex
;
421 if ((Fcb
->Flags
& FCB_IS_FATX_ENTRY
) && !vfatFCBIsRoot(Fcb
))
423 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
424 Fcb
->dirIndex
= DirContext
->DirIndex
-2;
425 Fcb
->startIndex
= DirContext
->StartIndex
-2;
427 Fcb
->RFCB
.FileSize
.QuadPart
= Size
;
428 Fcb
->RFCB
.ValidDataLength
.QuadPart
= Size
;
429 Fcb
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP_64(Size
, Vcb
->FatInfo
.BytesPerCluster
);
433 vfatSetFCBNewDirName(
434 PDEVICE_EXTENSION pVCB
,
439 UNICODE_STRING NewNameU
;
441 /* Get full path name */
442 Status
= vfatMakeFullName(ParentFcb
, &Fcb
->LongNameU
, &Fcb
->ShortNameU
, &NewNameU
);
443 if (!NT_SUCCESS(Status
))
448 /* Delete old name */
449 if (Fcb
->PathNameBuffer
)
451 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
453 Fcb
->PathNameU
= NewNameU
;
455 /* Delete from table */
456 vfatDelFCBFromTable(pVCB
, Fcb
);
458 /* Split it properly */
459 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
460 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
461 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
462 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
463 if (pVCB
->Flags
& VCB_IS_FATX
)
465 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
469 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
470 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
473 vfatAddFCBToTable(pVCB
, Fcb
);
474 vfatReleaseFCB(pVCB
, ParentFcb
);
476 return STATUS_SUCCESS
;
481 PDEVICE_EXTENSION pVCB
,
483 PVFAT_DIRENTRY_CONTEXT DirContext
,
489 DPRINT("vfatUpdateFCB(%p, %p, %p, %p)\n", pVCB
, Fcb
, DirContext
, ParentFcb
);
491 /* Get full path name */
492 Status
= vfatMakeFullName(ParentFcb
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &Fcb
->PathNameU
);
493 if (!NT_SUCCESS(Status
))
498 /* Delete old name */
499 if (Fcb
->PathNameBuffer
)
501 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
504 /* Delete from table */
505 vfatDelFCBFromTable(pVCB
, Fcb
);
507 /* Split it properly */
508 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
509 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
510 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
512 /* Save old parent */
513 OldParent
= Fcb
->parentFcb
;
514 RemoveEntryList(&Fcb
->ParentListEntry
);
517 vfatInitFCBFromDirEntry(pVCB
, Fcb
, DirContext
);
519 if (vfatFCBIsDirectory(Fcb
))
521 CcFlushCache(&Fcb
->SectionObjectPointers
, NULL
, 0, NULL
);
523 Fcb
->parentFcb
= ParentFcb
;
524 InsertTailList(&ParentFcb
->ParentListHead
, &Fcb
->ParentListEntry
);
525 vfatAddFCBToTable(pVCB
, Fcb
);
527 /* If we moved across directories, dereference our old parent
528 * We also dereference in case we're just renaming since AddFCBToTable references it
530 vfatReleaseFCB(pVCB
, OldParent
);
532 return STATUS_SUCCESS
;
536 vfatGrabFCBFromTable(
537 PDEVICE_EXTENSION pVCB
,
538 PUNICODE_STRING PathNameU
)
542 UNICODE_STRING DirNameU
;
543 UNICODE_STRING FileNameU
;
544 PUNICODE_STRING FcbNameU
;
548 DPRINT("'%wZ'\n", PathNameU
);
550 ASSERT(PathNameU
->Length
>= sizeof(WCHAR
) && PathNameU
->Buffer
[0] == L
'\\');
551 Hash
= vfatNameHash(0, PathNameU
);
553 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
556 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
561 if (entry
->Hash
== Hash
)
564 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
565 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
567 if (rcFCB
->Hash
.Hash
== Hash
)
569 FcbNameU
= &rcFCB
->LongNameU
;
573 FcbNameU
= &rcFCB
->ShortNameU
;
575 /* compare the file name */
576 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
577 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
579 vfatGrabFCB(pVCB
, rcFCB
);
591 vfatFCBInitializeCacheFromVolume(
595 PFILE_OBJECT fileObject
;
599 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
601 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
604 ObDereferenceObject(fileObject
);
605 return STATUS_INSUFFICIENT_RESOURCES
;
607 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
609 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
610 fileObject
->FsContext
= fcb
;
611 fileObject
->FsContext2
= newCCB
;
612 fcb
->FileObject
= fileObject
;
616 CcInitializeCacheMap(fileObject
,
617 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
619 &VfatGlobalData
->CacheMgrCallbacks
,
622 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
624 status
= _SEH2_GetExceptionCode();
625 fcb
->FileObject
= NULL
;
626 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
627 ObDereferenceObject(fileObject
);
632 vfatGrabFCB(vcb
, fcb
);
633 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
634 return STATUS_SUCCESS
;
639 PDEVICE_EXTENSION pVCB
)
642 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
643 NTSTATUS Status
= STATUS_SUCCESS
;
644 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
646 FCB
= vfatNewFCB(pVCB
, &NameU
);
647 if (FCB
->Flags
& FCB_IS_FATX_ENTRY
)
649 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
650 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
651 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
652 FCB
->entry
.FatX
.FirstCluster
= 1;
653 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
657 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
658 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
659 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
660 if (pVCB
->FatInfo
.FatType
== FAT32
)
662 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
663 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
664 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
666 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
668 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
669 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
674 FCB
->entry
.Fat
.FirstCluster
= 1;
675 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
678 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
681 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
682 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
683 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
684 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
686 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
687 vfatAddFCBToTable(pVCB
, FCB
);
694 PDEVICE_EXTENSION pVCB
)
697 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
699 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
702 FCB
= vfatMakeRootFCB(pVCB
);
709 vfatMakeFCBFromDirEntry(
711 PVFATFCB directoryFCB
,
712 PVFAT_DIRENTRY_CONTEXT DirContext
,
716 UNICODE_STRING NameU
;
719 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
720 if (!NT_SUCCESS(Status
))
725 rcFCB
= vfatNewFCB(vcb
, &NameU
);
726 vfatInitFCBFromDirEntry(vcb
, rcFCB
, DirContext
);
729 if (vfatFCBIsDirectory(rcFCB
))
731 vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
733 rcFCB
->parentFcb
= directoryFCB
;
734 InsertTailList(&directoryFCB
->ParentListHead
, &rcFCB
->ParentListEntry
);
735 vfatAddFCBToTable(vcb
, rcFCB
);
738 ExFreePool(NameU
.Buffer
);
739 return STATUS_SUCCESS
;
743 vfatAttachFCBToFileObject(
744 PDEVICE_EXTENSION vcb
,
746 PFILE_OBJECT fileObject
)
750 UNREFERENCED_PARAMETER(vcb
);
752 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
755 return STATUS_INSUFFICIENT_RESOURCES
;
757 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
759 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
760 fileObject
->FsContext
= fcb
;
761 fileObject
->FsContext2
= newCCB
;
762 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
764 return STATUS_SUCCESS
;
769 PDEVICE_EXTENSION pDeviceExt
,
770 PVFATFCB pDirectoryFCB
,
771 PUNICODE_STRING FileToFindU
,
775 PVOID Context
= NULL
;
777 BOOLEAN First
= TRUE
;
778 VFAT_DIRENTRY_CONTEXT DirContext
;
779 /* This buffer must have a size of 260 characters, because
780 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
781 WCHAR LongNameBuffer
[260];
782 WCHAR ShortNameBuffer
[13];
783 BOOLEAN FoundLong
= FALSE
;
784 BOOLEAN FoundShort
= FALSE
;
787 ASSERT(pDirectoryFCB
);
790 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
791 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
792 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
794 DirContext
.DirIndex
= 0;
795 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
796 DirContext
.LongNameU
.Length
= 0;
797 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
798 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
799 DirContext
.ShortNameU
.Length
= 0;
800 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
804 status
= pDeviceExt
->GetNextDirEntry(&Context
,
810 if (status
== STATUS_NO_MORE_ENTRIES
)
812 return STATUS_OBJECT_NAME_NOT_FOUND
;
814 if (!NT_SUCCESS(status
))
819 DPRINT(" Index:%u longName:%wZ\n",
820 DirContext
.DirIndex
, &DirContext
.LongNameU
);
822 if (!ENTRY_VOLUME(pDeviceExt
, &DirContext
.DirEntry
))
824 if (DirContext
.LongNameU
.Length
== 0 ||
825 DirContext
.ShortNameU
.Length
== 0)
827 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
828 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
830 ASSERT(DirContext
.LongNameU
.Length
!= 0 &&
831 DirContext
.ShortNameU
.Length
!= 0);
833 DirContext
.DirIndex
++;
836 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
837 if (FoundLong
== FALSE
)
839 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
841 if (FoundLong
|| FoundShort
)
843 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
847 CcUnpinData(Context
);
851 DirContext
.DirIndex
++;
854 return STATUS_OBJECT_NAME_NOT_FOUND
;
859 PDEVICE_EXTENSION pVCB
,
860 PVFATFCB
*pParentFCB
,
862 PUNICODE_STRING pFileNameU
)
867 UNICODE_STRING NameU
;
868 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
869 UNICODE_STRING FileNameU
;
870 WCHAR NameBuffer
[260];
871 PWCHAR curr
, prev
, last
;
874 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
875 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
877 RtlInitEmptyUnicodeString(&FileNameU
, NameBuffer
, sizeof(NameBuffer
));
879 parentFCB
= *pParentFCB
;
881 if (parentFCB
== NULL
)
883 /* Passed-in name is the full name */
884 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
886 // Trivial case, open of the root directory on volume
887 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
889 DPRINT("returning root FCB\n");
891 FCB
= vfatOpenRootFCB(pVCB
);
895 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
898 /* Check for an existing FCB */
899 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
903 *pParentFCB
= FCB
->parentFcb
;
904 vfatGrabFCB(pVCB
, *pParentFCB
);
905 return STATUS_SUCCESS
;
908 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
909 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
914 if (curr
> FileNameU
.Buffer
)
916 NameU
.Buffer
= FileNameU
.Buffer
;
917 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
918 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
921 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
922 if (Length
!= FCB
->PathNameU
.Length
)
924 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
926 vfatReleaseFCB(pVCB
, FCB
);
927 return STATUS_OBJECT_NAME_INVALID
;
929 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
930 curr
, FileNameU
.Length
- Length
);
931 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
932 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
933 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
935 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
945 FCB
= vfatOpenRootFCB(pVCB
);
946 curr
= FileNameU
.Buffer
;
954 /* Make absolute path */
955 RtlCopyUnicodeString(&FileNameU
, &parentFCB
->PathNameU
);
956 curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
959 RtlAppendUnicodeToString(&FileNameU
, L
"\\");
962 ASSERT(*curr
== L
'\\');
963 RtlAppendUnicodeStringToString(&FileNameU
, pFileNameU
);
968 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
975 vfatReleaseFCB(pVCB
, parentFCB
);
978 // fail if element in FCB is not a directory
979 if (!vfatFCBIsDirectory(FCB
))
981 DPRINT ("Element in requested path is not a directory\n");
983 vfatReleaseFCB(pVCB
, FCB
);
988 return STATUS_OBJECT_PATH_NOT_FOUND
;
993 Length
= (curr
- prev
) * sizeof(WCHAR
);
994 if (Length
!= parentFCB
->LongNameU
.Length
)
996 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
998 vfatReleaseFCB(pVCB
, parentFCB
);
1001 return STATUS_OBJECT_NAME_INVALID
;
1003 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
1004 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
1005 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
1006 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
1007 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
1009 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
1013 while (*curr
!= L
'\\' && curr
<= last
)
1017 NameU
.Buffer
= FileNameU
.Buffer
;
1018 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
1019 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
1020 DPRINT("%wZ\n", &NameU
);
1021 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
1024 NameU
.Buffer
= prev
;
1025 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
1026 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
1027 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1032 *pParentFCB
= parentFCB
;
1033 return STATUS_OBJECT_NAME_NOT_FOUND
;
1037 vfatReleaseFCB(pVCB
, parentFCB
);
1039 return STATUS_OBJECT_PATH_NOT_FOUND
;
1042 else if (!NT_SUCCESS(status
))
1044 vfatReleaseFCB(pVCB
, parentFCB
);
1053 *pParentFCB
= parentFCB
;
1056 return STATUS_SUCCESS
;