Skip to content

Commit 2c4012e

Browse files
authored
Derive the block size from the log block size (#662)
Derives the block size from the log block size.
1 parent 6f7e7f9 commit 2c4012e

4 files changed

Lines changed: 9 additions & 13 deletions

File tree

Sources/ContainerizationEXT4/EXT4+Formatter.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ extension EXT4 {
2525
/// The `EXT4.Formatter` class provides methods to format a block device with the ext4 filesystem.
2626
/// It allows customization of block size and maximum disk size.
2727
public class Formatter {
28-
let blockSize: UInt32
28+
private let logBlockSize: UInt32 = 2
29+
var blockSize: UInt32 { 1024 << logBlockSize }
2930
private var size: UInt64
3031
private let groupDescriptorSize: UInt32 = 32
3132

@@ -61,8 +62,6 @@ extension EXT4 {
6162
///
6263
/// - Parameters:
6364
/// - devicePath: The path to the block device where the ext4 filesystem will be created.
64-
/// - blockSize: The block size of the ext4 filesystem, specified in bytes. Common values are
65-
/// 4096 (4KB) or 1024 (1KB). Default is 4096 (4KB)
6665
/// - minDiskSize: The minimum disk size required for the formatted filesystem.
6766
///
6867
/// - 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 {
7170
///
7271
/// - Important: Ensure that the destination block device is accessible and has sufficient permissions
7372
/// for formatting. The formatting process will erase all existing data on the device.
74-
public init(_ devicePath: FilePath, blockSize: UInt32 = 4096, minDiskSize: UInt64 = 256.kib()) throws {
73+
public init(_ devicePath: FilePath, minDiskSize: UInt64 = 256.kib()) throws {
7574
/// The constructor performs the following steps:
7675
///
7776
/// 1. Creates the first 10 inodes:
@@ -96,7 +95,6 @@ extension EXT4 {
9695
throw Error.notFound(devicePath)
9796
}
9897
self.handle = fileHandle
99-
self.blockSize = blockSize
10098
self.size = minDiskSize
10199
// make this a 0 byte file
102100
guard ftruncate(self.handle.fileDescriptor, 0) == 0 else {
@@ -850,8 +848,8 @@ extension EXT4 {
850848
let freeInodesCount = computedInodes.lo - totalInodes
851849
superblock.freeInodesCount = freeInodesCount
852850
superblock.firstDataBlock = 0
853-
superblock.logBlockSize = 2
854-
superblock.logClusterSize = 2
851+
superblock.logBlockSize = logBlockSize
852+
superblock.logClusterSize = logBlockSize
855853
superblock.blocksPerGroup = self.blocksPerGroup
856854
superblock.clustersPerGroup = self.blocksPerGroup
857855
superblock.inodesPerGroup = blockGroupSize.inodesPerGroup

Sources/ContainerizationEXT4/EXT4+Reader.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ extension EXT4 {
3232

3333
var hardlinks: [FilePath: InodeNumber] = [:]
3434
var tree: EXT4.FileTree = EXT4.FileTree(EXT4.RootInode, ".")
35-
var blockSize: UInt64 {
36-
UInt64(1024 * (1 << _superBlock.logBlockSize))
37-
}
35+
var blockSize: UInt64 { UInt64(_superBlock.blockSize) }
3836

3937
private var groupDescriptorSize: UInt16 {
4038
if _superBlock.featureIncompat & EXT4.IncompatFeature.bit64.rawValue != 0 {
@@ -120,7 +118,7 @@ extension EXT4 {
120118
}
121119

122120
private func readGroupDescriptor(_ number: UInt32) throws -> GroupDescriptor {
123-
let bs = UInt64(1024 * (1 << _superBlock.logBlockSize))
121+
let bs = self.blockSize
124122
let offset = bs + UInt64(number) * UInt64(self.groupDescriptorSize)
125123
try self.handle.seek(toOffset: offset)
126124
guard let data = try? self.handle.read(upToCount: MemoryLayout<EXT4.GroupDescriptor>.size) else {

Sources/ContainerizationEXT4/EXT4+Types.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extension EXT4 {
2828
public var firstDataBlock: UInt32 = 0
2929
public var logBlockSize: UInt32 = 0
3030
public var logClusterSize: UInt32 = 0
31+
public var blockSize: UInt32 { 1024 << logBlockSize }
3132
public var blocksPerGroup: UInt32 = 0
3233
public var clustersPerGroup: UInt32 = 0
3334
public var inodesPerGroup: UInt32 = 0

Tests/ContainerizationEXT4Tests/TestEXT4Reader+IO.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ struct EXT4PathIOTests {
3737
/// }
3838
private func buildFS(
3939
minDiskSize: UInt64 = 4 * 1024 * 1024, // 4 MiB is enough for these tests
40-
blockSize: UInt32 = 4096,
4140
populate: (EXT4.Formatter) throws -> Void
4241
) throws -> URL {
4342
let url = makeTempImageURL()
4443
let path = FilePath(url.path)
4544

4645
// 1) Format image
47-
let formatter = try EXT4.Formatter(path, blockSize: blockSize, minDiskSize: minDiskSize)
46+
let formatter = try EXT4.Formatter(path, minDiskSize: minDiskSize)
4847

4948
// 2) Populate contents
5049
try populate(formatter)

0 commit comments

Comments
 (0)