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 * update an existing FAT entry
34 if (pFcb
->Flags
& FCB_IS_FATX_ENTRY
)
36 SizeDirEntry
= sizeof(FATX_DIR_ENTRY
);
37 dirIndex
= pFcb
->startIndex
;
41 SizeDirEntry
= sizeof(FAT_DIR_ENTRY
);
42 dirIndex
= pFcb
->dirIndex
;
45 DPRINT("updEntry dirIndex %u, PathName \'%wZ\'\n", dirIndex
, &pFcb
->PathNameU
);
47 if (vfatFCBIsRoot(pFcb
) || (pFcb
->Flags
& (FCB_IS_FAT
|FCB_IS_VOLUME
)))
49 return STATUS_SUCCESS
;
52 ASSERT(pFcb
->parentFcb
);
54 Offset
.u
.HighPart
= 0;
55 Offset
.u
.LowPart
= dirIndex
* SizeDirEntry
;
58 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, SizeDirEntry
, PIN_WAIT
, &Context
, (PVOID
*)&PinEntry
);
60 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
62 DPRINT1("Failed write to \'%wZ\'.\n", &pFcb
->parentFcb
->PathNameU
);
63 _SEH2_YIELD(return _SEH2_GetExceptionCode());
67 pFcb
->Flags
&= ~FCB_IS_DIRTY
;
68 RtlCopyMemory(PinEntry
, &pFcb
->entry
, SizeDirEntry
);
69 CcSetDirtyPinnedData(Context
, NULL
);
71 return STATUS_SUCCESS
;
75 * rename an existing FAT entry
79 IN PDEVICE_EXTENSION DeviceExt
,
81 IN PUNICODE_STRING FileName
,
82 IN BOOLEAN CaseChangeOnly
)
88 PFATX_DIR_ENTRY pDirEntry
;
91 DPRINT("vfatRenameEntry(%p, %p, %wZ, %d)\n", DeviceExt
, pFcb
, FileName
, CaseChangeOnly
);
93 if (pFcb
->Flags
& FCB_IS_FATX_ENTRY
)
95 VFAT_DIRENTRY_CONTEXT DirContext
;
97 /* Open associated dir entry */
98 StartIndex
= pFcb
->startIndex
;
99 Offset
.u
.HighPart
= 0;
100 Offset
.u
.LowPart
= (StartIndex
* sizeof(FATX_DIR_ENTRY
) / PAGE_SIZE
) * PAGE_SIZE
;
103 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, PAGE_SIZE
, PIN_WAIT
, &Context
, (PVOID
*)&pDirEntry
);
105 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
107 DPRINT1("CcPinRead(Offset %x:%x, Length %d) failed\n", Offset
.u
.HighPart
, Offset
.u
.LowPart
, PAGE_SIZE
);
108 _SEH2_YIELD(return _SEH2_GetExceptionCode());
112 pDirEntry
= &pDirEntry
[StartIndex
% (PAGE_SIZE
/ sizeof(FATX_DIR_ENTRY
))];
115 NameA
.Buffer
= (PCHAR
)pDirEntry
->Filename
;
117 NameA
.MaximumLength
= 42;
118 RtlUnicodeStringToOemString(&NameA
, FileName
, FALSE
);
119 pDirEntry
->FilenameLength
= (unsigned char)NameA
.Length
;
122 DirContext
.ShortNameU
.Length
= 0;
123 DirContext
.ShortNameU
.MaximumLength
= 0;
124 DirContext
.ShortNameU
.Buffer
= NULL
;
125 DirContext
.LongNameU
= *FileName
;
126 DirContext
.DirEntry
.FatX
= *pDirEntry
;
128 CcSetDirtyPinnedData(Context
, NULL
);
129 CcUnpinData(Context
);
131 Status
= vfatUpdateFCB(DeviceExt
, pFcb
, &DirContext
, pFcb
->parentFcb
);
132 if (NT_SUCCESS(Status
))
134 CcPurgeCacheSection(&pFcb
->parentFcb
->SectionObjectPointers
, NULL
, 0, FALSE
);
141 /* This we cannot handle properly, move file - would likely need love */
142 return VfatMoveEntry(DeviceExt
, pFcb
, FileName
, pFcb
->parentFcb
);
147 * try to find contiguous entries frees in directory,
148 * extend a directory if is neccesary
152 IN PDEVICE_EXTENSION DeviceExt
,
157 LARGE_INTEGER FileOffset
;
158 ULONG i
, count
, size
, nbFree
= 0;
159 PDIR_ENTRY pFatEntry
= NULL
;
160 PVOID Context
= NULL
;
163 FileOffset
.QuadPart
= 0;
165 if (DeviceExt
->Flags
& VCB_IS_FATX
)
166 SizeDirEntry
= sizeof(FATX_DIR_ENTRY
);
168 SizeDirEntry
= sizeof(FAT_DIR_ENTRY
);
170 count
= pDirFcb
->RFCB
.FileSize
.u
.LowPart
/ SizeDirEntry
;
171 size
= DeviceExt
->FatInfo
.BytesPerCluster
/ SizeDirEntry
;
172 for (i
= 0; i
< count
; i
++, pFatEntry
= (PDIR_ENTRY
)((ULONG_PTR
)pFatEntry
+ SizeDirEntry
))
174 if (Context
== NULL
|| (i
% size
) == 0)
178 CcUnpinData(Context
);
182 CcPinRead(pDirFcb
->FileObject
, &FileOffset
, DeviceExt
->FatInfo
.BytesPerCluster
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
184 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
186 _SEH2_YIELD(return FALSE
);
190 FileOffset
.u
.LowPart
+= DeviceExt
->FatInfo
.BytesPerCluster
;
192 if (ENTRY_END(DeviceExt
, pFatEntry
))
196 if (ENTRY_DELETED(DeviceExt
, pFatEntry
))
204 if (nbFree
== nbSlots
)
211 CcUnpinData(Context
);
214 if (nbFree
== nbSlots
)
216 /* found enough contiguous free slots */
217 *start
= i
- nbSlots
+ 1;
222 if (*start
+ nbSlots
> count
)
224 LARGE_INTEGER AllocationSize
;
225 /* extend the directory */
226 if (vfatFCBIsRoot(pDirFcb
) && DeviceExt
->FatInfo
.FatType
!= FAT32
)
228 /* We can't extend a root directory on a FAT12/FAT16/FATX partition */
231 AllocationSize
.QuadPart
= pDirFcb
->RFCB
.FileSize
.u
.LowPart
+ DeviceExt
->FatInfo
.BytesPerCluster
;
232 Status
= VfatSetAllocationSizeInformation(pDirFcb
->FileObject
, pDirFcb
,
233 DeviceExt
, &AllocationSize
);
234 if (!NT_SUCCESS(Status
))
238 /* clear the new dir cluster */
239 FileOffset
.u
.LowPart
= (ULONG
)(pDirFcb
->RFCB
.FileSize
.QuadPart
-
240 DeviceExt
->FatInfo
.BytesPerCluster
);
243 CcPinRead(pDirFcb
->FileObject
, &FileOffset
, DeviceExt
->FatInfo
.BytesPerCluster
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
245 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
247 _SEH2_YIELD(return FALSE
);
251 if (DeviceExt
->Flags
& VCB_IS_FATX
)
252 memset(pFatEntry
, 0xff, DeviceExt
->FatInfo
.BytesPerCluster
);
254 RtlZeroMemory(pFatEntry
, DeviceExt
->FatInfo
.BytesPerCluster
);
256 else if (*start
+ nbSlots
< count
)
258 /* clear the entry after the last new entry */
259 FileOffset
.u
.LowPart
= (*start
+ nbSlots
) * SizeDirEntry
;
262 CcPinRead(pDirFcb
->FileObject
, &FileOffset
, SizeDirEntry
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
264 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
266 _SEH2_YIELD(return FALSE
);
270 if (DeviceExt
->Flags
& VCB_IS_FATX
)
271 memset(pFatEntry
, 0xff, SizeDirEntry
);
273 RtlZeroMemory(pFatEntry
, SizeDirEntry
);
277 CcSetDirtyPinnedData(Context
, NULL
);
278 CcUnpinData(Context
);
281 DPRINT("nbSlots %u nbFree %u, entry number %u\n", nbSlots
, nbFree
, *start
);
286 create a new FAT entry
290 IN PDEVICE_EXTENSION DeviceExt
,
291 IN PUNICODE_STRING NameU
,
293 IN PVFATFCB ParentFcb
,
294 IN ULONG RequestedOptions
,
296 IN PVFAT_MOVE_CONTEXT MoveContext
)
298 PVOID Context
= NULL
;
299 PFAT_DIR_ENTRY pFatEntry
;
301 USHORT nbSlots
= 0, j
, posCar
;
303 BOOLEAN needTilde
= FALSE
, needLong
= FALSE
;
304 BOOLEAN lCaseBase
= FALSE
, uCaseBase
, lCaseExt
= FALSE
, uCaseExt
;
305 ULONG CurrentCluster
;
306 LARGE_INTEGER SystemTime
, FileOffset
;
307 NTSTATUS Status
= STATUS_SUCCESS
;
316 VFAT_DIRENTRY_CONTEXT DirContext
;
317 WCHAR LongNameBuffer
[LONGNAME_MAX_LENGTH
+ 1];
318 WCHAR ShortNameBuffer
[13];
320 DPRINT("addEntry: Name='%wZ', Dir='%wZ'\n", NameU
, &ParentFcb
->PathNameU
);
322 DirContext
.LongNameU
= *NameU
;
324 /* nb of entry needed for long name+normal entry */
325 nbSlots
= (DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) + 12) / 13 + 1;
326 DPRINT("NameLen= %u, nbSlots =%u\n", DirContext
.LongNameU
.Length
/ sizeof(WCHAR
), nbSlots
);
327 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, (nbSlots
- 1) * sizeof(FAT_DIR_ENTRY
), TAG_VFAT
);
330 return STATUS_INSUFFICIENT_RESOURCES
;
332 RtlZeroMemory(Buffer
, (nbSlots
- 1) * sizeof(FAT_DIR_ENTRY
));
333 pSlots
= (slot
*) Buffer
;
335 NameA
.Buffer
= aName
;
337 NameA
.MaximumLength
= sizeof(aName
);
339 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
340 DirContext
.ShortNameU
.Length
= 0;
341 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
343 RtlZeroMemory(&DirContext
.DirEntry
.Fat
, sizeof(FAT_DIR_ENTRY
));
345 IsNameLegal
= RtlIsNameLegalDOS8Dot3(&DirContext
.LongNameU
, &NameA
, &SpacesFound
);
347 if (!IsNameLegal
|| SpacesFound
)
349 GENERATE_NAME_CONTEXT NameContext
;
350 VFAT_DIRENTRY_CONTEXT SearchContext
;
351 WCHAR ShortSearchName
[13];
354 RtlZeroMemory(&NameContext
, sizeof(GENERATE_NAME_CONTEXT
));
355 SearchContext
.LongNameU
.Buffer
= LongNameBuffer
;
356 SearchContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
357 SearchContext
.ShortNameU
.Buffer
= ShortSearchName
;
358 SearchContext
.ShortNameU
.MaximumLength
= sizeof(ShortSearchName
);
360 for (i
= 0; i
< 100; i
++)
362 RtlGenerate8dot3Name(&DirContext
.LongNameU
, FALSE
, &NameContext
, &DirContext
.ShortNameU
);
363 DirContext
.ShortNameU
.Buffer
[DirContext
.ShortNameU
.Length
/ sizeof(WCHAR
)] = 0;
364 SearchContext
.DirIndex
= 0;
365 Status
= FindFile(DeviceExt
, ParentFcb
, &DirContext
.ShortNameU
, &SearchContext
, TRUE
);
366 if (!NT_SUCCESS(Status
))
370 else if (MoveContext
)
373 if (strncmp((char *)SearchContext
.DirEntry
.Fat
.ShortName
, (char *)(*Fcb
)->entry
.Fat
.ShortName
, 11) == 0)
375 if (MoveContext
->InPlace
)
377 ASSERT(SearchContext
.DirEntry
.Fat
.FileSize
== MoveContext
->FileSize
);
383 if (i
== 100) /* FIXME : what to do after this ? */
385 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
386 return STATUS_UNSUCCESSFUL
;
388 IsNameLegal
= RtlIsNameLegalDOS8Dot3(&DirContext
.ShortNameU
, &NameA
, &SpacesFound
);
389 aName
[NameA
.Length
]=0;
393 aName
[NameA
.Length
] = 0;
394 for (posCar
= 0; posCar
< DirContext
.LongNameU
.Length
/ sizeof(WCHAR
); posCar
++)
396 if (DirContext
.LongNameU
.Buffer
[posCar
] == L
'.')
401 /* check if the name and the extension contains upper case characters */
402 RtlDowncaseUnicodeString(&DirContext
.ShortNameU
, &DirContext
.LongNameU
, FALSE
);
403 DirContext
.ShortNameU
.Buffer
[DirContext
.ShortNameU
.Length
/ sizeof(WCHAR
)] = 0;
404 uCaseBase
= wcsncmp(DirContext
.LongNameU
.Buffer
,
405 DirContext
.ShortNameU
.Buffer
, posCar
) ? TRUE
: FALSE
;
406 if (posCar
< DirContext
.LongNameU
.Length
/sizeof(WCHAR
))
408 i
= DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) - posCar
;
409 uCaseExt
= wcsncmp(DirContext
.LongNameU
.Buffer
+ posCar
,
410 DirContext
.ShortNameU
.Buffer
+ posCar
, i
) ? TRUE
: FALSE
;
416 /* check if the name and the extension contains lower case characters */
417 RtlUpcaseUnicodeString(&DirContext
.ShortNameU
, &DirContext
.LongNameU
, FALSE
);
418 DirContext
.ShortNameU
.Buffer
[DirContext
.ShortNameU
.Length
/ sizeof(WCHAR
)] = 0;
419 lCaseBase
= wcsncmp(DirContext
.LongNameU
.Buffer
,
420 DirContext
.ShortNameU
.Buffer
, posCar
) ? TRUE
: FALSE
;
421 if (posCar
< DirContext
.LongNameU
.Length
/ sizeof(WCHAR
))
423 i
= DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) - posCar
;
424 lCaseExt
= wcsncmp(DirContext
.LongNameU
.Buffer
+ posCar
,
425 DirContext
.ShortNameU
.Buffer
+ posCar
, i
) ? TRUE
: FALSE
;
431 if ((lCaseBase
&& uCaseBase
) || (lCaseExt
&& uCaseExt
))
436 DPRINT("'%s', '%wZ', needTilde=%u, needLong=%u\n",
437 aName
, &DirContext
.LongNameU
, needTilde
, needLong
);
438 memset(DirContext
.DirEntry
.Fat
.ShortName
, ' ', 11);
439 for (i
= 0; i
< 8 && aName
[i
] && aName
[i
] != '.'; i
++)
441 DirContext
.DirEntry
.Fat
.Filename
[i
] = aName
[i
];
446 for (j
= 0; j
< 3 && aName
[i
]; j
++, i
++)
448 DirContext
.DirEntry
.Fat
.Ext
[j
] = aName
[i
];
451 if (DirContext
.DirEntry
.Fat
.Filename
[0] == 0xe5)
453 DirContext
.DirEntry
.Fat
.Filename
[0] = 0x05;
458 RtlCopyMemory(LongNameBuffer
, DirContext
.LongNameU
.Buffer
, DirContext
.LongNameU
.Length
);
459 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
460 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
461 DirContext
.LongNameU
.Buffer
[DirContext
.LongNameU
.Length
/ sizeof(WCHAR
)] = 0;
462 memset(DirContext
.LongNameU
.Buffer
+ DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) + 1, 0xff,
463 DirContext
.LongNameU
.MaximumLength
- DirContext
.LongNameU
.Length
- sizeof(WCHAR
));
470 DirContext
.DirEntry
.Fat
.lCase
|= VFAT_CASE_LOWER_BASE
;
474 DirContext
.DirEntry
.Fat
.lCase
|= VFAT_CASE_LOWER_EXT
;
478 DPRINT ("dos name=%11.11s\n", DirContext
.DirEntry
.Fat
.Filename
);
481 DirContext
.DirEntry
.Fat
.Attrib
= ReqAttr
;
482 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
484 DirContext
.DirEntry
.Fat
.Attrib
|= FILE_ATTRIBUTE_DIRECTORY
;
486 /* set dates and times */
487 KeQuerySystemTime(&SystemTime
);
488 FsdSystemTimeToDosDateTime(DeviceExt
, &SystemTime
, &DirContext
.DirEntry
.Fat
.CreationDate
,
489 &DirContext
.DirEntry
.Fat
.CreationTime
);
490 DirContext
.DirEntry
.Fat
.UpdateDate
= DirContext
.DirEntry
.Fat
.CreationDate
;
491 DirContext
.DirEntry
.Fat
.UpdateTime
= DirContext
.DirEntry
.Fat
.CreationTime
;
492 DirContext
.DirEntry
.Fat
.AccessDate
= DirContext
.DirEntry
.Fat
.CreationDate
;
493 /* If it's moving, preserve creation time and file size */
494 if (MoveContext
!= NULL
)
496 DirContext
.DirEntry
.Fat
.CreationDate
= MoveContext
->CreationDate
;
497 DirContext
.DirEntry
.Fat
.CreationTime
= MoveContext
->CreationTime
;
498 DirContext
.DirEntry
.Fat
.FileSize
= MoveContext
->FileSize
;
503 /* calculate checksum for 8.3 name */
504 for (pSlots
[0].alias_checksum
= 0, i
= 0; i
< 11; i
++)
506 pSlots
[0].alias_checksum
= (((pSlots
[0].alias_checksum
& 1) << 7
507 | ((pSlots
[0].alias_checksum
& 0xfe) >> 1))
508 + DirContext
.DirEntry
.Fat
.ShortName
[i
]);
510 /* construct slots and entry */
511 for (i
= nbSlots
- 2; i
>= 0; i
--)
513 DPRINT("construct slot %d\n", i
);
514 pSlots
[i
].attr
= 0xf;
517 pSlots
[i
].id
= (unsigned char)(nbSlots
- i
- 1);
521 pSlots
[i
].id
= (unsigned char)(nbSlots
- i
- 1 + 0x40);
523 pSlots
[i
].alias_checksum
= pSlots
[0].alias_checksum
;
524 RtlCopyMemory(pSlots
[i
].name0_4
, DirContext
.LongNameU
.Buffer
+ (nbSlots
- i
- 2) * 13, 10);
525 RtlCopyMemory(pSlots
[i
].name5_10
, DirContext
.LongNameU
.Buffer
+ (nbSlots
- i
- 2) * 13 + 5, 12);
526 RtlCopyMemory(pSlots
[i
].name11_12
, DirContext
.LongNameU
.Buffer
+ (nbSlots
- i
- 2) * 13 + 11, 4);
529 /* try to find nbSlots contiguous entries frees in directory */
530 if (!vfatFindDirSpace(DeviceExt
, ParentFcb
, nbSlots
, &DirContext
.StartIndex
))
532 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
533 return STATUS_DISK_FULL
;
535 DirContext
.DirIndex
= DirContext
.StartIndex
+ nbSlots
- 1;
536 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
538 /* If we aren't moving, use next */
539 if (MoveContext
== NULL
)
542 Status
= NextCluster(DeviceExt
, 0, &CurrentCluster
, TRUE
);
543 if (CurrentCluster
== 0xffffffff || !NT_SUCCESS(Status
))
545 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
546 if (!NT_SUCCESS(Status
))
550 return STATUS_DISK_FULL
;
555 CurrentCluster
= MoveContext
->FirstCluster
;
558 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
560 DirContext
.DirEntry
.Fat
.FirstClusterHigh
= (unsigned short)(CurrentCluster
>> 16);
562 DirContext
.DirEntry
.Fat
.FirstCluster
= (unsigned short)CurrentCluster
;
564 else if (MoveContext
!= NULL
)
566 CurrentCluster
= MoveContext
->FirstCluster
;
568 if (DeviceExt
->FatInfo
.FatType
== FAT32
)
570 DirContext
.DirEntry
.Fat
.FirstClusterHigh
= (unsigned short)(CurrentCluster
>> 16);
572 DirContext
.DirEntry
.Fat
.FirstCluster
= (unsigned short)CurrentCluster
;
575 i
= DeviceExt
->FatInfo
.BytesPerCluster
/ sizeof(FAT_DIR_ENTRY
);
576 FileOffset
.u
.HighPart
= 0;
577 FileOffset
.u
.LowPart
= DirContext
.StartIndex
* sizeof(FAT_DIR_ENTRY
);
578 if (DirContext
.StartIndex
/ i
== DirContext
.DirIndex
/ i
)
583 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, nbSlots
* sizeof(FAT_DIR_ENTRY
), PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
585 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
587 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
588 _SEH2_YIELD(return _SEH2_GetExceptionCode());
594 RtlCopyMemory(pFatEntry
, Buffer
, (nbSlots
- 1) * sizeof(FAT_DIR_ENTRY
));
596 RtlCopyMemory(pFatEntry
+ (nbSlots
- 1), &DirContext
.DirEntry
.Fat
, sizeof(FAT_DIR_ENTRY
));
601 size
= DeviceExt
->FatInfo
.BytesPerCluster
-
602 (DirContext
.StartIndex
* sizeof(FAT_DIR_ENTRY
)) % DeviceExt
->FatInfo
.BytesPerCluster
;
603 i
= size
/ sizeof(FAT_DIR_ENTRY
);
606 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, size
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
608 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
610 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
611 _SEH2_YIELD(return _SEH2_GetExceptionCode());
614 RtlCopyMemory(pFatEntry
, Buffer
, size
);
615 CcSetDirtyPinnedData(Context
, NULL
);
616 CcUnpinData(Context
);
617 FileOffset
.u
.LowPart
+= size
;
620 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, nbSlots
* sizeof(FAT_DIR_ENTRY
) - size
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
622 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
624 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
625 _SEH2_YIELD(return _SEH2_GetExceptionCode());
630 RtlCopyMemory(pFatEntry
, (PVOID
)(Buffer
+ size
), (nbSlots
- 1 - i
) * sizeof(FAT_DIR_ENTRY
));
632 RtlCopyMemory(pFatEntry
+ nbSlots
- 1 - i
, &DirContext
.DirEntry
.Fat
, sizeof(FAT_DIR_ENTRY
));
634 CcSetDirtyPinnedData(Context
, NULL
);
635 CcUnpinData(Context
);
637 if (MoveContext
!= NULL
)
639 /* We're modifying an existing FCB - likely rename/move */
640 Status
= vfatUpdateFCB(DeviceExt
, *Fcb
, &DirContext
, ParentFcb
);
641 (*Fcb
)->dirIndex
= DirContext
.DirIndex
;
642 (*Fcb
)->startIndex
= DirContext
.StartIndex
;
646 Status
= vfatMakeFCBFromDirEntry(DeviceExt
, ParentFcb
, &DirContext
, Fcb
);
648 if (!NT_SUCCESS(Status
))
650 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
654 DPRINT("new : entry=%11.11s\n", (*Fcb
)->entry
.Fat
.Filename
);
655 DPRINT("new : entry=%11.11s\n", DirContext
.DirEntry
.Fat
.Filename
);
657 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
659 FileOffset
.QuadPart
= 0;
662 CcPinRead((*Fcb
)->FileObject
, &FileOffset
, DeviceExt
->FatInfo
.BytesPerCluster
, PIN_WAIT
, &Context
, (PVOID
*)&pFatEntry
);
664 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
666 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
667 _SEH2_YIELD(return _SEH2_GetExceptionCode());
670 /* clear the new directory cluster if not moving */
671 if (MoveContext
== NULL
)
673 RtlZeroMemory(pFatEntry
, DeviceExt
->FatInfo
.BytesPerCluster
);
674 /* create '.' and '..' */
675 RtlCopyMemory(&pFatEntry
[0].Attrib
, &DirContext
.DirEntry
.Fat
.Attrib
, sizeof(FAT_DIR_ENTRY
) - 11);
676 RtlCopyMemory(pFatEntry
[0].ShortName
, ". ", 11);
677 RtlCopyMemory(&pFatEntry
[1].Attrib
, &DirContext
.DirEntry
.Fat
.Attrib
, sizeof(FAT_DIR_ENTRY
) - 11);
678 RtlCopyMemory(pFatEntry
[1].ShortName
, ".. ", 11);
681 pFatEntry
[1].FirstCluster
= ParentFcb
->entry
.Fat
.FirstCluster
;
682 pFatEntry
[1].FirstClusterHigh
= ParentFcb
->entry
.Fat
.FirstClusterHigh
;
683 if (vfatFCBIsRoot(ParentFcb
))
685 pFatEntry
[1].FirstCluster
= 0;
686 pFatEntry
[1].FirstClusterHigh
= 0;
688 CcSetDirtyPinnedData(Context
, NULL
);
689 CcUnpinData(Context
);
691 ExFreePoolWithTag(Buffer
, TAG_VFAT
);
692 DPRINT("addentry ok\n");
693 return STATUS_SUCCESS
;
697 create a new FAT entry
701 IN PDEVICE_EXTENSION DeviceExt
,
702 IN PUNICODE_STRING NameU
,
704 IN PVFATFCB ParentFcb
,
705 IN ULONG RequestedOptions
,
707 IN PVFAT_MOVE_CONTEXT MoveContext
)
709 PVOID Context
= NULL
;
710 LARGE_INTEGER SystemTime
, FileOffset
;
712 VFAT_DIRENTRY_CONTEXT DirContext
;
713 PFATX_DIR_ENTRY pFatXDirEntry
;
716 DPRINT("addEntry: Name='%wZ', Dir='%wZ'\n", NameU
, &ParentFcb
->PathNameU
);
718 DirContext
.LongNameU
= *NameU
;
720 if (DirContext
.LongNameU
.Length
/ sizeof(WCHAR
) > 42)
723 return STATUS_NAME_TOO_LONG
;
726 /* try to find 1 entry free in directory */
727 if (!vfatFindDirSpace(DeviceExt
, ParentFcb
, 1, &DirContext
.StartIndex
))
729 return STATUS_DISK_FULL
;
731 Index
= DirContext
.DirIndex
= DirContext
.StartIndex
;
732 if (!vfatFCBIsRoot(ParentFcb
))
734 DirContext
.DirIndex
+= 2;
735 DirContext
.StartIndex
+= 2;
738 DirContext
.ShortNameU
.Buffer
= 0;
739 DirContext
.ShortNameU
.Length
= 0;
740 DirContext
.ShortNameU
.MaximumLength
= 0;
741 RtlZeroMemory(&DirContext
.DirEntry
.FatX
, sizeof(FATX_DIR_ENTRY
));
742 memset(DirContext
.DirEntry
.FatX
.Filename
, 0xff, 42);
743 /* Use cluster, if moving */
744 if (MoveContext
!= NULL
)
746 DirContext
.DirEntry
.FatX
.FirstCluster
= MoveContext
->FirstCluster
;
750 DirContext
.DirEntry
.FatX
.FirstCluster
= 0;
752 DirContext
.DirEntry
.FatX
.FileSize
= 0;
755 NameA
.Buffer
= (PCHAR
)DirContext
.DirEntry
.FatX
.Filename
;
757 NameA
.MaximumLength
= 42;
758 RtlUnicodeStringToOemString(&NameA
, &DirContext
.LongNameU
, FALSE
);
759 DirContext
.DirEntry
.FatX
.FilenameLength
= (unsigned char)NameA
.Length
;
762 DirContext
.DirEntry
.FatX
.Attrib
= ReqAttr
;
763 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
765 DirContext
.DirEntry
.FatX
.Attrib
|= FILE_ATTRIBUTE_DIRECTORY
;
768 /* set dates and times */
769 KeQuerySystemTime(&SystemTime
);
770 FsdSystemTimeToDosDateTime(DeviceExt
, &SystemTime
, &DirContext
.DirEntry
.FatX
.CreationDate
,
771 &DirContext
.DirEntry
.FatX
.CreationTime
);
772 DirContext
.DirEntry
.FatX
.UpdateDate
= DirContext
.DirEntry
.FatX
.CreationDate
;
773 DirContext
.DirEntry
.FatX
.UpdateTime
= DirContext
.DirEntry
.FatX
.CreationTime
;
774 DirContext
.DirEntry
.FatX
.AccessDate
= DirContext
.DirEntry
.FatX
.CreationDate
;
775 DirContext
.DirEntry
.FatX
.AccessTime
= DirContext
.DirEntry
.FatX
.CreationTime
;
776 /* If it's moving, preserve creation time and file size */
777 if (MoveContext
!= NULL
)
779 DirContext
.DirEntry
.FatX
.CreationDate
= MoveContext
->CreationDate
;
780 DirContext
.DirEntry
.FatX
.CreationTime
= MoveContext
->CreationTime
;
781 DirContext
.DirEntry
.FatX
.FileSize
= MoveContext
->FileSize
;
784 /* add entry into parent directory */
785 FileOffset
.u
.HighPart
= 0;
786 FileOffset
.u
.LowPart
= Index
* sizeof(FATX_DIR_ENTRY
);
789 CcPinRead(ParentFcb
->FileObject
, &FileOffset
, sizeof(FATX_DIR_ENTRY
), PIN_WAIT
, &Context
, (PVOID
*)&pFatXDirEntry
);
791 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
793 _SEH2_YIELD(return _SEH2_GetExceptionCode());
796 RtlCopyMemory(pFatXDirEntry
, &DirContext
.DirEntry
.FatX
, sizeof(FATX_DIR_ENTRY
));
797 CcSetDirtyPinnedData(Context
, NULL
);
798 CcUnpinData(Context
);
800 if (MoveContext
!= NULL
)
802 /* We're modifying an existing FCB - likely rename/move */
803 /* FIXME: check status */
804 vfatUpdateFCB(DeviceExt
, *Fcb
, &DirContext
, ParentFcb
);
805 (*Fcb
)->dirIndex
= DirContext
.DirIndex
;
806 (*Fcb
)->startIndex
= DirContext
.StartIndex
;
810 /* FIXME: check status */
811 vfatMakeFCBFromDirEntry(DeviceExt
, ParentFcb
, &DirContext
, Fcb
);
814 DPRINT("addentry ok\n");
815 return STATUS_SUCCESS
;
820 IN PDEVICE_EXTENSION DeviceExt
,
821 IN PUNICODE_STRING NameU
,
823 IN PVFATFCB ParentFcb
,
824 IN ULONG RequestedOptions
,
826 IN PVFAT_MOVE_CONTEXT MoveContext
)
828 if (DeviceExt
->Flags
& VCB_IS_FATX
)
829 return FATXAddEntry(DeviceExt
, NameU
, Fcb
, ParentFcb
, RequestedOptions
, ReqAttr
, MoveContext
);
831 return FATAddEntry(DeviceExt
, NameU
, Fcb
, ParentFcb
, RequestedOptions
, ReqAttr
, MoveContext
);
835 * deleting an existing FAT entry
839 IN PDEVICE_EXTENSION DeviceExt
,
841 OUT PVFAT_MOVE_CONTEXT MoveContext
)
843 ULONG CurrentCluster
= 0, NextCluster
, i
;
844 PVOID Context
= NULL
;
845 LARGE_INTEGER Offset
;
846 PFAT_DIR_ENTRY pDirEntry
= NULL
;
849 ASSERT(pFcb
->parentFcb
);
851 DPRINT("delEntry PathName \'%wZ\'\n", &pFcb
->PathNameU
);
852 DPRINT("delete entry: %u to %u\n", pFcb
->startIndex
, pFcb
->dirIndex
);
853 Offset
.u
.HighPart
= 0;
854 for (i
= pFcb
->startIndex
; i
<= pFcb
->dirIndex
; i
++)
856 if (Context
== NULL
|| ((i
* sizeof(FAT_DIR_ENTRY
)) % PAGE_SIZE
) == 0)
860 CcSetDirtyPinnedData(Context
, NULL
);
861 CcUnpinData(Context
);
863 Offset
.u
.LowPart
= (i
* sizeof(FAT_DIR_ENTRY
) / PAGE_SIZE
) * PAGE_SIZE
;
866 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, PAGE_SIZE
, PIN_WAIT
, &Context
, (PVOID
*)&pDirEntry
);
868 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
870 _SEH2_YIELD(return _SEH2_GetExceptionCode());
874 pDirEntry
[i
% (PAGE_SIZE
/ sizeof(FAT_DIR_ENTRY
))].Filename
[0] = 0xe5;
875 if (i
== pFcb
->dirIndex
)
878 vfatDirEntryGetFirstCluster(DeviceExt
,
879 (PDIR_ENTRY
)&pDirEntry
[i
% (PAGE_SIZE
/ sizeof(FAT_DIR_ENTRY
))]);
883 /* In case of moving, save properties */
884 if (MoveContext
!= NULL
)
886 pDirEntry
= &pDirEntry
[pFcb
->dirIndex
% (PAGE_SIZE
/ sizeof(FAT_DIR_ENTRY
))];
887 MoveContext
->FirstCluster
= CurrentCluster
;
888 MoveContext
->FileSize
= pDirEntry
->FileSize
;
889 MoveContext
->CreationTime
= pDirEntry
->CreationTime
;
890 MoveContext
->CreationDate
= pDirEntry
->CreationDate
;
895 CcSetDirtyPinnedData(Context
, NULL
);
896 CcUnpinData(Context
);
899 /* In case of moving, don't delete data */
900 if (MoveContext
== NULL
)
902 while (CurrentCluster
&& CurrentCluster
!= 0xffffffff)
904 GetNextCluster(DeviceExt
, CurrentCluster
, &NextCluster
);
905 /* FIXME: check status */
906 WriteCluster(DeviceExt
, CurrentCluster
, 0);
907 CurrentCluster
= NextCluster
;
911 return STATUS_SUCCESS
;
915 * deleting an existing FAT entry
919 IN PDEVICE_EXTENSION DeviceExt
,
921 OUT PVFAT_MOVE_CONTEXT MoveContext
)
923 ULONG CurrentCluster
= 0, NextCluster
;
924 PVOID Context
= NULL
;
925 LARGE_INTEGER Offset
;
926 PFATX_DIR_ENTRY pDirEntry
;
930 ASSERT(pFcb
->parentFcb
);
931 ASSERT(pFcb
->Flags
& FCB_IS_FATX_ENTRY
);
933 StartIndex
= pFcb
->startIndex
;
935 DPRINT("delEntry PathName \'%wZ\'\n", &pFcb
->PathNameU
);
936 DPRINT("delete entry: %u\n", StartIndex
);
937 Offset
.u
.HighPart
= 0;
938 Offset
.u
.LowPart
= (StartIndex
* sizeof(FATX_DIR_ENTRY
) / PAGE_SIZE
) * PAGE_SIZE
;
941 CcPinRead(pFcb
->parentFcb
->FileObject
, &Offset
, PAGE_SIZE
, PIN_WAIT
, &Context
, (PVOID
*)&pDirEntry
);
943 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
945 DPRINT1("CcPinRead(Offset %x:%x, Length %d) failed\n", Offset
.u
.HighPart
, Offset
.u
.LowPart
, PAGE_SIZE
);
946 _SEH2_YIELD(return _SEH2_GetExceptionCode());
949 pDirEntry
= &pDirEntry
[StartIndex
% (PAGE_SIZE
/ sizeof(FATX_DIR_ENTRY
))];
950 pDirEntry
->FilenameLength
= 0xe5;
951 CurrentCluster
= vfatDirEntryGetFirstCluster(DeviceExt
,
952 (PDIR_ENTRY
)pDirEntry
);
954 /* In case of moving, save properties */
955 if (MoveContext
!= NULL
)
957 MoveContext
->FirstCluster
= CurrentCluster
;
958 MoveContext
->FileSize
= pDirEntry
->FileSize
;
959 MoveContext
->CreationTime
= pDirEntry
->CreationTime
;
960 MoveContext
->CreationDate
= pDirEntry
->CreationDate
;
963 CcSetDirtyPinnedData(Context
, NULL
);
964 CcUnpinData(Context
);
966 /* In case of moving, don't delete data */
967 if (MoveContext
== NULL
)
969 while (CurrentCluster
&& CurrentCluster
!= 0xffffffff)
971 GetNextCluster(DeviceExt
, CurrentCluster
, &NextCluster
);
972 /* FIXME: check status */
973 WriteCluster(DeviceExt
, CurrentCluster
, 0);
974 CurrentCluster
= NextCluster
;
978 return STATUS_SUCCESS
;
983 IN PDEVICE_EXTENSION DeviceExt
,
985 OUT PVFAT_MOVE_CONTEXT MoveContext
)
987 if (DeviceExt
->Flags
& VCB_IS_FATX
)
988 return FATXDelEntry(DeviceExt
, pFcb
, MoveContext
);
990 return FATDelEntry(DeviceExt
, pFcb
, MoveContext
);
994 * move an existing FAT entry
998 IN PDEVICE_EXTENSION DeviceExt
,
1000 IN PUNICODE_STRING FileName
,
1001 IN PVFATFCB ParentFcb
)
1005 VFAT_MOVE_CONTEXT MoveContext
;
1007 DPRINT("VfatMoveEntry(%p, %p, %wZ, %p)\n", DeviceExt
, pFcb
, FileName
, ParentFcb
);
1009 /* Delete old entry while keeping data */
1010 Status
= VfatDelEntry(DeviceExt
, pFcb
, &MoveContext
);
1011 if (!NT_SUCCESS(Status
))
1016 OldParent
= pFcb
->parentFcb
;
1017 CcPurgeCacheSection(&OldParent
->SectionObjectPointers
, NULL
, 0, FALSE
);
1018 MoveContext
.InPlace
= (OldParent
== ParentFcb
);
1020 /* Add our new entry with our cluster */
1021 Status
= VfatAddEntry(DeviceExt
,
1025 (vfatFCBIsDirectory(pFcb
) ? FILE_DIRECTORY_FILE
: 0),
1029 CcPurgeCacheSection(&pFcb
->parentFcb
->SectionObjectPointers
, NULL
, 0, FALSE
);