Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions crates/core/src/backend/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ impl ReadSource for LocalSource {
LocalSourceWalker {
walker: self.builder.build(),
save_opts: self.save_opts,
pending_error: None,
}
}
}
Expand All @@ -301,12 +302,17 @@ pub struct LocalSourceWalker {
walker: Walk,
/// The save options to use.
save_opts: LocalSourceSaveOptions,
/// A metadata error to report after yielding the readable entry.
pending_error: Option<Box<RusticError>>,
}

impl Iterator for LocalSourceWalker {
type Item = RusticResult<ReadSourceEntry<OpenFile>>;

fn next(&mut self) -> Option<Self::Item> {
if let Some(err) = self.pending_error.take() {
return Some(Err(err));
}
match self.walker.next() {
// ignore root dir, i.e. an entry with depth 0 of type dir
Some(Ok(entry)) if entry.depth() == 0 && entry.file_type().unwrap().is_dir() => {
Expand Down Expand Up @@ -336,14 +342,26 @@ impl Iterator for LocalSourceWalker {
}
})?;
let path = entry.path().display().to_string();
self.save_opts.map_entry(entry).map_err(|err| {
let (entry, xattr_error) =
self.save_opts
.map_entry_with_xattr_error(entry)
.map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to map directory entry `{path}` to a backup source entry.",
err,
)
.attach_context("path", path)
})?;
self.pending_error = xattr_error.map(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to map directory entry `{path}` to a backup source entry.",
"Failed to read extended attributes for `{path}`.",
err,
)
.attach_context("path", path)
})
.attach_context("path", entry.path.display().to_string())
});
Ok(entry)
})
}
}
174 changes: 139 additions & 35 deletions crates/core/src/backend/ignore/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{ffi::OsStr, path::Path};
use derive_setters::Setters;
use ignore::DirEntry;
use jiff::Timestamp;
#[cfg(all(not(windows), not(target_os = "openbsd")))]
use log::debug;
use log::warn;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
Expand Down Expand Up @@ -77,8 +79,18 @@ impl LocalSourceSaveOptions {
/// # Errors
///
/// * If metadata could not be read.
/// * If the xattr of the entry could not be read.
pub fn map_entry(self, entry: DirEntry) -> IgnoreResult<ReadSourceEntry<OpenFile>> {
let (entry, xattr_error) = self.map_entry_with_xattr_error(entry)?;
if let Some(err) = xattr_error {
warn!("Failed to read extended attributes: {err}");
}
Ok(entry)
}

pub(super) fn map_entry_with_xattr_error(
self,
entry: DirEntry,
) -> IgnoreResult<(ReadSourceEntry<OpenFile>, Option<IgnoreErrorKind>)> {
let name = entry.file_name();
let m = entry
.metadata()
Expand All @@ -105,12 +117,10 @@ impl LocalSourceSaveOptions {
.set_devid
.unwrap_or_default()
.map_or_else(|| Self::device_id(&m), Self::hardlink(&m));
let xattr = || {
Self::xattrs(entry.path())
.inspect_err(|err| warn!("ignoring error obtaining xargs: {err}"))
.unwrap_or_default()
let (extended_attributes, xattr_error) = match self.set_xattrs.unwrap_or_default() {
XattrOption::Yes => Self::xattrs(entry.path()),
XattrOption::No => (Vec::new(), None),
};
let extended_attributes = self.set_xattrs.unwrap_or_default().map_or_else(xattr);
let (mode, inode, links) = Self::nix_infos(&m);

let meta = Metadata {
Expand All @@ -132,7 +142,7 @@ impl LocalSourceSaveOptions {
let node = self.to_node(&entry, &m, meta)?;
let path = entry.into_path();
let open = Some(OpenFile(path.clone()));
Ok(ReadSourceEntry { path, node, open })
Ok((ReadSourceEntry { path, node, open }, xattr_error))
}

fn to_node(
Expand Down Expand Up @@ -191,37 +201,58 @@ impl LocalSourceSaveOptions {
(Some(mode), inode, links)
}

/// List [`ExtendedAttribute`] for a [`Node`] located at `path`
///
/// # Argument
///
/// * `path` to the [`Node`] for which to list attributes
///
/// # Errors
///
/// * If Xattr couldn't be listed or couldn't be read
/// List [`ExtendedAttribute`]s for a [`Node`] located at `path`, preserving readable
/// attributes even if another attribute fails to read.
#[cfg(not(target_os = "openbsd"))]
fn xattrs(path: &Path) -> IgnoreResult<Vec<ExtendedAttribute>> {
xattr::list(path)
.map_err(|err| IgnoreErrorKind::ErrorXattr {
path: path.to_path_buf(),
source: err,
})?
.map(|name| {
Ok(ExtendedAttribute {
fn xattrs(path: &Path) -> (Vec<ExtendedAttribute>, Option<IgnoreErrorKind>) {
let (attrs, error) =
Self::collect_xattrs(path, xattr::list(path), |p, name| xattr::get(p, name));
let error = error.map(|source| IgnoreErrorKind::ErrorXattr {
path: path.to_path_buf(),
source,
});
(attrs, error)
}

#[cfg(not(target_os = "openbsd"))]
fn collect_xattrs(
path: &Path,
names: std::io::Result<impl IntoIterator<Item = std::ffi::OsString>>,
mut get: impl FnMut(&Path, &OsStr) -> std::io::Result<Option<Vec<u8>>>,
) -> (Vec<ExtendedAttribute>, Option<std::io::Error>) {
let names = match names {
Ok(names) => names,
Err(err) if err.kind() == std::io::ErrorKind::Unsupported => {
debug!(
"extended attributes not supported for {}: {err}",
path.display()
);
return (Vec::new(), None);
}
Err(err) => return (Vec::new(), Some(err)),
};

let mut attrs = Vec::new();
let mut first_error = None;
for name in names {
match get(path, &name) {
Ok(Some(value)) => attrs.push(ExtendedAttribute {
name: name.to_string_lossy().to_string(),
value: xattr::get(path, name).map_err(|err| IgnoreErrorKind::ErrorXattr {
path: path.to_path_buf(),
source: err,
})?,
})
})
.collect::<IgnoreResult<Vec<ExtendedAttribute>>>()
value: Some(value),
}),
Ok(None) => {}
Err(err) => {
// Count the affected entry once, and still read the remaining names.
_ = first_error.get_or_insert(err);
}
}
}
(attrs, first_error)
}

#[cfg(target_os = "openbsd")]
fn xattrs(_path: &Path) -> IgnoreResult<Vec<ExtendedAttribute>> {
Ok(Vec::new())
fn xattrs(_path: &Path) -> (Vec<ExtendedAttribute>, Option<IgnoreErrorKind>) {
(Vec::new(), None)
}

fn to_node_other(self, name: &OsStr, m: &std::fs::Metadata, meta: Metadata) -> Node {
Expand Down Expand Up @@ -267,11 +298,84 @@ impl LocalSourceSaveOptions {
(None, 0, 0)
}

fn xattrs(_path: &Path) -> IgnoreResult<Vec<ExtendedAttribute>> {
Ok(Vec::new())
fn xattrs(_path: &Path) -> (Vec<ExtendedAttribute>, Option<IgnoreErrorKind>) {
(Vec::new(), None)
}

fn to_node_other(self, name: &OsStr, _m: &std::fs::Metadata, meta: Metadata) -> Node {
Node::new_node(name, NodeType::File, meta)
}
}

#[cfg(all(test, not(windows), not(target_os = "openbsd")))]
mod tests {
use std::{ffi::OsString, io};

use super::*;

#[test]
fn failed_xattr_values_report_one_error_and_preserve_readable_values() {
let (attrs, error) = LocalSourceSaveOptions::collect_xattrs(
Path::new("file.txt"),
Ok(["user.first", "user.unsupported", "user.denied", "user.last"].map(OsString::from)),
|_, name| match name.to_str().unwrap() {
"user.first" => Ok(Some(b"one".to_vec())),
"user.unsupported" => Err(io::ErrorKind::Unsupported.into()),
"user.denied" => Err(io::ErrorKind::PermissionDenied.into()),
"user.last" => Ok(Some(b"two".to_vec())),
_ => unreachable!(),
},
);

assert_eq!(
attrs,
vec![
ExtendedAttribute {
name: "user.first".to_string(),
value: Some(b"one".to_vec()),
},
ExtendedAttribute {
name: "user.last".to_string(),
value: Some(b"two".to_vec()),
},
]
);
assert_eq!(error.unwrap().kind(), io::ErrorKind::Unsupported);
}

#[test]
fn unsupported_xattr_filesystem_does_not_report_error() {
let (attrs, error) = LocalSourceSaveOptions::collect_xattrs(
Path::new("sshfs_file.txt"),
Err::<Vec<OsString>, _>(io::ErrorKind::Unsupported.into()),
|_, _| unreachable!("listing an unsupported filesystem yields no names"),
);

assert_eq!(attrs, Vec::new());
assert!(error.is_none());
}

#[test]
fn vanished_xattr_between_list_and_get_is_skipped_without_error() {
let (attrs, error) = LocalSourceSaveOptions::collect_xattrs(
Path::new("race.txt"),
Ok(["user.gone", "user.present"].map(OsString::from)),
|_, name| {
if name == "user.gone" {
Ok(None)
} else {
Ok(Some(Vec::new()))
}
},
);

assert_eq!(
attrs,
vec![ExtendedAttribute {
name: "user.present".to_string(),
value: Some(Vec::new()),
}]
);
assert!(error.is_none());
}
}
3 changes: 2 additions & 1 deletion crates/core/src/repofile/snapshotfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ pub struct SnapshotSummary {
/// Total duration that the rustic command ran in seconds
pub total_duration: f64,

/// Number of source files/directories that could not be read during this backup run.
/// Number of source read errors during this backup. Unreadable extended attributes
/// contribute one error per affected entry.
///
/// Serialized only when non-zero so existing restic/rustic snapshots stay unchanged.
#[serde(default, skip_serializing_if = "u64_is_zero")]
Expand Down
Loading