summaryrefslogtreecommitdiff
path: root/src/cmd/backup.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-11-22 15:00:46 +0000
committerLars Wirzenius <liw@liw.fi>2020-11-22 15:00:46 +0000
commit7194cdfb105b1b835dc2a4ff3bbfc1823f170243 (patch)
tree0484972dd085963e157de00f878d5f0e6d98a52a /src/cmd/backup.rs
parent0158bd9c06acae4245897e5e04681b4a1637bee9 (diff)
parent68e88efced88f05664ae9050f6888453cfe9cd30 (diff)
downloadobnam2-7194cdfb105b1b835dc2a4ff3bbfc1823f170243.tar.gz
Merge branch 'templite' into 'main'
feat! use temporary files for SQLite databases Closes #6 See merge request larswirzenius/obnam!20
Diffstat (limited to 'src/cmd/backup.rs')
-rw-r--r--src/cmd/backup.rs22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs
index 71f5aac..926b2b1 100644
--- a/src/cmd/backup.rs
+++ b/src/cmd/backup.rs
@@ -2,20 +2,38 @@ use crate::client::{BackupClient, ClientConfig};
use crate::fsiter::FsIterator;
use crate::generation::Generation;
use std::path::Path;
+use tempfile::NamedTempFile;
pub fn backup(config: &Path, buffer_size: usize) -> anyhow::Result<()> {
let config = ClientConfig::read_config(config)?;
let client = BackupClient::new(&config.server_url)?;
+ // Create a named temporary file. We don't meed the open file
+ // handle, so we discard that.
+ let dbname = {
+ let temp = NamedTempFile::new()?;
+ let (_, dbname) = temp.keep()?;
+ dbname
+ };
+
{
- let mut gen = Generation::create(&config.dbname)?;
+ // Create the SQLite database using the named temporary file.
+ // The fetching is in its own block so that the file handles
+ // get closed and data flushed to disk.
+ let mut gen = Generation::create(&dbname)?;
gen.insert_iter(FsIterator::new(&config.root).map(|entry| match entry {
Err(err) => Err(err),
Ok(entry) => client.upload_filesystem_entry(entry, buffer_size),
}))?;
}
- let gen_id = client.upload_generation(&config.dbname, buffer_size)?;
+
+ // Upload the SQLite file, i.e., the named temporary file, which
+ // still exists, since we persisted it above.
+ let gen_id = client.upload_generation(&dbname, buffer_size)?;
println!("gen id: {}", gen_id);
+ // Delete the temporary file.
+ std::fs::remove_file(&dbname)?;
+
Ok(())
}