summaryrefslogtreecommitdiff
path: root/src/backup_run.rs
blob: e3bfc5a322461c90581724b0eb85824791c49071 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::backup_progress::BackupProgress;
use crate::backup_reason::Reason;
use crate::chunkid::ChunkId;
use crate::client::{BackupClient, ClientConfig};
use crate::fsentry::FilesystemEntry;
use crate::generation::LocalGeneration;
use crate::policy::BackupPolicy;
use log::{info, warn};

pub struct BackupRun {
    client: BackupClient,
    policy: BackupPolicy,
    buffer_size: usize,
    progress: BackupProgress,
}

impl BackupRun {
    pub fn new(config: &ClientConfig, buffer_size: usize) -> anyhow::Result<Self> {
        let client = BackupClient::new(&config.server_url)?;
        let policy = BackupPolicy::new();
        let progress = BackupProgress::new();
        Ok(Self {
            client,
            policy,
            buffer_size,
            progress,
        })
    }

    pub fn client(&self) -> &BackupClient {
        &self.client
    }

    pub fn progress(&self) -> &BackupProgress {
        &self.progress
    }

    pub fn backup_file_initially(
        &self,
        entry: anyhow::Result<FilesystemEntry>,
    ) -> anyhow::Result<(FilesystemEntry, Vec<ChunkId>, Reason)> {
        match entry {
            Err(err) => Err(err.into()),
            Ok(entry) => {
                let path = &entry.pathbuf();
                info!("backup: {}", path.display());
                self.progress.found_live_file(path);
                let ids = self
                    .client
                    .upload_filesystem_entry(&entry, self.buffer_size)?;
                Ok((entry.clone(), ids, Reason::IsNew))
            }
        }
    }

    pub fn backup_file_incrementally(
        &self,
        entry: anyhow::Result<FilesystemEntry>,
        old: &LocalGeneration,
    ) -> anyhow::Result<(FilesystemEntry, Vec<ChunkId>, Reason)> {
        match entry {
            Err(err) => {
                warn!("backup: {}", err);
                self.progress.found_problem();
                Err(err)
            }
            Ok(entry) => {
                let path = &entry.pathbuf();
                info!("backup: {}", path.display());
                self.progress.found_live_file(path);
                let reason = self.policy.needs_backup(&old, &entry);
                match reason {
                    Reason::IsNew | Reason::Changed | Reason::Error => {
                        let ids = self
                            .client
                            .upload_filesystem_entry(&entry, self.buffer_size)?;
                        Ok((entry.clone(), ids, reason))
                    }
                    Reason::Unchanged | Reason::Skipped => {
                        let fileno = old.get_fileno(&entry.pathbuf())?;
                        let ids = if let Some(fileno) = fileno {
                            old.chunkids(fileno)?
                        } else {
                            vec![]
                        };
                        Ok((entry.clone(), ids, reason))
                    }
                }
            }
        }
    }
}