From 100d752451be361f009aaadfe7dc02079574c0e8 Mon Sep 17 00:00:00 2001 From: yutianwu Date: Wed, 3 Jul 2024 21:44:53 +0800 Subject: [PATCH 1/3] fix(cli): don't init datadir if it doesn't exist in db command --- crates/cli/commands/src/common.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/cli/commands/src/common.rs b/crates/cli/commands/src/common.rs index b382f7312cac..9b331734297b 100644 --- a/crates/cli/commands/src/common.rs +++ b/crates/cli/commands/src/common.rs @@ -59,9 +59,12 @@ impl EnvironmentArgs { let db_path = data_dir.db(); let sf_path = data_dir.static_files(); - if access.is_read_write() { - reth_fs_util::create_dir_all(&db_path)?; - reth_fs_util::create_dir_all(&sf_path)?; + // ensure the provided datadir exist + if !db_path.is_dir() { + return Err(eyre::eyre!("Database path does not exist: {:?}", db_path)); + } + if !sf_path.is_dir() { + return Err(eyre::eyre!("Static files path does not exist: {:?}", sf_path)); } let config_path = self.config.clone().unwrap_or_else(|| data_dir.config()); From 77ff0d227942033d5ef053957ebc458b5ae077a2 Mon Sep 17 00:00:00 2001 From: yutianwu Date: Thu, 4 Jul 2024 09:05:37 +0800 Subject: [PATCH 2/3] move the restrict to db commands --- crates/cli/commands/src/common.rs | 9 +++------ crates/cli/commands/src/db/mod.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/crates/cli/commands/src/common.rs b/crates/cli/commands/src/common.rs index 9b331734297b..b382f7312cac 100644 --- a/crates/cli/commands/src/common.rs +++ b/crates/cli/commands/src/common.rs @@ -59,12 +59,9 @@ impl EnvironmentArgs { let db_path = data_dir.db(); let sf_path = data_dir.static_files(); - // ensure the provided datadir exist - if !db_path.is_dir() { - return Err(eyre::eyre!("Database path does not exist: {:?}", db_path)); - } - if !sf_path.is_dir() { - return Err(eyre::eyre!("Static files path does not exist: {:?}", sf_path)); + if access.is_read_write() { + reth_fs_util::create_dir_all(&db_path)?; + reth_fs_util::create_dir_all(&sf_path)?; } let config_path = self.config.clone().unwrap_or_else(|| data_dir.config()); diff --git a/crates/cli/commands/src/db/mod.rs b/crates/cli/commands/src/db/mod.rs index dc247745f5ac..2089761375f1 100644 --- a/crates/cli/commands/src/db/mod.rs +++ b/crates/cli/commands/src/db/mod.rs @@ -67,6 +67,14 @@ impl Command { let db_path = data_dir.db(); let static_files_path = data_dir.static_files(); + // ensure the provided datadir exist + if !db_path.is_dir() { + return Err(eyre::eyre!("Database path does not exist: {:?}", db_path)); + } + if !static_files_path.is_dir() { + return Err(eyre::eyre!("Static files path does not exist: {:?}", static_files_path)); + } + match self.command { // TODO: We'll need to add this on the DB trait. Subcommands::Stats(command) => { From 7b68eac70130481617427e6c05317e9f7339ee38 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 4 Jul 2024 10:03:31 +0200 Subject: [PATCH 3/3] use eyre::ensure --- crates/cli/commands/src/db/mod.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/cli/commands/src/db/mod.rs b/crates/cli/commands/src/db/mod.rs index 2089761375f1..de1f1cc3826f 100644 --- a/crates/cli/commands/src/db/mod.rs +++ b/crates/cli/commands/src/db/mod.rs @@ -68,12 +68,14 @@ impl Command { let static_files_path = data_dir.static_files(); // ensure the provided datadir exist - if !db_path.is_dir() { - return Err(eyre::eyre!("Database path does not exist: {:?}", db_path)); - } - if !static_files_path.is_dir() { - return Err(eyre::eyre!("Static files path does not exist: {:?}", static_files_path)); - } + eyre::ensure!( + data_dir.data_dir().is_dir(), + "Datadir does not exist: {:?}", + data_dir.data_dir() + ); + + // ensure the provided database exist + eyre::ensure!(db_path.is_dir(), "Database does not exist: {:?}", db_path); match self.command { // TODO: We'll need to add this on the DB trait.