use libflate::gzip;
use std::{fs, path};
use tar;
#[derive(Debug, thiserror::Error)]
pub enum RenderError {
#[error("wrong target path {}: must be absolute path to existing directory", _0.display())]
WrongTargetPath(path::PathBuf),
#[error("io error")]
Io(#[from] std::io::Error)
}
pub fn unpack(layers: &[Vec<u8>], target_dir: &path::Path) -> Result<(), RenderError> {
if !target_dir.is_absolute() || !target_dir.exists() || !target_dir.is_dir() {
return Err(RenderError::WrongTargetPath(target_dir.to_path_buf()));
}
for l in layers {
let gz_dec = gzip::Decoder::new(l.as_slice())?;
let mut archive = tar::Archive::new(gz_dec);
archive.set_preserve_permissions(true);
archive.set_unpack_xattrs(true);
archive.unpack(target_dir)?;
let gz_dec = gzip::Decoder::new(l.as_slice())?;
let mut archive = tar::Archive::new(gz_dec);
for entry in archive.entries()? {
let file = entry?;
let path = file.path()?;
let parent = path.parent().unwrap_or_else(|| path::Path::new("/"));
if let Some(fname) = path.file_name() {
let wh_name = fname.to_string_lossy();
if wh_name == ".wh..wh..opq" {
} else if wh_name.starts_with(".wh.") {
let rel_parent =
path::PathBuf::from("./".to_string() + &parent.to_string_lossy());
let real_name = wh_name.trim_start_matches(".wh.");
let abs_real_path = target_dir.join(&rel_parent).join(real_name);
fs::remove_dir_all(abs_real_path)?;
let abs_wh_path = target_dir.join(&rel_parent).join(fname);
fs::remove_dir_all(abs_wh_path)?;
};
}
}
}
Ok(())
}