Skip to content

Commit

Permalink
fix: convert_random_data test
Browse files Browse the repository at this point in the history
  • Loading branch information
cilki committed Jun 2, 2024
1 parent eccb7ff commit 8e024c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 9 additions & 4 deletions goldboot-image/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl TryFrom<String> for ImageArch {
/// file. Clusters are variable in size and ideally smaller than their
/// associated blocks (due to compression). If a block does not have an
/// associated cluster, that block is zero.
#[derive(Debug)]
pub struct ImageHandle {
/// The primary file header
pub primary_header: PrimaryHeader,
Expand Down Expand Up @@ -482,7 +483,7 @@ impl ImageHandle {
Aes256Gcm::new(Key::<Aes256Gcm>::from_slice(&protected_header.cluster_key));

let dest = dest.as_ref();
info!(dest = ?dest, "Writing image");
info!(image = ?self, dest = ?dest, "Writing goldboot image");

let mut dest = std::fs::OpenOptions::new()
.create(true)
Expand Down Expand Up @@ -626,8 +627,12 @@ impl ImageBuilder {
pub fn convert(self, source: &Qcow3, size: u64) -> Result<ImageHandle> {
info!(name = self.name, "Exporting storage to goldboot image");

// The requested goldboot image must be at least as large as the qcow
assert!(source.header.size <= size);
assert!(
source.header.size >= size,
"source.header.size = {}, size = {}",
source.header.size,
size
);

let mut dest_file = File::create(&self.dest)?;
let mut source_file = File::open(&source.path)?;
Expand All @@ -652,7 +657,7 @@ impl ImageBuilder {
let mut primary_header = PrimaryHeader {
version: 1,
arch: ImageArch::Amd64, // TODO
size: source.header.size,
size,
directory_nonce: rng.gen::<[u8; 12]>(),
directory_offset: 0,
directory_size: 0,
Expand Down
5 changes: 3 additions & 2 deletions goldboot-image/src/qcow/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl L2Entry {
}
}

/// Read the contents of a given L2 Entry from `reader` into `buf`.
/// Read the contents of a given L2 Entry from `reader`.
pub fn read_contents(
&self,
reader: &mut (impl Read + Seek),
Expand All @@ -90,10 +90,11 @@ impl L2Entry {
match &self.cluster_descriptor {
ClusterDescriptor::Standard(cluster) => {
if cluster.all_zeroes || cluster.host_cluster_offset == 0 {
// TODO size properly
buf.fill(0);
} else {
reader.seek(SeekFrom::Start(cluster.host_cluster_offset))?;
let r = reader.take(cluster_size).read_to_end(&mut buf)?;
reader.take(cluster_size).read_to_end(&mut buf)?;
}
}
ClusterDescriptor::Compressed(cluster) => match comp_type {
Expand Down

0 comments on commit 8e024c1

Please sign in to comment.