Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix db paths when datadir is relative to current path #2682

Merged
merged 1 commit into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 3 additions & 7 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,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.
fs::remove_dir_all(client_config.get_db_path().ok_or("Failed to get db_path")?)
fs::remove_dir_all(client_config.get_db_path())
.map_err(|err| format!("Failed to remove chain_db: {}", err))?;

// Remove the freezer db.
fs::remove_dir_all(
client_config
.get_freezer_db_path()
.ok_or("Failed to get freezer db path")?,
)
.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