summaryrefslogtreecommitdiff
path: root/src/cmd/restore.rs
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/restore.rs
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/restore.rs')
-rw-r--r--src/cmd/restore.rs16
1 files changed, 15 insertions, 1 deletions
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(())
}