summaryrefslogtreecommitdiff
path: root/src/backup_run.rs
blob: 024f486ece61c14940c111f47b4ea662ea47432c (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::backup_progress::BackupProgress;
use crate::backup_reason::Reason;
use crate::chunkid::ChunkId;
use crate::client::{BackupClient, ClientConfig, ClientError};
use crate::fsentry::FilesystemEntry;
use crate::fsiter::{FsIterError, FsIterResult};
use crate::generation::{LocalGeneration, LocalGenerationError};
use crate::policy::BackupPolicy;
use log::{info, warn};
use std::path::Path;

pub struct InitialBackup<'a> {
    client: &'a BackupClient,
    buffer_size: usize,
    progress: BackupProgress,
}

pub struct IncrementalBackup<'a> {
    client: &'a BackupClient,
    policy: BackupPolicy,
    buffer_size: usize,
    progress: BackupProgress,
}

#[derive(Debug, thiserror::Error)]
pub enum BackupError {
    #[error(transparent)]
    ClientError(#[from] ClientError),

    #[error(transparent)]
    FsIterError(#[from] FsIterError),

    #[error(transparent)]
    LocalGenerationError(#[from] LocalGenerationError),
}

pub type BackupResult<T> = Result<T, BackupError>;

impl<'a> InitialBackup<'a> {
    pub fn new(config: &ClientConfig, client: &'a BackupClient) -> BackupResult<Self> {
        let progress = BackupProgress::initial();
        Ok(Self {
            client,
            buffer_size: config.chunk_size,
            progress,
        })
    }

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

    pub fn backup(
        &self,
        entry: FsIterResult<FilesystemEntry>,
    ) -> BackupResult<(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);
                backup_file(&self.client, &entry, &path, self.buffer_size, Reason::IsNew)
            }
        }
    }
}

impl<'a> IncrementalBackup<'a> {
    pub fn new(config: &ClientConfig, client: &'a BackupClient) -> BackupResult<Self> {
        let policy = BackupPolicy::new();
        let progress = BackupProgress::incremental();
        Ok(Self {
            client,
            policy,
            buffer_size: config.chunk_size,
            progress,
        })
    }

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

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

    pub fn backup(
        &self,
        entry: FsIterResult<FilesystemEntry>,
        old: &LocalGeneration,
    ) -> BackupResult<(FilesystemEntry, Vec<ChunkId>, Reason)> {
        match entry {
            Err(err) => {
                warn!("backup: {}", err);
                self.progress.found_problem();
                Err(BackupError::FsIterError(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::GenerationLookupError
                    | Reason::Unknown => {
                        backup_file(&self.client, &entry, &path, self.buffer_size, reason)
                    }
                    Reason::Unchanged | Reason::Skipped | Reason::FileError => {
                        let fileno = old.get_fileno(&entry.pathbuf())?;
                        let ids = if let Some(fileno) = fileno {
                            old.chunkids(fileno)?
                        } else {
                            vec![]
                        };
                        Ok((entry.clone(), ids, reason))
                    }
                }
            }
        }
    }
}

fn backup_file(
    client: &BackupClient,
    entry: &FilesystemEntry,
    path: &Path,
    chunk_size: usize,
    reason: Reason,
) -> BackupResult<(FilesystemEntry, Vec<ChunkId>, Reason)> {
    let ids = client.upload_filesystem_entry(&entry, chunk_size);
    match ids {
        Err(err) => {
            warn!("error backing up {}, skipping it: {}", path.display(), err);
            Ok((entry.clone(), vec![], Reason::FileError))
        }
        Ok(ids) => Ok((entry.clone(), ids, reason)),
    }
}