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 { impl Config {
/// Get the database path without initialising it. /// Get the database path without initialising it.
pub fn get_db_path(&self) -> Option<PathBuf> { pub fn get_db_path(&self) -> PathBuf {
self.get_data_dir() self.get_data_dir().join(&self.db_name)
.map(|data_dir| data_dir.join(&self.db_name))
} }
/// Get the database path, creating it if necessary. /// Get the database path, creating it if necessary.
pub fn create_db_path(&self) -> Result<PathBuf, String> { pub fn create_db_path(&self) -> Result<PathBuf, String> {
let db_path = self ensure_dir_exists(self.get_db_path())
.get_db_path()
.ok_or("Unable to locate user home directory")?;
ensure_dir_exists(db_path)
} }
/// Fetch default path to use for the freezer database. /// Fetch default path to use for the freezer database.
fn default_freezer_db_path(&self) -> Option<PathBuf> { fn default_freezer_db_path(&self) -> PathBuf {
self.get_data_dir() self.get_data_dir().join(DEFAULT_FREEZER_DB_DIR)
.map(|data_dir| data_dir.join(DEFAULT_FREEZER_DB_DIR))
} }
/// Returns the path to which the client may initialize the on-disk freezer database. /// 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 /// 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. /// 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 self.freezer_db_path
.clone() .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. /// Get the freezer DB path, creating it if necessary.
pub fn create_freezer_db_path(&self) -> Result<PathBuf, String> { pub fn create_freezer_db_path(&self) -> Result<PathBuf, String> {
let freezer_db_path = self ensure_dir_exists(self.get_freezer_db_path())
.get_freezer_db_path()
.ok_or("Unable to locate user home directory")?;
ensure_dir_exists(freezer_db_path)
} }
/// Returns the core path for the client. /// Returns the core path for the client.
/// ///
/// Will not create any directories. /// Will not create any directories.
pub fn get_data_dir(&self) -> Option<PathBuf> { pub fn get_data_dir(&self) -> PathBuf {
dirs::home_dir().map(|home_dir| home_dir.join(&self.data_dir)) self.data_dir.clone()
} }
/// Returns the core path for the client. /// Returns the core path for the client.
/// ///
/// Creates the directory if it does not exist. /// Creates the directory if it does not exist.
pub fn create_data_dir(&self) -> Result<PathBuf, String> { pub fn create_data_dir(&self) -> Result<PathBuf, String> {
let path = self ensure_dir_exists(self.get_data_dir())
.get_data_dir()
.ok_or("Unable to locate user home directory")?;
ensure_dir_exists(path)
} }
} }

View File

@ -36,20 +36,12 @@ pub fn get_config<E: EthSpec>(
// If necessary, remove any existing database and configuration // If necessary, remove any existing database and configuration
if client_config.data_dir.exists() && cli_args.is_present("purge-db") { if client_config.data_dir.exists() && cli_args.is_present("purge-db") {
// Remove the chain_db. // Remove the chain_db.
let chain_db = client_config.get_db_path().ok_or("Failed to get db_path")?; fs::remove_dir_all(client_config.get_db_path())
if chain_db.exists() { .map_err(|err| format!("Failed to remove chain_db: {}", err))?;
fs::remove_dir_all(chain_db)
.map_err(|err| format!("Failed to remove chain_db: {}", err))?;
}
// Remove the freezer db. // Remove the freezer db.
let freezer_db = client_config fs::remove_dir_all(client_config.get_freezer_db_path())
.get_freezer_db_path() .map_err(|err| format!("Failed to remove chain_db: {}", err))?;
.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))?;
}
} }
// Create `datadir` and any non-existing parent directories. // Create `datadir` and any non-existing parent directories.