summaryrefslogtreecommitdiff
path: root/src/cmd/list_files.rs
blob: 71b0d688a4fe443ef68389d7776b15435b48f6d5 (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
use crate::backup_reason::Reason;
use crate::client::BackupClient;
use crate::client::ClientConfig;
use crate::error::ObnamError;
use crate::fsentry::{FilesystemEntry, FilesystemKind};
use tempfile::NamedTempFile;

pub fn list_files(config: &ClientConfig, gen_ref: &str) -> Result<(), ObnamError> {
    let temp = NamedTempFile::new()?;

    let client = BackupClient::new(config)?;

    let genlist = client.list_generations()?;
    let gen_id: String = genlist.resolve(gen_ref)?;

    let gen = client.fetch_generation(&gen_id, temp.path())?;
    for file in gen.files()? {
        println!("{}", format_entry(&file.entry(), file.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)
}