summaryrefslogtreecommitdiff
path: root/src/cmd/list_files.rs
blob: e8276cdaff242e709a0627ee11bafe7197fc5532 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! The `list-files` subcommand.

use crate::backup_reason::Reason;
use crate::chunk::ClientTrust;
use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use crate::fsentry::{FilesystemEntry, FilesystemKind};
use clap::Parser;
use tempfile::NamedTempFile;
use tokio::runtime::Runtime;

/// 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))
    }

    async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
        let temp = NamedTempFile::new()?;

        let client = BackupClient::new(config)?;
        let trust = client
            .get_client_trust()
            .await?
            .or_else(|| Some(ClientTrust::new("FIXME", None, "".to_string(), vec![])))
            .unwrap();

        let genlist = client.list_generations(&trust);
        let gen_id = genlist.resolve(&self.gen_id)?;

        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(())
    }
}

fn format_entry(e: &FilesystemEntry, reason: Reason) -> String {
    let kind = match e.kind() {
        FilesystemKind::Regular => "-",
        FilesystemKind::Directory => "d",
        FilesystemKind::Symlink => "l",
        FilesystemKind::Socket => "s",
        FilesystemKind::Fifo => "p",
    };
    format!("{} {} ({})", kind, e.pathbuf().display(), reason)
}