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
;
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 ExFreePool(pFCB
->PathNameBuffer
);
269 ExDeleteResourceLite(&pFCB
->PagingIoResource
);
270 ExDeleteResourceLite(&pFCB
->MainResource
);
271 ExFreeToNPagedLookasideList(&VfatGlobalData
->FcbLookasideList
, pFCB
);
278 return ((*FCB
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
285 return FCB
->PathNameU
.Length
== sizeof(WCHAR
) && FCB
->PathNameU
.Buffer
[0] == L
'\\' ? TRUE
: FALSE
;
290 PDEVICE_EXTENSION pVCB
,
295 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
296 pFCB
, &pFCB
->PathNameU
, pFCB
->RefCount
);
301 if (pFCB
->RefCount
== 0)
303 ASSERT(pFCB
->OpenHandleCount
== 0);
304 tmpFcb
= pFCB
->parentFcb
;
305 RemoveEntryList (&pFCB
->FcbListEntry
);
306 vfatDelFCBFromTable(pVCB
, pFCB
);
307 vfatDestroyFCB(pFCB
);
320 PDEVICE_EXTENSION pVCB
,
326 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
327 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
329 InsertTailList(&pVCB
->FcbListHead
, &pFCB
->FcbListEntry
);
331 pFCB
->Hash
.next
= pVCB
->FcbHashTable
[Index
];
332 pVCB
->FcbHashTable
[Index
] = &pFCB
->Hash
;
333 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
335 pFCB
->ShortHash
.next
= pVCB
->FcbHashTable
[ShortIndex
];
336 pVCB
->FcbHashTable
[ShortIndex
] = &pFCB
->ShortHash
;
340 pFCB
->parentFcb
->RefCount
++;
346 PDEVICE_EXTENSION pVCB
,
348 PUNICODE_STRING LongName
,
349 PUNICODE_STRING ShortName
,
355 DPRINT("vfatUpdateFCB(%p, %p, %wZ, %wZ, %p)\n", pVCB
, Fcb
, LongName
, ShortName
, ParentFcb
);
357 /* Delete old name */
358 if (Fcb
->PathNameBuffer
)
360 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
363 /* Delete from table */
364 vfatDelFCBFromTable(pVCB
, Fcb
);
366 /* Get full path name */
367 Status
= vfatMakeFullName(ParentFcb
, LongName
, ShortName
, &Fcb
->PathNameU
);
368 if (!NT_SUCCESS(Status
))
373 /* Split it properly */
374 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
375 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
376 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
378 /* Copy short name */
379 RtlCopyUnicodeString(&Fcb
->ShortNameU
, ShortName
);
381 /* Recompute hashes */
382 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
383 if (pVCB
->Flags
& VCB_IS_FATX
)
385 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
389 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
390 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
394 OldParent
= Fcb
->parentFcb
;
395 Fcb
->parentFcb
= ParentFcb
;
397 /* Add to the table */
398 vfatAddFCBToTable(pVCB
, Fcb
);
400 /* If we moved accross directories, dereferenced our old parent
401 * We also derefence in case we're just renaming since AddFCBToTable references it
403 vfatReleaseFCB(pVCB
, OldParent
);
405 /* In case we were moving accross directories, reset caching on old parent */
406 //if (OldParent != ParentFcb)
408 // CcUninitializeCacheMap(OldParent->FileObject, NULL, NULL);
411 return STATUS_SUCCESS
;
415 vfatGrabFCBFromTable(
416 PDEVICE_EXTENSION pVCB
,
417 PUNICODE_STRING PathNameU
)
421 UNICODE_STRING DirNameU
;
422 UNICODE_STRING FileNameU
;
423 PUNICODE_STRING FcbNameU
;
427 DPRINT("'%wZ'\n", PathNameU
);
429 Hash
= vfatNameHash(0, PathNameU
);
431 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
434 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
439 if (entry
->Hash
== Hash
)
442 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
443 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
445 if (rcFCB
->Hash
.Hash
== Hash
)
447 FcbNameU
= &rcFCB
->LongNameU
;
451 FcbNameU
= &rcFCB
->ShortNameU
;
453 /* compare the file name */
454 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
455 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
469 vfatFCBInitializeCacheFromVolume(
473 PFILE_OBJECT fileObject
;
477 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
479 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
482 ObDereferenceObject(fileObject
);
483 return STATUS_INSUFFICIENT_RESOURCES
;
485 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
487 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
488 fileObject
->FsContext
= fcb
;
489 fileObject
->FsContext2
= newCCB
;
490 fcb
->FileObject
= fileObject
;
495 CcInitializeCacheMap(fileObject
,
496 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
498 &VfatGlobalData
->CacheMgrCallbacks
,
501 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
503 status
= _SEH2_GetExceptionCode();
505 fcb
->FileObject
= NULL
;
506 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
507 ObDereferenceObject(fileObject
);
512 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
513 return STATUS_SUCCESS
;
518 PDEVICE_EXTENSION pVCB
)
521 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
522 NTSTATUS Status
= STATUS_SUCCESS
;
523 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
525 FCB
= vfatNewFCB(pVCB
, &NameU
);
526 if (FCB
->Flags
& FCB_IS_FATX_ENTRY
)
528 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
529 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
530 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
531 FCB
->entry
.FatX
.FirstCluster
= 1;
532 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
536 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
537 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
538 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
539 if (pVCB
->FatInfo
.FatType
== FAT32
)
541 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
542 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
543 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
545 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
547 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
548 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
553 FCB
->entry
.Fat
.FirstCluster
= 1;
554 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
557 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
560 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
561 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
562 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
563 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
565 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
566 vfatAddFCBToTable(pVCB
, FCB
);
573 PDEVICE_EXTENSION pVCB
)
576 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
578 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
581 FCB
= vfatMakeRootFCB(pVCB
);
588 vfatMakeFCBFromDirEntry(
590 PVFATFCB directoryFCB
,
591 PVFAT_DIRENTRY_CONTEXT DirContext
,
596 UNICODE_STRING NameU
;
599 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
600 if (!NT_SUCCESS(Status
))
605 rcFCB
= vfatNewFCB(vcb
, &NameU
);
606 RtlCopyMemory(&rcFCB
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
607 RtlCopyUnicodeString(&rcFCB
->ShortNameU
, &DirContext
->ShortNameU
);
608 if (vcb
->Flags
& VCB_IS_FATX
)
610 rcFCB
->ShortHash
.Hash
= rcFCB
->Hash
.Hash
;
614 rcFCB
->ShortHash
.Hash
= vfatNameHash(0, &rcFCB
->DirNameU
);
615 rcFCB
->ShortHash
.Hash
= vfatNameHash(rcFCB
->ShortHash
.Hash
, &rcFCB
->ShortNameU
);
618 if (vfatFCBIsDirectory(rcFCB
))
620 ULONG FirstCluster
, CurrentCluster
;
621 NTSTATUS Status
= STATUS_SUCCESS
;
623 FirstCluster
= vfatDirEntryGetFirstCluster(vcb
, &rcFCB
->entry
);
624 if (FirstCluster
== 1)
626 Size
= vcb
->FatInfo
.rootDirectorySectors
* vcb
->FatInfo
.BytesPerSector
;
628 else if (FirstCluster
!= 0)
630 CurrentCluster
= FirstCluster
;
631 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
633 Size
+= vcb
->FatInfo
.BytesPerCluster
;
634 Status
= NextCluster(vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
638 else if (rcFCB
->Flags
& FCB_IS_FATX_ENTRY
)
640 Size
= rcFCB
->entry
.FatX
.FileSize
;
644 Size
= rcFCB
->entry
.Fat
.FileSize
;
646 rcFCB
->dirIndex
= DirContext
->DirIndex
;
647 rcFCB
->startIndex
= DirContext
->StartIndex
;
648 if ((rcFCB
->Flags
& FCB_IS_FATX_ENTRY
) && !vfatFCBIsRoot(directoryFCB
))
650 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
651 rcFCB
->dirIndex
= DirContext
->DirIndex
-2;
652 rcFCB
->startIndex
= DirContext
->StartIndex
-2;
654 rcFCB
->RFCB
.FileSize
.QuadPart
= Size
;
655 rcFCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
656 rcFCB
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP(Size
, vcb
->FatInfo
.BytesPerCluster
);
658 if (vfatFCBIsDirectory(rcFCB
))
660 vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
662 rcFCB
->parentFcb
= directoryFCB
;
663 vfatAddFCBToTable(vcb
, rcFCB
);
666 ExFreePool(NameU
.Buffer
);
667 return STATUS_SUCCESS
;
671 vfatAttachFCBToFileObject(
672 PDEVICE_EXTENSION vcb
,
674 PFILE_OBJECT fileObject
)
678 UNREFERENCED_PARAMETER(vcb
);
680 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
683 return STATUS_INSUFFICIENT_RESOURCES
;
685 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
687 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
688 fileObject
->FsContext
= fcb
;
689 fileObject
->FsContext2
= newCCB
;
690 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
692 return STATUS_SUCCESS
;
697 PDEVICE_EXTENSION pDeviceExt
,
698 PVFATFCB pDirectoryFCB
,
699 PUNICODE_STRING FileToFindU
,
703 PVOID Context
= NULL
;
705 BOOLEAN First
= TRUE
;
706 VFAT_DIRENTRY_CONTEXT DirContext
;
707 /* This buffer must have a size of 260 characters, because
708 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
709 WCHAR LongNameBuffer
[260];
710 WCHAR ShortNameBuffer
[13];
711 BOOLEAN FoundLong
= FALSE
;
712 BOOLEAN FoundShort
= FALSE
;
715 ASSERT(pDirectoryFCB
);
718 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
719 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
720 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
722 DirContext
.DirIndex
= 0;
723 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
724 DirContext
.LongNameU
.Length
= 0;
725 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
726 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
727 DirContext
.ShortNameU
.Length
= 0;
728 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
732 status
= pDeviceExt
->GetNextDirEntry(&Context
,
738 if (status
== STATUS_NO_MORE_ENTRIES
)
740 return STATUS_OBJECT_NAME_NOT_FOUND
;
742 if (!NT_SUCCESS(status
))
747 DPRINT(" Index:%u longName:%wZ\n",
748 DirContext
.DirIndex
, &DirContext
.LongNameU
);
750 if (!ENTRY_VOLUME(pDeviceExt
, &DirContext
.DirEntry
))
752 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
753 if (FoundLong
== FALSE
)
755 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
757 if (FoundLong
|| FoundShort
)
759 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
763 CcUnpinData(Context
);
767 DirContext
.DirIndex
++;
770 return STATUS_OBJECT_NAME_NOT_FOUND
;
775 PDEVICE_EXTENSION pVCB
,
776 PVFATFCB
*pParentFCB
,
778 PUNICODE_STRING pFileNameU
)
783 UNICODE_STRING NameU
;
784 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
785 UNICODE_STRING FileNameU
;
786 WCHAR NameBuffer
[260];
787 PWCHAR curr
, prev
, last
;
790 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
791 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
793 FileNameU
.Buffer
= NameBuffer
;
794 FileNameU
.MaximumLength
= sizeof(NameBuffer
);
795 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
797 parentFCB
= *pParentFCB
;
799 if (parentFCB
== NULL
)
801 // Trivial case, open of the root directory on volume
802 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
804 DPRINT("returning root FCB\n");
806 FCB
= vfatOpenRootFCB(pVCB
);
810 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
813 /* Check for an existing FCB */
814 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
818 *pParentFCB
= FCB
->parentFcb
;
819 (*pParentFCB
)->RefCount
++;
820 return STATUS_SUCCESS
;
823 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
824 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
829 if (curr
> FileNameU
.Buffer
)
831 NameU
.Buffer
= FileNameU
.Buffer
;
832 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
833 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
836 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
837 if (Length
!= FCB
->PathNameU
.Length
)
839 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
841 vfatReleaseFCB(pVCB
, FCB
);
842 return STATUS_OBJECT_NAME_INVALID
;
844 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
845 curr
, FileNameU
.Length
- Length
);
846 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
847 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
848 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
850 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
860 FCB
= vfatOpenRootFCB(pVCB
);
861 curr
= FileNameU
.Buffer
;
871 prev
= curr
= FileNameU
.Buffer
- 1;
872 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
879 vfatReleaseFCB(pVCB
, parentFCB
);
882 // fail if element in FCB is not a directory
883 if (!vfatFCBIsDirectory(FCB
))
885 DPRINT ("Element in requested path is not a directory\n");
887 vfatReleaseFCB(pVCB
, FCB
);
892 return STATUS_OBJECT_PATH_NOT_FOUND
;
897 Length
= (curr
- prev
) * sizeof(WCHAR
);
898 if (Length
!= parentFCB
->LongNameU
.Length
)
900 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
902 vfatReleaseFCB(pVCB
, parentFCB
);
903 return STATUS_OBJECT_NAME_INVALID
;
905 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
906 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
907 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
908 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
909 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
911 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
915 while (*curr
!= L
'\\' && curr
<= last
)
919 NameU
.Buffer
= FileNameU
.Buffer
;
920 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
921 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
922 DPRINT("%wZ\n", &NameU
);
923 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
927 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
928 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
929 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
934 *pParentFCB
= parentFCB
;
935 return STATUS_OBJECT_NAME_NOT_FOUND
;
939 vfatReleaseFCB(pVCB
, parentFCB
);
941 return STATUS_OBJECT_PATH_NOT_FOUND
;
944 else if (!NT_SUCCESS(status
))
946 vfatReleaseFCB(pVCB
, parentFCB
);
955 *pParentFCB
= parentFCB
;
958 return STATUS_SUCCESS
;