From 76371659e92dc87746e24dfe2d1ba056efae26a5 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Tue, 2 Jul 2019 17:32:14 +1000 Subject: [PATCH] Improve error handling of default directory --- account_manager/src/main.rs | 21 ++++++++++++++++----- beacon_node/eth2-libp2p/src/rpc/protocol.rs | 1 - beacon_node/src/main.rs | 21 ++++++++++++++++----- validator_client/src/main.rs | 21 ++++++++++++++++----- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/account_manager/src/main.rs b/account_manager/src/main.rs index e242e8ae4..ee0e86d60 100644 --- a/account_manager/src/main.rs +++ b/account_manager/src/main.rs @@ -61,13 +61,24 @@ fn main() { ) .get_matches(); - let mut default_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); - default_dir.push(DEFAULT_DATA_DIR); - - let data_dir = &matches + let data_dir = match matches .value_of("datadir") .and_then(|v| Some(PathBuf::from(v))) - .unwrap_or_else(|| PathBuf::from(default_dir)); + { + Some(v) => v, + None => { + // use the default + let mut default_dir = match dirs::home_dir() { + Some(v) => v, + None => { + crit!(log, "Failed to find a home directory"); + return; + } + }; + default_dir.push(DEFAULT_DATA_DIR); + PathBuf::from(default_dir) + } + }; // create the directory if needed match fs::create_dir_all(&data_dir) { diff --git a/beacon_node/eth2-libp2p/src/rpc/protocol.rs b/beacon_node/eth2-libp2p/src/rpc/protocol.rs index 2f461988a..7afded3ac 100644 --- a/beacon_node/eth2-libp2p/src/rpc/protocol.rs +++ b/beacon_node/eth2-libp2p/src/rpc/protocol.rs @@ -11,7 +11,6 @@ use tokio::io::{AsyncRead, AsyncWrite}; const MAX_READ_SIZE: usize = 4_194_304; // 4M /// Implementation of the `ConnectionUpgrade` for the rpc protocol. - #[derive(Debug, Clone)] pub struct RPCProtocol; diff --git a/beacon_node/src/main.rs b/beacon_node/src/main.rs index 651ad8e0c..791feae54 100644 --- a/beacon_node/src/main.rs +++ b/beacon_node/src/main.rs @@ -161,13 +161,24 @@ fn main() { let log = slog::Logger::root(drain.fuse(), o!()); - let mut default_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); - default_dir.push(DEFAULT_DATA_DIR); - - let data_dir = &matches + let data_dir = match matches .value_of("datadir") .and_then(|v| Some(PathBuf::from(v))) - .unwrap_or_else(|| PathBuf::from(default_dir)); + { + Some(v) => v, + None => { + // use the default + let mut default_dir = match dirs::home_dir() { + Some(v) => v, + None => { + crit!(log, "Failed to find a home directory"); + return; + } + }; + default_dir.push(DEFAULT_DATA_DIR); + PathBuf::from(default_dir) + } + }; // create the directory if needed match fs::create_dir_all(&data_dir) { diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 43ef2f994..e37b6530e 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -67,13 +67,24 @@ fn main() { ) .get_matches(); - let mut default_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); - default_dir.push(DEFAULT_DATA_DIR); - - let data_dir = &matches + let data_dir = match matches .value_of("datadir") .and_then(|v| Some(PathBuf::from(v))) - .unwrap_or_else(|| PathBuf::from(default_dir)); + { + Some(v) => v, + None => { + // use the default + let mut default_dir = match dirs::home_dir() { + Some(v) => v, + None => { + crit!(log, "Failed to find a home directory"); + return; + } + }; + default_dir.push(DEFAULT_DATA_DIR); + PathBuf::from(default_dir) + } + }; // create the directory if needed match fs::create_dir_all(&data_dir) {