diff options
Diffstat (limited to 'src/cmd/list_files.rs')
-rw-r--r-- | src/cmd/list_files.rs | 61 |
1 files changed, 39 insertions, 22 deletions
diff --git a/src/cmd/list_files.rs b/src/cmd/list_files.rs index a69c3df..e8276cd 100644 --- a/src/cmd/list_files.rs +++ b/src/cmd/list_files.rs @@ -1,36 +1,51 @@ +//! The `list-files` subcommand. + use crate::backup_reason::Reason; +use crate::chunk::ClientTrust; use crate::client::BackupClient; -use crate::client::ClientConfig; +use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::{FilesystemEntry, FilesystemKind}; +use clap::Parser; use tempfile::NamedTempFile; +use tokio::runtime::Runtime; -pub fn list_files(config: &ClientConfig, gen_ref: &str) -> anyhow::Result<()> { - // Create a named temporary file. We don't meed the open file - // handle, so we discard that. - let dbname = { - let temp = NamedTempFile::new()?; - let (_, dbname) = temp.keep()?; - dbname - }; +/// List files in a backup. +#[derive(Debug, Parser)] +pub struct ListFiles { + /// Reference to backup to list files in. + #[clap(default_value = "latest")] + gen_id: String, +} + +impl ListFiles { + /// Run the command. + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let rt = Runtime::new()?; + rt.block_on(self.run_async(config)) + } - let client = BackupClient::new(&config.server_url)?; + async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let temp = NamedTempFile::new()?; - 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 client = BackupClient::new(config)?; + let trust = client + .get_client_trust() + .await? + .or_else(|| Some(ClientTrust::new("FIXME", None, "".to_string(), vec![]))) + .unwrap(); - let gen = client.fetch_generation(&gen_id, &dbname)?; - for file in gen.files()? { - println!("{}", format_entry(&file.entry(), file.reason())); - } + let genlist = client.list_generations(&trust); + let gen_id = genlist.resolve(&self.gen_id)?; - // Delete the temporary file. - std::fs::remove_file(&dbname)?; + let gen = client.fetch_generation(&gen_id, temp.path()).await?; + for file in gen.files()?.iter()? { + let (_, entry, reason, _) = file?; + println!("{}", format_entry(&entry, reason)); + } - Ok(()) + Ok(()) + } } fn format_entry(e: &FilesystemEntry, reason: Reason) -> String { @@ -38,6 +53,8 @@ fn format_entry(e: &FilesystemEntry, reason: Reason) -> String { FilesystemKind::Regular => "-", FilesystemKind::Directory => "d", FilesystemKind::Symlink => "l", + FilesystemKind::Socket => "s", + FilesystemKind::Fifo => "p", }; format!("{} {} ({})", kind, e.pathbuf().display(), reason) } |