Fix db paths when datadir is relative (#2682)

This commit is contained in:
Pawan Dhananjay 2021-10-06 19:22:19 +05:30 committed by Paul Hauner
parent 67a6f91df6
commit aa1d57aa55
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
2 changed files with 15 additions and 34 deletions

View File

@ -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())
}
}

View File

@ -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.