Synchronize with trunk r58457.
[reactos.git] / drivers / filesystems / fastfat / fcb.c
1 /*
2 * FILE: drivers/fs/vfat/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 */
10
11 /* ------------------------------------------------------- INCLUDES */
12
13 #ifdef __GNUC__
14 #include <wctype.h> /* towlower prototype */
15 #endif
16
17 #define NDEBUG
18 #include "vfat.h"
19
20 /* -------------------------------------------------------- DEFINES */
21
22 #define TAG_FCB 'BCFV'
23
24 /* -------------------------------------------------------- PUBLICS */
25
26 static ULONG vfatNameHash(ULONG hash, PUNICODE_STRING NameU)
27 {
28 PWCHAR last;
29 PWCHAR curr;
30 register WCHAR c;
31
32 // LFN could start from "."
33 //ASSERT(NameU->Buffer[0] != L'.');
34 curr = NameU->Buffer;
35 last = NameU->Buffer + NameU->Length / sizeof(WCHAR);
36
37 while(curr < last)
38 {
39 c = towlower(*curr++);
40 hash = (hash + (c << 4) + (c >> 4)) * 11;
41 }
42 return hash;
43 }
44
45 VOID
46 vfatSplitPathName(PUNICODE_STRING PathNameU, PUNICODE_STRING DirNameU, PUNICODE_STRING FileNameU)
47 {
48 PWCHAR pName;
49 USHORT Length = 0;
50 pName = PathNameU->Buffer + PathNameU->Length / sizeof(WCHAR) - 1;
51 while (*pName != L'\\' && pName >= PathNameU->Buffer)
52 {
53 pName--;
54 Length++;
55 }
56 ASSERT(*pName == L'\\' || pName < PathNameU->Buffer);
57 if (FileNameU)
58 {
59 FileNameU->Buffer = pName + 1;
60 FileNameU->Length = FileNameU->MaximumLength = Length * sizeof(WCHAR);
61 }
62 if (DirNameU)
63 {
64 DirNameU->Buffer = PathNameU->Buffer;
65 DirNameU->Length = (pName + 1 - PathNameU->Buffer) * sizeof(WCHAR);
66 DirNameU->MaximumLength = DirNameU->Length;
67 }
68 }
69
70 static VOID
71 vfatInitFcb(PVFATFCB Fcb, PUNICODE_STRING NameU)
72 {
73 USHORT PathNameBufferLength;
74
75 if (NameU)
76 PathNameBufferLength = NameU->Length + sizeof(WCHAR);
77 else
78 PathNameBufferLength = 0;
79
80 Fcb->PathNameBuffer = ExAllocatePoolWithTag(NonPagedPool, PathNameBufferLength, TAG_FCB);
81 if (!Fcb->PathNameBuffer)
82 {
83 /* FIXME: what to do if no more memory? */
84 DPRINT1("Unable to initialize FCB for filename '%wZ'\n", NameU);
85 KeBugCheckEx(FAT_FILE_SYSTEM, (ULONG_PTR)Fcb, (ULONG_PTR)NameU, 0, 0);
86 }
87
88 Fcb->PathNameU.Length = 0;
89 Fcb->PathNameU.Buffer = Fcb->PathNameBuffer;
90 Fcb->PathNameU.MaximumLength = PathNameBufferLength;
91 Fcb->ShortNameU.Length = 0;
92 Fcb->ShortNameU.Buffer = Fcb->ShortNameBuffer;
93 Fcb->ShortNameU.MaximumLength = sizeof(Fcb->ShortNameBuffer);
94 Fcb->DirNameU.Buffer = Fcb->PathNameU.Buffer;
95 if (NameU && NameU->Length)
96 {
97 RtlCopyUnicodeString(&Fcb->PathNameU, NameU);
98 vfatSplitPathName(&Fcb->PathNameU, &Fcb->DirNameU, &Fcb->LongNameU);
99 }
100 else
101 {
102 Fcb->DirNameU.Buffer = Fcb->LongNameU.Buffer = NULL;
103 Fcb->DirNameU.MaximumLength = Fcb->DirNameU.Length = 0;
104 Fcb->LongNameU.MaximumLength = Fcb->LongNameU.Length = 0;
105 }
106 RtlZeroMemory(&Fcb->FCBShareAccess, sizeof(SHARE_ACCESS));
107 Fcb->OpenHandleCount = 0;
108 }
109
110 PVFATFCB
111 vfatNewFCB(PDEVICE_EXTENSION pVCB, PUNICODE_STRING pFileNameU)
112 {
113 PVFATFCB rcFCB;
114
115 DPRINT("'%wZ'\n", pFileNameU);
116
117 rcFCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->FcbLookasideList);
118 if (rcFCB == NULL)
119 {
120 return NULL;
121 }
122 RtlZeroMemory(rcFCB, sizeof(VFATFCB));
123 vfatInitFcb(rcFCB, pFileNameU);
124 if (pVCB->Flags & VCB_IS_FATX)
125 {
126 rcFCB->Flags |= FCB_IS_FATX_ENTRY;
127 rcFCB->Attributes = &rcFCB->entry.FatX.Attrib;
128 }
129 else
130 rcFCB->Attributes = &rcFCB->entry.Fat.Attrib;
131 rcFCB->Hash.Hash = vfatNameHash(0, &rcFCB->PathNameU);
132 rcFCB->Hash.self = rcFCB;
133 rcFCB->ShortHash.self = rcFCB;
134 ExInitializeResourceLite(&rcFCB->PagingIoResource);
135 ExInitializeResourceLite(&rcFCB->MainResource);
136 FsRtlInitializeFileLock(&rcFCB->FileLock, NULL, NULL);
137 ExInitializeFastMutex(&rcFCB->LastMutex);
138 rcFCB->RFCB.PagingIoResource = &rcFCB->PagingIoResource;
139 rcFCB->RFCB.Resource = &rcFCB->MainResource;
140 rcFCB->RFCB.IsFastIoPossible = FastIoIsNotPossible;
141
142 return rcFCB;
143 }
144
145 VOID
146 vfatDestroyCCB(PVFATCCB pCcb)
147 {
148 if (pCcb->SearchPattern.Buffer)
149 {
150 ExFreePoolWithTag(pCcb->SearchPattern.Buffer, TAG_VFAT);
151 }
152 ExFreeToNPagedLookasideList(&VfatGlobalData->CcbLookasideList, pCcb);
153 }
154
155 VOID
156 vfatDestroyFCB(PVFATFCB pFCB)
157 {
158 FsRtlUninitializeFileLock(&pFCB->FileLock);
159 ExFreePool(pFCB->PathNameBuffer);
160 ExDeleteResourceLite(&pFCB->PagingIoResource);
161 ExDeleteResourceLite(&pFCB->MainResource);
162 ExFreeToNPagedLookasideList(&VfatGlobalData->FcbLookasideList, pFCB);
163 }
164
165 BOOLEAN
166 vfatFCBIsDirectory(PVFATFCB FCB)
167 {
168 return *FCB->Attributes & FILE_ATTRIBUTE_DIRECTORY;
169 }
170
171 BOOLEAN
172 vfatFCBIsRoot(PVFATFCB FCB)
173 {
174 return FCB->PathNameU.Length == sizeof(WCHAR) && FCB->PathNameU.Buffer[0] == L'\\' ? TRUE : FALSE;
175 }
176
177 VOID
178 vfatReleaseFCB(PDEVICE_EXTENSION pVCB, PVFATFCB pFCB)
179 {
180 HASHENTRY* entry;
181 ULONG Index;
182 ULONG ShortIndex;
183 PVFATFCB tmpFcb;
184
185 DPRINT ("releasing FCB at %p: %wZ, refCount:%d\n",
186 pFCB,
187 &pFCB->PathNameU,
188 pFCB->RefCount);
189
190 while (pFCB)
191 {
192 Index = pFCB->Hash.Hash % pVCB->HashTableSize;
193 ShortIndex = pFCB->ShortHash.Hash % pVCB->HashTableSize;
194 pFCB->RefCount--;
195 if (pFCB->RefCount == 0)
196 {
197 tmpFcb = pFCB->parentFcb;
198 RemoveEntryList (&pFCB->FcbListEntry);
199 if (pFCB->Hash.Hash != pFCB->ShortHash.Hash)
200 {
201 entry = pVCB->FcbHashTable[ShortIndex];
202 if (entry->self == pFCB)
203 {
204 pVCB->FcbHashTable[ShortIndex] = entry->next;
205 }
206 else
207 {
208 while (entry->next->self != pFCB)
209 {
210 entry = entry->next;
211 }
212 entry->next = pFCB->ShortHash.next;
213 }
214 }
215 entry = pVCB->FcbHashTable[Index];
216 if (entry->self == pFCB)
217 {
218 pVCB->FcbHashTable[Index] = entry->next;
219 }
220 else
221 {
222 while (entry->next->self != pFCB)
223 {
224 entry = entry->next;
225 }
226 entry->next = pFCB->Hash.next;
227 }
228 vfatDestroyFCB (pFCB);
229 }
230 else
231 {
232 tmpFcb = NULL;
233 }
234 pFCB = tmpFcb;
235 }
236 }
237
238 VOID
239 vfatAddFCBToTable(PDEVICE_EXTENSION pVCB, PVFATFCB pFCB)
240 {
241 ULONG Index;
242 ULONG ShortIndex;
243
244 Index = pFCB->Hash.Hash % pVCB->HashTableSize;
245 ShortIndex = pFCB->ShortHash.Hash % pVCB->HashTableSize;
246
247 InsertTailList (&pVCB->FcbListHead, &pFCB->FcbListEntry);
248
249 pFCB->Hash.next = pVCB->FcbHashTable[Index];
250 pVCB->FcbHashTable[Index] = &pFCB->Hash;
251 if (pFCB->Hash.Hash != pFCB->ShortHash.Hash)
252 {
253 pFCB->ShortHash.next = pVCB->FcbHashTable[ShortIndex];
254 pVCB->FcbHashTable[ShortIndex] = &pFCB->ShortHash;
255 }
256 if (pFCB->parentFcb)
257 {
258 pFCB->parentFcb->RefCount++;
259 }
260 }
261
262 PVFATFCB
263 vfatGrabFCBFromTable(PDEVICE_EXTENSION pVCB, PUNICODE_STRING PathNameU)
264 {
265 PVFATFCB rcFCB;
266 ULONG Hash;
267 UNICODE_STRING DirNameU;
268 UNICODE_STRING FileNameU;
269 PUNICODE_STRING FcbNameU;
270
271 HASHENTRY* entry;
272
273 DPRINT("'%wZ'\n", PathNameU);
274
275 Hash = vfatNameHash(0, PathNameU);
276
277 entry = pVCB->FcbHashTable[Hash % pVCB->HashTableSize];
278 if (entry)
279 {
280 vfatSplitPathName(PathNameU, &DirNameU, &FileNameU);
281 }
282
283 while (entry)
284 {
285 if (entry->Hash == Hash)
286 {
287 rcFCB = entry->self;
288 DPRINT("'%wZ' '%wZ'\n", &DirNameU, &rcFCB->DirNameU);
289 if (RtlEqualUnicodeString(&DirNameU, &rcFCB->DirNameU, TRUE))
290 {
291 if (rcFCB->Hash.Hash == Hash)
292 {
293 FcbNameU = &rcFCB->LongNameU;
294 }
295 else
296 {
297 FcbNameU = &rcFCB->ShortNameU;
298 }
299 /* compare the file name */
300 DPRINT("'%wZ' '%wZ'\n", &FileNameU, FcbNameU);
301 if (RtlEqualUnicodeString(&FileNameU, FcbNameU, TRUE))
302 {
303 rcFCB->RefCount++;
304 return rcFCB;
305 }
306 }
307 }
308 entry = entry->next;
309 }
310 return NULL;
311 }
312
313 static NTSTATUS
314 vfatFCBInitializeCacheFromVolume (PVCB vcb, PVFATFCB fcb)
315 {
316 PFILE_OBJECT fileObject;
317 PVFATCCB newCCB;
318
319 fileObject = IoCreateStreamFileObject (NULL, vcb->StorageDevice);
320
321 newCCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
322 if (newCCB == NULL)
323 {
324 return STATUS_INSUFFICIENT_RESOURCES;
325 }
326 RtlZeroMemory(newCCB, sizeof (VFATCCB));
327
328 fileObject->SectionObjectPointer = &fcb->SectionObjectPointers;
329 fileObject->FsContext = fcb;
330 fileObject->FsContext2 = newCCB;
331 fcb->FileObject = fileObject;
332 fcb->RefCount++;
333
334 CcInitializeCacheMap(fileObject,
335 (PCC_FILE_SIZES)(&fcb->RFCB.AllocationSize),
336 TRUE,
337 &VfatGlobalData->CacheMgrCallbacks,
338 fcb);
339
340 fcb->Flags |= FCB_CACHE_INITIALIZED;
341 return STATUS_SUCCESS;
342 }
343
344 PVFATFCB
345 vfatMakeRootFCB(PDEVICE_EXTENSION pVCB)
346 {
347 PVFATFCB FCB;
348 ULONG FirstCluster, CurrentCluster, Size = 0;
349 NTSTATUS Status = STATUS_SUCCESS;
350 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\");
351
352 FCB = vfatNewFCB(pVCB, &NameU);
353 if (FCB->Flags & FCB_IS_FATX_ENTRY)
354 {
355 memset(FCB->entry.FatX.Filename, ' ', 42);
356 FCB->entry.FatX.FileSize = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
357 FCB->entry.FatX.Attrib = FILE_ATTRIBUTE_DIRECTORY;
358 FCB->entry.FatX.FirstCluster = 1;
359 Size = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
360 }
361 else
362 {
363 memset(FCB->entry.Fat.ShortName, ' ', 11);
364 FCB->entry.Fat.FileSize = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
365 FCB->entry.Fat.Attrib = FILE_ATTRIBUTE_DIRECTORY;
366 if (pVCB->FatInfo.FatType == FAT32)
367 {
368 CurrentCluster = FirstCluster = pVCB->FatInfo.RootCluster;
369 FCB->entry.Fat.FirstCluster = (unsigned short)(FirstCluster & 0xffff);
370 FCB->entry.Fat.FirstClusterHigh = (unsigned short)(FirstCluster >> 16);
371
372 while (CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
373 {
374 Size += pVCB->FatInfo.BytesPerCluster;
375 Status = NextCluster (pVCB, FirstCluster, &CurrentCluster, FALSE);
376 }
377 }
378 else
379 {
380 FCB->entry.Fat.FirstCluster = 1;
381 Size = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
382 }
383 }
384 FCB->ShortHash.Hash = FCB->Hash.Hash;
385 FCB->RefCount = 2;
386 FCB->dirIndex = 0;
387 FCB->RFCB.FileSize.QuadPart = Size;
388 FCB->RFCB.ValidDataLength.QuadPart = Size;
389 FCB->RFCB.AllocationSize.QuadPart = Size;
390 FCB->RFCB.IsFastIoPossible = FastIoIsNotPossible;
391
392 vfatFCBInitializeCacheFromVolume(pVCB, FCB);
393 vfatAddFCBToTable(pVCB, FCB);
394
395 return(FCB);
396 }
397
398 PVFATFCB
399 vfatOpenRootFCB(PDEVICE_EXTENSION pVCB)
400 {
401 PVFATFCB FCB;
402 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\");
403
404 FCB = vfatGrabFCBFromTable (pVCB, &NameU);
405 if (FCB == NULL)
406 {
407 FCB = vfatMakeRootFCB (pVCB);
408 }
409
410 return FCB;
411 }
412
413 NTSTATUS
414 vfatMakeFCBFromDirEntry(
415 PVCB vcb,
416 PVFATFCB directoryFCB,
417 PVFAT_DIRENTRY_CONTEXT DirContext,
418 PVFATFCB* fileFCB)
419 {
420 PVFATFCB rcFCB;
421 PWCHAR PathNameBuffer;
422 USHORT PathNameLength;
423 ULONG Size;
424 ULONG hash;
425
426 UNICODE_STRING NameU;
427
428 PathNameLength = directoryFCB->PathNameU.Length + max(DirContext->LongNameU.Length, DirContext->ShortNameU.Length);
429 if (!vfatFCBIsRoot (directoryFCB))
430 {
431 PathNameLength += sizeof(WCHAR);
432 }
433
434 if (PathNameLength > LONGNAME_MAX_LENGTH * sizeof(WCHAR))
435 {
436 return STATUS_OBJECT_NAME_INVALID;
437 }
438 PathNameBuffer = ExAllocatePoolWithTag(NonPagedPool, PathNameLength + sizeof(WCHAR), TAG_FCB);
439 if (!PathNameBuffer)
440 {
441 return STATUS_INSUFFICIENT_RESOURCES;
442 }
443 NameU.Buffer = PathNameBuffer;
444 NameU.Length = 0;
445 NameU.MaximumLength = PathNameLength;
446
447 RtlCopyUnicodeString(&NameU, &directoryFCB->PathNameU);
448 if (!vfatFCBIsRoot (directoryFCB))
449 {
450 RtlAppendUnicodeToString(&NameU, L"\\");
451 }
452 hash = vfatNameHash(0, &NameU);
453 if (DirContext->LongNameU.Length > 0)
454 {
455 RtlAppendUnicodeStringToString(&NameU, &DirContext->LongNameU);
456 }
457 else
458 {
459 RtlAppendUnicodeStringToString(&NameU, &DirContext->ShortNameU);
460 }
461 NameU.Buffer[NameU.Length / sizeof(WCHAR)] = 0;
462
463 rcFCB = vfatNewFCB (vcb, &NameU);
464 RtlCopyMemory (&rcFCB->entry, &DirContext->DirEntry, sizeof (DIR_ENTRY));
465 RtlCopyUnicodeString(&rcFCB->ShortNameU, &DirContext->ShortNameU);
466 if (vcb->Flags & VCB_IS_FATX)
467 {
468 rcFCB->ShortHash.Hash = rcFCB->Hash.Hash;
469 }
470 else
471 {
472 rcFCB->ShortHash.Hash = vfatNameHash(hash, &rcFCB->ShortNameU);
473 }
474
475 if (vfatFCBIsDirectory(rcFCB))
476 {
477 ULONG FirstCluster, CurrentCluster;
478 NTSTATUS Status = STATUS_SUCCESS;
479 Size = 0;
480 FirstCluster = vfatDirEntryGetFirstCluster (vcb, &rcFCB->entry);
481 if (FirstCluster == 1)
482 {
483 Size = vcb->FatInfo.rootDirectorySectors * vcb->FatInfo.BytesPerSector;
484 }
485 else if (FirstCluster != 0)
486 {
487 CurrentCluster = FirstCluster;
488 while (CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
489 {
490 Size += vcb->FatInfo.BytesPerCluster;
491 Status = NextCluster (vcb, FirstCluster, &CurrentCluster, FALSE);
492 }
493 }
494 }
495 else if (rcFCB->Flags & FCB_IS_FATX_ENTRY)
496 {
497 Size = rcFCB->entry.FatX.FileSize;
498 }
499 else
500 {
501 Size = rcFCB->entry.Fat.FileSize;
502 }
503 rcFCB->dirIndex = DirContext->DirIndex;
504 rcFCB->startIndex = DirContext->StartIndex;
505 if ((rcFCB->Flags & FCB_IS_FATX_ENTRY) && !vfatFCBIsRoot (directoryFCB))
506 {
507 ASSERT(DirContext->DirIndex >= 2 && DirContext->StartIndex >= 2);
508 rcFCB->dirIndex = DirContext->DirIndex-2;
509 rcFCB->startIndex = DirContext->StartIndex-2;
510 }
511 rcFCB->RFCB.FileSize.QuadPart = Size;
512 rcFCB->RFCB.ValidDataLength.QuadPart = Size;
513 rcFCB->RFCB.AllocationSize.QuadPart = ROUND_UP(Size, vcb->FatInfo.BytesPerCluster);
514 rcFCB->RefCount++;
515 if (vfatFCBIsDirectory(rcFCB))
516 {
517 vfatFCBInitializeCacheFromVolume(vcb, rcFCB);
518 }
519 rcFCB->parentFcb = directoryFCB;
520 vfatAddFCBToTable (vcb, rcFCB);
521 *fileFCB = rcFCB;
522
523 ExFreePool(PathNameBuffer);
524 return STATUS_SUCCESS;
525 }
526
527 NTSTATUS
528 vfatAttachFCBToFileObject (
529 PDEVICE_EXTENSION vcb,
530 PVFATFCB fcb,
531 PFILE_OBJECT fileObject)
532 {
533 PVFATCCB newCCB;
534
535 newCCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
536 if (newCCB == NULL)
537 {
538 return STATUS_INSUFFICIENT_RESOURCES;
539 }
540 RtlZeroMemory (newCCB, sizeof (VFATCCB));
541
542 fileObject->SectionObjectPointer = &fcb->SectionObjectPointers;
543 fileObject->FsContext = fcb;
544 fileObject->FsContext2 = newCCB;
545 DPRINT ("file open: fcb:%p PathName:%wZ\n", fcb, &fcb->PathNameU);
546
547 return STATUS_SUCCESS;
548 }
549
550 NTSTATUS
551 vfatDirFindFile (
552 PDEVICE_EXTENSION pDeviceExt,
553 PVFATFCB pDirectoryFCB,
554 PUNICODE_STRING FileToFindU,
555 PVFATFCB * pFoundFCB)
556 {
557 NTSTATUS status;
558 PVOID Context = NULL;
559 PVOID Page = NULL;
560 BOOLEAN First = TRUE;
561 VFAT_DIRENTRY_CONTEXT DirContext;
562 /* This buffer must have a size of 260 characters, because
563 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
564 WCHAR LongNameBuffer[260];
565 WCHAR ShortNameBuffer[13];
566 BOOLEAN FoundLong = FALSE;
567 BOOLEAN FoundShort = FALSE;
568
569 ASSERT(pDeviceExt);
570 ASSERT(pDirectoryFCB);
571 ASSERT(FileToFindU);
572
573 DPRINT ("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
574 pDeviceExt,
575 pDirectoryFCB,
576 FileToFindU);
577 DPRINT ("Dir Path:%wZ\n", &pDirectoryFCB->PathNameU);
578
579 DirContext.DirIndex = 0;
580 DirContext.LongNameU.Buffer = LongNameBuffer;
581 DirContext.LongNameU.Length = 0;
582 DirContext.LongNameU.MaximumLength = sizeof(LongNameBuffer);
583 DirContext.ShortNameU.Buffer = ShortNameBuffer;
584 DirContext.ShortNameU.Length = 0;
585 DirContext.ShortNameU.MaximumLength = sizeof(ShortNameBuffer);
586
587 while (TRUE)
588 {
589 status = pDeviceExt->GetNextDirEntry(&Context,
590 &Page,
591 pDirectoryFCB,
592 &DirContext,
593 First);
594 First = FALSE;
595 if (status == STATUS_NO_MORE_ENTRIES)
596 {
597 return STATUS_OBJECT_NAME_NOT_FOUND;
598 }
599 if (!NT_SUCCESS(status))
600 {
601 return status;
602 }
603
604 DPRINT (" Index:%d longName:%wZ\n",
605 DirContext.DirIndex,
606 &DirContext.LongNameU);
607
608 if (!ENTRY_VOLUME(pDeviceExt, &DirContext.DirEntry))
609 {
610 FoundLong = RtlEqualUnicodeString(FileToFindU, &DirContext.LongNameU, TRUE);
611 if (FoundLong == FALSE)
612 {
613 FoundShort = RtlEqualUnicodeString(FileToFindU, &DirContext.ShortNameU, TRUE);
614 }
615 if (FoundLong || FoundShort)
616 {
617 status = vfatMakeFCBFromDirEntry (pDeviceExt,
618 pDirectoryFCB,
619 &DirContext,
620 pFoundFCB);
621 CcUnpinData(Context);
622 return status;
623 }
624 }
625 DirContext.DirIndex++;
626 }
627
628 return STATUS_OBJECT_NAME_NOT_FOUND;
629 }
630
631 NTSTATUS
632 vfatGetFCBForFile (
633 PDEVICE_EXTENSION pVCB,
634 PVFATFCB *pParentFCB,
635 PVFATFCB *pFCB,
636 PUNICODE_STRING pFileNameU)
637 {
638 NTSTATUS status;
639 PVFATFCB FCB = NULL;
640 PVFATFCB parentFCB;
641 UNICODE_STRING NameU;
642 UNICODE_STRING RootNameU = RTL_CONSTANT_STRING(L"\\");
643 UNICODE_STRING FileNameU;
644 WCHAR NameBuffer[260];
645 PWCHAR curr, prev, last;
646 ULONG Length;
647
648 DPRINT ("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
649 pVCB,
650 pParentFCB,
651 pFCB,
652 pFileNameU);
653
654 FileNameU.Buffer = NameBuffer;
655 FileNameU.MaximumLength = sizeof(NameBuffer);
656 RtlCopyUnicodeString(&FileNameU, pFileNameU);
657
658 parentFCB = *pParentFCB;
659
660 if (parentFCB == NULL)
661 {
662 // Trivial case, open of the root directory on volume
663 if (RtlEqualUnicodeString(&FileNameU, &RootNameU, FALSE))
664 {
665 DPRINT ("returning root FCB\n");
666
667 FCB = vfatOpenRootFCB (pVCB);
668 *pFCB = FCB;
669 *pParentFCB = NULL;
670
671 return (FCB != NULL) ? STATUS_SUCCESS : STATUS_OBJECT_PATH_NOT_FOUND;
672 }
673
674 /* Check for an existing FCB */
675 FCB = vfatGrabFCBFromTable (pVCB, &FileNameU);
676 if (FCB)
677 {
678 *pFCB = FCB;
679 *pParentFCB = FCB->parentFcb;
680 (*pParentFCB)->RefCount++;
681 return STATUS_SUCCESS;
682 }
683
684 last = curr = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
685 while (*curr != L'\\' && curr > FileNameU.Buffer)
686 {
687 curr--;
688 }
689
690 if (curr > FileNameU.Buffer)
691 {
692 NameU.Buffer = FileNameU.Buffer;
693 NameU.MaximumLength = NameU.Length = (curr - FileNameU.Buffer) * sizeof(WCHAR);
694 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
695 if (FCB)
696 {
697 Length = (curr - FileNameU.Buffer) * sizeof(WCHAR);
698 if (Length != FCB->PathNameU.Length)
699 {
700 if (FileNameU.Length + FCB->PathNameU.Length - Length > FileNameU.MaximumLength)
701 {
702 vfatReleaseFCB (pVCB, FCB);
703 return STATUS_OBJECT_NAME_INVALID;
704 }
705 RtlMoveMemory(FileNameU.Buffer + FCB->PathNameU.Length / sizeof(WCHAR),
706 curr, FileNameU.Length - Length);
707 FileNameU.Length += (USHORT)(FCB->PathNameU.Length - Length);
708 curr = FileNameU.Buffer + FCB->PathNameU.Length / sizeof(WCHAR);
709 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
710 }
711 RtlCopyMemory(FileNameU.Buffer, FCB->PathNameU.Buffer, FCB->PathNameU.Length);
712 }
713 }
714 else
715 {
716 FCB = NULL;
717 }
718
719 if (FCB == NULL)
720 {
721 FCB = vfatOpenRootFCB(pVCB);
722 curr = FileNameU.Buffer;
723 }
724
725 parentFCB = NULL;
726 prev = curr;
727 }
728 else
729 {
730 FCB = parentFCB;
731 parentFCB = NULL;
732 prev = curr = FileNameU.Buffer - 1;
733 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
734 }
735
736 while (curr <= last)
737 {
738 if (parentFCB)
739 {
740 vfatReleaseFCB (pVCB, parentFCB);
741 parentFCB = 0;
742 }
743 // fail if element in FCB is not a directory
744 if (!vfatFCBIsDirectory (FCB))
745 {
746 DPRINT ("Element in requested path is not a directory\n");
747
748 vfatReleaseFCB (pVCB, FCB);
749 FCB = NULL;
750 *pParentFCB = NULL;
751 *pFCB = NULL;
752
753 return STATUS_OBJECT_PATH_NOT_FOUND;
754 }
755 parentFCB = FCB;
756 if (prev < curr)
757 {
758 Length = (curr - prev) * sizeof(WCHAR);
759 if (Length != parentFCB->LongNameU.Length)
760 {
761 if (FileNameU.Length + parentFCB->LongNameU.Length - Length > FileNameU.MaximumLength)
762 {
763 vfatReleaseFCB (pVCB, parentFCB);
764 return STATUS_OBJECT_NAME_INVALID;
765 }
766 RtlMoveMemory(prev + parentFCB->LongNameU.Length / sizeof(WCHAR), curr,
767 FileNameU.Length - (curr - FileNameU.Buffer) * sizeof(WCHAR));
768 FileNameU.Length += (USHORT)(parentFCB->LongNameU.Length - Length);
769 curr = prev + parentFCB->LongNameU.Length / sizeof(WCHAR);
770 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
771 }
772 RtlCopyMemory(prev, parentFCB->LongNameU.Buffer, parentFCB->LongNameU.Length);
773 }
774 curr++;
775 prev = curr;
776 while (*curr != L'\\' && curr <= last)
777 {
778 curr++;
779 }
780 NameU.Buffer = FileNameU.Buffer;
781 NameU.Length = (curr - NameU.Buffer) * sizeof(WCHAR);
782 NameU.MaximumLength = FileNameU.MaximumLength;
783 DPRINT("%wZ\n", &NameU);
784 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
785 if (FCB == NULL)
786 {
787 NameU.Buffer = prev;
788 NameU.MaximumLength = NameU.Length = (curr - prev) * sizeof(WCHAR);
789 status = vfatDirFindFile(pVCB, parentFCB, &NameU, &FCB);
790 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
791 {
792 *pFCB = NULL;
793 if (curr > last)
794 {
795 *pParentFCB = parentFCB;
796 return STATUS_OBJECT_NAME_NOT_FOUND;
797 }
798 else
799 {
800 vfatReleaseFCB (pVCB, parentFCB);
801 *pParentFCB = NULL;
802 return STATUS_OBJECT_PATH_NOT_FOUND;
803 }
804 }
805 else if (!NT_SUCCESS (status))
806 {
807 vfatReleaseFCB (pVCB, parentFCB);
808 *pParentFCB = NULL;
809 *pFCB = NULL;
810
811 return status;
812 }
813 }
814 }
815
816 *pParentFCB = parentFCB;
817 *pFCB = FCB;
818
819 return STATUS_SUCCESS;
820 }
821