Skip to main content

inspect_gzip/
inspect_gzip.rs

1//! Inspect a saved gzip artifact without writing decoded bytes or printing them.
2
3use ic_host_tools::artifact::{Sha256Digest, decode_gzip, read_file};
4use std::{error::Error, io, path::Path};
5
6fn main() -> Result<(), Box<dyn Error>> {
7    let mut args = std::env::args_os().skip(1);
8    let path = args.next().ok_or_else(|| {
9        io::Error::new(
10            io::ErrorKind::InvalidInput,
11            "usage: inspect_gzip PATH MAX_COMPRESSED_BYTES MAX_DECODED_BYTES",
12        )
13    })?;
14    let mut limit = || -> Result<usize, Box<dyn Error>> {
15        Ok(args
16            .next()
17            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "missing byte limit"))?
18            .to_str()
19            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "limits must be UTF-8"))?
20            .parse()?)
21    };
22    let max_compressed = limit()?;
23    let max_decoded = limit()?;
24    if args.next().is_some() {
25        return Err(io::Error::new(io::ErrorKind::InvalidInput, "unexpected argument").into());
26    }
27    let compressed = read_file(Path::new(&path), max_compressed)?;
28    let decoded = decode_gzip(&compressed, max_compressed, max_decoded)?;
29    println!(
30        "compressed_bytes={} sha256={}",
31        compressed.len(),
32        Sha256Digest::compute(&compressed)
33    );
34    println!(
35        "decoded_bytes={} sha256={}",
36        decoded.len(),
37        Sha256Digest::compute(&decoded)
38    );
39    Ok(())
40}