From 17fcac7e63828195728238c368fd6c547ed9059d Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sun, 12 Jun 2022 17:03:39 +1000 Subject: [PATCH] Refactor: Simplify `untar` with new enum `Op` Signed-off-by: Jiahao XU --- src/helpers/async_extracter.rs | 10 ++----- src/helpers/extracter.rs | 52 ++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/helpers/async_extracter.rs b/src/helpers/async_extracter.rs index 8564d19e..80ed397c 100644 --- a/src/helpers/async_extracter.rs +++ b/src/helpers/async_extracter.rs @@ -225,8 +225,7 @@ where extract_compressed_from_readable::( ReadableRx::new(&mut rx), fmt, - &path, - None, + Op::UnpackToPath(&path), ) }) .await @@ -246,12 +245,7 @@ where extract_impl(stream, move |mut rx| { fs::create_dir_all(path.parent().unwrap())?; - extract_compressed_from_readable( - ReadableRx::new(&mut rx), - fmt, - &*path, - Some(&mut visitor), - )?; + extract_compressed_from_readable(ReadableRx::new(&mut rx), fmt, Op::Visit(&mut visitor))?; Ok(visitor) }) diff --git a/src/helpers/extracter.rs b/src/helpers/extracter.rs index c24634cf..12133da2 100644 --- a/src/helpers/extracter.rs +++ b/src/helpers/extracter.rs @@ -23,23 +23,28 @@ impl TarEntriesVisitor for &mut V { } } +#[derive(Debug)] +pub(super) enum Op<'a, V: TarEntriesVisitor> { + UnpackToPath(&'a Path), + Visit(V), +} + /// * `f` - If Some, then this function will pass /// the entries of the `dat` to it and let it decides /// what to do with the tar. -fn untar( - dat: R, - path: &Path, - visitor: Option, -) -> Result<(), BinstallError> { +fn untar(dat: R, op: Op<'_, V>) -> Result<(), BinstallError> { let mut tar = Archive::new(dat); - if let Some(mut visitor) = visitor { - debug!("Untaring with filter"); + match op { + Op::Visit(mut visitor) => { + debug!("Untaring with callback"); - visitor.visit(tar.entries()?)?; - } else { - debug!("Untaring entire tar"); - tar.unpack(path)?; + visitor.visit(tar.entries()?)?; + } + Op::UnpackToPath(path) => { + debug!("Untaring entire tar"); + tar.unpack(path)?; + } } debug!("Untaring completed"); @@ -57,42 +62,47 @@ fn untar( pub(super) fn extract_compressed_from_readable( dat: R, fmt: TarBasedFmt, - path: &Path, - visitor: Option, + op: Op<'_, V>, ) -> Result<(), BinstallError> { use TarBasedFmt::*; + let msg = if let Op::UnpackToPath(path) = op { + format!("destination: {path:#?}") + } else { + "process in-memory".to_string() + }; + match fmt { Tar => { // Extract to install dir - debug!("Extracting from tar archive to `{path:?}`"); + debug!("Extracting from tar archive: {msg}"); - untar(dat, path, visitor)? + untar(dat, op)? } Tgz => { // Extract to install dir - debug!("Decompressing from tgz archive to `{path:?}`"); + debug!("Decompressing from tgz archive {msg}"); let tar = GzDecoder::new(dat); - untar(tar, path, visitor)?; + untar(tar, op)?; } Txz => { // Extract to install dir - debug!("Decompressing from txz archive to `{path:?}`"); + debug!("Decompressing from txz archive {msg}"); let tar = XzDecoder::new(dat); - untar(tar, path, visitor)?; + untar(tar, op)?; } Tzstd => { // Extract to install dir - debug!("Decompressing from tzstd archive to `{path:?}`"); + debug!("Decompressing from tzstd archive {msg}"); // The error can only come from raw::Decoder::with_dictionary // as of zstd 0.10.2 and 0.11.2, which is specified // as &[] by ZstdDecoder::new, thus ZstdDecoder::new // should not return any error. let tar = ZstdDecoder::with_buffer(dat)?; - untar(tar, path, visitor)?; + untar(tar, op)?; } };