Rename target to image_data, make compression optional

This commit is contained in:
Bruce Leidl 2019-01-15 09:38:42 -05:00
parent 41179a720d
commit 91530279cf

View File

@ -10,7 +10,7 @@ use BuildConfig;
pub struct UpdateBuilder { pub struct UpdateBuilder {
config: BuildConfig, config: BuildConfig,
target: PathBuf, image_data: PathBuf,
nblocks: Option<usize>, nblocks: Option<usize>,
shasum: Option<String>, shasum: Option<String>,
@ -29,9 +29,9 @@ impl UpdateBuilder {
pub fn new(config: BuildConfig) -> UpdateBuilder { pub fn new(config: BuildConfig) -> UpdateBuilder {
let filename = UpdateBuilder::target_filename(&config); let filename = UpdateBuilder::target_filename(&config);
let target = config.workdir_path(&filename); let image_data= config.workdir_path(&filename);
UpdateBuilder { UpdateBuilder {
config, target, config, image_data,
nblocks: None, shasum: None, verity_salt: None, nblocks: None, shasum: None, verity_salt: None,
verity_root: None, verity_root: None,
} }
@ -42,8 +42,8 @@ impl UpdateBuilder {
} }
pub fn build(&mut self) -> Result<()> { pub fn build(&mut self) -> Result<()> {
info!("Copying source file to {}", self.target.display()); info!("Copying source file to {}", self.image_data.display());
fs::copy(self.config.source(), &self.target)?; fs::copy(self.config.source(), &self.image_data)?;
self.pad_image() self.pad_image()
.context("failed writing padding to image")?; .context("failed writing padding to image")?;
@ -61,7 +61,7 @@ impl UpdateBuilder {
} }
fn pad_image(&mut self) -> Result<()> { fn pad_image(&mut self) -> Result<()> {
let meta = self.target.metadata()?; let meta = self.image_data.metadata()?;
let len = meta.len() as usize; let len = meta.len() as usize;
if len % 512 != 0 { if len % 512 != 0 {
bail!("Image file size is not a multiple of sector size (512 bytes)"); bail!("Image file size is not a multiple of sector size (512 bytes)");
@ -73,7 +73,7 @@ impl UpdateBuilder {
let zeros = vec![0u8; padlen]; let zeros = vec![0u8; padlen];
let mut file = OpenOptions::new() let mut file = OpenOptions::new()
.append(true) .append(true)
.open(&self.target)?; .open(&self.image_data)?;
file.write_all(&zeros)?; file.write_all(&zeros)?;
} }
@ -85,8 +85,8 @@ impl UpdateBuilder {
} }
fn calculate_shasum(&mut self) -> Result<()> { fn calculate_shasum(&mut self) -> Result<()> {
let output = util::exec_cmdline_with_output("sha256sum", format!("{}", self.target.display())) let output = util::exec_cmdline_with_output("sha256sum", format!("{}", self.image_data.display()))
.context(format!("failed to calculate sha256 on {}", self.target.display()))?; .context(format!("failed to calculate sha256 on {}", self.image_data.display()))?;
let v: Vec<&str> = output.split_whitespace().collect(); let v: Vec<&str> = output.split_whitespace().collect();
let shasum = v[0].trim().to_owned(); let shasum = v[0].trim().to_owned();
info!("Sha256 of image data is {}", shasum); info!("Sha256 of image data is {}", shasum);
@ -98,7 +98,7 @@ impl UpdateBuilder {
let hashfile = self.config.workdir_path(&format!("verity-hash-{}-{:03}", self.config.image_type(), self.config.version())); let hashfile = self.config.workdir_path(&format!("verity-hash-{}-{:03}", self.config.image_type(), self.config.version()));
let outfile = self.config.workdir_path("verity-format.out"); let outfile = self.config.workdir_path("verity-format.out");
let verity = verity::generate_initial_hashtree(&self.target, &hashfile)?; let verity = verity::generate_initial_hashtree(&self.image_data, &hashfile)?;
fs::write(outfile, verity.output()) fs::write(outfile, verity.output())
.context("failed to write veritysetup command output to a file")?; .context("failed to write veritysetup command output to a file")?;
@ -123,25 +123,29 @@ impl UpdateBuilder {
} }
fn compress_image(&self) -> Result<()> { fn compress_image(&self) -> Result<()> {
if self.config.compress() {
info!("Compressing image data"); info!("Compressing image data");
util::exec_cmdline("xz", format!("-T0 {}", self.target.display())) util::exec_cmdline("xz", format!("-T0 {}", self.image_data.display()))
.context(format!("failed to decompress {}", self.target.display()))?; .context(format!("failed to compress {}", self.image_data.display()))?;
// Rename back to original image_data filename
let xz_filename = UpdateBuilder::target_filename(&self.config) + ".xz";
fs::rename(self.config.workdir_path(&xz_filename), &self.image_data)?;
}
Ok(()) Ok(())
} }
fn write_final_image(&self) -> Result<()> { fn write_final_image(&self) -> Result<()> {
let header = self.generate_header()?; let header = self.generate_header()?;
let filename = format!("{}.img", UpdateBuilder::target_filename(&self.config)); let filename = format!("{}.img", UpdateBuilder::target_filename(&self.config));
let mut image_path = self.config.workdir_path(&filename); let image_path = self.config.workdir_path(&filename);
let mut out = File::create(&image_path) let mut out = File::create(&image_path)
.context(format!("could not open output file {}", image_path.display()))?; .context(format!("could not open output file {}", image_path.display()))?;
header.write_header(&out)?; header.write_header(&out)?;
image_path.set_extension("xz"); let mut data = File::open(&self.image_data)
let mut data = File::open(&image_path) .context(format!("could not open image data file {}", self.image_data.display()))?;
.context(format!("could not open compressed image data file {}", image_path.display()))?;
io::copy(&mut data, &mut out) io::copy(&mut data, &mut out)
.context("error copying image data to output file")?; .context("error copying image data to output file")?;
Ok(()) Ok(())
@ -149,7 +153,10 @@ impl UpdateBuilder {
fn generate_header(&self) -> Result<ImageHeader> { fn generate_header(&self) -> Result<ImageHeader> {
let hdr = ImageHeader::new(); let hdr = ImageHeader::new();
if self.config.compress() {
hdr.set_flag(ImageHeader::FLAG_DATA_COMPRESSED); hdr.set_flag(ImageHeader::FLAG_DATA_COMPRESSED);
}
let metainfo = self.generate_metainfo(); let metainfo = self.generate_metainfo();
fs::write(self.config.workdir_path("metainfo"), &metainfo)?; fs::write(self.config.workdir_path("metainfo"), &metainfo)?;