From b22cd1aa4604001671324972a9c9bcee64c9b14f Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 13 Oct 2022 10:28:20 -0700 Subject: [PATCH] Append network to data dir (#650) --- src/index.rs | 8 ++++- src/options.rs | 63 +++++++++++++++++++++++----------------- src/subcommand/server.rs | 15 ++++++++-- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src/index.rs b/src/index.rs index 1dbe747b13..8370f2f5b9 100644 --- a/src/index.rs +++ b/src/index.rs @@ -111,7 +111,13 @@ impl Index { let client = Client::new(&rpc_url, Auth::CookieFile(cookie_file)) .context("Failed to connect to RPC URL")?; - let database_path = options.data_dir()?.join("index.redb"); + let data_dir = options.data_dir()?; + + if let Err(err) = fs::create_dir_all(&data_dir) { + bail!("Failed to create data dir `{}`: {err}", data_dir.display()); + } + + let database_path = data_dir.join("index.redb"); let database = match unsafe { redb::Database::open(&database_path) } { Ok(database) => database, diff --git a/src/options.rs b/src/options.rs index 547e49b1ed..cd99b5ed3f 100644 --- a/src/options.rs +++ b/src/options.rs @@ -62,21 +62,14 @@ impl Options { } pub(crate) fn data_dir(&self) -> Result { - if let Some(data_dir) = &self.data_dir { - return Ok(data_dir.clone()); - } - - let path = dirs::data_dir() - .ok_or_else(|| anyhow!("Failed to retrieve data dir"))? - .join("ord"); - - let path = self.chain.join_with_data_dir(&path); - - if let Err(err) = fs::create_dir_all(&path) { - bail!("Failed to create data dir `{}`: {err}", path.display()); - } + let base = match &self.data_dir { + Some(base) => base.clone(), + None => dirs::data_dir() + .ok_or_else(|| anyhow!("Failed to retrieve data dir"))? + .join("ord"), + }; - Ok(path) + Ok(self.chain.join_with_data_dir(&base)) } pub(crate) fn bitcoin_rpc_client(&self) -> Result { @@ -228,9 +221,8 @@ mod tests { #[test] fn mainnet_cookie_file_path() { - let arguments = Arguments::try_parse_from(&["ord", "index"]).unwrap(); - - let cookie_file = arguments + let cookie_file = Arguments::try_parse_from(&["ord", "index"]) + .unwrap() .options .cookie_file() .unwrap() @@ -280,20 +272,39 @@ mod tests { #[test] fn mainnet_data_dir() { - let arguments = Arguments::try_parse_from(&["ord", "index"]).unwrap(); - - let data_dir = arguments.options.data_dir().unwrap().display().to_string(); - - assert!(data_dir.ends_with("/ord")); + let data_dir = Arguments::try_parse_from(&["ord", "index"]) + .unwrap() + .options + .data_dir() + .unwrap() + .display() + .to_string(); + assert!(data_dir.ends_with("/ord"), "{data_dir}"); } #[test] fn othernet_data_dir() { - let arguments = Arguments::try_parse_from(&["ord", "--chain=signet", "index"]).unwrap(); - - let data_dir = arguments.options.data_dir().unwrap().display().to_string(); + let data_dir = Arguments::try_parse_from(&["ord", "--chain=signet", "index"]) + .unwrap() + .options + .data_dir() + .unwrap() + .display() + .to_string(); + assert!(data_dir.ends_with("/ord/signet"), "{data_dir}"); + } - assert!(data_dir.ends_with("/ord/signet")); + #[test] + fn network_is_joined_with_data_dir() { + let data_dir = + Arguments::try_parse_from(&["ord", "--chain=signet", "--data-dir", "foo", "index"]) + .unwrap() + .options + .data_dir() + .unwrap() + .display() + .to_string(); + assert!(data_dir.ends_with("foo/signet"), "{data_dir}"); } #[test] diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 377f723a7f..dd16222ab3 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -213,11 +213,20 @@ impl Server { } fn acme_cache(acme_cache: Option<&PathBuf>, options: &Options) -> Result { - if let Some(acme_cache) = acme_cache { - Ok(acme_cache.clone()) + let acme_cache = if let Some(acme_cache) = acme_cache { + acme_cache.clone() } else { - Ok(options.data_dir()?.join("acme-cache")) + options.data_dir()?.join("acme-cache") + }; + + if let Err(err) = fs::create_dir_all(&acme_cache) { + bail!( + "Failed to create acme cache dir `{}`: {err}", + acme_cache.display() + ); } + + Ok(acme_cache) } fn acme_domains(acme_domain: &Vec) -> Result> {