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
);
303 PDEVICE_EXTENSION pVCB
,
308 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
309 pFCB
, &pFCB
->PathNameU
, pFCB
->RefCount
);
311 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
315 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
317 if (pFCB
->RefCount
== 0)
319 ASSERT(pFCB
->OpenHandleCount
== 0);
320 tmpFcb
= pFCB
->parentFcb
;
321 vfatDelFCBFromTable(pVCB
, pFCB
);
322 vfatDestroyFCB(pFCB
);
335 PDEVICE_EXTENSION pVCB
,
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 PDEVICE_EXTENSION pVCB
,
363 PUNICODE_STRING LongName
,
364 PUNICODE_STRING ShortName
,
370 DPRINT("vfatUpdateFCB(%p, %p, %wZ, %wZ, %p)\n", pVCB
, Fcb
, LongName
, ShortName
, ParentFcb
);
372 /* Delete old name */
373 if (Fcb
->PathNameBuffer
)
375 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
378 /* Delete from table */
379 vfatDelFCBFromTable(pVCB
, Fcb
);
381 /* Get full path name */
382 Status
= vfatMakeFullName(ParentFcb
, LongName
, ShortName
, &Fcb
->PathNameU
);
383 if (!NT_SUCCESS(Status
))
388 /* Split it properly */
389 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
390 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
391 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
393 /* Copy short name */
394 RtlCopyUnicodeString(&Fcb
->ShortNameU
, ShortName
);
396 /* Recompute hashes */
397 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
398 if (pVCB
->Flags
& VCB_IS_FATX
)
400 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
404 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
405 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
409 OldParent
= Fcb
->parentFcb
;
410 Fcb
->parentFcb
= ParentFcb
;
412 /* Add to the table */
413 vfatAddFCBToTable(pVCB
, Fcb
);
415 /* If we moved accross directories, dereferenced our old parent
416 * We also derefence in case we're just renaming since AddFCBToTable references it
418 vfatReleaseFCB(pVCB
, OldParent
);
420 return STATUS_SUCCESS
;
424 vfatGrabFCBFromTable(
425 PDEVICE_EXTENSION pVCB
,
426 PUNICODE_STRING PathNameU
)
430 UNICODE_STRING DirNameU
;
431 UNICODE_STRING FileNameU
;
432 PUNICODE_STRING FcbNameU
;
436 DPRINT("'%wZ'\n", PathNameU
);
438 Hash
= vfatNameHash(0, PathNameU
);
440 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
443 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
448 if (entry
->Hash
== Hash
)
451 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
452 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
454 if (rcFCB
->Hash
.Hash
== Hash
)
456 FcbNameU
= &rcFCB
->LongNameU
;
460 FcbNameU
= &rcFCB
->ShortNameU
;
462 /* compare the file name */
463 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
464 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
466 vfatGrabFCB(pVCB
, rcFCB
);
478 vfatFCBInitializeCacheFromVolume(
482 PFILE_OBJECT fileObject
;
486 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
488 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
491 ObDereferenceObject(fileObject
);
492 return STATUS_INSUFFICIENT_RESOURCES
;
494 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
496 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
497 fileObject
->FsContext
= fcb
;
498 fileObject
->FsContext2
= newCCB
;
499 fcb
->FileObject
= fileObject
;
500 vfatGrabFCB(vcb
, fcb
);
504 CcInitializeCacheMap(fileObject
,
505 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
507 &VfatGlobalData
->CacheMgrCallbacks
,
510 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
512 status
= _SEH2_GetExceptionCode();
514 fcb
->FileObject
= NULL
;
515 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
516 ObDereferenceObject(fileObject
);
521 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
522 return STATUS_SUCCESS
;
527 PDEVICE_EXTENSION pVCB
)
530 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
531 NTSTATUS Status
= STATUS_SUCCESS
;
532 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
534 FCB
= vfatNewFCB(pVCB
, &NameU
);
535 if (FCB
->Flags
& FCB_IS_FATX_ENTRY
)
537 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
538 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
539 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
540 FCB
->entry
.FatX
.FirstCluster
= 1;
541 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
545 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
546 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
547 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
548 if (pVCB
->FatInfo
.FatType
== FAT32
)
550 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
551 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
552 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
554 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
556 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
557 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
562 FCB
->entry
.Fat
.FirstCluster
= 1;
563 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
566 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
569 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
570 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
571 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
572 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
574 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
575 vfatAddFCBToTable(pVCB
, FCB
);
582 PDEVICE_EXTENSION pVCB
)
585 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
587 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
590 FCB
= vfatMakeRootFCB(pVCB
);
597 vfatMakeFCBFromDirEntry(
599 PVFATFCB directoryFCB
,
600 PVFAT_DIRENTRY_CONTEXT DirContext
,
605 UNICODE_STRING NameU
;
608 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
609 if (!NT_SUCCESS(Status
))
614 rcFCB
= vfatNewFCB(vcb
, &NameU
);
615 RtlCopyMemory(&rcFCB
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
616 RtlCopyUnicodeString(&rcFCB
->ShortNameU
, &DirContext
->ShortNameU
);
617 if (vcb
->Flags
& VCB_IS_FATX
)
619 rcFCB
->ShortHash
.Hash
= rcFCB
->Hash
.Hash
;
623 rcFCB
->ShortHash
.Hash
= vfatNameHash(0, &rcFCB
->DirNameU
);
624 rcFCB
->ShortHash
.Hash
= vfatNameHash(rcFCB
->ShortHash
.Hash
, &rcFCB
->ShortNameU
);
627 if (vfatFCBIsDirectory(rcFCB
))
629 ULONG FirstCluster
, CurrentCluster
;
630 NTSTATUS Status
= STATUS_SUCCESS
;
632 FirstCluster
= vfatDirEntryGetFirstCluster(vcb
, &rcFCB
->entry
);
633 if (FirstCluster
== 1)
635 Size
= vcb
->FatInfo
.rootDirectorySectors
* vcb
->FatInfo
.BytesPerSector
;
637 else if (FirstCluster
!= 0)
639 CurrentCluster
= FirstCluster
;
640 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
642 Size
+= vcb
->FatInfo
.BytesPerCluster
;
643 Status
= NextCluster(vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
647 else if (rcFCB
->Flags
& FCB_IS_FATX_ENTRY
)
649 Size
= rcFCB
->entry
.FatX
.FileSize
;
653 Size
= rcFCB
->entry
.Fat
.FileSize
;
655 rcFCB
->dirIndex
= DirContext
->DirIndex
;
656 rcFCB
->startIndex
= DirContext
->StartIndex
;
657 if ((rcFCB
->Flags
& FCB_IS_FATX_ENTRY
) && !vfatFCBIsRoot(directoryFCB
))
659 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
660 rcFCB
->dirIndex
= DirContext
->DirIndex
-2;
661 rcFCB
->startIndex
= DirContext
->StartIndex
-2;
663 rcFCB
->RFCB
.FileSize
.QuadPart
= Size
;
664 rcFCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
665 rcFCB
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP(Size
, vcb
->FatInfo
.BytesPerCluster
);
666 vfatGrabFCB(vcb
, rcFCB
);
667 if (vfatFCBIsDirectory(rcFCB
))
669 vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
671 rcFCB
->parentFcb
= directoryFCB
;
672 vfatAddFCBToTable(vcb
, rcFCB
);
675 ExFreePool(NameU
.Buffer
);
676 return STATUS_SUCCESS
;
680 vfatAttachFCBToFileObject(
681 PDEVICE_EXTENSION vcb
,
683 PFILE_OBJECT fileObject
)
687 UNREFERENCED_PARAMETER(vcb
);
689 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
692 return STATUS_INSUFFICIENT_RESOURCES
;
694 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
696 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
697 fileObject
->FsContext
= fcb
;
698 fileObject
->FsContext2
= newCCB
;
699 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
701 return STATUS_SUCCESS
;
706 PDEVICE_EXTENSION pDeviceExt
,
707 PVFATFCB pDirectoryFCB
,
708 PUNICODE_STRING FileToFindU
,
712 PVOID Context
= NULL
;
714 BOOLEAN First
= TRUE
;
715 VFAT_DIRENTRY_CONTEXT DirContext
;
716 /* This buffer must have a size of 260 characters, because
717 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
718 WCHAR LongNameBuffer
[260];
719 WCHAR ShortNameBuffer
[13];
720 BOOLEAN FoundLong
= FALSE
;
721 BOOLEAN FoundShort
= FALSE
;
724 ASSERT(pDirectoryFCB
);
727 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
728 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
729 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
731 DirContext
.DirIndex
= 0;
732 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
733 DirContext
.LongNameU
.Length
= 0;
734 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
735 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
736 DirContext
.ShortNameU
.Length
= 0;
737 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
741 status
= pDeviceExt
->GetNextDirEntry(&Context
,
747 if (status
== STATUS_NO_MORE_ENTRIES
)
749 return STATUS_OBJECT_NAME_NOT_FOUND
;
751 if (!NT_SUCCESS(status
))
756 DPRINT(" Index:%u longName:%wZ\n",
757 DirContext
.DirIndex
, &DirContext
.LongNameU
);
759 if (!ENTRY_VOLUME(pDeviceExt
, &DirContext
.DirEntry
))
761 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
762 if (FoundLong
== FALSE
)
764 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
766 if (FoundLong
|| FoundShort
)
768 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
772 CcUnpinData(Context
);
776 DirContext
.DirIndex
++;
779 return STATUS_OBJECT_NAME_NOT_FOUND
;
784 PDEVICE_EXTENSION pVCB
,
785 PVFATFCB
*pParentFCB
,
787 PUNICODE_STRING pFileNameU
)
792 UNICODE_STRING NameU
;
793 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
794 UNICODE_STRING FileNameU
;
795 WCHAR NameBuffer
[260];
796 PWCHAR curr
, prev
, last
;
799 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
800 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
802 FileNameU
.Buffer
= NameBuffer
;
803 FileNameU
.MaximumLength
= sizeof(NameBuffer
);
804 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
806 parentFCB
= *pParentFCB
;
808 if (parentFCB
== NULL
)
810 // Trivial case, open of the root directory on volume
811 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
813 DPRINT("returning root FCB\n");
815 FCB
= vfatOpenRootFCB(pVCB
);
819 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
822 /* Check for an existing FCB */
823 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
827 *pParentFCB
= FCB
->parentFcb
;
828 vfatGrabFCB(pVCB
, *pParentFCB
);
829 return STATUS_SUCCESS
;
832 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
833 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
838 if (curr
> FileNameU
.Buffer
)
840 NameU
.Buffer
= FileNameU
.Buffer
;
841 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
842 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
845 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
846 if (Length
!= FCB
->PathNameU
.Length
)
848 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
850 vfatReleaseFCB(pVCB
, FCB
);
851 return STATUS_OBJECT_NAME_INVALID
;
853 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
854 curr
, FileNameU
.Length
- Length
);
855 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
856 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
857 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
859 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
869 FCB
= vfatOpenRootFCB(pVCB
);
870 curr
= FileNameU
.Buffer
;
880 prev
= curr
= FileNameU
.Buffer
- 1;
881 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
888 vfatReleaseFCB(pVCB
, parentFCB
);
891 // fail if element in FCB is not a directory
892 if (!vfatFCBIsDirectory(FCB
))
894 DPRINT ("Element in requested path is not a directory\n");
896 vfatReleaseFCB(pVCB
, FCB
);
901 return STATUS_OBJECT_PATH_NOT_FOUND
;
906 Length
= (curr
- prev
) * sizeof(WCHAR
);
907 if (Length
!= parentFCB
->LongNameU
.Length
)
909 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
911 vfatReleaseFCB(pVCB
, parentFCB
);
912 return STATUS_OBJECT_NAME_INVALID
;
914 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
915 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
916 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
917 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
918 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
920 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
924 while (*curr
!= L
'\\' && curr
<= last
)
928 NameU
.Buffer
= FileNameU
.Buffer
;
929 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
930 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
931 DPRINT("%wZ\n", &NameU
);
932 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
936 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
937 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
938 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
943 *pParentFCB
= parentFCB
;
944 return STATUS_OBJECT_NAME_NOT_FOUND
;
948 vfatReleaseFCB(pVCB
, parentFCB
);
950 return STATUS_OBJECT_PATH_NOT_FOUND
;
953 else if (!NT_SUCCESS(status
))
955 vfatReleaseFCB(pVCB
, parentFCB
);
964 *pParentFCB
= parentFCB
;
967 return STATUS_SUCCESS
;