use crate::unpack::media_types;
use std::io;
use std::path::Path;
pub const DEFAULT_ZSTD_LEVEL: i32 = 3;
pub fn pack_files_tar_zstd(
root: &Path,
files: &[&str],
level: i32,
) -> io::Result<(Vec<u8>, String)> {
let encoder = zstd::stream::write::Encoder::new(Vec::new(), level)?;
let mut builder = tar::Builder::new(encoder);
for name in files {
let path = root.join(name);
let mut f = std::fs::File::open(&path).map_err(|e| {
io::Error::new(
e.kind(),
format!("open {} for packing: {e}", path.display()),
)
})?;
builder
.append_file(name, &mut f)
.map_err(|e| io::Error::other(format!("append {} to layer: {e}", path.display())))?;
}
let encoder = builder
.into_inner()
.map_err(|e| io::Error::other(format!("finalize tar: {e}")))?;
let compressed = encoder
.finish()
.map_err(|e| io::Error::other(format!("finalize zstd: {e}")))?;
Ok((compressed, media_types::TAR_ZSTD.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::unpack::LayerUnpacker;
#[tokio::test]
async fn pack_then_unpack_roundtrips_files_flat() {
let src = tempfile::tempdir().unwrap();
std::fs::write(src.path().join("disk.img"), b"DISK-CONTENT").unwrap();
std::fs::write(src.path().join("hardware-model.bin"), b"HWMODEL").unwrap();
std::fs::write(src.path().join("aux.img"), b"AUXDATA").unwrap();
let (bytes, media_type) = pack_files_tar_zstd(
src.path(),
&["disk.img", "hardware-model.bin", "aux.img"],
DEFAULT_ZSTD_LEVEL,
)
.unwrap();
assert_eq!(media_type, media_types::TAR_ZSTD);
assert!(!bytes.is_empty());
let out = tempfile::tempdir().unwrap();
let mut unpacker = LayerUnpacker::new(out.path().to_path_buf());
unpacker
.unpack_layers(&[(bytes, media_type)])
.await
.unwrap();
assert_eq!(
std::fs::read(out.path().join("disk.img")).unwrap(),
b"DISK-CONTENT"
);
assert_eq!(
std::fs::read(out.path().join("hardware-model.bin")).unwrap(),
b"HWMODEL"
);
assert_eq!(
std::fs::read(out.path().join("aux.img")).unwrap(),
b"AUXDATA"
);
}
#[test]
fn missing_file_is_an_error() {
let src = tempfile::tempdir().unwrap();
let err = pack_files_tar_zstd(src.path(), &["nope.img"], DEFAULT_ZSTD_LEVEL).unwrap_err();
assert!(err.to_string().contains("nope.img"));
}
}