#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Gap {
pub lba_start: u64,
pub lba_end: u64,
pub byte_size: u64,
pub kind: GapKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GapKind {
PrePartition,
Between,
PostPartition,
}
#[must_use]
pub fn compute_gaps(
extents: &[(u64, u64)],
first_usable: u64,
disk_last_lba: u64,
sector_size: u64,
) -> Vec<Gap> {
let mut gaps = Vec::new();
let mut cursor = first_usable;
for &(start, end) in extents {
if start > cursor {
let kind = if cursor == first_usable {
GapKind::PrePartition
} else {
GapKind::Between
};
let lba_end = start - 1;
gaps.push(Gap {
lba_start: cursor,
lba_end,
byte_size: (lba_end - cursor + 1) * sector_size,
kind,
});
}
if end >= cursor {
cursor = end + 1;
}
}
if cursor <= disk_last_lba {
gaps.push(Gap {
lba_start: cursor,
lba_end: disk_last_lba,
byte_size: (disk_last_lba - cursor + 1) * sector_size,
kind: GapKind::PostPartition,
});
}
gaps
}