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
));
302 PDEVICE_EXTENSION pVCB
,
307 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
308 pFCB
, &pFCB
->PathNameU
, pFCB
->RefCount
);
310 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
315 if (pFCB
->RefCount
== 0)
317 ASSERT(pFCB
->OpenHandleCount
== 0);
318 tmpFcb
= pFCB
->parentFcb
;
319 vfatDelFCBFromTable(pVCB
, pFCB
);
320 vfatDestroyFCB(pFCB
);
333 PDEVICE_EXTENSION pVCB
,
339 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
340 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
342 InsertTailList(&pVCB
->FcbListHead
, &pFCB
->FcbListEntry
);
344 pFCB
->Hash
.next
= pVCB
->FcbHashTable
[Index
];
345 pVCB
->FcbHashTable
[Index
] = &pFCB
->Hash
;
346 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
348 pFCB
->ShortHash
.next
= pVCB
->FcbHashTable
[ShortIndex
];
349 pVCB
->FcbHashTable
[ShortIndex
] = &pFCB
->ShortHash
;
353 vfatGrabFCB(pVCB
, pFCB
->parentFcb
);
359 PDEVICE_EXTENSION pVCB
,
361 PUNICODE_STRING LongName
,
362 PUNICODE_STRING ShortName
,
368 DPRINT("vfatUpdateFCB(%p, %p, %wZ, %wZ, %p)\n", pVCB
, Fcb
, LongName
, ShortName
, ParentFcb
);
370 /* Delete old name */
371 if (Fcb
->PathNameBuffer
)
373 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
376 /* Delete from table */
377 vfatDelFCBFromTable(pVCB
, Fcb
);
379 /* Get full path name */
380 Status
= vfatMakeFullName(ParentFcb
, LongName
, ShortName
, &Fcb
->PathNameU
);
381 if (!NT_SUCCESS(Status
))
386 /* Split it properly */
387 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
388 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
389 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
391 /* Copy short name */
392 RtlCopyUnicodeString(&Fcb
->ShortNameU
, ShortName
);
394 /* Recompute hashes */
395 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
396 if (pVCB
->Flags
& VCB_IS_FATX
)
398 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
402 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
403 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
407 OldParent
= Fcb
->parentFcb
;
408 Fcb
->parentFcb
= ParentFcb
;
410 /* Add to the table */
411 vfatAddFCBToTable(pVCB
, Fcb
);
413 /* If we moved accross directories, dereferenced our old parent
414 * We also derefence in case we're just renaming since AddFCBToTable references it
416 vfatReleaseFCB(pVCB
, OldParent
);
418 return STATUS_SUCCESS
;
422 vfatGrabFCBFromTable(
423 PDEVICE_EXTENSION pVCB
,
424 PUNICODE_STRING PathNameU
)
428 UNICODE_STRING DirNameU
;
429 UNICODE_STRING FileNameU
;
430 PUNICODE_STRING FcbNameU
;
434 DPRINT("'%wZ'\n", PathNameU
);
436 Hash
= vfatNameHash(0, PathNameU
);
438 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
441 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
446 if (entry
->Hash
== Hash
)
449 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
450 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
452 if (rcFCB
->Hash
.Hash
== Hash
)
454 FcbNameU
= &rcFCB
->LongNameU
;
458 FcbNameU
= &rcFCB
->ShortNameU
;
460 /* compare the file name */
461 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
462 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
464 vfatGrabFCB(pVCB
, rcFCB
);
476 vfatFCBInitializeCacheFromVolume(
480 PFILE_OBJECT fileObject
;
484 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
486 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
489 ObDereferenceObject(fileObject
);
490 return STATUS_INSUFFICIENT_RESOURCES
;
492 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
494 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
495 fileObject
->FsContext
= fcb
;
496 fileObject
->FsContext2
= newCCB
;
497 fcb
->FileObject
= fileObject
;
498 vfatGrabFCB(vcb
, fcb
);
502 CcInitializeCacheMap(fileObject
,
503 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
505 &VfatGlobalData
->CacheMgrCallbacks
,
508 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
510 status
= _SEH2_GetExceptionCode();
512 fcb
->FileObject
= NULL
;
513 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
514 ObDereferenceObject(fileObject
);
519 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
520 return STATUS_SUCCESS
;
525 PDEVICE_EXTENSION pVCB
)
528 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
529 NTSTATUS Status
= STATUS_SUCCESS
;
530 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
532 FCB
= vfatNewFCB(pVCB
, &NameU
);
533 if (FCB
->Flags
& FCB_IS_FATX_ENTRY
)
535 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
536 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
537 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
538 FCB
->entry
.FatX
.FirstCluster
= 1;
539 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
543 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
544 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
545 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
546 if (pVCB
->FatInfo
.FatType
== FAT32
)
548 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
549 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
550 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
552 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
554 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
555 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
560 FCB
->entry
.Fat
.FirstCluster
= 1;
561 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
564 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
567 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
568 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
569 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
570 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
572 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
573 vfatAddFCBToTable(pVCB
, FCB
);
580 PDEVICE_EXTENSION pVCB
)
583 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
585 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
588 FCB
= vfatMakeRootFCB(pVCB
);
595 vfatMakeFCBFromDirEntry(
597 PVFATFCB directoryFCB
,
598 PVFAT_DIRENTRY_CONTEXT DirContext
,
603 UNICODE_STRING NameU
;
606 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
607 if (!NT_SUCCESS(Status
))
612 rcFCB
= vfatNewFCB(vcb
, &NameU
);
613 RtlCopyMemory(&rcFCB
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
614 RtlCopyUnicodeString(&rcFCB
->ShortNameU
, &DirContext
->ShortNameU
);
615 if (vcb
->Flags
& VCB_IS_FATX
)
617 rcFCB
->ShortHash
.Hash
= rcFCB
->Hash
.Hash
;
621 rcFCB
->ShortHash
.Hash
= vfatNameHash(0, &rcFCB
->DirNameU
);
622 rcFCB
->ShortHash
.Hash
= vfatNameHash(rcFCB
->ShortHash
.Hash
, &rcFCB
->ShortNameU
);
625 if (vfatFCBIsDirectory(rcFCB
))
627 ULONG FirstCluster
, CurrentCluster
;
628 NTSTATUS Status
= STATUS_SUCCESS
;
630 FirstCluster
= vfatDirEntryGetFirstCluster(vcb
, &rcFCB
->entry
);
631 if (FirstCluster
== 1)
633 Size
= vcb
->FatInfo
.rootDirectorySectors
* vcb
->FatInfo
.BytesPerSector
;
635 else if (FirstCluster
!= 0)
637 CurrentCluster
= FirstCluster
;
638 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
640 Size
+= vcb
->FatInfo
.BytesPerCluster
;
641 Status
= NextCluster(vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
645 else if (rcFCB
->Flags
& FCB_IS_FATX_ENTRY
)
647 Size
= rcFCB
->entry
.FatX
.FileSize
;
651 Size
= rcFCB
->entry
.Fat
.FileSize
;
653 rcFCB
->dirIndex
= DirContext
->DirIndex
;
654 rcFCB
->startIndex
= DirContext
->StartIndex
;
655 if ((rcFCB
->Flags
& FCB_IS_FATX_ENTRY
) && !vfatFCBIsRoot(directoryFCB
))
657 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
658 rcFCB
->dirIndex
= DirContext
->DirIndex
-2;
659 rcFCB
->startIndex
= DirContext
->StartIndex
-2;
661 rcFCB
->RFCB
.FileSize
.QuadPart
= Size
;
662 rcFCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
663 rcFCB
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP(Size
, vcb
->FatInfo
.BytesPerCluster
);
664 vfatGrabFCB(vcb
, rcFCB
);
665 if (vfatFCBIsDirectory(rcFCB
))
667 vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
669 rcFCB
->parentFcb
= directoryFCB
;
670 vfatAddFCBToTable(vcb
, rcFCB
);
673 ExFreePool(NameU
.Buffer
);
674 return STATUS_SUCCESS
;
678 vfatAttachFCBToFileObject(
679 PDEVICE_EXTENSION vcb
,
681 PFILE_OBJECT fileObject
)
685 UNREFERENCED_PARAMETER(vcb
);
687 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
690 return STATUS_INSUFFICIENT_RESOURCES
;
692 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
694 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
695 fileObject
->FsContext
= fcb
;
696 fileObject
->FsContext2
= newCCB
;
697 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
699 return STATUS_SUCCESS
;
704 PDEVICE_EXTENSION pDeviceExt
,
705 PVFATFCB pDirectoryFCB
,
706 PUNICODE_STRING FileToFindU
,
710 PVOID Context
= NULL
;
712 BOOLEAN First
= TRUE
;
713 VFAT_DIRENTRY_CONTEXT DirContext
;
714 /* This buffer must have a size of 260 characters, because
715 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
716 WCHAR LongNameBuffer
[260];
717 WCHAR ShortNameBuffer
[13];
718 BOOLEAN FoundLong
= FALSE
;
719 BOOLEAN FoundShort
= FALSE
;
722 ASSERT(pDirectoryFCB
);
725 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
726 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
727 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
729 DirContext
.DirIndex
= 0;
730 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
731 DirContext
.LongNameU
.Length
= 0;
732 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
733 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
734 DirContext
.ShortNameU
.Length
= 0;
735 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
739 status
= pDeviceExt
->GetNextDirEntry(&Context
,
745 if (status
== STATUS_NO_MORE_ENTRIES
)
747 return STATUS_OBJECT_NAME_NOT_FOUND
;
749 if (!NT_SUCCESS(status
))
754 DPRINT(" Index:%u longName:%wZ\n",
755 DirContext
.DirIndex
, &DirContext
.LongNameU
);
757 if (!ENTRY_VOLUME(pDeviceExt
, &DirContext
.DirEntry
))
759 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
760 if (FoundLong
== FALSE
)
762 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
764 if (FoundLong
|| FoundShort
)
766 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
770 CcUnpinData(Context
);
774 DirContext
.DirIndex
++;
777 return STATUS_OBJECT_NAME_NOT_FOUND
;
782 PDEVICE_EXTENSION pVCB
,
783 PVFATFCB
*pParentFCB
,
785 PUNICODE_STRING pFileNameU
)
790 UNICODE_STRING NameU
;
791 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
792 UNICODE_STRING FileNameU
;
793 WCHAR NameBuffer
[260];
794 PWCHAR curr
, prev
, last
;
797 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
798 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
800 FileNameU
.Buffer
= NameBuffer
;
801 FileNameU
.MaximumLength
= sizeof(NameBuffer
);
802 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
804 parentFCB
= *pParentFCB
;
806 if (parentFCB
== NULL
)
808 // Trivial case, open of the root directory on volume
809 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
811 DPRINT("returning root FCB\n");
813 FCB
= vfatOpenRootFCB(pVCB
);
817 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
820 /* Check for an existing FCB */
821 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
825 *pParentFCB
= FCB
->parentFcb
;
826 vfatGrabFCB(pVCB
, *pParentFCB
);
827 return STATUS_SUCCESS
;
830 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
831 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
836 if (curr
> FileNameU
.Buffer
)
838 NameU
.Buffer
= FileNameU
.Buffer
;
839 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
840 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
843 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
844 if (Length
!= FCB
->PathNameU
.Length
)
846 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
848 vfatReleaseFCB(pVCB
, FCB
);
849 return STATUS_OBJECT_NAME_INVALID
;
851 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
852 curr
, FileNameU
.Length
- Length
);
853 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
854 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
855 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
857 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
867 FCB
= vfatOpenRootFCB(pVCB
);
868 curr
= FileNameU
.Buffer
;
878 prev
= curr
= FileNameU
.Buffer
- 1;
879 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
886 vfatReleaseFCB(pVCB
, parentFCB
);
889 // fail if element in FCB is not a directory
890 if (!vfatFCBIsDirectory(FCB
))
892 DPRINT ("Element in requested path is not a directory\n");
894 vfatReleaseFCB(pVCB
, FCB
);
899 return STATUS_OBJECT_PATH_NOT_FOUND
;
904 Length
= (curr
- prev
) * sizeof(WCHAR
);
905 if (Length
!= parentFCB
->LongNameU
.Length
)
907 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
909 vfatReleaseFCB(pVCB
, parentFCB
);
910 return STATUS_OBJECT_NAME_INVALID
;
912 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
913 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
914 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
915 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
916 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
918 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
922 while (*curr
!= L
'\\' && curr
<= last
)
926 NameU
.Buffer
= FileNameU
.Buffer
;
927 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
928 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
929 DPRINT("%wZ\n", &NameU
);
930 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
934 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
935 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
936 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
941 *pParentFCB
= parentFCB
;
942 return STATUS_OBJECT_NAME_NOT_FOUND
;
946 vfatReleaseFCB(pVCB
, parentFCB
);
948 return STATUS_OBJECT_PATH_NOT_FOUND
;
951 else if (!NT_SUCCESS(status
))
953 vfatReleaseFCB(pVCB
, parentFCB
);
962 *pParentFCB
= parentFCB
;
965 return STATUS_SUCCESS
;