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 ExFreePool(pFCB
->PathNameBuffer
);
272 ExDeleteResourceLite(&pFCB
->PagingIoResource
);
273 ExDeleteResourceLite(&pFCB
->MainResource
);
274 ExFreeToNPagedLookasideList(&VfatGlobalData
->FcbLookasideList
, pFCB
);
275 RemoveEntryList(&pFCB
->ParentListEntry
);
276 ASSERT(IsListEmpty(&pFCB
->ParentListHead
));
283 return ((*FCB
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
290 return FCB
->PathNameU
.Length
== sizeof(WCHAR
) && FCB
->PathNameU
.Buffer
[0] == L
'\\' ? TRUE
: FALSE
;
295 PDEVICE_EXTENSION pVCB
,
298 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
300 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
301 ASSERT(pFCB
->RefCount
> 0);
307 PDEVICE_EXTENSION pVCB
,
312 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
313 pFCB
, &pFCB
->PathNameU
, pFCB
->RefCount
);
315 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
319 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
320 ASSERT(pFCB
->RefCount
> 0);
322 if (pFCB
->RefCount
== 0)
324 ASSERT(pFCB
->OpenHandleCount
== 0);
325 tmpFcb
= pFCB
->parentFcb
;
326 vfatDelFCBFromTable(pVCB
, pFCB
);
327 vfatDestroyFCB(pFCB
);
340 PDEVICE_EXTENSION pVCB
,
346 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
347 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
349 InsertTailList(&pVCB
->FcbListHead
, &pFCB
->FcbListEntry
);
351 pFCB
->Hash
.next
= pVCB
->FcbHashTable
[Index
];
352 pVCB
->FcbHashTable
[Index
] = &pFCB
->Hash
;
353 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
355 pFCB
->ShortHash
.next
= pVCB
->FcbHashTable
[ShortIndex
];
356 pVCB
->FcbHashTable
[ShortIndex
] = &pFCB
->ShortHash
;
360 vfatGrabFCB(pVCB
, pFCB
->parentFcb
);
366 vfatInitFCBFromDirEntry(
367 PDEVICE_EXTENSION Vcb
,
369 PVFAT_DIRENTRY_CONTEXT DirContext
)
373 RtlCopyMemory(&Fcb
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
374 RtlCopyUnicodeString(&Fcb
->ShortNameU
, &DirContext
->ShortNameU
);
375 if (Vcb
->Flags
& VCB_IS_FATX
)
377 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
381 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
382 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
385 if (vfatFCBIsDirectory(Fcb
))
387 ULONG FirstCluster
, CurrentCluster
;
388 NTSTATUS Status
= STATUS_SUCCESS
;
390 FirstCluster
= vfatDirEntryGetFirstCluster(Vcb
, &Fcb
->entry
);
391 if (FirstCluster
== 1)
393 Size
= Vcb
->FatInfo
.rootDirectorySectors
* Vcb
->FatInfo
.BytesPerSector
;
395 else if (FirstCluster
!= 0)
397 CurrentCluster
= FirstCluster
;
398 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
400 Size
+= Vcb
->FatInfo
.BytesPerCluster
;
401 Status
= NextCluster(Vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
405 else if (Fcb
->Flags
& FCB_IS_FATX_ENTRY
)
407 Size
= Fcb
->entry
.FatX
.FileSize
;
411 Size
= Fcb
->entry
.Fat
.FileSize
;
413 Fcb
->dirIndex
= DirContext
->DirIndex
;
414 Fcb
->startIndex
= DirContext
->StartIndex
;
415 if ((Fcb
->Flags
& FCB_IS_FATX_ENTRY
) && !vfatFCBIsRoot(Fcb
))
417 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
418 Fcb
->dirIndex
= DirContext
->DirIndex
-2;
419 Fcb
->startIndex
= DirContext
->StartIndex
-2;
421 Fcb
->RFCB
.FileSize
.QuadPart
= Size
;
422 Fcb
->RFCB
.ValidDataLength
.QuadPart
= Size
;
423 Fcb
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP_64(Size
, Vcb
->FatInfo
.BytesPerCluster
);
428 PDEVICE_EXTENSION pVCB
,
430 PVFAT_DIRENTRY_CONTEXT DirContext
,
436 DPRINT("vfatUpdateFCB(%p, %p, %p, %p)\n", pVCB
, Fcb
, DirContext
, ParentFcb
);
438 /* Get full path name */
439 Status
= vfatMakeFullName(ParentFcb
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &Fcb
->PathNameU
);
440 if (!NT_SUCCESS(Status
))
445 /* Delete old name */
446 if (Fcb
->PathNameBuffer
)
448 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
451 /* Delete from table */
452 vfatDelFCBFromTable(pVCB
, Fcb
);
454 /* Split it properly */
455 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
456 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
457 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
459 /* Save old parent */
460 OldParent
= Fcb
->parentFcb
;
461 RemoveEntryList(&Fcb
->ParentListEntry
);
464 vfatInitFCBFromDirEntry(pVCB
, Fcb
, DirContext
);
466 if (vfatFCBIsDirectory(Fcb
))
468 CcFlushCache(&Fcb
->SectionObjectPointers
, NULL
, 0, NULL
);
470 Fcb
->parentFcb
= ParentFcb
;
471 InsertTailList(&ParentFcb
->ParentListHead
, &Fcb
->ParentListEntry
);
472 vfatAddFCBToTable(pVCB
, Fcb
);
474 /* If we moved across directories, dereference our old parent
475 * We also dereference in case we're just renaming since AddFCBToTable references it
477 vfatReleaseFCB(pVCB
, OldParent
);
479 return STATUS_SUCCESS
;
483 vfatGrabFCBFromTable(
484 PDEVICE_EXTENSION pVCB
,
485 PUNICODE_STRING PathNameU
)
489 UNICODE_STRING DirNameU
;
490 UNICODE_STRING FileNameU
;
491 PUNICODE_STRING FcbNameU
;
495 DPRINT("'%wZ'\n", PathNameU
);
497 ASSERT(PathNameU
->Length
>= sizeof(WCHAR
) && PathNameU
->Buffer
[0] == L
'\\');
498 Hash
= vfatNameHash(0, PathNameU
);
500 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
503 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
508 if (entry
->Hash
== Hash
)
511 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
512 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
514 if (rcFCB
->Hash
.Hash
== Hash
)
516 FcbNameU
= &rcFCB
->LongNameU
;
520 FcbNameU
= &rcFCB
->ShortNameU
;
522 /* compare the file name */
523 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
524 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
526 vfatGrabFCB(pVCB
, rcFCB
);
538 vfatFCBInitializeCacheFromVolume(
542 PFILE_OBJECT fileObject
;
546 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
548 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
551 ObDereferenceObject(fileObject
);
552 return STATUS_INSUFFICIENT_RESOURCES
;
554 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
556 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
557 fileObject
->FsContext
= fcb
;
558 fileObject
->FsContext2
= newCCB
;
559 fcb
->FileObject
= fileObject
;
563 CcInitializeCacheMap(fileObject
,
564 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
566 &VfatGlobalData
->CacheMgrCallbacks
,
569 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
571 status
= _SEH2_GetExceptionCode();
572 fcb
->FileObject
= NULL
;
573 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
574 ObDereferenceObject(fileObject
);
579 vfatGrabFCB(vcb
, fcb
);
580 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
581 return STATUS_SUCCESS
;
586 PDEVICE_EXTENSION pVCB
)
589 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
590 NTSTATUS Status
= STATUS_SUCCESS
;
591 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
593 FCB
= vfatNewFCB(pVCB
, &NameU
);
594 if (FCB
->Flags
& FCB_IS_FATX_ENTRY
)
596 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
597 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
598 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
599 FCB
->entry
.FatX
.FirstCluster
= 1;
600 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
604 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
605 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
606 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
607 if (pVCB
->FatInfo
.FatType
== FAT32
)
609 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
610 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
611 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
613 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
615 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
616 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
621 FCB
->entry
.Fat
.FirstCluster
= 1;
622 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
625 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
628 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
629 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
630 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
631 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
633 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
634 vfatAddFCBToTable(pVCB
, FCB
);
641 PDEVICE_EXTENSION pVCB
)
644 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
646 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
649 FCB
= vfatMakeRootFCB(pVCB
);
656 vfatMakeFCBFromDirEntry(
658 PVFATFCB directoryFCB
,
659 PVFAT_DIRENTRY_CONTEXT DirContext
,
663 UNICODE_STRING NameU
;
666 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
667 if (!NT_SUCCESS(Status
))
672 rcFCB
= vfatNewFCB(vcb
, &NameU
);
673 vfatInitFCBFromDirEntry(vcb
, rcFCB
, DirContext
);
676 if (vfatFCBIsDirectory(rcFCB
))
678 vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
680 rcFCB
->parentFcb
= directoryFCB
;
681 InsertTailList(&directoryFCB
->ParentListHead
, &rcFCB
->ParentListEntry
);
682 vfatAddFCBToTable(vcb
, rcFCB
);
685 ExFreePool(NameU
.Buffer
);
686 return STATUS_SUCCESS
;
690 vfatAttachFCBToFileObject(
691 PDEVICE_EXTENSION vcb
,
693 PFILE_OBJECT fileObject
)
697 UNREFERENCED_PARAMETER(vcb
);
699 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
702 return STATUS_INSUFFICIENT_RESOURCES
;
704 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
706 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
707 fileObject
->FsContext
= fcb
;
708 fileObject
->FsContext2
= newCCB
;
709 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
711 return STATUS_SUCCESS
;
716 PDEVICE_EXTENSION pDeviceExt
,
717 PVFATFCB pDirectoryFCB
,
718 PUNICODE_STRING FileToFindU
,
722 PVOID Context
= NULL
;
724 BOOLEAN First
= TRUE
;
725 VFAT_DIRENTRY_CONTEXT DirContext
;
726 /* This buffer must have a size of 260 characters, because
727 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
728 WCHAR LongNameBuffer
[260];
729 WCHAR ShortNameBuffer
[13];
730 BOOLEAN FoundLong
= FALSE
;
731 BOOLEAN FoundShort
= FALSE
;
734 ASSERT(pDirectoryFCB
);
737 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
738 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
739 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
741 DirContext
.DirIndex
= 0;
742 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
743 DirContext
.LongNameU
.Length
= 0;
744 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
745 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
746 DirContext
.ShortNameU
.Length
= 0;
747 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
751 status
= pDeviceExt
->GetNextDirEntry(&Context
,
757 if (status
== STATUS_NO_MORE_ENTRIES
)
759 return STATUS_OBJECT_NAME_NOT_FOUND
;
761 if (!NT_SUCCESS(status
))
766 DPRINT(" Index:%u longName:%wZ\n",
767 DirContext
.DirIndex
, &DirContext
.LongNameU
);
769 if (!ENTRY_VOLUME(pDeviceExt
, &DirContext
.DirEntry
))
771 if (DirContext
.LongNameU
.Length
== 0 ||
772 DirContext
.ShortNameU
.Length
== 0)
774 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
775 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
777 ASSERT(DirContext
.LongNameU
.Length
!= 0 &&
778 DirContext
.ShortNameU
.Length
!= 0);
780 DirContext
.DirIndex
++;
783 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
784 if (FoundLong
== FALSE
)
786 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
788 if (FoundLong
|| FoundShort
)
790 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
794 CcUnpinData(Context
);
798 DirContext
.DirIndex
++;
801 return STATUS_OBJECT_NAME_NOT_FOUND
;
806 PDEVICE_EXTENSION pVCB
,
807 PVFATFCB
*pParentFCB
,
809 PUNICODE_STRING pFileNameU
)
814 UNICODE_STRING NameU
;
815 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
816 UNICODE_STRING FileNameU
;
817 WCHAR NameBuffer
[260];
818 PWCHAR curr
, prev
, last
;
821 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
822 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
824 RtlInitEmptyUnicodeString(&FileNameU
, NameBuffer
, sizeof(NameBuffer
));
826 parentFCB
= *pParentFCB
;
828 if (parentFCB
== NULL
)
830 /* Passed-in name is the full name */
831 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
833 // Trivial case, open of the root directory on volume
834 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
836 DPRINT("returning root FCB\n");
838 FCB
= vfatOpenRootFCB(pVCB
);
842 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
845 /* Check for an existing FCB */
846 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
850 *pParentFCB
= FCB
->parentFcb
;
851 vfatGrabFCB(pVCB
, *pParentFCB
);
852 return STATUS_SUCCESS
;
855 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
856 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
861 if (curr
> FileNameU
.Buffer
)
863 NameU
.Buffer
= FileNameU
.Buffer
;
864 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
865 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
868 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
869 if (Length
!= FCB
->PathNameU
.Length
)
871 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
873 vfatReleaseFCB(pVCB
, FCB
);
874 return STATUS_OBJECT_NAME_INVALID
;
876 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
877 curr
, FileNameU
.Length
- Length
);
878 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
879 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
880 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
882 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
892 FCB
= vfatOpenRootFCB(pVCB
);
893 curr
= FileNameU
.Buffer
;
901 /* Make absolute path */
902 RtlCopyUnicodeString(&FileNameU
, &parentFCB
->PathNameU
);
903 curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
906 RtlAppendUnicodeToString(&FileNameU
, L
"\\");
909 ASSERT(*curr
== L
'\\');
910 RtlAppendUnicodeStringToString(&FileNameU
, pFileNameU
);
915 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
922 vfatReleaseFCB(pVCB
, parentFCB
);
925 // fail if element in FCB is not a directory
926 if (!vfatFCBIsDirectory(FCB
))
928 DPRINT ("Element in requested path is not a directory\n");
930 vfatReleaseFCB(pVCB
, FCB
);
935 return STATUS_OBJECT_PATH_NOT_FOUND
;
940 Length
= (curr
- prev
) * sizeof(WCHAR
);
941 if (Length
!= parentFCB
->LongNameU
.Length
)
943 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
945 vfatReleaseFCB(pVCB
, parentFCB
);
948 return STATUS_OBJECT_NAME_INVALID
;
950 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
951 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
952 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
953 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
954 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
956 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
960 while (*curr
!= L
'\\' && curr
<= last
)
964 NameU
.Buffer
= FileNameU
.Buffer
;
965 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
966 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
967 DPRINT("%wZ\n", &NameU
);
968 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
972 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
973 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
974 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
979 *pParentFCB
= parentFCB
;
980 return STATUS_OBJECT_NAME_NOT_FOUND
;
984 vfatReleaseFCB(pVCB
, parentFCB
);
986 return STATUS_OBJECT_PATH_NOT_FOUND
;
989 else if (!NT_SUCCESS(status
))
991 vfatReleaseFCB(pVCB
, parentFCB
);
1000 *pParentFCB
= parentFCB
;
1003 return STATUS_SUCCESS
;