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 /* Delete old name */
375 if (Fcb
->PathNameBuffer
)
377 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
380 /* Delete from table */
381 vfatDelFCBFromTable(pVCB
, Fcb
);
383 /* Get full path name */
384 Status
= vfatMakeFullName(ParentFcb
, LongName
, ShortName
, &Fcb
->PathNameU
);
385 if (!NT_SUCCESS(Status
))
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 accross directories, dereferenced our old parent
418 * We also derefence 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 Hash
= vfatNameHash(0, PathNameU
);
442 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
445 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
450 if (entry
->Hash
== Hash
)
453 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
454 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
456 if (rcFCB
->Hash
.Hash
== Hash
)
458 FcbNameU
= &rcFCB
->LongNameU
;
462 FcbNameU
= &rcFCB
->ShortNameU
;
464 /* compare the file name */
465 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
466 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
468 vfatGrabFCB(pVCB
, rcFCB
);
480 vfatFCBInitializeCacheFromVolume(
484 PFILE_OBJECT fileObject
;
488 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
490 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
493 ObDereferenceObject(fileObject
);
494 return STATUS_INSUFFICIENT_RESOURCES
;
496 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
498 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
499 fileObject
->FsContext
= fcb
;
500 fileObject
->FsContext2
= newCCB
;
501 fcb
->FileObject
= fileObject
;
505 CcInitializeCacheMap(fileObject
,
506 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
508 &VfatGlobalData
->CacheMgrCallbacks
,
511 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
513 status
= _SEH2_GetExceptionCode();
514 fcb
->FileObject
= NULL
;
515 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
516 ObDereferenceObject(fileObject
);
521 vfatGrabFCB(vcb
, fcb
);
522 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
523 return STATUS_SUCCESS
;
528 PDEVICE_EXTENSION pVCB
)
531 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
532 NTSTATUS Status
= STATUS_SUCCESS
;
533 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
535 FCB
= vfatNewFCB(pVCB
, &NameU
);
536 if (FCB
->Flags
& FCB_IS_FATX_ENTRY
)
538 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
539 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
540 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
541 FCB
->entry
.FatX
.FirstCluster
= 1;
542 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
546 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
547 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
548 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
549 if (pVCB
->FatInfo
.FatType
== FAT32
)
551 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
552 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
553 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
555 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
557 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
558 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
563 FCB
->entry
.Fat
.FirstCluster
= 1;
564 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
567 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
570 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
571 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
572 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
573 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
575 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
576 vfatAddFCBToTable(pVCB
, FCB
);
583 PDEVICE_EXTENSION pVCB
)
586 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
588 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
591 FCB
= vfatMakeRootFCB(pVCB
);
598 vfatMakeFCBFromDirEntry(
600 PVFATFCB directoryFCB
,
601 PVFAT_DIRENTRY_CONTEXT DirContext
,
606 UNICODE_STRING NameU
;
609 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
610 if (!NT_SUCCESS(Status
))
615 rcFCB
= vfatNewFCB(vcb
, &NameU
);
616 RtlCopyMemory(&rcFCB
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
617 RtlCopyUnicodeString(&rcFCB
->ShortNameU
, &DirContext
->ShortNameU
);
618 if (vcb
->Flags
& VCB_IS_FATX
)
620 rcFCB
->ShortHash
.Hash
= rcFCB
->Hash
.Hash
;
624 rcFCB
->ShortHash
.Hash
= vfatNameHash(0, &rcFCB
->DirNameU
);
625 rcFCB
->ShortHash
.Hash
= vfatNameHash(rcFCB
->ShortHash
.Hash
, &rcFCB
->ShortNameU
);
628 if (vfatFCBIsDirectory(rcFCB
))
630 ULONG FirstCluster
, CurrentCluster
;
631 NTSTATUS Status
= STATUS_SUCCESS
;
633 FirstCluster
= vfatDirEntryGetFirstCluster(vcb
, &rcFCB
->entry
);
634 if (FirstCluster
== 1)
636 Size
= vcb
->FatInfo
.rootDirectorySectors
* vcb
->FatInfo
.BytesPerSector
;
638 else if (FirstCluster
!= 0)
640 CurrentCluster
= FirstCluster
;
641 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
643 Size
+= vcb
->FatInfo
.BytesPerCluster
;
644 Status
= NextCluster(vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
648 else if (rcFCB
->Flags
& FCB_IS_FATX_ENTRY
)
650 Size
= rcFCB
->entry
.FatX
.FileSize
;
654 Size
= rcFCB
->entry
.Fat
.FileSize
;
656 rcFCB
->dirIndex
= DirContext
->DirIndex
;
657 rcFCB
->startIndex
= DirContext
->StartIndex
;
658 if ((rcFCB
->Flags
& FCB_IS_FATX_ENTRY
) && !vfatFCBIsRoot(directoryFCB
))
660 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
661 rcFCB
->dirIndex
= DirContext
->DirIndex
-2;
662 rcFCB
->startIndex
= DirContext
->StartIndex
-2;
664 rcFCB
->RFCB
.FileSize
.QuadPart
= Size
;
665 rcFCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
666 rcFCB
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP(Size
, vcb
->FatInfo
.BytesPerCluster
);
668 if (vfatFCBIsDirectory(rcFCB
))
670 vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
672 rcFCB
->parentFcb
= directoryFCB
;
673 vfatAddFCBToTable(vcb
, rcFCB
);
676 ExFreePool(NameU
.Buffer
);
677 return STATUS_SUCCESS
;
681 vfatAttachFCBToFileObject(
682 PDEVICE_EXTENSION vcb
,
684 PFILE_OBJECT fileObject
)
688 UNREFERENCED_PARAMETER(vcb
);
690 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
693 return STATUS_INSUFFICIENT_RESOURCES
;
695 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
697 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
698 fileObject
->FsContext
= fcb
;
699 fileObject
->FsContext2
= newCCB
;
700 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
702 return STATUS_SUCCESS
;
707 PDEVICE_EXTENSION pDeviceExt
,
708 PVFATFCB pDirectoryFCB
,
709 PUNICODE_STRING FileToFindU
,
713 PVOID Context
= NULL
;
715 BOOLEAN First
= TRUE
;
716 VFAT_DIRENTRY_CONTEXT DirContext
;
717 /* This buffer must have a size of 260 characters, because
718 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
719 WCHAR LongNameBuffer
[260];
720 WCHAR ShortNameBuffer
[13];
721 BOOLEAN FoundLong
= FALSE
;
722 BOOLEAN FoundShort
= FALSE
;
725 ASSERT(pDirectoryFCB
);
728 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
729 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
730 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
732 DirContext
.DirIndex
= 0;
733 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
734 DirContext
.LongNameU
.Length
= 0;
735 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
736 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
737 DirContext
.ShortNameU
.Length
= 0;
738 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
742 status
= pDeviceExt
->GetNextDirEntry(&Context
,
748 if (status
== STATUS_NO_MORE_ENTRIES
)
750 return STATUS_OBJECT_NAME_NOT_FOUND
;
752 if (!NT_SUCCESS(status
))
757 DPRINT(" Index:%u longName:%wZ\n",
758 DirContext
.DirIndex
, &DirContext
.LongNameU
);
760 if (!ENTRY_VOLUME(pDeviceExt
, &DirContext
.DirEntry
))
762 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
763 if (FoundLong
== FALSE
)
765 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
767 if (FoundLong
|| FoundShort
)
769 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
773 CcUnpinData(Context
);
777 DirContext
.DirIndex
++;
780 return STATUS_OBJECT_NAME_NOT_FOUND
;
785 PDEVICE_EXTENSION pVCB
,
786 PVFATFCB
*pParentFCB
,
788 PUNICODE_STRING pFileNameU
)
793 UNICODE_STRING NameU
;
794 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
795 UNICODE_STRING FileNameU
;
796 WCHAR NameBuffer
[260];
797 PWCHAR curr
, prev
, last
;
800 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
801 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
803 FileNameU
.Buffer
= NameBuffer
;
804 FileNameU
.MaximumLength
= sizeof(NameBuffer
);
805 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
807 parentFCB
= *pParentFCB
;
809 if (parentFCB
== NULL
)
811 // Trivial case, open of the root directory on volume
812 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
814 DPRINT("returning root FCB\n");
816 FCB
= vfatOpenRootFCB(pVCB
);
820 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
823 /* Check for an existing FCB */
824 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
828 *pParentFCB
= FCB
->parentFcb
;
829 vfatGrabFCB(pVCB
, *pParentFCB
);
830 return STATUS_SUCCESS
;
833 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
834 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
839 if (curr
> FileNameU
.Buffer
)
841 NameU
.Buffer
= FileNameU
.Buffer
;
842 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
843 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
846 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
847 if (Length
!= FCB
->PathNameU
.Length
)
849 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
851 vfatReleaseFCB(pVCB
, FCB
);
852 return STATUS_OBJECT_NAME_INVALID
;
854 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
855 curr
, FileNameU
.Length
- Length
);
856 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
857 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
858 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
860 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
870 FCB
= vfatOpenRootFCB(pVCB
);
871 curr
= FileNameU
.Buffer
;
881 prev
= curr
= FileNameU
.Buffer
- 1;
882 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
889 vfatReleaseFCB(pVCB
, parentFCB
);
892 // fail if element in FCB is not a directory
893 if (!vfatFCBIsDirectory(FCB
))
895 DPRINT ("Element in requested path is not a directory\n");
897 vfatReleaseFCB(pVCB
, FCB
);
902 return STATUS_OBJECT_PATH_NOT_FOUND
;
907 Length
= (curr
- prev
) * sizeof(WCHAR
);
908 if (Length
!= parentFCB
->LongNameU
.Length
)
910 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
912 vfatReleaseFCB(pVCB
, parentFCB
);
913 return STATUS_OBJECT_NAME_INVALID
;
915 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
916 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
917 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
918 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
919 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
921 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
925 while (*curr
!= L
'\\' && curr
<= last
)
929 NameU
.Buffer
= FileNameU
.Buffer
;
930 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
931 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
932 DPRINT("%wZ\n", &NameU
);
933 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
937 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
938 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
939 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
944 *pParentFCB
= parentFCB
;
945 return STATUS_OBJECT_NAME_NOT_FOUND
;
949 vfatReleaseFCB(pVCB
, parentFCB
);
951 return STATUS_OBJECT_PATH_NOT_FOUND
;
954 else if (!NT_SUCCESS(status
))
956 vfatReleaseFCB(pVCB
, parentFCB
);
965 *pParentFCB
= parentFCB
;
968 return STATUS_SUCCESS
;