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
));
42 if (!ExIsResourceAcquiredExclusive(&vcb
->DirResource
))
44 ExAcquireResourceExclusiveLite(&vcb
->DirResource
, TRUE
);
48 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
51 if (DebugFile
.Buffer
!= NULL
&& FsRtlIsNameInExpression(&DebugFile
, &fcb
->LongNameU
, FALSE
, NULL
))
53 DPRINT1("Attaching %p to %p (%d)\n", fcb
, fileObject
, fcb
->RefCount
);
57 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
60 ObDereferenceObject(fileObject
);
61 return STATUS_INSUFFICIENT_RESOURCES
;
63 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
65 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
66 fileObject
->FsContext
= fcb
;
67 fileObject
->FsContext2
= newCCB
;
68 fileObject
->Vpb
= vcb
->IoVPB
;
69 fcb
->FileObject
= fileObject
;
73 CcInitializeCacheMap(fileObject
,
74 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
76 &VfatGlobalData
->CacheMgrCallbacks
,
79 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
81 status
= _SEH2_GetExceptionCode();
82 fcb
->FileObject
= NULL
;
83 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
84 ObDereferenceObject(fileObject
);
89 vfatGrabFCB(vcb
, fcb
);
90 SetFlag(fcb
->Flags
, FCB_CACHE_INITIALIZED
);
94 ExReleaseResourceLite(&vcb
->DirResource
);
97 return STATUS_SUCCESS
;
101 * update an existing FAT entry
105 IN PDEVICE_EXTENSION DeviceExt
,
110 LARGE_INTEGER Offset
;
117 if (vfatVolumeIsFatX(DeviceExt
))
119 SizeDirEntry
= sizeof(FATX_DIR_ENTRY
);
120 dirIndex
= pFcb
->startIndex
;
124 SizeDirEntry
= sizeof(FAT_DIR_ENTRY
);
125 dirIndex
= pFcb
->dirIndex
;
128 DPRINT("updEntry dirIndex %u, PathName \'%wZ\'\n", dirIndex
, &pFcb
->PathNameU
);
130 if (vfatFCBIsRoot(pFcb
) || BooleanFlagOn(pFcb
->Flags
, FCB_IS_FAT
| FCB_IS_VOLUME
))
132 return STATUS_SUCCESS
;
135 ASSERT(pFcb
->parentFcb
);
137 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pFcb
->parentFcb
);
138 if (!NT_SUCCESS(Status
))
143 Offset
.u
.HighPart
= 0;
144 Offset
.u
.LowPart
= dirIndex
* SizeDirEntry
;
147 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, SizeDirEntry
, PIN_WAIT
, &Context
, (PVOID
*)&PinEntry
);
149 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
151 DPRINT1("Failed write to \'%wZ\'.\n", &pFcb
->parentFcb
->PathNameU
);
152 _SEH2_YIELD(return _SEH2_GetExceptionCode());
156 pFcb
->Flags
&= ~FCB_IS_DIRTY
;
157 RtlCopyMemory(PinEntry
, &pFcb
->entry
, SizeDirEntry
);
158 CcSetDirtyPinnedData(Context
, NULL
);
159 CcUnpinData(Context
);
160 return STATUS_SUCCESS
;
164 * rename an existing FAT entry
168 IN PDEVICE_EXTENSION DeviceExt
,
170 IN PUNICODE_STRING FileName
,
171 IN BOOLEAN CaseChangeOnly
)
175 PVOID Context
= NULL
;
176 LARGE_INTEGER Offset
;
177 PFATX_DIR_ENTRY pDirEntry
;
180 DPRINT("vfatRenameEntry(%p, %p, %wZ, %d)\n", DeviceExt
, pFcb
, FileName
, CaseChangeOnly
);
182 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pFcb
->parentFcb
);
183 if (!NT_SUCCESS(Status
))
188 if (vfatVolumeIsFatX(DeviceExt
))
190 VFAT_DIRENTRY_CONTEXT DirContext
;
192 /* Open associated dir entry */
193 StartIndex
= pFcb
->startIndex
;
194 Offset
.u
.HighPart
= 0;
195 Offset
.u
.LowPart
= (StartIndex
* sizeof(FATX_DIR_ENTRY
) / PAGE_SIZE
) * PAGE_SIZE
;
198 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, PAGE_SIZE
, PIN_WAIT
, &Context
, (PVOID
*)&pDirEntry
);
200 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
202 DPRINT1("CcPinRead(Offset %x:%x, Length %d) failed\n", Offset
.u
.HighPart
, Offset
.u
.LowPart
, PAGE_SIZE
);
203 _SEH2_YIELD(return _SEH2_GetExceptionCode());
207 pDirEntry
= &pDirEntry
[StartIndex
% (PAGE_SIZE
/ sizeof(FATX_DIR_ENTRY
))];
210 NameA
.Buffer
= (PCHAR
)pDirEntry
->Filename
;
212 NameA
.MaximumLength
= 42;
213 RtlUnicodeStringToOemString(&NameA
, FileName
, FALSE
);
214 pDirEntry
->FilenameLength
= (unsigned char)NameA
.Length
;
217 DirContext
.DeviceExt
= DeviceExt
;
218 DirContext
.ShortNameU
.Length
= 0;
219 DirContext
.ShortNameU
.MaximumLength
= 0;
220 DirContext
.ShortNameU
.Buffer
= NULL
;
221 DirContext
.LongNameU
= *FileName
;
222 DirContext
.DirEntry
.FatX
= *pDirEntry
;
224 CcSetDirtyPinnedData(Context
, NULL
);
225 CcUnpinData(Context
);
227 Status
= vfatUpdateFCB(DeviceExt
, pFcb
, &DirContext
, pFcb
->parentFcb
);
228 if (NT_SUCCESS(Status
))
230 CcFlushCache(&pFcb
->parentFcb
->SectionObjectPointers
, NULL
, 0, NULL
);
237 /* This we cannot handle properly, move file - would likely need love */
238 return VfatMoveEntry(DeviceExt
, pFcb
, FileName
, pFcb
->parentFcb
);
243 * try to find contiguous entries frees in directory,
244 * extend a directory if is necessary
248 IN PDEVICE_EXTENSION DeviceExt
,
253 LARGE_INTEGER FileOffset
;
254 ULONG i
, count
, size
, nbFree
= 0;
255 PDIR_ENTRY pFatEntry
= NULL
;
256 PVOID Context
= NULL
;
259 BOOLEAN IsFatX
= vfatVolumeIsFatX(DeviceExt
);
260 FileOffset
.QuadPart
= 0;
263 SizeDirEntry
= sizeof(FATX_DIR_ENTRY
);
265 SizeDirEntry
= sizeof(FAT_DIR_ENTRY
);
267 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pDirFcb
);
268 if (!NT_SUCCESS(Status
))
273 count
= pDirFcb
->RFCB
.FileSize
.u
.LowPart
/ SizeDirEntry
;
274 size
= DeviceExt
->FatInfo
.BytesPerCluster
/ SizeDirEntry
;
275 for (i
= 0; i
< count
; i
++, pFatEntry
= (PDIR_ENTRY
)((ULONG_PTR
)pFatEntry
+ SizeDirEntry
))
277 if (Context
== NULL
|| (i
% size
) == 0)
281 CcUnpinData(Context
);
285 CcPinRead(pDirFcb
->FileObject
, &FileOffset
, DeviceExt
->FatInfo
.BytesPerCluster
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
287 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
289 _SEH2_YIELD(return FALSE
);
293 FileOffset
.u
.LowPart
+= DeviceExt
->FatInfo
.BytesPerCluster
;
295 if (ENTRY_END(IsFatX
, pFatEntry
))
299 if (ENTRY_DELETED(IsFatX
, pFatEntry
))
307 if (nbFree
== nbSlots
)
314 CcUnpinData(Context
);
317 if (nbFree
== nbSlots
)
319 /* found enough contiguous free slots */
320 *start
= i
- nbSlots
+ 1;
325 if (*start
+ nbSlots
> count
)
327 LARGE_INTEGER AllocationSize
;
328 /* extend the directory */
329 if (vfatFCBIsRoot(pDirFcb
) && DeviceExt
->FatInfo
.FatType
!= FAT32
)
331 /* We can't extend a root directory on a FAT12/FAT16/FATX partition */
334 AllocationSize
.QuadPart
= pDirFcb
->RFCB
.FileSize
.u
.LowPart
+ DeviceExt
->FatInfo
.BytesPerCluster
;
335 Status
= VfatSetAllocationSizeInformation(pDirFcb
->FileObject
, pDirFcb
,
336 DeviceExt
, &AllocationSize
);
337 if (!NT_SUCCESS(Status
))
341 /* clear the new dir cluster */
342 FileOffset
.u
.LowPart
= (ULONG
)(pDirFcb
->RFCB
.FileSize
.QuadPart
-
343 DeviceExt
->FatInfo
.BytesPerCluster
);
346 CcPinRead(pDirFcb
->FileObject
, &FileOffset
, DeviceExt
->FatInfo
.BytesPerCluster
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
348 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
350 _SEH2_YIELD(return FALSE
);
355 memset(pFatEntry
, 0xff, DeviceExt
->FatInfo
.BytesPerCluster
);
357 RtlZeroMemory(pFatEntry
, DeviceExt
->FatInfo
.BytesPerCluster
);
359 else if (*start
+ nbSlots
< count
)
361 /* clear the entry after the last new entry */
362 FileOffset
.u
.LowPart
= (*start
+ nbSlots
) * SizeDirEntry
;
365 CcPinRead(pDirFcb
->FileObject
, &FileOffset
, SizeDirEntry
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
367 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
369 _SEH2_YIELD(return FALSE
);
374 memset(pFatEntry
, 0xff, SizeDirEntry
);
376 RtlZeroMemory(pFatEntry
, SizeDirEntry
);
380 CcSetDirtyPinnedData(Context
, NULL
);
381 CcUnpinData(Context
);
384 DPRINT("nbSlots %u nbFree %u, entry number %u\n", nbSlots
, nbFree
, *start
);
389 create a new FAT entry
393 IN PDEVICE_EXTENSION DeviceExt
,
394 IN PUNICODE_STRING NameU
,
396 IN PVFATFCB ParentFcb
,
397 IN ULONG RequestedOptions
,
399 IN PVFAT_MOVE_CONTEXT MoveContext
)
401 PVOID Context
= NULL
;
402 PFAT_DIR_ENTRY pFatEntry
;
404 USHORT nbSlots
= 0, j
;
406 BOOLEAN needTilde
= FALSE
, needLong
= FALSE
;
407 BOOLEAN BaseAllLower
, BaseAllUpper
;
408 BOOLEAN ExtensionAllLower
, ExtensionAllUpper
;
412 ULONG CurrentCluster
;
413 LARGE_INTEGER SystemTime
, FileOffset
;
414 NTSTATUS Status
= STATUS_SUCCESS
;
423 VFAT_DIRENTRY_CONTEXT DirContext
;
424 WCHAR LongNameBuffer
[LONGNAME_MAX_LENGTH
+ 1];
425 WCHAR ShortNameBuffer
[13];
427 DPRINT("addEntry: Name='%wZ', Dir='%wZ'\n", NameU
, &ParentFcb
->PathNameU
);
429 DirContext
.LongNameU
= *NameU
;
430 IsDirectory
= BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
);
432 /* nb of entry needed for long name+normal entry */
433 nbSlots
= (DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) + 12) / 13 + 1;
434 DPRINT("NameLen= %u, nbSlots =%u\n", DirContext
.LongNameU
.Length
/ sizeof(WCHAR
), nbSlots
);
435 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, (nbSlots
- 1) * sizeof(FAT_DIR_ENTRY
), TAG_VFAT
);
438 return STATUS_INSUFFICIENT_RESOURCES
;
440 RtlZeroMemory(Buffer
, (nbSlots
- 1) * sizeof(FAT_DIR_ENTRY
));
441 pSlots
= (slot
*) Buffer
;
443 NameA
.Buffer
= aName
;
445 NameA
.MaximumLength
= sizeof(aName
);
447 DirContext
.DeviceExt
= DeviceExt
;
448 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
449 DirContext
.ShortNameU
.Length
= 0;
450 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
452 RtlZeroMemory(&DirContext
.DirEntry
.Fat
, sizeof(FAT_DIR_ENTRY
));
454 IsNameLegal
= RtlIsNameLegalDOS8Dot3(&DirContext
.LongNameU
, &NameA
, &SpacesFound
);
456 if (!IsNameLegal
|| SpacesFound
)
458 GENERATE_NAME_CONTEXT NameContext
;
459 VFAT_DIRENTRY_CONTEXT SearchContext
;
460 WCHAR ShortSearchName
[13];
463 RtlZeroMemory(&NameContext
, sizeof(GENERATE_NAME_CONTEXT
));
464 SearchContext
.DeviceExt
= DeviceExt
;
465 SearchContext
.LongNameU
.Buffer
= LongNameBuffer
;
466 SearchContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
467 SearchContext
.ShortNameU
.Buffer
= ShortSearchName
;
468 SearchContext
.ShortNameU
.MaximumLength
= sizeof(ShortSearchName
);
470 for (i
= 0; i
< 100; i
++)
472 RtlGenerate8dot3Name(&DirContext
.LongNameU
, FALSE
, &NameContext
, &DirContext
.ShortNameU
);
473 DirContext
.ShortNameU
.Buffer
[DirContext
.ShortNameU
.Length
/ sizeof(WCHAR
)] = 0;
474 SearchContext
.DirIndex
= 0;
475 Status
= FindFile(DeviceExt
, ParentFcb
, &DirContext
.ShortNameU
, &SearchContext
, TRUE
);
476 if (!NT_SUCCESS(Status
))
480 else if (MoveContext
)
483 if (strncmp((char *)SearchContext
.DirEntry
.Fat
.ShortName
, (char *)(*Fcb
)->entry
.Fat
.ShortName
, 11) == 0)
485 if (MoveContext
->InPlace
)
487 ASSERT(SearchContext
.DirEntry
.Fat
.FileSize
== MoveContext
->FileSize
);
493 if (i
== 100) /* FIXME : what to do after this ? */
495 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
496 return STATUS_UNSUCCESSFUL
;
498 IsNameLegal
= RtlIsNameLegalDOS8Dot3(&DirContext
.ShortNameU
, &NameA
, &SpacesFound
);
502 BaseAllLower
= BaseAllUpper
= TRUE
;
503 ExtensionAllLower
= ExtensionAllUpper
= TRUE
;
505 for (i
= 0; i
< DirContext
.LongNameU
.Length
/ sizeof(WCHAR
); i
++)
507 c
= DirContext
.LongNameU
.Buffer
[i
];
508 if (c
>= L
'A' && c
<= L
'Z')
511 ExtensionAllLower
= FALSE
;
513 BaseAllLower
= FALSE
;
515 else if (c
>= L
'a' && c
<= L
'z')
518 ExtensionAllUpper
= FALSE
;
520 BaseAllUpper
= FALSE
;
534 if ((!BaseAllLower
&& !BaseAllUpper
) ||
535 (!ExtensionAllLower
&& !ExtensionAllUpper
))
540 RtlUpcaseUnicodeString(&DirContext
.ShortNameU
, &DirContext
.LongNameU
, FALSE
);
541 DirContext
.ShortNameU
.Buffer
[DirContext
.ShortNameU
.Length
/ sizeof(WCHAR
)] = 0;
543 aName
[NameA
.Length
] = 0;
544 DPRINT("'%s', '%wZ', needTilde=%u, needLong=%u\n",
545 aName
, &DirContext
.LongNameU
, needTilde
, needLong
);
546 memset(DirContext
.DirEntry
.Fat
.ShortName
, ' ', 11);
547 for (i
= 0; i
< 8 && aName
[i
] && aName
[i
] != '.'; i
++)
549 DirContext
.DirEntry
.Fat
.Filename
[i
] = aName
[i
];
554 for (j
= 0; j
< 3 && aName
[i
]; j
++, i
++)
556 DirContext
.DirEntry
.Fat
.Ext
[j
] = aName
[i
];
559 if (DirContext
.DirEntry
.Fat
.Filename
[0] == 0xe5)
561 DirContext
.DirEntry
.Fat
.Filename
[0] = 0x05;
566 RtlCopyMemory(LongNameBuffer
, DirContext
.LongNameU
.Buffer
, DirContext
.LongNameU
.Length
);
567 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
568 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
569 DirContext
.LongNameU
.Buffer
[DirContext
.LongNameU
.Length
/ sizeof(WCHAR
)] = 0;
570 memset(DirContext
.LongNameU
.Buffer
+ DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) + 1, 0xff,
571 DirContext
.LongNameU
.MaximumLength
- DirContext
.LongNameU
.Length
- sizeof(WCHAR
));
576 if (BaseAllLower
&& !BaseAllUpper
)
578 DirContext
.DirEntry
.Fat
.lCase
|= VFAT_CASE_LOWER_BASE
;
580 if (ExtensionAllLower
&& !ExtensionAllUpper
)
582 DirContext
.DirEntry
.Fat
.lCase
|= VFAT_CASE_LOWER_EXT
;
586 DPRINT ("dos name=%11.11s\n", DirContext
.DirEntry
.Fat
.Filename
);
589 DirContext
.DirEntry
.Fat
.Attrib
= ReqAttr
;
592 DirContext
.DirEntry
.Fat
.Attrib
|= FILE_ATTRIBUTE_DIRECTORY
;
594 /* set dates and times */
595 KeQuerySystemTime(&SystemTime
);
596 FsdSystemTimeToDosDateTime(DeviceExt
, &SystemTime
, &DirContext
.DirEntry
.Fat
.CreationDate
,
597 &DirContext
.DirEntry
.Fat
.CreationTime
);
598 DirContext
.DirEntry
.Fat
.UpdateDate
= DirContext
.DirEntry
.Fat
.CreationDate
;
599 DirContext
.DirEntry
.Fat
.UpdateTime
= DirContext
.DirEntry
.Fat
.CreationTime
;
600 DirContext
.DirEntry
.Fat
.AccessDate
= DirContext
.DirEntry
.Fat
.CreationDate
;
601 /* If it's moving, preserve creation time and file size */
602 if (MoveContext
!= NULL
)
604 DirContext
.DirEntry
.Fat
.CreationDate
= MoveContext
->CreationDate
;
605 DirContext
.DirEntry
.Fat
.CreationTime
= MoveContext
->CreationTime
;
606 DirContext
.DirEntry
.Fat
.FileSize
= MoveContext
->FileSize
;
611 /* calculate checksum for 8.3 name */
612 for (pSlots
[0].alias_checksum
= 0, i
= 0; i
< 11; i
++)
614 pSlots
[0].alias_checksum
= (((pSlots
[0].alias_checksum
& 1) << 7
615 | ((pSlots
[0].alias_checksum
& 0xfe) >> 1))
616 + DirContext
.DirEntry
.Fat
.ShortName
[i
]);
618 /* construct slots and entry */
619 for (i
= nbSlots
- 2; i
>= 0; i
--)
621 DPRINT("construct slot %d\n", i
);
622 pSlots
[i
].attr
= 0xf;
625 pSlots
[i
].id
= (unsigned char)(nbSlots
- i
- 1);
629 pSlots
[i
].id
= (unsigned char)(nbSlots
- i
- 1 + 0x40);
631 pSlots
[i
].alias_checksum
= pSlots
[0].alias_checksum
;
632 RtlCopyMemory(pSlots
[i
].name0_4
, DirContext
.LongNameU
.Buffer
+ (nbSlots
- i
- 2) * 13, 10);
633 RtlCopyMemory(pSlots
[i
].name5_10
, DirContext
.LongNameU
.Buffer
+ (nbSlots
- i
- 2) * 13 + 5, 12);
634 RtlCopyMemory(pSlots
[i
].name11_12
, DirContext
.LongNameU
.Buffer
+ (nbSlots
- i
- 2) * 13 + 11, 4);
637 /* try to find nbSlots contiguous entries frees in directory */
638 if (!vfatFindDirSpace(DeviceExt
, ParentFcb
, nbSlots
, &DirContext
.StartIndex
))
640 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
641 return STATUS_DISK_FULL
;
643 DirContext
.DirIndex
= DirContext
.StartIndex
+ nbSlots
- 1;
646 /* If we aren't moving, use next */
647 if (MoveContext
== NULL
)
650 Status
= NextCluster(DeviceExt
, 0, &CurrentCluster
, TRUE
);
651 if (CurrentCluster
== 0xffffffff || !NT_SUCCESS(Status
))
653 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
654 if (!NT_SUCCESS(Status
))
658 return STATUS_DISK_FULL
;
663 CurrentCluster
= MoveContext
->FirstCluster
;
666 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
668 DirContext
.DirEntry
.Fat
.FirstClusterHigh
= (unsigned short)(CurrentCluster
>> 16);
670 DirContext
.DirEntry
.Fat
.FirstCluster
= (unsigned short)CurrentCluster
;
672 else if (MoveContext
!= NULL
)
674 CurrentCluster
= MoveContext
->FirstCluster
;
676 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
678 DirContext
.DirEntry
.Fat
.FirstClusterHigh
= (unsigned short)(CurrentCluster
>> 16);
680 DirContext
.DirEntry
.Fat
.FirstCluster
= (unsigned short)CurrentCluster
;
683 /* No need to init cache here, vfatFindDirSpace() will have done it for us */
685 i
= DeviceExt
->FatInfo
.BytesPerCluster
/ sizeof(FAT_DIR_ENTRY
);
686 FileOffset
.u
.HighPart
= 0;
687 FileOffset
.u
.LowPart
= DirContext
.StartIndex
* sizeof(FAT_DIR_ENTRY
);
688 if (DirContext
.StartIndex
/ i
== DirContext
.DirIndex
/ i
)
693 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, nbSlots
* sizeof(FAT_DIR_ENTRY
), PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
695 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
697 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
698 _SEH2_YIELD(return _SEH2_GetExceptionCode());
704 RtlCopyMemory(pFatEntry
, Buffer
, (nbSlots
- 1) * sizeof(FAT_DIR_ENTRY
));
706 RtlCopyMemory(pFatEntry
+ (nbSlots
- 1), &DirContext
.DirEntry
.Fat
, sizeof(FAT_DIR_ENTRY
));
711 size
= DeviceExt
->FatInfo
.BytesPerCluster
-
712 (DirContext
.StartIndex
* sizeof(FAT_DIR_ENTRY
)) % DeviceExt
->FatInfo
.BytesPerCluster
;
713 i
= size
/ sizeof(FAT_DIR_ENTRY
);
716 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, size
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
718 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
720 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
721 _SEH2_YIELD(return _SEH2_GetExceptionCode());
724 RtlCopyMemory(pFatEntry
, Buffer
, size
);
725 CcSetDirtyPinnedData(Context
, NULL
);
726 CcUnpinData(Context
);
727 FileOffset
.u
.LowPart
+= size
;
730 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, nbSlots
* sizeof(FAT_DIR_ENTRY
) - size
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
732 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
734 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
735 _SEH2_YIELD(return _SEH2_GetExceptionCode());
740 RtlCopyMemory(pFatEntry
, (PVOID
)(Buffer
+ size
), (nbSlots
- 1 - i
) * sizeof(FAT_DIR_ENTRY
));
742 RtlCopyMemory(pFatEntry
+ nbSlots
- 1 - i
, &DirContext
.DirEntry
.Fat
, sizeof(FAT_DIR_ENTRY
));
744 CcSetDirtyPinnedData(Context
, NULL
);
745 CcUnpinData(Context
);
747 if (MoveContext
!= NULL
)
749 /* We're modifying an existing FCB - likely rename/move */
750 Status
= vfatUpdateFCB(DeviceExt
, *Fcb
, &DirContext
, ParentFcb
);
754 Status
= vfatMakeFCBFromDirEntry(DeviceExt
, ParentFcb
, &DirContext
, Fcb
);
756 if (!NT_SUCCESS(Status
))
758 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
762 DPRINT("new : entry=%11.11s\n", (*Fcb
)->entry
.Fat
.Filename
);
763 DPRINT("new : entry=%11.11s\n", DirContext
.DirEntry
.Fat
.Filename
);
767 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, (*Fcb
));
768 if (!NT_SUCCESS(Status
))
773 FileOffset
.QuadPart
= 0;
776 CcPinRead((*Fcb
)->FileObject
, &FileOffset
, DeviceExt
->FatInfo
.BytesPerCluster
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
778 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
780 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
781 _SEH2_YIELD(return _SEH2_GetExceptionCode());
784 /* clear the new directory cluster if not moving */
785 if (MoveContext
== NULL
)
787 RtlZeroMemory(pFatEntry
, DeviceExt
->FatInfo
.BytesPerCluster
);
788 /* create '.' and '..' */
789 RtlCopyMemory(&pFatEntry
[0].Attrib
, &DirContext
.DirEntry
.Fat
.Attrib
, sizeof(FAT_DIR_ENTRY
) - 11);
790 RtlCopyMemory(pFatEntry
[0].ShortName
, ". ", 11);
791 RtlCopyMemory(&pFatEntry
[1].Attrib
, &DirContext
.DirEntry
.Fat
.Attrib
, sizeof(FAT_DIR_ENTRY
) - 11);
792 RtlCopyMemory(pFatEntry
[1].ShortName
, ".. ", 11);
795 pFatEntry
[1].FirstCluster
= ParentFcb
->entry
.Fat
.FirstCluster
;
796 pFatEntry
[1].FirstClusterHigh
= ParentFcb
->entry
.Fat
.FirstClusterHigh
;
797 if (vfatFCBIsRoot(ParentFcb
))
799 pFatEntry
[1].FirstCluster
= 0;
800 pFatEntry
[1].FirstClusterHigh
= 0;
802 CcSetDirtyPinnedData(Context
, NULL
);
803 CcUnpinData(Context
);
805 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
806 DPRINT("addentry ok\n");
807 return STATUS_SUCCESS
;
811 create a new FAT entry
815 IN PDEVICE_EXTENSION DeviceExt
,
816 IN PUNICODE_STRING NameU
,
818 IN PVFATFCB ParentFcb
,
819 IN ULONG RequestedOptions
,
821 IN PVFAT_MOVE_CONTEXT MoveContext
)
823 PVOID Context
= NULL
;
824 LARGE_INTEGER SystemTime
, FileOffset
;
826 VFAT_DIRENTRY_CONTEXT DirContext
;
827 PFATX_DIR_ENTRY pFatXDirEntry
;
830 DPRINT("addEntry: Name='%wZ', Dir='%wZ'\n", NameU
, &ParentFcb
->PathNameU
);
832 DirContext
.LongNameU
= *NameU
;
834 if (DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) > 42)
837 return STATUS_NAME_TOO_LONG
;
840 /* try to find 1 entry free in directory */
841 if (!vfatFindDirSpace(DeviceExt
, ParentFcb
, 1, &DirContext
.StartIndex
))
843 return STATUS_DISK_FULL
;
845 Index
= DirContext
.DirIndex
= DirContext
.StartIndex
;
846 if (!vfatFCBIsRoot(ParentFcb
))
848 DirContext
.DirIndex
+= 2;
849 DirContext
.StartIndex
+= 2;
852 DirContext
.ShortNameU
.Buffer
= 0;
853 DirContext
.ShortNameU
.Length
= 0;
854 DirContext
.ShortNameU
.MaximumLength
= 0;
855 DirContext
.DeviceExt
= DeviceExt
;
856 RtlZeroMemory(&DirContext
.DirEntry
.FatX
, sizeof(FATX_DIR_ENTRY
));
857 memset(DirContext
.DirEntry
.FatX
.Filename
, 0xff, 42);
858 /* Use cluster, if moving */
859 if (MoveContext
!= NULL
)
861 DirContext
.DirEntry
.FatX
.FirstCluster
= MoveContext
->FirstCluster
;
865 DirContext
.DirEntry
.FatX
.FirstCluster
= 0;
867 DirContext
.DirEntry
.FatX
.FileSize
= 0;
870 NameA
.Buffer
= (PCHAR
)DirContext
.DirEntry
.FatX
.Filename
;
872 NameA
.MaximumLength
= 42;
873 RtlUnicodeStringToOemString(&NameA
, &DirContext
.LongNameU
, FALSE
);
874 DirContext
.DirEntry
.FatX
.FilenameLength
= (unsigned char)NameA
.Length
;
877 DirContext
.DirEntry
.FatX
.Attrib
= ReqAttr
;
878 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
))
880 DirContext
.DirEntry
.FatX
.Attrib
|= FILE_ATTRIBUTE_DIRECTORY
;
883 /* set dates and times */
884 KeQuerySystemTime(&SystemTime
);
885 FsdSystemTimeToDosDateTime(DeviceExt
, &SystemTime
, &DirContext
.DirEntry
.FatX
.CreationDate
,
886 &DirContext
.DirEntry
.FatX
.CreationTime
);
887 DirContext
.DirEntry
.FatX
.UpdateDate
= DirContext
.DirEntry
.FatX
.CreationDate
;
888 DirContext
.DirEntry
.FatX
.UpdateTime
= DirContext
.DirEntry
.FatX
.CreationTime
;
889 DirContext
.DirEntry
.FatX
.AccessDate
= DirContext
.DirEntry
.FatX
.CreationDate
;
890 DirContext
.DirEntry
.FatX
.AccessTime
= DirContext
.DirEntry
.FatX
.CreationTime
;
891 /* If it's moving, preserve creation time and file size */
892 if (MoveContext
!= NULL
)
894 DirContext
.DirEntry
.FatX
.CreationDate
= MoveContext
->CreationDate
;
895 DirContext
.DirEntry
.FatX
.CreationTime
= MoveContext
->CreationTime
;
896 DirContext
.DirEntry
.FatX
.FileSize
= MoveContext
->FileSize
;
899 /* No need to init cache here, vfatFindDirSpace() will have done it for us */
901 /* add entry into parent directory */
902 FileOffset
.u
.HighPart
= 0;
903 FileOffset
.u
.LowPart
= Index
* sizeof(FATX_DIR_ENTRY
);
906 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, sizeof(FATX_DIR_ENTRY
), PIN_WAIT
, &Context
, (PVOID
*)&pFatXDirEntry
);
908 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
910 _SEH2_YIELD(return _SEH2_GetExceptionCode());
913 RtlCopyMemory(pFatXDirEntry
, &DirContext
.DirEntry
.FatX
, sizeof(FATX_DIR_ENTRY
));
914 CcSetDirtyPinnedData(Context
, NULL
);
915 CcUnpinData(Context
);
917 if (MoveContext
!= NULL
)
919 /* We're modifying an existing FCB - likely rename/move */
920 /* FIXME: check status */
921 vfatUpdateFCB(DeviceExt
, *Fcb
, &DirContext
, ParentFcb
);
925 /* FIXME: check status */
926 vfatMakeFCBFromDirEntry(DeviceExt
, ParentFcb
, &DirContext
, Fcb
);
929 DPRINT("addentry ok\n");
930 return STATUS_SUCCESS
;
934 * deleting an existing FAT entry
938 IN PDEVICE_EXTENSION DeviceExt
,
940 OUT PVFAT_MOVE_CONTEXT MoveContext
)
942 ULONG CurrentCluster
= 0, NextCluster
, i
;
943 PVOID Context
= NULL
;
944 LARGE_INTEGER Offset
;
945 PFAT_DIR_ENTRY pDirEntry
= NULL
;
949 ASSERT(pFcb
->parentFcb
);
951 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pFcb
->parentFcb
);
952 if (!NT_SUCCESS(Status
))
957 DPRINT("delEntry PathName \'%wZ\'\n", &pFcb
->PathNameU
);
958 DPRINT("delete entry: %u to %u\n", pFcb
->startIndex
, pFcb
->dirIndex
);
959 Offset
.u
.HighPart
= 0;
960 for (i
= pFcb
->startIndex
; i
<= pFcb
->dirIndex
; i
++)
962 if (Context
== NULL
|| ((i
* sizeof(FAT_DIR_ENTRY
)) % PAGE_SIZE
) == 0)
966 CcSetDirtyPinnedData(Context
, NULL
);
967 CcUnpinData(Context
);
969 Offset
.u
.LowPart
= (i
* sizeof(FAT_DIR_ENTRY
) / PAGE_SIZE
) * PAGE_SIZE
;
972 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, PAGE_SIZE
, PIN_WAIT
, &Context
, (PVOID
*)&pDirEntry
);
974 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
976 _SEH2_YIELD(return _SEH2_GetExceptionCode());
980 pDirEntry
[i
% (PAGE_SIZE
/ sizeof(FAT_DIR_ENTRY
))].Filename
[0] = 0xe5;
981 if (i
== pFcb
->dirIndex
)
984 vfatDirEntryGetFirstCluster(DeviceExt
,
985 (PDIR_ENTRY
)&pDirEntry
[i
% (PAGE_SIZE
/ sizeof(FAT_DIR_ENTRY
))]);
989 /* In case of moving, save properties */
990 if (MoveContext
!= NULL
)
992 pDirEntry
= &pDirEntry
[pFcb
->dirIndex
% (PAGE_SIZE
/ sizeof(FAT_DIR_ENTRY
))];
993 MoveContext
->FirstCluster
= CurrentCluster
;
994 MoveContext
->FileSize
= pDirEntry
->FileSize
;
995 MoveContext
->CreationTime
= pDirEntry
->CreationTime
;
996 MoveContext
->CreationDate
= pDirEntry
->CreationDate
;
1001 CcSetDirtyPinnedData(Context
, NULL
);
1002 CcUnpinData(Context
);
1005 /* In case of moving, don't delete data */
1006 if (MoveContext
== NULL
)
1008 while (CurrentCluster
&& CurrentCluster
!= 0xffffffff)
1010 GetNextCluster(DeviceExt
, CurrentCluster
, &NextCluster
);
1011 /* FIXME: check status */
1012 WriteCluster(DeviceExt
, CurrentCluster
, 0);
1013 CurrentCluster
= NextCluster
;
1017 return STATUS_SUCCESS
;
1021 * deleting an existing FAT entry
1025 IN PDEVICE_EXTENSION DeviceExt
,
1027 OUT PVFAT_MOVE_CONTEXT MoveContext
)
1029 ULONG CurrentCluster
= 0, NextCluster
;
1030 PVOID Context
= NULL
;
1031 LARGE_INTEGER Offset
;
1032 PFATX_DIR_ENTRY pDirEntry
;
1037 ASSERT(pFcb
->parentFcb
);
1038 ASSERT(vfatVolumeIsFatX(DeviceExt
));
1040 StartIndex
= pFcb
->startIndex
;
1042 Status
= vfatFCBInitializeCacheFromVolume(DeviceExt
, pFcb
->parentFcb
);
1043 if (!NT_SUCCESS(Status
))
1048 DPRINT("delEntry PathName \'%wZ\'\n", &pFcb
->PathNameU
);
1049 DPRINT("delete entry: %u\n", StartIndex
);
1050 Offset
.u
.HighPart
= 0;
1051 Offset
.u
.LowPart
= (StartIndex
* sizeof(FATX_DIR_ENTRY
) / PAGE_SIZE
) * PAGE_SIZE
;
1054 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, PAGE_SIZE
, PIN_WAIT
, &Context
, (PVOID
*)&pDirEntry
);
1056 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1058 DPRINT1("CcPinRead(Offset %x:%x, Length %d) failed\n", Offset
.u
.HighPart
, Offset
.u
.LowPart
, PAGE_SIZE
);
1059 _SEH2_YIELD(return _SEH2_GetExceptionCode());
1062 pDirEntry
= &pDirEntry
[StartIndex
% (PAGE_SIZE
/ sizeof(FATX_DIR_ENTRY
))];
1063 pDirEntry
->FilenameLength
= 0xe5;
1064 CurrentCluster
= vfatDirEntryGetFirstCluster(DeviceExt
,
1065 (PDIR_ENTRY
)pDirEntry
);
1067 /* In case of moving, save properties */
1068 if (MoveContext
!= NULL
)
1070 MoveContext
->FirstCluster
= CurrentCluster
;
1071 MoveContext
->FileSize
= pDirEntry
->FileSize
;
1072 MoveContext
->CreationTime
= pDirEntry
->CreationTime
;
1073 MoveContext
->CreationDate
= pDirEntry
->CreationDate
;
1076 CcSetDirtyPinnedData(Context
, NULL
);
1077 CcUnpinData(Context
);
1079 /* In case of moving, don't delete data */
1080 if (MoveContext
== NULL
)
1082 while (CurrentCluster
&& CurrentCluster
!= 0xffffffff)
1084 GetNextCluster(DeviceExt
, CurrentCluster
, &NextCluster
);
1085 /* FIXME: check status */
1086 WriteCluster(DeviceExt
, CurrentCluster
, 0);
1087 CurrentCluster
= NextCluster
;
1091 return STATUS_SUCCESS
;
1095 * move an existing FAT entry
1099 IN PDEVICE_EXTENSION DeviceExt
,
1101 IN PUNICODE_STRING FileName
,
1102 IN PVFATFCB ParentFcb
)
1106 VFAT_MOVE_CONTEXT MoveContext
;
1108 DPRINT("VfatMoveEntry(%p, %p, %wZ, %p)\n", DeviceExt
, pFcb
, FileName
, ParentFcb
);
1110 /* Delete old entry while keeping data */
1111 Status
= VfatDelEntry(DeviceExt
, pFcb
, &MoveContext
);
1112 if (!NT_SUCCESS(Status
))
1117 OldParent
= pFcb
->parentFcb
;
1118 CcFlushCache(&OldParent
->SectionObjectPointers
, NULL
, 0, NULL
);
1119 MoveContext
.InPlace
= (OldParent
== ParentFcb
);
1121 /* Add our new entry with our cluster */
1122 Status
= VfatAddEntry(DeviceExt
,
1126 (vfatFCBIsDirectory(pFcb
) ? FILE_DIRECTORY_FILE
: 0),
1130 CcFlushCache(&pFcb
->parentFcb
->SectionObjectPointers
, NULL
, 0, NULL
);
1135 extern BOOLEAN
FATXIsDirectoryEmpty(PDEVICE_EXTENSION DeviceExt
, PVFATFCB Fcb
);
1136 extern BOOLEAN
FATIsDirectoryEmpty(PDEVICE_EXTENSION DeviceExt
, PVFATFCB Fcb
);
1137 extern NTSTATUS
FATGetNextDirEntry(PVOID
*pContext
, PVOID
*pPage
, PVFATFCB pDirFcb
, PVFAT_DIRENTRY_CONTEXT DirContext
, BOOLEAN First
);
1138 extern NTSTATUS
FATXGetNextDirEntry(PVOID
*pContext
, PVOID
*pPage
, PVFATFCB pDirFcb
, PVFAT_DIRENTRY_CONTEXT DirContext
, BOOLEAN First
);
1140 VFAT_DISPATCH FatXDispatch
= {
1141 FATXIsDirectoryEmpty
, // .IsDirectoryEmpty
1142 FATXAddEntry
, // .AddEntry
1143 FATXDelEntry
, // .DelEntry
1144 FATXGetNextDirEntry
, // .GetNextDirEntry
1147 VFAT_DISPATCH FatDispatch
= {
1148 FATIsDirectoryEmpty
, // .IsDirectoryEmpty
1149 FATAddEntry
, // .AddEntry
1150 FATDelEntry
, // .DelEntry
1151 FATGetNextDirEntry
, // .GetNextDirEntry