2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/filesystems/fastfat/dirwr.c
5 * PURPOSE: VFAT Filesystem : write in directory
6 * PROGRAMMER: Rex Jolliff (rex@lvcablemodem.com)
7 * Herve Poussineau (reactos@poussine.freesurf.fr)
8 * Pierre Schweitzer (pierre@reactos.org)
12 /* INCLUDES *****************************************************************/
20 extern UNICODE_STRING DebugFile
;
24 vfatFCBInitializeCacheFromVolume(
28 PFILE_OBJECT fileObject
;
33 /* Don't re-initialize if already done */
34 if (BooleanFlagOn(fcb
->Flags
, FCB_CACHE_INITIALIZED
))
36 return STATUS_SUCCESS
;
39 ASSERT(vfatFCBIsDirectory(fcb
));
40 ASSERT(fcb
->FileObject
== NULL
);
43 if (!ExIsResourceAcquiredExclusive(&vcb
->DirResource
))
45 ExAcquireResourceExclusiveLite(&vcb
->DirResource
, TRUE
);
49 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
52 if (DebugFile
.Buffer
!= NULL
&& FsRtlIsNameInExpression(&DebugFile
, &fcb
->LongNameU
, FALSE
, NULL
))
54 DPRINT1("Attaching %p to %p (%d)\n", fcb
, fileObject
, fcb
->RefCount
);
58 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
61 status
= STATUS_INSUFFICIENT_RESOURCES
;
62 ObDereferenceObject(fileObject
);
65 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
67 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
68 fileObject
->FsContext
= fcb
;
69 fileObject
->FsContext2
= newCCB
;
70 fileObject
->Vpb
= vcb
->IoVPB
;
71 fcb
->FileObject
= fileObject
;
75 CcInitializeCacheMap(fileObject
,
76 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
78 &VfatGlobalData
->CacheMgrCallbacks
,
81 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
83 status
= _SEH2_GetExceptionCode();
84 fcb
->FileObject
= NULL
;
85 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
86 ObDereferenceObject(fileObject
);
89 ExReleaseResourceLite(&vcb
->DirResource
);
95 vfatGrabFCB(vcb
, fcb
);
96 SetFlag(fcb
->Flags
, FCB_CACHE_INITIALIZED
);
97 status
= STATUS_SUCCESS
;
102 ExReleaseResourceLite(&vcb
->DirResource
);
109 * update an existing FAT entry
113 IN PDEVICE_EXTENSION DeviceExt
,
118 LARGE_INTEGER Offset
;
125 if (vfatVolumeIsFatX(DeviceExt
))
127 SizeDirEntry
= sizeof(FATX_DIR_ENTRY
);
128 dirIndex
= pFcb
->startIndex
;
132 SizeDirEntry
= sizeof(FAT_DIR_ENTRY
);
133 dirIndex
= pFcb
->dirIndex
;
136 DPRINT("updEntry dirIndex %u, PathName \'%wZ\'\n", dirIndex
, &pFcb
->PathNameU
);
138 if (vfatFCBIsRoot(pFcb
) || BooleanFlagOn(pFcb
->Flags
, FCB_IS_FAT
| FCB_IS_VOLUME
))
140 return STATUS_SUCCESS
;
143 ASSERT(pFcb
->parentFcb
);
145 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pFcb
->parentFcb
);
146 if (!NT_SUCCESS(Status
))
151 Offset
.u
.HighPart
= 0;
152 Offset
.u
.LowPart
= dirIndex
* SizeDirEntry
;
155 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, SizeDirEntry
, PIN_WAIT
, &Context
, (PVOID
*)&PinEntry
);
157 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
159 DPRINT1("Failed write to \'%wZ\'.\n", &pFcb
->parentFcb
->PathNameU
);
160 _SEH2_YIELD(return _SEH2_GetExceptionCode());
164 pFcb
->Flags
&= ~FCB_IS_DIRTY
;
165 RtlCopyMemory(PinEntry
, &pFcb
->entry
, SizeDirEntry
);
166 CcSetDirtyPinnedData(Context
, NULL
);
167 CcUnpinData(Context
);
168 return STATUS_SUCCESS
;
172 * rename an existing FAT entry
176 IN PDEVICE_EXTENSION DeviceExt
,
178 IN PUNICODE_STRING FileName
,
179 IN BOOLEAN CaseChangeOnly
)
183 PVOID Context
= NULL
;
184 LARGE_INTEGER Offset
;
185 PFATX_DIR_ENTRY pDirEntry
;
188 DPRINT("vfatRenameEntry(%p, %p, %wZ, %d)\n", DeviceExt
, pFcb
, FileName
, CaseChangeOnly
);
190 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pFcb
->parentFcb
);
191 if (!NT_SUCCESS(Status
))
196 if (vfatVolumeIsFatX(DeviceExt
))
198 VFAT_DIRENTRY_CONTEXT DirContext
;
200 /* Open associated dir entry */
201 StartIndex
= pFcb
->startIndex
;
202 Offset
.u
.HighPart
= 0;
203 Offset
.u
.LowPart
= (StartIndex
* sizeof(FATX_DIR_ENTRY
) / PAGE_SIZE
) * PAGE_SIZE
;
206 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, PAGE_SIZE
, PIN_WAIT
, &Context
, (PVOID
*)&pDirEntry
);
208 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
210 DPRINT1("CcPinRead(Offset %x:%x, Length %d) failed\n", Offset
.u
.HighPart
, Offset
.u
.LowPart
, PAGE_SIZE
);
211 _SEH2_YIELD(return _SEH2_GetExceptionCode());
215 pDirEntry
= &pDirEntry
[StartIndex
% (PAGE_SIZE
/ sizeof(FATX_DIR_ENTRY
))];
218 NameA
.Buffer
= (PCHAR
)pDirEntry
->Filename
;
220 NameA
.MaximumLength
= 42;
221 RtlUnicodeStringToOemString(&NameA
, FileName
, FALSE
);
222 pDirEntry
->FilenameLength
= (unsigned char)NameA
.Length
;
225 DirContext
.DeviceExt
= DeviceExt
;
226 DirContext
.ShortNameU
.Length
= 0;
227 DirContext
.ShortNameU
.MaximumLength
= 0;
228 DirContext
.ShortNameU
.Buffer
= NULL
;
229 DirContext
.LongNameU
= *FileName
;
230 DirContext
.DirEntry
.FatX
= *pDirEntry
;
232 CcSetDirtyPinnedData(Context
, NULL
);
233 CcUnpinData(Context
);
235 Status
= vfatUpdateFCB(DeviceExt
, pFcb
, &DirContext
, pFcb
->parentFcb
);
236 if (NT_SUCCESS(Status
))
238 CcFlushCache(&pFcb
->parentFcb
->SectionObjectPointers
, NULL
, 0, NULL
);
245 /* This we cannot handle properly, move file - would likely need love */
246 return VfatMoveEntry(DeviceExt
, pFcb
, FileName
, pFcb
->parentFcb
);
251 * try to find contiguous entries frees in directory,
252 * extend a directory if is necessary
256 IN PDEVICE_EXTENSION DeviceExt
,
261 LARGE_INTEGER FileOffset
;
262 ULONG i
, count
, size
, nbFree
= 0;
263 PDIR_ENTRY pFatEntry
= NULL
;
264 PVOID Context
= NULL
;
267 BOOLEAN IsFatX
= vfatVolumeIsFatX(DeviceExt
);
268 FileOffset
.QuadPart
= 0;
271 SizeDirEntry
= sizeof(FATX_DIR_ENTRY
);
273 SizeDirEntry
= sizeof(FAT_DIR_ENTRY
);
275 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pDirFcb
);
276 if (!NT_SUCCESS(Status
))
281 count
= pDirFcb
->RFCB
.FileSize
.u
.LowPart
/ SizeDirEntry
;
282 size
= DeviceExt
->FatInfo
.BytesPerCluster
/ SizeDirEntry
;
283 for (i
= 0; i
< count
; i
++, pFatEntry
= (PDIR_ENTRY
)((ULONG_PTR
)pFatEntry
+ SizeDirEntry
))
285 if (Context
== NULL
|| (i
% size
) == 0)
289 CcUnpinData(Context
);
293 CcPinRead(pDirFcb
->FileObject
, &FileOffset
, DeviceExt
->FatInfo
.BytesPerCluster
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
295 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
297 _SEH2_YIELD(return FALSE
);
301 FileOffset
.u
.LowPart
+= DeviceExt
->FatInfo
.BytesPerCluster
;
303 if (ENTRY_END(IsFatX
, pFatEntry
))
307 if (ENTRY_DELETED(IsFatX
, pFatEntry
))
315 if (nbFree
== nbSlots
)
322 CcUnpinData(Context
);
325 if (nbFree
== nbSlots
)
327 /* found enough contiguous free slots */
328 *start
= i
- nbSlots
+ 1;
333 if (*start
+ nbSlots
> count
)
335 LARGE_INTEGER AllocationSize
;
336 /* extend the directory */
337 if (vfatFCBIsRoot(pDirFcb
) && DeviceExt
->FatInfo
.FatType
!= FAT32
)
339 /* We can't extend a root directory on a FAT12/FAT16/FATX partition */
342 AllocationSize
.QuadPart
= pDirFcb
->RFCB
.FileSize
.u
.LowPart
+ DeviceExt
->FatInfo
.BytesPerCluster
;
343 Status
= VfatSetAllocationSizeInformation(pDirFcb
->FileObject
, pDirFcb
,
344 DeviceExt
, &AllocationSize
);
345 if (!NT_SUCCESS(Status
))
349 /* clear the new dir cluster */
350 FileOffset
.u
.LowPart
= (ULONG
)(pDirFcb
->RFCB
.FileSize
.QuadPart
-
351 DeviceExt
->FatInfo
.BytesPerCluster
);
354 CcPinRead(pDirFcb
->FileObject
, &FileOffset
, DeviceExt
->FatInfo
.BytesPerCluster
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
356 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
358 _SEH2_YIELD(return FALSE
);
363 memset(pFatEntry
, 0xff, DeviceExt
->FatInfo
.BytesPerCluster
);
365 RtlZeroMemory(pFatEntry
, DeviceExt
->FatInfo
.BytesPerCluster
);
367 else if (*start
+ nbSlots
< count
)
369 /* clear the entry after the last new entry */
370 FileOffset
.u
.LowPart
= (*start
+ nbSlots
) * SizeDirEntry
;
373 CcPinRead(pDirFcb
->FileObject
, &FileOffset
, SizeDirEntry
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
375 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
377 _SEH2_YIELD(return FALSE
);
382 memset(pFatEntry
, 0xff, SizeDirEntry
);
384 RtlZeroMemory(pFatEntry
, SizeDirEntry
);
388 CcSetDirtyPinnedData(Context
, NULL
);
389 CcUnpinData(Context
);
392 DPRINT("nbSlots %u nbFree %u, entry number %u\n", nbSlots
, nbFree
, *start
);
397 create a new FAT entry
401 IN PDEVICE_EXTENSION DeviceExt
,
402 IN PUNICODE_STRING NameU
,
404 IN PVFATFCB ParentFcb
,
405 IN ULONG RequestedOptions
,
407 IN PVFAT_MOVE_CONTEXT MoveContext
)
409 PVOID Context
= NULL
;
410 PFAT_DIR_ENTRY pFatEntry
;
412 USHORT nbSlots
= 0, j
;
414 BOOLEAN needTilde
= FALSE
, needLong
= FALSE
;
415 BOOLEAN BaseAllLower
, BaseAllUpper
;
416 BOOLEAN ExtensionAllLower
, ExtensionAllUpper
;
420 ULONG CurrentCluster
;
421 LARGE_INTEGER SystemTime
, FileOffset
;
422 NTSTATUS Status
= STATUS_SUCCESS
;
431 VFAT_DIRENTRY_CONTEXT DirContext
;
432 WCHAR LongNameBuffer
[LONGNAME_MAX_LENGTH
+ 1];
433 WCHAR ShortNameBuffer
[13];
435 DPRINT("addEntry: Name='%wZ', Dir='%wZ'\n", NameU
, &ParentFcb
->PathNameU
);
437 DirContext
.LongNameU
= *NameU
;
438 IsDirectory
= BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
);
440 /* nb of entry needed for long name+normal entry */
441 nbSlots
= (DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) + 12) / 13 + 1;
442 DPRINT("NameLen= %u, nbSlots =%u\n", DirContext
.LongNameU
.Length
/ sizeof(WCHAR
), nbSlots
);
443 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, (nbSlots
- 1) * sizeof(FAT_DIR_ENTRY
), TAG_VFAT
);
446 return STATUS_INSUFFICIENT_RESOURCES
;
448 RtlZeroMemory(Buffer
, (nbSlots
- 1) * sizeof(FAT_DIR_ENTRY
));
449 pSlots
= (slot
*) Buffer
;
451 NameA
.Buffer
= aName
;
453 NameA
.MaximumLength
= sizeof(aName
);
455 DirContext
.DeviceExt
= DeviceExt
;
456 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
457 DirContext
.ShortNameU
.Length
= 0;
458 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
460 RtlZeroMemory(&DirContext
.DirEntry
.Fat
, sizeof(FAT_DIR_ENTRY
));
462 IsNameLegal
= RtlIsNameLegalDOS8Dot3(&DirContext
.LongNameU
, &NameA
, &SpacesFound
);
464 if (!IsNameLegal
|| SpacesFound
)
466 GENERATE_NAME_CONTEXT NameContext
;
467 VFAT_DIRENTRY_CONTEXT SearchContext
;
468 WCHAR ShortSearchName
[13];
471 RtlZeroMemory(&NameContext
, sizeof(GENERATE_NAME_CONTEXT
));
472 SearchContext
.DeviceExt
= DeviceExt
;
473 SearchContext
.LongNameU
.Buffer
= LongNameBuffer
;
474 SearchContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
475 SearchContext
.ShortNameU
.Buffer
= ShortSearchName
;
476 SearchContext
.ShortNameU
.MaximumLength
= sizeof(ShortSearchName
);
478 for (i
= 0; i
< 100; i
++)
480 RtlGenerate8dot3Name(&DirContext
.LongNameU
, FALSE
, &NameContext
, &DirContext
.ShortNameU
);
481 DirContext
.ShortNameU
.Buffer
[DirContext
.ShortNameU
.Length
/ sizeof(WCHAR
)] = 0;
482 SearchContext
.DirIndex
= 0;
483 Status
= FindFile(DeviceExt
, ParentFcb
, &DirContext
.ShortNameU
, &SearchContext
, TRUE
);
484 if (!NT_SUCCESS(Status
))
488 else if (MoveContext
)
491 if (strncmp((char *)SearchContext
.DirEntry
.Fat
.ShortName
, (char *)(*Fcb
)->entry
.Fat
.ShortName
, 11) == 0)
493 if (MoveContext
->InPlace
)
495 ASSERT(SearchContext
.DirEntry
.Fat
.FileSize
== MoveContext
->FileSize
);
501 if (i
== 100) /* FIXME : what to do after this ? */
503 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
504 return STATUS_UNSUCCESSFUL
;
506 IsNameLegal
= RtlIsNameLegalDOS8Dot3(&DirContext
.ShortNameU
, &NameA
, &SpacesFound
);
510 BaseAllLower
= BaseAllUpper
= TRUE
;
511 ExtensionAllLower
= ExtensionAllUpper
= TRUE
;
513 for (i
= 0; i
< DirContext
.LongNameU
.Length
/ sizeof(WCHAR
); i
++)
515 c
= DirContext
.LongNameU
.Buffer
[i
];
516 if (c
>= L
'A' && c
<= L
'Z')
519 ExtensionAllLower
= FALSE
;
521 BaseAllLower
= FALSE
;
523 else if (c
>= L
'a' && c
<= L
'z')
526 ExtensionAllUpper
= FALSE
;
528 BaseAllUpper
= FALSE
;
542 if ((!BaseAllLower
&& !BaseAllUpper
) ||
543 (!ExtensionAllLower
&& !ExtensionAllUpper
))
548 RtlUpcaseUnicodeString(&DirContext
.ShortNameU
, &DirContext
.LongNameU
, FALSE
);
549 DirContext
.ShortNameU
.Buffer
[DirContext
.ShortNameU
.Length
/ sizeof(WCHAR
)] = 0;
551 aName
[NameA
.Length
] = 0;
552 DPRINT("'%s', '%wZ', needTilde=%u, needLong=%u\n",
553 aName
, &DirContext
.LongNameU
, needTilde
, needLong
);
554 memset(DirContext
.DirEntry
.Fat
.ShortName
, ' ', 11);
555 for (i
= 0; i
< 8 && aName
[i
] && aName
[i
] != '.'; i
++)
557 DirContext
.DirEntry
.Fat
.Filename
[i
] = aName
[i
];
562 for (j
= 0; j
< 3 && aName
[i
]; j
++, i
++)
564 DirContext
.DirEntry
.Fat
.Ext
[j
] = aName
[i
];
567 if (DirContext
.DirEntry
.Fat
.Filename
[0] == 0xe5)
569 DirContext
.DirEntry
.Fat
.Filename
[0] = 0x05;
574 RtlCopyMemory(LongNameBuffer
, DirContext
.LongNameU
.Buffer
, DirContext
.LongNameU
.Length
);
575 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
576 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
577 DirContext
.LongNameU
.Buffer
[DirContext
.LongNameU
.Length
/ sizeof(WCHAR
)] = 0;
578 memset(DirContext
.LongNameU
.Buffer
+ DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) + 1, 0xff,
579 DirContext
.LongNameU
.MaximumLength
- DirContext
.LongNameU
.Length
- sizeof(WCHAR
));
584 if (BaseAllLower
&& !BaseAllUpper
)
586 DirContext
.DirEntry
.Fat
.lCase
|= VFAT_CASE_LOWER_BASE
;
588 if (ExtensionAllLower
&& !ExtensionAllUpper
)
590 DirContext
.DirEntry
.Fat
.lCase
|= VFAT_CASE_LOWER_EXT
;
594 DPRINT ("dos name=%11.11s\n", DirContext
.DirEntry
.Fat
.Filename
);
597 DirContext
.DirEntry
.Fat
.Attrib
= ReqAttr
;
600 DirContext
.DirEntry
.Fat
.Attrib
|= FILE_ATTRIBUTE_DIRECTORY
;
602 /* set dates and times */
603 KeQuerySystemTime(&SystemTime
);
604 FsdSystemTimeToDosDateTime(DeviceExt
, &SystemTime
, &DirContext
.DirEntry
.Fat
.CreationDate
,
605 &DirContext
.DirEntry
.Fat
.CreationTime
);
606 DirContext
.DirEntry
.Fat
.UpdateDate
= DirContext
.DirEntry
.Fat
.CreationDate
;
607 DirContext
.DirEntry
.Fat
.UpdateTime
= DirContext
.DirEntry
.Fat
.CreationTime
;
608 DirContext
.DirEntry
.Fat
.AccessDate
= DirContext
.DirEntry
.Fat
.CreationDate
;
609 /* If it's moving, preserve creation time and file size */
610 if (MoveContext
!= NULL
)
612 DirContext
.DirEntry
.Fat
.CreationDate
= MoveContext
->CreationDate
;
613 DirContext
.DirEntry
.Fat
.CreationTime
= MoveContext
->CreationTime
;
614 DirContext
.DirEntry
.Fat
.FileSize
= MoveContext
->FileSize
;
619 /* calculate checksum for 8.3 name */
620 for (pSlots
[0].alias_checksum
= 0, i
= 0; i
< 11; i
++)
622 pSlots
[0].alias_checksum
= (((pSlots
[0].alias_checksum
& 1) << 7
623 | ((pSlots
[0].alias_checksum
& 0xfe) >> 1))
624 + DirContext
.DirEntry
.Fat
.ShortName
[i
]);
626 /* construct slots and entry */
627 for (i
= nbSlots
- 2; i
>= 0; i
--)
629 DPRINT("construct slot %d\n", i
);
630 pSlots
[i
].attr
= 0xf;
633 pSlots
[i
].id
= (unsigned char)(nbSlots
- i
- 1);
637 pSlots
[i
].id
= (unsigned char)(nbSlots
- i
- 1 + 0x40);
639 pSlots
[i
].alias_checksum
= pSlots
[0].alias_checksum
;
640 RtlCopyMemory(pSlots
[i
].name0_4
, DirContext
.LongNameU
.Buffer
+ (nbSlots
- i
- 2) * 13, 10);
641 RtlCopyMemory(pSlots
[i
].name5_10
, DirContext
.LongNameU
.Buffer
+ (nbSlots
- i
- 2) * 13 + 5, 12);
642 RtlCopyMemory(pSlots
[i
].name11_12
, DirContext
.LongNameU
.Buffer
+ (nbSlots
- i
- 2) * 13 + 11, 4);
645 /* try to find nbSlots contiguous entries frees in directory */
646 if (!vfatFindDirSpace(DeviceExt
, ParentFcb
, nbSlots
, &DirContext
.StartIndex
))
648 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
649 return STATUS_DISK_FULL
;
651 DirContext
.DirIndex
= DirContext
.StartIndex
+ nbSlots
- 1;
654 /* If we aren't moving, use next */
655 if (MoveContext
== NULL
)
658 Status
= NextCluster(DeviceExt
, 0, &CurrentCluster
, TRUE
);
659 if (CurrentCluster
== 0xffffffff || !NT_SUCCESS(Status
))
661 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
662 if (!NT_SUCCESS(Status
))
666 return STATUS_DISK_FULL
;
671 CurrentCluster
= MoveContext
->FirstCluster
;
674 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
676 DirContext
.DirEntry
.Fat
.FirstClusterHigh
= (unsigned short)(CurrentCluster
>> 16);
678 DirContext
.DirEntry
.Fat
.FirstCluster
= (unsigned short)CurrentCluster
;
680 else if (MoveContext
!= NULL
)
682 CurrentCluster
= MoveContext
->FirstCluster
;
684 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
686 DirContext
.DirEntry
.Fat
.FirstClusterHigh
= (unsigned short)(CurrentCluster
>> 16);
688 DirContext
.DirEntry
.Fat
.FirstCluster
= (unsigned short)CurrentCluster
;
691 /* No need to init cache here, vfatFindDirSpace() will have done it for us */
692 ASSERT(BooleanFlagOn(ParentFcb
->Flags
, FCB_CACHE_INITIALIZED
));
694 i
= DeviceExt
->FatInfo
.BytesPerCluster
/ sizeof(FAT_DIR_ENTRY
);
695 FileOffset
.u
.HighPart
= 0;
696 FileOffset
.u
.LowPart
= DirContext
.StartIndex
* sizeof(FAT_DIR_ENTRY
);
697 if (DirContext
.StartIndex
/ i
== DirContext
.DirIndex
/ i
)
702 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, nbSlots
* sizeof(FAT_DIR_ENTRY
), PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
704 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
706 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
707 _SEH2_YIELD(return _SEH2_GetExceptionCode());
713 RtlCopyMemory(pFatEntry
, Buffer
, (nbSlots
- 1) * sizeof(FAT_DIR_ENTRY
));
715 RtlCopyMemory(pFatEntry
+ (nbSlots
- 1), &DirContext
.DirEntry
.Fat
, sizeof(FAT_DIR_ENTRY
));
720 size
= DeviceExt
->FatInfo
.BytesPerCluster
-
721 (DirContext
.StartIndex
* sizeof(FAT_DIR_ENTRY
)) % DeviceExt
->FatInfo
.BytesPerCluster
;
722 i
= size
/ sizeof(FAT_DIR_ENTRY
);
725 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, size
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
727 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
729 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
730 _SEH2_YIELD(return _SEH2_GetExceptionCode());
733 RtlCopyMemory(pFatEntry
, Buffer
, size
);
734 CcSetDirtyPinnedData(Context
, NULL
);
735 CcUnpinData(Context
);
736 FileOffset
.u
.LowPart
+= size
;
739 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, nbSlots
* sizeof(FAT_DIR_ENTRY
) - size
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
741 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
743 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
744 _SEH2_YIELD(return _SEH2_GetExceptionCode());
749 RtlCopyMemory(pFatEntry
, (PVOID
)(Buffer
+ size
), (nbSlots
- 1 - i
) * sizeof(FAT_DIR_ENTRY
));
751 RtlCopyMemory(pFatEntry
+ nbSlots
- 1 - i
, &DirContext
.DirEntry
.Fat
, sizeof(FAT_DIR_ENTRY
));
753 CcSetDirtyPinnedData(Context
, NULL
);
754 CcUnpinData(Context
);
756 if (MoveContext
!= NULL
)
758 /* We're modifying an existing FCB - likely rename/move */
759 Status
= vfatUpdateFCB(DeviceExt
, *Fcb
, &DirContext
, ParentFcb
);
763 Status
= vfatMakeFCBFromDirEntry(DeviceExt
, ParentFcb
, &DirContext
, Fcb
);
765 if (!NT_SUCCESS(Status
))
767 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
771 DPRINT("new : entry=%11.11s\n", (*Fcb
)->entry
.Fat
.Filename
);
772 DPRINT("new : entry=%11.11s\n", DirContext
.DirEntry
.Fat
.Filename
);
776 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, (*Fcb
));
777 if (!NT_SUCCESS(Status
))
779 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
783 FileOffset
.QuadPart
= 0;
786 CcPinRead((*Fcb
)->FileObject
, &FileOffset
, DeviceExt
->FatInfo
.BytesPerCluster
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
788 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
790 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
791 _SEH2_YIELD(return _SEH2_GetExceptionCode());
794 /* clear the new directory cluster if not moving */
795 if (MoveContext
== NULL
)
797 RtlZeroMemory(pFatEntry
, DeviceExt
->FatInfo
.BytesPerCluster
);
798 /* create '.' and '..' */
799 RtlCopyMemory(&pFatEntry
[0].Attrib
, &DirContext
.DirEntry
.Fat
.Attrib
, sizeof(FAT_DIR_ENTRY
) - 11);
800 RtlCopyMemory(pFatEntry
[0].ShortName
, ". ", 11);
801 RtlCopyMemory(&pFatEntry
[1].Attrib
, &DirContext
.DirEntry
.Fat
.Attrib
, sizeof(FAT_DIR_ENTRY
) - 11);
802 RtlCopyMemory(pFatEntry
[1].ShortName
, ".. ", 11);
805 pFatEntry
[1].FirstCluster
= ParentFcb
->entry
.Fat
.FirstCluster
;
806 pFatEntry
[1].FirstClusterHigh
= ParentFcb
->entry
.Fat
.FirstClusterHigh
;
807 if (vfatFCBIsRoot(ParentFcb
))
809 pFatEntry
[1].FirstCluster
= 0;
810 pFatEntry
[1].FirstClusterHigh
= 0;
812 CcSetDirtyPinnedData(Context
, NULL
);
813 CcUnpinData(Context
);
815 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
816 DPRINT("addentry ok\n");
817 return STATUS_SUCCESS
;
821 create a new FAT entry
825 IN PDEVICE_EXTENSION DeviceExt
,
826 IN PUNICODE_STRING NameU
,
828 IN PVFATFCB ParentFcb
,
829 IN ULONG RequestedOptions
,
831 IN PVFAT_MOVE_CONTEXT MoveContext
)
833 PVOID Context
= NULL
;
834 LARGE_INTEGER SystemTime
, FileOffset
;
836 VFAT_DIRENTRY_CONTEXT DirContext
;
837 PFATX_DIR_ENTRY pFatXDirEntry
;
840 DPRINT("addEntry: Name='%wZ', Dir='%wZ'\n", NameU
, &ParentFcb
->PathNameU
);
842 DirContext
.LongNameU
= *NameU
;
844 if (DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) > 42)
847 return STATUS_NAME_TOO_LONG
;
850 /* try to find 1 entry free in directory */
851 if (!vfatFindDirSpace(DeviceExt
, ParentFcb
, 1, &DirContext
.StartIndex
))
853 return STATUS_DISK_FULL
;
855 Index
= DirContext
.DirIndex
= DirContext
.StartIndex
;
856 if (!vfatFCBIsRoot(ParentFcb
))
858 DirContext
.DirIndex
+= 2;
859 DirContext
.StartIndex
+= 2;
862 DirContext
.ShortNameU
.Buffer
= 0;
863 DirContext
.ShortNameU
.Length
= 0;
864 DirContext
.ShortNameU
.MaximumLength
= 0;
865 DirContext
.DeviceExt
= DeviceExt
;
866 RtlZeroMemory(&DirContext
.DirEntry
.FatX
, sizeof(FATX_DIR_ENTRY
));
867 memset(DirContext
.DirEntry
.FatX
.Filename
, 0xff, 42);
868 /* Use cluster, if moving */
869 if (MoveContext
!= NULL
)
871 DirContext
.DirEntry
.FatX
.FirstCluster
= MoveContext
->FirstCluster
;
875 DirContext
.DirEntry
.FatX
.FirstCluster
= 0;
877 DirContext
.DirEntry
.FatX
.FileSize
= 0;
880 NameA
.Buffer
= (PCHAR
)DirContext
.DirEntry
.FatX
.Filename
;
882 NameA
.MaximumLength
= 42;
883 RtlUnicodeStringToOemString(&NameA
, &DirContext
.LongNameU
, FALSE
);
884 DirContext
.DirEntry
.FatX
.FilenameLength
= (unsigned char)NameA
.Length
;
887 DirContext
.DirEntry
.FatX
.Attrib
= ReqAttr
;
888 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
))
890 DirContext
.DirEntry
.FatX
.Attrib
|= FILE_ATTRIBUTE_DIRECTORY
;
893 /* set dates and times */
894 KeQuerySystemTime(&SystemTime
);
895 FsdSystemTimeToDosDateTime(DeviceExt
, &SystemTime
, &DirContext
.DirEntry
.FatX
.CreationDate
,
896 &DirContext
.DirEntry
.FatX
.CreationTime
);
897 DirContext
.DirEntry
.FatX
.UpdateDate
= DirContext
.DirEntry
.FatX
.CreationDate
;
898 DirContext
.DirEntry
.FatX
.UpdateTime
= DirContext
.DirEntry
.FatX
.CreationTime
;
899 DirContext
.DirEntry
.FatX
.AccessDate
= DirContext
.DirEntry
.FatX
.CreationDate
;
900 DirContext
.DirEntry
.FatX
.AccessTime
= DirContext
.DirEntry
.FatX
.CreationTime
;
901 /* If it's moving, preserve creation time and file size */
902 if (MoveContext
!= NULL
)
904 DirContext
.DirEntry
.FatX
.CreationDate
= MoveContext
->CreationDate
;
905 DirContext
.DirEntry
.FatX
.CreationTime
= MoveContext
->CreationTime
;
906 DirContext
.DirEntry
.FatX
.FileSize
= MoveContext
->FileSize
;
909 /* No need to init cache here, vfatFindDirSpace() will have done it for us */
910 ASSERT(BooleanFlagOn(ParentFcb
->Flags
, FCB_CACHE_INITIALIZED
));
912 /* add entry into parent directory */
913 FileOffset
.u
.HighPart
= 0;
914 FileOffset
.u
.LowPart
= Index
* sizeof(FATX_DIR_ENTRY
);
917 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, sizeof(FATX_DIR_ENTRY
), PIN_WAIT
, &Context
, (PVOID
*)&pFatXDirEntry
);
919 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
921 _SEH2_YIELD(return _SEH2_GetExceptionCode());
924 RtlCopyMemory(pFatXDirEntry
, &DirContext
.DirEntry
.FatX
, sizeof(FATX_DIR_ENTRY
));
925 CcSetDirtyPinnedData(Context
, NULL
);
926 CcUnpinData(Context
);
928 if (MoveContext
!= NULL
)
930 /* We're modifying an existing FCB - likely rename/move */
931 /* FIXME: check status */
932 vfatUpdateFCB(DeviceExt
, *Fcb
, &DirContext
, ParentFcb
);
936 /* FIXME: check status */
937 vfatMakeFCBFromDirEntry(DeviceExt
, ParentFcb
, &DirContext
, Fcb
);
940 DPRINT("addentry ok\n");
941 return STATUS_SUCCESS
;
945 * deleting an existing FAT entry
949 IN PDEVICE_EXTENSION DeviceExt
,
951 OUT PVFAT_MOVE_CONTEXT MoveContext
)
953 ULONG CurrentCluster
= 0, NextCluster
, i
;
954 PVOID Context
= NULL
;
955 LARGE_INTEGER Offset
;
956 PFAT_DIR_ENTRY pDirEntry
= NULL
;
960 ASSERT(pFcb
->parentFcb
);
962 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pFcb
->parentFcb
);
963 if (!NT_SUCCESS(Status
))
968 DPRINT("delEntry PathName \'%wZ\'\n", &pFcb
->PathNameU
);
969 DPRINT("delete entry: %u to %u\n", pFcb
->startIndex
, pFcb
->dirIndex
);
970 Offset
.u
.HighPart
= 0;
971 for (i
= pFcb
->startIndex
; i
<= pFcb
->dirIndex
; i
++)
973 if (Context
== NULL
|| ((i
* sizeof(FAT_DIR_ENTRY
)) % PAGE_SIZE
) == 0)
977 CcSetDirtyPinnedData(Context
, NULL
);
978 CcUnpinData(Context
);
980 Offset
.u
.LowPart
= (i
* sizeof(FAT_DIR_ENTRY
) / PAGE_SIZE
) * PAGE_SIZE
;
983 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, PAGE_SIZE
, PIN_WAIT
, &Context
, (PVOID
*)&pDirEntry
);
985 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
987 _SEH2_YIELD(return _SEH2_GetExceptionCode());
991 pDirEntry
[i
% (PAGE_SIZE
/ sizeof(FAT_DIR_ENTRY
))].Filename
[0] = 0xe5;
992 if (i
== pFcb
->dirIndex
)
995 vfatDirEntryGetFirstCluster(DeviceExt
,
996 (PDIR_ENTRY
)&pDirEntry
[i
% (PAGE_SIZE
/ sizeof(FAT_DIR_ENTRY
))]);
1000 /* In case of moving, save properties */
1001 if (MoveContext
!= NULL
)
1003 pDirEntry
= &pDirEntry
[pFcb
->dirIndex
% (PAGE_SIZE
/ sizeof(FAT_DIR_ENTRY
))];
1004 MoveContext
->FirstCluster
= CurrentCluster
;
1005 MoveContext
->FileSize
= pDirEntry
->FileSize
;
1006 MoveContext
->CreationTime
= pDirEntry
->CreationTime
;
1007 MoveContext
->CreationDate
= pDirEntry
->CreationDate
;
1012 CcSetDirtyPinnedData(Context
, NULL
);
1013 CcUnpinData(Context
);
1016 /* In case of moving, don't delete data */
1017 if (MoveContext
== NULL
)
1019 while (CurrentCluster
&& CurrentCluster
!= 0xffffffff)
1021 GetNextCluster(DeviceExt
, CurrentCluster
, &NextCluster
);
1022 /* FIXME: check status */
1023 WriteCluster(DeviceExt
, CurrentCluster
, 0);
1024 CurrentCluster
= NextCluster
;
1028 return STATUS_SUCCESS
;
1032 * deleting an existing FAT entry
1036 IN PDEVICE_EXTENSION DeviceExt
,
1038 OUT PVFAT_MOVE_CONTEXT MoveContext
)
1040 ULONG CurrentCluster
= 0, NextCluster
;
1041 PVOID Context
= NULL
;
1042 LARGE_INTEGER Offset
;
1043 PFATX_DIR_ENTRY pDirEntry
;
1048 ASSERT(pFcb
->parentFcb
);
1049 ASSERT(vfatVolumeIsFatX(DeviceExt
));
1051 StartIndex
= pFcb
->startIndex
;
1053 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pFcb
->parentFcb
);
1054 if (!NT_SUCCESS(Status
))
1059 DPRINT("delEntry PathName \'%wZ\'\n", &pFcb
->PathNameU
);
1060 DPRINT("delete entry: %u\n", StartIndex
);
1061 Offset
.u
.HighPart
= 0;
1062 Offset
.u
.LowPart
= (StartIndex
* sizeof(FATX_DIR_ENTRY
) / PAGE_SIZE
) * PAGE_SIZE
;
1065 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, PAGE_SIZE
, PIN_WAIT
, &Context
, (PVOID
*)&pDirEntry
);
1067 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1069 DPRINT1("CcPinRead(Offset %x:%x, Length %d) failed\n", Offset
.u
.HighPart
, Offset
.u
.LowPart
, PAGE_SIZE
);
1070 _SEH2_YIELD(return _SEH2_GetExceptionCode());
1073 pDirEntry
= &pDirEntry
[StartIndex
% (PAGE_SIZE
/ sizeof(FATX_DIR_ENTRY
))];
1074 pDirEntry
->FilenameLength
= 0xe5;
1075 CurrentCluster
= vfatDirEntryGetFirstCluster(DeviceExt
,
1076 (PDIR_ENTRY
)pDirEntry
);
1078 /* In case of moving, save properties */
1079 if (MoveContext
!= NULL
)
1081 MoveContext
->FirstCluster
= CurrentCluster
;
1082 MoveContext
->FileSize
= pDirEntry
->FileSize
;
1083 MoveContext
->CreationTime
= pDirEntry
->CreationTime
;
1084 MoveContext
->CreationDate
= pDirEntry
->CreationDate
;
1087 CcSetDirtyPinnedData(Context
, NULL
);
1088 CcUnpinData(Context
);
1090 /* In case of moving, don't delete data */
1091 if (MoveContext
== NULL
)
1093 while (CurrentCluster
&& CurrentCluster
!= 0xffffffff)
1095 GetNextCluster(DeviceExt
, CurrentCluster
, &NextCluster
);
1096 /* FIXME: check status */
1097 WriteCluster(DeviceExt
, CurrentCluster
, 0);
1098 CurrentCluster
= NextCluster
;
1102 return STATUS_SUCCESS
;
1106 * move an existing FAT entry
1110 IN PDEVICE_EXTENSION DeviceExt
,
1112 IN PUNICODE_STRING FileName
,
1113 IN PVFATFCB ParentFcb
)
1117 VFAT_MOVE_CONTEXT MoveContext
;
1119 DPRINT("VfatMoveEntry(%p, %p, %wZ, %p)\n", DeviceExt
, pFcb
, FileName
, ParentFcb
);
1121 /* Delete old entry while keeping data */
1122 Status
= VfatDelEntry(DeviceExt
, pFcb
, &MoveContext
);
1123 if (!NT_SUCCESS(Status
))
1128 OldParent
= pFcb
->parentFcb
;
1129 CcFlushCache(&OldParent
->SectionObjectPointers
, NULL
, 0, NULL
);
1130 MoveContext
.InPlace
= (OldParent
== ParentFcb
);
1132 /* Add our new entry with our cluster */
1133 Status
= VfatAddEntry(DeviceExt
,
1137 (vfatFCBIsDirectory(pFcb
) ? FILE_DIRECTORY_FILE
: 0),
1141 CcFlushCache(&pFcb
->parentFcb
->SectionObjectPointers
, NULL
, 0, NULL
);
1146 extern BOOLEAN
FATXIsDirectoryEmpty(PDEVICE_EXTENSION DeviceExt
, PVFATFCB Fcb
);
1147 extern BOOLEAN
FATIsDirectoryEmpty(PDEVICE_EXTENSION DeviceExt
, PVFATFCB Fcb
);
1148 extern NTSTATUS
FATGetNextDirEntry(PVOID
*pContext
, PVOID
*pPage
, PVFATFCB pDirFcb
, PVFAT_DIRENTRY_CONTEXT DirContext
, BOOLEAN First
);
1149 extern NTSTATUS
FATXGetNextDirEntry(PVOID
*pContext
, PVOID
*pPage
, PVFATFCB pDirFcb
, PVFAT_DIRENTRY_CONTEXT DirContext
, BOOLEAN First
);
1151 VFAT_DISPATCH FatXDispatch
= {
1152 FATXIsDirectoryEmpty
, // .IsDirectoryEmpty
1153 FATXAddEntry
, // .AddEntry
1154 FATXDelEntry
, // .DelEntry
1155 FATXGetNextDirEntry
, // .GetNextDirEntry
1158 VFAT_DISPATCH FatDispatch
= {
1159 FATIsDirectoryEmpty
, // .IsDirectoryEmpty
1160 FATAddEntry
, // .AddEntry
1161 FATDelEntry
, // .DelEntry
1162 FATGetNextDirEntry
, // .GetNextDirEntry