diff --git a/Sources/ContainerizationEXT4/EXT4+Formatter.swift b/Sources/ContainerizationEXT4/EXT4+Formatter.swift index e961744b..90b4deb1 100644 --- a/Sources/ContainerizationEXT4/EXT4+Formatter.swift +++ b/Sources/ContainerizationEXT4/EXT4+Formatter.swift @@ -25,7 +25,8 @@ extension EXT4 { /// The `EXT4.Formatter` class provides methods to format a block device with the ext4 filesystem. /// It allows customization of block size and maximum disk size. public class Formatter { - let blockSize: UInt32 + private let logBlockSize: UInt32 = 2 + var blockSize: UInt32 { 1024 << logBlockSize } private var size: UInt64 private let groupDescriptorSize: UInt32 = 32 @@ -61,8 +62,6 @@ extension EXT4 { /// /// - Parameters: /// - devicePath: The path to the block device where the ext4 filesystem will be created. - /// - blockSize: The block size of the ext4 filesystem, specified in bytes. Common values are - /// 4096 (4KB) or 1024 (1KB). Default is 4096 (4KB) /// - minDiskSize: The minimum disk size required for the formatted filesystem. /// /// - Note: This ext4 formatter is designed for creating block devices out of container images and does not support all the @@ -71,7 +70,7 @@ extension EXT4 { /// /// - Important: Ensure that the destination block device is accessible and has sufficient permissions /// for formatting. The formatting process will erase all existing data on the device. - public init(_ devicePath: FilePath, blockSize: UInt32 = 4096, minDiskSize: UInt64 = 256.kib()) throws { + public init(_ devicePath: FilePath, minDiskSize: UInt64 = 256.kib()) throws { /// The constructor performs the following steps: /// /// 1. Creates the first 10 inodes: @@ -96,7 +95,6 @@ extension EXT4 { throw Error.notFound(devicePath) } self.handle = fileHandle - self.blockSize = blockSize self.size = minDiskSize // make this a 0 byte file guard ftruncate(self.handle.fileDescriptor, 0) == 0 else { @@ -865,8 +863,8 @@ extension EXT4 { let freeInodesCount = computedInodes.lo - totalInodes superblock.freeInodesCount = freeInodesCount superblock.firstDataBlock = 0 - superblock.logBlockSize = 2 - superblock.logClusterSize = 2 + superblock.logBlockSize = logBlockSize + superblock.logClusterSize = logBlockSize superblock.blocksPerGroup = self.blocksPerGroup superblock.clustersPerGroup = self.blocksPerGroup superblock.inodesPerGroup = blockGroupSize.inodesPerGroup diff --git a/Sources/ContainerizationEXT4/EXT4+Reader.swift b/Sources/ContainerizationEXT4/EXT4+Reader.swift index 54274aa7..9c00b4f8 100644 --- a/Sources/ContainerizationEXT4/EXT4+Reader.swift +++ b/Sources/ContainerizationEXT4/EXT4+Reader.swift @@ -32,9 +32,7 @@ extension EXT4 { var hardlinks: [FilePath: InodeNumber] = [:] var tree: EXT4.FileTree = EXT4.FileTree(EXT4.RootInode, ".") - var blockSize: UInt64 { - UInt64(1024 * (1 << _superBlock.logBlockSize)) - } + var blockSize: UInt64 { UInt64(_superBlock.blockSize) } private var groupDescriptorSize: UInt16 { if _superBlock.featureIncompat & EXT4.IncompatFeature.bit64.rawValue != 0 { @@ -120,7 +118,7 @@ extension EXT4 { } private func readGroupDescriptor(_ number: UInt32) throws -> GroupDescriptor { - let bs = UInt64(1024 * (1 << _superBlock.logBlockSize)) + let bs = self.blockSize let offset = bs + UInt64(number) * UInt64(self.groupDescriptorSize) try self.handle.seek(toOffset: offset) guard let data = try? self.handle.read(upToCount: MemoryLayout.size) else { diff --git a/Sources/ContainerizationEXT4/EXT4+Types.swift b/Sources/ContainerizationEXT4/EXT4+Types.swift index a63bd365..dfb7d53d 100644 --- a/Sources/ContainerizationEXT4/EXT4+Types.swift +++ b/Sources/ContainerizationEXT4/EXT4+Types.swift @@ -28,6 +28,7 @@ extension EXT4 { public var firstDataBlock: UInt32 = 0 public var logBlockSize: UInt32 = 0 public var logClusterSize: UInt32 = 0 + public var blockSize: UInt32 { 1024 << logBlockSize } public var blocksPerGroup: UInt32 = 0 public var clustersPerGroup: UInt32 = 0 public var inodesPerGroup: UInt32 = 0 diff --git a/Tests/ContainerizationEXT4Tests/TestEXT4Reader+IO.swift b/Tests/ContainerizationEXT4Tests/TestEXT4Reader+IO.swift index 5a773952..1180fe66 100644 --- a/Tests/ContainerizationEXT4Tests/TestEXT4Reader+IO.swift +++ b/Tests/ContainerizationEXT4Tests/TestEXT4Reader+IO.swift @@ -37,14 +37,13 @@ struct EXT4PathIOTests { /// } private func buildFS( minDiskSize: UInt64 = 4 * 1024 * 1024, // 4 MiB is enough for these tests - blockSize: UInt32 = 4096, populate: (EXT4.Formatter) throws -> Void ) throws -> URL { let url = makeTempImageURL() let path = FilePath(url.path) // 1) Format image - let formatter = try EXT4.Formatter(path, blockSize: blockSize, minDiskSize: minDiskSize) + let formatter = try EXT4.Formatter(path, minDiskSize: minDiskSize) // 2) Populate contents try populate(formatter)