diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-07-22 17:59:13 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-07-23 19:23:12 +0300 |
commit | 34a039ee8b882e577faa3aa8298c1ad08fefa14f (patch) | |
tree | 91924c87caede2ffb676d5d7d2fb240eee42679a /src/cmd | |
parent | da4ac27a82029d562a9e9c48ce2a709692930cc1 (diff) | |
download | obnam2-34a039ee8b882e577faa3aa8298c1ad08fefa14f.tar.gz |
refactor: use async for "obnam restore"
Sponsored-by: author
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/restore.rs | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index d794fe4..458397d 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -1,5 +1,5 @@ use crate::backup_reason::Reason; -use crate::client::{BackupClient, ClientError}; +use crate::client::{AsyncBackupClient, ClientError}; use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::{FilesystemEntry, FilesystemKind}; @@ -17,6 +17,7 @@ use std::path::StripPrefixError; use std::path::{Path, PathBuf}; use structopt::StructOpt; use tempfile::NamedTempFile; +use tokio::runtime::Runtime; #[derive(Debug, StructOpt)] pub struct Restore { @@ -29,29 +30,37 @@ pub struct Restore { impl Restore { pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let rt = Runtime::new()?; + rt.block_on(self.run_async(config)) + } + + async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> { let temp = NamedTempFile::new()?; - let client = BackupClient::new(config)?; + let client = AsyncBackupClient::new(config)?; - let genlist = client.list_generations()?; + let genlist = client.list_generations().await?; let gen_id: String = genlist.resolve(&self.gen_id)?; info!("generation id is {}", gen_id); - let gen = client.fetch_generation(&gen_id, temp.path())?; + let gen = client.fetch_generation(&gen_id, temp.path()).await?; info!("restoring {} files", gen.file_count()?); let progress = create_progress_bar(gen.file_count()?, true); for file in gen.files()?.iter()? { let file = file?; match file.reason() { Reason::FileError => (), - _ => restore_generation( - &client, - &gen, - file.fileno(), - file.entry(), - &self.to, - &progress, - )?, + _ => { + restore_generation( + &client, + &gen, + file.fileno(), + file.entry(), + &self.to, + &progress, + ) + .await? + } } } for file in gen.files()?.iter()? { @@ -118,8 +127,8 @@ pub enum RestoreError { SetTimestamp(PathBuf, std::io::Error), } -fn restore_generation( - client: &BackupClient, +async fn restore_generation( + client: &AsyncBackupClient, gen: &LocalGeneration, fileid: i64, entry: &FilesystemEntry, @@ -132,7 +141,7 @@ fn restore_generation( let to = restored_path(entry, to)?; match entry.kind() { - FilesystemKind::Regular => restore_regular(client, &gen, &to, fileid, &entry)?, + FilesystemKind::Regular => restore_regular(client, &gen, &to, fileid, &entry).await?, FilesystemKind::Directory => restore_directory(&to)?, FilesystemKind::Symlink => restore_symlink(&to, &entry)?, FilesystemKind::Socket => restore_socket(&to, &entry)?, @@ -170,8 +179,8 @@ fn restored_path(entry: &FilesystemEntry, to: &Path) -> Result<PathBuf, RestoreE Ok(to.join(path)) } -fn restore_regular( - client: &BackupClient, +async fn restore_regular( + client: &AsyncBackupClient, gen: &LocalGeneration, path: &Path, fileid: i64, @@ -187,7 +196,7 @@ fn restore_regular( .map_err(|err| RestoreError::CreateFile(path.to_path_buf(), err))?; for chunkid in gen.chunkids(fileid)?.iter()? { let chunkid = chunkid?; - let chunk = client.fetch_chunk(&chunkid)?; + let chunk = client.fetch_chunk(&chunkid).await?; file.write_all(chunk.data()) .map_err(|err| RestoreError::WriteFile(path.to_path_buf(), err))?; } |