From 467f7f68341245f56bcfda85568928c430b7bfc9 Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Mon, 13 Jun 2022 01:12:21 +1000
Subject: [PATCH] Refactor: Call `create_tar_decoder` directly

in `extract_tar_based_stream*`.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/helpers/async_extracter.rs | 17 ++++++-----
 src/helpers/extracter.rs       | 54 ----------------------------------
 2 files changed, 10 insertions(+), 61 deletions(-)

diff --git a/src/helpers/async_extracter.rs b/src/helpers/async_extracter.rs
index 04a6117b..2ae31a23 100644
--- a/src/helpers/async_extracter.rs
+++ b/src/helpers/async_extracter.rs
@@ -21,6 +21,7 @@ use std::path::Path;
 
 use bytes::Bytes;
 use futures_util::stream::{Stream, StreamExt};
+use log::debug;
 use scopeguard::{guard, ScopeGuard};
 use tar::Entries;
 use tempfile::tempfile;
@@ -222,11 +223,10 @@ where
         Box::new(move |rx| {
             fs::create_dir_all(path.parent().unwrap())?;
 
-            extract_compressed_from_readable::<DummyVisitor, _>(
-                ReadableRx::new(rx),
-                fmt,
-                Op::UnpackToPath(&path),
-            )
+            debug!("Extracting from {fmt} archive to {path:#?}");
+            create_tar_decoder(ReadableRx::new(rx), fmt)?.unpack(path)?;
+
+            Ok(())
         }),
     )
     .await
@@ -243,8 +243,11 @@ where
     extract_impl(
         stream,
         Box::new(move |rx| {
-            extract_compressed_from_readable(ReadableRx::new(rx), fmt, Op::Visit(&mut visitor))
-                .map(|_| visitor)
+            debug!("Extracting from {fmt} archive to process it in memory");
+
+            let mut tar = create_tar_decoder(ReadableRx::new(rx), fmt)?;
+            visitor.visit(tar.entries()?)?;
+            Ok(visitor)
         }),
     )
     .await
diff --git a/src/helpers/extracter.rs b/src/helpers/extracter.rs
index 5994882f..ee5129b3 100644
--- a/src/helpers/extracter.rs
+++ b/src/helpers/extracter.rs
@@ -23,36 +23,6 @@ impl<V: TarEntriesVisitor> 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<R: Read, V: TarEntriesVisitor>(
-    mut tar: Archive<R>,
-    op: Op<'_, V>,
-) -> Result<(), BinstallError> {
-    match op {
-        Op::Visit(mut visitor) => {
-            debug!("Untaring with callback");
-
-            visitor.visit(tar.entries()?)?;
-        }
-        Op::UnpackToPath(path) => {
-            debug!("Untaring entire tar");
-            tar.unpack(path)?;
-        }
-    }
-
-    debug!("Untaring completed");
-
-    Ok(())
-}
-
 pub(super) fn create_tar_decoder(
     dat: impl BufRead + 'static,
     fmt: TarBasedFmt,
@@ -75,30 +45,6 @@ pub(super) fn create_tar_decoder(
     Ok(Archive::new(r))
 }
 
-/// Extract files from the specified source onto the specified path.
-///
-///  * `fmt` - must not be `PkgFmt::Bin` or `PkgFmt::Zip`.
-///  * `filter` - If Some, then it will pass the path of the file to it
-///    and only extract ones which filter returns `true`.
-///    Note that this is a best-effort and it only works when `fmt`
-///    is not `PkgFmt::Bin` or `PkgFmt::Zip`.
-pub(super) fn extract_compressed_from_readable<V: TarEntriesVisitor, R: BufRead + 'static>(
-    dat: R,
-    fmt: TarBasedFmt,
-    op: Op<'_, V>,
-) -> Result<(), BinstallError> {
-    let msg = if let Op::UnpackToPath(path) = op {
-        format!("destination: {path:#?}")
-    } else {
-        "process in-memory".to_string()
-    };
-
-    debug!("Extracting from {fmt} archive: {msg}");
-
-    let tar = create_tar_decoder(dat, fmt)?;
-    untar(tar, op)
-}
-
 pub(super) fn unzip(dat: File, dst: &Path) -> Result<(), BinstallError> {
     debug!("Decompressing from zip archive to `{dst:?}`");