Skip to content

Commit

Permalink
Add bmap checksum field in Bmap struct
Browse files Browse the repository at this point in the history
bmap_file_checksum is going to be used to check the integrity of the
file. It is now included in the Bmap struct type and the
builder.

Signed-off-by: Rafael Garcia Ruiz <[email protected]>
  • Loading branch information
Razaloc committed Jan 13, 2023
1 parent 786c786 commit ec421bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions bmap-parser/src/bmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct Bmap {
blocks: u64,
mapped_blocks: u64,
checksum_type: HashType,
bmap_file_checksum: String,
blockmap: Vec<BlockRange>,
}

Expand Down Expand Up @@ -92,6 +93,11 @@ impl Bmap {
pub fn block_map(&self) -> impl ExactSizeIterator + Iterator<Item = &BlockRange> {
self.blockmap.iter()
}

pub fn bmap_file_checksum(&self) -> String {
self.bmap_file_checksum.clone()
}

pub fn total_mapped_size(&self) -> u64 {
self.block_size * self.mapped_blocks
}
Expand All @@ -109,6 +115,8 @@ pub enum BmapBuilderError {
MissingMappedBlocks,
#[error("Checksum type missing")]
MissingChecksumType,
#[error("Bmap file checksum missing")]
MissingBmapFileChecksum,
#[error("No block ranges")]
NoBlockRanges,
}
Expand All @@ -120,6 +128,7 @@ pub struct BmapBuilder {
blocks: Option<u64>,
checksum_type: Option<HashType>,
mapped_blocks: Option<u64>,
bmap_file_checksum: Option<String>,
blockmap: Vec<BlockRange>,
}

Expand Down Expand Up @@ -149,6 +158,11 @@ impl BmapBuilder {
self
}

pub fn bmap_file_checksum(&mut self, bmap_file_checksum: String) -> &mut Self {
self.bmap_file_checksum = Some(bmap_file_checksum);
self
}

pub fn add_block_range(&mut self, start: u64, end: u64, checksum: HashValue) -> &mut Self {
let bs = self.block_size.expect("Blocksize needs to be set first");
let total = self.image_size.expect("Image size needs to be set first");
Expand Down Expand Up @@ -177,6 +191,9 @@ impl BmapBuilder {
let checksum_type = self
.checksum_type
.ok_or(BmapBuilderError::MissingChecksumType)?;
let bmap_file_checksum = self
.bmap_file_checksum
.ok_or(BmapBuilderError::MissingBmapFileChecksum)?;
let blockmap = self.blockmap;

Ok(Bmap {
Expand All @@ -185,6 +202,7 @@ impl BmapBuilder {
blocks,
mapped_blocks,
checksum_type,
bmap_file_checksum,
blockmap,
})
}
Expand Down
1 change: 1 addition & 0 deletions bmap-parser/src/bmap/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub(crate) fn from_xml(xml: &str) -> Result<crate::bmap::Bmap, XmlError> {
.block_size(b.block_size)
.blocks(b.blocks_count)
.checksum_type(hash_type)
.bmap_file_checksum(b.bmap_file_checksum)
.mapped_blocks(b.mapped_blocks_count);

for range in b.block_map.ranges {
Expand Down

0 comments on commit ec421bc

Please sign in to comment.