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 PVFAT_DIRENTRY_CONTEXT DirContext
,
371 DPRINT("vfatUpdateFCB(%p, %p, %p, %p)\n", pVCB
, Fcb
, DirContext
, ParentFcb
);
373 /* Get full path name */
374 Status
= vfatMakeFullName(ParentFcb
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &Fcb
->PathNameU
);
375 if (!NT_SUCCESS(Status
))
380 /* Delete old name */
381 if (Fcb
->PathNameBuffer
)
383 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
386 /* Delete from table */
387 vfatDelFCBFromTable(pVCB
, Fcb
);
389 /* Split it properly */
390 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
391 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
392 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
394 /* Copy short name */
395 RtlCopyUnicodeString(&Fcb
->ShortNameU
, &DirContext
->ShortNameU
);
397 /* Recompute hashes */
398 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
399 if (pVCB
->Flags
& VCB_IS_FATX
)
401 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
403 RtlCopyMemory(Fcb
->entry
.FatX
.Filename
, DirContext
->DirEntry
.FatX
.Filename
, 42);
404 Fcb
->entry
.FatX
.FilenameLength
= DirContext
->DirEntry
.FatX
.FilenameLength
;
408 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
409 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
411 RtlCopyMemory(Fcb
->entry
.Fat
.ShortName
, DirContext
->DirEntry
.Fat
.ShortName
, 11);
415 OldParent
= Fcb
->parentFcb
;
416 Fcb
->parentFcb
= ParentFcb
;
418 /* Add to the table */
419 vfatAddFCBToTable(pVCB
, Fcb
);
421 /* If we moved across directories, dereference our old parent
422 * We also dereference in case we're just renaming since AddFCBToTable references it
424 vfatReleaseFCB(pVCB
, OldParent
);
426 return STATUS_SUCCESS
;
430 vfatGrabFCBFromTable(
431 PDEVICE_EXTENSION pVCB
,
432 PUNICODE_STRING PathNameU
)
436 UNICODE_STRING DirNameU
;
437 UNICODE_STRING FileNameU
;
438 PUNICODE_STRING FcbNameU
;
442 DPRINT("'%wZ'\n", PathNameU
);
444 ASSERT(PathNameU
->Length
>= sizeof(WCHAR
) && PathNameU
->Buffer
[0] == L
'\\');
445 Hash
= vfatNameHash(0, PathNameU
);
447 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
450 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
455 if (entry
->Hash
== Hash
)
458 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
459 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
461 if (rcFCB
->Hash
.Hash
== Hash
)
463 FcbNameU
= &rcFCB
->LongNameU
;
467 FcbNameU
= &rcFCB
->ShortNameU
;
469 /* compare the file name */
470 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
471 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
473 vfatGrabFCB(pVCB
, rcFCB
);
485 vfatFCBInitializeCacheFromVolume(
489 PFILE_OBJECT fileObject
;
493 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
495 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
498 ObDereferenceObject(fileObject
);
499 return STATUS_INSUFFICIENT_RESOURCES
;
501 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
503 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
504 fileObject
->FsContext
= fcb
;
505 fileObject
->FsContext2
= newCCB
;
506 fcb
->FileObject
= fileObject
;
510 CcInitializeCacheMap(fileObject
,
511 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
513 &VfatGlobalData
->CacheMgrCallbacks
,
516 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
518 status
= _SEH2_GetExceptionCode();
519 fcb
->FileObject
= NULL
;
520 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
521 ObDereferenceObject(fileObject
);
526 vfatGrabFCB(vcb
, fcb
);
527 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
528 return STATUS_SUCCESS
;
533 PDEVICE_EXTENSION pVCB
)
536 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
537 NTSTATUS Status
= STATUS_SUCCESS
;
538 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
540 FCB
= vfatNewFCB(pVCB
, &NameU
);
541 if (FCB
->Flags
& FCB_IS_FATX_ENTRY
)
543 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
544 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
545 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
546 FCB
->entry
.FatX
.FirstCluster
= 1;
547 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
551 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
552 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
553 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
554 if (pVCB
->FatInfo
.FatType
== FAT32
)
556 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
557 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
558 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
560 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
562 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
563 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
568 FCB
->entry
.Fat
.FirstCluster
= 1;
569 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
572 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
575 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
576 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
577 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
578 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
580 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
581 vfatAddFCBToTable(pVCB
, FCB
);
588 PDEVICE_EXTENSION pVCB
)
591 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
593 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
596 FCB
= vfatMakeRootFCB(pVCB
);
603 vfatMakeFCBFromDirEntry(
605 PVFATFCB directoryFCB
,
606 PVFAT_DIRENTRY_CONTEXT DirContext
,
611 UNICODE_STRING NameU
;
614 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
615 if (!NT_SUCCESS(Status
))
620 rcFCB
= vfatNewFCB(vcb
, &NameU
);
621 RtlCopyMemory(&rcFCB
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
622 RtlCopyUnicodeString(&rcFCB
->ShortNameU
, &DirContext
->ShortNameU
);
623 if (vcb
->Flags
& VCB_IS_FATX
)
625 rcFCB
->ShortHash
.Hash
= rcFCB
->Hash
.Hash
;
629 rcFCB
->ShortHash
.Hash
= vfatNameHash(0, &rcFCB
->DirNameU
);
630 rcFCB
->ShortHash
.Hash
= vfatNameHash(rcFCB
->ShortHash
.Hash
, &rcFCB
->ShortNameU
);
633 if (vfatFCBIsDirectory(rcFCB
))
635 ULONG FirstCluster
, CurrentCluster
;
636 NTSTATUS Status
= STATUS_SUCCESS
;
638 FirstCluster
= vfatDirEntryGetFirstCluster(vcb
, &rcFCB
->entry
);
639 if (FirstCluster
== 1)
641 Size
= vcb
->FatInfo
.rootDirectorySectors
* vcb
->FatInfo
.BytesPerSector
;
643 else if (FirstCluster
!= 0)
645 CurrentCluster
= FirstCluster
;
646 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
648 Size
+= vcb
->FatInfo
.BytesPerCluster
;
649 Status
= NextCluster(vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
653 else if (rcFCB
->Flags
& FCB_IS_FATX_ENTRY
)
655 Size
= rcFCB
->entry
.FatX
.FileSize
;
659 Size
= rcFCB
->entry
.Fat
.FileSize
;
661 rcFCB
->dirIndex
= DirContext
->DirIndex
;
662 rcFCB
->startIndex
= DirContext
->StartIndex
;
663 if ((rcFCB
->Flags
& FCB_IS_FATX_ENTRY
) && !vfatFCBIsRoot(directoryFCB
))
665 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
666 rcFCB
->dirIndex
= DirContext
->DirIndex
-2;
667 rcFCB
->startIndex
= DirContext
->StartIndex
-2;
669 rcFCB
->RFCB
.FileSize
.QuadPart
= Size
;
670 rcFCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
671 rcFCB
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP_64(Size
, vcb
->FatInfo
.BytesPerCluster
);
673 if (vfatFCBIsDirectory(rcFCB
))
675 vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
677 rcFCB
->parentFcb
= directoryFCB
;
678 vfatAddFCBToTable(vcb
, rcFCB
);
681 ExFreePool(NameU
.Buffer
);
682 return STATUS_SUCCESS
;
686 vfatAttachFCBToFileObject(
687 PDEVICE_EXTENSION vcb
,
689 PFILE_OBJECT fileObject
)
693 UNREFERENCED_PARAMETER(vcb
);
695 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
698 return STATUS_INSUFFICIENT_RESOURCES
;
700 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
702 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
703 fileObject
->FsContext
= fcb
;
704 fileObject
->FsContext2
= newCCB
;
705 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
707 return STATUS_SUCCESS
;
712 PDEVICE_EXTENSION pDeviceExt
,
713 PVFATFCB pDirectoryFCB
,
714 PUNICODE_STRING FileToFindU
,
718 PVOID Context
= NULL
;
720 BOOLEAN First
= TRUE
;
721 VFAT_DIRENTRY_CONTEXT DirContext
;
722 /* This buffer must have a size of 260 characters, because
723 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
724 WCHAR LongNameBuffer
[260];
725 WCHAR ShortNameBuffer
[13];
726 BOOLEAN FoundLong
= FALSE
;
727 BOOLEAN FoundShort
= FALSE
;
730 ASSERT(pDirectoryFCB
);
733 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
734 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
735 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
737 DirContext
.DirIndex
= 0;
738 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
739 DirContext
.LongNameU
.Length
= 0;
740 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
741 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
742 DirContext
.ShortNameU
.Length
= 0;
743 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
747 status
= pDeviceExt
->GetNextDirEntry(&Context
,
753 if (status
== STATUS_NO_MORE_ENTRIES
)
755 return STATUS_OBJECT_NAME_NOT_FOUND
;
757 if (!NT_SUCCESS(status
))
762 DPRINT(" Index:%u longName:%wZ\n",
763 DirContext
.DirIndex
, &DirContext
.LongNameU
);
765 if (!ENTRY_VOLUME(pDeviceExt
, &DirContext
.DirEntry
))
767 if (DirContext
.LongNameU
.Length
== 0 ||
768 DirContext
.ShortNameU
.Length
== 0)
770 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
771 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
773 ASSERT(DirContext
.LongNameU
.Length
!= 0 &&
774 DirContext
.ShortNameU
.Length
!= 0);
776 DirContext
.DirIndex
++;
779 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
780 if (FoundLong
== FALSE
)
782 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
784 if (FoundLong
|| FoundShort
)
786 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
790 CcUnpinData(Context
);
794 DirContext
.DirIndex
++;
797 return STATUS_OBJECT_NAME_NOT_FOUND
;
802 PDEVICE_EXTENSION pVCB
,
803 PVFATFCB
*pParentFCB
,
805 PUNICODE_STRING pFileNameU
)
810 UNICODE_STRING NameU
;
811 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
812 UNICODE_STRING FileNameU
;
813 WCHAR NameBuffer
[260];
814 PWCHAR curr
, prev
, last
;
817 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
818 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
820 RtlInitEmptyUnicodeString(&FileNameU
, NameBuffer
, sizeof(NameBuffer
));
822 parentFCB
= *pParentFCB
;
824 if (parentFCB
== NULL
)
826 /* Passed-in name is the full name */
827 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
829 // Trivial case, open of the root directory on volume
830 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
832 DPRINT("returning root FCB\n");
834 FCB
= vfatOpenRootFCB(pVCB
);
838 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
841 /* Check for an existing FCB */
842 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
846 *pParentFCB
= FCB
->parentFcb
;
847 vfatGrabFCB(pVCB
, *pParentFCB
);
848 return STATUS_SUCCESS
;
851 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
852 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
857 if (curr
> FileNameU
.Buffer
)
859 NameU
.Buffer
= FileNameU
.Buffer
;
860 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
861 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
864 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
865 if (Length
!= FCB
->PathNameU
.Length
)
867 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
869 vfatReleaseFCB(pVCB
, FCB
);
870 return STATUS_OBJECT_NAME_INVALID
;
872 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
873 curr
, FileNameU
.Length
- Length
);
874 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
875 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
876 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
878 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
888 FCB
= vfatOpenRootFCB(pVCB
);
889 curr
= FileNameU
.Buffer
;
897 /* Make absolute path */
898 RtlCopyUnicodeString(&FileNameU
, &parentFCB
->PathNameU
);
899 curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
902 RtlAppendUnicodeToString(&FileNameU
, L
"\\");
905 ASSERT(*curr
== L
'\\');
906 RtlAppendUnicodeStringToString(&FileNameU
, pFileNameU
);
911 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
918 vfatReleaseFCB(pVCB
, parentFCB
);
921 // fail if element in FCB is not a directory
922 if (!vfatFCBIsDirectory(FCB
))
924 DPRINT ("Element in requested path is not a directory\n");
926 vfatReleaseFCB(pVCB
, FCB
);
931 return STATUS_OBJECT_PATH_NOT_FOUND
;
936 Length
= (curr
- prev
) * sizeof(WCHAR
);
937 if (Length
!= parentFCB
->LongNameU
.Length
)
939 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
941 vfatReleaseFCB(pVCB
, parentFCB
);
944 return STATUS_OBJECT_NAME_INVALID
;
946 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
947 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
948 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
949 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
950 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
952 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
956 while (*curr
!= L
'\\' && curr
<= last
)
960 NameU
.Buffer
= FileNameU
.Buffer
;
961 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
962 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
963 DPRINT("%wZ\n", &NameU
);
964 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
968 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
969 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
970 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
975 *pParentFCB
= parentFCB
;
976 return STATUS_OBJECT_NAME_NOT_FOUND
;
980 vfatReleaseFCB(pVCB
, parentFCB
);
982 return STATUS_OBJECT_PATH_NOT_FOUND
;
985 else if (!NT_SUCCESS(status
))
987 vfatReleaseFCB(pVCB
, parentFCB
);
996 *pParentFCB
= parentFCB
;
999 return STATUS_SUCCESS
;