summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-02-16 09:44:54 +0200
committerLars Wirzenius <liw@liw.fi>2021-02-16 11:03:20 +0200
commit89c46016090b345b9acdea5398178d0c90aef4a7 (patch)
treebe712e6c648a38f1756edd56ea0be40c609f1c53
parentc714af9e675fe5762914c28a566e679a98a417ab (diff)
downloadobnam2-89c46016090b345b9acdea5398178d0c90aef4a7.tar.gz
refactor: split BackupRun into initial, incremental variants
This is clearer, easier to modify than having a flag to indicate which variant we're running.
-rw-r--r--src/backup_run.rs46
-rw-r--r--src/cmd/backup.rs40
2 files changed, 58 insertions, 28 deletions
diff --git a/src/backup_run.rs b/src/backup_run.rs
index 91e06f0..024f486 100644
--- a/src/backup_run.rs
+++ b/src/backup_run.rs
@@ -9,8 +9,14 @@ use crate::policy::BackupPolicy;
use log::{info, warn};
use std::path::Path;
-pub struct BackupRun {
- client: BackupClient,
+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,
@@ -30,28 +36,21 @@ pub enum BackupError {
pub type BackupResult<T> = Result<T, BackupError>;
-impl BackupRun {
- pub fn new(config: &ClientConfig) -> BackupResult<Self> {
- let client = BackupClient::new(config)?;
- let policy = BackupPolicy::new();
+impl<'a> InitialBackup<'a> {
+ pub fn new(config: &ClientConfig, client: &'a BackupClient) -> BackupResult<Self> {
let progress = BackupProgress::initial();
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_file_initially(
+ pub fn backup(
&self,
entry: FsIterResult<FilesystemEntry>,
) -> BackupResult<(FilesystemEntry, Vec<ChunkId>, Reason)> {
@@ -65,8 +64,29 @@ impl BackupRun {
}
}
}
+}
+
+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_file_incrementally(
+ pub fn backup(
&self,
entry: FsIterResult<FilesystemEntry>,
old: &LocalGeneration,
diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs
index cb2e9af..16d3f69 100644
--- a/src/cmd/backup.rs
+++ b/src/cmd/backup.rs
@@ -1,6 +1,6 @@
-use crate::backup_run::BackupRun;
+use crate::backup_run::{IncrementalBackup, InitialBackup};
use crate::chunkid::ChunkId;
-use crate::client::ClientConfig;
+use crate::client::{BackupClient, ClientConfig};
use crate::error::ObnamError;
use crate::fsiter::FsIterator;
use crate::generation::NascentGeneration;
@@ -14,8 +14,6 @@ const SQLITE_CHUNK_SIZE: usize = 1024 * 1024;
pub fn backup(config: &ClientConfig) -> Result<(), ObnamError> {
let runtime = SystemTime::now();
- let run = BackupRun::new(config)?;
-
// Create a named temporary file. We don't meed the open file
// handle, so we discard that.
let oldname = {
@@ -32,18 +30,26 @@ pub fn backup(config: &ClientConfig) -> Result<(), ObnamError> {
dbname
};
- let genlist = run.client().list_generations()?;
+ let client = BackupClient::new(config)?;
+ let genlist = client.list_generations()?;
let file_count = match genlist.resolve("latest") {
- Err(_) => initial_backup(&config.roots, &newname, &run)?,
- Ok(old) => incremental_backup(&old, &config.roots, &newname, &oldname, &run)?,
+ Err(_) => {
+ let run = InitialBackup::new(config, &client)?;
+ let count = initial_backup(&config.roots, &newname, &run)?;
+ run.progress().finish();
+ count
+ }
+ Ok(old) => {
+ let run = IncrementalBackup::new(config, &client)?;
+ let count = incremental_backup(&old, &config.roots, &newname, &oldname, &run)?;
+ run.progress().finish();
+ count
+ }
};
- run.progress().finish();
// Upload the SQLite file, i.e., the named temporary file, which
// still exists, since we persisted it above.
- let gen_id = run
- .client()
- .upload_generation(&newname, SQLITE_CHUNK_SIZE)?;
+ let gen_id = client.upload_generation(&newname, SQLITE_CHUNK_SIZE)?;
// Delete the temporary file.q
std::fs::remove_file(&newname)?;
@@ -62,13 +68,17 @@ fn report_stats(runtime: &SystemTime, file_count: i64, gen_id: &ChunkId) -> Resu
Ok(())
}
-fn initial_backup(roots: &[PathBuf], newname: &Path, run: &BackupRun) -> Result<i64, ObnamError> {
+fn initial_backup(
+ roots: &[PathBuf],
+ newname: &Path,
+ run: &InitialBackup,
+) -> Result<i64, ObnamError> {
info!("fresh backup without a previous generation");
let mut new = NascentGeneration::create(&newname)?;
for root in roots {
let iter = FsIterator::new(root);
- new.insert_iter(iter.map(|entry| run.backup_file_initially(entry)))?;
+ new.insert_iter(iter.map(|entry| run.backup(entry)))?;
}
Ok(new.file_count())
}
@@ -78,7 +88,7 @@ fn incremental_backup(
roots: &[PathBuf],
newname: &Path,
oldname: &Path,
- run: &BackupRun,
+ run: &IncrementalBackup,
) -> Result<i64, ObnamError> {
info!("incremental backup based on {}", old);
@@ -88,7 +98,7 @@ fn incremental_backup(
let iter = FsIterator::new(root);
run.progress()
.files_in_previous_generation(old.file_count()? as u64);
- new.insert_iter(iter.map(|entry| run.backup_file_incrementally(entry, &old)))?;
+ new.insert_iter(iter.map(|entry| run.backup(entry, &old)))?;
}
Ok(new.file_count())
}