Skip to content

Commit

Permalink
Moves create_and_canonicalize_directories() into accounts-db utils (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Jan 23, 2024
1 parent 9263cc6 commit 8ff511e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
14 changes: 14 additions & 0 deletions accounts-db/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ pub fn delete_contents_of_path(path: impl AsRef<Path>) {
}
}

/// Creates directories if they do not exist, and canonicalizes the paths.
pub fn create_and_canonicalize_directories(
directories: impl IntoIterator<Item = impl AsRef<Path>>,
) -> std::io::Result<Vec<PathBuf>> {
directories
.into_iter()
.map(|path| {
fs::create_dir_all(&path)?;
let path = fs::canonicalize(&path)?;
Ok(path)
})
.collect()
}

#[cfg(test)]
mod tests {
use {super::*, tempfile::TempDir};
Expand Down
21 changes: 12 additions & 9 deletions ledger-tool/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use {
accounts_db::{AccountsDb, AccountsDbConfig},
accounts_index::{AccountsIndexConfig, IndexLimitMb},
partitioned_rewards::TestPartitionedEpochRewards,
utils::create_and_canonicalize_directories,
},
solana_clap_utils::input_parsers::pubkeys_of,
solana_ledger::{
blockstore_processor::ProcessOptions,
use_snapshot_archives_at_startup::{self, UseSnapshotArchivesAtStartup},
},
solana_runtime::{runtime_config::RuntimeConfig, snapshot_utils},
solana_runtime::runtime_config::RuntimeConfig,
solana_sdk::clock::Slot,
std::{
collections::HashSet,
Expand Down Expand Up @@ -116,14 +117,16 @@ pub fn get_accounts_db_config(
.unwrap_or_else(|| {
ledger_tool_ledger_path.join(AccountsDb::DEFAULT_ACCOUNTS_HASH_CACHE_DIR)
});
let accounts_hash_cache_path =
snapshot_utils::create_and_canonicalize_directories(&[accounts_hash_cache_path])
.unwrap_or_else(|err| {
eprintln!("Unable to access accounts hash cache path: {err}");
std::process::exit(1);
})
.pop()
.unwrap();
let accounts_hash_cache_path = create_and_canonicalize_directories([&accounts_hash_cache_path])
.unwrap_or_else(|err| {
eprintln!(
"Unable to access accounts hash cache path '{}': {err}",
accounts_hash_cache_path.display(),
);
std::process::exit(1);
})
.pop()
.unwrap();

AccountsDbConfig {
index: Some(accounts_index_config),
Expand Down
12 changes: 0 additions & 12 deletions runtime/src/snapshot_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,18 +530,6 @@ pub enum GetSnapshotAccountsHardLinkDirError {
},
}

/// Creates directories if they do not exist, and canonicalizes the paths.
pub fn create_and_canonicalize_directories(directories: &[PathBuf]) -> Result<Vec<PathBuf>> {
directories
.iter()
.map(|path| {
fs_err::create_dir_all(path)?;
let path = fs_err::canonicalize(path)?;
Ok(path)
})
.collect()
}

/// Moves and asynchronously deletes the contents of a directory to avoid blocking on it.
/// The directory is re-created after the move, and should now be empty.
pub fn move_and_async_delete_path_contents(path: impl AsRef<Path>) {
Expand Down
29 changes: 16 additions & 13 deletions validator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use {
AccountsIndexConfig, IndexLimitMb,
},
partitioned_rewards::TestPartitionedEpochRewards,
utils::create_all_accounts_run_and_snapshot_dirs,
utils::{create_all_accounts_run_and_snapshot_dirs, create_and_canonicalize_directories},
},
solana_clap_utils::input_parsers::{keypair_of, keypairs_of, pubkey_of, value_of},
solana_core::{
Expand Down Expand Up @@ -48,9 +48,7 @@ use {
runtime_config::RuntimeConfig,
snapshot_bank_utils::DISABLED_SNAPSHOT_ARCHIVE_INTERVAL,
snapshot_config::{SnapshotConfig, SnapshotUsage},
snapshot_utils::{
self, create_and_canonicalize_directories, ArchiveFormat, SnapshotVersion,
},
snapshot_utils::{self, ArchiveFormat, SnapshotVersion},
},
solana_sdk::{
clock::{Slot, DEFAULT_S_PER_SLOT},
Expand Down Expand Up @@ -991,9 +989,12 @@ pub fn main() {
.map(BlockstoreRecoveryMode::from);

// Canonicalize ledger path to avoid issues with symlink creation
let ledger_path = create_and_canonicalize_directories(&[ledger_path])
let ledger_path = create_and_canonicalize_directories([&ledger_path])
.unwrap_or_else(|err| {
eprintln!("Unable to access ledger path: {err}");
eprintln!(
"Unable to access ledger path '{}': {err}",
ledger_path.display(),
);
exit(1);
})
.pop()
Expand All @@ -1003,9 +1004,12 @@ pub fn main() {
.value_of("accounts_hash_cache_path")
.map(Into::into)
.unwrap_or_else(|| ledger_path.join(AccountsDb::DEFAULT_ACCOUNTS_HASH_CACHE_DIR));
let accounts_hash_cache_path = create_and_canonicalize_directories(&[accounts_hash_cache_path])
let accounts_hash_cache_path = create_and_canonicalize_directories([&accounts_hash_cache_path])
.unwrap_or_else(|err| {
eprintln!("Unable to access accounts hash cache path: {err}");
eprintln!(
"Unable to access accounts hash cache path '{}': {err}",
accounts_hash_cache_path.display(),
);
exit(1);
})
.pop()
Expand Down Expand Up @@ -1443,11 +1447,10 @@ pub fn main() {
} else {
vec![ledger_path.join("accounts")]
};
let account_paths = snapshot_utils::create_and_canonicalize_directories(&account_paths)
.unwrap_or_else(|err| {
eprintln!("Unable to access account path: {err}");
exit(1);
});
let account_paths = create_and_canonicalize_directories(account_paths).unwrap_or_else(|err| {
eprintln!("Unable to access account path: {err}");
exit(1);
});

let account_shrink_paths: Option<Vec<PathBuf>> =
values_t!(matches, "account_shrink_path", String)
Expand Down

0 comments on commit 8ff511e

Please sign in to comment.