[pbs-devel] [PATCH v2 proxmox-backup 3/4] pxar: add UniqueContext helper
Gabriel Goller
g.goller at proxmox.com
Tue Feb 20 11:28:52 CET 2024
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 at proxmox.com>
---
pbs-client/src/pxar/create.rs | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/pbs-client/src/pxar/create.rs b/pbs-client/src/pxar/create.rs
index 06f396e0..679b3c62 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,12 +89,32 @@ 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))
+ }
+ }
}
}
-
#[derive(Eq, PartialEq, Hash)]
struct HardLinkInfo {
st_dev: u64,
@@ -242,7 +263,8 @@ 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;
--
2.43.0
More information about the pbs-devel
mailing list