From: Max Carrara <m.carrara@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH proxmox-backup 3/4] pxar: add UniqueContext helper
Date: Fri, 16 Feb 2024 18:44:44 +0100 [thread overview]
Message-ID: <1b3d2c4e-a520-4421-98f7-c5c99543cfc5@proxmox.com> (raw)
In-Reply-To: <20240216153317.323260-4-g.goller@proxmox.com>
On 2/16/24 16:33, Gabriel Goller wrote:
> To create a pxar archive, we recursively traverse the target folder.
> If there is an error further down and we add a context using anyhow,
> the context will be duplicated and we get an output like:
>
>> Error: error at "xattr/xattr.txt": error at "xattr/xattr.txt": E2BIG [skip]
>
> This is obviously not optimal, so in recursive contexts we can use the
> UniqueContext, which quickly checks the context from the last item in
> the error chain and only adds it if it is unique.
>
> Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
> ---
> pbs-client/src/pxar/create.rs | 25 ++++++++++++++++++++++++-
> 1 file changed, 24 insertions(+), 1 deletion(-)
>
> diff --git a/pbs-client/src/pxar/create.rs b/pbs-client/src/pxar/create.rs
> index 06f396e0..7bec35c4 100644
> --- a/pbs-client/src/pxar/create.rs
> +++ b/pbs-client/src/pxar/create.rs
> @@ -1,5 +1,6 @@
> use std::collections::{HashMap, HashSet};
> use std::ffi::{CStr, CString, OsStr};
> +use std::fmt::Display;
> use std::io::{self, Read};
> use std::os::unix::ffi::OsStrExt;
> use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
> @@ -88,8 +89,30 @@ pub fn is_virtual_file_system(magic: i64) -> bool {
> SYSFS_MAGIC)
> }
>
> +trait UniqueContext<T> {
> + fn unique_context<S>(self, context: S) -> Result<T, anyhow::Error>
> + where
> + S: Display + Send + Sync + 'static;
> }
>
> +impl<T> UniqueContext<T> for Result<T, anyhow::Error>
> +{
> + fn unique_context<S>(self, context: S) -> Result<T, anyhow::Error>
> + where
> + S: Display + Send + Sync + 'static
> + {
> + match self {
> + Ok(ok) => Ok(ok),
> + Err(err) => {
> + let last_error = err.chain().next();
> + if let Some(e) = last_error {
> + if e.to_string() == context.to_string() {
> + return Err(err);
> + }
> + }
> + Err(err.context(context))
> + },
> + }
> }
> }
The trait plus the impl could perhaps be inlined inside the body of
`Archiver::archive_dir_contents`, as `unique_context()` is only used in there,
but I think it's also fine if it says where it is. Looks good otherwise!
>
> @@ -242,7 +265,7 @@ impl Archiver {
> (self.callback)(&file_entry.path)?;
> self.path = file_entry.path;
> self.add_entry(encoder, dir_fd, &file_entry.name, &file_entry.stat)
> - .await.context(format!("error at {:?}", self.path))?;
> + .await.unique_context(format!("error at {:?}", self.path))?;
> }
> self.path = old_path;
> self.entry_counter = entry_counter;
next prev parent reply other threads:[~2024-02-16 17:45 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-16 15:33 [pbs-devel] [PATCH proxmox{, -backup} 0/4] output full anyhow context in client Gabriel Goller
2024-02-16 15:33 ` [pbs-devel] [PATCH proxmox 1/4] CLI: print fatal errors including causes Gabriel Goller
2024-02-16 15:33 ` [pbs-devel] [PATCH proxmox-backup 2/4] pxar: remove ArchiveError Gabriel Goller
2024-02-16 15:33 ` [pbs-devel] [PATCH proxmox-backup 3/4] pxar: add UniqueContext helper Gabriel Goller
2024-02-16 17:44 ` Max Carrara [this message]
2024-02-16 15:33 ` [pbs-devel] [PATCH proxmox-backup 4/4] pxar: use anyhow::Error in PxarBackupStream Gabriel Goller
2024-02-16 17:47 ` Max Carrara
2024-02-19 10:53 ` Gabriel Goller
2024-02-16 17:49 ` [pbs-devel] [PATCH proxmox{, -backup} 0/4] output full anyhow context in client Max Carrara
2024-02-16 17:55 ` Max Carrara
2024-02-19 10:54 ` Gabriel Goller
2024-02-20 10:30 ` Gabriel Goller
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=1b3d2c4e-a520-4421-98f7-c5c99543cfc5@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