Improve error handling of default directory

This commit is contained in:
Age Manning 2019-07-02 17:32:14 +10:00
parent 6c18b417c3
commit 76371659e9
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
4 changed files with 48 additions and 16 deletions

View File

@ -61,13 +61,24 @@ fn main() {
) )
.get_matches(); .get_matches();
let mut default_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); let data_dir = match matches
default_dir.push(DEFAULT_DATA_DIR);
let data_dir = &matches
.value_of("datadir") .value_of("datadir")
.and_then(|v| Some(PathBuf::from(v))) .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 // create the directory if needed
match fs::create_dir_all(&data_dir) { match fs::create_dir_all(&data_dir) {

View File

@ -11,7 +11,6 @@ use tokio::io::{AsyncRead, AsyncWrite};
const MAX_READ_SIZE: usize = 4_194_304; // 4M const MAX_READ_SIZE: usize = 4_194_304; // 4M
/// Implementation of the `ConnectionUpgrade` for the rpc protocol. /// Implementation of the `ConnectionUpgrade` for the rpc protocol.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RPCProtocol; pub struct RPCProtocol;

View File

@ -161,13 +161,24 @@ fn main() {
let log = slog::Logger::root(drain.fuse(), o!()); let log = slog::Logger::root(drain.fuse(), o!());
let mut default_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); let data_dir = match matches
default_dir.push(DEFAULT_DATA_DIR);
let data_dir = &matches
.value_of("datadir") .value_of("datadir")
.and_then(|v| Some(PathBuf::from(v))) .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 // create the directory if needed
match fs::create_dir_all(&data_dir) { match fs::create_dir_all(&data_dir) {

View File

@ -67,13 +67,24 @@ fn main() {
) )
.get_matches(); .get_matches();
let mut default_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); let data_dir = match matches
default_dir.push(DEFAULT_DATA_DIR);
let data_dir = &matches
.value_of("datadir") .value_of("datadir")
.and_then(|v| Some(PathBuf::from(v))) .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 // create the directory if needed
match fs::create_dir_all(&data_dir) { match fs::create_dir_all(&data_dir) {