diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-02-03 09:11:49 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-02-04 09:14:01 +0200 |
commit | a2adcb5a90c15b473a2fcf114555443fba8a20ce (patch) | |
tree | 7ec36f244daa105b0da774d6705ef736f9135f64 /src/cmd/restore.rs | |
parent | bf08ea67ca035fc0e78364450599cefff7cd9bc6 (diff) | |
download | obnam2-a2adcb5a90c15b473a2fcf114555443fba8a20ce.tar.gz |
refactor: have per-module error enums
This means that a function that parses step bindings can't return an
error that the document is missing a title. Such an error return would
be nonsensical, and we use the Rust type system to prevent it, at a
small cost of being a bit verbose.
Additional benefit is that the library portion of Obnam doesn't return
anyhow::Result values anymore.
Diffstat (limited to 'src/cmd/restore.rs')
-rw-r--r-- | src/cmd/restore.rs | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index d783a70..16d5320 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -1,8 +1,8 @@ -use crate::client::BackupClient; use crate::client::ClientConfig; +use crate::client::{BackupClient, ClientError}; use crate::error::ObnamError; use crate::fsentry::{FilesystemEntry, FilesystemKind}; -use crate::generation::LocalGeneration; +use crate::generation::{LocalGeneration, LocalGenerationError}; use indicatif::{ProgressBar, ProgressStyle}; use libc::{fchmod, futimens, timespec}; use log::{debug, error, info}; @@ -11,11 +11,12 @@ use std::io::prelude::*; use std::io::Error; use std::os::unix::fs::symlink; use std::os::unix::io::AsRawFd; +use std::path::StripPrefixError; use std::path::{Path, PathBuf}; use structopt::StructOpt; use tempfile::NamedTempFile; -pub fn restore(config: &ClientConfig, gen_ref: &str, to: &Path) -> anyhow::Result<()> { +pub fn restore(config: &ClientConfig, gen_ref: &str, to: &Path) -> Result<(), ObnamError> { // Create a named temporary file. We don't meed the open file // handle, so we discard that. let dbname = { @@ -27,10 +28,7 @@ pub fn restore(config: &ClientConfig, gen_ref: &str, to: &Path) -> anyhow::Resul let client = BackupClient::new(&config.server_url)?; let genlist = client.list_generations()?; - let gen_id: String = match genlist.resolve(gen_ref) { - None => return Err(ObnamError::UnknownGeneration(gen_ref.to_string()).into()), - Some(id) => id, - }; + let gen_id: String = genlist.resolve(gen_ref)?; info!("generation id is {}", gen_id); let gen = client.fetch_generation(&gen_id, &dbname)?; @@ -68,6 +66,26 @@ struct Opt { to: PathBuf, } +#[derive(Debug, thiserror::Error)] +pub enum RestoreError { + #[error(transparent)] + ClientError(#[from] ClientError), + + #[error(transparent)] + LocalGenerationError(#[from] LocalGenerationError), + + #[error(transparent)] + StripPrefixError(#[from] StripPrefixError), + + #[error(transparent)] + IoError(#[from] std::io::Error), + + #[error(transparent)] + SerdeYamlError(#[from] serde_yaml::Error), +} + +pub type RestoreResult<T> = Result<T, RestoreError>; + fn restore_generation( client: &BackupClient, gen: &LocalGeneration, @@ -75,7 +93,7 @@ fn restore_generation( entry: &FilesystemEntry, to: &Path, progress: &ProgressBar, -) -> anyhow::Result<()> { +) -> RestoreResult<()> { info!("restoring {:?}", entry); progress.set_message(&format!("{}", entry.pathbuf().display())); progress.inc(1); @@ -89,13 +107,13 @@ fn restore_generation( Ok(()) } -fn restore_directory(path: &Path) -> anyhow::Result<()> { +fn restore_directory(path: &Path) -> RestoreResult<()> { debug!("restoring directory {}", path.display()); std::fs::create_dir_all(path)?; Ok(()) } -fn restore_directory_metadata(entry: &FilesystemEntry, to: &Path) -> anyhow::Result<()> { +fn restore_directory_metadata(entry: &FilesystemEntry, to: &Path) -> RestoreResult<()> { let to = restored_path(entry, to)?; match entry.kind() { FilesystemKind::Directory => restore_metadata(&to, entry)?, @@ -107,7 +125,7 @@ fn restore_directory_metadata(entry: &FilesystemEntry, to: &Path) -> anyhow::Res Ok(()) } -fn restored_path(entry: &FilesystemEntry, to: &Path) -> anyhow::Result<PathBuf> { +fn restored_path(entry: &FilesystemEntry, to: &Path) -> RestoreResult<PathBuf> { let path = &entry.pathbuf(); let path = if path.is_absolute() { path.strip_prefix("/")? @@ -123,7 +141,7 @@ fn restore_regular( path: &Path, fileid: i64, entry: &FilesystemEntry, -) -> anyhow::Result<()> { +) -> RestoreResult<()> { debug!("restoring regular {}", path.display()); let parent = path.parent().unwrap(); debug!(" mkdir {}", parent.display()); @@ -140,7 +158,7 @@ fn restore_regular( Ok(()) } -fn restore_symlink(path: &Path, entry: &FilesystemEntry) -> anyhow::Result<()> { +fn restore_symlink(path: &Path, entry: &FilesystemEntry) -> RestoreResult<()> { debug!("restoring symlink {}", path.display()); let parent = path.parent().unwrap(); debug!(" mkdir {}", parent.display()); @@ -154,7 +172,7 @@ fn restore_symlink(path: &Path, entry: &FilesystemEntry) -> anyhow::Result<()> { Ok(()) } -fn restore_metadata(path: &Path, entry: &FilesystemEntry) -> anyhow::Result<()> { +fn restore_metadata(path: &Path, entry: &FilesystemEntry) -> RestoreResult<()> { debug!("restoring metadata for {}", entry.pathbuf().display()); let handle = File::open(path)?; |