From be5e8616a230038f6d0c360d17bcd31702344a34 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 9 Jun 2022 13:15:23 +1000 Subject: [PATCH] Impl new fn `helpers::extracter::untar` Signed-off-by: Jiahao XU --- src/helpers/extracter.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/helpers/extracter.rs b/src/helpers/extracter.rs index c851b0c8..d0e2ea6e 100644 --- a/src/helpers/extracter.rs +++ b/src/helpers/extracter.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::fs::File; use std::io::Read; use std::path::Path; @@ -11,6 +12,31 @@ use zstd::stream::Decoder as ZstdDecoder; use crate::{BinstallError, PkgFmt}; +fn untar( + dat: impl Read, + path: &Path, + desired_outputs: Option<&[Cow<'_, Path>]>, +) -> Result<(), BinstallError> { + let mut tar = Archive::new(dat); + + if let Some(desired_outputs) = desired_outputs { + for res in tar.entries()? { + let mut entry = res?; + let entry_path = entry.path()?; + + if desired_outputs.contains(&entry_path) { + let dst = path.join(entry_path); + + entry.unpack(dst)?; + } + } + } else { + tar.unpack(path)?; + } + + Ok(()) +} + /// Extract files from the specified source onto the specified path. /// /// * `fmt` - must not be `PkgFmt::Bin` or `PkgFmt::Zip`.