summaryrefslogtreecommitdiff
path: root/src/backup_run.rs
blob: ea5888a9f2b4139edcec30ffbb316309e33ac2fd (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::backup_progress::BackupProgress;
use crate::backup_reason::Reason;
use crate::chunkid::ChunkId;
use crate::client::{BackupClient, ClientConfig, ClientError};
use crate::error::ObnamError;
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: Option<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 drop(&self) {
        &self.progress.finish();
    }

    pub fn backup(
        &self,
        entry: FsIterResult<FilesystemEntry>,
    ) -> BackupResult<(FilesystemEntry, Vec<ChunkId>, Reason)> {
        match entry {
            Err(err) => {
                warn!("backup: there was a problem: {:?}", err);
                self.progress.found_problem();
                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();
        Ok(Self {
            client,
            policy,
            buffer_size: config.chunk_size,
            progress: None,
        })
    }

    pub fn start_backup(&mut self, old: &LocalGeneration) -> Result<(), ObnamError> {
        let progress = BackupProgress::incremental();
        progress.files_in_previous_generation(old.file_count()? as u64);
        self.progress = Some(progress);
        Ok(())
    }

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

    pub fn drop(&self) {
        if let Some(progress) = &self.progress {
            progress.finish();
        }
    }

    pub fn fetch_previous_generation(
        &self,
        genid: &str,
        oldname: &Path,
    ) -> Result<LocalGeneration, ObnamError> {
        let progress = BackupProgress::download_generation(genid);
        let old = self.client().fetch_generation(genid, &oldname)?;
        progress.finish();
        Ok(old)
    }

    pub fn backup(
        &self,
        entry: FsIterResult<FilesystemEntry>,
        old: &LocalGeneration,
    ) -> BackupResult<(FilesystemEntry, Vec<ChunkId>, Reason)> {
        match entry {
            Err(err) => {
                warn!("backup: {}", err);
                self.found_problem();
                Err(BackupError::FsIterError(err))
            }
            Ok(entry) => {
                let path = &entry.pathbuf();
                info!("backup: {}", path.display());
                self.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 found_live_file(&self, path: &Path) {
        if let Some(progress) = &self.progress {
            progress.found_live_file(path);
        }
    }

    fn found_problem(&self) {
        if let Some(progress) = &self.progress {
            progress.found_problem();
        }
    }
}

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