public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Max Carrara <m.carrara@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [RFC PATCH proxmox-backup 2/2] pxar-bin: Use async instead of sync extractor
Date: Mon, 28 Aug 2023 16:42:04 +0200	[thread overview]
Message-ID: <20230828144204.3591503-3-m.carrara@proxmox.com> (raw)
In-Reply-To: <20230828144204.3591503-1-m.carrara@proxmox.com>

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@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





  parent reply	other threads:[~2023-08-28 14:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-28 14:42 [pbs-devel] [RFC PATCH proxmox-backup 0/2] Introduce experimental `AsyncExtractor<T>` Max Carrara
2023-08-28 14:42 ` [pbs-devel] [RFC PATCH proxmox-backup 1/2] pbs-client: pxar: Add prototype implementation of `AsyncExtractor<T>` Max Carrara
2023-08-28 14:42 ` Max Carrara [this message]
2023-12-15 16:33 ` [pbs-devel] [RFC PATCH proxmox-backup 0/2] Introduce experimental `AsyncExtractor<T>` Max Carrara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230828144204.3591503-3-m.carrara@proxmox.com \
    --to=m.carrara@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal