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. --- src/bin/obnam.rs | 10 +--------- src/client.rs | 1 - src/cmd/backup.rs | 22 ++++++++++++++++++++-- src/cmd/restore.rs | 16 +++++++++++++++- 4 files changed, 36 insertions(+), 13 deletions(-) (limited to 'src') 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(()) } -- cgit v1.2.1