Skip to content

Commit

Permalink
Fix db paths when datadir is relative (#2682)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 authored and paulhauner committed Dec 2, 2021
1 parent 67a6f91 commit aa1d57a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
33 changes: 11 additions & 22 deletions beacon_node/client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,58 +116,47 @@ impl Default for Config {

impl Config {
/// Get the database path without initialising it.
pub fn get_db_path(&self) -> Option<PathBuf> {
self.get_data_dir()
.map(|data_dir| data_dir.join(&self.db_name))
pub fn get_db_path(&self) -> PathBuf {
self.get_data_dir().join(&self.db_name)
}

/// Get the database path, creating it if necessary.
pub fn create_db_path(&self) -> Result<PathBuf, String> {
let db_path = self
.get_db_path()
.ok_or("Unable to locate user home directory")?;
ensure_dir_exists(db_path)
ensure_dir_exists(self.get_db_path())
}

/// Fetch default path to use for the freezer database.
fn default_freezer_db_path(&self) -> Option<PathBuf> {
self.get_data_dir()
.map(|data_dir| data_dir.join(DEFAULT_FREEZER_DB_DIR))
fn default_freezer_db_path(&self) -> PathBuf {
self.get_data_dir().join(DEFAULT_FREEZER_DB_DIR)
}

/// Returns the path to which the client may initialize the on-disk freezer database.
///
/// Will attempt to use the user-supplied path from e.g. the CLI, or will default
/// to a directory in the data_dir if no path is provided.
pub fn get_freezer_db_path(&self) -> Option<PathBuf> {
pub fn get_freezer_db_path(&self) -> PathBuf {
self.freezer_db_path
.clone()
.or_else(|| self.default_freezer_db_path())
.unwrap_or_else(|| self.default_freezer_db_path())
}

/// Get the freezer DB path, creating it if necessary.
pub fn create_freezer_db_path(&self) -> Result<PathBuf, String> {
let freezer_db_path = self
.get_freezer_db_path()
.ok_or("Unable to locate user home directory")?;
ensure_dir_exists(freezer_db_path)
ensure_dir_exists(self.get_freezer_db_path())
}

/// Returns the core path for the client.
///
/// Will not create any directories.
pub fn get_data_dir(&self) -> Option<PathBuf> {
dirs::home_dir().map(|home_dir| home_dir.join(&self.data_dir))
pub fn get_data_dir(&self) -> PathBuf {
self.data_dir.clone()
}

/// Returns the core path for the client.
///
/// Creates the directory if it does not exist.
pub fn create_data_dir(&self) -> Result<PathBuf, String> {
let path = self
.get_data_dir()
.ok_or("Unable to locate user home directory")?;
ensure_dir_exists(path)
ensure_dir_exists(self.get_data_dir())
}
}

Expand Down
16 changes: 4 additions & 12 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,12 @@ pub fn get_config<E: EthSpec>(
// If necessary, remove any existing database and configuration
if client_config.data_dir.exists() && cli_args.is_present("purge-db") {
// Remove the chain_db.
let chain_db = client_config.get_db_path().ok_or("Failed to get db_path")?;
if chain_db.exists() {
fs::remove_dir_all(chain_db)
.map_err(|err| format!("Failed to remove chain_db: {}", err))?;
}
fs::remove_dir_all(client_config.get_db_path())
.map_err(|err| format!("Failed to remove chain_db: {}", err))?;

// Remove the freezer db.
let freezer_db = client_config
.get_freezer_db_path()
.ok_or("Failed to get freezer db path")?;
if freezer_db.exists() {
fs::remove_dir_all(freezer_db)
.map_err(|err| format!("Failed to remove chain_db: {}", err))?;
}
fs::remove_dir_all(client_config.get_freezer_db_path())
.map_err(|err| format!("Failed to remove chain_db: {}", err))?;
}

// Create `datadir` and any non-existing parent directories.
Expand Down

0 comments on commit aa1d57a

Please # to comment.