summaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-11-22 15:06:07 +0200
committerLars Wirzenius <liw@liw.fi>2020-11-22 16:58:52 +0200
commit68e88efced88f05664ae9050f6888453cfe9cd30 (patch)
tree0484972dd085963e157de00f878d5f0e6d98a52a /src/cmd
parent0158bd9c06acae4245897e5e04681b4a1637bee9 (diff)
downloadobnam2-68e88efced88f05664ae9050f6888453cfe9cd30.tar.gz
feat! use temporary files for SQLite databases
The user should not have to specify filenames for the databases, since they don't actually care where they're stored.
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/backup.rs22
-rw-r--r--src/cmd/restore.rs16
2 files changed, 35 insertions, 3 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(())
}
diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs
index 7956b5f..0051d20 100644
--- a/src/cmd/restore.rs
+++ b/src/cmd/restore.rs
@@ -7,14 +7,25 @@ use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
+use tempfile::NamedTempFile;
+
+pub fn restore(config: &Path, gen_id: &str, to: &Path) -> anyhow::Result<()> {
+ // 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
+ };
-pub fn restore(config: &Path, gen_id: &str, dbname: &Path, to: &Path) -> anyhow::Result<()> {
let config = ClientConfig::read_config(&config).unwrap();
let client = BackupClient::new(&config.server_url)?;
let gen_chunk = client.fetch_generation(&gen_id)?;
debug!("gen: {:?}", gen_chunk);
+
{
+ // Fetch the SQLite file, storing it in the temporary file.
let mut dbfile = File::create(&dbname)?;
for id in gen_chunk.chunk_ids() {
let chunk = client.fetch_chunk(id)?;
@@ -28,6 +39,9 @@ pub fn restore(config: &Path, gen_id: &str, dbname: &Path, to: &Path) -> anyhow:
restore_generation(&client, &gen, fileid, entry, &to)?;
}
+ // Delete the temporary file.
+ std::fs::remove_file(&dbname)?;
+
Ok(())
}