From 68e88efced88f05664ae9050f6888453cfe9cd30 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 22 Nov 2020 15:06:07 +0200 Subject: 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. --- obnam.md | 3 +-- src/bin/obnam.rs | 10 +--------- src/client.rs | 1 - src/cmd/backup.rs | 22 ++++++++++++++++++++-- src/cmd/restore.rs | 16 +++++++++++++++- subplot/client.py | 5 ++--- subplot/client.yaml | 2 +- 7 files changed, 40 insertions(+), 19 deletions(-) diff --git a/obnam.md b/obnam.md index e93a79f..3beb67d 100644 --- a/obnam.md +++ b/obnam.md @@ -504,13 +504,12 @@ when I run obnam backup smoke.yaml then backup generation is GEN when I run obnam list smoke.yaml then generation list contains -when I invoke obnam restore smoke.yaml restore.db rest +when I invoke obnam restore smoke.yaml rest then data in live and rest match ~~~ ~~~{#smoke.yaml .file .yaml .numberLines} root: live -dbname: tmp.db ~~~ diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index d0cf271..1394b6c 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -15,12 +15,7 @@ fn main() -> anyhow::Result<()> { match opt { Opt::Backup { config } => backup(&config, BUFFER_SIZE)?, Opt::List { config } => list(&config)?, - Opt::Restore { - config, - gen_id, - dbname, - to, - } => restore(&config, &gen_id, &dbname, &to)?, + Opt::Restore { config, gen_id, to } => restore(&config, &gen_id, &to)?, } Ok(()) } @@ -43,9 +38,6 @@ enum Opt { #[structopt()] gen_id: String, - #[structopt(parse(from_os_str))] - dbname: PathBuf, - #[structopt(parse(from_os_str))] to: PathBuf, }, diff --git a/src/client.rs b/src/client.rs index d7dca36..658e3ed 100644 --- a/src/client.rs +++ b/src/client.rs @@ -14,7 +14,6 @@ use std::path::{Path, PathBuf}; #[derive(Debug, Deserialize, Clone)] pub struct ClientConfig { pub server_url: String, - pub dbname: PathBuf, pub root: PathBuf, } 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(()) } diff --git a/subplot/client.py b/subplot/client.py index 7556986..acdc48a 100644 --- a/subplot/client.py +++ b/subplot/client.py @@ -24,13 +24,12 @@ def configure_client(ctx, filename=None): yaml.safe_dump(config, stream=f) -def run_obnam_restore(ctx, filename=None, genid=None, dbname=None, todir=None): +def run_obnam_restore(ctx, filename=None, genid=None, todir=None): runcmd_run = globals()["runcmd_run"] genid = ctx["vars"][genid] runcmd_run( - ctx, - ["env", "RUST_LOG=obnam", "obnam", "restore", filename, genid, dbname, todir], + ctx, ["env", "RUST_LOG=obnam", "obnam", "restore", filename, genid, todir] ) diff --git a/subplot/client.yaml b/subplot/client.yaml index 80b69f2..63033c3 100644 --- a/subplot/client.yaml +++ b/subplot/client.yaml @@ -4,7 +4,7 @@ - given: "a client config based on {filename}" function: configure_client -- when: "I invoke obnam restore {filename} <{genid}> {dbname} {todir}" +- when: "I invoke obnam restore {filename} <{genid}> {todir}" function: run_obnam_restore - then: "backup generation is {varname}" -- cgit v1.2.1