From 7e4097fb6faa187dde94ed5208bce1b5e67d7e4e Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Mon, 20 Dec 2021 15:40:27 +0100 Subject: [PATCH] disk: sector size can now be per partition table Allow the sector size to be specified on the partition table level by introducing a new `SectorSize` field. Modify the conversion helpers to use that new field with a fallback to default sector size in case the field is `0`. --- internal/disk/disk.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/disk/disk.go b/internal/disk/disk.go index 31bb498cd..69a6118a0 100644 --- a/internal/disk/disk.go +++ b/internal/disk/disk.go @@ -24,6 +24,8 @@ type PartitionTable struct { UUID string // Unique identifier of the partition table (GPT only). Type string // Partition table type, e.g. dos, gpt. Partitions []Partition + + SectorSize uint64 // Sector size in bytes } type Partition struct { @@ -55,12 +57,20 @@ type Filesystem struct { // Convert the given bytes to the number of sectors. func (pt *PartitionTable) BytesToSectors(size uint64) uint64 { - return size / DefaultSectorSize + sectorSize := pt.SectorSize + if sectorSize == 0 { + sectorSize = DefaultSectorSize + } + return size / sectorSize } // Convert the given number of sectors to bytes. func (pt *PartitionTable) SectorsToBytes(size uint64) uint64 { - return size * DefaultSectorSize + sectorSize := pt.SectorSize + if sectorSize == 0 { + sectorSize = DefaultSectorSize + } + return size * sectorSize } // Clone the partition table (deep copy). @@ -79,6 +89,8 @@ func (pt *PartitionTable) Clone() *PartitionTable { UUID: pt.UUID, Type: pt.Type, Partitions: partitions, + + SectorSize: pt.SectorSize, } }