From 8ff511e8fa86e574eb9fb695af0aff3c5c11a397 Mon Sep 17 00:00:00 2001 From: Brooks Date: Tue, 23 Jan 2024 06:46:27 -0500 Subject: [PATCH] Moves create_and_canonicalize_directories() into accounts-db utils (#34882) --- accounts-db/src/utils.rs | 14 ++++++++++++++ ledger-tool/src/args.rs | 21 ++++++++++++--------- runtime/src/snapshot_utils.rs | 12 ------------ validator/src/main.rs | 29 ++++++++++++++++------------- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/accounts-db/src/utils.rs b/accounts-db/src/utils.rs index 1e3a68555..7a38d23b0 100644 --- a/accounts-db/src/utils.rs +++ b/accounts-db/src/utils.rs @@ -85,6 +85,20 @@ pub fn delete_contents_of_path(path: impl AsRef) { } } +/// Creates directories if they do not exist, and canonicalizes the paths. +pub fn create_and_canonicalize_directories( + directories: impl IntoIterator>, +) -> std::io::Result> { + 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}; diff --git a/ledger-tool/src/args.rs b/ledger-tool/src/args.rs index db39f6062..1c6f97444 100644 --- a/ledger-tool/src/args.rs +++ b/ledger-tool/src/args.rs @@ -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, @@ -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), diff --git a/runtime/src/snapshot_utils.rs b/runtime/src/snapshot_utils.rs index f20f5297f..76a2d8cf6 100644 --- a/runtime/src/snapshot_utils.rs +++ b/runtime/src/snapshot_utils.rs @@ -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> { - 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) { diff --git a/validator/src/main.rs b/validator/src/main.rs index 986a38929..c0ea702da 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -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::{ @@ -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}, @@ -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() @@ -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() @@ -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> = values_t!(matches, "account_shrink_path", String)