[pbs-devel] [RFC PATCH proxmox-backup 2/2] pxar-bin: Use async instead of sync extractor

Max Carrara m.carrara at proxmox.com
Mon Aug 28 16:42:04 CEST 2023


This commit serves as an example of how the new `AsyncExtractor<T>`
may be used. The extraction options are created using the new
builder patterns as well.

In this case, the sync version can almost be directly swapped in
place with the async version.

Signed-off-by: Max Carrara <m.carrara at proxmox.com>
---
 pxar-bin/src/main.rs | 91 +++++++++++++++++++++++---------------------
 1 file changed, 47 insertions(+), 44 deletions(-)

diff --git a/pxar-bin/src/main.rs b/pxar-bin/src/main.rs
index bc044035..6dc7d375 100644
--- a/pxar-bin/src/main.rs
+++ b/pxar-bin/src/main.rs
@@ -12,30 +12,12 @@ use futures::select;
 use tokio::signal::unix::{signal, SignalKind};

 use pathpatterns::{MatchEntry, MatchType, PatternFlag};
-use pbs_client::pxar::{
-    format_single_line_entry, Flags, OverwriteFlags, PxarExtractOptions, ENCODER_MAX_ENTRIES,
-};
+use pbs_client::pxar::aio;
+use pbs_client::pxar::{format_single_line_entry, Flags, OverwriteFlags, ENCODER_MAX_ENTRIES};

 use proxmox_router::cli::*;
 use proxmox_schema::api;

-fn extract_archive_from_reader<R: std::io::Read>(
-    reader: &mut R,
-    target: &str,
-    feature_flags: Flags,
-    options: PxarExtractOptions,
-) -> Result<(), Error> {
-    pbs_client::pxar::extract_archive(
-        pxar::decoder::Decoder::from_std(reader)?,
-        Path::new(target),
-        feature_flags,
-        |path| {
-            log::debug!("{:?}", path);
-        },
-        options,
-    )
-}
-
 #[api(
     input: {
         properties: {
@@ -124,7 +106,7 @@ fn extract_archive_from_reader<R: std::io::Read>(
 )]
 /// Extract an archive.
 #[allow(clippy::too_many_arguments)]
-fn extract_archive(
+async fn extract_archive(
     archive: String,
     pattern: Option<Vec<String>>,
     target: Option<String>,
@@ -193,38 +175,59 @@ fn extract_archive(

     let extract_match_default = match_list.is_empty();

+    // Use the new options builder for convenienve
+    let mut options_builder = aio::PxarExtractOptions::builder(PathBuf::from(target));
+
+    options_builder
+        .feature_flags(feature_flags)
+        .overwrite_flags(overwrite_flags)
+        .allow_existing_dirs(allow_existing_dirs)
+        .default_match(extract_match_default)
+        .push_match_list(match_list);
+
+    // The builder makes it easier to conditionally configure the extractor
     let was_ok = Arc::new(AtomicBool::new(true));
-    let on_error = if strict {
-        // by default errors are propagated up
-        None
-    } else {
+    if strict {
         let was_ok = Arc::clone(&was_ok);
-        // otherwise we want to log them but not act on them
-        Some(Box::new(move |err| {
+        // log errors but don't act upon them
+
+        let error_handler = Box::new(move |error| {
             was_ok.store(false, Ordering::Release);
-            log::error!("error: {}", err);
+            log::error!("error: {}", error);
             Ok(())
-        })
-            as Box<dyn FnMut(Error) -> Result<(), Error> + Send>)
-    };
+        });

-    let options = PxarExtractOptions {
-        match_list: &match_list,
-        allow_existing_dirs,
-        overwrite_flags,
-        extract_match_default,
-        on_error,
-    };
+        options_builder.error_handler(error_handler);
+    }
+
+    let options = options_builder.build();

     if archive == "-" {
-        let stdin = std::io::stdin();
-        let mut reader = stdin.lock();
-        extract_archive_from_reader(&mut reader, target, feature_flags, options)?;
+        let stdin = tokio::io::stdin();
+        let decoder =
+            pxar::decoder::aio::Decoder::from_tokio(tokio::io::BufReader::new(stdin)).await?;
+        let mut extractor = aio::AsyncExtractor::new(decoder, options);
+
+        while let Some(result) = extractor.next().await {
+            if let Err(error) = result {
+                bail!(
+                    "encountered unexpected error during extraction:\n{:?}",
+                    error
+                )
+            }
+        }
     } else {
         log::debug!("PXAR extract: {}", archive);
-        let file = std::fs::File::open(archive)?;
-        let mut reader = std::io::BufReader::new(file);
-        extract_archive_from_reader(&mut reader, target, feature_flags, options)?;
+
+        let mut extractor = aio::AsyncExtractor::with_file(archive, options).await?;
+        while let Some(result) = extractor.next().await {
+            if let Err(error) = result {
+                bail!(
+                    "encountered unexpected error during extraction:\n{:?}",
+                    error
+                )
+            }
+        }
     }

     if !was_ok.load(Ordering::Acquire) {
--
2.39.2






More information about the pbs-devel mailing list