Author: ekohl Date: Mon Jan 11 20:44:06 2016 New Revision: 70576
URL: http://svn.reactos.org/svn/reactos?rev=70576&view=rev Log: [vfatlib] - Use a single wipe function (FatWipeSectors) for FAT12, FAT16 and FAT32. - Code has been reviewed by Hermès and me. Ready for merge into 0.4.
Modified: trunk/reactos/lib/fslib/vfatlib/common.c trunk/reactos/lib/fslib/vfatlib/common.h trunk/reactos/lib/fslib/vfatlib/fat12.c trunk/reactos/lib/fslib/vfatlib/fat16.c trunk/reactos/lib/fslib/vfatlib/fat32.c
Modified: trunk/reactos/lib/fslib/vfatlib/common.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/common.c?... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/common.c [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/common.c [iso-8859-1] Mon Jan 11 20:44:06 2016 @@ -50,36 +50,36 @@ return Serial; }
-/***** Wipe function for FAT12 and FAT16 formats, adapted from FAT32 code *****/ +/***** Wipe function for FAT12, FAT16 and FAT32 formats, adapted from FAT32 code *****/ NTSTATUS -Fat1216WipeSectors( +FatWipeSectors( IN HANDLE FileHandle, - IN PFAT16_BOOT_SECTOR BootSector, + IN ULONG TotalSectors, + IN ULONG SectorsPerCluster, + IN ULONG BytesPerSector, IN OUT PFORMAT_CONTEXT Context) { IO_STATUS_BLOCK IoStatusBlock; PUCHAR Buffer; LARGE_INTEGER FileOffset; ULONGLONG Sector; - ULONG SectorsHuge; ULONG Length; NTSTATUS Status; + + Length = SectorsPerCluster * BytesPerSector;
/* Allocate buffer for the cluster */ Buffer = (PUCHAR)RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, - BootSector->SectorsPerCluster * BootSector->BytesPerSector); + Length); if (Buffer == NULL) return STATUS_INSUFFICIENT_RESOURCES;
+ /* Wipe all clusters */ Sector = 0; - Length = BootSector->SectorsPerCluster * BootSector->BytesPerSector; - - SectorsHuge = (BootSector->SectorsHuge != 0 ? BootSector->SectorsHuge : BootSector->Sectors); - - while (Sector + BootSector->SectorsPerCluster < SectorsHuge) + while (Sector + SectorsPerCluster < TotalSectors) { - FileOffset.QuadPart = Sector * BootSector->BytesPerSector; + FileOffset.QuadPart = Sector * BytesPerSector;
Status = NtWriteFile(FileHandle, NULL, @@ -96,17 +96,18 @@ goto done; }
- UpdateProgress(Context, (ULONG)BootSector->SectorsPerCluster); + UpdateProgress(Context, SectorsPerCluster);
- Sector += BootSector->SectorsPerCluster; + Sector += SectorsPerCluster; }
- if (Sector + BootSector->SectorsPerCluster > SectorsHuge) + /* Wipe the trailing space behind the last cluster */ + if (Sector < TotalSectors) { - DPRINT("Remaining sectors %lu\n", SectorsHuge - Sector); + DPRINT("Remaining sectors %lu\n", TotalSectors - Sector);
- FileOffset.QuadPart = Sector * BootSector->BytesPerSector; - Length = (SectorsHuge - Sector) * BootSector->BytesPerSector; + FileOffset.QuadPart = Sector * BytesPerSector; + Length = (TotalSectors - Sector) * BytesPerSector;
Status = NtWriteFile(FileHandle, NULL, @@ -123,7 +124,7 @@ goto done; }
- UpdateProgress(Context, SectorsHuge - Sector); + UpdateProgress(Context, TotalSectors - Sector); }
done:
Modified: trunk/reactos/lib/fslib/vfatlib/common.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/common.h?... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/common.h [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/common.h [iso-8859-1] Mon Jan 11 20:44:06 2016 @@ -14,9 +14,11 @@ ULONG CalcVolumeSerialNumber(VOID);
NTSTATUS -Fat1216WipeSectors( +FatWipeSectors( IN HANDLE FileHandle, - IN PFAT16_BOOT_SECTOR BootSector, + IN ULONG TotalSectors, + IN ULONG SectorsPerCluster, + IN ULONG BytesPerSector, IN OUT PFORMAT_CONTEXT Context);
#endif /* _VFATCOMMON_H_ */
Modified: trunk/reactos/lib/fslib/vfatlib/fat12.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/fat12.c?r... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/fat12.c [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/fat12.c [iso-8859-1] Mon Jan 11 20:44:06 2016 @@ -326,9 +326,11 @@ { Context->TotalSectorCount += SectorCount;
- Status = Fat1216WipeSectors(FileHandle, - &BootSector, - Context); + Status = FatWipeSectors(FileHandle, + SectorCount, + (ULONG)BootSector.SectorsPerCluster, + (ULONG)BootSector.BytesPerSector, + Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat12WipeSectors() failed with status 0x%.08x\n", Status);
Modified: trunk/reactos/lib/fslib/vfatlib/fat16.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/fat16.c?r... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/fat16.c [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/fat16.c [iso-8859-1] Mon Jan 11 20:44:06 2016 @@ -334,9 +334,11 @@ { Context->TotalSectorCount += SectorCount;
- Status = Fat1216WipeSectors(FileHandle, - &BootSector, - Context); + Status = FatWipeSectors(FileHandle, + SectorCount, + (ULONG)BootSector.SectorsPerCluster, + (ULONG)BootSector.BytesPerSector, + Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat16WipeSectors() failed with status 0x%.08x\n", Status);
Modified: trunk/reactos/lib/fslib/vfatlib/fat32.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/fat32.c?r... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/fat32.c [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/fat32.c [iso-8859-1] Mon Jan 11 20:44:06 2016 @@ -375,86 +375,6 @@ }
UpdateProgress(Context, (ULONG)BootSector->SectorsPerCluster); - -done: - /* Free the buffer */ - RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer); - return Status; -} - - -static -NTSTATUS -Fat32WipeSectors( - IN HANDLE FileHandle, - IN PFAT32_BOOT_SECTOR BootSector, - IN OUT PFORMAT_CONTEXT Context) -{ - IO_STATUS_BLOCK IoStatusBlock; - PUCHAR Buffer; - LARGE_INTEGER FileOffset; - ULONGLONG Sector; - ULONG Length; - NTSTATUS Status; - - /* Allocate buffer for the cluster */ - Buffer = (PUCHAR)RtlAllocateHeap(RtlGetProcessHeap(), - HEAP_ZERO_MEMORY, - BootSector->SectorsPerCluster * BootSector->BytesPerSector); - if (Buffer == NULL) - return STATUS_INSUFFICIENT_RESOURCES; - - Sector = 0; - Length = BootSector->SectorsPerCluster * BootSector->BytesPerSector; - - while (Sector + BootSector->SectorsPerCluster < BootSector->SectorsHuge) - { - FileOffset.QuadPart = Sector * BootSector->BytesPerSector; - - Status = NtWriteFile(FileHandle, - NULL, - NULL, - NULL, - &IoStatusBlock, - Buffer, - Length, - &FileOffset, - NULL); - if (!NT_SUCCESS(Status)) - { - DPRINT("NtWriteFile() failed (Status %lx)\n", Status); - goto done; - } - - UpdateProgress(Context, (ULONG)BootSector->SectorsPerCluster); - - Sector += BootSector->SectorsPerCluster; - } - - if (Sector + BootSector->SectorsPerCluster > BootSector->SectorsHuge) - { - DPRINT("Remaining sectors %lu\n", BootSector->SectorsHuge - Sector); - - FileOffset.QuadPart = Sector * BootSector->BytesPerSector; - Length = (BootSector->SectorsHuge - Sector) * BootSector->BytesPerSector; - - Status = NtWriteFile(FileHandle, - NULL, - NULL, - NULL, - &IoStatusBlock, - Buffer, - Length, - &FileOffset, - NULL); - if (!NT_SUCCESS(Status)) - { - DPRINT("NtWriteFile() failed (Status %lx)\n", Status); - goto done; - } - - UpdateProgress(Context, BootSector->SectorsHuge - Sector); - }
done: /* Free the buffer */ @@ -557,9 +477,11 @@ { Context->TotalSectorCount += BootSector.SectorsHuge;
- Status = Fat32WipeSectors(FileHandle, - &BootSector, - Context); + Status = FatWipeSectors(FileHandle, + BootSector.SectorsHuge, + (ULONG)BootSector.SectorsPerCluster, + (ULONG)BootSector.BytesPerSector, + Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat32WipeSectors() failed with status 0x%.08x\n", Status);