Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Sources/ContainerizationEXT4/EXT4+Formatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions Sources/ContainerizationEXT4/EXT4+Reader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<EXT4.GroupDescriptor>.size) else {
Expand Down
1 change: 1 addition & 0 deletions Sources/ContainerizationEXT4/EXT4+Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions Tests/ContainerizationEXT4Tests/TestEXT4Reader+IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading