restore: Add error handling

This commit is contained in:
Alexander Weiss 2022-07-07 12:08:52 +02:00
parent b9656a720c
commit b2f251c086
2 changed files with 30 additions and 20 deletions

View File

@ -194,17 +194,18 @@ impl LocalBackend {
fs::create_dir(&dirname).unwrap();
}
pub fn set_times(&self, item: impl AsRef<Path>, meta: &Metadata) {
pub fn set_times(&self, item: impl AsRef<Path>, meta: &Metadata) -> Result<()> {
let filename = self.path.join(item);
if let Some(mtime) = meta.mtime.map(|t| FileTime::from_system_time(t.into())) {
set_file_mtime(&filename, mtime).unwrap();
set_file_mtime(&filename, mtime)?;
}
if let Some(atime) = meta.atime.map(|t| FileTime::from_system_time(t.into())) {
set_file_atime(&filename, atime).unwrap();
set_file_atime(&filename, atime)?;
}
Ok(())
}
pub fn set_user_group(&self, item: impl AsRef<Path>, meta: &Metadata) {
pub fn set_user_group(&self, item: impl AsRef<Path>, meta: &Metadata) -> Result<()> {
let filename = self.path.join(item);
let user = meta
@ -222,24 +223,26 @@ impl LocalBackend {
// use gid from group if valid, else from saved gid (if saved)
let gid = group.map(|g| g.gid).or_else(|| meta.gid.map(Gid::from_raw));
chown(&filename, uid, gid).unwrap();
chown(&filename, uid, gid)?;
Ok(())
}
pub fn set_uid_gid(&self, item: impl AsRef<Path>, meta: &Metadata) {
pub fn set_uid_gid(&self, item: impl AsRef<Path>, meta: &Metadata) -> Result<()> {
let filename = self.path.join(item);
let uid = meta.uid.map(Uid::from_raw);
let gid = meta.gid.map(Gid::from_raw);
chown(&filename, uid, gid).unwrap();
chown(&filename, uid, gid)?;
Ok(())
}
pub fn set_permission(&self, item: impl AsRef<Path>, meta: &Metadata) {
pub fn set_permission(&self, item: impl AsRef<Path>, meta: &Metadata) -> Result<()> {
let filename = self.path.join(item);
let mode = map_mode_from_go(*meta.mode());
std::fs::set_permissions(&filename, fs::Permissions::from_mode(mode))
.unwrap_or_else(|_| panic!("error chmod {:?}", filename));
std::fs::set_permissions(&filename, fs::Permissions::from_mode(mode))?;
Ok(())
}
pub fn create_file(&self, item: impl AsRef<Path>, size: u64) {
@ -248,35 +251,36 @@ impl LocalBackend {
f.set_len(size).unwrap();
}
pub fn create_special(&self, item: impl AsRef<Path>, node: &Node) {
pub fn create_special(&self, item: impl AsRef<Path>, node: &Node) -> Result<()> {
let filename = self.path.join(item);
match node.node_type() {
NodeType::Symlink { linktarget } => {
symlink(linktarget, filename).unwrap();
symlink(linktarget, filename)?;
}
NodeType::Dev { device } => {
#[cfg(not(target_os = "macos"))]
let device = *device;
#[cfg(target_os = "macos")]
let device = *device as i32;
mknod(&filename, SFlag::S_IFBLK, Mode::empty(), device).unwrap();
mknod(&filename, SFlag::S_IFBLK, Mode::empty(), device)?;
}
NodeType::Chardev { device } => {
#[cfg(not(target_os = "macos"))]
let device = *device;
#[cfg(target_os = "macos")]
let device = *device as i32;
mknod(&filename, SFlag::S_IFCHR, Mode::empty(), device).unwrap();
mknod(&filename, SFlag::S_IFCHR, Mode::empty(), device)?;
}
NodeType::Fifo => {
mknod(&filename, SFlag::S_IFIFO, Mode::empty(), 0).unwrap();
mknod(&filename, SFlag::S_IFIFO, Mode::empty(), 0)?;
}
NodeType::Socket => {
mknod(&filename, SFlag::S_IFSOCK, Mode::empty(), 0).unwrap();
mknod(&filename, SFlag::S_IFSOCK, Mode::empty(), 0)?;
}
_ => {}
}
Ok(())
}
pub fn write_at(&self, item: impl AsRef<Path>, offset: u64, data: &[u8]) {

View File

@ -154,14 +154,20 @@ async fn restore_metadata(
let mut node_streamer = NodeStreamer::new(index, tree).await?;
while let Some((path, node)) = node_streamer.try_next().await? {
if !opts.dry_run {
dest.create_special(&path, &node);
dest.create_special(&path, &node)
.unwrap_or_else(|_| eprintln!("restore {:?}: creating special file failed.", path));
if opts.numeric_id {
dest.set_uid_gid(&path, node.meta());
dest.set_uid_gid(&path, node.meta())
.unwrap_or_else(|_| eprintln!("restore {:?}: setting UID/GID failed.", path));
} else {
dest.set_user_group(&path, node.meta());
dest.set_user_group(&path, node.meta()).unwrap_or_else(|_| {
eprintln!("restore {:?}: setting User/Group failed.", path)
});
}
dest.set_permission(&path, node.meta());
dest.set_permission(&path, node.meta())
.unwrap_or_else(|_| eprintln!("restore {:?}: chmod failed.", path));
dest.set_times(&path, node.meta())
.unwrap_or_else(|_| eprintln!("restore {:?}: setting file times failed.", path));
}
}