From 34a039ee8b882e577faa3aa8298c1ad08fefa14f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 22 Jul 2021 17:59:13 +0300 Subject: refactor: use async for "obnam restore" Sponsored-by: author --- src/cmd/restore.rs | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'src') 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