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
;
163 PDEVICE_EXTENSION pVCB
,
170 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
171 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
173 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
175 entry
= pVCB
->FcbHashTable
[ShortIndex
];
176 if (entry
->self
== pFCB
)
178 pVCB
->FcbHashTable
[ShortIndex
] = entry
->next
;
182 while (entry
->next
->self
!= pFCB
)
186 entry
->next
= pFCB
->ShortHash
.next
;
189 entry
= pVCB
->FcbHashTable
[Index
];
190 if (entry
->self
== pFCB
)
192 pVCB
->FcbHashTable
[Index
] = entry
->next
;
196 while (entry
->next
->self
!= pFCB
)
200 entry
->next
= pFCB
->Hash
.next
;
203 RemoveEntryList(&pFCB
->FcbListEntry
);
209 PVFATFCB directoryFCB
,
210 PUNICODE_STRING LongNameU
,
211 PUNICODE_STRING ShortNameU
,
212 PUNICODE_STRING NameU
)
214 PWCHAR PathNameBuffer
;
215 USHORT PathNameLength
;
217 PathNameLength
= directoryFCB
->PathNameU
.Length
+ max(LongNameU
->Length
, ShortNameU
->Length
);
218 if (!vfatFCBIsRoot(directoryFCB
))
220 PathNameLength
+= sizeof(WCHAR
);
223 if (PathNameLength
> LONGNAME_MAX_LENGTH
* sizeof(WCHAR
))
225 return STATUS_OBJECT_NAME_INVALID
;
227 PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameLength
+ sizeof(WCHAR
), TAG_FCB
);
230 return STATUS_INSUFFICIENT_RESOURCES
;
232 NameU
->Buffer
= PathNameBuffer
;
234 NameU
->MaximumLength
= PathNameLength
;
236 RtlCopyUnicodeString(NameU
, &directoryFCB
->PathNameU
);
237 if (!vfatFCBIsRoot(directoryFCB
))
239 RtlAppendUnicodeToString(NameU
, L
"\\");
241 if (LongNameU
->Length
> 0)
243 RtlAppendUnicodeStringToString(NameU
, LongNameU
);
247 RtlAppendUnicodeStringToString(NameU
, ShortNameU
);
249 NameU
->Buffer
[NameU
->Length
/ sizeof(WCHAR
)] = 0;
251 return STATUS_SUCCESS
;
258 if (pCcb
->SearchPattern
.Buffer
)
260 ExFreePoolWithTag(pCcb
->SearchPattern
.Buffer
, TAG_VFAT
);
262 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, pCcb
);
269 FsRtlUninitializeFileLock(&pFCB
->FileLock
);
270 ExFreePool(pFCB
->PathNameBuffer
);
271 ExDeleteResourceLite(&pFCB
->PagingIoResource
);
272 ExDeleteResourceLite(&pFCB
->MainResource
);
273 ExFreeToNPagedLookasideList(&VfatGlobalData
->FcbLookasideList
, pFCB
);
280 return ((*FCB
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
287 return FCB
->PathNameU
.Length
== sizeof(WCHAR
) && FCB
->PathNameU
.Buffer
[0] == L
'\\' ? TRUE
: FALSE
;
292 PDEVICE_EXTENSION pVCB
,
295 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
297 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
298 ASSERT(pFCB
->RefCount
> 0);
304 PDEVICE_EXTENSION pVCB
,
309 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
310 pFCB
, &pFCB
->PathNameU
, pFCB
->RefCount
);
312 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
316 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
317 ASSERT(pFCB
->RefCount
> 0);
319 if (pFCB
->RefCount
== 0)
321 ASSERT(pFCB
->OpenHandleCount
== 0);
322 tmpFcb
= pFCB
->parentFcb
;
323 vfatDelFCBFromTable(pVCB
, pFCB
);
324 vfatDestroyFCB(pFCB
);
337 PDEVICE_EXTENSION pVCB
,
343 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
344 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
346 InsertTailList(&pVCB
->FcbListHead
, &pFCB
->FcbListEntry
);
348 pFCB
->Hash
.next
= pVCB
->FcbHashTable
[Index
];
349 pVCB
->FcbHashTable
[Index
] = &pFCB
->Hash
;
350 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
352 pFCB
->ShortHash
.next
= pVCB
->FcbHashTable
[ShortIndex
];
353 pVCB
->FcbHashTable
[ShortIndex
] = &pFCB
->ShortHash
;
357 vfatGrabFCB(pVCB
, pFCB
->parentFcb
);
363 PDEVICE_EXTENSION pVCB
,
365 PUNICODE_STRING LongName
,
366 PUNICODE_STRING ShortName
,
372 DPRINT("vfatUpdateFCB(%p, %p, %wZ, %wZ, %p)\n", pVCB
, Fcb
, LongName
, ShortName
, ParentFcb
);
374 /* Get full path name */
375 Status
= vfatMakeFullName(ParentFcb
, LongName
, ShortName
, &Fcb
->PathNameU
);
376 if (!NT_SUCCESS(Status
))
381 /* Delete old name */
382 if (Fcb
->PathNameBuffer
)
384 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
387 /* Delete from table */
388 vfatDelFCBFromTable(pVCB
, Fcb
);
390 /* Split it properly */
391 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
392 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
393 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
395 /* Copy short name */
396 RtlCopyUnicodeString(&Fcb
->ShortNameU
, ShortName
);
398 /* Recompute hashes */
399 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
400 if (pVCB
->Flags
& VCB_IS_FATX
)
402 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
406 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
407 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
411 OldParent
= Fcb
->parentFcb
;
412 Fcb
->parentFcb
= ParentFcb
;
414 /* Add to the table */
415 vfatAddFCBToTable(pVCB
, Fcb
);
417 /* If we moved across directories, dereference our old parent
418 * We also dereference in case we're just renaming since AddFCBToTable references it
420 vfatReleaseFCB(pVCB
, OldParent
);
422 return STATUS_SUCCESS
;
426 vfatGrabFCBFromTable(
427 PDEVICE_EXTENSION pVCB
,
428 PUNICODE_STRING PathNameU
)
432 UNICODE_STRING DirNameU
;
433 UNICODE_STRING FileNameU
;
434 PUNICODE_STRING FcbNameU
;
438 DPRINT("'%wZ'\n", PathNameU
);
440 ASSERT(PathNameU
->Length
>= sizeof(WCHAR
) && PathNameU
->Buffer
[0] == L
'\\');
441 Hash
= vfatNameHash(0, PathNameU
);
443 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
446 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
451 if (entry
->Hash
== Hash
)
454 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
455 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
457 if (rcFCB
->Hash
.Hash
== Hash
)
459 FcbNameU
= &rcFCB
->LongNameU
;
463 FcbNameU
= &rcFCB
->ShortNameU
;
465 /* compare the file name */
466 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
467 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
469 vfatGrabFCB(pVCB
, rcFCB
);
481 vfatFCBInitializeCacheFromVolume(
485 PFILE_OBJECT fileObject
;
489 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
491 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
494 ObDereferenceObject(fileObject
);
495 return STATUS_INSUFFICIENT_RESOURCES
;
497 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
499 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
500 fileObject
->FsContext
= fcb
;
501 fileObject
->FsContext2
= newCCB
;
502 fcb
->FileObject
= fileObject
;
506 CcInitializeCacheMap(fileObject
,
507 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
509 &VfatGlobalData
->CacheMgrCallbacks
,
512 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
514 status
= _SEH2_GetExceptionCode();
515 fcb
->FileObject
= NULL
;
516 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
517 ObDereferenceObject(fileObject
);
522 vfatGrabFCB(vcb
, fcb
);
523 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
524 return STATUS_SUCCESS
;
529 PDEVICE_EXTENSION pVCB
)
532 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
533 NTSTATUS Status
= STATUS_SUCCESS
;
534 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
536 FCB
= vfatNewFCB(pVCB
, &NameU
);
537 if (FCB
->Flags
& FCB_IS_FATX_ENTRY
)
539 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
540 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
541 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
542 FCB
->entry
.FatX
.FirstCluster
= 1;
543 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
547 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
548 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
549 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
550 if (pVCB
->FatInfo
.FatType
== FAT32
)
552 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
553 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
554 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
556 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
558 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
559 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
564 FCB
->entry
.Fat
.FirstCluster
= 1;
565 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
568 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
571 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
572 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
573 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
574 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
576 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
577 vfatAddFCBToTable(pVCB
, FCB
);
584 PDEVICE_EXTENSION pVCB
)
587 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
589 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
592 FCB
= vfatMakeRootFCB(pVCB
);
599 vfatMakeFCBFromDirEntry(
601 PVFATFCB directoryFCB
,
602 PVFAT_DIRENTRY_CONTEXT DirContext
,
607 UNICODE_STRING NameU
;
610 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
611 if (!NT_SUCCESS(Status
))
616 rcFCB
= vfatNewFCB(vcb
, &NameU
);
617 RtlCopyMemory(&rcFCB
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
618 RtlCopyUnicodeString(&rcFCB
->ShortNameU
, &DirContext
->ShortNameU
);
619 if (vcb
->Flags
& VCB_IS_FATX
)
621 rcFCB
->ShortHash
.Hash
= rcFCB
->Hash
.Hash
;
625 rcFCB
->ShortHash
.Hash
= vfatNameHash(0, &rcFCB
->DirNameU
);
626 rcFCB
->ShortHash
.Hash
= vfatNameHash(rcFCB
->ShortHash
.Hash
, &rcFCB
->ShortNameU
);
629 if (vfatFCBIsDirectory(rcFCB
))
631 ULONG FirstCluster
, CurrentCluster
;
632 NTSTATUS Status
= STATUS_SUCCESS
;
634 FirstCluster
= vfatDirEntryGetFirstCluster(vcb
, &rcFCB
->entry
);
635 if (FirstCluster
== 1)
637 Size
= vcb
->FatInfo
.rootDirectorySectors
* vcb
->FatInfo
.BytesPerSector
;
639 else if (FirstCluster
!= 0)
641 CurrentCluster
= FirstCluster
;
642 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
644 Size
+= vcb
->FatInfo
.BytesPerCluster
;
645 Status
= NextCluster(vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
649 else if (rcFCB
->Flags
& FCB_IS_FATX_ENTRY
)
651 Size
= rcFCB
->entry
.FatX
.FileSize
;
655 Size
= rcFCB
->entry
.Fat
.FileSize
;
657 rcFCB
->dirIndex
= DirContext
->DirIndex
;
658 rcFCB
->startIndex
= DirContext
->StartIndex
;
659 if ((rcFCB
->Flags
& FCB_IS_FATX_ENTRY
) && !vfatFCBIsRoot(directoryFCB
))
661 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
662 rcFCB
->dirIndex
= DirContext
->DirIndex
-2;
663 rcFCB
->startIndex
= DirContext
->StartIndex
-2;
665 rcFCB
->RFCB
.FileSize
.QuadPart
= Size
;
666 rcFCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
667 rcFCB
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP(Size
, vcb
->FatInfo
.BytesPerCluster
);
669 if (vfatFCBIsDirectory(rcFCB
))
671 vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
673 rcFCB
->parentFcb
= directoryFCB
;
674 vfatAddFCBToTable(vcb
, rcFCB
);
677 ExFreePool(NameU
.Buffer
);
678 return STATUS_SUCCESS
;
682 vfatAttachFCBToFileObject(
683 PDEVICE_EXTENSION vcb
,
685 PFILE_OBJECT fileObject
)
689 UNREFERENCED_PARAMETER(vcb
);
691 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
694 return STATUS_INSUFFICIENT_RESOURCES
;
696 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
698 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
699 fileObject
->FsContext
= fcb
;
700 fileObject
->FsContext2
= newCCB
;
701 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
703 return STATUS_SUCCESS
;
708 PDEVICE_EXTENSION pDeviceExt
,
709 PVFATFCB pDirectoryFCB
,
710 PUNICODE_STRING FileToFindU
,
714 PVOID Context
= NULL
;
716 BOOLEAN First
= TRUE
;
717 VFAT_DIRENTRY_CONTEXT DirContext
;
718 /* This buffer must have a size of 260 characters, because
719 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
720 WCHAR LongNameBuffer
[260];
721 WCHAR ShortNameBuffer
[13];
722 BOOLEAN FoundLong
= FALSE
;
723 BOOLEAN FoundShort
= FALSE
;
726 ASSERT(pDirectoryFCB
);
729 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
730 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
731 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
733 DirContext
.DirIndex
= 0;
734 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
735 DirContext
.LongNameU
.Length
= 0;
736 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
737 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
738 DirContext
.ShortNameU
.Length
= 0;
739 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
743 status
= pDeviceExt
->GetNextDirEntry(&Context
,
749 if (status
== STATUS_NO_MORE_ENTRIES
)
751 return STATUS_OBJECT_NAME_NOT_FOUND
;
753 if (!NT_SUCCESS(status
))
758 DPRINT(" Index:%u longName:%wZ\n",
759 DirContext
.DirIndex
, &DirContext
.LongNameU
);
761 if (!ENTRY_VOLUME(pDeviceExt
, &DirContext
.DirEntry
))
763 if (DirContext
.LongNameU
.Length
== 0 ||
764 DirContext
.ShortNameU
.Length
== 0)
766 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
767 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
769 ASSERT(DirContext
.LongNameU
.Length
!= 0 &&
770 DirContext
.ShortNameU
.Length
!= 0);
772 DirContext
.DirIndex
++;
775 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
776 if (FoundLong
== FALSE
)
778 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
780 if (FoundLong
|| FoundShort
)
782 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
786 CcUnpinData(Context
);
790 DirContext
.DirIndex
++;
793 return STATUS_OBJECT_NAME_NOT_FOUND
;
798 PDEVICE_EXTENSION pVCB
,
799 PVFATFCB
*pParentFCB
,
801 PUNICODE_STRING pFileNameU
)
806 UNICODE_STRING NameU
;
807 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
808 UNICODE_STRING FileNameU
;
809 WCHAR NameBuffer
[260];
810 PWCHAR curr
, prev
, last
;
813 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
814 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
816 RtlInitEmptyUnicodeString(&FileNameU
, NameBuffer
, sizeof(NameBuffer
));
818 parentFCB
= *pParentFCB
;
820 if (parentFCB
== NULL
)
822 /* Passed-in name is the full name */
823 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
825 // Trivial case, open of the root directory on volume
826 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
828 DPRINT("returning root FCB\n");
830 FCB
= vfatOpenRootFCB(pVCB
);
834 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
837 /* Check for an existing FCB */
838 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
842 *pParentFCB
= FCB
->parentFcb
;
843 vfatGrabFCB(pVCB
, *pParentFCB
);
844 return STATUS_SUCCESS
;
847 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
848 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
853 if (curr
> FileNameU
.Buffer
)
855 NameU
.Buffer
= FileNameU
.Buffer
;
856 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
857 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
860 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
861 if (Length
!= FCB
->PathNameU
.Length
)
863 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
865 vfatReleaseFCB(pVCB
, FCB
);
866 return STATUS_OBJECT_NAME_INVALID
;
868 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
869 curr
, FileNameU
.Length
- Length
);
870 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
871 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
872 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
874 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
884 FCB
= vfatOpenRootFCB(pVCB
);
885 curr
= FileNameU
.Buffer
;
893 /* Make absolute path */
894 RtlCopyUnicodeString(&FileNameU
, &parentFCB
->PathNameU
);
895 curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
898 RtlAppendUnicodeToString(&FileNameU
, L
"\\");
901 ASSERT(*curr
== L
'\\');
902 RtlAppendUnicodeStringToString(&FileNameU
, pFileNameU
);
907 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
914 vfatReleaseFCB(pVCB
, parentFCB
);
917 // fail if element in FCB is not a directory
918 if (!vfatFCBIsDirectory(FCB
))
920 DPRINT ("Element in requested path is not a directory\n");
922 vfatReleaseFCB(pVCB
, FCB
);
927 return STATUS_OBJECT_PATH_NOT_FOUND
;
932 Length
= (curr
- prev
) * sizeof(WCHAR
);
933 if (Length
!= parentFCB
->LongNameU
.Length
)
935 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
937 vfatReleaseFCB(pVCB
, parentFCB
);
940 return STATUS_OBJECT_NAME_INVALID
;
942 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
943 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
944 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
945 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
946 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
948 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
952 while (*curr
!= L
'\\' && curr
<= last
)
956 NameU
.Buffer
= FileNameU
.Buffer
;
957 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
958 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
959 DPRINT("%wZ\n", &NameU
);
960 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
964 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
965 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
966 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
971 *pParentFCB
= parentFCB
;
972 return STATUS_OBJECT_NAME_NOT_FOUND
;
976 vfatReleaseFCB(pVCB
, parentFCB
);
978 return STATUS_OBJECT_PATH_NOT_FOUND
;
981 else if (!NT_SUCCESS(status
))
983 vfatReleaseFCB(pVCB
, parentFCB
);
992 *pParentFCB
= parentFCB
;
995 return STATUS_SUCCESS
;